blob: 2eda0bd991335f4b4913a7edfec91ce45987756a [file] [log] [blame]
Harald Welte59b04682009-06-10 05:40:52 +08001/* GSM Mobile Radio Interface Layer 3 messages on the A-bis interface
2 * 3GPP TS 04.08 version 7.21.0 Release 1998 / ETSI TS 100 940 V7.21.0 */
3
4/* (C) 2008-2009 by Harald Welte <laforge@gnumonks.org>
5 * (C) 2008, 2009 by Holger Hans Peter Freyther <zecke@selfish.org>
6 *
7 * 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>
30#include <time.h>
31#include <netinet/in.h>
32
33#include <openbsc/db.h>
Harald Weltef4625b12010-02-20 16:24:02 +010034#include <osmocore/msgb.h>
35#include <osmocore/bitvec.h>
36#include <osmocore/tlv.h>
Harald Welte59b04682009-06-10 05:40:52 +080037#include <openbsc/debug.h>
38#include <openbsc/gsm_data.h>
Harald Weltef4625b12010-02-20 16:24:02 +010039#include <osmocore/gsm_utils.h>
Harald Welte59b04682009-06-10 05:40:52 +080040#include <openbsc/gsm_subscriber.h>
41#include <openbsc/gsm_04_11.h>
42#include <openbsc/gsm_04_08.h>
43#include <openbsc/abis_rsl.h>
44#include <openbsc/chan_alloc.h>
45#include <openbsc/paging.h>
46#include <openbsc/signal.h>
47#include <openbsc/trau_frame.h>
48#include <openbsc/trau_mux.h>
Harald Welte3c062072009-07-28 18:25:29 +020049#include <openbsc/rtp_proxy.h>
Harald Weltef4625b12010-02-20 16:24:02 +010050#include <osmocore/talloc.h>
Harald Weltefdc93d92010-03-07 23:40:35 +010051#include <osmocore/gsm48.h>
Harald Weltec2189a62009-07-23 18:56:43 +020052#include <openbsc/transaction.h>
Harald Welte03115042009-10-16 08:32:58 +020053#include <openbsc/ussd.h>
Harald Weltef6a38ec2009-12-29 11:49:12 +010054#include <openbsc/silent_call.h>
Harald Welte59b04682009-06-10 05:40:52 +080055
Harald Welte (local)8751ee92009-08-15 02:30:58 +020056void *tall_locop_ctx;
Harald Weltea8379772009-06-20 22:36:41 +020057
Harald Welte59b04682009-06-10 05:40:52 +080058int gsm0408_loc_upd_acc(struct gsm_lchan *lchan, u_int32_t tmsi);
59static int gsm48_tx_simple(struct gsm_lchan *lchan,
60 u_int8_t pdisc, u_int8_t msg_type);
61static void schedule_reject(struct gsm_lchan *lchan);
62
63struct gsm_lai {
64 u_int16_t mcc;
65 u_int16_t mnc;
66 u_int16_t lac;
67};
68
Harald Welte03740842009-06-10 23:11:52 +080069static u_int32_t new_callref = 0x80000001;
70
Harald Welte59b04682009-06-10 05:40:52 +080071static int authorize_subscriber(struct gsm_loc_updating_operation *loc,
72 struct gsm_subscriber *subscriber)
73{
74 if (!subscriber)
75 return 0;
76
77 /*
78 * Do not send accept yet as more information should arrive. Some
79 * phones will not send us the information and we will have to check
80 * what we want to do with that.
81 */
82 if (loc && (loc->waiting_for_imsi || loc->waiting_for_imei))
83 return 0;
84
Jan Luebbe3d6294e2009-08-12 12:48:00 +020085 switch (subscriber->net->auth_policy) {
86 case GSM_AUTH_POLICY_CLOSED:
87 return subscriber->authorized;
Harald Welte (local)16480eb2009-08-13 13:49:51 +020088 case GSM_AUTH_POLICY_TOKEN:
Harald Welte (local)272f5c72009-08-13 20:44:23 +020089 if (subscriber->authorized)
90 return subscriber->authorized;
Harald Welte (local)16480eb2009-08-13 13:49:51 +020091 return (subscriber->flags & GSM_SUBSCRIBER_FIRST_CONTACT);
Jan Luebbe3d6294e2009-08-12 12:48:00 +020092 case GSM_AUTH_POLICY_ACCEPT_ALL:
Harald Welte59b04682009-06-10 05:40:52 +080093 return 1;
Jan Luebbe3d6294e2009-08-12 12:48:00 +020094 default:
95 return 0;
96 }
Harald Welte59b04682009-06-10 05:40:52 +080097}
98
99static void release_loc_updating_req(struct gsm_lchan *lchan)
100{
101 if (!lchan->loc_operation)
102 return;
103
104 bsc_del_timer(&lchan->loc_operation->updating_timer);
Harald Weltea8379772009-06-20 22:36:41 +0200105 talloc_free(lchan->loc_operation);
Harald Welte59b04682009-06-10 05:40:52 +0800106 lchan->loc_operation = 0;
107 put_lchan(lchan);
108}
109
110static void allocate_loc_updating_req(struct gsm_lchan *lchan)
111{
112 use_lchan(lchan);
113 release_loc_updating_req(lchan);
114
Harald Welte857e00d2009-06-26 20:25:23 +0200115 lchan->loc_operation = talloc_zero(tall_locop_ctx,
116 struct gsm_loc_updating_operation);
Harald Welte59b04682009-06-10 05:40:52 +0800117}
118
119static int gsm0408_authorize(struct gsm_lchan *lchan, struct msgb *msg)
120{
Harald Welte59b04682009-06-10 05:40:52 +0800121 if (authorize_subscriber(lchan->loc_operation, lchan->subscr)) {
Harald Welte5070e942009-08-09 19:07:00 +0200122 int rc;
123
Harald Welte59b04682009-06-10 05:40:52 +0800124 db_subscriber_alloc_tmsi(lchan->subscr);
Harald Welte59b04682009-06-10 05:40:52 +0800125 release_loc_updating_req(lchan);
Holger Hans Peter Freythercd8bacf2009-08-19 12:53:57 +0200126 rc = gsm0408_loc_upd_acc(msg->lchan, lchan->subscr->tmsi);
Harald Weltea310f3e2009-12-14 09:00:24 +0100127 if (lchan->ts->trx->bts->network->send_mm_info) {
128 /* send MM INFO with network name */
129 rc = gsm48_tx_mm_info(msg->lchan);
130 }
Harald Welte9ed54ff2009-12-12 21:00:48 +0100131
Harald Welte5070e942009-08-09 19:07:00 +0200132 /* call subscr_update after putting the loc_upd_acc
133 * in the transmit queue, since S_SUBSCR_ATTACHED might
134 * trigger further action like SMS delivery */
135 subscr_update(lchan->subscr, msg->trx->bts,
136 GSM_SUBSCRIBER_UPDATE_ATTACHED);
Harald Welte9ed54ff2009-12-12 21:00:48 +0100137 /* try to close channel ASAP */
138 lchan_auto_release(lchan);
Harald Welte5070e942009-08-09 19:07:00 +0200139 return rc;
Harald Welte59b04682009-06-10 05:40:52 +0800140 }
141
142 return 0;
143}
144
145static int gsm0408_handle_lchan_signal(unsigned int subsys, unsigned int signal,
146 void *handler_data, void *signal_data)
147{
Harald Welte03740842009-06-10 23:11:52 +0800148 struct gsm_trans *trans, *temp;
149
Harald Welte59b04682009-06-10 05:40:52 +0800150 if (subsys != SS_LCHAN || signal != S_LCHAN_UNEXPECTED_RELEASE)
151 return 0;
152
153 /*
154 * Cancel any outstanding location updating request
155 * operation taking place on the lchan.
156 */
Harald Welte12560da2009-07-04 09:35:21 +0200157 struct gsm_lchan *lchan = (struct gsm_lchan *)signal_data;
Harald Welte1ff81b52009-06-26 20:17:06 +0200158 if (!lchan)
159 return 0;
160
Harald Welte59b04682009-06-10 05:40:52 +0800161 release_loc_updating_req(lchan);
162
Harald Welte03740842009-06-10 23:11:52 +0800163 /* Free all transactions that are associated with the released lchan */
Harald Weltec2189a62009-07-23 18:56:43 +0200164 /* FIXME: this is not neccessarily the right thing to do, we should
165 * only set trans->lchan to NULL and wait for another lchan to be
166 * established to the same MM entity (phone/subscriber) */
Harald Welte03740842009-06-10 23:11:52 +0800167 llist_for_each_entry_safe(trans, temp, &lchan->ts->trx->bts->network->trans_list, entry) {
168 if (trans->lchan == lchan)
Harald Weltec2189a62009-07-23 18:56:43 +0200169 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +0800170 }
171
Harald Welte59b04682009-06-10 05:40:52 +0800172 return 0;
173}
174
Harald Welte59b04682009-06-10 05:40:52 +0800175/* Chapter 9.2.14 : Send LOCATION UPDATING REJECT */
176int gsm0408_loc_upd_rej(struct gsm_lchan *lchan, u_int8_t cause)
177{
Harald Welte3edc5a92009-12-22 00:41:05 +0100178 struct gsm_bts *bts = lchan->ts->trx->bts;
Harald Welte59b04682009-06-10 05:40:52 +0800179 struct msgb *msg = gsm48_msgb_alloc();
180 struct gsm48_hdr *gh;
181
182 msg->lchan = lchan;
183
184 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
185 gh->proto_discr = GSM48_PDISC_MM;
186 gh->msg_type = GSM48_MT_MM_LOC_UPD_REJECT;
187 gh->data[0] = cause;
188
Harald Welte (local)38a9e5d2009-12-26 22:15:28 +0100189 LOGP(DMM, LOGL_INFO, "Subscriber %s: LOCATION UPDATING REJECT "
190 "LAC=%u BTS=%u\n", lchan->subscr ?
191 subscr_name(lchan->subscr) : "unknown",
192 lchan->ts->trx->bts->location_area_code, lchan->ts->trx->bts->nr);
Harald Welte3edc5a92009-12-22 00:41:05 +0100193
Harald Weltebdbb7442009-12-22 19:07:32 +0100194 counter_inc(bts->network->stats.loc_upd_resp.reject);
Harald Welte59b04682009-06-10 05:40:52 +0800195
Harald Welte36fe2e82009-07-23 21:13:03 +0200196 return gsm48_sendmsg(msg, NULL);
Harald Welte59b04682009-06-10 05:40:52 +0800197}
198
199/* Chapter 9.2.13 : Send LOCATION UPDATE ACCEPT */
200int gsm0408_loc_upd_acc(struct gsm_lchan *lchan, u_int32_t tmsi)
201{
202 struct gsm_bts *bts = lchan->ts->trx->bts;
203 struct msgb *msg = gsm48_msgb_alloc();
204 struct gsm48_hdr *gh;
205 struct gsm48_loc_area_id *lai;
206 u_int8_t *mid;
Harald Welte59b04682009-06-10 05:40:52 +0800207
208 msg->lchan = lchan;
209
210 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
211 gh->proto_discr = GSM48_PDISC_MM;
212 gh->msg_type = GSM48_MT_MM_LOC_UPD_ACCEPT;
213
214 lai = (struct gsm48_loc_area_id *) msgb_put(msg, sizeof(*lai));
Harald Welted8493ac2010-03-04 10:55:40 +0100215 gsm48_generate_lai(lai, bts->network->country_code,
Harald Welte59b04682009-06-10 05:40:52 +0800216 bts->network->network_code, bts->location_area_code);
217
Holger Hans Peter Freyther36897102009-08-01 07:26:59 +0200218 mid = msgb_put(msg, GSM48_MID_TMSI_LEN);
Holger Hans Peter Freyther20a0f322009-08-20 08:41:24 +0200219 gsm48_generate_mid_from_tmsi(mid, tmsi);
Harald Welte59b04682009-06-10 05:40:52 +0800220
221 DEBUGP(DMM, "-> LOCATION UPDATE ACCEPT\n");
222
Harald Weltebdbb7442009-12-22 19:07:32 +0100223 counter_inc(bts->network->stats.loc_upd_resp.accept);
Harald Welte3edc5a92009-12-22 00:41:05 +0100224
Harald Welte9ed54ff2009-12-12 21:00:48 +0100225 return gsm48_sendmsg(msg, NULL);
Harald Welte59b04682009-06-10 05:40:52 +0800226}
227
Harald Welte59b04682009-06-10 05:40:52 +0800228/* Transmit Chapter 9.2.10 Identity Request */
229static int mm_tx_identity_req(struct gsm_lchan *lchan, u_int8_t id_type)
230{
231 struct msgb *msg = gsm48_msgb_alloc();
232 struct gsm48_hdr *gh;
233
234 msg->lchan = lchan;
235
236 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
237 gh->proto_discr = GSM48_PDISC_MM;
238 gh->msg_type = GSM48_MT_MM_ID_REQ;
239 gh->data[0] = id_type;
240
Harald Welte36fe2e82009-07-23 21:13:03 +0200241 return gsm48_sendmsg(msg, NULL);
Harald Welte59b04682009-06-10 05:40:52 +0800242}
243
Harald Welte59b04682009-06-10 05:40:52 +0800244
245/* Parse Chapter 9.2.11 Identity Response */
246static int mm_rx_id_resp(struct msgb *msg)
247{
248 struct gsm48_hdr *gh = msgb_l3(msg);
249 struct gsm_lchan *lchan = msg->lchan;
Harald Welte75350412009-07-23 18:46:00 +0200250 struct gsm_bts *bts = lchan->ts->trx->bts;
251 struct gsm_network *net = bts->network;
Harald Welte59b04682009-06-10 05:40:52 +0800252 u_int8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +0200253 char mi_string[GSM48_MI_SIZE];
Harald Welte59b04682009-06-10 05:40:52 +0800254
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +0200255 gsm48_mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
Harald Welte59b04682009-06-10 05:40:52 +0800256 DEBUGP(DMM, "IDENTITY RESPONSE: mi_type=0x%02x MI(%s)\n",
257 mi_type, mi_string);
258
Harald Welteedf48af2009-12-13 12:39:18 +0100259 dispatch_signal(SS_SUBSCR, S_SUBSCR_IDENTITY, gh->data);
260
Harald Welte59b04682009-06-10 05:40:52 +0800261 switch (mi_type) {
262 case GSM_MI_TYPE_IMSI:
Jan Luebbe9bdbd622009-08-12 10:19:34 +0200263 /* look up subscriber based on IMSI, create if not found */
264 if (!lchan->subscr) {
265 lchan->subscr = subscr_get_by_imsi(net, mi_string);
Harald Welte (local)16480eb2009-08-13 13:49:51 +0200266 if (!lchan->subscr)
267 lchan->subscr = db_create_subscriber(net, mi_string);
Jan Luebbee2974032009-08-12 10:12:52 +0200268 }
Harald Welte59b04682009-06-10 05:40:52 +0800269 if (lchan->loc_operation)
270 lchan->loc_operation->waiting_for_imsi = 0;
271 break;
272 case GSM_MI_TYPE_IMEI:
273 case GSM_MI_TYPE_IMEISV:
274 /* update subscribe <-> IMEI mapping */
Harald Welte (local)ced09ed2009-08-17 09:39:55 +0200275 if (lchan->subscr) {
Harald Welte59b04682009-06-10 05:40:52 +0800276 db_subscriber_assoc_imei(lchan->subscr, mi_string);
Harald Welte (local)ced09ed2009-08-17 09:39:55 +0200277 db_sync_equipment(&lchan->subscr->equipment);
278 }
Harald Welte59b04682009-06-10 05:40:52 +0800279 if (lchan->loc_operation)
280 lchan->loc_operation->waiting_for_imei = 0;
281 break;
282 }
283
284 /* Check if we can let the mobile station enter */
285 return gsm0408_authorize(lchan, msg);
286}
287
288
289static void loc_upd_rej_cb(void *data)
290{
291 struct gsm_lchan *lchan = data;
Harald Welte59936d72009-11-18 20:33:19 +0100292 struct gsm_bts *bts = lchan->ts->trx->bts;
Harald Welte59b04682009-06-10 05:40:52 +0800293
294 release_loc_updating_req(lchan);
Harald Welte59936d72009-11-18 20:33:19 +0100295 gsm0408_loc_upd_rej(lchan, bts->network->reject_cause);
Harald Welte59b04682009-06-10 05:40:52 +0800296 lchan_auto_release(lchan);
297}
298
299static void schedule_reject(struct gsm_lchan *lchan)
300{
301 lchan->loc_operation->updating_timer.cb = loc_upd_rej_cb;
302 lchan->loc_operation->updating_timer.data = lchan;
303 bsc_schedule_timer(&lchan->loc_operation->updating_timer, 5, 0);
304}
305
306static const char *lupd_name(u_int8_t type)
307{
308 switch (type) {
309 case GSM48_LUPD_NORMAL:
310 return "NORMAL";
311 case GSM48_LUPD_PERIODIC:
312 return "PEROIDOC";
313 case GSM48_LUPD_IMSI_ATT:
314 return "IMSI ATTACH";
315 default:
316 return "UNKNOWN";
317 }
318}
319
Harald Welte59b04682009-06-10 05:40:52 +0800320/* Chapter 9.2.15: Receive Location Updating Request */
321static int mm_rx_loc_upd_req(struct msgb *msg)
322{
323 struct gsm48_hdr *gh = msgb_l3(msg);
324 struct gsm48_loc_upd_req *lu;
Harald Welte03740842009-06-10 23:11:52 +0800325 struct gsm_subscriber *subscr = NULL;
Harald Welte59b04682009-06-10 05:40:52 +0800326 struct gsm_lchan *lchan = msg->lchan;
Harald Welte75350412009-07-23 18:46:00 +0200327 struct gsm_bts *bts = lchan->ts->trx->bts;
Harald Welte59b04682009-06-10 05:40:52 +0800328 u_int8_t mi_type;
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +0200329 char mi_string[GSM48_MI_SIZE];
Harald Welte59b04682009-06-10 05:40:52 +0800330 int rc;
331
332 lu = (struct gsm48_loc_upd_req *) gh->data;
333
334 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
335
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +0200336 gsm48_mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
Harald Welte59b04682009-06-10 05:40:52 +0800337
Harald Welte79639662009-06-27 02:58:43 +0200338 DEBUGPC(DMM, "mi_type=0x%02x MI(%s) type=%s ", mi_type, mi_string,
Harald Welte59b04682009-06-10 05:40:52 +0800339 lupd_name(lu->type));
340
Harald Welteedf48af2009-12-13 12:39:18 +0100341 dispatch_signal(SS_SUBSCR, S_SUBSCR_IDENTITY, &lu->mi_len);
342
Harald Welte3edc5a92009-12-22 00:41:05 +0100343 switch (lu->type) {
344 case GSM48_LUPD_NORMAL:
Harald Weltebdbb7442009-12-22 19:07:32 +0100345 counter_inc(bts->network->stats.loc_upd_type.normal);
Harald Welte3edc5a92009-12-22 00:41:05 +0100346 break;
347 case GSM48_LUPD_IMSI_ATT:
Harald Weltebdbb7442009-12-22 19:07:32 +0100348 counter_inc(bts->network->stats.loc_upd_type.attach);
Harald Welte3edc5a92009-12-22 00:41:05 +0100349 break;
350 case GSM48_LUPD_PERIODIC:
Harald Weltebdbb7442009-12-22 19:07:32 +0100351 counter_inc(bts->network->stats.loc_upd_type.periodic);
Harald Welte3edc5a92009-12-22 00:41:05 +0100352 break;
353 }
354
Harald Welte59b04682009-06-10 05:40:52 +0800355 /*
356 * Pseudo Spoof detection: Just drop a second/concurrent
357 * location updating request.
358 */
359 if (lchan->loc_operation) {
Harald Welte79639662009-06-27 02:58:43 +0200360 DEBUGPC(DMM, "ignoring request due an existing one: %p.\n",
Harald Welte59b04682009-06-10 05:40:52 +0800361 lchan->loc_operation);
362 gsm0408_loc_upd_rej(lchan, GSM48_REJECT_PROTOCOL_ERROR);
363 return 0;
364 }
365
366 allocate_loc_updating_req(lchan);
367
368 switch (mi_type) {
369 case GSM_MI_TYPE_IMSI:
Harald Welte79639662009-06-27 02:58:43 +0200370 DEBUGPC(DMM, "\n");
Harald Welte59b04682009-06-10 05:40:52 +0800371 /* we always want the IMEI, too */
Harald Welte59b04682009-06-10 05:40:52 +0800372 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMEI);
373 lchan->loc_operation->waiting_for_imei = 1;
374
Jan Luebbe9bdbd622009-08-12 10:19:34 +0200375 /* look up subscriber based on IMSI, create if not found */
376 subscr = subscr_get_by_imsi(bts->network, mi_string);
377 if (!subscr) {
378 subscr = db_create_subscriber(bts->network, mi_string);
379 }
Harald Welte59b04682009-06-10 05:40:52 +0800380 break;
381 case GSM_MI_TYPE_TMSI:
Harald Welte79639662009-06-27 02:58:43 +0200382 DEBUGPC(DMM, "\n");
Harald Welte59b04682009-06-10 05:40:52 +0800383 /* we always want the IMEI, too */
Harald Welte59b04682009-06-10 05:40:52 +0800384 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMEI);
385 lchan->loc_operation->waiting_for_imei = 1;
386
387 /* look up the subscriber based on TMSI, request IMSI if it fails */
Holger Hans Peter Freythercd8bacf2009-08-19 12:53:57 +0200388 subscr = subscr_get_by_tmsi(bts->network,
389 tmsi_from_string(mi_string));
Harald Welte59b04682009-06-10 05:40:52 +0800390 if (!subscr) {
391 /* send IDENTITY REQUEST message to get IMSI */
Harald Welte59b04682009-06-10 05:40:52 +0800392 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMSI);
393 lchan->loc_operation->waiting_for_imsi = 1;
394 }
395 break;
396 case GSM_MI_TYPE_IMEI:
397 case GSM_MI_TYPE_IMEISV:
398 /* no sim card... FIXME: what to do ? */
Harald Welte79639662009-06-27 02:58:43 +0200399 DEBUGPC(DMM, "unimplemented mobile identity type\n");
Harald Welte59b04682009-06-10 05:40:52 +0800400 break;
401 default:
Harald Welte79639662009-06-27 02:58:43 +0200402 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte59b04682009-06-10 05:40:52 +0800403 break;
404 }
405
Harald Welteccd69362009-07-04 10:18:00 +0200406 /* schedule the reject timer */
407 schedule_reject(lchan);
408
Harald Welte03740842009-06-10 23:11:52 +0800409 if (!subscr) {
Harald Welte79639662009-06-27 02:58:43 +0200410 DEBUGPC(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte03740842009-06-10 23:11:52 +0800411 /* FIXME: request id? close channel? */
412 return -EINVAL;
413 }
414
Harald Welte59b04682009-06-10 05:40:52 +0800415 lchan->subscr = subscr;
Harald Welte (local)ced09ed2009-08-17 09:39:55 +0200416 lchan->subscr->equipment.classmark1 = lu->classmark1;
Harald Welte59b04682009-06-10 05:40:52 +0800417
Harald Welteccd69362009-07-04 10:18:00 +0200418 /* check if we can let the subscriber into our network immediately
419 * or if we need to wait for identity responses. */
Harald Welte59b04682009-06-10 05:40:52 +0800420 return gsm0408_authorize(lchan, msg);
421}
422
Harald Welte03740842009-06-10 23:11:52 +0800423#if 0
424static u_int8_t to_bcd8(u_int8_t val)
425{
426 return ((val / 10) << 4) | (val % 10);
427}
428#endif
429
Harald Welte59b04682009-06-10 05:40:52 +0800430/* Section 9.2.15a */
431int gsm48_tx_mm_info(struct gsm_lchan *lchan)
432{
433 struct msgb *msg = gsm48_msgb_alloc();
434 struct gsm48_hdr *gh;
435 struct gsm_network *net = lchan->ts->trx->bts->network;
436 u_int8_t *ptr8;
Daniel Willmannc8188202009-08-13 03:42:07 +0200437 int name_len, name_pad;
Harald Welte03740842009-06-10 23:11:52 +0800438#if 0
439 time_t cur_t;
440 struct tm* cur_time;
441 int tz15min;
442#endif
Harald Welte59b04682009-06-10 05:40:52 +0800443
444 msg->lchan = lchan;
445
446 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
447 gh->proto_discr = GSM48_PDISC_MM;
448 gh->msg_type = GSM48_MT_MM_INFO;
449
450 if (net->name_long) {
Daniel Willmannc8188202009-08-13 03:42:07 +0200451#if 0
Harald Welte59b04682009-06-10 05:40:52 +0800452 name_len = strlen(net->name_long);
453 /* 10.5.3.5a */
454 ptr8 = msgb_put(msg, 3);
455 ptr8[0] = GSM48_IE_NAME_LONG;
456 ptr8[1] = name_len*2 +1;
457 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
458
459 ptr16 = (u_int16_t *) msgb_put(msg, name_len*2);
460 for (i = 0; i < name_len; i++)
461 ptr16[i] = htons(net->name_long[i]);
462
463 /* FIXME: Use Cell Broadcast, not UCS-2, since
464 * UCS-2 is only supported by later revisions of the spec */
Daniel Willmannc8188202009-08-13 03:42:07 +0200465#endif
466 name_len = (strlen(net->name_long)*7)/8;
467 name_pad = (8 - strlen(net->name_long)*7)%8;
468 if (name_pad > 0)
469 name_len++;
470 /* 10.5.3.5a */
471 ptr8 = msgb_put(msg, 3);
472 ptr8[0] = GSM48_IE_NAME_LONG;
473 ptr8[1] = name_len +1;
474 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
475
476 ptr8 = msgb_put(msg, name_len);
477 gsm_7bit_encode(ptr8, net->name_long);
478
Harald Welte59b04682009-06-10 05:40:52 +0800479 }
480
481 if (net->name_short) {
Daniel Willmannc8188202009-08-13 03:42:07 +0200482#if 0
Harald Welte59b04682009-06-10 05:40:52 +0800483 name_len = strlen(net->name_short);
484 /* 10.5.3.5a */
485 ptr8 = (u_int8_t *) msgb_put(msg, 3);
Harald Weltef6284712009-07-19 17:51:36 +0200486 ptr8[0] = GSM48_IE_NAME_SHORT;
Harald Welte59b04682009-06-10 05:40:52 +0800487 ptr8[1] = name_len*2 + 1;
488 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
489
490 ptr16 = (u_int16_t *) msgb_put(msg, name_len*2);
491 for (i = 0; i < name_len; i++)
492 ptr16[i] = htons(net->name_short[i]);
Daniel Willmannc8188202009-08-13 03:42:07 +0200493#endif
494 name_len = (strlen(net->name_short)*7)/8;
495 name_pad = (8 - strlen(net->name_short)*7)%8;
496 if (name_pad > 0)
497 name_len++;
498 /* 10.5.3.5a */
499 ptr8 = (u_int8_t *) msgb_put(msg, 3);
500 ptr8[0] = GSM48_IE_NAME_SHORT;
501 ptr8[1] = name_len +1;
502 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
503
504 ptr8 = msgb_put(msg, name_len);
505 gsm_7bit_encode(ptr8, net->name_short);
506
Harald Welte59b04682009-06-10 05:40:52 +0800507 }
508
509#if 0
Harald Welte59b04682009-06-10 05:40:52 +0800510 /* Section 10.5.3.9 */
511 cur_t = time(NULL);
Harald Welte03740842009-06-10 23:11:52 +0800512 cur_time = gmtime(&cur_t);
Harald Welte59b04682009-06-10 05:40:52 +0800513 ptr8 = msgb_put(msg, 8);
514 ptr8[0] = GSM48_IE_NET_TIME_TZ;
515 ptr8[1] = to_bcd8(cur_time->tm_year % 100);
516 ptr8[2] = to_bcd8(cur_time->tm_mon);
517 ptr8[3] = to_bcd8(cur_time->tm_mday);
518 ptr8[4] = to_bcd8(cur_time->tm_hour);
519 ptr8[5] = to_bcd8(cur_time->tm_min);
520 ptr8[6] = to_bcd8(cur_time->tm_sec);
521 /* 02.42: coded as BCD encoded signed value in units of 15 minutes */
522 tz15min = (cur_time->tm_gmtoff)/(60*15);
Harald Welte03740842009-06-10 23:11:52 +0800523 ptr8[7] = to_bcd8(tz15min);
Harald Welte59b04682009-06-10 05:40:52 +0800524 if (tz15min < 0)
Harald Welte03740842009-06-10 23:11:52 +0800525 ptr8[7] |= 0x80;
Harald Welte59b04682009-06-10 05:40:52 +0800526#endif
527
Daniel Willmannc8188202009-08-13 03:42:07 +0200528 DEBUGP(DMM, "-> MM INFO\n");
529
Harald Welte36fe2e82009-07-23 21:13:03 +0200530 return gsm48_sendmsg(msg, NULL);
Harald Welte59b04682009-06-10 05:40:52 +0800531}
532
Harald Welte36970f62009-08-12 22:56:50 +0200533/* Section 9.2.2 */
Sylvain Munaut0789c592009-12-24 00:23:46 +0100534int gsm48_tx_mm_auth_req(struct gsm_lchan *lchan, u_int8_t *rand, int key_seq)
Harald Welte36970f62009-08-12 22:56:50 +0200535{
536 struct msgb *msg = gsm48_msgb_alloc();
537 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Sylvain Munaut6de6dc12009-09-27 11:10:17 +0200538 struct gsm48_auth_req *ar = (struct gsm48_auth_req *) msgb_put(msg, sizeof(*ar));
Harald Welte36970f62009-08-12 22:56:50 +0200539
Sylvain Munautb1a4f282009-12-24 00:24:29 +0100540 DEBUGP(DMM, "-> AUTH REQ (rand = %s)\n", hexdump(rand, 16));
Harald Welte36970f62009-08-12 22:56:50 +0200541
542 msg->lchan = lchan;
543 gh->proto_discr = GSM48_PDISC_MM;
544 gh->msg_type = GSM48_MT_MM_AUTH_REQ;
545
Sylvain Munaut0789c592009-12-24 00:23:46 +0100546 ar->key_seq = key_seq;
Sylvain Munaut6de6dc12009-09-27 11:10:17 +0200547
Harald Welte36970f62009-08-12 22:56:50 +0200548 /* 16 bytes RAND parameters */
Harald Welte36970f62009-08-12 22:56:50 +0200549 if (rand)
Sylvain Munaut6de6dc12009-09-27 11:10:17 +0200550 memcpy(ar->rand, rand, 16);
Harald Welte36970f62009-08-12 22:56:50 +0200551
552 return gsm48_sendmsg(msg, NULL);
553}
554
555/* Section 9.2.1 */
556int gsm48_tx_mm_auth_rej(struct gsm_lchan *lchan)
557{
558 DEBUGP(DMM, "-> AUTH REJECT\n");
559 return gsm48_tx_simple(lchan, GSM48_PDISC_MM, GSM48_MT_MM_AUTH_REJ);
560}
561
Harald Welte59b04682009-06-10 05:40:52 +0800562static int gsm48_tx_mm_serv_ack(struct gsm_lchan *lchan)
563{
564 DEBUGP(DMM, "-> CM SERVICE ACK\n");
565 return gsm48_tx_simple(lchan, GSM48_PDISC_MM, GSM48_MT_MM_CM_SERV_ACC);
566}
567
568/* 9.2.6 CM service reject */
569static int gsm48_tx_mm_serv_rej(struct gsm_lchan *lchan,
570 enum gsm48_reject_value value)
571{
572 struct msgb *msg = gsm48_msgb_alloc();
573 struct gsm48_hdr *gh;
574
575 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
576
577 msg->lchan = lchan;
578 use_lchan(lchan);
579
580 gh->proto_discr = GSM48_PDISC_MM;
581 gh->msg_type = GSM48_MT_MM_CM_SERV_REJ;
582 gh->data[0] = value;
583 DEBUGP(DMM, "-> CM SERVICE Reject cause: %d\n", value);
584
Harald Welte36fe2e82009-07-23 21:13:03 +0200585 return gsm48_sendmsg(msg, NULL);
Harald Welte59b04682009-06-10 05:40:52 +0800586}
587
Harald Welte59b04682009-06-10 05:40:52 +0800588/*
589 * Handle CM Service Requests
590 * a) Verify that the packet is long enough to contain the information
591 * we require otherwsie reject with INCORRECT_MESSAGE
592 * b) Try to parse the TMSI. If we do not have one reject
593 * c) Check that we know the subscriber with the TMSI otherwise reject
594 * with a HLR cause
595 * d) Set the subscriber on the gsm_lchan and accept
596 */
597static int gsm48_rx_mm_serv_req(struct msgb *msg)
598{
599 u_int8_t mi_type;
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +0200600 char mi_string[GSM48_MI_SIZE];
Harald Welte59b04682009-06-10 05:40:52 +0800601
Harald Welte75350412009-07-23 18:46:00 +0200602 struct gsm_bts *bts = msg->lchan->ts->trx->bts;
Harald Welte59b04682009-06-10 05:40:52 +0800603 struct gsm_subscriber *subscr;
604 struct gsm48_hdr *gh = msgb_l3(msg);
605 struct gsm48_service_request *req =
606 (struct gsm48_service_request *)gh->data;
607 /* unfortunately in Phase1 the classmar2 length is variable */
608 u_int8_t classmark2_len = gh->data[1];
609 u_int8_t *classmark2 = gh->data+2;
610 u_int8_t mi_len = *(classmark2 + classmark2_len);
611 u_int8_t *mi = (classmark2 + classmark2_len + 1);
612
613 DEBUGP(DMM, "<- CM SERVICE REQUEST ");
614 if (msg->data_len < sizeof(struct gsm48_service_request*)) {
615 DEBUGPC(DMM, "wrong sized message\n");
616 return gsm48_tx_mm_serv_rej(msg->lchan,
617 GSM48_REJECT_INCORRECT_MESSAGE);
618 }
619
620 if (msg->data_len < req->mi_len + 6) {
621 DEBUGPC(DMM, "does not fit in packet\n");
622 return gsm48_tx_mm_serv_rej(msg->lchan,
623 GSM48_REJECT_INCORRECT_MESSAGE);
624 }
625
626 mi_type = mi[0] & GSM_MI_TYPE_MASK;
627 if (mi_type != GSM_MI_TYPE_TMSI) {
628 DEBUGPC(DMM, "mi_type is not TMSI: %d\n", mi_type);
629 return gsm48_tx_mm_serv_rej(msg->lchan,
630 GSM48_REJECT_INCORRECT_MESSAGE);
631 }
632
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +0200633 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
Harald Welte59b04682009-06-10 05:40:52 +0800634 DEBUGPC(DMM, "serv_type=0x%02x mi_type=0x%02x M(%s)\n",
635 req->cm_service_type, mi_type, mi_string);
636
Harald Welteedf48af2009-12-13 12:39:18 +0100637 dispatch_signal(SS_SUBSCR, S_SUBSCR_IDENTITY, (classmark2 + classmark2_len));
638
Harald Welte583ceaf2009-08-10 10:12:45 +0200639 if (is_siemens_bts(bts))
640 send_siemens_mrpci(msg->lchan, classmark2-1);
641
Holger Hans Peter Freythercd8bacf2009-08-19 12:53:57 +0200642 subscr = subscr_get_by_tmsi(bts->network,
643 tmsi_from_string(mi_string));
Harald Welte59b04682009-06-10 05:40:52 +0800644
645 /* FIXME: if we don't know the TMSI, inquire abit IMSI and allocate new TMSI */
646 if (!subscr)
647 return gsm48_tx_mm_serv_rej(msg->lchan,
648 GSM48_REJECT_IMSI_UNKNOWN_IN_HLR);
649
650 if (!msg->lchan->subscr)
651 msg->lchan->subscr = subscr;
Sylvain Munaut7b9852c2009-12-18 18:28:10 +0100652 else if (msg->lchan->subscr == subscr)
653 subscr_put(subscr); /* lchan already has a ref, don't need another one */
654 else {
Harald Welte59b04682009-06-10 05:40:52 +0800655 DEBUGP(DMM, "<- CM Channel already owned by someone else?\n");
656 subscr_put(subscr);
657 }
658
Harald Weltef6845a72009-07-05 14:08:13 +0200659 subscr->equipment.classmark2_len = classmark2_len;
660 memcpy(subscr->equipment.classmark2, classmark2, classmark2_len);
661 db_sync_equipment(&subscr->equipment);
Harald Welte59b04682009-06-10 05:40:52 +0800662
663 return gsm48_tx_mm_serv_ack(msg->lchan);
664}
665
666static int gsm48_rx_mm_imsi_detach_ind(struct msgb *msg)
667{
Harald Welte75350412009-07-23 18:46:00 +0200668 struct gsm_bts *bts = msg->lchan->ts->trx->bts;
Harald Welte59b04682009-06-10 05:40:52 +0800669 struct gsm48_hdr *gh = msgb_l3(msg);
670 struct gsm48_imsi_detach_ind *idi =
671 (struct gsm48_imsi_detach_ind *) gh->data;
672 u_int8_t mi_type = idi->mi[0] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +0200673 char mi_string[GSM48_MI_SIZE];
Harald Welte03740842009-06-10 23:11:52 +0800674 struct gsm_subscriber *subscr = NULL;
Harald Welte59b04682009-06-10 05:40:52 +0800675
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +0200676 gsm48_mi_to_string(mi_string, sizeof(mi_string), idi->mi, idi->mi_len);
Harald Welte59b04682009-06-10 05:40:52 +0800677 DEBUGP(DMM, "IMSI DETACH INDICATION: mi_type=0x%02x MI(%s): ",
678 mi_type, mi_string);
679
Harald Weltebdbb7442009-12-22 19:07:32 +0100680 counter_inc(bts->network->stats.loc_upd_type.detach);
Harald Welte3edc5a92009-12-22 00:41:05 +0100681
Harald Welte59b04682009-06-10 05:40:52 +0800682 switch (mi_type) {
683 case GSM_MI_TYPE_TMSI:
Holger Hans Peter Freythercd8bacf2009-08-19 12:53:57 +0200684 subscr = subscr_get_by_tmsi(bts->network,
685 tmsi_from_string(mi_string));
Harald Welte59b04682009-06-10 05:40:52 +0800686 break;
687 case GSM_MI_TYPE_IMSI:
Harald Welte75350412009-07-23 18:46:00 +0200688 subscr = subscr_get_by_imsi(bts->network, mi_string);
Harald Welte59b04682009-06-10 05:40:52 +0800689 break;
690 case GSM_MI_TYPE_IMEI:
691 case GSM_MI_TYPE_IMEISV:
692 /* no sim card... FIXME: what to do ? */
693 DEBUGPC(DMM, "unimplemented mobile identity type\n");
694 break;
695 default:
696 DEBUGPC(DMM, "unknown mobile identity type\n");
697 break;
698 }
699
700 if (subscr) {
701 subscr_update(subscr, msg->trx->bts,
702 GSM_SUBSCRIBER_UPDATE_DETACHED);
Harald Welte0747c6a2009-12-24 14:50:24 +0100703 DEBUGP(DMM, "Subscriber: %s\n", subscr_name(subscr));
Harald Welte (local)ced09ed2009-08-17 09:39:55 +0200704
705 subscr->equipment.classmark1 = idi->classmark1;
706 db_sync_equipment(&subscr->equipment);
707
Harald Welte59b04682009-06-10 05:40:52 +0800708 subscr_put(subscr);
709 } else
710 DEBUGP(DMM, "Unknown Subscriber ?!?\n");
711
Harald Welte43250dd2009-12-20 09:58:40 +0100712 /* FIXME: iterate over all transactions and release them,
713 * imagine an IMSI DETACH happening during an active call! */
714
Harald Welte9ed54ff2009-12-12 21:00:48 +0100715 /* subscriber is detached: should we release lchan? */
Harald Welte92d88912009-12-21 13:30:17 +0100716 lchan_auto_release(msg->lchan);
Harald Welte9ed54ff2009-12-12 21:00:48 +0100717
Harald Welte59b04682009-06-10 05:40:52 +0800718 return 0;
719}
720
721static int gsm48_rx_mm_status(struct msgb *msg)
722{
723 struct gsm48_hdr *gh = msgb_l3(msg);
724
725 DEBUGP(DMM, "MM STATUS (reject cause 0x%02x)\n", gh->data[0]);
726
727 return 0;
728}
729
730/* Receive a GSM 04.08 Mobility Management (MM) message */
731static int gsm0408_rcv_mm(struct msgb *msg)
732{
733 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte03740842009-06-10 23:11:52 +0800734 int rc = 0;
Harald Welte59b04682009-06-10 05:40:52 +0800735
736 switch (gh->msg_type & 0xbf) {
737 case GSM48_MT_MM_LOC_UPD_REQUEST:
Harald Welte79639662009-06-27 02:58:43 +0200738 DEBUGP(DMM, "LOCATION UPDATING REQUEST: ");
Harald Welte59b04682009-06-10 05:40:52 +0800739 rc = mm_rx_loc_upd_req(msg);
740 break;
741 case GSM48_MT_MM_ID_RESP:
742 rc = mm_rx_id_resp(msg);
743 break;
744 case GSM48_MT_MM_CM_SERV_REQ:
745 rc = gsm48_rx_mm_serv_req(msg);
746 break;
747 case GSM48_MT_MM_STATUS:
748 rc = gsm48_rx_mm_status(msg);
749 break;
750 case GSM48_MT_MM_TMSI_REALL_COMPL:
751 DEBUGP(DMM, "TMSI Reallocation Completed. Subscriber: %s\n",
752 msg->lchan->subscr ?
Harald Welte0747c6a2009-12-24 14:50:24 +0100753 subscr_name(msg->lchan->subscr) :
Harald Welte59b04682009-06-10 05:40:52 +0800754 "unknown subscriber");
755 break;
756 case GSM48_MT_MM_IMSI_DETACH_IND:
757 rc = gsm48_rx_mm_imsi_detach_ind(msg);
758 break;
759 case GSM48_MT_MM_CM_REEST_REQ:
760 DEBUGP(DMM, "CM REESTABLISH REQUEST: Not implemented\n");
761 break;
762 case GSM48_MT_MM_AUTH_RESP:
763 DEBUGP(DMM, "AUTHENTICATION RESPONSE: Not implemented\n");
764 break;
765 default:
Harald Welte60e5f4f2009-12-24 12:13:17 +0100766 LOGP(DMM, LOGL_NOTICE, "Unknown GSM 04.08 MM msg type 0x%02x\n",
Harald Welte59b04682009-06-10 05:40:52 +0800767 gh->msg_type);
768 break;
769 }
770
771 return rc;
772}
773
774/* Receive a PAGING RESPONSE message from the MS */
Holger Hans Peter Freytherf10170e2009-10-23 13:48:54 +0200775static int gsm48_rx_rr_pag_resp(struct msgb *msg)
Harald Welte59b04682009-06-10 05:40:52 +0800776{
Harald Welte75350412009-07-23 18:46:00 +0200777 struct gsm_bts *bts = msg->lchan->ts->trx->bts;
Harald Welte59b04682009-06-10 05:40:52 +0800778 struct gsm48_hdr *gh = msgb_l3(msg);
779 u_int8_t *classmark2_lv = gh->data + 1;
Holger Hans Peter Freyther1e43b8d2009-08-21 05:18:21 +0200780 u_int8_t mi_type;
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +0200781 char mi_string[GSM48_MI_SIZE];
Harald Welte03740842009-06-10 23:11:52 +0800782 struct gsm_subscriber *subscr = NULL;
Harald Welte59b04682009-06-10 05:40:52 +0800783 int rc = 0;
784
Holger Hans Peter Freyther1e43b8d2009-08-21 05:18:21 +0200785 gsm48_paging_extract_mi(msg, mi_string, &mi_type);
Harald Welte59b04682009-06-10 05:40:52 +0800786 DEBUGP(DRR, "PAGING RESPONSE: mi_type=0x%02x MI(%s)\n",
787 mi_type, mi_string);
Harald Welte583ceaf2009-08-10 10:12:45 +0200788
Harald Welte59b04682009-06-10 05:40:52 +0800789 switch (mi_type) {
790 case GSM_MI_TYPE_TMSI:
Holger Hans Peter Freythercd8bacf2009-08-19 12:53:57 +0200791 subscr = subscr_get_by_tmsi(bts->network,
792 tmsi_from_string(mi_string));
Harald Welte59b04682009-06-10 05:40:52 +0800793 break;
794 case GSM_MI_TYPE_IMSI:
Harald Welte75350412009-07-23 18:46:00 +0200795 subscr = subscr_get_by_imsi(bts->network, mi_string);
Harald Welte59b04682009-06-10 05:40:52 +0800796 break;
797 }
798
799 if (!subscr) {
800 DEBUGP(DRR, "<- Can't find any subscriber for this ID\n");
801 /* FIXME: request id? close channel? */
802 return -EINVAL;
803 }
804 DEBUGP(DRR, "<- Channel was requested by %s\n",
Harald Welte68b7df22009-08-08 16:03:15 +0200805 subscr->name && strlen(subscr->name) ? subscr->name : subscr->imsi);
Harald Welte59b04682009-06-10 05:40:52 +0800806
Harald Weltef6845a72009-07-05 14:08:13 +0200807 subscr->equipment.classmark2_len = *classmark2_lv;
808 memcpy(subscr->equipment.classmark2, classmark2_lv+1, *classmark2_lv);
809 db_sync_equipment(&subscr->equipment);
Harald Welte59b04682009-06-10 05:40:52 +0800810
Holger Hans Peter Freyther1e43b8d2009-08-21 05:18:21 +0200811 rc = gsm48_handle_paging_resp(msg, subscr);
Harald Welte59b04682009-06-10 05:40:52 +0800812 return rc;
813}
814
815static int gsm48_rx_rr_classmark(struct msgb *msg)
816{
817 struct gsm48_hdr *gh = msgb_l3(msg);
818 struct gsm_subscriber *subscr = msg->lchan->subscr;
819 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
820 u_int8_t cm2_len, cm3_len = 0;
821 u_int8_t *cm2, *cm3 = NULL;
822
823 DEBUGP(DRR, "CLASSMARK CHANGE ");
824
825 /* classmark 2 */
826 cm2_len = gh->data[0];
827 cm2 = &gh->data[1];
828 DEBUGPC(DRR, "CM2(len=%u) ", cm2_len);
829
830 if (payload_len > cm2_len + 1) {
831 /* we must have a classmark3 */
832 if (gh->data[cm2_len+1] != 0x20) {
833 DEBUGPC(DRR, "ERR CM3 TAG\n");
834 return -EINVAL;
835 }
836 if (cm2_len > 3) {
837 DEBUGPC(DRR, "CM2 too long!\n");
838 return -EINVAL;
839 }
840
841 cm3_len = gh->data[cm2_len+2];
842 cm3 = &gh->data[cm2_len+3];
843 if (cm3_len > 14) {
844 DEBUGPC(DRR, "CM3 len %u too long!\n", cm3_len);
845 return -EINVAL;
846 }
847 DEBUGPC(DRR, "CM3(len=%u)\n", cm3_len);
848 }
849 if (subscr) {
Harald Weltef6845a72009-07-05 14:08:13 +0200850 subscr->equipment.classmark2_len = cm2_len;
851 memcpy(subscr->equipment.classmark2, cm2, cm2_len);
Harald Welte59b04682009-06-10 05:40:52 +0800852 if (cm3) {
Harald Weltef6845a72009-07-05 14:08:13 +0200853 subscr->equipment.classmark3_len = cm3_len;
854 memcpy(subscr->equipment.classmark3, cm3, cm3_len);
Harald Welte59b04682009-06-10 05:40:52 +0800855 }
Harald Weltef6845a72009-07-05 14:08:13 +0200856 db_sync_equipment(&subscr->equipment);
Harald Welte59b04682009-06-10 05:40:52 +0800857 }
858
Harald Welte59b04682009-06-10 05:40:52 +0800859 return 0;
860}
861
862static int gsm48_rx_rr_status(struct msgb *msg)
863{
864 struct gsm48_hdr *gh = msgb_l3(msg);
865
866 DEBUGP(DRR, "STATUS rr_cause = %s\n",
867 rr_cause_name(gh->data[0]));
868
869 return 0;
870}
871
872static int gsm48_rx_rr_meas_rep(struct msgb *msg)
873{
Harald Weltef9476812009-12-15 21:36:05 +0100874 struct gsm_meas_rep *meas_rep = lchan_next_meas_rep(msg->lchan);
Harald Welte59b04682009-06-10 05:40:52 +0800875
Harald Weltec20bd1d2009-11-29 19:07:28 +0100876 /* This shouldn't actually end up here, as RSL treats
877 * L3 Info of 08.58 MEASUREMENT REPORT different by calling
878 * directly into gsm48_parse_meas_rep */
879 DEBUGP(DMEAS, "DIRECT GSM48 MEASUREMENT REPORT ?!? ");
Harald Weltef9476812009-12-15 21:36:05 +0100880 gsm48_parse_meas_rep(meas_rep, msg);
Harald Welte59b04682009-06-10 05:40:52 +0800881
882 return 0;
883}
884
Harald Welte (local)b5103922009-08-15 23:32:44 +0200885static int gsm48_rx_rr_app_info(struct msgb *msg)
886{
887 struct gsm48_hdr *gh = msgb_l3(msg);
888 u_int8_t apdu_id_flags;
889 u_int8_t apdu_len;
890 u_int8_t *apdu_data;
891
892 apdu_id_flags = gh->data[0];
893 apdu_len = gh->data[1];
894 apdu_data = gh->data+2;
895
896 DEBUGP(DNM, "RX APPLICATION INFO id/flags=0x%02x apdu_len=%u apdu=%s",
897 apdu_id_flags, apdu_len, hexdump(apdu_data, apdu_len));
898
Harald Welte (local)bd72aa22009-08-16 10:40:10 +0200899 return db_apdu_blob_store(msg->lchan->subscr, apdu_id_flags, apdu_len, apdu_data);
Harald Welte (local)b5103922009-08-15 23:32:44 +0200900}
901
Harald Welte6720a432009-11-29 22:45:52 +0100902/* Chapter 9.1.16 Handover complete */
Harald Weltec6b3c8c2009-12-14 17:51:15 +0100903static int gsm48_rx_rr_ho_compl(struct msgb *msg)
Harald Welte6720a432009-11-29 22:45:52 +0100904{
905 struct gsm48_hdr *gh = msgb_l3(msg);
906
907 DEBUGP(DRR, "HANDOVER COMPLETE cause = %s\n",
908 rr_cause_name(gh->data[0]));
909
910 dispatch_signal(SS_LCHAN, S_LCHAN_HANDOVER_COMPL, msg->lchan);
911 /* FIXME: release old channel */
912
913 return 0;
914}
915
916/* Chapter 9.1.17 Handover Failure */
Harald Weltec6b3c8c2009-12-14 17:51:15 +0100917static int gsm48_rx_rr_ho_fail(struct msgb *msg)
Harald Welte6720a432009-11-29 22:45:52 +0100918{
919 struct gsm48_hdr *gh = msgb_l3(msg);
920
921 DEBUGP(DRR, "HANDOVER FAILED cause = %s\n",
922 rr_cause_name(gh->data[0]));
923
924 dispatch_signal(SS_LCHAN, S_LCHAN_HANDOVER_FAIL, msg->lchan);
925 /* FIXME: release allocated new channel */
926
927 return 0;
928}
Harald Welte (local)b5103922009-08-15 23:32:44 +0200929
Harald Welte59b04682009-06-10 05:40:52 +0800930/* Receive a GSM 04.08 Radio Resource (RR) message */
931static int gsm0408_rcv_rr(struct msgb *msg)
932{
933 struct gsm48_hdr *gh = msgb_l3(msg);
934 int rc = 0;
935
936 switch (gh->msg_type) {
937 case GSM48_MT_RR_CLSM_CHG:
938 rc = gsm48_rx_rr_classmark(msg);
939 break;
940 case GSM48_MT_RR_GPRS_SUSP_REQ:
941 DEBUGP(DRR, "GRPS SUSPEND REQUEST\n");
942 break;
943 case GSM48_MT_RR_PAG_RESP:
Holger Hans Peter Freytherf10170e2009-10-23 13:48:54 +0200944 rc = gsm48_rx_rr_pag_resp(msg);
Harald Welte59b04682009-06-10 05:40:52 +0800945 break;
946 case GSM48_MT_RR_CHAN_MODE_MODIF_ACK:
Holger Hans Peter Freyther9e6d6ce2009-10-22 15:23:11 +0200947 rc = gsm48_rx_rr_modif_ack(msg);
Harald Welte59b04682009-06-10 05:40:52 +0800948 break;
949 case GSM48_MT_RR_STATUS:
950 rc = gsm48_rx_rr_status(msg);
951 break;
952 case GSM48_MT_RR_MEAS_REP:
953 rc = gsm48_rx_rr_meas_rep(msg);
954 break;
Harald Welte (local)b5103922009-08-15 23:32:44 +0200955 case GSM48_MT_RR_APP_INFO:
956 rc = gsm48_rx_rr_app_info(msg);
957 break;
Harald Welted2dd9de2009-08-30 15:37:11 +0900958 case GSM48_MT_RR_CIPH_M_COMPL:
959 DEBUGP(DRR, "CIPHERING MODE COMPLETE\n");
960 /* FIXME: check for MI (if any) */
961 break;
Harald Welte323f4e02009-11-29 20:02:20 +0100962 case GSM48_MT_RR_HANDO_COMPL:
Harald Welte6720a432009-11-29 22:45:52 +0100963 rc = gsm48_rx_rr_ho_compl(msg);
Harald Welte323f4e02009-11-29 20:02:20 +0100964 break;
965 case GSM48_MT_RR_HANDO_FAIL:
Harald Welte6720a432009-11-29 22:45:52 +0100966 rc = gsm48_rx_rr_ho_fail(msg);
Harald Welte323f4e02009-11-29 20:02:20 +0100967 break;
Harald Welte59b04682009-06-10 05:40:52 +0800968 default:
Harald Welte60e5f4f2009-12-24 12:13:17 +0100969 LOGP(DRR, LOGL_NOTICE, "Unimplemented "
970 "GSM 04.08 RR msg type 0x%02x\n", gh->msg_type);
Harald Welte59b04682009-06-10 05:40:52 +0800971 break;
972 }
973
974 return rc;
975}
976
Harald Welte (local)b5103922009-08-15 23:32:44 +0200977int gsm48_send_rr_app_info(struct gsm_lchan *lchan, u_int8_t apdu_id,
Holger Hans Peter Freytherd70da872009-10-22 15:42:19 +0200978 u_int8_t apdu_len, const u_int8_t *apdu)
Harald Welte (local)b5103922009-08-15 23:32:44 +0200979{
980 struct msgb *msg = gsm48_msgb_alloc();
981 struct gsm48_hdr *gh;
982
983 msg->lchan = lchan;
984
985 DEBUGP(DRR, "TX APPLICATION INFO id=0x%02x, len=%u\n",
986 apdu_id, apdu_len);
987
988 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 2 + apdu_len);
989 gh->proto_discr = GSM48_PDISC_RR;
990 gh->msg_type = GSM48_MT_RR_APP_INFO;
991 gh->data[0] = apdu_id;
992 gh->data[1] = apdu_len;
993 memcpy(gh->data+2, apdu, apdu_len);
994
995 return gsm48_sendmsg(msg, NULL);
996}
997
Harald Welte59b04682009-06-10 05:40:52 +0800998/* Call Control */
999
1000/* The entire call control code is written in accordance with Figure 7.10c
1001 * for 'very early assignment', i.e. we allocate a TCH/F during IMMEDIATE
1002 * ASSIGN, then first use that TCH/F for signalling and later MODE MODIFY
1003 * it for voice */
1004
Harald Welte03740842009-06-10 23:11:52 +08001005static void new_cc_state(struct gsm_trans *trans, int state)
1006{
1007 if (state > 31 || state < 0)
1008 return;
1009
1010 DEBUGP(DCC, "new state %s -> %s\n",
Harald Welteb30935e2010-03-25 12:13:02 +08001011 gsm48_cc_state_name(trans->cc.state),
1012 gsm48_cc_state_name(state));
Harald Welte03740842009-06-10 23:11:52 +08001013
Harald Weltec2189a62009-07-23 18:56:43 +02001014 trans->cc.state = state;
Harald Welte03740842009-06-10 23:11:52 +08001015}
1016
1017static int gsm48_cc_tx_status(struct gsm_trans *trans, void *arg)
Harald Welte59b04682009-06-10 05:40:52 +08001018{
1019 struct msgb *msg = gsm48_msgb_alloc();
1020 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1021 u_int8_t *cause, *call_state;
1022
Harald Welte59b04682009-06-10 05:40:52 +08001023 gh->msg_type = GSM48_MT_CC_STATUS;
1024
1025 cause = msgb_put(msg, 3);
1026 cause[0] = 2;
1027 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
1028 cause[2] = 0x80 | 30; /* response to status inquiry */
1029
1030 call_state = msgb_put(msg, 1);
1031 call_state[0] = 0xc0 | 0x00;
1032
Harald Welte36fe2e82009-07-23 21:13:03 +02001033 return gsm48_sendmsg(msg, trans);
Harald Welte59b04682009-06-10 05:40:52 +08001034}
1035
1036static int gsm48_tx_simple(struct gsm_lchan *lchan,
1037 u_int8_t pdisc, u_int8_t msg_type)
1038{
1039 struct msgb *msg = gsm48_msgb_alloc();
1040 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1041
1042 msg->lchan = lchan;
1043
1044 gh->proto_discr = pdisc;
1045 gh->msg_type = msg_type;
1046
Harald Welte36fe2e82009-07-23 21:13:03 +02001047 return gsm48_sendmsg(msg, NULL);
Harald Welte59b04682009-06-10 05:40:52 +08001048}
1049
Harald Welte03740842009-06-10 23:11:52 +08001050static void gsm48_stop_cc_timer(struct gsm_trans *trans)
1051{
Harald Weltec2189a62009-07-23 18:56:43 +02001052 if (bsc_timer_pending(&trans->cc.timer)) {
1053 DEBUGP(DCC, "stopping pending timer T%x\n", trans->cc.Tcurrent);
1054 bsc_del_timer(&trans->cc.timer);
1055 trans->cc.Tcurrent = 0;
Harald Welte03740842009-06-10 23:11:52 +08001056 }
1057}
1058
1059static int mncc_recvmsg(struct gsm_network *net, struct gsm_trans *trans,
1060 int msg_type, struct gsm_mncc *mncc)
1061{
1062 struct msgb *msg;
1063
1064 if (trans)
1065 if (trans->lchan)
Harald Welte4861c822009-07-23 21:21:14 +02001066 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte03740842009-06-10 23:11:52 +08001067 "Sending '%s' to MNCC.\n",
1068 trans->lchan->ts->trx->bts->nr,
1069 trans->lchan->ts->trx->nr,
1070 trans->lchan->ts->nr, trans->transaction_id,
1071 (trans->subscr)?(trans->subscr->extension):"-",
1072 get_mncc_name(msg_type));
1073 else
1074 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
1075 "Sending '%s' to MNCC.\n",
1076 (trans->subscr)?(trans->subscr->extension):"-",
1077 get_mncc_name(msg_type));
1078 else
1079 DEBUGP(DCC, "(bts - trx - ts - ti -- sub -) "
1080 "Sending '%s' to MNCC.\n", get_mncc_name(msg_type));
1081
1082 mncc->msg_type = msg_type;
1083
Harald Welte9cfc9352009-06-26 19:39:35 +02001084 msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
Harald Welte03740842009-06-10 23:11:52 +08001085 if (!msg)
1086 return -ENOMEM;
1087 memcpy(msg->data, mncc, sizeof(struct gsm_mncc));
1088 msgb_enqueue(&net->upqueue, msg);
1089
1090 return 0;
1091}
1092
1093int mncc_release_ind(struct gsm_network *net, struct gsm_trans *trans,
1094 u_int32_t callref, int location, int value)
1095{
1096 struct gsm_mncc rel;
1097
Harald Weltecb0595f2009-06-12 01:54:08 +08001098 memset(&rel, 0, sizeof(rel));
Harald Welte03740842009-06-10 23:11:52 +08001099 rel.callref = callref;
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08001100 mncc_set_cause(&rel, location, value);
Harald Welte03740842009-06-10 23:11:52 +08001101 return mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
1102}
1103
Harald Weltec2189a62009-07-23 18:56:43 +02001104/* Call Control Specific transaction release.
1105 * gets called by trans_free, DO NOT CALL YOURSELF! */
1106void _gsm48_cc_trans_free(struct gsm_trans *trans)
Harald Welte03740842009-06-10 23:11:52 +08001107{
Harald Welte03740842009-06-10 23:11:52 +08001108 gsm48_stop_cc_timer(trans);
1109
1110 /* send release to L4, if callref still exists */
1111 if (trans->callref) {
1112 /* Ressource unavailable */
Harald Welte0abe5a62009-07-23 19:06:52 +02001113 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08001114 GSM48_CAUSE_LOC_PRN_S_LU,
1115 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte03740842009-06-10 23:11:52 +08001116 }
Harald Weltec2189a62009-07-23 18:56:43 +02001117 if (trans->cc.state != GSM_CSTATE_NULL)
Harald Welte03740842009-06-10 23:11:52 +08001118 new_cc_state(trans, GSM_CSTATE_NULL);
Harald Weltec2189a62009-07-23 18:56:43 +02001119 if (trans->lchan)
1120 trau_mux_unmap(&trans->lchan->ts->e1_link, trans->callref);
Harald Welte03740842009-06-10 23:11:52 +08001121}
1122
1123static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg);
1124
Harald Welte59b04682009-06-10 05:40:52 +08001125/* call-back from paging the B-end of the connection */
1126static int setup_trig_pag_evt(unsigned int hooknum, unsigned int event,
1127 struct msgb *msg, void *_lchan, void *param)
1128{
1129 struct gsm_lchan *lchan = _lchan;
Harald Welte03740842009-06-10 23:11:52 +08001130 struct gsm_subscriber *subscr = param;
1131 struct gsm_trans *transt, *tmp;
1132 struct gsm_network *net;
Harald Welte1ff81b52009-06-26 20:17:06 +02001133
Harald Welte59b04682009-06-10 05:40:52 +08001134 if (hooknum != GSM_HOOK_RR_PAGING)
1135 return -EINVAL;
Harald Welte03740842009-06-10 23:11:52 +08001136
1137 if (!subscr)
1138 return -EINVAL;
1139 net = subscr->net;
1140 if (!net) {
1141 DEBUGP(DCC, "Error Network not set!\n");
1142 return -EINVAL;
Harald Welte59b04682009-06-10 05:40:52 +08001143 }
1144
Harald Welte03740842009-06-10 23:11:52 +08001145 /* check all tranactions (without lchan) for subscriber */
1146 llist_for_each_entry_safe(transt, tmp, &net->trans_list, entry) {
1147 if (transt->subscr != subscr || transt->lchan)
1148 continue;
1149 switch (event) {
1150 case GSM_PAGING_SUCCEEDED:
1151 if (!lchan) // paranoid
1152 break;
1153 DEBUGP(DCC, "Paging subscr %s succeeded!\n",
1154 subscr->extension);
1155 /* Assign lchan */
1156 if (!transt->lchan) {
1157 transt->lchan = lchan;
1158 use_lchan(lchan);
1159 }
1160 /* send SETUP request to called party */
Harald Weltec2189a62009-07-23 18:56:43 +02001161 gsm48_cc_tx_setup(transt, &transt->cc.msg);
Harald Welte03740842009-06-10 23:11:52 +08001162 break;
1163 case GSM_PAGING_EXPIRED:
1164 DEBUGP(DCC, "Paging subscr %s expired!\n",
1165 subscr->extension);
1166 /* Temporarily out of order */
Harald Welte0abe5a62009-07-23 19:06:52 +02001167 mncc_release_ind(transt->subscr->net, transt,
1168 transt->callref,
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08001169 GSM48_CAUSE_LOC_PRN_S_LU,
1170 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte03740842009-06-10 23:11:52 +08001171 transt->callref = 0;
Harald Weltec2189a62009-07-23 18:56:43 +02001172 trans_free(transt);
Harald Welte03740842009-06-10 23:11:52 +08001173 break;
1174 }
1175 }
Harald Welte59b04682009-06-10 05:40:52 +08001176 return 0;
1177}
1178
Harald Welte3971ad52009-12-19 22:23:05 +01001179static int tch_recv_mncc(struct gsm_network *net, u_int32_t callref, int enable);
1180
Harald Welte3c062072009-07-28 18:25:29 +02001181/* some other part of the code sends us a signal */
1182static int handle_abisip_signal(unsigned int subsys, unsigned int signal,
1183 void *handler_data, void *signal_data)
1184{
1185 struct gsm_lchan *lchan = signal_data;
Harald Welte3c062072009-07-28 18:25:29 +02001186 int rc;
Harald Welte3971ad52009-12-19 22:23:05 +01001187 struct gsm_network *net;
1188 struct gsm_trans *trans;
Harald Welte3c062072009-07-28 18:25:29 +02001189
1190 if (subsys != SS_ABISIP)
1191 return 0;
1192
1193 /* in case we use direct BTS-to-BTS RTP */
1194 if (ipacc_rtp_direct)
1195 return 0;
1196
Harald Welte3c062072009-07-28 18:25:29 +02001197 switch (signal) {
Holger Hans Peter Freyther5ea7ea62009-11-18 21:06:12 +01001198 case S_ABISIP_CRCX_ACK:
Harald Welte3971ad52009-12-19 22:23:05 +01001199 /* check if any transactions on this lchan still have
1200 * a tch_recv_mncc request pending */
1201 net = lchan->ts->trx->bts->network;
1202 llist_for_each_entry(trans, &net->trans_list, entry) {
1203 if (trans->lchan == lchan && trans->tch_recv) {
1204 DEBUGP(DCC, "pending tch_recv_mncc request\n");
1205 tch_recv_mncc(net, trans->callref, 1);
1206 }
1207 }
Harald Welte3c062072009-07-28 18:25:29 +02001208 break;
Harald Welte3c062072009-07-28 18:25:29 +02001209 }
1210
1211 return 0;
Harald Welte3c062072009-07-28 18:25:29 +02001212}
1213
Harald Welte59b04682009-06-10 05:40:52 +08001214/* map two ipaccess RTP streams onto each other */
1215static int tch_map(struct gsm_lchan *lchan, struct gsm_lchan *remote_lchan)
1216{
1217 struct gsm_bts *bts = lchan->ts->trx->bts;
1218 struct gsm_bts *remote_bts = remote_lchan->ts->trx->bts;
Harald Welte3c062072009-07-28 18:25:29 +02001219 int rc;
Harald Welte59b04682009-06-10 05:40:52 +08001220
1221 DEBUGP(DCC, "Setting up TCH map between (bts=%u,trx=%u,ts=%u) and (bts=%u,trx=%u,ts=%u)\n",
1222 bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
1223 remote_bts->nr, remote_lchan->ts->trx->nr, remote_lchan->ts->nr);
1224
1225 if (bts->type != remote_bts->type) {
1226 DEBUGP(DCC, "Cannot switch calls between different BTS types yet\n");
1227 return -EINVAL;
1228 }
Harald Welte3971ad52009-12-19 22:23:05 +01001229
1230 // todo: map between different bts types
Harald Welte59b04682009-06-10 05:40:52 +08001231 switch (bts->type) {
Mike Haben66e0ba02009-10-02 12:19:34 +01001232 case GSM_BTS_TYPE_NANOBTS:
Harald Welte3c062072009-07-28 18:25:29 +02001233 if (!ipacc_rtp_direct) {
1234 /* connect the TCH's to our RTP proxy */
Harald Welte9947d9f2009-12-20 16:51:09 +01001235 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welte3c062072009-07-28 18:25:29 +02001236 if (rc < 0)
1237 return rc;
Harald Welte9947d9f2009-12-20 16:51:09 +01001238 rc = rsl_ipacc_mdcx_to_rtpsock(remote_lchan);
Harald Welte3971ad52009-12-19 22:23:05 +01001239#warning do we need a check of rc here?
Harald Welte3c062072009-07-28 18:25:29 +02001240
1241 /* connect them with each other */
Harald Welte87504212009-12-02 01:56:49 +05301242 rtp_socket_proxy(lchan->abis_ip.rtp_socket,
1243 remote_lchan->abis_ip.rtp_socket);
Harald Welte3c062072009-07-28 18:25:29 +02001244 } else {
1245 /* directly connect TCH RTP streams to each other */
Harald Welte87504212009-12-02 01:56:49 +05301246 rc = rsl_ipacc_mdcx(lchan, remote_lchan->abis_ip.bound_ip,
1247 remote_lchan->abis_ip.bound_port,
Harald Welte87504212009-12-02 01:56:49 +05301248 remote_lchan->abis_ip.rtp_payload2);
Harald Welte3c062072009-07-28 18:25:29 +02001249 if (rc < 0)
1250 return rc;
Harald Welte87504212009-12-02 01:56:49 +05301251 rc = rsl_ipacc_mdcx(remote_lchan, lchan->abis_ip.bound_ip,
1252 lchan->abis_ip.bound_port,
Harald Welte87504212009-12-02 01:56:49 +05301253 lchan->abis_ip.rtp_payload2);
Harald Welte3c062072009-07-28 18:25:29 +02001254 }
Harald Welte59b04682009-06-10 05:40:52 +08001255 break;
1256 case GSM_BTS_TYPE_BS11:
1257 trau_mux_map_lchan(lchan, remote_lchan);
1258 break;
1259 default:
1260 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
Harald Welte3971ad52009-12-19 22:23:05 +01001261 return -EINVAL;
Harald Welte59b04682009-06-10 05:40:52 +08001262 }
1263
1264 return 0;
1265}
1266
Harald Welte03740842009-06-10 23:11:52 +08001267/* bridge channels of two transactions */
1268static int tch_bridge(struct gsm_network *net, u_int32_t *refs)
Harald Welte59b04682009-06-10 05:40:52 +08001269{
Harald Weltec2189a62009-07-23 18:56:43 +02001270 struct gsm_trans *trans1 = trans_find_by_callref(net, refs[0]);
1271 struct gsm_trans *trans2 = trans_find_by_callref(net, refs[1]);
Harald Welte59b04682009-06-10 05:40:52 +08001272
Harald Welte03740842009-06-10 23:11:52 +08001273 if (!trans1 || !trans2)
Harald Welte59b04682009-06-10 05:40:52 +08001274 return -EIO;
1275
Harald Welte03740842009-06-10 23:11:52 +08001276 if (!trans1->lchan || !trans2->lchan)
1277 return -EIO;
1278
1279 /* through-connect channel */
1280 return tch_map(trans1->lchan, trans2->lchan);
Harald Welte59b04682009-06-10 05:40:52 +08001281}
1282
Harald Welte3971ad52009-12-19 22:23:05 +01001283/* enable receive of channels to MNCC upqueue */
1284static int tch_recv_mncc(struct gsm_network *net, u_int32_t callref, int enable)
Harald Welte03740842009-06-10 23:11:52 +08001285{
1286 struct gsm_trans *trans;
Harald Welte3971ad52009-12-19 22:23:05 +01001287 struct gsm_lchan *lchan;
1288 struct gsm_bts *bts;
1289 int rc;
Harald Welte59b04682009-06-10 05:40:52 +08001290
Harald Welte03740842009-06-10 23:11:52 +08001291 /* Find callref */
Harald Welte3971ad52009-12-19 22:23:05 +01001292 trans = trans_find_by_callref(net, callref);
Harald Welte03740842009-06-10 23:11:52 +08001293 if (!trans)
1294 return -EIO;
1295 if (!trans->lchan)
1296 return 0;
Harald Welte3971ad52009-12-19 22:23:05 +01001297 lchan = trans->lchan;
1298 bts = lchan->ts->trx->bts;
Harald Welte03740842009-06-10 23:11:52 +08001299
Harald Welte3971ad52009-12-19 22:23:05 +01001300 switch (bts->type) {
1301 case GSM_BTS_TYPE_NANOBTS:
1302 if (ipacc_rtp_direct) {
1303 DEBUGP(DCC, "Error: RTP proxy is disabled\n");
1304 return -EINVAL;
1305 }
1306 /* in case, we don't have a RTP socket yet, we note this
1307 * in the transaction and try later */
1308 if (!lchan->abis_ip.rtp_socket) {
1309 trans->tch_recv = enable;
1310 DEBUGP(DCC, "queue tch_recv_mncc request (%d)\n", enable);
1311 return 0;
1312 }
1313 if (enable) {
1314 /* connect the TCH's to our RTP proxy */
Harald Welte9947d9f2009-12-20 16:51:09 +01001315 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welte3971ad52009-12-19 22:23:05 +01001316 if (rc < 0)
1317 return rc;
1318 /* assign socket to application interface */
1319 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
1320 net, callref);
1321 } else
1322 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
1323 net, 0);
1324 break;
1325 case GSM_BTS_TYPE_BS11:
1326 if (enable)
1327 return trau_recv_lchan(lchan, callref);
1328 return trau_mux_unmap(NULL, callref);
1329 break;
1330 default:
1331 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
1332 return -EINVAL;
1333 }
1334
1335 return 0;
Harald Welte03740842009-06-10 23:11:52 +08001336}
1337
Harald Welte03740842009-06-10 23:11:52 +08001338static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
1339{
1340 DEBUGP(DCC, "-> STATUS ENQ\n");
1341 return gsm48_cc_tx_status(trans, msg);
1342}
1343
1344static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg);
1345static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg);
1346
1347static void gsm48_cc_timeout(void *arg)
1348{
1349 struct gsm_trans *trans = arg;
1350 int disconnect = 0, release = 0;
Harald Weltebbc636a2009-06-11 14:23:20 +08001351 int mo_cause = GSM48_CC_CAUSE_RECOVERY_TIMER;
1352 int mo_location = GSM48_CAUSE_LOC_USER;
1353 int l4_cause = GSM48_CC_CAUSE_NORMAL_UNSPEC;
1354 int l4_location = GSM48_CAUSE_LOC_PRN_S_LU;
Harald Welte03740842009-06-10 23:11:52 +08001355 struct gsm_mncc mo_rel, l4_rel;
1356
1357 memset(&mo_rel, 0, sizeof(struct gsm_mncc));
1358 mo_rel.callref = trans->callref;
1359 memset(&l4_rel, 0, sizeof(struct gsm_mncc));
1360 l4_rel.callref = trans->callref;
1361
Harald Weltec2189a62009-07-23 18:56:43 +02001362 switch(trans->cc.Tcurrent) {
Harald Welte03740842009-06-10 23:11:52 +08001363 case 0x303:
1364 release = 1;
Harald Weltebbc636a2009-06-11 14:23:20 +08001365 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte03740842009-06-10 23:11:52 +08001366 break;
1367 case 0x310:
1368 disconnect = 1;
Harald Weltebbc636a2009-06-11 14:23:20 +08001369 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte03740842009-06-10 23:11:52 +08001370 break;
1371 case 0x313:
1372 disconnect = 1;
1373 /* unknown, did not find it in the specs */
1374 break;
1375 case 0x301:
1376 disconnect = 1;
Harald Weltebbc636a2009-06-11 14:23:20 +08001377 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte03740842009-06-10 23:11:52 +08001378 break;
1379 case 0x308:
Harald Weltec2189a62009-07-23 18:56:43 +02001380 if (!trans->cc.T308_second) {
Harald Welte03740842009-06-10 23:11:52 +08001381 /* restart T308 a second time */
Harald Weltec2189a62009-07-23 18:56:43 +02001382 gsm48_cc_tx_release(trans, &trans->cc.msg);
1383 trans->cc.T308_second = 1;
Harald Welte03740842009-06-10 23:11:52 +08001384 break; /* stay in release state */
1385 }
Harald Weltec2189a62009-07-23 18:56:43 +02001386 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08001387 return;
1388// release = 1;
1389// l4_cause = 14;
1390// break;
1391 case 0x306:
1392 release = 1;
Harald Weltec2189a62009-07-23 18:56:43 +02001393 mo_cause = trans->cc.msg.cause.value;
1394 mo_location = trans->cc.msg.cause.location;
Harald Welte03740842009-06-10 23:11:52 +08001395 break;
1396 case 0x323:
1397 disconnect = 1;
1398 break;
1399 default:
1400 release = 1;
1401 }
1402
1403 if (release && trans->callref) {
1404 /* process release towards layer 4 */
Harald Welte0abe5a62009-07-23 19:06:52 +02001405 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Harald Welte03740842009-06-10 23:11:52 +08001406 l4_location, l4_cause);
1407 trans->callref = 0;
1408 }
1409
1410 if (disconnect && trans->callref) {
1411 /* process disconnect towards layer 4 */
1412 mncc_set_cause(&l4_rel, l4_location, l4_cause);
Harald Welte0abe5a62009-07-23 19:06:52 +02001413 mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &l4_rel);
Harald Welte03740842009-06-10 23:11:52 +08001414 }
1415
1416 /* process disconnect towards mobile station */
1417 if (disconnect || release) {
1418 mncc_set_cause(&mo_rel, mo_location, mo_cause);
Harald Weltec2189a62009-07-23 18:56:43 +02001419 mo_rel.cause.diag[0] = ((trans->cc.Tcurrent & 0xf00) >> 8) + '0';
1420 mo_rel.cause.diag[1] = ((trans->cc.Tcurrent & 0x0f0) >> 4) + '0';
1421 mo_rel.cause.diag[2] = (trans->cc.Tcurrent & 0x00f) + '0';
Harald Welte03740842009-06-10 23:11:52 +08001422 mo_rel.cause.diag_len = 3;
1423
1424 if (disconnect)
1425 gsm48_cc_tx_disconnect(trans, &mo_rel);
1426 if (release)
1427 gsm48_cc_tx_release(trans, &mo_rel);
1428 }
1429
1430}
1431
1432static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
1433 int sec, int micro)
1434{
1435 DEBUGP(DCC, "starting timer T%x with %d seconds\n", current, sec);
Harald Weltec2189a62009-07-23 18:56:43 +02001436 trans->cc.timer.cb = gsm48_cc_timeout;
1437 trans->cc.timer.data = trans;
1438 bsc_schedule_timer(&trans->cc.timer, sec, micro);
1439 trans->cc.Tcurrent = current;
Harald Welte03740842009-06-10 23:11:52 +08001440}
1441
1442static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
1443{
1444 struct gsm48_hdr *gh = msgb_l3(msg);
1445 u_int8_t msg_type = gh->msg_type & 0xbf;
1446 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1447 struct tlv_parsed tp;
1448 struct gsm_mncc setup;
1449
1450 memset(&setup, 0, sizeof(struct gsm_mncc));
1451 setup.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01001452 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte03740842009-06-10 23:11:52 +08001453 /* emergency setup is identified by msg_type */
1454 if (msg_type == GSM48_MT_CC_EMERG_SETUP)
1455 setup.emergency = 1;
1456
1457 /* use subscriber as calling party number */
1458 if (trans->subscr) {
1459 setup.fields |= MNCC_F_CALLING;
1460 strncpy(setup.calling.number, trans->subscr->extension,
1461 sizeof(setup.calling.number)-1);
Andreas Eversberg9eaa5da2009-06-15 23:22:09 +02001462 strncpy(setup.imsi, trans->subscr->imsi,
1463 sizeof(setup.imsi)-1);
Harald Welte03740842009-06-10 23:11:52 +08001464 }
1465 /* bearer capability */
1466 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1467 setup.fields |= MNCC_F_BEARER_CAP;
Harald Weltefdc93d92010-03-07 23:40:35 +01001468 gsm48_decode_bearer_cap(&setup.bearer_cap,
Harald Welte03740842009-06-10 23:11:52 +08001469 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1470 }
1471 /* facility */
1472 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1473 setup.fields |= MNCC_F_FACILITY;
Harald Weltefdc93d92010-03-07 23:40:35 +01001474 gsm48_decode_facility(&setup.facility,
Harald Welte03740842009-06-10 23:11:52 +08001475 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1476 }
1477 /* called party bcd number */
1478 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
1479 setup.fields |= MNCC_F_CALLED;
Harald Weltefdc93d92010-03-07 23:40:35 +01001480 gsm48_decode_called(&setup.called,
Harald Welte03740842009-06-10 23:11:52 +08001481 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
1482 }
1483 /* user-user */
1484 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1485 setup.fields |= MNCC_F_USERUSER;
Harald Weltefdc93d92010-03-07 23:40:35 +01001486 gsm48_decode_useruser(&setup.useruser,
Harald Welte03740842009-06-10 23:11:52 +08001487 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1488 }
1489 /* ss-version */
1490 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1491 setup.fields |= MNCC_F_SSVERSION;
Harald Weltefdc93d92010-03-07 23:40:35 +01001492 gsm48_decode_ssversion(&setup.ssversion,
Harald Welte03740842009-06-10 23:11:52 +08001493 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1494 }
1495 /* CLIR suppression */
1496 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
1497 setup.clir.sup = 1;
1498 /* CLIR invocation */
1499 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
1500 setup.clir.inv = 1;
1501 /* cc cap */
1502 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1503 setup.fields |= MNCC_F_CCCAP;
Harald Weltefdc93d92010-03-07 23:40:35 +01001504 gsm48_decode_cccap(&setup.cccap,
Harald Welte03740842009-06-10 23:11:52 +08001505 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1506 }
1507
Harald Welte03740842009-06-10 23:11:52 +08001508 new_cc_state(trans, GSM_CSTATE_INITIATED);
1509
Harald Welte (local)5b722d42009-12-26 19:45:03 +01001510 LOGP(DCC, LOGL_INFO, "Subscriber %s (%s) sends SETUP to %s\n",
1511 subscr_name(trans->subscr), trans->subscr->extension,
1512 setup.called.number);
1513
Harald Welte03740842009-06-10 23:11:52 +08001514 /* indicate setup to MNCC */
Harald Welte0abe5a62009-07-23 19:06:52 +02001515 mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_IND, &setup);
Harald Welte03740842009-06-10 23:11:52 +08001516
Harald Welteca745e22009-07-29 12:10:35 +02001517 /* MNCC code will modify the channel asynchronously, we should
1518 * ipaccess-bind only after the modification has been made to the
1519 * lchan->tch_mode */
Harald Welte03740842009-06-10 23:11:52 +08001520 return 0;
1521}
1522
1523static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
Harald Welte59b04682009-06-10 05:40:52 +08001524{
1525 struct msgb *msg = gsm48_msgb_alloc();
1526 struct gsm48_hdr *gh;
Harald Welte03740842009-06-10 23:11:52 +08001527 struct gsm_mncc *setup = arg;
Harald Welte165fc332009-07-23 21:36:44 +02001528 int rc, trans_id;
Harald Welte59b04682009-06-10 05:40:52 +08001529
1530 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1531
Harald Welte03740842009-06-10 23:11:52 +08001532 /* transaction id must not be assigned */
1533 if (trans->transaction_id != 0xff) { /* unasssigned */
1534 DEBUGP(DCC, "TX Setup with assigned transaction. "
1535 "This is not allowed!\n");
1536 /* Temporarily out of order */
Harald Welte0abe5a62009-07-23 19:06:52 +02001537 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08001538 GSM48_CAUSE_LOC_PRN_S_LU,
1539 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte03740842009-06-10 23:11:52 +08001540 trans->callref = 0;
Harald Weltec2189a62009-07-23 18:56:43 +02001541 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08001542 return rc;
1543 }
1544
1545 /* Get free transaction_id */
Harald Welte165fc332009-07-23 21:36:44 +02001546 trans_id = trans_assign_trans_id(trans->subscr, GSM48_PDISC_CC, 0);
1547 if (trans_id < 0) {
Harald Welte03740842009-06-10 23:11:52 +08001548 /* no free transaction ID */
Harald Welte0abe5a62009-07-23 19:06:52 +02001549 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08001550 GSM48_CAUSE_LOC_PRN_S_LU,
1551 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte03740842009-06-10 23:11:52 +08001552 trans->callref = 0;
Harald Weltec2189a62009-07-23 18:56:43 +02001553 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08001554 return rc;
1555 }
Harald Welte165fc332009-07-23 21:36:44 +02001556 trans->transaction_id = trans_id;
Harald Welte59b04682009-06-10 05:40:52 +08001557
Harald Welte59b04682009-06-10 05:40:52 +08001558 gh->msg_type = GSM48_MT_CC_SETUP;
1559
Harald Welte03740842009-06-10 23:11:52 +08001560 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
Harald Welte59b04682009-06-10 05:40:52 +08001561
Harald Welte03740842009-06-10 23:11:52 +08001562 /* bearer capability */
1563 if (setup->fields & MNCC_F_BEARER_CAP)
Harald Weltefdc93d92010-03-07 23:40:35 +01001564 gsm48_encode_bearer_cap(msg, 0, &setup->bearer_cap);
Harald Welte03740842009-06-10 23:11:52 +08001565 /* facility */
1566 if (setup->fields & MNCC_F_FACILITY)
Harald Weltefdc93d92010-03-07 23:40:35 +01001567 gsm48_encode_facility(msg, 0, &setup->facility);
Harald Welte03740842009-06-10 23:11:52 +08001568 /* progress */
1569 if (setup->fields & MNCC_F_PROGRESS)
Harald Weltefdc93d92010-03-07 23:40:35 +01001570 gsm48_encode_progress(msg, 0, &setup->progress);
Harald Welte03740842009-06-10 23:11:52 +08001571 /* calling party BCD number */
1572 if (setup->fields & MNCC_F_CALLING)
Harald Weltefdc93d92010-03-07 23:40:35 +01001573 gsm48_encode_calling(msg, &setup->calling);
Harald Welte03740842009-06-10 23:11:52 +08001574 /* called party BCD number */
1575 if (setup->fields & MNCC_F_CALLED)
Harald Weltefdc93d92010-03-07 23:40:35 +01001576 gsm48_encode_called(msg, &setup->called);
Harald Welte03740842009-06-10 23:11:52 +08001577 /* user-user */
1578 if (setup->fields & MNCC_F_USERUSER)
Harald Weltefdc93d92010-03-07 23:40:35 +01001579 gsm48_encode_useruser(msg, 0, &setup->useruser);
Harald Welte03740842009-06-10 23:11:52 +08001580 /* redirecting party BCD number */
1581 if (setup->fields & MNCC_F_REDIRECTING)
Harald Weltefdc93d92010-03-07 23:40:35 +01001582 gsm48_encode_redirecting(msg, &setup->redirecting);
Harald Welte03740842009-06-10 23:11:52 +08001583 /* signal */
1584 if (setup->fields & MNCC_F_SIGNAL)
Harald Weltefdc93d92010-03-07 23:40:35 +01001585 gsm48_encode_signal(msg, setup->signal);
Harald Welte03740842009-06-10 23:11:52 +08001586
1587 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
Harald Welte59b04682009-06-10 05:40:52 +08001588
Harald Welte36fe2e82009-07-23 21:13:03 +02001589 return gsm48_sendmsg(msg, trans);
Harald Welte59b04682009-06-10 05:40:52 +08001590}
1591
Harald Welte03740842009-06-10 23:11:52 +08001592static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
1593{
1594 struct gsm48_hdr *gh = msgb_l3(msg);
1595 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1596 struct tlv_parsed tp;
1597 struct gsm_mncc call_conf;
1598
1599 gsm48_stop_cc_timer(trans);
1600 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
1601
1602 memset(&call_conf, 0, sizeof(struct gsm_mncc));
1603 call_conf.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01001604 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte03740842009-06-10 23:11:52 +08001605#if 0
1606 /* repeat */
1607 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
1608 call_conf.repeat = 1;
1609 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
1610 call_conf.repeat = 2;
1611#endif
1612 /* bearer capability */
1613 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1614 call_conf.fields |= MNCC_F_BEARER_CAP;
Harald Weltefdc93d92010-03-07 23:40:35 +01001615 gsm48_decode_bearer_cap(&call_conf.bearer_cap,
Harald Welte03740842009-06-10 23:11:52 +08001616 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1617 }
1618 /* cause */
1619 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1620 call_conf.fields |= MNCC_F_CAUSE;
Harald Weltefdc93d92010-03-07 23:40:35 +01001621 gsm48_decode_cause(&call_conf.cause,
Harald Welte03740842009-06-10 23:11:52 +08001622 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1623 }
1624 /* cc cap */
1625 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1626 call_conf.fields |= MNCC_F_CCCAP;
Harald Weltefdc93d92010-03-07 23:40:35 +01001627 gsm48_decode_cccap(&call_conf.cccap,
Harald Welte03740842009-06-10 23:11:52 +08001628 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1629 }
1630
1631 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
1632
Harald Welte0abe5a62009-07-23 19:06:52 +02001633 return mncc_recvmsg(trans->subscr->net, trans, MNCC_CALL_CONF_IND,
1634 &call_conf);
Harald Welte03740842009-06-10 23:11:52 +08001635}
1636
1637static int gsm48_cc_tx_call_proc(struct gsm_trans *trans, void *arg)
1638{
1639 struct gsm_mncc *proceeding = arg;
1640 struct msgb *msg = gsm48_msgb_alloc();
1641 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1642
Harald Welte03740842009-06-10 23:11:52 +08001643 gh->msg_type = GSM48_MT_CC_CALL_PROC;
1644
1645 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
1646
1647 /* bearer capability */
1648 if (proceeding->fields & MNCC_F_BEARER_CAP)
Harald Weltefdc93d92010-03-07 23:40:35 +01001649 gsm48_encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
Harald Welte03740842009-06-10 23:11:52 +08001650 /* facility */
1651 if (proceeding->fields & MNCC_F_FACILITY)
Harald Weltefdc93d92010-03-07 23:40:35 +01001652 gsm48_encode_facility(msg, 0, &proceeding->facility);
Harald Welte03740842009-06-10 23:11:52 +08001653 /* progress */
1654 if (proceeding->fields & MNCC_F_PROGRESS)
Harald Weltefdc93d92010-03-07 23:40:35 +01001655 gsm48_encode_progress(msg, 0, &proceeding->progress);
Harald Welte03740842009-06-10 23:11:52 +08001656
Harald Welte36fe2e82009-07-23 21:13:03 +02001657 return gsm48_sendmsg(msg, trans);
Harald Welte03740842009-06-10 23:11:52 +08001658}
1659
1660static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
1661{
1662 struct gsm48_hdr *gh = msgb_l3(msg);
1663 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1664 struct tlv_parsed tp;
1665 struct gsm_mncc alerting;
1666
1667 gsm48_stop_cc_timer(trans);
1668 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
1669
1670 memset(&alerting, 0, sizeof(struct gsm_mncc));
1671 alerting.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01001672 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte03740842009-06-10 23:11:52 +08001673 /* facility */
1674 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1675 alerting.fields |= MNCC_F_FACILITY;
Harald Weltefdc93d92010-03-07 23:40:35 +01001676 gsm48_decode_facility(&alerting.facility,
Harald Welte03740842009-06-10 23:11:52 +08001677 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1678 }
1679
1680 /* progress */
1681 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
1682 alerting.fields |= MNCC_F_PROGRESS;
Harald Weltefdc93d92010-03-07 23:40:35 +01001683 gsm48_decode_progress(&alerting.progress,
Harald Welte03740842009-06-10 23:11:52 +08001684 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
1685 }
1686 /* ss-version */
1687 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1688 alerting.fields |= MNCC_F_SSVERSION;
Harald Weltefdc93d92010-03-07 23:40:35 +01001689 gsm48_decode_ssversion(&alerting.ssversion,
Harald Welte03740842009-06-10 23:11:52 +08001690 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1691 }
1692
1693 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
1694
Harald Welte0abe5a62009-07-23 19:06:52 +02001695 return mncc_recvmsg(trans->subscr->net, trans, MNCC_ALERT_IND,
1696 &alerting);
Harald Welte03740842009-06-10 23:11:52 +08001697}
1698
1699static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
1700{
1701 struct gsm_mncc *alerting = arg;
1702 struct msgb *msg = gsm48_msgb_alloc();
1703 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1704
Harald Welte03740842009-06-10 23:11:52 +08001705 gh->msg_type = GSM48_MT_CC_ALERTING;
1706
1707 /* facility */
1708 if (alerting->fields & MNCC_F_FACILITY)
Harald Weltefdc93d92010-03-07 23:40:35 +01001709 gsm48_encode_facility(msg, 0, &alerting->facility);
Harald Welte03740842009-06-10 23:11:52 +08001710 /* progress */
1711 if (alerting->fields & MNCC_F_PROGRESS)
Harald Weltefdc93d92010-03-07 23:40:35 +01001712 gsm48_encode_progress(msg, 0, &alerting->progress);
Harald Welte03740842009-06-10 23:11:52 +08001713 /* user-user */
1714 if (alerting->fields & MNCC_F_USERUSER)
Harald Weltefdc93d92010-03-07 23:40:35 +01001715 gsm48_encode_useruser(msg, 0, &alerting->useruser);
Harald Welte03740842009-06-10 23:11:52 +08001716
1717 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
1718
Harald Welte36fe2e82009-07-23 21:13:03 +02001719 return gsm48_sendmsg(msg, trans);
Harald Welte03740842009-06-10 23:11:52 +08001720}
1721
1722static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
1723{
1724 struct gsm_mncc *progress = arg;
1725 struct msgb *msg = gsm48_msgb_alloc();
1726 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1727
Harald Welte03740842009-06-10 23:11:52 +08001728 gh->msg_type = GSM48_MT_CC_PROGRESS;
1729
1730 /* progress */
Harald Weltefdc93d92010-03-07 23:40:35 +01001731 gsm48_encode_progress(msg, 1, &progress->progress);
Harald Welte03740842009-06-10 23:11:52 +08001732 /* user-user */
1733 if (progress->fields & MNCC_F_USERUSER)
Harald Weltefdc93d92010-03-07 23:40:35 +01001734 gsm48_encode_useruser(msg, 0, &progress->useruser);
Harald Welte03740842009-06-10 23:11:52 +08001735
Harald Welte36fe2e82009-07-23 21:13:03 +02001736 return gsm48_sendmsg(msg, trans);
Harald Welte03740842009-06-10 23:11:52 +08001737}
1738
1739static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
1740{
1741 struct gsm_mncc *connect = arg;
1742 struct msgb *msg = gsm48_msgb_alloc();
1743 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1744
Harald Welte03740842009-06-10 23:11:52 +08001745 gh->msg_type = GSM48_MT_CC_CONNECT;
1746
1747 gsm48_stop_cc_timer(trans);
1748 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
1749
1750 /* facility */
1751 if (connect->fields & MNCC_F_FACILITY)
Harald Weltefdc93d92010-03-07 23:40:35 +01001752 gsm48_encode_facility(msg, 0, &connect->facility);
Harald Welte03740842009-06-10 23:11:52 +08001753 /* progress */
1754 if (connect->fields & MNCC_F_PROGRESS)
Harald Weltefdc93d92010-03-07 23:40:35 +01001755 gsm48_encode_progress(msg, 0, &connect->progress);
Harald Welte03740842009-06-10 23:11:52 +08001756 /* connected number */
1757 if (connect->fields & MNCC_F_CONNECTED)
Harald Weltefdc93d92010-03-07 23:40:35 +01001758 gsm48_encode_connected(msg, &connect->connected);
Harald Welte03740842009-06-10 23:11:52 +08001759 /* user-user */
1760 if (connect->fields & MNCC_F_USERUSER)
Harald Weltefdc93d92010-03-07 23:40:35 +01001761 gsm48_encode_useruser(msg, 0, &connect->useruser);
Harald Welte03740842009-06-10 23:11:52 +08001762
1763 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
1764
Harald Welte36fe2e82009-07-23 21:13:03 +02001765 return gsm48_sendmsg(msg, trans);
Harald Welte03740842009-06-10 23:11:52 +08001766}
1767
1768static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
1769{
1770 struct gsm48_hdr *gh = msgb_l3(msg);
1771 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1772 struct tlv_parsed tp;
1773 struct gsm_mncc connect;
1774
1775 gsm48_stop_cc_timer(trans);
1776
1777 memset(&connect, 0, sizeof(struct gsm_mncc));
1778 connect.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01001779 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte03740842009-06-10 23:11:52 +08001780 /* use subscriber as connected party number */
1781 if (trans->subscr) {
1782 connect.fields |= MNCC_F_CONNECTED;
1783 strncpy(connect.connected.number, trans->subscr->extension,
1784 sizeof(connect.connected.number)-1);
Andreas Eversberg9eaa5da2009-06-15 23:22:09 +02001785 strncpy(connect.imsi, trans->subscr->imsi,
1786 sizeof(connect.imsi)-1);
Harald Welte03740842009-06-10 23:11:52 +08001787 }
1788 /* facility */
1789 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1790 connect.fields |= MNCC_F_FACILITY;
Harald Weltefdc93d92010-03-07 23:40:35 +01001791 gsm48_decode_facility(&connect.facility,
Harald Welte03740842009-06-10 23:11:52 +08001792 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1793 }
1794 /* user-user */
1795 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1796 connect.fields |= MNCC_F_USERUSER;
Harald Weltefdc93d92010-03-07 23:40:35 +01001797 gsm48_decode_useruser(&connect.useruser,
Harald Welte03740842009-06-10 23:11:52 +08001798 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1799 }
1800 /* ss-version */
1801 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1802 connect.fields |= MNCC_F_SSVERSION;
Harald Weltefdc93d92010-03-07 23:40:35 +01001803 gsm48_decode_ssversion(&connect.ssversion,
Harald Welte03740842009-06-10 23:11:52 +08001804 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1805 }
1806
1807 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
1808
Harald Welte0abe5a62009-07-23 19:06:52 +02001809 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_CNF, &connect);
Harald Welte03740842009-06-10 23:11:52 +08001810}
1811
1812
1813static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
1814{
1815 struct gsm_mncc connect_ack;
1816
1817 gsm48_stop_cc_timer(trans);
1818
1819 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1820
1821 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
1822 connect_ack.callref = trans->callref;
Harald Welte0abe5a62009-07-23 19:06:52 +02001823 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_COMPL_IND,
Harald Welte03740842009-06-10 23:11:52 +08001824 &connect_ack);
1825}
1826
1827static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
1828{
1829 struct msgb *msg = gsm48_msgb_alloc();
1830 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1831
Harald Welte03740842009-06-10 23:11:52 +08001832 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
1833
1834 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1835
Harald Welte36fe2e82009-07-23 21:13:03 +02001836 return gsm48_sendmsg(msg, trans);
Harald Welte03740842009-06-10 23:11:52 +08001837}
1838
1839static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
1840{
1841 struct gsm48_hdr *gh = msgb_l3(msg);
1842 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1843 struct tlv_parsed tp;
1844 struct gsm_mncc disc;
1845
1846 gsm48_stop_cc_timer(trans);
1847
1848 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
1849
1850 memset(&disc, 0, sizeof(struct gsm_mncc));
1851 disc.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01001852 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
Harald Welte03740842009-06-10 23:11:52 +08001853 /* cause */
1854 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1855 disc.fields |= MNCC_F_CAUSE;
Harald Weltefdc93d92010-03-07 23:40:35 +01001856 gsm48_decode_cause(&disc.cause,
Harald Welte03740842009-06-10 23:11:52 +08001857 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1858 }
1859 /* facility */
1860 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1861 disc.fields |= MNCC_F_FACILITY;
Harald Weltefdc93d92010-03-07 23:40:35 +01001862 gsm48_decode_facility(&disc.facility,
Harald Welte03740842009-06-10 23:11:52 +08001863 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1864 }
1865 /* user-user */
1866 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1867 disc.fields |= MNCC_F_USERUSER;
Harald Weltefdc93d92010-03-07 23:40:35 +01001868 gsm48_decode_useruser(&disc.useruser,
Harald Welte03740842009-06-10 23:11:52 +08001869 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1870 }
1871 /* ss-version */
1872 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1873 disc.fields |= MNCC_F_SSVERSION;
Harald Weltefdc93d92010-03-07 23:40:35 +01001874 gsm48_decode_ssversion(&disc.ssversion,
Harald Welte03740842009-06-10 23:11:52 +08001875 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1876 }
1877
Harald Welte0abe5a62009-07-23 19:06:52 +02001878 return mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &disc);
Harald Welte03740842009-06-10 23:11:52 +08001879
1880}
1881
Harald Weltebbc636a2009-06-11 14:23:20 +08001882static struct gsm_mncc_cause default_cause = {
1883 .location = GSM48_CAUSE_LOC_PRN_S_LU,
1884 .coding = 0,
1885 .rec = 0,
1886 .rec_val = 0,
1887 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
1888 .diag_len = 0,
1889 .diag = { 0 },
1890};
Harald Welte03740842009-06-10 23:11:52 +08001891
1892static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
1893{
1894 struct gsm_mncc *disc = arg;
1895 struct msgb *msg = gsm48_msgb_alloc();
1896 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1897
Harald Welte03740842009-06-10 23:11:52 +08001898 gh->msg_type = GSM48_MT_CC_DISCONNECT;
1899
1900 gsm48_stop_cc_timer(trans);
1901 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
1902
1903 /* cause */
1904 if (disc->fields & MNCC_F_CAUSE)
Harald Weltefdc93d92010-03-07 23:40:35 +01001905 gsm48_encode_cause(msg, 1, &disc->cause);
Harald Welte03740842009-06-10 23:11:52 +08001906 else
Harald Weltefdc93d92010-03-07 23:40:35 +01001907 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte03740842009-06-10 23:11:52 +08001908
1909 /* facility */
1910 if (disc->fields & MNCC_F_FACILITY)
Harald Weltefdc93d92010-03-07 23:40:35 +01001911 gsm48_encode_facility(msg, 0, &disc->facility);
Harald Welte03740842009-06-10 23:11:52 +08001912 /* progress */
1913 if (disc->fields & MNCC_F_PROGRESS)
Harald Weltefdc93d92010-03-07 23:40:35 +01001914 gsm48_encode_progress(msg, 0, &disc->progress);
Harald Welte03740842009-06-10 23:11:52 +08001915 /* user-user */
1916 if (disc->fields & MNCC_F_USERUSER)
Harald Weltefdc93d92010-03-07 23:40:35 +01001917 gsm48_encode_useruser(msg, 0, &disc->useruser);
Harald Welte03740842009-06-10 23:11:52 +08001918
1919 /* store disconnect cause for T306 expiry */
Harald Weltec2189a62009-07-23 18:56:43 +02001920 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
Harald Welte03740842009-06-10 23:11:52 +08001921
1922 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
1923
Harald Welte36fe2e82009-07-23 21:13:03 +02001924 return gsm48_sendmsg(msg, trans);
Harald Welte03740842009-06-10 23:11:52 +08001925}
1926
1927static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
1928{
1929 struct gsm48_hdr *gh = msgb_l3(msg);
1930 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1931 struct tlv_parsed tp;
1932 struct gsm_mncc rel;
1933 int rc;
1934
1935 gsm48_stop_cc_timer(trans);
1936
1937 memset(&rel, 0, sizeof(struct gsm_mncc));
1938 rel.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01001939 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte03740842009-06-10 23:11:52 +08001940 /* cause */
1941 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1942 rel.fields |= MNCC_F_CAUSE;
Harald Weltefdc93d92010-03-07 23:40:35 +01001943 gsm48_decode_cause(&rel.cause,
Harald Welte03740842009-06-10 23:11:52 +08001944 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1945 }
1946 /* facility */
1947 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1948 rel.fields |= MNCC_F_FACILITY;
Harald Weltefdc93d92010-03-07 23:40:35 +01001949 gsm48_decode_facility(&rel.facility,
Harald Welte03740842009-06-10 23:11:52 +08001950 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1951 }
1952 /* user-user */
1953 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1954 rel.fields |= MNCC_F_USERUSER;
Harald Weltefdc93d92010-03-07 23:40:35 +01001955 gsm48_decode_useruser(&rel.useruser,
Harald Welte03740842009-06-10 23:11:52 +08001956 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1957 }
1958 /* ss-version */
1959 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1960 rel.fields |= MNCC_F_SSVERSION;
Harald Weltefdc93d92010-03-07 23:40:35 +01001961 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte03740842009-06-10 23:11:52 +08001962 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1963 }
1964
Harald Weltec2189a62009-07-23 18:56:43 +02001965 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
Harald Welte03740842009-06-10 23:11:52 +08001966 /* release collision 5.4.5 */
Harald Welte0abe5a62009-07-23 19:06:52 +02001967 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_CNF, &rel);
Harald Welte03740842009-06-10 23:11:52 +08001968 } else {
Harald Welte0abe5a62009-07-23 19:06:52 +02001969 rc = gsm48_tx_simple(msg->lchan,
Harald Welte4861c822009-07-23 21:21:14 +02001970 GSM48_PDISC_CC | (trans->transaction_id << 4),
Harald Welte0abe5a62009-07-23 19:06:52 +02001971 GSM48_MT_CC_RELEASE_COMPL);
1972 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_IND, &rel);
Harald Welte03740842009-06-10 23:11:52 +08001973 }
1974
1975 new_cc_state(trans, GSM_CSTATE_NULL);
1976
1977 trans->callref = 0;
Harald Weltec2189a62009-07-23 18:56:43 +02001978 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08001979
1980 return rc;
1981}
1982
1983static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
1984{
1985 struct gsm_mncc *rel = arg;
1986 struct msgb *msg = gsm48_msgb_alloc();
1987 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1988
Harald Welte03740842009-06-10 23:11:52 +08001989 gh->msg_type = GSM48_MT_CC_RELEASE;
1990
1991 trans->callref = 0;
1992
1993 gsm48_stop_cc_timer(trans);
1994 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
1995
1996 /* cause */
1997 if (rel->fields & MNCC_F_CAUSE)
Harald Weltefdc93d92010-03-07 23:40:35 +01001998 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte03740842009-06-10 23:11:52 +08001999 /* facility */
2000 if (rel->fields & MNCC_F_FACILITY)
Harald Weltefdc93d92010-03-07 23:40:35 +01002001 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte03740842009-06-10 23:11:52 +08002002 /* user-user */
2003 if (rel->fields & MNCC_F_USERUSER)
Harald Weltefdc93d92010-03-07 23:40:35 +01002004 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte03740842009-06-10 23:11:52 +08002005
Harald Weltec2189a62009-07-23 18:56:43 +02002006 trans->cc.T308_second = 0;
2007 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
Harald Welte03740842009-06-10 23:11:52 +08002008
Harald Weltec2189a62009-07-23 18:56:43 +02002009 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
Harald Welte03740842009-06-10 23:11:52 +08002010 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
2011
Harald Welte36fe2e82009-07-23 21:13:03 +02002012 return gsm48_sendmsg(msg, trans);
Harald Welte03740842009-06-10 23:11:52 +08002013}
2014
2015static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
2016{
2017 struct gsm48_hdr *gh = msgb_l3(msg);
2018 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2019 struct tlv_parsed tp;
2020 struct gsm_mncc rel;
2021 int rc = 0;
2022
2023 gsm48_stop_cc_timer(trans);
2024
2025 memset(&rel, 0, sizeof(struct gsm_mncc));
2026 rel.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002027 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte03740842009-06-10 23:11:52 +08002028 /* cause */
2029 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2030 rel.fields |= MNCC_F_CAUSE;
Harald Weltefdc93d92010-03-07 23:40:35 +01002031 gsm48_decode_cause(&rel.cause,
Harald Welte03740842009-06-10 23:11:52 +08002032 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2033 }
2034 /* facility */
2035 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2036 rel.fields |= MNCC_F_FACILITY;
Harald Weltefdc93d92010-03-07 23:40:35 +01002037 gsm48_decode_facility(&rel.facility,
Harald Welte03740842009-06-10 23:11:52 +08002038 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2039 }
2040 /* user-user */
2041 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2042 rel.fields |= MNCC_F_USERUSER;
Harald Weltefdc93d92010-03-07 23:40:35 +01002043 gsm48_decode_useruser(&rel.useruser,
Harald Welte03740842009-06-10 23:11:52 +08002044 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2045 }
2046 /* ss-version */
2047 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2048 rel.fields |= MNCC_F_SSVERSION;
Harald Weltefdc93d92010-03-07 23:40:35 +01002049 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte03740842009-06-10 23:11:52 +08002050 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2051 }
2052
2053 if (trans->callref) {
Harald Weltec2189a62009-07-23 18:56:43 +02002054 switch (trans->cc.state) {
Harald Welte03740842009-06-10 23:11:52 +08002055 case GSM_CSTATE_CALL_PRESENT:
Harald Welte0abe5a62009-07-23 19:06:52 +02002056 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte03740842009-06-10 23:11:52 +08002057 MNCC_REJ_IND, &rel);
2058 break;
2059 case GSM_CSTATE_RELEASE_REQ:
Harald Welte0abe5a62009-07-23 19:06:52 +02002060 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte03740842009-06-10 23:11:52 +08002061 MNCC_REL_CNF, &rel);
Harald Welte9ed54ff2009-12-12 21:00:48 +01002062 /* FIXME: in case of multiple calls, we can't simply
2063 * hang up here ! */
2064 lchan_auto_release(msg->lchan);
Harald Welte03740842009-06-10 23:11:52 +08002065 break;
2066 default:
Harald Welte0abe5a62009-07-23 19:06:52 +02002067 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte03740842009-06-10 23:11:52 +08002068 MNCC_REL_IND, &rel);
2069 }
2070 }
2071
2072 trans->callref = 0;
Harald Weltec2189a62009-07-23 18:56:43 +02002073 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08002074
2075 return rc;
2076}
2077
2078static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
2079{
2080 struct gsm_mncc *rel = arg;
2081 struct msgb *msg = gsm48_msgb_alloc();
2082 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2083
Harald Welte03740842009-06-10 23:11:52 +08002084 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
2085
2086 trans->callref = 0;
2087
2088 gsm48_stop_cc_timer(trans);
2089
2090 /* cause */
2091 if (rel->fields & MNCC_F_CAUSE)
Harald Weltefdc93d92010-03-07 23:40:35 +01002092 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte03740842009-06-10 23:11:52 +08002093 /* facility */
2094 if (rel->fields & MNCC_F_FACILITY)
Harald Weltefdc93d92010-03-07 23:40:35 +01002095 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte03740842009-06-10 23:11:52 +08002096 /* user-user */
2097 if (rel->fields & MNCC_F_USERUSER)
Harald Weltefdc93d92010-03-07 23:40:35 +01002098 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte03740842009-06-10 23:11:52 +08002099
Harald Weltec2189a62009-07-23 18:56:43 +02002100 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08002101
Harald Welte36fe2e82009-07-23 21:13:03 +02002102 return gsm48_sendmsg(msg, trans);
Harald Welte03740842009-06-10 23:11:52 +08002103}
2104
2105static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
2106{
2107 struct gsm48_hdr *gh = msgb_l3(msg);
2108 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2109 struct tlv_parsed tp;
2110 struct gsm_mncc fac;
2111
2112 memset(&fac, 0, sizeof(struct gsm_mncc));
2113 fac.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002114 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
Harald Welte03740842009-06-10 23:11:52 +08002115 /* facility */
2116 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2117 fac.fields |= MNCC_F_FACILITY;
Harald Weltefdc93d92010-03-07 23:40:35 +01002118 gsm48_decode_facility(&fac.facility,
Harald Welte03740842009-06-10 23:11:52 +08002119 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2120 }
2121 /* ss-version */
2122 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2123 fac.fields |= MNCC_F_SSVERSION;
Harald Weltefdc93d92010-03-07 23:40:35 +01002124 gsm48_decode_ssversion(&fac.ssversion,
Harald Welte03740842009-06-10 23:11:52 +08002125 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2126 }
2127
Harald Welte0abe5a62009-07-23 19:06:52 +02002128 return mncc_recvmsg(trans->subscr->net, trans, MNCC_FACILITY_IND, &fac);
Harald Welte03740842009-06-10 23:11:52 +08002129}
2130
2131static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
2132{
2133 struct gsm_mncc *fac = arg;
2134 struct msgb *msg = gsm48_msgb_alloc();
2135 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2136
Harald Welte03740842009-06-10 23:11:52 +08002137 gh->msg_type = GSM48_MT_CC_FACILITY;
2138
2139 /* facility */
Harald Weltefdc93d92010-03-07 23:40:35 +01002140 gsm48_encode_facility(msg, 1, &fac->facility);
Harald Welte03740842009-06-10 23:11:52 +08002141
Harald Welte36fe2e82009-07-23 21:13:03 +02002142 return gsm48_sendmsg(msg, trans);
Harald Welte03740842009-06-10 23:11:52 +08002143}
2144
2145static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
2146{
2147 struct gsm_mncc hold;
2148
2149 memset(&hold, 0, sizeof(struct gsm_mncc));
2150 hold.callref = trans->callref;
Harald Welte0abe5a62009-07-23 19:06:52 +02002151 return mncc_recvmsg(trans->subscr->net, trans, MNCC_HOLD_IND, &hold);
Harald Welte03740842009-06-10 23:11:52 +08002152}
2153
2154static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
2155{
2156 struct msgb *msg = gsm48_msgb_alloc();
2157 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2158
Harald Welte03740842009-06-10 23:11:52 +08002159 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
2160
Harald Welte36fe2e82009-07-23 21:13:03 +02002161 return gsm48_sendmsg(msg, trans);
Harald Welte03740842009-06-10 23:11:52 +08002162}
2163
2164static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
2165{
2166 struct gsm_mncc *hold_rej = arg;
2167 struct msgb *msg = gsm48_msgb_alloc();
2168 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2169
Harald Welte03740842009-06-10 23:11:52 +08002170 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
2171
2172 /* cause */
2173 if (hold_rej->fields & MNCC_F_CAUSE)
Harald Weltefdc93d92010-03-07 23:40:35 +01002174 gsm48_encode_cause(msg, 1, &hold_rej->cause);
Harald Welte03740842009-06-10 23:11:52 +08002175 else
Harald Weltefdc93d92010-03-07 23:40:35 +01002176 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte03740842009-06-10 23:11:52 +08002177
Harald Welte36fe2e82009-07-23 21:13:03 +02002178 return gsm48_sendmsg(msg, trans);
Harald Welte03740842009-06-10 23:11:52 +08002179}
2180
2181static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
2182{
2183 struct gsm_mncc retrieve;
2184
2185 memset(&retrieve, 0, sizeof(struct gsm_mncc));
2186 retrieve.callref = trans->callref;
Harald Welte0abe5a62009-07-23 19:06:52 +02002187 return mncc_recvmsg(trans->subscr->net, trans, MNCC_RETRIEVE_IND,
2188 &retrieve);
Harald Welte03740842009-06-10 23:11:52 +08002189}
2190
2191static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
2192{
2193 struct msgb *msg = gsm48_msgb_alloc();
2194 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2195
Harald Welte03740842009-06-10 23:11:52 +08002196 gh->msg_type = GSM48_MT_CC_RETR_ACK;
2197
Harald Welte36fe2e82009-07-23 21:13:03 +02002198 return gsm48_sendmsg(msg, trans);
Harald Welte03740842009-06-10 23:11:52 +08002199}
2200
2201static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
2202{
2203 struct gsm_mncc *retrieve_rej = arg;
2204 struct msgb *msg = gsm48_msgb_alloc();
2205 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2206
Harald Welte03740842009-06-10 23:11:52 +08002207 gh->msg_type = GSM48_MT_CC_RETR_REJ;
2208
2209 /* cause */
2210 if (retrieve_rej->fields & MNCC_F_CAUSE)
Harald Weltefdc93d92010-03-07 23:40:35 +01002211 gsm48_encode_cause(msg, 1, &retrieve_rej->cause);
Harald Welte03740842009-06-10 23:11:52 +08002212 else
Harald Weltefdc93d92010-03-07 23:40:35 +01002213 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte03740842009-06-10 23:11:52 +08002214
Harald Welte36fe2e82009-07-23 21:13:03 +02002215 return gsm48_sendmsg(msg, trans);
Harald Welte03740842009-06-10 23:11:52 +08002216}
2217
2218static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
2219{
2220 struct gsm48_hdr *gh = msgb_l3(msg);
2221 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2222 struct tlv_parsed tp;
2223 struct gsm_mncc dtmf;
2224
2225 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2226 dtmf.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002227 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte03740842009-06-10 23:11:52 +08002228 /* keypad facility */
2229 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
2230 dtmf.fields |= MNCC_F_KEYPAD;
Harald Weltefdc93d92010-03-07 23:40:35 +01002231 gsm48_decode_keypad(&dtmf.keypad,
Harald Welte03740842009-06-10 23:11:52 +08002232 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
2233 }
2234
Harald Welte0abe5a62009-07-23 19:06:52 +02002235 return mncc_recvmsg(trans->subscr->net, trans, MNCC_START_DTMF_IND, &dtmf);
Harald Welte03740842009-06-10 23:11:52 +08002236}
2237
2238static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
2239{
2240 struct gsm_mncc *dtmf = arg;
2241 struct msgb *msg = gsm48_msgb_alloc();
2242 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2243
Harald Welte03740842009-06-10 23:11:52 +08002244 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
2245
2246 /* keypad */
2247 if (dtmf->fields & MNCC_F_KEYPAD)
Harald Weltefdc93d92010-03-07 23:40:35 +01002248 gsm48_encode_keypad(msg, dtmf->keypad);
Harald Welte03740842009-06-10 23:11:52 +08002249
Harald Welte36fe2e82009-07-23 21:13:03 +02002250 return gsm48_sendmsg(msg, trans);
Harald Welte03740842009-06-10 23:11:52 +08002251}
2252
2253static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
2254{
2255 struct gsm_mncc *dtmf = arg;
2256 struct msgb *msg = gsm48_msgb_alloc();
2257 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2258
Harald Welte03740842009-06-10 23:11:52 +08002259 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
2260
2261 /* cause */
2262 if (dtmf->fields & MNCC_F_CAUSE)
Harald Weltefdc93d92010-03-07 23:40:35 +01002263 gsm48_encode_cause(msg, 1, &dtmf->cause);
Harald Welte03740842009-06-10 23:11:52 +08002264 else
Harald Weltefdc93d92010-03-07 23:40:35 +01002265 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte03740842009-06-10 23:11:52 +08002266
Harald Welte36fe2e82009-07-23 21:13:03 +02002267 return gsm48_sendmsg(msg, trans);
Harald Welte03740842009-06-10 23:11:52 +08002268}
2269
2270static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
2271{
2272 struct msgb *msg = gsm48_msgb_alloc();
2273 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2274
Harald Welte03740842009-06-10 23:11:52 +08002275 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
2276
Harald Welte36fe2e82009-07-23 21:13:03 +02002277 return gsm48_sendmsg(msg, trans);
Harald Welte03740842009-06-10 23:11:52 +08002278}
2279
2280static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
2281{
2282 struct gsm_mncc dtmf;
2283
2284 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2285 dtmf.callref = trans->callref;
2286
Harald Welte0abe5a62009-07-23 19:06:52 +02002287 return mncc_recvmsg(trans->subscr->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
Harald Welte03740842009-06-10 23:11:52 +08002288}
2289
2290static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
2291{
2292 struct gsm48_hdr *gh = msgb_l3(msg);
2293 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2294 struct tlv_parsed tp;
2295 struct gsm_mncc modify;
2296
2297 memset(&modify, 0, sizeof(struct gsm_mncc));
2298 modify.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002299 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte03740842009-06-10 23:11:52 +08002300 /* bearer capability */
2301 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2302 modify.fields |= MNCC_F_BEARER_CAP;
Harald Weltefdc93d92010-03-07 23:40:35 +01002303 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte03740842009-06-10 23:11:52 +08002304 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2305 }
2306
2307 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
2308
Harald Welte0abe5a62009-07-23 19:06:52 +02002309 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_IND, &modify);
Harald Welte03740842009-06-10 23:11:52 +08002310}
2311
2312static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
2313{
2314 struct gsm_mncc *modify = arg;
2315 struct msgb *msg = gsm48_msgb_alloc();
2316 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2317
Harald Welte03740842009-06-10 23:11:52 +08002318 gh->msg_type = GSM48_MT_CC_MODIFY;
2319
2320 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
2321
2322 /* bearer capability */
Harald Weltefdc93d92010-03-07 23:40:35 +01002323 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte03740842009-06-10 23:11:52 +08002324
2325 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
2326
Harald Welte36fe2e82009-07-23 21:13:03 +02002327 return gsm48_sendmsg(msg, trans);
Harald Welte03740842009-06-10 23:11:52 +08002328}
2329
2330static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
2331{
2332 struct gsm48_hdr *gh = msgb_l3(msg);
2333 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2334 struct tlv_parsed tp;
2335 struct gsm_mncc modify;
2336
2337 gsm48_stop_cc_timer(trans);
2338
2339 memset(&modify, 0, sizeof(struct gsm_mncc));
2340 modify.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002341 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte03740842009-06-10 23:11:52 +08002342 /* bearer capability */
2343 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2344 modify.fields |= MNCC_F_BEARER_CAP;
Harald Weltefdc93d92010-03-07 23:40:35 +01002345 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte03740842009-06-10 23:11:52 +08002346 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2347 }
2348
2349 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2350
Harald Welte0abe5a62009-07-23 19:06:52 +02002351 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_CNF, &modify);
Harald Welte03740842009-06-10 23:11:52 +08002352}
2353
2354static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
2355{
2356 struct gsm_mncc *modify = arg;
2357 struct msgb *msg = gsm48_msgb_alloc();
2358 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2359
Harald Welte03740842009-06-10 23:11:52 +08002360 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
2361
2362 /* bearer capability */
Harald Weltefdc93d92010-03-07 23:40:35 +01002363 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte03740842009-06-10 23:11:52 +08002364
2365 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2366
Harald Welte36fe2e82009-07-23 21:13:03 +02002367 return gsm48_sendmsg(msg, trans);
Harald Welte03740842009-06-10 23:11:52 +08002368}
2369
2370static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
2371{
2372 struct gsm48_hdr *gh = msgb_l3(msg);
2373 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2374 struct tlv_parsed tp;
2375 struct gsm_mncc modify;
2376
2377 gsm48_stop_cc_timer(trans);
2378
2379 memset(&modify, 0, sizeof(struct gsm_mncc));
2380 modify.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002381 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
Harald Welte03740842009-06-10 23:11:52 +08002382 /* bearer capability */
2383 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2384 modify.fields |= GSM48_IE_BEARER_CAP;
Harald Weltefdc93d92010-03-07 23:40:35 +01002385 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte03740842009-06-10 23:11:52 +08002386 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2387 }
2388 /* cause */
2389 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2390 modify.fields |= MNCC_F_CAUSE;
Harald Weltefdc93d92010-03-07 23:40:35 +01002391 gsm48_decode_cause(&modify.cause,
Harald Welte03740842009-06-10 23:11:52 +08002392 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2393 }
2394
2395 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2396
Harald Welte0abe5a62009-07-23 19:06:52 +02002397 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_REJ, &modify);
Harald Welte03740842009-06-10 23:11:52 +08002398}
2399
2400static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
2401{
2402 struct gsm_mncc *modify = arg;
2403 struct msgb *msg = gsm48_msgb_alloc();
2404 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2405
Harald Welte03740842009-06-10 23:11:52 +08002406 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
2407
2408 /* bearer capability */
Harald Weltefdc93d92010-03-07 23:40:35 +01002409 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte03740842009-06-10 23:11:52 +08002410 /* cause */
Harald Weltefdc93d92010-03-07 23:40:35 +01002411 gsm48_encode_cause(msg, 1, &modify->cause);
Harald Welte03740842009-06-10 23:11:52 +08002412
2413 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2414
Harald Welte36fe2e82009-07-23 21:13:03 +02002415 return gsm48_sendmsg(msg, trans);
Harald Welte03740842009-06-10 23:11:52 +08002416}
2417
2418static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
2419{
2420 struct gsm_mncc *notify = arg;
2421 struct msgb *msg = gsm48_msgb_alloc();
2422 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2423
Harald Welte03740842009-06-10 23:11:52 +08002424 gh->msg_type = GSM48_MT_CC_NOTIFY;
2425
2426 /* notify */
Harald Weltefdc93d92010-03-07 23:40:35 +01002427 gsm48_encode_notify(msg, notify->notify);
Harald Welte03740842009-06-10 23:11:52 +08002428
Harald Welte36fe2e82009-07-23 21:13:03 +02002429 return gsm48_sendmsg(msg, trans);
Harald Welte03740842009-06-10 23:11:52 +08002430}
2431
2432static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
2433{
2434 struct gsm48_hdr *gh = msgb_l3(msg);
2435 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2436// struct tlv_parsed tp;
2437 struct gsm_mncc notify;
2438
2439 memset(&notify, 0, sizeof(struct gsm_mncc));
2440 notify.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002441// tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len);
Harald Welte03740842009-06-10 23:11:52 +08002442 if (payload_len >= 1)
Harald Weltefdc93d92010-03-07 23:40:35 +01002443 gsm48_decode_notify(&notify.notify, gh->data);
Harald Welte03740842009-06-10 23:11:52 +08002444
Harald Welte0abe5a62009-07-23 19:06:52 +02002445 return mncc_recvmsg(trans->subscr->net, trans, MNCC_NOTIFY_IND, &notify);
Harald Welte03740842009-06-10 23:11:52 +08002446}
2447
2448static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
2449{
2450 struct gsm_mncc *user = arg;
2451 struct msgb *msg = gsm48_msgb_alloc();
2452 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2453
Harald Welte03740842009-06-10 23:11:52 +08002454 gh->msg_type = GSM48_MT_CC_USER_INFO;
2455
2456 /* user-user */
2457 if (user->fields & MNCC_F_USERUSER)
Harald Weltefdc93d92010-03-07 23:40:35 +01002458 gsm48_encode_useruser(msg, 1, &user->useruser);
Harald Welte03740842009-06-10 23:11:52 +08002459 /* more data */
2460 if (user->more)
Harald Weltefdc93d92010-03-07 23:40:35 +01002461 gsm48_encode_more(msg);
Harald Welte03740842009-06-10 23:11:52 +08002462
Harald Welte36fe2e82009-07-23 21:13:03 +02002463 return gsm48_sendmsg(msg, trans);
Harald Welte03740842009-06-10 23:11:52 +08002464}
2465
2466static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
2467{
2468 struct gsm48_hdr *gh = msgb_l3(msg);
2469 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2470 struct tlv_parsed tp;
2471 struct gsm_mncc user;
2472
2473 memset(&user, 0, sizeof(struct gsm_mncc));
2474 user.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002475 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
Harald Welte03740842009-06-10 23:11:52 +08002476 /* user-user */
2477 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2478 user.fields |= MNCC_F_USERUSER;
Harald Weltefdc93d92010-03-07 23:40:35 +01002479 gsm48_decode_useruser(&user.useruser,
Harald Welte03740842009-06-10 23:11:52 +08002480 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2481 }
2482 /* more data */
2483 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
2484 user.more = 1;
2485
Harald Welte0abe5a62009-07-23 19:06:52 +02002486 return mncc_recvmsg(trans->subscr->net, trans, MNCC_USERINFO_IND, &user);
Harald Welte03740842009-06-10 23:11:52 +08002487}
2488
Holger Hans Peter Freythercf0f3362009-10-22 15:13:00 +02002489static int _gsm48_lchan_modify(struct gsm_trans *trans, void *arg)
Harald Welte03740842009-06-10 23:11:52 +08002490{
2491 struct gsm_mncc *mode = arg;
2492
Holger Hans Peter Freyther3cce58f2009-11-18 22:57:02 +01002493 return gsm48_lchan_modify(trans->lchan, mode->lchan_mode);
Harald Welte03740842009-06-10 23:11:52 +08002494}
2495
2496static struct downstate {
2497 u_int32_t states;
2498 int type;
2499 int (*rout) (struct gsm_trans *trans, void *arg);
2500} downstatelist[] = {
2501 /* mobile originating call establishment */
2502 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
2503 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc},
2504 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
2505 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
2506 {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 */
2507 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
2508 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
2509 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
2510 /* mobile terminating call establishment */
2511 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
2512 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
2513 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
2514 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
2515 /* signalling during call */
2516 {SBIT(GSM_CSTATE_ACTIVE),
2517 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
2518 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
2519 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
2520 {ALL_STATES,
2521 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
2522 {ALL_STATES,
2523 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
2524 {ALL_STATES,
2525 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
2526 {SBIT(GSM_CSTATE_ACTIVE),
2527 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
2528 {SBIT(GSM_CSTATE_ACTIVE),
2529 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
2530 {SBIT(GSM_CSTATE_ACTIVE),
2531 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
2532 {SBIT(GSM_CSTATE_ACTIVE),
2533 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
2534 {SBIT(GSM_CSTATE_ACTIVE),
2535 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
2536 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2537 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
2538 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2539 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
2540 {SBIT(GSM_CSTATE_ACTIVE),
2541 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
2542 /* clearing */
2543 {SBIT(GSM_CSTATE_INITIATED),
2544 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
2545 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
2546 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
2547 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
2548 MNCC_REL_REQ, gsm48_cc_tx_release},
2549 /* special */
2550 {ALL_STATES,
Holger Hans Peter Freythercf0f3362009-10-22 15:13:00 +02002551 MNCC_LCHAN_MODIFY, _gsm48_lchan_modify},
Harald Welte03740842009-06-10 23:11:52 +08002552};
2553
2554#define DOWNSLLEN \
2555 (sizeof(downstatelist) / sizeof(struct downstate))
2556
2557
2558int mncc_send(struct gsm_network *net, int msg_type, void *arg)
2559{
Harald Welteaa60edb2009-08-09 18:52:33 +02002560 int i, rc = 0;
Harald Welte03740842009-06-10 23:11:52 +08002561 struct gsm_trans *trans = NULL, *transt;
Harald Welteaa60edb2009-08-09 18:52:33 +02002562 struct gsm_lchan *lchan = NULL;
Harald Welte03740842009-06-10 23:11:52 +08002563 struct gsm_bts *bts = NULL;
Harald Welte03740842009-06-10 23:11:52 +08002564 struct gsm_mncc *data = arg, rel;
2565
2566 /* handle special messages */
2567 switch(msg_type) {
2568 case MNCC_BRIDGE:
2569 return tch_bridge(net, arg);
2570 case MNCC_FRAME_DROP:
Harald Welte3971ad52009-12-19 22:23:05 +01002571 return tch_recv_mncc(net, data->callref, 0);
Harald Welte03740842009-06-10 23:11:52 +08002572 case MNCC_FRAME_RECV:
Harald Welte3971ad52009-12-19 22:23:05 +01002573 return tch_recv_mncc(net, data->callref, 1);
2574 case GSM_TCHF_FRAME:
2575 /* Find callref */
2576 trans = trans_find_by_callref(net, data->callref);
2577 if (!trans)
2578 return -EIO;
2579 if (!trans->lchan)
2580 return 0;
2581 if (trans->lchan->type != GSM_LCHAN_TCH_F)
2582 return 0;
2583 bts = trans->lchan->ts->trx->bts;
2584 switch (bts->type) {
2585 case GSM_BTS_TYPE_NANOBTS:
2586 if (!trans->lchan->abis_ip.rtp_socket)
2587 return 0;
2588 return rtp_send_frame(trans->lchan->abis_ip.rtp_socket, arg);
2589 case GSM_BTS_TYPE_BS11:
2590 return trau_send_frame(trans->lchan, arg);
2591 default:
2592 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
2593 }
2594 return -EINVAL;
Harald Welte03740842009-06-10 23:11:52 +08002595 }
2596
2597 memset(&rel, 0, sizeof(struct gsm_mncc));
2598 rel.callref = data->callref;
2599
2600 /* Find callref */
Harald Weltec2189a62009-07-23 18:56:43 +02002601 trans = trans_find_by_callref(net, data->callref);
Harald Welte03740842009-06-10 23:11:52 +08002602
2603 /* Callref unknown */
2604 if (!trans) {
Holger Hans Peter Freythere2a3c9f2009-10-27 14:21:14 +01002605 struct gsm_subscriber *subscr;
2606
Harald Welte6e1536e2009-07-04 10:11:24 +02002607 if (msg_type != MNCC_SETUP_REQ) {
Harald Welte03740842009-06-10 23:11:52 +08002608 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2609 "Received '%s' from MNCC with "
2610 "unknown callref %d\n", data->called.number,
2611 get_mncc_name(msg_type), data->callref);
2612 /* Invalid call reference */
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08002613 return mncc_release_ind(net, NULL, data->callref,
2614 GSM48_CAUSE_LOC_PRN_S_LU,
2615 GSM48_CC_CAUSE_INVAL_TRANS_ID);
Harald Welte03740842009-06-10 23:11:52 +08002616 }
Andreas Eversberg9eaa5da2009-06-15 23:22:09 +02002617 if (!data->called.number[0] && !data->imsi[0]) {
2618 DEBUGP(DCC, "(bts - trx - ts - ti) "
2619 "Received '%s' from MNCC with "
2620 "no number or IMSI\n", get_mncc_name(msg_type));
2621 /* Invalid number */
2622 return mncc_release_ind(net, NULL, data->callref,
2623 GSM48_CAUSE_LOC_PRN_S_LU,
2624 GSM48_CC_CAUSE_INV_NR_FORMAT);
2625 }
Harald Welte03740842009-06-10 23:11:52 +08002626 /* New transaction due to setup, find subscriber */
Andreas Eversberg9eaa5da2009-06-15 23:22:09 +02002627 if (data->called.number[0])
Harald Welte75350412009-07-23 18:46:00 +02002628 subscr = subscr_get_by_extension(net,
2629 data->called.number);
Andreas Eversberg9eaa5da2009-06-15 23:22:09 +02002630 else
Harald Welte75350412009-07-23 18:46:00 +02002631 subscr = subscr_get_by_imsi(net, data->imsi);
Harald Welte03740842009-06-10 23:11:52 +08002632 /* If subscriber is not found */
2633 if (!subscr) {
2634 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2635 "Received '%s' from MNCC with "
2636 "unknown subscriber %s\n", data->called.number,
2637 get_mncc_name(msg_type), data->called.number);
2638 /* Unknown subscriber */
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08002639 return mncc_release_ind(net, NULL, data->callref,
2640 GSM48_CAUSE_LOC_PRN_S_LU,
2641 GSM48_CC_CAUSE_UNASSIGNED_NR);
Harald Welte03740842009-06-10 23:11:52 +08002642 }
2643 /* If subscriber is not "attached" */
2644 if (!subscr->lac) {
2645 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2646 "Received '%s' from MNCC with "
2647 "detached subscriber %s\n", data->called.number,
2648 get_mncc_name(msg_type), data->called.number);
2649 subscr_put(subscr);
2650 /* Temporarily out of order */
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08002651 return mncc_release_ind(net, NULL, data->callref,
2652 GSM48_CAUSE_LOC_PRN_S_LU,
2653 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte03740842009-06-10 23:11:52 +08002654 }
2655 /* Create transaction */
Harald Weltec2189a62009-07-23 18:56:43 +02002656 trans = trans_alloc(subscr, GSM48_PDISC_CC, 0xff, data->callref);
2657 if (!trans) {
Harald Welte03740842009-06-10 23:11:52 +08002658 DEBUGP(DCC, "No memory for trans.\n");
2659 subscr_put(subscr);
2660 /* Ressource unavailable */
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08002661 mncc_release_ind(net, NULL, data->callref,
2662 GSM48_CAUSE_LOC_PRN_S_LU,
2663 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte03740842009-06-10 23:11:52 +08002664 return -ENOMEM;
2665 }
Harald Welte03740842009-06-10 23:11:52 +08002666 /* Find lchan */
Harald Welteaa60edb2009-08-09 18:52:33 +02002667 lchan = lchan_for_subscr(subscr);
Harald Welte03740842009-06-10 23:11:52 +08002668 /* If subscriber has no lchan */
2669 if (!lchan) {
2670 /* find transaction with this subscriber already paging */
2671 llist_for_each_entry(transt, &net->trans_list, entry) {
2672 /* Transaction of our lchan? */
2673 if (transt == trans ||
2674 transt->subscr != subscr)
2675 continue;
2676 DEBUGP(DCC, "(bts %d trx - ts - ti -- sub %s) "
2677 "Received '%s' from MNCC with "
2678 "unallocated channel, paging already "
2679 "started.\n", bts->nr,
2680 data->called.number,
2681 get_mncc_name(msg_type));
Holger Hans Peter Freythere2a3c9f2009-10-27 14:21:14 +01002682 subscr_put(subscr);
2683 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08002684 return 0;
2685 }
2686 /* store setup informations until paging was successfull */
Harald Weltec2189a62009-07-23 18:56:43 +02002687 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
Harald Welte2a3a81b2009-08-01 19:31:47 +02002688 /* Trigger paging */
2689 paging_request(net, subscr, RSL_CHANNEED_TCH_F,
2690 setup_trig_pag_evt, subscr);
Holger Hans Peter Freythere2a3c9f2009-10-27 14:21:14 +01002691 subscr_put(subscr);
Harald Welte03740842009-06-10 23:11:52 +08002692 return 0;
2693 }
2694 /* Assign lchan */
2695 trans->lchan = lchan;
2696 use_lchan(lchan);
Holger Hans Peter Freythere2a3c9f2009-10-27 14:21:14 +01002697 subscr_put(subscr);
Harald Welte03740842009-06-10 23:11:52 +08002698 }
2699 lchan = trans->lchan;
2700
2701 /* if paging did not respond yet */
2702 if (!lchan) {
2703 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2704 "Received '%s' from MNCC in paging state\n",
2705 (trans->subscr)?(trans->subscr->extension):"-",
2706 get_mncc_name(msg_type));
Harald Weltebbc636a2009-06-11 14:23:20 +08002707 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
2708 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Harald Welte03740842009-06-10 23:11:52 +08002709 if (msg_type == MNCC_REL_REQ)
2710 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
2711 else
2712 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
2713 trans->callref = 0;
Harald Weltec2189a62009-07-23 18:56:43 +02002714 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08002715 return rc;
2716 }
2717
2718 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x sub %s) "
2719 "Received '%s' from MNCC in state %d (%s)\n",
2720 lchan->ts->trx->bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
2721 trans->transaction_id,
2722 (lchan->subscr)?(lchan->subscr->extension):"-",
Harald Weltec2189a62009-07-23 18:56:43 +02002723 get_mncc_name(msg_type), trans->cc.state,
Harald Welteb30935e2010-03-25 12:13:02 +08002724 gsm48_cc_state_name(trans->cc.state));
Harald Welte03740842009-06-10 23:11:52 +08002725
2726 /* Find function for current state and message */
2727 for (i = 0; i < DOWNSLLEN; i++)
2728 if ((msg_type == downstatelist[i].type)
Harald Weltec2189a62009-07-23 18:56:43 +02002729 && ((1 << trans->cc.state) & downstatelist[i].states))
Harald Welte03740842009-06-10 23:11:52 +08002730 break;
2731 if (i == DOWNSLLEN) {
2732 DEBUGP(DCC, "Message unhandled at this state.\n");
2733 return 0;
2734 }
2735
2736 rc = downstatelist[i].rout(trans, arg);
2737
2738 return rc;
2739}
2740
2741
2742static struct datastate {
2743 u_int32_t states;
2744 int type;
2745 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
2746} datastatelist[] = {
2747 /* mobile originating call establishment */
2748 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
2749 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
2750 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
2751 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
2752 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
2753 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
2754 /* mobile terminating call establishment */
2755 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
2756 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
2757 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
2758 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
2759 {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 */
2760 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
2761 /* signalling during call */
2762 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
2763 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
2764 {SBIT(GSM_CSTATE_ACTIVE),
2765 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
2766 {ALL_STATES,
2767 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
2768 {ALL_STATES,
2769 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
2770 {ALL_STATES,
2771 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
2772 {SBIT(GSM_CSTATE_ACTIVE),
2773 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
2774 {SBIT(GSM_CSTATE_ACTIVE),
2775 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
2776 {SBIT(GSM_CSTATE_ACTIVE),
2777 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
2778 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
2779 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
2780 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
2781 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
2782 {SBIT(GSM_CSTATE_ACTIVE),
2783 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
2784 /* clearing */
2785 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
2786 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
2787 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
2788 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
2789 {ALL_STATES, /* 5.4.3.4 */
2790 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
2791};
2792
2793#define DATASLLEN \
2794 (sizeof(datastatelist) / sizeof(struct datastate))
2795
Harald Welte59b04682009-06-10 05:40:52 +08002796static int gsm0408_rcv_cc(struct msgb *msg)
2797{
2798 struct gsm48_hdr *gh = msgb_l3(msg);
2799 u_int8_t msg_type = gh->msg_type & 0xbf;
Harald Welte4861c822009-07-23 21:21:14 +02002800 u_int8_t transaction_id = ((gh->proto_discr & 0xf0) ^ 0x80) >> 4; /* flip */
Harald Welte03740842009-06-10 23:11:52 +08002801 struct gsm_lchan *lchan = msg->lchan;
Harald Weltec2189a62009-07-23 18:56:43 +02002802 struct gsm_trans *trans = NULL;
Harald Welte03740842009-06-10 23:11:52 +08002803 int i, rc = 0;
Harald Welte59b04682009-06-10 05:40:52 +08002804
Harald Welte03740842009-06-10 23:11:52 +08002805 if (msg_type & 0x80) {
2806 DEBUGP(DCC, "MSG 0x%2x not defined for PD error\n", msg_type);
2807 return -EINVAL;
Harald Welte59b04682009-06-10 05:40:52 +08002808 }
Harald Welte03740842009-06-10 23:11:52 +08002809
2810 /* Find transaction */
Harald Welte0d824162009-07-23 21:58:40 +02002811 trans = trans_find_by_id(lchan->subscr, GSM48_PDISC_CC, transaction_id);
Harald Weltec2189a62009-07-23 18:56:43 +02002812
Harald Welte4861c822009-07-23 21:21:14 +02002813 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte03740842009-06-10 23:11:52 +08002814 "Received '%s' from MS in state %d (%s)\n",
2815 lchan->ts->trx->bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
2816 transaction_id, (lchan->subscr)?(lchan->subscr->extension):"-",
Harald Welteb30935e2010-03-25 12:13:02 +08002817 gsm48_cc_msg_name(msg_type), trans?(trans->cc.state):0,
2818 gsm48_cc_state_name(trans?(trans->cc.state):0));
Harald Welte03740842009-06-10 23:11:52 +08002819
2820 /* Create transaction */
2821 if (!trans) {
Harald Welte4861c822009-07-23 21:21:14 +02002822 DEBUGP(DCC, "Unknown transaction ID %x, "
Harald Welte03740842009-06-10 23:11:52 +08002823 "creating new trans.\n", transaction_id);
2824 /* Create transaction */
Harald Weltec2189a62009-07-23 18:56:43 +02002825 trans = trans_alloc(lchan->subscr, GSM48_PDISC_CC,
2826 transaction_id, new_callref++);
2827 if (!trans) {
Harald Welte03740842009-06-10 23:11:52 +08002828 DEBUGP(DCC, "No memory for trans.\n");
2829 rc = gsm48_tx_simple(msg->lchan,
Harald Welte4861c822009-07-23 21:21:14 +02002830 GSM48_PDISC_CC | (transaction_id << 4),
Harald Welte03740842009-06-10 23:11:52 +08002831 GSM48_MT_CC_RELEASE_COMPL);
2832 return -ENOMEM;
2833 }
Harald Welte03740842009-06-10 23:11:52 +08002834 /* Assign transaction */
Harald Welte03740842009-06-10 23:11:52 +08002835 trans->lchan = lchan;
2836 use_lchan(lchan);
Harald Welte03740842009-06-10 23:11:52 +08002837 }
2838
2839 /* find function for current state and message */
2840 for (i = 0; i < DATASLLEN; i++)
2841 if ((msg_type == datastatelist[i].type)
Harald Weltec2189a62009-07-23 18:56:43 +02002842 && ((1 << trans->cc.state) & datastatelist[i].states))
Harald Welte03740842009-06-10 23:11:52 +08002843 break;
2844 if (i == DATASLLEN) {
2845 DEBUGP(DCC, "Message unhandled at this state.\n");
2846 return 0;
2847 }
2848
2849 rc = datastatelist[i].rout(trans, msg);
Harald Welte59b04682009-06-10 05:40:52 +08002850
2851 return rc;
2852}
2853
2854/* here we pass in a msgb from the RSL->RLL. We expect the l3 pointer to be set */
Harald Welte (local)64994ce2009-08-14 11:41:12 +02002855int gsm0408_rcvmsg(struct msgb *msg, u_int8_t link_id)
Harald Welte59b04682009-06-10 05:40:52 +08002856{
2857 struct gsm48_hdr *gh = msgb_l3(msg);
2858 u_int8_t pdisc = gh->proto_discr & 0x0f;
2859 int rc = 0;
Harald Weltef6a38ec2009-12-29 11:49:12 +01002860
2861 if (silent_call_reroute(msg))
2862 return silent_call_rx(msg);
Harald Welte59b04682009-06-10 05:40:52 +08002863
2864 switch (pdisc) {
2865 case GSM48_PDISC_CC:
2866 rc = gsm0408_rcv_cc(msg);
2867 break;
2868 case GSM48_PDISC_MM:
2869 rc = gsm0408_rcv_mm(msg);
2870 break;
2871 case GSM48_PDISC_RR:
2872 rc = gsm0408_rcv_rr(msg);
2873 break;
2874 case GSM48_PDISC_SMS:
Harald Welte (local)64994ce2009-08-14 11:41:12 +02002875 rc = gsm0411_rcv_sms(msg, link_id);
Harald Welte59b04682009-06-10 05:40:52 +08002876 break;
2877 case GSM48_PDISC_MM_GPRS:
2878 case GSM48_PDISC_SM_GPRS:
Harald Welte60e5f4f2009-12-24 12:13:17 +01002879 LOGP(DRLL, LOGL_NOTICE, "Unimplemented "
2880 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Harald Welte59b04682009-06-10 05:40:52 +08002881 break;
Harald Welte03115042009-10-16 08:32:58 +02002882 case GSM48_PDISC_NC_SS:
2883 rc = handle_rcv_ussd(msg);
2884 break;
Harald Welte59b04682009-06-10 05:40:52 +08002885 default:
Harald Welte60e5f4f2009-12-24 12:13:17 +01002886 LOGP(DRLL, LOGL_NOTICE, "Unknown "
2887 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Harald Welte59b04682009-06-10 05:40:52 +08002888 break;
2889 }
2890
2891 return rc;
2892}
2893
Harald Welte03740842009-06-10 23:11:52 +08002894/* dequeue messages to layer 4 */
2895int bsc_upqueue(struct gsm_network *net)
2896{
2897 struct gsm_mncc *mncc;
2898 struct msgb *msg;
2899 int work = 0;
2900
2901 if (net)
2902 while ((msg = msgb_dequeue(&net->upqueue))) {
2903 mncc = (struct gsm_mncc *)msg->data;
2904 if (net->mncc_recv)
2905 net->mncc_recv(net, mncc->msg_type, mncc);
2906 work = 1; /* work done */
Harald Weltebaf4d3a2009-06-26 19:40:48 +02002907 talloc_free(msg);
Harald Welte03740842009-06-10 23:11:52 +08002908 }
2909
2910 return work;
2911}
Harald Weltec2189a62009-07-23 18:56:43 +02002912
Harald Welte3c062072009-07-28 18:25:29 +02002913/*
2914 * This will be ran by the linker when loading the DSO. We use it to
2915 * do system initialization, e.g. registration of signal handlers.
2916 */
2917static __attribute__((constructor)) void on_dso_load_0408(void)
2918{
Harald Welte3c062072009-07-28 18:25:29 +02002919 register_signal_handler(SS_LCHAN, gsm0408_handle_lchan_signal, NULL);
2920 register_signal_handler(SS_ABISIP, handle_abisip_signal, NULL);
2921}