blob: 61eba2c5a6af73287e5c8d7699f0772d0525dfb8 [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 Welte51008772009-12-29 11:49:12 +010053#include <openbsc/silent_call.h>
Harald Welte52b1f982008-12-23 20:25:15 +000054
Harald Welte0c389302009-06-10 12:08:54 +080055#define GSM_MAX_FACILITY 128
56#define GSM_MAX_SSVERSION 128
57#define GSM_MAX_USERUSER 128
58
Harald Welte (local)d19e58b2009-08-15 02:30:58 +020059void *tall_locop_ctx;
Harald Welte2cf161b2009-06-20 22:36:41 +020060
Harald Welte09e38af2009-02-16 22:52:23 +000061static const struct tlv_definition rsl_att_tlvdef = {
62 .def = {
63 [GSM48_IE_MOBILE_ID] = { TLV_TYPE_TLV },
64 [GSM48_IE_NAME_LONG] = { TLV_TYPE_TLV },
65 [GSM48_IE_NAME_SHORT] = { TLV_TYPE_TLV },
66 [GSM48_IE_UTC] = { TLV_TYPE_TV },
67 [GSM48_IE_NET_TIME_TZ] = { TLV_TYPE_FIXED, 7 },
68 [GSM48_IE_LSA_IDENT] = { TLV_TYPE_TLV },
69
70 [GSM48_IE_BEARER_CAP] = { TLV_TYPE_TLV },
71 [GSM48_IE_CAUSE] = { TLV_TYPE_TLV },
72 [GSM48_IE_CC_CAP] = { TLV_TYPE_TLV },
73 [GSM48_IE_ALERT] = { TLV_TYPE_TLV },
74 [GSM48_IE_FACILITY] = { TLV_TYPE_TLV },
75 [GSM48_IE_PROGR_IND] = { TLV_TYPE_TLV },
76 [GSM48_IE_AUX_STATUS] = { TLV_TYPE_TLV },
Harald Welte0c389302009-06-10 12:08:54 +080077 [GSM48_IE_NOTIFY] = { TLV_TYPE_TV },
Harald Welte09e38af2009-02-16 22:52:23 +000078 [GSM48_IE_KPD_FACILITY] = { TLV_TYPE_TV },
79 [GSM48_IE_SIGNAL] = { TLV_TYPE_TV },
Harald Welte0c389302009-06-10 12:08:54 +080080 [GSM48_IE_CONN_BCD] = { TLV_TYPE_TLV },
81 [GSM48_IE_CONN_SUB] = { TLV_TYPE_TLV },
Harald Welte09e38af2009-02-16 22:52:23 +000082 [GSM48_IE_CALLING_BCD] = { TLV_TYPE_TLV },
83 [GSM48_IE_CALLING_SUB] = { TLV_TYPE_TLV },
84 [GSM48_IE_CALLED_BCD] = { TLV_TYPE_TLV },
85 [GSM48_IE_CALLED_SUB] = { TLV_TYPE_TLV },
86 [GSM48_IE_REDIR_BCD] = { TLV_TYPE_TLV },
87 [GSM48_IE_REDIR_SUB] = { TLV_TYPE_TLV },
88 [GSM48_IE_LOWL_COMPAT] = { TLV_TYPE_TLV },
89 [GSM48_IE_HIGHL_COMPAT] = { TLV_TYPE_TLV },
90 [GSM48_IE_USER_USER] = { TLV_TYPE_TLV },
91 [GSM48_IE_SS_VERS] = { TLV_TYPE_TLV },
92 [GSM48_IE_MORE_DATA] = { TLV_TYPE_T },
93 [GSM48_IE_CLIR_SUPP] = { TLV_TYPE_T },
94 [GSM48_IE_CLIR_INVOC] = { TLV_TYPE_T },
95 [GSM48_IE_REV_C_SETUP] = { TLV_TYPE_T },
Harald Welte0c389302009-06-10 12:08:54 +080096 [GSM48_IE_REPEAT_CIR] = { TLV_TYPE_T },
97 [GSM48_IE_REPEAT_SEQ] = { TLV_TYPE_T },
Harald Welte09e38af2009-02-16 22:52:23 +000098 /* FIXME: more elements */
99 },
100};
Harald Weltecf5b3592009-05-01 18:28:42 +0000101
102static const char *rr_cause_names[] = {
103 [GSM48_RR_CAUSE_NORMAL] = "Normal event",
104 [GSM48_RR_CAUSE_ABNORMAL_UNSPEC] = "Abnormal release, unspecified",
105 [GSM48_RR_CAUSE_ABNORMAL_UNACCT] = "Abnormal release, channel unacceptable",
106 [GSM48_RR_CAUSE_ABNORMAL_TIMER] = "Abnormal release, timer expired",
107 [GSM48_RR_CAUSE_ABNORMAL_NOACT] = "Abnormal release, no activity on radio path",
108 [GSM48_RR_CAUSE_PREMPTIVE_REL] = "Preemptive release",
109 [GSM48_RR_CAUSE_HNDOVER_IMP] = "Handover impossible, timing advance out of range",
110 [GSM48_RR_CAUSE_CHAN_MODE_UNACCT] = "Channel mode unacceptable",
111 [GSM48_RR_CAUSE_FREQ_NOT_IMPL] = "Frequency not implemented",
112 [GSM48_RR_CAUSE_CALL_CLEARED] = "Call already cleared",
113 [GSM48_RR_CAUSE_SEMANT_INCORR] = "Semantically incorrect message",
114 [GSM48_RR_CAUSE_INVALID_MAND_INF] = "Invalid mandatory information",
115 [GSM48_RR_CAUSE_MSG_TYPE_N] = "Message type non-existant or not implemented",
116 [GSM48_RR_CAUSE_MSG_TYPE_N_COMPAT] = "Message type not compatible with protocol state",
117 [GSM48_RR_CAUSE_COND_IE_ERROR] = "Conditional IE error",
118 [GSM48_RR_CAUSE_NO_CELL_ALLOC_A] = "No cell allocation available",
119 [GSM48_RR_CAUSE_PROT_ERROR_UNSPC] = "Protocol error unspecified",
120};
121
Harald Welte4bfdfe72009-06-10 23:11:52 +0800122static const char *cc_state_names[] = {
123 "NULL",
124 "INITIATED",
125 "illegal state 2",
126 "MO_CALL_PROC",
127 "CALL_DELIVERED",
128 "illegal state 5",
129 "CALL_PRESENT",
130 "CALL_RECEIVED",
131 "CONNECT_REQUEST",
132 "MO_TERM_CALL_CONF",
133 "ACTIVE",
134 "DISCONNECT_REQ",
135 "DISCONNECT_IND",
136 "illegal state 13",
137 "illegal state 14",
138 "illegal state 15",
139 "illegal state 16",
140 "illegal state 17",
141 "illegal state 18",
142 "RELEASE_REQ",
143 "illegal state 20",
144 "illegal state 21",
145 "illegal state 22",
146 "illegal state 23",
147 "illegal state 24",
148 "illegal state 25",
149 "MO_ORIG_MODIFY",
150 "MO_TERM_MODIFY",
151 "CONNECT_IND",
152 "illegal state 29",
153 "illegal state 30",
154 "illegal state 31",
155};
156
Harald Weltecf5b3592009-05-01 18:28:42 +0000157static char strbuf[64];
158
159static const char *rr_cause_name(u_int8_t cause)
160{
161 if (cause < ARRAY_SIZE(rr_cause_names) &&
162 rr_cause_names[cause])
163 return rr_cause_names[cause];
164
165 snprintf(strbuf, sizeof(strbuf), "0x%02x", cause);
166 return strbuf;
167}
168
Holger Freytherd51524f2009-06-09 08:27:07 +0000169int gsm0408_loc_upd_acc(struct gsm_lchan *lchan, u_int32_t tmsi);
Harald Welte65e74cc2008-12-29 01:55:35 +0000170static int gsm48_tx_simple(struct gsm_lchan *lchan,
171 u_int8_t pdisc, u_int8_t msg_type);
Holger Freytherb7193e42008-12-29 17:44:08 +0000172static void schedule_reject(struct gsm_lchan *lchan);
Harald Welte65e74cc2008-12-29 01:55:35 +0000173
Harald Welte52b1f982008-12-23 20:25:15 +0000174struct gsm_lai {
175 u_int16_t mcc;
176 u_int16_t mnc;
177 u_int16_t lac;
178};
179
Harald Welte4bfdfe72009-06-10 23:11:52 +0800180static u_int32_t new_callref = 0x80000001;
181
Holger Freyther73487a22008-12-31 18:53:57 +0000182static int authorize_subscriber(struct gsm_loc_updating_operation *loc,
183 struct gsm_subscriber *subscriber)
Holger Freyther89824fc2008-12-30 16:18:18 +0000184{
185 if (!subscriber)
186 return 0;
187
Holger Freyther73487a22008-12-31 18:53:57 +0000188 /*
189 * Do not send accept yet as more information should arrive. Some
190 * phones will not send us the information and we will have to check
191 * what we want to do with that.
192 */
193 if (loc && (loc->waiting_for_imsi || loc->waiting_for_imei))
194 return 0;
195
Jan Luebbe06513f22009-08-12 12:48:00 +0200196 switch (subscriber->net->auth_policy) {
197 case GSM_AUTH_POLICY_CLOSED:
198 return subscriber->authorized;
Harald Welte (local)aa9dc192009-08-13 13:49:51 +0200199 case GSM_AUTH_POLICY_TOKEN:
Harald Welte (local)ee9afe32009-08-13 20:44:23 +0200200 if (subscriber->authorized)
201 return subscriber->authorized;
Harald Welte (local)aa9dc192009-08-13 13:49:51 +0200202 return (subscriber->flags & GSM_SUBSCRIBER_FIRST_CONTACT);
Jan Luebbe06513f22009-08-12 12:48:00 +0200203 case GSM_AUTH_POLICY_ACCEPT_ALL:
Holger Freyther89824fc2008-12-30 16:18:18 +0000204 return 1;
Jan Luebbe06513f22009-08-12 12:48:00 +0200205 default:
206 return 0;
207 }
Holger Freyther89824fc2008-12-30 16:18:18 +0000208}
Holger Freyther07cc8d82008-12-29 06:23:46 +0000209
Holger Freyther73487a22008-12-31 18:53:57 +0000210static void release_loc_updating_req(struct gsm_lchan *lchan)
211{
Harald Welte179f0642008-12-31 23:59:18 +0000212 if (!lchan->loc_operation)
Holger Freyther73487a22008-12-31 18:53:57 +0000213 return;
214
Harald Welteff117a82009-05-23 05:22:08 +0000215 bsc_del_timer(&lchan->loc_operation->updating_timer);
Harald Welte2cf161b2009-06-20 22:36:41 +0200216 talloc_free(lchan->loc_operation);
Holger Freyther73487a22008-12-31 18:53:57 +0000217 lchan->loc_operation = 0;
Holger Freyther3eaa7922009-01-01 02:59:03 +0000218 put_lchan(lchan);
Holger Freyther73487a22008-12-31 18:53:57 +0000219}
220
221static void allocate_loc_updating_req(struct gsm_lchan *lchan)
222{
Holger Freyther67b4b9a2009-01-01 03:46:11 +0000223 use_lchan(lchan);
Holger Freyther73487a22008-12-31 18:53:57 +0000224 release_loc_updating_req(lchan);
225
Harald Welte470ec292009-06-26 20:25:23 +0200226 lchan->loc_operation = talloc_zero(tall_locop_ctx,
227 struct gsm_loc_updating_operation);
Holger Freyther73487a22008-12-31 18:53:57 +0000228}
Holger Freyther07cc8d82008-12-29 06:23:46 +0000229
Holger Freytherd51524f2009-06-09 08:27:07 +0000230static int gsm0408_authorize(struct gsm_lchan *lchan, struct msgb *msg)
231{
Holger Freytherd51524f2009-06-09 08:27:07 +0000232 if (authorize_subscriber(lchan->loc_operation, lchan->subscr)) {
Harald Welteee5ad162009-08-09 19:07:00 +0200233 int rc;
234
Holger Freytherd51524f2009-06-09 08:27:07 +0000235 db_subscriber_alloc_tmsi(lchan->subscr);
Holger Freytherd51524f2009-06-09 08:27:07 +0000236 release_loc_updating_req(lchan);
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200237 rc = gsm0408_loc_upd_acc(msg->lchan, lchan->subscr->tmsi);
Harald Welte648b6ce2009-12-14 09:00:24 +0100238 if (lchan->ts->trx->bts->network->send_mm_info) {
239 /* send MM INFO with network name */
240 rc = gsm48_tx_mm_info(msg->lchan);
241 }
Harald Welteb83d9382009-12-12 21:00:48 +0100242
Harald Welteee5ad162009-08-09 19:07:00 +0200243 /* call subscr_update after putting the loc_upd_acc
244 * in the transmit queue, since S_SUBSCR_ATTACHED might
245 * trigger further action like SMS delivery */
246 subscr_update(lchan->subscr, msg->trx->bts,
247 GSM_SUBSCRIBER_UPDATE_ATTACHED);
Harald Welteb83d9382009-12-12 21:00:48 +0100248 /* try to close channel ASAP */
249 lchan_auto_release(lchan);
Harald Welteee5ad162009-08-09 19:07:00 +0200250 return rc;
Holger Freytherd51524f2009-06-09 08:27:07 +0000251 }
252
253 return 0;
254}
255
Holger Freyther7c19f742009-06-06 13:54:35 +0000256static int gsm0408_handle_lchan_signal(unsigned int subsys, unsigned int signal,
257 void *handler_data, void *signal_data)
258{
Harald Welte4bfdfe72009-06-10 23:11:52 +0800259 struct gsm_trans *trans, *temp;
260
Holger Freyther7c19f742009-06-06 13:54:35 +0000261 if (subsys != SS_LCHAN || signal != S_LCHAN_UNEXPECTED_RELEASE)
262 return 0;
263
264 /*
265 * Cancel any outstanding location updating request
266 * operation taking place on the lchan.
267 */
Harald Welte1a5c6bd2009-07-04 09:35:21 +0200268 struct gsm_lchan *lchan = (struct gsm_lchan *)signal_data;
Harald Weltec05677b2009-06-26 20:17:06 +0200269 if (!lchan)
270 return 0;
271
Holger Freyther7c19f742009-06-06 13:54:35 +0000272 release_loc_updating_req(lchan);
273
Harald Welte4bfdfe72009-06-10 23:11:52 +0800274 /* Free all transactions that are associated with the released lchan */
Harald Weltedcaf5652009-07-23 18:56:43 +0200275 /* FIXME: this is not neccessarily the right thing to do, we should
276 * only set trans->lchan to NULL and wait for another lchan to be
277 * established to the same MM entity (phone/subscriber) */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800278 llist_for_each_entry_safe(trans, temp, &lchan->ts->trx->bts->network->trans_list, entry) {
279 if (trans->lchan == lchan)
Harald Weltedcaf5652009-07-23 18:56:43 +0200280 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +0800281 }
282
Holger Freyther7c19f742009-06-06 13:54:35 +0000283 return 0;
284}
285
Harald Welte09e38af2009-02-16 22:52:23 +0000286static const char bcd_num_digits[] = {
287 '0', '1', '2', '3', '4', '5', '6', '7',
288 '8', '9', '*', '#', 'a', 'b', 'c', '\0'
289};
290
Harald Welte0c389302009-06-10 12:08:54 +0800291/* decode a 'called/calling/connect party BCD number' as in 10.5.4.7 */
292int decode_bcd_number(char *output, int output_len, const u_int8_t *bcd_lv,
293 int h_len)
Harald Welte09e38af2009-02-16 22:52:23 +0000294{
295 u_int8_t in_len = bcd_lv[0];
296 int i;
297
Harald Welte0c389302009-06-10 12:08:54 +0800298 for (i = 1 + h_len; i <= in_len; i++) {
Harald Welte09e38af2009-02-16 22:52:23 +0000299 /* lower nibble */
300 output_len--;
301 if (output_len <= 1)
302 break;
303 *output++ = bcd_num_digits[bcd_lv[i] & 0xf];
304
305 /* higher nibble */
306 output_len--;
307 if (output_len <= 1)
308 break;
309 *output++ = bcd_num_digits[bcd_lv[i] >> 4];
310 }
311 if (output_len >= 1)
312 *output++ = '\0';
313
Harald Welte0c389302009-06-10 12:08:54 +0800314 return 0;
Harald Welte09e38af2009-02-16 22:52:23 +0000315}
316
317/* convert a single ASCII character to call-control BCD */
318static int asc_to_bcd(const char asc)
319{
320 int i;
321
322 for (i = 0; i < ARRAY_SIZE(bcd_num_digits); i++) {
323 if (bcd_num_digits[i] == asc)
324 return i;
325 }
326 return -EINVAL;
327}
328
Harald Welte0c389302009-06-10 12:08:54 +0800329/* convert a ASCII phone number to 'called/calling/connect party BCD number' */
Harald Welte09e38af2009-02-16 22:52:23 +0000330int encode_bcd_number(u_int8_t *bcd_lv, u_int8_t max_len,
Harald Welte0c389302009-06-10 12:08:54 +0800331 int h_len, const char *input)
Harald Welte09e38af2009-02-16 22:52:23 +0000332{
333 int in_len = strlen(input);
334 int i;
Harald Welte0c389302009-06-10 12:08:54 +0800335 u_int8_t *bcd_cur = bcd_lv + 1 + h_len;
Harald Welte09e38af2009-02-16 22:52:23 +0000336
337 /* two digits per byte, plus type byte */
Harald Welte0c389302009-06-10 12:08:54 +0800338 bcd_lv[0] = in_len/2 + h_len;
Harald Welte09e38af2009-02-16 22:52:23 +0000339 if (in_len % 2)
340 bcd_lv[0]++;
341
Harald Welte0c389302009-06-10 12:08:54 +0800342 if (bcd_lv[0] > max_len)
343 return -EIO;
Harald Welte09e38af2009-02-16 22:52:23 +0000344
345 for (i = 0; i < in_len; i++) {
346 int rc = asc_to_bcd(input[i]);
347 if (rc < 0)
348 return rc;
349 if (i % 2 == 0)
350 *bcd_cur = rc;
351 else
352 *bcd_cur++ |= (rc << 4);
353 }
354 /* append padding nibble in case of odd length */
355 if (i % 2)
356 *bcd_cur++ |= 0xf0;
357
358 /* return how many bytes we used */
359 return (bcd_cur - bcd_lv);
360}
361
Harald Welte0c389302009-06-10 12:08:54 +0800362/* decode 'bearer capability' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800363static int decode_bearer_cap(struct gsm_mncc_bearer_cap *bcap,
Harald Welte0c389302009-06-10 12:08:54 +0800364 const u_int8_t *lv)
365{
366 u_int8_t in_len = lv[0];
367 int i, s;
368
369 if (in_len < 1)
370 return -EINVAL;
371
Harald Welte4bfdfe72009-06-10 23:11:52 +0800372 bcap->speech_ver[0] = -1; /* end of list, of maximum 7 values */
Harald Welte0c389302009-06-10 12:08:54 +0800373
374 /* octet 3 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800375 bcap->transfer = lv[1] & 0x07;
376 bcap->mode = (lv[1] & 0x08) >> 3;
377 bcap->coding = (lv[1] & 0x10) >> 4;
378 bcap->radio = (lv[1] & 0x60) >> 5;
Harald Welte0c389302009-06-10 12:08:54 +0800379
Harald Welte09b7e7f2009-12-12 21:36:53 +0100380 if (bcap->transfer == GSM_MNCC_BCAP_SPEECH) {
381 i = 1;
382 s = 0;
383 while(!(lv[i] & 0x80)) {
384 i++; /* octet 3a etc */
385 if (in_len < i)
386 return 0;
387 bcap->speech_ver[s++] = lv[i] & 0x0f;
388 bcap->speech_ver[s] = -1; /* end of list */
389 if (i == 2) /* octet 3a */
390 bcap->speech_ctm = (lv[i] & 0x20) >> 5;
391 if (s == 7) /* maximum speech versions + end of list */
392 return 0;
393 }
394 } else {
395 i = 1;
396 while (!(lv[i] & 0x80)) {
397 i++; /* octet 3a etc */
398 if (in_len < i)
399 return 0;
400 /* ignore them */
401 }
402 /* FIXME: implement OCTET 4+ parsing */
Harald Welte0c389302009-06-10 12:08:54 +0800403 }
404
405 return 0;
406}
407
408/* encode 'bearer capability' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800409static int encode_bearer_cap(struct msgb *msg, int lv_only,
410 const struct gsm_mncc_bearer_cap *bcap)
Harald Welte0c389302009-06-10 12:08:54 +0800411{
412 u_int8_t lv[32 + 1];
Harald Welte09b7e7f2009-12-12 21:36:53 +0100413 int i = 1, s;
Harald Welte0c389302009-06-10 12:08:54 +0800414
Harald Welte4bfdfe72009-06-10 23:11:52 +0800415 lv[1] = bcap->transfer;
416 lv[1] |= bcap->mode << 3;
417 lv[1] |= bcap->coding << 4;
418 lv[1] |= bcap->radio << 5;
Harald Welte0c389302009-06-10 12:08:54 +0800419
Harald Welte09b7e7f2009-12-12 21:36:53 +0100420 if (bcap->transfer == GSM_MNCC_BCAP_SPEECH) {
421 for (s = 0; bcap->speech_ver[s] >= 0; s++) {
422 i++; /* octet 3a etc */
423 lv[i] = bcap->speech_ver[s];
424 if (i == 2) /* octet 3a */
425 lv[i] |= bcap->speech_ctm << 5;
426 }
427 lv[i] |= 0x80; /* last IE of octet 3 etc */
428 } else {
429 /* FIXME: implement OCTET 4+ encoding */
Harald Welte0c389302009-06-10 12:08:54 +0800430 }
Harald Welte0c389302009-06-10 12:08:54 +0800431
432 lv[0] = i;
433 if (lv_only)
434 msgb_lv_put(msg, lv[0], lv+1);
435 else
436 msgb_tlv_put(msg, GSM48_IE_BEARER_CAP, lv[0], lv+1);
437
438 return 0;
439}
440
441/* decode 'call control cap' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800442static int decode_cccap(struct gsm_mncc_cccap *ccap, const u_int8_t *lv)
Harald Welte0c389302009-06-10 12:08:54 +0800443{
444 u_int8_t in_len = lv[0];
445
446 if (in_len < 1)
447 return -EINVAL;
448
449 /* octet 3 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800450 ccap->dtmf = lv[1] & 0x01;
451 ccap->pcp = (lv[1] & 0x02) >> 1;
Harald Welte0c389302009-06-10 12:08:54 +0800452
453 return 0;
454}
455
456/* decode 'called party BCD number' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800457static int decode_called(struct gsm_mncc_number *called,
458 const u_int8_t *lv)
Harald Welte0c389302009-06-10 12:08:54 +0800459{
460 u_int8_t in_len = lv[0];
461
462 if (in_len < 1)
463 return -EINVAL;
464
465 /* octet 3 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800466 called->plan = lv[1] & 0x0f;
467 called->type = (lv[1] & 0x70) >> 4;
Harald Welte0c389302009-06-10 12:08:54 +0800468
469 /* octet 4..N */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800470 decode_bcd_number(called->number, sizeof(called->number), lv, 1);
Harald Welte0c389302009-06-10 12:08:54 +0800471
472 return 0;
473}
474
475/* encode 'called party BCD number' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800476static int encode_called(struct msgb *msg,
477 const struct gsm_mncc_number *called)
Harald Welte0c389302009-06-10 12:08:54 +0800478{
479 u_int8_t lv[18];
480 int ret;
481
482 /* octet 3 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800483 lv[1] = called->plan;
484 lv[1] |= called->type << 4;
Harald Welte0c389302009-06-10 12:08:54 +0800485
486 /* octet 4..N, octet 2 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800487 ret = encode_bcd_number(lv, sizeof(lv), 1, called->number);
Harald Welte0c389302009-06-10 12:08:54 +0800488 if (ret < 0)
489 return ret;
490
491 msgb_tlv_put(msg, GSM48_IE_CALLED_BCD, lv[0], lv+1);
492
493 return 0;
494}
495
496/* encode callerid of various IEs */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800497static int encode_callerid(struct msgb *msg, int ie,
498 const struct gsm_mncc_number *callerid)
Harald Welte0c389302009-06-10 12:08:54 +0800499{
500 u_int8_t lv[13];
501 int h_len = 1;
502 int ret;
503
504 /* octet 3 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800505 lv[1] = callerid->plan;
506 lv[1] |= callerid->type << 4;
Harald Welte0c389302009-06-10 12:08:54 +0800507
Harald Welte4bfdfe72009-06-10 23:11:52 +0800508 if (callerid->present || callerid->screen) {
Harald Welte0c389302009-06-10 12:08:54 +0800509 /* octet 3a */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800510 lv[2] = callerid->screen;
511 lv[2] |= callerid->present << 5;
Harald Welte0c389302009-06-10 12:08:54 +0800512 lv[2] |= 0x80;
513 h_len++;
514 } else
515 lv[1] |= 0x80;
516
517 /* octet 4..N, octet 2 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800518 ret = encode_bcd_number(lv, sizeof(lv), h_len, callerid->number);
Harald Welte0c389302009-06-10 12:08:54 +0800519 if (ret < 0)
520 return ret;
521
522 msgb_tlv_put(msg, ie, lv[0], lv+1);
523
524 return 0;
525}
526
527/* decode 'cause' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800528static int decode_cause(struct gsm_mncc_cause *cause,
Harald Welte0c389302009-06-10 12:08:54 +0800529 const u_int8_t *lv)
530{
531 u_int8_t in_len = lv[0];
532 int i;
533
534 if (in_len < 2)
535 return -EINVAL;
536
Harald Welte4bfdfe72009-06-10 23:11:52 +0800537 cause->diag_len = 0;
Harald Welte0c389302009-06-10 12:08:54 +0800538
539 /* octet 3 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800540 cause->location = lv[1] & 0x0f;
541 cause->coding = (lv[1] & 0x60) >> 5;
Harald Welte0c389302009-06-10 12:08:54 +0800542
543 i = 1;
544 if (!(lv[i] & 0x80)) {
545 i++; /* octet 3a */
546 if (in_len < i+1)
547 return 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800548 cause->rec = 1;
549 cause->rec_val = lv[i] & 0x7f;
Harald Welte0c389302009-06-10 12:08:54 +0800550
551 }
552 i++;
553
554 /* octet 4 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800555 cause->value = lv[i] & 0x7f;
Harald Welte0c389302009-06-10 12:08:54 +0800556 i++;
557
558 if (in_len < i) /* no diag */
559 return 0;
560
561 if (in_len - (i-1) > 32) /* maximum 32 octets */
562 return 0;
563
564 /* octet 5-N */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800565 memcpy(cause->diag, lv + i, in_len - (i-1));
566 cause->diag_len = in_len - (i-1);
Harald Welte0c389302009-06-10 12:08:54 +0800567
568 return 0;
569}
570
571/* encode 'cause' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800572static int encode_cause(struct msgb *msg, int lv_only,
573 const struct gsm_mncc_cause *cause)
Harald Welte0c389302009-06-10 12:08:54 +0800574{
575 u_int8_t lv[32+4];
576 int i;
577
Harald Welte4bfdfe72009-06-10 23:11:52 +0800578 if (cause->diag_len > 32)
Harald Welte0c389302009-06-10 12:08:54 +0800579 return -EINVAL;
580
581 /* octet 3 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800582 lv[1] = cause->location;
583 lv[1] |= cause->coding << 5;
Harald Welte0c389302009-06-10 12:08:54 +0800584
585 i = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800586 if (cause->rec) {
Harald Welte0c389302009-06-10 12:08:54 +0800587 i++; /* octet 3a */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800588 lv[i] = cause->rec_val;
Harald Welte0c389302009-06-10 12:08:54 +0800589 }
590 lv[i] |= 0x80; /* end of octet 3 */
591
592 /* octet 4 */
593 i++;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800594 lv[i] = 0x80 | cause->value;
Harald Welte0c389302009-06-10 12:08:54 +0800595
596 /* octet 5-N */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800597 if (cause->diag_len) {
598 memcpy(lv + i, cause->diag, cause->diag_len);
599 i += cause->diag_len;
Harald Welte0c389302009-06-10 12:08:54 +0800600 }
601
602 lv[0] = i;
603 if (lv_only)
604 msgb_lv_put(msg, lv[0], lv+1);
605 else
606 msgb_tlv_put(msg, GSM48_IE_CAUSE, lv[0], lv+1);
607
608 return 0;
609}
610
611/* encode 'calling number' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800612static int encode_calling(struct msgb *msg,
613 const struct gsm_mncc_number *calling)
Harald Welte0c389302009-06-10 12:08:54 +0800614{
Harald Welte4bfdfe72009-06-10 23:11:52 +0800615 return encode_callerid(msg, GSM48_IE_CALLING_BCD, calling);
Harald Welte0c389302009-06-10 12:08:54 +0800616}
617
618/* encode 'connected number' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800619static int encode_connected(struct msgb *msg,
620 const struct gsm_mncc_number *connected)
Harald Welte0c389302009-06-10 12:08:54 +0800621{
Harald Welte4bfdfe72009-06-10 23:11:52 +0800622 return encode_callerid(msg, GSM48_IE_CONN_BCD, connected);
Harald Welte0c389302009-06-10 12:08:54 +0800623}
624
625/* encode 'redirecting number' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800626static int encode_redirecting(struct msgb *msg,
627 const struct gsm_mncc_number *redirecting)
Harald Welte0c389302009-06-10 12:08:54 +0800628{
Harald Welte4bfdfe72009-06-10 23:11:52 +0800629 return encode_callerid(msg, GSM48_IE_REDIR_BCD, redirecting);
Harald Welte0c389302009-06-10 12:08:54 +0800630}
631
632/* decode 'facility' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800633static int decode_facility(struct gsm_mncc_facility *facility,
Harald Welte0c389302009-06-10 12:08:54 +0800634 const u_int8_t *lv)
635{
636 u_int8_t in_len = lv[0];
637
638 if (in_len < 1)
639 return -EINVAL;
640
Harald Welte4bfdfe72009-06-10 23:11:52 +0800641 if (in_len > sizeof(facility->info))
Harald Welte0c389302009-06-10 12:08:54 +0800642 return -EINVAL;
643
Harald Welte4bfdfe72009-06-10 23:11:52 +0800644 memcpy(facility->info, lv+1, in_len);
645 facility->len = in_len;
Harald Welte0c389302009-06-10 12:08:54 +0800646
647 return 0;
648}
649
650/* encode 'facility' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800651static int encode_facility(struct msgb *msg, int lv_only,
652 const struct gsm_mncc_facility *facility)
Harald Welte0c389302009-06-10 12:08:54 +0800653{
654 u_int8_t lv[GSM_MAX_FACILITY + 1];
655
Harald Welte4bfdfe72009-06-10 23:11:52 +0800656 if (facility->len < 1 || facility->len > GSM_MAX_FACILITY)
Harald Welte0c389302009-06-10 12:08:54 +0800657 return -EINVAL;
658
Harald Welte4bfdfe72009-06-10 23:11:52 +0800659 memcpy(lv+1, facility->info, facility->len);
660 lv[0] = facility->len;
Harald Welte0c389302009-06-10 12:08:54 +0800661 if (lv_only)
662 msgb_lv_put(msg, lv[0], lv+1);
663 else
664 msgb_tlv_put(msg, GSM48_IE_FACILITY, lv[0], lv+1);
665
666 return 0;
667}
668
669/* decode 'notify' */
670static int decode_notify(int *notify, const u_int8_t *v)
671{
672 *notify = v[0] & 0x7f;
673
674 return 0;
675}
676
677/* encode 'notify' */
678static int encode_notify(struct msgb *msg, int notify)
679{
680 msgb_v_put(msg, notify | 0x80);
681
682 return 0;
683}
684
685/* encode 'signal' */
686static int encode_signal(struct msgb *msg, int signal)
687{
688 msgb_tv_put(msg, GSM48_IE_SIGNAL, signal);
689
690 return 0;
691}
692
693/* decode 'keypad' */
694static int decode_keypad(int *keypad, const u_int8_t *lv)
695{
696 u_int8_t in_len = lv[0];
697
698 if (in_len < 1)
699 return -EINVAL;
700
701 *keypad = lv[1] & 0x7f;
702
703 return 0;
704}
705
706/* encode 'keypad' */
707static int encode_keypad(struct msgb *msg, int keypad)
708{
709 msgb_tv_put(msg, GSM48_IE_KPD_FACILITY, keypad);
710
711 return 0;
712}
713
714/* decode 'progress' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800715static int decode_progress(struct gsm_mncc_progress *progress,
Harald Welte0c389302009-06-10 12:08:54 +0800716 const u_int8_t *lv)
717{
718 u_int8_t in_len = lv[0];
719
720 if (in_len < 2)
721 return -EINVAL;
722
Harald Welte4bfdfe72009-06-10 23:11:52 +0800723 progress->coding = (lv[1] & 0x60) >> 5;
724 progress->location = lv[1] & 0x0f;
725 progress->descr = lv[2] & 0x7f;
Harald Welte0c389302009-06-10 12:08:54 +0800726
727 return 0;
728}
729
730/* encode 'progress' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800731static int encode_progress(struct msgb *msg, int lv_only,
732 const struct gsm_mncc_progress *p)
Harald Welte0c389302009-06-10 12:08:54 +0800733{
734 u_int8_t lv[3];
735
736 lv[0] = 2;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800737 lv[1] = 0x80 | ((p->coding & 0x3) << 5) | (p->location & 0xf);
738 lv[2] = 0x80 | (p->descr & 0x7f);
Harald Welte0c389302009-06-10 12:08:54 +0800739 if (lv_only)
740 msgb_lv_put(msg, lv[0], lv+1);
741 else
742 msgb_tlv_put(msg, GSM48_IE_PROGR_IND, lv[0], lv+1);
743
744 return 0;
745}
746
747/* decode 'user-user' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800748static int decode_useruser(struct gsm_mncc_useruser *uu,
Harald Welte0c389302009-06-10 12:08:54 +0800749 const u_int8_t *lv)
750{
751 u_int8_t in_len = lv[0];
Harald Welte4bfdfe72009-06-10 23:11:52 +0800752 char *info = uu->info;
753 int info_len = sizeof(uu->info);
Harald Welte0c389302009-06-10 12:08:54 +0800754 int i;
755
756 if (in_len < 1)
757 return -EINVAL;
758
Harald Welte4bfdfe72009-06-10 23:11:52 +0800759 uu->proto = lv[1];
Harald Welte0c389302009-06-10 12:08:54 +0800760
761 for (i = 2; i <= in_len; i++) {
762 info_len--;
763 if (info_len <= 1)
764 break;
765 *info++ = lv[i];
766 }
767 if (info_len >= 1)
768 *info++ = '\0';
769
770 return 0;
771}
772
773/* encode 'useruser' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800774static int encode_useruser(struct msgb *msg, int lv_only,
775 const struct gsm_mncc_useruser *uu)
Harald Welte0c389302009-06-10 12:08:54 +0800776{
777 u_int8_t lv[GSM_MAX_USERUSER + 2];
778
Harald Welte4bfdfe72009-06-10 23:11:52 +0800779 if (strlen(uu->info) > GSM_MAX_USERUSER)
Harald Welte0c389302009-06-10 12:08:54 +0800780 return -EINVAL;
781
Harald Welte4bfdfe72009-06-10 23:11:52 +0800782 lv[0] = 1 + strlen(uu->info);
783 lv[1] = uu->proto;
784 memcpy(lv + 2, uu->info, strlen(uu->info));
Harald Welte0c389302009-06-10 12:08:54 +0800785 if (lv_only)
786 msgb_lv_put(msg, lv[0], lv+1);
787 else
788 msgb_tlv_put(msg, GSM48_IE_USER_USER, lv[0], lv+1);
789
790 return 0;
791}
792
793/* decode 'ss version' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800794static int decode_ssversion(struct gsm_mncc_ssversion *ssv,
Harald Welte0c389302009-06-10 12:08:54 +0800795 const u_int8_t *lv)
796{
797 u_int8_t in_len = lv[0];
798
Harald Welte4bfdfe72009-06-10 23:11:52 +0800799 if (in_len < 1 || in_len < sizeof(ssv->info))
Harald Welte0c389302009-06-10 12:08:54 +0800800 return -EINVAL;
801
Harald Welte4bfdfe72009-06-10 23:11:52 +0800802 memcpy(ssv->info, lv + 1, in_len);
803 ssv->len = in_len;
Harald Welte0c389302009-06-10 12:08:54 +0800804
805 return 0;
806}
807
808/* encode 'more data' */
809static int encode_more(struct msgb *msg)
810{
811 u_int8_t *ie;
812
813 ie = msgb_put(msg, 1);
814 ie[0] = GSM48_IE_MORE_DATA;
815
816 return 0;
817}
818
Holger Freyther429e7762008-12-30 13:28:30 +0000819/* Chapter 9.2.14 : Send LOCATION UPDATING REJECT */
Harald Welte8470bf22008-12-25 23:28:35 +0000820int gsm0408_loc_upd_rej(struct gsm_lchan *lchan, u_int8_t cause)
Harald Welte52b1f982008-12-23 20:25:15 +0000821{
Harald Welte24ff6ee2009-12-22 00:41:05 +0100822 struct gsm_bts *bts = lchan->ts->trx->bts;
Harald Welte8470bf22008-12-25 23:28:35 +0000823 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000824 struct gsm48_hdr *gh;
825
Harald Welte8470bf22008-12-25 23:28:35 +0000826 msg->lchan = lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000827
828 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
829 gh->proto_discr = GSM48_PDISC_MM;
Harald Welte10b487b2008-12-27 19:53:37 +0000830 gh->msg_type = GSM48_MT_MM_LOC_UPD_REJECT;
Harald Welte52b1f982008-12-23 20:25:15 +0000831 gh->data[0] = cause;
832
Harald Welte (local)198d5ad2009-12-26 22:15:28 +0100833 LOGP(DMM, LOGL_INFO, "Subscriber %s: LOCATION UPDATING REJECT "
834 "LAC=%u BTS=%u\n", lchan->subscr ?
835 subscr_name(lchan->subscr) : "unknown",
836 lchan->ts->trx->bts->location_area_code, lchan->ts->trx->bts->nr);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100837
Harald Welteffa55a42009-12-22 19:07:32 +0100838 counter_inc(bts->network->stats.loc_upd_resp.reject);
Harald Weltedb253af2008-12-30 17:56:55 +0000839
Harald Welte39e2ead2009-07-23 21:13:03 +0200840 return gsm48_sendmsg(msg, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000841}
842
843/* Chapter 9.2.13 : Send LOCATION UPDATE ACCEPT */
Harald Welte75a983f2008-12-27 21:34:06 +0000844int gsm0408_loc_upd_acc(struct gsm_lchan *lchan, u_int32_t tmsi)
Harald Welte52b1f982008-12-23 20:25:15 +0000845{
Harald Welte8470bf22008-12-25 23:28:35 +0000846 struct gsm_bts *bts = lchan->ts->trx->bts;
847 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000848 struct gsm48_hdr *gh;
849 struct gsm48_loc_area_id *lai;
850 u_int8_t *mid;
851
Harald Welte8470bf22008-12-25 23:28:35 +0000852 msg->lchan = lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000853
854 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
855 gh->proto_discr = GSM48_PDISC_MM;
856 gh->msg_type = GSM48_MT_MM_LOC_UPD_ACCEPT;
857
858 lai = (struct gsm48_loc_area_id *) msgb_put(msg, sizeof(*lai));
Holger Freyther17746612008-12-28 16:32:44 +0000859 gsm0408_generate_lai(lai, bts->network->country_code,
Harald Welte52b1f982008-12-23 20:25:15 +0000860 bts->network->network_code, bts->location_area_code);
861
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +0200862 mid = msgb_put(msg, GSM48_MID_TMSI_LEN);
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +0200863 gsm48_generate_mid_from_tmsi(mid, tmsi);
Harald Welte52b1f982008-12-23 20:25:15 +0000864
865 DEBUGP(DMM, "-> LOCATION UPDATE ACCEPT\n");
866
Harald Welteffa55a42009-12-22 19:07:32 +0100867 counter_inc(bts->network->stats.loc_upd_resp.accept);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100868
Harald Welteb83d9382009-12-12 21:00:48 +0100869 return gsm48_sendmsg(msg, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000870}
871
Harald Weltebf5e8df2009-02-03 12:59:45 +0000872/* Transmit Chapter 9.2.10 Identity Request */
Harald Welte231ad4f2008-12-27 11:15:38 +0000873static int mm_tx_identity_req(struct gsm_lchan *lchan, u_int8_t id_type)
874{
875 struct msgb *msg = gsm48_msgb_alloc();
876 struct gsm48_hdr *gh;
Harald Weltefc977a82008-12-27 10:19:37 +0000877
Harald Welte231ad4f2008-12-27 11:15:38 +0000878 msg->lchan = lchan;
879
880 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
881 gh->proto_discr = GSM48_PDISC_MM;
882 gh->msg_type = GSM48_MT_MM_ID_REQ;
883 gh->data[0] = id_type;
884
Harald Welte39e2ead2009-07-23 21:13:03 +0200885 return gsm48_sendmsg(msg, NULL);
Harald Welte231ad4f2008-12-27 11:15:38 +0000886}
887
Harald Welte231ad4f2008-12-27 11:15:38 +0000888
Harald Weltebf5e8df2009-02-03 12:59:45 +0000889/* Parse Chapter 9.2.11 Identity Response */
Harald Welte231ad4f2008-12-27 11:15:38 +0000890static int mm_rx_id_resp(struct msgb *msg)
891{
892 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte75a983f2008-12-27 21:34:06 +0000893 struct gsm_lchan *lchan = msg->lchan;
Harald Welte9176bd42009-07-23 18:46:00 +0200894 struct gsm_bts *bts = lchan->ts->trx->bts;
895 struct gsm_network *net = bts->network;
Harald Welte231ad4f2008-12-27 11:15:38 +0000896 u_int8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200897 char mi_string[GSM48_MI_SIZE];
Harald Welte231ad4f2008-12-27 11:15:38 +0000898
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200899 gsm48_mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
Harald Welte61253062008-12-27 11:25:50 +0000900 DEBUGP(DMM, "IDENTITY RESPONSE: mi_type=0x%02x MI(%s)\n",
Harald Welte231ad4f2008-12-27 11:15:38 +0000901 mi_type, mi_string);
902
Harald Welte7659de12009-12-13 12:39:18 +0100903 dispatch_signal(SS_SUBSCR, S_SUBSCR_IDENTITY, gh->data);
904
Harald Welte75a983f2008-12-27 21:34:06 +0000905 switch (mi_type) {
906 case GSM_MI_TYPE_IMSI:
Jan Luebbe370b41d2009-08-12 10:19:34 +0200907 /* look up subscriber based on IMSI, create if not found */
908 if (!lchan->subscr) {
909 lchan->subscr = subscr_get_by_imsi(net, mi_string);
Harald Welte (local)aa9dc192009-08-13 13:49:51 +0200910 if (!lchan->subscr)
911 lchan->subscr = db_create_subscriber(net, mi_string);
Jan Luebbeb0dfc312009-08-12 10:12:52 +0200912 }
Holger Freyther73487a22008-12-31 18:53:57 +0000913 if (lchan->loc_operation)
914 lchan->loc_operation->waiting_for_imsi = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000915 break;
916 case GSM_MI_TYPE_IMEI:
Harald Welte255539c2008-12-28 02:26:27 +0000917 case GSM_MI_TYPE_IMEISV:
Harald Welte75a983f2008-12-27 21:34:06 +0000918 /* update subscribe <-> IMEI mapping */
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200919 if (lchan->subscr) {
Harald Welte75a983f2008-12-27 21:34:06 +0000920 db_subscriber_assoc_imei(lchan->subscr, mi_string);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200921 db_sync_equipment(&lchan->subscr->equipment);
922 }
Holger Freyther73487a22008-12-31 18:53:57 +0000923 if (lchan->loc_operation)
924 lchan->loc_operation->waiting_for_imei = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000925 break;
926 }
Holger Freyther73487a22008-12-31 18:53:57 +0000927
928 /* Check if we can let the mobile station enter */
Holger Freytherd51524f2009-06-09 08:27:07 +0000929 return gsm0408_authorize(lchan, msg);
Harald Welte231ad4f2008-12-27 11:15:38 +0000930}
931
Harald Welte255539c2008-12-28 02:26:27 +0000932
933static void loc_upd_rej_cb(void *data)
934{
935 struct gsm_lchan *lchan = data;
Harald Welte1085c092009-11-18 20:33:19 +0100936 struct gsm_bts *bts = lchan->ts->trx->bts;
Harald Welte255539c2008-12-28 02:26:27 +0000937
Holger Freyther73487a22008-12-31 18:53:57 +0000938 release_loc_updating_req(lchan);
Harald Welte1085c092009-11-18 20:33:19 +0100939 gsm0408_loc_upd_rej(lchan, bts->network->reject_cause);
Holger Freyther67b4b9a2009-01-01 03:46:11 +0000940 lchan_auto_release(lchan);
Harald Welte255539c2008-12-28 02:26:27 +0000941}
942
Holger Freytherb7193e42008-12-29 17:44:08 +0000943static void schedule_reject(struct gsm_lchan *lchan)
944{
Holger Freyther73487a22008-12-31 18:53:57 +0000945 lchan->loc_operation->updating_timer.cb = loc_upd_rej_cb;
946 lchan->loc_operation->updating_timer.data = lchan;
Harald Welteff117a82009-05-23 05:22:08 +0000947 bsc_schedule_timer(&lchan->loc_operation->updating_timer, 5, 0);
Holger Freytherb7193e42008-12-29 17:44:08 +0000948}
949
Harald Welte2a139372009-02-22 21:14:55 +0000950static const char *lupd_name(u_int8_t type)
951{
952 switch (type) {
953 case GSM48_LUPD_NORMAL:
954 return "NORMAL";
955 case GSM48_LUPD_PERIODIC:
956 return "PEROIDOC";
957 case GSM48_LUPD_IMSI_ATT:
958 return "IMSI ATTACH";
959 default:
960 return "UNKNOWN";
961 }
962}
963
Harald Weltebf5e8df2009-02-03 12:59:45 +0000964/* Chapter 9.2.15: Receive Location Updating Request */
Harald Welte231ad4f2008-12-27 11:15:38 +0000965static int mm_rx_loc_upd_req(struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000966{
Harald Welte8470bf22008-12-25 23:28:35 +0000967 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000968 struct gsm48_loc_upd_req *lu;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800969 struct gsm_subscriber *subscr = NULL;
Harald Welte255539c2008-12-28 02:26:27 +0000970 struct gsm_lchan *lchan = msg->lchan;
Harald Welte9176bd42009-07-23 18:46:00 +0200971 struct gsm_bts *bts = lchan->ts->trx->bts;
Harald Welte8470bf22008-12-25 23:28:35 +0000972 u_int8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200973 char mi_string[GSM48_MI_SIZE];
Harald Welte231ad4f2008-12-27 11:15:38 +0000974 int rc;
Harald Welte52b1f982008-12-23 20:25:15 +0000975
Harald Welte8470bf22008-12-25 23:28:35 +0000976 lu = (struct gsm48_loc_upd_req *) gh->data;
977
978 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
Harald Welte52b1f982008-12-23 20:25:15 +0000979
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200980 gsm48_mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
Harald Weltefc977a82008-12-27 10:19:37 +0000981
Harald Weltea0368542009-06-27 02:58:43 +0200982 DEBUGPC(DMM, "mi_type=0x%02x MI(%s) type=%s ", mi_type, mi_string,
Harald Welte2a139372009-02-22 21:14:55 +0000983 lupd_name(lu->type));
Holger Freyther73487a22008-12-31 18:53:57 +0000984
Harald Welte7659de12009-12-13 12:39:18 +0100985 dispatch_signal(SS_SUBSCR, S_SUBSCR_IDENTITY, &lu->mi_len);
986
Harald Welte24ff6ee2009-12-22 00:41:05 +0100987 switch (lu->type) {
988 case GSM48_LUPD_NORMAL:
Harald Welteffa55a42009-12-22 19:07:32 +0100989 counter_inc(bts->network->stats.loc_upd_type.normal);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100990 break;
991 case GSM48_LUPD_IMSI_ATT:
Harald Welteffa55a42009-12-22 19:07:32 +0100992 counter_inc(bts->network->stats.loc_upd_type.attach);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100993 break;
994 case GSM48_LUPD_PERIODIC:
Harald Welteffa55a42009-12-22 19:07:32 +0100995 counter_inc(bts->network->stats.loc_upd_type.periodic);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100996 break;
997 }
998
Holger Freythereaf04692009-06-06 13:54:44 +0000999 /*
1000 * Pseudo Spoof detection: Just drop a second/concurrent
1001 * location updating request.
1002 */
1003 if (lchan->loc_operation) {
Harald Weltea0368542009-06-27 02:58:43 +02001004 DEBUGPC(DMM, "ignoring request due an existing one: %p.\n",
Holger Freythereaf04692009-06-06 13:54:44 +00001005 lchan->loc_operation);
1006 gsm0408_loc_upd_rej(lchan, GSM48_REJECT_PROTOCOL_ERROR);
1007 return 0;
1008 }
1009
Holger Freyther73487a22008-12-31 18:53:57 +00001010 allocate_loc_updating_req(lchan);
1011
Harald Welte52b1f982008-12-23 20:25:15 +00001012 switch (mi_type) {
1013 case GSM_MI_TYPE_IMSI:
Harald Weltea0368542009-06-27 02:58:43 +02001014 DEBUGPC(DMM, "\n");
Harald Welte231ad4f2008-12-27 11:15:38 +00001015 /* we always want the IMEI, too */
Harald Welte015b9ad2009-02-28 18:22:03 +00001016 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMEI);
Holger Freyther73487a22008-12-31 18:53:57 +00001017 lchan->loc_operation->waiting_for_imei = 1;
Holger Freytherc6ea9db2008-12-30 19:18:21 +00001018
Jan Luebbe370b41d2009-08-12 10:19:34 +02001019 /* look up subscriber based on IMSI, create if not found */
1020 subscr = subscr_get_by_imsi(bts->network, mi_string);
1021 if (!subscr) {
1022 subscr = db_create_subscriber(bts->network, mi_string);
1023 }
Harald Welte4b634542008-12-27 01:55:51 +00001024 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001025 case GSM_MI_TYPE_TMSI:
Harald Weltea0368542009-06-27 02:58:43 +02001026 DEBUGPC(DMM, "\n");
Harald Welte231ad4f2008-12-27 11:15:38 +00001027 /* we always want the IMEI, too */
Harald Welte015b9ad2009-02-28 18:22:03 +00001028 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMEI);
Holger Freyther73487a22008-12-31 18:53:57 +00001029 lchan->loc_operation->waiting_for_imei = 1;
Holger Freytherc6ea9db2008-12-30 19:18:21 +00001030
Harald Welte52b1f982008-12-23 20:25:15 +00001031 /* look up the subscriber based on TMSI, request IMSI if it fails */
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +02001032 subscr = subscr_get_by_tmsi(bts->network,
1033 tmsi_from_string(mi_string));
Harald Welte52b1f982008-12-23 20:25:15 +00001034 if (!subscr) {
Harald Welte231ad4f2008-12-27 11:15:38 +00001035 /* send IDENTITY REQUEST message to get IMSI */
Harald Welte255539c2008-12-28 02:26:27 +00001036 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMSI);
Holger Freyther73487a22008-12-31 18:53:57 +00001037 lchan->loc_operation->waiting_for_imsi = 1;
Harald Welte52b1f982008-12-23 20:25:15 +00001038 }
1039 break;
1040 case GSM_MI_TYPE_IMEI:
1041 case GSM_MI_TYPE_IMEISV:
1042 /* no sim card... FIXME: what to do ? */
Harald Weltea0368542009-06-27 02:58:43 +02001043 DEBUGPC(DMM, "unimplemented mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +00001044 break;
1045 default:
Harald Weltea0368542009-06-27 02:58:43 +02001046 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +00001047 break;
1048 }
1049
Harald Welte24516ea2009-07-04 10:18:00 +02001050 /* schedule the reject timer */
1051 schedule_reject(lchan);
1052
Harald Welte4bfdfe72009-06-10 23:11:52 +08001053 if (!subscr) {
Harald Weltea0368542009-06-27 02:58:43 +02001054 DEBUGPC(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001055 /* FIXME: request id? close channel? */
1056 return -EINVAL;
1057 }
1058
Harald Welte255539c2008-12-28 02:26:27 +00001059 lchan->subscr = subscr;
Harald Welte (local)ee4410a2009-08-17 09:39:55 +02001060 lchan->subscr->equipment.classmark1 = lu->classmark1;
Harald Welte255539c2008-12-28 02:26:27 +00001061
Harald Welte24516ea2009-07-04 10:18:00 +02001062 /* check if we can let the subscriber into our network immediately
1063 * or if we need to wait for identity responses. */
Holger Freytherd51524f2009-06-09 08:27:07 +00001064 return gsm0408_authorize(lchan, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001065}
1066
Harald Welte4bfdfe72009-06-10 23:11:52 +08001067#if 0
1068static u_int8_t to_bcd8(u_int8_t val)
1069{
1070 return ((val / 10) << 4) | (val % 10);
1071}
1072#endif
1073
Harald Weltedb253af2008-12-30 17:56:55 +00001074/* Section 9.2.15a */
1075int gsm48_tx_mm_info(struct gsm_lchan *lchan)
1076{
1077 struct msgb *msg = gsm48_msgb_alloc();
1078 struct gsm48_hdr *gh;
1079 struct gsm_network *net = lchan->ts->trx->bts->network;
Harald Weltedb253af2008-12-30 17:56:55 +00001080 u_int8_t *ptr8;
Daniel Willmanneea93372009-08-13 03:42:07 +02001081 int name_len, name_pad;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001082#if 0
1083 time_t cur_t;
1084 struct tm* cur_time;
1085 int tz15min;
1086#endif
Harald Weltedb253af2008-12-30 17:56:55 +00001087
1088 msg->lchan = lchan;
1089
1090 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1091 gh->proto_discr = GSM48_PDISC_MM;
1092 gh->msg_type = GSM48_MT_MM_INFO;
1093
1094 if (net->name_long) {
Daniel Willmanneea93372009-08-13 03:42:07 +02001095#if 0
Harald Weltedb253af2008-12-30 17:56:55 +00001096 name_len = strlen(net->name_long);
1097 /* 10.5.3.5a */
1098 ptr8 = msgb_put(msg, 3);
1099 ptr8[0] = GSM48_IE_NAME_LONG;
1100 ptr8[1] = name_len*2 +1;
1101 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
1102
1103 ptr16 = (u_int16_t *) msgb_put(msg, name_len*2);
1104 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +00001105 ptr16[i] = htons(net->name_long[i]);
Harald Weltedb253af2008-12-30 17:56:55 +00001106
1107 /* FIXME: Use Cell Broadcast, not UCS-2, since
1108 * UCS-2 is only supported by later revisions of the spec */
Daniel Willmanneea93372009-08-13 03:42:07 +02001109#endif
1110 name_len = (strlen(net->name_long)*7)/8;
1111 name_pad = (8 - strlen(net->name_long)*7)%8;
1112 if (name_pad > 0)
1113 name_len++;
1114 /* 10.5.3.5a */
1115 ptr8 = msgb_put(msg, 3);
1116 ptr8[0] = GSM48_IE_NAME_LONG;
1117 ptr8[1] = name_len +1;
1118 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
1119
1120 ptr8 = msgb_put(msg, name_len);
1121 gsm_7bit_encode(ptr8, net->name_long);
1122
Harald Weltedb253af2008-12-30 17:56:55 +00001123 }
1124
1125 if (net->name_short) {
Daniel Willmanneea93372009-08-13 03:42:07 +02001126#if 0
Harald Weltedb253af2008-12-30 17:56:55 +00001127 name_len = strlen(net->name_short);
1128 /* 10.5.3.5a */
1129 ptr8 = (u_int8_t *) msgb_put(msg, 3);
Harald Welte7543eb72009-07-19 17:51:36 +02001130 ptr8[0] = GSM48_IE_NAME_SHORT;
Harald Weltedb253af2008-12-30 17:56:55 +00001131 ptr8[1] = name_len*2 + 1;
1132 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
1133
Harald Weltee872cb12009-01-01 00:33:37 +00001134 ptr16 = (u_int16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +00001135 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +00001136 ptr16[i] = htons(net->name_short[i]);
Daniel Willmanneea93372009-08-13 03:42:07 +02001137#endif
1138 name_len = (strlen(net->name_short)*7)/8;
1139 name_pad = (8 - strlen(net->name_short)*7)%8;
1140 if (name_pad > 0)
1141 name_len++;
1142 /* 10.5.3.5a */
1143 ptr8 = (u_int8_t *) msgb_put(msg, 3);
1144 ptr8[0] = GSM48_IE_NAME_SHORT;
1145 ptr8[1] = name_len +1;
1146 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
1147
1148 ptr8 = msgb_put(msg, name_len);
1149 gsm_7bit_encode(ptr8, net->name_short);
1150
Harald Weltedb253af2008-12-30 17:56:55 +00001151 }
1152
1153#if 0
1154 /* Section 10.5.3.9 */
1155 cur_t = time(NULL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001156 cur_time = gmtime(&cur_t);
Harald Weltedb253af2008-12-30 17:56:55 +00001157 ptr8 = msgb_put(msg, 8);
1158 ptr8[0] = GSM48_IE_NET_TIME_TZ;
1159 ptr8[1] = to_bcd8(cur_time->tm_year % 100);
1160 ptr8[2] = to_bcd8(cur_time->tm_mon);
1161 ptr8[3] = to_bcd8(cur_time->tm_mday);
1162 ptr8[4] = to_bcd8(cur_time->tm_hour);
1163 ptr8[5] = to_bcd8(cur_time->tm_min);
1164 ptr8[6] = to_bcd8(cur_time->tm_sec);
1165 /* 02.42: coded as BCD encoded signed value in units of 15 minutes */
1166 tz15min = (cur_time->tm_gmtoff)/(60*15);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001167 ptr8[7] = to_bcd8(tz15min);
Harald Weltedb253af2008-12-30 17:56:55 +00001168 if (tz15min < 0)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001169 ptr8[7] |= 0x80;
Harald Weltedb253af2008-12-30 17:56:55 +00001170#endif
1171
Daniel Willmanneea93372009-08-13 03:42:07 +02001172 DEBUGP(DMM, "-> MM INFO\n");
1173
Harald Welte39e2ead2009-07-23 21:13:03 +02001174 return gsm48_sendmsg(msg, NULL);
Harald Weltedb253af2008-12-30 17:56:55 +00001175}
1176
Harald Welte7984d5c2009-08-12 22:56:50 +02001177/* Section 9.2.2 */
Sylvain Munautbd55a6d2009-12-24 00:23:46 +01001178int gsm48_tx_mm_auth_req(struct gsm_lchan *lchan, u_int8_t *rand, int key_seq)
Harald Welte7984d5c2009-08-12 22:56:50 +02001179{
1180 struct msgb *msg = gsm48_msgb_alloc();
1181 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Sylvain Munaut849f5542009-09-27 11:10:17 +02001182 struct gsm48_auth_req *ar = (struct gsm48_auth_req *) msgb_put(msg, sizeof(*ar));
Harald Welte7984d5c2009-08-12 22:56:50 +02001183
Sylvain Munaut8608e7c2009-12-24 00:24:29 +01001184 DEBUGP(DMM, "-> AUTH REQ (rand = %s)\n", hexdump(rand, 16));
Harald Welte7984d5c2009-08-12 22:56:50 +02001185
1186 msg->lchan = lchan;
1187 gh->proto_discr = GSM48_PDISC_MM;
1188 gh->msg_type = GSM48_MT_MM_AUTH_REQ;
1189
Sylvain Munautbd55a6d2009-12-24 00:23:46 +01001190 ar->key_seq = key_seq;
Sylvain Munaut849f5542009-09-27 11:10:17 +02001191
Harald Welte7984d5c2009-08-12 22:56:50 +02001192 /* 16 bytes RAND parameters */
Harald Welte7984d5c2009-08-12 22:56:50 +02001193 if (rand)
Sylvain Munaut849f5542009-09-27 11:10:17 +02001194 memcpy(ar->rand, rand, 16);
Harald Welte7984d5c2009-08-12 22:56:50 +02001195
1196 return gsm48_sendmsg(msg, NULL);
1197}
1198
1199/* Section 9.2.1 */
1200int gsm48_tx_mm_auth_rej(struct gsm_lchan *lchan)
1201{
1202 DEBUGP(DMM, "-> AUTH REJECT\n");
1203 return gsm48_tx_simple(lchan, GSM48_PDISC_MM, GSM48_MT_MM_AUTH_REJ);
1204}
1205
Harald Welte4b634542008-12-27 01:55:51 +00001206static int gsm48_tx_mm_serv_ack(struct gsm_lchan *lchan)
1207{
Harald Welte4b634542008-12-27 01:55:51 +00001208 DEBUGP(DMM, "-> CM SERVICE ACK\n");
Harald Welte65e74cc2008-12-29 01:55:35 +00001209 return gsm48_tx_simple(lchan, GSM48_PDISC_MM, GSM48_MT_MM_CM_SERV_ACC);
Harald Welte4b634542008-12-27 01:55:51 +00001210}
Harald Welteba4cf162009-01-10 01:49:35 +00001211
1212/* 9.2.6 CM service reject */
1213static int gsm48_tx_mm_serv_rej(struct gsm_lchan *lchan,
1214 enum gsm48_reject_value value)
1215{
1216 struct msgb *msg = gsm48_msgb_alloc();
1217 struct gsm48_hdr *gh;
1218
1219 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
1220
1221 msg->lchan = lchan;
1222 use_lchan(lchan);
1223
1224 gh->proto_discr = GSM48_PDISC_MM;
1225 gh->msg_type = GSM48_MT_MM_CM_SERV_REJ;
1226 gh->data[0] = value;
1227 DEBUGP(DMM, "-> CM SERVICE Reject cause: %d\n", value);
1228
Harald Welte39e2ead2009-07-23 21:13:03 +02001229 return gsm48_sendmsg(msg, NULL);
Harald Welteba4cf162009-01-10 01:49:35 +00001230}
1231
Harald Welte4ed0e922009-01-10 03:17:30 +00001232/*
1233 * Handle CM Service Requests
1234 * a) Verify that the packet is long enough to contain the information
1235 * we require otherwsie reject with INCORRECT_MESSAGE
1236 * b) Try to parse the TMSI. If we do not have one reject
1237 * c) Check that we know the subscriber with the TMSI otherwise reject
1238 * with a HLR cause
1239 * d) Set the subscriber on the gsm_lchan and accept
1240 */
Harald Welte4b634542008-12-27 01:55:51 +00001241static int gsm48_rx_mm_serv_req(struct msgb *msg)
1242{
Harald Welteba4cf162009-01-10 01:49:35 +00001243 u_int8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001244 char mi_string[GSM48_MI_SIZE];
Harald Welte4b634542008-12-27 01:55:51 +00001245
Harald Welte9176bd42009-07-23 18:46:00 +02001246 struct gsm_bts *bts = msg->lchan->ts->trx->bts;
Harald Welteba4cf162009-01-10 01:49:35 +00001247 struct gsm_subscriber *subscr;
1248 struct gsm48_hdr *gh = msgb_l3(msg);
1249 struct gsm48_service_request *req =
1250 (struct gsm48_service_request *)gh->data;
Harald Weltec9e02182009-05-01 19:07:53 +00001251 /* unfortunately in Phase1 the classmar2 length is variable */
1252 u_int8_t classmark2_len = gh->data[1];
1253 u_int8_t *classmark2 = gh->data+2;
1254 u_int8_t mi_len = *(classmark2 + classmark2_len);
1255 u_int8_t *mi = (classmark2 + classmark2_len + 1);
Harald Welteba4cf162009-01-10 01:49:35 +00001256
Harald Weltec9e02182009-05-01 19:07:53 +00001257 DEBUGP(DMM, "<- CM SERVICE REQUEST ");
Harald Welteba4cf162009-01-10 01:49:35 +00001258 if (msg->data_len < sizeof(struct gsm48_service_request*)) {
Harald Weltec9e02182009-05-01 19:07:53 +00001259 DEBUGPC(DMM, "wrong sized message\n");
Harald Welteba4cf162009-01-10 01:49:35 +00001260 return gsm48_tx_mm_serv_rej(msg->lchan,
1261 GSM48_REJECT_INCORRECT_MESSAGE);
1262 }
1263
1264 if (msg->data_len < req->mi_len + 6) {
Harald Weltec9e02182009-05-01 19:07:53 +00001265 DEBUGPC(DMM, "does not fit in packet\n");
Harald Welteba4cf162009-01-10 01:49:35 +00001266 return gsm48_tx_mm_serv_rej(msg->lchan,
1267 GSM48_REJECT_INCORRECT_MESSAGE);
1268 }
1269
Harald Weltec9e02182009-05-01 19:07:53 +00001270 mi_type = mi[0] & GSM_MI_TYPE_MASK;
Harald Welteba4cf162009-01-10 01:49:35 +00001271 if (mi_type != GSM_MI_TYPE_TMSI) {
Harald Weltec9e02182009-05-01 19:07:53 +00001272 DEBUGPC(DMM, "mi_type is not TMSI: %d\n", mi_type);
Harald Welteba4cf162009-01-10 01:49:35 +00001273 return gsm48_tx_mm_serv_rej(msg->lchan,
1274 GSM48_REJECT_INCORRECT_MESSAGE);
1275 }
1276
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001277 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
Harald Weltec9e02182009-05-01 19:07:53 +00001278 DEBUGPC(DMM, "serv_type=0x%02x mi_type=0x%02x M(%s)\n",
Harald Welte4ed0e922009-01-10 03:17:30 +00001279 req->cm_service_type, mi_type, mi_string);
Harald Weltebcae43f2008-12-27 21:45:37 +00001280
Harald Welte7659de12009-12-13 12:39:18 +01001281 dispatch_signal(SS_SUBSCR, S_SUBSCR_IDENTITY, (classmark2 + classmark2_len));
1282
Harald Welte3ac7f102009-08-10 10:12:45 +02001283 if (is_siemens_bts(bts))
1284 send_siemens_mrpci(msg->lchan, classmark2-1);
1285
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +02001286 subscr = subscr_get_by_tmsi(bts->network,
1287 tmsi_from_string(mi_string));
Holger Freythereb443982009-06-04 13:58:42 +00001288
Harald Welte2a139372009-02-22 21:14:55 +00001289 /* FIXME: if we don't know the TMSI, inquire abit IMSI and allocate new TMSI */
Harald Welte4ed0e922009-01-10 03:17:30 +00001290 if (!subscr)
1291 return gsm48_tx_mm_serv_rej(msg->lchan,
1292 GSM48_REJECT_IMSI_UNKNOWN_IN_HLR);
1293
1294 if (!msg->lchan->subscr)
1295 msg->lchan->subscr = subscr;
Sylvain Munautea3f6742009-12-18 18:28:10 +01001296 else if (msg->lchan->subscr == subscr)
1297 subscr_put(subscr); /* lchan already has a ref, don't need another one */
1298 else {
Harald Welte9bb7c702009-01-10 03:21:41 +00001299 DEBUGP(DMM, "<- CM Channel already owned by someone else?\n");
1300 subscr_put(subscr);
1301 }
1302
Harald Weltec2e302d2009-07-05 14:08:13 +02001303 subscr->equipment.classmark2_len = classmark2_len;
1304 memcpy(subscr->equipment.classmark2, classmark2, classmark2_len);
1305 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +00001306
Harald Welte4b634542008-12-27 01:55:51 +00001307 return gsm48_tx_mm_serv_ack(msg->lchan);
1308}
1309
Harald Welte2a139372009-02-22 21:14:55 +00001310static int gsm48_rx_mm_imsi_detach_ind(struct msgb *msg)
1311{
Harald Welte9176bd42009-07-23 18:46:00 +02001312 struct gsm_bts *bts = msg->lchan->ts->trx->bts;
Harald Welte2a139372009-02-22 21:14:55 +00001313 struct gsm48_hdr *gh = msgb_l3(msg);
1314 struct gsm48_imsi_detach_ind *idi =
1315 (struct gsm48_imsi_detach_ind *) gh->data;
1316 u_int8_t mi_type = idi->mi[0] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001317 char mi_string[GSM48_MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +08001318 struct gsm_subscriber *subscr = NULL;
Harald Welte2a139372009-02-22 21:14:55 +00001319
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001320 gsm48_mi_to_string(mi_string, sizeof(mi_string), idi->mi, idi->mi_len);
Harald Welte2a139372009-02-22 21:14:55 +00001321 DEBUGP(DMM, "IMSI DETACH INDICATION: mi_type=0x%02x MI(%s): ",
1322 mi_type, mi_string);
1323
Harald Welteffa55a42009-12-22 19:07:32 +01001324 counter_inc(bts->network->stats.loc_upd_type.detach);
Harald Welte24ff6ee2009-12-22 00:41:05 +01001325
Harald Welte2a139372009-02-22 21:14:55 +00001326 switch (mi_type) {
1327 case GSM_MI_TYPE_TMSI:
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +02001328 subscr = subscr_get_by_tmsi(bts->network,
1329 tmsi_from_string(mi_string));
Harald Welte2a139372009-02-22 21:14:55 +00001330 break;
1331 case GSM_MI_TYPE_IMSI:
Harald Welte9176bd42009-07-23 18:46:00 +02001332 subscr = subscr_get_by_imsi(bts->network, mi_string);
Harald Welte2a139372009-02-22 21:14:55 +00001333 break;
1334 case GSM_MI_TYPE_IMEI:
1335 case GSM_MI_TYPE_IMEISV:
1336 /* no sim card... FIXME: what to do ? */
Holger Freyther79f4ae62009-06-02 03:25:04 +00001337 DEBUGPC(DMM, "unimplemented mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +00001338 break;
1339 default:
Holger Freyther79f4ae62009-06-02 03:25:04 +00001340 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +00001341 break;
1342 }
1343
Holger Freyther4a49e772009-04-12 05:37:29 +00001344 if (subscr) {
1345 subscr_update(subscr, msg->trx->bts,
1346 GSM_SUBSCRIBER_UPDATE_DETACHED);
Harald Welte2e6d4682009-12-24 14:50:24 +01001347 DEBUGP(DMM, "Subscriber: %s\n", subscr_name(subscr));
Harald Welte (local)ee4410a2009-08-17 09:39:55 +02001348
1349 subscr->equipment.classmark1 = idi->classmark1;
1350 db_sync_equipment(&subscr->equipment);
1351
Holger Freytherc21cfbc2009-06-02 02:54:57 +00001352 subscr_put(subscr);
Holger Freyther4a49e772009-04-12 05:37:29 +00001353 } else
Harald Welte2a139372009-02-22 21:14:55 +00001354 DEBUGP(DMM, "Unknown Subscriber ?!?\n");
1355
Harald Welte31981a02009-12-20 09:58:40 +01001356 /* FIXME: iterate over all transactions and release them,
1357 * imagine an IMSI DETACH happening during an active call! */
1358
Harald Welteb83d9382009-12-12 21:00:48 +01001359 /* subscriber is detached: should we release lchan? */
Harald Weltef7c28b02009-12-21 13:30:17 +01001360 lchan_auto_release(msg->lchan);
Harald Welteb83d9382009-12-12 21:00:48 +01001361
Harald Welte2a139372009-02-22 21:14:55 +00001362 return 0;
1363}
1364
Harald Welted2a7f5a2009-06-05 20:08:20 +00001365static int gsm48_rx_mm_status(struct msgb *msg)
1366{
1367 struct gsm48_hdr *gh = msgb_l3(msg);
1368
1369 DEBUGP(DMM, "MM STATUS (reject cause 0x%02x)\n", gh->data[0]);
1370
1371 return 0;
1372}
1373
Harald Weltebf5e8df2009-02-03 12:59:45 +00001374/* Receive a GSM 04.08 Mobility Management (MM) message */
Harald Welte52b1f982008-12-23 20:25:15 +00001375static int gsm0408_rcv_mm(struct msgb *msg)
1376{
1377 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001378 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001379
1380 switch (gh->msg_type & 0xbf) {
1381 case GSM48_MT_MM_LOC_UPD_REQUEST:
Harald Weltea0368542009-06-27 02:58:43 +02001382 DEBUGP(DMM, "LOCATION UPDATING REQUEST: ");
Harald Welte231ad4f2008-12-27 11:15:38 +00001383 rc = mm_rx_loc_upd_req(msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001384 break;
1385 case GSM48_MT_MM_ID_RESP:
Harald Welte231ad4f2008-12-27 11:15:38 +00001386 rc = mm_rx_id_resp(msg);
1387 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001388 case GSM48_MT_MM_CM_SERV_REQ:
Harald Welte4b634542008-12-27 01:55:51 +00001389 rc = gsm48_rx_mm_serv_req(msg);
1390 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001391 case GSM48_MT_MM_STATUS:
Harald Welted2a7f5a2009-06-05 20:08:20 +00001392 rc = gsm48_rx_mm_status(msg);
Harald Welte231ad4f2008-12-27 11:15:38 +00001393 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001394 case GSM48_MT_MM_TMSI_REALL_COMPL:
Harald Welte69b2af22009-01-06 19:47:00 +00001395 DEBUGP(DMM, "TMSI Reallocation Completed. Subscriber: %s\n",
1396 msg->lchan->subscr ?
Harald Welte2e6d4682009-12-24 14:50:24 +01001397 subscr_name(msg->lchan->subscr) :
Harald Welte69b2af22009-01-06 19:47:00 +00001398 "unknown subscriber");
1399 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001400 case GSM48_MT_MM_IMSI_DETACH_IND:
Harald Welte2a139372009-02-22 21:14:55 +00001401 rc = gsm48_rx_mm_imsi_detach_ind(msg);
1402 break;
1403 case GSM48_MT_MM_CM_REEST_REQ:
1404 DEBUGP(DMM, "CM REESTABLISH REQUEST: Not implemented\n");
1405 break;
1406 case GSM48_MT_MM_AUTH_RESP:
1407 DEBUGP(DMM, "AUTHENTICATION RESPONSE: Not implemented\n");
Harald Welte52b1f982008-12-23 20:25:15 +00001408 break;
1409 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01001410 LOGP(DMM, LOGL_NOTICE, "Unknown GSM 04.08 MM msg type 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +00001411 gh->msg_type);
1412 break;
1413 }
1414
1415 return rc;
1416}
Harald Weltebf5e8df2009-02-03 12:59:45 +00001417
Harald Welte2d35ae62009-02-06 12:02:13 +00001418/* Receive a PAGING RESPONSE message from the MS */
Holger Hans Peter Freytherca0fcbe2009-10-23 13:48:54 +02001419static int gsm48_rx_rr_pag_resp(struct msgb *msg)
Harald Welte2d35ae62009-02-06 12:02:13 +00001420{
Harald Welte9176bd42009-07-23 18:46:00 +02001421 struct gsm_bts *bts = msg->lchan->ts->trx->bts;
Harald Welte2d35ae62009-02-06 12:02:13 +00001422 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte61548982009-02-22 21:26:29 +00001423 u_int8_t *classmark2_lv = gh->data + 1;
Holger Hans Peter Freyther3ee5d3e2009-08-21 05:18:21 +02001424 u_int8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001425 char mi_string[GSM48_MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +08001426 struct gsm_subscriber *subscr = NULL;
Harald Welte2d35ae62009-02-06 12:02:13 +00001427 int rc = 0;
1428
Holger Hans Peter Freyther3ee5d3e2009-08-21 05:18:21 +02001429 gsm48_paging_extract_mi(msg, mi_string, &mi_type);
Harald Welte2d35ae62009-02-06 12:02:13 +00001430 DEBUGP(DRR, "PAGING RESPONSE: mi_type=0x%02x MI(%s)\n",
1431 mi_type, mi_string);
Harald Welte3ac7f102009-08-10 10:12:45 +02001432
Harald Weltefe18d8f2009-02-22 21:14:24 +00001433 switch (mi_type) {
1434 case GSM_MI_TYPE_TMSI:
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +02001435 subscr = subscr_get_by_tmsi(bts->network,
1436 tmsi_from_string(mi_string));
Harald Weltefe18d8f2009-02-22 21:14:24 +00001437 break;
1438 case GSM_MI_TYPE_IMSI:
Harald Welte9176bd42009-07-23 18:46:00 +02001439 subscr = subscr_get_by_imsi(bts->network, mi_string);
Harald Weltefe18d8f2009-02-22 21:14:24 +00001440 break;
1441 }
Harald Welte2d35ae62009-02-06 12:02:13 +00001442
1443 if (!subscr) {
1444 DEBUGP(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte09e38af2009-02-16 22:52:23 +00001445 /* FIXME: request id? close channel? */
Harald Welte2d35ae62009-02-06 12:02:13 +00001446 return -EINVAL;
1447 }
1448 DEBUGP(DRR, "<- Channel was requested by %s\n",
Harald Welte76042182009-08-08 16:03:15 +02001449 subscr->name && strlen(subscr->name) ? subscr->name : subscr->imsi);
Holger Freyther053e09d2009-02-14 22:51:06 +00001450
Harald Weltec2e302d2009-07-05 14:08:13 +02001451 subscr->equipment.classmark2_len = *classmark2_lv;
1452 memcpy(subscr->equipment.classmark2, classmark2_lv+1, *classmark2_lv);
1453 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +00001454
Holger Hans Peter Freyther3ee5d3e2009-08-21 05:18:21 +02001455 rc = gsm48_handle_paging_resp(msg, subscr);
Harald Welte2d35ae62009-02-06 12:02:13 +00001456 return rc;
1457}
1458
Harald Weltef7c43522009-06-09 20:24:21 +00001459static int gsm48_rx_rr_classmark(struct msgb *msg)
1460{
1461 struct gsm48_hdr *gh = msgb_l3(msg);
1462 struct gsm_subscriber *subscr = msg->lchan->subscr;
1463 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1464 u_int8_t cm2_len, cm3_len = 0;
1465 u_int8_t *cm2, *cm3 = NULL;
1466
1467 DEBUGP(DRR, "CLASSMARK CHANGE ");
1468
1469 /* classmark 2 */
1470 cm2_len = gh->data[0];
1471 cm2 = &gh->data[1];
1472 DEBUGPC(DRR, "CM2(len=%u) ", cm2_len);
1473
1474 if (payload_len > cm2_len + 1) {
1475 /* we must have a classmark3 */
1476 if (gh->data[cm2_len+1] != 0x20) {
1477 DEBUGPC(DRR, "ERR CM3 TAG\n");
1478 return -EINVAL;
1479 }
1480 if (cm2_len > 3) {
1481 DEBUGPC(DRR, "CM2 too long!\n");
1482 return -EINVAL;
1483 }
1484
1485 cm3_len = gh->data[cm2_len+2];
1486 cm3 = &gh->data[cm2_len+3];
1487 if (cm3_len > 14) {
1488 DEBUGPC(DRR, "CM3 len %u too long!\n", cm3_len);
1489 return -EINVAL;
1490 }
1491 DEBUGPC(DRR, "CM3(len=%u)\n", cm3_len);
1492 }
1493 if (subscr) {
Harald Weltec2e302d2009-07-05 14:08:13 +02001494 subscr->equipment.classmark2_len = cm2_len;
1495 memcpy(subscr->equipment.classmark2, cm2, cm2_len);
Harald Weltef7c43522009-06-09 20:24:21 +00001496 if (cm3) {
Harald Weltec2e302d2009-07-05 14:08:13 +02001497 subscr->equipment.classmark3_len = cm3_len;
1498 memcpy(subscr->equipment.classmark3, cm3, cm3_len);
Harald Weltef7c43522009-06-09 20:24:21 +00001499 }
Harald Weltec2e302d2009-07-05 14:08:13 +02001500 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +00001501 }
1502
Harald Weltef7c43522009-06-09 20:24:21 +00001503 return 0;
1504}
1505
Harald Weltecf5b3592009-05-01 18:28:42 +00001506static int gsm48_rx_rr_status(struct msgb *msg)
1507{
1508 struct gsm48_hdr *gh = msgb_l3(msg);
1509
1510 DEBUGP(DRR, "STATUS rr_cause = %s\n",
1511 rr_cause_name(gh->data[0]));
1512
1513 return 0;
1514}
1515
Harald Weltef7c43522009-06-09 20:24:21 +00001516static int gsm48_rx_rr_meas_rep(struct msgb *msg)
1517{
Harald Welted12b0fd2009-12-15 21:36:05 +01001518 struct gsm_meas_rep *meas_rep = lchan_next_meas_rep(msg->lchan);
Harald Weltef7c43522009-06-09 20:24:21 +00001519
Harald Welte3c7dc6e2009-11-29 19:07:28 +01001520 /* This shouldn't actually end up here, as RSL treats
1521 * L3 Info of 08.58 MEASUREMENT REPORT different by calling
1522 * directly into gsm48_parse_meas_rep */
1523 DEBUGP(DMEAS, "DIRECT GSM48 MEASUREMENT REPORT ?!? ");
Harald Welted12b0fd2009-12-15 21:36:05 +01001524 gsm48_parse_meas_rep(meas_rep, msg);
Harald Weltef7c43522009-06-09 20:24:21 +00001525
1526 return 0;
1527}
1528
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001529static int gsm48_rx_rr_app_info(struct msgb *msg)
1530{
1531 struct gsm48_hdr *gh = msgb_l3(msg);
1532 u_int8_t apdu_id_flags;
1533 u_int8_t apdu_len;
1534 u_int8_t *apdu_data;
1535
1536 apdu_id_flags = gh->data[0];
1537 apdu_len = gh->data[1];
1538 apdu_data = gh->data+2;
1539
1540 DEBUGP(DNM, "RX APPLICATION INFO id/flags=0x%02x apdu_len=%u apdu=%s",
1541 apdu_id_flags, apdu_len, hexdump(apdu_data, apdu_len));
1542
Harald Welte (local)026531e2009-08-16 10:40:10 +02001543 return db_apdu_blob_store(msg->lchan->subscr, apdu_id_flags, apdu_len, apdu_data);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001544}
1545
Harald Welted011e8b2009-11-29 22:45:52 +01001546/* Chapter 9.1.16 Handover complete */
Harald Welte1e191c52009-12-14 17:51:15 +01001547static int gsm48_rx_rr_ho_compl(struct msgb *msg)
Harald Welted011e8b2009-11-29 22:45:52 +01001548{
1549 struct gsm48_hdr *gh = msgb_l3(msg);
1550
1551 DEBUGP(DRR, "HANDOVER COMPLETE cause = %s\n",
1552 rr_cause_name(gh->data[0]));
1553
1554 dispatch_signal(SS_LCHAN, S_LCHAN_HANDOVER_COMPL, msg->lchan);
1555 /* FIXME: release old channel */
1556
1557 return 0;
1558}
1559
1560/* Chapter 9.1.17 Handover Failure */
Harald Welte1e191c52009-12-14 17:51:15 +01001561static int gsm48_rx_rr_ho_fail(struct msgb *msg)
Harald Welted011e8b2009-11-29 22:45:52 +01001562{
1563 struct gsm48_hdr *gh = msgb_l3(msg);
1564
1565 DEBUGP(DRR, "HANDOVER FAILED cause = %s\n",
1566 rr_cause_name(gh->data[0]));
1567
1568 dispatch_signal(SS_LCHAN, S_LCHAN_HANDOVER_FAIL, msg->lchan);
1569 /* FIXME: release allocated new channel */
1570
1571 return 0;
1572}
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001573
Harald Weltebf5e8df2009-02-03 12:59:45 +00001574/* Receive a GSM 04.08 Radio Resource (RR) message */
Harald Welte52b1f982008-12-23 20:25:15 +00001575static int gsm0408_rcv_rr(struct msgb *msg)
1576{
1577 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001578 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001579
1580 switch (gh->msg_type) {
1581 case GSM48_MT_RR_CLSM_CHG:
Harald Weltef7c43522009-06-09 20:24:21 +00001582 rc = gsm48_rx_rr_classmark(msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001583 break;
Harald Weltefc977a82008-12-27 10:19:37 +00001584 case GSM48_MT_RR_GPRS_SUSP_REQ:
1585 DEBUGP(DRR, "GRPS SUSPEND REQUEST\n");
1586 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001587 case GSM48_MT_RR_PAG_RESP:
Holger Hans Peter Freytherca0fcbe2009-10-23 13:48:54 +02001588 rc = gsm48_rx_rr_pag_resp(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001589 break;
Harald Welte7ccf7782009-02-17 01:43:01 +00001590 case GSM48_MT_RR_CHAN_MODE_MODIF_ACK:
Holger Hans Peter Freytherf520e642009-10-22 15:23:11 +02001591 rc = gsm48_rx_rr_modif_ack(msg);
Harald Welte7ccf7782009-02-17 01:43:01 +00001592 break;
Harald Weltecf5b3592009-05-01 18:28:42 +00001593 case GSM48_MT_RR_STATUS:
1594 rc = gsm48_rx_rr_status(msg);
1595 break;
Harald Weltef7c43522009-06-09 20:24:21 +00001596 case GSM48_MT_RR_MEAS_REP:
1597 rc = gsm48_rx_rr_meas_rep(msg);
1598 break;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001599 case GSM48_MT_RR_APP_INFO:
1600 rc = gsm48_rx_rr_app_info(msg);
1601 break;
Harald Welte08d91a52009-08-30 15:37:11 +09001602 case GSM48_MT_RR_CIPH_M_COMPL:
1603 DEBUGP(DRR, "CIPHERING MODE COMPLETE\n");
1604 /* FIXME: check for MI (if any) */
1605 break;
Harald Welteccd5a882009-11-29 20:02:20 +01001606 case GSM48_MT_RR_HANDO_COMPL:
Harald Welted011e8b2009-11-29 22:45:52 +01001607 rc = gsm48_rx_rr_ho_compl(msg);
Harald Welteccd5a882009-11-29 20:02:20 +01001608 break;
1609 case GSM48_MT_RR_HANDO_FAIL:
Harald Welted011e8b2009-11-29 22:45:52 +01001610 rc = gsm48_rx_rr_ho_fail(msg);
Harald Welteccd5a882009-11-29 20:02:20 +01001611 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001612 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01001613 LOGP(DRR, LOGL_NOTICE, "Unimplemented "
1614 "GSM 04.08 RR msg type 0x%02x\n", gh->msg_type);
Harald Welte52b1f982008-12-23 20:25:15 +00001615 break;
1616 }
1617
Harald Welte2d35ae62009-02-06 12:02:13 +00001618 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +00001619}
1620
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001621int gsm48_send_rr_app_info(struct gsm_lchan *lchan, u_int8_t apdu_id,
Holger Hans Peter Freyther8b77a342009-10-22 15:42:19 +02001622 u_int8_t apdu_len, const u_int8_t *apdu)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001623{
1624 struct msgb *msg = gsm48_msgb_alloc();
1625 struct gsm48_hdr *gh;
1626
1627 msg->lchan = lchan;
1628
1629 DEBUGP(DRR, "TX APPLICATION INFO id=0x%02x, len=%u\n",
1630 apdu_id, apdu_len);
1631
1632 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 2 + apdu_len);
1633 gh->proto_discr = GSM48_PDISC_RR;
1634 gh->msg_type = GSM48_MT_RR_APP_INFO;
1635 gh->data[0] = apdu_id;
1636 gh->data[1] = apdu_len;
1637 memcpy(gh->data+2, apdu, apdu_len);
1638
1639 return gsm48_sendmsg(msg, NULL);
1640}
1641
Harald Welte4bc90a12008-12-27 16:32:52 +00001642/* Call Control */
1643
Harald Welte7584aea2009-02-11 11:44:12 +00001644/* The entire call control code is written in accordance with Figure 7.10c
1645 * for 'very early assignment', i.e. we allocate a TCH/F during IMMEDIATE
1646 * ASSIGN, then first use that TCH/F for signalling and later MODE MODIFY
1647 * it for voice */
1648
Harald Welte4bfdfe72009-06-10 23:11:52 +08001649static void new_cc_state(struct gsm_trans *trans, int state)
1650{
1651 if (state > 31 || state < 0)
1652 return;
1653
1654 DEBUGP(DCC, "new state %s -> %s\n",
Harald Weltedcaf5652009-07-23 18:56:43 +02001655 cc_state_names[trans->cc.state], cc_state_names[state]);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001656
Harald Weltedcaf5652009-07-23 18:56:43 +02001657 trans->cc.state = state;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001658}
1659
1660static int gsm48_cc_tx_status(struct gsm_trans *trans, void *arg)
Harald Welte4bc90a12008-12-27 16:32:52 +00001661{
1662 struct msgb *msg = gsm48_msgb_alloc();
1663 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1664 u_int8_t *cause, *call_state;
1665
Harald Welte4bc90a12008-12-27 16:32:52 +00001666 gh->msg_type = GSM48_MT_CC_STATUS;
1667
1668 cause = msgb_put(msg, 3);
1669 cause[0] = 2;
1670 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
1671 cause[2] = 0x80 | 30; /* response to status inquiry */
1672
1673 call_state = msgb_put(msg, 1);
1674 call_state[0] = 0xc0 | 0x00;
1675
Harald Welte39e2ead2009-07-23 21:13:03 +02001676 return gsm48_sendmsg(msg, trans);
Harald Welte4bc90a12008-12-27 16:32:52 +00001677}
1678
Harald Welte6f4b7532008-12-29 00:39:37 +00001679static int gsm48_tx_simple(struct gsm_lchan *lchan,
1680 u_int8_t pdisc, u_int8_t msg_type)
Harald Welte4bc90a12008-12-27 16:32:52 +00001681{
1682 struct msgb *msg = gsm48_msgb_alloc();
1683 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1684
1685 msg->lchan = lchan;
1686
Harald Welte6f4b7532008-12-29 00:39:37 +00001687 gh->proto_discr = pdisc;
Harald Welte4bc90a12008-12-27 16:32:52 +00001688 gh->msg_type = msg_type;
1689
Harald Welte39e2ead2009-07-23 21:13:03 +02001690 return gsm48_sendmsg(msg, NULL);
Harald Welte4bc90a12008-12-27 16:32:52 +00001691}
1692
Harald Welte4bfdfe72009-06-10 23:11:52 +08001693static void gsm48_stop_cc_timer(struct gsm_trans *trans)
1694{
Harald Weltedcaf5652009-07-23 18:56:43 +02001695 if (bsc_timer_pending(&trans->cc.timer)) {
1696 DEBUGP(DCC, "stopping pending timer T%x\n", trans->cc.Tcurrent);
1697 bsc_del_timer(&trans->cc.timer);
1698 trans->cc.Tcurrent = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001699 }
1700}
1701
1702static int mncc_recvmsg(struct gsm_network *net, struct gsm_trans *trans,
1703 int msg_type, struct gsm_mncc *mncc)
1704{
1705 struct msgb *msg;
1706
1707 if (trans)
1708 if (trans->lchan)
Harald Welte6f5aee02009-07-23 21:21:14 +02001709 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08001710 "Sending '%s' to MNCC.\n",
1711 trans->lchan->ts->trx->bts->nr,
1712 trans->lchan->ts->trx->nr,
1713 trans->lchan->ts->nr, trans->transaction_id,
1714 (trans->subscr)?(trans->subscr->extension):"-",
1715 get_mncc_name(msg_type));
1716 else
1717 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
1718 "Sending '%s' to MNCC.\n",
1719 (trans->subscr)?(trans->subscr->extension):"-",
1720 get_mncc_name(msg_type));
1721 else
1722 DEBUGP(DCC, "(bts - trx - ts - ti -- sub -) "
1723 "Sending '%s' to MNCC.\n", get_mncc_name(msg_type));
1724
1725 mncc->msg_type = msg_type;
1726
Harald Welte966636f2009-06-26 19:39:35 +02001727 msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001728 if (!msg)
1729 return -ENOMEM;
1730 memcpy(msg->data, mncc, sizeof(struct gsm_mncc));
1731 msgb_enqueue(&net->upqueue, msg);
1732
1733 return 0;
1734}
1735
1736int mncc_release_ind(struct gsm_network *net, struct gsm_trans *trans,
1737 u_int32_t callref, int location, int value)
1738{
1739 struct gsm_mncc rel;
1740
Harald Welte92f70c52009-06-12 01:54:08 +08001741 memset(&rel, 0, sizeof(rel));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001742 rel.callref = callref;
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001743 mncc_set_cause(&rel, location, value);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001744 return mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
1745}
1746
Harald Weltedcaf5652009-07-23 18:56:43 +02001747/* Call Control Specific transaction release.
1748 * gets called by trans_free, DO NOT CALL YOURSELF! */
1749void _gsm48_cc_trans_free(struct gsm_trans *trans)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001750{
Harald Welte4bfdfe72009-06-10 23:11:52 +08001751 gsm48_stop_cc_timer(trans);
1752
1753 /* send release to L4, if callref still exists */
1754 if (trans->callref) {
1755 /* Ressource unavailable */
Harald Welte596fed42009-07-23 19:06:52 +02001756 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001757 GSM48_CAUSE_LOC_PRN_S_LU,
1758 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001759 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001760 if (trans->cc.state != GSM_CSTATE_NULL)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001761 new_cc_state(trans, GSM_CSTATE_NULL);
Harald Weltedcaf5652009-07-23 18:56:43 +02001762 if (trans->lchan)
1763 trau_mux_unmap(&trans->lchan->ts->e1_link, trans->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001764}
1765
1766static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg);
1767
Harald Welte09e38af2009-02-16 22:52:23 +00001768/* call-back from paging the B-end of the connection */
1769static int setup_trig_pag_evt(unsigned int hooknum, unsigned int event,
Harald Welte7ccf7782009-02-17 01:43:01 +00001770 struct msgb *msg, void *_lchan, void *param)
Harald Welte09e38af2009-02-16 22:52:23 +00001771{
Harald Welte7ccf7782009-02-17 01:43:01 +00001772 struct gsm_lchan *lchan = _lchan;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001773 struct gsm_subscriber *subscr = param;
1774 struct gsm_trans *transt, *tmp;
1775 struct gsm_network *net;
Harald Weltec05677b2009-06-26 20:17:06 +02001776
Harald Welte09e38af2009-02-16 22:52:23 +00001777 if (hooknum != GSM_HOOK_RR_PAGING)
1778 return -EINVAL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001779
1780 if (!subscr)
1781 return -EINVAL;
1782 net = subscr->net;
1783 if (!net) {
1784 DEBUGP(DCC, "Error Network not set!\n");
1785 return -EINVAL;
Harald Welte5a065df2009-02-22 21:13:18 +00001786 }
Harald Welte7584aea2009-02-11 11:44:12 +00001787
Harald Welte4bfdfe72009-06-10 23:11:52 +08001788 /* check all tranactions (without lchan) for subscriber */
1789 llist_for_each_entry_safe(transt, tmp, &net->trans_list, entry) {
1790 if (transt->subscr != subscr || transt->lchan)
1791 continue;
1792 switch (event) {
1793 case GSM_PAGING_SUCCEEDED:
1794 if (!lchan) // paranoid
1795 break;
1796 DEBUGP(DCC, "Paging subscr %s succeeded!\n",
1797 subscr->extension);
1798 /* Assign lchan */
1799 if (!transt->lchan) {
1800 transt->lchan = lchan;
1801 use_lchan(lchan);
1802 }
1803 /* send SETUP request to called party */
Harald Weltedcaf5652009-07-23 18:56:43 +02001804 gsm48_cc_tx_setup(transt, &transt->cc.msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001805 break;
1806 case GSM_PAGING_EXPIRED:
1807 DEBUGP(DCC, "Paging subscr %s expired!\n",
1808 subscr->extension);
1809 /* Temporarily out of order */
Harald Welte596fed42009-07-23 19:06:52 +02001810 mncc_release_ind(transt->subscr->net, transt,
1811 transt->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001812 GSM48_CAUSE_LOC_PRN_S_LU,
1813 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001814 transt->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001815 trans_free(transt);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001816 break;
1817 }
1818 }
Harald Welte09e38af2009-02-16 22:52:23 +00001819 return 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00001820}
Harald Welte7584aea2009-02-11 11:44:12 +00001821
Harald Welteda7ab742009-12-19 22:23:05 +01001822static int tch_recv_mncc(struct gsm_network *net, u_int32_t callref, int enable);
1823
Harald Welte805f6442009-07-28 18:25:29 +02001824/* some other part of the code sends us a signal */
1825static int handle_abisip_signal(unsigned int subsys, unsigned int signal,
1826 void *handler_data, void *signal_data)
1827{
1828 struct gsm_lchan *lchan = signal_data;
Harald Welte805f6442009-07-28 18:25:29 +02001829 int rc;
Harald Welteda7ab742009-12-19 22:23:05 +01001830 struct gsm_network *net;
1831 struct gsm_trans *trans;
Harald Welte805f6442009-07-28 18:25:29 +02001832
1833 if (subsys != SS_ABISIP)
1834 return 0;
1835
1836 /* in case we use direct BTS-to-BTS RTP */
1837 if (ipacc_rtp_direct)
1838 return 0;
1839
Harald Welte805f6442009-07-28 18:25:29 +02001840 switch (signal) {
Holger Hans Peter Freyther231163d2009-11-18 21:06:12 +01001841 case S_ABISIP_CRCX_ACK:
Harald Welteda7ab742009-12-19 22:23:05 +01001842 /* check if any transactions on this lchan still have
1843 * a tch_recv_mncc request pending */
1844 net = lchan->ts->trx->bts->network;
1845 llist_for_each_entry(trans, &net->trans_list, entry) {
1846 if (trans->lchan == lchan && trans->tch_recv) {
1847 DEBUGP(DCC, "pending tch_recv_mncc request\n");
1848 tch_recv_mncc(net, trans->callref, 1);
1849 }
1850 }
Harald Welte805f6442009-07-28 18:25:29 +02001851 break;
Harald Welte805f6442009-07-28 18:25:29 +02001852 }
1853
1854 return 0;
Harald Welte805f6442009-07-28 18:25:29 +02001855}
1856
Harald Welte49f48b82009-02-17 15:29:33 +00001857/* map two ipaccess RTP streams onto each other */
Harald Welte11fa29c2009-02-19 17:24:39 +00001858static int tch_map(struct gsm_lchan *lchan, struct gsm_lchan *remote_lchan)
Harald Welte49f48b82009-02-17 15:29:33 +00001859{
Harald Welte11fa29c2009-02-19 17:24:39 +00001860 struct gsm_bts *bts = lchan->ts->trx->bts;
1861 struct gsm_bts *remote_bts = remote_lchan->ts->trx->bts;
Harald Welte805f6442009-07-28 18:25:29 +02001862 int rc;
Harald Welte49f48b82009-02-17 15:29:33 +00001863
Harald Welte11fa29c2009-02-19 17:24:39 +00001864 DEBUGP(DCC, "Setting up TCH map between (bts=%u,trx=%u,ts=%u) and (bts=%u,trx=%u,ts=%u)\n",
1865 bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
1866 remote_bts->nr, remote_lchan->ts->trx->nr, remote_lchan->ts->nr);
1867
1868 if (bts->type != remote_bts->type) {
1869 DEBUGP(DCC, "Cannot switch calls between different BTS types yet\n");
1870 return -EINVAL;
1871 }
Harald Welteda7ab742009-12-19 22:23:05 +01001872
1873 // todo: map between different bts types
Harald Welte11fa29c2009-02-19 17:24:39 +00001874 switch (bts->type) {
Mike Habene2d82272009-10-02 12:19:34 +01001875 case GSM_BTS_TYPE_NANOBTS:
Harald Welte805f6442009-07-28 18:25:29 +02001876 if (!ipacc_rtp_direct) {
1877 /* connect the TCH's to our RTP proxy */
Harald Weltea72273e2009-12-20 16:51:09 +01001878 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welte805f6442009-07-28 18:25:29 +02001879 if (rc < 0)
1880 return rc;
Harald Weltea72273e2009-12-20 16:51:09 +01001881 rc = rsl_ipacc_mdcx_to_rtpsock(remote_lchan);
Harald Welteda7ab742009-12-19 22:23:05 +01001882#warning do we need a check of rc here?
Harald Welte805f6442009-07-28 18:25:29 +02001883
1884 /* connect them with each other */
Harald Welte2c828992009-12-02 01:56:49 +05301885 rtp_socket_proxy(lchan->abis_ip.rtp_socket,
1886 remote_lchan->abis_ip.rtp_socket);
Harald Welte805f6442009-07-28 18:25:29 +02001887 } else {
1888 /* directly connect TCH RTP streams to each other */
Harald Welte2c828992009-12-02 01:56:49 +05301889 rc = rsl_ipacc_mdcx(lchan, remote_lchan->abis_ip.bound_ip,
1890 remote_lchan->abis_ip.bound_port,
Harald Welte2c828992009-12-02 01:56:49 +05301891 remote_lchan->abis_ip.rtp_payload2);
Harald Welte805f6442009-07-28 18:25:29 +02001892 if (rc < 0)
1893 return rc;
Harald Welte2c828992009-12-02 01:56:49 +05301894 rc = rsl_ipacc_mdcx(remote_lchan, lchan->abis_ip.bound_ip,
1895 lchan->abis_ip.bound_port,
Harald Welte2c828992009-12-02 01:56:49 +05301896 lchan->abis_ip.rtp_payload2);
Harald Welte805f6442009-07-28 18:25:29 +02001897 }
Harald Welte11fa29c2009-02-19 17:24:39 +00001898 break;
1899 case GSM_BTS_TYPE_BS11:
1900 trau_mux_map_lchan(lchan, remote_lchan);
1901 break;
1902 default:
1903 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
Harald Welteda7ab742009-12-19 22:23:05 +01001904 return -EINVAL;
Harald Welte11fa29c2009-02-19 17:24:39 +00001905 }
Harald Welte49f48b82009-02-17 15:29:33 +00001906
1907 return 0;
1908}
1909
Harald Welte4bfdfe72009-06-10 23:11:52 +08001910/* bridge channels of two transactions */
1911static int tch_bridge(struct gsm_network *net, u_int32_t *refs)
Harald Welte7ccf7782009-02-17 01:43:01 +00001912{
Harald Weltedcaf5652009-07-23 18:56:43 +02001913 struct gsm_trans *trans1 = trans_find_by_callref(net, refs[0]);
1914 struct gsm_trans *trans2 = trans_find_by_callref(net, refs[1]);
Harald Welte7ccf7782009-02-17 01:43:01 +00001915
Harald Welte4bfdfe72009-06-10 23:11:52 +08001916 if (!trans1 || !trans2)
Harald Welte7ccf7782009-02-17 01:43:01 +00001917 return -EIO;
1918
Harald Welte4bfdfe72009-06-10 23:11:52 +08001919 if (!trans1->lchan || !trans2->lchan)
1920 return -EIO;
1921
1922 /* through-connect channel */
1923 return tch_map(trans1->lchan, trans2->lchan);
Harald Welte7ccf7782009-02-17 01:43:01 +00001924}
1925
Harald Welteda7ab742009-12-19 22:23:05 +01001926/* enable receive of channels to MNCC upqueue */
1927static int tch_recv_mncc(struct gsm_network *net, u_int32_t callref, int enable)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001928{
1929 struct gsm_trans *trans;
Harald Welteda7ab742009-12-19 22:23:05 +01001930 struct gsm_lchan *lchan;
1931 struct gsm_bts *bts;
1932 int rc;
Harald Welte7ccf7782009-02-17 01:43:01 +00001933
Harald Welte4bfdfe72009-06-10 23:11:52 +08001934 /* Find callref */
Harald Welteda7ab742009-12-19 22:23:05 +01001935 trans = trans_find_by_callref(net, callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001936 if (!trans)
1937 return -EIO;
1938 if (!trans->lchan)
1939 return 0;
Harald Welteda7ab742009-12-19 22:23:05 +01001940 lchan = trans->lchan;
1941 bts = lchan->ts->trx->bts;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001942
Harald Welteda7ab742009-12-19 22:23:05 +01001943 switch (bts->type) {
1944 case GSM_BTS_TYPE_NANOBTS:
1945 if (ipacc_rtp_direct) {
1946 DEBUGP(DCC, "Error: RTP proxy is disabled\n");
1947 return -EINVAL;
1948 }
1949 /* in case, we don't have a RTP socket yet, we note this
1950 * in the transaction and try later */
1951 if (!lchan->abis_ip.rtp_socket) {
1952 trans->tch_recv = enable;
1953 DEBUGP(DCC, "queue tch_recv_mncc request (%d)\n", enable);
1954 return 0;
1955 }
1956 if (enable) {
1957 /* connect the TCH's to our RTP proxy */
Harald Weltea72273e2009-12-20 16:51:09 +01001958 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welteda7ab742009-12-19 22:23:05 +01001959 if (rc < 0)
1960 return rc;
1961 /* assign socket to application interface */
1962 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
1963 net, callref);
1964 } else
1965 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
1966 net, 0);
1967 break;
1968 case GSM_BTS_TYPE_BS11:
1969 if (enable)
1970 return trau_recv_lchan(lchan, callref);
1971 return trau_mux_unmap(NULL, callref);
1972 break;
1973 default:
1974 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
1975 return -EINVAL;
1976 }
1977
1978 return 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001979}
1980
Harald Welte4bfdfe72009-06-10 23:11:52 +08001981static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
1982{
1983 DEBUGP(DCC, "-> STATUS ENQ\n");
1984 return gsm48_cc_tx_status(trans, msg);
1985}
1986
1987static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg);
1988static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg);
1989
1990static void gsm48_cc_timeout(void *arg)
1991{
1992 struct gsm_trans *trans = arg;
1993 int disconnect = 0, release = 0;
Harald Weltec66b71c2009-06-11 14:23:20 +08001994 int mo_cause = GSM48_CC_CAUSE_RECOVERY_TIMER;
1995 int mo_location = GSM48_CAUSE_LOC_USER;
1996 int l4_cause = GSM48_CC_CAUSE_NORMAL_UNSPEC;
1997 int l4_location = GSM48_CAUSE_LOC_PRN_S_LU;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001998 struct gsm_mncc mo_rel, l4_rel;
1999
2000 memset(&mo_rel, 0, sizeof(struct gsm_mncc));
2001 mo_rel.callref = trans->callref;
2002 memset(&l4_rel, 0, sizeof(struct gsm_mncc));
2003 l4_rel.callref = trans->callref;
2004
Harald Weltedcaf5652009-07-23 18:56:43 +02002005 switch(trans->cc.Tcurrent) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002006 case 0x303:
2007 release = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08002008 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002009 break;
2010 case 0x310:
2011 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08002012 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002013 break;
2014 case 0x313:
2015 disconnect = 1;
2016 /* unknown, did not find it in the specs */
2017 break;
2018 case 0x301:
2019 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08002020 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002021 break;
2022 case 0x308:
Harald Weltedcaf5652009-07-23 18:56:43 +02002023 if (!trans->cc.T308_second) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002024 /* restart T308 a second time */
Harald Weltedcaf5652009-07-23 18:56:43 +02002025 gsm48_cc_tx_release(trans, &trans->cc.msg);
2026 trans->cc.T308_second = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002027 break; /* stay in release state */
2028 }
Harald Weltedcaf5652009-07-23 18:56:43 +02002029 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002030 return;
2031// release = 1;
2032// l4_cause = 14;
2033// break;
2034 case 0x306:
2035 release = 1;
Harald Weltedcaf5652009-07-23 18:56:43 +02002036 mo_cause = trans->cc.msg.cause.value;
2037 mo_location = trans->cc.msg.cause.location;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002038 break;
2039 case 0x323:
2040 disconnect = 1;
2041 break;
2042 default:
2043 release = 1;
2044 }
2045
2046 if (release && trans->callref) {
2047 /* process release towards layer 4 */
Harald Welte596fed42009-07-23 19:06:52 +02002048 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002049 l4_location, l4_cause);
2050 trans->callref = 0;
2051 }
2052
2053 if (disconnect && trans->callref) {
2054 /* process disconnect towards layer 4 */
2055 mncc_set_cause(&l4_rel, l4_location, l4_cause);
Harald Welte596fed42009-07-23 19:06:52 +02002056 mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &l4_rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002057 }
2058
2059 /* process disconnect towards mobile station */
2060 if (disconnect || release) {
2061 mncc_set_cause(&mo_rel, mo_location, mo_cause);
Harald Weltedcaf5652009-07-23 18:56:43 +02002062 mo_rel.cause.diag[0] = ((trans->cc.Tcurrent & 0xf00) >> 8) + '0';
2063 mo_rel.cause.diag[1] = ((trans->cc.Tcurrent & 0x0f0) >> 4) + '0';
2064 mo_rel.cause.diag[2] = (trans->cc.Tcurrent & 0x00f) + '0';
Harald Welte4bfdfe72009-06-10 23:11:52 +08002065 mo_rel.cause.diag_len = 3;
2066
2067 if (disconnect)
2068 gsm48_cc_tx_disconnect(trans, &mo_rel);
2069 if (release)
2070 gsm48_cc_tx_release(trans, &mo_rel);
2071 }
2072
2073}
2074
2075static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
2076 int sec, int micro)
2077{
2078 DEBUGP(DCC, "starting timer T%x with %d seconds\n", current, sec);
Harald Weltedcaf5652009-07-23 18:56:43 +02002079 trans->cc.timer.cb = gsm48_cc_timeout;
2080 trans->cc.timer.data = trans;
2081 bsc_schedule_timer(&trans->cc.timer, sec, micro);
2082 trans->cc.Tcurrent = current;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002083}
2084
2085static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
2086{
2087 struct gsm48_hdr *gh = msgb_l3(msg);
2088 u_int8_t msg_type = gh->msg_type & 0xbf;
2089 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2090 struct tlv_parsed tp;
2091 struct gsm_mncc setup;
2092
2093 memset(&setup, 0, sizeof(struct gsm_mncc));
2094 setup.callref = trans->callref;
2095 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2096 /* emergency setup is identified by msg_type */
2097 if (msg_type == GSM48_MT_CC_EMERG_SETUP)
2098 setup.emergency = 1;
2099
2100 /* use subscriber as calling party number */
2101 if (trans->subscr) {
2102 setup.fields |= MNCC_F_CALLING;
2103 strncpy(setup.calling.number, trans->subscr->extension,
2104 sizeof(setup.calling.number)-1);
Andreas Eversbergc079be42009-06-15 23:22:09 +02002105 strncpy(setup.imsi, trans->subscr->imsi,
2106 sizeof(setup.imsi)-1);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002107 }
2108 /* bearer capability */
2109 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2110 setup.fields |= MNCC_F_BEARER_CAP;
2111 decode_bearer_cap(&setup.bearer_cap,
2112 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2113 }
2114 /* facility */
2115 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2116 setup.fields |= MNCC_F_FACILITY;
2117 decode_facility(&setup.facility,
2118 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2119 }
2120 /* called party bcd number */
2121 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
2122 setup.fields |= MNCC_F_CALLED;
2123 decode_called(&setup.called,
2124 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
2125 }
2126 /* user-user */
2127 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2128 setup.fields |= MNCC_F_USERUSER;
2129 decode_useruser(&setup.useruser,
2130 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2131 }
2132 /* ss-version */
2133 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2134 setup.fields |= MNCC_F_SSVERSION;
2135 decode_ssversion(&setup.ssversion,
2136 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2137 }
2138 /* CLIR suppression */
2139 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
2140 setup.clir.sup = 1;
2141 /* CLIR invocation */
2142 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
2143 setup.clir.inv = 1;
2144 /* cc cap */
2145 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
2146 setup.fields |= MNCC_F_CCCAP;
2147 decode_cccap(&setup.cccap,
2148 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
2149 }
2150
Harald Welte4bfdfe72009-06-10 23:11:52 +08002151 new_cc_state(trans, GSM_CSTATE_INITIATED);
2152
Harald Welte (local)47df3992009-12-26 19:45:03 +01002153 LOGP(DCC, LOGL_INFO, "Subscriber %s (%s) sends SETUP to %s\n",
2154 subscr_name(trans->subscr), trans->subscr->extension,
2155 setup.called.number);
2156
Harald Welte4bfdfe72009-06-10 23:11:52 +08002157 /* indicate setup to MNCC */
Harald Welte596fed42009-07-23 19:06:52 +02002158 mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_IND, &setup);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002159
Harald Welte13cac662009-07-29 12:10:35 +02002160 /* MNCC code will modify the channel asynchronously, we should
2161 * ipaccess-bind only after the modification has been made to the
2162 * lchan->tch_mode */
Harald Welte4bfdfe72009-06-10 23:11:52 +08002163 return 0;
2164}
2165
2166static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
Harald Welte65e74cc2008-12-29 01:55:35 +00002167{
2168 struct msgb *msg = gsm48_msgb_alloc();
2169 struct gsm48_hdr *gh;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002170 struct gsm_mncc *setup = arg;
Harald Welte78283ef2009-07-23 21:36:44 +02002171 int rc, trans_id;
Harald Welte65e74cc2008-12-29 01:55:35 +00002172
Harald Welte7ccf7782009-02-17 01:43:01 +00002173 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte65e74cc2008-12-29 01:55:35 +00002174
Harald Welte4bfdfe72009-06-10 23:11:52 +08002175 /* transaction id must not be assigned */
2176 if (trans->transaction_id != 0xff) { /* unasssigned */
2177 DEBUGP(DCC, "TX Setup with assigned transaction. "
2178 "This is not allowed!\n");
2179 /* Temporarily out of order */
Harald Welte596fed42009-07-23 19:06:52 +02002180 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002181 GSM48_CAUSE_LOC_PRN_S_LU,
2182 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002183 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002184 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002185 return rc;
2186 }
2187
2188 /* Get free transaction_id */
Harald Welte78283ef2009-07-23 21:36:44 +02002189 trans_id = trans_assign_trans_id(trans->subscr, GSM48_PDISC_CC, 0);
2190 if (trans_id < 0) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002191 /* no free transaction ID */
Harald Welte596fed42009-07-23 19:06:52 +02002192 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002193 GSM48_CAUSE_LOC_PRN_S_LU,
2194 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002195 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002196 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002197 return rc;
2198 }
Harald Welte78283ef2009-07-23 21:36:44 +02002199 trans->transaction_id = trans_id;
Harald Welte49f48b82009-02-17 15:29:33 +00002200
Harald Welte65e74cc2008-12-29 01:55:35 +00002201 gh->msg_type = GSM48_MT_CC_SETUP;
Harald Welte09e38af2009-02-16 22:52:23 +00002202
Harald Welte4bfdfe72009-06-10 23:11:52 +08002203 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
Harald Welte65e74cc2008-12-29 01:55:35 +00002204
Harald Welte4bfdfe72009-06-10 23:11:52 +08002205 /* bearer capability */
2206 if (setup->fields & MNCC_F_BEARER_CAP)
2207 encode_bearer_cap(msg, 0, &setup->bearer_cap);
2208 /* facility */
2209 if (setup->fields & MNCC_F_FACILITY)
2210 encode_facility(msg, 0, &setup->facility);
2211 /* progress */
2212 if (setup->fields & MNCC_F_PROGRESS)
2213 encode_progress(msg, 0, &setup->progress);
2214 /* calling party BCD number */
2215 if (setup->fields & MNCC_F_CALLING)
2216 encode_calling(msg, &setup->calling);
2217 /* called party BCD number */
2218 if (setup->fields & MNCC_F_CALLED)
2219 encode_called(msg, &setup->called);
2220 /* user-user */
2221 if (setup->fields & MNCC_F_USERUSER)
2222 encode_useruser(msg, 0, &setup->useruser);
2223 /* redirecting party BCD number */
2224 if (setup->fields & MNCC_F_REDIRECTING)
2225 encode_redirecting(msg, &setup->redirecting);
2226 /* signal */
2227 if (setup->fields & MNCC_F_SIGNAL)
2228 encode_signal(msg, setup->signal);
2229
2230 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
Harald Welte65e74cc2008-12-29 01:55:35 +00002231
Harald Welte39e2ead2009-07-23 21:13:03 +02002232 return gsm48_sendmsg(msg, trans);
Harald Welte65e74cc2008-12-29 01:55:35 +00002233}
2234
Harald Welte4bfdfe72009-06-10 23:11:52 +08002235static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
2236{
2237 struct gsm48_hdr *gh = msgb_l3(msg);
2238 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2239 struct tlv_parsed tp;
2240 struct gsm_mncc call_conf;
2241
2242 gsm48_stop_cc_timer(trans);
2243 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
2244
2245 memset(&call_conf, 0, sizeof(struct gsm_mncc));
2246 call_conf.callref = trans->callref;
2247 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2248#if 0
2249 /* repeat */
2250 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
2251 call_conf.repeat = 1;
2252 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
2253 call_conf.repeat = 2;
2254#endif
2255 /* bearer capability */
2256 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2257 call_conf.fields |= MNCC_F_BEARER_CAP;
2258 decode_bearer_cap(&call_conf.bearer_cap,
2259 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2260 }
2261 /* cause */
2262 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2263 call_conf.fields |= MNCC_F_CAUSE;
2264 decode_cause(&call_conf.cause,
2265 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2266 }
2267 /* cc cap */
2268 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
2269 call_conf.fields |= MNCC_F_CCCAP;
2270 decode_cccap(&call_conf.cccap,
2271 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
2272 }
2273
2274 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
2275
Harald Welte596fed42009-07-23 19:06:52 +02002276 return mncc_recvmsg(trans->subscr->net, trans, MNCC_CALL_CONF_IND,
2277 &call_conf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002278}
2279
2280static int gsm48_cc_tx_call_proc(struct gsm_trans *trans, void *arg)
2281{
2282 struct gsm_mncc *proceeding = arg;
2283 struct msgb *msg = gsm48_msgb_alloc();
2284 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2285
Harald Welte4bfdfe72009-06-10 23:11:52 +08002286 gh->msg_type = GSM48_MT_CC_CALL_PROC;
2287
2288 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
2289
2290 /* bearer capability */
2291 if (proceeding->fields & MNCC_F_BEARER_CAP)
2292 encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
2293 /* facility */
2294 if (proceeding->fields & MNCC_F_FACILITY)
2295 encode_facility(msg, 0, &proceeding->facility);
2296 /* progress */
2297 if (proceeding->fields & MNCC_F_PROGRESS)
2298 encode_progress(msg, 0, &proceeding->progress);
2299
Harald Welte39e2ead2009-07-23 21:13:03 +02002300 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002301}
2302
2303static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
2304{
2305 struct gsm48_hdr *gh = msgb_l3(msg);
2306 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2307 struct tlv_parsed tp;
2308 struct gsm_mncc alerting;
2309
2310 gsm48_stop_cc_timer(trans);
2311 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
2312
2313 memset(&alerting, 0, sizeof(struct gsm_mncc));
2314 alerting.callref = trans->callref;
2315 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2316 /* facility */
2317 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2318 alerting.fields |= MNCC_F_FACILITY;
2319 decode_facility(&alerting.facility,
2320 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2321 }
2322
2323 /* progress */
2324 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
2325 alerting.fields |= MNCC_F_PROGRESS;
2326 decode_progress(&alerting.progress,
2327 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
2328 }
2329 /* ss-version */
2330 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2331 alerting.fields |= MNCC_F_SSVERSION;
2332 decode_ssversion(&alerting.ssversion,
2333 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2334 }
2335
2336 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
2337
Harald Welte596fed42009-07-23 19:06:52 +02002338 return mncc_recvmsg(trans->subscr->net, trans, MNCC_ALERT_IND,
2339 &alerting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002340}
2341
2342static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
2343{
2344 struct gsm_mncc *alerting = arg;
2345 struct msgb *msg = gsm48_msgb_alloc();
2346 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2347
Harald Welte4bfdfe72009-06-10 23:11:52 +08002348 gh->msg_type = GSM48_MT_CC_ALERTING;
2349
2350 /* facility */
2351 if (alerting->fields & MNCC_F_FACILITY)
2352 encode_facility(msg, 0, &alerting->facility);
2353 /* progress */
2354 if (alerting->fields & MNCC_F_PROGRESS)
2355 encode_progress(msg, 0, &alerting->progress);
2356 /* user-user */
2357 if (alerting->fields & MNCC_F_USERUSER)
2358 encode_useruser(msg, 0, &alerting->useruser);
2359
2360 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
2361
Harald Welte39e2ead2009-07-23 21:13:03 +02002362 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002363}
2364
2365static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
2366{
2367 struct gsm_mncc *progress = arg;
2368 struct msgb *msg = gsm48_msgb_alloc();
2369 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2370
Harald Welte4bfdfe72009-06-10 23:11:52 +08002371 gh->msg_type = GSM48_MT_CC_PROGRESS;
2372
2373 /* progress */
2374 encode_progress(msg, 1, &progress->progress);
2375 /* user-user */
2376 if (progress->fields & MNCC_F_USERUSER)
2377 encode_useruser(msg, 0, &progress->useruser);
2378
Harald Welte39e2ead2009-07-23 21:13:03 +02002379 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002380}
2381
2382static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
2383{
2384 struct gsm_mncc *connect = arg;
2385 struct msgb *msg = gsm48_msgb_alloc();
2386 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2387
Harald Welte4bfdfe72009-06-10 23:11:52 +08002388 gh->msg_type = GSM48_MT_CC_CONNECT;
2389
2390 gsm48_stop_cc_timer(trans);
2391 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
2392
2393 /* facility */
2394 if (connect->fields & MNCC_F_FACILITY)
2395 encode_facility(msg, 0, &connect->facility);
2396 /* progress */
2397 if (connect->fields & MNCC_F_PROGRESS)
2398 encode_progress(msg, 0, &connect->progress);
2399 /* connected number */
2400 if (connect->fields & MNCC_F_CONNECTED)
2401 encode_connected(msg, &connect->connected);
2402 /* user-user */
2403 if (connect->fields & MNCC_F_USERUSER)
2404 encode_useruser(msg, 0, &connect->useruser);
2405
2406 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
2407
Harald Welte39e2ead2009-07-23 21:13:03 +02002408 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002409}
2410
2411static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
2412{
2413 struct gsm48_hdr *gh = msgb_l3(msg);
2414 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2415 struct tlv_parsed tp;
2416 struct gsm_mncc connect;
2417
2418 gsm48_stop_cc_timer(trans);
2419
2420 memset(&connect, 0, sizeof(struct gsm_mncc));
2421 connect.callref = trans->callref;
2422 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2423 /* use subscriber as connected party number */
2424 if (trans->subscr) {
2425 connect.fields |= MNCC_F_CONNECTED;
2426 strncpy(connect.connected.number, trans->subscr->extension,
2427 sizeof(connect.connected.number)-1);
Andreas Eversbergc079be42009-06-15 23:22:09 +02002428 strncpy(connect.imsi, trans->subscr->imsi,
2429 sizeof(connect.imsi)-1);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002430 }
2431 /* facility */
2432 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2433 connect.fields |= MNCC_F_FACILITY;
2434 decode_facility(&connect.facility,
2435 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2436 }
2437 /* user-user */
2438 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2439 connect.fields |= MNCC_F_USERUSER;
2440 decode_useruser(&connect.useruser,
2441 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2442 }
2443 /* ss-version */
2444 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2445 connect.fields |= MNCC_F_SSVERSION;
2446 decode_ssversion(&connect.ssversion,
2447 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2448 }
2449
2450 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
2451
Harald Welte596fed42009-07-23 19:06:52 +02002452 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_CNF, &connect);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002453}
2454
2455
2456static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
2457{
2458 struct gsm_mncc connect_ack;
2459
2460 gsm48_stop_cc_timer(trans);
2461
2462 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2463
2464 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
2465 connect_ack.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02002466 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_COMPL_IND,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002467 &connect_ack);
2468}
2469
2470static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
2471{
2472 struct msgb *msg = gsm48_msgb_alloc();
2473 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2474
Harald Welte4bfdfe72009-06-10 23:11:52 +08002475 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
2476
2477 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2478
Harald Welte39e2ead2009-07-23 21:13:03 +02002479 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002480}
2481
2482static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
2483{
2484 struct gsm48_hdr *gh = msgb_l3(msg);
2485 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2486 struct tlv_parsed tp;
2487 struct gsm_mncc disc;
2488
2489 gsm48_stop_cc_timer(trans);
2490
2491 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
2492
2493 memset(&disc, 0, sizeof(struct gsm_mncc));
2494 disc.callref = trans->callref;
2495 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
2496 /* cause */
2497 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2498 disc.fields |= MNCC_F_CAUSE;
2499 decode_cause(&disc.cause,
2500 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2501 }
2502 /* facility */
2503 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2504 disc.fields |= MNCC_F_FACILITY;
2505 decode_facility(&disc.facility,
2506 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2507 }
2508 /* user-user */
2509 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2510 disc.fields |= MNCC_F_USERUSER;
2511 decode_useruser(&disc.useruser,
2512 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2513 }
2514 /* ss-version */
2515 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2516 disc.fields |= MNCC_F_SSVERSION;
2517 decode_ssversion(&disc.ssversion,
2518 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2519 }
2520
Harald Welte596fed42009-07-23 19:06:52 +02002521 return mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &disc);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002522
2523}
2524
Harald Weltec66b71c2009-06-11 14:23:20 +08002525static struct gsm_mncc_cause default_cause = {
2526 .location = GSM48_CAUSE_LOC_PRN_S_LU,
2527 .coding = 0,
2528 .rec = 0,
2529 .rec_val = 0,
2530 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
2531 .diag_len = 0,
2532 .diag = { 0 },
2533};
Harald Welte4bfdfe72009-06-10 23:11:52 +08002534
2535static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
2536{
2537 struct gsm_mncc *disc = arg;
2538 struct msgb *msg = gsm48_msgb_alloc();
2539 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2540
Harald Welte4bfdfe72009-06-10 23:11:52 +08002541 gh->msg_type = GSM48_MT_CC_DISCONNECT;
2542
2543 gsm48_stop_cc_timer(trans);
2544 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
2545
2546 /* cause */
2547 if (disc->fields & MNCC_F_CAUSE)
2548 encode_cause(msg, 1, &disc->cause);
2549 else
2550 encode_cause(msg, 1, &default_cause);
2551
2552 /* facility */
2553 if (disc->fields & MNCC_F_FACILITY)
2554 encode_facility(msg, 0, &disc->facility);
2555 /* progress */
2556 if (disc->fields & MNCC_F_PROGRESS)
2557 encode_progress(msg, 0, &disc->progress);
2558 /* user-user */
2559 if (disc->fields & MNCC_F_USERUSER)
2560 encode_useruser(msg, 0, &disc->useruser);
2561
2562 /* store disconnect cause for T306 expiry */
Harald Weltedcaf5652009-07-23 18:56:43 +02002563 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002564
2565 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
2566
Harald Welte39e2ead2009-07-23 21:13:03 +02002567 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002568}
2569
2570static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
2571{
2572 struct gsm48_hdr *gh = msgb_l3(msg);
2573 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2574 struct tlv_parsed tp;
2575 struct gsm_mncc rel;
2576 int rc;
2577
2578 gsm48_stop_cc_timer(trans);
2579
2580 memset(&rel, 0, sizeof(struct gsm_mncc));
2581 rel.callref = trans->callref;
2582 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2583 /* cause */
2584 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2585 rel.fields |= MNCC_F_CAUSE;
2586 decode_cause(&rel.cause,
2587 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2588 }
2589 /* facility */
2590 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2591 rel.fields |= MNCC_F_FACILITY;
2592 decode_facility(&rel.facility,
2593 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2594 }
2595 /* user-user */
2596 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2597 rel.fields |= MNCC_F_USERUSER;
2598 decode_useruser(&rel.useruser,
2599 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2600 }
2601 /* ss-version */
2602 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2603 rel.fields |= MNCC_F_SSVERSION;
2604 decode_ssversion(&rel.ssversion,
2605 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2606 }
2607
Harald Weltedcaf5652009-07-23 18:56:43 +02002608 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002609 /* release collision 5.4.5 */
Harald Welte596fed42009-07-23 19:06:52 +02002610 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_CNF, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002611 } else {
Harald Welte596fed42009-07-23 19:06:52 +02002612 rc = gsm48_tx_simple(msg->lchan,
Harald Welte6f5aee02009-07-23 21:21:14 +02002613 GSM48_PDISC_CC | (trans->transaction_id << 4),
Harald Welte596fed42009-07-23 19:06:52 +02002614 GSM48_MT_CC_RELEASE_COMPL);
2615 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_IND, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002616 }
2617
2618 new_cc_state(trans, GSM_CSTATE_NULL);
2619
2620 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002621 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002622
2623 return rc;
2624}
2625
2626static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
2627{
2628 struct gsm_mncc *rel = arg;
2629 struct msgb *msg = gsm48_msgb_alloc();
2630 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2631
Harald Welte4bfdfe72009-06-10 23:11:52 +08002632 gh->msg_type = GSM48_MT_CC_RELEASE;
2633
2634 trans->callref = 0;
2635
2636 gsm48_stop_cc_timer(trans);
2637 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
2638
2639 /* cause */
2640 if (rel->fields & MNCC_F_CAUSE)
2641 encode_cause(msg, 0, &rel->cause);
2642 /* facility */
2643 if (rel->fields & MNCC_F_FACILITY)
2644 encode_facility(msg, 0, &rel->facility);
2645 /* user-user */
2646 if (rel->fields & MNCC_F_USERUSER)
2647 encode_useruser(msg, 0, &rel->useruser);
2648
Harald Weltedcaf5652009-07-23 18:56:43 +02002649 trans->cc.T308_second = 0;
2650 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002651
Harald Weltedcaf5652009-07-23 18:56:43 +02002652 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002653 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
2654
Harald Welte39e2ead2009-07-23 21:13:03 +02002655 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002656}
2657
2658static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
2659{
2660 struct gsm48_hdr *gh = msgb_l3(msg);
2661 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2662 struct tlv_parsed tp;
2663 struct gsm_mncc rel;
2664 int rc = 0;
2665
2666 gsm48_stop_cc_timer(trans);
2667
2668 memset(&rel, 0, sizeof(struct gsm_mncc));
2669 rel.callref = trans->callref;
2670 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2671 /* cause */
2672 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2673 rel.fields |= MNCC_F_CAUSE;
2674 decode_cause(&rel.cause,
2675 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2676 }
2677 /* facility */
2678 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2679 rel.fields |= MNCC_F_FACILITY;
2680 decode_facility(&rel.facility,
2681 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2682 }
2683 /* user-user */
2684 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2685 rel.fields |= MNCC_F_USERUSER;
2686 decode_useruser(&rel.useruser,
2687 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2688 }
2689 /* ss-version */
2690 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2691 rel.fields |= MNCC_F_SSVERSION;
2692 decode_ssversion(&rel.ssversion,
2693 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2694 }
2695
2696 if (trans->callref) {
Harald Weltedcaf5652009-07-23 18:56:43 +02002697 switch (trans->cc.state) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002698 case GSM_CSTATE_CALL_PRESENT:
Harald Welte596fed42009-07-23 19:06:52 +02002699 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002700 MNCC_REJ_IND, &rel);
2701 break;
2702 case GSM_CSTATE_RELEASE_REQ:
Harald Welte596fed42009-07-23 19:06:52 +02002703 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002704 MNCC_REL_CNF, &rel);
Harald Welteb83d9382009-12-12 21:00:48 +01002705 /* FIXME: in case of multiple calls, we can't simply
2706 * hang up here ! */
2707 lchan_auto_release(msg->lchan);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002708 break;
2709 default:
Harald Welte596fed42009-07-23 19:06:52 +02002710 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002711 MNCC_REL_IND, &rel);
2712 }
2713 }
2714
2715 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002716 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002717
2718 return rc;
2719}
2720
2721static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
2722{
2723 struct gsm_mncc *rel = arg;
2724 struct msgb *msg = gsm48_msgb_alloc();
2725 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2726
Harald Welte4bfdfe72009-06-10 23:11:52 +08002727 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
2728
2729 trans->callref = 0;
2730
2731 gsm48_stop_cc_timer(trans);
2732
2733 /* cause */
2734 if (rel->fields & MNCC_F_CAUSE)
2735 encode_cause(msg, 0, &rel->cause);
2736 /* facility */
2737 if (rel->fields & MNCC_F_FACILITY)
2738 encode_facility(msg, 0, &rel->facility);
2739 /* user-user */
2740 if (rel->fields & MNCC_F_USERUSER)
2741 encode_useruser(msg, 0, &rel->useruser);
2742
Harald Weltedcaf5652009-07-23 18:56:43 +02002743 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002744
Harald Welte39e2ead2009-07-23 21:13:03 +02002745 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002746}
2747
2748static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
2749{
2750 struct gsm48_hdr *gh = msgb_l3(msg);
2751 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2752 struct tlv_parsed tp;
2753 struct gsm_mncc fac;
2754
2755 memset(&fac, 0, sizeof(struct gsm_mncc));
2756 fac.callref = trans->callref;
2757 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
2758 /* facility */
2759 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2760 fac.fields |= MNCC_F_FACILITY;
2761 decode_facility(&fac.facility,
2762 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2763 }
2764 /* ss-version */
2765 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2766 fac.fields |= MNCC_F_SSVERSION;
2767 decode_ssversion(&fac.ssversion,
2768 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2769 }
2770
Harald Welte596fed42009-07-23 19:06:52 +02002771 return mncc_recvmsg(trans->subscr->net, trans, MNCC_FACILITY_IND, &fac);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002772}
2773
2774static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
2775{
2776 struct gsm_mncc *fac = arg;
2777 struct msgb *msg = gsm48_msgb_alloc();
2778 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2779
Harald Welte4bfdfe72009-06-10 23:11:52 +08002780 gh->msg_type = GSM48_MT_CC_FACILITY;
2781
2782 /* facility */
2783 encode_facility(msg, 1, &fac->facility);
2784
Harald Welte39e2ead2009-07-23 21:13:03 +02002785 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002786}
2787
2788static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
2789{
2790 struct gsm_mncc hold;
2791
2792 memset(&hold, 0, sizeof(struct gsm_mncc));
2793 hold.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02002794 return mncc_recvmsg(trans->subscr->net, trans, MNCC_HOLD_IND, &hold);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002795}
2796
2797static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
2798{
2799 struct msgb *msg = gsm48_msgb_alloc();
2800 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2801
Harald Welte4bfdfe72009-06-10 23:11:52 +08002802 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
2803
Harald Welte39e2ead2009-07-23 21:13:03 +02002804 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002805}
2806
2807static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
2808{
2809 struct gsm_mncc *hold_rej = arg;
2810 struct msgb *msg = gsm48_msgb_alloc();
2811 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2812
Harald Welte4bfdfe72009-06-10 23:11:52 +08002813 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
2814
2815 /* cause */
2816 if (hold_rej->fields & MNCC_F_CAUSE)
2817 encode_cause(msg, 1, &hold_rej->cause);
2818 else
2819 encode_cause(msg, 1, &default_cause);
2820
Harald Welte39e2ead2009-07-23 21:13:03 +02002821 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002822}
2823
2824static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
2825{
2826 struct gsm_mncc retrieve;
2827
2828 memset(&retrieve, 0, sizeof(struct gsm_mncc));
2829 retrieve.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02002830 return mncc_recvmsg(trans->subscr->net, trans, MNCC_RETRIEVE_IND,
2831 &retrieve);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002832}
2833
2834static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
2835{
2836 struct msgb *msg = gsm48_msgb_alloc();
2837 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2838
Harald Welte4bfdfe72009-06-10 23:11:52 +08002839 gh->msg_type = GSM48_MT_CC_RETR_ACK;
2840
Harald Welte39e2ead2009-07-23 21:13:03 +02002841 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002842}
2843
2844static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
2845{
2846 struct gsm_mncc *retrieve_rej = arg;
2847 struct msgb *msg = gsm48_msgb_alloc();
2848 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2849
Harald Welte4bfdfe72009-06-10 23:11:52 +08002850 gh->msg_type = GSM48_MT_CC_RETR_REJ;
2851
2852 /* cause */
2853 if (retrieve_rej->fields & MNCC_F_CAUSE)
2854 encode_cause(msg, 1, &retrieve_rej->cause);
2855 else
2856 encode_cause(msg, 1, &default_cause);
2857
Harald Welte39e2ead2009-07-23 21:13:03 +02002858 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002859}
2860
2861static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
2862{
2863 struct gsm48_hdr *gh = msgb_l3(msg);
2864 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2865 struct tlv_parsed tp;
2866 struct gsm_mncc dtmf;
2867
2868 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2869 dtmf.callref = trans->callref;
2870 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2871 /* keypad facility */
2872 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
2873 dtmf.fields |= MNCC_F_KEYPAD;
2874 decode_keypad(&dtmf.keypad,
2875 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
2876 }
2877
Harald Welte596fed42009-07-23 19:06:52 +02002878 return mncc_recvmsg(trans->subscr->net, trans, MNCC_START_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002879}
2880
2881static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
2882{
2883 struct gsm_mncc *dtmf = arg;
2884 struct msgb *msg = gsm48_msgb_alloc();
2885 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2886
Harald Welte4bfdfe72009-06-10 23:11:52 +08002887 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
2888
2889 /* keypad */
2890 if (dtmf->fields & MNCC_F_KEYPAD)
2891 encode_keypad(msg, dtmf->keypad);
2892
Harald Welte39e2ead2009-07-23 21:13:03 +02002893 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002894}
2895
2896static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
2897{
2898 struct gsm_mncc *dtmf = arg;
2899 struct msgb *msg = gsm48_msgb_alloc();
2900 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2901
Harald Welte4bfdfe72009-06-10 23:11:52 +08002902 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
2903
2904 /* cause */
2905 if (dtmf->fields & MNCC_F_CAUSE)
2906 encode_cause(msg, 1, &dtmf->cause);
2907 else
2908 encode_cause(msg, 1, &default_cause);
2909
Harald Welte39e2ead2009-07-23 21:13:03 +02002910 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002911}
2912
2913static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
2914{
2915 struct msgb *msg = gsm48_msgb_alloc();
2916 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2917
Harald Welte4bfdfe72009-06-10 23:11:52 +08002918 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
2919
Harald Welte39e2ead2009-07-23 21:13:03 +02002920 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002921}
2922
2923static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
2924{
2925 struct gsm_mncc dtmf;
2926
2927 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2928 dtmf.callref = trans->callref;
2929
Harald Welte596fed42009-07-23 19:06:52 +02002930 return mncc_recvmsg(trans->subscr->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002931}
2932
2933static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
2934{
2935 struct gsm48_hdr *gh = msgb_l3(msg);
2936 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2937 struct tlv_parsed tp;
2938 struct gsm_mncc modify;
2939
2940 memset(&modify, 0, sizeof(struct gsm_mncc));
2941 modify.callref = trans->callref;
2942 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
2943 /* bearer capability */
2944 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2945 modify.fields |= MNCC_F_BEARER_CAP;
2946 decode_bearer_cap(&modify.bearer_cap,
2947 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2948 }
2949
2950 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
2951
Harald Welte596fed42009-07-23 19:06:52 +02002952 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_IND, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002953}
2954
2955static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
2956{
2957 struct gsm_mncc *modify = arg;
2958 struct msgb *msg = gsm48_msgb_alloc();
2959 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2960
Harald Welte4bfdfe72009-06-10 23:11:52 +08002961 gh->msg_type = GSM48_MT_CC_MODIFY;
2962
2963 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
2964
2965 /* bearer capability */
2966 encode_bearer_cap(msg, 1, &modify->bearer_cap);
2967
2968 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
2969
Harald Welte39e2ead2009-07-23 21:13:03 +02002970 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002971}
2972
2973static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
2974{
2975 struct gsm48_hdr *gh = msgb_l3(msg);
2976 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2977 struct tlv_parsed tp;
2978 struct gsm_mncc modify;
2979
2980 gsm48_stop_cc_timer(trans);
2981
2982 memset(&modify, 0, sizeof(struct gsm_mncc));
2983 modify.callref = trans->callref;
2984 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
2985 /* bearer capability */
2986 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2987 modify.fields |= MNCC_F_BEARER_CAP;
2988 decode_bearer_cap(&modify.bearer_cap,
2989 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2990 }
2991
2992 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2993
Harald Welte596fed42009-07-23 19:06:52 +02002994 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_CNF, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002995}
2996
2997static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
2998{
2999 struct gsm_mncc *modify = arg;
3000 struct msgb *msg = gsm48_msgb_alloc();
3001 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3002
Harald Welte4bfdfe72009-06-10 23:11:52 +08003003 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
3004
3005 /* bearer capability */
3006 encode_bearer_cap(msg, 1, &modify->bearer_cap);
3007
3008 new_cc_state(trans, GSM_CSTATE_ACTIVE);
3009
Harald Welte39e2ead2009-07-23 21:13:03 +02003010 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003011}
3012
3013static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
3014{
3015 struct gsm48_hdr *gh = msgb_l3(msg);
3016 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
3017 struct tlv_parsed tp;
3018 struct gsm_mncc modify;
3019
3020 gsm48_stop_cc_timer(trans);
3021
3022 memset(&modify, 0, sizeof(struct gsm_mncc));
3023 modify.callref = trans->callref;
3024 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
3025 /* bearer capability */
3026 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
3027 modify.fields |= GSM48_IE_BEARER_CAP;
3028 decode_bearer_cap(&modify.bearer_cap,
3029 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
3030 }
3031 /* cause */
3032 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
3033 modify.fields |= MNCC_F_CAUSE;
3034 decode_cause(&modify.cause,
3035 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
3036 }
3037
3038 new_cc_state(trans, GSM_CSTATE_ACTIVE);
3039
Harald Welte596fed42009-07-23 19:06:52 +02003040 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_REJ, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003041}
3042
3043static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
3044{
3045 struct gsm_mncc *modify = arg;
3046 struct msgb *msg = gsm48_msgb_alloc();
3047 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3048
Harald Welte4bfdfe72009-06-10 23:11:52 +08003049 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
3050
3051 /* bearer capability */
3052 encode_bearer_cap(msg, 1, &modify->bearer_cap);
3053 /* cause */
3054 encode_cause(msg, 1, &modify->cause);
3055
3056 new_cc_state(trans, GSM_CSTATE_ACTIVE);
3057
Harald Welte39e2ead2009-07-23 21:13:03 +02003058 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003059}
3060
3061static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
3062{
3063 struct gsm_mncc *notify = arg;
3064 struct msgb *msg = gsm48_msgb_alloc();
3065 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3066
Harald Welte4bfdfe72009-06-10 23:11:52 +08003067 gh->msg_type = GSM48_MT_CC_NOTIFY;
3068
3069 /* notify */
3070 encode_notify(msg, notify->notify);
3071
Harald Welte39e2ead2009-07-23 21:13:03 +02003072 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003073}
3074
3075static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
3076{
3077 struct gsm48_hdr *gh = msgb_l3(msg);
3078 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
3079// struct tlv_parsed tp;
3080 struct gsm_mncc notify;
3081
3082 memset(&notify, 0, sizeof(struct gsm_mncc));
3083 notify.callref = trans->callref;
3084// tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len);
3085 if (payload_len >= 1)
3086 decode_notify(&notify.notify, gh->data);
3087
Harald Welte596fed42009-07-23 19:06:52 +02003088 return mncc_recvmsg(trans->subscr->net, trans, MNCC_NOTIFY_IND, &notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003089}
3090
3091static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
3092{
3093 struct gsm_mncc *user = arg;
3094 struct msgb *msg = gsm48_msgb_alloc();
3095 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3096
Harald Welte4bfdfe72009-06-10 23:11:52 +08003097 gh->msg_type = GSM48_MT_CC_USER_INFO;
3098
3099 /* user-user */
3100 if (user->fields & MNCC_F_USERUSER)
3101 encode_useruser(msg, 1, &user->useruser);
3102 /* more data */
3103 if (user->more)
3104 encode_more(msg);
3105
Harald Welte39e2ead2009-07-23 21:13:03 +02003106 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003107}
3108
3109static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
3110{
3111 struct gsm48_hdr *gh = msgb_l3(msg);
3112 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
3113 struct tlv_parsed tp;
3114 struct gsm_mncc user;
3115
3116 memset(&user, 0, sizeof(struct gsm_mncc));
3117 user.callref = trans->callref;
3118 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
3119 /* user-user */
3120 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
3121 user.fields |= MNCC_F_USERUSER;
3122 decode_useruser(&user.useruser,
3123 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
3124 }
3125 /* more data */
3126 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
3127 user.more = 1;
3128
Harald Welte596fed42009-07-23 19:06:52 +02003129 return mncc_recvmsg(trans->subscr->net, trans, MNCC_USERINFO_IND, &user);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003130}
3131
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +02003132static int _gsm48_lchan_modify(struct gsm_trans *trans, void *arg)
Harald Welte4bfdfe72009-06-10 23:11:52 +08003133{
3134 struct gsm_mncc *mode = arg;
3135
Holger Hans Peter Freytherea528022009-11-18 22:57:02 +01003136 return gsm48_lchan_modify(trans->lchan, mode->lchan_mode);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003137}
3138
3139static struct downstate {
3140 u_int32_t states;
3141 int type;
3142 int (*rout) (struct gsm_trans *trans, void *arg);
3143} downstatelist[] = {
3144 /* mobile originating call establishment */
3145 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
3146 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc},
3147 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
3148 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
3149 {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 */
3150 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
3151 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
3152 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
3153 /* mobile terminating call establishment */
3154 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
3155 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
3156 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
3157 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
3158 /* signalling during call */
3159 {SBIT(GSM_CSTATE_ACTIVE),
3160 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
3161 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
3162 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
3163 {ALL_STATES,
3164 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
3165 {ALL_STATES,
3166 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
3167 {ALL_STATES,
3168 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
3169 {SBIT(GSM_CSTATE_ACTIVE),
3170 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
3171 {SBIT(GSM_CSTATE_ACTIVE),
3172 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
3173 {SBIT(GSM_CSTATE_ACTIVE),
3174 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
3175 {SBIT(GSM_CSTATE_ACTIVE),
3176 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
3177 {SBIT(GSM_CSTATE_ACTIVE),
3178 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
3179 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
3180 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
3181 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
3182 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
3183 {SBIT(GSM_CSTATE_ACTIVE),
3184 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
3185 /* clearing */
3186 {SBIT(GSM_CSTATE_INITIATED),
3187 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
3188 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
3189 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
3190 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
3191 MNCC_REL_REQ, gsm48_cc_tx_release},
3192 /* special */
3193 {ALL_STATES,
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +02003194 MNCC_LCHAN_MODIFY, _gsm48_lchan_modify},
Harald Welte4bfdfe72009-06-10 23:11:52 +08003195};
3196
3197#define DOWNSLLEN \
3198 (sizeof(downstatelist) / sizeof(struct downstate))
3199
3200
3201int mncc_send(struct gsm_network *net, int msg_type, void *arg)
3202{
Harald Welte1a6f7982009-08-09 18:52:33 +02003203 int i, rc = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003204 struct gsm_trans *trans = NULL, *transt;
Harald Welte1a6f7982009-08-09 18:52:33 +02003205 struct gsm_lchan *lchan = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003206 struct gsm_bts *bts = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003207 struct gsm_mncc *data = arg, rel;
3208
3209 /* handle special messages */
3210 switch(msg_type) {
3211 case MNCC_BRIDGE:
3212 return tch_bridge(net, arg);
3213 case MNCC_FRAME_DROP:
Harald Welteda7ab742009-12-19 22:23:05 +01003214 return tch_recv_mncc(net, data->callref, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003215 case MNCC_FRAME_RECV:
Harald Welteda7ab742009-12-19 22:23:05 +01003216 return tch_recv_mncc(net, data->callref, 1);
3217 case GSM_TCHF_FRAME:
3218 /* Find callref */
3219 trans = trans_find_by_callref(net, data->callref);
3220 if (!trans)
3221 return -EIO;
3222 if (!trans->lchan)
3223 return 0;
3224 if (trans->lchan->type != GSM_LCHAN_TCH_F)
3225 return 0;
3226 bts = trans->lchan->ts->trx->bts;
3227 switch (bts->type) {
3228 case GSM_BTS_TYPE_NANOBTS:
3229 if (!trans->lchan->abis_ip.rtp_socket)
3230 return 0;
3231 return rtp_send_frame(trans->lchan->abis_ip.rtp_socket, arg);
3232 case GSM_BTS_TYPE_BS11:
3233 return trau_send_frame(trans->lchan, arg);
3234 default:
3235 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
3236 }
3237 return -EINVAL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003238 }
3239
3240 memset(&rel, 0, sizeof(struct gsm_mncc));
3241 rel.callref = data->callref;
3242
3243 /* Find callref */
Harald Weltedcaf5652009-07-23 18:56:43 +02003244 trans = trans_find_by_callref(net, data->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003245
3246 /* Callref unknown */
3247 if (!trans) {
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003248 struct gsm_subscriber *subscr;
3249
Harald Welte4a3464c2009-07-04 10:11:24 +02003250 if (msg_type != MNCC_SETUP_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003251 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3252 "Received '%s' from MNCC with "
3253 "unknown callref %d\n", data->called.number,
3254 get_mncc_name(msg_type), data->callref);
3255 /* Invalid call reference */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003256 return mncc_release_ind(net, NULL, data->callref,
3257 GSM48_CAUSE_LOC_PRN_S_LU,
3258 GSM48_CC_CAUSE_INVAL_TRANS_ID);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003259 }
Andreas Eversbergc079be42009-06-15 23:22:09 +02003260 if (!data->called.number[0] && !data->imsi[0]) {
3261 DEBUGP(DCC, "(bts - trx - ts - ti) "
3262 "Received '%s' from MNCC with "
3263 "no number or IMSI\n", get_mncc_name(msg_type));
3264 /* Invalid number */
3265 return mncc_release_ind(net, NULL, data->callref,
3266 GSM48_CAUSE_LOC_PRN_S_LU,
3267 GSM48_CC_CAUSE_INV_NR_FORMAT);
3268 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003269 /* New transaction due to setup, find subscriber */
Andreas Eversbergc079be42009-06-15 23:22:09 +02003270 if (data->called.number[0])
Harald Welte9176bd42009-07-23 18:46:00 +02003271 subscr = subscr_get_by_extension(net,
3272 data->called.number);
Andreas Eversbergc079be42009-06-15 23:22:09 +02003273 else
Harald Welte9176bd42009-07-23 18:46:00 +02003274 subscr = subscr_get_by_imsi(net, data->imsi);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003275 /* If subscriber is not found */
3276 if (!subscr) {
3277 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3278 "Received '%s' from MNCC with "
3279 "unknown subscriber %s\n", data->called.number,
3280 get_mncc_name(msg_type), data->called.number);
3281 /* Unknown subscriber */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003282 return mncc_release_ind(net, NULL, data->callref,
3283 GSM48_CAUSE_LOC_PRN_S_LU,
3284 GSM48_CC_CAUSE_UNASSIGNED_NR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003285 }
3286 /* If subscriber is not "attached" */
3287 if (!subscr->lac) {
3288 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3289 "Received '%s' from MNCC with "
3290 "detached subscriber %s\n", data->called.number,
3291 get_mncc_name(msg_type), data->called.number);
3292 subscr_put(subscr);
3293 /* Temporarily out of order */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003294 return mncc_release_ind(net, NULL, data->callref,
3295 GSM48_CAUSE_LOC_PRN_S_LU,
3296 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003297 }
3298 /* Create transaction */
Harald Weltedcaf5652009-07-23 18:56:43 +02003299 trans = trans_alloc(subscr, GSM48_PDISC_CC, 0xff, data->callref);
3300 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003301 DEBUGP(DCC, "No memory for trans.\n");
3302 subscr_put(subscr);
3303 /* Ressource unavailable */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003304 mncc_release_ind(net, NULL, data->callref,
3305 GSM48_CAUSE_LOC_PRN_S_LU,
3306 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003307 return -ENOMEM;
3308 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003309 /* Find lchan */
Harald Welte1a6f7982009-08-09 18:52:33 +02003310 lchan = lchan_for_subscr(subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003311 /* If subscriber has no lchan */
3312 if (!lchan) {
3313 /* find transaction with this subscriber already paging */
3314 llist_for_each_entry(transt, &net->trans_list, entry) {
3315 /* Transaction of our lchan? */
3316 if (transt == trans ||
3317 transt->subscr != subscr)
3318 continue;
3319 DEBUGP(DCC, "(bts %d trx - ts - ti -- sub %s) "
3320 "Received '%s' from MNCC with "
3321 "unallocated channel, paging already "
3322 "started.\n", bts->nr,
3323 data->called.number,
3324 get_mncc_name(msg_type));
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003325 subscr_put(subscr);
3326 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003327 return 0;
3328 }
3329 /* store setup informations until paging was successfull */
Harald Weltedcaf5652009-07-23 18:56:43 +02003330 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
Harald Weltea1b28582009-08-01 19:31:47 +02003331 /* Trigger paging */
3332 paging_request(net, subscr, RSL_CHANNEED_TCH_F,
3333 setup_trig_pag_evt, subscr);
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003334 subscr_put(subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003335 return 0;
3336 }
3337 /* Assign lchan */
3338 trans->lchan = lchan;
3339 use_lchan(lchan);
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003340 subscr_put(subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003341 }
3342 lchan = trans->lchan;
3343
3344 /* if paging did not respond yet */
3345 if (!lchan) {
3346 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3347 "Received '%s' from MNCC in paging state\n",
3348 (trans->subscr)?(trans->subscr->extension):"-",
3349 get_mncc_name(msg_type));
Harald Weltec66b71c2009-06-11 14:23:20 +08003350 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
3351 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003352 if (msg_type == MNCC_REL_REQ)
3353 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
3354 else
3355 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
3356 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02003357 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003358 return rc;
3359 }
3360
3361 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x sub %s) "
3362 "Received '%s' from MNCC in state %d (%s)\n",
3363 lchan->ts->trx->bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
3364 trans->transaction_id,
3365 (lchan->subscr)?(lchan->subscr->extension):"-",
Harald Weltedcaf5652009-07-23 18:56:43 +02003366 get_mncc_name(msg_type), trans->cc.state,
3367 cc_state_names[trans->cc.state]);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003368
3369 /* Find function for current state and message */
3370 for (i = 0; i < DOWNSLLEN; i++)
3371 if ((msg_type == downstatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003372 && ((1 << trans->cc.state) & downstatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003373 break;
3374 if (i == DOWNSLLEN) {
3375 DEBUGP(DCC, "Message unhandled at this state.\n");
3376 return 0;
3377 }
3378
3379 rc = downstatelist[i].rout(trans, arg);
3380
3381 return rc;
3382}
3383
3384
3385static struct datastate {
3386 u_int32_t states;
3387 int type;
3388 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
3389} datastatelist[] = {
3390 /* mobile originating call establishment */
3391 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3392 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
3393 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3394 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
3395 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
3396 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
3397 /* mobile terminating call establishment */
3398 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
3399 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
3400 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
3401 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
3402 {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 */
3403 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
3404 /* signalling during call */
3405 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
3406 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
3407 {SBIT(GSM_CSTATE_ACTIVE),
3408 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
3409 {ALL_STATES,
3410 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
3411 {ALL_STATES,
3412 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
3413 {ALL_STATES,
3414 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
3415 {SBIT(GSM_CSTATE_ACTIVE),
3416 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
3417 {SBIT(GSM_CSTATE_ACTIVE),
3418 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
3419 {SBIT(GSM_CSTATE_ACTIVE),
3420 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
3421 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3422 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
3423 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3424 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
3425 {SBIT(GSM_CSTATE_ACTIVE),
3426 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
3427 /* clearing */
3428 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
3429 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
3430 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
3431 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
3432 {ALL_STATES, /* 5.4.3.4 */
3433 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
3434};
3435
3436#define DATASLLEN \
3437 (sizeof(datastatelist) / sizeof(struct datastate))
3438
Harald Welte4bc90a12008-12-27 16:32:52 +00003439static int gsm0408_rcv_cc(struct msgb *msg)
3440{
3441 struct gsm48_hdr *gh = msgb_l3(msg);
3442 u_int8_t msg_type = gh->msg_type & 0xbf;
Harald Welte6f5aee02009-07-23 21:21:14 +02003443 u_int8_t transaction_id = ((gh->proto_discr & 0xf0) ^ 0x80) >> 4; /* flip */
Harald Welte4bfdfe72009-06-10 23:11:52 +08003444 struct gsm_lchan *lchan = msg->lchan;
Harald Weltedcaf5652009-07-23 18:56:43 +02003445 struct gsm_trans *trans = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003446 int i, rc = 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00003447
Harald Welte4bfdfe72009-06-10 23:11:52 +08003448 if (msg_type & 0x80) {
3449 DEBUGP(DCC, "MSG 0x%2x not defined for PD error\n", msg_type);
3450 return -EINVAL;
Harald Welte4bc90a12008-12-27 16:32:52 +00003451 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003452
3453 /* Find transaction */
Harald Welteb8b40732009-07-23 21:58:40 +02003454 trans = trans_find_by_id(lchan->subscr, GSM48_PDISC_CC, transaction_id);
Harald Weltedcaf5652009-07-23 18:56:43 +02003455
Harald Welte6f5aee02009-07-23 21:21:14 +02003456 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003457 "Received '%s' from MS in state %d (%s)\n",
3458 lchan->ts->trx->bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
3459 transaction_id, (lchan->subscr)?(lchan->subscr->extension):"-",
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +02003460 gsm0408_cc_msg_names[msg_type], trans?(trans->cc.state):0,
Harald Weltedcaf5652009-07-23 18:56:43 +02003461 cc_state_names[trans?(trans->cc.state):0]);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003462
3463 /* Create transaction */
3464 if (!trans) {
Harald Welte6f5aee02009-07-23 21:21:14 +02003465 DEBUGP(DCC, "Unknown transaction ID %x, "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003466 "creating new trans.\n", transaction_id);
3467 /* Create transaction */
Harald Weltedcaf5652009-07-23 18:56:43 +02003468 trans = trans_alloc(lchan->subscr, GSM48_PDISC_CC,
3469 transaction_id, new_callref++);
3470 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003471 DEBUGP(DCC, "No memory for trans.\n");
3472 rc = gsm48_tx_simple(msg->lchan,
Harald Welte6f5aee02009-07-23 21:21:14 +02003473 GSM48_PDISC_CC | (transaction_id << 4),
Harald Welte4bfdfe72009-06-10 23:11:52 +08003474 GSM48_MT_CC_RELEASE_COMPL);
3475 return -ENOMEM;
3476 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003477 /* Assign transaction */
Harald Welte4bfdfe72009-06-10 23:11:52 +08003478 trans->lchan = lchan;
3479 use_lchan(lchan);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003480 }
3481
3482 /* find function for current state and message */
3483 for (i = 0; i < DATASLLEN; i++)
3484 if ((msg_type == datastatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003485 && ((1 << trans->cc.state) & datastatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003486 break;
3487 if (i == DATASLLEN) {
3488 DEBUGP(DCC, "Message unhandled at this state.\n");
3489 return 0;
3490 }
3491
3492 rc = datastatelist[i].rout(trans, msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00003493
3494 return rc;
3495}
3496
Harald Welte52b1f982008-12-23 20:25:15 +00003497/* 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 +02003498int gsm0408_rcvmsg(struct msgb *msg, u_int8_t link_id)
Harald Welte52b1f982008-12-23 20:25:15 +00003499{
3500 struct gsm48_hdr *gh = msgb_l3(msg);
3501 u_int8_t pdisc = gh->proto_discr & 0x0f;
Harald Welte8470bf22008-12-25 23:28:35 +00003502 int rc = 0;
Harald Welte51008772009-12-29 11:49:12 +01003503
3504 if (silent_call_reroute(msg))
3505 return silent_call_rx(msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003506
3507 switch (pdisc) {
3508 case GSM48_PDISC_CC:
3509 rc = gsm0408_rcv_cc(msg);
3510 break;
3511 case GSM48_PDISC_MM:
3512 rc = gsm0408_rcv_mm(msg);
3513 break;
3514 case GSM48_PDISC_RR:
3515 rc = gsm0408_rcv_rr(msg);
3516 break;
Harald Weltebcae43f2008-12-27 21:45:37 +00003517 case GSM48_PDISC_SMS:
Harald Welte (local)daef6062009-08-14 11:41:12 +02003518 rc = gsm0411_rcv_sms(msg, link_id);
Harald Weltebcae43f2008-12-27 21:45:37 +00003519 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003520 case GSM48_PDISC_MM_GPRS:
Harald Weltebcae43f2008-12-27 21:45:37 +00003521 case GSM48_PDISC_SM_GPRS:
Harald Welte5d24ba12009-12-24 12:13:17 +01003522 LOGP(DRLL, LOGL_NOTICE, "Unimplemented "
3523 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Harald Welte52b1f982008-12-23 20:25:15 +00003524 break;
Harald Welte6eafe912009-10-16 08:32:58 +02003525 case GSM48_PDISC_NC_SS:
3526 rc = handle_rcv_ussd(msg);
3527 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003528 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01003529 LOGP(DRLL, LOGL_NOTICE, "Unknown "
3530 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Harald Welte52b1f982008-12-23 20:25:15 +00003531 break;
3532 }
3533
3534 return rc;
3535}
Harald Welte8470bf22008-12-25 23:28:35 +00003536
Harald Welte4bfdfe72009-06-10 23:11:52 +08003537/* dequeue messages to layer 4 */
3538int bsc_upqueue(struct gsm_network *net)
3539{
3540 struct gsm_mncc *mncc;
3541 struct msgb *msg;
3542 int work = 0;
3543
3544 if (net)
3545 while ((msg = msgb_dequeue(&net->upqueue))) {
3546 mncc = (struct gsm_mncc *)msg->data;
3547 if (net->mncc_recv)
3548 net->mncc_recv(net, mncc->msg_type, mncc);
3549 work = 1; /* work done */
Harald Welte316c8252009-06-26 19:40:48 +02003550 talloc_free(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003551 }
3552
3553 return work;
3554}
Harald Weltedcaf5652009-07-23 18:56:43 +02003555
Harald Welte805f6442009-07-28 18:25:29 +02003556/*
3557 * This will be ran by the linker when loading the DSO. We use it to
3558 * do system initialization, e.g. registration of signal handlers.
3559 */
3560static __attribute__((constructor)) void on_dso_load_0408(void)
3561{
Harald Welte805f6442009-07-28 18:25:29 +02003562 register_signal_handler(SS_LCHAN, gsm0408_handle_lchan_signal, NULL);
3563 register_signal_handler(SS_ABISIP, handle_abisip_signal, NULL);
3564}