blob: 2eda0bd991335f4b4913a7edfec91ce45987756a [file] [log] [blame]
Harald Welte52b1f982008-12-23 20:25:15 +00001/* 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
Harald Weltebf5e8df2009-02-03 12:59:45 +00004/* (C) 2008-2009 by Harald Welte <laforge@gnumonks.org>
Harald Welte498b0bb2009-01-09 21:27:43 +00005 * (C) 2008, 2009 by Holger Hans Peter Freyther <zecke@selfish.org>
Harald Welte8470bf22008-12-25 23:28:35 +00006 *
Harald Welte52b1f982008-12-23 20:25:15 +00007 * All Rights Reserved
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 *
23 */
24
25
26#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
29#include <errno.h>
Harald Weltedb253af2008-12-30 17:56:55 +000030#include <time.h>
Harald Welte4b634542008-12-27 01:55:51 +000031#include <netinet/in.h>
Harald Welte52b1f982008-12-23 20:25:15 +000032
Harald Welte75a983f2008-12-27 21:34:06 +000033#include <openbsc/db.h>
Harald Weltedfe6c7d2010-02-20 16:24:02 +010034#include <osmocore/msgb.h>
35#include <osmocore/bitvec.h>
36#include <osmocore/tlv.h>
Harald Welte8470bf22008-12-25 23:28:35 +000037#include <openbsc/debug.h>
38#include <openbsc/gsm_data.h>
Harald Weltedfe6c7d2010-02-20 16:24:02 +010039#include <osmocore/gsm_utils.h>
Harald Welte8470bf22008-12-25 23:28:35 +000040#include <openbsc/gsm_subscriber.h>
Daniel Willmann8b3390e2008-12-28 00:31:09 +000041#include <openbsc/gsm_04_11.h>
Harald Welte8470bf22008-12-25 23:28:35 +000042#include <openbsc/gsm_04_08.h>
43#include <openbsc/abis_rsl.h>
Holger Freytherca362a62009-01-04 21:05:01 +000044#include <openbsc/chan_alloc.h>
Harald Welte0b4c34e2009-02-09 17:54:43 +000045#include <openbsc/paging.h>
Holger Freyther053e09d2009-02-14 22:51:06 +000046#include <openbsc/signal.h>
Harald Welte45b407a2009-05-23 15:51:12 +000047#include <openbsc/trau_frame.h>
Harald Welte11fa29c2009-02-19 17:24:39 +000048#include <openbsc/trau_mux.h>
Harald Welte805f6442009-07-28 18:25:29 +020049#include <openbsc/rtp_proxy.h>
Harald Weltedfe6c7d2010-02-20 16:24:02 +010050#include <osmocore/talloc.h>
Harald Welte55c8f352010-03-07 23:40:35 +010051#include <osmocore/gsm48.h>
Harald Weltedcaf5652009-07-23 18:56:43 +020052#include <openbsc/transaction.h>
Harald Welte6eafe912009-10-16 08:32:58 +020053#include <openbsc/ussd.h>
Harald Welte51008772009-12-29 11:49:12 +010054#include <openbsc/silent_call.h>
Harald Welte52b1f982008-12-23 20:25:15 +000055
Harald Welte (local)d19e58b2009-08-15 02:30:58 +020056void *tall_locop_ctx;
Harald Welte2cf161b2009-06-20 22:36:41 +020057
Holger Freytherd51524f2009-06-09 08:27:07 +000058int gsm0408_loc_upd_acc(struct gsm_lchan *lchan, u_int32_t tmsi);
Harald Welte65e74cc2008-12-29 01:55:35 +000059static int gsm48_tx_simple(struct gsm_lchan *lchan,
60 u_int8_t pdisc, u_int8_t msg_type);
Holger Freytherb7193e42008-12-29 17:44:08 +000061static void schedule_reject(struct gsm_lchan *lchan);
Harald Welte65e74cc2008-12-29 01:55:35 +000062
Harald Welte52b1f982008-12-23 20:25:15 +000063struct gsm_lai {
64 u_int16_t mcc;
65 u_int16_t mnc;
66 u_int16_t lac;
67};
68
Harald Welte4bfdfe72009-06-10 23:11:52 +080069static u_int32_t new_callref = 0x80000001;
70
Holger Freyther73487a22008-12-31 18:53:57 +000071static int authorize_subscriber(struct gsm_loc_updating_operation *loc,
72 struct gsm_subscriber *subscriber)
Holger Freyther89824fc2008-12-30 16:18:18 +000073{
74 if (!subscriber)
75 return 0;
76
Holger Freyther73487a22008-12-31 18:53:57 +000077 /*
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 Luebbe06513f22009-08-12 12:48:00 +020085 switch (subscriber->net->auth_policy) {
86 case GSM_AUTH_POLICY_CLOSED:
87 return subscriber->authorized;
Harald Welte (local)aa9dc192009-08-13 13:49:51 +020088 case GSM_AUTH_POLICY_TOKEN:
Harald Welte (local)ee9afe32009-08-13 20:44:23 +020089 if (subscriber->authorized)
90 return subscriber->authorized;
Harald Welte (local)aa9dc192009-08-13 13:49:51 +020091 return (subscriber->flags & GSM_SUBSCRIBER_FIRST_CONTACT);
Jan Luebbe06513f22009-08-12 12:48:00 +020092 case GSM_AUTH_POLICY_ACCEPT_ALL:
Holger Freyther89824fc2008-12-30 16:18:18 +000093 return 1;
Jan Luebbe06513f22009-08-12 12:48:00 +020094 default:
95 return 0;
96 }
Holger Freyther89824fc2008-12-30 16:18:18 +000097}
Holger Freyther07cc8d82008-12-29 06:23:46 +000098
Holger Freyther73487a22008-12-31 18:53:57 +000099static void release_loc_updating_req(struct gsm_lchan *lchan)
100{
Harald Welte179f0642008-12-31 23:59:18 +0000101 if (!lchan->loc_operation)
Holger Freyther73487a22008-12-31 18:53:57 +0000102 return;
103
Harald Welteff117a82009-05-23 05:22:08 +0000104 bsc_del_timer(&lchan->loc_operation->updating_timer);
Harald Welte2cf161b2009-06-20 22:36:41 +0200105 talloc_free(lchan->loc_operation);
Holger Freyther73487a22008-12-31 18:53:57 +0000106 lchan->loc_operation = 0;
Holger Freyther3eaa7922009-01-01 02:59:03 +0000107 put_lchan(lchan);
Holger Freyther73487a22008-12-31 18:53:57 +0000108}
109
110static void allocate_loc_updating_req(struct gsm_lchan *lchan)
111{
Holger Freyther67b4b9a2009-01-01 03:46:11 +0000112 use_lchan(lchan);
Holger Freyther73487a22008-12-31 18:53:57 +0000113 release_loc_updating_req(lchan);
114
Harald Welte470ec292009-06-26 20:25:23 +0200115 lchan->loc_operation = talloc_zero(tall_locop_ctx,
116 struct gsm_loc_updating_operation);
Holger Freyther73487a22008-12-31 18:53:57 +0000117}
Holger Freyther07cc8d82008-12-29 06:23:46 +0000118
Holger Freytherd51524f2009-06-09 08:27:07 +0000119static int gsm0408_authorize(struct gsm_lchan *lchan, struct msgb *msg)
120{
Holger Freytherd51524f2009-06-09 08:27:07 +0000121 if (authorize_subscriber(lchan->loc_operation, lchan->subscr)) {
Harald Welteee5ad162009-08-09 19:07:00 +0200122 int rc;
123
Holger Freytherd51524f2009-06-09 08:27:07 +0000124 db_subscriber_alloc_tmsi(lchan->subscr);
Holger Freytherd51524f2009-06-09 08:27:07 +0000125 release_loc_updating_req(lchan);
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200126 rc = gsm0408_loc_upd_acc(msg->lchan, lchan->subscr->tmsi);
Harald Welte648b6ce2009-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 Welteb83d9382009-12-12 21:00:48 +0100131
Harald Welteee5ad162009-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 Welteb83d9382009-12-12 21:00:48 +0100137 /* try to close channel ASAP */
138 lchan_auto_release(lchan);
Harald Welteee5ad162009-08-09 19:07:00 +0200139 return rc;
Holger Freytherd51524f2009-06-09 08:27:07 +0000140 }
141
142 return 0;
143}
144
Holger Freyther7c19f742009-06-06 13:54:35 +0000145static int gsm0408_handle_lchan_signal(unsigned int subsys, unsigned int signal,
146 void *handler_data, void *signal_data)
147{
Harald Welte4bfdfe72009-06-10 23:11:52 +0800148 struct gsm_trans *trans, *temp;
149
Holger Freyther7c19f742009-06-06 13:54:35 +0000150 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 Welte1a5c6bd2009-07-04 09:35:21 +0200157 struct gsm_lchan *lchan = (struct gsm_lchan *)signal_data;
Harald Weltec05677b2009-06-26 20:17:06 +0200158 if (!lchan)
159 return 0;
160
Holger Freyther7c19f742009-06-06 13:54:35 +0000161 release_loc_updating_req(lchan);
162
Harald Welte4bfdfe72009-06-10 23:11:52 +0800163 /* Free all transactions that are associated with the released lchan */
Harald Weltedcaf5652009-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 Welte4bfdfe72009-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 Weltedcaf5652009-07-23 18:56:43 +0200169 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +0800170 }
171
Holger Freyther7c19f742009-06-06 13:54:35 +0000172 return 0;
173}
174
Holger Freyther429e7762008-12-30 13:28:30 +0000175/* Chapter 9.2.14 : Send LOCATION UPDATING REJECT */
Harald Welte8470bf22008-12-25 23:28:35 +0000176int gsm0408_loc_upd_rej(struct gsm_lchan *lchan, u_int8_t cause)
Harald Welte52b1f982008-12-23 20:25:15 +0000177{
Harald Welte24ff6ee2009-12-22 00:41:05 +0100178 struct gsm_bts *bts = lchan->ts->trx->bts;
Harald Welte8470bf22008-12-25 23:28:35 +0000179 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000180 struct gsm48_hdr *gh;
181
Harald Welte8470bf22008-12-25 23:28:35 +0000182 msg->lchan = lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000183
184 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
185 gh->proto_discr = GSM48_PDISC_MM;
Harald Welte10b487b2008-12-27 19:53:37 +0000186 gh->msg_type = GSM48_MT_MM_LOC_UPD_REJECT;
Harald Welte52b1f982008-12-23 20:25:15 +0000187 gh->data[0] = cause;
188
Harald Welte (local)198d5ad2009-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 Welte24ff6ee2009-12-22 00:41:05 +0100193
Harald Welteffa55a42009-12-22 19:07:32 +0100194 counter_inc(bts->network->stats.loc_upd_resp.reject);
Harald Weltedb253af2008-12-30 17:56:55 +0000195
Harald Welte39e2ead2009-07-23 21:13:03 +0200196 return gsm48_sendmsg(msg, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000197}
198
199/* Chapter 9.2.13 : Send LOCATION UPDATE ACCEPT */
Harald Welte75a983f2008-12-27 21:34:06 +0000200int gsm0408_loc_upd_acc(struct gsm_lchan *lchan, u_int32_t tmsi)
Harald Welte52b1f982008-12-23 20:25:15 +0000201{
Harald Welte8470bf22008-12-25 23:28:35 +0000202 struct gsm_bts *bts = lchan->ts->trx->bts;
203 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000204 struct gsm48_hdr *gh;
205 struct gsm48_loc_area_id *lai;
206 u_int8_t *mid;
207
Harald Welte8470bf22008-12-25 23:28:35 +0000208 msg->lchan = lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000209
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 Welteafedeab2010-03-04 10:55:40 +0100215 gsm48_generate_lai(lai, bts->network->country_code,
Harald Welte52b1f982008-12-23 20:25:15 +0000216 bts->network->network_code, bts->location_area_code);
217
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +0200218 mid = msgb_put(msg, GSM48_MID_TMSI_LEN);
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +0200219 gsm48_generate_mid_from_tmsi(mid, tmsi);
Harald Welte52b1f982008-12-23 20:25:15 +0000220
221 DEBUGP(DMM, "-> LOCATION UPDATE ACCEPT\n");
222
Harald Welteffa55a42009-12-22 19:07:32 +0100223 counter_inc(bts->network->stats.loc_upd_resp.accept);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100224
Harald Welteb83d9382009-12-12 21:00:48 +0100225 return gsm48_sendmsg(msg, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000226}
227
Harald Weltebf5e8df2009-02-03 12:59:45 +0000228/* Transmit Chapter 9.2.10 Identity Request */
Harald Welte231ad4f2008-12-27 11:15:38 +0000229static 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;
Harald Weltefc977a82008-12-27 10:19:37 +0000233
Harald Welte231ad4f2008-12-27 11:15:38 +0000234 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 Welte39e2ead2009-07-23 21:13:03 +0200241 return gsm48_sendmsg(msg, NULL);
Harald Welte231ad4f2008-12-27 11:15:38 +0000242}
243
Harald Welte231ad4f2008-12-27 11:15:38 +0000244
Harald Weltebf5e8df2009-02-03 12:59:45 +0000245/* Parse Chapter 9.2.11 Identity Response */
Harald Welte231ad4f2008-12-27 11:15:38 +0000246static int mm_rx_id_resp(struct msgb *msg)
247{
248 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte75a983f2008-12-27 21:34:06 +0000249 struct gsm_lchan *lchan = msg->lchan;
Harald Welte9176bd42009-07-23 18:46:00 +0200250 struct gsm_bts *bts = lchan->ts->trx->bts;
251 struct gsm_network *net = bts->network;
Harald Welte231ad4f2008-12-27 11:15:38 +0000252 u_int8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200253 char mi_string[GSM48_MI_SIZE];
Harald Welte231ad4f2008-12-27 11:15:38 +0000254
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200255 gsm48_mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
Harald Welte61253062008-12-27 11:25:50 +0000256 DEBUGP(DMM, "IDENTITY RESPONSE: mi_type=0x%02x MI(%s)\n",
Harald Welte231ad4f2008-12-27 11:15:38 +0000257 mi_type, mi_string);
258
Harald Welte7659de12009-12-13 12:39:18 +0100259 dispatch_signal(SS_SUBSCR, S_SUBSCR_IDENTITY, gh->data);
260
Harald Welte75a983f2008-12-27 21:34:06 +0000261 switch (mi_type) {
262 case GSM_MI_TYPE_IMSI:
Jan Luebbe370b41d2009-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)aa9dc192009-08-13 13:49:51 +0200266 if (!lchan->subscr)
267 lchan->subscr = db_create_subscriber(net, mi_string);
Jan Luebbeb0dfc312009-08-12 10:12:52 +0200268 }
Holger Freyther73487a22008-12-31 18:53:57 +0000269 if (lchan->loc_operation)
270 lchan->loc_operation->waiting_for_imsi = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000271 break;
272 case GSM_MI_TYPE_IMEI:
Harald Welte255539c2008-12-28 02:26:27 +0000273 case GSM_MI_TYPE_IMEISV:
Harald Welte75a983f2008-12-27 21:34:06 +0000274 /* update subscribe <-> IMEI mapping */
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200275 if (lchan->subscr) {
Harald Welte75a983f2008-12-27 21:34:06 +0000276 db_subscriber_assoc_imei(lchan->subscr, mi_string);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200277 db_sync_equipment(&lchan->subscr->equipment);
278 }
Holger Freyther73487a22008-12-31 18:53:57 +0000279 if (lchan->loc_operation)
280 lchan->loc_operation->waiting_for_imei = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000281 break;
282 }
Holger Freyther73487a22008-12-31 18:53:57 +0000283
284 /* Check if we can let the mobile station enter */
Holger Freytherd51524f2009-06-09 08:27:07 +0000285 return gsm0408_authorize(lchan, msg);
Harald Welte231ad4f2008-12-27 11:15:38 +0000286}
287
Harald Welte255539c2008-12-28 02:26:27 +0000288
289static void loc_upd_rej_cb(void *data)
290{
291 struct gsm_lchan *lchan = data;
Harald Welte1085c092009-11-18 20:33:19 +0100292 struct gsm_bts *bts = lchan->ts->trx->bts;
Harald Welte255539c2008-12-28 02:26:27 +0000293
Holger Freyther73487a22008-12-31 18:53:57 +0000294 release_loc_updating_req(lchan);
Harald Welte1085c092009-11-18 20:33:19 +0100295 gsm0408_loc_upd_rej(lchan, bts->network->reject_cause);
Holger Freyther67b4b9a2009-01-01 03:46:11 +0000296 lchan_auto_release(lchan);
Harald Welte255539c2008-12-28 02:26:27 +0000297}
298
Holger Freytherb7193e42008-12-29 17:44:08 +0000299static void schedule_reject(struct gsm_lchan *lchan)
300{
Holger Freyther73487a22008-12-31 18:53:57 +0000301 lchan->loc_operation->updating_timer.cb = loc_upd_rej_cb;
302 lchan->loc_operation->updating_timer.data = lchan;
Harald Welteff117a82009-05-23 05:22:08 +0000303 bsc_schedule_timer(&lchan->loc_operation->updating_timer, 5, 0);
Holger Freytherb7193e42008-12-29 17:44:08 +0000304}
305
Harald Welte2a139372009-02-22 21:14:55 +0000306static 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 Weltebf5e8df2009-02-03 12:59:45 +0000320/* Chapter 9.2.15: Receive Location Updating Request */
Harald Welte231ad4f2008-12-27 11:15:38 +0000321static int mm_rx_loc_upd_req(struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000322{
Harald Welte8470bf22008-12-25 23:28:35 +0000323 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000324 struct gsm48_loc_upd_req *lu;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800325 struct gsm_subscriber *subscr = NULL;
Harald Welte255539c2008-12-28 02:26:27 +0000326 struct gsm_lchan *lchan = msg->lchan;
Harald Welte9176bd42009-07-23 18:46:00 +0200327 struct gsm_bts *bts = lchan->ts->trx->bts;
Harald Welte8470bf22008-12-25 23:28:35 +0000328 u_int8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200329 char mi_string[GSM48_MI_SIZE];
Harald Welte231ad4f2008-12-27 11:15:38 +0000330 int rc;
Harald Welte52b1f982008-12-23 20:25:15 +0000331
Harald Welte8470bf22008-12-25 23:28:35 +0000332 lu = (struct gsm48_loc_upd_req *) gh->data;
333
334 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
Harald Welte52b1f982008-12-23 20:25:15 +0000335
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200336 gsm48_mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
Harald Weltefc977a82008-12-27 10:19:37 +0000337
Harald Weltea0368542009-06-27 02:58:43 +0200338 DEBUGPC(DMM, "mi_type=0x%02x MI(%s) type=%s ", mi_type, mi_string,
Harald Welte2a139372009-02-22 21:14:55 +0000339 lupd_name(lu->type));
Holger Freyther73487a22008-12-31 18:53:57 +0000340
Harald Welte7659de12009-12-13 12:39:18 +0100341 dispatch_signal(SS_SUBSCR, S_SUBSCR_IDENTITY, &lu->mi_len);
342
Harald Welte24ff6ee2009-12-22 00:41:05 +0100343 switch (lu->type) {
344 case GSM48_LUPD_NORMAL:
Harald Welteffa55a42009-12-22 19:07:32 +0100345 counter_inc(bts->network->stats.loc_upd_type.normal);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100346 break;
347 case GSM48_LUPD_IMSI_ATT:
Harald Welteffa55a42009-12-22 19:07:32 +0100348 counter_inc(bts->network->stats.loc_upd_type.attach);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100349 break;
350 case GSM48_LUPD_PERIODIC:
Harald Welteffa55a42009-12-22 19:07:32 +0100351 counter_inc(bts->network->stats.loc_upd_type.periodic);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100352 break;
353 }
354
Holger Freythereaf04692009-06-06 13:54:44 +0000355 /*
356 * Pseudo Spoof detection: Just drop a second/concurrent
357 * location updating request.
358 */
359 if (lchan->loc_operation) {
Harald Weltea0368542009-06-27 02:58:43 +0200360 DEBUGPC(DMM, "ignoring request due an existing one: %p.\n",
Holger Freythereaf04692009-06-06 13:54:44 +0000361 lchan->loc_operation);
362 gsm0408_loc_upd_rej(lchan, GSM48_REJECT_PROTOCOL_ERROR);
363 return 0;
364 }
365
Holger Freyther73487a22008-12-31 18:53:57 +0000366 allocate_loc_updating_req(lchan);
367
Harald Welte52b1f982008-12-23 20:25:15 +0000368 switch (mi_type) {
369 case GSM_MI_TYPE_IMSI:
Harald Weltea0368542009-06-27 02:58:43 +0200370 DEBUGPC(DMM, "\n");
Harald Welte231ad4f2008-12-27 11:15:38 +0000371 /* we always want the IMEI, too */
Harald Welte015b9ad2009-02-28 18:22:03 +0000372 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMEI);
Holger Freyther73487a22008-12-31 18:53:57 +0000373 lchan->loc_operation->waiting_for_imei = 1;
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000374
Jan Luebbe370b41d2009-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 Welte4b634542008-12-27 01:55:51 +0000380 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000381 case GSM_MI_TYPE_TMSI:
Harald Weltea0368542009-06-27 02:58:43 +0200382 DEBUGPC(DMM, "\n");
Harald Welte231ad4f2008-12-27 11:15:38 +0000383 /* we always want the IMEI, too */
Harald Welte015b9ad2009-02-28 18:22:03 +0000384 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMEI);
Holger Freyther73487a22008-12-31 18:53:57 +0000385 lchan->loc_operation->waiting_for_imei = 1;
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000386
Harald Welte52b1f982008-12-23 20:25:15 +0000387 /* look up the subscriber based on TMSI, request IMSI if it fails */
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200388 subscr = subscr_get_by_tmsi(bts->network,
389 tmsi_from_string(mi_string));
Harald Welte52b1f982008-12-23 20:25:15 +0000390 if (!subscr) {
Harald Welte231ad4f2008-12-27 11:15:38 +0000391 /* send IDENTITY REQUEST message to get IMSI */
Harald Welte255539c2008-12-28 02:26:27 +0000392 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMSI);
Holger Freyther73487a22008-12-31 18:53:57 +0000393 lchan->loc_operation->waiting_for_imsi = 1;
Harald Welte52b1f982008-12-23 20:25:15 +0000394 }
395 break;
396 case GSM_MI_TYPE_IMEI:
397 case GSM_MI_TYPE_IMEISV:
398 /* no sim card... FIXME: what to do ? */
Harald Weltea0368542009-06-27 02:58:43 +0200399 DEBUGPC(DMM, "unimplemented mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000400 break;
401 default:
Harald Weltea0368542009-06-27 02:58:43 +0200402 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000403 break;
404 }
405
Harald Welte24516ea2009-07-04 10:18:00 +0200406 /* schedule the reject timer */
407 schedule_reject(lchan);
408
Harald Welte4bfdfe72009-06-10 23:11:52 +0800409 if (!subscr) {
Harald Weltea0368542009-06-27 02:58:43 +0200410 DEBUGPC(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte4bfdfe72009-06-10 23:11:52 +0800411 /* FIXME: request id? close channel? */
412 return -EINVAL;
413 }
414
Harald Welte255539c2008-12-28 02:26:27 +0000415 lchan->subscr = subscr;
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200416 lchan->subscr->equipment.classmark1 = lu->classmark1;
Harald Welte255539c2008-12-28 02:26:27 +0000417
Harald Welte24516ea2009-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. */
Holger Freytherd51524f2009-06-09 08:27:07 +0000420 return gsm0408_authorize(lchan, msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000421}
422
Harald Welte4bfdfe72009-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 Weltedb253af2008-12-30 17:56:55 +0000430/* 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;
Harald Weltedb253af2008-12-30 17:56:55 +0000436 u_int8_t *ptr8;
Daniel Willmanneea93372009-08-13 03:42:07 +0200437 int name_len, name_pad;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800438#if 0
439 time_t cur_t;
440 struct tm* cur_time;
441 int tz15min;
442#endif
Harald Weltedb253af2008-12-30 17:56:55 +0000443
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 Willmanneea93372009-08-13 03:42:07 +0200451#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000452 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++)
Harald Welte179f0642008-12-31 23:59:18 +0000461 ptr16[i] = htons(net->name_long[i]);
Harald Weltedb253af2008-12-30 17:56:55 +0000462
463 /* FIXME: Use Cell Broadcast, not UCS-2, since
464 * UCS-2 is only supported by later revisions of the spec */
Daniel Willmanneea93372009-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 Weltedb253af2008-12-30 17:56:55 +0000479 }
480
481 if (net->name_short) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200482#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000483 name_len = strlen(net->name_short);
484 /* 10.5.3.5a */
485 ptr8 = (u_int8_t *) msgb_put(msg, 3);
Harald Welte7543eb72009-07-19 17:51:36 +0200486 ptr8[0] = GSM48_IE_NAME_SHORT;
Harald Weltedb253af2008-12-30 17:56:55 +0000487 ptr8[1] = name_len*2 + 1;
488 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
489
Harald Weltee872cb12009-01-01 00:33:37 +0000490 ptr16 = (u_int16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000491 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000492 ptr16[i] = htons(net->name_short[i]);
Daniel Willmanneea93372009-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 Weltedb253af2008-12-30 17:56:55 +0000507 }
508
509#if 0
510 /* Section 10.5.3.9 */
511 cur_t = time(NULL);
Harald Welte4bfdfe72009-06-10 23:11:52 +0800512 cur_time = gmtime(&cur_t);
Harald Weltedb253af2008-12-30 17:56:55 +0000513 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 Welte4bfdfe72009-06-10 23:11:52 +0800523 ptr8[7] = to_bcd8(tz15min);
Harald Weltedb253af2008-12-30 17:56:55 +0000524 if (tz15min < 0)
Harald Welte4bfdfe72009-06-10 23:11:52 +0800525 ptr8[7] |= 0x80;
Harald Weltedb253af2008-12-30 17:56:55 +0000526#endif
527
Daniel Willmanneea93372009-08-13 03:42:07 +0200528 DEBUGP(DMM, "-> MM INFO\n");
529
Harald Welte39e2ead2009-07-23 21:13:03 +0200530 return gsm48_sendmsg(msg, NULL);
Harald Weltedb253af2008-12-30 17:56:55 +0000531}
532
Harald Welte7984d5c2009-08-12 22:56:50 +0200533/* Section 9.2.2 */
Sylvain Munautbd55a6d2009-12-24 00:23:46 +0100534int gsm48_tx_mm_auth_req(struct gsm_lchan *lchan, u_int8_t *rand, int key_seq)
Harald Welte7984d5c2009-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 Munaut849f5542009-09-27 11:10:17 +0200538 struct gsm48_auth_req *ar = (struct gsm48_auth_req *) msgb_put(msg, sizeof(*ar));
Harald Welte7984d5c2009-08-12 22:56:50 +0200539
Sylvain Munaut8608e7c2009-12-24 00:24:29 +0100540 DEBUGP(DMM, "-> AUTH REQ (rand = %s)\n", hexdump(rand, 16));
Harald Welte7984d5c2009-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 Munautbd55a6d2009-12-24 00:23:46 +0100546 ar->key_seq = key_seq;
Sylvain Munaut849f5542009-09-27 11:10:17 +0200547
Harald Welte7984d5c2009-08-12 22:56:50 +0200548 /* 16 bytes RAND parameters */
Harald Welte7984d5c2009-08-12 22:56:50 +0200549 if (rand)
Sylvain Munaut849f5542009-09-27 11:10:17 +0200550 memcpy(ar->rand, rand, 16);
Harald Welte7984d5c2009-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 Welte4b634542008-12-27 01:55:51 +0000562static int gsm48_tx_mm_serv_ack(struct gsm_lchan *lchan)
563{
Harald Welte4b634542008-12-27 01:55:51 +0000564 DEBUGP(DMM, "-> CM SERVICE ACK\n");
Harald Welte65e74cc2008-12-29 01:55:35 +0000565 return gsm48_tx_simple(lchan, GSM48_PDISC_MM, GSM48_MT_MM_CM_SERV_ACC);
Harald Welte4b634542008-12-27 01:55:51 +0000566}
Harald Welteba4cf162009-01-10 01:49:35 +0000567
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 Welte39e2ead2009-07-23 21:13:03 +0200585 return gsm48_sendmsg(msg, NULL);
Harald Welteba4cf162009-01-10 01:49:35 +0000586}
587
Harald Welte4ed0e922009-01-10 03:17:30 +0000588/*
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 */
Harald Welte4b634542008-12-27 01:55:51 +0000597static int gsm48_rx_mm_serv_req(struct msgb *msg)
598{
Harald Welteba4cf162009-01-10 01:49:35 +0000599 u_int8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200600 char mi_string[GSM48_MI_SIZE];
Harald Welte4b634542008-12-27 01:55:51 +0000601
Harald Welte9176bd42009-07-23 18:46:00 +0200602 struct gsm_bts *bts = msg->lchan->ts->trx->bts;
Harald Welteba4cf162009-01-10 01:49:35 +0000603 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;
Harald Weltec9e02182009-05-01 19:07:53 +0000607 /* 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);
Harald Welteba4cf162009-01-10 01:49:35 +0000612
Harald Weltec9e02182009-05-01 19:07:53 +0000613 DEBUGP(DMM, "<- CM SERVICE REQUEST ");
Harald Welteba4cf162009-01-10 01:49:35 +0000614 if (msg->data_len < sizeof(struct gsm48_service_request*)) {
Harald Weltec9e02182009-05-01 19:07:53 +0000615 DEBUGPC(DMM, "wrong sized message\n");
Harald Welteba4cf162009-01-10 01:49:35 +0000616 return gsm48_tx_mm_serv_rej(msg->lchan,
617 GSM48_REJECT_INCORRECT_MESSAGE);
618 }
619
620 if (msg->data_len < req->mi_len + 6) {
Harald Weltec9e02182009-05-01 19:07:53 +0000621 DEBUGPC(DMM, "does not fit in packet\n");
Harald Welteba4cf162009-01-10 01:49:35 +0000622 return gsm48_tx_mm_serv_rej(msg->lchan,
623 GSM48_REJECT_INCORRECT_MESSAGE);
624 }
625
Harald Weltec9e02182009-05-01 19:07:53 +0000626 mi_type = mi[0] & GSM_MI_TYPE_MASK;
Harald Welteba4cf162009-01-10 01:49:35 +0000627 if (mi_type != GSM_MI_TYPE_TMSI) {
Harald Weltec9e02182009-05-01 19:07:53 +0000628 DEBUGPC(DMM, "mi_type is not TMSI: %d\n", mi_type);
Harald Welteba4cf162009-01-10 01:49:35 +0000629 return gsm48_tx_mm_serv_rej(msg->lchan,
630 GSM48_REJECT_INCORRECT_MESSAGE);
631 }
632
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200633 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
Harald Weltec9e02182009-05-01 19:07:53 +0000634 DEBUGPC(DMM, "serv_type=0x%02x mi_type=0x%02x M(%s)\n",
Harald Welte4ed0e922009-01-10 03:17:30 +0000635 req->cm_service_type, mi_type, mi_string);
Harald Weltebcae43f2008-12-27 21:45:37 +0000636
Harald Welte7659de12009-12-13 12:39:18 +0100637 dispatch_signal(SS_SUBSCR, S_SUBSCR_IDENTITY, (classmark2 + classmark2_len));
638
Harald Welte3ac7f102009-08-10 10:12:45 +0200639 if (is_siemens_bts(bts))
640 send_siemens_mrpci(msg->lchan, classmark2-1);
641
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200642 subscr = subscr_get_by_tmsi(bts->network,
643 tmsi_from_string(mi_string));
Holger Freythereb443982009-06-04 13:58:42 +0000644
Harald Welte2a139372009-02-22 21:14:55 +0000645 /* FIXME: if we don't know the TMSI, inquire abit IMSI and allocate new TMSI */
Harald Welte4ed0e922009-01-10 03:17:30 +0000646 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 Munautea3f6742009-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 Welte9bb7c702009-01-10 03:21:41 +0000655 DEBUGP(DMM, "<- CM Channel already owned by someone else?\n");
656 subscr_put(subscr);
657 }
658
Harald Weltec2e302d2009-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 Weltef7c43522009-06-09 20:24:21 +0000662
Harald Welte4b634542008-12-27 01:55:51 +0000663 return gsm48_tx_mm_serv_ack(msg->lchan);
664}
665
Harald Welte2a139372009-02-22 21:14:55 +0000666static int gsm48_rx_mm_imsi_detach_ind(struct msgb *msg)
667{
Harald Welte9176bd42009-07-23 18:46:00 +0200668 struct gsm_bts *bts = msg->lchan->ts->trx->bts;
Harald Welte2a139372009-02-22 21:14:55 +0000669 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 Freytherd1862d72009-08-19 07:54:59 +0200673 char mi_string[GSM48_MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +0800674 struct gsm_subscriber *subscr = NULL;
Harald Welte2a139372009-02-22 21:14:55 +0000675
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200676 gsm48_mi_to_string(mi_string, sizeof(mi_string), idi->mi, idi->mi_len);
Harald Welte2a139372009-02-22 21:14:55 +0000677 DEBUGP(DMM, "IMSI DETACH INDICATION: mi_type=0x%02x MI(%s): ",
678 mi_type, mi_string);
679
Harald Welteffa55a42009-12-22 19:07:32 +0100680 counter_inc(bts->network->stats.loc_upd_type.detach);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100681
Harald Welte2a139372009-02-22 21:14:55 +0000682 switch (mi_type) {
683 case GSM_MI_TYPE_TMSI:
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200684 subscr = subscr_get_by_tmsi(bts->network,
685 tmsi_from_string(mi_string));
Harald Welte2a139372009-02-22 21:14:55 +0000686 break;
687 case GSM_MI_TYPE_IMSI:
Harald Welte9176bd42009-07-23 18:46:00 +0200688 subscr = subscr_get_by_imsi(bts->network, mi_string);
Harald Welte2a139372009-02-22 21:14:55 +0000689 break;
690 case GSM_MI_TYPE_IMEI:
691 case GSM_MI_TYPE_IMEISV:
692 /* no sim card... FIXME: what to do ? */
Holger Freyther79f4ae62009-06-02 03:25:04 +0000693 DEBUGPC(DMM, "unimplemented mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +0000694 break;
695 default:
Holger Freyther79f4ae62009-06-02 03:25:04 +0000696 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +0000697 break;
698 }
699
Holger Freyther4a49e772009-04-12 05:37:29 +0000700 if (subscr) {
701 subscr_update(subscr, msg->trx->bts,
702 GSM_SUBSCRIBER_UPDATE_DETACHED);
Harald Welte2e6d4682009-12-24 14:50:24 +0100703 DEBUGP(DMM, "Subscriber: %s\n", subscr_name(subscr));
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200704
705 subscr->equipment.classmark1 = idi->classmark1;
706 db_sync_equipment(&subscr->equipment);
707
Holger Freytherc21cfbc2009-06-02 02:54:57 +0000708 subscr_put(subscr);
Holger Freyther4a49e772009-04-12 05:37:29 +0000709 } else
Harald Welte2a139372009-02-22 21:14:55 +0000710 DEBUGP(DMM, "Unknown Subscriber ?!?\n");
711
Harald Welte31981a02009-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 Welteb83d9382009-12-12 21:00:48 +0100715 /* subscriber is detached: should we release lchan? */
Harald Weltef7c28b02009-12-21 13:30:17 +0100716 lchan_auto_release(msg->lchan);
Harald Welteb83d9382009-12-12 21:00:48 +0100717
Harald Welte2a139372009-02-22 21:14:55 +0000718 return 0;
719}
720
Harald Welted2a7f5a2009-06-05 20:08:20 +0000721static 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
Harald Weltebf5e8df2009-02-03 12:59:45 +0000730/* Receive a GSM 04.08 Mobility Management (MM) message */
Harald Welte52b1f982008-12-23 20:25:15 +0000731static int gsm0408_rcv_mm(struct msgb *msg)
732{
733 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +0800734 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000735
736 switch (gh->msg_type & 0xbf) {
737 case GSM48_MT_MM_LOC_UPD_REQUEST:
Harald Weltea0368542009-06-27 02:58:43 +0200738 DEBUGP(DMM, "LOCATION UPDATING REQUEST: ");
Harald Welte231ad4f2008-12-27 11:15:38 +0000739 rc = mm_rx_loc_upd_req(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000740 break;
741 case GSM48_MT_MM_ID_RESP:
Harald Welte231ad4f2008-12-27 11:15:38 +0000742 rc = mm_rx_id_resp(msg);
743 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000744 case GSM48_MT_MM_CM_SERV_REQ:
Harald Welte4b634542008-12-27 01:55:51 +0000745 rc = gsm48_rx_mm_serv_req(msg);
746 break;
Harald Welte231ad4f2008-12-27 11:15:38 +0000747 case GSM48_MT_MM_STATUS:
Harald Welted2a7f5a2009-06-05 20:08:20 +0000748 rc = gsm48_rx_mm_status(msg);
Harald Welte231ad4f2008-12-27 11:15:38 +0000749 break;
Harald Welte231ad4f2008-12-27 11:15:38 +0000750 case GSM48_MT_MM_TMSI_REALL_COMPL:
Harald Welte69b2af22009-01-06 19:47:00 +0000751 DEBUGP(DMM, "TMSI Reallocation Completed. Subscriber: %s\n",
752 msg->lchan->subscr ?
Harald Welte2e6d4682009-12-24 14:50:24 +0100753 subscr_name(msg->lchan->subscr) :
Harald Welte69b2af22009-01-06 19:47:00 +0000754 "unknown subscriber");
755 break;
Harald Welte231ad4f2008-12-27 11:15:38 +0000756 case GSM48_MT_MM_IMSI_DETACH_IND:
Harald Welte2a139372009-02-22 21:14:55 +0000757 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");
Harald Welte52b1f982008-12-23 20:25:15 +0000764 break;
765 default:
Harald Welte5d24ba12009-12-24 12:13:17 +0100766 LOGP(DMM, LOGL_NOTICE, "Unknown GSM 04.08 MM msg type 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +0000767 gh->msg_type);
768 break;
769 }
770
771 return rc;
772}
Harald Weltebf5e8df2009-02-03 12:59:45 +0000773
Harald Welte2d35ae62009-02-06 12:02:13 +0000774/* Receive a PAGING RESPONSE message from the MS */
Holger Hans Peter Freytherca0fcbe2009-10-23 13:48:54 +0200775static int gsm48_rx_rr_pag_resp(struct msgb *msg)
Harald Welte2d35ae62009-02-06 12:02:13 +0000776{
Harald Welte9176bd42009-07-23 18:46:00 +0200777 struct gsm_bts *bts = msg->lchan->ts->trx->bts;
Harald Welte2d35ae62009-02-06 12:02:13 +0000778 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte61548982009-02-22 21:26:29 +0000779 u_int8_t *classmark2_lv = gh->data + 1;
Holger Hans Peter Freyther3ee5d3e2009-08-21 05:18:21 +0200780 u_int8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200781 char mi_string[GSM48_MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +0800782 struct gsm_subscriber *subscr = NULL;
Harald Welte2d35ae62009-02-06 12:02:13 +0000783 int rc = 0;
784
Holger Hans Peter Freyther3ee5d3e2009-08-21 05:18:21 +0200785 gsm48_paging_extract_mi(msg, mi_string, &mi_type);
Harald Welte2d35ae62009-02-06 12:02:13 +0000786 DEBUGP(DRR, "PAGING RESPONSE: mi_type=0x%02x MI(%s)\n",
787 mi_type, mi_string);
Harald Welte3ac7f102009-08-10 10:12:45 +0200788
Harald Weltefe18d8f2009-02-22 21:14:24 +0000789 switch (mi_type) {
790 case GSM_MI_TYPE_TMSI:
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200791 subscr = subscr_get_by_tmsi(bts->network,
792 tmsi_from_string(mi_string));
Harald Weltefe18d8f2009-02-22 21:14:24 +0000793 break;
794 case GSM_MI_TYPE_IMSI:
Harald Welte9176bd42009-07-23 18:46:00 +0200795 subscr = subscr_get_by_imsi(bts->network, mi_string);
Harald Weltefe18d8f2009-02-22 21:14:24 +0000796 break;
797 }
Harald Welte2d35ae62009-02-06 12:02:13 +0000798
799 if (!subscr) {
800 DEBUGP(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte09e38af2009-02-16 22:52:23 +0000801 /* FIXME: request id? close channel? */
Harald Welte2d35ae62009-02-06 12:02:13 +0000802 return -EINVAL;
803 }
804 DEBUGP(DRR, "<- Channel was requested by %s\n",
Harald Welte76042182009-08-08 16:03:15 +0200805 subscr->name && strlen(subscr->name) ? subscr->name : subscr->imsi);
Holger Freyther053e09d2009-02-14 22:51:06 +0000806
Harald Weltec2e302d2009-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 Weltef7c43522009-06-09 20:24:21 +0000810
Holger Hans Peter Freyther3ee5d3e2009-08-21 05:18:21 +0200811 rc = gsm48_handle_paging_resp(msg, subscr);
Harald Welte2d35ae62009-02-06 12:02:13 +0000812 return rc;
813}
814
Harald Weltef7c43522009-06-09 20:24:21 +0000815static 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 Weltec2e302d2009-07-05 14:08:13 +0200850 subscr->equipment.classmark2_len = cm2_len;
851 memcpy(subscr->equipment.classmark2, cm2, cm2_len);
Harald Weltef7c43522009-06-09 20:24:21 +0000852 if (cm3) {
Harald Weltec2e302d2009-07-05 14:08:13 +0200853 subscr->equipment.classmark3_len = cm3_len;
854 memcpy(subscr->equipment.classmark3, cm3, cm3_len);
Harald Weltef7c43522009-06-09 20:24:21 +0000855 }
Harald Weltec2e302d2009-07-05 14:08:13 +0200856 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +0000857 }
858
Harald Weltef7c43522009-06-09 20:24:21 +0000859 return 0;
860}
861
Harald Weltecf5b3592009-05-01 18:28:42 +0000862static 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
Harald Weltef7c43522009-06-09 20:24:21 +0000872static int gsm48_rx_rr_meas_rep(struct msgb *msg)
873{
Harald Welted12b0fd2009-12-15 21:36:05 +0100874 struct gsm_meas_rep *meas_rep = lchan_next_meas_rep(msg->lchan);
Harald Weltef7c43522009-06-09 20:24:21 +0000875
Harald Welte3c7dc6e2009-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 Welted12b0fd2009-12-15 21:36:05 +0100880 gsm48_parse_meas_rep(meas_rep, msg);
Harald Weltef7c43522009-06-09 20:24:21 +0000881
882 return 0;
883}
884
Harald Welte (local)6eef5642009-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)026531e2009-08-16 10:40:10 +0200899 return db_apdu_blob_store(msg->lchan->subscr, apdu_id_flags, apdu_len, apdu_data);
Harald Welte (local)6eef5642009-08-15 23:32:44 +0200900}
901
Harald Welted011e8b2009-11-29 22:45:52 +0100902/* Chapter 9.1.16 Handover complete */
Harald Welte1e191c52009-12-14 17:51:15 +0100903static int gsm48_rx_rr_ho_compl(struct msgb *msg)
Harald Welted011e8b2009-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 Welte1e191c52009-12-14 17:51:15 +0100917static int gsm48_rx_rr_ho_fail(struct msgb *msg)
Harald Welted011e8b2009-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)6eef5642009-08-15 23:32:44 +0200929
Harald Weltebf5e8df2009-02-03 12:59:45 +0000930/* Receive a GSM 04.08 Radio Resource (RR) message */
Harald Welte52b1f982008-12-23 20:25:15 +0000931static int gsm0408_rcv_rr(struct msgb *msg)
932{
933 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +0000934 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000935
936 switch (gh->msg_type) {
937 case GSM48_MT_RR_CLSM_CHG:
Harald Weltef7c43522009-06-09 20:24:21 +0000938 rc = gsm48_rx_rr_classmark(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000939 break;
Harald Weltefc977a82008-12-27 10:19:37 +0000940 case GSM48_MT_RR_GPRS_SUSP_REQ:
941 DEBUGP(DRR, "GRPS SUSPEND REQUEST\n");
942 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000943 case GSM48_MT_RR_PAG_RESP:
Holger Hans Peter Freytherca0fcbe2009-10-23 13:48:54 +0200944 rc = gsm48_rx_rr_pag_resp(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +0000945 break;
Harald Welte7ccf7782009-02-17 01:43:01 +0000946 case GSM48_MT_RR_CHAN_MODE_MODIF_ACK:
Holger Hans Peter Freytherf520e642009-10-22 15:23:11 +0200947 rc = gsm48_rx_rr_modif_ack(msg);
Harald Welte7ccf7782009-02-17 01:43:01 +0000948 break;
Harald Weltecf5b3592009-05-01 18:28:42 +0000949 case GSM48_MT_RR_STATUS:
950 rc = gsm48_rx_rr_status(msg);
951 break;
Harald Weltef7c43522009-06-09 20:24:21 +0000952 case GSM48_MT_RR_MEAS_REP:
953 rc = gsm48_rx_rr_meas_rep(msg);
954 break;
Harald Welte (local)6eef5642009-08-15 23:32:44 +0200955 case GSM48_MT_RR_APP_INFO:
956 rc = gsm48_rx_rr_app_info(msg);
957 break;
Harald Welte08d91a52009-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 Welteccd5a882009-11-29 20:02:20 +0100962 case GSM48_MT_RR_HANDO_COMPL:
Harald Welted011e8b2009-11-29 22:45:52 +0100963 rc = gsm48_rx_rr_ho_compl(msg);
Harald Welteccd5a882009-11-29 20:02:20 +0100964 break;
965 case GSM48_MT_RR_HANDO_FAIL:
Harald Welted011e8b2009-11-29 22:45:52 +0100966 rc = gsm48_rx_rr_ho_fail(msg);
Harald Welteccd5a882009-11-29 20:02:20 +0100967 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000968 default:
Harald Welte5d24ba12009-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 Welte52b1f982008-12-23 20:25:15 +0000971 break;
972 }
973
Harald Welte2d35ae62009-02-06 12:02:13 +0000974 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +0000975}
976
Harald Welte (local)6eef5642009-08-15 23:32:44 +0200977int gsm48_send_rr_app_info(struct gsm_lchan *lchan, u_int8_t apdu_id,
Holger Hans Peter Freyther8b77a342009-10-22 15:42:19 +0200978 u_int8_t apdu_len, const u_int8_t *apdu)
Harald Welte (local)6eef5642009-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 Welte4bc90a12008-12-27 16:32:52 +0000998/* Call Control */
999
Harald Welte7584aea2009-02-11 11:44:12 +00001000/* 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 Welte4bfdfe72009-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 Weltee95daf12010-03-25 12:13:02 +08001011 gsm48_cc_state_name(trans->cc.state),
1012 gsm48_cc_state_name(state));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001013
Harald Weltedcaf5652009-07-23 18:56:43 +02001014 trans->cc.state = state;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001015}
1016
1017static int gsm48_cc_tx_status(struct gsm_trans *trans, void *arg)
Harald Welte4bc90a12008-12-27 16:32:52 +00001018{
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 Welte4bc90a12008-12-27 16:32:52 +00001023 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 Welte39e2ead2009-07-23 21:13:03 +02001033 return gsm48_sendmsg(msg, trans);
Harald Welte4bc90a12008-12-27 16:32:52 +00001034}
1035
Harald Welte6f4b7532008-12-29 00:39:37 +00001036static int gsm48_tx_simple(struct gsm_lchan *lchan,
1037 u_int8_t pdisc, u_int8_t msg_type)
Harald Welte4bc90a12008-12-27 16:32:52 +00001038{
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
Harald Welte6f4b7532008-12-29 00:39:37 +00001044 gh->proto_discr = pdisc;
Harald Welte4bc90a12008-12-27 16:32:52 +00001045 gh->msg_type = msg_type;
1046
Harald Welte39e2ead2009-07-23 21:13:03 +02001047 return gsm48_sendmsg(msg, NULL);
Harald Welte4bc90a12008-12-27 16:32:52 +00001048}
1049
Harald Welte4bfdfe72009-06-10 23:11:52 +08001050static void gsm48_stop_cc_timer(struct gsm_trans *trans)
1051{
Harald Weltedcaf5652009-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 Welte4bfdfe72009-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 Welte6f5aee02009-07-23 21:21:14 +02001066 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-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 Welte966636f2009-06-26 19:39:35 +02001084 msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
Harald Welte4bfdfe72009-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 Welte92f70c52009-06-12 01:54:08 +08001098 memset(&rel, 0, sizeof(rel));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001099 rel.callref = callref;
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001100 mncc_set_cause(&rel, location, value);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001101 return mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
1102}
1103
Harald Weltedcaf5652009-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 Welte4bfdfe72009-06-10 23:11:52 +08001107{
Harald Welte4bfdfe72009-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 Welte596fed42009-07-23 19:06:52 +02001113 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001114 GSM48_CAUSE_LOC_PRN_S_LU,
1115 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001116 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001117 if (trans->cc.state != GSM_CSTATE_NULL)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001118 new_cc_state(trans, GSM_CSTATE_NULL);
Harald Weltedcaf5652009-07-23 18:56:43 +02001119 if (trans->lchan)
1120 trau_mux_unmap(&trans->lchan->ts->e1_link, trans->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001121}
1122
1123static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg);
1124
Harald Welte09e38af2009-02-16 22:52:23 +00001125/* call-back from paging the B-end of the connection */
1126static int setup_trig_pag_evt(unsigned int hooknum, unsigned int event,
Harald Welte7ccf7782009-02-17 01:43:01 +00001127 struct msgb *msg, void *_lchan, void *param)
Harald Welte09e38af2009-02-16 22:52:23 +00001128{
Harald Welte7ccf7782009-02-17 01:43:01 +00001129 struct gsm_lchan *lchan = _lchan;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001130 struct gsm_subscriber *subscr = param;
1131 struct gsm_trans *transt, *tmp;
1132 struct gsm_network *net;
Harald Weltec05677b2009-06-26 20:17:06 +02001133
Harald Welte09e38af2009-02-16 22:52:23 +00001134 if (hooknum != GSM_HOOK_RR_PAGING)
1135 return -EINVAL;
Harald Welte4bfdfe72009-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 Welte5a065df2009-02-22 21:13:18 +00001143 }
Harald Welte7584aea2009-02-11 11:44:12 +00001144
Harald Welte4bfdfe72009-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 Weltedcaf5652009-07-23 18:56:43 +02001161 gsm48_cc_tx_setup(transt, &transt->cc.msg);
Harald Welte4bfdfe72009-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 Welte596fed42009-07-23 19:06:52 +02001167 mncc_release_ind(transt->subscr->net, transt,
1168 transt->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001169 GSM48_CAUSE_LOC_PRN_S_LU,
1170 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001171 transt->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001172 trans_free(transt);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001173 break;
1174 }
1175 }
Harald Welte09e38af2009-02-16 22:52:23 +00001176 return 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00001177}
Harald Welte7584aea2009-02-11 11:44:12 +00001178
Harald Welteda7ab742009-12-19 22:23:05 +01001179static int tch_recv_mncc(struct gsm_network *net, u_int32_t callref, int enable);
1180
Harald Welte805f6442009-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 Welte805f6442009-07-28 18:25:29 +02001186 int rc;
Harald Welteda7ab742009-12-19 22:23:05 +01001187 struct gsm_network *net;
1188 struct gsm_trans *trans;
Harald Welte805f6442009-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 Welte805f6442009-07-28 18:25:29 +02001197 switch (signal) {
Holger Hans Peter Freyther231163d2009-11-18 21:06:12 +01001198 case S_ABISIP_CRCX_ACK:
Harald Welteda7ab742009-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 Welte805f6442009-07-28 18:25:29 +02001208 break;
Harald Welte805f6442009-07-28 18:25:29 +02001209 }
1210
1211 return 0;
Harald Welte805f6442009-07-28 18:25:29 +02001212}
1213
Harald Welte49f48b82009-02-17 15:29:33 +00001214/* map two ipaccess RTP streams onto each other */
Harald Welte11fa29c2009-02-19 17:24:39 +00001215static int tch_map(struct gsm_lchan *lchan, struct gsm_lchan *remote_lchan)
Harald Welte49f48b82009-02-17 15:29:33 +00001216{
Harald Welte11fa29c2009-02-19 17:24:39 +00001217 struct gsm_bts *bts = lchan->ts->trx->bts;
1218 struct gsm_bts *remote_bts = remote_lchan->ts->trx->bts;
Harald Welte805f6442009-07-28 18:25:29 +02001219 int rc;
Harald Welte49f48b82009-02-17 15:29:33 +00001220
Harald Welte11fa29c2009-02-19 17:24:39 +00001221 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 Welteda7ab742009-12-19 22:23:05 +01001229
1230 // todo: map between different bts types
Harald Welte11fa29c2009-02-19 17:24:39 +00001231 switch (bts->type) {
Mike Habene2d82272009-10-02 12:19:34 +01001232 case GSM_BTS_TYPE_NANOBTS:
Harald Welte805f6442009-07-28 18:25:29 +02001233 if (!ipacc_rtp_direct) {
1234 /* connect the TCH's to our RTP proxy */
Harald Weltea72273e2009-12-20 16:51:09 +01001235 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welte805f6442009-07-28 18:25:29 +02001236 if (rc < 0)
1237 return rc;
Harald Weltea72273e2009-12-20 16:51:09 +01001238 rc = rsl_ipacc_mdcx_to_rtpsock(remote_lchan);
Harald Welteda7ab742009-12-19 22:23:05 +01001239#warning do we need a check of rc here?
Harald Welte805f6442009-07-28 18:25:29 +02001240
1241 /* connect them with each other */
Harald Welte2c828992009-12-02 01:56:49 +05301242 rtp_socket_proxy(lchan->abis_ip.rtp_socket,
1243 remote_lchan->abis_ip.rtp_socket);
Harald Welte805f6442009-07-28 18:25:29 +02001244 } else {
1245 /* directly connect TCH RTP streams to each other */
Harald Welte2c828992009-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 Welte2c828992009-12-02 01:56:49 +05301248 remote_lchan->abis_ip.rtp_payload2);
Harald Welte805f6442009-07-28 18:25:29 +02001249 if (rc < 0)
1250 return rc;
Harald Welte2c828992009-12-02 01:56:49 +05301251 rc = rsl_ipacc_mdcx(remote_lchan, lchan->abis_ip.bound_ip,
1252 lchan->abis_ip.bound_port,
Harald Welte2c828992009-12-02 01:56:49 +05301253 lchan->abis_ip.rtp_payload2);
Harald Welte805f6442009-07-28 18:25:29 +02001254 }
Harald Welte11fa29c2009-02-19 17:24:39 +00001255 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 Welteda7ab742009-12-19 22:23:05 +01001261 return -EINVAL;
Harald Welte11fa29c2009-02-19 17:24:39 +00001262 }
Harald Welte49f48b82009-02-17 15:29:33 +00001263
1264 return 0;
1265}
1266
Harald Welte4bfdfe72009-06-10 23:11:52 +08001267/* bridge channels of two transactions */
1268static int tch_bridge(struct gsm_network *net, u_int32_t *refs)
Harald Welte7ccf7782009-02-17 01:43:01 +00001269{
Harald Weltedcaf5652009-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 Welte7ccf7782009-02-17 01:43:01 +00001272
Harald Welte4bfdfe72009-06-10 23:11:52 +08001273 if (!trans1 || !trans2)
Harald Welte7ccf7782009-02-17 01:43:01 +00001274 return -EIO;
1275
Harald Welte4bfdfe72009-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 Welte7ccf7782009-02-17 01:43:01 +00001281}
1282
Harald Welteda7ab742009-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 Welte4bfdfe72009-06-10 23:11:52 +08001285{
1286 struct gsm_trans *trans;
Harald Welteda7ab742009-12-19 22:23:05 +01001287 struct gsm_lchan *lchan;
1288 struct gsm_bts *bts;
1289 int rc;
Harald Welte7ccf7782009-02-17 01:43:01 +00001290
Harald Welte4bfdfe72009-06-10 23:11:52 +08001291 /* Find callref */
Harald Welteda7ab742009-12-19 22:23:05 +01001292 trans = trans_find_by_callref(net, callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001293 if (!trans)
1294 return -EIO;
1295 if (!trans->lchan)
1296 return 0;
Harald Welteda7ab742009-12-19 22:23:05 +01001297 lchan = trans->lchan;
1298 bts = lchan->ts->trx->bts;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001299
Harald Welteda7ab742009-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 Weltea72273e2009-12-20 16:51:09 +01001315 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welteda7ab742009-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 Welte4bfdfe72009-06-10 23:11:52 +08001336}
1337
Harald Welte4bfdfe72009-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 Weltec66b71c2009-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 Welte4bfdfe72009-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 Weltedcaf5652009-07-23 18:56:43 +02001362 switch(trans->cc.Tcurrent) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001363 case 0x303:
1364 release = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001365 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001366 break;
1367 case 0x310:
1368 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001369 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-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 Weltec66b71c2009-06-11 14:23:20 +08001377 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001378 break;
1379 case 0x308:
Harald Weltedcaf5652009-07-23 18:56:43 +02001380 if (!trans->cc.T308_second) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001381 /* restart T308 a second time */
Harald Weltedcaf5652009-07-23 18:56:43 +02001382 gsm48_cc_tx_release(trans, &trans->cc.msg);
1383 trans->cc.T308_second = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001384 break; /* stay in release state */
1385 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001386 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001387 return;
1388// release = 1;
1389// l4_cause = 14;
1390// break;
1391 case 0x306:
1392 release = 1;
Harald Weltedcaf5652009-07-23 18:56:43 +02001393 mo_cause = trans->cc.msg.cause.value;
1394 mo_location = trans->cc.msg.cause.location;
Harald Welte4bfdfe72009-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 Welte596fed42009-07-23 19:06:52 +02001405 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Harald Welte4bfdfe72009-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 Welte596fed42009-07-23 19:06:52 +02001413 mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &l4_rel);
Harald Welte4bfdfe72009-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 Weltedcaf5652009-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 Welte4bfdfe72009-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 Weltedcaf5652009-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 Welte4bfdfe72009-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 Welte474d19f2010-03-02 23:18:30 +01001452 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-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 Eversbergc079be42009-06-15 23:22:09 +02001462 strncpy(setup.imsi, trans->subscr->imsi,
1463 sizeof(setup.imsi)-1);
Harald Welte4bfdfe72009-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 Welte55c8f352010-03-07 23:40:35 +01001468 gsm48_decode_bearer_cap(&setup.bearer_cap,
Harald Welte4bfdfe72009-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 Welte55c8f352010-03-07 23:40:35 +01001474 gsm48_decode_facility(&setup.facility,
Harald Welte4bfdfe72009-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 Welte55c8f352010-03-07 23:40:35 +01001480 gsm48_decode_called(&setup.called,
Harald Welte4bfdfe72009-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 Welte55c8f352010-03-07 23:40:35 +01001486 gsm48_decode_useruser(&setup.useruser,
Harald Welte4bfdfe72009-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 Welte55c8f352010-03-07 23:40:35 +01001492 gsm48_decode_ssversion(&setup.ssversion,
Harald Welte4bfdfe72009-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 Welte55c8f352010-03-07 23:40:35 +01001504 gsm48_decode_cccap(&setup.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001505 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1506 }
1507
Harald Welte4bfdfe72009-06-10 23:11:52 +08001508 new_cc_state(trans, GSM_CSTATE_INITIATED);
1509
Harald Welte (local)47df3992009-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 Welte4bfdfe72009-06-10 23:11:52 +08001514 /* indicate setup to MNCC */
Harald Welte596fed42009-07-23 19:06:52 +02001515 mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_IND, &setup);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001516
Harald Welte13cac662009-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 Welte4bfdfe72009-06-10 23:11:52 +08001520 return 0;
1521}
1522
1523static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
Harald Welte65e74cc2008-12-29 01:55:35 +00001524{
1525 struct msgb *msg = gsm48_msgb_alloc();
1526 struct gsm48_hdr *gh;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001527 struct gsm_mncc *setup = arg;
Harald Welte78283ef2009-07-23 21:36:44 +02001528 int rc, trans_id;
Harald Welte65e74cc2008-12-29 01:55:35 +00001529
Harald Welte7ccf7782009-02-17 01:43:01 +00001530 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte65e74cc2008-12-29 01:55:35 +00001531
Harald Welte4bfdfe72009-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 Welte596fed42009-07-23 19:06:52 +02001537 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001538 GSM48_CAUSE_LOC_PRN_S_LU,
1539 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001540 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001541 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001542 return rc;
1543 }
1544
1545 /* Get free transaction_id */
Harald Welte78283ef2009-07-23 21:36:44 +02001546 trans_id = trans_assign_trans_id(trans->subscr, GSM48_PDISC_CC, 0);
1547 if (trans_id < 0) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001548 /* no free transaction ID */
Harald Welte596fed42009-07-23 19:06:52 +02001549 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001550 GSM48_CAUSE_LOC_PRN_S_LU,
1551 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001552 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001553 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001554 return rc;
1555 }
Harald Welte78283ef2009-07-23 21:36:44 +02001556 trans->transaction_id = trans_id;
Harald Welte49f48b82009-02-17 15:29:33 +00001557
Harald Welte65e74cc2008-12-29 01:55:35 +00001558 gh->msg_type = GSM48_MT_CC_SETUP;
Harald Welte09e38af2009-02-16 22:52:23 +00001559
Harald Welte4bfdfe72009-06-10 23:11:52 +08001560 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
Harald Welte65e74cc2008-12-29 01:55:35 +00001561
Harald Welte4bfdfe72009-06-10 23:11:52 +08001562 /* bearer capability */
1563 if (setup->fields & MNCC_F_BEARER_CAP)
Harald Welte55c8f352010-03-07 23:40:35 +01001564 gsm48_encode_bearer_cap(msg, 0, &setup->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001565 /* facility */
1566 if (setup->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001567 gsm48_encode_facility(msg, 0, &setup->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001568 /* progress */
1569 if (setup->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001570 gsm48_encode_progress(msg, 0, &setup->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001571 /* calling party BCD number */
1572 if (setup->fields & MNCC_F_CALLING)
Harald Welte55c8f352010-03-07 23:40:35 +01001573 gsm48_encode_calling(msg, &setup->calling);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001574 /* called party BCD number */
1575 if (setup->fields & MNCC_F_CALLED)
Harald Welte55c8f352010-03-07 23:40:35 +01001576 gsm48_encode_called(msg, &setup->called);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001577 /* user-user */
1578 if (setup->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001579 gsm48_encode_useruser(msg, 0, &setup->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001580 /* redirecting party BCD number */
1581 if (setup->fields & MNCC_F_REDIRECTING)
Harald Welte55c8f352010-03-07 23:40:35 +01001582 gsm48_encode_redirecting(msg, &setup->redirecting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001583 /* signal */
1584 if (setup->fields & MNCC_F_SIGNAL)
Harald Welte55c8f352010-03-07 23:40:35 +01001585 gsm48_encode_signal(msg, setup->signal);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001586
1587 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
Harald Welte65e74cc2008-12-29 01:55:35 +00001588
Harald Welte39e2ead2009-07-23 21:13:03 +02001589 return gsm48_sendmsg(msg, trans);
Harald Welte65e74cc2008-12-29 01:55:35 +00001590}
1591
Harald Welte4bfdfe72009-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 Welte474d19f2010-03-02 23:18:30 +01001604 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-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 Welte55c8f352010-03-07 23:40:35 +01001615 gsm48_decode_bearer_cap(&call_conf.bearer_cap,
Harald Welte4bfdfe72009-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 Welte55c8f352010-03-07 23:40:35 +01001621 gsm48_decode_cause(&call_conf.cause,
Harald Welte4bfdfe72009-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 Welte55c8f352010-03-07 23:40:35 +01001627 gsm48_decode_cccap(&call_conf.cccap,
Harald Welte4bfdfe72009-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 Welte596fed42009-07-23 19:06:52 +02001633 return mncc_recvmsg(trans->subscr->net, trans, MNCC_CALL_CONF_IND,
1634 &call_conf);
Harald Welte4bfdfe72009-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 Welte4bfdfe72009-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 Welte55c8f352010-03-07 23:40:35 +01001649 gsm48_encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001650 /* facility */
1651 if (proceeding->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001652 gsm48_encode_facility(msg, 0, &proceeding->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001653 /* progress */
1654 if (proceeding->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001655 gsm48_encode_progress(msg, 0, &proceeding->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001656
Harald Welte39e2ead2009-07-23 21:13:03 +02001657 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-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 Welte474d19f2010-03-02 23:18:30 +01001672 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001673 /* facility */
1674 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1675 alerting.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001676 gsm48_decode_facility(&alerting.facility,
Harald Welte4bfdfe72009-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 Welte55c8f352010-03-07 23:40:35 +01001683 gsm48_decode_progress(&alerting.progress,
Harald Welte4bfdfe72009-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 Welte55c8f352010-03-07 23:40:35 +01001689 gsm48_decode_ssversion(&alerting.ssversion,
Harald Welte4bfdfe72009-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 Welte596fed42009-07-23 19:06:52 +02001695 return mncc_recvmsg(trans->subscr->net, trans, MNCC_ALERT_IND,
1696 &alerting);
Harald Welte4bfdfe72009-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 Welte4bfdfe72009-06-10 23:11:52 +08001705 gh->msg_type = GSM48_MT_CC_ALERTING;
1706
1707 /* facility */
1708 if (alerting->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001709 gsm48_encode_facility(msg, 0, &alerting->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001710 /* progress */
1711 if (alerting->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001712 gsm48_encode_progress(msg, 0, &alerting->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001713 /* user-user */
1714 if (alerting->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001715 gsm48_encode_useruser(msg, 0, &alerting->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001716
1717 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
1718
Harald Welte39e2ead2009-07-23 21:13:03 +02001719 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-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 Welte4bfdfe72009-06-10 23:11:52 +08001728 gh->msg_type = GSM48_MT_CC_PROGRESS;
1729
1730 /* progress */
Harald Welte55c8f352010-03-07 23:40:35 +01001731 gsm48_encode_progress(msg, 1, &progress->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001732 /* user-user */
1733 if (progress->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001734 gsm48_encode_useruser(msg, 0, &progress->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001735
Harald Welte39e2ead2009-07-23 21:13:03 +02001736 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-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 Welte4bfdfe72009-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 Welte55c8f352010-03-07 23:40:35 +01001752 gsm48_encode_facility(msg, 0, &connect->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001753 /* progress */
1754 if (connect->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001755 gsm48_encode_progress(msg, 0, &connect->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001756 /* connected number */
1757 if (connect->fields & MNCC_F_CONNECTED)
Harald Welte55c8f352010-03-07 23:40:35 +01001758 gsm48_encode_connected(msg, &connect->connected);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001759 /* user-user */
1760 if (connect->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001761 gsm48_encode_useruser(msg, 0, &connect->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001762
1763 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
1764
Harald Welte39e2ead2009-07-23 21:13:03 +02001765 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-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 Welte474d19f2010-03-02 23:18:30 +01001779 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-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 Eversbergc079be42009-06-15 23:22:09 +02001785 strncpy(connect.imsi, trans->subscr->imsi,
1786 sizeof(connect.imsi)-1);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001787 }
1788 /* facility */
1789 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1790 connect.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001791 gsm48_decode_facility(&connect.facility,
Harald Welte4bfdfe72009-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 Welte55c8f352010-03-07 23:40:35 +01001797 gsm48_decode_useruser(&connect.useruser,
Harald Welte4bfdfe72009-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 Welte55c8f352010-03-07 23:40:35 +01001803 gsm48_decode_ssversion(&connect.ssversion,
Harald Welte4bfdfe72009-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 Welte596fed42009-07-23 19:06:52 +02001809 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_CNF, &connect);
Harald Welte4bfdfe72009-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 Welte596fed42009-07-23 19:06:52 +02001823 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_COMPL_IND,
Harald Welte4bfdfe72009-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 Welte4bfdfe72009-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 Welte39e2ead2009-07-23 21:13:03 +02001836 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-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 Welte474d19f2010-03-02 23:18:30 +01001852 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001853 /* cause */
1854 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1855 disc.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01001856 gsm48_decode_cause(&disc.cause,
Harald Welte4bfdfe72009-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 Welte55c8f352010-03-07 23:40:35 +01001862 gsm48_decode_facility(&disc.facility,
Harald Welte4bfdfe72009-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 Welte55c8f352010-03-07 23:40:35 +01001868 gsm48_decode_useruser(&disc.useruser,
Harald Welte4bfdfe72009-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 Welte55c8f352010-03-07 23:40:35 +01001874 gsm48_decode_ssversion(&disc.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001875 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1876 }
1877
Harald Welte596fed42009-07-23 19:06:52 +02001878 return mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &disc);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001879
1880}
1881
Harald Weltec66b71c2009-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 Welte4bfdfe72009-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 Welte4bfdfe72009-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 Welte55c8f352010-03-07 23:40:35 +01001905 gsm48_encode_cause(msg, 1, &disc->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001906 else
Harald Welte55c8f352010-03-07 23:40:35 +01001907 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001908
1909 /* facility */
1910 if (disc->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001911 gsm48_encode_facility(msg, 0, &disc->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001912 /* progress */
1913 if (disc->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001914 gsm48_encode_progress(msg, 0, &disc->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001915 /* user-user */
1916 if (disc->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001917 gsm48_encode_useruser(msg, 0, &disc->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001918
1919 /* store disconnect cause for T306 expiry */
Harald Weltedcaf5652009-07-23 18:56:43 +02001920 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001921
1922 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
1923
Harald Welte39e2ead2009-07-23 21:13:03 +02001924 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-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 Welte474d19f2010-03-02 23:18:30 +01001939 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001940 /* cause */
1941 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1942 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01001943 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-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 Welte55c8f352010-03-07 23:40:35 +01001949 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-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 Welte55c8f352010-03-07 23:40:35 +01001955 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-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 Welte55c8f352010-03-07 23:40:35 +01001961 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001962 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1963 }
1964
Harald Weltedcaf5652009-07-23 18:56:43 +02001965 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001966 /* release collision 5.4.5 */
Harald Welte596fed42009-07-23 19:06:52 +02001967 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_CNF, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001968 } else {
Harald Welte596fed42009-07-23 19:06:52 +02001969 rc = gsm48_tx_simple(msg->lchan,
Harald Welte6f5aee02009-07-23 21:21:14 +02001970 GSM48_PDISC_CC | (trans->transaction_id << 4),
Harald Welte596fed42009-07-23 19:06:52 +02001971 GSM48_MT_CC_RELEASE_COMPL);
1972 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_IND, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001973 }
1974
1975 new_cc_state(trans, GSM_CSTATE_NULL);
1976
1977 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001978 trans_free(trans);
Harald Welte4bfdfe72009-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 Welte4bfdfe72009-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 Welte55c8f352010-03-07 23:40:35 +01001998 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001999 /* facility */
2000 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002001 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002002 /* user-user */
2003 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002004 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002005
Harald Weltedcaf5652009-07-23 18:56:43 +02002006 trans->cc.T308_second = 0;
2007 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002008
Harald Weltedcaf5652009-07-23 18:56:43 +02002009 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002010 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
2011
Harald Welte39e2ead2009-07-23 21:13:03 +02002012 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-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 Welte474d19f2010-03-02 23:18:30 +01002027 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002028 /* cause */
2029 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2030 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002031 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-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 Welte55c8f352010-03-07 23:40:35 +01002037 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-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 Welte55c8f352010-03-07 23:40:35 +01002043 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-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 Welte55c8f352010-03-07 23:40:35 +01002049 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002050 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2051 }
2052
2053 if (trans->callref) {
Harald Weltedcaf5652009-07-23 18:56:43 +02002054 switch (trans->cc.state) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002055 case GSM_CSTATE_CALL_PRESENT:
Harald Welte596fed42009-07-23 19:06:52 +02002056 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002057 MNCC_REJ_IND, &rel);
2058 break;
2059 case GSM_CSTATE_RELEASE_REQ:
Harald Welte596fed42009-07-23 19:06:52 +02002060 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002061 MNCC_REL_CNF, &rel);
Harald Welteb83d9382009-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 Welte4bfdfe72009-06-10 23:11:52 +08002065 break;
2066 default:
Harald Welte596fed42009-07-23 19:06:52 +02002067 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002068 MNCC_REL_IND, &rel);
2069 }
2070 }
2071
2072 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002073 trans_free(trans);
Harald Welte4bfdfe72009-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 Welte4bfdfe72009-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 Welte55c8f352010-03-07 23:40:35 +01002092 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002093 /* facility */
2094 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002095 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002096 /* user-user */
2097 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002098 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002099
Harald Weltedcaf5652009-07-23 18:56:43 +02002100 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002101
Harald Welte39e2ead2009-07-23 21:13:03 +02002102 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-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 Welte474d19f2010-03-02 23:18:30 +01002114 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002115 /* facility */
2116 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2117 fac.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002118 gsm48_decode_facility(&fac.facility,
Harald Welte4bfdfe72009-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 Welte55c8f352010-03-07 23:40:35 +01002124 gsm48_decode_ssversion(&fac.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002125 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2126 }
2127
Harald Welte596fed42009-07-23 19:06:52 +02002128 return mncc_recvmsg(trans->subscr->net, trans, MNCC_FACILITY_IND, &fac);
Harald Welte4bfdfe72009-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 Welte4bfdfe72009-06-10 23:11:52 +08002137 gh->msg_type = GSM48_MT_CC_FACILITY;
2138
2139 /* facility */
Harald Welte55c8f352010-03-07 23:40:35 +01002140 gsm48_encode_facility(msg, 1, &fac->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002141
Harald Welte39e2ead2009-07-23 21:13:03 +02002142 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-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 Welte596fed42009-07-23 19:06:52 +02002151 return mncc_recvmsg(trans->subscr->net, trans, MNCC_HOLD_IND, &hold);
Harald Welte4bfdfe72009-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 Welte4bfdfe72009-06-10 23:11:52 +08002159 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
2160
Harald Welte39e2ead2009-07-23 21:13:03 +02002161 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-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 Welte4bfdfe72009-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 Welte55c8f352010-03-07 23:40:35 +01002174 gsm48_encode_cause(msg, 1, &hold_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002175 else
Harald Welte55c8f352010-03-07 23:40:35 +01002176 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002177
Harald Welte39e2ead2009-07-23 21:13:03 +02002178 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002179}
2180
2181static int gsm48_cc_rx_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 Welte596fed42009-07-23 19:06:52 +02002187 return mncc_recvmsg(trans->subscr->net, trans, MNCC_RETRIEVE_IND,
2188 &retrieve);
Harald Welte4bfdfe72009-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 Welte4bfdfe72009-06-10 23:11:52 +08002196 gh->msg_type = GSM48_MT_CC_RETR_ACK;
2197
Harald Welte39e2ead2009-07-23 21:13:03 +02002198 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-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 Welte4bfdfe72009-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 Welte55c8f352010-03-07 23:40:35 +01002211 gsm48_encode_cause(msg, 1, &retrieve_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002212 else
Harald Welte55c8f352010-03-07 23:40:35 +01002213 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002214
Harald Welte39e2ead2009-07-23 21:13:03 +02002215 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-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 Welte474d19f2010-03-02 23:18:30 +01002227 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002228 /* keypad facility */
2229 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
2230 dtmf.fields |= MNCC_F_KEYPAD;
Harald Welte55c8f352010-03-07 23:40:35 +01002231 gsm48_decode_keypad(&dtmf.keypad,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002232 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
2233 }
2234
Harald Welte596fed42009-07-23 19:06:52 +02002235 return mncc_recvmsg(trans->subscr->net, trans, MNCC_START_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-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 Welte4bfdfe72009-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 Welte55c8f352010-03-07 23:40:35 +01002248 gsm48_encode_keypad(msg, dtmf->keypad);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002249
Harald Welte39e2ead2009-07-23 21:13:03 +02002250 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-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 Welte4bfdfe72009-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 Welte55c8f352010-03-07 23:40:35 +01002263 gsm48_encode_cause(msg, 1, &dtmf->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002264 else
Harald Welte55c8f352010-03-07 23:40:35 +01002265 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002266
Harald Welte39e2ead2009-07-23 21:13:03 +02002267 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-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 Welte4bfdfe72009-06-10 23:11:52 +08002275 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
2276
Harald Welte39e2ead2009-07-23 21:13:03 +02002277 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-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 Welte596fed42009-07-23 19:06:52 +02002287 return mncc_recvmsg(trans->subscr->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-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 Welte474d19f2010-03-02 23:18:30 +01002299 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-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 Welte55c8f352010-03-07 23:40:35 +01002303 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-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 Welte596fed42009-07-23 19:06:52 +02002309 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_IND, &modify);
Harald Welte4bfdfe72009-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 Welte4bfdfe72009-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 Welte55c8f352010-03-07 23:40:35 +01002323 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002324
2325 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
2326
Harald Welte39e2ead2009-07-23 21:13:03 +02002327 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002328}
2329
2330static int gsm48_cc_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 Welte474d19f2010-03-02 23:18:30 +01002341 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-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 Welte55c8f352010-03-07 23:40:35 +01002345 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-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 Welte596fed42009-07-23 19:06:52 +02002351 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_CNF, &modify);
Harald Welte4bfdfe72009-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 Welte4bfdfe72009-06-10 23:11:52 +08002360 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
2361
2362 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002363 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002364
2365 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2366
Harald Welte39e2ead2009-07-23 21:13:03 +02002367 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-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 Welte474d19f2010-03-02 23:18:30 +01002381 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
Harald Welte4bfdfe72009-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 Welte55c8f352010-03-07 23:40:35 +01002385 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-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 Welte55c8f352010-03-07 23:40:35 +01002391 gsm48_decode_cause(&modify.cause,
Harald Welte4bfdfe72009-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 Welte596fed42009-07-23 19:06:52 +02002397 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_REJ, &modify);
Harald Welte4bfdfe72009-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 Welte4bfdfe72009-06-10 23:11:52 +08002406 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
2407
2408 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002409 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002410 /* cause */
Harald Welte55c8f352010-03-07 23:40:35 +01002411 gsm48_encode_cause(msg, 1, &modify->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002412
2413 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2414
Harald Welte39e2ead2009-07-23 21:13:03 +02002415 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-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 Welte4bfdfe72009-06-10 23:11:52 +08002424 gh->msg_type = GSM48_MT_CC_NOTIFY;
2425
2426 /* notify */
Harald Welte55c8f352010-03-07 23:40:35 +01002427 gsm48_encode_notify(msg, notify->notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002428
Harald Welte39e2ead2009-07-23 21:13:03 +02002429 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-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 Welte474d19f2010-03-02 23:18:30 +01002441// tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002442 if (payload_len >= 1)
Harald Welte55c8f352010-03-07 23:40:35 +01002443 gsm48_decode_notify(&notify.notify, gh->data);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002444
Harald Welte596fed42009-07-23 19:06:52 +02002445 return mncc_recvmsg(trans->subscr->net, trans, MNCC_NOTIFY_IND, &notify);
Harald Welte4bfdfe72009-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 Welte4bfdfe72009-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 Welte55c8f352010-03-07 23:40:35 +01002458 gsm48_encode_useruser(msg, 1, &user->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002459 /* more data */
2460 if (user->more)
Harald Welte55c8f352010-03-07 23:40:35 +01002461 gsm48_encode_more(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002462
Harald Welte39e2ead2009-07-23 21:13:03 +02002463 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-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 Welte474d19f2010-03-02 23:18:30 +01002475 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002476 /* user-user */
2477 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2478 user.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002479 gsm48_decode_useruser(&user.useruser,
Harald Welte4bfdfe72009-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 Welte596fed42009-07-23 19:06:52 +02002486 return mncc_recvmsg(trans->subscr->net, trans, MNCC_USERINFO_IND, &user);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002487}
2488
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +02002489static int _gsm48_lchan_modify(struct gsm_trans *trans, void *arg)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002490{
2491 struct gsm_mncc *mode = arg;
2492
Holger Hans Peter Freytherea528022009-11-18 22:57:02 +01002493 return gsm48_lchan_modify(trans->lchan, mode->lchan_mode);
Harald Welte4bfdfe72009-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 Freytherff3f2602009-10-22 15:13:00 +02002551 MNCC_LCHAN_MODIFY, _gsm48_lchan_modify},
Harald Welte4bfdfe72009-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 Welte1a6f7982009-08-09 18:52:33 +02002560 int i, rc = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002561 struct gsm_trans *trans = NULL, *transt;
Harald Welte1a6f7982009-08-09 18:52:33 +02002562 struct gsm_lchan *lchan = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002563 struct gsm_bts *bts = NULL;
Harald Welte4bfdfe72009-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 Welteda7ab742009-12-19 22:23:05 +01002571 return tch_recv_mncc(net, data->callref, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002572 case MNCC_FRAME_RECV:
Harald Welteda7ab742009-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 Welte4bfdfe72009-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 Weltedcaf5652009-07-23 18:56:43 +02002601 trans = trans_find_by_callref(net, data->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002602
2603 /* Callref unknown */
2604 if (!trans) {
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01002605 struct gsm_subscriber *subscr;
2606
Harald Welte4a3464c2009-07-04 10:11:24 +02002607 if (msg_type != MNCC_SETUP_REQ) {
Harald Welte4bfdfe72009-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 Eversberg7563ac92009-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 Welte4bfdfe72009-06-10 23:11:52 +08002616 }
Andreas Eversbergc079be42009-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 Welte4bfdfe72009-06-10 23:11:52 +08002626 /* New transaction due to setup, find subscriber */
Andreas Eversbergc079be42009-06-15 23:22:09 +02002627 if (data->called.number[0])
Harald Welte9176bd42009-07-23 18:46:00 +02002628 subscr = subscr_get_by_extension(net,
2629 data->called.number);
Andreas Eversbergc079be42009-06-15 23:22:09 +02002630 else
Harald Welte9176bd42009-07-23 18:46:00 +02002631 subscr = subscr_get_by_imsi(net, data->imsi);
Harald Welte4bfdfe72009-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 Eversberg7563ac92009-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 Welte4bfdfe72009-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 Eversberg7563ac92009-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 Welte4bfdfe72009-06-10 23:11:52 +08002654 }
2655 /* Create transaction */
Harald Weltedcaf5652009-07-23 18:56:43 +02002656 trans = trans_alloc(subscr, GSM48_PDISC_CC, 0xff, data->callref);
2657 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002658 DEBUGP(DCC, "No memory for trans.\n");
2659 subscr_put(subscr);
2660 /* Ressource unavailable */
Andreas Eversberg7563ac92009-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 Welte4bfdfe72009-06-10 23:11:52 +08002664 return -ENOMEM;
2665 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08002666 /* Find lchan */
Harald Welte1a6f7982009-08-09 18:52:33 +02002667 lchan = lchan_for_subscr(subscr);
Harald Welte4bfdfe72009-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 Freytherccf53c62009-10-27 14:21:14 +01002682 subscr_put(subscr);
2683 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002684 return 0;
2685 }
2686 /* store setup informations until paging was successfull */
Harald Weltedcaf5652009-07-23 18:56:43 +02002687 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
Harald Weltea1b28582009-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 Freytherccf53c62009-10-27 14:21:14 +01002691 subscr_put(subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002692 return 0;
2693 }
2694 /* Assign lchan */
2695 trans->lchan = lchan;
2696 use_lchan(lchan);
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01002697 subscr_put(subscr);
Harald Welte4bfdfe72009-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 Weltec66b71c2009-06-11 14:23:20 +08002707 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
2708 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Harald Welte4bfdfe72009-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 Weltedcaf5652009-07-23 18:56:43 +02002714 trans_free(trans);
Harald Welte4bfdfe72009-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 Weltedcaf5652009-07-23 18:56:43 +02002723 get_mncc_name(msg_type), trans->cc.state,
Harald Weltee95daf12010-03-25 12:13:02 +08002724 gsm48_cc_state_name(trans->cc.state));
Harald Welte4bfdfe72009-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 Weltedcaf5652009-07-23 18:56:43 +02002729 && ((1 << trans->cc.state) & downstatelist[i].states))
Harald Welte4bfdfe72009-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 Welte4bc90a12008-12-27 16:32:52 +00002796static 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 Welte6f5aee02009-07-23 21:21:14 +02002800 u_int8_t transaction_id = ((gh->proto_discr & 0xf0) ^ 0x80) >> 4; /* flip */
Harald Welte4bfdfe72009-06-10 23:11:52 +08002801 struct gsm_lchan *lchan = msg->lchan;
Harald Weltedcaf5652009-07-23 18:56:43 +02002802 struct gsm_trans *trans = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002803 int i, rc = 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00002804
Harald Welte4bfdfe72009-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 Welte4bc90a12008-12-27 16:32:52 +00002808 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08002809
2810 /* Find transaction */
Harald Welteb8b40732009-07-23 21:58:40 +02002811 trans = trans_find_by_id(lchan->subscr, GSM48_PDISC_CC, transaction_id);
Harald Weltedcaf5652009-07-23 18:56:43 +02002812
Harald Welte6f5aee02009-07-23 21:21:14 +02002813 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-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 Weltee95daf12010-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 Welte4bfdfe72009-06-10 23:11:52 +08002819
2820 /* Create transaction */
2821 if (!trans) {
Harald Welte6f5aee02009-07-23 21:21:14 +02002822 DEBUGP(DCC, "Unknown transaction ID %x, "
Harald Welte4bfdfe72009-06-10 23:11:52 +08002823 "creating new trans.\n", transaction_id);
2824 /* Create transaction */
Harald Weltedcaf5652009-07-23 18:56:43 +02002825 trans = trans_alloc(lchan->subscr, GSM48_PDISC_CC,
2826 transaction_id, new_callref++);
2827 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002828 DEBUGP(DCC, "No memory for trans.\n");
2829 rc = gsm48_tx_simple(msg->lchan,
Harald Welte6f5aee02009-07-23 21:21:14 +02002830 GSM48_PDISC_CC | (transaction_id << 4),
Harald Welte4bfdfe72009-06-10 23:11:52 +08002831 GSM48_MT_CC_RELEASE_COMPL);
2832 return -ENOMEM;
2833 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08002834 /* Assign transaction */
Harald Welte4bfdfe72009-06-10 23:11:52 +08002835 trans->lchan = lchan;
2836 use_lchan(lchan);
Harald Welte4bfdfe72009-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 Weltedcaf5652009-07-23 18:56:43 +02002842 && ((1 << trans->cc.state) & datastatelist[i].states))
Harald Welte4bfdfe72009-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 Welte4bc90a12008-12-27 16:32:52 +00002850
2851 return rc;
2852}
2853
Harald Welte52b1f982008-12-23 20:25:15 +00002854/* here we pass in a msgb from the RSL->RLL. We expect the l3 pointer to be set */
Harald Welte (local)daef6062009-08-14 11:41:12 +02002855int gsm0408_rcvmsg(struct msgb *msg, u_int8_t link_id)
Harald Welte52b1f982008-12-23 20:25:15 +00002856{
2857 struct gsm48_hdr *gh = msgb_l3(msg);
2858 u_int8_t pdisc = gh->proto_discr & 0x0f;
Harald Welte8470bf22008-12-25 23:28:35 +00002859 int rc = 0;
Harald Welte51008772009-12-29 11:49:12 +01002860
2861 if (silent_call_reroute(msg))
2862 return silent_call_rx(msg);
Harald Welte52b1f982008-12-23 20:25:15 +00002863
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;
Harald Weltebcae43f2008-12-27 21:45:37 +00002874 case GSM48_PDISC_SMS:
Harald Welte (local)daef6062009-08-14 11:41:12 +02002875 rc = gsm0411_rcv_sms(msg, link_id);
Harald Weltebcae43f2008-12-27 21:45:37 +00002876 break;
Harald Welte52b1f982008-12-23 20:25:15 +00002877 case GSM48_PDISC_MM_GPRS:
Harald Weltebcae43f2008-12-27 21:45:37 +00002878 case GSM48_PDISC_SM_GPRS:
Harald Welte5d24ba12009-12-24 12:13:17 +01002879 LOGP(DRLL, LOGL_NOTICE, "Unimplemented "
2880 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Harald Welte52b1f982008-12-23 20:25:15 +00002881 break;
Harald Welte6eafe912009-10-16 08:32:58 +02002882 case GSM48_PDISC_NC_SS:
2883 rc = handle_rcv_ussd(msg);
2884 break;
Harald Welte52b1f982008-12-23 20:25:15 +00002885 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01002886 LOGP(DRLL, LOGL_NOTICE, "Unknown "
2887 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Harald Welte52b1f982008-12-23 20:25:15 +00002888 break;
2889 }
2890
2891 return rc;
2892}
Harald Welte8470bf22008-12-25 23:28:35 +00002893
Harald Welte4bfdfe72009-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 Welte316c8252009-06-26 19:40:48 +02002907 talloc_free(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002908 }
2909
2910 return work;
2911}
Harald Weltedcaf5652009-07-23 18:56:43 +02002912
Harald Welte805f6442009-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 Welte805f6442009-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}