blob: cc1e1307e5d3c11941c1ef7e2c6a499ce4067e6d [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
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100206static int gsm0408_authorize(struct gsm_subscriber_connection *conn, struct msgb *msg)
Holger Freytherd51524f2009-06-09 08:27:07 +0000207{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100208 if (authorize_subscriber(conn->loc_operation, conn->subscr)) {
Harald Welteee5ad162009-08-09 19:07:00 +0200209 int rc;
210
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100211 db_subscriber_alloc_tmsi(conn->subscr);
212 release_loc_updating_req(conn);
213 rc = gsm0408_loc_upd_acc(msg->lchan, conn->subscr->tmsi);
214 if (msg->lchan->ts->trx->bts->network->send_mm_info) {
Harald Welte648b6ce2009-12-14 09:00:24 +0100215 /* send MM INFO with network name */
216 rc = gsm48_tx_mm_info(msg->lchan);
217 }
Harald Welteb83d9382009-12-12 21:00:48 +0100218
Harald Welteee5ad162009-08-09 19:07:00 +0200219 /* call subscr_update after putting the loc_upd_acc
220 * in the transmit queue, since S_SUBSCR_ATTACHED might
221 * trigger further action like SMS delivery */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100222 subscr_update(conn->subscr, msg->trx->bts,
Harald Welteee5ad162009-08-09 19:07:00 +0200223 GSM_SUBSCRIBER_UPDATE_ATTACHED);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100224
Harald Welteb83d9382009-12-12 21:00:48 +0100225 /* try to close channel ASAP */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100226 lchan_auto_release(conn->lchan);
Harald Welteee5ad162009-08-09 19:07:00 +0200227 return rc;
Holger Freytherd51524f2009-06-09 08:27:07 +0000228 }
229
230 return 0;
231}
232
Holger Freyther7c19f742009-06-06 13:54:35 +0000233static int gsm0408_handle_lchan_signal(unsigned int subsys, unsigned int signal,
234 void *handler_data, void *signal_data)
235{
Harald Welte4bfdfe72009-06-10 23:11:52 +0800236 struct gsm_trans *trans, *temp;
237
Holger Freyther7c19f742009-06-06 13:54:35 +0000238 if (subsys != SS_LCHAN || signal != S_LCHAN_UNEXPECTED_RELEASE)
239 return 0;
240
241 /*
242 * Cancel any outstanding location updating request
243 * operation taking place on the lchan.
244 */
Harald Welte1a5c6bd2009-07-04 09:35:21 +0200245 struct gsm_lchan *lchan = (struct gsm_lchan *)signal_data;
Harald Weltec05677b2009-06-26 20:17:06 +0200246 if (!lchan)
247 return 0;
248
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100249 release_loc_updating_req(&lchan->conn);
Sylvain Munaut30a15382009-12-24 00:27:26 +0100250 release_security_operation(&lchan->conn);
Holger Freyther7c19f742009-06-06 13:54:35 +0000251
Harald Welte4bfdfe72009-06-10 23:11:52 +0800252 /* Free all transactions that are associated with the released lchan */
Harald Weltedcaf5652009-07-23 18:56:43 +0200253 /* FIXME: this is not neccessarily the right thing to do, we should
254 * only set trans->lchan to NULL and wait for another lchan to be
255 * established to the same MM entity (phone/subscriber) */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800256 llist_for_each_entry_safe(trans, temp, &lchan->ts->trx->bts->network->trans_list, entry) {
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +0100257 if (trans->conn && trans->conn->lchan == lchan)
Harald Weltedcaf5652009-07-23 18:56:43 +0200258 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +0800259 }
260
Holger Freyther7c19f742009-06-06 13:54:35 +0000261 return 0;
262}
263
Holger Freyther429e7762008-12-30 13:28:30 +0000264/* Chapter 9.2.14 : Send LOCATION UPDATING REJECT */
Harald Welte8470bf22008-12-25 23:28:35 +0000265int gsm0408_loc_upd_rej(struct gsm_lchan *lchan, u_int8_t cause)
Harald Welte52b1f982008-12-23 20:25:15 +0000266{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100267 struct gsm_subscriber_connection *conn;
Harald Welte24ff6ee2009-12-22 00:41:05 +0100268 struct gsm_bts *bts = lchan->ts->trx->bts;
Harald Welte8470bf22008-12-25 23:28:35 +0000269 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000270 struct gsm48_hdr *gh;
271
Harald Welte8470bf22008-12-25 23:28:35 +0000272 msg->lchan = lchan;
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100273 conn = &lchan->conn;
Harald Welte52b1f982008-12-23 20:25:15 +0000274
275 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
276 gh->proto_discr = GSM48_PDISC_MM;
Harald Welte10b487b2008-12-27 19:53:37 +0000277 gh->msg_type = GSM48_MT_MM_LOC_UPD_REJECT;
Harald Welte52b1f982008-12-23 20:25:15 +0000278 gh->data[0] = cause;
279
Harald Welte (local)198d5ad2009-12-26 22:15:28 +0100280 LOGP(DMM, LOGL_INFO, "Subscriber %s: LOCATION UPDATING REJECT "
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100281 "LAC=%u BTS=%u\n", conn->subscr ?
282 subscr_name(conn->subscr) : "unknown",
Harald Welte (local)198d5ad2009-12-26 22:15:28 +0100283 lchan->ts->trx->bts->location_area_code, lchan->ts->trx->bts->nr);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100284
Harald Welteffa55a42009-12-22 19:07:32 +0100285 counter_inc(bts->network->stats.loc_upd_resp.reject);
Harald Weltedb253af2008-12-30 17:56:55 +0000286
Harald Welte39e2ead2009-07-23 21:13:03 +0200287 return gsm48_sendmsg(msg, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000288}
289
290/* Chapter 9.2.13 : Send LOCATION UPDATE ACCEPT */
Harald Welte75a983f2008-12-27 21:34:06 +0000291int gsm0408_loc_upd_acc(struct gsm_lchan *lchan, u_int32_t tmsi)
Harald Welte52b1f982008-12-23 20:25:15 +0000292{
Harald Welte8470bf22008-12-25 23:28:35 +0000293 struct gsm_bts *bts = lchan->ts->trx->bts;
294 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000295 struct gsm48_hdr *gh;
296 struct gsm48_loc_area_id *lai;
297 u_int8_t *mid;
298
Harald Welte8470bf22008-12-25 23:28:35 +0000299 msg->lchan = lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000300
301 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
302 gh->proto_discr = GSM48_PDISC_MM;
303 gh->msg_type = GSM48_MT_MM_LOC_UPD_ACCEPT;
304
305 lai = (struct gsm48_loc_area_id *) msgb_put(msg, sizeof(*lai));
Harald Welteafedeab2010-03-04 10:55:40 +0100306 gsm48_generate_lai(lai, bts->network->country_code,
Harald Welte52b1f982008-12-23 20:25:15 +0000307 bts->network->network_code, bts->location_area_code);
308
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +0200309 mid = msgb_put(msg, GSM48_MID_TMSI_LEN);
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +0200310 gsm48_generate_mid_from_tmsi(mid, tmsi);
Harald Welte52b1f982008-12-23 20:25:15 +0000311
312 DEBUGP(DMM, "-> LOCATION UPDATE ACCEPT\n");
313
Harald Welteffa55a42009-12-22 19:07:32 +0100314 counter_inc(bts->network->stats.loc_upd_resp.accept);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100315
Harald Welteb83d9382009-12-12 21:00:48 +0100316 return gsm48_sendmsg(msg, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000317}
318
Harald Weltebf5e8df2009-02-03 12:59:45 +0000319/* Transmit Chapter 9.2.10 Identity Request */
Harald Welte231ad4f2008-12-27 11:15:38 +0000320static int mm_tx_identity_req(struct gsm_lchan *lchan, u_int8_t id_type)
321{
322 struct msgb *msg = gsm48_msgb_alloc();
323 struct gsm48_hdr *gh;
Harald Weltefc977a82008-12-27 10:19:37 +0000324
Harald Welte231ad4f2008-12-27 11:15:38 +0000325 msg->lchan = lchan;
326
327 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
328 gh->proto_discr = GSM48_PDISC_MM;
329 gh->msg_type = GSM48_MT_MM_ID_REQ;
330 gh->data[0] = id_type;
331
Harald Welte39e2ead2009-07-23 21:13:03 +0200332 return gsm48_sendmsg(msg, NULL);
Harald Welte231ad4f2008-12-27 11:15:38 +0000333}
334
Harald Welte231ad4f2008-12-27 11:15:38 +0000335
Harald Weltebf5e8df2009-02-03 12:59:45 +0000336/* Parse Chapter 9.2.11 Identity Response */
Harald Welte231ad4f2008-12-27 11:15:38 +0000337static int mm_rx_id_resp(struct msgb *msg)
338{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100339 struct gsm_subscriber_connection *conn;
Harald Welte231ad4f2008-12-27 11:15:38 +0000340 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte75a983f2008-12-27 21:34:06 +0000341 struct gsm_lchan *lchan = msg->lchan;
Harald Welte9176bd42009-07-23 18:46:00 +0200342 struct gsm_bts *bts = lchan->ts->trx->bts;
343 struct gsm_network *net = bts->network;
Harald Welte231ad4f2008-12-27 11:15:38 +0000344 u_int8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200345 char mi_string[GSM48_MI_SIZE];
Harald Welte231ad4f2008-12-27 11:15:38 +0000346
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200347 gsm48_mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
Harald Welte61253062008-12-27 11:25:50 +0000348 DEBUGP(DMM, "IDENTITY RESPONSE: mi_type=0x%02x MI(%s)\n",
Harald Welte231ad4f2008-12-27 11:15:38 +0000349 mi_type, mi_string);
350
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100351 conn = &lchan->conn;
352
Harald Welte7659de12009-12-13 12:39:18 +0100353 dispatch_signal(SS_SUBSCR, S_SUBSCR_IDENTITY, gh->data);
354
Harald Welte75a983f2008-12-27 21:34:06 +0000355 switch (mi_type) {
356 case GSM_MI_TYPE_IMSI:
Jan Luebbe370b41d2009-08-12 10:19:34 +0200357 /* look up subscriber based on IMSI, create if not found */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100358 if (!conn->subscr) {
359 conn->subscr = subscr_get_by_imsi(net, mi_string);
360 if (!conn->subscr)
361 conn->subscr = db_create_subscriber(net, mi_string);
Jan Luebbeb0dfc312009-08-12 10:12:52 +0200362 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100363 if (conn->loc_operation)
364 conn->loc_operation->waiting_for_imsi = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000365 break;
366 case GSM_MI_TYPE_IMEI:
Harald Welte255539c2008-12-28 02:26:27 +0000367 case GSM_MI_TYPE_IMEISV:
Harald Welte75a983f2008-12-27 21:34:06 +0000368 /* update subscribe <-> IMEI mapping */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100369 if (conn->subscr) {
370 db_subscriber_assoc_imei(conn->subscr, mi_string);
371 db_sync_equipment(&conn->subscr->equipment);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200372 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100373 if (conn->loc_operation)
374 conn->loc_operation->waiting_for_imei = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000375 break;
376 }
Holger Freyther73487a22008-12-31 18:53:57 +0000377
378 /* Check if we can let the mobile station enter */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100379 return gsm0408_authorize(conn, msg);
Harald Welte231ad4f2008-12-27 11:15:38 +0000380}
381
Harald Welte255539c2008-12-28 02:26:27 +0000382
383static void loc_upd_rej_cb(void *data)
384{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100385 struct gsm_subscriber_connection *conn = data;
386 struct gsm_lchan *lchan = conn->lchan;
Harald Welte1085c092009-11-18 20:33:19 +0100387 struct gsm_bts *bts = lchan->ts->trx->bts;
Harald Welte255539c2008-12-28 02:26:27 +0000388
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100389 release_loc_updating_req(conn);
Harald Welte1085c092009-11-18 20:33:19 +0100390 gsm0408_loc_upd_rej(lchan, bts->network->reject_cause);
Holger Freyther67b4b9a2009-01-01 03:46:11 +0000391 lchan_auto_release(lchan);
Harald Welte255539c2008-12-28 02:26:27 +0000392}
393
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100394static void schedule_reject(struct gsm_subscriber_connection *conn)
Holger Freytherb7193e42008-12-29 17:44:08 +0000395{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100396 conn->loc_operation->updating_timer.cb = loc_upd_rej_cb;
397 conn->loc_operation->updating_timer.data = conn;
398 bsc_schedule_timer(&conn->loc_operation->updating_timer, 5, 0);
Holger Freytherb7193e42008-12-29 17:44:08 +0000399}
400
Harald Welte2a139372009-02-22 21:14:55 +0000401static const char *lupd_name(u_int8_t type)
402{
403 switch (type) {
404 case GSM48_LUPD_NORMAL:
405 return "NORMAL";
406 case GSM48_LUPD_PERIODIC:
407 return "PEROIDOC";
408 case GSM48_LUPD_IMSI_ATT:
409 return "IMSI ATTACH";
410 default:
411 return "UNKNOWN";
412 }
413}
414
Harald Weltebf5e8df2009-02-03 12:59:45 +0000415/* Chapter 9.2.15: Receive Location Updating Request */
Harald Welte231ad4f2008-12-27 11:15:38 +0000416static int mm_rx_loc_upd_req(struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000417{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100418 struct gsm_subscriber_connection *conn;
Harald Welte8470bf22008-12-25 23:28:35 +0000419 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000420 struct gsm48_loc_upd_req *lu;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800421 struct gsm_subscriber *subscr = NULL;
Harald Welte255539c2008-12-28 02:26:27 +0000422 struct gsm_lchan *lchan = msg->lchan;
Harald Welte9176bd42009-07-23 18:46:00 +0200423 struct gsm_bts *bts = lchan->ts->trx->bts;
Harald Welte8470bf22008-12-25 23:28:35 +0000424 u_int8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200425 char mi_string[GSM48_MI_SIZE];
Harald Welte231ad4f2008-12-27 11:15:38 +0000426 int rc;
Harald Welte52b1f982008-12-23 20:25:15 +0000427
Harald Welte8470bf22008-12-25 23:28:35 +0000428 lu = (struct gsm48_loc_upd_req *) gh->data;
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100429 conn = &lchan->conn;
Harald Welte8470bf22008-12-25 23:28:35 +0000430
431 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
Harald Welte52b1f982008-12-23 20:25:15 +0000432
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200433 gsm48_mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
Harald Weltefc977a82008-12-27 10:19:37 +0000434
Harald Weltea0368542009-06-27 02:58:43 +0200435 DEBUGPC(DMM, "mi_type=0x%02x MI(%s) type=%s ", mi_type, mi_string,
Harald Welte2a139372009-02-22 21:14:55 +0000436 lupd_name(lu->type));
Holger Freyther73487a22008-12-31 18:53:57 +0000437
Harald Welte7659de12009-12-13 12:39:18 +0100438 dispatch_signal(SS_SUBSCR, S_SUBSCR_IDENTITY, &lu->mi_len);
439
Harald Welte24ff6ee2009-12-22 00:41:05 +0100440 switch (lu->type) {
441 case GSM48_LUPD_NORMAL:
Harald Welteffa55a42009-12-22 19:07:32 +0100442 counter_inc(bts->network->stats.loc_upd_type.normal);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100443 break;
444 case GSM48_LUPD_IMSI_ATT:
Harald Welteffa55a42009-12-22 19:07:32 +0100445 counter_inc(bts->network->stats.loc_upd_type.attach);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100446 break;
447 case GSM48_LUPD_PERIODIC:
Harald Welteffa55a42009-12-22 19:07:32 +0100448 counter_inc(bts->network->stats.loc_upd_type.periodic);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100449 break;
450 }
451
Holger Freythereaf04692009-06-06 13:54:44 +0000452 /*
453 * Pseudo Spoof detection: Just drop a second/concurrent
454 * location updating request.
455 */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100456 if (conn->loc_operation) {
Harald Weltea0368542009-06-27 02:58:43 +0200457 DEBUGPC(DMM, "ignoring request due an existing one: %p.\n",
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100458 conn->loc_operation);
Holger Freythereaf04692009-06-06 13:54:44 +0000459 gsm0408_loc_upd_rej(lchan, GSM48_REJECT_PROTOCOL_ERROR);
460 return 0;
461 }
462
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100463 allocate_loc_updating_req(&lchan->conn);
Holger Freyther73487a22008-12-31 18:53:57 +0000464
Sylvain Munaut2030a2a2010-06-10 13:36:59 +0200465 conn->loc_operation->key_seq = lu->key_seq;
466
Harald Welte52b1f982008-12-23 20:25:15 +0000467 switch (mi_type) {
468 case GSM_MI_TYPE_IMSI:
Harald Weltea0368542009-06-27 02:58:43 +0200469 DEBUGPC(DMM, "\n");
Harald Welte231ad4f2008-12-27 11:15:38 +0000470 /* we always want the IMEI, too */
Harald Welte015b9ad2009-02-28 18:22:03 +0000471 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMEI);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100472 conn->loc_operation->waiting_for_imei = 1;
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000473
Jan Luebbe370b41d2009-08-12 10:19:34 +0200474 /* look up subscriber based on IMSI, create if not found */
475 subscr = subscr_get_by_imsi(bts->network, mi_string);
476 if (!subscr) {
477 subscr = db_create_subscriber(bts->network, mi_string);
478 }
Harald Welte4b634542008-12-27 01:55:51 +0000479 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000480 case GSM_MI_TYPE_TMSI:
Harald Weltea0368542009-06-27 02:58:43 +0200481 DEBUGPC(DMM, "\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000482 /* look up the subscriber based on TMSI, request IMSI if it fails */
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200483 subscr = subscr_get_by_tmsi(bts->network,
484 tmsi_from_string(mi_string));
Harald Welte52b1f982008-12-23 20:25:15 +0000485 if (!subscr) {
Harald Welte231ad4f2008-12-27 11:15:38 +0000486 /* send IDENTITY REQUEST message to get IMSI */
Harald Welte255539c2008-12-28 02:26:27 +0000487 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMSI);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100488 conn->loc_operation->waiting_for_imsi = 1;
Harald Welte52b1f982008-12-23 20:25:15 +0000489 }
Harald Weltef5f512c2010-06-07 17:56:32 +0200490 /* we always want the IMEI, too */
491 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMEI);
492 conn->loc_operation->waiting_for_imei = 1;
Harald Welte52b1f982008-12-23 20:25:15 +0000493 break;
494 case GSM_MI_TYPE_IMEI:
495 case GSM_MI_TYPE_IMEISV:
496 /* no sim card... FIXME: what to do ? */
Harald Weltea0368542009-06-27 02:58:43 +0200497 DEBUGPC(DMM, "unimplemented mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000498 break;
499 default:
Harald Weltea0368542009-06-27 02:58:43 +0200500 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000501 break;
502 }
503
Harald Welte24516ea2009-07-04 10:18:00 +0200504 /* schedule the reject timer */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100505 schedule_reject(conn);
Harald Welte24516ea2009-07-04 10:18:00 +0200506
Harald Welte4bfdfe72009-06-10 23:11:52 +0800507 if (!subscr) {
Harald Weltea0368542009-06-27 02:58:43 +0200508 DEBUGPC(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte4bfdfe72009-06-10 23:11:52 +0800509 /* FIXME: request id? close channel? */
510 return -EINVAL;
511 }
512
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100513 conn->subscr = subscr;
514 conn->subscr->equipment.classmark1 = lu->classmark1;
Harald Welte255539c2008-12-28 02:26:27 +0000515
Harald Welte24516ea2009-07-04 10:18:00 +0200516 /* check if we can let the subscriber into our network immediately
517 * or if we need to wait for identity responses. */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100518 return gsm0408_authorize(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000519}
520
Harald Welte4bfdfe72009-06-10 23:11:52 +0800521#if 0
522static u_int8_t to_bcd8(u_int8_t val)
523{
524 return ((val / 10) << 4) | (val % 10);
525}
526#endif
527
Harald Weltedb253af2008-12-30 17:56:55 +0000528/* Section 9.2.15a */
529int gsm48_tx_mm_info(struct gsm_lchan *lchan)
530{
531 struct msgb *msg = gsm48_msgb_alloc();
532 struct gsm48_hdr *gh;
533 struct gsm_network *net = lchan->ts->trx->bts->network;
Harald Weltedb253af2008-12-30 17:56:55 +0000534 u_int8_t *ptr8;
Daniel Willmanneea93372009-08-13 03:42:07 +0200535 int name_len, name_pad;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800536#if 0
537 time_t cur_t;
538 struct tm* cur_time;
539 int tz15min;
540#endif
Harald Weltedb253af2008-12-30 17:56:55 +0000541
542 msg->lchan = lchan;
543
544 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
545 gh->proto_discr = GSM48_PDISC_MM;
546 gh->msg_type = GSM48_MT_MM_INFO;
547
548 if (net->name_long) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200549#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000550 name_len = strlen(net->name_long);
551 /* 10.5.3.5a */
552 ptr8 = msgb_put(msg, 3);
553 ptr8[0] = GSM48_IE_NAME_LONG;
554 ptr8[1] = name_len*2 +1;
555 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
556
557 ptr16 = (u_int16_t *) msgb_put(msg, name_len*2);
558 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000559 ptr16[i] = htons(net->name_long[i]);
Harald Weltedb253af2008-12-30 17:56:55 +0000560
561 /* FIXME: Use Cell Broadcast, not UCS-2, since
562 * UCS-2 is only supported by later revisions of the spec */
Daniel Willmanneea93372009-08-13 03:42:07 +0200563#endif
564 name_len = (strlen(net->name_long)*7)/8;
565 name_pad = (8 - strlen(net->name_long)*7)%8;
566 if (name_pad > 0)
567 name_len++;
568 /* 10.5.3.5a */
569 ptr8 = msgb_put(msg, 3);
570 ptr8[0] = GSM48_IE_NAME_LONG;
571 ptr8[1] = name_len +1;
572 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
573
574 ptr8 = msgb_put(msg, name_len);
575 gsm_7bit_encode(ptr8, net->name_long);
576
Harald Weltedb253af2008-12-30 17:56:55 +0000577 }
578
579 if (net->name_short) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200580#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000581 name_len = strlen(net->name_short);
582 /* 10.5.3.5a */
583 ptr8 = (u_int8_t *) msgb_put(msg, 3);
Harald Welte7543eb72009-07-19 17:51:36 +0200584 ptr8[0] = GSM48_IE_NAME_SHORT;
Harald Weltedb253af2008-12-30 17:56:55 +0000585 ptr8[1] = name_len*2 + 1;
586 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
587
Harald Weltee872cb12009-01-01 00:33:37 +0000588 ptr16 = (u_int16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000589 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000590 ptr16[i] = htons(net->name_short[i]);
Daniel Willmanneea93372009-08-13 03:42:07 +0200591#endif
592 name_len = (strlen(net->name_short)*7)/8;
593 name_pad = (8 - strlen(net->name_short)*7)%8;
594 if (name_pad > 0)
595 name_len++;
596 /* 10.5.3.5a */
597 ptr8 = (u_int8_t *) msgb_put(msg, 3);
598 ptr8[0] = GSM48_IE_NAME_SHORT;
599 ptr8[1] = name_len +1;
600 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
601
602 ptr8 = msgb_put(msg, name_len);
603 gsm_7bit_encode(ptr8, net->name_short);
604
Harald Weltedb253af2008-12-30 17:56:55 +0000605 }
606
607#if 0
608 /* Section 10.5.3.9 */
609 cur_t = time(NULL);
Harald Welte4bfdfe72009-06-10 23:11:52 +0800610 cur_time = gmtime(&cur_t);
Harald Weltedb253af2008-12-30 17:56:55 +0000611 ptr8 = msgb_put(msg, 8);
612 ptr8[0] = GSM48_IE_NET_TIME_TZ;
613 ptr8[1] = to_bcd8(cur_time->tm_year % 100);
614 ptr8[2] = to_bcd8(cur_time->tm_mon);
615 ptr8[3] = to_bcd8(cur_time->tm_mday);
616 ptr8[4] = to_bcd8(cur_time->tm_hour);
617 ptr8[5] = to_bcd8(cur_time->tm_min);
618 ptr8[6] = to_bcd8(cur_time->tm_sec);
619 /* 02.42: coded as BCD encoded signed value in units of 15 minutes */
620 tz15min = (cur_time->tm_gmtoff)/(60*15);
Harald Welte4bfdfe72009-06-10 23:11:52 +0800621 ptr8[7] = to_bcd8(tz15min);
Harald Weltedb253af2008-12-30 17:56:55 +0000622 if (tz15min < 0)
Harald Welte4bfdfe72009-06-10 23:11:52 +0800623 ptr8[7] |= 0x80;
Harald Weltedb253af2008-12-30 17:56:55 +0000624#endif
625
Daniel Willmanneea93372009-08-13 03:42:07 +0200626 DEBUGP(DMM, "-> MM INFO\n");
627
Harald Welte39e2ead2009-07-23 21:13:03 +0200628 return gsm48_sendmsg(msg, NULL);
Harald Weltedb253af2008-12-30 17:56:55 +0000629}
630
Harald Welte7984d5c2009-08-12 22:56:50 +0200631/* Section 9.2.2 */
Sylvain Munautbd55a6d2009-12-24 00:23:46 +0100632int gsm48_tx_mm_auth_req(struct gsm_lchan *lchan, u_int8_t *rand, int key_seq)
Harald Welte7984d5c2009-08-12 22:56:50 +0200633{
634 struct msgb *msg = gsm48_msgb_alloc();
635 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Sylvain Munaut849f5542009-09-27 11:10:17 +0200636 struct gsm48_auth_req *ar = (struct gsm48_auth_req *) msgb_put(msg, sizeof(*ar));
Harald Welte7984d5c2009-08-12 22:56:50 +0200637
Sylvain Munaut8608e7c2009-12-24 00:24:29 +0100638 DEBUGP(DMM, "-> AUTH REQ (rand = %s)\n", hexdump(rand, 16));
Harald Welte7984d5c2009-08-12 22:56:50 +0200639
640 msg->lchan = lchan;
641 gh->proto_discr = GSM48_PDISC_MM;
642 gh->msg_type = GSM48_MT_MM_AUTH_REQ;
643
Sylvain Munautbd55a6d2009-12-24 00:23:46 +0100644 ar->key_seq = key_seq;
Sylvain Munaut849f5542009-09-27 11:10:17 +0200645
Harald Welte7984d5c2009-08-12 22:56:50 +0200646 /* 16 bytes RAND parameters */
Harald Welte7984d5c2009-08-12 22:56:50 +0200647 if (rand)
Sylvain Munaut849f5542009-09-27 11:10:17 +0200648 memcpy(ar->rand, rand, 16);
Harald Welte7984d5c2009-08-12 22:56:50 +0200649
650 return gsm48_sendmsg(msg, NULL);
651}
652
653/* Section 9.2.1 */
654int gsm48_tx_mm_auth_rej(struct gsm_lchan *lchan)
655{
656 DEBUGP(DMM, "-> AUTH REJECT\n");
657 return gsm48_tx_simple(lchan, GSM48_PDISC_MM, GSM48_MT_MM_AUTH_REJ);
658}
659
Harald Welte4b634542008-12-27 01:55:51 +0000660static int gsm48_tx_mm_serv_ack(struct gsm_lchan *lchan)
661{
Harald Welte4b634542008-12-27 01:55:51 +0000662 DEBUGP(DMM, "-> CM SERVICE ACK\n");
Harald Welte65e74cc2008-12-29 01:55:35 +0000663 return gsm48_tx_simple(lchan, GSM48_PDISC_MM, GSM48_MT_MM_CM_SERV_ACC);
Harald Welte4b634542008-12-27 01:55:51 +0000664}
Harald Welteba4cf162009-01-10 01:49:35 +0000665
666/* 9.2.6 CM service reject */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100667static int gsm48_tx_mm_serv_rej(struct gsm_subscriber_connection *conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000668 enum gsm48_reject_value value)
669{
670 struct msgb *msg = gsm48_msgb_alloc();
671 struct gsm48_hdr *gh;
672
673 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
674
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100675 msg->lchan = conn->lchan;
676 use_subscr_con(conn);
Harald Welteba4cf162009-01-10 01:49:35 +0000677
678 gh->proto_discr = GSM48_PDISC_MM;
679 gh->msg_type = GSM48_MT_MM_CM_SERV_REJ;
680 gh->data[0] = value;
681 DEBUGP(DMM, "-> CM SERVICE Reject cause: %d\n", value);
682
Harald Welte39e2ead2009-07-23 21:13:03 +0200683 return gsm48_sendmsg(msg, NULL);
Harald Welteba4cf162009-01-10 01:49:35 +0000684}
685
Harald Welte4ed0e922009-01-10 03:17:30 +0000686/*
687 * Handle CM Service Requests
688 * a) Verify that the packet is long enough to contain the information
689 * we require otherwsie reject with INCORRECT_MESSAGE
690 * b) Try to parse the TMSI. If we do not have one reject
691 * c) Check that we know the subscriber with the TMSI otherwise reject
692 * with a HLR cause
693 * d) Set the subscriber on the gsm_lchan and accept
694 */
Harald Welte4b634542008-12-27 01:55:51 +0000695static int gsm48_rx_mm_serv_req(struct msgb *msg)
696{
Harald Welteba4cf162009-01-10 01:49:35 +0000697 u_int8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200698 char mi_string[GSM48_MI_SIZE];
Harald Welte4b634542008-12-27 01:55:51 +0000699
Harald Welte9176bd42009-07-23 18:46:00 +0200700 struct gsm_bts *bts = msg->lchan->ts->trx->bts;
Harald Welteba4cf162009-01-10 01:49:35 +0000701 struct gsm_subscriber *subscr;
702 struct gsm48_hdr *gh = msgb_l3(msg);
703 struct gsm48_service_request *req =
704 (struct gsm48_service_request *)gh->data;
Holger Hans Peter Freyther5d658062010-05-14 08:02:08 +0800705 /* unfortunately in Phase1 the classmark2 length is variable */
Harald Weltec9e02182009-05-01 19:07:53 +0000706 u_int8_t classmark2_len = gh->data[1];
707 u_int8_t *classmark2 = gh->data+2;
708 u_int8_t mi_len = *(classmark2 + classmark2_len);
709 u_int8_t *mi = (classmark2 + classmark2_len + 1);
Harald Welteba4cf162009-01-10 01:49:35 +0000710
Harald Weltec9e02182009-05-01 19:07:53 +0000711 DEBUGP(DMM, "<- CM SERVICE REQUEST ");
Harald Welteba4cf162009-01-10 01:49:35 +0000712 if (msg->data_len < sizeof(struct gsm48_service_request*)) {
Harald Weltec9e02182009-05-01 19:07:53 +0000713 DEBUGPC(DMM, "wrong sized message\n");
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100714 return gsm48_tx_mm_serv_rej(&msg->lchan->conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000715 GSM48_REJECT_INCORRECT_MESSAGE);
716 }
717
718 if (msg->data_len < req->mi_len + 6) {
Harald Weltec9e02182009-05-01 19:07:53 +0000719 DEBUGPC(DMM, "does not fit in packet\n");
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100720 return gsm48_tx_mm_serv_rej(&msg->lchan->conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000721 GSM48_REJECT_INCORRECT_MESSAGE);
722 }
723
Harald Weltec9e02182009-05-01 19:07:53 +0000724 mi_type = mi[0] & GSM_MI_TYPE_MASK;
Harald Welteba4cf162009-01-10 01:49:35 +0000725 if (mi_type != GSM_MI_TYPE_TMSI) {
Harald Weltec9e02182009-05-01 19:07:53 +0000726 DEBUGPC(DMM, "mi_type is not TMSI: %d\n", mi_type);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100727 return gsm48_tx_mm_serv_rej(&msg->lchan->conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000728 GSM48_REJECT_INCORRECT_MESSAGE);
729 }
730
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200731 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
Harald Weltec9e02182009-05-01 19:07:53 +0000732 DEBUGPC(DMM, "serv_type=0x%02x mi_type=0x%02x M(%s)\n",
Harald Welte4ed0e922009-01-10 03:17:30 +0000733 req->cm_service_type, mi_type, mi_string);
Harald Weltebcae43f2008-12-27 21:45:37 +0000734
Harald Welte7659de12009-12-13 12:39:18 +0100735 dispatch_signal(SS_SUBSCR, S_SUBSCR_IDENTITY, (classmark2 + classmark2_len));
736
Harald Welte3ac7f102009-08-10 10:12:45 +0200737 if (is_siemens_bts(bts))
738 send_siemens_mrpci(msg->lchan, classmark2-1);
739
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200740 subscr = subscr_get_by_tmsi(bts->network,
741 tmsi_from_string(mi_string));
Holger Freythereb443982009-06-04 13:58:42 +0000742
Harald Welte2a139372009-02-22 21:14:55 +0000743 /* FIXME: if we don't know the TMSI, inquire abit IMSI and allocate new TMSI */
Harald Welte4ed0e922009-01-10 03:17:30 +0000744 if (!subscr)
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100745 return gsm48_tx_mm_serv_rej(&msg->lchan->conn,
Harald Welte4ed0e922009-01-10 03:17:30 +0000746 GSM48_REJECT_IMSI_UNKNOWN_IN_HLR);
747
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100748 if (!msg->lchan->conn.subscr)
749 msg->lchan->conn.subscr = subscr;
750 else if (msg->lchan->conn.subscr == subscr)
Sylvain Munautea3f6742009-12-18 18:28:10 +0100751 subscr_put(subscr); /* lchan already has a ref, don't need another one */
752 else {
Harald Welte9bb7c702009-01-10 03:21:41 +0000753 DEBUGP(DMM, "<- CM Channel already owned by someone else?\n");
754 subscr_put(subscr);
755 }
756
Harald Weltec2e302d2009-07-05 14:08:13 +0200757 subscr->equipment.classmark2_len = classmark2_len;
758 memcpy(subscr->equipment.classmark2, classmark2, classmark2_len);
759 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +0000760
Harald Welte4b634542008-12-27 01:55:51 +0000761 return gsm48_tx_mm_serv_ack(msg->lchan);
762}
763
Harald Welte2a139372009-02-22 21:14:55 +0000764static int gsm48_rx_mm_imsi_detach_ind(struct msgb *msg)
765{
Harald Welte9176bd42009-07-23 18:46:00 +0200766 struct gsm_bts *bts = msg->lchan->ts->trx->bts;
Harald Welte2a139372009-02-22 21:14:55 +0000767 struct gsm48_hdr *gh = msgb_l3(msg);
768 struct gsm48_imsi_detach_ind *idi =
769 (struct gsm48_imsi_detach_ind *) gh->data;
770 u_int8_t mi_type = idi->mi[0] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200771 char mi_string[GSM48_MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +0800772 struct gsm_subscriber *subscr = NULL;
Harald Welte2a139372009-02-22 21:14:55 +0000773
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200774 gsm48_mi_to_string(mi_string, sizeof(mi_string), idi->mi, idi->mi_len);
Harald Welte2a139372009-02-22 21:14:55 +0000775 DEBUGP(DMM, "IMSI DETACH INDICATION: mi_type=0x%02x MI(%s): ",
776 mi_type, mi_string);
777
Harald Welteffa55a42009-12-22 19:07:32 +0100778 counter_inc(bts->network->stats.loc_upd_type.detach);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100779
Harald Welte2a139372009-02-22 21:14:55 +0000780 switch (mi_type) {
781 case GSM_MI_TYPE_TMSI:
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200782 subscr = subscr_get_by_tmsi(bts->network,
783 tmsi_from_string(mi_string));
Harald Welte2a139372009-02-22 21:14:55 +0000784 break;
785 case GSM_MI_TYPE_IMSI:
Harald Welte9176bd42009-07-23 18:46:00 +0200786 subscr = subscr_get_by_imsi(bts->network, mi_string);
Harald Welte2a139372009-02-22 21:14:55 +0000787 break;
788 case GSM_MI_TYPE_IMEI:
789 case GSM_MI_TYPE_IMEISV:
790 /* no sim card... FIXME: what to do ? */
Holger Freyther79f4ae62009-06-02 03:25:04 +0000791 DEBUGPC(DMM, "unimplemented mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +0000792 break;
793 default:
Holger Freyther79f4ae62009-06-02 03:25:04 +0000794 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +0000795 break;
796 }
797
Holger Freyther4a49e772009-04-12 05:37:29 +0000798 if (subscr) {
799 subscr_update(subscr, msg->trx->bts,
800 GSM_SUBSCRIBER_UPDATE_DETACHED);
Harald Welte2e6d4682009-12-24 14:50:24 +0100801 DEBUGP(DMM, "Subscriber: %s\n", subscr_name(subscr));
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200802
803 subscr->equipment.classmark1 = idi->classmark1;
804 db_sync_equipment(&subscr->equipment);
805
Holger Freytherc21cfbc2009-06-02 02:54:57 +0000806 subscr_put(subscr);
Holger Freyther4a49e772009-04-12 05:37:29 +0000807 } else
Harald Welte2a139372009-02-22 21:14:55 +0000808 DEBUGP(DMM, "Unknown Subscriber ?!?\n");
809
Harald Welte31981a02009-12-20 09:58:40 +0100810 /* FIXME: iterate over all transactions and release them,
811 * imagine an IMSI DETACH happening during an active call! */
812
Harald Welteb83d9382009-12-12 21:00:48 +0100813 /* subscriber is detached: should we release lchan? */
Harald Weltef7c28b02009-12-21 13:30:17 +0100814 lchan_auto_release(msg->lchan);
Harald Welteb83d9382009-12-12 21:00:48 +0100815
Harald Welte2a139372009-02-22 21:14:55 +0000816 return 0;
817}
818
Harald Welted2a7f5a2009-06-05 20:08:20 +0000819static int gsm48_rx_mm_status(struct msgb *msg)
820{
821 struct gsm48_hdr *gh = msgb_l3(msg);
822
823 DEBUGP(DMM, "MM STATUS (reject cause 0x%02x)\n", gh->data[0]);
824
825 return 0;
826}
827
Sylvain Munaut30a15382009-12-24 00:27:26 +0100828/* Chapter 9.2.3: Authentication Response */
829static int gsm48_rx_mm_auth_resp(struct msgb *msg)
830{
831 struct gsm48_hdr *gh = msgb_l3(msg);
832 struct gsm48_auth_resp *ar = (struct gsm48_auth_resp*) gh->data;
833 struct gsm_lchan *lchan = msg->lchan;
834 struct gsm_subscriber_connection *conn = &msg->lchan->conn;
835 struct gsm_network *net = lchan->ts->trx->bts->network;
836
837 DEBUGP(DMM, "MM AUTHENTICATION RESPONSE (sres = %s)\n",
838 hexdump(ar->sres, 4));
839
840 /* Safety check */
841 if (!conn->sec_operation) {
842 DEBUGP(DMM, "No authentication/cipher operation in progress !!!\n");
843 return -EIO;
844 }
845
846 /* Validate SRES */
847 if (memcmp(conn->sec_operation->atuple.sres, ar->sres,4)) {
848 gsm_cbfn *cb = conn->sec_operation->cb;
849 if (cb)
850 cb(GSM_HOOK_RR_SECURITY, GSM_SECURITY_AUTH_FAILED,
851 NULL, lchan, conn->sec_operation->cb_data);
852
853 release_security_operation(conn);
854 return gsm48_tx_mm_auth_rej(lchan);
855 }
856
857 /* Start ciphering */
858 lchan->encr.alg_id = RSL_ENC_ALG_A5(net->a5_encryption);
859 lchan->encr.key_len = 8;
860 memcpy(msg->lchan->encr.key, conn->sec_operation->atuple.kc, 8);
861
862 return gsm48_send_rr_ciph_mode(msg->lchan, 0);
863}
864
Harald Weltebf5e8df2009-02-03 12:59:45 +0000865/* Receive a GSM 04.08 Mobility Management (MM) message */
Harald Welte52b1f982008-12-23 20:25:15 +0000866static int gsm0408_rcv_mm(struct msgb *msg)
867{
868 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +0800869 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000870
871 switch (gh->msg_type & 0xbf) {
872 case GSM48_MT_MM_LOC_UPD_REQUEST:
Harald Weltea0368542009-06-27 02:58:43 +0200873 DEBUGP(DMM, "LOCATION UPDATING REQUEST: ");
Harald Welte231ad4f2008-12-27 11:15:38 +0000874 rc = mm_rx_loc_upd_req(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000875 break;
876 case GSM48_MT_MM_ID_RESP:
Harald Welte231ad4f2008-12-27 11:15:38 +0000877 rc = mm_rx_id_resp(msg);
878 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000879 case GSM48_MT_MM_CM_SERV_REQ:
Harald Welte4b634542008-12-27 01:55:51 +0000880 rc = gsm48_rx_mm_serv_req(msg);
881 break;
Harald Welte231ad4f2008-12-27 11:15:38 +0000882 case GSM48_MT_MM_STATUS:
Harald Welted2a7f5a2009-06-05 20:08:20 +0000883 rc = gsm48_rx_mm_status(msg);
Harald Welte231ad4f2008-12-27 11:15:38 +0000884 break;
Harald Welte231ad4f2008-12-27 11:15:38 +0000885 case GSM48_MT_MM_TMSI_REALL_COMPL:
Harald Welte69b2af22009-01-06 19:47:00 +0000886 DEBUGP(DMM, "TMSI Reallocation Completed. Subscriber: %s\n",
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100887 msg->lchan->conn.subscr ?
888 subscr_name(msg->lchan->conn.subscr) :
Harald Welte69b2af22009-01-06 19:47:00 +0000889 "unknown subscriber");
890 break;
Harald Welte231ad4f2008-12-27 11:15:38 +0000891 case GSM48_MT_MM_IMSI_DETACH_IND:
Harald Welte2a139372009-02-22 21:14:55 +0000892 rc = gsm48_rx_mm_imsi_detach_ind(msg);
893 break;
894 case GSM48_MT_MM_CM_REEST_REQ:
895 DEBUGP(DMM, "CM REESTABLISH REQUEST: Not implemented\n");
896 break;
897 case GSM48_MT_MM_AUTH_RESP:
Sylvain Munaut30a15382009-12-24 00:27:26 +0100898 rc = gsm48_rx_mm_auth_resp(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000899 break;
900 default:
Harald Welte5d24ba12009-12-24 12:13:17 +0100901 LOGP(DMM, LOGL_NOTICE, "Unknown GSM 04.08 MM msg type 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +0000902 gh->msg_type);
903 break;
904 }
905
906 return rc;
907}
Harald Weltebf5e8df2009-02-03 12:59:45 +0000908
Harald Welte2d35ae62009-02-06 12:02:13 +0000909/* Receive a PAGING RESPONSE message from the MS */
Holger Hans Peter Freytherca0fcbe2009-10-23 13:48:54 +0200910static int gsm48_rx_rr_pag_resp(struct msgb *msg)
Harald Welte2d35ae62009-02-06 12:02:13 +0000911{
Harald Welte9176bd42009-07-23 18:46:00 +0200912 struct gsm_bts *bts = msg->lchan->ts->trx->bts;
Harald Welte2d35ae62009-02-06 12:02:13 +0000913 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +0800914 struct gsm48_pag_resp *resp;
Harald Welte61548982009-02-22 21:26:29 +0000915 u_int8_t *classmark2_lv = gh->data + 1;
Holger Hans Peter Freyther3ee5d3e2009-08-21 05:18:21 +0200916 u_int8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200917 char mi_string[GSM48_MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +0800918 struct gsm_subscriber *subscr = NULL;
Harald Welte2d35ae62009-02-06 12:02:13 +0000919 int rc = 0;
920
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +0800921 resp = (struct gsm48_pag_resp *) &gh->data[0];
922 gsm48_paging_extract_mi(resp, msgb_l3len(msg) - sizeof(*gh),
923 mi_string, &mi_type);
Harald Welte2d35ae62009-02-06 12:02:13 +0000924 DEBUGP(DRR, "PAGING RESPONSE: mi_type=0x%02x MI(%s)\n",
925 mi_type, mi_string);
Harald Welte3ac7f102009-08-10 10:12:45 +0200926
Harald Weltefe18d8f2009-02-22 21:14:24 +0000927 switch (mi_type) {
928 case GSM_MI_TYPE_TMSI:
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200929 subscr = subscr_get_by_tmsi(bts->network,
930 tmsi_from_string(mi_string));
Harald Weltefe18d8f2009-02-22 21:14:24 +0000931 break;
932 case GSM_MI_TYPE_IMSI:
Harald Welte9176bd42009-07-23 18:46:00 +0200933 subscr = subscr_get_by_imsi(bts->network, mi_string);
Harald Weltefe18d8f2009-02-22 21:14:24 +0000934 break;
935 }
Harald Welte2d35ae62009-02-06 12:02:13 +0000936
937 if (!subscr) {
938 DEBUGP(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte09e38af2009-02-16 22:52:23 +0000939 /* FIXME: request id? close channel? */
Harald Welte2d35ae62009-02-06 12:02:13 +0000940 return -EINVAL;
941 }
942 DEBUGP(DRR, "<- Channel was requested by %s\n",
Harald Welte76042182009-08-08 16:03:15 +0200943 subscr->name && strlen(subscr->name) ? subscr->name : subscr->imsi);
Holger Freyther053e09d2009-02-14 22:51:06 +0000944
Harald Weltec2e302d2009-07-05 14:08:13 +0200945 subscr->equipment.classmark2_len = *classmark2_lv;
946 memcpy(subscr->equipment.classmark2, classmark2_lv+1, *classmark2_lv);
947 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +0000948
Holger Hans Peter Freyther3ee5d3e2009-08-21 05:18:21 +0200949 rc = gsm48_handle_paging_resp(msg, subscr);
Harald Welte2d35ae62009-02-06 12:02:13 +0000950 return rc;
951}
952
Harald Weltef7c43522009-06-09 20:24:21 +0000953static int gsm48_rx_rr_classmark(struct msgb *msg)
954{
955 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100956 struct gsm_subscriber *subscr = msg->lchan->conn.subscr;
Harald Weltef7c43522009-06-09 20:24:21 +0000957 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
958 u_int8_t cm2_len, cm3_len = 0;
959 u_int8_t *cm2, *cm3 = NULL;
960
961 DEBUGP(DRR, "CLASSMARK CHANGE ");
962
963 /* classmark 2 */
964 cm2_len = gh->data[0];
965 cm2 = &gh->data[1];
966 DEBUGPC(DRR, "CM2(len=%u) ", cm2_len);
967
968 if (payload_len > cm2_len + 1) {
969 /* we must have a classmark3 */
970 if (gh->data[cm2_len+1] != 0x20) {
971 DEBUGPC(DRR, "ERR CM3 TAG\n");
972 return -EINVAL;
973 }
974 if (cm2_len > 3) {
975 DEBUGPC(DRR, "CM2 too long!\n");
976 return -EINVAL;
977 }
978
979 cm3_len = gh->data[cm2_len+2];
980 cm3 = &gh->data[cm2_len+3];
981 if (cm3_len > 14) {
982 DEBUGPC(DRR, "CM3 len %u too long!\n", cm3_len);
983 return -EINVAL;
984 }
985 DEBUGPC(DRR, "CM3(len=%u)\n", cm3_len);
986 }
987 if (subscr) {
Harald Weltec2e302d2009-07-05 14:08:13 +0200988 subscr->equipment.classmark2_len = cm2_len;
989 memcpy(subscr->equipment.classmark2, cm2, cm2_len);
Harald Weltef7c43522009-06-09 20:24:21 +0000990 if (cm3) {
Harald Weltec2e302d2009-07-05 14:08:13 +0200991 subscr->equipment.classmark3_len = cm3_len;
992 memcpy(subscr->equipment.classmark3, cm3, cm3_len);
Harald Weltef7c43522009-06-09 20:24:21 +0000993 }
Harald Weltec2e302d2009-07-05 14:08:13 +0200994 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +0000995 }
996
Harald Weltef7c43522009-06-09 20:24:21 +0000997 return 0;
998}
999
Harald Weltecf5b3592009-05-01 18:28:42 +00001000static int gsm48_rx_rr_status(struct msgb *msg)
1001{
1002 struct gsm48_hdr *gh = msgb_l3(msg);
1003
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001004 DEBUGP(DRR, "STATUS rr_cause = %s\n",
Harald Weltecf5b3592009-05-01 18:28:42 +00001005 rr_cause_name(gh->data[0]));
1006
1007 return 0;
1008}
1009
Harald Weltef7c43522009-06-09 20:24:21 +00001010static int gsm48_rx_rr_meas_rep(struct msgb *msg)
1011{
Harald Welted12b0fd2009-12-15 21:36:05 +01001012 struct gsm_meas_rep *meas_rep = lchan_next_meas_rep(msg->lchan);
Harald Weltef7c43522009-06-09 20:24:21 +00001013
Harald Welte3c7dc6e2009-11-29 19:07:28 +01001014 /* This shouldn't actually end up here, as RSL treats
1015 * L3 Info of 08.58 MEASUREMENT REPORT different by calling
1016 * directly into gsm48_parse_meas_rep */
1017 DEBUGP(DMEAS, "DIRECT GSM48 MEASUREMENT REPORT ?!? ");
Harald Welted12b0fd2009-12-15 21:36:05 +01001018 gsm48_parse_meas_rep(meas_rep, msg);
Harald Weltef7c43522009-06-09 20:24:21 +00001019
1020 return 0;
1021}
1022
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001023static int gsm48_rx_rr_app_info(struct msgb *msg)
1024{
1025 struct gsm48_hdr *gh = msgb_l3(msg);
1026 u_int8_t apdu_id_flags;
1027 u_int8_t apdu_len;
1028 u_int8_t *apdu_data;
1029
1030 apdu_id_flags = gh->data[0];
1031 apdu_len = gh->data[1];
1032 apdu_data = gh->data+2;
1033
1034 DEBUGP(DNM, "RX APPLICATION INFO id/flags=0x%02x apdu_len=%u apdu=%s",
1035 apdu_id_flags, apdu_len, hexdump(apdu_data, apdu_len));
1036
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01001037 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 +02001038}
1039
Sylvain Munaut30a15382009-12-24 00:27:26 +01001040/* Chapter 9.1.10 Ciphering Mode Complete */
1041static int gsm48_rx_rr_ciph_m_compl(struct msgb *msg)
1042{
1043 struct gsm_lchan *lchan = msg->lchan;
1044 struct gsm_subscriber_connection *conn = &lchan->conn;
1045 gsm_cbfn *cb;
1046 int rc = 0;
1047
1048 DEBUGP(DRR, "CIPHERING MODE COMPLETE\n");
1049
1050 /* Safety check */
1051 if (!conn->sec_operation) {
1052 DEBUGP(DRR, "No authentication/cipher operation in progress !!!\n");
1053 return -EIO;
1054 }
1055
1056 /* FIXME: check for MI (if any) */
1057
1058 /* Call back whatever was in progress (if anything) ... */
1059 cb = conn->sec_operation->cb;
1060 if (cb) {
1061 rc = cb(GSM_HOOK_RR_SECURITY, GSM_SECURITY_SUCCEEDED,
1062 NULL, lchan, conn->sec_operation->cb_data);
1063 }
1064
1065 /* Complete the operation */
1066 release_security_operation(conn);
1067
1068 return rc;
1069}
1070
Harald Welted011e8b2009-11-29 22:45:52 +01001071/* Chapter 9.1.16 Handover complete */
Harald Welte1e191c52009-12-14 17:51:15 +01001072static int gsm48_rx_rr_ho_compl(struct msgb *msg)
Harald Welted011e8b2009-11-29 22:45:52 +01001073{
1074 struct gsm48_hdr *gh = msgb_l3(msg);
1075
1076 DEBUGP(DRR, "HANDOVER COMPLETE cause = %s\n",
1077 rr_cause_name(gh->data[0]));
1078
1079 dispatch_signal(SS_LCHAN, S_LCHAN_HANDOVER_COMPL, msg->lchan);
1080 /* FIXME: release old channel */
1081
1082 return 0;
1083}
1084
1085/* Chapter 9.1.17 Handover Failure */
Harald Welte1e191c52009-12-14 17:51:15 +01001086static int gsm48_rx_rr_ho_fail(struct msgb *msg)
Harald Welted011e8b2009-11-29 22:45:52 +01001087{
1088 struct gsm48_hdr *gh = msgb_l3(msg);
1089
1090 DEBUGP(DRR, "HANDOVER FAILED cause = %s\n",
1091 rr_cause_name(gh->data[0]));
1092
1093 dispatch_signal(SS_LCHAN, S_LCHAN_HANDOVER_FAIL, msg->lchan);
1094 /* FIXME: release allocated new channel */
1095
1096 return 0;
1097}
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001098
Harald Weltebf5e8df2009-02-03 12:59:45 +00001099/* Receive a GSM 04.08 Radio Resource (RR) message */
Harald Welte52b1f982008-12-23 20:25:15 +00001100static int gsm0408_rcv_rr(struct msgb *msg)
1101{
1102 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001103 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001104
1105 switch (gh->msg_type) {
1106 case GSM48_MT_RR_CLSM_CHG:
Harald Weltef7c43522009-06-09 20:24:21 +00001107 rc = gsm48_rx_rr_classmark(msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001108 break;
Harald Weltefc977a82008-12-27 10:19:37 +00001109 case GSM48_MT_RR_GPRS_SUSP_REQ:
1110 DEBUGP(DRR, "GRPS SUSPEND REQUEST\n");
1111 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001112 case GSM48_MT_RR_PAG_RESP:
Holger Hans Peter Freytherca0fcbe2009-10-23 13:48:54 +02001113 rc = gsm48_rx_rr_pag_resp(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001114 break;
Harald Welte7ccf7782009-02-17 01:43:01 +00001115 case GSM48_MT_RR_CHAN_MODE_MODIF_ACK:
Holger Hans Peter Freytherf520e642009-10-22 15:23:11 +02001116 rc = gsm48_rx_rr_modif_ack(msg);
Harald Welte7ccf7782009-02-17 01:43:01 +00001117 break;
Harald Weltecf5b3592009-05-01 18:28:42 +00001118 case GSM48_MT_RR_STATUS:
1119 rc = gsm48_rx_rr_status(msg);
1120 break;
Harald Weltef7c43522009-06-09 20:24:21 +00001121 case GSM48_MT_RR_MEAS_REP:
1122 rc = gsm48_rx_rr_meas_rep(msg);
1123 break;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001124 case GSM48_MT_RR_APP_INFO:
1125 rc = gsm48_rx_rr_app_info(msg);
1126 break;
Harald Welte08d91a52009-08-30 15:37:11 +09001127 case GSM48_MT_RR_CIPH_M_COMPL:
Sylvain Munaut30a15382009-12-24 00:27:26 +01001128 rc = gsm48_rx_rr_ciph_m_compl(msg);
Harald Welte08d91a52009-08-30 15:37:11 +09001129 break;
Harald Welteccd5a882009-11-29 20:02:20 +01001130 case GSM48_MT_RR_HANDO_COMPL:
Harald Welted011e8b2009-11-29 22:45:52 +01001131 rc = gsm48_rx_rr_ho_compl(msg);
Harald Welteccd5a882009-11-29 20:02:20 +01001132 break;
1133 case GSM48_MT_RR_HANDO_FAIL:
Harald Welted011e8b2009-11-29 22:45:52 +01001134 rc = gsm48_rx_rr_ho_fail(msg);
Harald Welteccd5a882009-11-29 20:02:20 +01001135 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001136 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01001137 LOGP(DRR, LOGL_NOTICE, "Unimplemented "
1138 "GSM 04.08 RR msg type 0x%02x\n", gh->msg_type);
Harald Welte52b1f982008-12-23 20:25:15 +00001139 break;
1140 }
1141
Harald Welte2d35ae62009-02-06 12:02:13 +00001142 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +00001143}
1144
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001145int gsm48_send_rr_app_info(struct gsm_lchan *lchan, u_int8_t apdu_id,
Holger Hans Peter Freyther8b77a342009-10-22 15:42:19 +02001146 u_int8_t apdu_len, const u_int8_t *apdu)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001147{
1148 struct msgb *msg = gsm48_msgb_alloc();
1149 struct gsm48_hdr *gh;
1150
1151 msg->lchan = lchan;
1152
1153 DEBUGP(DRR, "TX APPLICATION INFO id=0x%02x, len=%u\n",
1154 apdu_id, apdu_len);
1155
1156 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 2 + apdu_len);
1157 gh->proto_discr = GSM48_PDISC_RR;
1158 gh->msg_type = GSM48_MT_RR_APP_INFO;
1159 gh->data[0] = apdu_id;
1160 gh->data[1] = apdu_len;
1161 memcpy(gh->data+2, apdu, apdu_len);
1162
1163 return gsm48_sendmsg(msg, NULL);
1164}
1165
Harald Welte4bc90a12008-12-27 16:32:52 +00001166/* Call Control */
1167
Harald Welte7584aea2009-02-11 11:44:12 +00001168/* The entire call control code is written in accordance with Figure 7.10c
1169 * for 'very early assignment', i.e. we allocate a TCH/F during IMMEDIATE
1170 * ASSIGN, then first use that TCH/F for signalling and later MODE MODIFY
1171 * it for voice */
1172
Harald Welte4bfdfe72009-06-10 23:11:52 +08001173static void new_cc_state(struct gsm_trans *trans, int state)
1174{
1175 if (state > 31 || state < 0)
1176 return;
1177
1178 DEBUGP(DCC, "new state %s -> %s\n",
Harald Weltee95daf12010-03-25 12:13:02 +08001179 gsm48_cc_state_name(trans->cc.state),
1180 gsm48_cc_state_name(state));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001181
Harald Weltedcaf5652009-07-23 18:56:43 +02001182 trans->cc.state = state;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001183}
1184
1185static int gsm48_cc_tx_status(struct gsm_trans *trans, void *arg)
Harald Welte4bc90a12008-12-27 16:32:52 +00001186{
1187 struct msgb *msg = gsm48_msgb_alloc();
1188 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1189 u_int8_t *cause, *call_state;
1190
Harald Welte4bc90a12008-12-27 16:32:52 +00001191 gh->msg_type = GSM48_MT_CC_STATUS;
1192
1193 cause = msgb_put(msg, 3);
1194 cause[0] = 2;
1195 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
1196 cause[2] = 0x80 | 30; /* response to status inquiry */
1197
1198 call_state = msgb_put(msg, 1);
1199 call_state[0] = 0xc0 | 0x00;
1200
Harald Welte39e2ead2009-07-23 21:13:03 +02001201 return gsm48_sendmsg(msg, trans);
Harald Welte4bc90a12008-12-27 16:32:52 +00001202}
1203
Harald Welte6f4b7532008-12-29 00:39:37 +00001204static int gsm48_tx_simple(struct gsm_lchan *lchan,
1205 u_int8_t pdisc, u_int8_t msg_type)
Harald Welte4bc90a12008-12-27 16:32:52 +00001206{
1207 struct msgb *msg = gsm48_msgb_alloc();
1208 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1209
1210 msg->lchan = lchan;
1211
Harald Welte6f4b7532008-12-29 00:39:37 +00001212 gh->proto_discr = pdisc;
Harald Welte4bc90a12008-12-27 16:32:52 +00001213 gh->msg_type = msg_type;
1214
Harald Welte39e2ead2009-07-23 21:13:03 +02001215 return gsm48_sendmsg(msg, NULL);
Harald Welte4bc90a12008-12-27 16:32:52 +00001216}
1217
Harald Welte4bfdfe72009-06-10 23:11:52 +08001218static void gsm48_stop_cc_timer(struct gsm_trans *trans)
1219{
Harald Weltedcaf5652009-07-23 18:56:43 +02001220 if (bsc_timer_pending(&trans->cc.timer)) {
1221 DEBUGP(DCC, "stopping pending timer T%x\n", trans->cc.Tcurrent);
1222 bsc_del_timer(&trans->cc.timer);
1223 trans->cc.Tcurrent = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001224 }
1225}
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001226
Harald Welte4bfdfe72009-06-10 23:11:52 +08001227static int mncc_recvmsg(struct gsm_network *net, struct gsm_trans *trans,
1228 int msg_type, struct gsm_mncc *mncc)
1229{
1230 struct msgb *msg;
1231
1232 if (trans)
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001233 if (trans->conn)
Harald Welte6f5aee02009-07-23 21:21:14 +02001234 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08001235 "Sending '%s' to MNCC.\n",
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001236 trans->conn->lchan->ts->trx->bts->nr,
1237 trans->conn->lchan->ts->trx->nr,
1238 trans->conn->lchan->ts->nr, trans->transaction_id,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001239 (trans->subscr)?(trans->subscr->extension):"-",
1240 get_mncc_name(msg_type));
1241 else
1242 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
1243 "Sending '%s' to MNCC.\n",
1244 (trans->subscr)?(trans->subscr->extension):"-",
1245 get_mncc_name(msg_type));
1246 else
1247 DEBUGP(DCC, "(bts - trx - ts - ti -- sub -) "
1248 "Sending '%s' to MNCC.\n", get_mncc_name(msg_type));
1249
1250 mncc->msg_type = msg_type;
1251
Harald Welte966636f2009-06-26 19:39:35 +02001252 msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001253 if (!msg)
1254 return -ENOMEM;
1255 memcpy(msg->data, mncc, sizeof(struct gsm_mncc));
1256 msgb_enqueue(&net->upqueue, msg);
1257
1258 return 0;
1259}
1260
1261int mncc_release_ind(struct gsm_network *net, struct gsm_trans *trans,
1262 u_int32_t callref, int location, int value)
1263{
1264 struct gsm_mncc rel;
1265
Harald Welte92f70c52009-06-12 01:54:08 +08001266 memset(&rel, 0, sizeof(rel));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001267 rel.callref = callref;
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001268 mncc_set_cause(&rel, location, value);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001269 return mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
1270}
1271
Harald Weltedcaf5652009-07-23 18:56:43 +02001272/* Call Control Specific transaction release.
1273 * gets called by trans_free, DO NOT CALL YOURSELF! */
1274void _gsm48_cc_trans_free(struct gsm_trans *trans)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001275{
Harald Welte4bfdfe72009-06-10 23:11:52 +08001276 gsm48_stop_cc_timer(trans);
1277
1278 /* send release to L4, if callref still exists */
1279 if (trans->callref) {
1280 /* Ressource unavailable */
Harald Welte596fed42009-07-23 19:06:52 +02001281 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001282 GSM48_CAUSE_LOC_PRN_S_LU,
1283 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001284 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001285 if (trans->cc.state != GSM_CSTATE_NULL)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001286 new_cc_state(trans, GSM_CSTATE_NULL);
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001287 if (trans->conn)
1288 trau_mux_unmap(&trans->conn->lchan->ts->e1_link, trans->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001289}
1290
1291static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg);
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001292
Harald Welte09e38af2009-02-16 22:52:23 +00001293/* call-back from paging the B-end of the connection */
1294static int setup_trig_pag_evt(unsigned int hooknum, unsigned int event,
Harald Welte7ccf7782009-02-17 01:43:01 +00001295 struct msgb *msg, void *_lchan, void *param)
Harald Welte09e38af2009-02-16 22:52:23 +00001296{
Harald Welte7ccf7782009-02-17 01:43:01 +00001297 struct gsm_lchan *lchan = _lchan;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001298 struct gsm_subscriber *subscr = param;
1299 struct gsm_trans *transt, *tmp;
1300 struct gsm_network *net;
Harald Weltec05677b2009-06-26 20:17:06 +02001301
Harald Welte09e38af2009-02-16 22:52:23 +00001302 if (hooknum != GSM_HOOK_RR_PAGING)
1303 return -EINVAL;
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001304
Harald Welte4bfdfe72009-06-10 23:11:52 +08001305 if (!subscr)
1306 return -EINVAL;
1307 net = subscr->net;
1308 if (!net) {
1309 DEBUGP(DCC, "Error Network not set!\n");
1310 return -EINVAL;
Harald Welte5a065df2009-02-22 21:13:18 +00001311 }
Harald Welte7584aea2009-02-11 11:44:12 +00001312
Harald Welte4bfdfe72009-06-10 23:11:52 +08001313 /* check all tranactions (without lchan) for subscriber */
1314 llist_for_each_entry_safe(transt, tmp, &net->trans_list, entry) {
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001315 if (transt->subscr != subscr || transt->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001316 continue;
1317 switch (event) {
1318 case GSM_PAGING_SUCCEEDED:
1319 if (!lchan) // paranoid
1320 break;
1321 DEBUGP(DCC, "Paging subscr %s succeeded!\n",
1322 subscr->extension);
1323 /* Assign lchan */
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001324 if (!transt->conn) {
1325 transt->conn = &lchan->conn;
1326 use_subscr_con(transt->conn);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001327 }
1328 /* send SETUP request to called party */
Harald Weltedcaf5652009-07-23 18:56:43 +02001329 gsm48_cc_tx_setup(transt, &transt->cc.msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001330 break;
1331 case GSM_PAGING_EXPIRED:
1332 DEBUGP(DCC, "Paging subscr %s expired!\n",
1333 subscr->extension);
1334 /* Temporarily out of order */
Harald Welte596fed42009-07-23 19:06:52 +02001335 mncc_release_ind(transt->subscr->net, transt,
1336 transt->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001337 GSM48_CAUSE_LOC_PRN_S_LU,
1338 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001339 transt->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001340 trans_free(transt);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001341 break;
1342 }
1343 }
Harald Welte09e38af2009-02-16 22:52:23 +00001344 return 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00001345}
Harald Welte7584aea2009-02-11 11:44:12 +00001346
Harald Welteda7ab742009-12-19 22:23:05 +01001347static int tch_recv_mncc(struct gsm_network *net, u_int32_t callref, int enable);
1348
Harald Welte805f6442009-07-28 18:25:29 +02001349/* some other part of the code sends us a signal */
1350static int handle_abisip_signal(unsigned int subsys, unsigned int signal,
1351 void *handler_data, void *signal_data)
1352{
1353 struct gsm_lchan *lchan = signal_data;
Harald Welte805f6442009-07-28 18:25:29 +02001354 int rc;
Harald Welteda7ab742009-12-19 22:23:05 +01001355 struct gsm_network *net;
1356 struct gsm_trans *trans;
Harald Welte805f6442009-07-28 18:25:29 +02001357
1358 if (subsys != SS_ABISIP)
1359 return 0;
1360
1361 /* in case we use direct BTS-to-BTS RTP */
1362 if (ipacc_rtp_direct)
1363 return 0;
1364
Harald Welte805f6442009-07-28 18:25:29 +02001365 switch (signal) {
Holger Hans Peter Freyther231163d2009-11-18 21:06:12 +01001366 case S_ABISIP_CRCX_ACK:
Harald Welteda7ab742009-12-19 22:23:05 +01001367 /* check if any transactions on this lchan still have
1368 * a tch_recv_mncc request pending */
1369 net = lchan->ts->trx->bts->network;
1370 llist_for_each_entry(trans, &net->trans_list, entry) {
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001371 if (trans->conn && trans->conn->lchan == lchan && trans->tch_recv) {
Harald Welteda7ab742009-12-19 22:23:05 +01001372 DEBUGP(DCC, "pending tch_recv_mncc request\n");
1373 tch_recv_mncc(net, trans->callref, 1);
1374 }
1375 }
Harald Welte805f6442009-07-28 18:25:29 +02001376 break;
Harald Welte805f6442009-07-28 18:25:29 +02001377 }
1378
1379 return 0;
Harald Welte805f6442009-07-28 18:25:29 +02001380}
1381
Harald Welte49f48b82009-02-17 15:29:33 +00001382/* map two ipaccess RTP streams onto each other */
Harald Welte11fa29c2009-02-19 17:24:39 +00001383static int tch_map(struct gsm_lchan *lchan, struct gsm_lchan *remote_lchan)
Harald Welte49f48b82009-02-17 15:29:33 +00001384{
Harald Welte11fa29c2009-02-19 17:24:39 +00001385 struct gsm_bts *bts = lchan->ts->trx->bts;
1386 struct gsm_bts *remote_bts = remote_lchan->ts->trx->bts;
Harald Welte805f6442009-07-28 18:25:29 +02001387 int rc;
Harald Welte49f48b82009-02-17 15:29:33 +00001388
Harald Welte11fa29c2009-02-19 17:24:39 +00001389 DEBUGP(DCC, "Setting up TCH map between (bts=%u,trx=%u,ts=%u) and (bts=%u,trx=%u,ts=%u)\n",
1390 bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
1391 remote_bts->nr, remote_lchan->ts->trx->nr, remote_lchan->ts->nr);
1392
1393 if (bts->type != remote_bts->type) {
1394 DEBUGP(DCC, "Cannot switch calls between different BTS types yet\n");
1395 return -EINVAL;
1396 }
Harald Welteda7ab742009-12-19 22:23:05 +01001397
1398 // todo: map between different bts types
Harald Welte11fa29c2009-02-19 17:24:39 +00001399 switch (bts->type) {
Mike Habene2d82272009-10-02 12:19:34 +01001400 case GSM_BTS_TYPE_NANOBTS:
Harald Welte805f6442009-07-28 18:25:29 +02001401 if (!ipacc_rtp_direct) {
1402 /* connect the TCH's to our RTP proxy */
Harald Weltea72273e2009-12-20 16:51:09 +01001403 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welte805f6442009-07-28 18:25:29 +02001404 if (rc < 0)
1405 return rc;
Harald Weltea72273e2009-12-20 16:51:09 +01001406 rc = rsl_ipacc_mdcx_to_rtpsock(remote_lchan);
Harald Welteda7ab742009-12-19 22:23:05 +01001407#warning do we need a check of rc here?
Harald Welte805f6442009-07-28 18:25:29 +02001408
1409 /* connect them with each other */
Harald Welte2c828992009-12-02 01:56:49 +05301410 rtp_socket_proxy(lchan->abis_ip.rtp_socket,
1411 remote_lchan->abis_ip.rtp_socket);
Harald Welte805f6442009-07-28 18:25:29 +02001412 } else {
1413 /* directly connect TCH RTP streams to each other */
Harald Welte2c828992009-12-02 01:56:49 +05301414 rc = rsl_ipacc_mdcx(lchan, remote_lchan->abis_ip.bound_ip,
1415 remote_lchan->abis_ip.bound_port,
Harald Welte2c828992009-12-02 01:56:49 +05301416 remote_lchan->abis_ip.rtp_payload2);
Harald Welte805f6442009-07-28 18:25:29 +02001417 if (rc < 0)
1418 return rc;
Harald Welte2c828992009-12-02 01:56:49 +05301419 rc = rsl_ipacc_mdcx(remote_lchan, lchan->abis_ip.bound_ip,
1420 lchan->abis_ip.bound_port,
Harald Welte2c828992009-12-02 01:56:49 +05301421 lchan->abis_ip.rtp_payload2);
Harald Welte805f6442009-07-28 18:25:29 +02001422 }
Harald Welte11fa29c2009-02-19 17:24:39 +00001423 break;
1424 case GSM_BTS_TYPE_BS11:
1425 trau_mux_map_lchan(lchan, remote_lchan);
1426 break;
1427 default:
1428 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
Harald Welteda7ab742009-12-19 22:23:05 +01001429 return -EINVAL;
Harald Welte11fa29c2009-02-19 17:24:39 +00001430 }
Harald Welte49f48b82009-02-17 15:29:33 +00001431
1432 return 0;
1433}
1434
Harald Welte4bfdfe72009-06-10 23:11:52 +08001435/* bridge channels of two transactions */
1436static int tch_bridge(struct gsm_network *net, u_int32_t *refs)
Harald Welte7ccf7782009-02-17 01:43:01 +00001437{
Harald Weltedcaf5652009-07-23 18:56:43 +02001438 struct gsm_trans *trans1 = trans_find_by_callref(net, refs[0]);
1439 struct gsm_trans *trans2 = trans_find_by_callref(net, refs[1]);
Harald Welte7ccf7782009-02-17 01:43:01 +00001440
Harald Welte4bfdfe72009-06-10 23:11:52 +08001441 if (!trans1 || !trans2)
Harald Welte7ccf7782009-02-17 01:43:01 +00001442 return -EIO;
1443
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001444 if (!trans1->conn || !trans2->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001445 return -EIO;
1446
1447 /* through-connect channel */
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001448 return tch_map(trans1->conn->lchan, trans2->conn->lchan);
Harald Welte7ccf7782009-02-17 01:43:01 +00001449}
1450
Harald Welteda7ab742009-12-19 22:23:05 +01001451/* enable receive of channels to MNCC upqueue */
1452static int tch_recv_mncc(struct gsm_network *net, u_int32_t callref, int enable)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001453{
1454 struct gsm_trans *trans;
Harald Welteda7ab742009-12-19 22:23:05 +01001455 struct gsm_lchan *lchan;
1456 struct gsm_bts *bts;
1457 int rc;
Harald Welte7ccf7782009-02-17 01:43:01 +00001458
Harald Welte4bfdfe72009-06-10 23:11:52 +08001459 /* Find callref */
Harald Welteda7ab742009-12-19 22:23:05 +01001460 trans = trans_find_by_callref(net, callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001461 if (!trans)
1462 return -EIO;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001463 if (!trans->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001464 return 0;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001465 lchan = trans->conn->lchan;
Harald Welteda7ab742009-12-19 22:23:05 +01001466 bts = lchan->ts->trx->bts;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001467
Harald Welteda7ab742009-12-19 22:23:05 +01001468 switch (bts->type) {
1469 case GSM_BTS_TYPE_NANOBTS:
1470 if (ipacc_rtp_direct) {
1471 DEBUGP(DCC, "Error: RTP proxy is disabled\n");
1472 return -EINVAL;
1473 }
1474 /* in case, we don't have a RTP socket yet, we note this
1475 * in the transaction and try later */
1476 if (!lchan->abis_ip.rtp_socket) {
1477 trans->tch_recv = enable;
1478 DEBUGP(DCC, "queue tch_recv_mncc request (%d)\n", enable);
1479 return 0;
1480 }
1481 if (enable) {
1482 /* connect the TCH's to our RTP proxy */
Harald Weltea72273e2009-12-20 16:51:09 +01001483 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welteda7ab742009-12-19 22:23:05 +01001484 if (rc < 0)
1485 return rc;
1486 /* assign socket to application interface */
1487 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
1488 net, callref);
1489 } else
1490 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
1491 net, 0);
1492 break;
1493 case GSM_BTS_TYPE_BS11:
1494 if (enable)
1495 return trau_recv_lchan(lchan, callref);
1496 return trau_mux_unmap(NULL, callref);
1497 break;
1498 default:
1499 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
1500 return -EINVAL;
1501 }
1502
1503 return 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001504}
1505
Harald Welte4bfdfe72009-06-10 23:11:52 +08001506static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
1507{
1508 DEBUGP(DCC, "-> STATUS ENQ\n");
1509 return gsm48_cc_tx_status(trans, msg);
1510}
1511
1512static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg);
1513static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg);
1514
1515static void gsm48_cc_timeout(void *arg)
1516{
1517 struct gsm_trans *trans = arg;
1518 int disconnect = 0, release = 0;
Harald Weltec66b71c2009-06-11 14:23:20 +08001519 int mo_cause = GSM48_CC_CAUSE_RECOVERY_TIMER;
1520 int mo_location = GSM48_CAUSE_LOC_USER;
1521 int l4_cause = GSM48_CC_CAUSE_NORMAL_UNSPEC;
1522 int l4_location = GSM48_CAUSE_LOC_PRN_S_LU;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001523 struct gsm_mncc mo_rel, l4_rel;
1524
1525 memset(&mo_rel, 0, sizeof(struct gsm_mncc));
1526 mo_rel.callref = trans->callref;
1527 memset(&l4_rel, 0, sizeof(struct gsm_mncc));
1528 l4_rel.callref = trans->callref;
1529
Harald Weltedcaf5652009-07-23 18:56:43 +02001530 switch(trans->cc.Tcurrent) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001531 case 0x303:
1532 release = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001533 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001534 break;
1535 case 0x310:
1536 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001537 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001538 break;
1539 case 0x313:
1540 disconnect = 1;
1541 /* unknown, did not find it in the specs */
1542 break;
1543 case 0x301:
1544 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001545 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001546 break;
1547 case 0x308:
Harald Weltedcaf5652009-07-23 18:56:43 +02001548 if (!trans->cc.T308_second) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001549 /* restart T308 a second time */
Harald Weltedcaf5652009-07-23 18:56:43 +02001550 gsm48_cc_tx_release(trans, &trans->cc.msg);
1551 trans->cc.T308_second = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001552 break; /* stay in release state */
1553 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001554 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001555 return;
1556// release = 1;
1557// l4_cause = 14;
1558// break;
1559 case 0x306:
1560 release = 1;
Harald Weltedcaf5652009-07-23 18:56:43 +02001561 mo_cause = trans->cc.msg.cause.value;
1562 mo_location = trans->cc.msg.cause.location;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001563 break;
1564 case 0x323:
1565 disconnect = 1;
1566 break;
1567 default:
1568 release = 1;
1569 }
1570
1571 if (release && trans->callref) {
1572 /* process release towards layer 4 */
Harald Welte596fed42009-07-23 19:06:52 +02001573 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001574 l4_location, l4_cause);
1575 trans->callref = 0;
1576 }
1577
1578 if (disconnect && trans->callref) {
1579 /* process disconnect towards layer 4 */
1580 mncc_set_cause(&l4_rel, l4_location, l4_cause);
Harald Welte596fed42009-07-23 19:06:52 +02001581 mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &l4_rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001582 }
1583
1584 /* process disconnect towards mobile station */
1585 if (disconnect || release) {
1586 mncc_set_cause(&mo_rel, mo_location, mo_cause);
Harald Weltedcaf5652009-07-23 18:56:43 +02001587 mo_rel.cause.diag[0] = ((trans->cc.Tcurrent & 0xf00) >> 8) + '0';
1588 mo_rel.cause.diag[1] = ((trans->cc.Tcurrent & 0x0f0) >> 4) + '0';
1589 mo_rel.cause.diag[2] = (trans->cc.Tcurrent & 0x00f) + '0';
Harald Welte4bfdfe72009-06-10 23:11:52 +08001590 mo_rel.cause.diag_len = 3;
1591
1592 if (disconnect)
1593 gsm48_cc_tx_disconnect(trans, &mo_rel);
1594 if (release)
1595 gsm48_cc_tx_release(trans, &mo_rel);
1596 }
1597
1598}
1599
1600static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
1601 int sec, int micro)
1602{
1603 DEBUGP(DCC, "starting timer T%x with %d seconds\n", current, sec);
Harald Weltedcaf5652009-07-23 18:56:43 +02001604 trans->cc.timer.cb = gsm48_cc_timeout;
1605 trans->cc.timer.data = trans;
1606 bsc_schedule_timer(&trans->cc.timer, sec, micro);
1607 trans->cc.Tcurrent = current;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001608}
1609
1610static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
1611{
1612 struct gsm48_hdr *gh = msgb_l3(msg);
1613 u_int8_t msg_type = gh->msg_type & 0xbf;
1614 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1615 struct tlv_parsed tp;
1616 struct gsm_mncc setup;
1617
1618 memset(&setup, 0, sizeof(struct gsm_mncc));
1619 setup.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01001620 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001621 /* emergency setup is identified by msg_type */
1622 if (msg_type == GSM48_MT_CC_EMERG_SETUP)
1623 setup.emergency = 1;
1624
1625 /* use subscriber as calling party number */
1626 if (trans->subscr) {
1627 setup.fields |= MNCC_F_CALLING;
1628 strncpy(setup.calling.number, trans->subscr->extension,
1629 sizeof(setup.calling.number)-1);
Andreas Eversbergc079be42009-06-15 23:22:09 +02001630 strncpy(setup.imsi, trans->subscr->imsi,
1631 sizeof(setup.imsi)-1);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001632 }
1633 /* bearer capability */
1634 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1635 setup.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001636 gsm48_decode_bearer_cap(&setup.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001637 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1638 }
1639 /* facility */
1640 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1641 setup.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001642 gsm48_decode_facility(&setup.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001643 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1644 }
1645 /* called party bcd number */
1646 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
1647 setup.fields |= MNCC_F_CALLED;
Harald Welte55c8f352010-03-07 23:40:35 +01001648 gsm48_decode_called(&setup.called,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001649 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
1650 }
1651 /* user-user */
1652 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1653 setup.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01001654 gsm48_decode_useruser(&setup.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001655 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1656 }
1657 /* ss-version */
1658 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1659 setup.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01001660 gsm48_decode_ssversion(&setup.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001661 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1662 }
1663 /* CLIR suppression */
1664 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
1665 setup.clir.sup = 1;
1666 /* CLIR invocation */
1667 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
1668 setup.clir.inv = 1;
1669 /* cc cap */
1670 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1671 setup.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001672 gsm48_decode_cccap(&setup.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001673 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1674 }
1675
Harald Welte4bfdfe72009-06-10 23:11:52 +08001676 new_cc_state(trans, GSM_CSTATE_INITIATED);
1677
Harald Welte (local)47df3992009-12-26 19:45:03 +01001678 LOGP(DCC, LOGL_INFO, "Subscriber %s (%s) sends SETUP to %s\n",
1679 subscr_name(trans->subscr), trans->subscr->extension,
1680 setup.called.number);
1681
Harald Welte4bfdfe72009-06-10 23:11:52 +08001682 /* indicate setup to MNCC */
Harald Welte596fed42009-07-23 19:06:52 +02001683 mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_IND, &setup);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001684
Harald Welte13cac662009-07-29 12:10:35 +02001685 /* MNCC code will modify the channel asynchronously, we should
1686 * ipaccess-bind only after the modification has been made to the
1687 * lchan->tch_mode */
Harald Welte4bfdfe72009-06-10 23:11:52 +08001688 return 0;
1689}
1690
1691static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
Harald Welte65e74cc2008-12-29 01:55:35 +00001692{
1693 struct msgb *msg = gsm48_msgb_alloc();
1694 struct gsm48_hdr *gh;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001695 struct gsm_mncc *setup = arg;
Harald Welte78283ef2009-07-23 21:36:44 +02001696 int rc, trans_id;
Harald Welte65e74cc2008-12-29 01:55:35 +00001697
Harald Welte7ccf7782009-02-17 01:43:01 +00001698 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte65e74cc2008-12-29 01:55:35 +00001699
Harald Welte4bfdfe72009-06-10 23:11:52 +08001700 /* transaction id must not be assigned */
1701 if (trans->transaction_id != 0xff) { /* unasssigned */
1702 DEBUGP(DCC, "TX Setup with assigned transaction. "
1703 "This is not allowed!\n");
1704 /* Temporarily out of order */
Harald Welte596fed42009-07-23 19:06:52 +02001705 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001706 GSM48_CAUSE_LOC_PRN_S_LU,
1707 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001708 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001709 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001710 return rc;
1711 }
1712
1713 /* Get free transaction_id */
Harald Welte78283ef2009-07-23 21:36:44 +02001714 trans_id = trans_assign_trans_id(trans->subscr, GSM48_PDISC_CC, 0);
1715 if (trans_id < 0) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001716 /* no free transaction ID */
Harald Welte596fed42009-07-23 19:06:52 +02001717 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001718 GSM48_CAUSE_LOC_PRN_S_LU,
1719 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001720 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001721 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001722 return rc;
1723 }
Harald Welte78283ef2009-07-23 21:36:44 +02001724 trans->transaction_id = trans_id;
Harald Welte49f48b82009-02-17 15:29:33 +00001725
Harald Welte65e74cc2008-12-29 01:55:35 +00001726 gh->msg_type = GSM48_MT_CC_SETUP;
Harald Welte09e38af2009-02-16 22:52:23 +00001727
Harald Welte4bfdfe72009-06-10 23:11:52 +08001728 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
Harald Welte65e74cc2008-12-29 01:55:35 +00001729
Harald Welte4bfdfe72009-06-10 23:11:52 +08001730 /* bearer capability */
1731 if (setup->fields & MNCC_F_BEARER_CAP)
Harald Welte55c8f352010-03-07 23:40:35 +01001732 gsm48_encode_bearer_cap(msg, 0, &setup->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001733 /* facility */
1734 if (setup->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001735 gsm48_encode_facility(msg, 0, &setup->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001736 /* progress */
1737 if (setup->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001738 gsm48_encode_progress(msg, 0, &setup->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001739 /* calling party BCD number */
1740 if (setup->fields & MNCC_F_CALLING)
Harald Welte55c8f352010-03-07 23:40:35 +01001741 gsm48_encode_calling(msg, &setup->calling);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001742 /* called party BCD number */
1743 if (setup->fields & MNCC_F_CALLED)
Harald Welte55c8f352010-03-07 23:40:35 +01001744 gsm48_encode_called(msg, &setup->called);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001745 /* user-user */
1746 if (setup->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001747 gsm48_encode_useruser(msg, 0, &setup->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001748 /* redirecting party BCD number */
1749 if (setup->fields & MNCC_F_REDIRECTING)
Harald Welte55c8f352010-03-07 23:40:35 +01001750 gsm48_encode_redirecting(msg, &setup->redirecting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001751 /* signal */
1752 if (setup->fields & MNCC_F_SIGNAL)
Harald Welte55c8f352010-03-07 23:40:35 +01001753 gsm48_encode_signal(msg, setup->signal);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001754
1755 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
Harald Welte65e74cc2008-12-29 01:55:35 +00001756
Harald Welte39e2ead2009-07-23 21:13:03 +02001757 return gsm48_sendmsg(msg, trans);
Harald Welte65e74cc2008-12-29 01:55:35 +00001758}
1759
Harald Welte4bfdfe72009-06-10 23:11:52 +08001760static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
1761{
1762 struct gsm48_hdr *gh = msgb_l3(msg);
1763 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1764 struct tlv_parsed tp;
1765 struct gsm_mncc call_conf;
1766
1767 gsm48_stop_cc_timer(trans);
1768 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
1769
1770 memset(&call_conf, 0, sizeof(struct gsm_mncc));
1771 call_conf.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01001772 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001773#if 0
1774 /* repeat */
1775 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
1776 call_conf.repeat = 1;
1777 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
1778 call_conf.repeat = 2;
1779#endif
1780 /* bearer capability */
1781 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1782 call_conf.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001783 gsm48_decode_bearer_cap(&call_conf.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001784 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1785 }
1786 /* cause */
1787 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1788 call_conf.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01001789 gsm48_decode_cause(&call_conf.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001790 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1791 }
1792 /* cc cap */
1793 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1794 call_conf.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001795 gsm48_decode_cccap(&call_conf.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001796 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1797 }
1798
1799 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
1800
Harald Welte596fed42009-07-23 19:06:52 +02001801 return mncc_recvmsg(trans->subscr->net, trans, MNCC_CALL_CONF_IND,
1802 &call_conf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001803}
1804
1805static int gsm48_cc_tx_call_proc(struct gsm_trans *trans, void *arg)
1806{
1807 struct gsm_mncc *proceeding = arg;
1808 struct msgb *msg = gsm48_msgb_alloc();
1809 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1810
Harald Welte4bfdfe72009-06-10 23:11:52 +08001811 gh->msg_type = GSM48_MT_CC_CALL_PROC;
1812
1813 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
1814
1815 /* bearer capability */
1816 if (proceeding->fields & MNCC_F_BEARER_CAP)
Harald Welte55c8f352010-03-07 23:40:35 +01001817 gsm48_encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001818 /* facility */
1819 if (proceeding->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001820 gsm48_encode_facility(msg, 0, &proceeding->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001821 /* progress */
1822 if (proceeding->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001823 gsm48_encode_progress(msg, 0, &proceeding->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001824
Harald Welte39e2ead2009-07-23 21:13:03 +02001825 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001826}
1827
1828static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
1829{
1830 struct gsm48_hdr *gh = msgb_l3(msg);
1831 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1832 struct tlv_parsed tp;
1833 struct gsm_mncc alerting;
1834
1835 gsm48_stop_cc_timer(trans);
1836 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
1837
1838 memset(&alerting, 0, sizeof(struct gsm_mncc));
1839 alerting.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01001840 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001841 /* facility */
1842 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1843 alerting.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001844 gsm48_decode_facility(&alerting.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001845 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1846 }
1847
1848 /* progress */
1849 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
1850 alerting.fields |= MNCC_F_PROGRESS;
Harald Welte55c8f352010-03-07 23:40:35 +01001851 gsm48_decode_progress(&alerting.progress,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001852 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
1853 }
1854 /* ss-version */
1855 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1856 alerting.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01001857 gsm48_decode_ssversion(&alerting.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001858 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1859 }
1860
1861 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
1862
Harald Welte596fed42009-07-23 19:06:52 +02001863 return mncc_recvmsg(trans->subscr->net, trans, MNCC_ALERT_IND,
1864 &alerting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001865}
1866
1867static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
1868{
1869 struct gsm_mncc *alerting = arg;
1870 struct msgb *msg = gsm48_msgb_alloc();
1871 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1872
Harald Welte4bfdfe72009-06-10 23:11:52 +08001873 gh->msg_type = GSM48_MT_CC_ALERTING;
1874
1875 /* facility */
1876 if (alerting->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001877 gsm48_encode_facility(msg, 0, &alerting->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001878 /* progress */
1879 if (alerting->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001880 gsm48_encode_progress(msg, 0, &alerting->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001881 /* user-user */
1882 if (alerting->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001883 gsm48_encode_useruser(msg, 0, &alerting->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001884
1885 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
1886
Harald Welte39e2ead2009-07-23 21:13:03 +02001887 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001888}
1889
1890static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
1891{
1892 struct gsm_mncc *progress = arg;
1893 struct msgb *msg = gsm48_msgb_alloc();
1894 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1895
Harald Welte4bfdfe72009-06-10 23:11:52 +08001896 gh->msg_type = GSM48_MT_CC_PROGRESS;
1897
1898 /* progress */
Harald Welte55c8f352010-03-07 23:40:35 +01001899 gsm48_encode_progress(msg, 1, &progress->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001900 /* user-user */
1901 if (progress->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001902 gsm48_encode_useruser(msg, 0, &progress->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001903
Harald Welte39e2ead2009-07-23 21:13:03 +02001904 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001905}
1906
1907static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
1908{
1909 struct gsm_mncc *connect = arg;
1910 struct msgb *msg = gsm48_msgb_alloc();
1911 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1912
Harald Welte4bfdfe72009-06-10 23:11:52 +08001913 gh->msg_type = GSM48_MT_CC_CONNECT;
1914
1915 gsm48_stop_cc_timer(trans);
1916 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
1917
1918 /* facility */
1919 if (connect->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001920 gsm48_encode_facility(msg, 0, &connect->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001921 /* progress */
1922 if (connect->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001923 gsm48_encode_progress(msg, 0, &connect->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001924 /* connected number */
1925 if (connect->fields & MNCC_F_CONNECTED)
Harald Welte55c8f352010-03-07 23:40:35 +01001926 gsm48_encode_connected(msg, &connect->connected);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001927 /* user-user */
1928 if (connect->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001929 gsm48_encode_useruser(msg, 0, &connect->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001930
1931 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
1932
Harald Welte39e2ead2009-07-23 21:13:03 +02001933 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001934}
1935
1936static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
1937{
1938 struct gsm48_hdr *gh = msgb_l3(msg);
1939 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1940 struct tlv_parsed tp;
1941 struct gsm_mncc connect;
1942
1943 gsm48_stop_cc_timer(trans);
1944
1945 memset(&connect, 0, sizeof(struct gsm_mncc));
1946 connect.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01001947 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001948 /* use subscriber as connected party number */
1949 if (trans->subscr) {
1950 connect.fields |= MNCC_F_CONNECTED;
1951 strncpy(connect.connected.number, trans->subscr->extension,
1952 sizeof(connect.connected.number)-1);
Andreas Eversbergc079be42009-06-15 23:22:09 +02001953 strncpy(connect.imsi, trans->subscr->imsi,
1954 sizeof(connect.imsi)-1);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001955 }
1956 /* facility */
1957 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1958 connect.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001959 gsm48_decode_facility(&connect.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001960 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1961 }
1962 /* user-user */
1963 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1964 connect.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01001965 gsm48_decode_useruser(&connect.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001966 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1967 }
1968 /* ss-version */
1969 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1970 connect.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01001971 gsm48_decode_ssversion(&connect.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001972 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1973 }
1974
1975 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
1976
Harald Welte596fed42009-07-23 19:06:52 +02001977 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_CNF, &connect);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001978}
1979
1980
1981static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
1982{
1983 struct gsm_mncc connect_ack;
1984
1985 gsm48_stop_cc_timer(trans);
1986
1987 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1988
1989 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
1990 connect_ack.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02001991 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_COMPL_IND,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001992 &connect_ack);
1993}
1994
1995static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
1996{
1997 struct msgb *msg = gsm48_msgb_alloc();
1998 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1999
Harald Welte4bfdfe72009-06-10 23:11:52 +08002000 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
2001
2002 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2003
Harald Welte39e2ead2009-07-23 21:13:03 +02002004 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002005}
2006
2007static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
2008{
2009 struct gsm48_hdr *gh = msgb_l3(msg);
2010 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2011 struct tlv_parsed tp;
2012 struct gsm_mncc disc;
2013
2014 gsm48_stop_cc_timer(trans);
2015
2016 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
2017
2018 memset(&disc, 0, sizeof(struct gsm_mncc));
2019 disc.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002020 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002021 /* cause */
2022 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2023 disc.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002024 gsm48_decode_cause(&disc.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002025 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2026 }
2027 /* facility */
2028 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2029 disc.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002030 gsm48_decode_facility(&disc.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002031 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2032 }
2033 /* user-user */
2034 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2035 disc.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002036 gsm48_decode_useruser(&disc.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002037 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2038 }
2039 /* ss-version */
2040 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2041 disc.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002042 gsm48_decode_ssversion(&disc.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002043 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2044 }
2045
Harald Welte596fed42009-07-23 19:06:52 +02002046 return mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &disc);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002047
2048}
2049
Harald Weltec66b71c2009-06-11 14:23:20 +08002050static struct gsm_mncc_cause default_cause = {
2051 .location = GSM48_CAUSE_LOC_PRN_S_LU,
2052 .coding = 0,
2053 .rec = 0,
2054 .rec_val = 0,
2055 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
2056 .diag_len = 0,
2057 .diag = { 0 },
2058};
Harald Welte4bfdfe72009-06-10 23:11:52 +08002059
2060static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
2061{
2062 struct gsm_mncc *disc = arg;
2063 struct msgb *msg = gsm48_msgb_alloc();
2064 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2065
Harald Welte4bfdfe72009-06-10 23:11:52 +08002066 gh->msg_type = GSM48_MT_CC_DISCONNECT;
2067
2068 gsm48_stop_cc_timer(trans);
2069 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
2070
2071 /* cause */
2072 if (disc->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002073 gsm48_encode_cause(msg, 1, &disc->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002074 else
Harald Welte55c8f352010-03-07 23:40:35 +01002075 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002076
2077 /* facility */
2078 if (disc->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002079 gsm48_encode_facility(msg, 0, &disc->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002080 /* progress */
2081 if (disc->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002082 gsm48_encode_progress(msg, 0, &disc->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002083 /* user-user */
2084 if (disc->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002085 gsm48_encode_useruser(msg, 0, &disc->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002086
2087 /* store disconnect cause for T306 expiry */
Harald Weltedcaf5652009-07-23 18:56:43 +02002088 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002089
2090 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
2091
Harald Welte39e2ead2009-07-23 21:13:03 +02002092 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002093}
2094
2095static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
2096{
2097 struct gsm48_hdr *gh = msgb_l3(msg);
2098 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2099 struct tlv_parsed tp;
2100 struct gsm_mncc rel;
2101 int rc;
2102
2103 gsm48_stop_cc_timer(trans);
2104
2105 memset(&rel, 0, sizeof(struct gsm_mncc));
2106 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002107 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002108 /* cause */
2109 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2110 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002111 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002112 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2113 }
2114 /* facility */
2115 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2116 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002117 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002118 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2119 }
2120 /* user-user */
2121 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2122 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002123 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002124 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2125 }
2126 /* ss-version */
2127 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2128 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002129 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002130 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2131 }
2132
Harald Weltedcaf5652009-07-23 18:56:43 +02002133 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002134 /* release collision 5.4.5 */
Harald Welte596fed42009-07-23 19:06:52 +02002135 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_CNF, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002136 } else {
Harald Welte596fed42009-07-23 19:06:52 +02002137 rc = gsm48_tx_simple(msg->lchan,
Harald Welte6f5aee02009-07-23 21:21:14 +02002138 GSM48_PDISC_CC | (trans->transaction_id << 4),
Harald Welte596fed42009-07-23 19:06:52 +02002139 GSM48_MT_CC_RELEASE_COMPL);
2140 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_IND, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002141 }
2142
2143 new_cc_state(trans, GSM_CSTATE_NULL);
2144
2145 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002146 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002147
2148 return rc;
2149}
2150
2151static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
2152{
2153 struct gsm_mncc *rel = arg;
2154 struct msgb *msg = gsm48_msgb_alloc();
2155 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2156
Harald Welte4bfdfe72009-06-10 23:11:52 +08002157 gh->msg_type = GSM48_MT_CC_RELEASE;
2158
2159 trans->callref = 0;
2160
2161 gsm48_stop_cc_timer(trans);
2162 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
2163
2164 /* cause */
2165 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002166 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002167 /* facility */
2168 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002169 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002170 /* user-user */
2171 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002172 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002173
Harald Weltedcaf5652009-07-23 18:56:43 +02002174 trans->cc.T308_second = 0;
2175 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002176
Harald Weltedcaf5652009-07-23 18:56:43 +02002177 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002178 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
2179
Harald Welte39e2ead2009-07-23 21:13:03 +02002180 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002181}
2182
2183static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
2184{
2185 struct gsm48_hdr *gh = msgb_l3(msg);
2186 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2187 struct tlv_parsed tp;
2188 struct gsm_mncc rel;
2189 int rc = 0;
2190
2191 gsm48_stop_cc_timer(trans);
2192
2193 memset(&rel, 0, sizeof(struct gsm_mncc));
2194 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002195 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002196 /* cause */
2197 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2198 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002199 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002200 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2201 }
2202 /* facility */
2203 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2204 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002205 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002206 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2207 }
2208 /* user-user */
2209 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2210 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002211 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002212 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2213 }
2214 /* ss-version */
2215 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2216 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002217 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002218 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2219 }
2220
2221 if (trans->callref) {
Harald Weltedcaf5652009-07-23 18:56:43 +02002222 switch (trans->cc.state) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002223 case GSM_CSTATE_CALL_PRESENT:
Harald Welte596fed42009-07-23 19:06:52 +02002224 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002225 MNCC_REJ_IND, &rel);
2226 break;
2227 case GSM_CSTATE_RELEASE_REQ:
Harald Welte596fed42009-07-23 19:06:52 +02002228 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002229 MNCC_REL_CNF, &rel);
Harald Welteb83d9382009-12-12 21:00:48 +01002230 /* FIXME: in case of multiple calls, we can't simply
2231 * hang up here ! */
2232 lchan_auto_release(msg->lchan);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002233 break;
2234 default:
Harald Welte596fed42009-07-23 19:06:52 +02002235 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002236 MNCC_REL_IND, &rel);
2237 }
2238 }
2239
2240 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002241 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002242
2243 return rc;
2244}
2245
2246static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
2247{
2248 struct gsm_mncc *rel = arg;
2249 struct msgb *msg = gsm48_msgb_alloc();
2250 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2251
Harald Welte4bfdfe72009-06-10 23:11:52 +08002252 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
2253
2254 trans->callref = 0;
2255
2256 gsm48_stop_cc_timer(trans);
2257
2258 /* cause */
2259 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002260 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002261 /* facility */
2262 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002263 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002264 /* user-user */
2265 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002266 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002267
Harald Weltedcaf5652009-07-23 18:56:43 +02002268 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002269
Harald Welte39e2ead2009-07-23 21:13:03 +02002270 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002271}
2272
2273static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
2274{
2275 struct gsm48_hdr *gh = msgb_l3(msg);
2276 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2277 struct tlv_parsed tp;
2278 struct gsm_mncc fac;
2279
2280 memset(&fac, 0, sizeof(struct gsm_mncc));
2281 fac.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002282 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002283 /* facility */
2284 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2285 fac.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002286 gsm48_decode_facility(&fac.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002287 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2288 }
2289 /* ss-version */
2290 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2291 fac.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002292 gsm48_decode_ssversion(&fac.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002293 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2294 }
2295
Harald Welte596fed42009-07-23 19:06:52 +02002296 return mncc_recvmsg(trans->subscr->net, trans, MNCC_FACILITY_IND, &fac);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002297}
2298
2299static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
2300{
2301 struct gsm_mncc *fac = arg;
2302 struct msgb *msg = gsm48_msgb_alloc();
2303 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2304
Harald Welte4bfdfe72009-06-10 23:11:52 +08002305 gh->msg_type = GSM48_MT_CC_FACILITY;
2306
2307 /* facility */
Harald Welte55c8f352010-03-07 23:40:35 +01002308 gsm48_encode_facility(msg, 1, &fac->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002309
Harald Welte39e2ead2009-07-23 21:13:03 +02002310 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002311}
2312
2313static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
2314{
2315 struct gsm_mncc hold;
2316
2317 memset(&hold, 0, sizeof(struct gsm_mncc));
2318 hold.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02002319 return mncc_recvmsg(trans->subscr->net, trans, MNCC_HOLD_IND, &hold);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002320}
2321
2322static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
2323{
2324 struct msgb *msg = gsm48_msgb_alloc();
2325 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2326
Harald Welte4bfdfe72009-06-10 23:11:52 +08002327 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
2328
Harald Welte39e2ead2009-07-23 21:13:03 +02002329 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002330}
2331
2332static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
2333{
2334 struct gsm_mncc *hold_rej = arg;
2335 struct msgb *msg = gsm48_msgb_alloc();
2336 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2337
Harald Welte4bfdfe72009-06-10 23:11:52 +08002338 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
2339
2340 /* cause */
2341 if (hold_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002342 gsm48_encode_cause(msg, 1, &hold_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002343 else
Harald Welte55c8f352010-03-07 23:40:35 +01002344 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002345
Harald Welte39e2ead2009-07-23 21:13:03 +02002346 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002347}
2348
2349static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
2350{
2351 struct gsm_mncc retrieve;
2352
2353 memset(&retrieve, 0, sizeof(struct gsm_mncc));
2354 retrieve.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02002355 return mncc_recvmsg(trans->subscr->net, trans, MNCC_RETRIEVE_IND,
2356 &retrieve);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002357}
2358
2359static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
2360{
2361 struct msgb *msg = gsm48_msgb_alloc();
2362 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2363
Harald Welte4bfdfe72009-06-10 23:11:52 +08002364 gh->msg_type = GSM48_MT_CC_RETR_ACK;
2365
Harald Welte39e2ead2009-07-23 21:13:03 +02002366 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002367}
2368
2369static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
2370{
2371 struct gsm_mncc *retrieve_rej = arg;
2372 struct msgb *msg = gsm48_msgb_alloc();
2373 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2374
Harald Welte4bfdfe72009-06-10 23:11:52 +08002375 gh->msg_type = GSM48_MT_CC_RETR_REJ;
2376
2377 /* cause */
2378 if (retrieve_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002379 gsm48_encode_cause(msg, 1, &retrieve_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002380 else
Harald Welte55c8f352010-03-07 23:40:35 +01002381 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002382
Harald Welte39e2ead2009-07-23 21:13:03 +02002383 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002384}
2385
2386static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
2387{
2388 struct gsm48_hdr *gh = msgb_l3(msg);
2389 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2390 struct tlv_parsed tp;
2391 struct gsm_mncc dtmf;
2392
2393 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2394 dtmf.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002395 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002396 /* keypad facility */
2397 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
2398 dtmf.fields |= MNCC_F_KEYPAD;
Harald Welte55c8f352010-03-07 23:40:35 +01002399 gsm48_decode_keypad(&dtmf.keypad,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002400 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
2401 }
2402
Harald Welte596fed42009-07-23 19:06:52 +02002403 return mncc_recvmsg(trans->subscr->net, trans, MNCC_START_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002404}
2405
2406static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
2407{
2408 struct gsm_mncc *dtmf = arg;
2409 struct msgb *msg = gsm48_msgb_alloc();
2410 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2411
Harald Welte4bfdfe72009-06-10 23:11:52 +08002412 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
2413
2414 /* keypad */
2415 if (dtmf->fields & MNCC_F_KEYPAD)
Harald Welte55c8f352010-03-07 23:40:35 +01002416 gsm48_encode_keypad(msg, dtmf->keypad);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002417
Harald Welte39e2ead2009-07-23 21:13:03 +02002418 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002419}
2420
2421static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
2422{
2423 struct gsm_mncc *dtmf = arg;
2424 struct msgb *msg = gsm48_msgb_alloc();
2425 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2426
Harald Welte4bfdfe72009-06-10 23:11:52 +08002427 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
2428
2429 /* cause */
2430 if (dtmf->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002431 gsm48_encode_cause(msg, 1, &dtmf->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002432 else
Harald Welte55c8f352010-03-07 23:40:35 +01002433 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002434
Harald Welte39e2ead2009-07-23 21:13:03 +02002435 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002436}
2437
2438static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
2439{
2440 struct msgb *msg = gsm48_msgb_alloc();
2441 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2442
Harald Welte4bfdfe72009-06-10 23:11:52 +08002443 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
2444
Harald Welte39e2ead2009-07-23 21:13:03 +02002445 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002446}
2447
2448static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
2449{
2450 struct gsm_mncc dtmf;
2451
2452 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2453 dtmf.callref = trans->callref;
2454
Harald Welte596fed42009-07-23 19:06:52 +02002455 return mncc_recvmsg(trans->subscr->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002456}
2457
2458static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
2459{
2460 struct gsm48_hdr *gh = msgb_l3(msg);
2461 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2462 struct tlv_parsed tp;
2463 struct gsm_mncc modify;
2464
2465 memset(&modify, 0, sizeof(struct gsm_mncc));
2466 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002467 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002468 /* bearer capability */
2469 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2470 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002471 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002472 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2473 }
2474
2475 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
2476
Harald Welte596fed42009-07-23 19:06:52 +02002477 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_IND, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002478}
2479
2480static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
2481{
2482 struct gsm_mncc *modify = arg;
2483 struct msgb *msg = gsm48_msgb_alloc();
2484 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2485
Harald Welte4bfdfe72009-06-10 23:11:52 +08002486 gh->msg_type = GSM48_MT_CC_MODIFY;
2487
2488 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
2489
2490 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002491 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002492
2493 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
2494
Harald Welte39e2ead2009-07-23 21:13:03 +02002495 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002496}
2497
2498static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
2499{
2500 struct gsm48_hdr *gh = msgb_l3(msg);
2501 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2502 struct tlv_parsed tp;
2503 struct gsm_mncc modify;
2504
2505 gsm48_stop_cc_timer(trans);
2506
2507 memset(&modify, 0, sizeof(struct gsm_mncc));
2508 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002509 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002510 /* bearer capability */
2511 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2512 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002513 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002514 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2515 }
2516
2517 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2518
Harald Welte596fed42009-07-23 19:06:52 +02002519 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_CNF, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002520}
2521
2522static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
2523{
2524 struct gsm_mncc *modify = arg;
2525 struct msgb *msg = gsm48_msgb_alloc();
2526 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2527
Harald Welte4bfdfe72009-06-10 23:11:52 +08002528 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
2529
2530 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002531 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002532
2533 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2534
Harald Welte39e2ead2009-07-23 21:13:03 +02002535 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002536}
2537
2538static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
2539{
2540 struct gsm48_hdr *gh = msgb_l3(msg);
2541 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2542 struct tlv_parsed tp;
2543 struct gsm_mncc modify;
2544
2545 gsm48_stop_cc_timer(trans);
2546
2547 memset(&modify, 0, sizeof(struct gsm_mncc));
2548 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002549 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002550 /* bearer capability */
2551 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2552 modify.fields |= GSM48_IE_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002553 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002554 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2555 }
2556 /* cause */
2557 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2558 modify.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002559 gsm48_decode_cause(&modify.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002560 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2561 }
2562
2563 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2564
Harald Welte596fed42009-07-23 19:06:52 +02002565 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_REJ, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002566}
2567
2568static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
2569{
2570 struct gsm_mncc *modify = arg;
2571 struct msgb *msg = gsm48_msgb_alloc();
2572 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2573
Harald Welte4bfdfe72009-06-10 23:11:52 +08002574 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
2575
2576 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002577 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002578 /* cause */
Harald Welte55c8f352010-03-07 23:40:35 +01002579 gsm48_encode_cause(msg, 1, &modify->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002580
2581 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2582
Harald Welte39e2ead2009-07-23 21:13:03 +02002583 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002584}
2585
2586static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
2587{
2588 struct gsm_mncc *notify = arg;
2589 struct msgb *msg = gsm48_msgb_alloc();
2590 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2591
Harald Welte4bfdfe72009-06-10 23:11:52 +08002592 gh->msg_type = GSM48_MT_CC_NOTIFY;
2593
2594 /* notify */
Harald Welte55c8f352010-03-07 23:40:35 +01002595 gsm48_encode_notify(msg, notify->notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002596
Harald Welte39e2ead2009-07-23 21:13:03 +02002597 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002598}
2599
2600static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
2601{
2602 struct gsm48_hdr *gh = msgb_l3(msg);
2603 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2604// struct tlv_parsed tp;
2605 struct gsm_mncc notify;
2606
2607 memset(&notify, 0, sizeof(struct gsm_mncc));
2608 notify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002609// tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002610 if (payload_len >= 1)
Harald Welte55c8f352010-03-07 23:40:35 +01002611 gsm48_decode_notify(&notify.notify, gh->data);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002612
Harald Welte596fed42009-07-23 19:06:52 +02002613 return mncc_recvmsg(trans->subscr->net, trans, MNCC_NOTIFY_IND, &notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002614}
2615
2616static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
2617{
2618 struct gsm_mncc *user = arg;
2619 struct msgb *msg = gsm48_msgb_alloc();
2620 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2621
Harald Welte4bfdfe72009-06-10 23:11:52 +08002622 gh->msg_type = GSM48_MT_CC_USER_INFO;
2623
2624 /* user-user */
2625 if (user->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002626 gsm48_encode_useruser(msg, 1, &user->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002627 /* more data */
2628 if (user->more)
Harald Welte55c8f352010-03-07 23:40:35 +01002629 gsm48_encode_more(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002630
Harald Welte39e2ead2009-07-23 21:13:03 +02002631 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002632}
2633
2634static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
2635{
2636 struct gsm48_hdr *gh = msgb_l3(msg);
2637 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2638 struct tlv_parsed tp;
2639 struct gsm_mncc user;
2640
2641 memset(&user, 0, sizeof(struct gsm_mncc));
2642 user.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002643 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002644 /* user-user */
2645 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2646 user.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002647 gsm48_decode_useruser(&user.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002648 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2649 }
2650 /* more data */
2651 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
2652 user.more = 1;
2653
Harald Welte596fed42009-07-23 19:06:52 +02002654 return mncc_recvmsg(trans->subscr->net, trans, MNCC_USERINFO_IND, &user);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002655}
2656
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +02002657static int _gsm48_lchan_modify(struct gsm_trans *trans, void *arg)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002658{
2659 struct gsm_mncc *mode = arg;
2660
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002661 return gsm48_lchan_modify(trans->conn->lchan, mode->lchan_mode);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002662}
2663
2664static struct downstate {
2665 u_int32_t states;
2666 int type;
2667 int (*rout) (struct gsm_trans *trans, void *arg);
2668} downstatelist[] = {
2669 /* mobile originating call establishment */
2670 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
2671 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc},
2672 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
2673 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
2674 {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 */
2675 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
2676 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
2677 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
2678 /* mobile terminating call establishment */
2679 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
2680 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
2681 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
2682 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
2683 /* signalling during call */
2684 {SBIT(GSM_CSTATE_ACTIVE),
2685 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
2686 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
2687 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
2688 {ALL_STATES,
2689 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
2690 {ALL_STATES,
2691 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
2692 {ALL_STATES,
2693 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
2694 {SBIT(GSM_CSTATE_ACTIVE),
2695 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
2696 {SBIT(GSM_CSTATE_ACTIVE),
2697 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
2698 {SBIT(GSM_CSTATE_ACTIVE),
2699 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
2700 {SBIT(GSM_CSTATE_ACTIVE),
2701 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
2702 {SBIT(GSM_CSTATE_ACTIVE),
2703 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
2704 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2705 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
2706 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2707 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
2708 {SBIT(GSM_CSTATE_ACTIVE),
2709 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
2710 /* clearing */
2711 {SBIT(GSM_CSTATE_INITIATED),
2712 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
2713 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
2714 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
2715 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
2716 MNCC_REL_REQ, gsm48_cc_tx_release},
2717 /* special */
2718 {ALL_STATES,
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +02002719 MNCC_LCHAN_MODIFY, _gsm48_lchan_modify},
Harald Welte4bfdfe72009-06-10 23:11:52 +08002720};
2721
2722#define DOWNSLLEN \
2723 (sizeof(downstatelist) / sizeof(struct downstate))
2724
2725
2726int mncc_send(struct gsm_network *net, int msg_type, void *arg)
2727{
Harald Welte1a6f7982009-08-09 18:52:33 +02002728 int i, rc = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002729 struct gsm_trans *trans = NULL, *transt;
Harald Welte1a6f7982009-08-09 18:52:33 +02002730 struct gsm_lchan *lchan = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002731 struct gsm_bts *bts = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002732 struct gsm_mncc *data = arg, rel;
2733
2734 /* handle special messages */
2735 switch(msg_type) {
2736 case MNCC_BRIDGE:
2737 return tch_bridge(net, arg);
2738 case MNCC_FRAME_DROP:
Harald Welteda7ab742009-12-19 22:23:05 +01002739 return tch_recv_mncc(net, data->callref, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002740 case MNCC_FRAME_RECV:
Harald Welteda7ab742009-12-19 22:23:05 +01002741 return tch_recv_mncc(net, data->callref, 1);
2742 case GSM_TCHF_FRAME:
2743 /* Find callref */
2744 trans = trans_find_by_callref(net, data->callref);
2745 if (!trans)
2746 return -EIO;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002747 if (!trans->conn)
Harald Welteda7ab742009-12-19 22:23:05 +01002748 return 0;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002749 if (trans->conn->lchan->type != GSM_LCHAN_TCH_F)
Harald Welteda7ab742009-12-19 22:23:05 +01002750 return 0;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002751 bts = trans->conn->lchan->ts->trx->bts;
Harald Welteda7ab742009-12-19 22:23:05 +01002752 switch (bts->type) {
2753 case GSM_BTS_TYPE_NANOBTS:
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002754 if (!trans->conn->lchan->abis_ip.rtp_socket)
Harald Welteda7ab742009-12-19 22:23:05 +01002755 return 0;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002756 return rtp_send_frame(trans->conn->lchan->abis_ip.rtp_socket, arg);
Harald Welteda7ab742009-12-19 22:23:05 +01002757 case GSM_BTS_TYPE_BS11:
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002758 return trau_send_frame(trans->conn->lchan, arg);
Harald Welteda7ab742009-12-19 22:23:05 +01002759 default:
2760 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
2761 }
2762 return -EINVAL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002763 }
2764
2765 memset(&rel, 0, sizeof(struct gsm_mncc));
2766 rel.callref = data->callref;
2767
2768 /* Find callref */
Harald Weltedcaf5652009-07-23 18:56:43 +02002769 trans = trans_find_by_callref(net, data->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002770
2771 /* Callref unknown */
2772 if (!trans) {
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01002773 struct gsm_subscriber *subscr;
2774
Harald Welte4a3464c2009-07-04 10:11:24 +02002775 if (msg_type != MNCC_SETUP_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002776 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2777 "Received '%s' from MNCC with "
2778 "unknown callref %d\n", data->called.number,
2779 get_mncc_name(msg_type), data->callref);
2780 /* Invalid call reference */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002781 return mncc_release_ind(net, NULL, data->callref,
2782 GSM48_CAUSE_LOC_PRN_S_LU,
2783 GSM48_CC_CAUSE_INVAL_TRANS_ID);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002784 }
Andreas Eversbergc079be42009-06-15 23:22:09 +02002785 if (!data->called.number[0] && !data->imsi[0]) {
2786 DEBUGP(DCC, "(bts - trx - ts - ti) "
2787 "Received '%s' from MNCC with "
2788 "no number or IMSI\n", get_mncc_name(msg_type));
2789 /* Invalid number */
2790 return mncc_release_ind(net, NULL, data->callref,
2791 GSM48_CAUSE_LOC_PRN_S_LU,
2792 GSM48_CC_CAUSE_INV_NR_FORMAT);
2793 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08002794 /* New transaction due to setup, find subscriber */
Andreas Eversbergc079be42009-06-15 23:22:09 +02002795 if (data->called.number[0])
Harald Welte9176bd42009-07-23 18:46:00 +02002796 subscr = subscr_get_by_extension(net,
2797 data->called.number);
Andreas Eversbergc079be42009-06-15 23:22:09 +02002798 else
Harald Welte9176bd42009-07-23 18:46:00 +02002799 subscr = subscr_get_by_imsi(net, data->imsi);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002800 /* If subscriber is not found */
2801 if (!subscr) {
2802 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2803 "Received '%s' from MNCC with "
2804 "unknown subscriber %s\n", data->called.number,
2805 get_mncc_name(msg_type), data->called.number);
2806 /* Unknown subscriber */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002807 return mncc_release_ind(net, NULL, data->callref,
2808 GSM48_CAUSE_LOC_PRN_S_LU,
2809 GSM48_CC_CAUSE_UNASSIGNED_NR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002810 }
2811 /* If subscriber is not "attached" */
2812 if (!subscr->lac) {
2813 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2814 "Received '%s' from MNCC with "
2815 "detached subscriber %s\n", data->called.number,
2816 get_mncc_name(msg_type), data->called.number);
2817 subscr_put(subscr);
2818 /* Temporarily out of order */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002819 return mncc_release_ind(net, NULL, data->callref,
2820 GSM48_CAUSE_LOC_PRN_S_LU,
2821 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002822 }
2823 /* Create transaction */
Harald Weltedcaf5652009-07-23 18:56:43 +02002824 trans = trans_alloc(subscr, GSM48_PDISC_CC, 0xff, data->callref);
2825 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002826 DEBUGP(DCC, "No memory for trans.\n");
2827 subscr_put(subscr);
2828 /* Ressource unavailable */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002829 mncc_release_ind(net, NULL, data->callref,
2830 GSM48_CAUSE_LOC_PRN_S_LU,
2831 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002832 return -ENOMEM;
2833 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08002834 /* Find lchan */
Harald Welte1a6f7982009-08-09 18:52:33 +02002835 lchan = lchan_for_subscr(subscr);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01002836
Harald Welte4bfdfe72009-06-10 23:11:52 +08002837 /* If subscriber has no lchan */
2838 if (!lchan) {
2839 /* find transaction with this subscriber already paging */
2840 llist_for_each_entry(transt, &net->trans_list, entry) {
2841 /* Transaction of our lchan? */
2842 if (transt == trans ||
2843 transt->subscr != subscr)
2844 continue;
2845 DEBUGP(DCC, "(bts %d trx - ts - ti -- sub %s) "
2846 "Received '%s' from MNCC with "
2847 "unallocated channel, paging already "
2848 "started.\n", bts->nr,
2849 data->called.number,
2850 get_mncc_name(msg_type));
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01002851 subscr_put(subscr);
2852 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002853 return 0;
2854 }
2855 /* store setup informations until paging was successfull */
Harald Weltedcaf5652009-07-23 18:56:43 +02002856 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
Harald Weltea1b28582009-08-01 19:31:47 +02002857 /* Trigger paging */
2858 paging_request(net, subscr, RSL_CHANNEED_TCH_F,
2859 setup_trig_pag_evt, subscr);
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01002860 subscr_put(subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002861 return 0;
2862 }
2863 /* Assign lchan */
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002864 trans->conn = &lchan->conn;
2865 use_subscr_con(trans->conn);
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01002866 subscr_put(subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002867 }
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002868
2869 if (trans->conn)
2870 lchan = trans->conn->lchan;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002871
2872 /* if paging did not respond yet */
2873 if (!lchan) {
2874 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02002875 "Received '%s' from MNCC in paging state\n",
Harald Welte4bfdfe72009-06-10 23:11:52 +08002876 (trans->subscr)?(trans->subscr->extension):"-",
2877 get_mncc_name(msg_type));
Harald Weltec66b71c2009-06-11 14:23:20 +08002878 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
2879 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002880 if (msg_type == MNCC_REL_REQ)
2881 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
2882 else
2883 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
2884 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002885 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002886 return rc;
2887 }
2888
2889 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x sub %s) "
2890 "Received '%s' from MNCC in state %d (%s)\n",
2891 lchan->ts->trx->bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
2892 trans->transaction_id,
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002893 (trans->conn->subscr)?(trans->conn->subscr->extension):"-",
Harald Weltedcaf5652009-07-23 18:56:43 +02002894 get_mncc_name(msg_type), trans->cc.state,
Harald Weltee95daf12010-03-25 12:13:02 +08002895 gsm48_cc_state_name(trans->cc.state));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002896
2897 /* Find function for current state and message */
2898 for (i = 0; i < DOWNSLLEN; i++)
2899 if ((msg_type == downstatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02002900 && ((1 << trans->cc.state) & downstatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08002901 break;
2902 if (i == DOWNSLLEN) {
2903 DEBUGP(DCC, "Message unhandled at this state.\n");
2904 return 0;
2905 }
2906
2907 rc = downstatelist[i].rout(trans, arg);
2908
2909 return rc;
2910}
2911
2912
2913static struct datastate {
2914 u_int32_t states;
2915 int type;
2916 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
2917} datastatelist[] = {
2918 /* mobile originating call establishment */
2919 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
2920 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
2921 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
2922 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
2923 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
2924 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
2925 /* mobile terminating call establishment */
2926 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
2927 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
2928 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
2929 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02002930 {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 +08002931 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
2932 /* signalling during call */
2933 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
2934 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
2935 {SBIT(GSM_CSTATE_ACTIVE),
2936 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
2937 {ALL_STATES,
2938 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
2939 {ALL_STATES,
2940 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
2941 {ALL_STATES,
2942 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
2943 {SBIT(GSM_CSTATE_ACTIVE),
2944 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
2945 {SBIT(GSM_CSTATE_ACTIVE),
2946 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
2947 {SBIT(GSM_CSTATE_ACTIVE),
2948 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
2949 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
2950 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
2951 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
2952 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
2953 {SBIT(GSM_CSTATE_ACTIVE),
2954 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
2955 /* clearing */
2956 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
2957 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
2958 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
2959 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
2960 {ALL_STATES, /* 5.4.3.4 */
2961 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
2962};
2963
2964#define DATASLLEN \
2965 (sizeof(datastatelist) / sizeof(struct datastate))
2966
Harald Welte4bc90a12008-12-27 16:32:52 +00002967static int gsm0408_rcv_cc(struct msgb *msg)
2968{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01002969 struct gsm_subscriber_connection *conn;
Harald Welte4bc90a12008-12-27 16:32:52 +00002970 struct gsm48_hdr *gh = msgb_l3(msg);
2971 u_int8_t msg_type = gh->msg_type & 0xbf;
Harald Welte6f5aee02009-07-23 21:21:14 +02002972 u_int8_t transaction_id = ((gh->proto_discr & 0xf0) ^ 0x80) >> 4; /* flip */
Harald Welte4bfdfe72009-06-10 23:11:52 +08002973 struct gsm_lchan *lchan = msg->lchan;
Harald Weltedcaf5652009-07-23 18:56:43 +02002974 struct gsm_trans *trans = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002975 int i, rc = 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00002976
Harald Welte4bfdfe72009-06-10 23:11:52 +08002977 if (msg_type & 0x80) {
2978 DEBUGP(DCC, "MSG 0x%2x not defined for PD error\n", msg_type);
2979 return -EINVAL;
Harald Welte4bc90a12008-12-27 16:32:52 +00002980 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01002981
2982 conn = &lchan->conn;
2983
Harald Welte4bfdfe72009-06-10 23:11:52 +08002984 /* Find transaction */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01002985 trans = trans_find_by_id(conn->subscr, GSM48_PDISC_CC, transaction_id);
Harald Weltedcaf5652009-07-23 18:56:43 +02002986
Harald Welte6f5aee02009-07-23 21:21:14 +02002987 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08002988 "Received '%s' from MS in state %d (%s)\n",
2989 lchan->ts->trx->bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01002990 transaction_id, (conn->subscr)?(conn->subscr->extension):"-",
Harald Weltee95daf12010-03-25 12:13:02 +08002991 gsm48_cc_msg_name(msg_type), trans?(trans->cc.state):0,
2992 gsm48_cc_state_name(trans?(trans->cc.state):0));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002993
2994 /* Create transaction */
2995 if (!trans) {
Harald Welte6f5aee02009-07-23 21:21:14 +02002996 DEBUGP(DCC, "Unknown transaction ID %x, "
Harald Welte4bfdfe72009-06-10 23:11:52 +08002997 "creating new trans.\n", transaction_id);
2998 /* Create transaction */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01002999 trans = trans_alloc(conn->subscr, GSM48_PDISC_CC,
Harald Weltedcaf5652009-07-23 18:56:43 +02003000 transaction_id, new_callref++);
3001 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003002 DEBUGP(DCC, "No memory for trans.\n");
3003 rc = gsm48_tx_simple(msg->lchan,
Harald Welte6f5aee02009-07-23 21:21:14 +02003004 GSM48_PDISC_CC | (transaction_id << 4),
Harald Welte4bfdfe72009-06-10 23:11:52 +08003005 GSM48_MT_CC_RELEASE_COMPL);
3006 return -ENOMEM;
3007 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003008 /* Assign transaction */
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003009 trans->conn = &lchan->conn;
3010 use_subscr_con(trans->conn);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003011 }
3012
3013 /* find function for current state and message */
3014 for (i = 0; i < DATASLLEN; i++)
3015 if ((msg_type == datastatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003016 && ((1 << trans->cc.state) & datastatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003017 break;
3018 if (i == DATASLLEN) {
3019 DEBUGP(DCC, "Message unhandled at this state.\n");
3020 return 0;
3021 }
3022
3023 rc = datastatelist[i].rout(trans, msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00003024
3025 return rc;
3026}
3027
Harald Welte52b1f982008-12-23 20:25:15 +00003028/* 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 +02003029int gsm0408_rcvmsg(struct msgb *msg, u_int8_t link_id)
Harald Welte52b1f982008-12-23 20:25:15 +00003030{
3031 struct gsm48_hdr *gh = msgb_l3(msg);
3032 u_int8_t pdisc = gh->proto_discr & 0x0f;
Harald Welte8470bf22008-12-25 23:28:35 +00003033 int rc = 0;
Harald Welte51008772009-12-29 11:49:12 +01003034
3035 if (silent_call_reroute(msg))
3036 return silent_call_rx(msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003037
3038 switch (pdisc) {
3039 case GSM48_PDISC_CC:
3040 rc = gsm0408_rcv_cc(msg);
3041 break;
3042 case GSM48_PDISC_MM:
3043 rc = gsm0408_rcv_mm(msg);
3044 break;
3045 case GSM48_PDISC_RR:
3046 rc = gsm0408_rcv_rr(msg);
3047 break;
Harald Weltebcae43f2008-12-27 21:45:37 +00003048 case GSM48_PDISC_SMS:
Harald Welte (local)daef6062009-08-14 11:41:12 +02003049 rc = gsm0411_rcv_sms(msg, link_id);
Harald Weltebcae43f2008-12-27 21:45:37 +00003050 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003051 case GSM48_PDISC_MM_GPRS:
Harald Weltebcae43f2008-12-27 21:45:37 +00003052 case GSM48_PDISC_SM_GPRS:
Harald Welte5d24ba12009-12-24 12:13:17 +01003053 LOGP(DRLL, LOGL_NOTICE, "Unimplemented "
3054 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Harald Welte52b1f982008-12-23 20:25:15 +00003055 break;
Harald Welte6eafe912009-10-16 08:32:58 +02003056 case GSM48_PDISC_NC_SS:
3057 rc = handle_rcv_ussd(msg);
3058 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003059 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01003060 LOGP(DRLL, LOGL_NOTICE, "Unknown "
3061 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Harald Welte52b1f982008-12-23 20:25:15 +00003062 break;
3063 }
3064
3065 return rc;
3066}
Harald Welte8470bf22008-12-25 23:28:35 +00003067
Harald Welte805f6442009-07-28 18:25:29 +02003068/*
3069 * This will be ran by the linker when loading the DSO. We use it to
3070 * do system initialization, e.g. registration of signal handlers.
3071 */
3072static __attribute__((constructor)) void on_dso_load_0408(void)
3073{
Harald Welte805f6442009-07-28 18:25:29 +02003074 register_signal_handler(SS_LCHAN, gsm0408_handle_lchan_signal, NULL);
3075 register_signal_handler(SS_ABISIP, handle_abisip_signal, NULL);
3076}