blob: 2563158529e62a896f8533442509b7632f33e507 [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
Harald Welte52b1f982008-12-23 20:25:15 +0000465 switch (mi_type) {
466 case GSM_MI_TYPE_IMSI:
Harald Weltea0368542009-06-27 02:58:43 +0200467 DEBUGPC(DMM, "\n");
Harald Welte231ad4f2008-12-27 11:15:38 +0000468 /* we always want the IMEI, too */
Harald Welte015b9ad2009-02-28 18:22:03 +0000469 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMEI);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100470 conn->loc_operation->waiting_for_imei = 1;
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000471
Jan Luebbe370b41d2009-08-12 10:19:34 +0200472 /* look up subscriber based on IMSI, create if not found */
473 subscr = subscr_get_by_imsi(bts->network, mi_string);
474 if (!subscr) {
475 subscr = db_create_subscriber(bts->network, mi_string);
476 }
Harald Welte4b634542008-12-27 01:55:51 +0000477 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000478 case GSM_MI_TYPE_TMSI:
Harald Weltea0368542009-06-27 02:58:43 +0200479 DEBUGPC(DMM, "\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000480 /* look up the subscriber based on TMSI, request IMSI if it fails */
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200481 subscr = subscr_get_by_tmsi(bts->network,
482 tmsi_from_string(mi_string));
Harald Welte52b1f982008-12-23 20:25:15 +0000483 if (!subscr) {
Harald Welte231ad4f2008-12-27 11:15:38 +0000484 /* send IDENTITY REQUEST message to get IMSI */
Harald Welte255539c2008-12-28 02:26:27 +0000485 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMSI);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100486 conn->loc_operation->waiting_for_imsi = 1;
Harald Welte52b1f982008-12-23 20:25:15 +0000487 }
Harald Weltef5f512c2010-06-07 17:56:32 +0200488 /* we always want the IMEI, too */
489 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMEI);
490 conn->loc_operation->waiting_for_imei = 1;
Harald Welte52b1f982008-12-23 20:25:15 +0000491 break;
492 case GSM_MI_TYPE_IMEI:
493 case GSM_MI_TYPE_IMEISV:
494 /* no sim card... FIXME: what to do ? */
Harald Weltea0368542009-06-27 02:58:43 +0200495 DEBUGPC(DMM, "unimplemented mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000496 break;
497 default:
Harald Weltea0368542009-06-27 02:58:43 +0200498 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000499 break;
500 }
501
Harald Welte24516ea2009-07-04 10:18:00 +0200502 /* schedule the reject timer */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100503 schedule_reject(conn);
Harald Welte24516ea2009-07-04 10:18:00 +0200504
Harald Welte4bfdfe72009-06-10 23:11:52 +0800505 if (!subscr) {
Harald Weltea0368542009-06-27 02:58:43 +0200506 DEBUGPC(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte4bfdfe72009-06-10 23:11:52 +0800507 /* FIXME: request id? close channel? */
508 return -EINVAL;
509 }
510
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100511 conn->subscr = subscr;
512 conn->subscr->equipment.classmark1 = lu->classmark1;
Harald Welte255539c2008-12-28 02:26:27 +0000513
Harald Welte24516ea2009-07-04 10:18:00 +0200514 /* check if we can let the subscriber into our network immediately
515 * or if we need to wait for identity responses. */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100516 return gsm0408_authorize(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000517}
518
Harald Welte4bfdfe72009-06-10 23:11:52 +0800519#if 0
520static u_int8_t to_bcd8(u_int8_t val)
521{
522 return ((val / 10) << 4) | (val % 10);
523}
524#endif
525
Harald Weltedb253af2008-12-30 17:56:55 +0000526/* Section 9.2.15a */
527int gsm48_tx_mm_info(struct gsm_lchan *lchan)
528{
529 struct msgb *msg = gsm48_msgb_alloc();
530 struct gsm48_hdr *gh;
531 struct gsm_network *net = lchan->ts->trx->bts->network;
Harald Weltedb253af2008-12-30 17:56:55 +0000532 u_int8_t *ptr8;
Daniel Willmanneea93372009-08-13 03:42:07 +0200533 int name_len, name_pad;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800534#if 0
535 time_t cur_t;
536 struct tm* cur_time;
537 int tz15min;
538#endif
Harald Weltedb253af2008-12-30 17:56:55 +0000539
540 msg->lchan = lchan;
541
542 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
543 gh->proto_discr = GSM48_PDISC_MM;
544 gh->msg_type = GSM48_MT_MM_INFO;
545
546 if (net->name_long) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200547#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000548 name_len = strlen(net->name_long);
549 /* 10.5.3.5a */
550 ptr8 = msgb_put(msg, 3);
551 ptr8[0] = GSM48_IE_NAME_LONG;
552 ptr8[1] = name_len*2 +1;
553 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
554
555 ptr16 = (u_int16_t *) msgb_put(msg, name_len*2);
556 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000557 ptr16[i] = htons(net->name_long[i]);
Harald Weltedb253af2008-12-30 17:56:55 +0000558
559 /* FIXME: Use Cell Broadcast, not UCS-2, since
560 * UCS-2 is only supported by later revisions of the spec */
Daniel Willmanneea93372009-08-13 03:42:07 +0200561#endif
562 name_len = (strlen(net->name_long)*7)/8;
563 name_pad = (8 - strlen(net->name_long)*7)%8;
564 if (name_pad > 0)
565 name_len++;
566 /* 10.5.3.5a */
567 ptr8 = msgb_put(msg, 3);
568 ptr8[0] = GSM48_IE_NAME_LONG;
569 ptr8[1] = name_len +1;
570 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
571
572 ptr8 = msgb_put(msg, name_len);
573 gsm_7bit_encode(ptr8, net->name_long);
574
Harald Weltedb253af2008-12-30 17:56:55 +0000575 }
576
577 if (net->name_short) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200578#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000579 name_len = strlen(net->name_short);
580 /* 10.5.3.5a */
581 ptr8 = (u_int8_t *) msgb_put(msg, 3);
Harald Welte7543eb72009-07-19 17:51:36 +0200582 ptr8[0] = GSM48_IE_NAME_SHORT;
Harald Weltedb253af2008-12-30 17:56:55 +0000583 ptr8[1] = name_len*2 + 1;
584 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
585
Harald Weltee872cb12009-01-01 00:33:37 +0000586 ptr16 = (u_int16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000587 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000588 ptr16[i] = htons(net->name_short[i]);
Daniel Willmanneea93372009-08-13 03:42:07 +0200589#endif
590 name_len = (strlen(net->name_short)*7)/8;
591 name_pad = (8 - strlen(net->name_short)*7)%8;
592 if (name_pad > 0)
593 name_len++;
594 /* 10.5.3.5a */
595 ptr8 = (u_int8_t *) msgb_put(msg, 3);
596 ptr8[0] = GSM48_IE_NAME_SHORT;
597 ptr8[1] = name_len +1;
598 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
599
600 ptr8 = msgb_put(msg, name_len);
601 gsm_7bit_encode(ptr8, net->name_short);
602
Harald Weltedb253af2008-12-30 17:56:55 +0000603 }
604
605#if 0
606 /* Section 10.5.3.9 */
607 cur_t = time(NULL);
Harald Welte4bfdfe72009-06-10 23:11:52 +0800608 cur_time = gmtime(&cur_t);
Harald Weltedb253af2008-12-30 17:56:55 +0000609 ptr8 = msgb_put(msg, 8);
610 ptr8[0] = GSM48_IE_NET_TIME_TZ;
611 ptr8[1] = to_bcd8(cur_time->tm_year % 100);
612 ptr8[2] = to_bcd8(cur_time->tm_mon);
613 ptr8[3] = to_bcd8(cur_time->tm_mday);
614 ptr8[4] = to_bcd8(cur_time->tm_hour);
615 ptr8[5] = to_bcd8(cur_time->tm_min);
616 ptr8[6] = to_bcd8(cur_time->tm_sec);
617 /* 02.42: coded as BCD encoded signed value in units of 15 minutes */
618 tz15min = (cur_time->tm_gmtoff)/(60*15);
Harald Welte4bfdfe72009-06-10 23:11:52 +0800619 ptr8[7] = to_bcd8(tz15min);
Harald Weltedb253af2008-12-30 17:56:55 +0000620 if (tz15min < 0)
Harald Welte4bfdfe72009-06-10 23:11:52 +0800621 ptr8[7] |= 0x80;
Harald Weltedb253af2008-12-30 17:56:55 +0000622#endif
623
Daniel Willmanneea93372009-08-13 03:42:07 +0200624 DEBUGP(DMM, "-> MM INFO\n");
625
Harald Welte39e2ead2009-07-23 21:13:03 +0200626 return gsm48_sendmsg(msg, NULL);
Harald Weltedb253af2008-12-30 17:56:55 +0000627}
628
Harald Welte7984d5c2009-08-12 22:56:50 +0200629/* Section 9.2.2 */
Sylvain Munautbd55a6d2009-12-24 00:23:46 +0100630int gsm48_tx_mm_auth_req(struct gsm_lchan *lchan, u_int8_t *rand, int key_seq)
Harald Welte7984d5c2009-08-12 22:56:50 +0200631{
632 struct msgb *msg = gsm48_msgb_alloc();
633 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Sylvain Munaut849f5542009-09-27 11:10:17 +0200634 struct gsm48_auth_req *ar = (struct gsm48_auth_req *) msgb_put(msg, sizeof(*ar));
Harald Welte7984d5c2009-08-12 22:56:50 +0200635
Sylvain Munaut8608e7c2009-12-24 00:24:29 +0100636 DEBUGP(DMM, "-> AUTH REQ (rand = %s)\n", hexdump(rand, 16));
Harald Welte7984d5c2009-08-12 22:56:50 +0200637
638 msg->lchan = lchan;
639 gh->proto_discr = GSM48_PDISC_MM;
640 gh->msg_type = GSM48_MT_MM_AUTH_REQ;
641
Sylvain Munautbd55a6d2009-12-24 00:23:46 +0100642 ar->key_seq = key_seq;
Sylvain Munaut849f5542009-09-27 11:10:17 +0200643
Harald Welte7984d5c2009-08-12 22:56:50 +0200644 /* 16 bytes RAND parameters */
Harald Welte7984d5c2009-08-12 22:56:50 +0200645 if (rand)
Sylvain Munaut849f5542009-09-27 11:10:17 +0200646 memcpy(ar->rand, rand, 16);
Harald Welte7984d5c2009-08-12 22:56:50 +0200647
648 return gsm48_sendmsg(msg, NULL);
649}
650
651/* Section 9.2.1 */
652int gsm48_tx_mm_auth_rej(struct gsm_lchan *lchan)
653{
654 DEBUGP(DMM, "-> AUTH REJECT\n");
655 return gsm48_tx_simple(lchan, GSM48_PDISC_MM, GSM48_MT_MM_AUTH_REJ);
656}
657
Harald Welte4b634542008-12-27 01:55:51 +0000658static int gsm48_tx_mm_serv_ack(struct gsm_lchan *lchan)
659{
Harald Welte4b634542008-12-27 01:55:51 +0000660 DEBUGP(DMM, "-> CM SERVICE ACK\n");
Harald Welte65e74cc2008-12-29 01:55:35 +0000661 return gsm48_tx_simple(lchan, GSM48_PDISC_MM, GSM48_MT_MM_CM_SERV_ACC);
Harald Welte4b634542008-12-27 01:55:51 +0000662}
Harald Welteba4cf162009-01-10 01:49:35 +0000663
664/* 9.2.6 CM service reject */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100665static int gsm48_tx_mm_serv_rej(struct gsm_subscriber_connection *conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000666 enum gsm48_reject_value value)
667{
668 struct msgb *msg = gsm48_msgb_alloc();
669 struct gsm48_hdr *gh;
670
671 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
672
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100673 msg->lchan = conn->lchan;
674 use_subscr_con(conn);
Harald Welteba4cf162009-01-10 01:49:35 +0000675
676 gh->proto_discr = GSM48_PDISC_MM;
677 gh->msg_type = GSM48_MT_MM_CM_SERV_REJ;
678 gh->data[0] = value;
679 DEBUGP(DMM, "-> CM SERVICE Reject cause: %d\n", value);
680
Harald Welte39e2ead2009-07-23 21:13:03 +0200681 return gsm48_sendmsg(msg, NULL);
Harald Welteba4cf162009-01-10 01:49:35 +0000682}
683
Harald Welte4ed0e922009-01-10 03:17:30 +0000684/*
685 * Handle CM Service Requests
686 * a) Verify that the packet is long enough to contain the information
687 * we require otherwsie reject with INCORRECT_MESSAGE
688 * b) Try to parse the TMSI. If we do not have one reject
689 * c) Check that we know the subscriber with the TMSI otherwise reject
690 * with a HLR cause
691 * d) Set the subscriber on the gsm_lchan and accept
692 */
Harald Welte4b634542008-12-27 01:55:51 +0000693static int gsm48_rx_mm_serv_req(struct msgb *msg)
694{
Harald Welteba4cf162009-01-10 01:49:35 +0000695 u_int8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200696 char mi_string[GSM48_MI_SIZE];
Harald Welte4b634542008-12-27 01:55:51 +0000697
Harald Welte9176bd42009-07-23 18:46:00 +0200698 struct gsm_bts *bts = msg->lchan->ts->trx->bts;
Harald Welteba4cf162009-01-10 01:49:35 +0000699 struct gsm_subscriber *subscr;
700 struct gsm48_hdr *gh = msgb_l3(msg);
701 struct gsm48_service_request *req =
702 (struct gsm48_service_request *)gh->data;
Holger Hans Peter Freyther5d658062010-05-14 08:02:08 +0800703 /* unfortunately in Phase1 the classmark2 length is variable */
Harald Weltec9e02182009-05-01 19:07:53 +0000704 u_int8_t classmark2_len = gh->data[1];
705 u_int8_t *classmark2 = gh->data+2;
706 u_int8_t mi_len = *(classmark2 + classmark2_len);
707 u_int8_t *mi = (classmark2 + classmark2_len + 1);
Harald Welteba4cf162009-01-10 01:49:35 +0000708
Harald Weltec9e02182009-05-01 19:07:53 +0000709 DEBUGP(DMM, "<- CM SERVICE REQUEST ");
Harald Welteba4cf162009-01-10 01:49:35 +0000710 if (msg->data_len < sizeof(struct gsm48_service_request*)) {
Harald Weltec9e02182009-05-01 19:07:53 +0000711 DEBUGPC(DMM, "wrong sized message\n");
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100712 return gsm48_tx_mm_serv_rej(&msg->lchan->conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000713 GSM48_REJECT_INCORRECT_MESSAGE);
714 }
715
716 if (msg->data_len < req->mi_len + 6) {
Harald Weltec9e02182009-05-01 19:07:53 +0000717 DEBUGPC(DMM, "does not fit in packet\n");
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100718 return gsm48_tx_mm_serv_rej(&msg->lchan->conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000719 GSM48_REJECT_INCORRECT_MESSAGE);
720 }
721
Harald Weltec9e02182009-05-01 19:07:53 +0000722 mi_type = mi[0] & GSM_MI_TYPE_MASK;
Harald Welteba4cf162009-01-10 01:49:35 +0000723 if (mi_type != GSM_MI_TYPE_TMSI) {
Harald Weltec9e02182009-05-01 19:07:53 +0000724 DEBUGPC(DMM, "mi_type is not TMSI: %d\n", mi_type);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100725 return gsm48_tx_mm_serv_rej(&msg->lchan->conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000726 GSM48_REJECT_INCORRECT_MESSAGE);
727 }
728
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200729 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
Harald Weltec9e02182009-05-01 19:07:53 +0000730 DEBUGPC(DMM, "serv_type=0x%02x mi_type=0x%02x M(%s)\n",
Harald Welte4ed0e922009-01-10 03:17:30 +0000731 req->cm_service_type, mi_type, mi_string);
Harald Weltebcae43f2008-12-27 21:45:37 +0000732
Harald Welte7659de12009-12-13 12:39:18 +0100733 dispatch_signal(SS_SUBSCR, S_SUBSCR_IDENTITY, (classmark2 + classmark2_len));
734
Harald Welte3ac7f102009-08-10 10:12:45 +0200735 if (is_siemens_bts(bts))
736 send_siemens_mrpci(msg->lchan, classmark2-1);
737
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200738 subscr = subscr_get_by_tmsi(bts->network,
739 tmsi_from_string(mi_string));
Holger Freythereb443982009-06-04 13:58:42 +0000740
Harald Welte2a139372009-02-22 21:14:55 +0000741 /* FIXME: if we don't know the TMSI, inquire abit IMSI and allocate new TMSI */
Harald Welte4ed0e922009-01-10 03:17:30 +0000742 if (!subscr)
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100743 return gsm48_tx_mm_serv_rej(&msg->lchan->conn,
Harald Welte4ed0e922009-01-10 03:17:30 +0000744 GSM48_REJECT_IMSI_UNKNOWN_IN_HLR);
745
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100746 if (!msg->lchan->conn.subscr)
747 msg->lchan->conn.subscr = subscr;
748 else if (msg->lchan->conn.subscr == subscr)
Sylvain Munautea3f6742009-12-18 18:28:10 +0100749 subscr_put(subscr); /* lchan already has a ref, don't need another one */
750 else {
Harald Welte9bb7c702009-01-10 03:21:41 +0000751 DEBUGP(DMM, "<- CM Channel already owned by someone else?\n");
752 subscr_put(subscr);
753 }
754
Harald Weltec2e302d2009-07-05 14:08:13 +0200755 subscr->equipment.classmark2_len = classmark2_len;
756 memcpy(subscr->equipment.classmark2, classmark2, classmark2_len);
757 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +0000758
Harald Welte4b634542008-12-27 01:55:51 +0000759 return gsm48_tx_mm_serv_ack(msg->lchan);
760}
761
Harald Welte2a139372009-02-22 21:14:55 +0000762static int gsm48_rx_mm_imsi_detach_ind(struct msgb *msg)
763{
Harald Welte9176bd42009-07-23 18:46:00 +0200764 struct gsm_bts *bts = msg->lchan->ts->trx->bts;
Harald Welte2a139372009-02-22 21:14:55 +0000765 struct gsm48_hdr *gh = msgb_l3(msg);
766 struct gsm48_imsi_detach_ind *idi =
767 (struct gsm48_imsi_detach_ind *) gh->data;
768 u_int8_t mi_type = idi->mi[0] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200769 char mi_string[GSM48_MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +0800770 struct gsm_subscriber *subscr = NULL;
Harald Welte2a139372009-02-22 21:14:55 +0000771
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200772 gsm48_mi_to_string(mi_string, sizeof(mi_string), idi->mi, idi->mi_len);
Harald Welte2a139372009-02-22 21:14:55 +0000773 DEBUGP(DMM, "IMSI DETACH INDICATION: mi_type=0x%02x MI(%s): ",
774 mi_type, mi_string);
775
Harald Welteffa55a42009-12-22 19:07:32 +0100776 counter_inc(bts->network->stats.loc_upd_type.detach);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100777
Harald Welte2a139372009-02-22 21:14:55 +0000778 switch (mi_type) {
779 case GSM_MI_TYPE_TMSI:
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200780 subscr = subscr_get_by_tmsi(bts->network,
781 tmsi_from_string(mi_string));
Harald Welte2a139372009-02-22 21:14:55 +0000782 break;
783 case GSM_MI_TYPE_IMSI:
Harald Welte9176bd42009-07-23 18:46:00 +0200784 subscr = subscr_get_by_imsi(bts->network, mi_string);
Harald Welte2a139372009-02-22 21:14:55 +0000785 break;
786 case GSM_MI_TYPE_IMEI:
787 case GSM_MI_TYPE_IMEISV:
788 /* no sim card... FIXME: what to do ? */
Holger Freyther79f4ae62009-06-02 03:25:04 +0000789 DEBUGPC(DMM, "unimplemented mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +0000790 break;
791 default:
Holger Freyther79f4ae62009-06-02 03:25:04 +0000792 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +0000793 break;
794 }
795
Holger Freyther4a49e772009-04-12 05:37:29 +0000796 if (subscr) {
797 subscr_update(subscr, msg->trx->bts,
798 GSM_SUBSCRIBER_UPDATE_DETACHED);
Harald Welte2e6d4682009-12-24 14:50:24 +0100799 DEBUGP(DMM, "Subscriber: %s\n", subscr_name(subscr));
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200800
801 subscr->equipment.classmark1 = idi->classmark1;
802 db_sync_equipment(&subscr->equipment);
803
Holger Freytherc21cfbc2009-06-02 02:54:57 +0000804 subscr_put(subscr);
Holger Freyther4a49e772009-04-12 05:37:29 +0000805 } else
Harald Welte2a139372009-02-22 21:14:55 +0000806 DEBUGP(DMM, "Unknown Subscriber ?!?\n");
807
Harald Welte31981a02009-12-20 09:58:40 +0100808 /* FIXME: iterate over all transactions and release them,
809 * imagine an IMSI DETACH happening during an active call! */
810
Harald Welteb83d9382009-12-12 21:00:48 +0100811 /* subscriber is detached: should we release lchan? */
Harald Weltef7c28b02009-12-21 13:30:17 +0100812 lchan_auto_release(msg->lchan);
Harald Welteb83d9382009-12-12 21:00:48 +0100813
Harald Welte2a139372009-02-22 21:14:55 +0000814 return 0;
815}
816
Harald Welted2a7f5a2009-06-05 20:08:20 +0000817static int gsm48_rx_mm_status(struct msgb *msg)
818{
819 struct gsm48_hdr *gh = msgb_l3(msg);
820
821 DEBUGP(DMM, "MM STATUS (reject cause 0x%02x)\n", gh->data[0]);
822
823 return 0;
824}
825
Sylvain Munaut30a15382009-12-24 00:27:26 +0100826/* Chapter 9.2.3: Authentication Response */
827static int gsm48_rx_mm_auth_resp(struct msgb *msg)
828{
829 struct gsm48_hdr *gh = msgb_l3(msg);
830 struct gsm48_auth_resp *ar = (struct gsm48_auth_resp*) gh->data;
831 struct gsm_lchan *lchan = msg->lchan;
832 struct gsm_subscriber_connection *conn = &msg->lchan->conn;
833 struct gsm_network *net = lchan->ts->trx->bts->network;
834
835 DEBUGP(DMM, "MM AUTHENTICATION RESPONSE (sres = %s)\n",
836 hexdump(ar->sres, 4));
837
838 /* Safety check */
839 if (!conn->sec_operation) {
840 DEBUGP(DMM, "No authentication/cipher operation in progress !!!\n");
841 return -EIO;
842 }
843
844 /* Validate SRES */
845 if (memcmp(conn->sec_operation->atuple.sres, ar->sres,4)) {
846 gsm_cbfn *cb = conn->sec_operation->cb;
847 if (cb)
848 cb(GSM_HOOK_RR_SECURITY, GSM_SECURITY_AUTH_FAILED,
849 NULL, lchan, conn->sec_operation->cb_data);
850
851 release_security_operation(conn);
852 return gsm48_tx_mm_auth_rej(lchan);
853 }
854
855 /* Start ciphering */
856 lchan->encr.alg_id = RSL_ENC_ALG_A5(net->a5_encryption);
857 lchan->encr.key_len = 8;
858 memcpy(msg->lchan->encr.key, conn->sec_operation->atuple.kc, 8);
859
860 return gsm48_send_rr_ciph_mode(msg->lchan, 0);
861}
862
Harald Weltebf5e8df2009-02-03 12:59:45 +0000863/* Receive a GSM 04.08 Mobility Management (MM) message */
Harald Welte52b1f982008-12-23 20:25:15 +0000864static int gsm0408_rcv_mm(struct msgb *msg)
865{
866 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +0800867 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000868
869 switch (gh->msg_type & 0xbf) {
870 case GSM48_MT_MM_LOC_UPD_REQUEST:
Harald Weltea0368542009-06-27 02:58:43 +0200871 DEBUGP(DMM, "LOCATION UPDATING REQUEST: ");
Harald Welte231ad4f2008-12-27 11:15:38 +0000872 rc = mm_rx_loc_upd_req(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000873 break;
874 case GSM48_MT_MM_ID_RESP:
Harald Welte231ad4f2008-12-27 11:15:38 +0000875 rc = mm_rx_id_resp(msg);
876 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000877 case GSM48_MT_MM_CM_SERV_REQ:
Harald Welte4b634542008-12-27 01:55:51 +0000878 rc = gsm48_rx_mm_serv_req(msg);
879 break;
Harald Welte231ad4f2008-12-27 11:15:38 +0000880 case GSM48_MT_MM_STATUS:
Harald Welted2a7f5a2009-06-05 20:08:20 +0000881 rc = gsm48_rx_mm_status(msg);
Harald Welte231ad4f2008-12-27 11:15:38 +0000882 break;
Harald Welte231ad4f2008-12-27 11:15:38 +0000883 case GSM48_MT_MM_TMSI_REALL_COMPL:
Harald Welte69b2af22009-01-06 19:47:00 +0000884 DEBUGP(DMM, "TMSI Reallocation Completed. Subscriber: %s\n",
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100885 msg->lchan->conn.subscr ?
886 subscr_name(msg->lchan->conn.subscr) :
Harald Welte69b2af22009-01-06 19:47:00 +0000887 "unknown subscriber");
888 break;
Harald Welte231ad4f2008-12-27 11:15:38 +0000889 case GSM48_MT_MM_IMSI_DETACH_IND:
Harald Welte2a139372009-02-22 21:14:55 +0000890 rc = gsm48_rx_mm_imsi_detach_ind(msg);
891 break;
892 case GSM48_MT_MM_CM_REEST_REQ:
893 DEBUGP(DMM, "CM REESTABLISH REQUEST: Not implemented\n");
894 break;
895 case GSM48_MT_MM_AUTH_RESP:
Sylvain Munaut30a15382009-12-24 00:27:26 +0100896 rc = gsm48_rx_mm_auth_resp(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000897 break;
898 default:
Harald Welte5d24ba12009-12-24 12:13:17 +0100899 LOGP(DMM, LOGL_NOTICE, "Unknown GSM 04.08 MM msg type 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +0000900 gh->msg_type);
901 break;
902 }
903
904 return rc;
905}
Harald Weltebf5e8df2009-02-03 12:59:45 +0000906
Harald Welte2d35ae62009-02-06 12:02:13 +0000907/* Receive a PAGING RESPONSE message from the MS */
Holger Hans Peter Freytherca0fcbe2009-10-23 13:48:54 +0200908static int gsm48_rx_rr_pag_resp(struct msgb *msg)
Harald Welte2d35ae62009-02-06 12:02:13 +0000909{
Harald Welte9176bd42009-07-23 18:46:00 +0200910 struct gsm_bts *bts = msg->lchan->ts->trx->bts;
Harald Welte2d35ae62009-02-06 12:02:13 +0000911 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +0800912 struct gsm48_pag_resp *resp;
Harald Welte61548982009-02-22 21:26:29 +0000913 u_int8_t *classmark2_lv = gh->data + 1;
Holger Hans Peter Freyther3ee5d3e2009-08-21 05:18:21 +0200914 u_int8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200915 char mi_string[GSM48_MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +0800916 struct gsm_subscriber *subscr = NULL;
Harald Welte2d35ae62009-02-06 12:02:13 +0000917 int rc = 0;
918
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +0800919 resp = (struct gsm48_pag_resp *) &gh->data[0];
920 gsm48_paging_extract_mi(resp, msgb_l3len(msg) - sizeof(*gh),
921 mi_string, &mi_type);
Harald Welte2d35ae62009-02-06 12:02:13 +0000922 DEBUGP(DRR, "PAGING RESPONSE: mi_type=0x%02x MI(%s)\n",
923 mi_type, mi_string);
Harald Welte3ac7f102009-08-10 10:12:45 +0200924
Harald Weltefe18d8f2009-02-22 21:14:24 +0000925 switch (mi_type) {
926 case GSM_MI_TYPE_TMSI:
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200927 subscr = subscr_get_by_tmsi(bts->network,
928 tmsi_from_string(mi_string));
Harald Weltefe18d8f2009-02-22 21:14:24 +0000929 break;
930 case GSM_MI_TYPE_IMSI:
Harald Welte9176bd42009-07-23 18:46:00 +0200931 subscr = subscr_get_by_imsi(bts->network, mi_string);
Harald Weltefe18d8f2009-02-22 21:14:24 +0000932 break;
933 }
Harald Welte2d35ae62009-02-06 12:02:13 +0000934
935 if (!subscr) {
936 DEBUGP(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte09e38af2009-02-16 22:52:23 +0000937 /* FIXME: request id? close channel? */
Harald Welte2d35ae62009-02-06 12:02:13 +0000938 return -EINVAL;
939 }
940 DEBUGP(DRR, "<- Channel was requested by %s\n",
Harald Welte76042182009-08-08 16:03:15 +0200941 subscr->name && strlen(subscr->name) ? subscr->name : subscr->imsi);
Holger Freyther053e09d2009-02-14 22:51:06 +0000942
Harald Weltec2e302d2009-07-05 14:08:13 +0200943 subscr->equipment.classmark2_len = *classmark2_lv;
944 memcpy(subscr->equipment.classmark2, classmark2_lv+1, *classmark2_lv);
945 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +0000946
Holger Hans Peter Freyther3ee5d3e2009-08-21 05:18:21 +0200947 rc = gsm48_handle_paging_resp(msg, subscr);
Harald Welte2d35ae62009-02-06 12:02:13 +0000948 return rc;
949}
950
Harald Weltef7c43522009-06-09 20:24:21 +0000951static int gsm48_rx_rr_classmark(struct msgb *msg)
952{
953 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100954 struct gsm_subscriber *subscr = msg->lchan->conn.subscr;
Harald Weltef7c43522009-06-09 20:24:21 +0000955 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
956 u_int8_t cm2_len, cm3_len = 0;
957 u_int8_t *cm2, *cm3 = NULL;
958
959 DEBUGP(DRR, "CLASSMARK CHANGE ");
960
961 /* classmark 2 */
962 cm2_len = gh->data[0];
963 cm2 = &gh->data[1];
964 DEBUGPC(DRR, "CM2(len=%u) ", cm2_len);
965
966 if (payload_len > cm2_len + 1) {
967 /* we must have a classmark3 */
968 if (gh->data[cm2_len+1] != 0x20) {
969 DEBUGPC(DRR, "ERR CM3 TAG\n");
970 return -EINVAL;
971 }
972 if (cm2_len > 3) {
973 DEBUGPC(DRR, "CM2 too long!\n");
974 return -EINVAL;
975 }
976
977 cm3_len = gh->data[cm2_len+2];
978 cm3 = &gh->data[cm2_len+3];
979 if (cm3_len > 14) {
980 DEBUGPC(DRR, "CM3 len %u too long!\n", cm3_len);
981 return -EINVAL;
982 }
983 DEBUGPC(DRR, "CM3(len=%u)\n", cm3_len);
984 }
985 if (subscr) {
Harald Weltec2e302d2009-07-05 14:08:13 +0200986 subscr->equipment.classmark2_len = cm2_len;
987 memcpy(subscr->equipment.classmark2, cm2, cm2_len);
Harald Weltef7c43522009-06-09 20:24:21 +0000988 if (cm3) {
Harald Weltec2e302d2009-07-05 14:08:13 +0200989 subscr->equipment.classmark3_len = cm3_len;
990 memcpy(subscr->equipment.classmark3, cm3, cm3_len);
Harald Weltef7c43522009-06-09 20:24:21 +0000991 }
Harald Weltec2e302d2009-07-05 14:08:13 +0200992 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +0000993 }
994
Harald Weltef7c43522009-06-09 20:24:21 +0000995 return 0;
996}
997
Harald Weltecf5b3592009-05-01 18:28:42 +0000998static int gsm48_rx_rr_status(struct msgb *msg)
999{
1000 struct gsm48_hdr *gh = msgb_l3(msg);
1001
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001002 DEBUGP(DRR, "STATUS rr_cause = %s\n",
Harald Weltecf5b3592009-05-01 18:28:42 +00001003 rr_cause_name(gh->data[0]));
1004
1005 return 0;
1006}
1007
Harald Weltef7c43522009-06-09 20:24:21 +00001008static int gsm48_rx_rr_meas_rep(struct msgb *msg)
1009{
Harald Welted12b0fd2009-12-15 21:36:05 +01001010 struct gsm_meas_rep *meas_rep = lchan_next_meas_rep(msg->lchan);
Harald Weltef7c43522009-06-09 20:24:21 +00001011
Harald Welte3c7dc6e2009-11-29 19:07:28 +01001012 /* This shouldn't actually end up here, as RSL treats
1013 * L3 Info of 08.58 MEASUREMENT REPORT different by calling
1014 * directly into gsm48_parse_meas_rep */
1015 DEBUGP(DMEAS, "DIRECT GSM48 MEASUREMENT REPORT ?!? ");
Harald Welted12b0fd2009-12-15 21:36:05 +01001016 gsm48_parse_meas_rep(meas_rep, msg);
Harald Weltef7c43522009-06-09 20:24:21 +00001017
1018 return 0;
1019}
1020
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001021static int gsm48_rx_rr_app_info(struct msgb *msg)
1022{
1023 struct gsm48_hdr *gh = msgb_l3(msg);
1024 u_int8_t apdu_id_flags;
1025 u_int8_t apdu_len;
1026 u_int8_t *apdu_data;
1027
1028 apdu_id_flags = gh->data[0];
1029 apdu_len = gh->data[1];
1030 apdu_data = gh->data+2;
1031
1032 DEBUGP(DNM, "RX APPLICATION INFO id/flags=0x%02x apdu_len=%u apdu=%s",
1033 apdu_id_flags, apdu_len, hexdump(apdu_data, apdu_len));
1034
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01001035 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 +02001036}
1037
Sylvain Munaut30a15382009-12-24 00:27:26 +01001038/* Chapter 9.1.10 Ciphering Mode Complete */
1039static int gsm48_rx_rr_ciph_m_compl(struct msgb *msg)
1040{
1041 struct gsm_lchan *lchan = msg->lchan;
1042 struct gsm_subscriber_connection *conn = &lchan->conn;
1043 gsm_cbfn *cb;
1044 int rc = 0;
1045
1046 DEBUGP(DRR, "CIPHERING MODE COMPLETE\n");
1047
1048 /* Safety check */
1049 if (!conn->sec_operation) {
1050 DEBUGP(DRR, "No authentication/cipher operation in progress !!!\n");
1051 return -EIO;
1052 }
1053
1054 /* FIXME: check for MI (if any) */
1055
1056 /* Call back whatever was in progress (if anything) ... */
1057 cb = conn->sec_operation->cb;
1058 if (cb) {
1059 rc = cb(GSM_HOOK_RR_SECURITY, GSM_SECURITY_SUCCEEDED,
1060 NULL, lchan, conn->sec_operation->cb_data);
1061 }
1062
1063 /* Complete the operation */
1064 release_security_operation(conn);
1065
1066 return rc;
1067}
1068
Harald Welted011e8b2009-11-29 22:45:52 +01001069/* Chapter 9.1.16 Handover complete */
Harald Welte1e191c52009-12-14 17:51:15 +01001070static int gsm48_rx_rr_ho_compl(struct msgb *msg)
Harald Welted011e8b2009-11-29 22:45:52 +01001071{
1072 struct gsm48_hdr *gh = msgb_l3(msg);
1073
1074 DEBUGP(DRR, "HANDOVER COMPLETE cause = %s\n",
1075 rr_cause_name(gh->data[0]));
1076
1077 dispatch_signal(SS_LCHAN, S_LCHAN_HANDOVER_COMPL, msg->lchan);
1078 /* FIXME: release old channel */
1079
1080 return 0;
1081}
1082
1083/* Chapter 9.1.17 Handover Failure */
Harald Welte1e191c52009-12-14 17:51:15 +01001084static int gsm48_rx_rr_ho_fail(struct msgb *msg)
Harald Welted011e8b2009-11-29 22:45:52 +01001085{
1086 struct gsm48_hdr *gh = msgb_l3(msg);
1087
1088 DEBUGP(DRR, "HANDOVER FAILED cause = %s\n",
1089 rr_cause_name(gh->data[0]));
1090
1091 dispatch_signal(SS_LCHAN, S_LCHAN_HANDOVER_FAIL, msg->lchan);
1092 /* FIXME: release allocated new channel */
1093
1094 return 0;
1095}
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001096
Harald Weltebf5e8df2009-02-03 12:59:45 +00001097/* Receive a GSM 04.08 Radio Resource (RR) message */
Harald Welte52b1f982008-12-23 20:25:15 +00001098static int gsm0408_rcv_rr(struct msgb *msg)
1099{
1100 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001101 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001102
1103 switch (gh->msg_type) {
1104 case GSM48_MT_RR_CLSM_CHG:
Harald Weltef7c43522009-06-09 20:24:21 +00001105 rc = gsm48_rx_rr_classmark(msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001106 break;
Harald Weltefc977a82008-12-27 10:19:37 +00001107 case GSM48_MT_RR_GPRS_SUSP_REQ:
1108 DEBUGP(DRR, "GRPS SUSPEND REQUEST\n");
1109 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001110 case GSM48_MT_RR_PAG_RESP:
Holger Hans Peter Freytherca0fcbe2009-10-23 13:48:54 +02001111 rc = gsm48_rx_rr_pag_resp(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001112 break;
Harald Welte7ccf7782009-02-17 01:43:01 +00001113 case GSM48_MT_RR_CHAN_MODE_MODIF_ACK:
Holger Hans Peter Freytherf520e642009-10-22 15:23:11 +02001114 rc = gsm48_rx_rr_modif_ack(msg);
Harald Welte7ccf7782009-02-17 01:43:01 +00001115 break;
Harald Weltecf5b3592009-05-01 18:28:42 +00001116 case GSM48_MT_RR_STATUS:
1117 rc = gsm48_rx_rr_status(msg);
1118 break;
Harald Weltef7c43522009-06-09 20:24:21 +00001119 case GSM48_MT_RR_MEAS_REP:
1120 rc = gsm48_rx_rr_meas_rep(msg);
1121 break;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001122 case GSM48_MT_RR_APP_INFO:
1123 rc = gsm48_rx_rr_app_info(msg);
1124 break;
Harald Welte08d91a52009-08-30 15:37:11 +09001125 case GSM48_MT_RR_CIPH_M_COMPL:
Sylvain Munaut30a15382009-12-24 00:27:26 +01001126 rc = gsm48_rx_rr_ciph_m_compl(msg);
Harald Welte08d91a52009-08-30 15:37:11 +09001127 break;
Harald Welteccd5a882009-11-29 20:02:20 +01001128 case GSM48_MT_RR_HANDO_COMPL:
Harald Welted011e8b2009-11-29 22:45:52 +01001129 rc = gsm48_rx_rr_ho_compl(msg);
Harald Welteccd5a882009-11-29 20:02:20 +01001130 break;
1131 case GSM48_MT_RR_HANDO_FAIL:
Harald Welted011e8b2009-11-29 22:45:52 +01001132 rc = gsm48_rx_rr_ho_fail(msg);
Harald Welteccd5a882009-11-29 20:02:20 +01001133 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001134 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01001135 LOGP(DRR, LOGL_NOTICE, "Unimplemented "
1136 "GSM 04.08 RR msg type 0x%02x\n", gh->msg_type);
Harald Welte52b1f982008-12-23 20:25:15 +00001137 break;
1138 }
1139
Harald Welte2d35ae62009-02-06 12:02:13 +00001140 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +00001141}
1142
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001143int gsm48_send_rr_app_info(struct gsm_lchan *lchan, u_int8_t apdu_id,
Holger Hans Peter Freyther8b77a342009-10-22 15:42:19 +02001144 u_int8_t apdu_len, const u_int8_t *apdu)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001145{
1146 struct msgb *msg = gsm48_msgb_alloc();
1147 struct gsm48_hdr *gh;
1148
1149 msg->lchan = lchan;
1150
1151 DEBUGP(DRR, "TX APPLICATION INFO id=0x%02x, len=%u\n",
1152 apdu_id, apdu_len);
1153
1154 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 2 + apdu_len);
1155 gh->proto_discr = GSM48_PDISC_RR;
1156 gh->msg_type = GSM48_MT_RR_APP_INFO;
1157 gh->data[0] = apdu_id;
1158 gh->data[1] = apdu_len;
1159 memcpy(gh->data+2, apdu, apdu_len);
1160
1161 return gsm48_sendmsg(msg, NULL);
1162}
1163
Harald Welte4bc90a12008-12-27 16:32:52 +00001164/* Call Control */
1165
Harald Welte7584aea2009-02-11 11:44:12 +00001166/* The entire call control code is written in accordance with Figure 7.10c
1167 * for 'very early assignment', i.e. we allocate a TCH/F during IMMEDIATE
1168 * ASSIGN, then first use that TCH/F for signalling and later MODE MODIFY
1169 * it for voice */
1170
Harald Welte4bfdfe72009-06-10 23:11:52 +08001171static void new_cc_state(struct gsm_trans *trans, int state)
1172{
1173 if (state > 31 || state < 0)
1174 return;
1175
1176 DEBUGP(DCC, "new state %s -> %s\n",
Harald Weltee95daf12010-03-25 12:13:02 +08001177 gsm48_cc_state_name(trans->cc.state),
1178 gsm48_cc_state_name(state));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001179
Harald Weltedcaf5652009-07-23 18:56:43 +02001180 trans->cc.state = state;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001181}
1182
1183static int gsm48_cc_tx_status(struct gsm_trans *trans, void *arg)
Harald Welte4bc90a12008-12-27 16:32:52 +00001184{
1185 struct msgb *msg = gsm48_msgb_alloc();
1186 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1187 u_int8_t *cause, *call_state;
1188
Harald Welte4bc90a12008-12-27 16:32:52 +00001189 gh->msg_type = GSM48_MT_CC_STATUS;
1190
1191 cause = msgb_put(msg, 3);
1192 cause[0] = 2;
1193 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
1194 cause[2] = 0x80 | 30; /* response to status inquiry */
1195
1196 call_state = msgb_put(msg, 1);
1197 call_state[0] = 0xc0 | 0x00;
1198
Harald Welte39e2ead2009-07-23 21:13:03 +02001199 return gsm48_sendmsg(msg, trans);
Harald Welte4bc90a12008-12-27 16:32:52 +00001200}
1201
Harald Welte6f4b7532008-12-29 00:39:37 +00001202static int gsm48_tx_simple(struct gsm_lchan *lchan,
1203 u_int8_t pdisc, u_int8_t msg_type)
Harald Welte4bc90a12008-12-27 16:32:52 +00001204{
1205 struct msgb *msg = gsm48_msgb_alloc();
1206 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1207
1208 msg->lchan = lchan;
1209
Harald Welte6f4b7532008-12-29 00:39:37 +00001210 gh->proto_discr = pdisc;
Harald Welte4bc90a12008-12-27 16:32:52 +00001211 gh->msg_type = msg_type;
1212
Harald Welte39e2ead2009-07-23 21:13:03 +02001213 return gsm48_sendmsg(msg, NULL);
Harald Welte4bc90a12008-12-27 16:32:52 +00001214}
1215
Harald Welte4bfdfe72009-06-10 23:11:52 +08001216static void gsm48_stop_cc_timer(struct gsm_trans *trans)
1217{
Harald Weltedcaf5652009-07-23 18:56:43 +02001218 if (bsc_timer_pending(&trans->cc.timer)) {
1219 DEBUGP(DCC, "stopping pending timer T%x\n", trans->cc.Tcurrent);
1220 bsc_del_timer(&trans->cc.timer);
1221 trans->cc.Tcurrent = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001222 }
1223}
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001224
Harald Welte4bfdfe72009-06-10 23:11:52 +08001225static int mncc_recvmsg(struct gsm_network *net, struct gsm_trans *trans,
1226 int msg_type, struct gsm_mncc *mncc)
1227{
1228 struct msgb *msg;
1229
1230 if (trans)
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001231 if (trans->conn)
Harald Welte6f5aee02009-07-23 21:21:14 +02001232 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08001233 "Sending '%s' to MNCC.\n",
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001234 trans->conn->lchan->ts->trx->bts->nr,
1235 trans->conn->lchan->ts->trx->nr,
1236 trans->conn->lchan->ts->nr, trans->transaction_id,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001237 (trans->subscr)?(trans->subscr->extension):"-",
1238 get_mncc_name(msg_type));
1239 else
1240 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
1241 "Sending '%s' to MNCC.\n",
1242 (trans->subscr)?(trans->subscr->extension):"-",
1243 get_mncc_name(msg_type));
1244 else
1245 DEBUGP(DCC, "(bts - trx - ts - ti -- sub -) "
1246 "Sending '%s' to MNCC.\n", get_mncc_name(msg_type));
1247
1248 mncc->msg_type = msg_type;
1249
Harald Welte966636f2009-06-26 19:39:35 +02001250 msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001251 if (!msg)
1252 return -ENOMEM;
1253 memcpy(msg->data, mncc, sizeof(struct gsm_mncc));
1254 msgb_enqueue(&net->upqueue, msg);
1255
1256 return 0;
1257}
1258
1259int mncc_release_ind(struct gsm_network *net, struct gsm_trans *trans,
1260 u_int32_t callref, int location, int value)
1261{
1262 struct gsm_mncc rel;
1263
Harald Welte92f70c52009-06-12 01:54:08 +08001264 memset(&rel, 0, sizeof(rel));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001265 rel.callref = callref;
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001266 mncc_set_cause(&rel, location, value);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001267 return mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
1268}
1269
Harald Weltedcaf5652009-07-23 18:56:43 +02001270/* Call Control Specific transaction release.
1271 * gets called by trans_free, DO NOT CALL YOURSELF! */
1272void _gsm48_cc_trans_free(struct gsm_trans *trans)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001273{
Harald Welte4bfdfe72009-06-10 23:11:52 +08001274 gsm48_stop_cc_timer(trans);
1275
1276 /* send release to L4, if callref still exists */
1277 if (trans->callref) {
1278 /* Ressource unavailable */
Harald Welte596fed42009-07-23 19:06:52 +02001279 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001280 GSM48_CAUSE_LOC_PRN_S_LU,
1281 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001282 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001283 if (trans->cc.state != GSM_CSTATE_NULL)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001284 new_cc_state(trans, GSM_CSTATE_NULL);
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001285 if (trans->conn)
1286 trau_mux_unmap(&trans->conn->lchan->ts->e1_link, trans->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001287}
1288
1289static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg);
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001290
Harald Welte09e38af2009-02-16 22:52:23 +00001291/* call-back from paging the B-end of the connection */
1292static int setup_trig_pag_evt(unsigned int hooknum, unsigned int event,
Harald Welte7ccf7782009-02-17 01:43:01 +00001293 struct msgb *msg, void *_lchan, void *param)
Harald Welte09e38af2009-02-16 22:52:23 +00001294{
Harald Welte7ccf7782009-02-17 01:43:01 +00001295 struct gsm_lchan *lchan = _lchan;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001296 struct gsm_subscriber *subscr = param;
1297 struct gsm_trans *transt, *tmp;
1298 struct gsm_network *net;
Harald Weltec05677b2009-06-26 20:17:06 +02001299
Harald Welte09e38af2009-02-16 22:52:23 +00001300 if (hooknum != GSM_HOOK_RR_PAGING)
1301 return -EINVAL;
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001302
Harald Welte4bfdfe72009-06-10 23:11:52 +08001303 if (!subscr)
1304 return -EINVAL;
1305 net = subscr->net;
1306 if (!net) {
1307 DEBUGP(DCC, "Error Network not set!\n");
1308 return -EINVAL;
Harald Welte5a065df2009-02-22 21:13:18 +00001309 }
Harald Welte7584aea2009-02-11 11:44:12 +00001310
Harald Welte4bfdfe72009-06-10 23:11:52 +08001311 /* check all tranactions (without lchan) for subscriber */
1312 llist_for_each_entry_safe(transt, tmp, &net->trans_list, entry) {
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001313 if (transt->subscr != subscr || transt->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001314 continue;
1315 switch (event) {
1316 case GSM_PAGING_SUCCEEDED:
1317 if (!lchan) // paranoid
1318 break;
1319 DEBUGP(DCC, "Paging subscr %s succeeded!\n",
1320 subscr->extension);
1321 /* Assign lchan */
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001322 if (!transt->conn) {
1323 transt->conn = &lchan->conn;
1324 use_subscr_con(transt->conn);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001325 }
1326 /* send SETUP request to called party */
Harald Weltedcaf5652009-07-23 18:56:43 +02001327 gsm48_cc_tx_setup(transt, &transt->cc.msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001328 break;
1329 case GSM_PAGING_EXPIRED:
1330 DEBUGP(DCC, "Paging subscr %s expired!\n",
1331 subscr->extension);
1332 /* Temporarily out of order */
Harald Welte596fed42009-07-23 19:06:52 +02001333 mncc_release_ind(transt->subscr->net, transt,
1334 transt->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001335 GSM48_CAUSE_LOC_PRN_S_LU,
1336 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001337 transt->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001338 trans_free(transt);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001339 break;
1340 }
1341 }
Harald Welte09e38af2009-02-16 22:52:23 +00001342 return 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00001343}
Harald Welte7584aea2009-02-11 11:44:12 +00001344
Harald Welteda7ab742009-12-19 22:23:05 +01001345static int tch_recv_mncc(struct gsm_network *net, u_int32_t callref, int enable);
1346
Harald Welte805f6442009-07-28 18:25:29 +02001347/* some other part of the code sends us a signal */
1348static int handle_abisip_signal(unsigned int subsys, unsigned int signal,
1349 void *handler_data, void *signal_data)
1350{
1351 struct gsm_lchan *lchan = signal_data;
Harald Welte805f6442009-07-28 18:25:29 +02001352 int rc;
Harald Welteda7ab742009-12-19 22:23:05 +01001353 struct gsm_network *net;
1354 struct gsm_trans *trans;
Harald Welte805f6442009-07-28 18:25:29 +02001355
1356 if (subsys != SS_ABISIP)
1357 return 0;
1358
1359 /* in case we use direct BTS-to-BTS RTP */
1360 if (ipacc_rtp_direct)
1361 return 0;
1362
Harald Welte805f6442009-07-28 18:25:29 +02001363 switch (signal) {
Holger Hans Peter Freyther231163d2009-11-18 21:06:12 +01001364 case S_ABISIP_CRCX_ACK:
Harald Welteda7ab742009-12-19 22:23:05 +01001365 /* check if any transactions on this lchan still have
1366 * a tch_recv_mncc request pending */
1367 net = lchan->ts->trx->bts->network;
1368 llist_for_each_entry(trans, &net->trans_list, entry) {
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001369 if (trans->conn && trans->conn->lchan == lchan && trans->tch_recv) {
Harald Welteda7ab742009-12-19 22:23:05 +01001370 DEBUGP(DCC, "pending tch_recv_mncc request\n");
1371 tch_recv_mncc(net, trans->callref, 1);
1372 }
1373 }
Harald Welte805f6442009-07-28 18:25:29 +02001374 break;
Harald Welte805f6442009-07-28 18:25:29 +02001375 }
1376
1377 return 0;
Harald Welte805f6442009-07-28 18:25:29 +02001378}
1379
Harald Welte49f48b82009-02-17 15:29:33 +00001380/* map two ipaccess RTP streams onto each other */
Harald Welte11fa29c2009-02-19 17:24:39 +00001381static int tch_map(struct gsm_lchan *lchan, struct gsm_lchan *remote_lchan)
Harald Welte49f48b82009-02-17 15:29:33 +00001382{
Harald Welte11fa29c2009-02-19 17:24:39 +00001383 struct gsm_bts *bts = lchan->ts->trx->bts;
1384 struct gsm_bts *remote_bts = remote_lchan->ts->trx->bts;
Harald Welte805f6442009-07-28 18:25:29 +02001385 int rc;
Harald Welte49f48b82009-02-17 15:29:33 +00001386
Harald Welte11fa29c2009-02-19 17:24:39 +00001387 DEBUGP(DCC, "Setting up TCH map between (bts=%u,trx=%u,ts=%u) and (bts=%u,trx=%u,ts=%u)\n",
1388 bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
1389 remote_bts->nr, remote_lchan->ts->trx->nr, remote_lchan->ts->nr);
1390
1391 if (bts->type != remote_bts->type) {
1392 DEBUGP(DCC, "Cannot switch calls between different BTS types yet\n");
1393 return -EINVAL;
1394 }
Harald Welteda7ab742009-12-19 22:23:05 +01001395
1396 // todo: map between different bts types
Harald Welte11fa29c2009-02-19 17:24:39 +00001397 switch (bts->type) {
Mike Habene2d82272009-10-02 12:19:34 +01001398 case GSM_BTS_TYPE_NANOBTS:
Harald Welte805f6442009-07-28 18:25:29 +02001399 if (!ipacc_rtp_direct) {
1400 /* connect the TCH's to our RTP proxy */
Harald Weltea72273e2009-12-20 16:51:09 +01001401 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welte805f6442009-07-28 18:25:29 +02001402 if (rc < 0)
1403 return rc;
Harald Weltea72273e2009-12-20 16:51:09 +01001404 rc = rsl_ipacc_mdcx_to_rtpsock(remote_lchan);
Harald Welteda7ab742009-12-19 22:23:05 +01001405#warning do we need a check of rc here?
Harald Welte805f6442009-07-28 18:25:29 +02001406
1407 /* connect them with each other */
Harald Welte2c828992009-12-02 01:56:49 +05301408 rtp_socket_proxy(lchan->abis_ip.rtp_socket,
1409 remote_lchan->abis_ip.rtp_socket);
Harald Welte805f6442009-07-28 18:25:29 +02001410 } else {
1411 /* directly connect TCH RTP streams to each other */
Harald Welte2c828992009-12-02 01:56:49 +05301412 rc = rsl_ipacc_mdcx(lchan, remote_lchan->abis_ip.bound_ip,
1413 remote_lchan->abis_ip.bound_port,
Harald Welte2c828992009-12-02 01:56:49 +05301414 remote_lchan->abis_ip.rtp_payload2);
Harald Welte805f6442009-07-28 18:25:29 +02001415 if (rc < 0)
1416 return rc;
Harald Welte2c828992009-12-02 01:56:49 +05301417 rc = rsl_ipacc_mdcx(remote_lchan, lchan->abis_ip.bound_ip,
1418 lchan->abis_ip.bound_port,
Harald Welte2c828992009-12-02 01:56:49 +05301419 lchan->abis_ip.rtp_payload2);
Harald Welte805f6442009-07-28 18:25:29 +02001420 }
Harald Welte11fa29c2009-02-19 17:24:39 +00001421 break;
1422 case GSM_BTS_TYPE_BS11:
1423 trau_mux_map_lchan(lchan, remote_lchan);
1424 break;
1425 default:
1426 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
Harald Welteda7ab742009-12-19 22:23:05 +01001427 return -EINVAL;
Harald Welte11fa29c2009-02-19 17:24:39 +00001428 }
Harald Welte49f48b82009-02-17 15:29:33 +00001429
1430 return 0;
1431}
1432
Harald Welte4bfdfe72009-06-10 23:11:52 +08001433/* bridge channels of two transactions */
1434static int tch_bridge(struct gsm_network *net, u_int32_t *refs)
Harald Welte7ccf7782009-02-17 01:43:01 +00001435{
Harald Weltedcaf5652009-07-23 18:56:43 +02001436 struct gsm_trans *trans1 = trans_find_by_callref(net, refs[0]);
1437 struct gsm_trans *trans2 = trans_find_by_callref(net, refs[1]);
Harald Welte7ccf7782009-02-17 01:43:01 +00001438
Harald Welte4bfdfe72009-06-10 23:11:52 +08001439 if (!trans1 || !trans2)
Harald Welte7ccf7782009-02-17 01:43:01 +00001440 return -EIO;
1441
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001442 if (!trans1->conn || !trans2->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001443 return -EIO;
1444
1445 /* through-connect channel */
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001446 return tch_map(trans1->conn->lchan, trans2->conn->lchan);
Harald Welte7ccf7782009-02-17 01:43:01 +00001447}
1448
Harald Welteda7ab742009-12-19 22:23:05 +01001449/* enable receive of channels to MNCC upqueue */
1450static int tch_recv_mncc(struct gsm_network *net, u_int32_t callref, int enable)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001451{
1452 struct gsm_trans *trans;
Harald Welteda7ab742009-12-19 22:23:05 +01001453 struct gsm_lchan *lchan;
1454 struct gsm_bts *bts;
1455 int rc;
Harald Welte7ccf7782009-02-17 01:43:01 +00001456
Harald Welte4bfdfe72009-06-10 23:11:52 +08001457 /* Find callref */
Harald Welteda7ab742009-12-19 22:23:05 +01001458 trans = trans_find_by_callref(net, callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001459 if (!trans)
1460 return -EIO;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001461 if (!trans->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001462 return 0;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001463 lchan = trans->conn->lchan;
Harald Welteda7ab742009-12-19 22:23:05 +01001464 bts = lchan->ts->trx->bts;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001465
Harald Welteda7ab742009-12-19 22:23:05 +01001466 switch (bts->type) {
1467 case GSM_BTS_TYPE_NANOBTS:
1468 if (ipacc_rtp_direct) {
1469 DEBUGP(DCC, "Error: RTP proxy is disabled\n");
1470 return -EINVAL;
1471 }
1472 /* in case, we don't have a RTP socket yet, we note this
1473 * in the transaction and try later */
1474 if (!lchan->abis_ip.rtp_socket) {
1475 trans->tch_recv = enable;
1476 DEBUGP(DCC, "queue tch_recv_mncc request (%d)\n", enable);
1477 return 0;
1478 }
1479 if (enable) {
1480 /* connect the TCH's to our RTP proxy */
Harald Weltea72273e2009-12-20 16:51:09 +01001481 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welteda7ab742009-12-19 22:23:05 +01001482 if (rc < 0)
1483 return rc;
1484 /* assign socket to application interface */
1485 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
1486 net, callref);
1487 } else
1488 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
1489 net, 0);
1490 break;
1491 case GSM_BTS_TYPE_BS11:
1492 if (enable)
1493 return trau_recv_lchan(lchan, callref);
1494 return trau_mux_unmap(NULL, callref);
1495 break;
1496 default:
1497 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
1498 return -EINVAL;
1499 }
1500
1501 return 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001502}
1503
Harald Welte4bfdfe72009-06-10 23:11:52 +08001504static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
1505{
1506 DEBUGP(DCC, "-> STATUS ENQ\n");
1507 return gsm48_cc_tx_status(trans, msg);
1508}
1509
1510static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg);
1511static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg);
1512
1513static void gsm48_cc_timeout(void *arg)
1514{
1515 struct gsm_trans *trans = arg;
1516 int disconnect = 0, release = 0;
Harald Weltec66b71c2009-06-11 14:23:20 +08001517 int mo_cause = GSM48_CC_CAUSE_RECOVERY_TIMER;
1518 int mo_location = GSM48_CAUSE_LOC_USER;
1519 int l4_cause = GSM48_CC_CAUSE_NORMAL_UNSPEC;
1520 int l4_location = GSM48_CAUSE_LOC_PRN_S_LU;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001521 struct gsm_mncc mo_rel, l4_rel;
1522
1523 memset(&mo_rel, 0, sizeof(struct gsm_mncc));
1524 mo_rel.callref = trans->callref;
1525 memset(&l4_rel, 0, sizeof(struct gsm_mncc));
1526 l4_rel.callref = trans->callref;
1527
Harald Weltedcaf5652009-07-23 18:56:43 +02001528 switch(trans->cc.Tcurrent) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001529 case 0x303:
1530 release = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001531 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001532 break;
1533 case 0x310:
1534 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001535 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001536 break;
1537 case 0x313:
1538 disconnect = 1;
1539 /* unknown, did not find it in the specs */
1540 break;
1541 case 0x301:
1542 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001543 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001544 break;
1545 case 0x308:
Harald Weltedcaf5652009-07-23 18:56:43 +02001546 if (!trans->cc.T308_second) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001547 /* restart T308 a second time */
Harald Weltedcaf5652009-07-23 18:56:43 +02001548 gsm48_cc_tx_release(trans, &trans->cc.msg);
1549 trans->cc.T308_second = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001550 break; /* stay in release state */
1551 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001552 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001553 return;
1554// release = 1;
1555// l4_cause = 14;
1556// break;
1557 case 0x306:
1558 release = 1;
Harald Weltedcaf5652009-07-23 18:56:43 +02001559 mo_cause = trans->cc.msg.cause.value;
1560 mo_location = trans->cc.msg.cause.location;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001561 break;
1562 case 0x323:
1563 disconnect = 1;
1564 break;
1565 default:
1566 release = 1;
1567 }
1568
1569 if (release && trans->callref) {
1570 /* process release towards layer 4 */
Harald Welte596fed42009-07-23 19:06:52 +02001571 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001572 l4_location, l4_cause);
1573 trans->callref = 0;
1574 }
1575
1576 if (disconnect && trans->callref) {
1577 /* process disconnect towards layer 4 */
1578 mncc_set_cause(&l4_rel, l4_location, l4_cause);
Harald Welte596fed42009-07-23 19:06:52 +02001579 mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &l4_rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001580 }
1581
1582 /* process disconnect towards mobile station */
1583 if (disconnect || release) {
1584 mncc_set_cause(&mo_rel, mo_location, mo_cause);
Harald Weltedcaf5652009-07-23 18:56:43 +02001585 mo_rel.cause.diag[0] = ((trans->cc.Tcurrent & 0xf00) >> 8) + '0';
1586 mo_rel.cause.diag[1] = ((trans->cc.Tcurrent & 0x0f0) >> 4) + '0';
1587 mo_rel.cause.diag[2] = (trans->cc.Tcurrent & 0x00f) + '0';
Harald Welte4bfdfe72009-06-10 23:11:52 +08001588 mo_rel.cause.diag_len = 3;
1589
1590 if (disconnect)
1591 gsm48_cc_tx_disconnect(trans, &mo_rel);
1592 if (release)
1593 gsm48_cc_tx_release(trans, &mo_rel);
1594 }
1595
1596}
1597
1598static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
1599 int sec, int micro)
1600{
1601 DEBUGP(DCC, "starting timer T%x with %d seconds\n", current, sec);
Harald Weltedcaf5652009-07-23 18:56:43 +02001602 trans->cc.timer.cb = gsm48_cc_timeout;
1603 trans->cc.timer.data = trans;
1604 bsc_schedule_timer(&trans->cc.timer, sec, micro);
1605 trans->cc.Tcurrent = current;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001606}
1607
1608static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
1609{
1610 struct gsm48_hdr *gh = msgb_l3(msg);
1611 u_int8_t msg_type = gh->msg_type & 0xbf;
1612 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1613 struct tlv_parsed tp;
1614 struct gsm_mncc setup;
1615
1616 memset(&setup, 0, sizeof(struct gsm_mncc));
1617 setup.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01001618 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001619 /* emergency setup is identified by msg_type */
1620 if (msg_type == GSM48_MT_CC_EMERG_SETUP)
1621 setup.emergency = 1;
1622
1623 /* use subscriber as calling party number */
1624 if (trans->subscr) {
1625 setup.fields |= MNCC_F_CALLING;
1626 strncpy(setup.calling.number, trans->subscr->extension,
1627 sizeof(setup.calling.number)-1);
Andreas Eversbergc079be42009-06-15 23:22:09 +02001628 strncpy(setup.imsi, trans->subscr->imsi,
1629 sizeof(setup.imsi)-1);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001630 }
1631 /* bearer capability */
1632 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1633 setup.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001634 gsm48_decode_bearer_cap(&setup.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001635 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1636 }
1637 /* facility */
1638 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1639 setup.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001640 gsm48_decode_facility(&setup.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001641 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1642 }
1643 /* called party bcd number */
1644 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
1645 setup.fields |= MNCC_F_CALLED;
Harald Welte55c8f352010-03-07 23:40:35 +01001646 gsm48_decode_called(&setup.called,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001647 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
1648 }
1649 /* user-user */
1650 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1651 setup.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01001652 gsm48_decode_useruser(&setup.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001653 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1654 }
1655 /* ss-version */
1656 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1657 setup.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01001658 gsm48_decode_ssversion(&setup.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001659 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1660 }
1661 /* CLIR suppression */
1662 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
1663 setup.clir.sup = 1;
1664 /* CLIR invocation */
1665 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
1666 setup.clir.inv = 1;
1667 /* cc cap */
1668 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1669 setup.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001670 gsm48_decode_cccap(&setup.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001671 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1672 }
1673
Harald Welte4bfdfe72009-06-10 23:11:52 +08001674 new_cc_state(trans, GSM_CSTATE_INITIATED);
1675
Harald Welte (local)47df3992009-12-26 19:45:03 +01001676 LOGP(DCC, LOGL_INFO, "Subscriber %s (%s) sends SETUP to %s\n",
1677 subscr_name(trans->subscr), trans->subscr->extension,
1678 setup.called.number);
1679
Harald Welte4bfdfe72009-06-10 23:11:52 +08001680 /* indicate setup to MNCC */
Harald Welte596fed42009-07-23 19:06:52 +02001681 mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_IND, &setup);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001682
Harald Welte13cac662009-07-29 12:10:35 +02001683 /* MNCC code will modify the channel asynchronously, we should
1684 * ipaccess-bind only after the modification has been made to the
1685 * lchan->tch_mode */
Harald Welte4bfdfe72009-06-10 23:11:52 +08001686 return 0;
1687}
1688
1689static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
Harald Welte65e74cc2008-12-29 01:55:35 +00001690{
1691 struct msgb *msg = gsm48_msgb_alloc();
1692 struct gsm48_hdr *gh;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001693 struct gsm_mncc *setup = arg;
Harald Welte78283ef2009-07-23 21:36:44 +02001694 int rc, trans_id;
Harald Welte65e74cc2008-12-29 01:55:35 +00001695
Harald Welte7ccf7782009-02-17 01:43:01 +00001696 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte65e74cc2008-12-29 01:55:35 +00001697
Harald Welte4bfdfe72009-06-10 23:11:52 +08001698 /* transaction id must not be assigned */
1699 if (trans->transaction_id != 0xff) { /* unasssigned */
1700 DEBUGP(DCC, "TX Setup with assigned transaction. "
1701 "This is not allowed!\n");
1702 /* Temporarily out of order */
Harald Welte596fed42009-07-23 19:06:52 +02001703 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001704 GSM48_CAUSE_LOC_PRN_S_LU,
1705 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001706 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001707 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001708 return rc;
1709 }
1710
1711 /* Get free transaction_id */
Harald Welte78283ef2009-07-23 21:36:44 +02001712 trans_id = trans_assign_trans_id(trans->subscr, GSM48_PDISC_CC, 0);
1713 if (trans_id < 0) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001714 /* no free transaction ID */
Harald Welte596fed42009-07-23 19:06:52 +02001715 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001716 GSM48_CAUSE_LOC_PRN_S_LU,
1717 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001718 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001719 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001720 return rc;
1721 }
Harald Welte78283ef2009-07-23 21:36:44 +02001722 trans->transaction_id = trans_id;
Harald Welte49f48b82009-02-17 15:29:33 +00001723
Harald Welte65e74cc2008-12-29 01:55:35 +00001724 gh->msg_type = GSM48_MT_CC_SETUP;
Harald Welte09e38af2009-02-16 22:52:23 +00001725
Harald Welte4bfdfe72009-06-10 23:11:52 +08001726 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
Harald Welte65e74cc2008-12-29 01:55:35 +00001727
Harald Welte4bfdfe72009-06-10 23:11:52 +08001728 /* bearer capability */
1729 if (setup->fields & MNCC_F_BEARER_CAP)
Harald Welte55c8f352010-03-07 23:40:35 +01001730 gsm48_encode_bearer_cap(msg, 0, &setup->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001731 /* facility */
1732 if (setup->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001733 gsm48_encode_facility(msg, 0, &setup->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001734 /* progress */
1735 if (setup->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001736 gsm48_encode_progress(msg, 0, &setup->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001737 /* calling party BCD number */
1738 if (setup->fields & MNCC_F_CALLING)
Harald Welte55c8f352010-03-07 23:40:35 +01001739 gsm48_encode_calling(msg, &setup->calling);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001740 /* called party BCD number */
1741 if (setup->fields & MNCC_F_CALLED)
Harald Welte55c8f352010-03-07 23:40:35 +01001742 gsm48_encode_called(msg, &setup->called);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001743 /* user-user */
1744 if (setup->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001745 gsm48_encode_useruser(msg, 0, &setup->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001746 /* redirecting party BCD number */
1747 if (setup->fields & MNCC_F_REDIRECTING)
Harald Welte55c8f352010-03-07 23:40:35 +01001748 gsm48_encode_redirecting(msg, &setup->redirecting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001749 /* signal */
1750 if (setup->fields & MNCC_F_SIGNAL)
Harald Welte55c8f352010-03-07 23:40:35 +01001751 gsm48_encode_signal(msg, setup->signal);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001752
1753 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
Harald Welte65e74cc2008-12-29 01:55:35 +00001754
Harald Welte39e2ead2009-07-23 21:13:03 +02001755 return gsm48_sendmsg(msg, trans);
Harald Welte65e74cc2008-12-29 01:55:35 +00001756}
1757
Harald Welte4bfdfe72009-06-10 23:11:52 +08001758static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
1759{
1760 struct gsm48_hdr *gh = msgb_l3(msg);
1761 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1762 struct tlv_parsed tp;
1763 struct gsm_mncc call_conf;
1764
1765 gsm48_stop_cc_timer(trans);
1766 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
1767
1768 memset(&call_conf, 0, sizeof(struct gsm_mncc));
1769 call_conf.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01001770 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001771#if 0
1772 /* repeat */
1773 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
1774 call_conf.repeat = 1;
1775 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
1776 call_conf.repeat = 2;
1777#endif
1778 /* bearer capability */
1779 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1780 call_conf.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001781 gsm48_decode_bearer_cap(&call_conf.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001782 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1783 }
1784 /* cause */
1785 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1786 call_conf.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01001787 gsm48_decode_cause(&call_conf.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001788 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1789 }
1790 /* cc cap */
1791 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1792 call_conf.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001793 gsm48_decode_cccap(&call_conf.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001794 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1795 }
1796
1797 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
1798
Harald Welte596fed42009-07-23 19:06:52 +02001799 return mncc_recvmsg(trans->subscr->net, trans, MNCC_CALL_CONF_IND,
1800 &call_conf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001801}
1802
1803static int gsm48_cc_tx_call_proc(struct gsm_trans *trans, void *arg)
1804{
1805 struct gsm_mncc *proceeding = arg;
1806 struct msgb *msg = gsm48_msgb_alloc();
1807 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1808
Harald Welte4bfdfe72009-06-10 23:11:52 +08001809 gh->msg_type = GSM48_MT_CC_CALL_PROC;
1810
1811 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
1812
1813 /* bearer capability */
1814 if (proceeding->fields & MNCC_F_BEARER_CAP)
Harald Welte55c8f352010-03-07 23:40:35 +01001815 gsm48_encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001816 /* facility */
1817 if (proceeding->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001818 gsm48_encode_facility(msg, 0, &proceeding->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001819 /* progress */
1820 if (proceeding->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001821 gsm48_encode_progress(msg, 0, &proceeding->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001822
Harald Welte39e2ead2009-07-23 21:13:03 +02001823 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001824}
1825
1826static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
1827{
1828 struct gsm48_hdr *gh = msgb_l3(msg);
1829 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1830 struct tlv_parsed tp;
1831 struct gsm_mncc alerting;
1832
1833 gsm48_stop_cc_timer(trans);
1834 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
1835
1836 memset(&alerting, 0, sizeof(struct gsm_mncc));
1837 alerting.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01001838 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001839 /* facility */
1840 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1841 alerting.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001842 gsm48_decode_facility(&alerting.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001843 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1844 }
1845
1846 /* progress */
1847 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
1848 alerting.fields |= MNCC_F_PROGRESS;
Harald Welte55c8f352010-03-07 23:40:35 +01001849 gsm48_decode_progress(&alerting.progress,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001850 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
1851 }
1852 /* ss-version */
1853 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1854 alerting.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01001855 gsm48_decode_ssversion(&alerting.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001856 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1857 }
1858
1859 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
1860
Harald Welte596fed42009-07-23 19:06:52 +02001861 return mncc_recvmsg(trans->subscr->net, trans, MNCC_ALERT_IND,
1862 &alerting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001863}
1864
1865static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
1866{
1867 struct gsm_mncc *alerting = arg;
1868 struct msgb *msg = gsm48_msgb_alloc();
1869 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1870
Harald Welte4bfdfe72009-06-10 23:11:52 +08001871 gh->msg_type = GSM48_MT_CC_ALERTING;
1872
1873 /* facility */
1874 if (alerting->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001875 gsm48_encode_facility(msg, 0, &alerting->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001876 /* progress */
1877 if (alerting->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001878 gsm48_encode_progress(msg, 0, &alerting->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001879 /* user-user */
1880 if (alerting->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001881 gsm48_encode_useruser(msg, 0, &alerting->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001882
1883 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
1884
Harald Welte39e2ead2009-07-23 21:13:03 +02001885 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001886}
1887
1888static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
1889{
1890 struct gsm_mncc *progress = arg;
1891 struct msgb *msg = gsm48_msgb_alloc();
1892 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1893
Harald Welte4bfdfe72009-06-10 23:11:52 +08001894 gh->msg_type = GSM48_MT_CC_PROGRESS;
1895
1896 /* progress */
Harald Welte55c8f352010-03-07 23:40:35 +01001897 gsm48_encode_progress(msg, 1, &progress->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001898 /* user-user */
1899 if (progress->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001900 gsm48_encode_useruser(msg, 0, &progress->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001901
Harald Welte39e2ead2009-07-23 21:13:03 +02001902 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001903}
1904
1905static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
1906{
1907 struct gsm_mncc *connect = arg;
1908 struct msgb *msg = gsm48_msgb_alloc();
1909 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1910
Harald Welte4bfdfe72009-06-10 23:11:52 +08001911 gh->msg_type = GSM48_MT_CC_CONNECT;
1912
1913 gsm48_stop_cc_timer(trans);
1914 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
1915
1916 /* facility */
1917 if (connect->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001918 gsm48_encode_facility(msg, 0, &connect->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001919 /* progress */
1920 if (connect->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001921 gsm48_encode_progress(msg, 0, &connect->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001922 /* connected number */
1923 if (connect->fields & MNCC_F_CONNECTED)
Harald Welte55c8f352010-03-07 23:40:35 +01001924 gsm48_encode_connected(msg, &connect->connected);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001925 /* user-user */
1926 if (connect->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001927 gsm48_encode_useruser(msg, 0, &connect->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001928
1929 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
1930
Harald Welte39e2ead2009-07-23 21:13:03 +02001931 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001932}
1933
1934static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
1935{
1936 struct gsm48_hdr *gh = msgb_l3(msg);
1937 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1938 struct tlv_parsed tp;
1939 struct gsm_mncc connect;
1940
1941 gsm48_stop_cc_timer(trans);
1942
1943 memset(&connect, 0, sizeof(struct gsm_mncc));
1944 connect.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01001945 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001946 /* use subscriber as connected party number */
1947 if (trans->subscr) {
1948 connect.fields |= MNCC_F_CONNECTED;
1949 strncpy(connect.connected.number, trans->subscr->extension,
1950 sizeof(connect.connected.number)-1);
Andreas Eversbergc079be42009-06-15 23:22:09 +02001951 strncpy(connect.imsi, trans->subscr->imsi,
1952 sizeof(connect.imsi)-1);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001953 }
1954 /* facility */
1955 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1956 connect.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001957 gsm48_decode_facility(&connect.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001958 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1959 }
1960 /* user-user */
1961 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1962 connect.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01001963 gsm48_decode_useruser(&connect.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001964 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1965 }
1966 /* ss-version */
1967 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1968 connect.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01001969 gsm48_decode_ssversion(&connect.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001970 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1971 }
1972
1973 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
1974
Harald Welte596fed42009-07-23 19:06:52 +02001975 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_CNF, &connect);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001976}
1977
1978
1979static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
1980{
1981 struct gsm_mncc connect_ack;
1982
1983 gsm48_stop_cc_timer(trans);
1984
1985 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1986
1987 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
1988 connect_ack.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02001989 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_COMPL_IND,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001990 &connect_ack);
1991}
1992
1993static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
1994{
1995 struct msgb *msg = gsm48_msgb_alloc();
1996 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1997
Harald Welte4bfdfe72009-06-10 23:11:52 +08001998 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
1999
2000 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2001
Harald Welte39e2ead2009-07-23 21:13:03 +02002002 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002003}
2004
2005static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
2006{
2007 struct gsm48_hdr *gh = msgb_l3(msg);
2008 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2009 struct tlv_parsed tp;
2010 struct gsm_mncc disc;
2011
2012 gsm48_stop_cc_timer(trans);
2013
2014 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
2015
2016 memset(&disc, 0, sizeof(struct gsm_mncc));
2017 disc.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002018 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002019 /* cause */
2020 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2021 disc.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002022 gsm48_decode_cause(&disc.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002023 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2024 }
2025 /* facility */
2026 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2027 disc.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002028 gsm48_decode_facility(&disc.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002029 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2030 }
2031 /* user-user */
2032 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2033 disc.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002034 gsm48_decode_useruser(&disc.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002035 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2036 }
2037 /* ss-version */
2038 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2039 disc.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002040 gsm48_decode_ssversion(&disc.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002041 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2042 }
2043
Harald Welte596fed42009-07-23 19:06:52 +02002044 return mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &disc);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002045
2046}
2047
Harald Weltec66b71c2009-06-11 14:23:20 +08002048static struct gsm_mncc_cause default_cause = {
2049 .location = GSM48_CAUSE_LOC_PRN_S_LU,
2050 .coding = 0,
2051 .rec = 0,
2052 .rec_val = 0,
2053 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
2054 .diag_len = 0,
2055 .diag = { 0 },
2056};
Harald Welte4bfdfe72009-06-10 23:11:52 +08002057
2058static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
2059{
2060 struct gsm_mncc *disc = arg;
2061 struct msgb *msg = gsm48_msgb_alloc();
2062 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2063
Harald Welte4bfdfe72009-06-10 23:11:52 +08002064 gh->msg_type = GSM48_MT_CC_DISCONNECT;
2065
2066 gsm48_stop_cc_timer(trans);
2067 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
2068
2069 /* cause */
2070 if (disc->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002071 gsm48_encode_cause(msg, 1, &disc->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002072 else
Harald Welte55c8f352010-03-07 23:40:35 +01002073 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002074
2075 /* facility */
2076 if (disc->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002077 gsm48_encode_facility(msg, 0, &disc->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002078 /* progress */
2079 if (disc->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002080 gsm48_encode_progress(msg, 0, &disc->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002081 /* user-user */
2082 if (disc->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002083 gsm48_encode_useruser(msg, 0, &disc->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002084
2085 /* store disconnect cause for T306 expiry */
Harald Weltedcaf5652009-07-23 18:56:43 +02002086 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002087
2088 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
2089
Harald Welte39e2ead2009-07-23 21:13:03 +02002090 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002091}
2092
2093static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
2094{
2095 struct gsm48_hdr *gh = msgb_l3(msg);
2096 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2097 struct tlv_parsed tp;
2098 struct gsm_mncc rel;
2099 int rc;
2100
2101 gsm48_stop_cc_timer(trans);
2102
2103 memset(&rel, 0, sizeof(struct gsm_mncc));
2104 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002105 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002106 /* cause */
2107 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2108 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002109 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002110 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2111 }
2112 /* facility */
2113 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2114 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002115 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002116 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2117 }
2118 /* user-user */
2119 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2120 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002121 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002122 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2123 }
2124 /* ss-version */
2125 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2126 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002127 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002128 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2129 }
2130
Harald Weltedcaf5652009-07-23 18:56:43 +02002131 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002132 /* release collision 5.4.5 */
Harald Welte596fed42009-07-23 19:06:52 +02002133 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_CNF, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002134 } else {
Harald Welte596fed42009-07-23 19:06:52 +02002135 rc = gsm48_tx_simple(msg->lchan,
Harald Welte6f5aee02009-07-23 21:21:14 +02002136 GSM48_PDISC_CC | (trans->transaction_id << 4),
Harald Welte596fed42009-07-23 19:06:52 +02002137 GSM48_MT_CC_RELEASE_COMPL);
2138 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_IND, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002139 }
2140
2141 new_cc_state(trans, GSM_CSTATE_NULL);
2142
2143 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002144 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002145
2146 return rc;
2147}
2148
2149static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
2150{
2151 struct gsm_mncc *rel = arg;
2152 struct msgb *msg = gsm48_msgb_alloc();
2153 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2154
Harald Welte4bfdfe72009-06-10 23:11:52 +08002155 gh->msg_type = GSM48_MT_CC_RELEASE;
2156
2157 trans->callref = 0;
2158
2159 gsm48_stop_cc_timer(trans);
2160 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
2161
2162 /* cause */
2163 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002164 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002165 /* facility */
2166 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002167 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002168 /* user-user */
2169 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002170 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002171
Harald Weltedcaf5652009-07-23 18:56:43 +02002172 trans->cc.T308_second = 0;
2173 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002174
Harald Weltedcaf5652009-07-23 18:56:43 +02002175 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002176 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
2177
Harald Welte39e2ead2009-07-23 21:13:03 +02002178 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002179}
2180
2181static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
2182{
2183 struct gsm48_hdr *gh = msgb_l3(msg);
2184 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2185 struct tlv_parsed tp;
2186 struct gsm_mncc rel;
2187 int rc = 0;
2188
2189 gsm48_stop_cc_timer(trans);
2190
2191 memset(&rel, 0, sizeof(struct gsm_mncc));
2192 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002193 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002194 /* cause */
2195 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2196 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002197 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002198 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2199 }
2200 /* facility */
2201 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2202 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002203 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002204 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2205 }
2206 /* user-user */
2207 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2208 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002209 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002210 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2211 }
2212 /* ss-version */
2213 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2214 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002215 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002216 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2217 }
2218
2219 if (trans->callref) {
Harald Weltedcaf5652009-07-23 18:56:43 +02002220 switch (trans->cc.state) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002221 case GSM_CSTATE_CALL_PRESENT:
Harald Welte596fed42009-07-23 19:06:52 +02002222 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002223 MNCC_REJ_IND, &rel);
2224 break;
2225 case GSM_CSTATE_RELEASE_REQ:
Harald Welte596fed42009-07-23 19:06:52 +02002226 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002227 MNCC_REL_CNF, &rel);
Harald Welteb83d9382009-12-12 21:00:48 +01002228 /* FIXME: in case of multiple calls, we can't simply
2229 * hang up here ! */
2230 lchan_auto_release(msg->lchan);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002231 break;
2232 default:
Harald Welte596fed42009-07-23 19:06:52 +02002233 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002234 MNCC_REL_IND, &rel);
2235 }
2236 }
2237
2238 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002239 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002240
2241 return rc;
2242}
2243
2244static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
2245{
2246 struct gsm_mncc *rel = arg;
2247 struct msgb *msg = gsm48_msgb_alloc();
2248 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2249
Harald Welte4bfdfe72009-06-10 23:11:52 +08002250 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
2251
2252 trans->callref = 0;
2253
2254 gsm48_stop_cc_timer(trans);
2255
2256 /* cause */
2257 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002258 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002259 /* facility */
2260 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002261 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002262 /* user-user */
2263 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002264 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002265
Harald Weltedcaf5652009-07-23 18:56:43 +02002266 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002267
Harald Welte39e2ead2009-07-23 21:13:03 +02002268 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002269}
2270
2271static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
2272{
2273 struct gsm48_hdr *gh = msgb_l3(msg);
2274 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2275 struct tlv_parsed tp;
2276 struct gsm_mncc fac;
2277
2278 memset(&fac, 0, sizeof(struct gsm_mncc));
2279 fac.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002280 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002281 /* facility */
2282 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2283 fac.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002284 gsm48_decode_facility(&fac.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002285 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2286 }
2287 /* ss-version */
2288 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2289 fac.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002290 gsm48_decode_ssversion(&fac.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002291 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2292 }
2293
Harald Welte596fed42009-07-23 19:06:52 +02002294 return mncc_recvmsg(trans->subscr->net, trans, MNCC_FACILITY_IND, &fac);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002295}
2296
2297static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
2298{
2299 struct gsm_mncc *fac = arg;
2300 struct msgb *msg = gsm48_msgb_alloc();
2301 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2302
Harald Welte4bfdfe72009-06-10 23:11:52 +08002303 gh->msg_type = GSM48_MT_CC_FACILITY;
2304
2305 /* facility */
Harald Welte55c8f352010-03-07 23:40:35 +01002306 gsm48_encode_facility(msg, 1, &fac->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002307
Harald Welte39e2ead2009-07-23 21:13:03 +02002308 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002309}
2310
2311static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
2312{
2313 struct gsm_mncc hold;
2314
2315 memset(&hold, 0, sizeof(struct gsm_mncc));
2316 hold.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02002317 return mncc_recvmsg(trans->subscr->net, trans, MNCC_HOLD_IND, &hold);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002318}
2319
2320static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
2321{
2322 struct msgb *msg = gsm48_msgb_alloc();
2323 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2324
Harald Welte4bfdfe72009-06-10 23:11:52 +08002325 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
2326
Harald Welte39e2ead2009-07-23 21:13:03 +02002327 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002328}
2329
2330static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
2331{
2332 struct gsm_mncc *hold_rej = arg;
2333 struct msgb *msg = gsm48_msgb_alloc();
2334 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2335
Harald Welte4bfdfe72009-06-10 23:11:52 +08002336 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
2337
2338 /* cause */
2339 if (hold_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002340 gsm48_encode_cause(msg, 1, &hold_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002341 else
Harald Welte55c8f352010-03-07 23:40:35 +01002342 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002343
Harald Welte39e2ead2009-07-23 21:13:03 +02002344 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002345}
2346
2347static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
2348{
2349 struct gsm_mncc retrieve;
2350
2351 memset(&retrieve, 0, sizeof(struct gsm_mncc));
2352 retrieve.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02002353 return mncc_recvmsg(trans->subscr->net, trans, MNCC_RETRIEVE_IND,
2354 &retrieve);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002355}
2356
2357static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
2358{
2359 struct msgb *msg = gsm48_msgb_alloc();
2360 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2361
Harald Welte4bfdfe72009-06-10 23:11:52 +08002362 gh->msg_type = GSM48_MT_CC_RETR_ACK;
2363
Harald Welte39e2ead2009-07-23 21:13:03 +02002364 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002365}
2366
2367static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
2368{
2369 struct gsm_mncc *retrieve_rej = arg;
2370 struct msgb *msg = gsm48_msgb_alloc();
2371 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2372
Harald Welte4bfdfe72009-06-10 23:11:52 +08002373 gh->msg_type = GSM48_MT_CC_RETR_REJ;
2374
2375 /* cause */
2376 if (retrieve_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002377 gsm48_encode_cause(msg, 1, &retrieve_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002378 else
Harald Welte55c8f352010-03-07 23:40:35 +01002379 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002380
Harald Welte39e2ead2009-07-23 21:13:03 +02002381 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002382}
2383
2384static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
2385{
2386 struct gsm48_hdr *gh = msgb_l3(msg);
2387 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2388 struct tlv_parsed tp;
2389 struct gsm_mncc dtmf;
2390
2391 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2392 dtmf.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002393 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002394 /* keypad facility */
2395 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
2396 dtmf.fields |= MNCC_F_KEYPAD;
Harald Welte55c8f352010-03-07 23:40:35 +01002397 gsm48_decode_keypad(&dtmf.keypad,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002398 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
2399 }
2400
Harald Welte596fed42009-07-23 19:06:52 +02002401 return mncc_recvmsg(trans->subscr->net, trans, MNCC_START_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002402}
2403
2404static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
2405{
2406 struct gsm_mncc *dtmf = arg;
2407 struct msgb *msg = gsm48_msgb_alloc();
2408 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2409
Harald Welte4bfdfe72009-06-10 23:11:52 +08002410 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
2411
2412 /* keypad */
2413 if (dtmf->fields & MNCC_F_KEYPAD)
Harald Welte55c8f352010-03-07 23:40:35 +01002414 gsm48_encode_keypad(msg, dtmf->keypad);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002415
Harald Welte39e2ead2009-07-23 21:13:03 +02002416 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002417}
2418
2419static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
2420{
2421 struct gsm_mncc *dtmf = arg;
2422 struct msgb *msg = gsm48_msgb_alloc();
2423 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2424
Harald Welte4bfdfe72009-06-10 23:11:52 +08002425 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
2426
2427 /* cause */
2428 if (dtmf->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002429 gsm48_encode_cause(msg, 1, &dtmf->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002430 else
Harald Welte55c8f352010-03-07 23:40:35 +01002431 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002432
Harald Welte39e2ead2009-07-23 21:13:03 +02002433 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002434}
2435
2436static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
2437{
2438 struct msgb *msg = gsm48_msgb_alloc();
2439 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2440
Harald Welte4bfdfe72009-06-10 23:11:52 +08002441 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
2442
Harald Welte39e2ead2009-07-23 21:13:03 +02002443 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002444}
2445
2446static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
2447{
2448 struct gsm_mncc dtmf;
2449
2450 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2451 dtmf.callref = trans->callref;
2452
Harald Welte596fed42009-07-23 19:06:52 +02002453 return mncc_recvmsg(trans->subscr->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002454}
2455
2456static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
2457{
2458 struct gsm48_hdr *gh = msgb_l3(msg);
2459 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2460 struct tlv_parsed tp;
2461 struct gsm_mncc modify;
2462
2463 memset(&modify, 0, sizeof(struct gsm_mncc));
2464 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002465 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002466 /* bearer capability */
2467 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2468 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002469 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002470 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2471 }
2472
2473 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
2474
Harald Welte596fed42009-07-23 19:06:52 +02002475 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_IND, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002476}
2477
2478static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
2479{
2480 struct gsm_mncc *modify = arg;
2481 struct msgb *msg = gsm48_msgb_alloc();
2482 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2483
Harald Welte4bfdfe72009-06-10 23:11:52 +08002484 gh->msg_type = GSM48_MT_CC_MODIFY;
2485
2486 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
2487
2488 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002489 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002490
2491 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
2492
Harald Welte39e2ead2009-07-23 21:13:03 +02002493 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002494}
2495
2496static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
2497{
2498 struct gsm48_hdr *gh = msgb_l3(msg);
2499 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2500 struct tlv_parsed tp;
2501 struct gsm_mncc modify;
2502
2503 gsm48_stop_cc_timer(trans);
2504
2505 memset(&modify, 0, sizeof(struct gsm_mncc));
2506 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002507 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002508 /* bearer capability */
2509 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2510 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002511 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002512 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2513 }
2514
2515 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2516
Harald Welte596fed42009-07-23 19:06:52 +02002517 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_CNF, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002518}
2519
2520static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
2521{
2522 struct gsm_mncc *modify = arg;
2523 struct msgb *msg = gsm48_msgb_alloc();
2524 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2525
Harald Welte4bfdfe72009-06-10 23:11:52 +08002526 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
2527
2528 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002529 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002530
2531 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2532
Harald Welte39e2ead2009-07-23 21:13:03 +02002533 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002534}
2535
2536static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
2537{
2538 struct gsm48_hdr *gh = msgb_l3(msg);
2539 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2540 struct tlv_parsed tp;
2541 struct gsm_mncc modify;
2542
2543 gsm48_stop_cc_timer(trans);
2544
2545 memset(&modify, 0, sizeof(struct gsm_mncc));
2546 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002547 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002548 /* bearer capability */
2549 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2550 modify.fields |= GSM48_IE_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002551 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002552 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2553 }
2554 /* cause */
2555 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2556 modify.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002557 gsm48_decode_cause(&modify.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002558 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2559 }
2560
2561 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2562
Harald Welte596fed42009-07-23 19:06:52 +02002563 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_REJ, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002564}
2565
2566static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
2567{
2568 struct gsm_mncc *modify = arg;
2569 struct msgb *msg = gsm48_msgb_alloc();
2570 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2571
Harald Welte4bfdfe72009-06-10 23:11:52 +08002572 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
2573
2574 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002575 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002576 /* cause */
Harald Welte55c8f352010-03-07 23:40:35 +01002577 gsm48_encode_cause(msg, 1, &modify->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002578
2579 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2580
Harald Welte39e2ead2009-07-23 21:13:03 +02002581 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002582}
2583
2584static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
2585{
2586 struct gsm_mncc *notify = arg;
2587 struct msgb *msg = gsm48_msgb_alloc();
2588 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2589
Harald Welte4bfdfe72009-06-10 23:11:52 +08002590 gh->msg_type = GSM48_MT_CC_NOTIFY;
2591
2592 /* notify */
Harald Welte55c8f352010-03-07 23:40:35 +01002593 gsm48_encode_notify(msg, notify->notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002594
Harald Welte39e2ead2009-07-23 21:13:03 +02002595 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002596}
2597
2598static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
2599{
2600 struct gsm48_hdr *gh = msgb_l3(msg);
2601 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2602// struct tlv_parsed tp;
2603 struct gsm_mncc notify;
2604
2605 memset(&notify, 0, sizeof(struct gsm_mncc));
2606 notify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002607// tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002608 if (payload_len >= 1)
Harald Welte55c8f352010-03-07 23:40:35 +01002609 gsm48_decode_notify(&notify.notify, gh->data);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002610
Harald Welte596fed42009-07-23 19:06:52 +02002611 return mncc_recvmsg(trans->subscr->net, trans, MNCC_NOTIFY_IND, &notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002612}
2613
2614static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
2615{
2616 struct gsm_mncc *user = arg;
2617 struct msgb *msg = gsm48_msgb_alloc();
2618 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2619
Harald Welte4bfdfe72009-06-10 23:11:52 +08002620 gh->msg_type = GSM48_MT_CC_USER_INFO;
2621
2622 /* user-user */
2623 if (user->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002624 gsm48_encode_useruser(msg, 1, &user->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002625 /* more data */
2626 if (user->more)
Harald Welte55c8f352010-03-07 23:40:35 +01002627 gsm48_encode_more(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002628
Harald Welte39e2ead2009-07-23 21:13:03 +02002629 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002630}
2631
2632static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
2633{
2634 struct gsm48_hdr *gh = msgb_l3(msg);
2635 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2636 struct tlv_parsed tp;
2637 struct gsm_mncc user;
2638
2639 memset(&user, 0, sizeof(struct gsm_mncc));
2640 user.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002641 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002642 /* user-user */
2643 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2644 user.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002645 gsm48_decode_useruser(&user.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002646 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2647 }
2648 /* more data */
2649 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
2650 user.more = 1;
2651
Harald Welte596fed42009-07-23 19:06:52 +02002652 return mncc_recvmsg(trans->subscr->net, trans, MNCC_USERINFO_IND, &user);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002653}
2654
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +02002655static int _gsm48_lchan_modify(struct gsm_trans *trans, void *arg)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002656{
2657 struct gsm_mncc *mode = arg;
2658
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002659 return gsm48_lchan_modify(trans->conn->lchan, mode->lchan_mode);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002660}
2661
2662static struct downstate {
2663 u_int32_t states;
2664 int type;
2665 int (*rout) (struct gsm_trans *trans, void *arg);
2666} downstatelist[] = {
2667 /* mobile originating call establishment */
2668 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
2669 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc},
2670 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
2671 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
2672 {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 */
2673 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
2674 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
2675 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
2676 /* mobile terminating call establishment */
2677 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
2678 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
2679 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
2680 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
2681 /* signalling during call */
2682 {SBIT(GSM_CSTATE_ACTIVE),
2683 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
2684 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
2685 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
2686 {ALL_STATES,
2687 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
2688 {ALL_STATES,
2689 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
2690 {ALL_STATES,
2691 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
2692 {SBIT(GSM_CSTATE_ACTIVE),
2693 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
2694 {SBIT(GSM_CSTATE_ACTIVE),
2695 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
2696 {SBIT(GSM_CSTATE_ACTIVE),
2697 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
2698 {SBIT(GSM_CSTATE_ACTIVE),
2699 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
2700 {SBIT(GSM_CSTATE_ACTIVE),
2701 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
2702 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2703 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
2704 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2705 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
2706 {SBIT(GSM_CSTATE_ACTIVE),
2707 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
2708 /* clearing */
2709 {SBIT(GSM_CSTATE_INITIATED),
2710 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
2711 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
2712 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
2713 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
2714 MNCC_REL_REQ, gsm48_cc_tx_release},
2715 /* special */
2716 {ALL_STATES,
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +02002717 MNCC_LCHAN_MODIFY, _gsm48_lchan_modify},
Harald Welte4bfdfe72009-06-10 23:11:52 +08002718};
2719
2720#define DOWNSLLEN \
2721 (sizeof(downstatelist) / sizeof(struct downstate))
2722
2723
2724int mncc_send(struct gsm_network *net, int msg_type, void *arg)
2725{
Harald Welte1a6f7982009-08-09 18:52:33 +02002726 int i, rc = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002727 struct gsm_trans *trans = NULL, *transt;
Harald Welte1a6f7982009-08-09 18:52:33 +02002728 struct gsm_lchan *lchan = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002729 struct gsm_bts *bts = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002730 struct gsm_mncc *data = arg, rel;
2731
2732 /* handle special messages */
2733 switch(msg_type) {
2734 case MNCC_BRIDGE:
2735 return tch_bridge(net, arg);
2736 case MNCC_FRAME_DROP:
Harald Welteda7ab742009-12-19 22:23:05 +01002737 return tch_recv_mncc(net, data->callref, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002738 case MNCC_FRAME_RECV:
Harald Welteda7ab742009-12-19 22:23:05 +01002739 return tch_recv_mncc(net, data->callref, 1);
2740 case GSM_TCHF_FRAME:
2741 /* Find callref */
2742 trans = trans_find_by_callref(net, data->callref);
2743 if (!trans)
2744 return -EIO;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002745 if (!trans->conn)
Harald Welteda7ab742009-12-19 22:23:05 +01002746 return 0;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002747 if (trans->conn->lchan->type != GSM_LCHAN_TCH_F)
Harald Welteda7ab742009-12-19 22:23:05 +01002748 return 0;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002749 bts = trans->conn->lchan->ts->trx->bts;
Harald Welteda7ab742009-12-19 22:23:05 +01002750 switch (bts->type) {
2751 case GSM_BTS_TYPE_NANOBTS:
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002752 if (!trans->conn->lchan->abis_ip.rtp_socket)
Harald Welteda7ab742009-12-19 22:23:05 +01002753 return 0;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002754 return rtp_send_frame(trans->conn->lchan->abis_ip.rtp_socket, arg);
Harald Welteda7ab742009-12-19 22:23:05 +01002755 case GSM_BTS_TYPE_BS11:
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002756 return trau_send_frame(trans->conn->lchan, arg);
Harald Welteda7ab742009-12-19 22:23:05 +01002757 default:
2758 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
2759 }
2760 return -EINVAL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002761 }
2762
2763 memset(&rel, 0, sizeof(struct gsm_mncc));
2764 rel.callref = data->callref;
2765
2766 /* Find callref */
Harald Weltedcaf5652009-07-23 18:56:43 +02002767 trans = trans_find_by_callref(net, data->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002768
2769 /* Callref unknown */
2770 if (!trans) {
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01002771 struct gsm_subscriber *subscr;
2772
Harald Welte4a3464c2009-07-04 10:11:24 +02002773 if (msg_type != MNCC_SETUP_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002774 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2775 "Received '%s' from MNCC with "
2776 "unknown callref %d\n", data->called.number,
2777 get_mncc_name(msg_type), data->callref);
2778 /* Invalid call reference */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002779 return mncc_release_ind(net, NULL, data->callref,
2780 GSM48_CAUSE_LOC_PRN_S_LU,
2781 GSM48_CC_CAUSE_INVAL_TRANS_ID);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002782 }
Andreas Eversbergc079be42009-06-15 23:22:09 +02002783 if (!data->called.number[0] && !data->imsi[0]) {
2784 DEBUGP(DCC, "(bts - trx - ts - ti) "
2785 "Received '%s' from MNCC with "
2786 "no number or IMSI\n", get_mncc_name(msg_type));
2787 /* Invalid number */
2788 return mncc_release_ind(net, NULL, data->callref,
2789 GSM48_CAUSE_LOC_PRN_S_LU,
2790 GSM48_CC_CAUSE_INV_NR_FORMAT);
2791 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08002792 /* New transaction due to setup, find subscriber */
Andreas Eversbergc079be42009-06-15 23:22:09 +02002793 if (data->called.number[0])
Harald Welte9176bd42009-07-23 18:46:00 +02002794 subscr = subscr_get_by_extension(net,
2795 data->called.number);
Andreas Eversbergc079be42009-06-15 23:22:09 +02002796 else
Harald Welte9176bd42009-07-23 18:46:00 +02002797 subscr = subscr_get_by_imsi(net, data->imsi);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002798 /* If subscriber is not found */
2799 if (!subscr) {
2800 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2801 "Received '%s' from MNCC with "
2802 "unknown subscriber %s\n", data->called.number,
2803 get_mncc_name(msg_type), data->called.number);
2804 /* Unknown subscriber */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002805 return mncc_release_ind(net, NULL, data->callref,
2806 GSM48_CAUSE_LOC_PRN_S_LU,
2807 GSM48_CC_CAUSE_UNASSIGNED_NR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002808 }
2809 /* If subscriber is not "attached" */
2810 if (!subscr->lac) {
2811 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2812 "Received '%s' from MNCC with "
2813 "detached subscriber %s\n", data->called.number,
2814 get_mncc_name(msg_type), data->called.number);
2815 subscr_put(subscr);
2816 /* Temporarily out of order */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002817 return mncc_release_ind(net, NULL, data->callref,
2818 GSM48_CAUSE_LOC_PRN_S_LU,
2819 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002820 }
2821 /* Create transaction */
Harald Weltedcaf5652009-07-23 18:56:43 +02002822 trans = trans_alloc(subscr, GSM48_PDISC_CC, 0xff, data->callref);
2823 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002824 DEBUGP(DCC, "No memory for trans.\n");
2825 subscr_put(subscr);
2826 /* Ressource unavailable */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002827 mncc_release_ind(net, NULL, data->callref,
2828 GSM48_CAUSE_LOC_PRN_S_LU,
2829 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002830 return -ENOMEM;
2831 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08002832 /* Find lchan */
Harald Welte1a6f7982009-08-09 18:52:33 +02002833 lchan = lchan_for_subscr(subscr);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01002834
Harald Welte4bfdfe72009-06-10 23:11:52 +08002835 /* If subscriber has no lchan */
2836 if (!lchan) {
2837 /* find transaction with this subscriber already paging */
2838 llist_for_each_entry(transt, &net->trans_list, entry) {
2839 /* Transaction of our lchan? */
2840 if (transt == trans ||
2841 transt->subscr != subscr)
2842 continue;
2843 DEBUGP(DCC, "(bts %d trx - ts - ti -- sub %s) "
2844 "Received '%s' from MNCC with "
2845 "unallocated channel, paging already "
2846 "started.\n", bts->nr,
2847 data->called.number,
2848 get_mncc_name(msg_type));
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01002849 subscr_put(subscr);
2850 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002851 return 0;
2852 }
2853 /* store setup informations until paging was successfull */
Harald Weltedcaf5652009-07-23 18:56:43 +02002854 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
Harald Weltea1b28582009-08-01 19:31:47 +02002855 /* Trigger paging */
2856 paging_request(net, subscr, RSL_CHANNEED_TCH_F,
2857 setup_trig_pag_evt, subscr);
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01002858 subscr_put(subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002859 return 0;
2860 }
2861 /* Assign lchan */
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002862 trans->conn = &lchan->conn;
2863 use_subscr_con(trans->conn);
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01002864 subscr_put(subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002865 }
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002866
2867 if (trans->conn)
2868 lchan = trans->conn->lchan;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002869
2870 /* if paging did not respond yet */
2871 if (!lchan) {
2872 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02002873 "Received '%s' from MNCC in paging state\n",
Harald Welte4bfdfe72009-06-10 23:11:52 +08002874 (trans->subscr)?(trans->subscr->extension):"-",
2875 get_mncc_name(msg_type));
Harald Weltec66b71c2009-06-11 14:23:20 +08002876 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
2877 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002878 if (msg_type == MNCC_REL_REQ)
2879 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
2880 else
2881 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
2882 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002883 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002884 return rc;
2885 }
2886
2887 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x sub %s) "
2888 "Received '%s' from MNCC in state %d (%s)\n",
2889 lchan->ts->trx->bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
2890 trans->transaction_id,
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002891 (trans->conn->subscr)?(trans->conn->subscr->extension):"-",
Harald Weltedcaf5652009-07-23 18:56:43 +02002892 get_mncc_name(msg_type), trans->cc.state,
Harald Weltee95daf12010-03-25 12:13:02 +08002893 gsm48_cc_state_name(trans->cc.state));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002894
2895 /* Find function for current state and message */
2896 for (i = 0; i < DOWNSLLEN; i++)
2897 if ((msg_type == downstatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02002898 && ((1 << trans->cc.state) & downstatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08002899 break;
2900 if (i == DOWNSLLEN) {
2901 DEBUGP(DCC, "Message unhandled at this state.\n");
2902 return 0;
2903 }
2904
2905 rc = downstatelist[i].rout(trans, arg);
2906
2907 return rc;
2908}
2909
2910
2911static struct datastate {
2912 u_int32_t states;
2913 int type;
2914 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
2915} datastatelist[] = {
2916 /* mobile originating call establishment */
2917 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
2918 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
2919 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
2920 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
2921 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
2922 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
2923 /* mobile terminating call establishment */
2924 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
2925 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
2926 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
2927 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02002928 {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 +08002929 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
2930 /* signalling during call */
2931 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
2932 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
2933 {SBIT(GSM_CSTATE_ACTIVE),
2934 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
2935 {ALL_STATES,
2936 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
2937 {ALL_STATES,
2938 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
2939 {ALL_STATES,
2940 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
2941 {SBIT(GSM_CSTATE_ACTIVE),
2942 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
2943 {SBIT(GSM_CSTATE_ACTIVE),
2944 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
2945 {SBIT(GSM_CSTATE_ACTIVE),
2946 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
2947 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
2948 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
2949 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
2950 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
2951 {SBIT(GSM_CSTATE_ACTIVE),
2952 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
2953 /* clearing */
2954 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
2955 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
2956 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
2957 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
2958 {ALL_STATES, /* 5.4.3.4 */
2959 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
2960};
2961
2962#define DATASLLEN \
2963 (sizeof(datastatelist) / sizeof(struct datastate))
2964
Harald Welte4bc90a12008-12-27 16:32:52 +00002965static int gsm0408_rcv_cc(struct msgb *msg)
2966{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01002967 struct gsm_subscriber_connection *conn;
Harald Welte4bc90a12008-12-27 16:32:52 +00002968 struct gsm48_hdr *gh = msgb_l3(msg);
2969 u_int8_t msg_type = gh->msg_type & 0xbf;
Harald Welte6f5aee02009-07-23 21:21:14 +02002970 u_int8_t transaction_id = ((gh->proto_discr & 0xf0) ^ 0x80) >> 4; /* flip */
Harald Welte4bfdfe72009-06-10 23:11:52 +08002971 struct gsm_lchan *lchan = msg->lchan;
Harald Weltedcaf5652009-07-23 18:56:43 +02002972 struct gsm_trans *trans = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002973 int i, rc = 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00002974
Harald Welte4bfdfe72009-06-10 23:11:52 +08002975 if (msg_type & 0x80) {
2976 DEBUGP(DCC, "MSG 0x%2x not defined for PD error\n", msg_type);
2977 return -EINVAL;
Harald Welte4bc90a12008-12-27 16:32:52 +00002978 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01002979
2980 conn = &lchan->conn;
2981
Harald Welte4bfdfe72009-06-10 23:11:52 +08002982 /* Find transaction */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01002983 trans = trans_find_by_id(conn->subscr, GSM48_PDISC_CC, transaction_id);
Harald Weltedcaf5652009-07-23 18:56:43 +02002984
Harald Welte6f5aee02009-07-23 21:21:14 +02002985 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08002986 "Received '%s' from MS in state %d (%s)\n",
2987 lchan->ts->trx->bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01002988 transaction_id, (conn->subscr)?(conn->subscr->extension):"-",
Harald Weltee95daf12010-03-25 12:13:02 +08002989 gsm48_cc_msg_name(msg_type), trans?(trans->cc.state):0,
2990 gsm48_cc_state_name(trans?(trans->cc.state):0));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002991
2992 /* Create transaction */
2993 if (!trans) {
Harald Welte6f5aee02009-07-23 21:21:14 +02002994 DEBUGP(DCC, "Unknown transaction ID %x, "
Harald Welte4bfdfe72009-06-10 23:11:52 +08002995 "creating new trans.\n", transaction_id);
2996 /* Create transaction */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01002997 trans = trans_alloc(conn->subscr, GSM48_PDISC_CC,
Harald Weltedcaf5652009-07-23 18:56:43 +02002998 transaction_id, new_callref++);
2999 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003000 DEBUGP(DCC, "No memory for trans.\n");
3001 rc = gsm48_tx_simple(msg->lchan,
Harald Welte6f5aee02009-07-23 21:21:14 +02003002 GSM48_PDISC_CC | (transaction_id << 4),
Harald Welte4bfdfe72009-06-10 23:11:52 +08003003 GSM48_MT_CC_RELEASE_COMPL);
3004 return -ENOMEM;
3005 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003006 /* Assign transaction */
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003007 trans->conn = &lchan->conn;
3008 use_subscr_con(trans->conn);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003009 }
3010
3011 /* find function for current state and message */
3012 for (i = 0; i < DATASLLEN; i++)
3013 if ((msg_type == datastatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003014 && ((1 << trans->cc.state) & datastatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003015 break;
3016 if (i == DATASLLEN) {
3017 DEBUGP(DCC, "Message unhandled at this state.\n");
3018 return 0;
3019 }
3020
3021 rc = datastatelist[i].rout(trans, msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00003022
3023 return rc;
3024}
3025
Harald Welte52b1f982008-12-23 20:25:15 +00003026/* 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 +02003027int gsm0408_rcvmsg(struct msgb *msg, u_int8_t link_id)
Harald Welte52b1f982008-12-23 20:25:15 +00003028{
3029 struct gsm48_hdr *gh = msgb_l3(msg);
3030 u_int8_t pdisc = gh->proto_discr & 0x0f;
Harald Welte8470bf22008-12-25 23:28:35 +00003031 int rc = 0;
Harald Welte51008772009-12-29 11:49:12 +01003032
3033 if (silent_call_reroute(msg))
3034 return silent_call_rx(msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003035
3036 switch (pdisc) {
3037 case GSM48_PDISC_CC:
3038 rc = gsm0408_rcv_cc(msg);
3039 break;
3040 case GSM48_PDISC_MM:
3041 rc = gsm0408_rcv_mm(msg);
3042 break;
3043 case GSM48_PDISC_RR:
3044 rc = gsm0408_rcv_rr(msg);
3045 break;
Harald Weltebcae43f2008-12-27 21:45:37 +00003046 case GSM48_PDISC_SMS:
Harald Welte (local)daef6062009-08-14 11:41:12 +02003047 rc = gsm0411_rcv_sms(msg, link_id);
Harald Weltebcae43f2008-12-27 21:45:37 +00003048 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003049 case GSM48_PDISC_MM_GPRS:
Harald Weltebcae43f2008-12-27 21:45:37 +00003050 case GSM48_PDISC_SM_GPRS:
Harald Welte5d24ba12009-12-24 12:13:17 +01003051 LOGP(DRLL, LOGL_NOTICE, "Unimplemented "
3052 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Harald Welte52b1f982008-12-23 20:25:15 +00003053 break;
Harald Welte6eafe912009-10-16 08:32:58 +02003054 case GSM48_PDISC_NC_SS:
3055 rc = handle_rcv_ussd(msg);
3056 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003057 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01003058 LOGP(DRLL, LOGL_NOTICE, "Unknown "
3059 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Harald Welte52b1f982008-12-23 20:25:15 +00003060 break;
3061 }
3062
3063 return rc;
3064}
Harald Welte8470bf22008-12-25 23:28:35 +00003065
Harald Welte805f6442009-07-28 18:25:29 +02003066/*
3067 * This will be ran by the linker when loading the DSO. We use it to
3068 * do system initialization, e.g. registration of signal handlers.
3069 */
3070static __attribute__((constructor)) void on_dso_load_0408(void)
3071{
Harald Welte805f6442009-07-28 18:25:29 +02003072 register_signal_handler(SS_LCHAN, gsm0408_handle_lchan_signal, NULL);
3073 register_signal_handler(SS_ABISIP, handle_abisip_signal, NULL);
3074}