blob: a1fa5462b371cc23910ccff427e01796d610f59d [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
Harald Weltec20bd1d2009-11-29 19:07:28 +0100169int gsm48_parse_meas_rep(struct gsm_meas_rep *rep, struct msgb *msg)
Harald Welte59b04682009-06-10 05:40:52 +0800170{
Harald Weltec20bd1d2009-11-29 19:07:28 +0100171 struct gsm48_hdr *gh = msgb_l3(msg);
172 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
173 u_int8_t *data = gh->data;
174
175 if (gh->msg_type != GSM48_MT_RR_MEAS_REP)
176 return -EINVAL;
Harald Welte59b04682009-06-10 05:40:52 +0800177
178 if (data[0] & 0x80)
179 rep->flags |= MEAS_REP_F_BA1;
180 if (data[0] & 0x40)
Harald Weltec20bd1d2009-11-29 19:07:28 +0100181 rep->flags |= MEAS_REP_F_UL_DTX;
Harald Welteba6589d2009-07-05 04:05:44 +0200182 if ((data[1] & 0x40) == 0x00)
Harald Weltec20bd1d2009-11-29 19:07:28 +0100183 rep->flags |= MEAS_REP_F_DL_VALID;
Harald Welte59b04682009-06-10 05:40:52 +0800184
Harald Weltec20bd1d2009-11-29 19:07:28 +0100185 rep->dl.full.rx_lev = data[0] & 0x3f;
186 rep->dl.sub.rx_lev = data[1] & 0x3f;
187 rep->dl.full.rx_qual = (data[3] >> 4) & 0x7;
188 rep->dl.sub.rx_qual = (data[3] >> 1) & 0x7;
189
Harald Welte59b04682009-06-10 05:40:52 +0800190 rep->num_cell = data[4] >> 6 | ((data[3] & 0x01) << 2);
191 if (rep->num_cell < 1)
Harald Weltec20bd1d2009-11-29 19:07:28 +0100192 return 0;
Harald Welte59b04682009-06-10 05:40:52 +0800193
194 /* an encoding nightmare in perfection */
195
196 rep->cell[0].rxlev = data[4] & 0x3f;
197 rep->cell[0].bcch_freq = data[5] >> 2;
198 rep->cell[0].bsic = ((data[5] & 0x03) << 3) | (data[6] >> 5);
199 if (rep->num_cell < 2)
Harald Weltec20bd1d2009-11-29 19:07:28 +0100200 return 0;
Harald Welte59b04682009-06-10 05:40:52 +0800201
202 rep->cell[1].rxlev = ((data[6] & 0x1f) << 1) | (data[7] >> 7);
203 rep->cell[1].bcch_freq = (data[7] >> 2) & 0x1f;
204 rep->cell[1].bsic = ((data[7] & 0x03) << 4) | (data[8] >> 4);
205 if (rep->num_cell < 3)
Harald Weltec20bd1d2009-11-29 19:07:28 +0100206 return 0;
Harald Welte59b04682009-06-10 05:40:52 +0800207
208 rep->cell[2].rxlev = ((data[8] & 0x0f) << 2) | (data[9] >> 6);
209 rep->cell[2].bcch_freq = (data[9] >> 1) & 0x1f;
210 rep->cell[2].bsic = ((data[9] & 0x01) << 6) | (data[10] >> 3);
211 if (rep->num_cell < 4)
Harald Weltec20bd1d2009-11-29 19:07:28 +0100212 return 0;
Harald Welte59b04682009-06-10 05:40:52 +0800213
214 rep->cell[3].rxlev = ((data[10] & 0x07) << 3) | (data[11] >> 5);
215 rep->cell[3].bcch_freq = data[11] & 0x1f;
216 rep->cell[3].bsic = data[12] >> 2;
217 if (rep->num_cell < 5)
Harald Weltec20bd1d2009-11-29 19:07:28 +0100218 return 0;
Harald Welte59b04682009-06-10 05:40:52 +0800219
220 rep->cell[4].rxlev = ((data[12] & 0x03) << 4) | (data[13] >> 4);
221 rep->cell[4].bcch_freq = ((data[13] & 0xf) << 1) | (data[14] >> 7);
222 rep->cell[4].bsic = (data[14] >> 1) & 0x3f;
223 if (rep->num_cell < 6)
Harald Weltec20bd1d2009-11-29 19:07:28 +0100224 return 0;
Harald Welte59b04682009-06-10 05:40:52 +0800225
226 rep->cell[5].rxlev = ((data[14] & 0x01) << 5) | (data[15] >> 3);
227 rep->cell[5].bcch_freq = ((data[15] & 0x07) << 2) | (data[16] >> 6);
228 rep->cell[5].bsic = data[16] & 0x3f;
Harald Weltec20bd1d2009-11-29 19:07:28 +0100229
230 return 0;
Harald Welte59b04682009-06-10 05:40:52 +0800231}
232
233int gsm0408_loc_upd_acc(struct gsm_lchan *lchan, u_int32_t tmsi);
234static int gsm48_tx_simple(struct gsm_lchan *lchan,
235 u_int8_t pdisc, u_int8_t msg_type);
236static void schedule_reject(struct gsm_lchan *lchan);
237
238struct gsm_lai {
239 u_int16_t mcc;
240 u_int16_t mnc;
241 u_int16_t lac;
242};
243
Harald Welte03740842009-06-10 23:11:52 +0800244static u_int32_t new_callref = 0x80000001;
245
Harald Welte59b04682009-06-10 05:40:52 +0800246static int authorize_subscriber(struct gsm_loc_updating_operation *loc,
247 struct gsm_subscriber *subscriber)
248{
249 if (!subscriber)
250 return 0;
251
252 /*
253 * Do not send accept yet as more information should arrive. Some
254 * phones will not send us the information and we will have to check
255 * what we want to do with that.
256 */
257 if (loc && (loc->waiting_for_imsi || loc->waiting_for_imei))
258 return 0;
259
Jan Luebbe3d6294e2009-08-12 12:48:00 +0200260 switch (subscriber->net->auth_policy) {
261 case GSM_AUTH_POLICY_CLOSED:
262 return subscriber->authorized;
Harald Welte (local)16480eb2009-08-13 13:49:51 +0200263 case GSM_AUTH_POLICY_TOKEN:
Harald Welte (local)272f5c72009-08-13 20:44:23 +0200264 if (subscriber->authorized)
265 return subscriber->authorized;
Harald Welte (local)16480eb2009-08-13 13:49:51 +0200266 return (subscriber->flags & GSM_SUBSCRIBER_FIRST_CONTACT);
Jan Luebbe3d6294e2009-08-12 12:48:00 +0200267 case GSM_AUTH_POLICY_ACCEPT_ALL:
Harald Welte59b04682009-06-10 05:40:52 +0800268 return 1;
Jan Luebbe3d6294e2009-08-12 12:48:00 +0200269 default:
270 return 0;
271 }
Harald Welte59b04682009-06-10 05:40:52 +0800272}
273
274static void release_loc_updating_req(struct gsm_lchan *lchan)
275{
276 if (!lchan->loc_operation)
277 return;
278
279 bsc_del_timer(&lchan->loc_operation->updating_timer);
Harald Weltea8379772009-06-20 22:36:41 +0200280 talloc_free(lchan->loc_operation);
Harald Welte59b04682009-06-10 05:40:52 +0800281 lchan->loc_operation = 0;
282 put_lchan(lchan);
283}
284
285static void allocate_loc_updating_req(struct gsm_lchan *lchan)
286{
287 use_lchan(lchan);
288 release_loc_updating_req(lchan);
289
Harald Welte857e00d2009-06-26 20:25:23 +0200290 lchan->loc_operation = talloc_zero(tall_locop_ctx,
291 struct gsm_loc_updating_operation);
Harald Welte59b04682009-06-10 05:40:52 +0800292}
293
294static int gsm0408_authorize(struct gsm_lchan *lchan, struct msgb *msg)
295{
Harald Welte59b04682009-06-10 05:40:52 +0800296 if (authorize_subscriber(lchan->loc_operation, lchan->subscr)) {
Harald Welte5070e942009-08-09 19:07:00 +0200297 int rc;
298
Harald Welte59b04682009-06-10 05:40:52 +0800299 db_subscriber_alloc_tmsi(lchan->subscr);
Harald Welte59b04682009-06-10 05:40:52 +0800300 release_loc_updating_req(lchan);
Holger Hans Peter Freythercd8bacf2009-08-19 12:53:57 +0200301 rc = gsm0408_loc_upd_acc(msg->lchan, lchan->subscr->tmsi);
Harald Welte9ed54ff2009-12-12 21:00:48 +0100302 /* send MM INFO with network name */
303 rc = gsm48_tx_mm_info(msg->lchan);
304
Harald Welte5070e942009-08-09 19:07:00 +0200305 /* call subscr_update after putting the loc_upd_acc
306 * in the transmit queue, since S_SUBSCR_ATTACHED might
307 * trigger further action like SMS delivery */
308 subscr_update(lchan->subscr, msg->trx->bts,
309 GSM_SUBSCRIBER_UPDATE_ATTACHED);
Harald Welte9ed54ff2009-12-12 21:00:48 +0100310 /* try to close channel ASAP */
311 lchan_auto_release(lchan);
Harald Welte5070e942009-08-09 19:07:00 +0200312 return rc;
Harald Welte59b04682009-06-10 05:40:52 +0800313 }
314
315 return 0;
316}
317
318static int gsm0408_handle_lchan_signal(unsigned int subsys, unsigned int signal,
319 void *handler_data, void *signal_data)
320{
Harald Welte03740842009-06-10 23:11:52 +0800321 struct gsm_trans *trans, *temp;
322
Harald Welte59b04682009-06-10 05:40:52 +0800323 if (subsys != SS_LCHAN || signal != S_LCHAN_UNEXPECTED_RELEASE)
324 return 0;
325
326 /*
327 * Cancel any outstanding location updating request
328 * operation taking place on the lchan.
329 */
Harald Welte12560da2009-07-04 09:35:21 +0200330 struct gsm_lchan *lchan = (struct gsm_lchan *)signal_data;
Harald Welte1ff81b52009-06-26 20:17:06 +0200331 if (!lchan)
332 return 0;
333
Harald Welte59b04682009-06-10 05:40:52 +0800334 release_loc_updating_req(lchan);
335
Harald Welte03740842009-06-10 23:11:52 +0800336 /* Free all transactions that are associated with the released lchan */
Harald Weltec2189a62009-07-23 18:56:43 +0200337 /* FIXME: this is not neccessarily the right thing to do, we should
338 * only set trans->lchan to NULL and wait for another lchan to be
339 * established to the same MM entity (phone/subscriber) */
Harald Welte03740842009-06-10 23:11:52 +0800340 llist_for_each_entry_safe(trans, temp, &lchan->ts->trx->bts->network->trans_list, entry) {
341 if (trans->lchan == lchan)
Harald Weltec2189a62009-07-23 18:56:43 +0200342 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +0800343 }
344
Harald Welte59b04682009-06-10 05:40:52 +0800345 return 0;
346}
347
Harald Welte59b04682009-06-10 05:40:52 +0800348static const char bcd_num_digits[] = {
349 '0', '1', '2', '3', '4', '5', '6', '7',
350 '8', '9', '*', '#', 'a', 'b', 'c', '\0'
351};
352
Harald Welteb8a18b52009-06-10 12:08:54 +0800353/* decode a 'called/calling/connect party BCD number' as in 10.5.4.7 */
354int decode_bcd_number(char *output, int output_len, const u_int8_t *bcd_lv,
355 int h_len)
Harald Welte59b04682009-06-10 05:40:52 +0800356{
357 u_int8_t in_len = bcd_lv[0];
358 int i;
359
Harald Welteb8a18b52009-06-10 12:08:54 +0800360 for (i = 1 + h_len; i <= in_len; i++) {
Harald Welte59b04682009-06-10 05:40:52 +0800361 /* lower nibble */
362 output_len--;
363 if (output_len <= 1)
364 break;
365 *output++ = bcd_num_digits[bcd_lv[i] & 0xf];
366
367 /* higher nibble */
368 output_len--;
369 if (output_len <= 1)
370 break;
371 *output++ = bcd_num_digits[bcd_lv[i] >> 4];
372 }
373 if (output_len >= 1)
374 *output++ = '\0';
375
Harald Welteb8a18b52009-06-10 12:08:54 +0800376 return 0;
Harald Welte59b04682009-06-10 05:40:52 +0800377}
378
379/* convert a single ASCII character to call-control BCD */
380static int asc_to_bcd(const char asc)
381{
382 int i;
383
384 for (i = 0; i < ARRAY_SIZE(bcd_num_digits); i++) {
385 if (bcd_num_digits[i] == asc)
386 return i;
387 }
388 return -EINVAL;
389}
390
Harald Welteb8a18b52009-06-10 12:08:54 +0800391/* convert a ASCII phone number to 'called/calling/connect party BCD number' */
Harald Welte59b04682009-06-10 05:40:52 +0800392int encode_bcd_number(u_int8_t *bcd_lv, u_int8_t max_len,
Harald Welteb8a18b52009-06-10 12:08:54 +0800393 int h_len, const char *input)
Harald Welte59b04682009-06-10 05:40:52 +0800394{
395 int in_len = strlen(input);
396 int i;
Harald Welteb8a18b52009-06-10 12:08:54 +0800397 u_int8_t *bcd_cur = bcd_lv + 1 + h_len;
Harald Welte59b04682009-06-10 05:40:52 +0800398
399 /* two digits per byte, plus type byte */
Harald Welteb8a18b52009-06-10 12:08:54 +0800400 bcd_lv[0] = in_len/2 + h_len;
Harald Welte59b04682009-06-10 05:40:52 +0800401 if (in_len % 2)
402 bcd_lv[0]++;
403
Harald Welteb8a18b52009-06-10 12:08:54 +0800404 if (bcd_lv[0] > max_len)
405 return -EIO;
Harald Welte59b04682009-06-10 05:40:52 +0800406
407 for (i = 0; i < in_len; i++) {
408 int rc = asc_to_bcd(input[i]);
409 if (rc < 0)
410 return rc;
411 if (i % 2 == 0)
412 *bcd_cur = rc;
413 else
414 *bcd_cur++ |= (rc << 4);
415 }
416 /* append padding nibble in case of odd length */
417 if (i % 2)
418 *bcd_cur++ |= 0xf0;
419
420 /* return how many bytes we used */
421 return (bcd_cur - bcd_lv);
422}
423
Harald Welteb8a18b52009-06-10 12:08:54 +0800424/* decode 'bearer capability' */
Harald Welte03740842009-06-10 23:11:52 +0800425static int decode_bearer_cap(struct gsm_mncc_bearer_cap *bcap,
Harald Welteb8a18b52009-06-10 12:08:54 +0800426 const u_int8_t *lv)
427{
428 u_int8_t in_len = lv[0];
429 int i, s;
430
431 if (in_len < 1)
432 return -EINVAL;
433
Harald Welte03740842009-06-10 23:11:52 +0800434 bcap->speech_ver[0] = -1; /* end of list, of maximum 7 values */
Harald Welteb8a18b52009-06-10 12:08:54 +0800435
436 /* octet 3 */
Harald Welte03740842009-06-10 23:11:52 +0800437 bcap->transfer = lv[1] & 0x07;
438 bcap->mode = (lv[1] & 0x08) >> 3;
439 bcap->coding = (lv[1] & 0x10) >> 4;
440 bcap->radio = (lv[1] & 0x60) >> 5;
Harald Welteb8a18b52009-06-10 12:08:54 +0800441
442 i = 1;
443 s = 0;
444 while(!(lv[i] & 0x80)) {
445 i++; /* octet 3a etc */
446 if (in_len < i)
447 return 0;
Harald Welte03740842009-06-10 23:11:52 +0800448 bcap->speech_ver[s++] = lv[i] & 0x0f;
449 bcap->speech_ver[s] = -1; /* end of list */
Harald Welteb8a18b52009-06-10 12:08:54 +0800450 if (i == 2) /* octet 3a */
Harald Welte03740842009-06-10 23:11:52 +0800451 bcap->speech_ctm = (lv[i] & 0x20) >> 5;
Harald Welteb8a18b52009-06-10 12:08:54 +0800452 if (s == 7) /* maximum speech versions + end of list */
453 return 0;
454 }
455
456 return 0;
457}
458
459/* encode 'bearer capability' */
Harald Welte03740842009-06-10 23:11:52 +0800460static int encode_bearer_cap(struct msgb *msg, int lv_only,
461 const struct gsm_mncc_bearer_cap *bcap)
Harald Welteb8a18b52009-06-10 12:08:54 +0800462{
463 u_int8_t lv[32 + 1];
464 int i, s;
465
Harald Welte03740842009-06-10 23:11:52 +0800466 lv[1] = bcap->transfer;
467 lv[1] |= bcap->mode << 3;
468 lv[1] |= bcap->coding << 4;
469 lv[1] |= bcap->radio << 5;
Harald Welteb8a18b52009-06-10 12:08:54 +0800470
471 i = 1;
Harald Welte03740842009-06-10 23:11:52 +0800472 for (s = 0; bcap->speech_ver[s] >= 0; s++) {
Harald Welteb8a18b52009-06-10 12:08:54 +0800473 i++; /* octet 3a etc */
Harald Welte03740842009-06-10 23:11:52 +0800474 lv[i] = bcap->speech_ver[s];
Harald Welteb8a18b52009-06-10 12:08:54 +0800475 if (i == 2) /* octet 3a */
Harald Welte03740842009-06-10 23:11:52 +0800476 lv[i] |= bcap->speech_ctm << 5;
Harald Welteb8a18b52009-06-10 12:08:54 +0800477 }
478 lv[i] |= 0x80; /* last IE of octet 3 etc */
479
480 lv[0] = i;
481 if (lv_only)
482 msgb_lv_put(msg, lv[0], lv+1);
483 else
484 msgb_tlv_put(msg, GSM48_IE_BEARER_CAP, lv[0], lv+1);
485
486 return 0;
487}
488
489/* decode 'call control cap' */
Harald Welte03740842009-06-10 23:11:52 +0800490static int decode_cccap(struct gsm_mncc_cccap *ccap, const u_int8_t *lv)
Harald Welteb8a18b52009-06-10 12:08:54 +0800491{
492 u_int8_t in_len = lv[0];
493
494 if (in_len < 1)
495 return -EINVAL;
496
497 /* octet 3 */
Harald Welte03740842009-06-10 23:11:52 +0800498 ccap->dtmf = lv[1] & 0x01;
499 ccap->pcp = (lv[1] & 0x02) >> 1;
Harald Welteb8a18b52009-06-10 12:08:54 +0800500
501 return 0;
502}
503
504/* decode 'called party BCD number' */
Harald Welte03740842009-06-10 23:11:52 +0800505static int decode_called(struct gsm_mncc_number *called,
506 const u_int8_t *lv)
Harald Welteb8a18b52009-06-10 12:08:54 +0800507{
508 u_int8_t in_len = lv[0];
509
510 if (in_len < 1)
511 return -EINVAL;
512
513 /* octet 3 */
Harald Welte03740842009-06-10 23:11:52 +0800514 called->plan = lv[1] & 0x0f;
515 called->type = (lv[1] & 0x70) >> 4;
Harald Welteb8a18b52009-06-10 12:08:54 +0800516
517 /* octet 4..N */
Harald Welte03740842009-06-10 23:11:52 +0800518 decode_bcd_number(called->number, sizeof(called->number), lv, 1);
Harald Welteb8a18b52009-06-10 12:08:54 +0800519
520 return 0;
521}
522
523/* encode 'called party BCD number' */
Harald Welte03740842009-06-10 23:11:52 +0800524static int encode_called(struct msgb *msg,
525 const struct gsm_mncc_number *called)
Harald Welteb8a18b52009-06-10 12:08:54 +0800526{
527 u_int8_t lv[18];
528 int ret;
529
530 /* octet 3 */
Harald Welte03740842009-06-10 23:11:52 +0800531 lv[1] = called->plan;
532 lv[1] |= called->type << 4;
Harald Welteb8a18b52009-06-10 12:08:54 +0800533
534 /* octet 4..N, octet 2 */
Harald Welte03740842009-06-10 23:11:52 +0800535 ret = encode_bcd_number(lv, sizeof(lv), 1, called->number);
Harald Welteb8a18b52009-06-10 12:08:54 +0800536 if (ret < 0)
537 return ret;
538
539 msgb_tlv_put(msg, GSM48_IE_CALLED_BCD, lv[0], lv+1);
540
541 return 0;
542}
543
544/* encode callerid of various IEs */
Harald Welte03740842009-06-10 23:11:52 +0800545static int encode_callerid(struct msgb *msg, int ie,
546 const struct gsm_mncc_number *callerid)
Harald Welteb8a18b52009-06-10 12:08:54 +0800547{
548 u_int8_t lv[13];
549 int h_len = 1;
550 int ret;
551
552 /* octet 3 */
Harald Welte03740842009-06-10 23:11:52 +0800553 lv[1] = callerid->plan;
554 lv[1] |= callerid->type << 4;
Harald Welteb8a18b52009-06-10 12:08:54 +0800555
Harald Welte03740842009-06-10 23:11:52 +0800556 if (callerid->present || callerid->screen) {
Harald Welteb8a18b52009-06-10 12:08:54 +0800557 /* octet 3a */
Harald Welte03740842009-06-10 23:11:52 +0800558 lv[2] = callerid->screen;
559 lv[2] |= callerid->present << 5;
Harald Welteb8a18b52009-06-10 12:08:54 +0800560 lv[2] |= 0x80;
561 h_len++;
562 } else
563 lv[1] |= 0x80;
564
565 /* octet 4..N, octet 2 */
Harald Welte03740842009-06-10 23:11:52 +0800566 ret = encode_bcd_number(lv, sizeof(lv), h_len, callerid->number);
Harald Welteb8a18b52009-06-10 12:08:54 +0800567 if (ret < 0)
568 return ret;
569
570 msgb_tlv_put(msg, ie, lv[0], lv+1);
571
572 return 0;
573}
574
575/* decode 'cause' */
Harald Welte03740842009-06-10 23:11:52 +0800576static int decode_cause(struct gsm_mncc_cause *cause,
Harald Welteb8a18b52009-06-10 12:08:54 +0800577 const u_int8_t *lv)
578{
579 u_int8_t in_len = lv[0];
580 int i;
581
582 if (in_len < 2)
583 return -EINVAL;
584
Harald Welte03740842009-06-10 23:11:52 +0800585 cause->diag_len = 0;
Harald Welteb8a18b52009-06-10 12:08:54 +0800586
587 /* octet 3 */
Harald Welte03740842009-06-10 23:11:52 +0800588 cause->location = lv[1] & 0x0f;
589 cause->coding = (lv[1] & 0x60) >> 5;
Harald Welteb8a18b52009-06-10 12:08:54 +0800590
591 i = 1;
592 if (!(lv[i] & 0x80)) {
593 i++; /* octet 3a */
594 if (in_len < i+1)
595 return 0;
Harald Welte03740842009-06-10 23:11:52 +0800596 cause->rec = 1;
597 cause->rec_val = lv[i] & 0x7f;
Harald Welteb8a18b52009-06-10 12:08:54 +0800598
599 }
600 i++;
601
602 /* octet 4 */
Harald Welte03740842009-06-10 23:11:52 +0800603 cause->value = lv[i] & 0x7f;
Harald Welteb8a18b52009-06-10 12:08:54 +0800604 i++;
605
606 if (in_len < i) /* no diag */
607 return 0;
608
609 if (in_len - (i-1) > 32) /* maximum 32 octets */
610 return 0;
611
612 /* octet 5-N */
Harald Welte03740842009-06-10 23:11:52 +0800613 memcpy(cause->diag, lv + i, in_len - (i-1));
614 cause->diag_len = in_len - (i-1);
Harald Welteb8a18b52009-06-10 12:08:54 +0800615
616 return 0;
617}
618
619/* encode 'cause' */
Harald Welte03740842009-06-10 23:11:52 +0800620static int encode_cause(struct msgb *msg, int lv_only,
621 const struct gsm_mncc_cause *cause)
Harald Welteb8a18b52009-06-10 12:08:54 +0800622{
623 u_int8_t lv[32+4];
624 int i;
625
Harald Welte03740842009-06-10 23:11:52 +0800626 if (cause->diag_len > 32)
Harald Welteb8a18b52009-06-10 12:08:54 +0800627 return -EINVAL;
628
629 /* octet 3 */
Harald Welte03740842009-06-10 23:11:52 +0800630 lv[1] = cause->location;
631 lv[1] |= cause->coding << 5;
Harald Welteb8a18b52009-06-10 12:08:54 +0800632
633 i = 1;
Harald Welte03740842009-06-10 23:11:52 +0800634 if (cause->rec) {
Harald Welteb8a18b52009-06-10 12:08:54 +0800635 i++; /* octet 3a */
Harald Welte03740842009-06-10 23:11:52 +0800636 lv[i] = cause->rec_val;
Harald Welteb8a18b52009-06-10 12:08:54 +0800637 }
638 lv[i] |= 0x80; /* end of octet 3 */
639
640 /* octet 4 */
641 i++;
Harald Welte03740842009-06-10 23:11:52 +0800642 lv[i] = 0x80 | cause->value;
Harald Welteb8a18b52009-06-10 12:08:54 +0800643
644 /* octet 5-N */
Harald Welte03740842009-06-10 23:11:52 +0800645 if (cause->diag_len) {
646 memcpy(lv + i, cause->diag, cause->diag_len);
647 i += cause->diag_len;
Harald Welteb8a18b52009-06-10 12:08:54 +0800648 }
649
650 lv[0] = i;
651 if (lv_only)
652 msgb_lv_put(msg, lv[0], lv+1);
653 else
654 msgb_tlv_put(msg, GSM48_IE_CAUSE, lv[0], lv+1);
655
656 return 0;
657}
658
659/* encode 'calling number' */
Harald Welte03740842009-06-10 23:11:52 +0800660static int encode_calling(struct msgb *msg,
661 const struct gsm_mncc_number *calling)
Harald Welteb8a18b52009-06-10 12:08:54 +0800662{
Harald Welte03740842009-06-10 23:11:52 +0800663 return encode_callerid(msg, GSM48_IE_CALLING_BCD, calling);
Harald Welteb8a18b52009-06-10 12:08:54 +0800664}
665
666/* encode 'connected number' */
Harald Welte03740842009-06-10 23:11:52 +0800667static int encode_connected(struct msgb *msg,
668 const struct gsm_mncc_number *connected)
Harald Welteb8a18b52009-06-10 12:08:54 +0800669{
Harald Welte03740842009-06-10 23:11:52 +0800670 return encode_callerid(msg, GSM48_IE_CONN_BCD, connected);
Harald Welteb8a18b52009-06-10 12:08:54 +0800671}
672
673/* encode 'redirecting number' */
Harald Welte03740842009-06-10 23:11:52 +0800674static int encode_redirecting(struct msgb *msg,
675 const struct gsm_mncc_number *redirecting)
Harald Welteb8a18b52009-06-10 12:08:54 +0800676{
Harald Welte03740842009-06-10 23:11:52 +0800677 return encode_callerid(msg, GSM48_IE_REDIR_BCD, redirecting);
Harald Welteb8a18b52009-06-10 12:08:54 +0800678}
679
680/* decode 'facility' */
Harald Welte03740842009-06-10 23:11:52 +0800681static int decode_facility(struct gsm_mncc_facility *facility,
Harald Welteb8a18b52009-06-10 12:08:54 +0800682 const u_int8_t *lv)
683{
684 u_int8_t in_len = lv[0];
685
686 if (in_len < 1)
687 return -EINVAL;
688
Harald Welte03740842009-06-10 23:11:52 +0800689 if (in_len > sizeof(facility->info))
Harald Welteb8a18b52009-06-10 12:08:54 +0800690 return -EINVAL;
691
Harald Welte03740842009-06-10 23:11:52 +0800692 memcpy(facility->info, lv+1, in_len);
693 facility->len = in_len;
Harald Welteb8a18b52009-06-10 12:08:54 +0800694
695 return 0;
696}
697
698/* encode 'facility' */
Harald Welte03740842009-06-10 23:11:52 +0800699static int encode_facility(struct msgb *msg, int lv_only,
700 const struct gsm_mncc_facility *facility)
Harald Welteb8a18b52009-06-10 12:08:54 +0800701{
702 u_int8_t lv[GSM_MAX_FACILITY + 1];
703
Harald Welte03740842009-06-10 23:11:52 +0800704 if (facility->len < 1 || facility->len > GSM_MAX_FACILITY)
Harald Welteb8a18b52009-06-10 12:08:54 +0800705 return -EINVAL;
706
Harald Welte03740842009-06-10 23:11:52 +0800707 memcpy(lv+1, facility->info, facility->len);
708 lv[0] = facility->len;
Harald Welteb8a18b52009-06-10 12:08:54 +0800709 if (lv_only)
710 msgb_lv_put(msg, lv[0], lv+1);
711 else
712 msgb_tlv_put(msg, GSM48_IE_FACILITY, lv[0], lv+1);
713
714 return 0;
715}
716
717/* decode 'notify' */
718static int decode_notify(int *notify, const u_int8_t *v)
719{
720 *notify = v[0] & 0x7f;
721
722 return 0;
723}
724
725/* encode 'notify' */
726static int encode_notify(struct msgb *msg, int notify)
727{
728 msgb_v_put(msg, notify | 0x80);
729
730 return 0;
731}
732
733/* encode 'signal' */
734static int encode_signal(struct msgb *msg, int signal)
735{
736 msgb_tv_put(msg, GSM48_IE_SIGNAL, signal);
737
738 return 0;
739}
740
741/* decode 'keypad' */
742static int decode_keypad(int *keypad, const u_int8_t *lv)
743{
744 u_int8_t in_len = lv[0];
745
746 if (in_len < 1)
747 return -EINVAL;
748
749 *keypad = lv[1] & 0x7f;
750
751 return 0;
752}
753
754/* encode 'keypad' */
755static int encode_keypad(struct msgb *msg, int keypad)
756{
757 msgb_tv_put(msg, GSM48_IE_KPD_FACILITY, keypad);
758
759 return 0;
760}
761
762/* decode 'progress' */
Harald Welte03740842009-06-10 23:11:52 +0800763static int decode_progress(struct gsm_mncc_progress *progress,
Harald Welteb8a18b52009-06-10 12:08:54 +0800764 const u_int8_t *lv)
765{
766 u_int8_t in_len = lv[0];
767
768 if (in_len < 2)
769 return -EINVAL;
770
Harald Welte03740842009-06-10 23:11:52 +0800771 progress->coding = (lv[1] & 0x60) >> 5;
772 progress->location = lv[1] & 0x0f;
773 progress->descr = lv[2] & 0x7f;
Harald Welteb8a18b52009-06-10 12:08:54 +0800774
775 return 0;
776}
777
778/* encode 'progress' */
Harald Welte03740842009-06-10 23:11:52 +0800779static int encode_progress(struct msgb *msg, int lv_only,
780 const struct gsm_mncc_progress *p)
Harald Welteb8a18b52009-06-10 12:08:54 +0800781{
782 u_int8_t lv[3];
783
784 lv[0] = 2;
Harald Welte03740842009-06-10 23:11:52 +0800785 lv[1] = 0x80 | ((p->coding & 0x3) << 5) | (p->location & 0xf);
786 lv[2] = 0x80 | (p->descr & 0x7f);
Harald Welteb8a18b52009-06-10 12:08:54 +0800787 if (lv_only)
788 msgb_lv_put(msg, lv[0], lv+1);
789 else
790 msgb_tlv_put(msg, GSM48_IE_PROGR_IND, lv[0], lv+1);
791
792 return 0;
793}
794
795/* decode 'user-user' */
Harald Welte03740842009-06-10 23:11:52 +0800796static int decode_useruser(struct gsm_mncc_useruser *uu,
Harald Welteb8a18b52009-06-10 12:08:54 +0800797 const u_int8_t *lv)
798{
799 u_int8_t in_len = lv[0];
Harald Welte03740842009-06-10 23:11:52 +0800800 char *info = uu->info;
801 int info_len = sizeof(uu->info);
Harald Welteb8a18b52009-06-10 12:08:54 +0800802 int i;
803
804 if (in_len < 1)
805 return -EINVAL;
806
Harald Welte03740842009-06-10 23:11:52 +0800807 uu->proto = lv[1];
Harald Welteb8a18b52009-06-10 12:08:54 +0800808
809 for (i = 2; i <= in_len; i++) {
810 info_len--;
811 if (info_len <= 1)
812 break;
813 *info++ = lv[i];
814 }
815 if (info_len >= 1)
816 *info++ = '\0';
817
818 return 0;
819}
820
821/* encode 'useruser' */
Harald Welte03740842009-06-10 23:11:52 +0800822static int encode_useruser(struct msgb *msg, int lv_only,
823 const struct gsm_mncc_useruser *uu)
Harald Welteb8a18b52009-06-10 12:08:54 +0800824{
825 u_int8_t lv[GSM_MAX_USERUSER + 2];
826
Harald Welte03740842009-06-10 23:11:52 +0800827 if (strlen(uu->info) > GSM_MAX_USERUSER)
Harald Welteb8a18b52009-06-10 12:08:54 +0800828 return -EINVAL;
829
Harald Welte03740842009-06-10 23:11:52 +0800830 lv[0] = 1 + strlen(uu->info);
831 lv[1] = uu->proto;
832 memcpy(lv + 2, uu->info, strlen(uu->info));
Harald Welteb8a18b52009-06-10 12:08:54 +0800833 if (lv_only)
834 msgb_lv_put(msg, lv[0], lv+1);
835 else
836 msgb_tlv_put(msg, GSM48_IE_USER_USER, lv[0], lv+1);
837
838 return 0;
839}
840
841/* decode 'ss version' */
Harald Welte03740842009-06-10 23:11:52 +0800842static int decode_ssversion(struct gsm_mncc_ssversion *ssv,
Harald Welteb8a18b52009-06-10 12:08:54 +0800843 const u_int8_t *lv)
844{
845 u_int8_t in_len = lv[0];
846
Harald Welte03740842009-06-10 23:11:52 +0800847 if (in_len < 1 || in_len < sizeof(ssv->info))
Harald Welteb8a18b52009-06-10 12:08:54 +0800848 return -EINVAL;
849
Harald Welte03740842009-06-10 23:11:52 +0800850 memcpy(ssv->info, lv + 1, in_len);
851 ssv->len = in_len;
Harald Welteb8a18b52009-06-10 12:08:54 +0800852
853 return 0;
854}
855
856/* encode 'more data' */
857static int encode_more(struct msgb *msg)
858{
859 u_int8_t *ie;
860
861 ie = msgb_put(msg, 1);
862 ie[0] = GSM48_IE_MORE_DATA;
863
864 return 0;
865}
866
Harald Welte59b04682009-06-10 05:40:52 +0800867/* Chapter 9.2.14 : Send LOCATION UPDATING REJECT */
868int gsm0408_loc_upd_rej(struct gsm_lchan *lchan, u_int8_t cause)
869{
870 struct msgb *msg = gsm48_msgb_alloc();
871 struct gsm48_hdr *gh;
872
873 msg->lchan = lchan;
874
875 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
876 gh->proto_discr = GSM48_PDISC_MM;
877 gh->msg_type = GSM48_MT_MM_LOC_UPD_REJECT;
878 gh->data[0] = cause;
879
880 DEBUGP(DMM, "-> LOCATION UPDATING REJECT on channel: %d\n", lchan->nr);
881
Harald Welte36fe2e82009-07-23 21:13:03 +0200882 return gsm48_sendmsg(msg, NULL);
Harald Welte59b04682009-06-10 05:40:52 +0800883}
884
885/* Chapter 9.2.13 : Send LOCATION UPDATE ACCEPT */
886int gsm0408_loc_upd_acc(struct gsm_lchan *lchan, u_int32_t tmsi)
887{
888 struct gsm_bts *bts = lchan->ts->trx->bts;
889 struct msgb *msg = gsm48_msgb_alloc();
890 struct gsm48_hdr *gh;
891 struct gsm48_loc_area_id *lai;
892 u_int8_t *mid;
Harald Welte59b04682009-06-10 05:40:52 +0800893
894 msg->lchan = lchan;
895
896 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
897 gh->proto_discr = GSM48_PDISC_MM;
898 gh->msg_type = GSM48_MT_MM_LOC_UPD_ACCEPT;
899
900 lai = (struct gsm48_loc_area_id *) msgb_put(msg, sizeof(*lai));
901 gsm0408_generate_lai(lai, bts->network->country_code,
902 bts->network->network_code, bts->location_area_code);
903
Holger Hans Peter Freyther36897102009-08-01 07:26:59 +0200904 mid = msgb_put(msg, GSM48_MID_TMSI_LEN);
Holger Hans Peter Freyther20a0f322009-08-20 08:41:24 +0200905 gsm48_generate_mid_from_tmsi(mid, tmsi);
Harald Welte59b04682009-06-10 05:40:52 +0800906
907 DEBUGP(DMM, "-> LOCATION UPDATE ACCEPT\n");
908
Harald Welte9ed54ff2009-12-12 21:00:48 +0100909 return gsm48_sendmsg(msg, NULL);
Harald Welte59b04682009-06-10 05:40:52 +0800910}
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;
Harald Welte59936d72009-11-18 20:33:19 +0100974 struct gsm_bts *bts = lchan->ts->trx->bts;
Harald Welte59b04682009-06-10 05:40:52 +0800975
976 release_loc_updating_req(lchan);
Harald Welte59936d72009-11-18 20:33:19 +0100977 gsm0408_loc_upd_rej(lchan, bts->network->reject_cause);
Harald Welte59b04682009-06-10 05:40:52 +0800978 lchan_auto_release(lchan);
979}
980
981static void schedule_reject(struct gsm_lchan *lchan)
982{
983 lchan->loc_operation->updating_timer.cb = loc_upd_rej_cb;
984 lchan->loc_operation->updating_timer.data = lchan;
985 bsc_schedule_timer(&lchan->loc_operation->updating_timer, 5, 0);
986}
987
988static const char *lupd_name(u_int8_t type)
989{
990 switch (type) {
991 case GSM48_LUPD_NORMAL:
992 return "NORMAL";
993 case GSM48_LUPD_PERIODIC:
994 return "PEROIDOC";
995 case GSM48_LUPD_IMSI_ATT:
996 return "IMSI ATTACH";
997 default:
998 return "UNKNOWN";
999 }
1000}
1001
Harald Welte59b04682009-06-10 05:40:52 +08001002/* Chapter 9.2.15: Receive Location Updating Request */
1003static int mm_rx_loc_upd_req(struct msgb *msg)
1004{
1005 struct gsm48_hdr *gh = msgb_l3(msg);
1006 struct gsm48_loc_upd_req *lu;
Harald Welte03740842009-06-10 23:11:52 +08001007 struct gsm_subscriber *subscr = NULL;
Harald Welte59b04682009-06-10 05:40:52 +08001008 struct gsm_lchan *lchan = msg->lchan;
Harald Welte75350412009-07-23 18:46:00 +02001009 struct gsm_bts *bts = lchan->ts->trx->bts;
Harald Welte59b04682009-06-10 05:40:52 +08001010 u_int8_t mi_type;
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +02001011 char mi_string[GSM48_MI_SIZE];
Harald Welte59b04682009-06-10 05:40:52 +08001012 int rc;
1013
1014 lu = (struct gsm48_loc_upd_req *) gh->data;
1015
1016 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
1017
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +02001018 gsm48_mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
Harald Welte59b04682009-06-10 05:40:52 +08001019
Harald Welte79639662009-06-27 02:58:43 +02001020 DEBUGPC(DMM, "mi_type=0x%02x MI(%s) type=%s ", mi_type, mi_string,
Harald Welte59b04682009-06-10 05:40:52 +08001021 lupd_name(lu->type));
1022
1023 /*
1024 * Pseudo Spoof detection: Just drop a second/concurrent
1025 * location updating request.
1026 */
1027 if (lchan->loc_operation) {
Harald Welte79639662009-06-27 02:58:43 +02001028 DEBUGPC(DMM, "ignoring request due an existing one: %p.\n",
Harald Welte59b04682009-06-10 05:40:52 +08001029 lchan->loc_operation);
1030 gsm0408_loc_upd_rej(lchan, GSM48_REJECT_PROTOCOL_ERROR);
1031 return 0;
1032 }
1033
1034 allocate_loc_updating_req(lchan);
1035
1036 switch (mi_type) {
1037 case GSM_MI_TYPE_IMSI:
Harald Welte79639662009-06-27 02:58:43 +02001038 DEBUGPC(DMM, "\n");
Harald Welte59b04682009-06-10 05:40:52 +08001039 /* we always want the IMEI, too */
Harald Welte59b04682009-06-10 05:40:52 +08001040 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMEI);
1041 lchan->loc_operation->waiting_for_imei = 1;
1042
Jan Luebbe9bdbd622009-08-12 10:19:34 +02001043 /* look up subscriber based on IMSI, create if not found */
1044 subscr = subscr_get_by_imsi(bts->network, mi_string);
1045 if (!subscr) {
1046 subscr = db_create_subscriber(bts->network, mi_string);
1047 }
Harald Welte59b04682009-06-10 05:40:52 +08001048 break;
1049 case GSM_MI_TYPE_TMSI:
Harald Welte79639662009-06-27 02:58:43 +02001050 DEBUGPC(DMM, "\n");
Harald Welte59b04682009-06-10 05:40:52 +08001051 /* we always want the IMEI, too */
Harald Welte59b04682009-06-10 05:40:52 +08001052 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMEI);
1053 lchan->loc_operation->waiting_for_imei = 1;
1054
1055 /* look up the subscriber based on TMSI, request IMSI if it fails */
Holger Hans Peter Freythercd8bacf2009-08-19 12:53:57 +02001056 subscr = subscr_get_by_tmsi(bts->network,
1057 tmsi_from_string(mi_string));
Harald Welte59b04682009-06-10 05:40:52 +08001058 if (!subscr) {
1059 /* send IDENTITY REQUEST message to get IMSI */
Harald Welte59b04682009-06-10 05:40:52 +08001060 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMSI);
1061 lchan->loc_operation->waiting_for_imsi = 1;
1062 }
1063 break;
1064 case GSM_MI_TYPE_IMEI:
1065 case GSM_MI_TYPE_IMEISV:
1066 /* no sim card... FIXME: what to do ? */
Harald Welte79639662009-06-27 02:58:43 +02001067 DEBUGPC(DMM, "unimplemented mobile identity type\n");
Harald Welte59b04682009-06-10 05:40:52 +08001068 break;
1069 default:
Harald Welte79639662009-06-27 02:58:43 +02001070 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte59b04682009-06-10 05:40:52 +08001071 break;
1072 }
1073
Harald Welteccd69362009-07-04 10:18:00 +02001074 /* schedule the reject timer */
1075 schedule_reject(lchan);
1076
Harald Welte03740842009-06-10 23:11:52 +08001077 if (!subscr) {
Harald Welte79639662009-06-27 02:58:43 +02001078 DEBUGPC(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte03740842009-06-10 23:11:52 +08001079 /* FIXME: request id? close channel? */
1080 return -EINVAL;
1081 }
1082
Harald Welte59b04682009-06-10 05:40:52 +08001083 lchan->subscr = subscr;
Harald Welte (local)ced09ed2009-08-17 09:39:55 +02001084 lchan->subscr->equipment.classmark1 = lu->classmark1;
Harald Welte59b04682009-06-10 05:40:52 +08001085
Harald Welteccd69362009-07-04 10:18:00 +02001086 /* check if we can let the subscriber into our network immediately
1087 * or if we need to wait for identity responses. */
Harald Welte59b04682009-06-10 05:40:52 +08001088 return gsm0408_authorize(lchan, msg);
1089}
1090
Harald Welte03740842009-06-10 23:11:52 +08001091#if 0
1092static u_int8_t to_bcd8(u_int8_t val)
1093{
1094 return ((val / 10) << 4) | (val % 10);
1095}
1096#endif
1097
Harald Welte59b04682009-06-10 05:40:52 +08001098/* Section 9.2.15a */
1099int gsm48_tx_mm_info(struct gsm_lchan *lchan)
1100{
1101 struct msgb *msg = gsm48_msgb_alloc();
1102 struct gsm48_hdr *gh;
1103 struct gsm_network *net = lchan->ts->trx->bts->network;
1104 u_int8_t *ptr8;
Daniel Willmannc8188202009-08-13 03:42:07 +02001105 int name_len, name_pad;
Harald Welte03740842009-06-10 23:11:52 +08001106#if 0
1107 time_t cur_t;
1108 struct tm* cur_time;
1109 int tz15min;
1110#endif
Harald Welte59b04682009-06-10 05:40:52 +08001111
1112 msg->lchan = lchan;
1113
1114 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1115 gh->proto_discr = GSM48_PDISC_MM;
1116 gh->msg_type = GSM48_MT_MM_INFO;
1117
1118 if (net->name_long) {
Daniel Willmannc8188202009-08-13 03:42:07 +02001119#if 0
Harald Welte59b04682009-06-10 05:40:52 +08001120 name_len = strlen(net->name_long);
1121 /* 10.5.3.5a */
1122 ptr8 = msgb_put(msg, 3);
1123 ptr8[0] = GSM48_IE_NAME_LONG;
1124 ptr8[1] = name_len*2 +1;
1125 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
1126
1127 ptr16 = (u_int16_t *) msgb_put(msg, name_len*2);
1128 for (i = 0; i < name_len; i++)
1129 ptr16[i] = htons(net->name_long[i]);
1130
1131 /* FIXME: Use Cell Broadcast, not UCS-2, since
1132 * UCS-2 is only supported by later revisions of the spec */
Daniel Willmannc8188202009-08-13 03:42:07 +02001133#endif
1134 name_len = (strlen(net->name_long)*7)/8;
1135 name_pad = (8 - strlen(net->name_long)*7)%8;
1136 if (name_pad > 0)
1137 name_len++;
1138 /* 10.5.3.5a */
1139 ptr8 = msgb_put(msg, 3);
1140 ptr8[0] = GSM48_IE_NAME_LONG;
1141 ptr8[1] = name_len +1;
1142 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
1143
1144 ptr8 = msgb_put(msg, name_len);
1145 gsm_7bit_encode(ptr8, net->name_long);
1146
Harald Welte59b04682009-06-10 05:40:52 +08001147 }
1148
1149 if (net->name_short) {
Daniel Willmannc8188202009-08-13 03:42:07 +02001150#if 0
Harald Welte59b04682009-06-10 05:40:52 +08001151 name_len = strlen(net->name_short);
1152 /* 10.5.3.5a */
1153 ptr8 = (u_int8_t *) msgb_put(msg, 3);
Harald Weltef6284712009-07-19 17:51:36 +02001154 ptr8[0] = GSM48_IE_NAME_SHORT;
Harald Welte59b04682009-06-10 05:40:52 +08001155 ptr8[1] = name_len*2 + 1;
1156 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
1157
1158 ptr16 = (u_int16_t *) msgb_put(msg, name_len*2);
1159 for (i = 0; i < name_len; i++)
1160 ptr16[i] = htons(net->name_short[i]);
Daniel Willmannc8188202009-08-13 03:42:07 +02001161#endif
1162 name_len = (strlen(net->name_short)*7)/8;
1163 name_pad = (8 - strlen(net->name_short)*7)%8;
1164 if (name_pad > 0)
1165 name_len++;
1166 /* 10.5.3.5a */
1167 ptr8 = (u_int8_t *) msgb_put(msg, 3);
1168 ptr8[0] = GSM48_IE_NAME_SHORT;
1169 ptr8[1] = name_len +1;
1170 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
1171
1172 ptr8 = msgb_put(msg, name_len);
1173 gsm_7bit_encode(ptr8, net->name_short);
1174
Harald Welte59b04682009-06-10 05:40:52 +08001175 }
1176
1177#if 0
Harald Welte59b04682009-06-10 05:40:52 +08001178 /* Section 10.5.3.9 */
1179 cur_t = time(NULL);
Harald Welte03740842009-06-10 23:11:52 +08001180 cur_time = gmtime(&cur_t);
Harald Welte59b04682009-06-10 05:40:52 +08001181 ptr8 = msgb_put(msg, 8);
1182 ptr8[0] = GSM48_IE_NET_TIME_TZ;
1183 ptr8[1] = to_bcd8(cur_time->tm_year % 100);
1184 ptr8[2] = to_bcd8(cur_time->tm_mon);
1185 ptr8[3] = to_bcd8(cur_time->tm_mday);
1186 ptr8[4] = to_bcd8(cur_time->tm_hour);
1187 ptr8[5] = to_bcd8(cur_time->tm_min);
1188 ptr8[6] = to_bcd8(cur_time->tm_sec);
1189 /* 02.42: coded as BCD encoded signed value in units of 15 minutes */
1190 tz15min = (cur_time->tm_gmtoff)/(60*15);
Harald Welte03740842009-06-10 23:11:52 +08001191 ptr8[7] = to_bcd8(tz15min);
Harald Welte59b04682009-06-10 05:40:52 +08001192 if (tz15min < 0)
Harald Welte03740842009-06-10 23:11:52 +08001193 ptr8[7] |= 0x80;
Harald Welte59b04682009-06-10 05:40:52 +08001194#endif
1195
Daniel Willmannc8188202009-08-13 03:42:07 +02001196 DEBUGP(DMM, "-> MM INFO\n");
1197
Harald Welte36fe2e82009-07-23 21:13:03 +02001198 return gsm48_sendmsg(msg, NULL);
Harald Welte59b04682009-06-10 05:40:52 +08001199}
1200
Harald Welte36970f62009-08-12 22:56:50 +02001201/* Section 9.2.2 */
1202int gsm48_tx_mm_auth_req(struct gsm_lchan *lchan, u_int8_t *rand)
1203{
1204 struct msgb *msg = gsm48_msgb_alloc();
1205 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Sylvain Munaut6de6dc12009-09-27 11:10:17 +02001206 struct gsm48_auth_req *ar = (struct gsm48_auth_req *) msgb_put(msg, sizeof(*ar));
Harald Welte36970f62009-08-12 22:56:50 +02001207
1208 DEBUGP(DMM, "-> AUTH REQ\n");
1209
1210 msg->lchan = lchan;
1211 gh->proto_discr = GSM48_PDISC_MM;
1212 gh->msg_type = GSM48_MT_MM_AUTH_REQ;
1213
Sylvain Munaut6de6dc12009-09-27 11:10:17 +02001214 /* Key Sequence: FIXME fixed to 0 */
1215 ar->key_seq = 0;
1216
Harald Welte36970f62009-08-12 22:56:50 +02001217 /* 16 bytes RAND parameters */
Harald Welte36970f62009-08-12 22:56:50 +02001218 if (rand)
Sylvain Munaut6de6dc12009-09-27 11:10:17 +02001219 memcpy(ar->rand, rand, 16);
Harald Welte36970f62009-08-12 22:56:50 +02001220
1221 return gsm48_sendmsg(msg, NULL);
1222}
1223
1224/* Section 9.2.1 */
1225int gsm48_tx_mm_auth_rej(struct gsm_lchan *lchan)
1226{
1227 DEBUGP(DMM, "-> AUTH REJECT\n");
1228 return gsm48_tx_simple(lchan, GSM48_PDISC_MM, GSM48_MT_MM_AUTH_REJ);
1229}
1230
Harald Welte59b04682009-06-10 05:40:52 +08001231static int gsm48_tx_mm_serv_ack(struct gsm_lchan *lchan)
1232{
1233 DEBUGP(DMM, "-> CM SERVICE ACK\n");
1234 return gsm48_tx_simple(lchan, GSM48_PDISC_MM, GSM48_MT_MM_CM_SERV_ACC);
1235}
1236
1237/* 9.2.6 CM service reject */
1238static int gsm48_tx_mm_serv_rej(struct gsm_lchan *lchan,
1239 enum gsm48_reject_value value)
1240{
1241 struct msgb *msg = gsm48_msgb_alloc();
1242 struct gsm48_hdr *gh;
1243
1244 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
1245
1246 msg->lchan = lchan;
1247 use_lchan(lchan);
1248
1249 gh->proto_discr = GSM48_PDISC_MM;
1250 gh->msg_type = GSM48_MT_MM_CM_SERV_REJ;
1251 gh->data[0] = value;
1252 DEBUGP(DMM, "-> CM SERVICE Reject cause: %d\n", value);
1253
Harald Welte36fe2e82009-07-23 21:13:03 +02001254 return gsm48_sendmsg(msg, NULL);
Harald Welte59b04682009-06-10 05:40:52 +08001255}
1256
Harald Welte59b04682009-06-10 05:40:52 +08001257/*
1258 * Handle CM Service Requests
1259 * a) Verify that the packet is long enough to contain the information
1260 * we require otherwsie reject with INCORRECT_MESSAGE
1261 * b) Try to parse the TMSI. If we do not have one reject
1262 * c) Check that we know the subscriber with the TMSI otherwise reject
1263 * with a HLR cause
1264 * d) Set the subscriber on the gsm_lchan and accept
1265 */
1266static int gsm48_rx_mm_serv_req(struct msgb *msg)
1267{
1268 u_int8_t mi_type;
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +02001269 char mi_string[GSM48_MI_SIZE];
Harald Welte59b04682009-06-10 05:40:52 +08001270
Harald Welte75350412009-07-23 18:46:00 +02001271 struct gsm_bts *bts = msg->lchan->ts->trx->bts;
Harald Welte59b04682009-06-10 05:40:52 +08001272 struct gsm_subscriber *subscr;
1273 struct gsm48_hdr *gh = msgb_l3(msg);
1274 struct gsm48_service_request *req =
1275 (struct gsm48_service_request *)gh->data;
1276 /* unfortunately in Phase1 the classmar2 length is variable */
1277 u_int8_t classmark2_len = gh->data[1];
1278 u_int8_t *classmark2 = gh->data+2;
1279 u_int8_t mi_len = *(classmark2 + classmark2_len);
1280 u_int8_t *mi = (classmark2 + classmark2_len + 1);
1281
1282 DEBUGP(DMM, "<- CM SERVICE REQUEST ");
1283 if (msg->data_len < sizeof(struct gsm48_service_request*)) {
1284 DEBUGPC(DMM, "wrong sized message\n");
1285 return gsm48_tx_mm_serv_rej(msg->lchan,
1286 GSM48_REJECT_INCORRECT_MESSAGE);
1287 }
1288
1289 if (msg->data_len < req->mi_len + 6) {
1290 DEBUGPC(DMM, "does not fit in packet\n");
1291 return gsm48_tx_mm_serv_rej(msg->lchan,
1292 GSM48_REJECT_INCORRECT_MESSAGE);
1293 }
1294
1295 mi_type = mi[0] & GSM_MI_TYPE_MASK;
1296 if (mi_type != GSM_MI_TYPE_TMSI) {
1297 DEBUGPC(DMM, "mi_type is not TMSI: %d\n", mi_type);
1298 return gsm48_tx_mm_serv_rej(msg->lchan,
1299 GSM48_REJECT_INCORRECT_MESSAGE);
1300 }
1301
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +02001302 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
Harald Welte59b04682009-06-10 05:40:52 +08001303 DEBUGPC(DMM, "serv_type=0x%02x mi_type=0x%02x M(%s)\n",
1304 req->cm_service_type, mi_type, mi_string);
1305
Harald Welte583ceaf2009-08-10 10:12:45 +02001306 if (is_siemens_bts(bts))
1307 send_siemens_mrpci(msg->lchan, classmark2-1);
1308
Holger Hans Peter Freythercd8bacf2009-08-19 12:53:57 +02001309 subscr = subscr_get_by_tmsi(bts->network,
1310 tmsi_from_string(mi_string));
Harald Welte59b04682009-06-10 05:40:52 +08001311
1312 /* FIXME: if we don't know the TMSI, inquire abit IMSI and allocate new TMSI */
1313 if (!subscr)
1314 return gsm48_tx_mm_serv_rej(msg->lchan,
1315 GSM48_REJECT_IMSI_UNKNOWN_IN_HLR);
1316
1317 if (!msg->lchan->subscr)
1318 msg->lchan->subscr = subscr;
1319 else if (msg->lchan->subscr != subscr) {
1320 DEBUGP(DMM, "<- CM Channel already owned by someone else?\n");
1321 subscr_put(subscr);
1322 }
1323
Harald Weltef6845a72009-07-05 14:08:13 +02001324 subscr->equipment.classmark2_len = classmark2_len;
1325 memcpy(subscr->equipment.classmark2, classmark2, classmark2_len);
1326 db_sync_equipment(&subscr->equipment);
Harald Welte59b04682009-06-10 05:40:52 +08001327
1328 return gsm48_tx_mm_serv_ack(msg->lchan);
1329}
1330
1331static int gsm48_rx_mm_imsi_detach_ind(struct msgb *msg)
1332{
Harald Welte75350412009-07-23 18:46:00 +02001333 struct gsm_bts *bts = msg->lchan->ts->trx->bts;
Harald Welte59b04682009-06-10 05:40:52 +08001334 struct gsm48_hdr *gh = msgb_l3(msg);
1335 struct gsm48_imsi_detach_ind *idi =
1336 (struct gsm48_imsi_detach_ind *) gh->data;
1337 u_int8_t mi_type = idi->mi[0] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +02001338 char mi_string[GSM48_MI_SIZE];
Harald Welte03740842009-06-10 23:11:52 +08001339 struct gsm_subscriber *subscr = NULL;
Harald Welte59b04682009-06-10 05:40:52 +08001340
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +02001341 gsm48_mi_to_string(mi_string, sizeof(mi_string), idi->mi, idi->mi_len);
Harald Welte59b04682009-06-10 05:40:52 +08001342 DEBUGP(DMM, "IMSI DETACH INDICATION: mi_type=0x%02x MI(%s): ",
1343 mi_type, mi_string);
1344
1345 switch (mi_type) {
1346 case GSM_MI_TYPE_TMSI:
Holger Hans Peter Freythercd8bacf2009-08-19 12:53:57 +02001347 subscr = subscr_get_by_tmsi(bts->network,
1348 tmsi_from_string(mi_string));
Harald Welte59b04682009-06-10 05:40:52 +08001349 break;
1350 case GSM_MI_TYPE_IMSI:
Harald Welte75350412009-07-23 18:46:00 +02001351 subscr = subscr_get_by_imsi(bts->network, mi_string);
Harald Welte59b04682009-06-10 05:40:52 +08001352 break;
1353 case GSM_MI_TYPE_IMEI:
1354 case GSM_MI_TYPE_IMEISV:
1355 /* no sim card... FIXME: what to do ? */
1356 DEBUGPC(DMM, "unimplemented mobile identity type\n");
1357 break;
1358 default:
1359 DEBUGPC(DMM, "unknown mobile identity type\n");
1360 break;
1361 }
1362
1363 if (subscr) {
1364 subscr_update(subscr, msg->trx->bts,
1365 GSM_SUBSCRIBER_UPDATE_DETACHED);
1366 DEBUGP(DMM, "Subscriber: %s\n",
1367 subscr->name ? subscr->name : subscr->imsi);
Harald Welte (local)ced09ed2009-08-17 09:39:55 +02001368
1369 subscr->equipment.classmark1 = idi->classmark1;
1370 db_sync_equipment(&subscr->equipment);
1371
Harald Welte59b04682009-06-10 05:40:52 +08001372 subscr_put(subscr);
1373 } else
1374 DEBUGP(DMM, "Unknown Subscriber ?!?\n");
1375
Harald Welte9ed54ff2009-12-12 21:00:48 +01001376 /* subscriber is detached: should we release lchan? */
1377
Harald Welte59b04682009-06-10 05:40:52 +08001378 return 0;
1379}
1380
1381static int gsm48_rx_mm_status(struct msgb *msg)
1382{
1383 struct gsm48_hdr *gh = msgb_l3(msg);
1384
1385 DEBUGP(DMM, "MM STATUS (reject cause 0x%02x)\n", gh->data[0]);
1386
1387 return 0;
1388}
1389
1390/* Receive a GSM 04.08 Mobility Management (MM) message */
1391static int gsm0408_rcv_mm(struct msgb *msg)
1392{
1393 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte03740842009-06-10 23:11:52 +08001394 int rc = 0;
Harald Welte59b04682009-06-10 05:40:52 +08001395
1396 switch (gh->msg_type & 0xbf) {
1397 case GSM48_MT_MM_LOC_UPD_REQUEST:
Harald Welte79639662009-06-27 02:58:43 +02001398 DEBUGP(DMM, "LOCATION UPDATING REQUEST: ");
Harald Welte59b04682009-06-10 05:40:52 +08001399 rc = mm_rx_loc_upd_req(msg);
1400 break;
1401 case GSM48_MT_MM_ID_RESP:
1402 rc = mm_rx_id_resp(msg);
1403 break;
1404 case GSM48_MT_MM_CM_SERV_REQ:
1405 rc = gsm48_rx_mm_serv_req(msg);
1406 break;
1407 case GSM48_MT_MM_STATUS:
1408 rc = gsm48_rx_mm_status(msg);
1409 break;
1410 case GSM48_MT_MM_TMSI_REALL_COMPL:
1411 DEBUGP(DMM, "TMSI Reallocation Completed. Subscriber: %s\n",
1412 msg->lchan->subscr ?
1413 msg->lchan->subscr->imsi :
1414 "unknown subscriber");
1415 break;
1416 case GSM48_MT_MM_IMSI_DETACH_IND:
1417 rc = gsm48_rx_mm_imsi_detach_ind(msg);
1418 break;
1419 case GSM48_MT_MM_CM_REEST_REQ:
1420 DEBUGP(DMM, "CM REESTABLISH REQUEST: Not implemented\n");
1421 break;
1422 case GSM48_MT_MM_AUTH_RESP:
1423 DEBUGP(DMM, "AUTHENTICATION RESPONSE: Not implemented\n");
1424 break;
1425 default:
1426 fprintf(stderr, "Unknown GSM 04.08 MM msg type 0x%02x\n",
1427 gh->msg_type);
1428 break;
1429 }
1430
1431 return rc;
1432}
1433
1434/* Receive a PAGING RESPONSE message from the MS */
Holger Hans Peter Freytherf10170e2009-10-23 13:48:54 +02001435static int gsm48_rx_rr_pag_resp(struct msgb *msg)
Harald Welte59b04682009-06-10 05:40:52 +08001436{
Harald Welte75350412009-07-23 18:46:00 +02001437 struct gsm_bts *bts = msg->lchan->ts->trx->bts;
Harald Welte59b04682009-06-10 05:40:52 +08001438 struct gsm48_hdr *gh = msgb_l3(msg);
1439 u_int8_t *classmark2_lv = gh->data + 1;
Holger Hans Peter Freyther1e43b8d2009-08-21 05:18:21 +02001440 u_int8_t mi_type;
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +02001441 char mi_string[GSM48_MI_SIZE];
Harald Welte03740842009-06-10 23:11:52 +08001442 struct gsm_subscriber *subscr = NULL;
Harald Welte59b04682009-06-10 05:40:52 +08001443 int rc = 0;
1444
Holger Hans Peter Freyther1e43b8d2009-08-21 05:18:21 +02001445 gsm48_paging_extract_mi(msg, mi_string, &mi_type);
Harald Welte59b04682009-06-10 05:40:52 +08001446 DEBUGP(DRR, "PAGING RESPONSE: mi_type=0x%02x MI(%s)\n",
1447 mi_type, mi_string);
Harald Welte583ceaf2009-08-10 10:12:45 +02001448
Harald Welte59b04682009-06-10 05:40:52 +08001449 switch (mi_type) {
1450 case GSM_MI_TYPE_TMSI:
Holger Hans Peter Freythercd8bacf2009-08-19 12:53:57 +02001451 subscr = subscr_get_by_tmsi(bts->network,
1452 tmsi_from_string(mi_string));
Harald Welte59b04682009-06-10 05:40:52 +08001453 break;
1454 case GSM_MI_TYPE_IMSI:
Harald Welte75350412009-07-23 18:46:00 +02001455 subscr = subscr_get_by_imsi(bts->network, mi_string);
Harald Welte59b04682009-06-10 05:40:52 +08001456 break;
1457 }
1458
1459 if (!subscr) {
1460 DEBUGP(DRR, "<- Can't find any subscriber for this ID\n");
1461 /* FIXME: request id? close channel? */
1462 return -EINVAL;
1463 }
1464 DEBUGP(DRR, "<- Channel was requested by %s\n",
Harald Welte68b7df22009-08-08 16:03:15 +02001465 subscr->name && strlen(subscr->name) ? subscr->name : subscr->imsi);
Harald Welte59b04682009-06-10 05:40:52 +08001466
Harald Weltef6845a72009-07-05 14:08:13 +02001467 subscr->equipment.classmark2_len = *classmark2_lv;
1468 memcpy(subscr->equipment.classmark2, classmark2_lv+1, *classmark2_lv);
1469 db_sync_equipment(&subscr->equipment);
Harald Welte59b04682009-06-10 05:40:52 +08001470
Holger Hans Peter Freyther1e43b8d2009-08-21 05:18:21 +02001471 rc = gsm48_handle_paging_resp(msg, subscr);
Harald Welte59b04682009-06-10 05:40:52 +08001472 return rc;
1473}
1474
1475static int gsm48_rx_rr_classmark(struct msgb *msg)
1476{
1477 struct gsm48_hdr *gh = msgb_l3(msg);
1478 struct gsm_subscriber *subscr = msg->lchan->subscr;
1479 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1480 u_int8_t cm2_len, cm3_len = 0;
1481 u_int8_t *cm2, *cm3 = NULL;
1482
1483 DEBUGP(DRR, "CLASSMARK CHANGE ");
1484
1485 /* classmark 2 */
1486 cm2_len = gh->data[0];
1487 cm2 = &gh->data[1];
1488 DEBUGPC(DRR, "CM2(len=%u) ", cm2_len);
1489
1490 if (payload_len > cm2_len + 1) {
1491 /* we must have a classmark3 */
1492 if (gh->data[cm2_len+1] != 0x20) {
1493 DEBUGPC(DRR, "ERR CM3 TAG\n");
1494 return -EINVAL;
1495 }
1496 if (cm2_len > 3) {
1497 DEBUGPC(DRR, "CM2 too long!\n");
1498 return -EINVAL;
1499 }
1500
1501 cm3_len = gh->data[cm2_len+2];
1502 cm3 = &gh->data[cm2_len+3];
1503 if (cm3_len > 14) {
1504 DEBUGPC(DRR, "CM3 len %u too long!\n", cm3_len);
1505 return -EINVAL;
1506 }
1507 DEBUGPC(DRR, "CM3(len=%u)\n", cm3_len);
1508 }
1509 if (subscr) {
Harald Weltef6845a72009-07-05 14:08:13 +02001510 subscr->equipment.classmark2_len = cm2_len;
1511 memcpy(subscr->equipment.classmark2, cm2, cm2_len);
Harald Welte59b04682009-06-10 05:40:52 +08001512 if (cm3) {
Harald Weltef6845a72009-07-05 14:08:13 +02001513 subscr->equipment.classmark3_len = cm3_len;
1514 memcpy(subscr->equipment.classmark3, cm3, cm3_len);
Harald Welte59b04682009-06-10 05:40:52 +08001515 }
Harald Weltef6845a72009-07-05 14:08:13 +02001516 db_sync_equipment(&subscr->equipment);
Harald Welte59b04682009-06-10 05:40:52 +08001517 }
1518
Harald Welte59b04682009-06-10 05:40:52 +08001519 return 0;
1520}
1521
1522static int gsm48_rx_rr_status(struct msgb *msg)
1523{
1524 struct gsm48_hdr *gh = msgb_l3(msg);
1525
1526 DEBUGP(DRR, "STATUS rr_cause = %s\n",
1527 rr_cause_name(gh->data[0]));
1528
1529 return 0;
1530}
1531
1532static int gsm48_rx_rr_meas_rep(struct msgb *msg)
1533{
Harald Welte59b04682009-06-10 05:40:52 +08001534 static struct gsm_meas_rep meas_rep;
1535
Harald Weltec20bd1d2009-11-29 19:07:28 +01001536 /* This shouldn't actually end up here, as RSL treats
1537 * L3 Info of 08.58 MEASUREMENT REPORT different by calling
1538 * directly into gsm48_parse_meas_rep */
1539 DEBUGP(DMEAS, "DIRECT GSM48 MEASUREMENT REPORT ?!? ");
1540 gsm48_parse_meas_rep(&meas_rep, msg);
Harald Welte59b04682009-06-10 05:40:52 +08001541
1542 return 0;
1543}
1544
Harald Welte (local)b5103922009-08-15 23:32:44 +02001545static int gsm48_rx_rr_app_info(struct msgb *msg)
1546{
1547 struct gsm48_hdr *gh = msgb_l3(msg);
1548 u_int8_t apdu_id_flags;
1549 u_int8_t apdu_len;
1550 u_int8_t *apdu_data;
1551
1552 apdu_id_flags = gh->data[0];
1553 apdu_len = gh->data[1];
1554 apdu_data = gh->data+2;
1555
1556 DEBUGP(DNM, "RX APPLICATION INFO id/flags=0x%02x apdu_len=%u apdu=%s",
1557 apdu_id_flags, apdu_len, hexdump(apdu_data, apdu_len));
1558
Harald Welte (local)bd72aa22009-08-16 10:40:10 +02001559 return db_apdu_blob_store(msg->lchan->subscr, apdu_id_flags, apdu_len, apdu_data);
Harald Welte (local)b5103922009-08-15 23:32:44 +02001560}
1561
Harald Welte6720a432009-11-29 22:45:52 +01001562/* Chapter 9.1.16 Handover complete */
1563static gsm48_rx_rr_ho_compl(struct msgb *msg)
1564{
1565 struct gsm48_hdr *gh = msgb_l3(msg);
1566
1567 DEBUGP(DRR, "HANDOVER COMPLETE cause = %s\n",
1568 rr_cause_name(gh->data[0]));
1569
1570 dispatch_signal(SS_LCHAN, S_LCHAN_HANDOVER_COMPL, msg->lchan);
1571 /* FIXME: release old channel */
1572
1573 return 0;
1574}
1575
1576/* Chapter 9.1.17 Handover Failure */
1577static gsm48_rx_rr_ho_fail(struct msgb *msg)
1578{
1579 struct gsm48_hdr *gh = msgb_l3(msg);
1580
1581 DEBUGP(DRR, "HANDOVER FAILED cause = %s\n",
1582 rr_cause_name(gh->data[0]));
1583
1584 dispatch_signal(SS_LCHAN, S_LCHAN_HANDOVER_FAIL, msg->lchan);
1585 /* FIXME: release allocated new channel */
1586
1587 return 0;
1588}
Harald Welte (local)b5103922009-08-15 23:32:44 +02001589
Harald Welte59b04682009-06-10 05:40:52 +08001590/* Receive a GSM 04.08 Radio Resource (RR) message */
1591static int gsm0408_rcv_rr(struct msgb *msg)
1592{
1593 struct gsm48_hdr *gh = msgb_l3(msg);
1594 int rc = 0;
1595
1596 switch (gh->msg_type) {
1597 case GSM48_MT_RR_CLSM_CHG:
1598 rc = gsm48_rx_rr_classmark(msg);
1599 break;
1600 case GSM48_MT_RR_GPRS_SUSP_REQ:
1601 DEBUGP(DRR, "GRPS SUSPEND REQUEST\n");
1602 break;
1603 case GSM48_MT_RR_PAG_RESP:
Holger Hans Peter Freytherf10170e2009-10-23 13:48:54 +02001604 rc = gsm48_rx_rr_pag_resp(msg);
Harald Welte59b04682009-06-10 05:40:52 +08001605 break;
1606 case GSM48_MT_RR_CHAN_MODE_MODIF_ACK:
Holger Hans Peter Freyther9e6d6ce2009-10-22 15:23:11 +02001607 rc = gsm48_rx_rr_modif_ack(msg);
Harald Welte59b04682009-06-10 05:40:52 +08001608 break;
1609 case GSM48_MT_RR_STATUS:
1610 rc = gsm48_rx_rr_status(msg);
1611 break;
1612 case GSM48_MT_RR_MEAS_REP:
1613 rc = gsm48_rx_rr_meas_rep(msg);
1614 break;
Harald Welte (local)b5103922009-08-15 23:32:44 +02001615 case GSM48_MT_RR_APP_INFO:
1616 rc = gsm48_rx_rr_app_info(msg);
1617 break;
Harald Welted2dd9de2009-08-30 15:37:11 +09001618 case GSM48_MT_RR_CIPH_M_COMPL:
1619 DEBUGP(DRR, "CIPHERING MODE COMPLETE\n");
1620 /* FIXME: check for MI (if any) */
1621 break;
Harald Welte323f4e02009-11-29 20:02:20 +01001622 case GSM48_MT_RR_HANDO_COMPL:
Harald Welte6720a432009-11-29 22:45:52 +01001623 rc = gsm48_rx_rr_ho_compl(msg);
Harald Welte323f4e02009-11-29 20:02:20 +01001624 break;
1625 case GSM48_MT_RR_HANDO_FAIL:
Harald Welte6720a432009-11-29 22:45:52 +01001626 rc = gsm48_rx_rr_ho_fail(msg);
Harald Welte323f4e02009-11-29 20:02:20 +01001627 break;
Harald Welte59b04682009-06-10 05:40:52 +08001628 default:
1629 fprintf(stderr, "Unimplemented GSM 04.08 RR msg type 0x%02x\n",
1630 gh->msg_type);
1631 break;
1632 }
1633
1634 return rc;
1635}
1636
Harald Welte (local)b5103922009-08-15 23:32:44 +02001637int gsm48_send_rr_app_info(struct gsm_lchan *lchan, u_int8_t apdu_id,
Holger Hans Peter Freytherd70da872009-10-22 15:42:19 +02001638 u_int8_t apdu_len, const u_int8_t *apdu)
Harald Welte (local)b5103922009-08-15 23:32:44 +02001639{
1640 struct msgb *msg = gsm48_msgb_alloc();
1641 struct gsm48_hdr *gh;
1642
1643 msg->lchan = lchan;
1644
1645 DEBUGP(DRR, "TX APPLICATION INFO id=0x%02x, len=%u\n",
1646 apdu_id, apdu_len);
1647
1648 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 2 + apdu_len);
1649 gh->proto_discr = GSM48_PDISC_RR;
1650 gh->msg_type = GSM48_MT_RR_APP_INFO;
1651 gh->data[0] = apdu_id;
1652 gh->data[1] = apdu_len;
1653 memcpy(gh->data+2, apdu, apdu_len);
1654
1655 return gsm48_sendmsg(msg, NULL);
1656}
1657
Harald Welte59b04682009-06-10 05:40:52 +08001658/* Call Control */
1659
1660/* The entire call control code is written in accordance with Figure 7.10c
1661 * for 'very early assignment', i.e. we allocate a TCH/F during IMMEDIATE
1662 * ASSIGN, then first use that TCH/F for signalling and later MODE MODIFY
1663 * it for voice */
1664
Harald Welte03740842009-06-10 23:11:52 +08001665static void new_cc_state(struct gsm_trans *trans, int state)
1666{
1667 if (state > 31 || state < 0)
1668 return;
1669
1670 DEBUGP(DCC, "new state %s -> %s\n",
Harald Weltec2189a62009-07-23 18:56:43 +02001671 cc_state_names[trans->cc.state], cc_state_names[state]);
Harald Welte03740842009-06-10 23:11:52 +08001672
Harald Weltec2189a62009-07-23 18:56:43 +02001673 trans->cc.state = state;
Harald Welte03740842009-06-10 23:11:52 +08001674}
1675
1676static int gsm48_cc_tx_status(struct gsm_trans *trans, void *arg)
Harald Welte59b04682009-06-10 05:40:52 +08001677{
1678 struct msgb *msg = gsm48_msgb_alloc();
1679 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1680 u_int8_t *cause, *call_state;
1681
Harald Welte59b04682009-06-10 05:40:52 +08001682 gh->msg_type = GSM48_MT_CC_STATUS;
1683
1684 cause = msgb_put(msg, 3);
1685 cause[0] = 2;
1686 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
1687 cause[2] = 0x80 | 30; /* response to status inquiry */
1688
1689 call_state = msgb_put(msg, 1);
1690 call_state[0] = 0xc0 | 0x00;
1691
Harald Welte36fe2e82009-07-23 21:13:03 +02001692 return gsm48_sendmsg(msg, trans);
Harald Welte59b04682009-06-10 05:40:52 +08001693}
1694
1695static int gsm48_tx_simple(struct gsm_lchan *lchan,
1696 u_int8_t pdisc, u_int8_t msg_type)
1697{
1698 struct msgb *msg = gsm48_msgb_alloc();
1699 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1700
1701 msg->lchan = lchan;
1702
1703 gh->proto_discr = pdisc;
1704 gh->msg_type = msg_type;
1705
Harald Welte36fe2e82009-07-23 21:13:03 +02001706 return gsm48_sendmsg(msg, NULL);
Harald Welte59b04682009-06-10 05:40:52 +08001707}
1708
Harald Welte03740842009-06-10 23:11:52 +08001709static void gsm48_stop_cc_timer(struct gsm_trans *trans)
1710{
Harald Weltec2189a62009-07-23 18:56:43 +02001711 if (bsc_timer_pending(&trans->cc.timer)) {
1712 DEBUGP(DCC, "stopping pending timer T%x\n", trans->cc.Tcurrent);
1713 bsc_del_timer(&trans->cc.timer);
1714 trans->cc.Tcurrent = 0;
Harald Welte03740842009-06-10 23:11:52 +08001715 }
1716}
1717
1718static int mncc_recvmsg(struct gsm_network *net, struct gsm_trans *trans,
1719 int msg_type, struct gsm_mncc *mncc)
1720{
1721 struct msgb *msg;
1722
1723 if (trans)
1724 if (trans->lchan)
Harald Welte4861c822009-07-23 21:21:14 +02001725 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte03740842009-06-10 23:11:52 +08001726 "Sending '%s' to MNCC.\n",
1727 trans->lchan->ts->trx->bts->nr,
1728 trans->lchan->ts->trx->nr,
1729 trans->lchan->ts->nr, trans->transaction_id,
1730 (trans->subscr)?(trans->subscr->extension):"-",
1731 get_mncc_name(msg_type));
1732 else
1733 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
1734 "Sending '%s' to MNCC.\n",
1735 (trans->subscr)?(trans->subscr->extension):"-",
1736 get_mncc_name(msg_type));
1737 else
1738 DEBUGP(DCC, "(bts - trx - ts - ti -- sub -) "
1739 "Sending '%s' to MNCC.\n", get_mncc_name(msg_type));
1740
1741 mncc->msg_type = msg_type;
1742
Harald Welte9cfc9352009-06-26 19:39:35 +02001743 msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
Harald Welte03740842009-06-10 23:11:52 +08001744 if (!msg)
1745 return -ENOMEM;
1746 memcpy(msg->data, mncc, sizeof(struct gsm_mncc));
1747 msgb_enqueue(&net->upqueue, msg);
1748
1749 return 0;
1750}
1751
1752int mncc_release_ind(struct gsm_network *net, struct gsm_trans *trans,
1753 u_int32_t callref, int location, int value)
1754{
1755 struct gsm_mncc rel;
1756
Harald Weltecb0595f2009-06-12 01:54:08 +08001757 memset(&rel, 0, sizeof(rel));
Harald Welte03740842009-06-10 23:11:52 +08001758 rel.callref = callref;
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08001759 mncc_set_cause(&rel, location, value);
Harald Welte03740842009-06-10 23:11:52 +08001760 return mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
1761}
1762
Harald Weltec2189a62009-07-23 18:56:43 +02001763/* Call Control Specific transaction release.
1764 * gets called by trans_free, DO NOT CALL YOURSELF! */
1765void _gsm48_cc_trans_free(struct gsm_trans *trans)
Harald Welte03740842009-06-10 23:11:52 +08001766{
Harald Welte03740842009-06-10 23:11:52 +08001767 gsm48_stop_cc_timer(trans);
1768
1769 /* send release to L4, if callref still exists */
1770 if (trans->callref) {
1771 /* Ressource unavailable */
Harald Welte0abe5a62009-07-23 19:06:52 +02001772 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08001773 GSM48_CAUSE_LOC_PRN_S_LU,
1774 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte03740842009-06-10 23:11:52 +08001775 }
Harald Weltec2189a62009-07-23 18:56:43 +02001776 if (trans->cc.state != GSM_CSTATE_NULL)
Harald Welte03740842009-06-10 23:11:52 +08001777 new_cc_state(trans, GSM_CSTATE_NULL);
Harald Weltec2189a62009-07-23 18:56:43 +02001778 if (trans->lchan)
1779 trau_mux_unmap(&trans->lchan->ts->e1_link, trans->callref);
Harald Welte03740842009-06-10 23:11:52 +08001780}
1781
1782static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg);
1783
Harald Welte59b04682009-06-10 05:40:52 +08001784/* call-back from paging the B-end of the connection */
1785static int setup_trig_pag_evt(unsigned int hooknum, unsigned int event,
1786 struct msgb *msg, void *_lchan, void *param)
1787{
1788 struct gsm_lchan *lchan = _lchan;
Harald Welte03740842009-06-10 23:11:52 +08001789 struct gsm_subscriber *subscr = param;
1790 struct gsm_trans *transt, *tmp;
1791 struct gsm_network *net;
Harald Welte1ff81b52009-06-26 20:17:06 +02001792
Harald Welte59b04682009-06-10 05:40:52 +08001793 if (hooknum != GSM_HOOK_RR_PAGING)
1794 return -EINVAL;
Harald Welte03740842009-06-10 23:11:52 +08001795
1796 if (!subscr)
1797 return -EINVAL;
1798 net = subscr->net;
1799 if (!net) {
1800 DEBUGP(DCC, "Error Network not set!\n");
1801 return -EINVAL;
Harald Welte59b04682009-06-10 05:40:52 +08001802 }
1803
Harald Welte03740842009-06-10 23:11:52 +08001804 /* check all tranactions (without lchan) for subscriber */
1805 llist_for_each_entry_safe(transt, tmp, &net->trans_list, entry) {
1806 if (transt->subscr != subscr || transt->lchan)
1807 continue;
1808 switch (event) {
1809 case GSM_PAGING_SUCCEEDED:
1810 if (!lchan) // paranoid
1811 break;
1812 DEBUGP(DCC, "Paging subscr %s succeeded!\n",
1813 subscr->extension);
1814 /* Assign lchan */
1815 if (!transt->lchan) {
1816 transt->lchan = lchan;
1817 use_lchan(lchan);
1818 }
1819 /* send SETUP request to called party */
Harald Weltec2189a62009-07-23 18:56:43 +02001820 gsm48_cc_tx_setup(transt, &transt->cc.msg);
Harald Welte03740842009-06-10 23:11:52 +08001821 break;
1822 case GSM_PAGING_EXPIRED:
1823 DEBUGP(DCC, "Paging subscr %s expired!\n",
1824 subscr->extension);
1825 /* Temporarily out of order */
Harald Welte0abe5a62009-07-23 19:06:52 +02001826 mncc_release_ind(transt->subscr->net, transt,
1827 transt->callref,
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08001828 GSM48_CAUSE_LOC_PRN_S_LU,
1829 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte03740842009-06-10 23:11:52 +08001830 transt->callref = 0;
Harald Weltec2189a62009-07-23 18:56:43 +02001831 trans_free(transt);
Harald Welte03740842009-06-10 23:11:52 +08001832 break;
1833 }
1834 }
Harald Welte59b04682009-06-10 05:40:52 +08001835 return 0;
1836}
1837
Harald Welte3c062072009-07-28 18:25:29 +02001838/* some other part of the code sends us a signal */
1839static int handle_abisip_signal(unsigned int subsys, unsigned int signal,
1840 void *handler_data, void *signal_data)
1841{
1842 struct gsm_lchan *lchan = signal_data;
Harald Welte3c062072009-07-28 18:25:29 +02001843 int rc;
1844
1845 if (subsys != SS_ABISIP)
1846 return 0;
1847
1848 /* in case we use direct BTS-to-BTS RTP */
1849 if (ipacc_rtp_direct)
1850 return 0;
1851
Harald Welte3c062072009-07-28 18:25:29 +02001852 switch (signal) {
Holger Hans Peter Freyther5ea7ea62009-11-18 21:06:12 +01001853 case S_ABISIP_CRCX_ACK:
Harald Welte3c062072009-07-28 18:25:29 +02001854 /* the BTS has successfully bound a TCH to a local ip/port,
1855 * which means we can connect our UDP socket to it */
Harald Welte87504212009-12-02 01:56:49 +05301856 if (lchan->abis_ip.rtp_socket) {
1857 rtp_socket_free(lchan->abis_ip.rtp_socket);
1858 lchan->abis_ip.rtp_socket = NULL;
Harald Welte3c062072009-07-28 18:25:29 +02001859 }
1860
Harald Welte87504212009-12-02 01:56:49 +05301861 lchan->abis_ip.rtp_socket = rtp_socket_create();
1862 if (!lchan->abis_ip.rtp_socket)
Harald Welte3c062072009-07-28 18:25:29 +02001863 goto out_err;
1864
Harald Welte87504212009-12-02 01:56:49 +05301865 rc = rtp_socket_connect(lchan->abis_ip.rtp_socket,
1866 lchan->abis_ip.bound_ip,
1867 lchan->abis_ip.bound_port);
Harald Welte3c062072009-07-28 18:25:29 +02001868 if (rc < 0)
1869 goto out_err;
1870 break;
Holger Hans Peter Freyther5ea7ea62009-11-18 21:06:12 +01001871 case S_ABISIP_DLCX_IND:
Harald Welte3c062072009-07-28 18:25:29 +02001872 /* the BTS tells us a RTP stream has been disconnected */
Harald Welte87504212009-12-02 01:56:49 +05301873 if (lchan->abis_ip.rtp_socket) {
1874 rtp_socket_free(lchan->abis_ip.rtp_socket);
1875 lchan->abis_ip.rtp_socket = NULL;
Harald Welte3c062072009-07-28 18:25:29 +02001876 }
1877 break;
1878 }
1879
1880 return 0;
1881out_err:
1882 /* FIXME: do something */
1883 return 0;
1884}
1885
1886/* bind rtp proxy to local IP/port and tell BTS to connect to it */
1887static int ipacc_connect_proxy_bind(struct gsm_lchan *lchan)
1888{
Harald Welte87504212009-12-02 01:56:49 +05301889 struct rtp_socket *rs = lchan->abis_ip.rtp_socket;
Harald Welte3c062072009-07-28 18:25:29 +02001890 int rc;
1891
Holger Hans Peter Freyther5ea7ea62009-11-18 21:06:12 +01001892 rc = rsl_ipacc_mdcx(lchan, ntohl(rs->rtp.sin_local.sin_addr.s_addr),
Harald Welte3c062072009-07-28 18:25:29 +02001893 ntohs(rs->rtp.sin_local.sin_port),
Harald Welte87504212009-12-02 01:56:49 +05301894 lchan->abis_ip.conn_id,
Harald Welte3c062072009-07-28 18:25:29 +02001895 /* FIXME: use RTP payload of bound socket, not BTS*/
Harald Welte87504212009-12-02 01:56:49 +05301896 lchan->abis_ip.rtp_payload2);
Harald Welte3c062072009-07-28 18:25:29 +02001897
1898 return rc;
1899}
1900
Harald Welte59b04682009-06-10 05:40:52 +08001901/* map two ipaccess RTP streams onto each other */
1902static int tch_map(struct gsm_lchan *lchan, struct gsm_lchan *remote_lchan)
1903{
1904 struct gsm_bts *bts = lchan->ts->trx->bts;
1905 struct gsm_bts *remote_bts = remote_lchan->ts->trx->bts;
Harald Welte3c062072009-07-28 18:25:29 +02001906 int rc;
Harald Welte59b04682009-06-10 05:40:52 +08001907
1908 DEBUGP(DCC, "Setting up TCH map between (bts=%u,trx=%u,ts=%u) and (bts=%u,trx=%u,ts=%u)\n",
1909 bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
1910 remote_bts->nr, remote_lchan->ts->trx->nr, remote_lchan->ts->nr);
1911
1912 if (bts->type != remote_bts->type) {
1913 DEBUGP(DCC, "Cannot switch calls between different BTS types yet\n");
1914 return -EINVAL;
1915 }
1916
1917 switch (bts->type) {
Mike Haben66e0ba02009-10-02 12:19:34 +01001918 case GSM_BTS_TYPE_NANOBTS:
Harald Welte3c062072009-07-28 18:25:29 +02001919 if (!ipacc_rtp_direct) {
1920 /* connect the TCH's to our RTP proxy */
1921 rc = ipacc_connect_proxy_bind(lchan);
1922 if (rc < 0)
1923 return rc;
1924 rc = ipacc_connect_proxy_bind(remote_lchan);
1925
1926 /* connect them with each other */
Harald Welte87504212009-12-02 01:56:49 +05301927 rtp_socket_proxy(lchan->abis_ip.rtp_socket,
1928 remote_lchan->abis_ip.rtp_socket);
Harald Welte3c062072009-07-28 18:25:29 +02001929 } else {
1930 /* directly connect TCH RTP streams to each other */
Harald Welte87504212009-12-02 01:56:49 +05301931 rc = rsl_ipacc_mdcx(lchan, remote_lchan->abis_ip.bound_ip,
1932 remote_lchan->abis_ip.bound_port,
1933 lchan->abis_ip.conn_id,
1934 remote_lchan->abis_ip.rtp_payload2);
Harald Welte3c062072009-07-28 18:25:29 +02001935 if (rc < 0)
1936 return rc;
Harald Welte87504212009-12-02 01:56:49 +05301937 rc = rsl_ipacc_mdcx(remote_lchan, lchan->abis_ip.bound_ip,
1938 lchan->abis_ip.bound_port,
1939 remote_lchan->abis_ip.conn_id,
1940 lchan->abis_ip.rtp_payload2);
Harald Welte3c062072009-07-28 18:25:29 +02001941 }
Harald Welte59b04682009-06-10 05:40:52 +08001942 break;
1943 case GSM_BTS_TYPE_BS11:
1944 trau_mux_map_lchan(lchan, remote_lchan);
1945 break;
1946 default:
1947 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
Harald Welte3c062072009-07-28 18:25:29 +02001948 rc = -EINVAL;
Harald Welte59b04682009-06-10 05:40:52 +08001949 break;
1950 }
1951
1952 return 0;
1953}
1954
Harald Welte03740842009-06-10 23:11:52 +08001955/* bridge channels of two transactions */
1956static int tch_bridge(struct gsm_network *net, u_int32_t *refs)
Harald Welte59b04682009-06-10 05:40:52 +08001957{
Harald Weltec2189a62009-07-23 18:56:43 +02001958 struct gsm_trans *trans1 = trans_find_by_callref(net, refs[0]);
1959 struct gsm_trans *trans2 = trans_find_by_callref(net, refs[1]);
Harald Welte59b04682009-06-10 05:40:52 +08001960
Harald Welte03740842009-06-10 23:11:52 +08001961 if (!trans1 || !trans2)
Harald Welte59b04682009-06-10 05:40:52 +08001962 return -EIO;
1963
Harald Welte03740842009-06-10 23:11:52 +08001964 if (!trans1->lchan || !trans2->lchan)
1965 return -EIO;
1966
1967 /* through-connect channel */
1968 return tch_map(trans1->lchan, trans2->lchan);
Harald Welte59b04682009-06-10 05:40:52 +08001969}
1970
Harald Welte03740842009-06-10 23:11:52 +08001971/* enable receive of channels to upqueue */
1972static int tch_recv(struct gsm_network *net, struct gsm_mncc *data, int enable)
1973{
1974 struct gsm_trans *trans;
Harald Welte59b04682009-06-10 05:40:52 +08001975
Harald Welte03740842009-06-10 23:11:52 +08001976 /* Find callref */
Harald Weltec2189a62009-07-23 18:56:43 +02001977 trans = trans_find_by_callref(net, data->callref);
Harald Welte03740842009-06-10 23:11:52 +08001978 if (!trans)
1979 return -EIO;
1980 if (!trans->lchan)
1981 return 0;
1982
1983 // todo IPACCESS
1984 if (enable)
1985 return trau_recv_lchan(trans->lchan, data->callref);
1986 return trau_mux_unmap(NULL, data->callref);
1987}
1988
1989/* send a frame to channel */
1990static int tch_frame(struct gsm_network *net, struct gsm_trau_frame *frame)
1991{
1992 struct gsm_trans *trans;
1993
1994 /* Find callref */
Harald Weltec2189a62009-07-23 18:56:43 +02001995 trans = trans_find_by_callref(net, frame->callref);
Harald Welte03740842009-06-10 23:11:52 +08001996 if (!trans)
1997 return -EIO;
1998 if (!trans->lchan)
1999 return 0;
2000 if (trans->lchan->type != GSM_LCHAN_TCH_F &&
2001 trans->lchan->type != GSM_LCHAN_TCH_H)
2002 return 0;
2003
2004 // todo IPACCESS
2005 return trau_send_lchan(trans->lchan,
2006 (struct decoded_trau_frame *)frame->data);
2007}
2008
2009
2010static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
2011{
2012 DEBUGP(DCC, "-> STATUS ENQ\n");
2013 return gsm48_cc_tx_status(trans, msg);
2014}
2015
2016static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg);
2017static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg);
2018
2019static void gsm48_cc_timeout(void *arg)
2020{
2021 struct gsm_trans *trans = arg;
2022 int disconnect = 0, release = 0;
Harald Weltebbc636a2009-06-11 14:23:20 +08002023 int mo_cause = GSM48_CC_CAUSE_RECOVERY_TIMER;
2024 int mo_location = GSM48_CAUSE_LOC_USER;
2025 int l4_cause = GSM48_CC_CAUSE_NORMAL_UNSPEC;
2026 int l4_location = GSM48_CAUSE_LOC_PRN_S_LU;
Harald Welte03740842009-06-10 23:11:52 +08002027 struct gsm_mncc mo_rel, l4_rel;
2028
2029 memset(&mo_rel, 0, sizeof(struct gsm_mncc));
2030 mo_rel.callref = trans->callref;
2031 memset(&l4_rel, 0, sizeof(struct gsm_mncc));
2032 l4_rel.callref = trans->callref;
2033
Harald Weltec2189a62009-07-23 18:56:43 +02002034 switch(trans->cc.Tcurrent) {
Harald Welte03740842009-06-10 23:11:52 +08002035 case 0x303:
2036 release = 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 0x310:
2040 disconnect = 1;
Harald Weltebbc636a2009-06-11 14:23:20 +08002041 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte03740842009-06-10 23:11:52 +08002042 break;
2043 case 0x313:
2044 disconnect = 1;
2045 /* unknown, did not find it in the specs */
2046 break;
2047 case 0x301:
2048 disconnect = 1;
Harald Weltebbc636a2009-06-11 14:23:20 +08002049 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte03740842009-06-10 23:11:52 +08002050 break;
2051 case 0x308:
Harald Weltec2189a62009-07-23 18:56:43 +02002052 if (!trans->cc.T308_second) {
Harald Welte03740842009-06-10 23:11:52 +08002053 /* restart T308 a second time */
Harald Weltec2189a62009-07-23 18:56:43 +02002054 gsm48_cc_tx_release(trans, &trans->cc.msg);
2055 trans->cc.T308_second = 1;
Harald Welte03740842009-06-10 23:11:52 +08002056 break; /* stay in release state */
2057 }
Harald Weltec2189a62009-07-23 18:56:43 +02002058 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08002059 return;
2060// release = 1;
2061// l4_cause = 14;
2062// break;
2063 case 0x306:
2064 release = 1;
Harald Weltec2189a62009-07-23 18:56:43 +02002065 mo_cause = trans->cc.msg.cause.value;
2066 mo_location = trans->cc.msg.cause.location;
Harald Welte03740842009-06-10 23:11:52 +08002067 break;
2068 case 0x323:
2069 disconnect = 1;
2070 break;
2071 default:
2072 release = 1;
2073 }
2074
2075 if (release && trans->callref) {
2076 /* process release towards layer 4 */
Harald Welte0abe5a62009-07-23 19:06:52 +02002077 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Harald Welte03740842009-06-10 23:11:52 +08002078 l4_location, l4_cause);
2079 trans->callref = 0;
2080 }
2081
2082 if (disconnect && trans->callref) {
2083 /* process disconnect towards layer 4 */
2084 mncc_set_cause(&l4_rel, l4_location, l4_cause);
Harald Welte0abe5a62009-07-23 19:06:52 +02002085 mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &l4_rel);
Harald Welte03740842009-06-10 23:11:52 +08002086 }
2087
2088 /* process disconnect towards mobile station */
2089 if (disconnect || release) {
2090 mncc_set_cause(&mo_rel, mo_location, mo_cause);
Harald Weltec2189a62009-07-23 18:56:43 +02002091 mo_rel.cause.diag[0] = ((trans->cc.Tcurrent & 0xf00) >> 8) + '0';
2092 mo_rel.cause.diag[1] = ((trans->cc.Tcurrent & 0x0f0) >> 4) + '0';
2093 mo_rel.cause.diag[2] = (trans->cc.Tcurrent & 0x00f) + '0';
Harald Welte03740842009-06-10 23:11:52 +08002094 mo_rel.cause.diag_len = 3;
2095
2096 if (disconnect)
2097 gsm48_cc_tx_disconnect(trans, &mo_rel);
2098 if (release)
2099 gsm48_cc_tx_release(trans, &mo_rel);
2100 }
2101
2102}
2103
2104static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
2105 int sec, int micro)
2106{
2107 DEBUGP(DCC, "starting timer T%x with %d seconds\n", current, sec);
Harald Weltec2189a62009-07-23 18:56:43 +02002108 trans->cc.timer.cb = gsm48_cc_timeout;
2109 trans->cc.timer.data = trans;
2110 bsc_schedule_timer(&trans->cc.timer, sec, micro);
2111 trans->cc.Tcurrent = current;
Harald Welte03740842009-06-10 23:11:52 +08002112}
2113
2114static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
2115{
2116 struct gsm48_hdr *gh = msgb_l3(msg);
2117 u_int8_t msg_type = gh->msg_type & 0xbf;
2118 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2119 struct tlv_parsed tp;
2120 struct gsm_mncc setup;
2121
2122 memset(&setup, 0, sizeof(struct gsm_mncc));
2123 setup.callref = trans->callref;
2124 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2125 /* emergency setup is identified by msg_type */
2126 if (msg_type == GSM48_MT_CC_EMERG_SETUP)
2127 setup.emergency = 1;
2128
2129 /* use subscriber as calling party number */
2130 if (trans->subscr) {
2131 setup.fields |= MNCC_F_CALLING;
2132 strncpy(setup.calling.number, trans->subscr->extension,
2133 sizeof(setup.calling.number)-1);
Andreas Eversberg9eaa5da2009-06-15 23:22:09 +02002134 strncpy(setup.imsi, trans->subscr->imsi,
2135 sizeof(setup.imsi)-1);
Harald Welte03740842009-06-10 23:11:52 +08002136 }
2137 /* bearer capability */
2138 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2139 setup.fields |= MNCC_F_BEARER_CAP;
2140 decode_bearer_cap(&setup.bearer_cap,
2141 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2142 }
2143 /* facility */
2144 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2145 setup.fields |= MNCC_F_FACILITY;
2146 decode_facility(&setup.facility,
2147 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2148 }
2149 /* called party bcd number */
2150 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
2151 setup.fields |= MNCC_F_CALLED;
2152 decode_called(&setup.called,
2153 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
2154 }
2155 /* user-user */
2156 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2157 setup.fields |= MNCC_F_USERUSER;
2158 decode_useruser(&setup.useruser,
2159 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2160 }
2161 /* ss-version */
2162 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2163 setup.fields |= MNCC_F_SSVERSION;
2164 decode_ssversion(&setup.ssversion,
2165 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2166 }
2167 /* CLIR suppression */
2168 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
2169 setup.clir.sup = 1;
2170 /* CLIR invocation */
2171 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
2172 setup.clir.inv = 1;
2173 /* cc cap */
2174 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
2175 setup.fields |= MNCC_F_CCCAP;
2176 decode_cccap(&setup.cccap,
2177 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
2178 }
2179
Harald Welte03740842009-06-10 23:11:52 +08002180 new_cc_state(trans, GSM_CSTATE_INITIATED);
2181
2182 /* indicate setup to MNCC */
Harald Welte0abe5a62009-07-23 19:06:52 +02002183 mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_IND, &setup);
Harald Welte03740842009-06-10 23:11:52 +08002184
Harald Welteca745e22009-07-29 12:10:35 +02002185 /* MNCC code will modify the channel asynchronously, we should
2186 * ipaccess-bind only after the modification has been made to the
2187 * lchan->tch_mode */
Harald Welte03740842009-06-10 23:11:52 +08002188 return 0;
2189}
2190
2191static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
Harald Welte59b04682009-06-10 05:40:52 +08002192{
2193 struct msgb *msg = gsm48_msgb_alloc();
2194 struct gsm48_hdr *gh;
Harald Welte03740842009-06-10 23:11:52 +08002195 struct gsm_mncc *setup = arg;
Harald Welte165fc332009-07-23 21:36:44 +02002196 int rc, trans_id;
Harald Welte59b04682009-06-10 05:40:52 +08002197
2198 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2199
Harald Welte03740842009-06-10 23:11:52 +08002200 /* transaction id must not be assigned */
2201 if (trans->transaction_id != 0xff) { /* unasssigned */
2202 DEBUGP(DCC, "TX Setup with assigned transaction. "
2203 "This is not allowed!\n");
2204 /* Temporarily out of order */
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 }
2212
2213 /* Get free transaction_id */
Harald Welte165fc332009-07-23 21:36:44 +02002214 trans_id = trans_assign_trans_id(trans->subscr, GSM48_PDISC_CC, 0);
2215 if (trans_id < 0) {
Harald Welte03740842009-06-10 23:11:52 +08002216 /* no free transaction ID */
Harald Welte0abe5a62009-07-23 19:06:52 +02002217 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08002218 GSM48_CAUSE_LOC_PRN_S_LU,
2219 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte03740842009-06-10 23:11:52 +08002220 trans->callref = 0;
Harald Weltec2189a62009-07-23 18:56:43 +02002221 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08002222 return rc;
2223 }
Harald Welte165fc332009-07-23 21:36:44 +02002224 trans->transaction_id = trans_id;
Harald Welte59b04682009-06-10 05:40:52 +08002225
Harald Welte59b04682009-06-10 05:40:52 +08002226 gh->msg_type = GSM48_MT_CC_SETUP;
2227
Harald Welte03740842009-06-10 23:11:52 +08002228 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
Harald Welte59b04682009-06-10 05:40:52 +08002229
Harald Welte03740842009-06-10 23:11:52 +08002230 /* bearer capability */
2231 if (setup->fields & MNCC_F_BEARER_CAP)
2232 encode_bearer_cap(msg, 0, &setup->bearer_cap);
2233 /* facility */
2234 if (setup->fields & MNCC_F_FACILITY)
2235 encode_facility(msg, 0, &setup->facility);
2236 /* progress */
2237 if (setup->fields & MNCC_F_PROGRESS)
2238 encode_progress(msg, 0, &setup->progress);
2239 /* calling party BCD number */
2240 if (setup->fields & MNCC_F_CALLING)
2241 encode_calling(msg, &setup->calling);
2242 /* called party BCD number */
2243 if (setup->fields & MNCC_F_CALLED)
2244 encode_called(msg, &setup->called);
2245 /* user-user */
2246 if (setup->fields & MNCC_F_USERUSER)
2247 encode_useruser(msg, 0, &setup->useruser);
2248 /* redirecting party BCD number */
2249 if (setup->fields & MNCC_F_REDIRECTING)
2250 encode_redirecting(msg, &setup->redirecting);
2251 /* signal */
2252 if (setup->fields & MNCC_F_SIGNAL)
2253 encode_signal(msg, setup->signal);
2254
2255 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
Harald Welte59b04682009-06-10 05:40:52 +08002256
Harald Welte36fe2e82009-07-23 21:13:03 +02002257 return gsm48_sendmsg(msg, trans);
Harald Welte59b04682009-06-10 05:40:52 +08002258}
2259
Harald Welte03740842009-06-10 23:11:52 +08002260static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
2261{
2262 struct gsm48_hdr *gh = msgb_l3(msg);
2263 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2264 struct tlv_parsed tp;
2265 struct gsm_mncc call_conf;
2266
2267 gsm48_stop_cc_timer(trans);
2268 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
2269
2270 memset(&call_conf, 0, sizeof(struct gsm_mncc));
2271 call_conf.callref = trans->callref;
2272 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2273#if 0
2274 /* repeat */
2275 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
2276 call_conf.repeat = 1;
2277 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
2278 call_conf.repeat = 2;
2279#endif
2280 /* bearer capability */
2281 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2282 call_conf.fields |= MNCC_F_BEARER_CAP;
2283 decode_bearer_cap(&call_conf.bearer_cap,
2284 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2285 }
2286 /* cause */
2287 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2288 call_conf.fields |= MNCC_F_CAUSE;
2289 decode_cause(&call_conf.cause,
2290 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2291 }
2292 /* cc cap */
2293 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
2294 call_conf.fields |= MNCC_F_CCCAP;
2295 decode_cccap(&call_conf.cccap,
2296 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
2297 }
2298
2299 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
2300
Harald Welte0abe5a62009-07-23 19:06:52 +02002301 return mncc_recvmsg(trans->subscr->net, trans, MNCC_CALL_CONF_IND,
2302 &call_conf);
Harald Welte03740842009-06-10 23:11:52 +08002303}
2304
2305static int gsm48_cc_tx_call_proc(struct gsm_trans *trans, void *arg)
2306{
2307 struct gsm_mncc *proceeding = arg;
2308 struct msgb *msg = gsm48_msgb_alloc();
2309 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2310
Harald Welte03740842009-06-10 23:11:52 +08002311 gh->msg_type = GSM48_MT_CC_CALL_PROC;
2312
2313 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
2314
2315 /* bearer capability */
2316 if (proceeding->fields & MNCC_F_BEARER_CAP)
2317 encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
2318 /* facility */
2319 if (proceeding->fields & MNCC_F_FACILITY)
2320 encode_facility(msg, 0, &proceeding->facility);
2321 /* progress */
2322 if (proceeding->fields & MNCC_F_PROGRESS)
2323 encode_progress(msg, 0, &proceeding->progress);
2324
Harald Welte36fe2e82009-07-23 21:13:03 +02002325 return gsm48_sendmsg(msg, trans);
Harald Welte03740842009-06-10 23:11:52 +08002326}
2327
2328static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
2329{
2330 struct gsm48_hdr *gh = msgb_l3(msg);
2331 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2332 struct tlv_parsed tp;
2333 struct gsm_mncc alerting;
2334
2335 gsm48_stop_cc_timer(trans);
2336 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
2337
2338 memset(&alerting, 0, sizeof(struct gsm_mncc));
2339 alerting.callref = trans->callref;
2340 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2341 /* facility */
2342 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2343 alerting.fields |= MNCC_F_FACILITY;
2344 decode_facility(&alerting.facility,
2345 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2346 }
2347
2348 /* progress */
2349 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
2350 alerting.fields |= MNCC_F_PROGRESS;
2351 decode_progress(&alerting.progress,
2352 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
2353 }
2354 /* ss-version */
2355 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2356 alerting.fields |= MNCC_F_SSVERSION;
2357 decode_ssversion(&alerting.ssversion,
2358 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2359 }
2360
2361 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
2362
Harald Welte0abe5a62009-07-23 19:06:52 +02002363 return mncc_recvmsg(trans->subscr->net, trans, MNCC_ALERT_IND,
2364 &alerting);
Harald Welte03740842009-06-10 23:11:52 +08002365}
2366
2367static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
2368{
2369 struct gsm_mncc *alerting = arg;
2370 struct msgb *msg = gsm48_msgb_alloc();
2371 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2372
Harald Welte03740842009-06-10 23:11:52 +08002373 gh->msg_type = GSM48_MT_CC_ALERTING;
2374
2375 /* facility */
2376 if (alerting->fields & MNCC_F_FACILITY)
2377 encode_facility(msg, 0, &alerting->facility);
2378 /* progress */
2379 if (alerting->fields & MNCC_F_PROGRESS)
2380 encode_progress(msg, 0, &alerting->progress);
2381 /* user-user */
2382 if (alerting->fields & MNCC_F_USERUSER)
2383 encode_useruser(msg, 0, &alerting->useruser);
2384
2385 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
2386
Harald Welte36fe2e82009-07-23 21:13:03 +02002387 return gsm48_sendmsg(msg, trans);
Harald Welte03740842009-06-10 23:11:52 +08002388}
2389
2390static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
2391{
2392 struct gsm_mncc *progress = arg;
2393 struct msgb *msg = gsm48_msgb_alloc();
2394 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2395
Harald Welte03740842009-06-10 23:11:52 +08002396 gh->msg_type = GSM48_MT_CC_PROGRESS;
2397
2398 /* progress */
2399 encode_progress(msg, 1, &progress->progress);
2400 /* user-user */
2401 if (progress->fields & MNCC_F_USERUSER)
2402 encode_useruser(msg, 0, &progress->useruser);
2403
Harald Welte36fe2e82009-07-23 21:13:03 +02002404 return gsm48_sendmsg(msg, trans);
Harald Welte03740842009-06-10 23:11:52 +08002405}
2406
2407static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
2408{
2409 struct gsm_mncc *connect = arg;
2410 struct msgb *msg = gsm48_msgb_alloc();
2411 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2412
Harald Welte03740842009-06-10 23:11:52 +08002413 gh->msg_type = GSM48_MT_CC_CONNECT;
2414
2415 gsm48_stop_cc_timer(trans);
2416 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
2417
2418 /* facility */
2419 if (connect->fields & MNCC_F_FACILITY)
2420 encode_facility(msg, 0, &connect->facility);
2421 /* progress */
2422 if (connect->fields & MNCC_F_PROGRESS)
2423 encode_progress(msg, 0, &connect->progress);
2424 /* connected number */
2425 if (connect->fields & MNCC_F_CONNECTED)
2426 encode_connected(msg, &connect->connected);
2427 /* user-user */
2428 if (connect->fields & MNCC_F_USERUSER)
2429 encode_useruser(msg, 0, &connect->useruser);
2430
2431 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
2432
Harald Welte36fe2e82009-07-23 21:13:03 +02002433 return gsm48_sendmsg(msg, trans);
Harald Welte03740842009-06-10 23:11:52 +08002434}
2435
2436static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
2437{
2438 struct gsm48_hdr *gh = msgb_l3(msg);
2439 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2440 struct tlv_parsed tp;
2441 struct gsm_mncc connect;
2442
2443 gsm48_stop_cc_timer(trans);
2444
2445 memset(&connect, 0, sizeof(struct gsm_mncc));
2446 connect.callref = trans->callref;
2447 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2448 /* use subscriber as connected party number */
2449 if (trans->subscr) {
2450 connect.fields |= MNCC_F_CONNECTED;
2451 strncpy(connect.connected.number, trans->subscr->extension,
2452 sizeof(connect.connected.number)-1);
Andreas Eversberg9eaa5da2009-06-15 23:22:09 +02002453 strncpy(connect.imsi, trans->subscr->imsi,
2454 sizeof(connect.imsi)-1);
Harald Welte03740842009-06-10 23:11:52 +08002455 }
2456 /* facility */
2457 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2458 connect.fields |= MNCC_F_FACILITY;
2459 decode_facility(&connect.facility,
2460 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2461 }
2462 /* user-user */
2463 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2464 connect.fields |= MNCC_F_USERUSER;
2465 decode_useruser(&connect.useruser,
2466 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2467 }
2468 /* ss-version */
2469 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2470 connect.fields |= MNCC_F_SSVERSION;
2471 decode_ssversion(&connect.ssversion,
2472 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2473 }
2474
2475 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
2476
Harald Welte0abe5a62009-07-23 19:06:52 +02002477 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_CNF, &connect);
Harald Welte03740842009-06-10 23:11:52 +08002478}
2479
2480
2481static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
2482{
2483 struct gsm_mncc connect_ack;
2484
2485 gsm48_stop_cc_timer(trans);
2486
2487 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2488
2489 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
2490 connect_ack.callref = trans->callref;
Harald Welte0abe5a62009-07-23 19:06:52 +02002491 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_COMPL_IND,
Harald Welte03740842009-06-10 23:11:52 +08002492 &connect_ack);
2493}
2494
2495static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
2496{
2497 struct msgb *msg = gsm48_msgb_alloc();
2498 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2499
Harald Welte03740842009-06-10 23:11:52 +08002500 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
2501
2502 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2503
Harald Welte36fe2e82009-07-23 21:13:03 +02002504 return gsm48_sendmsg(msg, trans);
Harald Welte03740842009-06-10 23:11:52 +08002505}
2506
2507static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
2508{
2509 struct gsm48_hdr *gh = msgb_l3(msg);
2510 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2511 struct tlv_parsed tp;
2512 struct gsm_mncc disc;
2513
2514 gsm48_stop_cc_timer(trans);
2515
2516 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
2517
2518 memset(&disc, 0, sizeof(struct gsm_mncc));
2519 disc.callref = trans->callref;
2520 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
2521 /* cause */
2522 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2523 disc.fields |= MNCC_F_CAUSE;
2524 decode_cause(&disc.cause,
2525 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2526 }
2527 /* facility */
2528 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2529 disc.fields |= MNCC_F_FACILITY;
2530 decode_facility(&disc.facility,
2531 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2532 }
2533 /* user-user */
2534 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2535 disc.fields |= MNCC_F_USERUSER;
2536 decode_useruser(&disc.useruser,
2537 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2538 }
2539 /* ss-version */
2540 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2541 disc.fields |= MNCC_F_SSVERSION;
2542 decode_ssversion(&disc.ssversion,
2543 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2544 }
2545
Harald Welte0abe5a62009-07-23 19:06:52 +02002546 return mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &disc);
Harald Welte03740842009-06-10 23:11:52 +08002547
2548}
2549
Harald Weltebbc636a2009-06-11 14:23:20 +08002550static struct gsm_mncc_cause default_cause = {
2551 .location = GSM48_CAUSE_LOC_PRN_S_LU,
2552 .coding = 0,
2553 .rec = 0,
2554 .rec_val = 0,
2555 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
2556 .diag_len = 0,
2557 .diag = { 0 },
2558};
Harald Welte03740842009-06-10 23:11:52 +08002559
2560static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
2561{
2562 struct gsm_mncc *disc = arg;
2563 struct msgb *msg = gsm48_msgb_alloc();
2564 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2565
Harald Welte03740842009-06-10 23:11:52 +08002566 gh->msg_type = GSM48_MT_CC_DISCONNECT;
2567
2568 gsm48_stop_cc_timer(trans);
2569 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
2570
2571 /* cause */
2572 if (disc->fields & MNCC_F_CAUSE)
2573 encode_cause(msg, 1, &disc->cause);
2574 else
2575 encode_cause(msg, 1, &default_cause);
2576
2577 /* facility */
2578 if (disc->fields & MNCC_F_FACILITY)
2579 encode_facility(msg, 0, &disc->facility);
2580 /* progress */
2581 if (disc->fields & MNCC_F_PROGRESS)
2582 encode_progress(msg, 0, &disc->progress);
2583 /* user-user */
2584 if (disc->fields & MNCC_F_USERUSER)
2585 encode_useruser(msg, 0, &disc->useruser);
2586
2587 /* store disconnect cause for T306 expiry */
Harald Weltec2189a62009-07-23 18:56:43 +02002588 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
Harald Welte03740842009-06-10 23:11:52 +08002589
2590 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
2591
Harald Welte36fe2e82009-07-23 21:13:03 +02002592 return gsm48_sendmsg(msg, trans);
Harald Welte03740842009-06-10 23:11:52 +08002593}
2594
2595static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
2596{
2597 struct gsm48_hdr *gh = msgb_l3(msg);
2598 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2599 struct tlv_parsed tp;
2600 struct gsm_mncc rel;
2601 int rc;
2602
2603 gsm48_stop_cc_timer(trans);
2604
2605 memset(&rel, 0, sizeof(struct gsm_mncc));
2606 rel.callref = trans->callref;
2607 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2608 /* cause */
2609 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2610 rel.fields |= MNCC_F_CAUSE;
2611 decode_cause(&rel.cause,
2612 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2613 }
2614 /* facility */
2615 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2616 rel.fields |= MNCC_F_FACILITY;
2617 decode_facility(&rel.facility,
2618 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2619 }
2620 /* user-user */
2621 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2622 rel.fields |= MNCC_F_USERUSER;
2623 decode_useruser(&rel.useruser,
2624 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2625 }
2626 /* ss-version */
2627 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2628 rel.fields |= MNCC_F_SSVERSION;
2629 decode_ssversion(&rel.ssversion,
2630 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2631 }
2632
Harald Weltec2189a62009-07-23 18:56:43 +02002633 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
Harald Welte03740842009-06-10 23:11:52 +08002634 /* release collision 5.4.5 */
Harald Welte0abe5a62009-07-23 19:06:52 +02002635 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_CNF, &rel);
Harald Welte03740842009-06-10 23:11:52 +08002636 } else {
Harald Welte0abe5a62009-07-23 19:06:52 +02002637 rc = gsm48_tx_simple(msg->lchan,
Harald Welte4861c822009-07-23 21:21:14 +02002638 GSM48_PDISC_CC | (trans->transaction_id << 4),
Harald Welte0abe5a62009-07-23 19:06:52 +02002639 GSM48_MT_CC_RELEASE_COMPL);
2640 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_IND, &rel);
Harald Welte03740842009-06-10 23:11:52 +08002641 }
2642
2643 new_cc_state(trans, GSM_CSTATE_NULL);
2644
2645 trans->callref = 0;
Harald Weltec2189a62009-07-23 18:56:43 +02002646 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08002647
2648 return rc;
2649}
2650
2651static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
2652{
2653 struct gsm_mncc *rel = arg;
2654 struct msgb *msg = gsm48_msgb_alloc();
2655 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2656
Harald Welte03740842009-06-10 23:11:52 +08002657 gh->msg_type = GSM48_MT_CC_RELEASE;
2658
2659 trans->callref = 0;
2660
2661 gsm48_stop_cc_timer(trans);
2662 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
2663
2664 /* cause */
2665 if (rel->fields & MNCC_F_CAUSE)
2666 encode_cause(msg, 0, &rel->cause);
2667 /* facility */
2668 if (rel->fields & MNCC_F_FACILITY)
2669 encode_facility(msg, 0, &rel->facility);
2670 /* user-user */
2671 if (rel->fields & MNCC_F_USERUSER)
2672 encode_useruser(msg, 0, &rel->useruser);
2673
Harald Weltec2189a62009-07-23 18:56:43 +02002674 trans->cc.T308_second = 0;
2675 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
Harald Welte03740842009-06-10 23:11:52 +08002676
Harald Weltec2189a62009-07-23 18:56:43 +02002677 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
Harald Welte03740842009-06-10 23:11:52 +08002678 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
2679
Harald Welte36fe2e82009-07-23 21:13:03 +02002680 return gsm48_sendmsg(msg, trans);
Harald Welte03740842009-06-10 23:11:52 +08002681}
2682
2683static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
2684{
2685 struct gsm48_hdr *gh = msgb_l3(msg);
2686 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2687 struct tlv_parsed tp;
2688 struct gsm_mncc rel;
2689 int rc = 0;
2690
2691 gsm48_stop_cc_timer(trans);
2692
2693 memset(&rel, 0, sizeof(struct gsm_mncc));
2694 rel.callref = trans->callref;
2695 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2696 /* cause */
2697 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2698 rel.fields |= MNCC_F_CAUSE;
2699 decode_cause(&rel.cause,
2700 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2701 }
2702 /* facility */
2703 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2704 rel.fields |= MNCC_F_FACILITY;
2705 decode_facility(&rel.facility,
2706 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2707 }
2708 /* user-user */
2709 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2710 rel.fields |= MNCC_F_USERUSER;
2711 decode_useruser(&rel.useruser,
2712 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2713 }
2714 /* ss-version */
2715 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2716 rel.fields |= MNCC_F_SSVERSION;
2717 decode_ssversion(&rel.ssversion,
2718 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2719 }
2720
2721 if (trans->callref) {
Harald Weltec2189a62009-07-23 18:56:43 +02002722 switch (trans->cc.state) {
Harald Welte03740842009-06-10 23:11:52 +08002723 case GSM_CSTATE_CALL_PRESENT:
Harald Welte0abe5a62009-07-23 19:06:52 +02002724 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte03740842009-06-10 23:11:52 +08002725 MNCC_REJ_IND, &rel);
2726 break;
2727 case GSM_CSTATE_RELEASE_REQ:
Harald Welte0abe5a62009-07-23 19:06:52 +02002728 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte03740842009-06-10 23:11:52 +08002729 MNCC_REL_CNF, &rel);
Harald Welte9ed54ff2009-12-12 21:00:48 +01002730 /* FIXME: in case of multiple calls, we can't simply
2731 * hang up here ! */
2732 lchan_auto_release(msg->lchan);
Harald Welte03740842009-06-10 23:11:52 +08002733 break;
2734 default:
Harald Welte0abe5a62009-07-23 19:06:52 +02002735 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte03740842009-06-10 23:11:52 +08002736 MNCC_REL_IND, &rel);
2737 }
2738 }
2739
2740 trans->callref = 0;
Harald Weltec2189a62009-07-23 18:56:43 +02002741 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08002742
2743 return rc;
2744}
2745
2746static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
2747{
2748 struct gsm_mncc *rel = arg;
2749 struct msgb *msg = gsm48_msgb_alloc();
2750 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2751
Harald Welte03740842009-06-10 23:11:52 +08002752 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
2753
2754 trans->callref = 0;
2755
2756 gsm48_stop_cc_timer(trans);
2757
2758 /* cause */
2759 if (rel->fields & MNCC_F_CAUSE)
2760 encode_cause(msg, 0, &rel->cause);
2761 /* facility */
2762 if (rel->fields & MNCC_F_FACILITY)
2763 encode_facility(msg, 0, &rel->facility);
2764 /* user-user */
2765 if (rel->fields & MNCC_F_USERUSER)
2766 encode_useruser(msg, 0, &rel->useruser);
2767
Harald Weltec2189a62009-07-23 18:56:43 +02002768 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08002769
Harald Welte36fe2e82009-07-23 21:13:03 +02002770 return gsm48_sendmsg(msg, trans);
Harald Welte03740842009-06-10 23:11:52 +08002771}
2772
2773static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
2774{
2775 struct gsm48_hdr *gh = msgb_l3(msg);
2776 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2777 struct tlv_parsed tp;
2778 struct gsm_mncc fac;
2779
2780 memset(&fac, 0, sizeof(struct gsm_mncc));
2781 fac.callref = trans->callref;
2782 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
2783 /* facility */
2784 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2785 fac.fields |= MNCC_F_FACILITY;
2786 decode_facility(&fac.facility,
2787 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2788 }
2789 /* ss-version */
2790 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2791 fac.fields |= MNCC_F_SSVERSION;
2792 decode_ssversion(&fac.ssversion,
2793 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2794 }
2795
Harald Welte0abe5a62009-07-23 19:06:52 +02002796 return mncc_recvmsg(trans->subscr->net, trans, MNCC_FACILITY_IND, &fac);
Harald Welte03740842009-06-10 23:11:52 +08002797}
2798
2799static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
2800{
2801 struct gsm_mncc *fac = arg;
2802 struct msgb *msg = gsm48_msgb_alloc();
2803 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2804
Harald Welte03740842009-06-10 23:11:52 +08002805 gh->msg_type = GSM48_MT_CC_FACILITY;
2806
2807 /* facility */
2808 encode_facility(msg, 1, &fac->facility);
2809
Harald Welte36fe2e82009-07-23 21:13:03 +02002810 return gsm48_sendmsg(msg, trans);
Harald Welte03740842009-06-10 23:11:52 +08002811}
2812
2813static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
2814{
2815 struct gsm_mncc hold;
2816
2817 memset(&hold, 0, sizeof(struct gsm_mncc));
2818 hold.callref = trans->callref;
Harald Welte0abe5a62009-07-23 19:06:52 +02002819 return mncc_recvmsg(trans->subscr->net, trans, MNCC_HOLD_IND, &hold);
Harald Welte03740842009-06-10 23:11:52 +08002820}
2821
2822static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
2823{
2824 struct msgb *msg = gsm48_msgb_alloc();
2825 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2826
Harald Welte03740842009-06-10 23:11:52 +08002827 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
2828
Harald Welte36fe2e82009-07-23 21:13:03 +02002829 return gsm48_sendmsg(msg, trans);
Harald Welte03740842009-06-10 23:11:52 +08002830}
2831
2832static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
2833{
2834 struct gsm_mncc *hold_rej = arg;
2835 struct msgb *msg = gsm48_msgb_alloc();
2836 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2837
Harald Welte03740842009-06-10 23:11:52 +08002838 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
2839
2840 /* cause */
2841 if (hold_rej->fields & MNCC_F_CAUSE)
2842 encode_cause(msg, 1, &hold_rej->cause);
2843 else
2844 encode_cause(msg, 1, &default_cause);
2845
Harald Welte36fe2e82009-07-23 21:13:03 +02002846 return gsm48_sendmsg(msg, trans);
Harald Welte03740842009-06-10 23:11:52 +08002847}
2848
2849static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
2850{
2851 struct gsm_mncc retrieve;
2852
2853 memset(&retrieve, 0, sizeof(struct gsm_mncc));
2854 retrieve.callref = trans->callref;
Harald Welte0abe5a62009-07-23 19:06:52 +02002855 return mncc_recvmsg(trans->subscr->net, trans, MNCC_RETRIEVE_IND,
2856 &retrieve);
Harald Welte03740842009-06-10 23:11:52 +08002857}
2858
2859static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
2860{
2861 struct msgb *msg = gsm48_msgb_alloc();
2862 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2863
Harald Welte03740842009-06-10 23:11:52 +08002864 gh->msg_type = GSM48_MT_CC_RETR_ACK;
2865
Harald Welte36fe2e82009-07-23 21:13:03 +02002866 return gsm48_sendmsg(msg, trans);
Harald Welte03740842009-06-10 23:11:52 +08002867}
2868
2869static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
2870{
2871 struct gsm_mncc *retrieve_rej = arg;
2872 struct msgb *msg = gsm48_msgb_alloc();
2873 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2874
Harald Welte03740842009-06-10 23:11:52 +08002875 gh->msg_type = GSM48_MT_CC_RETR_REJ;
2876
2877 /* cause */
2878 if (retrieve_rej->fields & MNCC_F_CAUSE)
2879 encode_cause(msg, 1, &retrieve_rej->cause);
2880 else
2881 encode_cause(msg, 1, &default_cause);
2882
Harald Welte36fe2e82009-07-23 21:13:03 +02002883 return gsm48_sendmsg(msg, trans);
Harald Welte03740842009-06-10 23:11:52 +08002884}
2885
2886static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
2887{
2888 struct gsm48_hdr *gh = msgb_l3(msg);
2889 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2890 struct tlv_parsed tp;
2891 struct gsm_mncc dtmf;
2892
2893 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2894 dtmf.callref = trans->callref;
2895 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2896 /* keypad facility */
2897 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
2898 dtmf.fields |= MNCC_F_KEYPAD;
2899 decode_keypad(&dtmf.keypad,
2900 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
2901 }
2902
Harald Welte0abe5a62009-07-23 19:06:52 +02002903 return mncc_recvmsg(trans->subscr->net, trans, MNCC_START_DTMF_IND, &dtmf);
Harald Welte03740842009-06-10 23:11:52 +08002904}
2905
2906static int gsm48_cc_tx_start_dtmf_ack(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_ACK;
2913
2914 /* keypad */
2915 if (dtmf->fields & MNCC_F_KEYPAD)
2916 encode_keypad(msg, dtmf->keypad);
2917
Harald Welte36fe2e82009-07-23 21:13:03 +02002918 return gsm48_sendmsg(msg, trans);
Harald Welte03740842009-06-10 23:11:52 +08002919}
2920
2921static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
2922{
2923 struct gsm_mncc *dtmf = arg;
2924 struct msgb *msg = gsm48_msgb_alloc();
2925 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2926
Harald Welte03740842009-06-10 23:11:52 +08002927 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
2928
2929 /* cause */
2930 if (dtmf->fields & MNCC_F_CAUSE)
2931 encode_cause(msg, 1, &dtmf->cause);
2932 else
2933 encode_cause(msg, 1, &default_cause);
2934
Harald Welte36fe2e82009-07-23 21:13:03 +02002935 return gsm48_sendmsg(msg, trans);
Harald Welte03740842009-06-10 23:11:52 +08002936}
2937
2938static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
2939{
2940 struct msgb *msg = gsm48_msgb_alloc();
2941 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2942
Harald Welte03740842009-06-10 23:11:52 +08002943 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
2944
Harald Welte36fe2e82009-07-23 21:13:03 +02002945 return gsm48_sendmsg(msg, trans);
Harald Welte03740842009-06-10 23:11:52 +08002946}
2947
2948static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
2949{
2950 struct gsm_mncc dtmf;
2951
2952 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2953 dtmf.callref = trans->callref;
2954
Harald Welte0abe5a62009-07-23 19:06:52 +02002955 return mncc_recvmsg(trans->subscr->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
Harald Welte03740842009-06-10 23:11:52 +08002956}
2957
2958static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
2959{
2960 struct gsm48_hdr *gh = msgb_l3(msg);
2961 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2962 struct tlv_parsed tp;
2963 struct gsm_mncc modify;
2964
2965 memset(&modify, 0, sizeof(struct gsm_mncc));
2966 modify.callref = trans->callref;
2967 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
2968 /* bearer capability */
2969 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2970 modify.fields |= MNCC_F_BEARER_CAP;
2971 decode_bearer_cap(&modify.bearer_cap,
2972 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2973 }
2974
2975 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
2976
Harald Welte0abe5a62009-07-23 19:06:52 +02002977 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_IND, &modify);
Harald Welte03740842009-06-10 23:11:52 +08002978}
2979
2980static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
2981{
2982 struct gsm_mncc *modify = arg;
2983 struct msgb *msg = gsm48_msgb_alloc();
2984 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2985
Harald Welte03740842009-06-10 23:11:52 +08002986 gh->msg_type = GSM48_MT_CC_MODIFY;
2987
2988 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
2989
2990 /* bearer capability */
2991 encode_bearer_cap(msg, 1, &modify->bearer_cap);
2992
2993 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
2994
Harald Welte36fe2e82009-07-23 21:13:03 +02002995 return gsm48_sendmsg(msg, trans);
Harald Welte03740842009-06-10 23:11:52 +08002996}
2997
2998static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
2999{
3000 struct gsm48_hdr *gh = msgb_l3(msg);
3001 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
3002 struct tlv_parsed tp;
3003 struct gsm_mncc modify;
3004
3005 gsm48_stop_cc_timer(trans);
3006
3007 memset(&modify, 0, sizeof(struct gsm_mncc));
3008 modify.callref = trans->callref;
3009 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
3010 /* bearer capability */
3011 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
3012 modify.fields |= MNCC_F_BEARER_CAP;
3013 decode_bearer_cap(&modify.bearer_cap,
3014 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
3015 }
3016
3017 new_cc_state(trans, GSM_CSTATE_ACTIVE);
3018
Harald Welte0abe5a62009-07-23 19:06:52 +02003019 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_CNF, &modify);
Harald Welte03740842009-06-10 23:11:52 +08003020}
3021
3022static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
3023{
3024 struct gsm_mncc *modify = arg;
3025 struct msgb *msg = gsm48_msgb_alloc();
3026 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3027
Harald Welte03740842009-06-10 23:11:52 +08003028 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
3029
3030 /* bearer capability */
3031 encode_bearer_cap(msg, 1, &modify->bearer_cap);
3032
3033 new_cc_state(trans, GSM_CSTATE_ACTIVE);
3034
Harald Welte36fe2e82009-07-23 21:13:03 +02003035 return gsm48_sendmsg(msg, trans);
Harald Welte03740842009-06-10 23:11:52 +08003036}
3037
3038static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
3039{
3040 struct gsm48_hdr *gh = msgb_l3(msg);
3041 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
3042 struct tlv_parsed tp;
3043 struct gsm_mncc modify;
3044
3045 gsm48_stop_cc_timer(trans);
3046
3047 memset(&modify, 0, sizeof(struct gsm_mncc));
3048 modify.callref = trans->callref;
3049 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
3050 /* bearer capability */
3051 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
3052 modify.fields |= GSM48_IE_BEARER_CAP;
3053 decode_bearer_cap(&modify.bearer_cap,
3054 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
3055 }
3056 /* cause */
3057 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
3058 modify.fields |= MNCC_F_CAUSE;
3059 decode_cause(&modify.cause,
3060 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
3061 }
3062
3063 new_cc_state(trans, GSM_CSTATE_ACTIVE);
3064
Harald Welte0abe5a62009-07-23 19:06:52 +02003065 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_REJ, &modify);
Harald Welte03740842009-06-10 23:11:52 +08003066}
3067
3068static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
3069{
3070 struct gsm_mncc *modify = arg;
3071 struct msgb *msg = gsm48_msgb_alloc();
3072 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3073
Harald Welte03740842009-06-10 23:11:52 +08003074 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
3075
3076 /* bearer capability */
3077 encode_bearer_cap(msg, 1, &modify->bearer_cap);
3078 /* cause */
3079 encode_cause(msg, 1, &modify->cause);
3080
3081 new_cc_state(trans, GSM_CSTATE_ACTIVE);
3082
Harald Welte36fe2e82009-07-23 21:13:03 +02003083 return gsm48_sendmsg(msg, trans);
Harald Welte03740842009-06-10 23:11:52 +08003084}
3085
3086static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
3087{
3088 struct gsm_mncc *notify = arg;
3089 struct msgb *msg = gsm48_msgb_alloc();
3090 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3091
Harald Welte03740842009-06-10 23:11:52 +08003092 gh->msg_type = GSM48_MT_CC_NOTIFY;
3093
3094 /* notify */
3095 encode_notify(msg, notify->notify);
3096
Harald Welte36fe2e82009-07-23 21:13:03 +02003097 return gsm48_sendmsg(msg, trans);
Harald Welte03740842009-06-10 23:11:52 +08003098}
3099
3100static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
3101{
3102 struct gsm48_hdr *gh = msgb_l3(msg);
3103 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
3104// struct tlv_parsed tp;
3105 struct gsm_mncc notify;
3106
3107 memset(&notify, 0, sizeof(struct gsm_mncc));
3108 notify.callref = trans->callref;
3109// tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len);
3110 if (payload_len >= 1)
3111 decode_notify(&notify.notify, gh->data);
3112
Harald Welte0abe5a62009-07-23 19:06:52 +02003113 return mncc_recvmsg(trans->subscr->net, trans, MNCC_NOTIFY_IND, &notify);
Harald Welte03740842009-06-10 23:11:52 +08003114}
3115
3116static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
3117{
3118 struct gsm_mncc *user = arg;
3119 struct msgb *msg = gsm48_msgb_alloc();
3120 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3121
Harald Welte03740842009-06-10 23:11:52 +08003122 gh->msg_type = GSM48_MT_CC_USER_INFO;
3123
3124 /* user-user */
3125 if (user->fields & MNCC_F_USERUSER)
3126 encode_useruser(msg, 1, &user->useruser);
3127 /* more data */
3128 if (user->more)
3129 encode_more(msg);
3130
Harald Welte36fe2e82009-07-23 21:13:03 +02003131 return gsm48_sendmsg(msg, trans);
Harald Welte03740842009-06-10 23:11:52 +08003132}
3133
3134static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
3135{
3136 struct gsm48_hdr *gh = msgb_l3(msg);
3137 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
3138 struct tlv_parsed tp;
3139 struct gsm_mncc user;
3140
3141 memset(&user, 0, sizeof(struct gsm_mncc));
3142 user.callref = trans->callref;
3143 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
3144 /* user-user */
3145 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
3146 user.fields |= MNCC_F_USERUSER;
3147 decode_useruser(&user.useruser,
3148 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
3149 }
3150 /* more data */
3151 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
3152 user.more = 1;
3153
Harald Welte0abe5a62009-07-23 19:06:52 +02003154 return mncc_recvmsg(trans->subscr->net, trans, MNCC_USERINFO_IND, &user);
Harald Welte03740842009-06-10 23:11:52 +08003155}
3156
Holger Hans Peter Freythercf0f3362009-10-22 15:13:00 +02003157static int _gsm48_lchan_modify(struct gsm_trans *trans, void *arg)
Harald Welte03740842009-06-10 23:11:52 +08003158{
3159 struct gsm_mncc *mode = arg;
3160
Holger Hans Peter Freyther3cce58f2009-11-18 22:57:02 +01003161 return gsm48_lchan_modify(trans->lchan, mode->lchan_mode);
Harald Welte03740842009-06-10 23:11:52 +08003162}
3163
3164static struct downstate {
3165 u_int32_t states;
3166 int type;
3167 int (*rout) (struct gsm_trans *trans, void *arg);
3168} downstatelist[] = {
3169 /* mobile originating call establishment */
3170 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
3171 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc},
3172 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
3173 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
3174 {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 */
3175 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
3176 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
3177 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
3178 /* mobile terminating call establishment */
3179 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
3180 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
3181 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
3182 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
3183 /* signalling during call */
3184 {SBIT(GSM_CSTATE_ACTIVE),
3185 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
3186 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
3187 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
3188 {ALL_STATES,
3189 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
3190 {ALL_STATES,
3191 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
3192 {ALL_STATES,
3193 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
3194 {SBIT(GSM_CSTATE_ACTIVE),
3195 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
3196 {SBIT(GSM_CSTATE_ACTIVE),
3197 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
3198 {SBIT(GSM_CSTATE_ACTIVE),
3199 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
3200 {SBIT(GSM_CSTATE_ACTIVE),
3201 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
3202 {SBIT(GSM_CSTATE_ACTIVE),
3203 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
3204 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
3205 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
3206 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
3207 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
3208 {SBIT(GSM_CSTATE_ACTIVE),
3209 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
3210 /* clearing */
3211 {SBIT(GSM_CSTATE_INITIATED),
3212 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
3213 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
3214 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
3215 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
3216 MNCC_REL_REQ, gsm48_cc_tx_release},
3217 /* special */
3218 {ALL_STATES,
Holger Hans Peter Freythercf0f3362009-10-22 15:13:00 +02003219 MNCC_LCHAN_MODIFY, _gsm48_lchan_modify},
Harald Welte03740842009-06-10 23:11:52 +08003220};
3221
3222#define DOWNSLLEN \
3223 (sizeof(downstatelist) / sizeof(struct downstate))
3224
3225
3226int mncc_send(struct gsm_network *net, int msg_type, void *arg)
3227{
Harald Welteaa60edb2009-08-09 18:52:33 +02003228 int i, rc = 0;
Harald Welte03740842009-06-10 23:11:52 +08003229 struct gsm_trans *trans = NULL, *transt;
Harald Welteaa60edb2009-08-09 18:52:33 +02003230 struct gsm_lchan *lchan = NULL;
Harald Welte03740842009-06-10 23:11:52 +08003231 struct gsm_bts *bts = NULL;
Harald Welte03740842009-06-10 23:11:52 +08003232 struct gsm_mncc *data = arg, rel;
3233
3234 /* handle special messages */
3235 switch(msg_type) {
3236 case MNCC_BRIDGE:
3237 return tch_bridge(net, arg);
3238 case MNCC_FRAME_DROP:
3239 return tch_recv(net, arg, 0);
3240 case MNCC_FRAME_RECV:
3241 return tch_recv(net, arg, 1);
3242 case GSM_TRAU_FRAME:
3243 return tch_frame(net, arg);
3244 }
3245
3246 memset(&rel, 0, sizeof(struct gsm_mncc));
3247 rel.callref = data->callref;
3248
3249 /* Find callref */
Harald Weltec2189a62009-07-23 18:56:43 +02003250 trans = trans_find_by_callref(net, data->callref);
Harald Welte03740842009-06-10 23:11:52 +08003251
3252 /* Callref unknown */
3253 if (!trans) {
Holger Hans Peter Freythere2a3c9f2009-10-27 14:21:14 +01003254 struct gsm_subscriber *subscr;
3255
Harald Welte6e1536e2009-07-04 10:11:24 +02003256 if (msg_type != MNCC_SETUP_REQ) {
Harald Welte03740842009-06-10 23:11:52 +08003257 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3258 "Received '%s' from MNCC with "
3259 "unknown callref %d\n", data->called.number,
3260 get_mncc_name(msg_type), data->callref);
3261 /* Invalid call reference */
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08003262 return mncc_release_ind(net, NULL, data->callref,
3263 GSM48_CAUSE_LOC_PRN_S_LU,
3264 GSM48_CC_CAUSE_INVAL_TRANS_ID);
Harald Welte03740842009-06-10 23:11:52 +08003265 }
Andreas Eversberg9eaa5da2009-06-15 23:22:09 +02003266 if (!data->called.number[0] && !data->imsi[0]) {
3267 DEBUGP(DCC, "(bts - trx - ts - ti) "
3268 "Received '%s' from MNCC with "
3269 "no number or IMSI\n", get_mncc_name(msg_type));
3270 /* Invalid number */
3271 return mncc_release_ind(net, NULL, data->callref,
3272 GSM48_CAUSE_LOC_PRN_S_LU,
3273 GSM48_CC_CAUSE_INV_NR_FORMAT);
3274 }
Harald Welte03740842009-06-10 23:11:52 +08003275 /* New transaction due to setup, find subscriber */
Andreas Eversberg9eaa5da2009-06-15 23:22:09 +02003276 if (data->called.number[0])
Harald Welte75350412009-07-23 18:46:00 +02003277 subscr = subscr_get_by_extension(net,
3278 data->called.number);
Andreas Eversberg9eaa5da2009-06-15 23:22:09 +02003279 else
Harald Welte75350412009-07-23 18:46:00 +02003280 subscr = subscr_get_by_imsi(net, data->imsi);
Harald Welte03740842009-06-10 23:11:52 +08003281 /* If subscriber is not found */
3282 if (!subscr) {
3283 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3284 "Received '%s' from MNCC with "
3285 "unknown subscriber %s\n", data->called.number,
3286 get_mncc_name(msg_type), data->called.number);
3287 /* Unknown subscriber */
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08003288 return mncc_release_ind(net, NULL, data->callref,
3289 GSM48_CAUSE_LOC_PRN_S_LU,
3290 GSM48_CC_CAUSE_UNASSIGNED_NR);
Harald Welte03740842009-06-10 23:11:52 +08003291 }
3292 /* If subscriber is not "attached" */
3293 if (!subscr->lac) {
3294 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3295 "Received '%s' from MNCC with "
3296 "detached subscriber %s\n", data->called.number,
3297 get_mncc_name(msg_type), data->called.number);
3298 subscr_put(subscr);
3299 /* Temporarily out of order */
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08003300 return mncc_release_ind(net, NULL, data->callref,
3301 GSM48_CAUSE_LOC_PRN_S_LU,
3302 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte03740842009-06-10 23:11:52 +08003303 }
3304 /* Create transaction */
Harald Weltec2189a62009-07-23 18:56:43 +02003305 trans = trans_alloc(subscr, GSM48_PDISC_CC, 0xff, data->callref);
3306 if (!trans) {
Harald Welte03740842009-06-10 23:11:52 +08003307 DEBUGP(DCC, "No memory for trans.\n");
3308 subscr_put(subscr);
3309 /* Ressource unavailable */
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08003310 mncc_release_ind(net, NULL, data->callref,
3311 GSM48_CAUSE_LOC_PRN_S_LU,
3312 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte03740842009-06-10 23:11:52 +08003313 return -ENOMEM;
3314 }
Harald Welte03740842009-06-10 23:11:52 +08003315 /* Find lchan */
Harald Welteaa60edb2009-08-09 18:52:33 +02003316 lchan = lchan_for_subscr(subscr);
Harald Welte03740842009-06-10 23:11:52 +08003317 /* If subscriber has no lchan */
3318 if (!lchan) {
3319 /* find transaction with this subscriber already paging */
3320 llist_for_each_entry(transt, &net->trans_list, entry) {
3321 /* Transaction of our lchan? */
3322 if (transt == trans ||
3323 transt->subscr != subscr)
3324 continue;
3325 DEBUGP(DCC, "(bts %d trx - ts - ti -- sub %s) "
3326 "Received '%s' from MNCC with "
3327 "unallocated channel, paging already "
3328 "started.\n", bts->nr,
3329 data->called.number,
3330 get_mncc_name(msg_type));
Holger Hans Peter Freythere2a3c9f2009-10-27 14:21:14 +01003331 subscr_put(subscr);
3332 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08003333 return 0;
3334 }
3335 /* store setup informations until paging was successfull */
Harald Weltec2189a62009-07-23 18:56:43 +02003336 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
Harald Welte2a3a81b2009-08-01 19:31:47 +02003337 /* Trigger paging */
3338 paging_request(net, subscr, RSL_CHANNEED_TCH_F,
3339 setup_trig_pag_evt, subscr);
Holger Hans Peter Freythere2a3c9f2009-10-27 14:21:14 +01003340 subscr_put(subscr);
Harald Welte03740842009-06-10 23:11:52 +08003341 return 0;
3342 }
3343 /* Assign lchan */
3344 trans->lchan = lchan;
3345 use_lchan(lchan);
Holger Hans Peter Freythere2a3c9f2009-10-27 14:21:14 +01003346 subscr_put(subscr);
Harald Welte03740842009-06-10 23:11:52 +08003347 }
3348 lchan = trans->lchan;
3349
3350 /* if paging did not respond yet */
3351 if (!lchan) {
3352 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3353 "Received '%s' from MNCC in paging state\n",
3354 (trans->subscr)?(trans->subscr->extension):"-",
3355 get_mncc_name(msg_type));
Harald Weltebbc636a2009-06-11 14:23:20 +08003356 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
3357 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Harald Welte03740842009-06-10 23:11:52 +08003358 if (msg_type == MNCC_REL_REQ)
3359 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
3360 else
3361 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
3362 trans->callref = 0;
Harald Weltec2189a62009-07-23 18:56:43 +02003363 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08003364 return rc;
3365 }
3366
3367 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x sub %s) "
3368 "Received '%s' from MNCC in state %d (%s)\n",
3369 lchan->ts->trx->bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
3370 trans->transaction_id,
3371 (lchan->subscr)?(lchan->subscr->extension):"-",
Harald Weltec2189a62009-07-23 18:56:43 +02003372 get_mncc_name(msg_type), trans->cc.state,
3373 cc_state_names[trans->cc.state]);
Harald Welte03740842009-06-10 23:11:52 +08003374
3375 /* Find function for current state and message */
3376 for (i = 0; i < DOWNSLLEN; i++)
3377 if ((msg_type == downstatelist[i].type)
Harald Weltec2189a62009-07-23 18:56:43 +02003378 && ((1 << trans->cc.state) & downstatelist[i].states))
Harald Welte03740842009-06-10 23:11:52 +08003379 break;
3380 if (i == DOWNSLLEN) {
3381 DEBUGP(DCC, "Message unhandled at this state.\n");
3382 return 0;
3383 }
3384
3385 rc = downstatelist[i].rout(trans, arg);
3386
3387 return rc;
3388}
3389
3390
3391static struct datastate {
3392 u_int32_t states;
3393 int type;
3394 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
3395} datastatelist[] = {
3396 /* mobile originating call establishment */
3397 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3398 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
3399 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3400 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
3401 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
3402 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
3403 /* mobile terminating call establishment */
3404 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
3405 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
3406 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
3407 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
3408 {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 */
3409 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
3410 /* signalling during call */
3411 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
3412 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
3413 {SBIT(GSM_CSTATE_ACTIVE),
3414 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
3415 {ALL_STATES,
3416 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
3417 {ALL_STATES,
3418 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
3419 {ALL_STATES,
3420 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
3421 {SBIT(GSM_CSTATE_ACTIVE),
3422 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
3423 {SBIT(GSM_CSTATE_ACTIVE),
3424 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
3425 {SBIT(GSM_CSTATE_ACTIVE),
3426 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
3427 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3428 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
3429 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3430 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
3431 {SBIT(GSM_CSTATE_ACTIVE),
3432 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
3433 /* clearing */
3434 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
3435 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
3436 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
3437 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
3438 {ALL_STATES, /* 5.4.3.4 */
3439 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
3440};
3441
3442#define DATASLLEN \
3443 (sizeof(datastatelist) / sizeof(struct datastate))
3444
Harald Welte59b04682009-06-10 05:40:52 +08003445static int gsm0408_rcv_cc(struct msgb *msg)
3446{
3447 struct gsm48_hdr *gh = msgb_l3(msg);
3448 u_int8_t msg_type = gh->msg_type & 0xbf;
Harald Welte4861c822009-07-23 21:21:14 +02003449 u_int8_t transaction_id = ((gh->proto_discr & 0xf0) ^ 0x80) >> 4; /* flip */
Harald Welte03740842009-06-10 23:11:52 +08003450 struct gsm_lchan *lchan = msg->lchan;
Harald Weltec2189a62009-07-23 18:56:43 +02003451 struct gsm_trans *trans = NULL;
Harald Welte03740842009-06-10 23:11:52 +08003452 int i, rc = 0;
Harald Welte59b04682009-06-10 05:40:52 +08003453
Harald Welte03740842009-06-10 23:11:52 +08003454 if (msg_type & 0x80) {
3455 DEBUGP(DCC, "MSG 0x%2x not defined for PD error\n", msg_type);
3456 return -EINVAL;
Harald Welte59b04682009-06-10 05:40:52 +08003457 }
Harald Welte03740842009-06-10 23:11:52 +08003458
3459 /* Find transaction */
Harald Welte0d824162009-07-23 21:58:40 +02003460 trans = trans_find_by_id(lchan->subscr, GSM48_PDISC_CC, transaction_id);
Harald Weltec2189a62009-07-23 18:56:43 +02003461
Harald Welte4861c822009-07-23 21:21:14 +02003462 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte03740842009-06-10 23:11:52 +08003463 "Received '%s' from MS in state %d (%s)\n",
3464 lchan->ts->trx->bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
3465 transaction_id, (lchan->subscr)?(lchan->subscr->extension):"-",
Holger Hans Peter Freyther36897102009-08-01 07:26:59 +02003466 gsm0408_cc_msg_names[msg_type], trans?(trans->cc.state):0,
Harald Weltec2189a62009-07-23 18:56:43 +02003467 cc_state_names[trans?(trans->cc.state):0]);
Harald Welte03740842009-06-10 23:11:52 +08003468
3469 /* Create transaction */
3470 if (!trans) {
Harald Welte4861c822009-07-23 21:21:14 +02003471 DEBUGP(DCC, "Unknown transaction ID %x, "
Harald Welte03740842009-06-10 23:11:52 +08003472 "creating new trans.\n", transaction_id);
3473 /* Create transaction */
Harald Weltec2189a62009-07-23 18:56:43 +02003474 trans = trans_alloc(lchan->subscr, GSM48_PDISC_CC,
3475 transaction_id, new_callref++);
3476 if (!trans) {
Harald Welte03740842009-06-10 23:11:52 +08003477 DEBUGP(DCC, "No memory for trans.\n");
3478 rc = gsm48_tx_simple(msg->lchan,
Harald Welte4861c822009-07-23 21:21:14 +02003479 GSM48_PDISC_CC | (transaction_id << 4),
Harald Welte03740842009-06-10 23:11:52 +08003480 GSM48_MT_CC_RELEASE_COMPL);
3481 return -ENOMEM;
3482 }
Harald Welte03740842009-06-10 23:11:52 +08003483 /* Assign transaction */
Harald Welte03740842009-06-10 23:11:52 +08003484 trans->lchan = lchan;
3485 use_lchan(lchan);
Harald Welte03740842009-06-10 23:11:52 +08003486 }
3487
3488 /* find function for current state and message */
3489 for (i = 0; i < DATASLLEN; i++)
3490 if ((msg_type == datastatelist[i].type)
Harald Weltec2189a62009-07-23 18:56:43 +02003491 && ((1 << trans->cc.state) & datastatelist[i].states))
Harald Welte03740842009-06-10 23:11:52 +08003492 break;
3493 if (i == DATASLLEN) {
3494 DEBUGP(DCC, "Message unhandled at this state.\n");
3495 return 0;
3496 }
3497
3498 rc = datastatelist[i].rout(trans, msg);
Harald Welte59b04682009-06-10 05:40:52 +08003499
3500 return rc;
3501}
3502
3503/* 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 +02003504int gsm0408_rcvmsg(struct msgb *msg, u_int8_t link_id)
Harald Welte59b04682009-06-10 05:40:52 +08003505{
3506 struct gsm48_hdr *gh = msgb_l3(msg);
3507 u_int8_t pdisc = gh->proto_discr & 0x0f;
3508 int rc = 0;
3509
3510 switch (pdisc) {
3511 case GSM48_PDISC_CC:
3512 rc = gsm0408_rcv_cc(msg);
3513 break;
3514 case GSM48_PDISC_MM:
3515 rc = gsm0408_rcv_mm(msg);
3516 break;
3517 case GSM48_PDISC_RR:
3518 rc = gsm0408_rcv_rr(msg);
3519 break;
3520 case GSM48_PDISC_SMS:
Harald Welte (local)64994ce2009-08-14 11:41:12 +02003521 rc = gsm0411_rcv_sms(msg, link_id);
Harald Welte59b04682009-06-10 05:40:52 +08003522 break;
3523 case GSM48_PDISC_MM_GPRS:
3524 case GSM48_PDISC_SM_GPRS:
Mike Haben2f8a6852009-09-26 19:00:03 +02003525 fprintf(stderr, "Unimplemented GSM 04.08 discriminator 0x%02x\n",
Harald Welte59b04682009-06-10 05:40:52 +08003526 pdisc);
3527 break;
Harald Welte03115042009-10-16 08:32:58 +02003528 case GSM48_PDISC_NC_SS:
3529 rc = handle_rcv_ussd(msg);
3530 break;
Harald Welte59b04682009-06-10 05:40:52 +08003531 default:
Mike Haben2f8a6852009-09-26 19:00:03 +02003532 fprintf(stderr, "Unknown GSM 04.08 discriminator 0x%02x\n",
Harald Welte59b04682009-06-10 05:40:52 +08003533 pdisc);
3534 break;
3535 }
3536
3537 return rc;
3538}
3539
Harald Welte03740842009-06-10 23:11:52 +08003540/* dequeue messages to layer 4 */
3541int bsc_upqueue(struct gsm_network *net)
3542{
3543 struct gsm_mncc *mncc;
3544 struct msgb *msg;
3545 int work = 0;
3546
3547 if (net)
3548 while ((msg = msgb_dequeue(&net->upqueue))) {
3549 mncc = (struct gsm_mncc *)msg->data;
3550 if (net->mncc_recv)
3551 net->mncc_recv(net, mncc->msg_type, mncc);
3552 work = 1; /* work done */
Harald Weltebaf4d3a2009-06-26 19:40:48 +02003553 talloc_free(msg);
Harald Welte03740842009-06-10 23:11:52 +08003554 }
3555
3556 return work;
3557}
Harald Weltec2189a62009-07-23 18:56:43 +02003558
Harald Welte3c062072009-07-28 18:25:29 +02003559/*
3560 * This will be ran by the linker when loading the DSO. We use it to
3561 * do system initialization, e.g. registration of signal handlers.
3562 */
3563static __attribute__((constructor)) void on_dso_load_0408(void)
3564{
Harald Welte3c062072009-07-28 18:25:29 +02003565 register_signal_handler(SS_LCHAN, gsm0408_handle_lchan_signal, NULL);
3566 register_signal_handler(SS_ABISIP, handle_abisip_signal, NULL);
3567}