blob: a29d0362bc5266a72218aa88f703a895b5676e18 [file] [log] [blame]
Harald Welte59b04682009-06-10 05:40:52 +08001/* GSM Mobile Radio Interface Layer 3 messages on the A-bis interface
2 * 3GPP TS 04.08 version 7.21.0 Release 1998 / ETSI TS 100 940 V7.21.0 */
3
4/* (C) 2008-2009 by Harald Welte <laforge@gnumonks.org>
5 * (C) 2008, 2009 by Holger Hans Peter Freyther <zecke@selfish.org>
6 *
7 * All Rights Reserved
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 *
23 */
24
25
26#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
29#include <errno.h>
30#include <time.h>
31#include <netinet/in.h>
32
33#include <openbsc/db.h>
34#include <openbsc/msgb.h>
35#include <openbsc/tlv.h>
36#include <openbsc/debug.h>
37#include <openbsc/gsm_data.h>
Daniel Willmannc8188202009-08-13 03:42:07 +020038#include <openbsc/gsm_utils.h>
Harald Welte59b04682009-06-10 05:40:52 +080039#include <openbsc/gsm_subscriber.h>
40#include <openbsc/gsm_04_11.h>
41#include <openbsc/gsm_04_08.h>
42#include <openbsc/abis_rsl.h>
43#include <openbsc/chan_alloc.h>
44#include <openbsc/paging.h>
45#include <openbsc/signal.h>
46#include <openbsc/trau_frame.h>
47#include <openbsc/trau_mux.h>
Harald Welte3c062072009-07-28 18:25:29 +020048#include <openbsc/rtp_proxy.h>
Harald Weltea8379772009-06-20 22:36:41 +020049#include <openbsc/talloc.h>
Harald Weltec2189a62009-07-23 18:56:43 +020050#include <openbsc/transaction.h>
Harald Welte03115042009-10-16 08:32:58 +020051#include <openbsc/ussd.h>
Harald Welte59b04682009-06-10 05:40:52 +080052
Harald Welteb8a18b52009-06-10 12:08:54 +080053#define GSM_MAX_FACILITY 128
54#define GSM_MAX_SSVERSION 128
55#define GSM_MAX_USERUSER 128
56
Harald Welte (local)8751ee92009-08-15 02:30:58 +020057void *tall_locop_ctx;
Harald Weltea8379772009-06-20 22:36:41 +020058
Holger Hans Peter Freyther36897102009-08-01 07:26:59 +020059extern int ipacc_rtp_direct;
Harald Welte3c062072009-07-28 18:25:29 +020060
Harald Welte59b04682009-06-10 05:40:52 +080061static 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 Welteb8a18b52009-06-10 12:08:54 +080077 [GSM48_IE_NOTIFY] = { TLV_TYPE_TV },
Harald Welte59b04682009-06-10 05:40:52 +080078 [GSM48_IE_KPD_FACILITY] = { TLV_TYPE_TV },
79 [GSM48_IE_SIGNAL] = { TLV_TYPE_TV },
Harald Welteb8a18b52009-06-10 12:08:54 +080080 [GSM48_IE_CONN_BCD] = { TLV_TYPE_TLV },
81 [GSM48_IE_CONN_SUB] = { TLV_TYPE_TLV },
Harald Welte59b04682009-06-10 05:40:52 +080082 [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 Welteb8a18b52009-06-10 12:08:54 +080096 [GSM48_IE_REPEAT_CIR] = { TLV_TYPE_T },
97 [GSM48_IE_REPEAT_SEQ] = { TLV_TYPE_T },
Harald Welte59b04682009-06-10 05:40:52 +080098 /* FIXME: more elements */
99 },
100};
101
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 Welte03740842009-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 Welte59b04682009-06-10 05:40:52 +0800157static 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
169static void parse_meas_rep(struct gsm_meas_rep *rep, const u_int8_t *data,
170 int len)
171{
172 memset(rep, 0, sizeof(*rep));
173
174 if (data[0] & 0x80)
175 rep->flags |= MEAS_REP_F_BA1;
176 if (data[0] & 0x40)
177 rep->flags |= MEAS_REP_F_DTX;
Harald Welteba6589d2009-07-05 04:05:44 +0200178 if ((data[1] & 0x40) == 0x00)
Harald Welte59b04682009-06-10 05:40:52 +0800179 rep->flags |= MEAS_REP_F_VALID;
180
181 rep->rxlev_full = data[0] & 0x3f;
182 rep->rxlev_sub = data[1] & 0x3f;
183 rep->rxqual_full = (data[3] >> 4) & 0x7;
184 rep->rxqual_sub = (data[3] >> 1) & 0x7;
185 rep->num_cell = data[4] >> 6 | ((data[3] & 0x01) << 2);
186 if (rep->num_cell < 1)
187 return;
188
189 /* an encoding nightmare in perfection */
190
191 rep->cell[0].rxlev = data[4] & 0x3f;
192 rep->cell[0].bcch_freq = data[5] >> 2;
193 rep->cell[0].bsic = ((data[5] & 0x03) << 3) | (data[6] >> 5);
194 if (rep->num_cell < 2)
195 return;
196
197 rep->cell[1].rxlev = ((data[6] & 0x1f) << 1) | (data[7] >> 7);
198 rep->cell[1].bcch_freq = (data[7] >> 2) & 0x1f;
199 rep->cell[1].bsic = ((data[7] & 0x03) << 4) | (data[8] >> 4);
200 if (rep->num_cell < 3)
201 return;
202
203 rep->cell[2].rxlev = ((data[8] & 0x0f) << 2) | (data[9] >> 6);
204 rep->cell[2].bcch_freq = (data[9] >> 1) & 0x1f;
205 rep->cell[2].bsic = ((data[9] & 0x01) << 6) | (data[10] >> 3);
206 if (rep->num_cell < 4)
207 return;
208
209 rep->cell[3].rxlev = ((data[10] & 0x07) << 3) | (data[11] >> 5);
210 rep->cell[3].bcch_freq = data[11] & 0x1f;
211 rep->cell[3].bsic = data[12] >> 2;
212 if (rep->num_cell < 5)
213 return;
214
215 rep->cell[4].rxlev = ((data[12] & 0x03) << 4) | (data[13] >> 4);
216 rep->cell[4].bcch_freq = ((data[13] & 0xf) << 1) | (data[14] >> 7);
217 rep->cell[4].bsic = (data[14] >> 1) & 0x3f;
218 if (rep->num_cell < 6)
219 return;
220
221 rep->cell[5].rxlev = ((data[14] & 0x01) << 5) | (data[15] >> 3);
222 rep->cell[5].bcch_freq = ((data[15] & 0x07) << 2) | (data[16] >> 6);
223 rep->cell[5].bsic = data[16] & 0x3f;
224}
225
226int gsm0408_loc_upd_acc(struct gsm_lchan *lchan, u_int32_t tmsi);
227static int gsm48_tx_simple(struct gsm_lchan *lchan,
228 u_int8_t pdisc, u_int8_t msg_type);
229static void schedule_reject(struct gsm_lchan *lchan);
230
231struct gsm_lai {
232 u_int16_t mcc;
233 u_int16_t mnc;
234 u_int16_t lac;
235};
236
Harald Welte59b04682009-06-10 05:40:52 +0800237static int reject_cause = 0;
238void gsm0408_set_reject_cause(int cause)
239{
240 reject_cause = cause;
241}
242
Harald Welte03740842009-06-10 23:11:52 +0800243static u_int32_t new_callref = 0x80000001;
244
Harald Welte59b04682009-06-10 05:40:52 +0800245static int authorize_subscriber(struct gsm_loc_updating_operation *loc,
246 struct gsm_subscriber *subscriber)
247{
248 if (!subscriber)
249 return 0;
250
251 /*
252 * Do not send accept yet as more information should arrive. Some
253 * phones will not send us the information and we will have to check
254 * what we want to do with that.
255 */
256 if (loc && (loc->waiting_for_imsi || loc->waiting_for_imei))
257 return 0;
258
Jan Luebbe3d6294e2009-08-12 12:48:00 +0200259 switch (subscriber->net->auth_policy) {
260 case GSM_AUTH_POLICY_CLOSED:
261 return subscriber->authorized;
Harald Welte (local)16480eb2009-08-13 13:49:51 +0200262 case GSM_AUTH_POLICY_TOKEN:
Harald Welte (local)272f5c72009-08-13 20:44:23 +0200263 if (subscriber->authorized)
264 return subscriber->authorized;
Harald Welte (local)16480eb2009-08-13 13:49:51 +0200265 return (subscriber->flags & GSM_SUBSCRIBER_FIRST_CONTACT);
Jan Luebbe3d6294e2009-08-12 12:48:00 +0200266 case GSM_AUTH_POLICY_ACCEPT_ALL:
Harald Welte59b04682009-06-10 05:40:52 +0800267 return 1;
Jan Luebbe3d6294e2009-08-12 12:48:00 +0200268 default:
269 return 0;
270 }
Harald Welte59b04682009-06-10 05:40:52 +0800271}
272
273static void release_loc_updating_req(struct gsm_lchan *lchan)
274{
275 if (!lchan->loc_operation)
276 return;
277
278 bsc_del_timer(&lchan->loc_operation->updating_timer);
Harald Weltea8379772009-06-20 22:36:41 +0200279 talloc_free(lchan->loc_operation);
Harald Welte59b04682009-06-10 05:40:52 +0800280 lchan->loc_operation = 0;
281 put_lchan(lchan);
282}
283
284static void allocate_loc_updating_req(struct gsm_lchan *lchan)
285{
286 use_lchan(lchan);
287 release_loc_updating_req(lchan);
288
Harald Welte857e00d2009-06-26 20:25:23 +0200289 lchan->loc_operation = talloc_zero(tall_locop_ctx,
290 struct gsm_loc_updating_operation);
Harald Welte59b04682009-06-10 05:40:52 +0800291}
292
293static int gsm0408_authorize(struct gsm_lchan *lchan, struct msgb *msg)
294{
Harald Welte59b04682009-06-10 05:40:52 +0800295 if (authorize_subscriber(lchan->loc_operation, lchan->subscr)) {
Harald Welte5070e942009-08-09 19:07:00 +0200296 int rc;
297
Harald Welte59b04682009-06-10 05:40:52 +0800298 db_subscriber_alloc_tmsi(lchan->subscr);
Harald Welte59b04682009-06-10 05:40:52 +0800299 release_loc_updating_req(lchan);
Holger Hans Peter Freythercd8bacf2009-08-19 12:53:57 +0200300 rc = gsm0408_loc_upd_acc(msg->lchan, lchan->subscr->tmsi);
Harald Welte5070e942009-08-09 19:07:00 +0200301 /* call subscr_update after putting the loc_upd_acc
302 * in the transmit queue, since S_SUBSCR_ATTACHED might
303 * trigger further action like SMS delivery */
304 subscr_update(lchan->subscr, msg->trx->bts,
305 GSM_SUBSCRIBER_UPDATE_ATTACHED);
306 return rc;
Harald Welte59b04682009-06-10 05:40:52 +0800307 }
308
309 return 0;
310}
311
312static int gsm0408_handle_lchan_signal(unsigned int subsys, unsigned int signal,
313 void *handler_data, void *signal_data)
314{
Harald Welte03740842009-06-10 23:11:52 +0800315 struct gsm_trans *trans, *temp;
316
Harald Welte59b04682009-06-10 05:40:52 +0800317 if (subsys != SS_LCHAN || signal != S_LCHAN_UNEXPECTED_RELEASE)
318 return 0;
319
320 /*
321 * Cancel any outstanding location updating request
322 * operation taking place on the lchan.
323 */
Harald Welte12560da2009-07-04 09:35:21 +0200324 struct gsm_lchan *lchan = (struct gsm_lchan *)signal_data;
Harald Welte1ff81b52009-06-26 20:17:06 +0200325 if (!lchan)
326 return 0;
327
Harald Welte59b04682009-06-10 05:40:52 +0800328 release_loc_updating_req(lchan);
329
Harald Welte03740842009-06-10 23:11:52 +0800330 /* Free all transactions that are associated with the released lchan */
Harald Weltec2189a62009-07-23 18:56:43 +0200331 /* FIXME: this is not neccessarily the right thing to do, we should
332 * only set trans->lchan to NULL and wait for another lchan to be
333 * established to the same MM entity (phone/subscriber) */
Harald Welte03740842009-06-10 23:11:52 +0800334 llist_for_each_entry_safe(trans, temp, &lchan->ts->trx->bts->network->trans_list, entry) {
335 if (trans->lchan == lchan)
Harald Weltec2189a62009-07-23 18:56:43 +0200336 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +0800337 }
338
Harald Welte59b04682009-06-10 05:40:52 +0800339 return 0;
340}
341
Harald Welte59b04682009-06-10 05:40:52 +0800342static const char bcd_num_digits[] = {
343 '0', '1', '2', '3', '4', '5', '6', '7',
344 '8', '9', '*', '#', 'a', 'b', 'c', '\0'
345};
346
Harald Welteb8a18b52009-06-10 12:08:54 +0800347/* decode a 'called/calling/connect party BCD number' as in 10.5.4.7 */
348int decode_bcd_number(char *output, int output_len, const u_int8_t *bcd_lv,
349 int h_len)
Harald Welte59b04682009-06-10 05:40:52 +0800350{
351 u_int8_t in_len = bcd_lv[0];
352 int i;
353
Harald Welteb8a18b52009-06-10 12:08:54 +0800354 for (i = 1 + h_len; i <= in_len; i++) {
Harald Welte59b04682009-06-10 05:40:52 +0800355 /* lower nibble */
356 output_len--;
357 if (output_len <= 1)
358 break;
359 *output++ = bcd_num_digits[bcd_lv[i] & 0xf];
360
361 /* higher nibble */
362 output_len--;
363 if (output_len <= 1)
364 break;
365 *output++ = bcd_num_digits[bcd_lv[i] >> 4];
366 }
367 if (output_len >= 1)
368 *output++ = '\0';
369
Harald Welteb8a18b52009-06-10 12:08:54 +0800370 return 0;
Harald Welte59b04682009-06-10 05:40:52 +0800371}
372
373/* convert a single ASCII character to call-control BCD */
374static int asc_to_bcd(const char asc)
375{
376 int i;
377
378 for (i = 0; i < ARRAY_SIZE(bcd_num_digits); i++) {
379 if (bcd_num_digits[i] == asc)
380 return i;
381 }
382 return -EINVAL;
383}
384
Harald Welteb8a18b52009-06-10 12:08:54 +0800385/* convert a ASCII phone number to 'called/calling/connect party BCD number' */
Harald Welte59b04682009-06-10 05:40:52 +0800386int encode_bcd_number(u_int8_t *bcd_lv, u_int8_t max_len,
Harald Welteb8a18b52009-06-10 12:08:54 +0800387 int h_len, const char *input)
Harald Welte59b04682009-06-10 05:40:52 +0800388{
389 int in_len = strlen(input);
390 int i;
Harald Welteb8a18b52009-06-10 12:08:54 +0800391 u_int8_t *bcd_cur = bcd_lv + 1 + h_len;
Harald Welte59b04682009-06-10 05:40:52 +0800392
393 /* two digits per byte, plus type byte */
Harald Welteb8a18b52009-06-10 12:08:54 +0800394 bcd_lv[0] = in_len/2 + h_len;
Harald Welte59b04682009-06-10 05:40:52 +0800395 if (in_len % 2)
396 bcd_lv[0]++;
397
Harald Welteb8a18b52009-06-10 12:08:54 +0800398 if (bcd_lv[0] > max_len)
399 return -EIO;
Harald Welte59b04682009-06-10 05:40:52 +0800400
401 for (i = 0; i < in_len; i++) {
402 int rc = asc_to_bcd(input[i]);
403 if (rc < 0)
404 return rc;
405 if (i % 2 == 0)
406 *bcd_cur = rc;
407 else
408 *bcd_cur++ |= (rc << 4);
409 }
410 /* append padding nibble in case of odd length */
411 if (i % 2)
412 *bcd_cur++ |= 0xf0;
413
414 /* return how many bytes we used */
415 return (bcd_cur - bcd_lv);
416}
417
Harald Welteb8a18b52009-06-10 12:08:54 +0800418/* decode 'bearer capability' */
Harald Welte03740842009-06-10 23:11:52 +0800419static int decode_bearer_cap(struct gsm_mncc_bearer_cap *bcap,
Harald Welteb8a18b52009-06-10 12:08:54 +0800420 const u_int8_t *lv)
421{
422 u_int8_t in_len = lv[0];
423 int i, s;
424
425 if (in_len < 1)
426 return -EINVAL;
427
Harald Welte03740842009-06-10 23:11:52 +0800428 bcap->speech_ver[0] = -1; /* end of list, of maximum 7 values */
Harald Welteb8a18b52009-06-10 12:08:54 +0800429
430 /* octet 3 */
Harald Welte03740842009-06-10 23:11:52 +0800431 bcap->transfer = lv[1] & 0x07;
432 bcap->mode = (lv[1] & 0x08) >> 3;
433 bcap->coding = (lv[1] & 0x10) >> 4;
434 bcap->radio = (lv[1] & 0x60) >> 5;
Harald Welteb8a18b52009-06-10 12:08:54 +0800435
436 i = 1;
437 s = 0;
438 while(!(lv[i] & 0x80)) {
439 i++; /* octet 3a etc */
440 if (in_len < i)
441 return 0;
Harald Welte03740842009-06-10 23:11:52 +0800442 bcap->speech_ver[s++] = lv[i] & 0x0f;
443 bcap->speech_ver[s] = -1; /* end of list */
Harald Welteb8a18b52009-06-10 12:08:54 +0800444 if (i == 2) /* octet 3a */
Harald Welte03740842009-06-10 23:11:52 +0800445 bcap->speech_ctm = (lv[i] & 0x20) >> 5;
Harald Welteb8a18b52009-06-10 12:08:54 +0800446 if (s == 7) /* maximum speech versions + end of list */
447 return 0;
448 }
449
450 return 0;
451}
452
453/* encode 'bearer capability' */
Harald Welte03740842009-06-10 23:11:52 +0800454static int encode_bearer_cap(struct msgb *msg, int lv_only,
455 const struct gsm_mncc_bearer_cap *bcap)
Harald Welteb8a18b52009-06-10 12:08:54 +0800456{
457 u_int8_t lv[32 + 1];
458 int i, s;
459
Harald Welte03740842009-06-10 23:11:52 +0800460 lv[1] = bcap->transfer;
461 lv[1] |= bcap->mode << 3;
462 lv[1] |= bcap->coding << 4;
463 lv[1] |= bcap->radio << 5;
Harald Welteb8a18b52009-06-10 12:08:54 +0800464
465 i = 1;
Harald Welte03740842009-06-10 23:11:52 +0800466 for (s = 0; bcap->speech_ver[s] >= 0; s++) {
Harald Welteb8a18b52009-06-10 12:08:54 +0800467 i++; /* octet 3a etc */
Harald Welte03740842009-06-10 23:11:52 +0800468 lv[i] = bcap->speech_ver[s];
Harald Welteb8a18b52009-06-10 12:08:54 +0800469 if (i == 2) /* octet 3a */
Harald Welte03740842009-06-10 23:11:52 +0800470 lv[i] |= bcap->speech_ctm << 5;
Harald Welteb8a18b52009-06-10 12:08:54 +0800471 }
472 lv[i] |= 0x80; /* last IE of octet 3 etc */
473
474 lv[0] = i;
475 if (lv_only)
476 msgb_lv_put(msg, lv[0], lv+1);
477 else
478 msgb_tlv_put(msg, GSM48_IE_BEARER_CAP, lv[0], lv+1);
479
480 return 0;
481}
482
483/* decode 'call control cap' */
Harald Welte03740842009-06-10 23:11:52 +0800484static int decode_cccap(struct gsm_mncc_cccap *ccap, const u_int8_t *lv)
Harald Welteb8a18b52009-06-10 12:08:54 +0800485{
486 u_int8_t in_len = lv[0];
487
488 if (in_len < 1)
489 return -EINVAL;
490
491 /* octet 3 */
Harald Welte03740842009-06-10 23:11:52 +0800492 ccap->dtmf = lv[1] & 0x01;
493 ccap->pcp = (lv[1] & 0x02) >> 1;
Harald Welteb8a18b52009-06-10 12:08:54 +0800494
495 return 0;
496}
497
498/* decode 'called party BCD number' */
Harald Welte03740842009-06-10 23:11:52 +0800499static int decode_called(struct gsm_mncc_number *called,
500 const u_int8_t *lv)
Harald Welteb8a18b52009-06-10 12:08:54 +0800501{
502 u_int8_t in_len = lv[0];
503
504 if (in_len < 1)
505 return -EINVAL;
506
507 /* octet 3 */
Harald Welte03740842009-06-10 23:11:52 +0800508 called->plan = lv[1] & 0x0f;
509 called->type = (lv[1] & 0x70) >> 4;
Harald Welteb8a18b52009-06-10 12:08:54 +0800510
511 /* octet 4..N */
Harald Welte03740842009-06-10 23:11:52 +0800512 decode_bcd_number(called->number, sizeof(called->number), lv, 1);
Harald Welteb8a18b52009-06-10 12:08:54 +0800513
514 return 0;
515}
516
517/* encode 'called party BCD number' */
Harald Welte03740842009-06-10 23:11:52 +0800518static int encode_called(struct msgb *msg,
519 const struct gsm_mncc_number *called)
Harald Welteb8a18b52009-06-10 12:08:54 +0800520{
521 u_int8_t lv[18];
522 int ret;
523
524 /* octet 3 */
Harald Welte03740842009-06-10 23:11:52 +0800525 lv[1] = called->plan;
526 lv[1] |= called->type << 4;
Harald Welteb8a18b52009-06-10 12:08:54 +0800527
528 /* octet 4..N, octet 2 */
Harald Welte03740842009-06-10 23:11:52 +0800529 ret = encode_bcd_number(lv, sizeof(lv), 1, called->number);
Harald Welteb8a18b52009-06-10 12:08:54 +0800530 if (ret < 0)
531 return ret;
532
533 msgb_tlv_put(msg, GSM48_IE_CALLED_BCD, lv[0], lv+1);
534
535 return 0;
536}
537
538/* encode callerid of various IEs */
Harald Welte03740842009-06-10 23:11:52 +0800539static int encode_callerid(struct msgb *msg, int ie,
540 const struct gsm_mncc_number *callerid)
Harald Welteb8a18b52009-06-10 12:08:54 +0800541{
542 u_int8_t lv[13];
543 int h_len = 1;
544 int ret;
545
546 /* octet 3 */
Harald Welte03740842009-06-10 23:11:52 +0800547 lv[1] = callerid->plan;
548 lv[1] |= callerid->type << 4;
Harald Welteb8a18b52009-06-10 12:08:54 +0800549
Harald Welte03740842009-06-10 23:11:52 +0800550 if (callerid->present || callerid->screen) {
Harald Welteb8a18b52009-06-10 12:08:54 +0800551 /* octet 3a */
Harald Welte03740842009-06-10 23:11:52 +0800552 lv[2] = callerid->screen;
553 lv[2] |= callerid->present << 5;
Harald Welteb8a18b52009-06-10 12:08:54 +0800554 lv[2] |= 0x80;
555 h_len++;
556 } else
557 lv[1] |= 0x80;
558
559 /* octet 4..N, octet 2 */
Harald Welte03740842009-06-10 23:11:52 +0800560 ret = encode_bcd_number(lv, sizeof(lv), h_len, callerid->number);
Harald Welteb8a18b52009-06-10 12:08:54 +0800561 if (ret < 0)
562 return ret;
563
564 msgb_tlv_put(msg, ie, lv[0], lv+1);
565
566 return 0;
567}
568
569/* decode 'cause' */
Harald Welte03740842009-06-10 23:11:52 +0800570static int decode_cause(struct gsm_mncc_cause *cause,
Harald Welteb8a18b52009-06-10 12:08:54 +0800571 const u_int8_t *lv)
572{
573 u_int8_t in_len = lv[0];
574 int i;
575
576 if (in_len < 2)
577 return -EINVAL;
578
Harald Welte03740842009-06-10 23:11:52 +0800579 cause->diag_len = 0;
Harald Welteb8a18b52009-06-10 12:08:54 +0800580
581 /* octet 3 */
Harald Welte03740842009-06-10 23:11:52 +0800582 cause->location = lv[1] & 0x0f;
583 cause->coding = (lv[1] & 0x60) >> 5;
Harald Welteb8a18b52009-06-10 12:08:54 +0800584
585 i = 1;
586 if (!(lv[i] & 0x80)) {
587 i++; /* octet 3a */
588 if (in_len < i+1)
589 return 0;
Harald Welte03740842009-06-10 23:11:52 +0800590 cause->rec = 1;
591 cause->rec_val = lv[i] & 0x7f;
Harald Welteb8a18b52009-06-10 12:08:54 +0800592
593 }
594 i++;
595
596 /* octet 4 */
Harald Welte03740842009-06-10 23:11:52 +0800597 cause->value = lv[i] & 0x7f;
Harald Welteb8a18b52009-06-10 12:08:54 +0800598 i++;
599
600 if (in_len < i) /* no diag */
601 return 0;
602
603 if (in_len - (i-1) > 32) /* maximum 32 octets */
604 return 0;
605
606 /* octet 5-N */
Harald Welte03740842009-06-10 23:11:52 +0800607 memcpy(cause->diag, lv + i, in_len - (i-1));
608 cause->diag_len = in_len - (i-1);
Harald Welteb8a18b52009-06-10 12:08:54 +0800609
610 return 0;
611}
612
613/* encode 'cause' */
Harald Welte03740842009-06-10 23:11:52 +0800614static int encode_cause(struct msgb *msg, int lv_only,
615 const struct gsm_mncc_cause *cause)
Harald Welteb8a18b52009-06-10 12:08:54 +0800616{
617 u_int8_t lv[32+4];
618 int i;
619
Harald Welte03740842009-06-10 23:11:52 +0800620 if (cause->diag_len > 32)
Harald Welteb8a18b52009-06-10 12:08:54 +0800621 return -EINVAL;
622
623 /* octet 3 */
Harald Welte03740842009-06-10 23:11:52 +0800624 lv[1] = cause->location;
625 lv[1] |= cause->coding << 5;
Harald Welteb8a18b52009-06-10 12:08:54 +0800626
627 i = 1;
Harald Welte03740842009-06-10 23:11:52 +0800628 if (cause->rec) {
Harald Welteb8a18b52009-06-10 12:08:54 +0800629 i++; /* octet 3a */
Harald Welte03740842009-06-10 23:11:52 +0800630 lv[i] = cause->rec_val;
Harald Welteb8a18b52009-06-10 12:08:54 +0800631 }
632 lv[i] |= 0x80; /* end of octet 3 */
633
634 /* octet 4 */
635 i++;
Harald Welte03740842009-06-10 23:11:52 +0800636 lv[i] = 0x80 | cause->value;
Harald Welteb8a18b52009-06-10 12:08:54 +0800637
638 /* octet 5-N */
Harald Welte03740842009-06-10 23:11:52 +0800639 if (cause->diag_len) {
640 memcpy(lv + i, cause->diag, cause->diag_len);
641 i += cause->diag_len;
Harald Welteb8a18b52009-06-10 12:08:54 +0800642 }
643
644 lv[0] = i;
645 if (lv_only)
646 msgb_lv_put(msg, lv[0], lv+1);
647 else
648 msgb_tlv_put(msg, GSM48_IE_CAUSE, lv[0], lv+1);
649
650 return 0;
651}
652
653/* encode 'calling number' */
Harald Welte03740842009-06-10 23:11:52 +0800654static int encode_calling(struct msgb *msg,
655 const struct gsm_mncc_number *calling)
Harald Welteb8a18b52009-06-10 12:08:54 +0800656{
Harald Welte03740842009-06-10 23:11:52 +0800657 return encode_callerid(msg, GSM48_IE_CALLING_BCD, calling);
Harald Welteb8a18b52009-06-10 12:08:54 +0800658}
659
660/* encode 'connected number' */
Harald Welte03740842009-06-10 23:11:52 +0800661static int encode_connected(struct msgb *msg,
662 const struct gsm_mncc_number *connected)
Harald Welteb8a18b52009-06-10 12:08:54 +0800663{
Harald Welte03740842009-06-10 23:11:52 +0800664 return encode_callerid(msg, GSM48_IE_CONN_BCD, connected);
Harald Welteb8a18b52009-06-10 12:08:54 +0800665}
666
667/* encode 'redirecting number' */
Harald Welte03740842009-06-10 23:11:52 +0800668static int encode_redirecting(struct msgb *msg,
669 const struct gsm_mncc_number *redirecting)
Harald Welteb8a18b52009-06-10 12:08:54 +0800670{
Harald Welte03740842009-06-10 23:11:52 +0800671 return encode_callerid(msg, GSM48_IE_REDIR_BCD, redirecting);
Harald Welteb8a18b52009-06-10 12:08:54 +0800672}
673
674/* decode 'facility' */
Harald Welte03740842009-06-10 23:11:52 +0800675static int decode_facility(struct gsm_mncc_facility *facility,
Harald Welteb8a18b52009-06-10 12:08:54 +0800676 const u_int8_t *lv)
677{
678 u_int8_t in_len = lv[0];
679
680 if (in_len < 1)
681 return -EINVAL;
682
Harald Welte03740842009-06-10 23:11:52 +0800683 if (in_len > sizeof(facility->info))
Harald Welteb8a18b52009-06-10 12:08:54 +0800684 return -EINVAL;
685
Harald Welte03740842009-06-10 23:11:52 +0800686 memcpy(facility->info, lv+1, in_len);
687 facility->len = in_len;
Harald Welteb8a18b52009-06-10 12:08:54 +0800688
689 return 0;
690}
691
692/* encode 'facility' */
Harald Welte03740842009-06-10 23:11:52 +0800693static int encode_facility(struct msgb *msg, int lv_only,
694 const struct gsm_mncc_facility *facility)
Harald Welteb8a18b52009-06-10 12:08:54 +0800695{
696 u_int8_t lv[GSM_MAX_FACILITY + 1];
697
Harald Welte03740842009-06-10 23:11:52 +0800698 if (facility->len < 1 || facility->len > GSM_MAX_FACILITY)
Harald Welteb8a18b52009-06-10 12:08:54 +0800699 return -EINVAL;
700
Harald Welte03740842009-06-10 23:11:52 +0800701 memcpy(lv+1, facility->info, facility->len);
702 lv[0] = facility->len;
Harald Welteb8a18b52009-06-10 12:08:54 +0800703 if (lv_only)
704 msgb_lv_put(msg, lv[0], lv+1);
705 else
706 msgb_tlv_put(msg, GSM48_IE_FACILITY, lv[0], lv+1);
707
708 return 0;
709}
710
711/* decode 'notify' */
712static int decode_notify(int *notify, const u_int8_t *v)
713{
714 *notify = v[0] & 0x7f;
715
716 return 0;
717}
718
719/* encode 'notify' */
720static int encode_notify(struct msgb *msg, int notify)
721{
722 msgb_v_put(msg, notify | 0x80);
723
724 return 0;
725}
726
727/* encode 'signal' */
728static int encode_signal(struct msgb *msg, int signal)
729{
730 msgb_tv_put(msg, GSM48_IE_SIGNAL, signal);
731
732 return 0;
733}
734
735/* decode 'keypad' */
736static int decode_keypad(int *keypad, const u_int8_t *lv)
737{
738 u_int8_t in_len = lv[0];
739
740 if (in_len < 1)
741 return -EINVAL;
742
743 *keypad = lv[1] & 0x7f;
744
745 return 0;
746}
747
748/* encode 'keypad' */
749static int encode_keypad(struct msgb *msg, int keypad)
750{
751 msgb_tv_put(msg, GSM48_IE_KPD_FACILITY, keypad);
752
753 return 0;
754}
755
756/* decode 'progress' */
Harald Welte03740842009-06-10 23:11:52 +0800757static int decode_progress(struct gsm_mncc_progress *progress,
Harald Welteb8a18b52009-06-10 12:08:54 +0800758 const u_int8_t *lv)
759{
760 u_int8_t in_len = lv[0];
761
762 if (in_len < 2)
763 return -EINVAL;
764
Harald Welte03740842009-06-10 23:11:52 +0800765 progress->coding = (lv[1] & 0x60) >> 5;
766 progress->location = lv[1] & 0x0f;
767 progress->descr = lv[2] & 0x7f;
Harald Welteb8a18b52009-06-10 12:08:54 +0800768
769 return 0;
770}
771
772/* encode 'progress' */
Harald Welte03740842009-06-10 23:11:52 +0800773static int encode_progress(struct msgb *msg, int lv_only,
774 const struct gsm_mncc_progress *p)
Harald Welteb8a18b52009-06-10 12:08:54 +0800775{
776 u_int8_t lv[3];
777
778 lv[0] = 2;
Harald Welte03740842009-06-10 23:11:52 +0800779 lv[1] = 0x80 | ((p->coding & 0x3) << 5) | (p->location & 0xf);
780 lv[2] = 0x80 | (p->descr & 0x7f);
Harald Welteb8a18b52009-06-10 12:08:54 +0800781 if (lv_only)
782 msgb_lv_put(msg, lv[0], lv+1);
783 else
784 msgb_tlv_put(msg, GSM48_IE_PROGR_IND, lv[0], lv+1);
785
786 return 0;
787}
788
789/* decode 'user-user' */
Harald Welte03740842009-06-10 23:11:52 +0800790static int decode_useruser(struct gsm_mncc_useruser *uu,
Harald Welteb8a18b52009-06-10 12:08:54 +0800791 const u_int8_t *lv)
792{
793 u_int8_t in_len = lv[0];
Harald Welte03740842009-06-10 23:11:52 +0800794 char *info = uu->info;
795 int info_len = sizeof(uu->info);
Harald Welteb8a18b52009-06-10 12:08:54 +0800796 int i;
797
798 if (in_len < 1)
799 return -EINVAL;
800
Harald Welte03740842009-06-10 23:11:52 +0800801 uu->proto = lv[1];
Harald Welteb8a18b52009-06-10 12:08:54 +0800802
803 for (i = 2; i <= in_len; i++) {
804 info_len--;
805 if (info_len <= 1)
806 break;
807 *info++ = lv[i];
808 }
809 if (info_len >= 1)
810 *info++ = '\0';
811
812 return 0;
813}
814
815/* encode 'useruser' */
Harald Welte03740842009-06-10 23:11:52 +0800816static int encode_useruser(struct msgb *msg, int lv_only,
817 const struct gsm_mncc_useruser *uu)
Harald Welteb8a18b52009-06-10 12:08:54 +0800818{
819 u_int8_t lv[GSM_MAX_USERUSER + 2];
820
Harald Welte03740842009-06-10 23:11:52 +0800821 if (strlen(uu->info) > GSM_MAX_USERUSER)
Harald Welteb8a18b52009-06-10 12:08:54 +0800822 return -EINVAL;
823
Harald Welte03740842009-06-10 23:11:52 +0800824 lv[0] = 1 + strlen(uu->info);
825 lv[1] = uu->proto;
826 memcpy(lv + 2, uu->info, strlen(uu->info));
Harald Welteb8a18b52009-06-10 12:08:54 +0800827 if (lv_only)
828 msgb_lv_put(msg, lv[0], lv+1);
829 else
830 msgb_tlv_put(msg, GSM48_IE_USER_USER, lv[0], lv+1);
831
832 return 0;
833}
834
835/* decode 'ss version' */
Harald Welte03740842009-06-10 23:11:52 +0800836static int decode_ssversion(struct gsm_mncc_ssversion *ssv,
Harald Welteb8a18b52009-06-10 12:08:54 +0800837 const u_int8_t *lv)
838{
839 u_int8_t in_len = lv[0];
840
Harald Welte03740842009-06-10 23:11:52 +0800841 if (in_len < 1 || in_len < sizeof(ssv->info))
Harald Welteb8a18b52009-06-10 12:08:54 +0800842 return -EINVAL;
843
Harald Welte03740842009-06-10 23:11:52 +0800844 memcpy(ssv->info, lv + 1, in_len);
845 ssv->len = in_len;
Harald Welteb8a18b52009-06-10 12:08:54 +0800846
847 return 0;
848}
849
850/* encode 'more data' */
851static int encode_more(struct msgb *msg)
852{
853 u_int8_t *ie;
854
855 ie = msgb_put(msg, 1);
856 ie[0] = GSM48_IE_MORE_DATA;
857
858 return 0;
859}
860
Harald Welte59b04682009-06-10 05:40:52 +0800861/* Chapter 9.2.14 : Send LOCATION UPDATING REJECT */
862int gsm0408_loc_upd_rej(struct gsm_lchan *lchan, u_int8_t cause)
863{
864 struct msgb *msg = gsm48_msgb_alloc();
865 struct gsm48_hdr *gh;
866
867 msg->lchan = lchan;
868
869 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
870 gh->proto_discr = GSM48_PDISC_MM;
871 gh->msg_type = GSM48_MT_MM_LOC_UPD_REJECT;
872 gh->data[0] = cause;
873
874 DEBUGP(DMM, "-> LOCATION UPDATING REJECT on channel: %d\n", lchan->nr);
875
Harald Welte36fe2e82009-07-23 21:13:03 +0200876 return gsm48_sendmsg(msg, NULL);
Harald Welte59b04682009-06-10 05:40:52 +0800877}
878
879/* Chapter 9.2.13 : Send LOCATION UPDATE ACCEPT */
880int gsm0408_loc_upd_acc(struct gsm_lchan *lchan, u_int32_t tmsi)
881{
882 struct gsm_bts *bts = lchan->ts->trx->bts;
883 struct msgb *msg = gsm48_msgb_alloc();
884 struct gsm48_hdr *gh;
885 struct gsm48_loc_area_id *lai;
886 u_int8_t *mid;
887 int ret;
888
889 msg->lchan = lchan;
890
891 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
892 gh->proto_discr = GSM48_PDISC_MM;
893 gh->msg_type = GSM48_MT_MM_LOC_UPD_ACCEPT;
894
895 lai = (struct gsm48_loc_area_id *) msgb_put(msg, sizeof(*lai));
896 gsm0408_generate_lai(lai, bts->network->country_code,
897 bts->network->network_code, bts->location_area_code);
898
Holger Hans Peter Freyther36897102009-08-01 07:26:59 +0200899 mid = msgb_put(msg, GSM48_MID_TMSI_LEN);
Holger Hans Peter Freyther20a0f322009-08-20 08:41:24 +0200900 gsm48_generate_mid_from_tmsi(mid, tmsi);
Harald Welte59b04682009-06-10 05:40:52 +0800901
902 DEBUGP(DMM, "-> LOCATION UPDATE ACCEPT\n");
903
Harald Welte36fe2e82009-07-23 21:13:03 +0200904 ret = gsm48_sendmsg(msg, NULL);
Harald Welte59b04682009-06-10 05:40:52 +0800905
Harald Welte5070e942009-08-09 19:07:00 +0200906 /* send MM INFO with network name */
Harald Welte59b04682009-06-10 05:40:52 +0800907 ret = gsm48_tx_mm_info(lchan);
908
909 return ret;
910}
911
Harald Welte59b04682009-06-10 05:40:52 +0800912/* Transmit Chapter 9.2.10 Identity Request */
913static int mm_tx_identity_req(struct gsm_lchan *lchan, u_int8_t id_type)
914{
915 struct msgb *msg = gsm48_msgb_alloc();
916 struct gsm48_hdr *gh;
917
918 msg->lchan = lchan;
919
920 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
921 gh->proto_discr = GSM48_PDISC_MM;
922 gh->msg_type = GSM48_MT_MM_ID_REQ;
923 gh->data[0] = id_type;
924
Harald Welte36fe2e82009-07-23 21:13:03 +0200925 return gsm48_sendmsg(msg, NULL);
Harald Welte59b04682009-06-10 05:40:52 +0800926}
927
Harald Welte59b04682009-06-10 05:40:52 +0800928
929/* Parse Chapter 9.2.11 Identity Response */
930static int mm_rx_id_resp(struct msgb *msg)
931{
932 struct gsm48_hdr *gh = msgb_l3(msg);
933 struct gsm_lchan *lchan = msg->lchan;
Harald Welte75350412009-07-23 18:46:00 +0200934 struct gsm_bts *bts = lchan->ts->trx->bts;
935 struct gsm_network *net = bts->network;
Harald Welte59b04682009-06-10 05:40:52 +0800936 u_int8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +0200937 char mi_string[GSM48_MI_SIZE];
Harald Welte59b04682009-06-10 05:40:52 +0800938
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +0200939 gsm48_mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
Harald Welte59b04682009-06-10 05:40:52 +0800940 DEBUGP(DMM, "IDENTITY RESPONSE: mi_type=0x%02x MI(%s)\n",
941 mi_type, mi_string);
942
Harald Welte59b04682009-06-10 05:40:52 +0800943 switch (mi_type) {
944 case GSM_MI_TYPE_IMSI:
Jan Luebbe9bdbd622009-08-12 10:19:34 +0200945 /* look up subscriber based on IMSI, create if not found */
946 if (!lchan->subscr) {
947 lchan->subscr = subscr_get_by_imsi(net, mi_string);
Harald Welte (local)16480eb2009-08-13 13:49:51 +0200948 if (!lchan->subscr)
949 lchan->subscr = db_create_subscriber(net, mi_string);
Jan Luebbee2974032009-08-12 10:12:52 +0200950 }
Harald Welte59b04682009-06-10 05:40:52 +0800951 if (lchan->loc_operation)
952 lchan->loc_operation->waiting_for_imsi = 0;
953 break;
954 case GSM_MI_TYPE_IMEI:
955 case GSM_MI_TYPE_IMEISV:
956 /* update subscribe <-> IMEI mapping */
Harald Welte (local)ced09ed2009-08-17 09:39:55 +0200957 if (lchan->subscr) {
Harald Welte59b04682009-06-10 05:40:52 +0800958 db_subscriber_assoc_imei(lchan->subscr, mi_string);
Harald Welte (local)ced09ed2009-08-17 09:39:55 +0200959 db_sync_equipment(&lchan->subscr->equipment);
960 }
Harald Welte59b04682009-06-10 05:40:52 +0800961 if (lchan->loc_operation)
962 lchan->loc_operation->waiting_for_imei = 0;
963 break;
964 }
965
966 /* Check if we can let the mobile station enter */
967 return gsm0408_authorize(lchan, msg);
968}
969
970
971static void loc_upd_rej_cb(void *data)
972{
973 struct gsm_lchan *lchan = data;
974
975 release_loc_updating_req(lchan);
976 gsm0408_loc_upd_rej(lchan, reject_cause);
977 lchan_auto_release(lchan);
978}
979
980static void schedule_reject(struct gsm_lchan *lchan)
981{
982 lchan->loc_operation->updating_timer.cb = loc_upd_rej_cb;
983 lchan->loc_operation->updating_timer.data = lchan;
984 bsc_schedule_timer(&lchan->loc_operation->updating_timer, 5, 0);
985}
986
987static const char *lupd_name(u_int8_t type)
988{
989 switch (type) {
990 case GSM48_LUPD_NORMAL:
991 return "NORMAL";
992 case GSM48_LUPD_PERIODIC:
993 return "PEROIDOC";
994 case GSM48_LUPD_IMSI_ATT:
995 return "IMSI ATTACH";
996 default:
997 return "UNKNOWN";
998 }
999}
1000
Harald Welte59b04682009-06-10 05:40:52 +08001001/* Chapter 9.2.15: Receive Location Updating Request */
1002static int mm_rx_loc_upd_req(struct msgb *msg)
1003{
1004 struct gsm48_hdr *gh = msgb_l3(msg);
1005 struct gsm48_loc_upd_req *lu;
Harald Welte03740842009-06-10 23:11:52 +08001006 struct gsm_subscriber *subscr = NULL;
Harald Welte59b04682009-06-10 05:40:52 +08001007 struct gsm_lchan *lchan = msg->lchan;
Harald Welte75350412009-07-23 18:46:00 +02001008 struct gsm_bts *bts = lchan->ts->trx->bts;
Harald Welte59b04682009-06-10 05:40:52 +08001009 u_int8_t mi_type;
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +02001010 char mi_string[GSM48_MI_SIZE];
Harald Welte59b04682009-06-10 05:40:52 +08001011 int rc;
1012
1013 lu = (struct gsm48_loc_upd_req *) gh->data;
1014
1015 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
1016
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +02001017 gsm48_mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
Harald Welte59b04682009-06-10 05:40:52 +08001018
Harald Welte79639662009-06-27 02:58:43 +02001019 DEBUGPC(DMM, "mi_type=0x%02x MI(%s) type=%s ", mi_type, mi_string,
Harald Welte59b04682009-06-10 05:40:52 +08001020 lupd_name(lu->type));
1021
1022 /*
1023 * Pseudo Spoof detection: Just drop a second/concurrent
1024 * location updating request.
1025 */
1026 if (lchan->loc_operation) {
Harald Welte79639662009-06-27 02:58:43 +02001027 DEBUGPC(DMM, "ignoring request due an existing one: %p.\n",
Harald Welte59b04682009-06-10 05:40:52 +08001028 lchan->loc_operation);
1029 gsm0408_loc_upd_rej(lchan, GSM48_REJECT_PROTOCOL_ERROR);
1030 return 0;
1031 }
1032
1033 allocate_loc_updating_req(lchan);
1034
1035 switch (mi_type) {
1036 case GSM_MI_TYPE_IMSI:
Harald Welte79639662009-06-27 02:58:43 +02001037 DEBUGPC(DMM, "\n");
Harald Welte59b04682009-06-10 05:40:52 +08001038 /* we always want the IMEI, too */
Harald Welte59b04682009-06-10 05:40:52 +08001039 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMEI);
1040 lchan->loc_operation->waiting_for_imei = 1;
1041
Jan Luebbe9bdbd622009-08-12 10:19:34 +02001042 /* look up subscriber based on IMSI, create if not found */
1043 subscr = subscr_get_by_imsi(bts->network, mi_string);
1044 if (!subscr) {
1045 subscr = db_create_subscriber(bts->network, mi_string);
1046 }
Harald Welte59b04682009-06-10 05:40:52 +08001047 break;
1048 case GSM_MI_TYPE_TMSI:
Harald Welte79639662009-06-27 02:58:43 +02001049 DEBUGPC(DMM, "\n");
Harald Welte59b04682009-06-10 05:40:52 +08001050 /* we always want the IMEI, too */
Harald Welte59b04682009-06-10 05:40:52 +08001051 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMEI);
1052 lchan->loc_operation->waiting_for_imei = 1;
1053
1054 /* look up the subscriber based on TMSI, request IMSI if it fails */
Holger Hans Peter Freythercd8bacf2009-08-19 12:53:57 +02001055 subscr = subscr_get_by_tmsi(bts->network,
1056 tmsi_from_string(mi_string));
Harald Welte59b04682009-06-10 05:40:52 +08001057 if (!subscr) {
1058 /* send IDENTITY REQUEST message to get IMSI */
Harald Welte59b04682009-06-10 05:40:52 +08001059 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMSI);
1060 lchan->loc_operation->waiting_for_imsi = 1;
1061 }
1062 break;
1063 case GSM_MI_TYPE_IMEI:
1064 case GSM_MI_TYPE_IMEISV:
1065 /* no sim card... FIXME: what to do ? */
Harald Welte79639662009-06-27 02:58:43 +02001066 DEBUGPC(DMM, "unimplemented mobile identity type\n");
Harald Welte59b04682009-06-10 05:40:52 +08001067 break;
1068 default:
Harald Welte79639662009-06-27 02:58:43 +02001069 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte59b04682009-06-10 05:40:52 +08001070 break;
1071 }
1072
Harald Welteccd69362009-07-04 10:18:00 +02001073 /* schedule the reject timer */
1074 schedule_reject(lchan);
1075
Harald Welte03740842009-06-10 23:11:52 +08001076 if (!subscr) {
Harald Welte79639662009-06-27 02:58:43 +02001077 DEBUGPC(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte03740842009-06-10 23:11:52 +08001078 /* FIXME: request id? close channel? */
1079 return -EINVAL;
1080 }
1081
Harald Welte59b04682009-06-10 05:40:52 +08001082 lchan->subscr = subscr;
Harald Welte (local)ced09ed2009-08-17 09:39:55 +02001083 lchan->subscr->equipment.classmark1 = lu->classmark1;
Harald Welte59b04682009-06-10 05:40:52 +08001084
Harald Welteccd69362009-07-04 10:18:00 +02001085 /* check if we can let the subscriber into our network immediately
1086 * or if we need to wait for identity responses. */
Harald Welte59b04682009-06-10 05:40:52 +08001087 return gsm0408_authorize(lchan, msg);
1088}
1089
Harald Welte03740842009-06-10 23:11:52 +08001090#if 0
1091static u_int8_t to_bcd8(u_int8_t val)
1092{
1093 return ((val / 10) << 4) | (val % 10);
1094}
1095#endif
1096
Harald Welte59b04682009-06-10 05:40:52 +08001097/* Section 9.2.15a */
1098int gsm48_tx_mm_info(struct gsm_lchan *lchan)
1099{
1100 struct msgb *msg = gsm48_msgb_alloc();
1101 struct gsm48_hdr *gh;
1102 struct gsm_network *net = lchan->ts->trx->bts->network;
1103 u_int8_t *ptr8;
Daniel Willmannc8188202009-08-13 03:42:07 +02001104 int name_len, name_pad;
Harald Welte03740842009-06-10 23:11:52 +08001105#if 0
1106 time_t cur_t;
1107 struct tm* cur_time;
1108 int tz15min;
1109#endif
Harald Welte59b04682009-06-10 05:40:52 +08001110
1111 msg->lchan = lchan;
1112
1113 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1114 gh->proto_discr = GSM48_PDISC_MM;
1115 gh->msg_type = GSM48_MT_MM_INFO;
1116
1117 if (net->name_long) {
Daniel Willmannc8188202009-08-13 03:42:07 +02001118#if 0
Harald Welte59b04682009-06-10 05:40:52 +08001119 name_len = strlen(net->name_long);
1120 /* 10.5.3.5a */
1121 ptr8 = msgb_put(msg, 3);
1122 ptr8[0] = GSM48_IE_NAME_LONG;
1123 ptr8[1] = name_len*2 +1;
1124 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
1125
1126 ptr16 = (u_int16_t *) msgb_put(msg, name_len*2);
1127 for (i = 0; i < name_len; i++)
1128 ptr16[i] = htons(net->name_long[i]);
1129
1130 /* FIXME: Use Cell Broadcast, not UCS-2, since
1131 * UCS-2 is only supported by later revisions of the spec */
Daniel Willmannc8188202009-08-13 03:42:07 +02001132#endif
1133 name_len = (strlen(net->name_long)*7)/8;
1134 name_pad = (8 - strlen(net->name_long)*7)%8;
1135 if (name_pad > 0)
1136 name_len++;
1137 /* 10.5.3.5a */
1138 ptr8 = msgb_put(msg, 3);
1139 ptr8[0] = GSM48_IE_NAME_LONG;
1140 ptr8[1] = name_len +1;
1141 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
1142
1143 ptr8 = msgb_put(msg, name_len);
1144 gsm_7bit_encode(ptr8, net->name_long);
1145
Harald Welte59b04682009-06-10 05:40:52 +08001146 }
1147
1148 if (net->name_short) {
Daniel Willmannc8188202009-08-13 03:42:07 +02001149#if 0
Harald Welte59b04682009-06-10 05:40:52 +08001150 name_len = strlen(net->name_short);
1151 /* 10.5.3.5a */
1152 ptr8 = (u_int8_t *) msgb_put(msg, 3);
Harald Weltef6284712009-07-19 17:51:36 +02001153 ptr8[0] = GSM48_IE_NAME_SHORT;
Harald Welte59b04682009-06-10 05:40:52 +08001154 ptr8[1] = name_len*2 + 1;
1155 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
1156
1157 ptr16 = (u_int16_t *) msgb_put(msg, name_len*2);
1158 for (i = 0; i < name_len; i++)
1159 ptr16[i] = htons(net->name_short[i]);
Daniel Willmannc8188202009-08-13 03:42:07 +02001160#endif
1161 name_len = (strlen(net->name_short)*7)/8;
1162 name_pad = (8 - strlen(net->name_short)*7)%8;
1163 if (name_pad > 0)
1164 name_len++;
1165 /* 10.5.3.5a */
1166 ptr8 = (u_int8_t *) msgb_put(msg, 3);
1167 ptr8[0] = GSM48_IE_NAME_SHORT;
1168 ptr8[1] = name_len +1;
1169 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
1170
1171 ptr8 = msgb_put(msg, name_len);
1172 gsm_7bit_encode(ptr8, net->name_short);
1173
Harald Welte59b04682009-06-10 05:40:52 +08001174 }
1175
1176#if 0
Harald Welte59b04682009-06-10 05:40:52 +08001177 /* Section 10.5.3.9 */
1178 cur_t = time(NULL);
Harald Welte03740842009-06-10 23:11:52 +08001179 cur_time = gmtime(&cur_t);
Harald Welte59b04682009-06-10 05:40:52 +08001180 ptr8 = msgb_put(msg, 8);
1181 ptr8[0] = GSM48_IE_NET_TIME_TZ;
1182 ptr8[1] = to_bcd8(cur_time->tm_year % 100);
1183 ptr8[2] = to_bcd8(cur_time->tm_mon);
1184 ptr8[3] = to_bcd8(cur_time->tm_mday);
1185 ptr8[4] = to_bcd8(cur_time->tm_hour);
1186 ptr8[5] = to_bcd8(cur_time->tm_min);
1187 ptr8[6] = to_bcd8(cur_time->tm_sec);
1188 /* 02.42: coded as BCD encoded signed value in units of 15 minutes */
1189 tz15min = (cur_time->tm_gmtoff)/(60*15);
Harald Welte03740842009-06-10 23:11:52 +08001190 ptr8[7] = to_bcd8(tz15min);
Harald Welte59b04682009-06-10 05:40:52 +08001191 if (tz15min < 0)
Harald Welte03740842009-06-10 23:11:52 +08001192 ptr8[7] |= 0x80;
Harald Welte59b04682009-06-10 05:40:52 +08001193#endif
1194
Daniel Willmannc8188202009-08-13 03:42:07 +02001195 DEBUGP(DMM, "-> MM INFO\n");
1196
Harald Welte36fe2e82009-07-23 21:13:03 +02001197 return gsm48_sendmsg(msg, NULL);
Harald Welte59b04682009-06-10 05:40:52 +08001198}
1199
Harald Welte36970f62009-08-12 22:56:50 +02001200/* Section 9.2.2 */
1201int gsm48_tx_mm_auth_req(struct gsm_lchan *lchan, u_int8_t *rand)
1202{
1203 struct msgb *msg = gsm48_msgb_alloc();
1204 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Sylvain Munaut6de6dc12009-09-27 11:10:17 +02001205 struct gsm48_auth_req *ar = (struct gsm48_auth_req *) msgb_put(msg, sizeof(*ar));
Harald Welte36970f62009-08-12 22:56:50 +02001206
1207 DEBUGP(DMM, "-> AUTH REQ\n");
1208
1209 msg->lchan = lchan;
1210 gh->proto_discr = GSM48_PDISC_MM;
1211 gh->msg_type = GSM48_MT_MM_AUTH_REQ;
1212
Sylvain Munaut6de6dc12009-09-27 11:10:17 +02001213 /* Key Sequence: FIXME fixed to 0 */
1214 ar->key_seq = 0;
1215
Harald Welte36970f62009-08-12 22:56:50 +02001216 /* 16 bytes RAND parameters */
Harald Welte36970f62009-08-12 22:56:50 +02001217 if (rand)
Sylvain Munaut6de6dc12009-09-27 11:10:17 +02001218 memcpy(ar->rand, rand, 16);
Harald Welte36970f62009-08-12 22:56:50 +02001219
1220 return gsm48_sendmsg(msg, NULL);
1221}
1222
1223/* Section 9.2.1 */
1224int gsm48_tx_mm_auth_rej(struct gsm_lchan *lchan)
1225{
1226 DEBUGP(DMM, "-> AUTH REJECT\n");
1227 return gsm48_tx_simple(lchan, GSM48_PDISC_MM, GSM48_MT_MM_AUTH_REJ);
1228}
1229
Harald Welte59b04682009-06-10 05:40:52 +08001230static int gsm48_tx_mm_serv_ack(struct gsm_lchan *lchan)
1231{
1232 DEBUGP(DMM, "-> CM SERVICE ACK\n");
1233 return gsm48_tx_simple(lchan, GSM48_PDISC_MM, GSM48_MT_MM_CM_SERV_ACC);
1234}
1235
1236/* 9.2.6 CM service reject */
1237static int gsm48_tx_mm_serv_rej(struct gsm_lchan *lchan,
1238 enum gsm48_reject_value value)
1239{
1240 struct msgb *msg = gsm48_msgb_alloc();
1241 struct gsm48_hdr *gh;
1242
1243 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
1244
1245 msg->lchan = lchan;
1246 use_lchan(lchan);
1247
1248 gh->proto_discr = GSM48_PDISC_MM;
1249 gh->msg_type = GSM48_MT_MM_CM_SERV_REJ;
1250 gh->data[0] = value;
1251 DEBUGP(DMM, "-> CM SERVICE Reject cause: %d\n", value);
1252
Harald Welte36fe2e82009-07-23 21:13:03 +02001253 return gsm48_sendmsg(msg, NULL);
Harald Welte59b04682009-06-10 05:40:52 +08001254}
1255
Harald Welte59b04682009-06-10 05:40:52 +08001256/*
1257 * Handle CM Service Requests
1258 * a) Verify that the packet is long enough to contain the information
1259 * we require otherwsie reject with INCORRECT_MESSAGE
1260 * b) Try to parse the TMSI. If we do not have one reject
1261 * c) Check that we know the subscriber with the TMSI otherwise reject
1262 * with a HLR cause
1263 * d) Set the subscriber on the gsm_lchan and accept
1264 */
1265static int gsm48_rx_mm_serv_req(struct msgb *msg)
1266{
1267 u_int8_t mi_type;
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +02001268 char mi_string[GSM48_MI_SIZE];
Harald Welte59b04682009-06-10 05:40:52 +08001269
Harald Welte75350412009-07-23 18:46:00 +02001270 struct gsm_bts *bts = msg->lchan->ts->trx->bts;
Harald Welte59b04682009-06-10 05:40:52 +08001271 struct gsm_subscriber *subscr;
1272 struct gsm48_hdr *gh = msgb_l3(msg);
1273 struct gsm48_service_request *req =
1274 (struct gsm48_service_request *)gh->data;
1275 /* unfortunately in Phase1 the classmar2 length is variable */
1276 u_int8_t classmark2_len = gh->data[1];
1277 u_int8_t *classmark2 = gh->data+2;
1278 u_int8_t mi_len = *(classmark2 + classmark2_len);
1279 u_int8_t *mi = (classmark2 + classmark2_len + 1);
1280
1281 DEBUGP(DMM, "<- CM SERVICE REQUEST ");
1282 if (msg->data_len < sizeof(struct gsm48_service_request*)) {
1283 DEBUGPC(DMM, "wrong sized message\n");
1284 return gsm48_tx_mm_serv_rej(msg->lchan,
1285 GSM48_REJECT_INCORRECT_MESSAGE);
1286 }
1287
1288 if (msg->data_len < req->mi_len + 6) {
1289 DEBUGPC(DMM, "does not fit in packet\n");
1290 return gsm48_tx_mm_serv_rej(msg->lchan,
1291 GSM48_REJECT_INCORRECT_MESSAGE);
1292 }
1293
1294 mi_type = mi[0] & GSM_MI_TYPE_MASK;
1295 if (mi_type != GSM_MI_TYPE_TMSI) {
1296 DEBUGPC(DMM, "mi_type is not TMSI: %d\n", mi_type);
1297 return gsm48_tx_mm_serv_rej(msg->lchan,
1298 GSM48_REJECT_INCORRECT_MESSAGE);
1299 }
1300
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +02001301 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
Harald Welte59b04682009-06-10 05:40:52 +08001302 DEBUGPC(DMM, "serv_type=0x%02x mi_type=0x%02x M(%s)\n",
1303 req->cm_service_type, mi_type, mi_string);
1304
Harald Welte583ceaf2009-08-10 10:12:45 +02001305 if (is_siemens_bts(bts))
1306 send_siemens_mrpci(msg->lchan, classmark2-1);
1307
Holger Hans Peter Freythercd8bacf2009-08-19 12:53:57 +02001308 subscr = subscr_get_by_tmsi(bts->network,
1309 tmsi_from_string(mi_string));
Harald Welte59b04682009-06-10 05:40:52 +08001310
1311 /* FIXME: if we don't know the TMSI, inquire abit IMSI and allocate new TMSI */
1312 if (!subscr)
1313 return gsm48_tx_mm_serv_rej(msg->lchan,
1314 GSM48_REJECT_IMSI_UNKNOWN_IN_HLR);
1315
1316 if (!msg->lchan->subscr)
1317 msg->lchan->subscr = subscr;
1318 else if (msg->lchan->subscr != subscr) {
1319 DEBUGP(DMM, "<- CM Channel already owned by someone else?\n");
1320 subscr_put(subscr);
1321 }
1322
Harald Weltef6845a72009-07-05 14:08:13 +02001323 subscr->equipment.classmark2_len = classmark2_len;
1324 memcpy(subscr->equipment.classmark2, classmark2, classmark2_len);
1325 db_sync_equipment(&subscr->equipment);
Harald Welte59b04682009-06-10 05:40:52 +08001326
1327 return gsm48_tx_mm_serv_ack(msg->lchan);
1328}
1329
1330static int gsm48_rx_mm_imsi_detach_ind(struct msgb *msg)
1331{
Harald Welte75350412009-07-23 18:46:00 +02001332 struct gsm_bts *bts = msg->lchan->ts->trx->bts;
Harald Welte59b04682009-06-10 05:40:52 +08001333 struct gsm48_hdr *gh = msgb_l3(msg);
1334 struct gsm48_imsi_detach_ind *idi =
1335 (struct gsm48_imsi_detach_ind *) gh->data;
1336 u_int8_t mi_type = idi->mi[0] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +02001337 char mi_string[GSM48_MI_SIZE];
Harald Welte03740842009-06-10 23:11:52 +08001338 struct gsm_subscriber *subscr = NULL;
Harald Welte59b04682009-06-10 05:40:52 +08001339
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +02001340 gsm48_mi_to_string(mi_string, sizeof(mi_string), idi->mi, idi->mi_len);
Harald Welte59b04682009-06-10 05:40:52 +08001341 DEBUGP(DMM, "IMSI DETACH INDICATION: mi_type=0x%02x MI(%s): ",
1342 mi_type, mi_string);
1343
1344 switch (mi_type) {
1345 case GSM_MI_TYPE_TMSI:
Holger Hans Peter Freythercd8bacf2009-08-19 12:53:57 +02001346 subscr = subscr_get_by_tmsi(bts->network,
1347 tmsi_from_string(mi_string));
Harald Welte59b04682009-06-10 05:40:52 +08001348 break;
1349 case GSM_MI_TYPE_IMSI:
Harald Welte75350412009-07-23 18:46:00 +02001350 subscr = subscr_get_by_imsi(bts->network, mi_string);
Harald Welte59b04682009-06-10 05:40:52 +08001351 break;
1352 case GSM_MI_TYPE_IMEI:
1353 case GSM_MI_TYPE_IMEISV:
1354 /* no sim card... FIXME: what to do ? */
1355 DEBUGPC(DMM, "unimplemented mobile identity type\n");
1356 break;
1357 default:
1358 DEBUGPC(DMM, "unknown mobile identity type\n");
1359 break;
1360 }
1361
1362 if (subscr) {
1363 subscr_update(subscr, msg->trx->bts,
1364 GSM_SUBSCRIBER_UPDATE_DETACHED);
1365 DEBUGP(DMM, "Subscriber: %s\n",
1366 subscr->name ? subscr->name : subscr->imsi);
Harald Welte (local)ced09ed2009-08-17 09:39:55 +02001367
1368 subscr->equipment.classmark1 = idi->classmark1;
1369 db_sync_equipment(&subscr->equipment);
1370
Harald Welte59b04682009-06-10 05:40:52 +08001371 subscr_put(subscr);
1372 } else
1373 DEBUGP(DMM, "Unknown Subscriber ?!?\n");
1374
Harald Welte59b04682009-06-10 05:40:52 +08001375 return 0;
1376}
1377
1378static int gsm48_rx_mm_status(struct msgb *msg)
1379{
1380 struct gsm48_hdr *gh = msgb_l3(msg);
1381
1382 DEBUGP(DMM, "MM STATUS (reject cause 0x%02x)\n", gh->data[0]);
1383
1384 return 0;
1385}
1386
1387/* Receive a GSM 04.08 Mobility Management (MM) message */
1388static int gsm0408_rcv_mm(struct msgb *msg)
1389{
1390 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte03740842009-06-10 23:11:52 +08001391 int rc = 0;
Harald Welte59b04682009-06-10 05:40:52 +08001392
1393 switch (gh->msg_type & 0xbf) {
1394 case GSM48_MT_MM_LOC_UPD_REQUEST:
Harald Welte79639662009-06-27 02:58:43 +02001395 DEBUGP(DMM, "LOCATION UPDATING REQUEST: ");
Harald Welte59b04682009-06-10 05:40:52 +08001396 rc = mm_rx_loc_upd_req(msg);
1397 break;
1398 case GSM48_MT_MM_ID_RESP:
1399 rc = mm_rx_id_resp(msg);
1400 break;
1401 case GSM48_MT_MM_CM_SERV_REQ:
1402 rc = gsm48_rx_mm_serv_req(msg);
1403 break;
1404 case GSM48_MT_MM_STATUS:
1405 rc = gsm48_rx_mm_status(msg);
1406 break;
1407 case GSM48_MT_MM_TMSI_REALL_COMPL:
1408 DEBUGP(DMM, "TMSI Reallocation Completed. Subscriber: %s\n",
1409 msg->lchan->subscr ?
1410 msg->lchan->subscr->imsi :
1411 "unknown subscriber");
1412 break;
1413 case GSM48_MT_MM_IMSI_DETACH_IND:
1414 rc = gsm48_rx_mm_imsi_detach_ind(msg);
1415 break;
1416 case GSM48_MT_MM_CM_REEST_REQ:
1417 DEBUGP(DMM, "CM REESTABLISH REQUEST: Not implemented\n");
1418 break;
1419 case GSM48_MT_MM_AUTH_RESP:
1420 DEBUGP(DMM, "AUTHENTICATION RESPONSE: Not implemented\n");
1421 break;
1422 default:
1423 fprintf(stderr, "Unknown GSM 04.08 MM msg type 0x%02x\n",
1424 gh->msg_type);
1425 break;
1426 }
1427
1428 return rc;
1429}
1430
1431/* Receive a PAGING RESPONSE message from the MS */
Holger Hans Peter Freytherf10170e2009-10-23 13:48:54 +02001432static int gsm48_rx_rr_pag_resp(struct msgb *msg)
Harald Welte59b04682009-06-10 05:40:52 +08001433{
Harald Welte75350412009-07-23 18:46:00 +02001434 struct gsm_bts *bts = msg->lchan->ts->trx->bts;
Harald Welte59b04682009-06-10 05:40:52 +08001435 struct gsm48_hdr *gh = msgb_l3(msg);
1436 u_int8_t *classmark2_lv = gh->data + 1;
Holger Hans Peter Freyther1e43b8d2009-08-21 05:18:21 +02001437 u_int8_t mi_type;
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +02001438 char mi_string[GSM48_MI_SIZE];
Harald Welte03740842009-06-10 23:11:52 +08001439 struct gsm_subscriber *subscr = NULL;
Harald Welte59b04682009-06-10 05:40:52 +08001440 int rc = 0;
1441
Holger Hans Peter Freyther1e43b8d2009-08-21 05:18:21 +02001442 gsm48_paging_extract_mi(msg, mi_string, &mi_type);
Harald Welte59b04682009-06-10 05:40:52 +08001443 DEBUGP(DRR, "PAGING RESPONSE: mi_type=0x%02x MI(%s)\n",
1444 mi_type, mi_string);
Harald Welte583ceaf2009-08-10 10:12:45 +02001445
Harald Welte59b04682009-06-10 05:40:52 +08001446 switch (mi_type) {
1447 case GSM_MI_TYPE_TMSI:
Holger Hans Peter Freythercd8bacf2009-08-19 12:53:57 +02001448 subscr = subscr_get_by_tmsi(bts->network,
1449 tmsi_from_string(mi_string));
Harald Welte59b04682009-06-10 05:40:52 +08001450 break;
1451 case GSM_MI_TYPE_IMSI:
Harald Welte75350412009-07-23 18:46:00 +02001452 subscr = subscr_get_by_imsi(bts->network, mi_string);
Harald Welte59b04682009-06-10 05:40:52 +08001453 break;
1454 }
1455
1456 if (!subscr) {
1457 DEBUGP(DRR, "<- Can't find any subscriber for this ID\n");
1458 /* FIXME: request id? close channel? */
1459 return -EINVAL;
1460 }
1461 DEBUGP(DRR, "<- Channel was requested by %s\n",
Harald Welte68b7df22009-08-08 16:03:15 +02001462 subscr->name && strlen(subscr->name) ? subscr->name : subscr->imsi);
Harald Welte59b04682009-06-10 05:40:52 +08001463
Harald Weltef6845a72009-07-05 14:08:13 +02001464 subscr->equipment.classmark2_len = *classmark2_lv;
1465 memcpy(subscr->equipment.classmark2, classmark2_lv+1, *classmark2_lv);
1466 db_sync_equipment(&subscr->equipment);
Harald Welte59b04682009-06-10 05:40:52 +08001467
Holger Hans Peter Freyther1e43b8d2009-08-21 05:18:21 +02001468 rc = gsm48_handle_paging_resp(msg, subscr);
Harald Welte59b04682009-06-10 05:40:52 +08001469 return rc;
1470}
1471
1472static int gsm48_rx_rr_classmark(struct msgb *msg)
1473{
1474 struct gsm48_hdr *gh = msgb_l3(msg);
1475 struct gsm_subscriber *subscr = msg->lchan->subscr;
1476 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1477 u_int8_t cm2_len, cm3_len = 0;
1478 u_int8_t *cm2, *cm3 = NULL;
1479
1480 DEBUGP(DRR, "CLASSMARK CHANGE ");
1481
1482 /* classmark 2 */
1483 cm2_len = gh->data[0];
1484 cm2 = &gh->data[1];
1485 DEBUGPC(DRR, "CM2(len=%u) ", cm2_len);
1486
1487 if (payload_len > cm2_len + 1) {
1488 /* we must have a classmark3 */
1489 if (gh->data[cm2_len+1] != 0x20) {
1490 DEBUGPC(DRR, "ERR CM3 TAG\n");
1491 return -EINVAL;
1492 }
1493 if (cm2_len > 3) {
1494 DEBUGPC(DRR, "CM2 too long!\n");
1495 return -EINVAL;
1496 }
1497
1498 cm3_len = gh->data[cm2_len+2];
1499 cm3 = &gh->data[cm2_len+3];
1500 if (cm3_len > 14) {
1501 DEBUGPC(DRR, "CM3 len %u too long!\n", cm3_len);
1502 return -EINVAL;
1503 }
1504 DEBUGPC(DRR, "CM3(len=%u)\n", cm3_len);
1505 }
1506 if (subscr) {
Harald Weltef6845a72009-07-05 14:08:13 +02001507 subscr->equipment.classmark2_len = cm2_len;
1508 memcpy(subscr->equipment.classmark2, cm2, cm2_len);
Harald Welte59b04682009-06-10 05:40:52 +08001509 if (cm3) {
Harald Weltef6845a72009-07-05 14:08:13 +02001510 subscr->equipment.classmark3_len = cm3_len;
1511 memcpy(subscr->equipment.classmark3, cm3, cm3_len);
Harald Welte59b04682009-06-10 05:40:52 +08001512 }
Harald Weltef6845a72009-07-05 14:08:13 +02001513 db_sync_equipment(&subscr->equipment);
Harald Welte59b04682009-06-10 05:40:52 +08001514 }
1515
Harald Welte59b04682009-06-10 05:40:52 +08001516 return 0;
1517}
1518
1519static int gsm48_rx_rr_status(struct msgb *msg)
1520{
1521 struct gsm48_hdr *gh = msgb_l3(msg);
1522
1523 DEBUGP(DRR, "STATUS rr_cause = %s\n",
1524 rr_cause_name(gh->data[0]));
1525
1526 return 0;
1527}
1528
1529static int gsm48_rx_rr_meas_rep(struct msgb *msg)
1530{
1531 struct gsm48_hdr *gh = msgb_l3(msg);
1532 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1533 static struct gsm_meas_rep meas_rep;
1534
Harald Welte02993682009-06-27 02:53:10 +02001535 DEBUGP(DMEAS, "MEASUREMENT REPORT ");
Harald Welte59b04682009-06-10 05:40:52 +08001536 parse_meas_rep(&meas_rep, gh->data, payload_len);
1537 if (meas_rep.flags & MEAS_REP_F_DTX)
Harald Welte02993682009-06-27 02:53:10 +02001538 DEBUGPC(DMEAS, "DTX ");
Harald Welte59b04682009-06-10 05:40:52 +08001539 if (meas_rep.flags & MEAS_REP_F_BA1)
Harald Welte02993682009-06-27 02:53:10 +02001540 DEBUGPC(DMEAS, "BA1 ");
Harald Welte59b04682009-06-10 05:40:52 +08001541 if (!(meas_rep.flags & MEAS_REP_F_VALID))
Harald Welte02993682009-06-27 02:53:10 +02001542 DEBUGPC(DMEAS, "NOT VALID ");
Harald Welte59b04682009-06-10 05:40:52 +08001543 else
Harald Welte02993682009-06-27 02:53:10 +02001544 DEBUGPC(DMEAS, "FULL(lev=%u, qual=%u) SUB(lev=%u, qual=%u) ",
Harald Welte59b04682009-06-10 05:40:52 +08001545 meas_rep.rxlev_full, meas_rep.rxqual_full, meas_rep.rxlev_sub,
1546 meas_rep.rxqual_sub);
1547
Harald Welte02993682009-06-27 02:53:10 +02001548 DEBUGPC(DMEAS, "NUM_NEIGH=%u\n", meas_rep.num_cell);
Harald Welte59b04682009-06-10 05:40:52 +08001549
1550 /* FIXME: put the results somwhere */
1551
1552 return 0;
1553}
1554
Harald Welte (local)b5103922009-08-15 23:32:44 +02001555static int gsm48_rx_rr_app_info(struct msgb *msg)
1556{
1557 struct gsm48_hdr *gh = msgb_l3(msg);
1558 u_int8_t apdu_id_flags;
1559 u_int8_t apdu_len;
1560 u_int8_t *apdu_data;
1561
1562 apdu_id_flags = gh->data[0];
1563 apdu_len = gh->data[1];
1564 apdu_data = gh->data+2;
1565
1566 DEBUGP(DNM, "RX APPLICATION INFO id/flags=0x%02x apdu_len=%u apdu=%s",
1567 apdu_id_flags, apdu_len, hexdump(apdu_data, apdu_len));
1568
Harald Welte (local)bd72aa22009-08-16 10:40:10 +02001569 return db_apdu_blob_store(msg->lchan->subscr, apdu_id_flags, apdu_len, apdu_data);
Harald Welte (local)b5103922009-08-15 23:32:44 +02001570}
1571
1572
Harald Welte59b04682009-06-10 05:40:52 +08001573/* Receive a GSM 04.08 Radio Resource (RR) message */
1574static int gsm0408_rcv_rr(struct msgb *msg)
1575{
1576 struct gsm48_hdr *gh = msgb_l3(msg);
1577 int rc = 0;
1578
1579 switch (gh->msg_type) {
1580 case GSM48_MT_RR_CLSM_CHG:
1581 rc = gsm48_rx_rr_classmark(msg);
1582 break;
1583 case GSM48_MT_RR_GPRS_SUSP_REQ:
1584 DEBUGP(DRR, "GRPS SUSPEND REQUEST\n");
1585 break;
1586 case GSM48_MT_RR_PAG_RESP:
Holger Hans Peter Freytherf10170e2009-10-23 13:48:54 +02001587 rc = gsm48_rx_rr_pag_resp(msg);
Harald Welte59b04682009-06-10 05:40:52 +08001588 break;
1589 case GSM48_MT_RR_CHAN_MODE_MODIF_ACK:
1590 DEBUGP(DRR, "CHANNEL MODE MODIFY ACK\n");
Harald Welteca745e22009-07-29 12:10:35 +02001591 /* We've successfully modified the MS side of the channel,
1592 * now go on to modify the BTS side of the channel */
Harald Welte39274f42009-07-29 15:41:29 +02001593 msg->lchan->rsl_cmode = RSL_CMOD_SPD_SPEECH;
Harald Welte59b04682009-06-10 05:40:52 +08001594 rc = rsl_chan_mode_modify_req(msg->lchan);
1595 break;
1596 case GSM48_MT_RR_STATUS:
1597 rc = gsm48_rx_rr_status(msg);
1598 break;
1599 case GSM48_MT_RR_MEAS_REP:
1600 rc = gsm48_rx_rr_meas_rep(msg);
1601 break;
Harald Welte (local)b5103922009-08-15 23:32:44 +02001602 case GSM48_MT_RR_APP_INFO:
1603 rc = gsm48_rx_rr_app_info(msg);
1604 break;
Harald Welted2dd9de2009-08-30 15:37:11 +09001605 case GSM48_MT_RR_CIPH_M_COMPL:
1606 DEBUGP(DRR, "CIPHERING MODE COMPLETE\n");
1607 /* FIXME: check for MI (if any) */
1608 break;
Harald Welte59b04682009-06-10 05:40:52 +08001609 default:
1610 fprintf(stderr, "Unimplemented GSM 04.08 RR msg type 0x%02x\n",
1611 gh->msg_type);
1612 break;
1613 }
1614
1615 return rc;
1616}
1617
Harald Welte (local)b5103922009-08-15 23:32:44 +02001618int gsm48_send_rr_app_info(struct gsm_lchan *lchan, u_int8_t apdu_id,
Holger Hans Peter Freytherd70da872009-10-22 15:42:19 +02001619 u_int8_t apdu_len, const u_int8_t *apdu)
Harald Welte (local)b5103922009-08-15 23:32:44 +02001620{
1621 struct msgb *msg = gsm48_msgb_alloc();
1622 struct gsm48_hdr *gh;
1623
1624 msg->lchan = lchan;
1625
1626 DEBUGP(DRR, "TX APPLICATION INFO id=0x%02x, len=%u\n",
1627 apdu_id, apdu_len);
1628
1629 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 2 + apdu_len);
1630 gh->proto_discr = GSM48_PDISC_RR;
1631 gh->msg_type = GSM48_MT_RR_APP_INFO;
1632 gh->data[0] = apdu_id;
1633 gh->data[1] = apdu_len;
1634 memcpy(gh->data+2, apdu, apdu_len);
1635
1636 return gsm48_sendmsg(msg, NULL);
1637}
1638
Harald Welte59b04682009-06-10 05:40:52 +08001639/* Call Control */
1640
1641/* The entire call control code is written in accordance with Figure 7.10c
1642 * for 'very early assignment', i.e. we allocate a TCH/F during IMMEDIATE
1643 * ASSIGN, then first use that TCH/F for signalling and later MODE MODIFY
1644 * it for voice */
1645
Harald Welte03740842009-06-10 23:11:52 +08001646static void new_cc_state(struct gsm_trans *trans, int state)
1647{
1648 if (state > 31 || state < 0)
1649 return;
1650
1651 DEBUGP(DCC, "new state %s -> %s\n",
Harald Weltec2189a62009-07-23 18:56:43 +02001652 cc_state_names[trans->cc.state], cc_state_names[state]);
Harald Welte03740842009-06-10 23:11:52 +08001653
Harald Weltec2189a62009-07-23 18:56:43 +02001654 trans->cc.state = state;
Harald Welte03740842009-06-10 23:11:52 +08001655}
1656
1657static int gsm48_cc_tx_status(struct gsm_trans *trans, void *arg)
Harald Welte59b04682009-06-10 05:40:52 +08001658{
1659 struct msgb *msg = gsm48_msgb_alloc();
1660 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1661 u_int8_t *cause, *call_state;
1662
Harald Welte59b04682009-06-10 05:40:52 +08001663 gh->msg_type = GSM48_MT_CC_STATUS;
1664
1665 cause = msgb_put(msg, 3);
1666 cause[0] = 2;
1667 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
1668 cause[2] = 0x80 | 30; /* response to status inquiry */
1669
1670 call_state = msgb_put(msg, 1);
1671 call_state[0] = 0xc0 | 0x00;
1672
Harald Welte36fe2e82009-07-23 21:13:03 +02001673 return gsm48_sendmsg(msg, trans);
Harald Welte59b04682009-06-10 05:40:52 +08001674}
1675
1676static int gsm48_tx_simple(struct gsm_lchan *lchan,
1677 u_int8_t pdisc, u_int8_t msg_type)
1678{
1679 struct msgb *msg = gsm48_msgb_alloc();
1680 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1681
1682 msg->lchan = lchan;
1683
1684 gh->proto_discr = pdisc;
1685 gh->msg_type = msg_type;
1686
Harald Welte36fe2e82009-07-23 21:13:03 +02001687 return gsm48_sendmsg(msg, NULL);
Harald Welte59b04682009-06-10 05:40:52 +08001688}
1689
Harald Welte03740842009-06-10 23:11:52 +08001690static void gsm48_stop_cc_timer(struct gsm_trans *trans)
1691{
Harald Weltec2189a62009-07-23 18:56:43 +02001692 if (bsc_timer_pending(&trans->cc.timer)) {
1693 DEBUGP(DCC, "stopping pending timer T%x\n", trans->cc.Tcurrent);
1694 bsc_del_timer(&trans->cc.timer);
1695 trans->cc.Tcurrent = 0;
Harald Welte03740842009-06-10 23:11:52 +08001696 }
1697}
1698
1699static int mncc_recvmsg(struct gsm_network *net, struct gsm_trans *trans,
1700 int msg_type, struct gsm_mncc *mncc)
1701{
1702 struct msgb *msg;
1703
1704 if (trans)
1705 if (trans->lchan)
Harald Welte4861c822009-07-23 21:21:14 +02001706 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte03740842009-06-10 23:11:52 +08001707 "Sending '%s' to MNCC.\n",
1708 trans->lchan->ts->trx->bts->nr,
1709 trans->lchan->ts->trx->nr,
1710 trans->lchan->ts->nr, trans->transaction_id,
1711 (trans->subscr)?(trans->subscr->extension):"-",
1712 get_mncc_name(msg_type));
1713 else
1714 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
1715 "Sending '%s' to MNCC.\n",
1716 (trans->subscr)?(trans->subscr->extension):"-",
1717 get_mncc_name(msg_type));
1718 else
1719 DEBUGP(DCC, "(bts - trx - ts - ti -- sub -) "
1720 "Sending '%s' to MNCC.\n", get_mncc_name(msg_type));
1721
1722 mncc->msg_type = msg_type;
1723
Harald Welte9cfc9352009-06-26 19:39:35 +02001724 msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
Harald Welte03740842009-06-10 23:11:52 +08001725 if (!msg)
1726 return -ENOMEM;
1727 memcpy(msg->data, mncc, sizeof(struct gsm_mncc));
1728 msgb_enqueue(&net->upqueue, msg);
1729
1730 return 0;
1731}
1732
1733int mncc_release_ind(struct gsm_network *net, struct gsm_trans *trans,
1734 u_int32_t callref, int location, int value)
1735{
1736 struct gsm_mncc rel;
1737
Harald Weltecb0595f2009-06-12 01:54:08 +08001738 memset(&rel, 0, sizeof(rel));
Harald Welte03740842009-06-10 23:11:52 +08001739 rel.callref = callref;
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08001740 mncc_set_cause(&rel, location, value);
Harald Welte03740842009-06-10 23:11:52 +08001741 return mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
1742}
1743
Harald Weltec2189a62009-07-23 18:56:43 +02001744/* Call Control Specific transaction release.
1745 * gets called by trans_free, DO NOT CALL YOURSELF! */
1746void _gsm48_cc_trans_free(struct gsm_trans *trans)
Harald Welte03740842009-06-10 23:11:52 +08001747{
Harald Welte03740842009-06-10 23:11:52 +08001748 gsm48_stop_cc_timer(trans);
1749
1750 /* send release to L4, if callref still exists */
1751 if (trans->callref) {
1752 /* Ressource unavailable */
Harald Welte0abe5a62009-07-23 19:06:52 +02001753 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08001754 GSM48_CAUSE_LOC_PRN_S_LU,
1755 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte03740842009-06-10 23:11:52 +08001756 }
Harald Weltec2189a62009-07-23 18:56:43 +02001757 if (trans->cc.state != GSM_CSTATE_NULL)
Harald Welte03740842009-06-10 23:11:52 +08001758 new_cc_state(trans, GSM_CSTATE_NULL);
Harald Weltec2189a62009-07-23 18:56:43 +02001759 if (trans->lchan)
1760 trau_mux_unmap(&trans->lchan->ts->e1_link, trans->callref);
Harald Welte03740842009-06-10 23:11:52 +08001761}
1762
1763static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg);
1764
Harald Welte59b04682009-06-10 05:40:52 +08001765/* call-back from paging the B-end of the connection */
1766static int setup_trig_pag_evt(unsigned int hooknum, unsigned int event,
1767 struct msgb *msg, void *_lchan, void *param)
1768{
1769 struct gsm_lchan *lchan = _lchan;
Harald Welte03740842009-06-10 23:11:52 +08001770 struct gsm_subscriber *subscr = param;
1771 struct gsm_trans *transt, *tmp;
1772 struct gsm_network *net;
Harald Welte1ff81b52009-06-26 20:17:06 +02001773
Harald Welte59b04682009-06-10 05:40:52 +08001774 if (hooknum != GSM_HOOK_RR_PAGING)
1775 return -EINVAL;
Harald Welte03740842009-06-10 23:11:52 +08001776
1777 if (!subscr)
1778 return -EINVAL;
1779 net = subscr->net;
1780 if (!net) {
1781 DEBUGP(DCC, "Error Network not set!\n");
1782 return -EINVAL;
Harald Welte59b04682009-06-10 05:40:52 +08001783 }
1784
Harald Welte03740842009-06-10 23:11:52 +08001785 /* check all tranactions (without lchan) for subscriber */
1786 llist_for_each_entry_safe(transt, tmp, &net->trans_list, entry) {
1787 if (transt->subscr != subscr || transt->lchan)
1788 continue;
1789 switch (event) {
1790 case GSM_PAGING_SUCCEEDED:
1791 if (!lchan) // paranoid
1792 break;
1793 DEBUGP(DCC, "Paging subscr %s succeeded!\n",
1794 subscr->extension);
1795 /* Assign lchan */
1796 if (!transt->lchan) {
1797 transt->lchan = lchan;
1798 use_lchan(lchan);
1799 }
1800 /* send SETUP request to called party */
Harald Weltec2189a62009-07-23 18:56:43 +02001801 gsm48_cc_tx_setup(transt, &transt->cc.msg);
Harald Welte03740842009-06-10 23:11:52 +08001802 break;
1803 case GSM_PAGING_EXPIRED:
1804 DEBUGP(DCC, "Paging subscr %s expired!\n",
1805 subscr->extension);
1806 /* Temporarily out of order */
Harald Welte0abe5a62009-07-23 19:06:52 +02001807 mncc_release_ind(transt->subscr->net, transt,
1808 transt->callref,
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08001809 GSM48_CAUSE_LOC_PRN_S_LU,
1810 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte03740842009-06-10 23:11:52 +08001811 transt->callref = 0;
Harald Weltec2189a62009-07-23 18:56:43 +02001812 trans_free(transt);
Harald Welte03740842009-06-10 23:11:52 +08001813 break;
1814 }
1815 }
Harald Welte59b04682009-06-10 05:40:52 +08001816 return 0;
1817}
1818
Harald Welte3c062072009-07-28 18:25:29 +02001819/* some other part of the code sends us a signal */
1820static int handle_abisip_signal(unsigned int subsys, unsigned int signal,
1821 void *handler_data, void *signal_data)
1822{
1823 struct gsm_lchan *lchan = signal_data;
1824 struct gsm_bts_trx_ts *ts;
1825 int rc;
1826
1827 if (subsys != SS_ABISIP)
1828 return 0;
1829
1830 /* in case we use direct BTS-to-BTS RTP */
1831 if (ipacc_rtp_direct)
1832 return 0;
1833
1834 ts = lchan->ts;
1835
1836 switch (signal) {
1837 case S_ABISIP_BIND_ACK:
1838 /* the BTS has successfully bound a TCH to a local ip/port,
1839 * which means we can connect our UDP socket to it */
1840 if (ts->abis_ip.rtp_socket) {
1841 rtp_socket_free(ts->abis_ip.rtp_socket);
1842 ts->abis_ip.rtp_socket = NULL;
1843 }
1844
1845 ts->abis_ip.rtp_socket = rtp_socket_create();
1846 if (!ts->abis_ip.rtp_socket)
1847 goto out_err;
1848
1849 rc = rtp_socket_connect(ts->abis_ip.rtp_socket,
1850 ts->abis_ip.bound_ip,
1851 ts->abis_ip.bound_port);
1852 if (rc < 0)
1853 goto out_err;
1854 break;
1855 case S_ABISIP_DISC_IND:
1856 /* the BTS tells us a RTP stream has been disconnected */
1857 if (ts->abis_ip.rtp_socket) {
1858 rtp_socket_free(ts->abis_ip.rtp_socket);
1859 ts->abis_ip.rtp_socket = NULL;
1860 }
1861 break;
1862 }
1863
1864 return 0;
1865out_err:
1866 /* FIXME: do something */
1867 return 0;
1868}
1869
1870/* bind rtp proxy to local IP/port and tell BTS to connect to it */
1871static int ipacc_connect_proxy_bind(struct gsm_lchan *lchan)
1872{
1873 struct gsm_bts_trx_ts *ts = lchan->ts;
1874 struct rtp_socket *rs = ts->abis_ip.rtp_socket;
1875 int rc;
1876
1877 rc = rsl_ipacc_connect(lchan, ntohl(rs->rtp.sin_local.sin_addr.s_addr),
1878 ntohs(rs->rtp.sin_local.sin_port),
1879 ts->abis_ip.conn_id,
1880 /* FIXME: use RTP payload of bound socket, not BTS*/
1881 ts->abis_ip.rtp_payload2);
1882
1883 return rc;
1884}
1885
Harald Welte59b04682009-06-10 05:40:52 +08001886/* map two ipaccess RTP streams onto each other */
1887static int tch_map(struct gsm_lchan *lchan, struct gsm_lchan *remote_lchan)
1888{
1889 struct gsm_bts *bts = lchan->ts->trx->bts;
1890 struct gsm_bts *remote_bts = remote_lchan->ts->trx->bts;
1891 struct gsm_bts_trx_ts *ts;
Harald Welte3c062072009-07-28 18:25:29 +02001892 int rc;
Harald Welte59b04682009-06-10 05:40:52 +08001893
1894 DEBUGP(DCC, "Setting up TCH map between (bts=%u,trx=%u,ts=%u) and (bts=%u,trx=%u,ts=%u)\n",
1895 bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
1896 remote_bts->nr, remote_lchan->ts->trx->nr, remote_lchan->ts->nr);
1897
1898 if (bts->type != remote_bts->type) {
1899 DEBUGP(DCC, "Cannot switch calls between different BTS types yet\n");
1900 return -EINVAL;
1901 }
1902
1903 switch (bts->type) {
Mike Haben66e0ba02009-10-02 12:19:34 +01001904 case GSM_BTS_TYPE_NANOBTS:
Harald Welte3c062072009-07-28 18:25:29 +02001905 if (!ipacc_rtp_direct) {
1906 /* connect the TCH's to our RTP proxy */
1907 rc = ipacc_connect_proxy_bind(lchan);
1908 if (rc < 0)
1909 return rc;
1910 rc = ipacc_connect_proxy_bind(remote_lchan);
1911
1912 /* connect them with each other */
1913 rtp_socket_proxy(lchan->ts->abis_ip.rtp_socket,
1914 remote_lchan->ts->abis_ip.rtp_socket);
1915 } else {
1916 /* directly connect TCH RTP streams to each other */
1917 ts = remote_lchan->ts;
1918 rc = rsl_ipacc_connect(lchan, ts->abis_ip.bound_ip,
1919 ts->abis_ip.bound_port,
1920 lchan->ts->abis_ip.conn_id,
1921 ts->abis_ip.rtp_payload2);
1922 if (rc < 0)
1923 return rc;
1924 ts = lchan->ts;
1925 rc = rsl_ipacc_connect(remote_lchan, ts->abis_ip.bound_ip,
1926 ts->abis_ip.bound_port,
1927 remote_lchan->ts->abis_ip.conn_id,
1928 ts->abis_ip.rtp_payload2);
1929 }
Harald Welte59b04682009-06-10 05:40:52 +08001930 break;
1931 case GSM_BTS_TYPE_BS11:
1932 trau_mux_map_lchan(lchan, remote_lchan);
1933 break;
1934 default:
1935 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
Harald Welte3c062072009-07-28 18:25:29 +02001936 rc = -EINVAL;
Harald Welte59b04682009-06-10 05:40:52 +08001937 break;
1938 }
1939
1940 return 0;
1941}
1942
Harald Welte03740842009-06-10 23:11:52 +08001943/* bridge channels of two transactions */
1944static int tch_bridge(struct gsm_network *net, u_int32_t *refs)
Harald Welte59b04682009-06-10 05:40:52 +08001945{
Harald Weltec2189a62009-07-23 18:56:43 +02001946 struct gsm_trans *trans1 = trans_find_by_callref(net, refs[0]);
1947 struct gsm_trans *trans2 = trans_find_by_callref(net, refs[1]);
Harald Welte59b04682009-06-10 05:40:52 +08001948
Harald Welte03740842009-06-10 23:11:52 +08001949 if (!trans1 || !trans2)
Harald Welte59b04682009-06-10 05:40:52 +08001950 return -EIO;
1951
Harald Welte03740842009-06-10 23:11:52 +08001952 if (!trans1->lchan || !trans2->lchan)
1953 return -EIO;
1954
1955 /* through-connect channel */
1956 return tch_map(trans1->lchan, trans2->lchan);
Harald Welte59b04682009-06-10 05:40:52 +08001957}
1958
Harald Welte03740842009-06-10 23:11:52 +08001959/* enable receive of channels to upqueue */
1960static int tch_recv(struct gsm_network *net, struct gsm_mncc *data, int enable)
1961{
1962 struct gsm_trans *trans;
Harald Welte59b04682009-06-10 05:40:52 +08001963
Harald Welte03740842009-06-10 23:11:52 +08001964 /* Find callref */
Harald Weltec2189a62009-07-23 18:56:43 +02001965 trans = trans_find_by_callref(net, data->callref);
Harald Welte03740842009-06-10 23:11:52 +08001966 if (!trans)
1967 return -EIO;
1968 if (!trans->lchan)
1969 return 0;
1970
1971 // todo IPACCESS
1972 if (enable)
1973 return trau_recv_lchan(trans->lchan, data->callref);
1974 return trau_mux_unmap(NULL, data->callref);
1975}
1976
1977/* send a frame to channel */
1978static int tch_frame(struct gsm_network *net, struct gsm_trau_frame *frame)
1979{
1980 struct gsm_trans *trans;
1981
1982 /* Find callref */
Harald Weltec2189a62009-07-23 18:56:43 +02001983 trans = trans_find_by_callref(net, frame->callref);
Harald Welte03740842009-06-10 23:11:52 +08001984 if (!trans)
1985 return -EIO;
1986 if (!trans->lchan)
1987 return 0;
1988 if (trans->lchan->type != GSM_LCHAN_TCH_F &&
1989 trans->lchan->type != GSM_LCHAN_TCH_H)
1990 return 0;
1991
1992 // todo IPACCESS
1993 return trau_send_lchan(trans->lchan,
1994 (struct decoded_trau_frame *)frame->data);
1995}
1996
1997
1998static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
1999{
2000 DEBUGP(DCC, "-> STATUS ENQ\n");
2001 return gsm48_cc_tx_status(trans, msg);
2002}
2003
2004static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg);
2005static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg);
2006
2007static void gsm48_cc_timeout(void *arg)
2008{
2009 struct gsm_trans *trans = arg;
2010 int disconnect = 0, release = 0;
Harald Weltebbc636a2009-06-11 14:23:20 +08002011 int mo_cause = GSM48_CC_CAUSE_RECOVERY_TIMER;
2012 int mo_location = GSM48_CAUSE_LOC_USER;
2013 int l4_cause = GSM48_CC_CAUSE_NORMAL_UNSPEC;
2014 int l4_location = GSM48_CAUSE_LOC_PRN_S_LU;
Harald Welte03740842009-06-10 23:11:52 +08002015 struct gsm_mncc mo_rel, l4_rel;
2016
2017 memset(&mo_rel, 0, sizeof(struct gsm_mncc));
2018 mo_rel.callref = trans->callref;
2019 memset(&l4_rel, 0, sizeof(struct gsm_mncc));
2020 l4_rel.callref = trans->callref;
2021
Harald Weltec2189a62009-07-23 18:56:43 +02002022 switch(trans->cc.Tcurrent) {
Harald Welte03740842009-06-10 23:11:52 +08002023 case 0x303:
2024 release = 1;
Harald Weltebbc636a2009-06-11 14:23:20 +08002025 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte03740842009-06-10 23:11:52 +08002026 break;
2027 case 0x310:
2028 disconnect = 1;
Harald Weltebbc636a2009-06-11 14:23:20 +08002029 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte03740842009-06-10 23:11:52 +08002030 break;
2031 case 0x313:
2032 disconnect = 1;
2033 /* unknown, did not find it in the specs */
2034 break;
2035 case 0x301:
2036 disconnect = 1;
Harald Weltebbc636a2009-06-11 14:23:20 +08002037 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte03740842009-06-10 23:11:52 +08002038 break;
2039 case 0x308:
Harald Weltec2189a62009-07-23 18:56:43 +02002040 if (!trans->cc.T308_second) {
Harald Welte03740842009-06-10 23:11:52 +08002041 /* restart T308 a second time */
Harald Weltec2189a62009-07-23 18:56:43 +02002042 gsm48_cc_tx_release(trans, &trans->cc.msg);
2043 trans->cc.T308_second = 1;
Harald Welte03740842009-06-10 23:11:52 +08002044 break; /* stay in release state */
2045 }
Harald Weltec2189a62009-07-23 18:56:43 +02002046 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08002047 return;
2048// release = 1;
2049// l4_cause = 14;
2050// break;
2051 case 0x306:
2052 release = 1;
Harald Weltec2189a62009-07-23 18:56:43 +02002053 mo_cause = trans->cc.msg.cause.value;
2054 mo_location = trans->cc.msg.cause.location;
Harald Welte03740842009-06-10 23:11:52 +08002055 break;
2056 case 0x323:
2057 disconnect = 1;
2058 break;
2059 default:
2060 release = 1;
2061 }
2062
2063 if (release && trans->callref) {
2064 /* process release towards layer 4 */
Harald Welte0abe5a62009-07-23 19:06:52 +02002065 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Harald Welte03740842009-06-10 23:11:52 +08002066 l4_location, l4_cause);
2067 trans->callref = 0;
2068 }
2069
2070 if (disconnect && trans->callref) {
2071 /* process disconnect towards layer 4 */
2072 mncc_set_cause(&l4_rel, l4_location, l4_cause);
Harald Welte0abe5a62009-07-23 19:06:52 +02002073 mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &l4_rel);
Harald Welte03740842009-06-10 23:11:52 +08002074 }
2075
2076 /* process disconnect towards mobile station */
2077 if (disconnect || release) {
2078 mncc_set_cause(&mo_rel, mo_location, mo_cause);
Harald Weltec2189a62009-07-23 18:56:43 +02002079 mo_rel.cause.diag[0] = ((trans->cc.Tcurrent & 0xf00) >> 8) + '0';
2080 mo_rel.cause.diag[1] = ((trans->cc.Tcurrent & 0x0f0) >> 4) + '0';
2081 mo_rel.cause.diag[2] = (trans->cc.Tcurrent & 0x00f) + '0';
Harald Welte03740842009-06-10 23:11:52 +08002082 mo_rel.cause.diag_len = 3;
2083
2084 if (disconnect)
2085 gsm48_cc_tx_disconnect(trans, &mo_rel);
2086 if (release)
2087 gsm48_cc_tx_release(trans, &mo_rel);
2088 }
2089
2090}
2091
2092static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
2093 int sec, int micro)
2094{
2095 DEBUGP(DCC, "starting timer T%x with %d seconds\n", current, sec);
Harald Weltec2189a62009-07-23 18:56:43 +02002096 trans->cc.timer.cb = gsm48_cc_timeout;
2097 trans->cc.timer.data = trans;
2098 bsc_schedule_timer(&trans->cc.timer, sec, micro);
2099 trans->cc.Tcurrent = current;
Harald Welte03740842009-06-10 23:11:52 +08002100}
2101
2102static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
2103{
2104 struct gsm48_hdr *gh = msgb_l3(msg);
2105 u_int8_t msg_type = gh->msg_type & 0xbf;
2106 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2107 struct tlv_parsed tp;
2108 struct gsm_mncc setup;
2109
2110 memset(&setup, 0, sizeof(struct gsm_mncc));
2111 setup.callref = trans->callref;
2112 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2113 /* emergency setup is identified by msg_type */
2114 if (msg_type == GSM48_MT_CC_EMERG_SETUP)
2115 setup.emergency = 1;
2116
2117 /* use subscriber as calling party number */
2118 if (trans->subscr) {
2119 setup.fields |= MNCC_F_CALLING;
2120 strncpy(setup.calling.number, trans->subscr->extension,
2121 sizeof(setup.calling.number)-1);
Andreas Eversberg9eaa5da2009-06-15 23:22:09 +02002122 strncpy(setup.imsi, trans->subscr->imsi,
2123 sizeof(setup.imsi)-1);
Harald Welte03740842009-06-10 23:11:52 +08002124 }
2125 /* bearer capability */
2126 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2127 setup.fields |= MNCC_F_BEARER_CAP;
2128 decode_bearer_cap(&setup.bearer_cap,
2129 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2130 }
2131 /* facility */
2132 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2133 setup.fields |= MNCC_F_FACILITY;
2134 decode_facility(&setup.facility,
2135 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2136 }
2137 /* called party bcd number */
2138 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
2139 setup.fields |= MNCC_F_CALLED;
2140 decode_called(&setup.called,
2141 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
2142 }
2143 /* user-user */
2144 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2145 setup.fields |= MNCC_F_USERUSER;
2146 decode_useruser(&setup.useruser,
2147 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2148 }
2149 /* ss-version */
2150 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2151 setup.fields |= MNCC_F_SSVERSION;
2152 decode_ssversion(&setup.ssversion,
2153 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2154 }
2155 /* CLIR suppression */
2156 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
2157 setup.clir.sup = 1;
2158 /* CLIR invocation */
2159 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
2160 setup.clir.inv = 1;
2161 /* cc cap */
2162 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
2163 setup.fields |= MNCC_F_CCCAP;
2164 decode_cccap(&setup.cccap,
2165 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
2166 }
2167
Harald Welte03740842009-06-10 23:11:52 +08002168 new_cc_state(trans, GSM_CSTATE_INITIATED);
2169
2170 /* indicate setup to MNCC */
Harald Welte0abe5a62009-07-23 19:06:52 +02002171 mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_IND, &setup);
Harald Welte03740842009-06-10 23:11:52 +08002172
Harald Welteca745e22009-07-29 12:10:35 +02002173 /* MNCC code will modify the channel asynchronously, we should
2174 * ipaccess-bind only after the modification has been made to the
2175 * lchan->tch_mode */
Harald Welte03740842009-06-10 23:11:52 +08002176 return 0;
2177}
2178
2179static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
Harald Welte59b04682009-06-10 05:40:52 +08002180{
2181 struct msgb *msg = gsm48_msgb_alloc();
2182 struct gsm48_hdr *gh;
Harald Welte03740842009-06-10 23:11:52 +08002183 struct gsm_mncc *setup = arg;
Harald Welte165fc332009-07-23 21:36:44 +02002184 int rc, trans_id;
Harald Welte59b04682009-06-10 05:40:52 +08002185
2186 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2187
Harald Welte03740842009-06-10 23:11:52 +08002188 /* transaction id must not be assigned */
2189 if (trans->transaction_id != 0xff) { /* unasssigned */
2190 DEBUGP(DCC, "TX Setup with assigned transaction. "
2191 "This is not allowed!\n");
2192 /* Temporarily out of order */
Harald Welte0abe5a62009-07-23 19:06:52 +02002193 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08002194 GSM48_CAUSE_LOC_PRN_S_LU,
2195 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte03740842009-06-10 23:11:52 +08002196 trans->callref = 0;
Harald Weltec2189a62009-07-23 18:56:43 +02002197 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08002198 return rc;
2199 }
2200
2201 /* Get free transaction_id */
Harald Welte165fc332009-07-23 21:36:44 +02002202 trans_id = trans_assign_trans_id(trans->subscr, GSM48_PDISC_CC, 0);
2203 if (trans_id < 0) {
Harald Welte03740842009-06-10 23:11:52 +08002204 /* no free transaction ID */
Harald Welte0abe5a62009-07-23 19:06:52 +02002205 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08002206 GSM48_CAUSE_LOC_PRN_S_LU,
2207 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte03740842009-06-10 23:11:52 +08002208 trans->callref = 0;
Harald Weltec2189a62009-07-23 18:56:43 +02002209 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08002210 return rc;
2211 }
Harald Welte165fc332009-07-23 21:36:44 +02002212 trans->transaction_id = trans_id;
Harald Welte59b04682009-06-10 05:40:52 +08002213
Harald Welte59b04682009-06-10 05:40:52 +08002214 gh->msg_type = GSM48_MT_CC_SETUP;
2215
Harald Welte03740842009-06-10 23:11:52 +08002216 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
Harald Welte59b04682009-06-10 05:40:52 +08002217
Harald Welte03740842009-06-10 23:11:52 +08002218 /* bearer capability */
2219 if (setup->fields & MNCC_F_BEARER_CAP)
2220 encode_bearer_cap(msg, 0, &setup->bearer_cap);
2221 /* facility */
2222 if (setup->fields & MNCC_F_FACILITY)
2223 encode_facility(msg, 0, &setup->facility);
2224 /* progress */
2225 if (setup->fields & MNCC_F_PROGRESS)
2226 encode_progress(msg, 0, &setup->progress);
2227 /* calling party BCD number */
2228 if (setup->fields & MNCC_F_CALLING)
2229 encode_calling(msg, &setup->calling);
2230 /* called party BCD number */
2231 if (setup->fields & MNCC_F_CALLED)
2232 encode_called(msg, &setup->called);
2233 /* user-user */
2234 if (setup->fields & MNCC_F_USERUSER)
2235 encode_useruser(msg, 0, &setup->useruser);
2236 /* redirecting party BCD number */
2237 if (setup->fields & MNCC_F_REDIRECTING)
2238 encode_redirecting(msg, &setup->redirecting);
2239 /* signal */
2240 if (setup->fields & MNCC_F_SIGNAL)
2241 encode_signal(msg, setup->signal);
2242
2243 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
Harald Welte59b04682009-06-10 05:40:52 +08002244
Harald Welte36fe2e82009-07-23 21:13:03 +02002245 return gsm48_sendmsg(msg, trans);
Harald Welte59b04682009-06-10 05:40:52 +08002246}
2247
Harald Welte03740842009-06-10 23:11:52 +08002248static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
2249{
2250 struct gsm48_hdr *gh = msgb_l3(msg);
2251 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2252 struct tlv_parsed tp;
2253 struct gsm_mncc call_conf;
2254
2255 gsm48_stop_cc_timer(trans);
2256 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
2257
2258 memset(&call_conf, 0, sizeof(struct gsm_mncc));
2259 call_conf.callref = trans->callref;
2260 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2261#if 0
2262 /* repeat */
2263 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
2264 call_conf.repeat = 1;
2265 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
2266 call_conf.repeat = 2;
2267#endif
2268 /* bearer capability */
2269 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2270 call_conf.fields |= MNCC_F_BEARER_CAP;
2271 decode_bearer_cap(&call_conf.bearer_cap,
2272 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2273 }
2274 /* cause */
2275 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2276 call_conf.fields |= MNCC_F_CAUSE;
2277 decode_cause(&call_conf.cause,
2278 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2279 }
2280 /* cc cap */
2281 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
2282 call_conf.fields |= MNCC_F_CCCAP;
2283 decode_cccap(&call_conf.cccap,
2284 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
2285 }
2286
2287 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
2288
Harald Welte0abe5a62009-07-23 19:06:52 +02002289 return mncc_recvmsg(trans->subscr->net, trans, MNCC_CALL_CONF_IND,
2290 &call_conf);
Harald Welte03740842009-06-10 23:11:52 +08002291}
2292
2293static int gsm48_cc_tx_call_proc(struct gsm_trans *trans, void *arg)
2294{
2295 struct gsm_mncc *proceeding = arg;
2296 struct msgb *msg = gsm48_msgb_alloc();
2297 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2298
Harald Welte03740842009-06-10 23:11:52 +08002299 gh->msg_type = GSM48_MT_CC_CALL_PROC;
2300
2301 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
2302
2303 /* bearer capability */
2304 if (proceeding->fields & MNCC_F_BEARER_CAP)
2305 encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
2306 /* facility */
2307 if (proceeding->fields & MNCC_F_FACILITY)
2308 encode_facility(msg, 0, &proceeding->facility);
2309 /* progress */
2310 if (proceeding->fields & MNCC_F_PROGRESS)
2311 encode_progress(msg, 0, &proceeding->progress);
2312
Harald Welte36fe2e82009-07-23 21:13:03 +02002313 return gsm48_sendmsg(msg, trans);
Harald Welte03740842009-06-10 23:11:52 +08002314}
2315
2316static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
2317{
2318 struct gsm48_hdr *gh = msgb_l3(msg);
2319 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2320 struct tlv_parsed tp;
2321 struct gsm_mncc alerting;
2322
2323 gsm48_stop_cc_timer(trans);
2324 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
2325
2326 memset(&alerting, 0, sizeof(struct gsm_mncc));
2327 alerting.callref = trans->callref;
2328 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2329 /* facility */
2330 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2331 alerting.fields |= MNCC_F_FACILITY;
2332 decode_facility(&alerting.facility,
2333 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2334 }
2335
2336 /* progress */
2337 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
2338 alerting.fields |= MNCC_F_PROGRESS;
2339 decode_progress(&alerting.progress,
2340 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
2341 }
2342 /* ss-version */
2343 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2344 alerting.fields |= MNCC_F_SSVERSION;
2345 decode_ssversion(&alerting.ssversion,
2346 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2347 }
2348
2349 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
2350
Harald Welte0abe5a62009-07-23 19:06:52 +02002351 return mncc_recvmsg(trans->subscr->net, trans, MNCC_ALERT_IND,
2352 &alerting);
Harald Welte03740842009-06-10 23:11:52 +08002353}
2354
2355static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
2356{
2357 struct gsm_mncc *alerting = arg;
2358 struct msgb *msg = gsm48_msgb_alloc();
2359 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2360
Harald Welte03740842009-06-10 23:11:52 +08002361 gh->msg_type = GSM48_MT_CC_ALERTING;
2362
2363 /* facility */
2364 if (alerting->fields & MNCC_F_FACILITY)
2365 encode_facility(msg, 0, &alerting->facility);
2366 /* progress */
2367 if (alerting->fields & MNCC_F_PROGRESS)
2368 encode_progress(msg, 0, &alerting->progress);
2369 /* user-user */
2370 if (alerting->fields & MNCC_F_USERUSER)
2371 encode_useruser(msg, 0, &alerting->useruser);
2372
2373 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
2374
Harald Welte36fe2e82009-07-23 21:13:03 +02002375 return gsm48_sendmsg(msg, trans);
Harald Welte03740842009-06-10 23:11:52 +08002376}
2377
2378static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
2379{
2380 struct gsm_mncc *progress = arg;
2381 struct msgb *msg = gsm48_msgb_alloc();
2382 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2383
Harald Welte03740842009-06-10 23:11:52 +08002384 gh->msg_type = GSM48_MT_CC_PROGRESS;
2385
2386 /* progress */
2387 encode_progress(msg, 1, &progress->progress);
2388 /* user-user */
2389 if (progress->fields & MNCC_F_USERUSER)
2390 encode_useruser(msg, 0, &progress->useruser);
2391
Harald Welte36fe2e82009-07-23 21:13:03 +02002392 return gsm48_sendmsg(msg, trans);
Harald Welte03740842009-06-10 23:11:52 +08002393}
2394
2395static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
2396{
2397 struct gsm_mncc *connect = arg;
2398 struct msgb *msg = gsm48_msgb_alloc();
2399 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2400
Harald Welte03740842009-06-10 23:11:52 +08002401 gh->msg_type = GSM48_MT_CC_CONNECT;
2402
2403 gsm48_stop_cc_timer(trans);
2404 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
2405
2406 /* facility */
2407 if (connect->fields & MNCC_F_FACILITY)
2408 encode_facility(msg, 0, &connect->facility);
2409 /* progress */
2410 if (connect->fields & MNCC_F_PROGRESS)
2411 encode_progress(msg, 0, &connect->progress);
2412 /* connected number */
2413 if (connect->fields & MNCC_F_CONNECTED)
2414 encode_connected(msg, &connect->connected);
2415 /* user-user */
2416 if (connect->fields & MNCC_F_USERUSER)
2417 encode_useruser(msg, 0, &connect->useruser);
2418
2419 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
2420
Harald Welte36fe2e82009-07-23 21:13:03 +02002421 return gsm48_sendmsg(msg, trans);
Harald Welte03740842009-06-10 23:11:52 +08002422}
2423
2424static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
2425{
2426 struct gsm48_hdr *gh = msgb_l3(msg);
2427 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2428 struct tlv_parsed tp;
2429 struct gsm_mncc connect;
2430
2431 gsm48_stop_cc_timer(trans);
2432
2433 memset(&connect, 0, sizeof(struct gsm_mncc));
2434 connect.callref = trans->callref;
2435 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2436 /* use subscriber as connected party number */
2437 if (trans->subscr) {
2438 connect.fields |= MNCC_F_CONNECTED;
2439 strncpy(connect.connected.number, trans->subscr->extension,
2440 sizeof(connect.connected.number)-1);
Andreas Eversberg9eaa5da2009-06-15 23:22:09 +02002441 strncpy(connect.imsi, trans->subscr->imsi,
2442 sizeof(connect.imsi)-1);
Harald Welte03740842009-06-10 23:11:52 +08002443 }
2444 /* facility */
2445 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2446 connect.fields |= MNCC_F_FACILITY;
2447 decode_facility(&connect.facility,
2448 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2449 }
2450 /* user-user */
2451 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2452 connect.fields |= MNCC_F_USERUSER;
2453 decode_useruser(&connect.useruser,
2454 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2455 }
2456 /* ss-version */
2457 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2458 connect.fields |= MNCC_F_SSVERSION;
2459 decode_ssversion(&connect.ssversion,
2460 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2461 }
2462
2463 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
2464
Harald Welte0abe5a62009-07-23 19:06:52 +02002465 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_CNF, &connect);
Harald Welte03740842009-06-10 23:11:52 +08002466}
2467
2468
2469static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
2470{
2471 struct gsm_mncc connect_ack;
2472
2473 gsm48_stop_cc_timer(trans);
2474
2475 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2476
2477 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
2478 connect_ack.callref = trans->callref;
Harald Welte0abe5a62009-07-23 19:06:52 +02002479 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_COMPL_IND,
Harald Welte03740842009-06-10 23:11:52 +08002480 &connect_ack);
2481}
2482
2483static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
2484{
2485 struct msgb *msg = gsm48_msgb_alloc();
2486 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2487
Harald Welte03740842009-06-10 23:11:52 +08002488 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
2489
2490 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2491
Harald Welte36fe2e82009-07-23 21:13:03 +02002492 return gsm48_sendmsg(msg, trans);
Harald Welte03740842009-06-10 23:11:52 +08002493}
2494
2495static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
2496{
2497 struct gsm48_hdr *gh = msgb_l3(msg);
2498 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2499 struct tlv_parsed tp;
2500 struct gsm_mncc disc;
2501
2502 gsm48_stop_cc_timer(trans);
2503
2504 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
2505
2506 memset(&disc, 0, sizeof(struct gsm_mncc));
2507 disc.callref = trans->callref;
2508 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
2509 /* cause */
2510 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2511 disc.fields |= MNCC_F_CAUSE;
2512 decode_cause(&disc.cause,
2513 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2514 }
2515 /* facility */
2516 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2517 disc.fields |= MNCC_F_FACILITY;
2518 decode_facility(&disc.facility,
2519 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2520 }
2521 /* user-user */
2522 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2523 disc.fields |= MNCC_F_USERUSER;
2524 decode_useruser(&disc.useruser,
2525 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2526 }
2527 /* ss-version */
2528 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2529 disc.fields |= MNCC_F_SSVERSION;
2530 decode_ssversion(&disc.ssversion,
2531 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2532 }
2533
Harald Welte0abe5a62009-07-23 19:06:52 +02002534 return mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &disc);
Harald Welte03740842009-06-10 23:11:52 +08002535
2536}
2537
Harald Weltebbc636a2009-06-11 14:23:20 +08002538static struct gsm_mncc_cause default_cause = {
2539 .location = GSM48_CAUSE_LOC_PRN_S_LU,
2540 .coding = 0,
2541 .rec = 0,
2542 .rec_val = 0,
2543 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
2544 .diag_len = 0,
2545 .diag = { 0 },
2546};
Harald Welte03740842009-06-10 23:11:52 +08002547
2548static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
2549{
2550 struct gsm_mncc *disc = arg;
2551 struct msgb *msg = gsm48_msgb_alloc();
2552 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2553
Harald Welte03740842009-06-10 23:11:52 +08002554 gh->msg_type = GSM48_MT_CC_DISCONNECT;
2555
2556 gsm48_stop_cc_timer(trans);
2557 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
2558
2559 /* cause */
2560 if (disc->fields & MNCC_F_CAUSE)
2561 encode_cause(msg, 1, &disc->cause);
2562 else
2563 encode_cause(msg, 1, &default_cause);
2564
2565 /* facility */
2566 if (disc->fields & MNCC_F_FACILITY)
2567 encode_facility(msg, 0, &disc->facility);
2568 /* progress */
2569 if (disc->fields & MNCC_F_PROGRESS)
2570 encode_progress(msg, 0, &disc->progress);
2571 /* user-user */
2572 if (disc->fields & MNCC_F_USERUSER)
2573 encode_useruser(msg, 0, &disc->useruser);
2574
2575 /* store disconnect cause for T306 expiry */
Harald Weltec2189a62009-07-23 18:56:43 +02002576 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
Harald Welte03740842009-06-10 23:11:52 +08002577
2578 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
2579
Harald Welte36fe2e82009-07-23 21:13:03 +02002580 return gsm48_sendmsg(msg, trans);
Harald Welte03740842009-06-10 23:11:52 +08002581}
2582
2583static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
2584{
2585 struct gsm48_hdr *gh = msgb_l3(msg);
2586 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2587 struct tlv_parsed tp;
2588 struct gsm_mncc rel;
2589 int rc;
2590
2591 gsm48_stop_cc_timer(trans);
2592
2593 memset(&rel, 0, sizeof(struct gsm_mncc));
2594 rel.callref = trans->callref;
2595 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2596 /* cause */
2597 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2598 rel.fields |= MNCC_F_CAUSE;
2599 decode_cause(&rel.cause,
2600 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2601 }
2602 /* facility */
2603 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2604 rel.fields |= MNCC_F_FACILITY;
2605 decode_facility(&rel.facility,
2606 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2607 }
2608 /* user-user */
2609 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2610 rel.fields |= MNCC_F_USERUSER;
2611 decode_useruser(&rel.useruser,
2612 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2613 }
2614 /* ss-version */
2615 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2616 rel.fields |= MNCC_F_SSVERSION;
2617 decode_ssversion(&rel.ssversion,
2618 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2619 }
2620
Harald Weltec2189a62009-07-23 18:56:43 +02002621 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
Harald Welte03740842009-06-10 23:11:52 +08002622 /* release collision 5.4.5 */
Harald Welte0abe5a62009-07-23 19:06:52 +02002623 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_CNF, &rel);
Harald Welte03740842009-06-10 23:11:52 +08002624 } else {
Harald Welte0abe5a62009-07-23 19:06:52 +02002625 rc = gsm48_tx_simple(msg->lchan,
Harald Welte4861c822009-07-23 21:21:14 +02002626 GSM48_PDISC_CC | (trans->transaction_id << 4),
Harald Welte0abe5a62009-07-23 19:06:52 +02002627 GSM48_MT_CC_RELEASE_COMPL);
2628 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_IND, &rel);
Harald Welte03740842009-06-10 23:11:52 +08002629 }
2630
2631 new_cc_state(trans, GSM_CSTATE_NULL);
2632
2633 trans->callref = 0;
Harald Weltec2189a62009-07-23 18:56:43 +02002634 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08002635
2636 return rc;
2637}
2638
2639static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
2640{
2641 struct gsm_mncc *rel = arg;
2642 struct msgb *msg = gsm48_msgb_alloc();
2643 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2644
Harald Welte03740842009-06-10 23:11:52 +08002645 gh->msg_type = GSM48_MT_CC_RELEASE;
2646
2647 trans->callref = 0;
2648
2649 gsm48_stop_cc_timer(trans);
2650 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
2651
2652 /* cause */
2653 if (rel->fields & MNCC_F_CAUSE)
2654 encode_cause(msg, 0, &rel->cause);
2655 /* facility */
2656 if (rel->fields & MNCC_F_FACILITY)
2657 encode_facility(msg, 0, &rel->facility);
2658 /* user-user */
2659 if (rel->fields & MNCC_F_USERUSER)
2660 encode_useruser(msg, 0, &rel->useruser);
2661
Harald Weltec2189a62009-07-23 18:56:43 +02002662 trans->cc.T308_second = 0;
2663 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
Harald Welte03740842009-06-10 23:11:52 +08002664
Harald Weltec2189a62009-07-23 18:56:43 +02002665 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
Harald Welte03740842009-06-10 23:11:52 +08002666 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
2667
Harald Welte36fe2e82009-07-23 21:13:03 +02002668 return gsm48_sendmsg(msg, trans);
Harald Welte03740842009-06-10 23:11:52 +08002669}
2670
2671static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
2672{
2673 struct gsm48_hdr *gh = msgb_l3(msg);
2674 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2675 struct tlv_parsed tp;
2676 struct gsm_mncc rel;
2677 int rc = 0;
2678
2679 gsm48_stop_cc_timer(trans);
2680
2681 memset(&rel, 0, sizeof(struct gsm_mncc));
2682 rel.callref = trans->callref;
2683 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2684 /* cause */
2685 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2686 rel.fields |= MNCC_F_CAUSE;
2687 decode_cause(&rel.cause,
2688 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2689 }
2690 /* facility */
2691 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2692 rel.fields |= MNCC_F_FACILITY;
2693 decode_facility(&rel.facility,
2694 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2695 }
2696 /* user-user */
2697 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2698 rel.fields |= MNCC_F_USERUSER;
2699 decode_useruser(&rel.useruser,
2700 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2701 }
2702 /* ss-version */
2703 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2704 rel.fields |= MNCC_F_SSVERSION;
2705 decode_ssversion(&rel.ssversion,
2706 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2707 }
2708
2709 if (trans->callref) {
Harald Weltec2189a62009-07-23 18:56:43 +02002710 switch (trans->cc.state) {
Harald Welte03740842009-06-10 23:11:52 +08002711 case GSM_CSTATE_CALL_PRESENT:
Harald Welte0abe5a62009-07-23 19:06:52 +02002712 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte03740842009-06-10 23:11:52 +08002713 MNCC_REJ_IND, &rel);
2714 break;
2715 case GSM_CSTATE_RELEASE_REQ:
Harald Welte0abe5a62009-07-23 19:06:52 +02002716 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte03740842009-06-10 23:11:52 +08002717 MNCC_REL_CNF, &rel);
2718 break;
2719 default:
Harald Welte0abe5a62009-07-23 19:06:52 +02002720 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte03740842009-06-10 23:11:52 +08002721 MNCC_REL_IND, &rel);
2722 }
2723 }
2724
2725 trans->callref = 0;
Harald Weltec2189a62009-07-23 18:56:43 +02002726 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08002727
2728 return rc;
2729}
2730
2731static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
2732{
2733 struct gsm_mncc *rel = arg;
2734 struct msgb *msg = gsm48_msgb_alloc();
2735 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2736
Harald Welte03740842009-06-10 23:11:52 +08002737 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
2738
2739 trans->callref = 0;
2740
2741 gsm48_stop_cc_timer(trans);
2742
2743 /* cause */
2744 if (rel->fields & MNCC_F_CAUSE)
2745 encode_cause(msg, 0, &rel->cause);
2746 /* facility */
2747 if (rel->fields & MNCC_F_FACILITY)
2748 encode_facility(msg, 0, &rel->facility);
2749 /* user-user */
2750 if (rel->fields & MNCC_F_USERUSER)
2751 encode_useruser(msg, 0, &rel->useruser);
2752
Harald Weltec2189a62009-07-23 18:56:43 +02002753 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08002754
Harald Welte36fe2e82009-07-23 21:13:03 +02002755 return gsm48_sendmsg(msg, trans);
Harald Welte03740842009-06-10 23:11:52 +08002756}
2757
2758static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
2759{
2760 struct gsm48_hdr *gh = msgb_l3(msg);
2761 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2762 struct tlv_parsed tp;
2763 struct gsm_mncc fac;
2764
2765 memset(&fac, 0, sizeof(struct gsm_mncc));
2766 fac.callref = trans->callref;
2767 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
2768 /* facility */
2769 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2770 fac.fields |= MNCC_F_FACILITY;
2771 decode_facility(&fac.facility,
2772 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2773 }
2774 /* ss-version */
2775 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2776 fac.fields |= MNCC_F_SSVERSION;
2777 decode_ssversion(&fac.ssversion,
2778 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2779 }
2780
Harald Welte0abe5a62009-07-23 19:06:52 +02002781 return mncc_recvmsg(trans->subscr->net, trans, MNCC_FACILITY_IND, &fac);
Harald Welte03740842009-06-10 23:11:52 +08002782}
2783
2784static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
2785{
2786 struct gsm_mncc *fac = arg;
2787 struct msgb *msg = gsm48_msgb_alloc();
2788 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2789
Harald Welte03740842009-06-10 23:11:52 +08002790 gh->msg_type = GSM48_MT_CC_FACILITY;
2791
2792 /* facility */
2793 encode_facility(msg, 1, &fac->facility);
2794
Harald Welte36fe2e82009-07-23 21:13:03 +02002795 return gsm48_sendmsg(msg, trans);
Harald Welte03740842009-06-10 23:11:52 +08002796}
2797
2798static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
2799{
2800 struct gsm_mncc hold;
2801
2802 memset(&hold, 0, sizeof(struct gsm_mncc));
2803 hold.callref = trans->callref;
Harald Welte0abe5a62009-07-23 19:06:52 +02002804 return mncc_recvmsg(trans->subscr->net, trans, MNCC_HOLD_IND, &hold);
Harald Welte03740842009-06-10 23:11:52 +08002805}
2806
2807static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
2808{
2809 struct msgb *msg = gsm48_msgb_alloc();
2810 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2811
Harald Welte03740842009-06-10 23:11:52 +08002812 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
2813
Harald Welte36fe2e82009-07-23 21:13:03 +02002814 return gsm48_sendmsg(msg, trans);
Harald Welte03740842009-06-10 23:11:52 +08002815}
2816
2817static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
2818{
2819 struct gsm_mncc *hold_rej = arg;
2820 struct msgb *msg = gsm48_msgb_alloc();
2821 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2822
Harald Welte03740842009-06-10 23:11:52 +08002823 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
2824
2825 /* cause */
2826 if (hold_rej->fields & MNCC_F_CAUSE)
2827 encode_cause(msg, 1, &hold_rej->cause);
2828 else
2829 encode_cause(msg, 1, &default_cause);
2830
Harald Welte36fe2e82009-07-23 21:13:03 +02002831 return gsm48_sendmsg(msg, trans);
Harald Welte03740842009-06-10 23:11:52 +08002832}
2833
2834static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
2835{
2836 struct gsm_mncc retrieve;
2837
2838 memset(&retrieve, 0, sizeof(struct gsm_mncc));
2839 retrieve.callref = trans->callref;
Harald Welte0abe5a62009-07-23 19:06:52 +02002840 return mncc_recvmsg(trans->subscr->net, trans, MNCC_RETRIEVE_IND,
2841 &retrieve);
Harald Welte03740842009-06-10 23:11:52 +08002842}
2843
2844static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
2845{
2846 struct msgb *msg = gsm48_msgb_alloc();
2847 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2848
Harald Welte03740842009-06-10 23:11:52 +08002849 gh->msg_type = GSM48_MT_CC_RETR_ACK;
2850
Harald Welte36fe2e82009-07-23 21:13:03 +02002851 return gsm48_sendmsg(msg, trans);
Harald Welte03740842009-06-10 23:11:52 +08002852}
2853
2854static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
2855{
2856 struct gsm_mncc *retrieve_rej = arg;
2857 struct msgb *msg = gsm48_msgb_alloc();
2858 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2859
Harald Welte03740842009-06-10 23:11:52 +08002860 gh->msg_type = GSM48_MT_CC_RETR_REJ;
2861
2862 /* cause */
2863 if (retrieve_rej->fields & MNCC_F_CAUSE)
2864 encode_cause(msg, 1, &retrieve_rej->cause);
2865 else
2866 encode_cause(msg, 1, &default_cause);
2867
Harald Welte36fe2e82009-07-23 21:13:03 +02002868 return gsm48_sendmsg(msg, trans);
Harald Welte03740842009-06-10 23:11:52 +08002869}
2870
2871static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
2872{
2873 struct gsm48_hdr *gh = msgb_l3(msg);
2874 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2875 struct tlv_parsed tp;
2876 struct gsm_mncc dtmf;
2877
2878 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2879 dtmf.callref = trans->callref;
2880 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2881 /* keypad facility */
2882 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
2883 dtmf.fields |= MNCC_F_KEYPAD;
2884 decode_keypad(&dtmf.keypad,
2885 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
2886 }
2887
Harald Welte0abe5a62009-07-23 19:06:52 +02002888 return mncc_recvmsg(trans->subscr->net, trans, MNCC_START_DTMF_IND, &dtmf);
Harald Welte03740842009-06-10 23:11:52 +08002889}
2890
2891static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
2892{
2893 struct gsm_mncc *dtmf = arg;
2894 struct msgb *msg = gsm48_msgb_alloc();
2895 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2896
Harald Welte03740842009-06-10 23:11:52 +08002897 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
2898
2899 /* keypad */
2900 if (dtmf->fields & MNCC_F_KEYPAD)
2901 encode_keypad(msg, dtmf->keypad);
2902
Harald Welte36fe2e82009-07-23 21:13:03 +02002903 return gsm48_sendmsg(msg, trans);
Harald Welte03740842009-06-10 23:11:52 +08002904}
2905
2906static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
2907{
2908 struct gsm_mncc *dtmf = arg;
2909 struct msgb *msg = gsm48_msgb_alloc();
2910 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2911
Harald Welte03740842009-06-10 23:11:52 +08002912 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
2913
2914 /* cause */
2915 if (dtmf->fields & MNCC_F_CAUSE)
2916 encode_cause(msg, 1, &dtmf->cause);
2917 else
2918 encode_cause(msg, 1, &default_cause);
2919
Harald Welte36fe2e82009-07-23 21:13:03 +02002920 return gsm48_sendmsg(msg, trans);
Harald Welte03740842009-06-10 23:11:52 +08002921}
2922
2923static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
2924{
2925 struct msgb *msg = gsm48_msgb_alloc();
2926 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2927
Harald Welte03740842009-06-10 23:11:52 +08002928 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
2929
Harald Welte36fe2e82009-07-23 21:13:03 +02002930 return gsm48_sendmsg(msg, trans);
Harald Welte03740842009-06-10 23:11:52 +08002931}
2932
2933static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
2934{
2935 struct gsm_mncc dtmf;
2936
2937 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2938 dtmf.callref = trans->callref;
2939
Harald Welte0abe5a62009-07-23 19:06:52 +02002940 return mncc_recvmsg(trans->subscr->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
Harald Welte03740842009-06-10 23:11:52 +08002941}
2942
2943static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
2944{
2945 struct gsm48_hdr *gh = msgb_l3(msg);
2946 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2947 struct tlv_parsed tp;
2948 struct gsm_mncc modify;
2949
2950 memset(&modify, 0, sizeof(struct gsm_mncc));
2951 modify.callref = trans->callref;
2952 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
2953 /* bearer capability */
2954 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2955 modify.fields |= MNCC_F_BEARER_CAP;
2956 decode_bearer_cap(&modify.bearer_cap,
2957 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2958 }
2959
2960 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
2961
Harald Welte0abe5a62009-07-23 19:06:52 +02002962 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_IND, &modify);
Harald Welte03740842009-06-10 23:11:52 +08002963}
2964
2965static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
2966{
2967 struct gsm_mncc *modify = arg;
2968 struct msgb *msg = gsm48_msgb_alloc();
2969 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2970
Harald Welte03740842009-06-10 23:11:52 +08002971 gh->msg_type = GSM48_MT_CC_MODIFY;
2972
2973 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
2974
2975 /* bearer capability */
2976 encode_bearer_cap(msg, 1, &modify->bearer_cap);
2977
2978 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
2979
Harald Welte36fe2e82009-07-23 21:13:03 +02002980 return gsm48_sendmsg(msg, trans);
Harald Welte03740842009-06-10 23:11:52 +08002981}
2982
2983static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
2984{
2985 struct gsm48_hdr *gh = msgb_l3(msg);
2986 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2987 struct tlv_parsed tp;
2988 struct gsm_mncc modify;
2989
2990 gsm48_stop_cc_timer(trans);
2991
2992 memset(&modify, 0, sizeof(struct gsm_mncc));
2993 modify.callref = trans->callref;
2994 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
2995 /* bearer capability */
2996 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2997 modify.fields |= MNCC_F_BEARER_CAP;
2998 decode_bearer_cap(&modify.bearer_cap,
2999 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
3000 }
3001
3002 new_cc_state(trans, GSM_CSTATE_ACTIVE);
3003
Harald Welte0abe5a62009-07-23 19:06:52 +02003004 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_CNF, &modify);
Harald Welte03740842009-06-10 23:11:52 +08003005}
3006
3007static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
3008{
3009 struct gsm_mncc *modify = arg;
3010 struct msgb *msg = gsm48_msgb_alloc();
3011 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3012
Harald Welte03740842009-06-10 23:11:52 +08003013 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
3014
3015 /* bearer capability */
3016 encode_bearer_cap(msg, 1, &modify->bearer_cap);
3017
3018 new_cc_state(trans, GSM_CSTATE_ACTIVE);
3019
Harald Welte36fe2e82009-07-23 21:13:03 +02003020 return gsm48_sendmsg(msg, trans);
Harald Welte03740842009-06-10 23:11:52 +08003021}
3022
3023static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
3024{
3025 struct gsm48_hdr *gh = msgb_l3(msg);
3026 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
3027 struct tlv_parsed tp;
3028 struct gsm_mncc modify;
3029
3030 gsm48_stop_cc_timer(trans);
3031
3032 memset(&modify, 0, sizeof(struct gsm_mncc));
3033 modify.callref = trans->callref;
3034 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
3035 /* bearer capability */
3036 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
3037 modify.fields |= GSM48_IE_BEARER_CAP;
3038 decode_bearer_cap(&modify.bearer_cap,
3039 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
3040 }
3041 /* cause */
3042 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
3043 modify.fields |= MNCC_F_CAUSE;
3044 decode_cause(&modify.cause,
3045 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
3046 }
3047
3048 new_cc_state(trans, GSM_CSTATE_ACTIVE);
3049
Harald Welte0abe5a62009-07-23 19:06:52 +02003050 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_REJ, &modify);
Harald Welte03740842009-06-10 23:11:52 +08003051}
3052
3053static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
3054{
3055 struct gsm_mncc *modify = arg;
3056 struct msgb *msg = gsm48_msgb_alloc();
3057 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3058
Harald Welte03740842009-06-10 23:11:52 +08003059 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
3060
3061 /* bearer capability */
3062 encode_bearer_cap(msg, 1, &modify->bearer_cap);
3063 /* cause */
3064 encode_cause(msg, 1, &modify->cause);
3065
3066 new_cc_state(trans, GSM_CSTATE_ACTIVE);
3067
Harald Welte36fe2e82009-07-23 21:13:03 +02003068 return gsm48_sendmsg(msg, trans);
Harald Welte03740842009-06-10 23:11:52 +08003069}
3070
3071static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
3072{
3073 struct gsm_mncc *notify = arg;
3074 struct msgb *msg = gsm48_msgb_alloc();
3075 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3076
Harald Welte03740842009-06-10 23:11:52 +08003077 gh->msg_type = GSM48_MT_CC_NOTIFY;
3078
3079 /* notify */
3080 encode_notify(msg, notify->notify);
3081
Harald Welte36fe2e82009-07-23 21:13:03 +02003082 return gsm48_sendmsg(msg, trans);
Harald Welte03740842009-06-10 23:11:52 +08003083}
3084
3085static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
3086{
3087 struct gsm48_hdr *gh = msgb_l3(msg);
3088 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
3089// struct tlv_parsed tp;
3090 struct gsm_mncc notify;
3091
3092 memset(&notify, 0, sizeof(struct gsm_mncc));
3093 notify.callref = trans->callref;
3094// tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len);
3095 if (payload_len >= 1)
3096 decode_notify(&notify.notify, gh->data);
3097
Harald Welte0abe5a62009-07-23 19:06:52 +02003098 return mncc_recvmsg(trans->subscr->net, trans, MNCC_NOTIFY_IND, &notify);
Harald Welte03740842009-06-10 23:11:52 +08003099}
3100
3101static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
3102{
3103 struct gsm_mncc *user = arg;
3104 struct msgb *msg = gsm48_msgb_alloc();
3105 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3106
Harald Welte03740842009-06-10 23:11:52 +08003107 gh->msg_type = GSM48_MT_CC_USER_INFO;
3108
3109 /* user-user */
3110 if (user->fields & MNCC_F_USERUSER)
3111 encode_useruser(msg, 1, &user->useruser);
3112 /* more data */
3113 if (user->more)
3114 encode_more(msg);
3115
Harald Welte36fe2e82009-07-23 21:13:03 +02003116 return gsm48_sendmsg(msg, trans);
Harald Welte03740842009-06-10 23:11:52 +08003117}
3118
3119static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
3120{
3121 struct gsm48_hdr *gh = msgb_l3(msg);
3122 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
3123 struct tlv_parsed tp;
3124 struct gsm_mncc user;
3125
3126 memset(&user, 0, sizeof(struct gsm_mncc));
3127 user.callref = trans->callref;
3128 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
3129 /* user-user */
3130 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
3131 user.fields |= MNCC_F_USERUSER;
3132 decode_useruser(&user.useruser,
3133 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
3134 }
3135 /* more data */
3136 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
3137 user.more = 1;
3138
Harald Welte0abe5a62009-07-23 19:06:52 +02003139 return mncc_recvmsg(trans->subscr->net, trans, MNCC_USERINFO_IND, &user);
Harald Welte03740842009-06-10 23:11:52 +08003140}
3141
Holger Hans Peter Freythercf0f3362009-10-22 15:13:00 +02003142static int _gsm48_lchan_modify(struct gsm_trans *trans, void *arg)
Harald Welte03740842009-06-10 23:11:52 +08003143{
3144 struct gsm_mncc *mode = arg;
3145
Holger Hans Peter Freythercf0f3362009-10-22 15:13:00 +02003146 return gsm48_lchan_modify(trans->lchan, mode->lchan_mode);
Harald Welte03740842009-06-10 23:11:52 +08003147}
3148
3149static struct downstate {
3150 u_int32_t states;
3151 int type;
3152 int (*rout) (struct gsm_trans *trans, void *arg);
3153} downstatelist[] = {
3154 /* mobile originating call establishment */
3155 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
3156 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc},
3157 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
3158 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
3159 {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 */
3160 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
3161 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
3162 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
3163 /* mobile terminating call establishment */
3164 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
3165 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
3166 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
3167 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
3168 /* signalling during call */
3169 {SBIT(GSM_CSTATE_ACTIVE),
3170 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
3171 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
3172 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
3173 {ALL_STATES,
3174 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
3175 {ALL_STATES,
3176 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
3177 {ALL_STATES,
3178 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
3179 {SBIT(GSM_CSTATE_ACTIVE),
3180 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
3181 {SBIT(GSM_CSTATE_ACTIVE),
3182 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
3183 {SBIT(GSM_CSTATE_ACTIVE),
3184 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
3185 {SBIT(GSM_CSTATE_ACTIVE),
3186 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
3187 {SBIT(GSM_CSTATE_ACTIVE),
3188 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
3189 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
3190 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
3191 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
3192 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
3193 {SBIT(GSM_CSTATE_ACTIVE),
3194 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
3195 /* clearing */
3196 {SBIT(GSM_CSTATE_INITIATED),
3197 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
3198 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
3199 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
3200 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
3201 MNCC_REL_REQ, gsm48_cc_tx_release},
3202 /* special */
3203 {ALL_STATES,
Holger Hans Peter Freythercf0f3362009-10-22 15:13:00 +02003204 MNCC_LCHAN_MODIFY, _gsm48_lchan_modify},
Harald Welte03740842009-06-10 23:11:52 +08003205};
3206
3207#define DOWNSLLEN \
3208 (sizeof(downstatelist) / sizeof(struct downstate))
3209
3210
3211int mncc_send(struct gsm_network *net, int msg_type, void *arg)
3212{
Harald Welteaa60edb2009-08-09 18:52:33 +02003213 int i, rc = 0;
Harald Welte03740842009-06-10 23:11:52 +08003214 struct gsm_trans *trans = NULL, *transt;
3215 struct gsm_subscriber *subscr;
Harald Welteaa60edb2009-08-09 18:52:33 +02003216 struct gsm_lchan *lchan = NULL;
Harald Welte03740842009-06-10 23:11:52 +08003217 struct gsm_bts *bts = NULL;
Harald Welte03740842009-06-10 23:11:52 +08003218 struct gsm_mncc *data = arg, rel;
3219
3220 /* handle special messages */
3221 switch(msg_type) {
3222 case MNCC_BRIDGE:
3223 return tch_bridge(net, arg);
3224 case MNCC_FRAME_DROP:
3225 return tch_recv(net, arg, 0);
3226 case MNCC_FRAME_RECV:
3227 return tch_recv(net, arg, 1);
3228 case GSM_TRAU_FRAME:
3229 return tch_frame(net, arg);
3230 }
3231
3232 memset(&rel, 0, sizeof(struct gsm_mncc));
3233 rel.callref = data->callref;
3234
3235 /* Find callref */
Harald Weltec2189a62009-07-23 18:56:43 +02003236 trans = trans_find_by_callref(net, data->callref);
Harald Welte03740842009-06-10 23:11:52 +08003237
3238 /* Callref unknown */
3239 if (!trans) {
Harald Welte6e1536e2009-07-04 10:11:24 +02003240 if (msg_type != MNCC_SETUP_REQ) {
Harald Welte03740842009-06-10 23:11:52 +08003241 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3242 "Received '%s' from MNCC with "
3243 "unknown callref %d\n", data->called.number,
3244 get_mncc_name(msg_type), data->callref);
3245 /* Invalid call reference */
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08003246 return mncc_release_ind(net, NULL, data->callref,
3247 GSM48_CAUSE_LOC_PRN_S_LU,
3248 GSM48_CC_CAUSE_INVAL_TRANS_ID);
Harald Welte03740842009-06-10 23:11:52 +08003249 }
Andreas Eversberg9eaa5da2009-06-15 23:22:09 +02003250 if (!data->called.number[0] && !data->imsi[0]) {
3251 DEBUGP(DCC, "(bts - trx - ts - ti) "
3252 "Received '%s' from MNCC with "
3253 "no number or IMSI\n", get_mncc_name(msg_type));
3254 /* Invalid number */
3255 return mncc_release_ind(net, NULL, data->callref,
3256 GSM48_CAUSE_LOC_PRN_S_LU,
3257 GSM48_CC_CAUSE_INV_NR_FORMAT);
3258 }
Harald Welte03740842009-06-10 23:11:52 +08003259 /* New transaction due to setup, find subscriber */
Andreas Eversberg9eaa5da2009-06-15 23:22:09 +02003260 if (data->called.number[0])
Harald Welte75350412009-07-23 18:46:00 +02003261 subscr = subscr_get_by_extension(net,
3262 data->called.number);
Andreas Eversberg9eaa5da2009-06-15 23:22:09 +02003263 else
Harald Welte75350412009-07-23 18:46:00 +02003264 subscr = subscr_get_by_imsi(net, data->imsi);
Harald Welte03740842009-06-10 23:11:52 +08003265 /* If subscriber is not found */
3266 if (!subscr) {
3267 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3268 "Received '%s' from MNCC with "
3269 "unknown subscriber %s\n", data->called.number,
3270 get_mncc_name(msg_type), data->called.number);
3271 /* Unknown subscriber */
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08003272 return mncc_release_ind(net, NULL, data->callref,
3273 GSM48_CAUSE_LOC_PRN_S_LU,
3274 GSM48_CC_CAUSE_UNASSIGNED_NR);
Harald Welte03740842009-06-10 23:11:52 +08003275 }
3276 /* If subscriber is not "attached" */
3277 if (!subscr->lac) {
3278 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3279 "Received '%s' from MNCC with "
3280 "detached subscriber %s\n", data->called.number,
3281 get_mncc_name(msg_type), data->called.number);
3282 subscr_put(subscr);
3283 /* Temporarily out of order */
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08003284 return mncc_release_ind(net, NULL, data->callref,
3285 GSM48_CAUSE_LOC_PRN_S_LU,
3286 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte03740842009-06-10 23:11:52 +08003287 }
3288 /* Create transaction */
Harald Weltec2189a62009-07-23 18:56:43 +02003289 trans = trans_alloc(subscr, GSM48_PDISC_CC, 0xff, data->callref);
3290 if (!trans) {
Harald Welte03740842009-06-10 23:11:52 +08003291 DEBUGP(DCC, "No memory for trans.\n");
3292 subscr_put(subscr);
3293 /* Ressource unavailable */
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08003294 mncc_release_ind(net, NULL, data->callref,
3295 GSM48_CAUSE_LOC_PRN_S_LU,
3296 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte03740842009-06-10 23:11:52 +08003297 return -ENOMEM;
3298 }
Harald Welte03740842009-06-10 23:11:52 +08003299 /* Find lchan */
Harald Welteaa60edb2009-08-09 18:52:33 +02003300 lchan = lchan_for_subscr(subscr);
Harald Welte03740842009-06-10 23:11:52 +08003301 /* If subscriber has no lchan */
3302 if (!lchan) {
3303 /* find transaction with this subscriber already paging */
3304 llist_for_each_entry(transt, &net->trans_list, entry) {
3305 /* Transaction of our lchan? */
3306 if (transt == trans ||
3307 transt->subscr != subscr)
3308 continue;
3309 DEBUGP(DCC, "(bts %d trx - ts - ti -- sub %s) "
3310 "Received '%s' from MNCC with "
3311 "unallocated channel, paging already "
3312 "started.\n", bts->nr,
3313 data->called.number,
3314 get_mncc_name(msg_type));
3315 return 0;
3316 }
3317 /* store setup informations until paging was successfull */
Harald Weltec2189a62009-07-23 18:56:43 +02003318 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
Harald Welte2a3a81b2009-08-01 19:31:47 +02003319 /* Trigger paging */
3320 paging_request(net, subscr, RSL_CHANNEED_TCH_F,
3321 setup_trig_pag_evt, subscr);
Harald Welte03740842009-06-10 23:11:52 +08003322 return 0;
3323 }
3324 /* Assign lchan */
3325 trans->lchan = lchan;
3326 use_lchan(lchan);
3327 }
3328 lchan = trans->lchan;
3329
3330 /* if paging did not respond yet */
3331 if (!lchan) {
3332 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3333 "Received '%s' from MNCC in paging state\n",
3334 (trans->subscr)?(trans->subscr->extension):"-",
3335 get_mncc_name(msg_type));
Harald Weltebbc636a2009-06-11 14:23:20 +08003336 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
3337 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Harald Welte03740842009-06-10 23:11:52 +08003338 if (msg_type == MNCC_REL_REQ)
3339 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
3340 else
3341 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
3342 trans->callref = 0;
Harald Weltec2189a62009-07-23 18:56:43 +02003343 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08003344 return rc;
3345 }
3346
3347 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x sub %s) "
3348 "Received '%s' from MNCC in state %d (%s)\n",
3349 lchan->ts->trx->bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
3350 trans->transaction_id,
3351 (lchan->subscr)?(lchan->subscr->extension):"-",
Harald Weltec2189a62009-07-23 18:56:43 +02003352 get_mncc_name(msg_type), trans->cc.state,
3353 cc_state_names[trans->cc.state]);
Harald Welte03740842009-06-10 23:11:52 +08003354
3355 /* Find function for current state and message */
3356 for (i = 0; i < DOWNSLLEN; i++)
3357 if ((msg_type == downstatelist[i].type)
Harald Weltec2189a62009-07-23 18:56:43 +02003358 && ((1 << trans->cc.state) & downstatelist[i].states))
Harald Welte03740842009-06-10 23:11:52 +08003359 break;
3360 if (i == DOWNSLLEN) {
3361 DEBUGP(DCC, "Message unhandled at this state.\n");
3362 return 0;
3363 }
3364
3365 rc = downstatelist[i].rout(trans, arg);
3366
3367 return rc;
3368}
3369
3370
3371static struct datastate {
3372 u_int32_t states;
3373 int type;
3374 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
3375} datastatelist[] = {
3376 /* mobile originating call establishment */
3377 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3378 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
3379 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3380 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
3381 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
3382 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
3383 /* mobile terminating call establishment */
3384 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
3385 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
3386 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
3387 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
3388 {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 */
3389 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
3390 /* signalling during call */
3391 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
3392 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
3393 {SBIT(GSM_CSTATE_ACTIVE),
3394 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
3395 {ALL_STATES,
3396 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
3397 {ALL_STATES,
3398 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
3399 {ALL_STATES,
3400 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
3401 {SBIT(GSM_CSTATE_ACTIVE),
3402 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
3403 {SBIT(GSM_CSTATE_ACTIVE),
3404 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
3405 {SBIT(GSM_CSTATE_ACTIVE),
3406 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
3407 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3408 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
3409 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3410 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
3411 {SBIT(GSM_CSTATE_ACTIVE),
3412 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
3413 /* clearing */
3414 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
3415 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
3416 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
3417 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
3418 {ALL_STATES, /* 5.4.3.4 */
3419 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
3420};
3421
3422#define DATASLLEN \
3423 (sizeof(datastatelist) / sizeof(struct datastate))
3424
Harald Welte59b04682009-06-10 05:40:52 +08003425static int gsm0408_rcv_cc(struct msgb *msg)
3426{
3427 struct gsm48_hdr *gh = msgb_l3(msg);
3428 u_int8_t msg_type = gh->msg_type & 0xbf;
Harald Welte4861c822009-07-23 21:21:14 +02003429 u_int8_t transaction_id = ((gh->proto_discr & 0xf0) ^ 0x80) >> 4; /* flip */
Harald Welte03740842009-06-10 23:11:52 +08003430 struct gsm_lchan *lchan = msg->lchan;
Harald Weltec2189a62009-07-23 18:56:43 +02003431 struct gsm_trans *trans = NULL;
Harald Welte03740842009-06-10 23:11:52 +08003432 int i, rc = 0;
Harald Welte59b04682009-06-10 05:40:52 +08003433
Harald Welte03740842009-06-10 23:11:52 +08003434 if (msg_type & 0x80) {
3435 DEBUGP(DCC, "MSG 0x%2x not defined for PD error\n", msg_type);
3436 return -EINVAL;
Harald Welte59b04682009-06-10 05:40:52 +08003437 }
Harald Welte03740842009-06-10 23:11:52 +08003438
3439 /* Find transaction */
Harald Welte0d824162009-07-23 21:58:40 +02003440 trans = trans_find_by_id(lchan->subscr, GSM48_PDISC_CC, transaction_id);
Harald Weltec2189a62009-07-23 18:56:43 +02003441
Harald Welte4861c822009-07-23 21:21:14 +02003442 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte03740842009-06-10 23:11:52 +08003443 "Received '%s' from MS in state %d (%s)\n",
3444 lchan->ts->trx->bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
3445 transaction_id, (lchan->subscr)?(lchan->subscr->extension):"-",
Holger Hans Peter Freyther36897102009-08-01 07:26:59 +02003446 gsm0408_cc_msg_names[msg_type], trans?(trans->cc.state):0,
Harald Weltec2189a62009-07-23 18:56:43 +02003447 cc_state_names[trans?(trans->cc.state):0]);
Harald Welte03740842009-06-10 23:11:52 +08003448
3449 /* Create transaction */
3450 if (!trans) {
Harald Welte4861c822009-07-23 21:21:14 +02003451 DEBUGP(DCC, "Unknown transaction ID %x, "
Harald Welte03740842009-06-10 23:11:52 +08003452 "creating new trans.\n", transaction_id);
3453 /* Create transaction */
Harald Weltec2189a62009-07-23 18:56:43 +02003454 trans = trans_alloc(lchan->subscr, GSM48_PDISC_CC,
3455 transaction_id, new_callref++);
3456 if (!trans) {
Harald Welte03740842009-06-10 23:11:52 +08003457 DEBUGP(DCC, "No memory for trans.\n");
3458 rc = gsm48_tx_simple(msg->lchan,
Harald Welte4861c822009-07-23 21:21:14 +02003459 GSM48_PDISC_CC | (transaction_id << 4),
Harald Welte03740842009-06-10 23:11:52 +08003460 GSM48_MT_CC_RELEASE_COMPL);
3461 return -ENOMEM;
3462 }
Harald Welte03740842009-06-10 23:11:52 +08003463 /* Assign transaction */
Harald Welte03740842009-06-10 23:11:52 +08003464 trans->lchan = lchan;
3465 use_lchan(lchan);
Harald Welte03740842009-06-10 23:11:52 +08003466 }
3467
3468 /* find function for current state and message */
3469 for (i = 0; i < DATASLLEN; i++)
3470 if ((msg_type == datastatelist[i].type)
Harald Weltec2189a62009-07-23 18:56:43 +02003471 && ((1 << trans->cc.state) & datastatelist[i].states))
Harald Welte03740842009-06-10 23:11:52 +08003472 break;
3473 if (i == DATASLLEN) {
3474 DEBUGP(DCC, "Message unhandled at this state.\n");
3475 return 0;
3476 }
3477
3478 rc = datastatelist[i].rout(trans, msg);
Harald Welte59b04682009-06-10 05:40:52 +08003479
3480 return rc;
3481}
3482
3483/* here we pass in a msgb from the RSL->RLL. We expect the l3 pointer to be set */
Harald Welte (local)64994ce2009-08-14 11:41:12 +02003484int gsm0408_rcvmsg(struct msgb *msg, u_int8_t link_id)
Harald Welte59b04682009-06-10 05:40:52 +08003485{
3486 struct gsm48_hdr *gh = msgb_l3(msg);
3487 u_int8_t pdisc = gh->proto_discr & 0x0f;
3488 int rc = 0;
3489
3490 switch (pdisc) {
3491 case GSM48_PDISC_CC:
3492 rc = gsm0408_rcv_cc(msg);
3493 break;
3494 case GSM48_PDISC_MM:
3495 rc = gsm0408_rcv_mm(msg);
3496 break;
3497 case GSM48_PDISC_RR:
3498 rc = gsm0408_rcv_rr(msg);
3499 break;
3500 case GSM48_PDISC_SMS:
Harald Welte (local)64994ce2009-08-14 11:41:12 +02003501 rc = gsm0411_rcv_sms(msg, link_id);
Harald Welte59b04682009-06-10 05:40:52 +08003502 break;
3503 case GSM48_PDISC_MM_GPRS:
3504 case GSM48_PDISC_SM_GPRS:
Mike Haben2f8a6852009-09-26 19:00:03 +02003505 fprintf(stderr, "Unimplemented GSM 04.08 discriminator 0x%02x\n",
Harald Welte59b04682009-06-10 05:40:52 +08003506 pdisc);
3507 break;
Harald Welte03115042009-10-16 08:32:58 +02003508 case GSM48_PDISC_NC_SS:
3509 rc = handle_rcv_ussd(msg);
3510 break;
Harald Welte59b04682009-06-10 05:40:52 +08003511 default:
Mike Haben2f8a6852009-09-26 19:00:03 +02003512 fprintf(stderr, "Unknown GSM 04.08 discriminator 0x%02x\n",
Harald Welte59b04682009-06-10 05:40:52 +08003513 pdisc);
3514 break;
3515 }
3516
3517 return rc;
3518}
3519
Harald Welte03740842009-06-10 23:11:52 +08003520/* dequeue messages to layer 4 */
3521int bsc_upqueue(struct gsm_network *net)
3522{
3523 struct gsm_mncc *mncc;
3524 struct msgb *msg;
3525 int work = 0;
3526
3527 if (net)
3528 while ((msg = msgb_dequeue(&net->upqueue))) {
3529 mncc = (struct gsm_mncc *)msg->data;
3530 if (net->mncc_recv)
3531 net->mncc_recv(net, mncc->msg_type, mncc);
3532 work = 1; /* work done */
Harald Weltebaf4d3a2009-06-26 19:40:48 +02003533 talloc_free(msg);
Harald Welte03740842009-06-10 23:11:52 +08003534 }
3535
3536 return work;
3537}
Harald Weltec2189a62009-07-23 18:56:43 +02003538
Harald Welte3c062072009-07-28 18:25:29 +02003539/*
3540 * This will be ran by the linker when loading the DSO. We use it to
3541 * do system initialization, e.g. registration of signal handlers.
3542 */
3543static __attribute__((constructor)) void on_dso_load_0408(void)
3544{
Harald Welte3c062072009-07-28 18:25:29 +02003545 register_signal_handler(SS_LCHAN, gsm0408_handle_lchan_signal, NULL);
3546 register_signal_handler(SS_ABISIP, handle_abisip_signal, NULL);
3547}