blob: 2b22122fba9fe06c417eaea5dcddb6eeece6e896 [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 Welte8470bf22008-12-25 23:28:35 +000034#include <openbsc/msgb.h>
Harald Weltef1dae192009-12-14 22:24:28 +010035#include <openbsc/bitvec.h>
Harald Welte7584aea2009-02-11 11:44:12 +000036#include <openbsc/tlv.h>
Harald Welte8470bf22008-12-25 23:28:35 +000037#include <openbsc/debug.h>
38#include <openbsc/gsm_data.h>
Daniel Willmanneea93372009-08-13 03:42:07 +020039#include <openbsc/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 Welte2cf161b2009-06-20 22:36:41 +020050#include <openbsc/talloc.h>
Harald Weltedcaf5652009-07-23 18:56:43 +020051#include <openbsc/transaction.h>
Harald Welte6eafe912009-10-16 08:32:58 +020052#include <openbsc/ussd.h>
Harald Welte52b1f982008-12-23 20:25:15 +000053
Harald Welte0c389302009-06-10 12:08:54 +080054#define GSM_MAX_FACILITY 128
55#define GSM_MAX_SSVERSION 128
56#define GSM_MAX_USERUSER 128
57
Harald Welte (local)d19e58b2009-08-15 02:30:58 +020058void *tall_locop_ctx;
Harald Welte2cf161b2009-06-20 22:36:41 +020059
Harald Welte09e38af2009-02-16 22:52:23 +000060static const struct tlv_definition rsl_att_tlvdef = {
61 .def = {
62 [GSM48_IE_MOBILE_ID] = { TLV_TYPE_TLV },
63 [GSM48_IE_NAME_LONG] = { TLV_TYPE_TLV },
64 [GSM48_IE_NAME_SHORT] = { TLV_TYPE_TLV },
65 [GSM48_IE_UTC] = { TLV_TYPE_TV },
66 [GSM48_IE_NET_TIME_TZ] = { TLV_TYPE_FIXED, 7 },
67 [GSM48_IE_LSA_IDENT] = { TLV_TYPE_TLV },
68
69 [GSM48_IE_BEARER_CAP] = { TLV_TYPE_TLV },
70 [GSM48_IE_CAUSE] = { TLV_TYPE_TLV },
71 [GSM48_IE_CC_CAP] = { TLV_TYPE_TLV },
72 [GSM48_IE_ALERT] = { TLV_TYPE_TLV },
73 [GSM48_IE_FACILITY] = { TLV_TYPE_TLV },
74 [GSM48_IE_PROGR_IND] = { TLV_TYPE_TLV },
75 [GSM48_IE_AUX_STATUS] = { TLV_TYPE_TLV },
Harald Welte0c389302009-06-10 12:08:54 +080076 [GSM48_IE_NOTIFY] = { TLV_TYPE_TV },
Harald Welte09e38af2009-02-16 22:52:23 +000077 [GSM48_IE_KPD_FACILITY] = { TLV_TYPE_TV },
78 [GSM48_IE_SIGNAL] = { TLV_TYPE_TV },
Harald Welte0c389302009-06-10 12:08:54 +080079 [GSM48_IE_CONN_BCD] = { TLV_TYPE_TLV },
80 [GSM48_IE_CONN_SUB] = { TLV_TYPE_TLV },
Harald Welte09e38af2009-02-16 22:52:23 +000081 [GSM48_IE_CALLING_BCD] = { TLV_TYPE_TLV },
82 [GSM48_IE_CALLING_SUB] = { TLV_TYPE_TLV },
83 [GSM48_IE_CALLED_BCD] = { TLV_TYPE_TLV },
84 [GSM48_IE_CALLED_SUB] = { TLV_TYPE_TLV },
85 [GSM48_IE_REDIR_BCD] = { TLV_TYPE_TLV },
86 [GSM48_IE_REDIR_SUB] = { TLV_TYPE_TLV },
87 [GSM48_IE_LOWL_COMPAT] = { TLV_TYPE_TLV },
88 [GSM48_IE_HIGHL_COMPAT] = { TLV_TYPE_TLV },
89 [GSM48_IE_USER_USER] = { TLV_TYPE_TLV },
90 [GSM48_IE_SS_VERS] = { TLV_TYPE_TLV },
91 [GSM48_IE_MORE_DATA] = { TLV_TYPE_T },
92 [GSM48_IE_CLIR_SUPP] = { TLV_TYPE_T },
93 [GSM48_IE_CLIR_INVOC] = { TLV_TYPE_T },
94 [GSM48_IE_REV_C_SETUP] = { TLV_TYPE_T },
Harald Welte0c389302009-06-10 12:08:54 +080095 [GSM48_IE_REPEAT_CIR] = { TLV_TYPE_T },
96 [GSM48_IE_REPEAT_SEQ] = { TLV_TYPE_T },
Harald Welte09e38af2009-02-16 22:52:23 +000097 /* FIXME: more elements */
98 },
99};
Harald Weltecf5b3592009-05-01 18:28:42 +0000100
101static const char *rr_cause_names[] = {
102 [GSM48_RR_CAUSE_NORMAL] = "Normal event",
103 [GSM48_RR_CAUSE_ABNORMAL_UNSPEC] = "Abnormal release, unspecified",
104 [GSM48_RR_CAUSE_ABNORMAL_UNACCT] = "Abnormal release, channel unacceptable",
105 [GSM48_RR_CAUSE_ABNORMAL_TIMER] = "Abnormal release, timer expired",
106 [GSM48_RR_CAUSE_ABNORMAL_NOACT] = "Abnormal release, no activity on radio path",
107 [GSM48_RR_CAUSE_PREMPTIVE_REL] = "Preemptive release",
108 [GSM48_RR_CAUSE_HNDOVER_IMP] = "Handover impossible, timing advance out of range",
109 [GSM48_RR_CAUSE_CHAN_MODE_UNACCT] = "Channel mode unacceptable",
110 [GSM48_RR_CAUSE_FREQ_NOT_IMPL] = "Frequency not implemented",
111 [GSM48_RR_CAUSE_CALL_CLEARED] = "Call already cleared",
112 [GSM48_RR_CAUSE_SEMANT_INCORR] = "Semantically incorrect message",
113 [GSM48_RR_CAUSE_INVALID_MAND_INF] = "Invalid mandatory information",
114 [GSM48_RR_CAUSE_MSG_TYPE_N] = "Message type non-existant or not implemented",
115 [GSM48_RR_CAUSE_MSG_TYPE_N_COMPAT] = "Message type not compatible with protocol state",
116 [GSM48_RR_CAUSE_COND_IE_ERROR] = "Conditional IE error",
117 [GSM48_RR_CAUSE_NO_CELL_ALLOC_A] = "No cell allocation available",
118 [GSM48_RR_CAUSE_PROT_ERROR_UNSPC] = "Protocol error unspecified",
119};
120
Harald Welte4bfdfe72009-06-10 23:11:52 +0800121static const char *cc_state_names[] = {
122 "NULL",
123 "INITIATED",
124 "illegal state 2",
125 "MO_CALL_PROC",
126 "CALL_DELIVERED",
127 "illegal state 5",
128 "CALL_PRESENT",
129 "CALL_RECEIVED",
130 "CONNECT_REQUEST",
131 "MO_TERM_CALL_CONF",
132 "ACTIVE",
133 "DISCONNECT_REQ",
134 "DISCONNECT_IND",
135 "illegal state 13",
136 "illegal state 14",
137 "illegal state 15",
138 "illegal state 16",
139 "illegal state 17",
140 "illegal state 18",
141 "RELEASE_REQ",
142 "illegal state 20",
143 "illegal state 21",
144 "illegal state 22",
145 "illegal state 23",
146 "illegal state 24",
147 "illegal state 25",
148 "MO_ORIG_MODIFY",
149 "MO_TERM_MODIFY",
150 "CONNECT_IND",
151 "illegal state 29",
152 "illegal state 30",
153 "illegal state 31",
154};
155
Harald Weltecf5b3592009-05-01 18:28:42 +0000156static char strbuf[64];
157
158static const char *rr_cause_name(u_int8_t cause)
159{
160 if (cause < ARRAY_SIZE(rr_cause_names) &&
161 rr_cause_names[cause])
162 return rr_cause_names[cause];
163
164 snprintf(strbuf, sizeof(strbuf), "0x%02x", cause);
165 return strbuf;
166}
167
Holger Freytherd51524f2009-06-09 08:27:07 +0000168int gsm0408_loc_upd_acc(struct gsm_lchan *lchan, u_int32_t tmsi);
Harald Welte65e74cc2008-12-29 01:55:35 +0000169static int gsm48_tx_simple(struct gsm_lchan *lchan,
170 u_int8_t pdisc, u_int8_t msg_type);
Holger Freytherb7193e42008-12-29 17:44:08 +0000171static void schedule_reject(struct gsm_lchan *lchan);
Harald Welte65e74cc2008-12-29 01:55:35 +0000172
Harald Welte52b1f982008-12-23 20:25:15 +0000173struct gsm_lai {
174 u_int16_t mcc;
175 u_int16_t mnc;
176 u_int16_t lac;
177};
178
Harald Welte4bfdfe72009-06-10 23:11:52 +0800179static u_int32_t new_callref = 0x80000001;
180
Holger Freyther73487a22008-12-31 18:53:57 +0000181static int authorize_subscriber(struct gsm_loc_updating_operation *loc,
182 struct gsm_subscriber *subscriber)
Holger Freyther89824fc2008-12-30 16:18:18 +0000183{
184 if (!subscriber)
185 return 0;
186
Holger Freyther73487a22008-12-31 18:53:57 +0000187 /*
188 * Do not send accept yet as more information should arrive. Some
189 * phones will not send us the information and we will have to check
190 * what we want to do with that.
191 */
192 if (loc && (loc->waiting_for_imsi || loc->waiting_for_imei))
193 return 0;
194
Jan Luebbe06513f22009-08-12 12:48:00 +0200195 switch (subscriber->net->auth_policy) {
196 case GSM_AUTH_POLICY_CLOSED:
197 return subscriber->authorized;
Harald Welte (local)aa9dc192009-08-13 13:49:51 +0200198 case GSM_AUTH_POLICY_TOKEN:
Harald Welte (local)ee9afe32009-08-13 20:44:23 +0200199 if (subscriber->authorized)
200 return subscriber->authorized;
Harald Welte (local)aa9dc192009-08-13 13:49:51 +0200201 return (subscriber->flags & GSM_SUBSCRIBER_FIRST_CONTACT);
Jan Luebbe06513f22009-08-12 12:48:00 +0200202 case GSM_AUTH_POLICY_ACCEPT_ALL:
Holger Freyther89824fc2008-12-30 16:18:18 +0000203 return 1;
Jan Luebbe06513f22009-08-12 12:48:00 +0200204 default:
205 return 0;
206 }
Holger Freyther89824fc2008-12-30 16:18:18 +0000207}
Holger Freyther07cc8d82008-12-29 06:23:46 +0000208
Holger Freyther73487a22008-12-31 18:53:57 +0000209static void release_loc_updating_req(struct gsm_lchan *lchan)
210{
Harald Welte179f0642008-12-31 23:59:18 +0000211 if (!lchan->loc_operation)
Holger Freyther73487a22008-12-31 18:53:57 +0000212 return;
213
Harald Welteff117a82009-05-23 05:22:08 +0000214 bsc_del_timer(&lchan->loc_operation->updating_timer);
Harald Welte2cf161b2009-06-20 22:36:41 +0200215 talloc_free(lchan->loc_operation);
Holger Freyther73487a22008-12-31 18:53:57 +0000216 lchan->loc_operation = 0;
Holger Freyther3eaa7922009-01-01 02:59:03 +0000217 put_lchan(lchan);
Holger Freyther73487a22008-12-31 18:53:57 +0000218}
219
220static void allocate_loc_updating_req(struct gsm_lchan *lchan)
221{
Holger Freyther67b4b9a2009-01-01 03:46:11 +0000222 use_lchan(lchan);
Holger Freyther73487a22008-12-31 18:53:57 +0000223 release_loc_updating_req(lchan);
224
Harald Welte470ec292009-06-26 20:25:23 +0200225 lchan->loc_operation = talloc_zero(tall_locop_ctx,
226 struct gsm_loc_updating_operation);
Holger Freyther73487a22008-12-31 18:53:57 +0000227}
Holger Freyther07cc8d82008-12-29 06:23:46 +0000228
Holger Freytherd51524f2009-06-09 08:27:07 +0000229static int gsm0408_authorize(struct gsm_lchan *lchan, struct msgb *msg)
230{
Holger Freytherd51524f2009-06-09 08:27:07 +0000231 if (authorize_subscriber(lchan->loc_operation, lchan->subscr)) {
Harald Welteee5ad162009-08-09 19:07:00 +0200232 int rc;
233
Holger Freytherd51524f2009-06-09 08:27:07 +0000234 db_subscriber_alloc_tmsi(lchan->subscr);
Holger Freytherd51524f2009-06-09 08:27:07 +0000235 release_loc_updating_req(lchan);
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200236 rc = gsm0408_loc_upd_acc(msg->lchan, lchan->subscr->tmsi);
Harald Welte648b6ce2009-12-14 09:00:24 +0100237 if (lchan->ts->trx->bts->network->send_mm_info) {
238 /* send MM INFO with network name */
239 rc = gsm48_tx_mm_info(msg->lchan);
240 }
Harald Welteb83d9382009-12-12 21:00:48 +0100241
Harald Welteee5ad162009-08-09 19:07:00 +0200242 /* call subscr_update after putting the loc_upd_acc
243 * in the transmit queue, since S_SUBSCR_ATTACHED might
244 * trigger further action like SMS delivery */
245 subscr_update(lchan->subscr, msg->trx->bts,
246 GSM_SUBSCRIBER_UPDATE_ATTACHED);
Harald Welteb83d9382009-12-12 21:00:48 +0100247 /* try to close channel ASAP */
248 lchan_auto_release(lchan);
Harald Welteee5ad162009-08-09 19:07:00 +0200249 return rc;
Holger Freytherd51524f2009-06-09 08:27:07 +0000250 }
251
252 return 0;
253}
254
Holger Freyther7c19f742009-06-06 13:54:35 +0000255static int gsm0408_handle_lchan_signal(unsigned int subsys, unsigned int signal,
256 void *handler_data, void *signal_data)
257{
Harald Welte4bfdfe72009-06-10 23:11:52 +0800258 struct gsm_trans *trans, *temp;
259
Holger Freyther7c19f742009-06-06 13:54:35 +0000260 if (subsys != SS_LCHAN || signal != S_LCHAN_UNEXPECTED_RELEASE)
261 return 0;
262
263 /*
264 * Cancel any outstanding location updating request
265 * operation taking place on the lchan.
266 */
Harald Welte1a5c6bd2009-07-04 09:35:21 +0200267 struct gsm_lchan *lchan = (struct gsm_lchan *)signal_data;
Harald Weltec05677b2009-06-26 20:17:06 +0200268 if (!lchan)
269 return 0;
270
Holger Freyther7c19f742009-06-06 13:54:35 +0000271 release_loc_updating_req(lchan);
272
Harald Welte4bfdfe72009-06-10 23:11:52 +0800273 /* Free all transactions that are associated with the released lchan */
Harald Weltedcaf5652009-07-23 18:56:43 +0200274 /* FIXME: this is not neccessarily the right thing to do, we should
275 * only set trans->lchan to NULL and wait for another lchan to be
276 * established to the same MM entity (phone/subscriber) */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800277 llist_for_each_entry_safe(trans, temp, &lchan->ts->trx->bts->network->trans_list, entry) {
278 if (trans->lchan == lchan)
Harald Weltedcaf5652009-07-23 18:56:43 +0200279 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +0800280 }
281
Holger Freyther7c19f742009-06-06 13:54:35 +0000282 return 0;
283}
284
Harald Welte09e38af2009-02-16 22:52:23 +0000285static const char bcd_num_digits[] = {
286 '0', '1', '2', '3', '4', '5', '6', '7',
287 '8', '9', '*', '#', 'a', 'b', 'c', '\0'
288};
289
Harald Welte0c389302009-06-10 12:08:54 +0800290/* decode a 'called/calling/connect party BCD number' as in 10.5.4.7 */
291int decode_bcd_number(char *output, int output_len, const u_int8_t *bcd_lv,
292 int h_len)
Harald Welte09e38af2009-02-16 22:52:23 +0000293{
294 u_int8_t in_len = bcd_lv[0];
295 int i;
296
Harald Welte0c389302009-06-10 12:08:54 +0800297 for (i = 1 + h_len; i <= in_len; i++) {
Harald Welte09e38af2009-02-16 22:52:23 +0000298 /* lower nibble */
299 output_len--;
300 if (output_len <= 1)
301 break;
302 *output++ = bcd_num_digits[bcd_lv[i] & 0xf];
303
304 /* higher nibble */
305 output_len--;
306 if (output_len <= 1)
307 break;
308 *output++ = bcd_num_digits[bcd_lv[i] >> 4];
309 }
310 if (output_len >= 1)
311 *output++ = '\0';
312
Harald Welte0c389302009-06-10 12:08:54 +0800313 return 0;
Harald Welte09e38af2009-02-16 22:52:23 +0000314}
315
316/* convert a single ASCII character to call-control BCD */
317static int asc_to_bcd(const char asc)
318{
319 int i;
320
321 for (i = 0; i < ARRAY_SIZE(bcd_num_digits); i++) {
322 if (bcd_num_digits[i] == asc)
323 return i;
324 }
325 return -EINVAL;
326}
327
Harald Welte0c389302009-06-10 12:08:54 +0800328/* convert a ASCII phone number to 'called/calling/connect party BCD number' */
Harald Welte09e38af2009-02-16 22:52:23 +0000329int encode_bcd_number(u_int8_t *bcd_lv, u_int8_t max_len,
Harald Welte0c389302009-06-10 12:08:54 +0800330 int h_len, const char *input)
Harald Welte09e38af2009-02-16 22:52:23 +0000331{
332 int in_len = strlen(input);
333 int i;
Harald Welte0c389302009-06-10 12:08:54 +0800334 u_int8_t *bcd_cur = bcd_lv + 1 + h_len;
Harald Welte09e38af2009-02-16 22:52:23 +0000335
336 /* two digits per byte, plus type byte */
Harald Welte0c389302009-06-10 12:08:54 +0800337 bcd_lv[0] = in_len/2 + h_len;
Harald Welte09e38af2009-02-16 22:52:23 +0000338 if (in_len % 2)
339 bcd_lv[0]++;
340
Harald Welte0c389302009-06-10 12:08:54 +0800341 if (bcd_lv[0] > max_len)
342 return -EIO;
Harald Welte09e38af2009-02-16 22:52:23 +0000343
344 for (i = 0; i < in_len; i++) {
345 int rc = asc_to_bcd(input[i]);
346 if (rc < 0)
347 return rc;
348 if (i % 2 == 0)
349 *bcd_cur = rc;
350 else
351 *bcd_cur++ |= (rc << 4);
352 }
353 /* append padding nibble in case of odd length */
354 if (i % 2)
355 *bcd_cur++ |= 0xf0;
356
357 /* return how many bytes we used */
358 return (bcd_cur - bcd_lv);
359}
360
Harald Welte0c389302009-06-10 12:08:54 +0800361/* decode 'bearer capability' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800362static int decode_bearer_cap(struct gsm_mncc_bearer_cap *bcap,
Harald Welte0c389302009-06-10 12:08:54 +0800363 const u_int8_t *lv)
364{
365 u_int8_t in_len = lv[0];
366 int i, s;
367
368 if (in_len < 1)
369 return -EINVAL;
370
Harald Welte4bfdfe72009-06-10 23:11:52 +0800371 bcap->speech_ver[0] = -1; /* end of list, of maximum 7 values */
Harald Welte0c389302009-06-10 12:08:54 +0800372
373 /* octet 3 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800374 bcap->transfer = lv[1] & 0x07;
375 bcap->mode = (lv[1] & 0x08) >> 3;
376 bcap->coding = (lv[1] & 0x10) >> 4;
377 bcap->radio = (lv[1] & 0x60) >> 5;
Harald Welte0c389302009-06-10 12:08:54 +0800378
Harald Welte09b7e7f2009-12-12 21:36:53 +0100379 if (bcap->transfer == GSM_MNCC_BCAP_SPEECH) {
380 i = 1;
381 s = 0;
382 while(!(lv[i] & 0x80)) {
383 i++; /* octet 3a etc */
384 if (in_len < i)
385 return 0;
386 bcap->speech_ver[s++] = lv[i] & 0x0f;
387 bcap->speech_ver[s] = -1; /* end of list */
388 if (i == 2) /* octet 3a */
389 bcap->speech_ctm = (lv[i] & 0x20) >> 5;
390 if (s == 7) /* maximum speech versions + end of list */
391 return 0;
392 }
393 } else {
394 i = 1;
395 while (!(lv[i] & 0x80)) {
396 i++; /* octet 3a etc */
397 if (in_len < i)
398 return 0;
399 /* ignore them */
400 }
401 /* FIXME: implement OCTET 4+ parsing */
Harald Welte0c389302009-06-10 12:08:54 +0800402 }
403
404 return 0;
405}
406
407/* encode 'bearer capability' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800408static int encode_bearer_cap(struct msgb *msg, int lv_only,
409 const struct gsm_mncc_bearer_cap *bcap)
Harald Welte0c389302009-06-10 12:08:54 +0800410{
411 u_int8_t lv[32 + 1];
Harald Welte09b7e7f2009-12-12 21:36:53 +0100412 int i = 1, s;
Harald Welte0c389302009-06-10 12:08:54 +0800413
Harald Welte4bfdfe72009-06-10 23:11:52 +0800414 lv[1] = bcap->transfer;
415 lv[1] |= bcap->mode << 3;
416 lv[1] |= bcap->coding << 4;
417 lv[1] |= bcap->radio << 5;
Harald Welte0c389302009-06-10 12:08:54 +0800418
Harald Welte09b7e7f2009-12-12 21:36:53 +0100419 if (bcap->transfer == GSM_MNCC_BCAP_SPEECH) {
420 for (s = 0; bcap->speech_ver[s] >= 0; s++) {
421 i++; /* octet 3a etc */
422 lv[i] = bcap->speech_ver[s];
423 if (i == 2) /* octet 3a */
424 lv[i] |= bcap->speech_ctm << 5;
425 }
426 lv[i] |= 0x80; /* last IE of octet 3 etc */
427 } else {
428 /* FIXME: implement OCTET 4+ encoding */
Harald Welte0c389302009-06-10 12:08:54 +0800429 }
Harald Welte0c389302009-06-10 12:08:54 +0800430
431 lv[0] = i;
432 if (lv_only)
433 msgb_lv_put(msg, lv[0], lv+1);
434 else
435 msgb_tlv_put(msg, GSM48_IE_BEARER_CAP, lv[0], lv+1);
436
437 return 0;
438}
439
440/* decode 'call control cap' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800441static int decode_cccap(struct gsm_mncc_cccap *ccap, const u_int8_t *lv)
Harald Welte0c389302009-06-10 12:08:54 +0800442{
443 u_int8_t in_len = lv[0];
444
445 if (in_len < 1)
446 return -EINVAL;
447
448 /* octet 3 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800449 ccap->dtmf = lv[1] & 0x01;
450 ccap->pcp = (lv[1] & 0x02) >> 1;
Harald Welte0c389302009-06-10 12:08:54 +0800451
452 return 0;
453}
454
455/* decode 'called party BCD number' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800456static int decode_called(struct gsm_mncc_number *called,
457 const u_int8_t *lv)
Harald Welte0c389302009-06-10 12:08:54 +0800458{
459 u_int8_t in_len = lv[0];
460
461 if (in_len < 1)
462 return -EINVAL;
463
464 /* octet 3 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800465 called->plan = lv[1] & 0x0f;
466 called->type = (lv[1] & 0x70) >> 4;
Harald Welte0c389302009-06-10 12:08:54 +0800467
468 /* octet 4..N */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800469 decode_bcd_number(called->number, sizeof(called->number), lv, 1);
Harald Welte0c389302009-06-10 12:08:54 +0800470
471 return 0;
472}
473
474/* encode 'called party BCD number' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800475static int encode_called(struct msgb *msg,
476 const struct gsm_mncc_number *called)
Harald Welte0c389302009-06-10 12:08:54 +0800477{
478 u_int8_t lv[18];
479 int ret;
480
481 /* octet 3 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800482 lv[1] = called->plan;
483 lv[1] |= called->type << 4;
Harald Welte0c389302009-06-10 12:08:54 +0800484
485 /* octet 4..N, octet 2 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800486 ret = encode_bcd_number(lv, sizeof(lv), 1, called->number);
Harald Welte0c389302009-06-10 12:08:54 +0800487 if (ret < 0)
488 return ret;
489
490 msgb_tlv_put(msg, GSM48_IE_CALLED_BCD, lv[0], lv+1);
491
492 return 0;
493}
494
495/* encode callerid of various IEs */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800496static int encode_callerid(struct msgb *msg, int ie,
497 const struct gsm_mncc_number *callerid)
Harald Welte0c389302009-06-10 12:08:54 +0800498{
499 u_int8_t lv[13];
500 int h_len = 1;
501 int ret;
502
503 /* octet 3 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800504 lv[1] = callerid->plan;
505 lv[1] |= callerid->type << 4;
Harald Welte0c389302009-06-10 12:08:54 +0800506
Harald Welte4bfdfe72009-06-10 23:11:52 +0800507 if (callerid->present || callerid->screen) {
Harald Welte0c389302009-06-10 12:08:54 +0800508 /* octet 3a */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800509 lv[2] = callerid->screen;
510 lv[2] |= callerid->present << 5;
Harald Welte0c389302009-06-10 12:08:54 +0800511 lv[2] |= 0x80;
512 h_len++;
513 } else
514 lv[1] |= 0x80;
515
516 /* octet 4..N, octet 2 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800517 ret = encode_bcd_number(lv, sizeof(lv), h_len, callerid->number);
Harald Welte0c389302009-06-10 12:08:54 +0800518 if (ret < 0)
519 return ret;
520
521 msgb_tlv_put(msg, ie, lv[0], lv+1);
522
523 return 0;
524}
525
526/* decode 'cause' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800527static int decode_cause(struct gsm_mncc_cause *cause,
Harald Welte0c389302009-06-10 12:08:54 +0800528 const u_int8_t *lv)
529{
530 u_int8_t in_len = lv[0];
531 int i;
532
533 if (in_len < 2)
534 return -EINVAL;
535
Harald Welte4bfdfe72009-06-10 23:11:52 +0800536 cause->diag_len = 0;
Harald Welte0c389302009-06-10 12:08:54 +0800537
538 /* octet 3 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800539 cause->location = lv[1] & 0x0f;
540 cause->coding = (lv[1] & 0x60) >> 5;
Harald Welte0c389302009-06-10 12:08:54 +0800541
542 i = 1;
543 if (!(lv[i] & 0x80)) {
544 i++; /* octet 3a */
545 if (in_len < i+1)
546 return 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800547 cause->rec = 1;
548 cause->rec_val = lv[i] & 0x7f;
Harald Welte0c389302009-06-10 12:08:54 +0800549
550 }
551 i++;
552
553 /* octet 4 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800554 cause->value = lv[i] & 0x7f;
Harald Welte0c389302009-06-10 12:08:54 +0800555 i++;
556
557 if (in_len < i) /* no diag */
558 return 0;
559
560 if (in_len - (i-1) > 32) /* maximum 32 octets */
561 return 0;
562
563 /* octet 5-N */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800564 memcpy(cause->diag, lv + i, in_len - (i-1));
565 cause->diag_len = in_len - (i-1);
Harald Welte0c389302009-06-10 12:08:54 +0800566
567 return 0;
568}
569
570/* encode 'cause' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800571static int encode_cause(struct msgb *msg, int lv_only,
572 const struct gsm_mncc_cause *cause)
Harald Welte0c389302009-06-10 12:08:54 +0800573{
574 u_int8_t lv[32+4];
575 int i;
576
Harald Welte4bfdfe72009-06-10 23:11:52 +0800577 if (cause->diag_len > 32)
Harald Welte0c389302009-06-10 12:08:54 +0800578 return -EINVAL;
579
580 /* octet 3 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800581 lv[1] = cause->location;
582 lv[1] |= cause->coding << 5;
Harald Welte0c389302009-06-10 12:08:54 +0800583
584 i = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800585 if (cause->rec) {
Harald Welte0c389302009-06-10 12:08:54 +0800586 i++; /* octet 3a */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800587 lv[i] = cause->rec_val;
Harald Welte0c389302009-06-10 12:08:54 +0800588 }
589 lv[i] |= 0x80; /* end of octet 3 */
590
591 /* octet 4 */
592 i++;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800593 lv[i] = 0x80 | cause->value;
Harald Welte0c389302009-06-10 12:08:54 +0800594
595 /* octet 5-N */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800596 if (cause->diag_len) {
597 memcpy(lv + i, cause->diag, cause->diag_len);
598 i += cause->diag_len;
Harald Welte0c389302009-06-10 12:08:54 +0800599 }
600
601 lv[0] = i;
602 if (lv_only)
603 msgb_lv_put(msg, lv[0], lv+1);
604 else
605 msgb_tlv_put(msg, GSM48_IE_CAUSE, lv[0], lv+1);
606
607 return 0;
608}
609
610/* encode 'calling number' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800611static int encode_calling(struct msgb *msg,
612 const struct gsm_mncc_number *calling)
Harald Welte0c389302009-06-10 12:08:54 +0800613{
Harald Welte4bfdfe72009-06-10 23:11:52 +0800614 return encode_callerid(msg, GSM48_IE_CALLING_BCD, calling);
Harald Welte0c389302009-06-10 12:08:54 +0800615}
616
617/* encode 'connected number' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800618static int encode_connected(struct msgb *msg,
619 const struct gsm_mncc_number *connected)
Harald Welte0c389302009-06-10 12:08:54 +0800620{
Harald Welte4bfdfe72009-06-10 23:11:52 +0800621 return encode_callerid(msg, GSM48_IE_CONN_BCD, connected);
Harald Welte0c389302009-06-10 12:08:54 +0800622}
623
624/* encode 'redirecting number' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800625static int encode_redirecting(struct msgb *msg,
626 const struct gsm_mncc_number *redirecting)
Harald Welte0c389302009-06-10 12:08:54 +0800627{
Harald Welte4bfdfe72009-06-10 23:11:52 +0800628 return encode_callerid(msg, GSM48_IE_REDIR_BCD, redirecting);
Harald Welte0c389302009-06-10 12:08:54 +0800629}
630
631/* decode 'facility' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800632static int decode_facility(struct gsm_mncc_facility *facility,
Harald Welte0c389302009-06-10 12:08:54 +0800633 const u_int8_t *lv)
634{
635 u_int8_t in_len = lv[0];
636
637 if (in_len < 1)
638 return -EINVAL;
639
Harald Welte4bfdfe72009-06-10 23:11:52 +0800640 if (in_len > sizeof(facility->info))
Harald Welte0c389302009-06-10 12:08:54 +0800641 return -EINVAL;
642
Harald Welte4bfdfe72009-06-10 23:11:52 +0800643 memcpy(facility->info, lv+1, in_len);
644 facility->len = in_len;
Harald Welte0c389302009-06-10 12:08:54 +0800645
646 return 0;
647}
648
649/* encode 'facility' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800650static int encode_facility(struct msgb *msg, int lv_only,
651 const struct gsm_mncc_facility *facility)
Harald Welte0c389302009-06-10 12:08:54 +0800652{
653 u_int8_t lv[GSM_MAX_FACILITY + 1];
654
Harald Welte4bfdfe72009-06-10 23:11:52 +0800655 if (facility->len < 1 || facility->len > GSM_MAX_FACILITY)
Harald Welte0c389302009-06-10 12:08:54 +0800656 return -EINVAL;
657
Harald Welte4bfdfe72009-06-10 23:11:52 +0800658 memcpy(lv+1, facility->info, facility->len);
659 lv[0] = facility->len;
Harald Welte0c389302009-06-10 12:08:54 +0800660 if (lv_only)
661 msgb_lv_put(msg, lv[0], lv+1);
662 else
663 msgb_tlv_put(msg, GSM48_IE_FACILITY, lv[0], lv+1);
664
665 return 0;
666}
667
668/* decode 'notify' */
669static int decode_notify(int *notify, const u_int8_t *v)
670{
671 *notify = v[0] & 0x7f;
672
673 return 0;
674}
675
676/* encode 'notify' */
677static int encode_notify(struct msgb *msg, int notify)
678{
679 msgb_v_put(msg, notify | 0x80);
680
681 return 0;
682}
683
684/* encode 'signal' */
685static int encode_signal(struct msgb *msg, int signal)
686{
687 msgb_tv_put(msg, GSM48_IE_SIGNAL, signal);
688
689 return 0;
690}
691
692/* decode 'keypad' */
693static int decode_keypad(int *keypad, const u_int8_t *lv)
694{
695 u_int8_t in_len = lv[0];
696
697 if (in_len < 1)
698 return -EINVAL;
699
700 *keypad = lv[1] & 0x7f;
701
702 return 0;
703}
704
705/* encode 'keypad' */
706static int encode_keypad(struct msgb *msg, int keypad)
707{
708 msgb_tv_put(msg, GSM48_IE_KPD_FACILITY, keypad);
709
710 return 0;
711}
712
713/* decode 'progress' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800714static int decode_progress(struct gsm_mncc_progress *progress,
Harald Welte0c389302009-06-10 12:08:54 +0800715 const u_int8_t *lv)
716{
717 u_int8_t in_len = lv[0];
718
719 if (in_len < 2)
720 return -EINVAL;
721
Harald Welte4bfdfe72009-06-10 23:11:52 +0800722 progress->coding = (lv[1] & 0x60) >> 5;
723 progress->location = lv[1] & 0x0f;
724 progress->descr = lv[2] & 0x7f;
Harald Welte0c389302009-06-10 12:08:54 +0800725
726 return 0;
727}
728
729/* encode 'progress' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800730static int encode_progress(struct msgb *msg, int lv_only,
731 const struct gsm_mncc_progress *p)
Harald Welte0c389302009-06-10 12:08:54 +0800732{
733 u_int8_t lv[3];
734
735 lv[0] = 2;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800736 lv[1] = 0x80 | ((p->coding & 0x3) << 5) | (p->location & 0xf);
737 lv[2] = 0x80 | (p->descr & 0x7f);
Harald Welte0c389302009-06-10 12:08:54 +0800738 if (lv_only)
739 msgb_lv_put(msg, lv[0], lv+1);
740 else
741 msgb_tlv_put(msg, GSM48_IE_PROGR_IND, lv[0], lv+1);
742
743 return 0;
744}
745
746/* decode 'user-user' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800747static int decode_useruser(struct gsm_mncc_useruser *uu,
Harald Welte0c389302009-06-10 12:08:54 +0800748 const u_int8_t *lv)
749{
750 u_int8_t in_len = lv[0];
Harald Welte4bfdfe72009-06-10 23:11:52 +0800751 char *info = uu->info;
752 int info_len = sizeof(uu->info);
Harald Welte0c389302009-06-10 12:08:54 +0800753 int i;
754
755 if (in_len < 1)
756 return -EINVAL;
757
Harald Welte4bfdfe72009-06-10 23:11:52 +0800758 uu->proto = lv[1];
Harald Welte0c389302009-06-10 12:08:54 +0800759
760 for (i = 2; i <= in_len; i++) {
761 info_len--;
762 if (info_len <= 1)
763 break;
764 *info++ = lv[i];
765 }
766 if (info_len >= 1)
767 *info++ = '\0';
768
769 return 0;
770}
771
772/* encode 'useruser' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800773static int encode_useruser(struct msgb *msg, int lv_only,
774 const struct gsm_mncc_useruser *uu)
Harald Welte0c389302009-06-10 12:08:54 +0800775{
776 u_int8_t lv[GSM_MAX_USERUSER + 2];
777
Harald Welte4bfdfe72009-06-10 23:11:52 +0800778 if (strlen(uu->info) > GSM_MAX_USERUSER)
Harald Welte0c389302009-06-10 12:08:54 +0800779 return -EINVAL;
780
Harald Welte4bfdfe72009-06-10 23:11:52 +0800781 lv[0] = 1 + strlen(uu->info);
782 lv[1] = uu->proto;
783 memcpy(lv + 2, uu->info, strlen(uu->info));
Harald Welte0c389302009-06-10 12:08:54 +0800784 if (lv_only)
785 msgb_lv_put(msg, lv[0], lv+1);
786 else
787 msgb_tlv_put(msg, GSM48_IE_USER_USER, lv[0], lv+1);
788
789 return 0;
790}
791
792/* decode 'ss version' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800793static int decode_ssversion(struct gsm_mncc_ssversion *ssv,
Harald Welte0c389302009-06-10 12:08:54 +0800794 const u_int8_t *lv)
795{
796 u_int8_t in_len = lv[0];
797
Harald Welte4bfdfe72009-06-10 23:11:52 +0800798 if (in_len < 1 || in_len < sizeof(ssv->info))
Harald Welte0c389302009-06-10 12:08:54 +0800799 return -EINVAL;
800
Harald Welte4bfdfe72009-06-10 23:11:52 +0800801 memcpy(ssv->info, lv + 1, in_len);
802 ssv->len = in_len;
Harald Welte0c389302009-06-10 12:08:54 +0800803
804 return 0;
805}
806
807/* encode 'more data' */
808static int encode_more(struct msgb *msg)
809{
810 u_int8_t *ie;
811
812 ie = msgb_put(msg, 1);
813 ie[0] = GSM48_IE_MORE_DATA;
814
815 return 0;
816}
817
Holger Freyther429e7762008-12-30 13:28:30 +0000818/* Chapter 9.2.14 : Send LOCATION UPDATING REJECT */
Harald Welte8470bf22008-12-25 23:28:35 +0000819int gsm0408_loc_upd_rej(struct gsm_lchan *lchan, u_int8_t cause)
Harald Welte52b1f982008-12-23 20:25:15 +0000820{
Harald Welte24ff6ee2009-12-22 00:41:05 +0100821 struct gsm_bts *bts = lchan->ts->trx->bts;
Harald Welte8470bf22008-12-25 23:28:35 +0000822 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000823 struct gsm48_hdr *gh;
824
Harald Welte8470bf22008-12-25 23:28:35 +0000825 msg->lchan = lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000826
827 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
828 gh->proto_discr = GSM48_PDISC_MM;
Harald Welte10b487b2008-12-27 19:53:37 +0000829 gh->msg_type = GSM48_MT_MM_LOC_UPD_REJECT;
Harald Welte52b1f982008-12-23 20:25:15 +0000830 gh->data[0] = cause;
831
Harald Weltedb253af2008-12-30 17:56:55 +0000832 DEBUGP(DMM, "-> LOCATION UPDATING REJECT on channel: %d\n", lchan->nr);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100833
Harald Welteffa55a42009-12-22 19:07:32 +0100834 counter_inc(bts->network->stats.loc_upd_resp.reject);
Harald Weltedb253af2008-12-30 17:56:55 +0000835
Harald Welte39e2ead2009-07-23 21:13:03 +0200836 return gsm48_sendmsg(msg, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000837}
838
839/* Chapter 9.2.13 : Send LOCATION UPDATE ACCEPT */
Harald Welte75a983f2008-12-27 21:34:06 +0000840int gsm0408_loc_upd_acc(struct gsm_lchan *lchan, u_int32_t tmsi)
Harald Welte52b1f982008-12-23 20:25:15 +0000841{
Harald Welte8470bf22008-12-25 23:28:35 +0000842 struct gsm_bts *bts = lchan->ts->trx->bts;
843 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000844 struct gsm48_hdr *gh;
845 struct gsm48_loc_area_id *lai;
846 u_int8_t *mid;
847
Harald Welte8470bf22008-12-25 23:28:35 +0000848 msg->lchan = lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000849
850 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
851 gh->proto_discr = GSM48_PDISC_MM;
852 gh->msg_type = GSM48_MT_MM_LOC_UPD_ACCEPT;
853
854 lai = (struct gsm48_loc_area_id *) msgb_put(msg, sizeof(*lai));
Holger Freyther17746612008-12-28 16:32:44 +0000855 gsm0408_generate_lai(lai, bts->network->country_code,
Harald Welte52b1f982008-12-23 20:25:15 +0000856 bts->network->network_code, bts->location_area_code);
857
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +0200858 mid = msgb_put(msg, GSM48_MID_TMSI_LEN);
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +0200859 gsm48_generate_mid_from_tmsi(mid, tmsi);
Harald Welte52b1f982008-12-23 20:25:15 +0000860
861 DEBUGP(DMM, "-> LOCATION UPDATE ACCEPT\n");
862
Harald Welteffa55a42009-12-22 19:07:32 +0100863 counter_inc(bts->network->stats.loc_upd_resp.accept);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100864
Harald Welteb83d9382009-12-12 21:00:48 +0100865 return gsm48_sendmsg(msg, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000866}
867
Harald Weltebf5e8df2009-02-03 12:59:45 +0000868/* Transmit Chapter 9.2.10 Identity Request */
Harald Welte231ad4f2008-12-27 11:15:38 +0000869static int mm_tx_identity_req(struct gsm_lchan *lchan, u_int8_t id_type)
870{
871 struct msgb *msg = gsm48_msgb_alloc();
872 struct gsm48_hdr *gh;
Harald Weltefc977a82008-12-27 10:19:37 +0000873
Harald Welte231ad4f2008-12-27 11:15:38 +0000874 msg->lchan = lchan;
875
876 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
877 gh->proto_discr = GSM48_PDISC_MM;
878 gh->msg_type = GSM48_MT_MM_ID_REQ;
879 gh->data[0] = id_type;
880
Harald Welte39e2ead2009-07-23 21:13:03 +0200881 return gsm48_sendmsg(msg, NULL);
Harald Welte231ad4f2008-12-27 11:15:38 +0000882}
883
Harald Welte231ad4f2008-12-27 11:15:38 +0000884
Harald Weltebf5e8df2009-02-03 12:59:45 +0000885/* Parse Chapter 9.2.11 Identity Response */
Harald Welte231ad4f2008-12-27 11:15:38 +0000886static int mm_rx_id_resp(struct msgb *msg)
887{
888 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte75a983f2008-12-27 21:34:06 +0000889 struct gsm_lchan *lchan = msg->lchan;
Harald Welte9176bd42009-07-23 18:46:00 +0200890 struct gsm_bts *bts = lchan->ts->trx->bts;
891 struct gsm_network *net = bts->network;
Harald Welte231ad4f2008-12-27 11:15:38 +0000892 u_int8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200893 char mi_string[GSM48_MI_SIZE];
Harald Welte231ad4f2008-12-27 11:15:38 +0000894
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200895 gsm48_mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
Harald Welte61253062008-12-27 11:25:50 +0000896 DEBUGP(DMM, "IDENTITY RESPONSE: mi_type=0x%02x MI(%s)\n",
Harald Welte231ad4f2008-12-27 11:15:38 +0000897 mi_type, mi_string);
898
Harald Welte7659de12009-12-13 12:39:18 +0100899 dispatch_signal(SS_SUBSCR, S_SUBSCR_IDENTITY, gh->data);
900
Harald Welte75a983f2008-12-27 21:34:06 +0000901 switch (mi_type) {
902 case GSM_MI_TYPE_IMSI:
Jan Luebbe370b41d2009-08-12 10:19:34 +0200903 /* look up subscriber based on IMSI, create if not found */
904 if (!lchan->subscr) {
905 lchan->subscr = subscr_get_by_imsi(net, mi_string);
Harald Welte (local)aa9dc192009-08-13 13:49:51 +0200906 if (!lchan->subscr)
907 lchan->subscr = db_create_subscriber(net, mi_string);
Jan Luebbeb0dfc312009-08-12 10:12:52 +0200908 }
Holger Freyther73487a22008-12-31 18:53:57 +0000909 if (lchan->loc_operation)
910 lchan->loc_operation->waiting_for_imsi = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000911 break;
912 case GSM_MI_TYPE_IMEI:
Harald Welte255539c2008-12-28 02:26:27 +0000913 case GSM_MI_TYPE_IMEISV:
Harald Welte75a983f2008-12-27 21:34:06 +0000914 /* update subscribe <-> IMEI mapping */
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200915 if (lchan->subscr) {
Harald Welte75a983f2008-12-27 21:34:06 +0000916 db_subscriber_assoc_imei(lchan->subscr, mi_string);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200917 db_sync_equipment(&lchan->subscr->equipment);
918 }
Holger Freyther73487a22008-12-31 18:53:57 +0000919 if (lchan->loc_operation)
920 lchan->loc_operation->waiting_for_imei = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000921 break;
922 }
Holger Freyther73487a22008-12-31 18:53:57 +0000923
924 /* Check if we can let the mobile station enter */
Holger Freytherd51524f2009-06-09 08:27:07 +0000925 return gsm0408_authorize(lchan, msg);
Harald Welte231ad4f2008-12-27 11:15:38 +0000926}
927
Harald Welte255539c2008-12-28 02:26:27 +0000928
929static void loc_upd_rej_cb(void *data)
930{
931 struct gsm_lchan *lchan = data;
Harald Welte1085c092009-11-18 20:33:19 +0100932 struct gsm_bts *bts = lchan->ts->trx->bts;
Harald Welte255539c2008-12-28 02:26:27 +0000933
Holger Freyther73487a22008-12-31 18:53:57 +0000934 release_loc_updating_req(lchan);
Harald Welte1085c092009-11-18 20:33:19 +0100935 gsm0408_loc_upd_rej(lchan, bts->network->reject_cause);
Holger Freyther67b4b9a2009-01-01 03:46:11 +0000936 lchan_auto_release(lchan);
Harald Welte255539c2008-12-28 02:26:27 +0000937}
938
Holger Freytherb7193e42008-12-29 17:44:08 +0000939static void schedule_reject(struct gsm_lchan *lchan)
940{
Holger Freyther73487a22008-12-31 18:53:57 +0000941 lchan->loc_operation->updating_timer.cb = loc_upd_rej_cb;
942 lchan->loc_operation->updating_timer.data = lchan;
Harald Welteff117a82009-05-23 05:22:08 +0000943 bsc_schedule_timer(&lchan->loc_operation->updating_timer, 5, 0);
Holger Freytherb7193e42008-12-29 17:44:08 +0000944}
945
Harald Welte2a139372009-02-22 21:14:55 +0000946static const char *lupd_name(u_int8_t type)
947{
948 switch (type) {
949 case GSM48_LUPD_NORMAL:
950 return "NORMAL";
951 case GSM48_LUPD_PERIODIC:
952 return "PEROIDOC";
953 case GSM48_LUPD_IMSI_ATT:
954 return "IMSI ATTACH";
955 default:
956 return "UNKNOWN";
957 }
958}
959
Harald Weltebf5e8df2009-02-03 12:59:45 +0000960/* Chapter 9.2.15: Receive Location Updating Request */
Harald Welte231ad4f2008-12-27 11:15:38 +0000961static int mm_rx_loc_upd_req(struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000962{
Harald Welte8470bf22008-12-25 23:28:35 +0000963 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000964 struct gsm48_loc_upd_req *lu;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800965 struct gsm_subscriber *subscr = NULL;
Harald Welte255539c2008-12-28 02:26:27 +0000966 struct gsm_lchan *lchan = msg->lchan;
Harald Welte9176bd42009-07-23 18:46:00 +0200967 struct gsm_bts *bts = lchan->ts->trx->bts;
Harald Welte8470bf22008-12-25 23:28:35 +0000968 u_int8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200969 char mi_string[GSM48_MI_SIZE];
Harald Welte231ad4f2008-12-27 11:15:38 +0000970 int rc;
Harald Welte52b1f982008-12-23 20:25:15 +0000971
Harald Welte8470bf22008-12-25 23:28:35 +0000972 lu = (struct gsm48_loc_upd_req *) gh->data;
973
974 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
Harald Welte52b1f982008-12-23 20:25:15 +0000975
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200976 gsm48_mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
Harald Weltefc977a82008-12-27 10:19:37 +0000977
Harald Weltea0368542009-06-27 02:58:43 +0200978 DEBUGPC(DMM, "mi_type=0x%02x MI(%s) type=%s ", mi_type, mi_string,
Harald Welte2a139372009-02-22 21:14:55 +0000979 lupd_name(lu->type));
Holger Freyther73487a22008-12-31 18:53:57 +0000980
Harald Welte7659de12009-12-13 12:39:18 +0100981 dispatch_signal(SS_SUBSCR, S_SUBSCR_IDENTITY, &lu->mi_len);
982
Harald Welte24ff6ee2009-12-22 00:41:05 +0100983 switch (lu->type) {
984 case GSM48_LUPD_NORMAL:
Harald Welteffa55a42009-12-22 19:07:32 +0100985 counter_inc(bts->network->stats.loc_upd_type.normal);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100986 break;
987 case GSM48_LUPD_IMSI_ATT:
Harald Welteffa55a42009-12-22 19:07:32 +0100988 counter_inc(bts->network->stats.loc_upd_type.attach);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100989 break;
990 case GSM48_LUPD_PERIODIC:
Harald Welteffa55a42009-12-22 19:07:32 +0100991 counter_inc(bts->network->stats.loc_upd_type.periodic);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100992 break;
993 }
994
Holger Freythereaf04692009-06-06 13:54:44 +0000995 /*
996 * Pseudo Spoof detection: Just drop a second/concurrent
997 * location updating request.
998 */
999 if (lchan->loc_operation) {
Harald Weltea0368542009-06-27 02:58:43 +02001000 DEBUGPC(DMM, "ignoring request due an existing one: %p.\n",
Holger Freythereaf04692009-06-06 13:54:44 +00001001 lchan->loc_operation);
1002 gsm0408_loc_upd_rej(lchan, GSM48_REJECT_PROTOCOL_ERROR);
1003 return 0;
1004 }
1005
Holger Freyther73487a22008-12-31 18:53:57 +00001006 allocate_loc_updating_req(lchan);
1007
Harald Welte52b1f982008-12-23 20:25:15 +00001008 switch (mi_type) {
1009 case GSM_MI_TYPE_IMSI:
Harald Weltea0368542009-06-27 02:58:43 +02001010 DEBUGPC(DMM, "\n");
Harald Welte231ad4f2008-12-27 11:15:38 +00001011 /* we always want the IMEI, too */
Harald Welte015b9ad2009-02-28 18:22:03 +00001012 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMEI);
Holger Freyther73487a22008-12-31 18:53:57 +00001013 lchan->loc_operation->waiting_for_imei = 1;
Holger Freytherc6ea9db2008-12-30 19:18:21 +00001014
Jan Luebbe370b41d2009-08-12 10:19:34 +02001015 /* look up subscriber based on IMSI, create if not found */
1016 subscr = subscr_get_by_imsi(bts->network, mi_string);
1017 if (!subscr) {
1018 subscr = db_create_subscriber(bts->network, mi_string);
1019 }
Harald Welte4b634542008-12-27 01:55:51 +00001020 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001021 case GSM_MI_TYPE_TMSI:
Harald Weltea0368542009-06-27 02:58:43 +02001022 DEBUGPC(DMM, "\n");
Harald Welte231ad4f2008-12-27 11:15:38 +00001023 /* we always want the IMEI, too */
Harald Welte015b9ad2009-02-28 18:22:03 +00001024 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMEI);
Holger Freyther73487a22008-12-31 18:53:57 +00001025 lchan->loc_operation->waiting_for_imei = 1;
Holger Freytherc6ea9db2008-12-30 19:18:21 +00001026
Harald Welte52b1f982008-12-23 20:25:15 +00001027 /* look up the subscriber based on TMSI, request IMSI if it fails */
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +02001028 subscr = subscr_get_by_tmsi(bts->network,
1029 tmsi_from_string(mi_string));
Harald Welte52b1f982008-12-23 20:25:15 +00001030 if (!subscr) {
Harald Welte231ad4f2008-12-27 11:15:38 +00001031 /* send IDENTITY REQUEST message to get IMSI */
Harald Welte255539c2008-12-28 02:26:27 +00001032 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMSI);
Holger Freyther73487a22008-12-31 18:53:57 +00001033 lchan->loc_operation->waiting_for_imsi = 1;
Harald Welte52b1f982008-12-23 20:25:15 +00001034 }
1035 break;
1036 case GSM_MI_TYPE_IMEI:
1037 case GSM_MI_TYPE_IMEISV:
1038 /* no sim card... FIXME: what to do ? */
Harald Weltea0368542009-06-27 02:58:43 +02001039 DEBUGPC(DMM, "unimplemented mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +00001040 break;
1041 default:
Harald Weltea0368542009-06-27 02:58:43 +02001042 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +00001043 break;
1044 }
1045
Harald Welte24516ea2009-07-04 10:18:00 +02001046 /* schedule the reject timer */
1047 schedule_reject(lchan);
1048
Harald Welte4bfdfe72009-06-10 23:11:52 +08001049 if (!subscr) {
Harald Weltea0368542009-06-27 02:58:43 +02001050 DEBUGPC(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001051 /* FIXME: request id? close channel? */
1052 return -EINVAL;
1053 }
1054
Harald Welte255539c2008-12-28 02:26:27 +00001055 lchan->subscr = subscr;
Harald Welte (local)ee4410a2009-08-17 09:39:55 +02001056 lchan->subscr->equipment.classmark1 = lu->classmark1;
Harald Welte255539c2008-12-28 02:26:27 +00001057
Harald Welte24516ea2009-07-04 10:18:00 +02001058 /* check if we can let the subscriber into our network immediately
1059 * or if we need to wait for identity responses. */
Holger Freytherd51524f2009-06-09 08:27:07 +00001060 return gsm0408_authorize(lchan, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001061}
1062
Harald Welte4bfdfe72009-06-10 23:11:52 +08001063#if 0
1064static u_int8_t to_bcd8(u_int8_t val)
1065{
1066 return ((val / 10) << 4) | (val % 10);
1067}
1068#endif
1069
Harald Weltedb253af2008-12-30 17:56:55 +00001070/* Section 9.2.15a */
1071int gsm48_tx_mm_info(struct gsm_lchan *lchan)
1072{
1073 struct msgb *msg = gsm48_msgb_alloc();
1074 struct gsm48_hdr *gh;
1075 struct gsm_network *net = lchan->ts->trx->bts->network;
Harald Weltedb253af2008-12-30 17:56:55 +00001076 u_int8_t *ptr8;
Daniel Willmanneea93372009-08-13 03:42:07 +02001077 int name_len, name_pad;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001078#if 0
1079 time_t cur_t;
1080 struct tm* cur_time;
1081 int tz15min;
1082#endif
Harald Weltedb253af2008-12-30 17:56:55 +00001083
1084 msg->lchan = lchan;
1085
1086 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1087 gh->proto_discr = GSM48_PDISC_MM;
1088 gh->msg_type = GSM48_MT_MM_INFO;
1089
1090 if (net->name_long) {
Daniel Willmanneea93372009-08-13 03:42:07 +02001091#if 0
Harald Weltedb253af2008-12-30 17:56:55 +00001092 name_len = strlen(net->name_long);
1093 /* 10.5.3.5a */
1094 ptr8 = msgb_put(msg, 3);
1095 ptr8[0] = GSM48_IE_NAME_LONG;
1096 ptr8[1] = name_len*2 +1;
1097 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
1098
1099 ptr16 = (u_int16_t *) msgb_put(msg, name_len*2);
1100 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +00001101 ptr16[i] = htons(net->name_long[i]);
Harald Weltedb253af2008-12-30 17:56:55 +00001102
1103 /* FIXME: Use Cell Broadcast, not UCS-2, since
1104 * UCS-2 is only supported by later revisions of the spec */
Daniel Willmanneea93372009-08-13 03:42:07 +02001105#endif
1106 name_len = (strlen(net->name_long)*7)/8;
1107 name_pad = (8 - strlen(net->name_long)*7)%8;
1108 if (name_pad > 0)
1109 name_len++;
1110 /* 10.5.3.5a */
1111 ptr8 = msgb_put(msg, 3);
1112 ptr8[0] = GSM48_IE_NAME_LONG;
1113 ptr8[1] = name_len +1;
1114 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
1115
1116 ptr8 = msgb_put(msg, name_len);
1117 gsm_7bit_encode(ptr8, net->name_long);
1118
Harald Weltedb253af2008-12-30 17:56:55 +00001119 }
1120
1121 if (net->name_short) {
Daniel Willmanneea93372009-08-13 03:42:07 +02001122#if 0
Harald Weltedb253af2008-12-30 17:56:55 +00001123 name_len = strlen(net->name_short);
1124 /* 10.5.3.5a */
1125 ptr8 = (u_int8_t *) msgb_put(msg, 3);
Harald Welte7543eb72009-07-19 17:51:36 +02001126 ptr8[0] = GSM48_IE_NAME_SHORT;
Harald Weltedb253af2008-12-30 17:56:55 +00001127 ptr8[1] = name_len*2 + 1;
1128 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
1129
Harald Weltee872cb12009-01-01 00:33:37 +00001130 ptr16 = (u_int16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +00001131 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +00001132 ptr16[i] = htons(net->name_short[i]);
Daniel Willmanneea93372009-08-13 03:42:07 +02001133#endif
1134 name_len = (strlen(net->name_short)*7)/8;
1135 name_pad = (8 - strlen(net->name_short)*7)%8;
1136 if (name_pad > 0)
1137 name_len++;
1138 /* 10.5.3.5a */
1139 ptr8 = (u_int8_t *) msgb_put(msg, 3);
1140 ptr8[0] = GSM48_IE_NAME_SHORT;
1141 ptr8[1] = name_len +1;
1142 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
1143
1144 ptr8 = msgb_put(msg, name_len);
1145 gsm_7bit_encode(ptr8, net->name_short);
1146
Harald Weltedb253af2008-12-30 17:56:55 +00001147 }
1148
1149#if 0
1150 /* Section 10.5.3.9 */
1151 cur_t = time(NULL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001152 cur_time = gmtime(&cur_t);
Harald Weltedb253af2008-12-30 17:56:55 +00001153 ptr8 = msgb_put(msg, 8);
1154 ptr8[0] = GSM48_IE_NET_TIME_TZ;
1155 ptr8[1] = to_bcd8(cur_time->tm_year % 100);
1156 ptr8[2] = to_bcd8(cur_time->tm_mon);
1157 ptr8[3] = to_bcd8(cur_time->tm_mday);
1158 ptr8[4] = to_bcd8(cur_time->tm_hour);
1159 ptr8[5] = to_bcd8(cur_time->tm_min);
1160 ptr8[6] = to_bcd8(cur_time->tm_sec);
1161 /* 02.42: coded as BCD encoded signed value in units of 15 minutes */
1162 tz15min = (cur_time->tm_gmtoff)/(60*15);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001163 ptr8[7] = to_bcd8(tz15min);
Harald Weltedb253af2008-12-30 17:56:55 +00001164 if (tz15min < 0)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001165 ptr8[7] |= 0x80;
Harald Weltedb253af2008-12-30 17:56:55 +00001166#endif
1167
Daniel Willmanneea93372009-08-13 03:42:07 +02001168 DEBUGP(DMM, "-> MM INFO\n");
1169
Harald Welte39e2ead2009-07-23 21:13:03 +02001170 return gsm48_sendmsg(msg, NULL);
Harald Weltedb253af2008-12-30 17:56:55 +00001171}
1172
Harald Welte7984d5c2009-08-12 22:56:50 +02001173/* Section 9.2.2 */
1174int gsm48_tx_mm_auth_req(struct gsm_lchan *lchan, u_int8_t *rand)
1175{
1176 struct msgb *msg = gsm48_msgb_alloc();
1177 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Sylvain Munaut849f5542009-09-27 11:10:17 +02001178 struct gsm48_auth_req *ar = (struct gsm48_auth_req *) msgb_put(msg, sizeof(*ar));
Harald Welte7984d5c2009-08-12 22:56:50 +02001179
1180 DEBUGP(DMM, "-> AUTH REQ\n");
1181
1182 msg->lchan = lchan;
1183 gh->proto_discr = GSM48_PDISC_MM;
1184 gh->msg_type = GSM48_MT_MM_AUTH_REQ;
1185
Sylvain Munaut849f5542009-09-27 11:10:17 +02001186 /* Key Sequence: FIXME fixed to 0 */
1187 ar->key_seq = 0;
1188
Harald Welte7984d5c2009-08-12 22:56:50 +02001189 /* 16 bytes RAND parameters */
Harald Welte7984d5c2009-08-12 22:56:50 +02001190 if (rand)
Sylvain Munaut849f5542009-09-27 11:10:17 +02001191 memcpy(ar->rand, rand, 16);
Harald Welte7984d5c2009-08-12 22:56:50 +02001192
1193 return gsm48_sendmsg(msg, NULL);
1194}
1195
1196/* Section 9.2.1 */
1197int gsm48_tx_mm_auth_rej(struct gsm_lchan *lchan)
1198{
1199 DEBUGP(DMM, "-> AUTH REJECT\n");
1200 return gsm48_tx_simple(lchan, GSM48_PDISC_MM, GSM48_MT_MM_AUTH_REJ);
1201}
1202
Harald Welte4b634542008-12-27 01:55:51 +00001203static int gsm48_tx_mm_serv_ack(struct gsm_lchan *lchan)
1204{
Harald Welte4b634542008-12-27 01:55:51 +00001205 DEBUGP(DMM, "-> CM SERVICE ACK\n");
Harald Welte65e74cc2008-12-29 01:55:35 +00001206 return gsm48_tx_simple(lchan, GSM48_PDISC_MM, GSM48_MT_MM_CM_SERV_ACC);
Harald Welte4b634542008-12-27 01:55:51 +00001207}
Harald Welteba4cf162009-01-10 01:49:35 +00001208
1209/* 9.2.6 CM service reject */
1210static int gsm48_tx_mm_serv_rej(struct gsm_lchan *lchan,
1211 enum gsm48_reject_value value)
1212{
1213 struct msgb *msg = gsm48_msgb_alloc();
1214 struct gsm48_hdr *gh;
1215
1216 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
1217
1218 msg->lchan = lchan;
1219 use_lchan(lchan);
1220
1221 gh->proto_discr = GSM48_PDISC_MM;
1222 gh->msg_type = GSM48_MT_MM_CM_SERV_REJ;
1223 gh->data[0] = value;
1224 DEBUGP(DMM, "-> CM SERVICE Reject cause: %d\n", value);
1225
Harald Welte39e2ead2009-07-23 21:13:03 +02001226 return gsm48_sendmsg(msg, NULL);
Harald Welteba4cf162009-01-10 01:49:35 +00001227}
1228
Harald Welte4ed0e922009-01-10 03:17:30 +00001229/*
1230 * Handle CM Service Requests
1231 * a) Verify that the packet is long enough to contain the information
1232 * we require otherwsie reject with INCORRECT_MESSAGE
1233 * b) Try to parse the TMSI. If we do not have one reject
1234 * c) Check that we know the subscriber with the TMSI otherwise reject
1235 * with a HLR cause
1236 * d) Set the subscriber on the gsm_lchan and accept
1237 */
Harald Welte4b634542008-12-27 01:55:51 +00001238static int gsm48_rx_mm_serv_req(struct msgb *msg)
1239{
Harald Welteba4cf162009-01-10 01:49:35 +00001240 u_int8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001241 char mi_string[GSM48_MI_SIZE];
Harald Welte4b634542008-12-27 01:55:51 +00001242
Harald Welte9176bd42009-07-23 18:46:00 +02001243 struct gsm_bts *bts = msg->lchan->ts->trx->bts;
Harald Welteba4cf162009-01-10 01:49:35 +00001244 struct gsm_subscriber *subscr;
1245 struct gsm48_hdr *gh = msgb_l3(msg);
1246 struct gsm48_service_request *req =
1247 (struct gsm48_service_request *)gh->data;
Harald Weltec9e02182009-05-01 19:07:53 +00001248 /* unfortunately in Phase1 the classmar2 length is variable */
1249 u_int8_t classmark2_len = gh->data[1];
1250 u_int8_t *classmark2 = gh->data+2;
1251 u_int8_t mi_len = *(classmark2 + classmark2_len);
1252 u_int8_t *mi = (classmark2 + classmark2_len + 1);
Harald Welteba4cf162009-01-10 01:49:35 +00001253
Harald Weltec9e02182009-05-01 19:07:53 +00001254 DEBUGP(DMM, "<- CM SERVICE REQUEST ");
Harald Welteba4cf162009-01-10 01:49:35 +00001255 if (msg->data_len < sizeof(struct gsm48_service_request*)) {
Harald Weltec9e02182009-05-01 19:07:53 +00001256 DEBUGPC(DMM, "wrong sized message\n");
Harald Welteba4cf162009-01-10 01:49:35 +00001257 return gsm48_tx_mm_serv_rej(msg->lchan,
1258 GSM48_REJECT_INCORRECT_MESSAGE);
1259 }
1260
1261 if (msg->data_len < req->mi_len + 6) {
Harald Weltec9e02182009-05-01 19:07:53 +00001262 DEBUGPC(DMM, "does not fit in packet\n");
Harald Welteba4cf162009-01-10 01:49:35 +00001263 return gsm48_tx_mm_serv_rej(msg->lchan,
1264 GSM48_REJECT_INCORRECT_MESSAGE);
1265 }
1266
Harald Weltec9e02182009-05-01 19:07:53 +00001267 mi_type = mi[0] & GSM_MI_TYPE_MASK;
Harald Welteba4cf162009-01-10 01:49:35 +00001268 if (mi_type != GSM_MI_TYPE_TMSI) {
Harald Weltec9e02182009-05-01 19:07:53 +00001269 DEBUGPC(DMM, "mi_type is not TMSI: %d\n", mi_type);
Harald Welteba4cf162009-01-10 01:49:35 +00001270 return gsm48_tx_mm_serv_rej(msg->lchan,
1271 GSM48_REJECT_INCORRECT_MESSAGE);
1272 }
1273
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001274 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
Harald Weltec9e02182009-05-01 19:07:53 +00001275 DEBUGPC(DMM, "serv_type=0x%02x mi_type=0x%02x M(%s)\n",
Harald Welte4ed0e922009-01-10 03:17:30 +00001276 req->cm_service_type, mi_type, mi_string);
Harald Weltebcae43f2008-12-27 21:45:37 +00001277
Harald Welte7659de12009-12-13 12:39:18 +01001278 dispatch_signal(SS_SUBSCR, S_SUBSCR_IDENTITY, (classmark2 + classmark2_len));
1279
Harald Welte3ac7f102009-08-10 10:12:45 +02001280 if (is_siemens_bts(bts))
1281 send_siemens_mrpci(msg->lchan, classmark2-1);
1282
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +02001283 subscr = subscr_get_by_tmsi(bts->network,
1284 tmsi_from_string(mi_string));
Holger Freythereb443982009-06-04 13:58:42 +00001285
Harald Welte2a139372009-02-22 21:14:55 +00001286 /* FIXME: if we don't know the TMSI, inquire abit IMSI and allocate new TMSI */
Harald Welte4ed0e922009-01-10 03:17:30 +00001287 if (!subscr)
1288 return gsm48_tx_mm_serv_rej(msg->lchan,
1289 GSM48_REJECT_IMSI_UNKNOWN_IN_HLR);
1290
1291 if (!msg->lchan->subscr)
1292 msg->lchan->subscr = subscr;
Sylvain Munautea3f6742009-12-18 18:28:10 +01001293 else if (msg->lchan->subscr == subscr)
1294 subscr_put(subscr); /* lchan already has a ref, don't need another one */
1295 else {
Harald Welte9bb7c702009-01-10 03:21:41 +00001296 DEBUGP(DMM, "<- CM Channel already owned by someone else?\n");
1297 subscr_put(subscr);
1298 }
1299
Harald Weltec2e302d2009-07-05 14:08:13 +02001300 subscr->equipment.classmark2_len = classmark2_len;
1301 memcpy(subscr->equipment.classmark2, classmark2, classmark2_len);
1302 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +00001303
Harald Welte4b634542008-12-27 01:55:51 +00001304 return gsm48_tx_mm_serv_ack(msg->lchan);
1305}
1306
Harald Welte2a139372009-02-22 21:14:55 +00001307static int gsm48_rx_mm_imsi_detach_ind(struct msgb *msg)
1308{
Harald Welte9176bd42009-07-23 18:46:00 +02001309 struct gsm_bts *bts = msg->lchan->ts->trx->bts;
Harald Welte2a139372009-02-22 21:14:55 +00001310 struct gsm48_hdr *gh = msgb_l3(msg);
1311 struct gsm48_imsi_detach_ind *idi =
1312 (struct gsm48_imsi_detach_ind *) gh->data;
1313 u_int8_t mi_type = idi->mi[0] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001314 char mi_string[GSM48_MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +08001315 struct gsm_subscriber *subscr = NULL;
Harald Welte2a139372009-02-22 21:14:55 +00001316
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001317 gsm48_mi_to_string(mi_string, sizeof(mi_string), idi->mi, idi->mi_len);
Harald Welte2a139372009-02-22 21:14:55 +00001318 DEBUGP(DMM, "IMSI DETACH INDICATION: mi_type=0x%02x MI(%s): ",
1319 mi_type, mi_string);
1320
Harald Welteffa55a42009-12-22 19:07:32 +01001321 counter_inc(bts->network->stats.loc_upd_type.detach);
Harald Welte24ff6ee2009-12-22 00:41:05 +01001322
Harald Welte2a139372009-02-22 21:14:55 +00001323 switch (mi_type) {
1324 case GSM_MI_TYPE_TMSI:
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +02001325 subscr = subscr_get_by_tmsi(bts->network,
1326 tmsi_from_string(mi_string));
Harald Welte2a139372009-02-22 21:14:55 +00001327 break;
1328 case GSM_MI_TYPE_IMSI:
Harald Welte9176bd42009-07-23 18:46:00 +02001329 subscr = subscr_get_by_imsi(bts->network, mi_string);
Harald Welte2a139372009-02-22 21:14:55 +00001330 break;
1331 case GSM_MI_TYPE_IMEI:
1332 case GSM_MI_TYPE_IMEISV:
1333 /* no sim card... FIXME: what to do ? */
Holger Freyther79f4ae62009-06-02 03:25:04 +00001334 DEBUGPC(DMM, "unimplemented mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +00001335 break;
1336 default:
Holger Freyther79f4ae62009-06-02 03:25:04 +00001337 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +00001338 break;
1339 }
1340
Holger Freyther4a49e772009-04-12 05:37:29 +00001341 if (subscr) {
1342 subscr_update(subscr, msg->trx->bts,
1343 GSM_SUBSCRIBER_UPDATE_DETACHED);
Harald Welte2a139372009-02-22 21:14:55 +00001344 DEBUGP(DMM, "Subscriber: %s\n",
1345 subscr->name ? subscr->name : subscr->imsi);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +02001346
1347 subscr->equipment.classmark1 = idi->classmark1;
1348 db_sync_equipment(&subscr->equipment);
1349
Holger Freytherc21cfbc2009-06-02 02:54:57 +00001350 subscr_put(subscr);
Holger Freyther4a49e772009-04-12 05:37:29 +00001351 } else
Harald Welte2a139372009-02-22 21:14:55 +00001352 DEBUGP(DMM, "Unknown Subscriber ?!?\n");
1353
Harald Welte31981a02009-12-20 09:58:40 +01001354 /* FIXME: iterate over all transactions and release them,
1355 * imagine an IMSI DETACH happening during an active call! */
1356
Harald Welteb83d9382009-12-12 21:00:48 +01001357 /* subscriber is detached: should we release lchan? */
Harald Weltef7c28b02009-12-21 13:30:17 +01001358 lchan_auto_release(msg->lchan);
Harald Welteb83d9382009-12-12 21:00:48 +01001359
Harald Welte2a139372009-02-22 21:14:55 +00001360 return 0;
1361}
1362
Harald Welted2a7f5a2009-06-05 20:08:20 +00001363static int gsm48_rx_mm_status(struct msgb *msg)
1364{
1365 struct gsm48_hdr *gh = msgb_l3(msg);
1366
1367 DEBUGP(DMM, "MM STATUS (reject cause 0x%02x)\n", gh->data[0]);
1368
1369 return 0;
1370}
1371
Harald Weltebf5e8df2009-02-03 12:59:45 +00001372/* Receive a GSM 04.08 Mobility Management (MM) message */
Harald Welte52b1f982008-12-23 20:25:15 +00001373static int gsm0408_rcv_mm(struct msgb *msg)
1374{
1375 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001376 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001377
1378 switch (gh->msg_type & 0xbf) {
1379 case GSM48_MT_MM_LOC_UPD_REQUEST:
Harald Weltea0368542009-06-27 02:58:43 +02001380 DEBUGP(DMM, "LOCATION UPDATING REQUEST: ");
Harald Welte231ad4f2008-12-27 11:15:38 +00001381 rc = mm_rx_loc_upd_req(msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001382 break;
1383 case GSM48_MT_MM_ID_RESP:
Harald Welte231ad4f2008-12-27 11:15:38 +00001384 rc = mm_rx_id_resp(msg);
1385 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001386 case GSM48_MT_MM_CM_SERV_REQ:
Harald Welte4b634542008-12-27 01:55:51 +00001387 rc = gsm48_rx_mm_serv_req(msg);
1388 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001389 case GSM48_MT_MM_STATUS:
Harald Welted2a7f5a2009-06-05 20:08:20 +00001390 rc = gsm48_rx_mm_status(msg);
Harald Welte231ad4f2008-12-27 11:15:38 +00001391 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001392 case GSM48_MT_MM_TMSI_REALL_COMPL:
Harald Welte69b2af22009-01-06 19:47:00 +00001393 DEBUGP(DMM, "TMSI Reallocation Completed. Subscriber: %s\n",
1394 msg->lchan->subscr ?
1395 msg->lchan->subscr->imsi :
1396 "unknown subscriber");
1397 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001398 case GSM48_MT_MM_IMSI_DETACH_IND:
Harald Welte2a139372009-02-22 21:14:55 +00001399 rc = gsm48_rx_mm_imsi_detach_ind(msg);
1400 break;
1401 case GSM48_MT_MM_CM_REEST_REQ:
1402 DEBUGP(DMM, "CM REESTABLISH REQUEST: Not implemented\n");
1403 break;
1404 case GSM48_MT_MM_AUTH_RESP:
1405 DEBUGP(DMM, "AUTHENTICATION RESPONSE: Not implemented\n");
Harald Welte52b1f982008-12-23 20:25:15 +00001406 break;
1407 default:
1408 fprintf(stderr, "Unknown GSM 04.08 MM msg type 0x%02x\n",
1409 gh->msg_type);
1410 break;
1411 }
1412
1413 return rc;
1414}
Harald Weltebf5e8df2009-02-03 12:59:45 +00001415
Harald Welte2d35ae62009-02-06 12:02:13 +00001416/* Receive a PAGING RESPONSE message from the MS */
Holger Hans Peter Freytherca0fcbe2009-10-23 13:48:54 +02001417static int gsm48_rx_rr_pag_resp(struct msgb *msg)
Harald Welte2d35ae62009-02-06 12:02:13 +00001418{
Harald Welte9176bd42009-07-23 18:46:00 +02001419 struct gsm_bts *bts = msg->lchan->ts->trx->bts;
Harald Welte2d35ae62009-02-06 12:02:13 +00001420 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte61548982009-02-22 21:26:29 +00001421 u_int8_t *classmark2_lv = gh->data + 1;
Holger Hans Peter Freyther3ee5d3e2009-08-21 05:18:21 +02001422 u_int8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001423 char mi_string[GSM48_MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +08001424 struct gsm_subscriber *subscr = NULL;
Harald Welte2d35ae62009-02-06 12:02:13 +00001425 int rc = 0;
1426
Holger Hans Peter Freyther3ee5d3e2009-08-21 05:18:21 +02001427 gsm48_paging_extract_mi(msg, mi_string, &mi_type);
Harald Welte2d35ae62009-02-06 12:02:13 +00001428 DEBUGP(DRR, "PAGING RESPONSE: mi_type=0x%02x MI(%s)\n",
1429 mi_type, mi_string);
Harald Welte3ac7f102009-08-10 10:12:45 +02001430
Harald Weltefe18d8f2009-02-22 21:14:24 +00001431 switch (mi_type) {
1432 case GSM_MI_TYPE_TMSI:
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +02001433 subscr = subscr_get_by_tmsi(bts->network,
1434 tmsi_from_string(mi_string));
Harald Weltefe18d8f2009-02-22 21:14:24 +00001435 break;
1436 case GSM_MI_TYPE_IMSI:
Harald Welte9176bd42009-07-23 18:46:00 +02001437 subscr = subscr_get_by_imsi(bts->network, mi_string);
Harald Weltefe18d8f2009-02-22 21:14:24 +00001438 break;
1439 }
Harald Welte2d35ae62009-02-06 12:02:13 +00001440
1441 if (!subscr) {
1442 DEBUGP(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte09e38af2009-02-16 22:52:23 +00001443 /* FIXME: request id? close channel? */
Harald Welte2d35ae62009-02-06 12:02:13 +00001444 return -EINVAL;
1445 }
1446 DEBUGP(DRR, "<- Channel was requested by %s\n",
Harald Welte76042182009-08-08 16:03:15 +02001447 subscr->name && strlen(subscr->name) ? subscr->name : subscr->imsi);
Holger Freyther053e09d2009-02-14 22:51:06 +00001448
Harald Weltec2e302d2009-07-05 14:08:13 +02001449 subscr->equipment.classmark2_len = *classmark2_lv;
1450 memcpy(subscr->equipment.classmark2, classmark2_lv+1, *classmark2_lv);
1451 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +00001452
Holger Hans Peter Freyther3ee5d3e2009-08-21 05:18:21 +02001453 rc = gsm48_handle_paging_resp(msg, subscr);
Harald Welte2d35ae62009-02-06 12:02:13 +00001454 return rc;
1455}
1456
Harald Weltef7c43522009-06-09 20:24:21 +00001457static int gsm48_rx_rr_classmark(struct msgb *msg)
1458{
1459 struct gsm48_hdr *gh = msgb_l3(msg);
1460 struct gsm_subscriber *subscr = msg->lchan->subscr;
1461 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1462 u_int8_t cm2_len, cm3_len = 0;
1463 u_int8_t *cm2, *cm3 = NULL;
1464
1465 DEBUGP(DRR, "CLASSMARK CHANGE ");
1466
1467 /* classmark 2 */
1468 cm2_len = gh->data[0];
1469 cm2 = &gh->data[1];
1470 DEBUGPC(DRR, "CM2(len=%u) ", cm2_len);
1471
1472 if (payload_len > cm2_len + 1) {
1473 /* we must have a classmark3 */
1474 if (gh->data[cm2_len+1] != 0x20) {
1475 DEBUGPC(DRR, "ERR CM3 TAG\n");
1476 return -EINVAL;
1477 }
1478 if (cm2_len > 3) {
1479 DEBUGPC(DRR, "CM2 too long!\n");
1480 return -EINVAL;
1481 }
1482
1483 cm3_len = gh->data[cm2_len+2];
1484 cm3 = &gh->data[cm2_len+3];
1485 if (cm3_len > 14) {
1486 DEBUGPC(DRR, "CM3 len %u too long!\n", cm3_len);
1487 return -EINVAL;
1488 }
1489 DEBUGPC(DRR, "CM3(len=%u)\n", cm3_len);
1490 }
1491 if (subscr) {
Harald Weltec2e302d2009-07-05 14:08:13 +02001492 subscr->equipment.classmark2_len = cm2_len;
1493 memcpy(subscr->equipment.classmark2, cm2, cm2_len);
Harald Weltef7c43522009-06-09 20:24:21 +00001494 if (cm3) {
Harald Weltec2e302d2009-07-05 14:08:13 +02001495 subscr->equipment.classmark3_len = cm3_len;
1496 memcpy(subscr->equipment.classmark3, cm3, cm3_len);
Harald Weltef7c43522009-06-09 20:24:21 +00001497 }
Harald Weltec2e302d2009-07-05 14:08:13 +02001498 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +00001499 }
1500
Harald Weltef7c43522009-06-09 20:24:21 +00001501 return 0;
1502}
1503
Harald Weltecf5b3592009-05-01 18:28:42 +00001504static int gsm48_rx_rr_status(struct msgb *msg)
1505{
1506 struct gsm48_hdr *gh = msgb_l3(msg);
1507
1508 DEBUGP(DRR, "STATUS rr_cause = %s\n",
1509 rr_cause_name(gh->data[0]));
1510
1511 return 0;
1512}
1513
Harald Weltef7c43522009-06-09 20:24:21 +00001514static int gsm48_rx_rr_meas_rep(struct msgb *msg)
1515{
Harald Welted12b0fd2009-12-15 21:36:05 +01001516 struct gsm_meas_rep *meas_rep = lchan_next_meas_rep(msg->lchan);
Harald Weltef7c43522009-06-09 20:24:21 +00001517
Harald Welte3c7dc6e2009-11-29 19:07:28 +01001518 /* This shouldn't actually end up here, as RSL treats
1519 * L3 Info of 08.58 MEASUREMENT REPORT different by calling
1520 * directly into gsm48_parse_meas_rep */
1521 DEBUGP(DMEAS, "DIRECT GSM48 MEASUREMENT REPORT ?!? ");
Harald Welted12b0fd2009-12-15 21:36:05 +01001522 gsm48_parse_meas_rep(meas_rep, msg);
Harald Weltef7c43522009-06-09 20:24:21 +00001523
1524 return 0;
1525}
1526
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001527static int gsm48_rx_rr_app_info(struct msgb *msg)
1528{
1529 struct gsm48_hdr *gh = msgb_l3(msg);
1530 u_int8_t apdu_id_flags;
1531 u_int8_t apdu_len;
1532 u_int8_t *apdu_data;
1533
1534 apdu_id_flags = gh->data[0];
1535 apdu_len = gh->data[1];
1536 apdu_data = gh->data+2;
1537
1538 DEBUGP(DNM, "RX APPLICATION INFO id/flags=0x%02x apdu_len=%u apdu=%s",
1539 apdu_id_flags, apdu_len, hexdump(apdu_data, apdu_len));
1540
Harald Welte (local)026531e2009-08-16 10:40:10 +02001541 return db_apdu_blob_store(msg->lchan->subscr, apdu_id_flags, apdu_len, apdu_data);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001542}
1543
Harald Welted011e8b2009-11-29 22:45:52 +01001544/* Chapter 9.1.16 Handover complete */
Harald Welte1e191c52009-12-14 17:51:15 +01001545static int gsm48_rx_rr_ho_compl(struct msgb *msg)
Harald Welted011e8b2009-11-29 22:45:52 +01001546{
1547 struct gsm48_hdr *gh = msgb_l3(msg);
1548
1549 DEBUGP(DRR, "HANDOVER COMPLETE cause = %s\n",
1550 rr_cause_name(gh->data[0]));
1551
1552 dispatch_signal(SS_LCHAN, S_LCHAN_HANDOVER_COMPL, msg->lchan);
1553 /* FIXME: release old channel */
1554
1555 return 0;
1556}
1557
1558/* Chapter 9.1.17 Handover Failure */
Harald Welte1e191c52009-12-14 17:51:15 +01001559static int gsm48_rx_rr_ho_fail(struct msgb *msg)
Harald Welted011e8b2009-11-29 22:45:52 +01001560{
1561 struct gsm48_hdr *gh = msgb_l3(msg);
1562
1563 DEBUGP(DRR, "HANDOVER FAILED cause = %s\n",
1564 rr_cause_name(gh->data[0]));
1565
1566 dispatch_signal(SS_LCHAN, S_LCHAN_HANDOVER_FAIL, msg->lchan);
1567 /* FIXME: release allocated new channel */
1568
1569 return 0;
1570}
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001571
Harald Weltebf5e8df2009-02-03 12:59:45 +00001572/* Receive a GSM 04.08 Radio Resource (RR) message */
Harald Welte52b1f982008-12-23 20:25:15 +00001573static int gsm0408_rcv_rr(struct msgb *msg)
1574{
1575 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001576 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001577
1578 switch (gh->msg_type) {
1579 case GSM48_MT_RR_CLSM_CHG:
Harald Weltef7c43522009-06-09 20:24:21 +00001580 rc = gsm48_rx_rr_classmark(msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001581 break;
Harald Weltefc977a82008-12-27 10:19:37 +00001582 case GSM48_MT_RR_GPRS_SUSP_REQ:
1583 DEBUGP(DRR, "GRPS SUSPEND REQUEST\n");
1584 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001585 case GSM48_MT_RR_PAG_RESP:
Holger Hans Peter Freytherca0fcbe2009-10-23 13:48:54 +02001586 rc = gsm48_rx_rr_pag_resp(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001587 break;
Harald Welte7ccf7782009-02-17 01:43:01 +00001588 case GSM48_MT_RR_CHAN_MODE_MODIF_ACK:
Holger Hans Peter Freytherf520e642009-10-22 15:23:11 +02001589 rc = gsm48_rx_rr_modif_ack(msg);
Harald Welte7ccf7782009-02-17 01:43:01 +00001590 break;
Harald Weltecf5b3592009-05-01 18:28:42 +00001591 case GSM48_MT_RR_STATUS:
1592 rc = gsm48_rx_rr_status(msg);
1593 break;
Harald Weltef7c43522009-06-09 20:24:21 +00001594 case GSM48_MT_RR_MEAS_REP:
1595 rc = gsm48_rx_rr_meas_rep(msg);
1596 break;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001597 case GSM48_MT_RR_APP_INFO:
1598 rc = gsm48_rx_rr_app_info(msg);
1599 break;
Harald Welte08d91a52009-08-30 15:37:11 +09001600 case GSM48_MT_RR_CIPH_M_COMPL:
1601 DEBUGP(DRR, "CIPHERING MODE COMPLETE\n");
1602 /* FIXME: check for MI (if any) */
1603 break;
Harald Welteccd5a882009-11-29 20:02:20 +01001604 case GSM48_MT_RR_HANDO_COMPL:
Harald Welted011e8b2009-11-29 22:45:52 +01001605 rc = gsm48_rx_rr_ho_compl(msg);
Harald Welteccd5a882009-11-29 20:02:20 +01001606 break;
1607 case GSM48_MT_RR_HANDO_FAIL:
Harald Welted011e8b2009-11-29 22:45:52 +01001608 rc = gsm48_rx_rr_ho_fail(msg);
Harald Welteccd5a882009-11-29 20:02:20 +01001609 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001610 default:
Harald Welte2d35ae62009-02-06 12:02:13 +00001611 fprintf(stderr, "Unimplemented GSM 04.08 RR msg type 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +00001612 gh->msg_type);
1613 break;
1614 }
1615
Harald Welte2d35ae62009-02-06 12:02:13 +00001616 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +00001617}
1618
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001619int gsm48_send_rr_app_info(struct gsm_lchan *lchan, u_int8_t apdu_id,
Holger Hans Peter Freyther8b77a342009-10-22 15:42:19 +02001620 u_int8_t apdu_len, const u_int8_t *apdu)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001621{
1622 struct msgb *msg = gsm48_msgb_alloc();
1623 struct gsm48_hdr *gh;
1624
1625 msg->lchan = lchan;
1626
1627 DEBUGP(DRR, "TX APPLICATION INFO id=0x%02x, len=%u\n",
1628 apdu_id, apdu_len);
1629
1630 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 2 + apdu_len);
1631 gh->proto_discr = GSM48_PDISC_RR;
1632 gh->msg_type = GSM48_MT_RR_APP_INFO;
1633 gh->data[0] = apdu_id;
1634 gh->data[1] = apdu_len;
1635 memcpy(gh->data+2, apdu, apdu_len);
1636
1637 return gsm48_sendmsg(msg, NULL);
1638}
1639
Harald Welte4bc90a12008-12-27 16:32:52 +00001640/* Call Control */
1641
Harald Welte7584aea2009-02-11 11:44:12 +00001642/* The entire call control code is written in accordance with Figure 7.10c
1643 * for 'very early assignment', i.e. we allocate a TCH/F during IMMEDIATE
1644 * ASSIGN, then first use that TCH/F for signalling and later MODE MODIFY
1645 * it for voice */
1646
Harald Welte4bfdfe72009-06-10 23:11:52 +08001647static void new_cc_state(struct gsm_trans *trans, int state)
1648{
1649 if (state > 31 || state < 0)
1650 return;
1651
1652 DEBUGP(DCC, "new state %s -> %s\n",
Harald Weltedcaf5652009-07-23 18:56:43 +02001653 cc_state_names[trans->cc.state], cc_state_names[state]);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001654
Harald Weltedcaf5652009-07-23 18:56:43 +02001655 trans->cc.state = state;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001656}
1657
1658static int gsm48_cc_tx_status(struct gsm_trans *trans, void *arg)
Harald Welte4bc90a12008-12-27 16:32:52 +00001659{
1660 struct msgb *msg = gsm48_msgb_alloc();
1661 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1662 u_int8_t *cause, *call_state;
1663
Harald Welte4bc90a12008-12-27 16:32:52 +00001664 gh->msg_type = GSM48_MT_CC_STATUS;
1665
1666 cause = msgb_put(msg, 3);
1667 cause[0] = 2;
1668 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
1669 cause[2] = 0x80 | 30; /* response to status inquiry */
1670
1671 call_state = msgb_put(msg, 1);
1672 call_state[0] = 0xc0 | 0x00;
1673
Harald Welte39e2ead2009-07-23 21:13:03 +02001674 return gsm48_sendmsg(msg, trans);
Harald Welte4bc90a12008-12-27 16:32:52 +00001675}
1676
Harald Welte6f4b7532008-12-29 00:39:37 +00001677static int gsm48_tx_simple(struct gsm_lchan *lchan,
1678 u_int8_t pdisc, u_int8_t msg_type)
Harald Welte4bc90a12008-12-27 16:32:52 +00001679{
1680 struct msgb *msg = gsm48_msgb_alloc();
1681 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1682
1683 msg->lchan = lchan;
1684
Harald Welte6f4b7532008-12-29 00:39:37 +00001685 gh->proto_discr = pdisc;
Harald Welte4bc90a12008-12-27 16:32:52 +00001686 gh->msg_type = msg_type;
1687
Harald Welte39e2ead2009-07-23 21:13:03 +02001688 return gsm48_sendmsg(msg, NULL);
Harald Welte4bc90a12008-12-27 16:32:52 +00001689}
1690
Harald Welte4bfdfe72009-06-10 23:11:52 +08001691static void gsm48_stop_cc_timer(struct gsm_trans *trans)
1692{
Harald Weltedcaf5652009-07-23 18:56:43 +02001693 if (bsc_timer_pending(&trans->cc.timer)) {
1694 DEBUGP(DCC, "stopping pending timer T%x\n", trans->cc.Tcurrent);
1695 bsc_del_timer(&trans->cc.timer);
1696 trans->cc.Tcurrent = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001697 }
1698}
1699
1700static int mncc_recvmsg(struct gsm_network *net, struct gsm_trans *trans,
1701 int msg_type, struct gsm_mncc *mncc)
1702{
1703 struct msgb *msg;
1704
1705 if (trans)
1706 if (trans->lchan)
Harald Welte6f5aee02009-07-23 21:21:14 +02001707 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08001708 "Sending '%s' to MNCC.\n",
1709 trans->lchan->ts->trx->bts->nr,
1710 trans->lchan->ts->trx->nr,
1711 trans->lchan->ts->nr, trans->transaction_id,
1712 (trans->subscr)?(trans->subscr->extension):"-",
1713 get_mncc_name(msg_type));
1714 else
1715 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
1716 "Sending '%s' to MNCC.\n",
1717 (trans->subscr)?(trans->subscr->extension):"-",
1718 get_mncc_name(msg_type));
1719 else
1720 DEBUGP(DCC, "(bts - trx - ts - ti -- sub -) "
1721 "Sending '%s' to MNCC.\n", get_mncc_name(msg_type));
1722
1723 mncc->msg_type = msg_type;
1724
Harald Welte966636f2009-06-26 19:39:35 +02001725 msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001726 if (!msg)
1727 return -ENOMEM;
1728 memcpy(msg->data, mncc, sizeof(struct gsm_mncc));
1729 msgb_enqueue(&net->upqueue, msg);
1730
1731 return 0;
1732}
1733
1734int mncc_release_ind(struct gsm_network *net, struct gsm_trans *trans,
1735 u_int32_t callref, int location, int value)
1736{
1737 struct gsm_mncc rel;
1738
Harald Welte92f70c52009-06-12 01:54:08 +08001739 memset(&rel, 0, sizeof(rel));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001740 rel.callref = callref;
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001741 mncc_set_cause(&rel, location, value);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001742 return mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
1743}
1744
Harald Weltedcaf5652009-07-23 18:56:43 +02001745/* Call Control Specific transaction release.
1746 * gets called by trans_free, DO NOT CALL YOURSELF! */
1747void _gsm48_cc_trans_free(struct gsm_trans *trans)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001748{
Harald Welte4bfdfe72009-06-10 23:11:52 +08001749 gsm48_stop_cc_timer(trans);
1750
1751 /* send release to L4, if callref still exists */
1752 if (trans->callref) {
1753 /* Ressource unavailable */
Harald Welte596fed42009-07-23 19:06:52 +02001754 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001755 GSM48_CAUSE_LOC_PRN_S_LU,
1756 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001757 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001758 if (trans->cc.state != GSM_CSTATE_NULL)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001759 new_cc_state(trans, GSM_CSTATE_NULL);
Harald Weltedcaf5652009-07-23 18:56:43 +02001760 if (trans->lchan)
1761 trau_mux_unmap(&trans->lchan->ts->e1_link, trans->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001762}
1763
1764static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg);
1765
Harald Welte09e38af2009-02-16 22:52:23 +00001766/* call-back from paging the B-end of the connection */
1767static int setup_trig_pag_evt(unsigned int hooknum, unsigned int event,
Harald Welte7ccf7782009-02-17 01:43:01 +00001768 struct msgb *msg, void *_lchan, void *param)
Harald Welte09e38af2009-02-16 22:52:23 +00001769{
Harald Welte7ccf7782009-02-17 01:43:01 +00001770 struct gsm_lchan *lchan = _lchan;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001771 struct gsm_subscriber *subscr = param;
1772 struct gsm_trans *transt, *tmp;
1773 struct gsm_network *net;
Harald Weltec05677b2009-06-26 20:17:06 +02001774
Harald Welte09e38af2009-02-16 22:52:23 +00001775 if (hooknum != GSM_HOOK_RR_PAGING)
1776 return -EINVAL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001777
1778 if (!subscr)
1779 return -EINVAL;
1780 net = subscr->net;
1781 if (!net) {
1782 DEBUGP(DCC, "Error Network not set!\n");
1783 return -EINVAL;
Harald Welte5a065df2009-02-22 21:13:18 +00001784 }
Harald Welte7584aea2009-02-11 11:44:12 +00001785
Harald Welte4bfdfe72009-06-10 23:11:52 +08001786 /* check all tranactions (without lchan) for subscriber */
1787 llist_for_each_entry_safe(transt, tmp, &net->trans_list, entry) {
1788 if (transt->subscr != subscr || transt->lchan)
1789 continue;
1790 switch (event) {
1791 case GSM_PAGING_SUCCEEDED:
1792 if (!lchan) // paranoid
1793 break;
1794 DEBUGP(DCC, "Paging subscr %s succeeded!\n",
1795 subscr->extension);
1796 /* Assign lchan */
1797 if (!transt->lchan) {
1798 transt->lchan = lchan;
1799 use_lchan(lchan);
1800 }
1801 /* send SETUP request to called party */
Harald Weltedcaf5652009-07-23 18:56:43 +02001802 gsm48_cc_tx_setup(transt, &transt->cc.msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001803 break;
1804 case GSM_PAGING_EXPIRED:
1805 DEBUGP(DCC, "Paging subscr %s expired!\n",
1806 subscr->extension);
1807 /* Temporarily out of order */
Harald Welte596fed42009-07-23 19:06:52 +02001808 mncc_release_ind(transt->subscr->net, transt,
1809 transt->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001810 GSM48_CAUSE_LOC_PRN_S_LU,
1811 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001812 transt->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001813 trans_free(transt);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001814 break;
1815 }
1816 }
Harald Welte09e38af2009-02-16 22:52:23 +00001817 return 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00001818}
Harald Welte7584aea2009-02-11 11:44:12 +00001819
Harald Welteda7ab742009-12-19 22:23:05 +01001820static int tch_recv_mncc(struct gsm_network *net, u_int32_t callref, int enable);
1821
Harald Welte805f6442009-07-28 18:25:29 +02001822/* some other part of the code sends us a signal */
1823static int handle_abisip_signal(unsigned int subsys, unsigned int signal,
1824 void *handler_data, void *signal_data)
1825{
1826 struct gsm_lchan *lchan = signal_data;
Harald Welte805f6442009-07-28 18:25:29 +02001827 int rc;
Harald Welteda7ab742009-12-19 22:23:05 +01001828 struct gsm_network *net;
1829 struct gsm_trans *trans;
Harald Welte805f6442009-07-28 18:25:29 +02001830
1831 if (subsys != SS_ABISIP)
1832 return 0;
1833
1834 /* in case we use direct BTS-to-BTS RTP */
1835 if (ipacc_rtp_direct)
1836 return 0;
1837
Harald Welte805f6442009-07-28 18:25:29 +02001838 switch (signal) {
Holger Hans Peter Freyther231163d2009-11-18 21:06:12 +01001839 case S_ABISIP_CRCX_ACK:
Harald Welteda7ab742009-12-19 22:23:05 +01001840 /* check if any transactions on this lchan still have
1841 * a tch_recv_mncc request pending */
1842 net = lchan->ts->trx->bts->network;
1843 llist_for_each_entry(trans, &net->trans_list, entry) {
1844 if (trans->lchan == lchan && trans->tch_recv) {
1845 DEBUGP(DCC, "pending tch_recv_mncc request\n");
1846 tch_recv_mncc(net, trans->callref, 1);
1847 }
1848 }
Harald Welte805f6442009-07-28 18:25:29 +02001849 break;
Harald Welte805f6442009-07-28 18:25:29 +02001850 }
1851
1852 return 0;
Harald Welte805f6442009-07-28 18:25:29 +02001853}
1854
Harald Welte49f48b82009-02-17 15:29:33 +00001855/* map two ipaccess RTP streams onto each other */
Harald Welte11fa29c2009-02-19 17:24:39 +00001856static int tch_map(struct gsm_lchan *lchan, struct gsm_lchan *remote_lchan)
Harald Welte49f48b82009-02-17 15:29:33 +00001857{
Harald Welte11fa29c2009-02-19 17:24:39 +00001858 struct gsm_bts *bts = lchan->ts->trx->bts;
1859 struct gsm_bts *remote_bts = remote_lchan->ts->trx->bts;
Harald Welte805f6442009-07-28 18:25:29 +02001860 int rc;
Harald Welte49f48b82009-02-17 15:29:33 +00001861
Harald Welte11fa29c2009-02-19 17:24:39 +00001862 DEBUGP(DCC, "Setting up TCH map between (bts=%u,trx=%u,ts=%u) and (bts=%u,trx=%u,ts=%u)\n",
1863 bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
1864 remote_bts->nr, remote_lchan->ts->trx->nr, remote_lchan->ts->nr);
1865
1866 if (bts->type != remote_bts->type) {
1867 DEBUGP(DCC, "Cannot switch calls between different BTS types yet\n");
1868 return -EINVAL;
1869 }
Harald Welteda7ab742009-12-19 22:23:05 +01001870
1871 // todo: map between different bts types
Harald Welte11fa29c2009-02-19 17:24:39 +00001872 switch (bts->type) {
Mike Habene2d82272009-10-02 12:19:34 +01001873 case GSM_BTS_TYPE_NANOBTS:
Harald Welte805f6442009-07-28 18:25:29 +02001874 if (!ipacc_rtp_direct) {
1875 /* connect the TCH's to our RTP proxy */
Harald Weltea72273e2009-12-20 16:51:09 +01001876 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welte805f6442009-07-28 18:25:29 +02001877 if (rc < 0)
1878 return rc;
Harald Weltea72273e2009-12-20 16:51:09 +01001879 rc = rsl_ipacc_mdcx_to_rtpsock(remote_lchan);
Harald Welteda7ab742009-12-19 22:23:05 +01001880#warning do we need a check of rc here?
Harald Welte805f6442009-07-28 18:25:29 +02001881
1882 /* connect them with each other */
Harald Welte2c828992009-12-02 01:56:49 +05301883 rtp_socket_proxy(lchan->abis_ip.rtp_socket,
1884 remote_lchan->abis_ip.rtp_socket);
Harald Welte805f6442009-07-28 18:25:29 +02001885 } else {
1886 /* directly connect TCH RTP streams to each other */
Harald Welte2c828992009-12-02 01:56:49 +05301887 rc = rsl_ipacc_mdcx(lchan, remote_lchan->abis_ip.bound_ip,
1888 remote_lchan->abis_ip.bound_port,
Harald Welte2c828992009-12-02 01:56:49 +05301889 remote_lchan->abis_ip.rtp_payload2);
Harald Welte805f6442009-07-28 18:25:29 +02001890 if (rc < 0)
1891 return rc;
Harald Welte2c828992009-12-02 01:56:49 +05301892 rc = rsl_ipacc_mdcx(remote_lchan, lchan->abis_ip.bound_ip,
1893 lchan->abis_ip.bound_port,
Harald Welte2c828992009-12-02 01:56:49 +05301894 lchan->abis_ip.rtp_payload2);
Harald Welte805f6442009-07-28 18:25:29 +02001895 }
Harald Welte11fa29c2009-02-19 17:24:39 +00001896 break;
1897 case GSM_BTS_TYPE_BS11:
1898 trau_mux_map_lchan(lchan, remote_lchan);
1899 break;
1900 default:
1901 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
Harald Welteda7ab742009-12-19 22:23:05 +01001902 return -EINVAL;
Harald Welte11fa29c2009-02-19 17:24:39 +00001903 }
Harald Welte49f48b82009-02-17 15:29:33 +00001904
1905 return 0;
1906}
1907
Harald Welte4bfdfe72009-06-10 23:11:52 +08001908/* bridge channels of two transactions */
1909static int tch_bridge(struct gsm_network *net, u_int32_t *refs)
Harald Welte7ccf7782009-02-17 01:43:01 +00001910{
Harald Weltedcaf5652009-07-23 18:56:43 +02001911 struct gsm_trans *trans1 = trans_find_by_callref(net, refs[0]);
1912 struct gsm_trans *trans2 = trans_find_by_callref(net, refs[1]);
Harald Welte7ccf7782009-02-17 01:43:01 +00001913
Harald Welte4bfdfe72009-06-10 23:11:52 +08001914 if (!trans1 || !trans2)
Harald Welte7ccf7782009-02-17 01:43:01 +00001915 return -EIO;
1916
Harald Welte4bfdfe72009-06-10 23:11:52 +08001917 if (!trans1->lchan || !trans2->lchan)
1918 return -EIO;
1919
1920 /* through-connect channel */
1921 return tch_map(trans1->lchan, trans2->lchan);
Harald Welte7ccf7782009-02-17 01:43:01 +00001922}
1923
Harald Welteda7ab742009-12-19 22:23:05 +01001924/* enable receive of channels to MNCC upqueue */
1925static int tch_recv_mncc(struct gsm_network *net, u_int32_t callref, int enable)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001926{
1927 struct gsm_trans *trans;
Harald Welteda7ab742009-12-19 22:23:05 +01001928 struct gsm_lchan *lchan;
1929 struct gsm_bts *bts;
1930 int rc;
Harald Welte7ccf7782009-02-17 01:43:01 +00001931
Harald Welte4bfdfe72009-06-10 23:11:52 +08001932 /* Find callref */
Harald Welteda7ab742009-12-19 22:23:05 +01001933 trans = trans_find_by_callref(net, callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001934 if (!trans)
1935 return -EIO;
1936 if (!trans->lchan)
1937 return 0;
Harald Welteda7ab742009-12-19 22:23:05 +01001938 lchan = trans->lchan;
1939 bts = lchan->ts->trx->bts;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001940
Harald Welteda7ab742009-12-19 22:23:05 +01001941 switch (bts->type) {
1942 case GSM_BTS_TYPE_NANOBTS:
1943 if (ipacc_rtp_direct) {
1944 DEBUGP(DCC, "Error: RTP proxy is disabled\n");
1945 return -EINVAL;
1946 }
1947 /* in case, we don't have a RTP socket yet, we note this
1948 * in the transaction and try later */
1949 if (!lchan->abis_ip.rtp_socket) {
1950 trans->tch_recv = enable;
1951 DEBUGP(DCC, "queue tch_recv_mncc request (%d)\n", enable);
1952 return 0;
1953 }
1954 if (enable) {
1955 /* connect the TCH's to our RTP proxy */
Harald Weltea72273e2009-12-20 16:51:09 +01001956 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welteda7ab742009-12-19 22:23:05 +01001957 if (rc < 0)
1958 return rc;
1959 /* assign socket to application interface */
1960 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
1961 net, callref);
1962 } else
1963 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
1964 net, 0);
1965 break;
1966 case GSM_BTS_TYPE_BS11:
1967 if (enable)
1968 return trau_recv_lchan(lchan, callref);
1969 return trau_mux_unmap(NULL, callref);
1970 break;
1971 default:
1972 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
1973 return -EINVAL;
1974 }
1975
1976 return 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001977}
1978
Harald Welte4bfdfe72009-06-10 23:11:52 +08001979static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
1980{
1981 DEBUGP(DCC, "-> STATUS ENQ\n");
1982 return gsm48_cc_tx_status(trans, msg);
1983}
1984
1985static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg);
1986static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg);
1987
1988static void gsm48_cc_timeout(void *arg)
1989{
1990 struct gsm_trans *trans = arg;
1991 int disconnect = 0, release = 0;
Harald Weltec66b71c2009-06-11 14:23:20 +08001992 int mo_cause = GSM48_CC_CAUSE_RECOVERY_TIMER;
1993 int mo_location = GSM48_CAUSE_LOC_USER;
1994 int l4_cause = GSM48_CC_CAUSE_NORMAL_UNSPEC;
1995 int l4_location = GSM48_CAUSE_LOC_PRN_S_LU;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001996 struct gsm_mncc mo_rel, l4_rel;
1997
1998 memset(&mo_rel, 0, sizeof(struct gsm_mncc));
1999 mo_rel.callref = trans->callref;
2000 memset(&l4_rel, 0, sizeof(struct gsm_mncc));
2001 l4_rel.callref = trans->callref;
2002
Harald Weltedcaf5652009-07-23 18:56:43 +02002003 switch(trans->cc.Tcurrent) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002004 case 0x303:
2005 release = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08002006 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002007 break;
2008 case 0x310:
2009 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08002010 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002011 break;
2012 case 0x313:
2013 disconnect = 1;
2014 /* unknown, did not find it in the specs */
2015 break;
2016 case 0x301:
2017 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08002018 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002019 break;
2020 case 0x308:
Harald Weltedcaf5652009-07-23 18:56:43 +02002021 if (!trans->cc.T308_second) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002022 /* restart T308 a second time */
Harald Weltedcaf5652009-07-23 18:56:43 +02002023 gsm48_cc_tx_release(trans, &trans->cc.msg);
2024 trans->cc.T308_second = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002025 break; /* stay in release state */
2026 }
Harald Weltedcaf5652009-07-23 18:56:43 +02002027 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002028 return;
2029// release = 1;
2030// l4_cause = 14;
2031// break;
2032 case 0x306:
2033 release = 1;
Harald Weltedcaf5652009-07-23 18:56:43 +02002034 mo_cause = trans->cc.msg.cause.value;
2035 mo_location = trans->cc.msg.cause.location;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002036 break;
2037 case 0x323:
2038 disconnect = 1;
2039 break;
2040 default:
2041 release = 1;
2042 }
2043
2044 if (release && trans->callref) {
2045 /* process release towards layer 4 */
Harald Welte596fed42009-07-23 19:06:52 +02002046 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002047 l4_location, l4_cause);
2048 trans->callref = 0;
2049 }
2050
2051 if (disconnect && trans->callref) {
2052 /* process disconnect towards layer 4 */
2053 mncc_set_cause(&l4_rel, l4_location, l4_cause);
Harald Welte596fed42009-07-23 19:06:52 +02002054 mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &l4_rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002055 }
2056
2057 /* process disconnect towards mobile station */
2058 if (disconnect || release) {
2059 mncc_set_cause(&mo_rel, mo_location, mo_cause);
Harald Weltedcaf5652009-07-23 18:56:43 +02002060 mo_rel.cause.diag[0] = ((trans->cc.Tcurrent & 0xf00) >> 8) + '0';
2061 mo_rel.cause.diag[1] = ((trans->cc.Tcurrent & 0x0f0) >> 4) + '0';
2062 mo_rel.cause.diag[2] = (trans->cc.Tcurrent & 0x00f) + '0';
Harald Welte4bfdfe72009-06-10 23:11:52 +08002063 mo_rel.cause.diag_len = 3;
2064
2065 if (disconnect)
2066 gsm48_cc_tx_disconnect(trans, &mo_rel);
2067 if (release)
2068 gsm48_cc_tx_release(trans, &mo_rel);
2069 }
2070
2071}
2072
2073static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
2074 int sec, int micro)
2075{
2076 DEBUGP(DCC, "starting timer T%x with %d seconds\n", current, sec);
Harald Weltedcaf5652009-07-23 18:56:43 +02002077 trans->cc.timer.cb = gsm48_cc_timeout;
2078 trans->cc.timer.data = trans;
2079 bsc_schedule_timer(&trans->cc.timer, sec, micro);
2080 trans->cc.Tcurrent = current;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002081}
2082
2083static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
2084{
2085 struct gsm48_hdr *gh = msgb_l3(msg);
2086 u_int8_t msg_type = gh->msg_type & 0xbf;
2087 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2088 struct tlv_parsed tp;
2089 struct gsm_mncc setup;
2090
2091 memset(&setup, 0, sizeof(struct gsm_mncc));
2092 setup.callref = trans->callref;
2093 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2094 /* emergency setup is identified by msg_type */
2095 if (msg_type == GSM48_MT_CC_EMERG_SETUP)
2096 setup.emergency = 1;
2097
2098 /* use subscriber as calling party number */
2099 if (trans->subscr) {
2100 setup.fields |= MNCC_F_CALLING;
2101 strncpy(setup.calling.number, trans->subscr->extension,
2102 sizeof(setup.calling.number)-1);
Andreas Eversbergc079be42009-06-15 23:22:09 +02002103 strncpy(setup.imsi, trans->subscr->imsi,
2104 sizeof(setup.imsi)-1);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002105 }
2106 /* bearer capability */
2107 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2108 setup.fields |= MNCC_F_BEARER_CAP;
2109 decode_bearer_cap(&setup.bearer_cap,
2110 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2111 }
2112 /* facility */
2113 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2114 setup.fields |= MNCC_F_FACILITY;
2115 decode_facility(&setup.facility,
2116 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2117 }
2118 /* called party bcd number */
2119 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
2120 setup.fields |= MNCC_F_CALLED;
2121 decode_called(&setup.called,
2122 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
2123 }
2124 /* user-user */
2125 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2126 setup.fields |= MNCC_F_USERUSER;
2127 decode_useruser(&setup.useruser,
2128 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2129 }
2130 /* ss-version */
2131 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2132 setup.fields |= MNCC_F_SSVERSION;
2133 decode_ssversion(&setup.ssversion,
2134 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2135 }
2136 /* CLIR suppression */
2137 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
2138 setup.clir.sup = 1;
2139 /* CLIR invocation */
2140 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
2141 setup.clir.inv = 1;
2142 /* cc cap */
2143 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
2144 setup.fields |= MNCC_F_CCCAP;
2145 decode_cccap(&setup.cccap,
2146 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
2147 }
2148
Harald Welte4bfdfe72009-06-10 23:11:52 +08002149 new_cc_state(trans, GSM_CSTATE_INITIATED);
2150
2151 /* indicate setup to MNCC */
Harald Welte596fed42009-07-23 19:06:52 +02002152 mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_IND, &setup);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002153
Harald Welte13cac662009-07-29 12:10:35 +02002154 /* MNCC code will modify the channel asynchronously, we should
2155 * ipaccess-bind only after the modification has been made to the
2156 * lchan->tch_mode */
Harald Welte4bfdfe72009-06-10 23:11:52 +08002157 return 0;
2158}
2159
2160static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
Harald Welte65e74cc2008-12-29 01:55:35 +00002161{
2162 struct msgb *msg = gsm48_msgb_alloc();
2163 struct gsm48_hdr *gh;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002164 struct gsm_mncc *setup = arg;
Harald Welte78283ef2009-07-23 21:36:44 +02002165 int rc, trans_id;
Harald Welte65e74cc2008-12-29 01:55:35 +00002166
Harald Welte7ccf7782009-02-17 01:43:01 +00002167 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte65e74cc2008-12-29 01:55:35 +00002168
Harald Welte4bfdfe72009-06-10 23:11:52 +08002169 /* transaction id must not be assigned */
2170 if (trans->transaction_id != 0xff) { /* unasssigned */
2171 DEBUGP(DCC, "TX Setup with assigned transaction. "
2172 "This is not allowed!\n");
2173 /* Temporarily out of order */
Harald Welte596fed42009-07-23 19:06:52 +02002174 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002175 GSM48_CAUSE_LOC_PRN_S_LU,
2176 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002177 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002178 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002179 return rc;
2180 }
2181
2182 /* Get free transaction_id */
Harald Welte78283ef2009-07-23 21:36:44 +02002183 trans_id = trans_assign_trans_id(trans->subscr, GSM48_PDISC_CC, 0);
2184 if (trans_id < 0) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002185 /* no free transaction ID */
Harald Welte596fed42009-07-23 19:06:52 +02002186 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002187 GSM48_CAUSE_LOC_PRN_S_LU,
2188 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002189 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002190 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002191 return rc;
2192 }
Harald Welte78283ef2009-07-23 21:36:44 +02002193 trans->transaction_id = trans_id;
Harald Welte49f48b82009-02-17 15:29:33 +00002194
Harald Welte65e74cc2008-12-29 01:55:35 +00002195 gh->msg_type = GSM48_MT_CC_SETUP;
Harald Welte09e38af2009-02-16 22:52:23 +00002196
Harald Welte4bfdfe72009-06-10 23:11:52 +08002197 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
Harald Welte65e74cc2008-12-29 01:55:35 +00002198
Harald Welte4bfdfe72009-06-10 23:11:52 +08002199 /* bearer capability */
2200 if (setup->fields & MNCC_F_BEARER_CAP)
2201 encode_bearer_cap(msg, 0, &setup->bearer_cap);
2202 /* facility */
2203 if (setup->fields & MNCC_F_FACILITY)
2204 encode_facility(msg, 0, &setup->facility);
2205 /* progress */
2206 if (setup->fields & MNCC_F_PROGRESS)
2207 encode_progress(msg, 0, &setup->progress);
2208 /* calling party BCD number */
2209 if (setup->fields & MNCC_F_CALLING)
2210 encode_calling(msg, &setup->calling);
2211 /* called party BCD number */
2212 if (setup->fields & MNCC_F_CALLED)
2213 encode_called(msg, &setup->called);
2214 /* user-user */
2215 if (setup->fields & MNCC_F_USERUSER)
2216 encode_useruser(msg, 0, &setup->useruser);
2217 /* redirecting party BCD number */
2218 if (setup->fields & MNCC_F_REDIRECTING)
2219 encode_redirecting(msg, &setup->redirecting);
2220 /* signal */
2221 if (setup->fields & MNCC_F_SIGNAL)
2222 encode_signal(msg, setup->signal);
2223
2224 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
Harald Welte65e74cc2008-12-29 01:55:35 +00002225
Harald Welte39e2ead2009-07-23 21:13:03 +02002226 return gsm48_sendmsg(msg, trans);
Harald Welte65e74cc2008-12-29 01:55:35 +00002227}
2228
Harald Welte4bfdfe72009-06-10 23:11:52 +08002229static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
2230{
2231 struct gsm48_hdr *gh = msgb_l3(msg);
2232 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2233 struct tlv_parsed tp;
2234 struct gsm_mncc call_conf;
2235
2236 gsm48_stop_cc_timer(trans);
2237 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
2238
2239 memset(&call_conf, 0, sizeof(struct gsm_mncc));
2240 call_conf.callref = trans->callref;
2241 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2242#if 0
2243 /* repeat */
2244 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
2245 call_conf.repeat = 1;
2246 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
2247 call_conf.repeat = 2;
2248#endif
2249 /* bearer capability */
2250 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2251 call_conf.fields |= MNCC_F_BEARER_CAP;
2252 decode_bearer_cap(&call_conf.bearer_cap,
2253 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2254 }
2255 /* cause */
2256 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2257 call_conf.fields |= MNCC_F_CAUSE;
2258 decode_cause(&call_conf.cause,
2259 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2260 }
2261 /* cc cap */
2262 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
2263 call_conf.fields |= MNCC_F_CCCAP;
2264 decode_cccap(&call_conf.cccap,
2265 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
2266 }
2267
2268 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
2269
Harald Welte596fed42009-07-23 19:06:52 +02002270 return mncc_recvmsg(trans->subscr->net, trans, MNCC_CALL_CONF_IND,
2271 &call_conf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002272}
2273
2274static int gsm48_cc_tx_call_proc(struct gsm_trans *trans, void *arg)
2275{
2276 struct gsm_mncc *proceeding = arg;
2277 struct msgb *msg = gsm48_msgb_alloc();
2278 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2279
Harald Welte4bfdfe72009-06-10 23:11:52 +08002280 gh->msg_type = GSM48_MT_CC_CALL_PROC;
2281
2282 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
2283
2284 /* bearer capability */
2285 if (proceeding->fields & MNCC_F_BEARER_CAP)
2286 encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
2287 /* facility */
2288 if (proceeding->fields & MNCC_F_FACILITY)
2289 encode_facility(msg, 0, &proceeding->facility);
2290 /* progress */
2291 if (proceeding->fields & MNCC_F_PROGRESS)
2292 encode_progress(msg, 0, &proceeding->progress);
2293
Harald Welte39e2ead2009-07-23 21:13:03 +02002294 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002295}
2296
2297static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
2298{
2299 struct gsm48_hdr *gh = msgb_l3(msg);
2300 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2301 struct tlv_parsed tp;
2302 struct gsm_mncc alerting;
2303
2304 gsm48_stop_cc_timer(trans);
2305 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
2306
2307 memset(&alerting, 0, sizeof(struct gsm_mncc));
2308 alerting.callref = trans->callref;
2309 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2310 /* facility */
2311 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2312 alerting.fields |= MNCC_F_FACILITY;
2313 decode_facility(&alerting.facility,
2314 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2315 }
2316
2317 /* progress */
2318 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
2319 alerting.fields |= MNCC_F_PROGRESS;
2320 decode_progress(&alerting.progress,
2321 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
2322 }
2323 /* ss-version */
2324 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2325 alerting.fields |= MNCC_F_SSVERSION;
2326 decode_ssversion(&alerting.ssversion,
2327 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2328 }
2329
2330 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
2331
Harald Welte596fed42009-07-23 19:06:52 +02002332 return mncc_recvmsg(trans->subscr->net, trans, MNCC_ALERT_IND,
2333 &alerting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002334}
2335
2336static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
2337{
2338 struct gsm_mncc *alerting = arg;
2339 struct msgb *msg = gsm48_msgb_alloc();
2340 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2341
Harald Welte4bfdfe72009-06-10 23:11:52 +08002342 gh->msg_type = GSM48_MT_CC_ALERTING;
2343
2344 /* facility */
2345 if (alerting->fields & MNCC_F_FACILITY)
2346 encode_facility(msg, 0, &alerting->facility);
2347 /* progress */
2348 if (alerting->fields & MNCC_F_PROGRESS)
2349 encode_progress(msg, 0, &alerting->progress);
2350 /* user-user */
2351 if (alerting->fields & MNCC_F_USERUSER)
2352 encode_useruser(msg, 0, &alerting->useruser);
2353
2354 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
2355
Harald Welte39e2ead2009-07-23 21:13:03 +02002356 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002357}
2358
2359static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
2360{
2361 struct gsm_mncc *progress = arg;
2362 struct msgb *msg = gsm48_msgb_alloc();
2363 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2364
Harald Welte4bfdfe72009-06-10 23:11:52 +08002365 gh->msg_type = GSM48_MT_CC_PROGRESS;
2366
2367 /* progress */
2368 encode_progress(msg, 1, &progress->progress);
2369 /* user-user */
2370 if (progress->fields & MNCC_F_USERUSER)
2371 encode_useruser(msg, 0, &progress->useruser);
2372
Harald Welte39e2ead2009-07-23 21:13:03 +02002373 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002374}
2375
2376static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
2377{
2378 struct gsm_mncc *connect = arg;
2379 struct msgb *msg = gsm48_msgb_alloc();
2380 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2381
Harald Welte4bfdfe72009-06-10 23:11:52 +08002382 gh->msg_type = GSM48_MT_CC_CONNECT;
2383
2384 gsm48_stop_cc_timer(trans);
2385 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
2386
2387 /* facility */
2388 if (connect->fields & MNCC_F_FACILITY)
2389 encode_facility(msg, 0, &connect->facility);
2390 /* progress */
2391 if (connect->fields & MNCC_F_PROGRESS)
2392 encode_progress(msg, 0, &connect->progress);
2393 /* connected number */
2394 if (connect->fields & MNCC_F_CONNECTED)
2395 encode_connected(msg, &connect->connected);
2396 /* user-user */
2397 if (connect->fields & MNCC_F_USERUSER)
2398 encode_useruser(msg, 0, &connect->useruser);
2399
2400 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
2401
Harald Welte39e2ead2009-07-23 21:13:03 +02002402 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002403}
2404
2405static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
2406{
2407 struct gsm48_hdr *gh = msgb_l3(msg);
2408 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2409 struct tlv_parsed tp;
2410 struct gsm_mncc connect;
2411
2412 gsm48_stop_cc_timer(trans);
2413
2414 memset(&connect, 0, sizeof(struct gsm_mncc));
2415 connect.callref = trans->callref;
2416 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2417 /* use subscriber as connected party number */
2418 if (trans->subscr) {
2419 connect.fields |= MNCC_F_CONNECTED;
2420 strncpy(connect.connected.number, trans->subscr->extension,
2421 sizeof(connect.connected.number)-1);
Andreas Eversbergc079be42009-06-15 23:22:09 +02002422 strncpy(connect.imsi, trans->subscr->imsi,
2423 sizeof(connect.imsi)-1);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002424 }
2425 /* facility */
2426 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2427 connect.fields |= MNCC_F_FACILITY;
2428 decode_facility(&connect.facility,
2429 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2430 }
2431 /* user-user */
2432 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2433 connect.fields |= MNCC_F_USERUSER;
2434 decode_useruser(&connect.useruser,
2435 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2436 }
2437 /* ss-version */
2438 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2439 connect.fields |= MNCC_F_SSVERSION;
2440 decode_ssversion(&connect.ssversion,
2441 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2442 }
2443
2444 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
2445
Harald Welte596fed42009-07-23 19:06:52 +02002446 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_CNF, &connect);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002447}
2448
2449
2450static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
2451{
2452 struct gsm_mncc connect_ack;
2453
2454 gsm48_stop_cc_timer(trans);
2455
2456 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2457
2458 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
2459 connect_ack.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02002460 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_COMPL_IND,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002461 &connect_ack);
2462}
2463
2464static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
2465{
2466 struct msgb *msg = gsm48_msgb_alloc();
2467 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2468
Harald Welte4bfdfe72009-06-10 23:11:52 +08002469 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
2470
2471 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2472
Harald Welte39e2ead2009-07-23 21:13:03 +02002473 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002474}
2475
2476static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
2477{
2478 struct gsm48_hdr *gh = msgb_l3(msg);
2479 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2480 struct tlv_parsed tp;
2481 struct gsm_mncc disc;
2482
2483 gsm48_stop_cc_timer(trans);
2484
2485 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
2486
2487 memset(&disc, 0, sizeof(struct gsm_mncc));
2488 disc.callref = trans->callref;
2489 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
2490 /* cause */
2491 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2492 disc.fields |= MNCC_F_CAUSE;
2493 decode_cause(&disc.cause,
2494 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2495 }
2496 /* facility */
2497 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2498 disc.fields |= MNCC_F_FACILITY;
2499 decode_facility(&disc.facility,
2500 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2501 }
2502 /* user-user */
2503 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2504 disc.fields |= MNCC_F_USERUSER;
2505 decode_useruser(&disc.useruser,
2506 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2507 }
2508 /* ss-version */
2509 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2510 disc.fields |= MNCC_F_SSVERSION;
2511 decode_ssversion(&disc.ssversion,
2512 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2513 }
2514
Harald Welte596fed42009-07-23 19:06:52 +02002515 return mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &disc);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002516
2517}
2518
Harald Weltec66b71c2009-06-11 14:23:20 +08002519static struct gsm_mncc_cause default_cause = {
2520 .location = GSM48_CAUSE_LOC_PRN_S_LU,
2521 .coding = 0,
2522 .rec = 0,
2523 .rec_val = 0,
2524 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
2525 .diag_len = 0,
2526 .diag = { 0 },
2527};
Harald Welte4bfdfe72009-06-10 23:11:52 +08002528
2529static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
2530{
2531 struct gsm_mncc *disc = arg;
2532 struct msgb *msg = gsm48_msgb_alloc();
2533 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2534
Harald Welte4bfdfe72009-06-10 23:11:52 +08002535 gh->msg_type = GSM48_MT_CC_DISCONNECT;
2536
2537 gsm48_stop_cc_timer(trans);
2538 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
2539
2540 /* cause */
2541 if (disc->fields & MNCC_F_CAUSE)
2542 encode_cause(msg, 1, &disc->cause);
2543 else
2544 encode_cause(msg, 1, &default_cause);
2545
2546 /* facility */
2547 if (disc->fields & MNCC_F_FACILITY)
2548 encode_facility(msg, 0, &disc->facility);
2549 /* progress */
2550 if (disc->fields & MNCC_F_PROGRESS)
2551 encode_progress(msg, 0, &disc->progress);
2552 /* user-user */
2553 if (disc->fields & MNCC_F_USERUSER)
2554 encode_useruser(msg, 0, &disc->useruser);
2555
2556 /* store disconnect cause for T306 expiry */
Harald Weltedcaf5652009-07-23 18:56:43 +02002557 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002558
2559 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
2560
Harald Welte39e2ead2009-07-23 21:13:03 +02002561 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002562}
2563
2564static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
2565{
2566 struct gsm48_hdr *gh = msgb_l3(msg);
2567 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2568 struct tlv_parsed tp;
2569 struct gsm_mncc rel;
2570 int rc;
2571
2572 gsm48_stop_cc_timer(trans);
2573
2574 memset(&rel, 0, sizeof(struct gsm_mncc));
2575 rel.callref = trans->callref;
2576 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2577 /* cause */
2578 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2579 rel.fields |= MNCC_F_CAUSE;
2580 decode_cause(&rel.cause,
2581 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2582 }
2583 /* facility */
2584 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2585 rel.fields |= MNCC_F_FACILITY;
2586 decode_facility(&rel.facility,
2587 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2588 }
2589 /* user-user */
2590 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2591 rel.fields |= MNCC_F_USERUSER;
2592 decode_useruser(&rel.useruser,
2593 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2594 }
2595 /* ss-version */
2596 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2597 rel.fields |= MNCC_F_SSVERSION;
2598 decode_ssversion(&rel.ssversion,
2599 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2600 }
2601
Harald Weltedcaf5652009-07-23 18:56:43 +02002602 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002603 /* release collision 5.4.5 */
Harald Welte596fed42009-07-23 19:06:52 +02002604 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_CNF, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002605 } else {
Harald Welte596fed42009-07-23 19:06:52 +02002606 rc = gsm48_tx_simple(msg->lchan,
Harald Welte6f5aee02009-07-23 21:21:14 +02002607 GSM48_PDISC_CC | (trans->transaction_id << 4),
Harald Welte596fed42009-07-23 19:06:52 +02002608 GSM48_MT_CC_RELEASE_COMPL);
2609 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_IND, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002610 }
2611
2612 new_cc_state(trans, GSM_CSTATE_NULL);
2613
2614 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002615 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002616
2617 return rc;
2618}
2619
2620static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
2621{
2622 struct gsm_mncc *rel = arg;
2623 struct msgb *msg = gsm48_msgb_alloc();
2624 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2625
Harald Welte4bfdfe72009-06-10 23:11:52 +08002626 gh->msg_type = GSM48_MT_CC_RELEASE;
2627
2628 trans->callref = 0;
2629
2630 gsm48_stop_cc_timer(trans);
2631 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
2632
2633 /* cause */
2634 if (rel->fields & MNCC_F_CAUSE)
2635 encode_cause(msg, 0, &rel->cause);
2636 /* facility */
2637 if (rel->fields & MNCC_F_FACILITY)
2638 encode_facility(msg, 0, &rel->facility);
2639 /* user-user */
2640 if (rel->fields & MNCC_F_USERUSER)
2641 encode_useruser(msg, 0, &rel->useruser);
2642
Harald Weltedcaf5652009-07-23 18:56:43 +02002643 trans->cc.T308_second = 0;
2644 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002645
Harald Weltedcaf5652009-07-23 18:56:43 +02002646 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002647 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
2648
Harald Welte39e2ead2009-07-23 21:13:03 +02002649 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002650}
2651
2652static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
2653{
2654 struct gsm48_hdr *gh = msgb_l3(msg);
2655 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2656 struct tlv_parsed tp;
2657 struct gsm_mncc rel;
2658 int rc = 0;
2659
2660 gsm48_stop_cc_timer(trans);
2661
2662 memset(&rel, 0, sizeof(struct gsm_mncc));
2663 rel.callref = trans->callref;
2664 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2665 /* cause */
2666 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2667 rel.fields |= MNCC_F_CAUSE;
2668 decode_cause(&rel.cause,
2669 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2670 }
2671 /* facility */
2672 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2673 rel.fields |= MNCC_F_FACILITY;
2674 decode_facility(&rel.facility,
2675 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2676 }
2677 /* user-user */
2678 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2679 rel.fields |= MNCC_F_USERUSER;
2680 decode_useruser(&rel.useruser,
2681 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2682 }
2683 /* ss-version */
2684 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2685 rel.fields |= MNCC_F_SSVERSION;
2686 decode_ssversion(&rel.ssversion,
2687 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2688 }
2689
2690 if (trans->callref) {
Harald Weltedcaf5652009-07-23 18:56:43 +02002691 switch (trans->cc.state) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002692 case GSM_CSTATE_CALL_PRESENT:
Harald Welte596fed42009-07-23 19:06:52 +02002693 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002694 MNCC_REJ_IND, &rel);
2695 break;
2696 case GSM_CSTATE_RELEASE_REQ:
Harald Welte596fed42009-07-23 19:06:52 +02002697 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002698 MNCC_REL_CNF, &rel);
Harald Welteb83d9382009-12-12 21:00:48 +01002699 /* FIXME: in case of multiple calls, we can't simply
2700 * hang up here ! */
2701 lchan_auto_release(msg->lchan);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002702 break;
2703 default:
Harald Welte596fed42009-07-23 19:06:52 +02002704 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002705 MNCC_REL_IND, &rel);
2706 }
2707 }
2708
2709 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002710 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002711
2712 return rc;
2713}
2714
2715static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
2716{
2717 struct gsm_mncc *rel = arg;
2718 struct msgb *msg = gsm48_msgb_alloc();
2719 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2720
Harald Welte4bfdfe72009-06-10 23:11:52 +08002721 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
2722
2723 trans->callref = 0;
2724
2725 gsm48_stop_cc_timer(trans);
2726
2727 /* cause */
2728 if (rel->fields & MNCC_F_CAUSE)
2729 encode_cause(msg, 0, &rel->cause);
2730 /* facility */
2731 if (rel->fields & MNCC_F_FACILITY)
2732 encode_facility(msg, 0, &rel->facility);
2733 /* user-user */
2734 if (rel->fields & MNCC_F_USERUSER)
2735 encode_useruser(msg, 0, &rel->useruser);
2736
Harald Weltedcaf5652009-07-23 18:56:43 +02002737 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002738
Harald Welte39e2ead2009-07-23 21:13:03 +02002739 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002740}
2741
2742static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
2743{
2744 struct gsm48_hdr *gh = msgb_l3(msg);
2745 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2746 struct tlv_parsed tp;
2747 struct gsm_mncc fac;
2748
2749 memset(&fac, 0, sizeof(struct gsm_mncc));
2750 fac.callref = trans->callref;
2751 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
2752 /* facility */
2753 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2754 fac.fields |= MNCC_F_FACILITY;
2755 decode_facility(&fac.facility,
2756 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2757 }
2758 /* ss-version */
2759 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2760 fac.fields |= MNCC_F_SSVERSION;
2761 decode_ssversion(&fac.ssversion,
2762 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2763 }
2764
Harald Welte596fed42009-07-23 19:06:52 +02002765 return mncc_recvmsg(trans->subscr->net, trans, MNCC_FACILITY_IND, &fac);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002766}
2767
2768static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
2769{
2770 struct gsm_mncc *fac = arg;
2771 struct msgb *msg = gsm48_msgb_alloc();
2772 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2773
Harald Welte4bfdfe72009-06-10 23:11:52 +08002774 gh->msg_type = GSM48_MT_CC_FACILITY;
2775
2776 /* facility */
2777 encode_facility(msg, 1, &fac->facility);
2778
Harald Welte39e2ead2009-07-23 21:13:03 +02002779 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002780}
2781
2782static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
2783{
2784 struct gsm_mncc hold;
2785
2786 memset(&hold, 0, sizeof(struct gsm_mncc));
2787 hold.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02002788 return mncc_recvmsg(trans->subscr->net, trans, MNCC_HOLD_IND, &hold);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002789}
2790
2791static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
2792{
2793 struct msgb *msg = gsm48_msgb_alloc();
2794 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2795
Harald Welte4bfdfe72009-06-10 23:11:52 +08002796 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
2797
Harald Welte39e2ead2009-07-23 21:13:03 +02002798 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002799}
2800
2801static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
2802{
2803 struct gsm_mncc *hold_rej = arg;
2804 struct msgb *msg = gsm48_msgb_alloc();
2805 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2806
Harald Welte4bfdfe72009-06-10 23:11:52 +08002807 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
2808
2809 /* cause */
2810 if (hold_rej->fields & MNCC_F_CAUSE)
2811 encode_cause(msg, 1, &hold_rej->cause);
2812 else
2813 encode_cause(msg, 1, &default_cause);
2814
Harald Welte39e2ead2009-07-23 21:13:03 +02002815 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002816}
2817
2818static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
2819{
2820 struct gsm_mncc retrieve;
2821
2822 memset(&retrieve, 0, sizeof(struct gsm_mncc));
2823 retrieve.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02002824 return mncc_recvmsg(trans->subscr->net, trans, MNCC_RETRIEVE_IND,
2825 &retrieve);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002826}
2827
2828static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
2829{
2830 struct msgb *msg = gsm48_msgb_alloc();
2831 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2832
Harald Welte4bfdfe72009-06-10 23:11:52 +08002833 gh->msg_type = GSM48_MT_CC_RETR_ACK;
2834
Harald Welte39e2ead2009-07-23 21:13:03 +02002835 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002836}
2837
2838static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
2839{
2840 struct gsm_mncc *retrieve_rej = arg;
2841 struct msgb *msg = gsm48_msgb_alloc();
2842 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2843
Harald Welte4bfdfe72009-06-10 23:11:52 +08002844 gh->msg_type = GSM48_MT_CC_RETR_REJ;
2845
2846 /* cause */
2847 if (retrieve_rej->fields & MNCC_F_CAUSE)
2848 encode_cause(msg, 1, &retrieve_rej->cause);
2849 else
2850 encode_cause(msg, 1, &default_cause);
2851
Harald Welte39e2ead2009-07-23 21:13:03 +02002852 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002853}
2854
2855static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
2856{
2857 struct gsm48_hdr *gh = msgb_l3(msg);
2858 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2859 struct tlv_parsed tp;
2860 struct gsm_mncc dtmf;
2861
2862 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2863 dtmf.callref = trans->callref;
2864 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2865 /* keypad facility */
2866 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
2867 dtmf.fields |= MNCC_F_KEYPAD;
2868 decode_keypad(&dtmf.keypad,
2869 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
2870 }
2871
Harald Welte596fed42009-07-23 19:06:52 +02002872 return mncc_recvmsg(trans->subscr->net, trans, MNCC_START_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002873}
2874
2875static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
2876{
2877 struct gsm_mncc *dtmf = arg;
2878 struct msgb *msg = gsm48_msgb_alloc();
2879 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2880
Harald Welte4bfdfe72009-06-10 23:11:52 +08002881 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
2882
2883 /* keypad */
2884 if (dtmf->fields & MNCC_F_KEYPAD)
2885 encode_keypad(msg, dtmf->keypad);
2886
Harald Welte39e2ead2009-07-23 21:13:03 +02002887 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002888}
2889
2890static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
2891{
2892 struct gsm_mncc *dtmf = arg;
2893 struct msgb *msg = gsm48_msgb_alloc();
2894 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2895
Harald Welte4bfdfe72009-06-10 23:11:52 +08002896 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
2897
2898 /* cause */
2899 if (dtmf->fields & MNCC_F_CAUSE)
2900 encode_cause(msg, 1, &dtmf->cause);
2901 else
2902 encode_cause(msg, 1, &default_cause);
2903
Harald Welte39e2ead2009-07-23 21:13:03 +02002904 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002905}
2906
2907static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
2908{
2909 struct msgb *msg = gsm48_msgb_alloc();
2910 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2911
Harald Welte4bfdfe72009-06-10 23:11:52 +08002912 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
2913
Harald Welte39e2ead2009-07-23 21:13:03 +02002914 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002915}
2916
2917static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
2918{
2919 struct gsm_mncc dtmf;
2920
2921 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2922 dtmf.callref = trans->callref;
2923
Harald Welte596fed42009-07-23 19:06:52 +02002924 return mncc_recvmsg(trans->subscr->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002925}
2926
2927static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
2928{
2929 struct gsm48_hdr *gh = msgb_l3(msg);
2930 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2931 struct tlv_parsed tp;
2932 struct gsm_mncc modify;
2933
2934 memset(&modify, 0, sizeof(struct gsm_mncc));
2935 modify.callref = trans->callref;
2936 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
2937 /* bearer capability */
2938 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2939 modify.fields |= MNCC_F_BEARER_CAP;
2940 decode_bearer_cap(&modify.bearer_cap,
2941 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2942 }
2943
2944 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
2945
Harald Welte596fed42009-07-23 19:06:52 +02002946 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_IND, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002947}
2948
2949static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
2950{
2951 struct gsm_mncc *modify = arg;
2952 struct msgb *msg = gsm48_msgb_alloc();
2953 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2954
Harald Welte4bfdfe72009-06-10 23:11:52 +08002955 gh->msg_type = GSM48_MT_CC_MODIFY;
2956
2957 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
2958
2959 /* bearer capability */
2960 encode_bearer_cap(msg, 1, &modify->bearer_cap);
2961
2962 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
2963
Harald Welte39e2ead2009-07-23 21:13:03 +02002964 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002965}
2966
2967static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
2968{
2969 struct gsm48_hdr *gh = msgb_l3(msg);
2970 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2971 struct tlv_parsed tp;
2972 struct gsm_mncc modify;
2973
2974 gsm48_stop_cc_timer(trans);
2975
2976 memset(&modify, 0, sizeof(struct gsm_mncc));
2977 modify.callref = trans->callref;
2978 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
2979 /* bearer capability */
2980 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2981 modify.fields |= MNCC_F_BEARER_CAP;
2982 decode_bearer_cap(&modify.bearer_cap,
2983 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2984 }
2985
2986 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2987
Harald Welte596fed42009-07-23 19:06:52 +02002988 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_CNF, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002989}
2990
2991static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
2992{
2993 struct gsm_mncc *modify = arg;
2994 struct msgb *msg = gsm48_msgb_alloc();
2995 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2996
Harald Welte4bfdfe72009-06-10 23:11:52 +08002997 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
2998
2999 /* bearer capability */
3000 encode_bearer_cap(msg, 1, &modify->bearer_cap);
3001
3002 new_cc_state(trans, GSM_CSTATE_ACTIVE);
3003
Harald Welte39e2ead2009-07-23 21:13:03 +02003004 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003005}
3006
3007static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
3008{
3009 struct gsm48_hdr *gh = msgb_l3(msg);
3010 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
3011 struct tlv_parsed tp;
3012 struct gsm_mncc modify;
3013
3014 gsm48_stop_cc_timer(trans);
3015
3016 memset(&modify, 0, sizeof(struct gsm_mncc));
3017 modify.callref = trans->callref;
3018 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
3019 /* bearer capability */
3020 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
3021 modify.fields |= GSM48_IE_BEARER_CAP;
3022 decode_bearer_cap(&modify.bearer_cap,
3023 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
3024 }
3025 /* cause */
3026 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
3027 modify.fields |= MNCC_F_CAUSE;
3028 decode_cause(&modify.cause,
3029 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
3030 }
3031
3032 new_cc_state(trans, GSM_CSTATE_ACTIVE);
3033
Harald Welte596fed42009-07-23 19:06:52 +02003034 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_REJ, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003035}
3036
3037static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
3038{
3039 struct gsm_mncc *modify = arg;
3040 struct msgb *msg = gsm48_msgb_alloc();
3041 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3042
Harald Welte4bfdfe72009-06-10 23:11:52 +08003043 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
3044
3045 /* bearer capability */
3046 encode_bearer_cap(msg, 1, &modify->bearer_cap);
3047 /* cause */
3048 encode_cause(msg, 1, &modify->cause);
3049
3050 new_cc_state(trans, GSM_CSTATE_ACTIVE);
3051
Harald Welte39e2ead2009-07-23 21:13:03 +02003052 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003053}
3054
3055static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
3056{
3057 struct gsm_mncc *notify = arg;
3058 struct msgb *msg = gsm48_msgb_alloc();
3059 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3060
Harald Welte4bfdfe72009-06-10 23:11:52 +08003061 gh->msg_type = GSM48_MT_CC_NOTIFY;
3062
3063 /* notify */
3064 encode_notify(msg, notify->notify);
3065
Harald Welte39e2ead2009-07-23 21:13:03 +02003066 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003067}
3068
3069static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
3070{
3071 struct gsm48_hdr *gh = msgb_l3(msg);
3072 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
3073// struct tlv_parsed tp;
3074 struct gsm_mncc notify;
3075
3076 memset(&notify, 0, sizeof(struct gsm_mncc));
3077 notify.callref = trans->callref;
3078// tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len);
3079 if (payload_len >= 1)
3080 decode_notify(&notify.notify, gh->data);
3081
Harald Welte596fed42009-07-23 19:06:52 +02003082 return mncc_recvmsg(trans->subscr->net, trans, MNCC_NOTIFY_IND, &notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003083}
3084
3085static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
3086{
3087 struct gsm_mncc *user = arg;
3088 struct msgb *msg = gsm48_msgb_alloc();
3089 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3090
Harald Welte4bfdfe72009-06-10 23:11:52 +08003091 gh->msg_type = GSM48_MT_CC_USER_INFO;
3092
3093 /* user-user */
3094 if (user->fields & MNCC_F_USERUSER)
3095 encode_useruser(msg, 1, &user->useruser);
3096 /* more data */
3097 if (user->more)
3098 encode_more(msg);
3099
Harald Welte39e2ead2009-07-23 21:13:03 +02003100 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003101}
3102
3103static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
3104{
3105 struct gsm48_hdr *gh = msgb_l3(msg);
3106 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
3107 struct tlv_parsed tp;
3108 struct gsm_mncc user;
3109
3110 memset(&user, 0, sizeof(struct gsm_mncc));
3111 user.callref = trans->callref;
3112 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
3113 /* user-user */
3114 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
3115 user.fields |= MNCC_F_USERUSER;
3116 decode_useruser(&user.useruser,
3117 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
3118 }
3119 /* more data */
3120 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
3121 user.more = 1;
3122
Harald Welte596fed42009-07-23 19:06:52 +02003123 return mncc_recvmsg(trans->subscr->net, trans, MNCC_USERINFO_IND, &user);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003124}
3125
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +02003126static int _gsm48_lchan_modify(struct gsm_trans *trans, void *arg)
Harald Welte4bfdfe72009-06-10 23:11:52 +08003127{
3128 struct gsm_mncc *mode = arg;
3129
Holger Hans Peter Freytherea528022009-11-18 22:57:02 +01003130 return gsm48_lchan_modify(trans->lchan, mode->lchan_mode);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003131}
3132
3133static struct downstate {
3134 u_int32_t states;
3135 int type;
3136 int (*rout) (struct gsm_trans *trans, void *arg);
3137} downstatelist[] = {
3138 /* mobile originating call establishment */
3139 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
3140 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc},
3141 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
3142 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
3143 {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 */
3144 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
3145 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
3146 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
3147 /* mobile terminating call establishment */
3148 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
3149 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
3150 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
3151 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
3152 /* signalling during call */
3153 {SBIT(GSM_CSTATE_ACTIVE),
3154 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
3155 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
3156 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
3157 {ALL_STATES,
3158 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
3159 {ALL_STATES,
3160 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
3161 {ALL_STATES,
3162 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
3163 {SBIT(GSM_CSTATE_ACTIVE),
3164 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
3165 {SBIT(GSM_CSTATE_ACTIVE),
3166 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
3167 {SBIT(GSM_CSTATE_ACTIVE),
3168 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
3169 {SBIT(GSM_CSTATE_ACTIVE),
3170 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
3171 {SBIT(GSM_CSTATE_ACTIVE),
3172 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
3173 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
3174 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
3175 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
3176 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
3177 {SBIT(GSM_CSTATE_ACTIVE),
3178 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
3179 /* clearing */
3180 {SBIT(GSM_CSTATE_INITIATED),
3181 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
3182 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
3183 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
3184 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
3185 MNCC_REL_REQ, gsm48_cc_tx_release},
3186 /* special */
3187 {ALL_STATES,
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +02003188 MNCC_LCHAN_MODIFY, _gsm48_lchan_modify},
Harald Welte4bfdfe72009-06-10 23:11:52 +08003189};
3190
3191#define DOWNSLLEN \
3192 (sizeof(downstatelist) / sizeof(struct downstate))
3193
3194
3195int mncc_send(struct gsm_network *net, int msg_type, void *arg)
3196{
Harald Welte1a6f7982009-08-09 18:52:33 +02003197 int i, rc = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003198 struct gsm_trans *trans = NULL, *transt;
Harald Welte1a6f7982009-08-09 18:52:33 +02003199 struct gsm_lchan *lchan = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003200 struct gsm_bts *bts = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003201 struct gsm_mncc *data = arg, rel;
3202
3203 /* handle special messages */
3204 switch(msg_type) {
3205 case MNCC_BRIDGE:
3206 return tch_bridge(net, arg);
3207 case MNCC_FRAME_DROP:
Harald Welteda7ab742009-12-19 22:23:05 +01003208 return tch_recv_mncc(net, data->callref, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003209 case MNCC_FRAME_RECV:
Harald Welteda7ab742009-12-19 22:23:05 +01003210 return tch_recv_mncc(net, data->callref, 1);
3211 case GSM_TCHF_FRAME:
3212 /* Find callref */
3213 trans = trans_find_by_callref(net, data->callref);
3214 if (!trans)
3215 return -EIO;
3216 if (!trans->lchan)
3217 return 0;
3218 if (trans->lchan->type != GSM_LCHAN_TCH_F)
3219 return 0;
3220 bts = trans->lchan->ts->trx->bts;
3221 switch (bts->type) {
3222 case GSM_BTS_TYPE_NANOBTS:
3223 if (!trans->lchan->abis_ip.rtp_socket)
3224 return 0;
3225 return rtp_send_frame(trans->lchan->abis_ip.rtp_socket, arg);
3226 case GSM_BTS_TYPE_BS11:
3227 return trau_send_frame(trans->lchan, arg);
3228 default:
3229 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
3230 }
3231 return -EINVAL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003232 }
3233
3234 memset(&rel, 0, sizeof(struct gsm_mncc));
3235 rel.callref = data->callref;
3236
3237 /* Find callref */
Harald Weltedcaf5652009-07-23 18:56:43 +02003238 trans = trans_find_by_callref(net, data->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003239
3240 /* Callref unknown */
3241 if (!trans) {
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003242 struct gsm_subscriber *subscr;
3243
Harald Welte4a3464c2009-07-04 10:11:24 +02003244 if (msg_type != MNCC_SETUP_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003245 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3246 "Received '%s' from MNCC with "
3247 "unknown callref %d\n", data->called.number,
3248 get_mncc_name(msg_type), data->callref);
3249 /* Invalid call reference */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003250 return mncc_release_ind(net, NULL, data->callref,
3251 GSM48_CAUSE_LOC_PRN_S_LU,
3252 GSM48_CC_CAUSE_INVAL_TRANS_ID);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003253 }
Andreas Eversbergc079be42009-06-15 23:22:09 +02003254 if (!data->called.number[0] && !data->imsi[0]) {
3255 DEBUGP(DCC, "(bts - trx - ts - ti) "
3256 "Received '%s' from MNCC with "
3257 "no number or IMSI\n", get_mncc_name(msg_type));
3258 /* Invalid number */
3259 return mncc_release_ind(net, NULL, data->callref,
3260 GSM48_CAUSE_LOC_PRN_S_LU,
3261 GSM48_CC_CAUSE_INV_NR_FORMAT);
3262 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003263 /* New transaction due to setup, find subscriber */
Andreas Eversbergc079be42009-06-15 23:22:09 +02003264 if (data->called.number[0])
Harald Welte9176bd42009-07-23 18:46:00 +02003265 subscr = subscr_get_by_extension(net,
3266 data->called.number);
Andreas Eversbergc079be42009-06-15 23:22:09 +02003267 else
Harald Welte9176bd42009-07-23 18:46:00 +02003268 subscr = subscr_get_by_imsi(net, data->imsi);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003269 /* If subscriber is not found */
3270 if (!subscr) {
3271 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3272 "Received '%s' from MNCC with "
3273 "unknown subscriber %s\n", data->called.number,
3274 get_mncc_name(msg_type), data->called.number);
3275 /* Unknown subscriber */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003276 return mncc_release_ind(net, NULL, data->callref,
3277 GSM48_CAUSE_LOC_PRN_S_LU,
3278 GSM48_CC_CAUSE_UNASSIGNED_NR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003279 }
3280 /* If subscriber is not "attached" */
3281 if (!subscr->lac) {
3282 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3283 "Received '%s' from MNCC with "
3284 "detached subscriber %s\n", data->called.number,
3285 get_mncc_name(msg_type), data->called.number);
3286 subscr_put(subscr);
3287 /* Temporarily out of order */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003288 return mncc_release_ind(net, NULL, data->callref,
3289 GSM48_CAUSE_LOC_PRN_S_LU,
3290 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003291 }
3292 /* Create transaction */
Harald Weltedcaf5652009-07-23 18:56:43 +02003293 trans = trans_alloc(subscr, GSM48_PDISC_CC, 0xff, data->callref);
3294 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003295 DEBUGP(DCC, "No memory for trans.\n");
3296 subscr_put(subscr);
3297 /* Ressource unavailable */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003298 mncc_release_ind(net, NULL, data->callref,
3299 GSM48_CAUSE_LOC_PRN_S_LU,
3300 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003301 return -ENOMEM;
3302 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003303 /* Find lchan */
Harald Welte1a6f7982009-08-09 18:52:33 +02003304 lchan = lchan_for_subscr(subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003305 /* If subscriber has no lchan */
3306 if (!lchan) {
3307 /* find transaction with this subscriber already paging */
3308 llist_for_each_entry(transt, &net->trans_list, entry) {
3309 /* Transaction of our lchan? */
3310 if (transt == trans ||
3311 transt->subscr != subscr)
3312 continue;
3313 DEBUGP(DCC, "(bts %d trx - ts - ti -- sub %s) "
3314 "Received '%s' from MNCC with "
3315 "unallocated channel, paging already "
3316 "started.\n", bts->nr,
3317 data->called.number,
3318 get_mncc_name(msg_type));
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003319 subscr_put(subscr);
3320 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003321 return 0;
3322 }
3323 /* store setup informations until paging was successfull */
Harald Weltedcaf5652009-07-23 18:56:43 +02003324 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
Harald Weltea1b28582009-08-01 19:31:47 +02003325 /* Trigger paging */
3326 paging_request(net, subscr, RSL_CHANNEED_TCH_F,
3327 setup_trig_pag_evt, subscr);
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003328 subscr_put(subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003329 return 0;
3330 }
3331 /* Assign lchan */
3332 trans->lchan = lchan;
3333 use_lchan(lchan);
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003334 subscr_put(subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003335 }
3336 lchan = trans->lchan;
3337
3338 /* if paging did not respond yet */
3339 if (!lchan) {
3340 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3341 "Received '%s' from MNCC in paging state\n",
3342 (trans->subscr)?(trans->subscr->extension):"-",
3343 get_mncc_name(msg_type));
Harald Weltec66b71c2009-06-11 14:23:20 +08003344 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
3345 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003346 if (msg_type == MNCC_REL_REQ)
3347 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
3348 else
3349 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
3350 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02003351 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003352 return rc;
3353 }
3354
3355 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x sub %s) "
3356 "Received '%s' from MNCC in state %d (%s)\n",
3357 lchan->ts->trx->bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
3358 trans->transaction_id,
3359 (lchan->subscr)?(lchan->subscr->extension):"-",
Harald Weltedcaf5652009-07-23 18:56:43 +02003360 get_mncc_name(msg_type), trans->cc.state,
3361 cc_state_names[trans->cc.state]);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003362
3363 /* Find function for current state and message */
3364 for (i = 0; i < DOWNSLLEN; i++)
3365 if ((msg_type == downstatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003366 && ((1 << trans->cc.state) & downstatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003367 break;
3368 if (i == DOWNSLLEN) {
3369 DEBUGP(DCC, "Message unhandled at this state.\n");
3370 return 0;
3371 }
3372
3373 rc = downstatelist[i].rout(trans, arg);
3374
3375 return rc;
3376}
3377
3378
3379static struct datastate {
3380 u_int32_t states;
3381 int type;
3382 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
3383} datastatelist[] = {
3384 /* mobile originating call establishment */
3385 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3386 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
3387 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3388 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
3389 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
3390 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
3391 /* mobile terminating call establishment */
3392 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
3393 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
3394 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
3395 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
3396 {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 */
3397 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
3398 /* signalling during call */
3399 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
3400 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
3401 {SBIT(GSM_CSTATE_ACTIVE),
3402 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
3403 {ALL_STATES,
3404 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
3405 {ALL_STATES,
3406 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
3407 {ALL_STATES,
3408 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
3409 {SBIT(GSM_CSTATE_ACTIVE),
3410 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
3411 {SBIT(GSM_CSTATE_ACTIVE),
3412 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
3413 {SBIT(GSM_CSTATE_ACTIVE),
3414 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
3415 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3416 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
3417 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3418 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
3419 {SBIT(GSM_CSTATE_ACTIVE),
3420 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
3421 /* clearing */
3422 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
3423 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
3424 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
3425 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
3426 {ALL_STATES, /* 5.4.3.4 */
3427 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
3428};
3429
3430#define DATASLLEN \
3431 (sizeof(datastatelist) / sizeof(struct datastate))
3432
Harald Welte4bc90a12008-12-27 16:32:52 +00003433static int gsm0408_rcv_cc(struct msgb *msg)
3434{
3435 struct gsm48_hdr *gh = msgb_l3(msg);
3436 u_int8_t msg_type = gh->msg_type & 0xbf;
Harald Welte6f5aee02009-07-23 21:21:14 +02003437 u_int8_t transaction_id = ((gh->proto_discr & 0xf0) ^ 0x80) >> 4; /* flip */
Harald Welte4bfdfe72009-06-10 23:11:52 +08003438 struct gsm_lchan *lchan = msg->lchan;
Harald Weltedcaf5652009-07-23 18:56:43 +02003439 struct gsm_trans *trans = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003440 int i, rc = 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00003441
Harald Welte4bfdfe72009-06-10 23:11:52 +08003442 if (msg_type & 0x80) {
3443 DEBUGP(DCC, "MSG 0x%2x not defined for PD error\n", msg_type);
3444 return -EINVAL;
Harald Welte4bc90a12008-12-27 16:32:52 +00003445 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003446
3447 /* Find transaction */
Harald Welteb8b40732009-07-23 21:58:40 +02003448 trans = trans_find_by_id(lchan->subscr, GSM48_PDISC_CC, transaction_id);
Harald Weltedcaf5652009-07-23 18:56:43 +02003449
Harald Welte6f5aee02009-07-23 21:21:14 +02003450 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003451 "Received '%s' from MS in state %d (%s)\n",
3452 lchan->ts->trx->bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
3453 transaction_id, (lchan->subscr)?(lchan->subscr->extension):"-",
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +02003454 gsm0408_cc_msg_names[msg_type], trans?(trans->cc.state):0,
Harald Weltedcaf5652009-07-23 18:56:43 +02003455 cc_state_names[trans?(trans->cc.state):0]);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003456
3457 /* Create transaction */
3458 if (!trans) {
Harald Welte6f5aee02009-07-23 21:21:14 +02003459 DEBUGP(DCC, "Unknown transaction ID %x, "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003460 "creating new trans.\n", transaction_id);
3461 /* Create transaction */
Harald Weltedcaf5652009-07-23 18:56:43 +02003462 trans = trans_alloc(lchan->subscr, GSM48_PDISC_CC,
3463 transaction_id, new_callref++);
3464 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003465 DEBUGP(DCC, "No memory for trans.\n");
3466 rc = gsm48_tx_simple(msg->lchan,
Harald Welte6f5aee02009-07-23 21:21:14 +02003467 GSM48_PDISC_CC | (transaction_id << 4),
Harald Welte4bfdfe72009-06-10 23:11:52 +08003468 GSM48_MT_CC_RELEASE_COMPL);
3469 return -ENOMEM;
3470 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003471 /* Assign transaction */
Harald Welte4bfdfe72009-06-10 23:11:52 +08003472 trans->lchan = lchan;
3473 use_lchan(lchan);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003474 }
3475
3476 /* find function for current state and message */
3477 for (i = 0; i < DATASLLEN; i++)
3478 if ((msg_type == datastatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003479 && ((1 << trans->cc.state) & datastatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003480 break;
3481 if (i == DATASLLEN) {
3482 DEBUGP(DCC, "Message unhandled at this state.\n");
3483 return 0;
3484 }
3485
3486 rc = datastatelist[i].rout(trans, msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00003487
3488 return rc;
3489}
3490
Harald Welte52b1f982008-12-23 20:25:15 +00003491/* 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 +02003492int gsm0408_rcvmsg(struct msgb *msg, u_int8_t link_id)
Harald Welte52b1f982008-12-23 20:25:15 +00003493{
3494 struct gsm48_hdr *gh = msgb_l3(msg);
3495 u_int8_t pdisc = gh->proto_discr & 0x0f;
Harald Welte8470bf22008-12-25 23:28:35 +00003496 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00003497
3498 switch (pdisc) {
3499 case GSM48_PDISC_CC:
3500 rc = gsm0408_rcv_cc(msg);
3501 break;
3502 case GSM48_PDISC_MM:
3503 rc = gsm0408_rcv_mm(msg);
3504 break;
3505 case GSM48_PDISC_RR:
3506 rc = gsm0408_rcv_rr(msg);
3507 break;
Harald Weltebcae43f2008-12-27 21:45:37 +00003508 case GSM48_PDISC_SMS:
Harald Welte (local)daef6062009-08-14 11:41:12 +02003509 rc = gsm0411_rcv_sms(msg, link_id);
Harald Weltebcae43f2008-12-27 21:45:37 +00003510 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003511 case GSM48_PDISC_MM_GPRS:
Harald Weltebcae43f2008-12-27 21:45:37 +00003512 case GSM48_PDISC_SM_GPRS:
Mike Haben59d284e2009-09-26 19:00:03 +02003513 fprintf(stderr, "Unimplemented GSM 04.08 discriminator 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +00003514 pdisc);
3515 break;
Harald Welte6eafe912009-10-16 08:32:58 +02003516 case GSM48_PDISC_NC_SS:
3517 rc = handle_rcv_ussd(msg);
3518 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003519 default:
Mike Haben59d284e2009-09-26 19:00:03 +02003520 fprintf(stderr, "Unknown GSM 04.08 discriminator 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +00003521 pdisc);
3522 break;
3523 }
3524
3525 return rc;
3526}
Harald Welte8470bf22008-12-25 23:28:35 +00003527
Harald Welte4bfdfe72009-06-10 23:11:52 +08003528/* dequeue messages to layer 4 */
3529int bsc_upqueue(struct gsm_network *net)
3530{
3531 struct gsm_mncc *mncc;
3532 struct msgb *msg;
3533 int work = 0;
3534
3535 if (net)
3536 while ((msg = msgb_dequeue(&net->upqueue))) {
3537 mncc = (struct gsm_mncc *)msg->data;
3538 if (net->mncc_recv)
3539 net->mncc_recv(net, mncc->msg_type, mncc);
3540 work = 1; /* work done */
Harald Welte316c8252009-06-26 19:40:48 +02003541 talloc_free(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003542 }
3543
3544 return work;
3545}
Harald Weltedcaf5652009-07-23 18:56:43 +02003546
Harald Welte805f6442009-07-28 18:25:29 +02003547/*
3548 * This will be ran by the linker when loading the DSO. We use it to
3549 * do system initialization, e.g. registration of signal handlers.
3550 */
3551static __attribute__((constructor)) void on_dso_load_0408(void)
3552{
Harald Welte805f6442009-07-28 18:25:29 +02003553 register_signal_handler(SS_LCHAN, gsm0408_handle_lchan_signal, NULL);
3554 register_signal_handler(SS_ABISIP, handle_abisip_signal, NULL);
3555}