blob: 8e5bfe88fb4f9b5530778972d1e5b14d32318d90 [file] [log] [blame]
Harald Welte52b1f982008-12-23 20:25:15 +00001/* GSM Mobile Radio Interface Layer 3 messages on the A-bis interface
2 * 3GPP TS 04.08 version 7.21.0 Release 1998 / ETSI TS 100 940 V7.21.0 */
3
Harald Weltebf5e8df2009-02-03 12:59:45 +00004/* (C) 2008-2009 by Harald Welte <laforge@gnumonks.org>
Harald Welte498b0bb2009-01-09 21:27:43 +00005 * (C) 2008, 2009 by Holger Hans Peter Freyther <zecke@selfish.org>
Harald Welte8470bf22008-12-25 23:28:35 +00006 *
Harald Welte52b1f982008-12-23 20:25:15 +00007 * All Rights Reserved
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 *
23 */
24
25
26#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
29#include <errno.h>
Harald Weltedb253af2008-12-30 17:56:55 +000030#include <time.h>
Harald Welte4b634542008-12-27 01:55:51 +000031#include <netinet/in.h>
Harald Welte52b1f982008-12-23 20:25:15 +000032
Harald Welte75a983f2008-12-27 21:34:06 +000033#include <openbsc/db.h>
Harald Welte8470bf22008-12-25 23:28:35 +000034#include <openbsc/msgb.h>
Harald Welte7584aea2009-02-11 11:44:12 +000035#include <openbsc/tlv.h>
Harald Welte8470bf22008-12-25 23:28:35 +000036#include <openbsc/debug.h>
37#include <openbsc/gsm_data.h>
38#include <openbsc/gsm_subscriber.h>
Daniel Willmann8b3390e2008-12-28 00:31:09 +000039#include <openbsc/gsm_04_11.h>
Harald Welte8470bf22008-12-25 23:28:35 +000040#include <openbsc/gsm_04_08.h>
41#include <openbsc/abis_rsl.h>
Holger Freytherca362a62009-01-04 21:05:01 +000042#include <openbsc/chan_alloc.h>
Harald Welte0b4c34e2009-02-09 17:54:43 +000043#include <openbsc/paging.h>
Holger Freyther053e09d2009-02-14 22:51:06 +000044#include <openbsc/signal.h>
Harald Welte45b407a2009-05-23 15:51:12 +000045#include <openbsc/trau_frame.h>
Harald Welte11fa29c2009-02-19 17:24:39 +000046#include <openbsc/trau_mux.h>
Harald Welte52b1f982008-12-23 20:25:15 +000047
Harald Welte8470bf22008-12-25 23:28:35 +000048#define GSM48_ALLOC_SIZE 1024
49#define GSM48_ALLOC_HEADROOM 128
Harald Welte52b1f982008-12-23 20:25:15 +000050
Harald Welte0c389302009-06-10 12:08:54 +080051#define GSM_MAX_FACILITY 128
52#define GSM_MAX_SSVERSION 128
53#define GSM_MAX_USERUSER 128
54
Harald Welte09e38af2009-02-16 22:52:23 +000055static const struct tlv_definition rsl_att_tlvdef = {
56 .def = {
57 [GSM48_IE_MOBILE_ID] = { TLV_TYPE_TLV },
58 [GSM48_IE_NAME_LONG] = { TLV_TYPE_TLV },
59 [GSM48_IE_NAME_SHORT] = { TLV_TYPE_TLV },
60 [GSM48_IE_UTC] = { TLV_TYPE_TV },
61 [GSM48_IE_NET_TIME_TZ] = { TLV_TYPE_FIXED, 7 },
62 [GSM48_IE_LSA_IDENT] = { TLV_TYPE_TLV },
63
64 [GSM48_IE_BEARER_CAP] = { TLV_TYPE_TLV },
65 [GSM48_IE_CAUSE] = { TLV_TYPE_TLV },
66 [GSM48_IE_CC_CAP] = { TLV_TYPE_TLV },
67 [GSM48_IE_ALERT] = { TLV_TYPE_TLV },
68 [GSM48_IE_FACILITY] = { TLV_TYPE_TLV },
69 [GSM48_IE_PROGR_IND] = { TLV_TYPE_TLV },
70 [GSM48_IE_AUX_STATUS] = { TLV_TYPE_TLV },
Harald Welte0c389302009-06-10 12:08:54 +080071 [GSM48_IE_NOTIFY] = { TLV_TYPE_TV },
Harald Welte09e38af2009-02-16 22:52:23 +000072 [GSM48_IE_KPD_FACILITY] = { TLV_TYPE_TV },
73 [GSM48_IE_SIGNAL] = { TLV_TYPE_TV },
Harald Welte0c389302009-06-10 12:08:54 +080074 [GSM48_IE_CONN_BCD] = { TLV_TYPE_TLV },
75 [GSM48_IE_CONN_SUB] = { TLV_TYPE_TLV },
Harald Welte09e38af2009-02-16 22:52:23 +000076 [GSM48_IE_CALLING_BCD] = { TLV_TYPE_TLV },
77 [GSM48_IE_CALLING_SUB] = { TLV_TYPE_TLV },
78 [GSM48_IE_CALLED_BCD] = { TLV_TYPE_TLV },
79 [GSM48_IE_CALLED_SUB] = { TLV_TYPE_TLV },
80 [GSM48_IE_REDIR_BCD] = { TLV_TYPE_TLV },
81 [GSM48_IE_REDIR_SUB] = { TLV_TYPE_TLV },
82 [GSM48_IE_LOWL_COMPAT] = { TLV_TYPE_TLV },
83 [GSM48_IE_HIGHL_COMPAT] = { TLV_TYPE_TLV },
84 [GSM48_IE_USER_USER] = { TLV_TYPE_TLV },
85 [GSM48_IE_SS_VERS] = { TLV_TYPE_TLV },
86 [GSM48_IE_MORE_DATA] = { TLV_TYPE_T },
87 [GSM48_IE_CLIR_SUPP] = { TLV_TYPE_T },
88 [GSM48_IE_CLIR_INVOC] = { TLV_TYPE_T },
89 [GSM48_IE_REV_C_SETUP] = { TLV_TYPE_T },
Harald Welte0c389302009-06-10 12:08:54 +080090 [GSM48_IE_REPEAT_CIR] = { TLV_TYPE_T },
91 [GSM48_IE_REPEAT_SEQ] = { TLV_TYPE_T },
Harald Welte09e38af2009-02-16 22:52:23 +000092 /* FIXME: more elements */
93 },
94};
Harald Weltecf5b3592009-05-01 18:28:42 +000095
96static const char *rr_cause_names[] = {
97 [GSM48_RR_CAUSE_NORMAL] = "Normal event",
98 [GSM48_RR_CAUSE_ABNORMAL_UNSPEC] = "Abnormal release, unspecified",
99 [GSM48_RR_CAUSE_ABNORMAL_UNACCT] = "Abnormal release, channel unacceptable",
100 [GSM48_RR_CAUSE_ABNORMAL_TIMER] = "Abnormal release, timer expired",
101 [GSM48_RR_CAUSE_ABNORMAL_NOACT] = "Abnormal release, no activity on radio path",
102 [GSM48_RR_CAUSE_PREMPTIVE_REL] = "Preemptive release",
103 [GSM48_RR_CAUSE_HNDOVER_IMP] = "Handover impossible, timing advance out of range",
104 [GSM48_RR_CAUSE_CHAN_MODE_UNACCT] = "Channel mode unacceptable",
105 [GSM48_RR_CAUSE_FREQ_NOT_IMPL] = "Frequency not implemented",
106 [GSM48_RR_CAUSE_CALL_CLEARED] = "Call already cleared",
107 [GSM48_RR_CAUSE_SEMANT_INCORR] = "Semantically incorrect message",
108 [GSM48_RR_CAUSE_INVALID_MAND_INF] = "Invalid mandatory information",
109 [GSM48_RR_CAUSE_MSG_TYPE_N] = "Message type non-existant or not implemented",
110 [GSM48_RR_CAUSE_MSG_TYPE_N_COMPAT] = "Message type not compatible with protocol state",
111 [GSM48_RR_CAUSE_COND_IE_ERROR] = "Conditional IE error",
112 [GSM48_RR_CAUSE_NO_CELL_ALLOC_A] = "No cell allocation available",
113 [GSM48_RR_CAUSE_PROT_ERROR_UNSPC] = "Protocol error unspecified",
114};
115
Harald Welte4bfdfe72009-06-10 23:11:52 +0800116static const char *cc_state_names[] = {
117 "NULL",
118 "INITIATED",
119 "illegal state 2",
120 "MO_CALL_PROC",
121 "CALL_DELIVERED",
122 "illegal state 5",
123 "CALL_PRESENT",
124 "CALL_RECEIVED",
125 "CONNECT_REQUEST",
126 "MO_TERM_CALL_CONF",
127 "ACTIVE",
128 "DISCONNECT_REQ",
129 "DISCONNECT_IND",
130 "illegal state 13",
131 "illegal state 14",
132 "illegal state 15",
133 "illegal state 16",
134 "illegal state 17",
135 "illegal state 18",
136 "RELEASE_REQ",
137 "illegal state 20",
138 "illegal state 21",
139 "illegal state 22",
140 "illegal state 23",
141 "illegal state 24",
142 "illegal state 25",
143 "MO_ORIG_MODIFY",
144 "MO_TERM_MODIFY",
145 "CONNECT_IND",
146 "illegal state 29",
147 "illegal state 30",
148 "illegal state 31",
149};
150
151static const char *cc_msg_names[] = {
152 "unknown 0x00",
153 "ALERTING",
154 "CALL_PROC",
155 "PROGRESS",
156 "ESTAB",
157 "SETUP",
158 "ESTAB_CONF",
159 "CONNECT",
160 "CALL_CONF",
161 "START_CC",
162 "unknown 0x0a",
163 "RECALL",
164 "unknown 0x0c",
165 "unknown 0x0d",
166 "EMERG_SETUP",
167 "CONNECT_ACK",
168 "USER_INFO",
169 "unknown 0x11",
170 "unknown 0x12",
171 "MODIFY_REJECT",
172 "unknown 0x14",
173 "unknown 0x15",
174 "unknown 0x16",
175 "MODIFY",
176 "HOLD",
177 "HOLD_ACK",
178 "HOLD_REJ",
179 "unknown 0x1b",
180 "RETR",
181 "RETR_ACK",
182 "RETR_REJ",
183 "MODIFY_COMPL",
184 "unknown 0x20",
185 "unknown 0x21",
186 "unknown 0x22",
187 "unknown 0x23",
188 "unknown 0x24",
189 "DISCONNECT",
190 "unknown 0x26",
191 "unknown 0x27",
192 "unknown 0x28",
193 "unknown 0x29",
194 "RELEASE_COMPL",
195 "unknown 0x2b",
196 "unknown 0x2c",
197 "RELEASE",
198 "unknown 0x2e",
199 "unknown 0x2f",
200 "unknown 0x30",
201 "STOP_DTMF",
202 "STOP_DTMF_ACK",
203 "unknown 0x33",
204 "STATUS_ENQ",
205 "START_DTMF",
206 "START_DTMF_ACK",
207 "START_DTMF_REJ",
208 "unknown 0x38",
209 "CONG_CTRL",
210 "FACILITY",
211 "unknown 0x3b",
212 "STATUS",
213 "unknown 0x3c",
214 "NOTIFY",
215 "unknown 0x3f",
216};
217
Harald Weltecf5b3592009-05-01 18:28:42 +0000218static char strbuf[64];
219
220static const char *rr_cause_name(u_int8_t cause)
221{
222 if (cause < ARRAY_SIZE(rr_cause_names) &&
223 rr_cause_names[cause])
224 return rr_cause_names[cause];
225
226 snprintf(strbuf, sizeof(strbuf), "0x%02x", cause);
227 return strbuf;
228}
229
Harald Weltef7c43522009-06-09 20:24:21 +0000230static void parse_meas_rep(struct gsm_meas_rep *rep, const u_int8_t *data,
231 int len)
232{
233 memset(rep, 0, sizeof(*rep));
234
235 if (data[0] & 0x80)
236 rep->flags |= MEAS_REP_F_BA1;
237 if (data[0] & 0x40)
238 rep->flags |= MEAS_REP_F_DTX;
239 if (data[1] & 0x40)
240 rep->flags |= MEAS_REP_F_VALID;
241
242 rep->rxlev_full = data[0] & 0x3f;
243 rep->rxlev_sub = data[1] & 0x3f;
244 rep->rxqual_full = (data[3] >> 4) & 0x7;
245 rep->rxqual_sub = (data[3] >> 1) & 0x7;
246 rep->num_cell = data[4] >> 6 | ((data[3] & 0x01) << 2);
247 if (rep->num_cell < 1)
248 return;
249
250 /* an encoding nightmare in perfection */
251
252 rep->cell[0].rxlev = data[4] & 0x3f;
253 rep->cell[0].bcch_freq = data[5] >> 2;
254 rep->cell[0].bsic = ((data[5] & 0x03) << 3) | (data[6] >> 5);
255 if (rep->num_cell < 2)
256 return;
257
258 rep->cell[1].rxlev = ((data[6] & 0x1f) << 1) | (data[7] >> 7);
259 rep->cell[1].bcch_freq = (data[7] >> 2) & 0x1f;
260 rep->cell[1].bsic = ((data[7] & 0x03) << 4) | (data[8] >> 4);
261 if (rep->num_cell < 3)
262 return;
263
264 rep->cell[2].rxlev = ((data[8] & 0x0f) << 2) | (data[9] >> 6);
265 rep->cell[2].bcch_freq = (data[9] >> 1) & 0x1f;
266 rep->cell[2].bsic = ((data[9] & 0x01) << 6) | (data[10] >> 3);
267 if (rep->num_cell < 4)
268 return;
269
270 rep->cell[3].rxlev = ((data[10] & 0x07) << 3) | (data[11] >> 5);
271 rep->cell[3].bcch_freq = data[11] & 0x1f;
272 rep->cell[3].bsic = data[12] >> 2;
273 if (rep->num_cell < 5)
274 return;
275
276 rep->cell[4].rxlev = ((data[12] & 0x03) << 4) | (data[13] >> 4);
277 rep->cell[4].bcch_freq = ((data[13] & 0xf) << 1) | (data[14] >> 7);
278 rep->cell[4].bsic = (data[14] >> 1) & 0x3f;
279 if (rep->num_cell < 6)
280 return;
281
282 rep->cell[5].rxlev = ((data[14] & 0x01) << 5) | (data[15] >> 3);
283 rep->cell[5].bcch_freq = ((data[15] & 0x07) << 2) | (data[16] >> 6);
284 rep->cell[5].bsic = data[16] & 0x3f;
285}
286
Holger Freytherd51524f2009-06-09 08:27:07 +0000287int gsm0408_loc_upd_acc(struct gsm_lchan *lchan, u_int32_t tmsi);
Harald Welte65e74cc2008-12-29 01:55:35 +0000288static int gsm48_tx_simple(struct gsm_lchan *lchan,
289 u_int8_t pdisc, u_int8_t msg_type);
Holger Freytherb7193e42008-12-29 17:44:08 +0000290static void schedule_reject(struct gsm_lchan *lchan);
Harald Welte4bfdfe72009-06-10 23:11:52 +0800291void free_trans(struct gsm_trans *trans);
Harald Welte65e74cc2008-12-29 01:55:35 +0000292
Harald Welte52b1f982008-12-23 20:25:15 +0000293struct gsm_lai {
294 u_int16_t mcc;
295 u_int16_t mnc;
296 u_int16_t lac;
297};
298
Holger Freyther89824fc2008-12-30 16:18:18 +0000299static int authorize_everonye = 0;
300void gsm0408_allow_everyone(int everyone)
301{
302 printf("Allowing everyone?\n");
303 authorize_everonye = everyone;
304}
305
Holger Freythere97f7fb2008-12-31 18:52:11 +0000306static int reject_cause = 0;
307void gsm0408_set_reject_cause(int cause)
308{
309 reject_cause = cause;
310}
311
Harald Welte4bfdfe72009-06-10 23:11:52 +0800312static u_int32_t new_callref = 0x80000001;
313
Holger Freyther73487a22008-12-31 18:53:57 +0000314static int authorize_subscriber(struct gsm_loc_updating_operation *loc,
315 struct gsm_subscriber *subscriber)
Holger Freyther89824fc2008-12-30 16:18:18 +0000316{
317 if (!subscriber)
318 return 0;
319
Holger Freyther73487a22008-12-31 18:53:57 +0000320 /*
321 * Do not send accept yet as more information should arrive. Some
322 * phones will not send us the information and we will have to check
323 * what we want to do with that.
324 */
325 if (loc && (loc->waiting_for_imsi || loc->waiting_for_imei))
326 return 0;
327
Holger Freyther89824fc2008-12-30 16:18:18 +0000328 if (authorize_everonye)
329 return 1;
330
331 return subscriber->authorized;
332}
Holger Freyther07cc8d82008-12-29 06:23:46 +0000333
Holger Freyther73487a22008-12-31 18:53:57 +0000334static void release_loc_updating_req(struct gsm_lchan *lchan)
335{
Harald Welte179f0642008-12-31 23:59:18 +0000336 if (!lchan->loc_operation)
Holger Freyther73487a22008-12-31 18:53:57 +0000337 return;
338
Harald Welteff117a82009-05-23 05:22:08 +0000339 bsc_del_timer(&lchan->loc_operation->updating_timer);
Holger Freyther73487a22008-12-31 18:53:57 +0000340 free(lchan->loc_operation);
341 lchan->loc_operation = 0;
Holger Freyther3eaa7922009-01-01 02:59:03 +0000342 put_lchan(lchan);
Holger Freyther73487a22008-12-31 18:53:57 +0000343}
344
345static void allocate_loc_updating_req(struct gsm_lchan *lchan)
346{
Holger Freyther67b4b9a2009-01-01 03:46:11 +0000347 use_lchan(lchan);
Holger Freyther73487a22008-12-31 18:53:57 +0000348 release_loc_updating_req(lchan);
349
350 lchan->loc_operation = (struct gsm_loc_updating_operation *)
351 malloc(sizeof(*lchan->loc_operation));
352 memset(lchan->loc_operation, 0, sizeof(*lchan->loc_operation));
353}
Holger Freyther07cc8d82008-12-29 06:23:46 +0000354
Holger Freytherd51524f2009-06-09 08:27:07 +0000355static int gsm0408_authorize(struct gsm_lchan *lchan, struct msgb *msg)
356{
357 u_int32_t tmsi;
358
359 if (authorize_subscriber(lchan->loc_operation, lchan->subscr)) {
360 db_subscriber_alloc_tmsi(lchan->subscr);
361 subscr_update(lchan->subscr, msg->trx->bts, GSM_SUBSCRIBER_UPDATE_ATTACHED);
362 tmsi = strtoul(lchan->subscr->tmsi, NULL, 10);
363 release_loc_updating_req(lchan);
364 return gsm0408_loc_upd_acc(msg->lchan, tmsi);
365 }
366
367 return 0;
368}
369
Holger Freyther7c19f742009-06-06 13:54:35 +0000370static int gsm0408_handle_lchan_signal(unsigned int subsys, unsigned int signal,
371 void *handler_data, void *signal_data)
372{
Harald Welte4bfdfe72009-06-10 23:11:52 +0800373 struct gsm_trans *trans, *temp;
374
Holger Freyther7c19f742009-06-06 13:54:35 +0000375 if (subsys != SS_LCHAN || signal != S_LCHAN_UNEXPECTED_RELEASE)
376 return 0;
377
378 /*
379 * Cancel any outstanding location updating request
380 * operation taking place on the lchan.
381 */
382 struct gsm_lchan *lchan = (struct gsm_lchan *)handler_data;
383 release_loc_updating_req(lchan);
384
Harald Welte4bfdfe72009-06-10 23:11:52 +0800385 /* Free all transactions that are associated with the released lchan */
386 llist_for_each_entry_safe(trans, temp, &lchan->ts->trx->bts->network->trans_list, entry) {
387 if (trans->lchan == lchan)
388 free_trans(trans);
389 }
390
Holger Freyther7c19f742009-06-06 13:54:35 +0000391 return 0;
392}
393
394/*
395 * This will be ran by the linker when loading the DSO. We use it to
396 * do system initialization, e.g. registration of signal handlers.
397 */
398static __attribute__((constructor)) void on_dso_load_0408(void)
399{
400 register_signal_handler(SS_LCHAN, gsm0408_handle_lchan_signal, NULL);
401}
402
Harald Welte52b1f982008-12-23 20:25:15 +0000403static void to_bcd(u_int8_t *bcd, u_int16_t val)
404{
Harald Welte4b634542008-12-27 01:55:51 +0000405 bcd[2] = val % 10;
Harald Welte52b1f982008-12-23 20:25:15 +0000406 val = val / 10;
407 bcd[1] = val % 10;
408 val = val / 10;
Harald Welte4b634542008-12-27 01:55:51 +0000409 bcd[0] = val % 10;
Harald Welte52b1f982008-12-23 20:25:15 +0000410 val = val / 10;
411}
412
Holger Freyther17746612008-12-28 16:32:44 +0000413void gsm0408_generate_lai(struct gsm48_loc_area_id *lai48, u_int16_t mcc,
Harald Welte52b1f982008-12-23 20:25:15 +0000414 u_int16_t mnc, u_int16_t lac)
415{
416 u_int8_t bcd[3];
417
418 to_bcd(bcd, mcc);
419 lai48->digits[0] = bcd[0] | (bcd[1] << 4);
420 lai48->digits[1] = bcd[2];
421
422 to_bcd(bcd, mnc);
Harald Welte4b634542008-12-27 01:55:51 +0000423 /* FIXME: do we need three-digit MNC? See Table 10.5.3 */
424#if 0
Harald Welte8470bf22008-12-25 23:28:35 +0000425 lai48->digits[1] |= bcd[2] << 4;
426 lai48->digits[2] = bcd[0] | (bcd[1] << 4);
Harald Welte4b634542008-12-27 01:55:51 +0000427#else
428 lai48->digits[1] |= 0xf << 4;
429 lai48->digits[2] = bcd[1] | (bcd[2] << 4);
430#endif
Harald Welte52b1f982008-12-23 20:25:15 +0000431
Harald Welte4b634542008-12-27 01:55:51 +0000432 lai48->lac = htons(lac);
Harald Welte52b1f982008-12-23 20:25:15 +0000433}
434
Harald Welte255539c2008-12-28 02:26:27 +0000435#define TMSI_LEN 5
Harald Welte52b1f982008-12-23 20:25:15 +0000436#define MID_TMSI_LEN (TMSI_LEN + 2)
437
Harald Welte255539c2008-12-28 02:26:27 +0000438int generate_mid_from_tmsi(u_int8_t *buf, u_int32_t tmsi)
Harald Welte52b1f982008-12-23 20:25:15 +0000439{
Harald Welte65e74cc2008-12-29 01:55:35 +0000440 u_int32_t *tptr = (u_int32_t *) &buf[3];
Harald Welte255539c2008-12-28 02:26:27 +0000441
Harald Welte4b634542008-12-27 01:55:51 +0000442 buf[0] = GSM48_IE_MOBILE_ID;
Harald Welte1a412182008-12-27 22:13:43 +0000443 buf[1] = TMSI_LEN;
Harald Welte4b634542008-12-27 01:55:51 +0000444 buf[2] = 0xf0 | GSM_MI_TYPE_TMSI;
Harald Welte255539c2008-12-28 02:26:27 +0000445 *tptr = htonl(tmsi);
446
447 return 7;
Harald Welte52b1f982008-12-23 20:25:15 +0000448}
449
Harald Welte09e38af2009-02-16 22:52:23 +0000450static const char bcd_num_digits[] = {
451 '0', '1', '2', '3', '4', '5', '6', '7',
452 '8', '9', '*', '#', 'a', 'b', 'c', '\0'
453};
454
Harald Welte0c389302009-06-10 12:08:54 +0800455/* decode a 'called/calling/connect party BCD number' as in 10.5.4.7 */
456int decode_bcd_number(char *output, int output_len, const u_int8_t *bcd_lv,
457 int h_len)
Harald Welte09e38af2009-02-16 22:52:23 +0000458{
459 u_int8_t in_len = bcd_lv[0];
460 int i;
461
Harald Welte0c389302009-06-10 12:08:54 +0800462 for (i = 1 + h_len; i <= in_len; i++) {
Harald Welte09e38af2009-02-16 22:52:23 +0000463 /* lower nibble */
464 output_len--;
465 if (output_len <= 1)
466 break;
467 *output++ = bcd_num_digits[bcd_lv[i] & 0xf];
468
469 /* higher nibble */
470 output_len--;
471 if (output_len <= 1)
472 break;
473 *output++ = bcd_num_digits[bcd_lv[i] >> 4];
474 }
475 if (output_len >= 1)
476 *output++ = '\0';
477
Harald Welte0c389302009-06-10 12:08:54 +0800478 return 0;
Harald Welte09e38af2009-02-16 22:52:23 +0000479}
480
481/* convert a single ASCII character to call-control BCD */
482static int asc_to_bcd(const char asc)
483{
484 int i;
485
486 for (i = 0; i < ARRAY_SIZE(bcd_num_digits); i++) {
487 if (bcd_num_digits[i] == asc)
488 return i;
489 }
490 return -EINVAL;
491}
492
Harald Welte0c389302009-06-10 12:08:54 +0800493/* convert a ASCII phone number to 'called/calling/connect party BCD number' */
Harald Welte09e38af2009-02-16 22:52:23 +0000494int encode_bcd_number(u_int8_t *bcd_lv, u_int8_t max_len,
Harald Welte0c389302009-06-10 12:08:54 +0800495 int h_len, const char *input)
Harald Welte09e38af2009-02-16 22:52:23 +0000496{
497 int in_len = strlen(input);
498 int i;
Harald Welte0c389302009-06-10 12:08:54 +0800499 u_int8_t *bcd_cur = bcd_lv + 1 + h_len;
Harald Welte09e38af2009-02-16 22:52:23 +0000500
501 /* two digits per byte, plus type byte */
Harald Welte0c389302009-06-10 12:08:54 +0800502 bcd_lv[0] = in_len/2 + h_len;
Harald Welte09e38af2009-02-16 22:52:23 +0000503 if (in_len % 2)
504 bcd_lv[0]++;
505
Harald Welte0c389302009-06-10 12:08:54 +0800506 if (bcd_lv[0] > max_len)
507 return -EIO;
Harald Welte09e38af2009-02-16 22:52:23 +0000508
509 for (i = 0; i < in_len; i++) {
510 int rc = asc_to_bcd(input[i]);
511 if (rc < 0)
512 return rc;
513 if (i % 2 == 0)
514 *bcd_cur = rc;
515 else
516 *bcd_cur++ |= (rc << 4);
517 }
518 /* append padding nibble in case of odd length */
519 if (i % 2)
520 *bcd_cur++ |= 0xf0;
521
522 /* return how many bytes we used */
523 return (bcd_cur - bcd_lv);
524}
525
Harald Welte0c389302009-06-10 12:08:54 +0800526/* decode 'bearer capability' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800527static int decode_bearer_cap(struct gsm_mncc_bearer_cap *bcap,
Harald Welte0c389302009-06-10 12:08:54 +0800528 const u_int8_t *lv)
529{
530 u_int8_t in_len = lv[0];
531 int i, s;
532
533 if (in_len < 1)
534 return -EINVAL;
535
Harald Welte4bfdfe72009-06-10 23:11:52 +0800536 bcap->speech_ver[0] = -1; /* end of list, of maximum 7 values */
Harald Welte0c389302009-06-10 12:08:54 +0800537
538 /* octet 3 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800539 bcap->transfer = lv[1] & 0x07;
540 bcap->mode = (lv[1] & 0x08) >> 3;
541 bcap->coding = (lv[1] & 0x10) >> 4;
542 bcap->radio = (lv[1] & 0x60) >> 5;
Harald Welte0c389302009-06-10 12:08:54 +0800543
544 i = 1;
545 s = 0;
546 while(!(lv[i] & 0x80)) {
547 i++; /* octet 3a etc */
548 if (in_len < i)
549 return 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800550 bcap->speech_ver[s++] = lv[i] & 0x0f;
551 bcap->speech_ver[s] = -1; /* end of list */
Harald Welte0c389302009-06-10 12:08:54 +0800552 if (i == 2) /* octet 3a */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800553 bcap->speech_ctm = (lv[i] & 0x20) >> 5;
Harald Welte0c389302009-06-10 12:08:54 +0800554 if (s == 7) /* maximum speech versions + end of list */
555 return 0;
556 }
557
558 return 0;
559}
560
561/* encode 'bearer capability' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800562static int encode_bearer_cap(struct msgb *msg, int lv_only,
563 const struct gsm_mncc_bearer_cap *bcap)
Harald Welte0c389302009-06-10 12:08:54 +0800564{
565 u_int8_t lv[32 + 1];
566 int i, s;
567
Harald Welte4bfdfe72009-06-10 23:11:52 +0800568 lv[1] = bcap->transfer;
569 lv[1] |= bcap->mode << 3;
570 lv[1] |= bcap->coding << 4;
571 lv[1] |= bcap->radio << 5;
Harald Welte0c389302009-06-10 12:08:54 +0800572
573 i = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800574 for (s = 0; bcap->speech_ver[s] >= 0; s++) {
Harald Welte0c389302009-06-10 12:08:54 +0800575 i++; /* octet 3a etc */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800576 lv[i] = bcap->speech_ver[s];
Harald Welte0c389302009-06-10 12:08:54 +0800577 if (i == 2) /* octet 3a */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800578 lv[i] |= bcap->speech_ctm << 5;
Harald Welte0c389302009-06-10 12:08:54 +0800579 }
580 lv[i] |= 0x80; /* last IE of octet 3 etc */
581
582 lv[0] = i;
583 if (lv_only)
584 msgb_lv_put(msg, lv[0], lv+1);
585 else
586 msgb_tlv_put(msg, GSM48_IE_BEARER_CAP, lv[0], lv+1);
587
588 return 0;
589}
590
591/* decode 'call control cap' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800592static int decode_cccap(struct gsm_mncc_cccap *ccap, const u_int8_t *lv)
Harald Welte0c389302009-06-10 12:08:54 +0800593{
594 u_int8_t in_len = lv[0];
595
596 if (in_len < 1)
597 return -EINVAL;
598
599 /* octet 3 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800600 ccap->dtmf = lv[1] & 0x01;
601 ccap->pcp = (lv[1] & 0x02) >> 1;
Harald Welte0c389302009-06-10 12:08:54 +0800602
603 return 0;
604}
605
606/* decode 'called party BCD number' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800607static int decode_called(struct gsm_mncc_number *called,
608 const u_int8_t *lv)
Harald Welte0c389302009-06-10 12:08:54 +0800609{
610 u_int8_t in_len = lv[0];
611
612 if (in_len < 1)
613 return -EINVAL;
614
615 /* octet 3 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800616 called->plan = lv[1] & 0x0f;
617 called->type = (lv[1] & 0x70) >> 4;
Harald Welte0c389302009-06-10 12:08:54 +0800618
619 /* octet 4..N */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800620 decode_bcd_number(called->number, sizeof(called->number), lv, 1);
Harald Welte0c389302009-06-10 12:08:54 +0800621
622 return 0;
623}
624
625/* encode 'called party BCD number' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800626static int encode_called(struct msgb *msg,
627 const struct gsm_mncc_number *called)
Harald Welte0c389302009-06-10 12:08:54 +0800628{
629 u_int8_t lv[18];
630 int ret;
631
632 /* octet 3 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800633 lv[1] = called->plan;
634 lv[1] |= called->type << 4;
Harald Welte0c389302009-06-10 12:08:54 +0800635
636 /* octet 4..N, octet 2 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800637 ret = encode_bcd_number(lv, sizeof(lv), 1, called->number);
Harald Welte0c389302009-06-10 12:08:54 +0800638 if (ret < 0)
639 return ret;
640
641 msgb_tlv_put(msg, GSM48_IE_CALLED_BCD, lv[0], lv+1);
642
643 return 0;
644}
645
646/* encode callerid of various IEs */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800647static int encode_callerid(struct msgb *msg, int ie,
648 const struct gsm_mncc_number *callerid)
Harald Welte0c389302009-06-10 12:08:54 +0800649{
650 u_int8_t lv[13];
651 int h_len = 1;
652 int ret;
653
654 /* octet 3 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800655 lv[1] = callerid->plan;
656 lv[1] |= callerid->type << 4;
Harald Welte0c389302009-06-10 12:08:54 +0800657
Harald Welte4bfdfe72009-06-10 23:11:52 +0800658 if (callerid->present || callerid->screen) {
Harald Welte0c389302009-06-10 12:08:54 +0800659 /* octet 3a */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800660 lv[2] = callerid->screen;
661 lv[2] |= callerid->present << 5;
Harald Welte0c389302009-06-10 12:08:54 +0800662 lv[2] |= 0x80;
663 h_len++;
664 } else
665 lv[1] |= 0x80;
666
667 /* octet 4..N, octet 2 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800668 ret = encode_bcd_number(lv, sizeof(lv), h_len, callerid->number);
Harald Welte0c389302009-06-10 12:08:54 +0800669 if (ret < 0)
670 return ret;
671
672 msgb_tlv_put(msg, ie, lv[0], lv+1);
673
674 return 0;
675}
676
677/* decode 'cause' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800678static int decode_cause(struct gsm_mncc_cause *cause,
Harald Welte0c389302009-06-10 12:08:54 +0800679 const u_int8_t *lv)
680{
681 u_int8_t in_len = lv[0];
682 int i;
683
684 if (in_len < 2)
685 return -EINVAL;
686
Harald Welte4bfdfe72009-06-10 23:11:52 +0800687 cause->diag_len = 0;
Harald Welte0c389302009-06-10 12:08:54 +0800688
689 /* octet 3 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800690 cause->location = lv[1] & 0x0f;
691 cause->coding = (lv[1] & 0x60) >> 5;
Harald Welte0c389302009-06-10 12:08:54 +0800692
693 i = 1;
694 if (!(lv[i] & 0x80)) {
695 i++; /* octet 3a */
696 if (in_len < i+1)
697 return 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800698 cause->rec = 1;
699 cause->rec_val = lv[i] & 0x7f;
Harald Welte0c389302009-06-10 12:08:54 +0800700
701 }
702 i++;
703
704 /* octet 4 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800705 cause->value = lv[i] & 0x7f;
Harald Welte0c389302009-06-10 12:08:54 +0800706 i++;
707
708 if (in_len < i) /* no diag */
709 return 0;
710
711 if (in_len - (i-1) > 32) /* maximum 32 octets */
712 return 0;
713
714 /* octet 5-N */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800715 memcpy(cause->diag, lv + i, in_len - (i-1));
716 cause->diag_len = in_len - (i-1);
Harald Welte0c389302009-06-10 12:08:54 +0800717
718 return 0;
719}
720
721/* encode 'cause' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800722static int encode_cause(struct msgb *msg, int lv_only,
723 const struct gsm_mncc_cause *cause)
Harald Welte0c389302009-06-10 12:08:54 +0800724{
725 u_int8_t lv[32+4];
726 int i;
727
Harald Welte4bfdfe72009-06-10 23:11:52 +0800728 if (cause->diag_len > 32)
Harald Welte0c389302009-06-10 12:08:54 +0800729 return -EINVAL;
730
731 /* octet 3 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800732 lv[1] = cause->location;
733 lv[1] |= cause->coding << 5;
Harald Welte0c389302009-06-10 12:08:54 +0800734
735 i = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800736 if (cause->rec) {
Harald Welte0c389302009-06-10 12:08:54 +0800737 i++; /* octet 3a */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800738 lv[i] = cause->rec_val;
Harald Welte0c389302009-06-10 12:08:54 +0800739 }
740 lv[i] |= 0x80; /* end of octet 3 */
741
742 /* octet 4 */
743 i++;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800744 lv[i] = 0x80 | cause->value;
Harald Welte0c389302009-06-10 12:08:54 +0800745
746 /* octet 5-N */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800747 if (cause->diag_len) {
748 memcpy(lv + i, cause->diag, cause->diag_len);
749 i += cause->diag_len;
Harald Welte0c389302009-06-10 12:08:54 +0800750 }
751
752 lv[0] = i;
753 if (lv_only)
754 msgb_lv_put(msg, lv[0], lv+1);
755 else
756 msgb_tlv_put(msg, GSM48_IE_CAUSE, lv[0], lv+1);
757
758 return 0;
759}
760
761/* encode 'calling number' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800762static int encode_calling(struct msgb *msg,
763 const struct gsm_mncc_number *calling)
Harald Welte0c389302009-06-10 12:08:54 +0800764{
Harald Welte4bfdfe72009-06-10 23:11:52 +0800765 return encode_callerid(msg, GSM48_IE_CALLING_BCD, calling);
Harald Welte0c389302009-06-10 12:08:54 +0800766}
767
768/* encode 'connected number' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800769static int encode_connected(struct msgb *msg,
770 const struct gsm_mncc_number *connected)
Harald Welte0c389302009-06-10 12:08:54 +0800771{
Harald Welte4bfdfe72009-06-10 23:11:52 +0800772 return encode_callerid(msg, GSM48_IE_CONN_BCD, connected);
Harald Welte0c389302009-06-10 12:08:54 +0800773}
774
775/* encode 'redirecting number' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800776static int encode_redirecting(struct msgb *msg,
777 const struct gsm_mncc_number *redirecting)
Harald Welte0c389302009-06-10 12:08:54 +0800778{
Harald Welte4bfdfe72009-06-10 23:11:52 +0800779 return encode_callerid(msg, GSM48_IE_REDIR_BCD, redirecting);
Harald Welte0c389302009-06-10 12:08:54 +0800780}
781
782/* decode 'facility' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800783static int decode_facility(struct gsm_mncc_facility *facility,
Harald Welte0c389302009-06-10 12:08:54 +0800784 const u_int8_t *lv)
785{
786 u_int8_t in_len = lv[0];
787
788 if (in_len < 1)
789 return -EINVAL;
790
Harald Welte4bfdfe72009-06-10 23:11:52 +0800791 if (in_len > sizeof(facility->info))
Harald Welte0c389302009-06-10 12:08:54 +0800792 return -EINVAL;
793
Harald Welte4bfdfe72009-06-10 23:11:52 +0800794 memcpy(facility->info, lv+1, in_len);
795 facility->len = in_len;
Harald Welte0c389302009-06-10 12:08:54 +0800796
797 return 0;
798}
799
800/* encode 'facility' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800801static int encode_facility(struct msgb *msg, int lv_only,
802 const struct gsm_mncc_facility *facility)
Harald Welte0c389302009-06-10 12:08:54 +0800803{
804 u_int8_t lv[GSM_MAX_FACILITY + 1];
805
Harald Welte4bfdfe72009-06-10 23:11:52 +0800806 if (facility->len < 1 || facility->len > GSM_MAX_FACILITY)
Harald Welte0c389302009-06-10 12:08:54 +0800807 return -EINVAL;
808
Harald Welte4bfdfe72009-06-10 23:11:52 +0800809 memcpy(lv+1, facility->info, facility->len);
810 lv[0] = facility->len;
Harald Welte0c389302009-06-10 12:08:54 +0800811 if (lv_only)
812 msgb_lv_put(msg, lv[0], lv+1);
813 else
814 msgb_tlv_put(msg, GSM48_IE_FACILITY, lv[0], lv+1);
815
816 return 0;
817}
818
819/* decode 'notify' */
820static int decode_notify(int *notify, const u_int8_t *v)
821{
822 *notify = v[0] & 0x7f;
823
824 return 0;
825}
826
827/* encode 'notify' */
828static int encode_notify(struct msgb *msg, int notify)
829{
830 msgb_v_put(msg, notify | 0x80);
831
832 return 0;
833}
834
835/* encode 'signal' */
836static int encode_signal(struct msgb *msg, int signal)
837{
838 msgb_tv_put(msg, GSM48_IE_SIGNAL, signal);
839
840 return 0;
841}
842
843/* decode 'keypad' */
844static int decode_keypad(int *keypad, const u_int8_t *lv)
845{
846 u_int8_t in_len = lv[0];
847
848 if (in_len < 1)
849 return -EINVAL;
850
851 *keypad = lv[1] & 0x7f;
852
853 return 0;
854}
855
856/* encode 'keypad' */
857static int encode_keypad(struct msgb *msg, int keypad)
858{
859 msgb_tv_put(msg, GSM48_IE_KPD_FACILITY, keypad);
860
861 return 0;
862}
863
864/* decode 'progress' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800865static int decode_progress(struct gsm_mncc_progress *progress,
Harald Welte0c389302009-06-10 12:08:54 +0800866 const u_int8_t *lv)
867{
868 u_int8_t in_len = lv[0];
869
870 if (in_len < 2)
871 return -EINVAL;
872
Harald Welte4bfdfe72009-06-10 23:11:52 +0800873 progress->coding = (lv[1] & 0x60) >> 5;
874 progress->location = lv[1] & 0x0f;
875 progress->descr = lv[2] & 0x7f;
Harald Welte0c389302009-06-10 12:08:54 +0800876
877 return 0;
878}
879
880/* encode 'progress' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800881static int encode_progress(struct msgb *msg, int lv_only,
882 const struct gsm_mncc_progress *p)
Harald Welte0c389302009-06-10 12:08:54 +0800883{
884 u_int8_t lv[3];
885
886 lv[0] = 2;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800887 lv[1] = 0x80 | ((p->coding & 0x3) << 5) | (p->location & 0xf);
888 lv[2] = 0x80 | (p->descr & 0x7f);
Harald Welte0c389302009-06-10 12:08:54 +0800889 if (lv_only)
890 msgb_lv_put(msg, lv[0], lv+1);
891 else
892 msgb_tlv_put(msg, GSM48_IE_PROGR_IND, lv[0], lv+1);
893
894 return 0;
895}
896
897/* decode 'user-user' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800898static int decode_useruser(struct gsm_mncc_useruser *uu,
Harald Welte0c389302009-06-10 12:08:54 +0800899 const u_int8_t *lv)
900{
901 u_int8_t in_len = lv[0];
Harald Welte4bfdfe72009-06-10 23:11:52 +0800902 char *info = uu->info;
903 int info_len = sizeof(uu->info);
Harald Welte0c389302009-06-10 12:08:54 +0800904 int i;
905
906 if (in_len < 1)
907 return -EINVAL;
908
Harald Welte4bfdfe72009-06-10 23:11:52 +0800909 uu->proto = lv[1];
Harald Welte0c389302009-06-10 12:08:54 +0800910
911 for (i = 2; i <= in_len; i++) {
912 info_len--;
913 if (info_len <= 1)
914 break;
915 *info++ = lv[i];
916 }
917 if (info_len >= 1)
918 *info++ = '\0';
919
920 return 0;
921}
922
923/* encode 'useruser' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800924static int encode_useruser(struct msgb *msg, int lv_only,
925 const struct gsm_mncc_useruser *uu)
Harald Welte0c389302009-06-10 12:08:54 +0800926{
927 u_int8_t lv[GSM_MAX_USERUSER + 2];
928
Harald Welte4bfdfe72009-06-10 23:11:52 +0800929 if (strlen(uu->info) > GSM_MAX_USERUSER)
Harald Welte0c389302009-06-10 12:08:54 +0800930 return -EINVAL;
931
Harald Welte4bfdfe72009-06-10 23:11:52 +0800932 lv[0] = 1 + strlen(uu->info);
933 lv[1] = uu->proto;
934 memcpy(lv + 2, uu->info, strlen(uu->info));
Harald Welte0c389302009-06-10 12:08:54 +0800935 if (lv_only)
936 msgb_lv_put(msg, lv[0], lv+1);
937 else
938 msgb_tlv_put(msg, GSM48_IE_USER_USER, lv[0], lv+1);
939
940 return 0;
941}
942
943/* decode 'ss version' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800944static int decode_ssversion(struct gsm_mncc_ssversion *ssv,
Harald Welte0c389302009-06-10 12:08:54 +0800945 const u_int8_t *lv)
946{
947 u_int8_t in_len = lv[0];
948
Harald Welte4bfdfe72009-06-10 23:11:52 +0800949 if (in_len < 1 || in_len < sizeof(ssv->info))
Harald Welte0c389302009-06-10 12:08:54 +0800950 return -EINVAL;
951
Harald Welte4bfdfe72009-06-10 23:11:52 +0800952 memcpy(ssv->info, lv + 1, in_len);
953 ssv->len = in_len;
Harald Welte0c389302009-06-10 12:08:54 +0800954
955 return 0;
956}
957
958/* encode 'more data' */
959static int encode_more(struct msgb *msg)
960{
961 u_int8_t *ie;
962
963 ie = msgb_put(msg, 1);
964 ie[0] = GSM48_IE_MORE_DATA;
965
966 return 0;
967}
968
Holger Freyther819dd202009-01-04 03:52:50 +0000969struct msgb *gsm48_msgb_alloc(void)
Harald Welte8470bf22008-12-25 23:28:35 +0000970{
971 return msgb_alloc_headroom(GSM48_ALLOC_SIZE, GSM48_ALLOC_HEADROOM);
972}
973
Holger Freyther3e2c3232009-01-04 03:55:31 +0000974int gsm48_sendmsg(struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000975{
Harald Welte65e74cc2008-12-29 01:55:35 +0000976 if (msg->lchan) {
Harald Welte4bfdfe72009-06-10 23:11:52 +0800977 struct gsm48_hdr *gh = (struct gsm48_hdr *) msg->data;
Harald Welte8470bf22008-12-25 23:28:35 +0000978 msg->trx = msg->lchan->ts->trx;
Harald Welte52b1f982008-12-23 20:25:15 +0000979
Harald Welte4bfdfe72009-06-10 23:11:52 +0800980 if ((gh->proto_discr & GSM48_PDISC_MASK) == GSM48_PDISC_CC)
981 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x) "
982 "Sending '%s' to MS.\n", msg->trx->bts->nr,
983 msg->trx->nr, msg->lchan->ts->nr,
984 gh->proto_discr & 0xf0,
985 cc_msg_names[gh->msg_type & 0x3f]);
986 else
987 DEBUGP(DCC, "(bts %d trx %d ts %d pd %02x) "
988 "Sending 0x%02x to MS.\n", msg->trx->bts->nr,
989 msg->trx->nr, msg->lchan->ts->nr,
990 gh->proto_discr, gh->msg_type);
Harald Welte65e74cc2008-12-29 01:55:35 +0000991 }
992
Harald Welte4b634542008-12-27 01:55:51 +0000993 msg->l3h = msg->data;
994
Harald Welte8470bf22008-12-25 23:28:35 +0000995 return rsl_data_request(msg, 0);
Harald Welte52b1f982008-12-23 20:25:15 +0000996}
997
Holger Freyther429e7762008-12-30 13:28:30 +0000998/* Chapter 9.2.14 : Send LOCATION UPDATING REJECT */
Harald Welte8470bf22008-12-25 23:28:35 +0000999int gsm0408_loc_upd_rej(struct gsm_lchan *lchan, u_int8_t cause)
Harald Welte52b1f982008-12-23 20:25:15 +00001000{
Harald Welte8470bf22008-12-25 23:28:35 +00001001 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +00001002 struct gsm48_hdr *gh;
1003
Harald Welte8470bf22008-12-25 23:28:35 +00001004 msg->lchan = lchan;
Harald Welte52b1f982008-12-23 20:25:15 +00001005
1006 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
1007 gh->proto_discr = GSM48_PDISC_MM;
Harald Welte10b487b2008-12-27 19:53:37 +00001008 gh->msg_type = GSM48_MT_MM_LOC_UPD_REJECT;
Harald Welte52b1f982008-12-23 20:25:15 +00001009 gh->data[0] = cause;
1010
Harald Weltedb253af2008-12-30 17:56:55 +00001011 DEBUGP(DMM, "-> LOCATION UPDATING REJECT on channel: %d\n", lchan->nr);
1012
Harald Welte65e74cc2008-12-29 01:55:35 +00001013 return gsm48_sendmsg(msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001014}
1015
1016/* Chapter 9.2.13 : Send LOCATION UPDATE ACCEPT */
Harald Welte75a983f2008-12-27 21:34:06 +00001017int gsm0408_loc_upd_acc(struct gsm_lchan *lchan, u_int32_t tmsi)
Harald Welte52b1f982008-12-23 20:25:15 +00001018{
Harald Welte8470bf22008-12-25 23:28:35 +00001019 struct gsm_bts *bts = lchan->ts->trx->bts;
1020 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +00001021 struct gsm48_hdr *gh;
1022 struct gsm48_loc_area_id *lai;
1023 u_int8_t *mid;
Holger Freyther07cc8d82008-12-29 06:23:46 +00001024 int ret;
Harald Welte52b1f982008-12-23 20:25:15 +00001025
Harald Welte8470bf22008-12-25 23:28:35 +00001026 msg->lchan = lchan;
Harald Welte52b1f982008-12-23 20:25:15 +00001027
1028 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1029 gh->proto_discr = GSM48_PDISC_MM;
1030 gh->msg_type = GSM48_MT_MM_LOC_UPD_ACCEPT;
1031
1032 lai = (struct gsm48_loc_area_id *) msgb_put(msg, sizeof(*lai));
Holger Freyther17746612008-12-28 16:32:44 +00001033 gsm0408_generate_lai(lai, bts->network->country_code,
Harald Welte52b1f982008-12-23 20:25:15 +00001034 bts->network->network_code, bts->location_area_code);
1035
1036 mid = msgb_put(msg, MID_TMSI_LEN);
1037 generate_mid_from_tmsi(mid, tmsi);
1038
1039 DEBUGP(DMM, "-> LOCATION UPDATE ACCEPT\n");
1040
Harald Weltedb253af2008-12-30 17:56:55 +00001041 ret = gsm48_sendmsg(msg);
1042
Harald Weltedb253af2008-12-30 17:56:55 +00001043 ret = gsm48_tx_mm_info(lchan);
Harald Weltedb253af2008-12-30 17:56:55 +00001044
Holger Freyther07cc8d82008-12-29 06:23:46 +00001045 return ret;
Harald Welte52b1f982008-12-23 20:25:15 +00001046}
1047
Harald Weltefc977a82008-12-27 10:19:37 +00001048static char bcd2char(u_int8_t bcd)
1049{
1050 if (bcd < 0xa)
1051 return '0' + bcd;
1052 else
1053 return 'A' + (bcd - 0xa);
1054}
1055
Harald Weltebf5e8df2009-02-03 12:59:45 +00001056/* Convert Mobile Identity (10.5.1.4) to string */
Harald Weltefc977a82008-12-27 10:19:37 +00001057static int mi_to_string(char *string, int str_len, u_int8_t *mi, int mi_len)
1058{
1059 int i;
1060 u_int8_t mi_type;
1061 char *str_cur = string;
Harald Welte4ed0e922009-01-10 03:17:30 +00001062 u_int32_t tmsi;
Harald Weltefc977a82008-12-27 10:19:37 +00001063
1064 mi_type = mi[0] & GSM_MI_TYPE_MASK;
1065
1066 switch (mi_type) {
1067 case GSM_MI_TYPE_NONE:
1068 break;
1069 case GSM_MI_TYPE_TMSI:
Harald Welte4ed0e922009-01-10 03:17:30 +00001070 /* Table 10.5.4.3, reverse generate_mid_from_tmsi */
1071 if (mi_len == TMSI_LEN && mi[0] == (0xf0 | GSM_MI_TYPE_TMSI)) {
1072 memcpy(&tmsi, &mi[1], 4);
1073 tmsi = ntohl(tmsi);
1074 return snprintf(string, str_len, "%u", tmsi);
Harald Weltefc977a82008-12-27 10:19:37 +00001075 }
1076 break;
1077 case GSM_MI_TYPE_IMSI:
1078 case GSM_MI_TYPE_IMEI:
1079 case GSM_MI_TYPE_IMEISV:
Harald Weltedb253af2008-12-30 17:56:55 +00001080 *str_cur++ = bcd2char(mi[0] >> 4);
1081
1082 for (i = 1; i < mi_len; i++) {
Harald Weltefc977a82008-12-27 10:19:37 +00001083 if (str_cur + 2 >= string + str_len)
1084 return str_cur - string;
1085 *str_cur++ = bcd2char(mi[i] & 0xf);
Harald Weltedb253af2008-12-30 17:56:55 +00001086 /* skip last nibble in last input byte when GSM_EVEN */
1087 if( (i != mi_len-1) || (mi[0] & GSM_MI_ODD))
1088 *str_cur++ = bcd2char(mi[i] >> 4);
Harald Weltefc977a82008-12-27 10:19:37 +00001089 }
1090 break;
1091 default:
1092 break;
1093 }
Harald Weltefc977a82008-12-27 10:19:37 +00001094 *str_cur++ = '\0';
Harald Weltedb253af2008-12-30 17:56:55 +00001095
Harald Weltefc977a82008-12-27 10:19:37 +00001096 return str_cur - string;
1097}
1098
Harald Weltebf5e8df2009-02-03 12:59:45 +00001099/* Transmit Chapter 9.2.10 Identity Request */
Harald Welte231ad4f2008-12-27 11:15:38 +00001100static int mm_tx_identity_req(struct gsm_lchan *lchan, u_int8_t id_type)
1101{
1102 struct msgb *msg = gsm48_msgb_alloc();
1103 struct gsm48_hdr *gh;
Harald Weltefc977a82008-12-27 10:19:37 +00001104
Harald Welte231ad4f2008-12-27 11:15:38 +00001105 msg->lchan = lchan;
1106
1107 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
1108 gh->proto_discr = GSM48_PDISC_MM;
1109 gh->msg_type = GSM48_MT_MM_ID_REQ;
1110 gh->data[0] = id_type;
1111
Harald Welte65e74cc2008-12-29 01:55:35 +00001112 return gsm48_sendmsg(msg);
Harald Welte231ad4f2008-12-27 11:15:38 +00001113}
1114
1115#define MI_SIZE 32
1116
Harald Weltebf5e8df2009-02-03 12:59:45 +00001117/* Parse Chapter 9.2.11 Identity Response */
Harald Welte231ad4f2008-12-27 11:15:38 +00001118static int mm_rx_id_resp(struct msgb *msg)
1119{
1120 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte75a983f2008-12-27 21:34:06 +00001121 struct gsm_lchan *lchan = msg->lchan;
Harald Welte231ad4f2008-12-27 11:15:38 +00001122 u_int8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
1123 char mi_string[MI_SIZE];
1124
1125 mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
Harald Welte61253062008-12-27 11:25:50 +00001126 DEBUGP(DMM, "IDENTITY RESPONSE: mi_type=0x%02x MI(%s)\n",
Harald Welte231ad4f2008-12-27 11:15:38 +00001127 mi_type, mi_string);
1128
Harald Welte75a983f2008-12-27 21:34:06 +00001129 switch (mi_type) {
1130 case GSM_MI_TYPE_IMSI:
1131 if (!lchan->subscr)
1132 lchan->subscr = db_create_subscriber(mi_string);
Holger Freyther73487a22008-12-31 18:53:57 +00001133 if (lchan->loc_operation)
1134 lchan->loc_operation->waiting_for_imsi = 0;
Harald Welte75a983f2008-12-27 21:34:06 +00001135 break;
1136 case GSM_MI_TYPE_IMEI:
Harald Welte255539c2008-12-28 02:26:27 +00001137 case GSM_MI_TYPE_IMEISV:
Harald Welte75a983f2008-12-27 21:34:06 +00001138 /* update subscribe <-> IMEI mapping */
1139 if (lchan->subscr)
1140 db_subscriber_assoc_imei(lchan->subscr, mi_string);
Holger Freyther73487a22008-12-31 18:53:57 +00001141 if (lchan->loc_operation)
1142 lchan->loc_operation->waiting_for_imei = 0;
Harald Welte75a983f2008-12-27 21:34:06 +00001143 break;
1144 }
Holger Freyther73487a22008-12-31 18:53:57 +00001145
1146 /* Check if we can let the mobile station enter */
Holger Freytherd51524f2009-06-09 08:27:07 +00001147 return gsm0408_authorize(lchan, msg);
Harald Welte231ad4f2008-12-27 11:15:38 +00001148}
1149
Harald Welte255539c2008-12-28 02:26:27 +00001150
1151static void loc_upd_rej_cb(void *data)
1152{
1153 struct gsm_lchan *lchan = data;
1154
Holger Freyther73487a22008-12-31 18:53:57 +00001155 release_loc_updating_req(lchan);
Holger Freythere97f7fb2008-12-31 18:52:11 +00001156 gsm0408_loc_upd_rej(lchan, reject_cause);
Holger Freyther67b4b9a2009-01-01 03:46:11 +00001157 lchan_auto_release(lchan);
Harald Welte255539c2008-12-28 02:26:27 +00001158}
1159
Holger Freytherb7193e42008-12-29 17:44:08 +00001160static void schedule_reject(struct gsm_lchan *lchan)
1161{
Holger Freyther73487a22008-12-31 18:53:57 +00001162 lchan->loc_operation->updating_timer.cb = loc_upd_rej_cb;
1163 lchan->loc_operation->updating_timer.data = lchan;
Harald Welteff117a82009-05-23 05:22:08 +00001164 bsc_schedule_timer(&lchan->loc_operation->updating_timer, 5, 0);
Holger Freytherb7193e42008-12-29 17:44:08 +00001165}
1166
Harald Welte2a139372009-02-22 21:14:55 +00001167static const char *lupd_name(u_int8_t type)
1168{
1169 switch (type) {
1170 case GSM48_LUPD_NORMAL:
1171 return "NORMAL";
1172 case GSM48_LUPD_PERIODIC:
1173 return "PEROIDOC";
1174 case GSM48_LUPD_IMSI_ATT:
1175 return "IMSI ATTACH";
1176 default:
1177 return "UNKNOWN";
1178 }
1179}
1180
Harald Welte231ad4f2008-12-27 11:15:38 +00001181#define MI_SIZE 32
Harald Weltebf5e8df2009-02-03 12:59:45 +00001182/* Chapter 9.2.15: Receive Location Updating Request */
Harald Welte231ad4f2008-12-27 11:15:38 +00001183static int mm_rx_loc_upd_req(struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001184{
Harald Welte8470bf22008-12-25 23:28:35 +00001185 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001186 struct gsm48_loc_upd_req *lu;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001187 struct gsm_subscriber *subscr = NULL;
Harald Welte255539c2008-12-28 02:26:27 +00001188 struct gsm_lchan *lchan = msg->lchan;
Harald Welte8470bf22008-12-25 23:28:35 +00001189 u_int8_t mi_type;
Harald Welte231ad4f2008-12-27 11:15:38 +00001190 char mi_string[MI_SIZE];
1191 int rc;
Harald Welte52b1f982008-12-23 20:25:15 +00001192
Harald Welte8470bf22008-12-25 23:28:35 +00001193 lu = (struct gsm48_loc_upd_req *) gh->data;
1194
1195 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
Harald Welte52b1f982008-12-23 20:25:15 +00001196
Harald Weltefc977a82008-12-27 10:19:37 +00001197 mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
1198
Holger Freyther79f4ae62009-06-02 03:25:04 +00001199 DEBUGP(DMM, "LUPDREQ: mi_type=0x%02x MI(%s) type=%s\n", mi_type, mi_string,
Harald Welte2a139372009-02-22 21:14:55 +00001200 lupd_name(lu->type));
Holger Freyther73487a22008-12-31 18:53:57 +00001201
Holger Freythereaf04692009-06-06 13:54:44 +00001202 /*
1203 * Pseudo Spoof detection: Just drop a second/concurrent
1204 * location updating request.
1205 */
1206 if (lchan->loc_operation) {
1207 DEBUGP(DMM, "LUPDREQ: ignoring request due an existing one: %p.\n",
1208 lchan->loc_operation);
1209 gsm0408_loc_upd_rej(lchan, GSM48_REJECT_PROTOCOL_ERROR);
1210 return 0;
1211 }
1212
Holger Freyther73487a22008-12-31 18:53:57 +00001213 allocate_loc_updating_req(lchan);
1214
Harald Welte52b1f982008-12-23 20:25:15 +00001215 switch (mi_type) {
1216 case GSM_MI_TYPE_IMSI:
Harald Welte4bfdfe72009-06-10 23:11:52 +08001217 DEBUGP(DMM, "\n");
Harald Welte231ad4f2008-12-27 11:15:38 +00001218 /* we always want the IMEI, too */
Harald Welte015b9ad2009-02-28 18:22:03 +00001219 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMEI);
Holger Freyther73487a22008-12-31 18:53:57 +00001220 lchan->loc_operation->waiting_for_imei = 1;
Holger Freytherc6ea9db2008-12-30 19:18:21 +00001221
Harald Welte52b1f982008-12-23 20:25:15 +00001222 /* look up subscriber based on IMSI */
Harald Welte75a983f2008-12-27 21:34:06 +00001223 subscr = db_create_subscriber(mi_string);
Harald Welte4b634542008-12-27 01:55:51 +00001224 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001225 case GSM_MI_TYPE_TMSI:
Harald Welte4bfdfe72009-06-10 23:11:52 +08001226 DEBUGP(DMM, "\n");
Harald Welte231ad4f2008-12-27 11:15:38 +00001227 /* we always want the IMEI, too */
Harald Welte015b9ad2009-02-28 18:22:03 +00001228 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMEI);
Holger Freyther73487a22008-12-31 18:53:57 +00001229 lchan->loc_operation->waiting_for_imei = 1;
Holger Freytherc6ea9db2008-12-30 19:18:21 +00001230
Harald Welte52b1f982008-12-23 20:25:15 +00001231 /* look up the subscriber based on TMSI, request IMSI if it fails */
Harald Welteba4cf162009-01-10 01:49:35 +00001232 subscr = subscr_get_by_tmsi(mi_string);
Harald Welte52b1f982008-12-23 20:25:15 +00001233 if (!subscr) {
Harald Welte231ad4f2008-12-27 11:15:38 +00001234 /* send IDENTITY REQUEST message to get IMSI */
Harald Welte255539c2008-12-28 02:26:27 +00001235 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMSI);
Holger Freyther73487a22008-12-31 18:53:57 +00001236 lchan->loc_operation->waiting_for_imsi = 1;
Harald Welte52b1f982008-12-23 20:25:15 +00001237 }
1238 break;
1239 case GSM_MI_TYPE_IMEI:
1240 case GSM_MI_TYPE_IMEISV:
1241 /* no sim card... FIXME: what to do ? */
Harald Welte2a139372009-02-22 21:14:55 +00001242 DEBUGP(DMM, "unimplemented mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +00001243 break;
1244 default:
Harald Welte2a139372009-02-22 21:14:55 +00001245 DEBUGP(DMM, "unknown mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +00001246 break;
1247 }
1248
Harald Welte4bfdfe72009-06-10 23:11:52 +08001249 if (!subscr) {
1250 DEBUGP(DRR, "<- Can't find any subscriber for this ID\n");
1251 /* FIXME: request id? close channel? */
1252 return -EINVAL;
1253 }
1254
Harald Welte255539c2008-12-28 02:26:27 +00001255 lchan->subscr = subscr;
1256
Holger Freyther73487a22008-12-31 18:53:57 +00001257 /*
1258 * Schedule the reject timer and check if we can let the
1259 * subscriber into our network immediately or if we need to wait
1260 * for identity responses.
1261 */
1262 schedule_reject(lchan);
Holger Freytherd51524f2009-06-09 08:27:07 +00001263 return gsm0408_authorize(lchan, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001264}
1265
Harald Welte7584aea2009-02-11 11:44:12 +00001266/* 9.1.5 Channel mode modify */
1267int gsm48_tx_chan_mode_modify(struct gsm_lchan *lchan, u_int8_t mode)
1268{
1269 struct msgb *msg = gsm48_msgb_alloc();
1270 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1271 struct gsm48_chan_mode_modify *cmm =
1272 (struct gsm48_chan_mode_modify *) msgb_put(msg, sizeof(*cmm));
Harald Welte4a543e82009-02-28 13:17:55 +00001273 u_int16_t arfcn = lchan->ts->trx->arfcn & 0x3ff;
Harald Welte7584aea2009-02-11 11:44:12 +00001274
Harald Welte4a543e82009-02-28 13:17:55 +00001275 DEBUGP(DRR, "-> CHANNEL MODE MODIFY mode=0x%02x\n", mode);
Harald Welte7ccf7782009-02-17 01:43:01 +00001276
Harald Welte45b407a2009-05-23 15:51:12 +00001277 lchan->tch_mode = mode;
Harald Welte7584aea2009-02-11 11:44:12 +00001278 msg->lchan = lchan;
1279 gh->proto_discr = GSM48_PDISC_RR;
1280 gh->msg_type = GSM48_MT_RR_CHAN_MODE_MODIF;
1281
1282 /* fill the channel information element, this code
1283 * should probably be shared with rsl_rx_chan_rqd() */
1284 cmm->chan_desc.chan_nr = lchan2chan_nr(lchan);
Harald Welte02b0e092009-02-28 13:11:07 +00001285 cmm->chan_desc.h0.tsc = lchan->ts->trx->bts->tsc;
Harald Welte7584aea2009-02-11 11:44:12 +00001286 cmm->chan_desc.h0.h = 0;
1287 cmm->chan_desc.h0.arfcn_high = arfcn >> 8;
1288 cmm->chan_desc.h0.arfcn_low = arfcn & 0xff;
1289 cmm->mode = mode;
1290
1291 return gsm48_sendmsg(msg);
1292}
1293
Harald Welte4bfdfe72009-06-10 23:11:52 +08001294#if 0
1295static u_int8_t to_bcd8(u_int8_t val)
1296{
1297 return ((val / 10) << 4) | (val % 10);
1298}
1299#endif
1300
Harald Weltedb253af2008-12-30 17:56:55 +00001301/* Section 9.2.15a */
1302int gsm48_tx_mm_info(struct gsm_lchan *lchan)
1303{
1304 struct msgb *msg = gsm48_msgb_alloc();
1305 struct gsm48_hdr *gh;
1306 struct gsm_network *net = lchan->ts->trx->bts->network;
Harald Weltedb253af2008-12-30 17:56:55 +00001307 u_int8_t *ptr8;
1308 u_int16_t *ptr16;
1309 int name_len;
Harald Weltedb253af2008-12-30 17:56:55 +00001310 int i;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001311#if 0
1312 time_t cur_t;
1313 struct tm* cur_time;
1314 int tz15min;
1315#endif
Harald Weltedb253af2008-12-30 17:56:55 +00001316
1317 msg->lchan = lchan;
1318
1319 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1320 gh->proto_discr = GSM48_PDISC_MM;
1321 gh->msg_type = GSM48_MT_MM_INFO;
1322
1323 if (net->name_long) {
1324 name_len = strlen(net->name_long);
1325 /* 10.5.3.5a */
1326 ptr8 = msgb_put(msg, 3);
1327 ptr8[0] = GSM48_IE_NAME_LONG;
1328 ptr8[1] = name_len*2 +1;
1329 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
1330
1331 ptr16 = (u_int16_t *) msgb_put(msg, name_len*2);
1332 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +00001333 ptr16[i] = htons(net->name_long[i]);
Harald Weltedb253af2008-12-30 17:56:55 +00001334
1335 /* FIXME: Use Cell Broadcast, not UCS-2, since
1336 * UCS-2 is only supported by later revisions of the spec */
1337 }
1338
1339 if (net->name_short) {
1340 name_len = strlen(net->name_short);
1341 /* 10.5.3.5a */
1342 ptr8 = (u_int8_t *) msgb_put(msg, 3);
1343 ptr8[0] = GSM48_IE_NAME_LONG;
1344 ptr8[1] = name_len*2 + 1;
1345 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
1346
Harald Weltee872cb12009-01-01 00:33:37 +00001347 ptr16 = (u_int16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +00001348 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +00001349 ptr16[i] = htons(net->name_short[i]);
Harald Weltedb253af2008-12-30 17:56:55 +00001350 }
1351
1352#if 0
1353 /* Section 10.5.3.9 */
1354 cur_t = time(NULL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001355 cur_time = gmtime(&cur_t);
Harald Weltedb253af2008-12-30 17:56:55 +00001356 ptr8 = msgb_put(msg, 8);
1357 ptr8[0] = GSM48_IE_NET_TIME_TZ;
1358 ptr8[1] = to_bcd8(cur_time->tm_year % 100);
1359 ptr8[2] = to_bcd8(cur_time->tm_mon);
1360 ptr8[3] = to_bcd8(cur_time->tm_mday);
1361 ptr8[4] = to_bcd8(cur_time->tm_hour);
1362 ptr8[5] = to_bcd8(cur_time->tm_min);
1363 ptr8[6] = to_bcd8(cur_time->tm_sec);
1364 /* 02.42: coded as BCD encoded signed value in units of 15 minutes */
1365 tz15min = (cur_time->tm_gmtoff)/(60*15);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001366 ptr8[7] = to_bcd8(tz15min);
Harald Weltedb253af2008-12-30 17:56:55 +00001367 if (tz15min < 0)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001368 ptr8[7] |= 0x80;
Harald Weltedb253af2008-12-30 17:56:55 +00001369#endif
1370
1371 return gsm48_sendmsg(msg);
1372}
1373
Harald Welte4b634542008-12-27 01:55:51 +00001374static int gsm48_tx_mm_serv_ack(struct gsm_lchan *lchan)
1375{
Harald Welte4b634542008-12-27 01:55:51 +00001376 DEBUGP(DMM, "-> CM SERVICE ACK\n");
Harald Welte65e74cc2008-12-29 01:55:35 +00001377 return gsm48_tx_simple(lchan, GSM48_PDISC_MM, GSM48_MT_MM_CM_SERV_ACC);
Harald Welte4b634542008-12-27 01:55:51 +00001378}
Harald Welteba4cf162009-01-10 01:49:35 +00001379
1380/* 9.2.6 CM service reject */
1381static int gsm48_tx_mm_serv_rej(struct gsm_lchan *lchan,
1382 enum gsm48_reject_value value)
1383{
1384 struct msgb *msg = gsm48_msgb_alloc();
1385 struct gsm48_hdr *gh;
1386
1387 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
1388
1389 msg->lchan = lchan;
1390 use_lchan(lchan);
1391
1392 gh->proto_discr = GSM48_PDISC_MM;
1393 gh->msg_type = GSM48_MT_MM_CM_SERV_REJ;
1394 gh->data[0] = value;
1395 DEBUGP(DMM, "-> CM SERVICE Reject cause: %d\n", value);
1396
1397 return gsm48_sendmsg(msg);
1398}
1399
Harald Welte4ed0e922009-01-10 03:17:30 +00001400
1401/*
1402 * Handle CM Service Requests
1403 * a) Verify that the packet is long enough to contain the information
1404 * we require otherwsie reject with INCORRECT_MESSAGE
1405 * b) Try to parse the TMSI. If we do not have one reject
1406 * c) Check that we know the subscriber with the TMSI otherwise reject
1407 * with a HLR cause
1408 * d) Set the subscriber on the gsm_lchan and accept
1409 */
Harald Welte4b634542008-12-27 01:55:51 +00001410static int gsm48_rx_mm_serv_req(struct msgb *msg)
1411{
Harald Welteba4cf162009-01-10 01:49:35 +00001412 u_int8_t mi_type;
Harald Welte4ed0e922009-01-10 03:17:30 +00001413 char mi_string[MI_SIZE];
Harald Welte4b634542008-12-27 01:55:51 +00001414
Harald Welteba4cf162009-01-10 01:49:35 +00001415 struct gsm_subscriber *subscr;
1416 struct gsm48_hdr *gh = msgb_l3(msg);
1417 struct gsm48_service_request *req =
1418 (struct gsm48_service_request *)gh->data;
Harald Weltec9e02182009-05-01 19:07:53 +00001419 /* unfortunately in Phase1 the classmar2 length is variable */
1420 u_int8_t classmark2_len = gh->data[1];
1421 u_int8_t *classmark2 = gh->data+2;
1422 u_int8_t mi_len = *(classmark2 + classmark2_len);
1423 u_int8_t *mi = (classmark2 + classmark2_len + 1);
Harald Welteba4cf162009-01-10 01:49:35 +00001424
Harald Weltec9e02182009-05-01 19:07:53 +00001425 DEBUGP(DMM, "<- CM SERVICE REQUEST ");
Harald Welteba4cf162009-01-10 01:49:35 +00001426 if (msg->data_len < sizeof(struct gsm48_service_request*)) {
Harald Weltec9e02182009-05-01 19:07:53 +00001427 DEBUGPC(DMM, "wrong sized message\n");
Harald Welteba4cf162009-01-10 01:49:35 +00001428 return gsm48_tx_mm_serv_rej(msg->lchan,
1429 GSM48_REJECT_INCORRECT_MESSAGE);
1430 }
1431
1432 if (msg->data_len < req->mi_len + 6) {
Harald Weltec9e02182009-05-01 19:07:53 +00001433 DEBUGPC(DMM, "does not fit in packet\n");
Harald Welteba4cf162009-01-10 01:49:35 +00001434 return gsm48_tx_mm_serv_rej(msg->lchan,
1435 GSM48_REJECT_INCORRECT_MESSAGE);
1436 }
1437
Harald Weltec9e02182009-05-01 19:07:53 +00001438 mi_type = mi[0] & GSM_MI_TYPE_MASK;
Harald Welteba4cf162009-01-10 01:49:35 +00001439 if (mi_type != GSM_MI_TYPE_TMSI) {
Harald Weltec9e02182009-05-01 19:07:53 +00001440 DEBUGPC(DMM, "mi_type is not TMSI: %d\n", mi_type);
Harald Welteba4cf162009-01-10 01:49:35 +00001441 return gsm48_tx_mm_serv_rej(msg->lchan,
1442 GSM48_REJECT_INCORRECT_MESSAGE);
1443 }
1444
Harald Weltec9e02182009-05-01 19:07:53 +00001445 mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
Harald Weltec9e02182009-05-01 19:07:53 +00001446 DEBUGPC(DMM, "serv_type=0x%02x mi_type=0x%02x M(%s)\n",
Harald Welte4ed0e922009-01-10 03:17:30 +00001447 req->cm_service_type, mi_type, mi_string);
Harald Weltebcae43f2008-12-27 21:45:37 +00001448
Holger Freythereb443982009-06-04 13:58:42 +00001449 subscr = subscr_get_by_tmsi(mi_string);
1450
Harald Welte2a139372009-02-22 21:14:55 +00001451 /* FIXME: if we don't know the TMSI, inquire abit IMSI and allocate new TMSI */
Harald Welte4ed0e922009-01-10 03:17:30 +00001452 if (!subscr)
1453 return gsm48_tx_mm_serv_rej(msg->lchan,
1454 GSM48_REJECT_IMSI_UNKNOWN_IN_HLR);
1455
1456 if (!msg->lchan->subscr)
1457 msg->lchan->subscr = subscr;
Harald Welte9bb7c702009-01-10 03:21:41 +00001458 else if (msg->lchan->subscr != subscr) {
1459 DEBUGP(DMM, "<- CM Channel already owned by someone else?\n");
1460 subscr_put(subscr);
1461 }
1462
Harald Weltef7c43522009-06-09 20:24:21 +00001463 subscr->classmark2_len = classmark2_len;
1464 memcpy(subscr->classmark2, classmark2, classmark2_len);
1465
Harald Welte4b634542008-12-27 01:55:51 +00001466 return gsm48_tx_mm_serv_ack(msg->lchan);
1467}
1468
Harald Welte2a139372009-02-22 21:14:55 +00001469static int gsm48_rx_mm_imsi_detach_ind(struct msgb *msg)
1470{
1471 struct gsm48_hdr *gh = msgb_l3(msg);
1472 struct gsm48_imsi_detach_ind *idi =
1473 (struct gsm48_imsi_detach_ind *) gh->data;
1474 u_int8_t mi_type = idi->mi[0] & GSM_MI_TYPE_MASK;
1475 char mi_string[MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +08001476 struct gsm_subscriber *subscr = NULL;
Harald Welte2a139372009-02-22 21:14:55 +00001477
1478 mi_to_string(mi_string, sizeof(mi_string), idi->mi, idi->mi_len);
1479 DEBUGP(DMM, "IMSI DETACH INDICATION: mi_type=0x%02x MI(%s): ",
1480 mi_type, mi_string);
1481
1482 switch (mi_type) {
1483 case GSM_MI_TYPE_TMSI:
1484 subscr = subscr_get_by_tmsi(mi_string);
1485 break;
1486 case GSM_MI_TYPE_IMSI:
1487 subscr = subscr_get_by_imsi(mi_string);
1488 break;
1489 case GSM_MI_TYPE_IMEI:
1490 case GSM_MI_TYPE_IMEISV:
1491 /* no sim card... FIXME: what to do ? */
Holger Freyther79f4ae62009-06-02 03:25:04 +00001492 DEBUGPC(DMM, "unimplemented mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +00001493 break;
1494 default:
Holger Freyther79f4ae62009-06-02 03:25:04 +00001495 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +00001496 break;
1497 }
1498
Holger Freyther4a49e772009-04-12 05:37:29 +00001499 if (subscr) {
1500 subscr_update(subscr, msg->trx->bts,
1501 GSM_SUBSCRIBER_UPDATE_DETACHED);
Harald Welte2a139372009-02-22 21:14:55 +00001502 DEBUGP(DMM, "Subscriber: %s\n",
1503 subscr->name ? subscr->name : subscr->imsi);
Holger Freytherc21cfbc2009-06-02 02:54:57 +00001504 subscr_put(subscr);
Holger Freyther4a49e772009-04-12 05:37:29 +00001505 } else
Harald Welte2a139372009-02-22 21:14:55 +00001506 DEBUGP(DMM, "Unknown Subscriber ?!?\n");
1507
Harald Welte2a139372009-02-22 21:14:55 +00001508 return 0;
1509}
1510
Harald Welted2a7f5a2009-06-05 20:08:20 +00001511static int gsm48_rx_mm_status(struct msgb *msg)
1512{
1513 struct gsm48_hdr *gh = msgb_l3(msg);
1514
1515 DEBUGP(DMM, "MM STATUS (reject cause 0x%02x)\n", gh->data[0]);
1516
1517 return 0;
1518}
1519
Harald Weltebf5e8df2009-02-03 12:59:45 +00001520/* Receive a GSM 04.08 Mobility Management (MM) message */
Harald Welte52b1f982008-12-23 20:25:15 +00001521static int gsm0408_rcv_mm(struct msgb *msg)
1522{
1523 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001524 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001525
1526 switch (gh->msg_type & 0xbf) {
1527 case GSM48_MT_MM_LOC_UPD_REQUEST:
Holger Freyther429e7762008-12-30 13:28:30 +00001528 DEBUGP(DMM, "LOCATION UPDATING REQUEST\n");
Harald Welte231ad4f2008-12-27 11:15:38 +00001529 rc = mm_rx_loc_upd_req(msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001530 break;
1531 case GSM48_MT_MM_ID_RESP:
Harald Welte231ad4f2008-12-27 11:15:38 +00001532 rc = mm_rx_id_resp(msg);
1533 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001534 case GSM48_MT_MM_CM_SERV_REQ:
Harald Welte4b634542008-12-27 01:55:51 +00001535 rc = gsm48_rx_mm_serv_req(msg);
1536 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001537 case GSM48_MT_MM_STATUS:
Harald Welted2a7f5a2009-06-05 20:08:20 +00001538 rc = gsm48_rx_mm_status(msg);
Harald Welte231ad4f2008-12-27 11:15:38 +00001539 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001540 case GSM48_MT_MM_TMSI_REALL_COMPL:
Harald Welte69b2af22009-01-06 19:47:00 +00001541 DEBUGP(DMM, "TMSI Reallocation Completed. Subscriber: %s\n",
1542 msg->lchan->subscr ?
1543 msg->lchan->subscr->imsi :
1544 "unknown subscriber");
1545 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001546 case GSM48_MT_MM_IMSI_DETACH_IND:
Harald Welte2a139372009-02-22 21:14:55 +00001547 rc = gsm48_rx_mm_imsi_detach_ind(msg);
1548 break;
1549 case GSM48_MT_MM_CM_REEST_REQ:
1550 DEBUGP(DMM, "CM REESTABLISH REQUEST: Not implemented\n");
1551 break;
1552 case GSM48_MT_MM_AUTH_RESP:
1553 DEBUGP(DMM, "AUTHENTICATION RESPONSE: Not implemented\n");
Harald Welte52b1f982008-12-23 20:25:15 +00001554 break;
1555 default:
1556 fprintf(stderr, "Unknown GSM 04.08 MM msg type 0x%02x\n",
1557 gh->msg_type);
1558 break;
1559 }
1560
1561 return rc;
1562}
Harald Weltebf5e8df2009-02-03 12:59:45 +00001563
Harald Welte2d35ae62009-02-06 12:02:13 +00001564/* Receive a PAGING RESPONSE message from the MS */
1565static int gsm48_rr_rx_pag_resp(struct msgb *msg)
1566{
1567 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte61548982009-02-22 21:26:29 +00001568 u_int8_t *classmark2_lv = gh->data + 1;
1569 u_int8_t *mi_lv = gh->data + 2 + *classmark2_lv;
1570 u_int8_t mi_type = mi_lv[1] & GSM_MI_TYPE_MASK;
Harald Welte2d35ae62009-02-06 12:02:13 +00001571 char mi_string[MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +08001572 struct gsm_subscriber *subscr = NULL;
Harald Welte595ad7b2009-02-16 22:05:44 +00001573 struct paging_signal_data sig_data;
Harald Welte2d35ae62009-02-06 12:02:13 +00001574 int rc = 0;
1575
Harald Welte61548982009-02-22 21:26:29 +00001576 mi_to_string(mi_string, sizeof(mi_string), mi_lv+1, *mi_lv);
Harald Welte2d35ae62009-02-06 12:02:13 +00001577 DEBUGP(DRR, "PAGING RESPONSE: mi_type=0x%02x MI(%s)\n",
1578 mi_type, mi_string);
Harald Weltefe18d8f2009-02-22 21:14:24 +00001579 switch (mi_type) {
1580 case GSM_MI_TYPE_TMSI:
1581 subscr = subscr_get_by_tmsi(mi_string);
1582 break;
1583 case GSM_MI_TYPE_IMSI:
1584 subscr = subscr_get_by_imsi(mi_string);
1585 break;
1586 }
Harald Welte2d35ae62009-02-06 12:02:13 +00001587
1588 if (!subscr) {
1589 DEBUGP(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte09e38af2009-02-16 22:52:23 +00001590 /* FIXME: request id? close channel? */
Harald Welte2d35ae62009-02-06 12:02:13 +00001591 return -EINVAL;
1592 }
1593 DEBUGP(DRR, "<- Channel was requested by %s\n",
1594 subscr->name ? subscr->name : subscr->imsi);
Holger Freyther053e09d2009-02-14 22:51:06 +00001595
Harald Weltef7c43522009-06-09 20:24:21 +00001596 subscr->classmark2_len = *classmark2_lv;
1597 memcpy(subscr->classmark2, classmark2_lv+1, *classmark2_lv);
1598
Holger Freytherc21cfbc2009-06-02 02:54:57 +00001599 if (!msg->lchan->subscr) {
Holger Freyther2fa4cb52009-02-14 23:53:15 +00001600 msg->lchan->subscr = subscr;
Holger Freytherc21cfbc2009-06-02 02:54:57 +00001601 } else if (msg->lchan->subscr != subscr) {
Holger Freyther2fa4cb52009-02-14 23:53:15 +00001602 DEBUGP(DRR, "<- Channel already owned by someone else?\n");
1603 subscr_put(subscr);
Holger Freytherc21cfbc2009-06-02 02:54:57 +00001604 return -EINVAL;
1605 } else {
1606 DEBUGP(DRR, "<- Channel already owned by us\n");
1607 subscr_put(subscr);
1608 subscr = msg->lchan->subscr;
Holger Freyther2fa4cb52009-02-14 23:53:15 +00001609 }
1610
Harald Welte595ad7b2009-02-16 22:05:44 +00001611 sig_data.subscr = subscr;
1612 sig_data.bts = msg->lchan->ts->trx->bts;
1613 sig_data.lchan = msg->lchan;
1614
1615 dispatch_signal(SS_PAGING, S_PAGING_COMPLETED, &sig_data);
Harald Weltebe143102009-06-10 11:21:55 +08001616
1617 /* Stop paging on the bts we received the paging response */
Harald Welte7ccf7782009-02-17 01:43:01 +00001618 paging_request_stop(msg->trx->bts, subscr, msg->lchan);
Harald Welte2d35ae62009-02-06 12:02:13 +00001619
Harald Welte7584aea2009-02-11 11:44:12 +00001620 /* FIXME: somehow signal the completion of the PAGING to
1621 * the entity that requested the paging */
1622
Harald Welte2d35ae62009-02-06 12:02:13 +00001623 return rc;
1624}
1625
Harald Weltef7c43522009-06-09 20:24:21 +00001626static int gsm48_rx_rr_classmark(struct msgb *msg)
1627{
1628 struct gsm48_hdr *gh = msgb_l3(msg);
1629 struct gsm_subscriber *subscr = msg->lchan->subscr;
1630 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1631 u_int8_t cm2_len, cm3_len = 0;
1632 u_int8_t *cm2, *cm3 = NULL;
1633
1634 DEBUGP(DRR, "CLASSMARK CHANGE ");
1635
1636 /* classmark 2 */
1637 cm2_len = gh->data[0];
1638 cm2 = &gh->data[1];
1639 DEBUGPC(DRR, "CM2(len=%u) ", cm2_len);
1640
1641 if (payload_len > cm2_len + 1) {
1642 /* we must have a classmark3 */
1643 if (gh->data[cm2_len+1] != 0x20) {
1644 DEBUGPC(DRR, "ERR CM3 TAG\n");
1645 return -EINVAL;
1646 }
1647 if (cm2_len > 3) {
1648 DEBUGPC(DRR, "CM2 too long!\n");
1649 return -EINVAL;
1650 }
1651
1652 cm3_len = gh->data[cm2_len+2];
1653 cm3 = &gh->data[cm2_len+3];
1654 if (cm3_len > 14) {
1655 DEBUGPC(DRR, "CM3 len %u too long!\n", cm3_len);
1656 return -EINVAL;
1657 }
1658 DEBUGPC(DRR, "CM3(len=%u)\n", cm3_len);
1659 }
1660 if (subscr) {
1661 subscr->classmark2_len = cm2_len;
1662 memcpy(subscr->classmark2, cm2, cm2_len);
1663 if (cm3) {
1664 subscr->classmark3_len = cm3_len;
1665 memcpy(subscr->classmark3, cm3, cm3_len);
1666 }
1667 }
1668
1669 /* FIXME: store the classmark2/3 values with the equipment register */
1670
1671 return 0;
1672}
1673
Harald Weltecf5b3592009-05-01 18:28:42 +00001674static int gsm48_rx_rr_status(struct msgb *msg)
1675{
1676 struct gsm48_hdr *gh = msgb_l3(msg);
1677
1678 DEBUGP(DRR, "STATUS rr_cause = %s\n",
1679 rr_cause_name(gh->data[0]));
1680
1681 return 0;
1682}
1683
Harald Weltef7c43522009-06-09 20:24:21 +00001684static int gsm48_rx_rr_meas_rep(struct msgb *msg)
1685{
1686 struct gsm48_hdr *gh = msgb_l3(msg);
1687 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1688 static struct gsm_meas_rep meas_rep;
1689
1690 DEBUGP(DRR, "MEASUREMENT REPORT ");
1691 parse_meas_rep(&meas_rep, gh->data, payload_len);
1692 if (meas_rep.flags & MEAS_REP_F_DTX)
1693 DEBUGPC(DRR, "DTX ");
1694 if (meas_rep.flags & MEAS_REP_F_BA1)
1695 DEBUGPC(DRR, "BA1 ");
1696 if (!(meas_rep.flags & MEAS_REP_F_VALID))
1697 DEBUGPC(DRR, "NOT VALID ");
1698 else
1699 DEBUGPC(DRR, "FULL(lev=%u, qual=%u) SUB(lev=%u, qual=%u) ",
1700 meas_rep.rxlev_full, meas_rep.rxqual_full, meas_rep.rxlev_sub,
1701 meas_rep.rxqual_sub);
1702
1703 DEBUGPC(DRR, "NUM_NEIGH=%u\n", meas_rep.num_cell);
1704
1705 /* FIXME: put the results somwhere */
1706
1707 return 0;
1708}
1709
Harald Weltebf5e8df2009-02-03 12:59:45 +00001710/* Receive a GSM 04.08 Radio Resource (RR) message */
Harald Welte52b1f982008-12-23 20:25:15 +00001711static int gsm0408_rcv_rr(struct msgb *msg)
1712{
1713 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001714 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001715
1716 switch (gh->msg_type) {
1717 case GSM48_MT_RR_CLSM_CHG:
Harald Weltef7c43522009-06-09 20:24:21 +00001718 rc = gsm48_rx_rr_classmark(msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001719 break;
Harald Weltefc977a82008-12-27 10:19:37 +00001720 case GSM48_MT_RR_GPRS_SUSP_REQ:
1721 DEBUGP(DRR, "GRPS SUSPEND REQUEST\n");
1722 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001723 case GSM48_MT_RR_PAG_RESP:
Harald Welte2d35ae62009-02-06 12:02:13 +00001724 rc = gsm48_rr_rx_pag_resp(msg);
1725 break;
Harald Welte7ccf7782009-02-17 01:43:01 +00001726 case GSM48_MT_RR_CHAN_MODE_MODIF_ACK:
1727 DEBUGP(DRR, "CHANNEL MODE MODIFY ACK\n");
Harald Welte2c38aa82009-02-18 03:44:24 +00001728 rc = rsl_chan_mode_modify_req(msg->lchan);
Harald Welte7ccf7782009-02-17 01:43:01 +00001729 break;
Harald Weltecf5b3592009-05-01 18:28:42 +00001730 case GSM48_MT_RR_STATUS:
1731 rc = gsm48_rx_rr_status(msg);
1732 break;
Harald Weltef7c43522009-06-09 20:24:21 +00001733 case GSM48_MT_RR_MEAS_REP:
1734 rc = gsm48_rx_rr_meas_rep(msg);
1735 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001736 default:
Harald Welte2d35ae62009-02-06 12:02:13 +00001737 fprintf(stderr, "Unimplemented GSM 04.08 RR msg type 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +00001738 gh->msg_type);
1739 break;
1740 }
1741
Harald Welte2d35ae62009-02-06 12:02:13 +00001742 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +00001743}
1744
Holger Freythere64a7a32009-02-06 21:55:37 +00001745/* 7.1.7 and 9.1.7 Channel release*/
1746int gsm48_send_rr_release(struct gsm_lchan *lchan)
1747{
1748 struct msgb *msg = gsm48_msgb_alloc();
1749 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1750 u_int8_t *cause;
1751
1752 msg->lchan = lchan;
1753 gh->proto_discr = GSM48_PDISC_RR;
1754 gh->msg_type = GSM48_MT_RR_CHAN_REL;
1755
1756 cause = msgb_put(msg, 1);
1757 cause[0] = GSM48_RR_CAUSE_NORMAL;
1758
1759 DEBUGP(DRR, "Sending Channel Release: Chan: Number: %d Type: %d\n",
1760 lchan->nr, lchan->type);
1761
1762 return gsm48_sendmsg(msg);
1763}
1764
Harald Welte4bc90a12008-12-27 16:32:52 +00001765/* Call Control */
1766
Harald Welte7584aea2009-02-11 11:44:12 +00001767/* The entire call control code is written in accordance with Figure 7.10c
1768 * for 'very early assignment', i.e. we allocate a TCH/F during IMMEDIATE
1769 * ASSIGN, then first use that TCH/F for signalling and later MODE MODIFY
1770 * it for voice */
1771
Harald Welte4bfdfe72009-06-10 23:11:52 +08001772static void new_cc_state(struct gsm_trans *trans, int state)
1773{
1774 if (state > 31 || state < 0)
1775 return;
1776
1777 DEBUGP(DCC, "new state %s -> %s\n",
1778 cc_state_names[trans->state], cc_state_names[state]);
1779
1780 trans->state = state;
1781}
1782
1783static int gsm48_cc_tx_status(struct gsm_trans *trans, void *arg)
Harald Welte4bc90a12008-12-27 16:32:52 +00001784{
1785 struct msgb *msg = gsm48_msgb_alloc();
1786 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1787 u_int8_t *cause, *call_state;
1788
Harald Welte4bfdfe72009-06-10 23:11:52 +08001789 gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id;
1790 msg->lchan = trans->lchan;
Harald Welte4bc90a12008-12-27 16:32:52 +00001791 gh->msg_type = GSM48_MT_CC_STATUS;
1792
1793 cause = msgb_put(msg, 3);
1794 cause[0] = 2;
1795 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
1796 cause[2] = 0x80 | 30; /* response to status inquiry */
1797
1798 call_state = msgb_put(msg, 1);
1799 call_state[0] = 0xc0 | 0x00;
1800
Harald Welte65e74cc2008-12-29 01:55:35 +00001801 return gsm48_sendmsg(msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00001802}
1803
Harald Welte6f4b7532008-12-29 00:39:37 +00001804static int gsm48_tx_simple(struct gsm_lchan *lchan,
1805 u_int8_t pdisc, u_int8_t msg_type)
Harald Welte4bc90a12008-12-27 16:32:52 +00001806{
1807 struct msgb *msg = gsm48_msgb_alloc();
1808 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1809
1810 msg->lchan = lchan;
1811
Harald Welte6f4b7532008-12-29 00:39:37 +00001812 gh->proto_discr = pdisc;
Harald Welte4bc90a12008-12-27 16:32:52 +00001813 gh->msg_type = msg_type;
1814
Harald Welte65e74cc2008-12-29 01:55:35 +00001815 return gsm48_sendmsg(msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00001816}
1817
Harald Welte4bfdfe72009-06-10 23:11:52 +08001818static void gsm48_stop_cc_timer(struct gsm_trans *trans)
1819{
1820 if (bsc_timer_pending(&trans->cc_timer)) {
1821 DEBUGP(DCC, "stopping pending timer T%x\n", trans->Tcurrent);
1822 bsc_del_timer(&trans->cc_timer);
1823 trans->Tcurrent = 0;
1824 }
1825}
1826
1827static int mncc_recvmsg(struct gsm_network *net, struct gsm_trans *trans,
1828 int msg_type, struct gsm_mncc *mncc)
1829{
1830 struct msgb *msg;
1831
1832 if (trans)
1833 if (trans->lchan)
1834 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x sub %s) "
1835 "Sending '%s' to MNCC.\n",
1836 trans->lchan->ts->trx->bts->nr,
1837 trans->lchan->ts->trx->nr,
1838 trans->lchan->ts->nr, trans->transaction_id,
1839 (trans->subscr)?(trans->subscr->extension):"-",
1840 get_mncc_name(msg_type));
1841 else
1842 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
1843 "Sending '%s' to MNCC.\n",
1844 (trans->subscr)?(trans->subscr->extension):"-",
1845 get_mncc_name(msg_type));
1846 else
1847 DEBUGP(DCC, "(bts - trx - ts - ti -- sub -) "
1848 "Sending '%s' to MNCC.\n", get_mncc_name(msg_type));
1849
1850 mncc->msg_type = msg_type;
1851
1852 msg = msgb_alloc(sizeof(struct gsm_mncc));
1853 if (!msg)
1854 return -ENOMEM;
1855 memcpy(msg->data, mncc, sizeof(struct gsm_mncc));
1856 msgb_enqueue(&net->upqueue, msg);
1857
1858 return 0;
1859}
1860
1861int mncc_release_ind(struct gsm_network *net, struct gsm_trans *trans,
1862 u_int32_t callref, int location, int value)
1863{
1864 struct gsm_mncc rel;
1865
Harald Welte92f70c52009-06-12 01:54:08 +08001866 memset(&rel, 0, sizeof(rel));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001867 rel.callref = callref;
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001868 mncc_set_cause(&rel, location, value);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001869 return mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
1870}
1871
1872void free_trans(struct gsm_trans *trans)
1873{
1874 struct gsm_bts *bts;
1875
1876 gsm48_stop_cc_timer(trans);
1877
1878 /* send release to L4, if callref still exists */
1879 if (trans->callref) {
1880 /* Ressource unavailable */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001881 mncc_release_ind(trans->network, trans, trans->callref,
1882 GSM48_CAUSE_LOC_PRN_S_LU,
1883 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001884 if (trans->state != GSM_CSTATE_NULL)
1885 new_cc_state(trans, GSM_CSTATE_NULL);
1886 }
1887
1888 if (!trans->lchan && trans->subscr && trans->subscr->net) {
1889 /* Stop paging on all bts' */
1890 bts = NULL;
1891 do {
1892 bts = gsm_bts_by_lac(trans->subscr->net,
1893 trans->subscr->lac, bts);
1894 if (!bts)
1895 break;
1896 /* Stop paging */
1897 paging_request_stop(bts, trans->subscr, NULL);
1898 } while (1);
1899 }
1900
1901 if (trans->lchan) {
1902 trau_mux_unmap(&trans->lchan->ts->e1_link, trans->callref);
1903 put_lchan(trans->lchan);
1904 }
1905
1906 if (trans->subscr)
1907 subscr_put(trans->subscr);
1908
1909 if (trans->state != GSM_CSTATE_NULL)
1910 new_cc_state(trans, GSM_CSTATE_NULL);
1911
1912 llist_del(&trans->entry);
1913
1914 free(trans);
1915}
1916
1917static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg);
1918
Harald Welte09e38af2009-02-16 22:52:23 +00001919/* call-back from paging the B-end of the connection */
1920static int setup_trig_pag_evt(unsigned int hooknum, unsigned int event,
Harald Welte7ccf7782009-02-17 01:43:01 +00001921 struct msgb *msg, void *_lchan, void *param)
Harald Welte09e38af2009-02-16 22:52:23 +00001922{
Harald Welte7ccf7782009-02-17 01:43:01 +00001923 struct gsm_lchan *lchan = _lchan;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001924 struct gsm_subscriber *subscr = param;
1925 struct gsm_trans *transt, *tmp;
1926 struct gsm_network *net;
1927
Harald Welte09e38af2009-02-16 22:52:23 +00001928 if (hooknum != GSM_HOOK_RR_PAGING)
1929 return -EINVAL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001930
1931 if (!subscr)
1932 return -EINVAL;
1933 net = subscr->net;
1934 if (!net) {
1935 DEBUGP(DCC, "Error Network not set!\n");
1936 return -EINVAL;
Harald Welte5a065df2009-02-22 21:13:18 +00001937 }
Harald Welte7584aea2009-02-11 11:44:12 +00001938
Harald Welte4bfdfe72009-06-10 23:11:52 +08001939 /* check all tranactions (without lchan) for subscriber */
1940 llist_for_each_entry_safe(transt, tmp, &net->trans_list, entry) {
1941 if (transt->subscr != subscr || transt->lchan)
1942 continue;
1943 switch (event) {
1944 case GSM_PAGING_SUCCEEDED:
1945 if (!lchan) // paranoid
1946 break;
1947 DEBUGP(DCC, "Paging subscr %s succeeded!\n",
1948 subscr->extension);
1949 /* Assign lchan */
1950 if (!transt->lchan) {
1951 transt->lchan = lchan;
1952 use_lchan(lchan);
1953 }
1954 /* send SETUP request to called party */
1955 gsm48_cc_tx_setup(transt, &transt->cc_msg);
1956 if (is_ipaccess_bts(lchan->ts->trx->bts))
1957 rsl_ipacc_bind(lchan);
1958 break;
1959 case GSM_PAGING_EXPIRED:
1960 DEBUGP(DCC, "Paging subscr %s expired!\n",
1961 subscr->extension);
1962 /* Temporarily out of order */
1963 mncc_release_ind(transt->network, transt, transt->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001964 GSM48_CAUSE_LOC_PRN_S_LU,
1965 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001966 transt->callref = 0;
1967 free_trans(transt);
1968 break;
1969 }
1970 }
Harald Welte09e38af2009-02-16 22:52:23 +00001971 return 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00001972}
Harald Welte7584aea2009-02-11 11:44:12 +00001973
Harald Welte49f48b82009-02-17 15:29:33 +00001974/* map two ipaccess RTP streams onto each other */
Harald Welte11fa29c2009-02-19 17:24:39 +00001975static int tch_map(struct gsm_lchan *lchan, struct gsm_lchan *remote_lchan)
Harald Welte49f48b82009-02-17 15:29:33 +00001976{
Harald Welte11fa29c2009-02-19 17:24:39 +00001977 struct gsm_bts *bts = lchan->ts->trx->bts;
1978 struct gsm_bts *remote_bts = remote_lchan->ts->trx->bts;
Harald Welte49f48b82009-02-17 15:29:33 +00001979 struct gsm_bts_trx_ts *ts;
1980
Harald Welte11fa29c2009-02-19 17:24:39 +00001981 DEBUGP(DCC, "Setting up TCH map between (bts=%u,trx=%u,ts=%u) and (bts=%u,trx=%u,ts=%u)\n",
1982 bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
1983 remote_bts->nr, remote_lchan->ts->trx->nr, remote_lchan->ts->nr);
1984
1985 if (bts->type != remote_bts->type) {
1986 DEBUGP(DCC, "Cannot switch calls between different BTS types yet\n");
1987 return -EINVAL;
1988 }
Harald Welte49f48b82009-02-17 15:29:33 +00001989
Harald Welte11fa29c2009-02-19 17:24:39 +00001990 switch (bts->type) {
1991 case GSM_BTS_TYPE_NANOBTS_900:
1992 case GSM_BTS_TYPE_NANOBTS_1800:
1993 ts = remote_lchan->ts;
1994 rsl_ipacc_connect(lchan, ts->abis_ip.bound_ip, ts->abis_ip.bound_port,
1995 lchan->ts->abis_ip.attr_f8, ts->abis_ip.attr_fc);
1996
1997 ts = lchan->ts;
1998 rsl_ipacc_connect(remote_lchan, ts->abis_ip.bound_ip, ts->abis_ip.bound_port,
1999 remote_lchan->ts->abis_ip.attr_f8, ts->abis_ip.attr_fc);
2000 break;
2001 case GSM_BTS_TYPE_BS11:
2002 trau_mux_map_lchan(lchan, remote_lchan);
2003 break;
2004 default:
2005 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
2006 break;
2007 }
Harald Welte49f48b82009-02-17 15:29:33 +00002008
2009 return 0;
2010}
2011
Harald Welte4bfdfe72009-06-10 23:11:52 +08002012static struct gsm_trans *get_trans_ref(struct gsm_network *net, u_int32_t callref)
Harald Welte7ccf7782009-02-17 01:43:01 +00002013{
Harald Welte4bfdfe72009-06-10 23:11:52 +08002014 struct gsm_trans *trans;
2015 llist_for_each_entry(trans, &net->trans_list, entry) {
2016 if (trans->callref == callref)
2017 return trans;
2018 }
2019 return NULL;
Harald Welte7ccf7782009-02-17 01:43:01 +00002020}
2021
Harald Welte4bfdfe72009-06-10 23:11:52 +08002022/* bridge channels of two transactions */
2023static int tch_bridge(struct gsm_network *net, u_int32_t *refs)
Harald Welte7ccf7782009-02-17 01:43:01 +00002024{
Harald Welte4bfdfe72009-06-10 23:11:52 +08002025 struct gsm_trans *trans1 = get_trans_ref(net, refs[0]);
2026 struct gsm_trans *trans2 = get_trans_ref(net, refs[1]);
Harald Welte7ccf7782009-02-17 01:43:01 +00002027
Harald Welte4bfdfe72009-06-10 23:11:52 +08002028 if (!trans1 || !trans2)
Harald Welte7ccf7782009-02-17 01:43:01 +00002029 return -EIO;
2030
Harald Welte4bfdfe72009-06-10 23:11:52 +08002031 if (!trans1->lchan || !trans2->lchan)
2032 return -EIO;
2033
2034 /* through-connect channel */
2035 return tch_map(trans1->lchan, trans2->lchan);
Harald Welte7ccf7782009-02-17 01:43:01 +00002036}
2037
Harald Welte4bfdfe72009-06-10 23:11:52 +08002038/* enable receive of channels to upqueue */
2039static int tch_recv(struct gsm_network *net, struct gsm_mncc *data, int enable)
2040{
2041 struct gsm_trans *trans;
Harald Welte7ccf7782009-02-17 01:43:01 +00002042
Harald Welte4bfdfe72009-06-10 23:11:52 +08002043 /* Find callref */
2044 trans = get_trans_ref(net, data->callref);
2045 if (!trans)
2046 return -EIO;
2047 if (!trans->lchan)
2048 return 0;
2049
2050 // todo IPACCESS
2051 if (enable)
2052 return trau_recv_lchan(trans->lchan, data->callref);
2053 return trau_mux_unmap(NULL, data->callref);
2054}
2055
2056/* send a frame to channel */
2057static int tch_frame(struct gsm_network *net, struct gsm_trau_frame *frame)
2058{
2059 struct gsm_trans *trans;
2060
2061 /* Find callref */
2062 trans = get_trans_ref(net, frame->callref);
2063 if (!trans)
2064 return -EIO;
2065 if (!trans->lchan)
2066 return 0;
2067 if (trans->lchan->type != GSM_LCHAN_TCH_F &&
2068 trans->lchan->type != GSM_LCHAN_TCH_H)
2069 return 0;
2070
2071 // todo IPACCESS
2072 return trau_send_lchan(trans->lchan,
2073 (struct decoded_trau_frame *)frame->data);
2074}
2075
2076
2077static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
2078{
2079 DEBUGP(DCC, "-> STATUS ENQ\n");
2080 return gsm48_cc_tx_status(trans, msg);
2081}
2082
2083static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg);
2084static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg);
2085
2086static void gsm48_cc_timeout(void *arg)
2087{
2088 struct gsm_trans *trans = arg;
2089 int disconnect = 0, release = 0;
Harald Weltec66b71c2009-06-11 14:23:20 +08002090 int mo_cause = GSM48_CC_CAUSE_RECOVERY_TIMER;
2091 int mo_location = GSM48_CAUSE_LOC_USER;
2092 int l4_cause = GSM48_CC_CAUSE_NORMAL_UNSPEC;
2093 int l4_location = GSM48_CAUSE_LOC_PRN_S_LU;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002094 struct gsm_mncc mo_rel, l4_rel;
2095
2096 memset(&mo_rel, 0, sizeof(struct gsm_mncc));
2097 mo_rel.callref = trans->callref;
2098 memset(&l4_rel, 0, sizeof(struct gsm_mncc));
2099 l4_rel.callref = trans->callref;
2100
2101 switch(trans->Tcurrent) {
2102 case 0x303:
2103 release = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08002104 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002105 break;
2106 case 0x310:
2107 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08002108 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002109 break;
2110 case 0x313:
2111 disconnect = 1;
2112 /* unknown, did not find it in the specs */
2113 break;
2114 case 0x301:
2115 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08002116 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002117 break;
2118 case 0x308:
2119 if (!trans->T308_second) {
2120 /* restart T308 a second time */
2121 gsm48_cc_tx_release(trans, &trans->cc_msg);
2122 trans->T308_second = 1;
2123 break; /* stay in release state */
2124 }
2125 free_trans(trans);
2126 return;
2127// release = 1;
2128// l4_cause = 14;
2129// break;
2130 case 0x306:
2131 release = 1;
2132 mo_cause = trans->cc_msg.cause.value;
2133 mo_location = trans->cc_msg.cause.location;
2134 break;
2135 case 0x323:
2136 disconnect = 1;
2137 break;
2138 default:
2139 release = 1;
2140 }
2141
2142 if (release && trans->callref) {
2143 /* process release towards layer 4 */
2144 mncc_release_ind(trans->network, trans, trans->callref,
2145 l4_location, l4_cause);
2146 trans->callref = 0;
2147 }
2148
2149 if (disconnect && trans->callref) {
2150 /* process disconnect towards layer 4 */
2151 mncc_set_cause(&l4_rel, l4_location, l4_cause);
2152 mncc_recvmsg(trans->network, trans, MNCC_DISC_IND, &l4_rel);
2153 }
2154
2155 /* process disconnect towards mobile station */
2156 if (disconnect || release) {
2157 mncc_set_cause(&mo_rel, mo_location, mo_cause);
2158 mo_rel.cause.diag[0] = ((trans->Tcurrent & 0xf00) >> 8) + '0';
2159 mo_rel.cause.diag[1] = ((trans->Tcurrent & 0x0f0) >> 4) + '0';
2160 mo_rel.cause.diag[2] = (trans->Tcurrent & 0x00f) + '0';
2161 mo_rel.cause.diag_len = 3;
2162
2163 if (disconnect)
2164 gsm48_cc_tx_disconnect(trans, &mo_rel);
2165 if (release)
2166 gsm48_cc_tx_release(trans, &mo_rel);
2167 }
2168
2169}
2170
2171static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
2172 int sec, int micro)
2173{
2174 DEBUGP(DCC, "starting timer T%x with %d seconds\n", current, sec);
2175 trans->cc_timer.cb = gsm48_cc_timeout;
2176 trans->cc_timer.data = trans;
2177 bsc_schedule_timer(&trans->cc_timer, sec, micro);
2178 trans->Tcurrent = current;
2179}
2180
2181static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
2182{
2183 struct gsm48_hdr *gh = msgb_l3(msg);
2184 u_int8_t msg_type = gh->msg_type & 0xbf;
2185 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2186 struct tlv_parsed tp;
2187 struct gsm_mncc setup;
2188
2189 memset(&setup, 0, sizeof(struct gsm_mncc));
2190 setup.callref = trans->callref;
2191 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2192 /* emergency setup is identified by msg_type */
2193 if (msg_type == GSM48_MT_CC_EMERG_SETUP)
2194 setup.emergency = 1;
2195
2196 /* use subscriber as calling party number */
2197 if (trans->subscr) {
2198 setup.fields |= MNCC_F_CALLING;
2199 strncpy(setup.calling.number, trans->subscr->extension,
2200 sizeof(setup.calling.number)-1);
2201 }
2202 /* bearer capability */
2203 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2204 setup.fields |= MNCC_F_BEARER_CAP;
2205 decode_bearer_cap(&setup.bearer_cap,
2206 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2207 }
2208 /* facility */
2209 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2210 setup.fields |= MNCC_F_FACILITY;
2211 decode_facility(&setup.facility,
2212 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2213 }
2214 /* called party bcd number */
2215 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
2216 setup.fields |= MNCC_F_CALLED;
2217 decode_called(&setup.called,
2218 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
2219 }
2220 /* user-user */
2221 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2222 setup.fields |= MNCC_F_USERUSER;
2223 decode_useruser(&setup.useruser,
2224 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2225 }
2226 /* ss-version */
2227 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2228 setup.fields |= MNCC_F_SSVERSION;
2229 decode_ssversion(&setup.ssversion,
2230 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2231 }
2232 /* CLIR suppression */
2233 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
2234 setup.clir.sup = 1;
2235 /* CLIR invocation */
2236 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
2237 setup.clir.inv = 1;
2238 /* cc cap */
2239 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
2240 setup.fields |= MNCC_F_CCCAP;
2241 decode_cccap(&setup.cccap,
2242 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
2243 }
2244
2245 if (is_ipaccess_bts(msg->trx->bts))
2246 rsl_ipacc_bind(msg->lchan);
2247
2248 new_cc_state(trans, GSM_CSTATE_INITIATED);
2249
2250 /* indicate setup to MNCC */
2251 mncc_recvmsg(trans->network, trans, MNCC_SETUP_IND, &setup);
2252
2253 return 0;
2254}
2255
2256static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
Harald Welte65e74cc2008-12-29 01:55:35 +00002257{
2258 struct msgb *msg = gsm48_msgb_alloc();
2259 struct gsm48_hdr *gh;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002260 struct gsm_mncc *setup = arg;
2261 struct gsm_trans *transt;
2262 u_int16_t trans_id_mask = 0;
2263 int rc, i;
Harald Welte65e74cc2008-12-29 01:55:35 +00002264
Harald Welte7ccf7782009-02-17 01:43:01 +00002265 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte65e74cc2008-12-29 01:55:35 +00002266
Harald Welte4bfdfe72009-06-10 23:11:52 +08002267 /* transaction id must not be assigned */
2268 if (trans->transaction_id != 0xff) { /* unasssigned */
2269 DEBUGP(DCC, "TX Setup with assigned transaction. "
2270 "This is not allowed!\n");
2271 /* Temporarily out of order */
2272 rc = mncc_release_ind(trans->network, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002273 GSM48_CAUSE_LOC_PRN_S_LU,
2274 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002275 trans->callref = 0;
2276 free_trans(trans);
2277 return rc;
2278 }
2279
2280 /* Get free transaction_id */
2281 llist_for_each_entry(transt, &trans->network->trans_list, entry) {
2282 /* Transaction of our lchan? */
2283 if (transt->lchan == trans->lchan &&
2284 transt->transaction_id != 0xff)
2285 trans_id_mask |= (1 << (transt->transaction_id >> 4));
2286 }
2287 /* Assign free transaction ID */
2288 if ((trans_id_mask & 0x007f) == 0x7f) {
2289 /* no free transaction ID */
2290 rc = mncc_release_ind(trans->network, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002291 GSM48_CAUSE_LOC_PRN_S_LU,
2292 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002293 trans->callref = 0;
2294 free_trans(trans);
2295 return rc;
2296 }
2297 for (i = 0; i < 7; i++) {
2298 if ((trans_id_mask & (1 << i)) == 0) {
2299 trans->transaction_id = i << 4; /* flag = 0 */
2300 break;
2301 }
2302 }
Harald Welte49f48b82009-02-17 15:29:33 +00002303
Harald Welte4bfdfe72009-06-10 23:11:52 +08002304 gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id;
2305 msg->lchan = trans->lchan;
Harald Welte65e74cc2008-12-29 01:55:35 +00002306 gh->msg_type = GSM48_MT_CC_SETUP;
Harald Welte09e38af2009-02-16 22:52:23 +00002307
Harald Welte4bfdfe72009-06-10 23:11:52 +08002308 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
Harald Welte65e74cc2008-12-29 01:55:35 +00002309
Harald Welte4bfdfe72009-06-10 23:11:52 +08002310 /* bearer capability */
2311 if (setup->fields & MNCC_F_BEARER_CAP)
2312 encode_bearer_cap(msg, 0, &setup->bearer_cap);
2313 /* facility */
2314 if (setup->fields & MNCC_F_FACILITY)
2315 encode_facility(msg, 0, &setup->facility);
2316 /* progress */
2317 if (setup->fields & MNCC_F_PROGRESS)
2318 encode_progress(msg, 0, &setup->progress);
2319 /* calling party BCD number */
2320 if (setup->fields & MNCC_F_CALLING)
2321 encode_calling(msg, &setup->calling);
2322 /* called party BCD number */
2323 if (setup->fields & MNCC_F_CALLED)
2324 encode_called(msg, &setup->called);
2325 /* user-user */
2326 if (setup->fields & MNCC_F_USERUSER)
2327 encode_useruser(msg, 0, &setup->useruser);
2328 /* redirecting party BCD number */
2329 if (setup->fields & MNCC_F_REDIRECTING)
2330 encode_redirecting(msg, &setup->redirecting);
2331 /* signal */
2332 if (setup->fields & MNCC_F_SIGNAL)
2333 encode_signal(msg, setup->signal);
2334
2335 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
Harald Welte65e74cc2008-12-29 01:55:35 +00002336
2337 return gsm48_sendmsg(msg);
2338}
2339
Harald Welte4bfdfe72009-06-10 23:11:52 +08002340static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
2341{
2342 struct gsm48_hdr *gh = msgb_l3(msg);
2343 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2344 struct tlv_parsed tp;
2345 struct gsm_mncc call_conf;
2346
2347 gsm48_stop_cc_timer(trans);
2348 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
2349
2350 memset(&call_conf, 0, sizeof(struct gsm_mncc));
2351 call_conf.callref = trans->callref;
2352 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2353#if 0
2354 /* repeat */
2355 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
2356 call_conf.repeat = 1;
2357 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
2358 call_conf.repeat = 2;
2359#endif
2360 /* bearer capability */
2361 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2362 call_conf.fields |= MNCC_F_BEARER_CAP;
2363 decode_bearer_cap(&call_conf.bearer_cap,
2364 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2365 }
2366 /* cause */
2367 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2368 call_conf.fields |= MNCC_F_CAUSE;
2369 decode_cause(&call_conf.cause,
2370 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2371 }
2372 /* cc cap */
2373 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
2374 call_conf.fields |= MNCC_F_CCCAP;
2375 decode_cccap(&call_conf.cccap,
2376 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
2377 }
2378
2379 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
2380
2381 return mncc_recvmsg(trans->network, trans, MNCC_CALL_CONF_IND, &call_conf);
2382}
2383
2384static int gsm48_cc_tx_call_proc(struct gsm_trans *trans, void *arg)
2385{
2386 struct gsm_mncc *proceeding = arg;
2387 struct msgb *msg = gsm48_msgb_alloc();
2388 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2389
2390 gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id;
2391 msg->lchan = trans->lchan;
2392 gh->msg_type = GSM48_MT_CC_CALL_PROC;
2393
2394 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
2395
2396 /* bearer capability */
2397 if (proceeding->fields & MNCC_F_BEARER_CAP)
2398 encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
2399 /* facility */
2400 if (proceeding->fields & MNCC_F_FACILITY)
2401 encode_facility(msg, 0, &proceeding->facility);
2402 /* progress */
2403 if (proceeding->fields & MNCC_F_PROGRESS)
2404 encode_progress(msg, 0, &proceeding->progress);
2405
2406 return gsm48_sendmsg(msg);
2407}
2408
2409static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
2410{
2411 struct gsm48_hdr *gh = msgb_l3(msg);
2412 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2413 struct tlv_parsed tp;
2414 struct gsm_mncc alerting;
2415
2416 gsm48_stop_cc_timer(trans);
2417 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
2418
2419 memset(&alerting, 0, sizeof(struct gsm_mncc));
2420 alerting.callref = trans->callref;
2421 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2422 /* facility */
2423 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2424 alerting.fields |= MNCC_F_FACILITY;
2425 decode_facility(&alerting.facility,
2426 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2427 }
2428
2429 /* progress */
2430 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
2431 alerting.fields |= MNCC_F_PROGRESS;
2432 decode_progress(&alerting.progress,
2433 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
2434 }
2435 /* ss-version */
2436 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2437 alerting.fields |= MNCC_F_SSVERSION;
2438 decode_ssversion(&alerting.ssversion,
2439 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2440 }
2441
2442 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
2443
2444 return mncc_recvmsg(trans->network, trans, MNCC_ALERT_IND, &alerting);
2445}
2446
2447static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
2448{
2449 struct gsm_mncc *alerting = arg;
2450 struct msgb *msg = gsm48_msgb_alloc();
2451 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2452
2453 gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id;
2454 msg->lchan = trans->lchan;
2455 gh->msg_type = GSM48_MT_CC_ALERTING;
2456
2457 /* facility */
2458 if (alerting->fields & MNCC_F_FACILITY)
2459 encode_facility(msg, 0, &alerting->facility);
2460 /* progress */
2461 if (alerting->fields & MNCC_F_PROGRESS)
2462 encode_progress(msg, 0, &alerting->progress);
2463 /* user-user */
2464 if (alerting->fields & MNCC_F_USERUSER)
2465 encode_useruser(msg, 0, &alerting->useruser);
2466
2467 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
2468
2469 return gsm48_sendmsg(msg);
2470}
2471
2472static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
2473{
2474 struct gsm_mncc *progress = arg;
2475 struct msgb *msg = gsm48_msgb_alloc();
2476 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2477
2478 gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id;
2479 msg->lchan = trans->lchan;
2480 gh->msg_type = GSM48_MT_CC_PROGRESS;
2481
2482 /* progress */
2483 encode_progress(msg, 1, &progress->progress);
2484 /* user-user */
2485 if (progress->fields & MNCC_F_USERUSER)
2486 encode_useruser(msg, 0, &progress->useruser);
2487
2488 return gsm48_sendmsg(msg);
2489}
2490
2491static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
2492{
2493 struct gsm_mncc *connect = arg;
2494 struct msgb *msg = gsm48_msgb_alloc();
2495 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2496
2497 gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id;
2498 msg->lchan = trans->lchan;
2499 gh->msg_type = GSM48_MT_CC_CONNECT;
2500
2501 gsm48_stop_cc_timer(trans);
2502 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
2503
2504 /* facility */
2505 if (connect->fields & MNCC_F_FACILITY)
2506 encode_facility(msg, 0, &connect->facility);
2507 /* progress */
2508 if (connect->fields & MNCC_F_PROGRESS)
2509 encode_progress(msg, 0, &connect->progress);
2510 /* connected number */
2511 if (connect->fields & MNCC_F_CONNECTED)
2512 encode_connected(msg, &connect->connected);
2513 /* user-user */
2514 if (connect->fields & MNCC_F_USERUSER)
2515 encode_useruser(msg, 0, &connect->useruser);
2516
2517 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
2518
2519 return gsm48_sendmsg(msg);
2520}
2521
2522static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
2523{
2524 struct gsm48_hdr *gh = msgb_l3(msg);
2525 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2526 struct tlv_parsed tp;
2527 struct gsm_mncc connect;
2528
2529 gsm48_stop_cc_timer(trans);
2530
2531 memset(&connect, 0, sizeof(struct gsm_mncc));
2532 connect.callref = trans->callref;
2533 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2534 /* use subscriber as connected party number */
2535 if (trans->subscr) {
2536 connect.fields |= MNCC_F_CONNECTED;
2537 strncpy(connect.connected.number, trans->subscr->extension,
2538 sizeof(connect.connected.number)-1);
2539 }
2540 /* facility */
2541 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2542 connect.fields |= MNCC_F_FACILITY;
2543 decode_facility(&connect.facility,
2544 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2545 }
2546 /* user-user */
2547 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2548 connect.fields |= MNCC_F_USERUSER;
2549 decode_useruser(&connect.useruser,
2550 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2551 }
2552 /* ss-version */
2553 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2554 connect.fields |= MNCC_F_SSVERSION;
2555 decode_ssversion(&connect.ssversion,
2556 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2557 }
2558
2559 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
2560
2561 return mncc_recvmsg(trans->network, trans, MNCC_SETUP_CNF, &connect);
2562}
2563
2564
2565static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
2566{
2567 struct gsm_mncc connect_ack;
2568
2569 gsm48_stop_cc_timer(trans);
2570
2571 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2572
2573 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
2574 connect_ack.callref = trans->callref;
2575 return mncc_recvmsg(trans->network, trans, MNCC_SETUP_COMPL_IND,
2576 &connect_ack);
2577}
2578
2579static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
2580{
2581 struct msgb *msg = gsm48_msgb_alloc();
2582 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2583
2584 gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id;
2585 msg->lchan = trans->lchan;
2586 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
2587
2588 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2589
2590 return gsm48_sendmsg(msg);
2591}
2592
2593static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
2594{
2595 struct gsm48_hdr *gh = msgb_l3(msg);
2596 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2597 struct tlv_parsed tp;
2598 struct gsm_mncc disc;
2599
2600 gsm48_stop_cc_timer(trans);
2601
2602 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
2603
2604 memset(&disc, 0, sizeof(struct gsm_mncc));
2605 disc.callref = trans->callref;
2606 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
2607 /* cause */
2608 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2609 disc.fields |= MNCC_F_CAUSE;
2610 decode_cause(&disc.cause,
2611 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2612 }
2613 /* facility */
2614 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2615 disc.fields |= MNCC_F_FACILITY;
2616 decode_facility(&disc.facility,
2617 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2618 }
2619 /* user-user */
2620 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2621 disc.fields |= MNCC_F_USERUSER;
2622 decode_useruser(&disc.useruser,
2623 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2624 }
2625 /* ss-version */
2626 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2627 disc.fields |= MNCC_F_SSVERSION;
2628 decode_ssversion(&disc.ssversion,
2629 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2630 }
2631
2632 return mncc_recvmsg(trans->network, trans, MNCC_DISC_IND, &disc);
2633
2634}
2635
Harald Weltec66b71c2009-06-11 14:23:20 +08002636static struct gsm_mncc_cause default_cause = {
2637 .location = GSM48_CAUSE_LOC_PRN_S_LU,
2638 .coding = 0,
2639 .rec = 0,
2640 .rec_val = 0,
2641 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
2642 .diag_len = 0,
2643 .diag = { 0 },
2644};
Harald Welte4bfdfe72009-06-10 23:11:52 +08002645
2646static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
2647{
2648 struct gsm_mncc *disc = arg;
2649 struct msgb *msg = gsm48_msgb_alloc();
2650 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2651
2652 gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id;
2653 msg->lchan = trans->lchan;
2654 gh->msg_type = GSM48_MT_CC_DISCONNECT;
2655
2656 gsm48_stop_cc_timer(trans);
2657 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
2658
2659 /* cause */
2660 if (disc->fields & MNCC_F_CAUSE)
2661 encode_cause(msg, 1, &disc->cause);
2662 else
2663 encode_cause(msg, 1, &default_cause);
2664
2665 /* facility */
2666 if (disc->fields & MNCC_F_FACILITY)
2667 encode_facility(msg, 0, &disc->facility);
2668 /* progress */
2669 if (disc->fields & MNCC_F_PROGRESS)
2670 encode_progress(msg, 0, &disc->progress);
2671 /* user-user */
2672 if (disc->fields & MNCC_F_USERUSER)
2673 encode_useruser(msg, 0, &disc->useruser);
2674
2675 /* store disconnect cause for T306 expiry */
2676 memcpy(&trans->cc_msg, disc, sizeof(struct gsm_mncc));
2677
2678 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
2679
2680 return gsm48_sendmsg(msg);
2681}
2682
2683static int gsm48_cc_rx_release(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;
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->state == GSM_CSTATE_RELEASE_REQ) {
2722 /* release collision 5.4.5 */
2723 rc = mncc_recvmsg(trans->network, trans, MNCC_REL_CNF, &rel);
2724 } else {
2725 rc = gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC | trans->transaction_id,
2726 GSM48_MT_CC_RELEASE_COMPL);
2727 rc = mncc_recvmsg(trans->network, trans, MNCC_REL_IND, &rel);
2728 }
2729
2730 new_cc_state(trans, GSM_CSTATE_NULL);
2731
2732 trans->callref = 0;
2733 free_trans(trans);
2734
2735 return rc;
2736}
2737
2738static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
2739{
2740 struct gsm_mncc *rel = arg;
2741 struct msgb *msg = gsm48_msgb_alloc();
2742 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2743
2744 gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id;
2745 msg->lchan = trans->lchan;
2746 gh->msg_type = GSM48_MT_CC_RELEASE;
2747
2748 trans->callref = 0;
2749
2750 gsm48_stop_cc_timer(trans);
2751 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
2752
2753 /* cause */
2754 if (rel->fields & MNCC_F_CAUSE)
2755 encode_cause(msg, 0, &rel->cause);
2756 /* facility */
2757 if (rel->fields & MNCC_F_FACILITY)
2758 encode_facility(msg, 0, &rel->facility);
2759 /* user-user */
2760 if (rel->fields & MNCC_F_USERUSER)
2761 encode_useruser(msg, 0, &rel->useruser);
2762
2763 trans->T308_second = 0;
2764 memcpy(&trans->cc_msg, rel, sizeof(struct gsm_mncc));
2765
2766 if (trans->state != GSM_CSTATE_RELEASE_REQ)
2767 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
2768
2769 return gsm48_sendmsg(msg);
2770}
2771
2772static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
2773{
2774 struct gsm48_hdr *gh = msgb_l3(msg);
2775 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2776 struct tlv_parsed tp;
2777 struct gsm_mncc rel;
2778 int rc = 0;
2779
2780 gsm48_stop_cc_timer(trans);
2781
2782 memset(&rel, 0, sizeof(struct gsm_mncc));
2783 rel.callref = trans->callref;
2784 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2785 /* cause */
2786 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2787 rel.fields |= MNCC_F_CAUSE;
2788 decode_cause(&rel.cause,
2789 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2790 }
2791 /* facility */
2792 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2793 rel.fields |= MNCC_F_FACILITY;
2794 decode_facility(&rel.facility,
2795 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2796 }
2797 /* user-user */
2798 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2799 rel.fields |= MNCC_F_USERUSER;
2800 decode_useruser(&rel.useruser,
2801 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2802 }
2803 /* ss-version */
2804 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2805 rel.fields |= MNCC_F_SSVERSION;
2806 decode_ssversion(&rel.ssversion,
2807 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2808 }
2809
2810 if (trans->callref) {
2811 switch (trans->state) {
2812 case GSM_CSTATE_CALL_PRESENT:
2813 rc = mncc_recvmsg(trans->network, trans,
2814 MNCC_REJ_IND, &rel);
2815 break;
2816 case GSM_CSTATE_RELEASE_REQ:
2817 rc = mncc_recvmsg(trans->network, trans,
2818 MNCC_REL_CNF, &rel);
2819 break;
2820 default:
2821 rc = mncc_recvmsg(trans->network, trans,
2822 MNCC_REL_IND, &rel);
2823 }
2824 }
2825
2826 trans->callref = 0;
2827 free_trans(trans);
2828
2829 return rc;
2830}
2831
2832static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
2833{
2834 struct gsm_mncc *rel = arg;
2835 struct msgb *msg = gsm48_msgb_alloc();
2836 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2837
2838 gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id;
2839 msg->lchan = trans->lchan;
2840 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
2841
2842 trans->callref = 0;
2843
2844 gsm48_stop_cc_timer(trans);
2845
2846 /* cause */
2847 if (rel->fields & MNCC_F_CAUSE)
2848 encode_cause(msg, 0, &rel->cause);
2849 /* facility */
2850 if (rel->fields & MNCC_F_FACILITY)
2851 encode_facility(msg, 0, &rel->facility);
2852 /* user-user */
2853 if (rel->fields & MNCC_F_USERUSER)
2854 encode_useruser(msg, 0, &rel->useruser);
2855
2856 free_trans(trans);
2857
2858 return gsm48_sendmsg(msg);
2859}
2860
2861static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
2862{
2863 struct gsm48_hdr *gh = msgb_l3(msg);
2864 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2865 struct tlv_parsed tp;
2866 struct gsm_mncc fac;
2867
2868 memset(&fac, 0, sizeof(struct gsm_mncc));
2869 fac.callref = trans->callref;
2870 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
2871 /* facility */
2872 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2873 fac.fields |= MNCC_F_FACILITY;
2874 decode_facility(&fac.facility,
2875 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2876 }
2877 /* ss-version */
2878 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2879 fac.fields |= MNCC_F_SSVERSION;
2880 decode_ssversion(&fac.ssversion,
2881 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2882 }
2883
2884 return mncc_recvmsg(trans->network, trans, MNCC_FACILITY_IND, &fac);
2885}
2886
2887static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
2888{
2889 struct gsm_mncc *fac = arg;
2890 struct msgb *msg = gsm48_msgb_alloc();
2891 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2892
2893 gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id;
2894 msg->lchan = trans->lchan;
2895 gh->msg_type = GSM48_MT_CC_FACILITY;
2896
2897 /* facility */
2898 encode_facility(msg, 1, &fac->facility);
2899
2900 return gsm48_sendmsg(msg);
2901}
2902
2903static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
2904{
2905 struct gsm_mncc hold;
2906
2907 memset(&hold, 0, sizeof(struct gsm_mncc));
2908 hold.callref = trans->callref;
2909 return mncc_recvmsg(trans->network, trans, MNCC_HOLD_IND, &hold);
2910}
2911
2912static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
2913{
2914 struct msgb *msg = gsm48_msgb_alloc();
2915 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2916
2917 gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id;
2918 msg->lchan = trans->lchan;
2919 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
2920
2921 return gsm48_sendmsg(msg);
2922}
2923
2924static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
2925{
2926 struct gsm_mncc *hold_rej = arg;
2927 struct msgb *msg = gsm48_msgb_alloc();
2928 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2929
2930 gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id;
2931 msg->lchan = trans->lchan;
2932 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
2933
2934 /* cause */
2935 if (hold_rej->fields & MNCC_F_CAUSE)
2936 encode_cause(msg, 1, &hold_rej->cause);
2937 else
2938 encode_cause(msg, 1, &default_cause);
2939
2940 return gsm48_sendmsg(msg);
2941}
2942
2943static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
2944{
2945 struct gsm_mncc retrieve;
2946
2947 memset(&retrieve, 0, sizeof(struct gsm_mncc));
2948 retrieve.callref = trans->callref;
2949 return mncc_recvmsg(trans->network, trans, MNCC_RETRIEVE_IND, &retrieve);
2950}
2951
2952static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
2953{
2954 struct msgb *msg = gsm48_msgb_alloc();
2955 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2956
2957 gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id;
2958 msg->lchan = trans->lchan;
2959 gh->msg_type = GSM48_MT_CC_RETR_ACK;
2960
2961 return gsm48_sendmsg(msg);
2962}
2963
2964static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
2965{
2966 struct gsm_mncc *retrieve_rej = arg;
2967 struct msgb *msg = gsm48_msgb_alloc();
2968 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2969
2970 gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id;
2971 msg->lchan = trans->lchan;
2972 gh->msg_type = GSM48_MT_CC_RETR_REJ;
2973
2974 /* cause */
2975 if (retrieve_rej->fields & MNCC_F_CAUSE)
2976 encode_cause(msg, 1, &retrieve_rej->cause);
2977 else
2978 encode_cause(msg, 1, &default_cause);
2979
2980 return gsm48_sendmsg(msg);
2981}
2982
2983static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
2984{
2985 struct gsm48_hdr *gh = msgb_l3(msg);
2986 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2987 struct tlv_parsed tp;
2988 struct gsm_mncc dtmf;
2989
2990 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2991 dtmf.callref = trans->callref;
2992 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2993 /* keypad facility */
2994 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
2995 dtmf.fields |= MNCC_F_KEYPAD;
2996 decode_keypad(&dtmf.keypad,
2997 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
2998 }
2999
3000 return mncc_recvmsg(trans->network, trans, MNCC_START_DTMF_IND, &dtmf);
3001}
3002
3003static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
3004{
3005 struct gsm_mncc *dtmf = arg;
3006 struct msgb *msg = gsm48_msgb_alloc();
3007 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3008
3009 gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id;
3010 msg->lchan = trans->lchan;
3011 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
3012
3013 /* keypad */
3014 if (dtmf->fields & MNCC_F_KEYPAD)
3015 encode_keypad(msg, dtmf->keypad);
3016
3017 return gsm48_sendmsg(msg);
3018}
3019
3020static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
3021{
3022 struct gsm_mncc *dtmf = arg;
3023 struct msgb *msg = gsm48_msgb_alloc();
3024 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3025
3026 gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id;
3027 msg->lchan = trans->lchan;
3028 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
3029
3030 /* cause */
3031 if (dtmf->fields & MNCC_F_CAUSE)
3032 encode_cause(msg, 1, &dtmf->cause);
3033 else
3034 encode_cause(msg, 1, &default_cause);
3035
3036 return gsm48_sendmsg(msg);
3037}
3038
3039static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
3040{
3041 struct msgb *msg = gsm48_msgb_alloc();
3042 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3043
3044 gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id;
3045 msg->lchan = trans->lchan;
3046 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
3047
3048 return gsm48_sendmsg(msg);
3049}
3050
3051static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
3052{
3053 struct gsm_mncc dtmf;
3054
3055 memset(&dtmf, 0, sizeof(struct gsm_mncc));
3056 dtmf.callref = trans->callref;
3057
3058 return mncc_recvmsg(trans->network, trans, MNCC_STOP_DTMF_IND, &dtmf);
3059}
3060
3061static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
3062{
3063 struct gsm48_hdr *gh = msgb_l3(msg);
3064 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
3065 struct tlv_parsed tp;
3066 struct gsm_mncc modify;
3067
3068 memset(&modify, 0, sizeof(struct gsm_mncc));
3069 modify.callref = trans->callref;
3070 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
3071 /* bearer capability */
3072 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
3073 modify.fields |= MNCC_F_BEARER_CAP;
3074 decode_bearer_cap(&modify.bearer_cap,
3075 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
3076 }
3077
3078 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
3079
3080 return mncc_recvmsg(trans->network, trans, MNCC_MODIFY_IND, &modify);
3081}
3082
3083static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
3084{
3085 struct gsm_mncc *modify = arg;
3086 struct msgb *msg = gsm48_msgb_alloc();
3087 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3088
3089 gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id;
3090 msg->lchan = trans->lchan;
3091 gh->msg_type = GSM48_MT_CC_MODIFY;
3092
3093 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
3094
3095 /* bearer capability */
3096 encode_bearer_cap(msg, 1, &modify->bearer_cap);
3097
3098 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
3099
3100 return gsm48_sendmsg(msg);
3101}
3102
3103static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
3104{
3105 struct gsm48_hdr *gh = msgb_l3(msg);
3106 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
3107 struct tlv_parsed tp;
3108 struct gsm_mncc modify;
3109
3110 gsm48_stop_cc_timer(trans);
3111
3112 memset(&modify, 0, sizeof(struct gsm_mncc));
3113 modify.callref = trans->callref;
3114 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
3115 /* bearer capability */
3116 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
3117 modify.fields |= MNCC_F_BEARER_CAP;
3118 decode_bearer_cap(&modify.bearer_cap,
3119 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
3120 }
3121
3122 new_cc_state(trans, GSM_CSTATE_ACTIVE);
3123
3124 return mncc_recvmsg(trans->network, trans, MNCC_MODIFY_CNF, &modify);
3125}
3126
3127static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
3128{
3129 struct gsm_mncc *modify = arg;
3130 struct msgb *msg = gsm48_msgb_alloc();
3131 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3132
3133 gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id;
3134 msg->lchan = trans->lchan;
3135 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
3136
3137 /* bearer capability */
3138 encode_bearer_cap(msg, 1, &modify->bearer_cap);
3139
3140 new_cc_state(trans, GSM_CSTATE_ACTIVE);
3141
3142 return gsm48_sendmsg(msg);
3143}
3144
3145static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
3146{
3147 struct gsm48_hdr *gh = msgb_l3(msg);
3148 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
3149 struct tlv_parsed tp;
3150 struct gsm_mncc modify;
3151
3152 gsm48_stop_cc_timer(trans);
3153
3154 memset(&modify, 0, sizeof(struct gsm_mncc));
3155 modify.callref = trans->callref;
3156 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
3157 /* bearer capability */
3158 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
3159 modify.fields |= GSM48_IE_BEARER_CAP;
3160 decode_bearer_cap(&modify.bearer_cap,
3161 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
3162 }
3163 /* cause */
3164 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
3165 modify.fields |= MNCC_F_CAUSE;
3166 decode_cause(&modify.cause,
3167 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
3168 }
3169
3170 new_cc_state(trans, GSM_CSTATE_ACTIVE);
3171
3172 return mncc_recvmsg(trans->network, trans, MNCC_MODIFY_REJ, &modify);
3173}
3174
3175static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
3176{
3177 struct gsm_mncc *modify = arg;
3178 struct msgb *msg = gsm48_msgb_alloc();
3179 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3180
3181 gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id;
3182 msg->lchan = trans->lchan;
3183 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
3184
3185 /* bearer capability */
3186 encode_bearer_cap(msg, 1, &modify->bearer_cap);
3187 /* cause */
3188 encode_cause(msg, 1, &modify->cause);
3189
3190 new_cc_state(trans, GSM_CSTATE_ACTIVE);
3191
3192 return gsm48_sendmsg(msg);
3193}
3194
3195static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
3196{
3197 struct gsm_mncc *notify = arg;
3198 struct msgb *msg = gsm48_msgb_alloc();
3199 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3200
3201 gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id;
3202 msg->lchan = trans->lchan;
3203 gh->msg_type = GSM48_MT_CC_NOTIFY;
3204
3205 /* notify */
3206 encode_notify(msg, notify->notify);
3207
3208 return gsm48_sendmsg(msg);
3209}
3210
3211static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
3212{
3213 struct gsm48_hdr *gh = msgb_l3(msg);
3214 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
3215// struct tlv_parsed tp;
3216 struct gsm_mncc notify;
3217
3218 memset(&notify, 0, sizeof(struct gsm_mncc));
3219 notify.callref = trans->callref;
3220// tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len);
3221 if (payload_len >= 1)
3222 decode_notify(&notify.notify, gh->data);
3223
3224 return mncc_recvmsg(trans->network, trans, MNCC_NOTIFY_IND, &notify);
3225}
3226
3227static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
3228{
3229 struct gsm_mncc *user = arg;
3230 struct msgb *msg = gsm48_msgb_alloc();
3231 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3232
3233 gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id;
3234 msg->lchan = trans->lchan;
3235 gh->msg_type = GSM48_MT_CC_USER_INFO;
3236
3237 /* user-user */
3238 if (user->fields & MNCC_F_USERUSER)
3239 encode_useruser(msg, 1, &user->useruser);
3240 /* more data */
3241 if (user->more)
3242 encode_more(msg);
3243
3244 return gsm48_sendmsg(msg);
3245}
3246
3247static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
3248{
3249 struct gsm48_hdr *gh = msgb_l3(msg);
3250 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
3251 struct tlv_parsed tp;
3252 struct gsm_mncc user;
3253
3254 memset(&user, 0, sizeof(struct gsm_mncc));
3255 user.callref = trans->callref;
3256 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
3257 /* user-user */
3258 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
3259 user.fields |= MNCC_F_USERUSER;
3260 decode_useruser(&user.useruser,
3261 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
3262 }
3263 /* more data */
3264 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
3265 user.more = 1;
3266
3267 return mncc_recvmsg(trans->network, trans, MNCC_USERINFO_IND, &user);
3268}
3269
3270static int gsm48_lchan_modify(struct gsm_trans *trans, void *arg)
3271{
3272 struct gsm_mncc *mode = arg;
3273
3274 return gsm48_tx_chan_mode_modify(trans->lchan, mode->lchan_mode);
3275}
3276
3277static struct downstate {
3278 u_int32_t states;
3279 int type;
3280 int (*rout) (struct gsm_trans *trans, void *arg);
3281} downstatelist[] = {
3282 /* mobile originating call establishment */
3283 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
3284 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc},
3285 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
3286 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
3287 {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 */
3288 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
3289 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
3290 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
3291 /* mobile terminating call establishment */
3292 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
3293 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
3294 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
3295 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
3296 /* signalling during call */
3297 {SBIT(GSM_CSTATE_ACTIVE),
3298 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
3299 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
3300 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
3301 {ALL_STATES,
3302 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
3303 {ALL_STATES,
3304 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
3305 {ALL_STATES,
3306 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
3307 {SBIT(GSM_CSTATE_ACTIVE),
3308 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
3309 {SBIT(GSM_CSTATE_ACTIVE),
3310 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
3311 {SBIT(GSM_CSTATE_ACTIVE),
3312 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
3313 {SBIT(GSM_CSTATE_ACTIVE),
3314 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
3315 {SBIT(GSM_CSTATE_ACTIVE),
3316 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
3317 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
3318 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
3319 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
3320 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
3321 {SBIT(GSM_CSTATE_ACTIVE),
3322 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
3323 /* clearing */
3324 {SBIT(GSM_CSTATE_INITIATED),
3325 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
3326 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
3327 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
3328 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
3329 MNCC_REL_REQ, gsm48_cc_tx_release},
3330 /* special */
3331 {ALL_STATES,
3332 MNCC_LCHAN_MODIFY, gsm48_lchan_modify},
3333};
3334
3335#define DOWNSLLEN \
3336 (sizeof(downstatelist) / sizeof(struct downstate))
3337
3338
3339int mncc_send(struct gsm_network *net, int msg_type, void *arg)
3340{
3341 int i, j, k, l, rc = 0;
3342 struct gsm_trans *trans = NULL, *transt;
3343 struct gsm_subscriber *subscr;
3344 struct gsm_lchan *lchan = NULL, *lchant;
3345 struct gsm_bts *bts = NULL;
3346 struct gsm_bts_trx *trx;
3347 struct gsm_bts_trx_ts *ts;
3348 struct gsm_mncc *data = arg, rel;
3349
3350 /* handle special messages */
3351 switch(msg_type) {
3352 case MNCC_BRIDGE:
3353 return tch_bridge(net, arg);
3354 case MNCC_FRAME_DROP:
3355 return tch_recv(net, arg, 0);
3356 case MNCC_FRAME_RECV:
3357 return tch_recv(net, arg, 1);
3358 case GSM_TRAU_FRAME:
3359 return tch_frame(net, arg);
3360 }
3361
3362 memset(&rel, 0, sizeof(struct gsm_mncc));
3363 rel.callref = data->callref;
3364
3365 /* Find callref */
3366 trans = get_trans_ref(net, data->callref);
3367
3368 /* Callref unknown */
3369 if (!trans) {
3370 if (msg_type != MNCC_SETUP_REQ || !data->called.number[0]) {
3371 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3372 "Received '%s' from MNCC with "
3373 "unknown callref %d\n", data->called.number,
3374 get_mncc_name(msg_type), data->callref);
3375 /* Invalid call reference */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003376 return mncc_release_ind(net, NULL, data->callref,
3377 GSM48_CAUSE_LOC_PRN_S_LU,
3378 GSM48_CC_CAUSE_INVAL_TRANS_ID);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003379 }
3380 /* New transaction due to setup, find subscriber */
3381 subscr = subscr_get_by_extension(data->called.number);
3382 /* If subscriber is not found */
3383 if (!subscr) {
3384 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3385 "Received '%s' from MNCC with "
3386 "unknown subscriber %s\n", data->called.number,
3387 get_mncc_name(msg_type), data->called.number);
3388 /* Unknown subscriber */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003389 return mncc_release_ind(net, NULL, data->callref,
3390 GSM48_CAUSE_LOC_PRN_S_LU,
3391 GSM48_CC_CAUSE_UNASSIGNED_NR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003392 }
3393 /* If subscriber is not "attached" */
3394 if (!subscr->lac) {
3395 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3396 "Received '%s' from MNCC with "
3397 "detached subscriber %s\n", data->called.number,
3398 get_mncc_name(msg_type), data->called.number);
3399 subscr_put(subscr);
3400 /* Temporarily out of order */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003401 return mncc_release_ind(net, NULL, data->callref,
3402 GSM48_CAUSE_LOC_PRN_S_LU,
3403 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003404 }
3405 /* Create transaction */
3406 if (!(trans = calloc(1, sizeof(struct gsm_trans)))) {
3407 DEBUGP(DCC, "No memory for trans.\n");
3408 subscr_put(subscr);
3409 /* Ressource unavailable */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003410 mncc_release_ind(net, NULL, data->callref,
3411 GSM48_CAUSE_LOC_PRN_S_LU,
3412 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003413 return -ENOMEM;
3414 }
3415 trans->callref = data->callref;
3416 trans->network = net;
3417 trans->transaction_id = 0xff; /* unassigned */
3418 llist_add_tail(&trans->entry, &net->trans_list);
3419 /* Assign subscriber to transaction */
3420 trans->subscr = subscr;
3421 /* Find lchan */
3422 for (i = 0; i < net->num_bts; i++) {
3423 bts = &net->bts[i];
3424 for (j = 0; j < bts->num_trx; j++) {
3425 trx = &bts->trx[j];
3426 for (k = 0; k < TRX_NR_TS; k++) {
3427 ts = &trx->ts[k];
3428 for (l = 0; l < TS_MAX_LCHAN; l++) {
3429 lchant = &ts->lchan[l];
3430 if (lchant->subscr == subscr) {
3431 lchan = lchant;
3432 break;
3433 }
3434 }
3435 }
3436 }
3437 }
3438
3439 /* If subscriber has no lchan */
3440 if (!lchan) {
3441 /* find transaction with this subscriber already paging */
3442 llist_for_each_entry(transt, &net->trans_list, entry) {
3443 /* Transaction of our lchan? */
3444 if (transt == trans ||
3445 transt->subscr != subscr)
3446 continue;
3447 DEBUGP(DCC, "(bts %d trx - ts - ti -- sub %s) "
3448 "Received '%s' from MNCC with "
3449 "unallocated channel, paging already "
3450 "started.\n", bts->nr,
3451 data->called.number,
3452 get_mncc_name(msg_type));
3453 return 0;
3454 }
3455 /* store setup informations until paging was successfull */
3456 memcpy(&trans->cc_msg, data, sizeof(struct gsm_mncc));
3457 /* start paging subscriber on all BTS with her location */
3458 subscr->net = net;
3459 bts = NULL;
3460 do {
3461 bts = gsm_bts_by_lac(net, subscr->lac, bts);
3462 if (!bts)
3463 break;
3464 DEBUGP(DCC, "(bts %d trx - ts - ti -- sub %s) "
3465 "Received '%s' from MNCC with "
3466 "unallocated channel, paging.\n",
3467 bts->nr, data->called.number,
3468 get_mncc_name(msg_type));
3469 /* Trigger paging */
Harald Welte92f70c52009-06-12 01:54:08 +08003470 paging_request(net, subscr, RSL_CHANNEED_TCH_F,
Harald Welte4bfdfe72009-06-10 23:11:52 +08003471 setup_trig_pag_evt, subscr);
3472 } while (1);
3473 return 0;
3474 }
3475 /* Assign lchan */
3476 trans->lchan = lchan;
3477 use_lchan(lchan);
3478 }
3479 lchan = trans->lchan;
3480
3481 /* if paging did not respond yet */
3482 if (!lchan) {
3483 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3484 "Received '%s' from MNCC in paging state\n",
3485 (trans->subscr)?(trans->subscr->extension):"-",
3486 get_mncc_name(msg_type));
Harald Weltec66b71c2009-06-11 14:23:20 +08003487 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
3488 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003489 if (msg_type == MNCC_REL_REQ)
3490 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
3491 else
3492 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
3493 trans->callref = 0;
3494 free_trans(trans);
3495 return rc;
3496 }
3497
3498 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x sub %s) "
3499 "Received '%s' from MNCC in state %d (%s)\n",
3500 lchan->ts->trx->bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
3501 trans->transaction_id,
3502 (lchan->subscr)?(lchan->subscr->extension):"-",
3503 get_mncc_name(msg_type), trans->state,
3504 cc_state_names[trans->state]);
3505
3506 /* Find function for current state and message */
3507 for (i = 0; i < DOWNSLLEN; i++)
3508 if ((msg_type == downstatelist[i].type)
3509 && ((1 << trans->state) & downstatelist[i].states))
3510 break;
3511 if (i == DOWNSLLEN) {
3512 DEBUGP(DCC, "Message unhandled at this state.\n");
3513 return 0;
3514 }
3515
3516 rc = downstatelist[i].rout(trans, arg);
3517
3518 return rc;
3519}
3520
3521
3522static struct datastate {
3523 u_int32_t states;
3524 int type;
3525 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
3526} datastatelist[] = {
3527 /* mobile originating call establishment */
3528 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3529 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
3530 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3531 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
3532 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
3533 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
3534 /* mobile terminating call establishment */
3535 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
3536 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
3537 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
3538 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
3539 {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 */
3540 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
3541 /* signalling during call */
3542 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
3543 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
3544 {SBIT(GSM_CSTATE_ACTIVE),
3545 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
3546 {ALL_STATES,
3547 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
3548 {ALL_STATES,
3549 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
3550 {ALL_STATES,
3551 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
3552 {SBIT(GSM_CSTATE_ACTIVE),
3553 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
3554 {SBIT(GSM_CSTATE_ACTIVE),
3555 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
3556 {SBIT(GSM_CSTATE_ACTIVE),
3557 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
3558 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3559 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
3560 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3561 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
3562 {SBIT(GSM_CSTATE_ACTIVE),
3563 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
3564 /* clearing */
3565 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
3566 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
3567 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
3568 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
3569 {ALL_STATES, /* 5.4.3.4 */
3570 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
3571};
3572
3573#define DATASLLEN \
3574 (sizeof(datastatelist) / sizeof(struct datastate))
3575
Harald Welte4bc90a12008-12-27 16:32:52 +00003576static int gsm0408_rcv_cc(struct msgb *msg)
3577{
3578 struct gsm48_hdr *gh = msgb_l3(msg);
3579 u_int8_t msg_type = gh->msg_type & 0xbf;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003580 u_int8_t transaction_id = (gh->proto_discr & 0xf0) ^ 0x80; /* flip */
3581 struct gsm_lchan *lchan = msg->lchan;
3582 struct gsm_trans *trans = NULL, *transt;
3583 struct gsm_network *net = lchan->ts->trx->bts->network;
3584 int i, rc = 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00003585
Harald Welte4bfdfe72009-06-10 23:11:52 +08003586 if (msg_type & 0x80) {
3587 DEBUGP(DCC, "MSG 0x%2x not defined for PD error\n", msg_type);
3588 return -EINVAL;
Harald Welte4bc90a12008-12-27 16:32:52 +00003589 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003590
3591 /* Find transaction */
3592 llist_for_each_entry(transt, &net->trans_list, entry) {
3593 /* Transaction of our lchan? */
3594 if (transt->lchan == lchan
3595 && transt->transaction_id == transaction_id) {
3596 trans = transt;
3597 }
3598 }
3599
3600 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x sub %s) "
3601 "Received '%s' from MS in state %d (%s)\n",
3602 lchan->ts->trx->bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
3603 transaction_id, (lchan->subscr)?(lchan->subscr->extension):"-",
3604 cc_msg_names[msg_type], trans?(trans->state):0,
3605 cc_state_names[trans?(trans->state):0]);
3606
3607 /* Create transaction */
3608 if (!trans) {
3609 DEBUGP(DCC, "Unknown transaction ID %02x, "
3610 "creating new trans.\n", transaction_id);
3611 /* Create transaction */
3612 if (!(trans = calloc(1, sizeof(struct gsm_trans)))) {
3613 DEBUGP(DCC, "No memory for trans.\n");
3614 rc = gsm48_tx_simple(msg->lchan,
3615 GSM48_PDISC_CC | transaction_id,
3616 GSM48_MT_CC_RELEASE_COMPL);
3617 return -ENOMEM;
3618 }
3619 llist_add_tail(&trans->entry, &net->trans_list);
3620 /* Assign transaction */
3621 trans->callref = new_callref++;
3622 trans->network = net;
3623 trans->transaction_id = transaction_id;
3624 trans->lchan = lchan;
3625 use_lchan(lchan);
3626 if (lchan->subscr) {
3627 trans->subscr = lchan->subscr;
3628 subscr_get(trans->subscr);
3629 }
3630 }
3631
3632 /* find function for current state and message */
3633 for (i = 0; i < DATASLLEN; i++)
3634 if ((msg_type == datastatelist[i].type)
3635 && ((1 << trans->state) & datastatelist[i].states))
3636 break;
3637 if (i == DATASLLEN) {
3638 DEBUGP(DCC, "Message unhandled at this state.\n");
3639 return 0;
3640 }
3641
3642 rc = datastatelist[i].rout(trans, msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00003643
3644 return rc;
3645}
3646
Harald Welte52b1f982008-12-23 20:25:15 +00003647/* here we pass in a msgb from the RSL->RLL. We expect the l3 pointer to be set */
3648int gsm0408_rcvmsg(struct msgb *msg)
3649{
3650 struct gsm48_hdr *gh = msgb_l3(msg);
3651 u_int8_t pdisc = gh->proto_discr & 0x0f;
Harald Welte8470bf22008-12-25 23:28:35 +00003652 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00003653
3654 switch (pdisc) {
3655 case GSM48_PDISC_CC:
3656 rc = gsm0408_rcv_cc(msg);
3657 break;
3658 case GSM48_PDISC_MM:
3659 rc = gsm0408_rcv_mm(msg);
3660 break;
3661 case GSM48_PDISC_RR:
3662 rc = gsm0408_rcv_rr(msg);
3663 break;
Harald Weltebcae43f2008-12-27 21:45:37 +00003664 case GSM48_PDISC_SMS:
Daniel Willmann8b3390e2008-12-28 00:31:09 +00003665 rc = gsm0411_rcv_sms(msg);
Harald Weltebcae43f2008-12-27 21:45:37 +00003666 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003667 case GSM48_PDISC_MM_GPRS:
Harald Weltebcae43f2008-12-27 21:45:37 +00003668 case GSM48_PDISC_SM_GPRS:
Harald Welte52b1f982008-12-23 20:25:15 +00003669 fprintf(stderr, "Unimplemented GSM 04.08 discriminator 0x%02d\n",
3670 pdisc);
3671 break;
3672 default:
3673 fprintf(stderr, "Unknown GSM 04.08 discriminator 0x%02d\n",
3674 pdisc);
3675 break;
3676 }
3677
3678 return rc;
3679}
Harald Welte8470bf22008-12-25 23:28:35 +00003680
Harald Welte8470bf22008-12-25 23:28:35 +00003681/* Section 9.1.8 / Table 9.9 */
3682struct chreq {
3683 u_int8_t val;
3684 u_int8_t mask;
3685 enum chreq_type type;
3686};
3687
3688/* If SYSTEM INFORMATION TYPE 4 NECI bit == 1 */
3689static const struct chreq chreq_type_neci1[] = {
3690 { 0xa0, 0xe0, CHREQ_T_EMERG_CALL },
3691 { 0xc0, 0xe0, CHREQ_T_CALL_REEST_TCH_F },
3692 { 0x68, 0xfc, CHREQ_T_CALL_REEST_TCH_H },
3693 { 0x6c, 0xfc, CHREQ_T_CALL_REEST_TCH_H_DBL },
3694 { 0xe0, 0xe0, CHREQ_T_SDCCH },
3695 { 0x40, 0xf0, CHREQ_T_VOICE_CALL_TCH_H },
3696 { 0x50, 0xf0, CHREQ_T_DATA_CALL_TCH_H },
3697 { 0x00, 0xf0, CHREQ_T_LOCATION_UPD },
3698 { 0x10, 0xf0, CHREQ_T_SDCCH },
3699 { 0x80, 0xe0, CHREQ_T_PAG_R_ANY },
3700 { 0x20, 0xf0, CHREQ_T_PAG_R_TCH_F },
3701 { 0x30, 0xf0, CHREQ_T_PAG_R_TCH_FH },
3702};
3703
3704/* If SYSTEM INFORMATION TYPE 4 NECI bit == 0 */
3705static const struct chreq chreq_type_neci0[] = {
3706 { 0xa0, 0xe0, CHREQ_T_EMERG_CALL },
3707 { 0xc0, 0xe0, CHREQ_T_CALL_REEST_TCH_H },
3708 { 0xe0, 0xe0, CHREQ_T_TCH_F },
3709 { 0x50, 0xf0, CHREQ_T_DATA_CALL_TCH_H },
3710 { 0x00, 0xe0, CHREQ_T_LOCATION_UPD },
3711 { 0x80, 0xe0, CHREQ_T_PAG_R_ANY },
3712 { 0x20, 0xf0, CHREQ_T_PAG_R_TCH_F },
3713 { 0x30, 0xf0, CHREQ_T_PAG_R_TCH_FH },
3714};
3715
3716static const enum gsm_chan_t ctype_by_chreq[] = {
3717 [CHREQ_T_EMERG_CALL] = GSM_LCHAN_TCH_F,
3718 [CHREQ_T_CALL_REEST_TCH_F] = GSM_LCHAN_TCH_F,
3719 [CHREQ_T_CALL_REEST_TCH_H] = GSM_LCHAN_TCH_H,
3720 [CHREQ_T_CALL_REEST_TCH_H_DBL] = GSM_LCHAN_TCH_H,
3721 [CHREQ_T_SDCCH] = GSM_LCHAN_SDCCH,
3722 [CHREQ_T_TCH_F] = GSM_LCHAN_TCH_F,
3723 [CHREQ_T_VOICE_CALL_TCH_H] = GSM_LCHAN_TCH_H,
3724 [CHREQ_T_DATA_CALL_TCH_H] = GSM_LCHAN_TCH_H,
3725 [CHREQ_T_LOCATION_UPD] = GSM_LCHAN_SDCCH,
3726 [CHREQ_T_PAG_R_ANY] = GSM_LCHAN_SDCCH,
3727 [CHREQ_T_PAG_R_TCH_F] = GSM_LCHAN_TCH_F,
3728 [CHREQ_T_PAG_R_TCH_FH] = GSM_LCHAN_TCH_F,
3729};
3730
Harald Weltee14a57c2008-12-29 04:08:28 +00003731static const enum gsm_chreq_reason_t reason_by_chreq[] = {
3732 [CHREQ_T_EMERG_CALL] = GSM_CHREQ_REASON_EMERG,
3733 [CHREQ_T_CALL_REEST_TCH_F] = GSM_CHREQ_REASON_CALL,
3734 [CHREQ_T_CALL_REEST_TCH_H] = GSM_CHREQ_REASON_CALL,
3735 [CHREQ_T_CALL_REEST_TCH_H_DBL] = GSM_CHREQ_REASON_CALL,
3736 [CHREQ_T_SDCCH] = GSM_CHREQ_REASON_OTHER,
3737 [CHREQ_T_TCH_F] = GSM_CHREQ_REASON_OTHER,
3738 [CHREQ_T_VOICE_CALL_TCH_H] = GSM_CHREQ_REASON_OTHER,
3739 [CHREQ_T_DATA_CALL_TCH_H] = GSM_CHREQ_REASON_OTHER,
3740 [CHREQ_T_LOCATION_UPD] = GSM_CHREQ_REASON_LOCATION_UPD,
3741 [CHREQ_T_PAG_R_ANY] = GSM_CHREQ_REASON_PAG,
3742 [CHREQ_T_PAG_R_TCH_F] = GSM_CHREQ_REASON_PAG,
3743 [CHREQ_T_PAG_R_TCH_FH] = GSM_CHREQ_REASON_PAG,
3744};
3745
Harald Welte8470bf22008-12-25 23:28:35 +00003746enum gsm_chan_t get_ctype_by_chreq(struct gsm_bts *bts, u_int8_t ra)
3747{
3748 int i;
3749 /* FIXME: determine if we set NECI = 0 in the BTS SI4 */
3750
3751 for (i = 0; i < ARRAY_SIZE(chreq_type_neci0); i++) {
3752 const struct chreq *chr = &chreq_type_neci0[i];
3753 if ((ra & chr->mask) == chr->val)
3754 return ctype_by_chreq[chr->type];
3755 }
3756 fprintf(stderr, "Unknown CHANNEL REQUEST RQD 0x%02x\n", ra);
3757 return GSM_LCHAN_SDCCH;
3758}
Harald Weltee14a57c2008-12-29 04:08:28 +00003759
3760enum gsm_chreq_reason_t get_reason_by_chreq(struct gsm_bts *bts, u_int8_t ra)
3761{
3762 int i;
3763 /* FIXME: determine if we set NECI = 0 in the BTS SI4 */
3764
3765 for (i = 0; i < ARRAY_SIZE(chreq_type_neci0); i++) {
3766 const struct chreq *chr = &chreq_type_neci0[i];
3767 if ((ra & chr->mask) == chr->val)
3768 return reason_by_chreq[chr->type];
3769 }
3770 fprintf(stderr, "Unknown CHANNEL REQUEST REASON 0x%02x\n", ra);
3771 return GSM_CHREQ_REASON_OTHER;
3772}
Harald Welte4bfdfe72009-06-10 23:11:52 +08003773
3774/* dequeue messages to layer 4 */
3775int bsc_upqueue(struct gsm_network *net)
3776{
3777 struct gsm_mncc *mncc;
3778 struct msgb *msg;
3779 int work = 0;
3780
3781 if (net)
3782 while ((msg = msgb_dequeue(&net->upqueue))) {
3783 mncc = (struct gsm_mncc *)msg->data;
3784 if (net->mncc_recv)
3785 net->mncc_recv(net, mncc->msg_type, mncc);
3786 work = 1; /* work done */
3787 }
3788
3789 return work;
3790}