blob: 3aaa0f53c1b5742a8c44b8e24873c86ebdaf8ef3 [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
1508 put_lchan(msg->lchan);
1509
1510 return 0;
1511}
1512
Harald Welted2a7f5a2009-06-05 20:08:20 +00001513static int gsm48_rx_mm_status(struct msgb *msg)
1514{
1515 struct gsm48_hdr *gh = msgb_l3(msg);
1516
1517 DEBUGP(DMM, "MM STATUS (reject cause 0x%02x)\n", gh->data[0]);
1518
1519 return 0;
1520}
1521
Harald Weltebf5e8df2009-02-03 12:59:45 +00001522/* Receive a GSM 04.08 Mobility Management (MM) message */
Harald Welte52b1f982008-12-23 20:25:15 +00001523static int gsm0408_rcv_mm(struct msgb *msg)
1524{
1525 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001526 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001527
1528 switch (gh->msg_type & 0xbf) {
1529 case GSM48_MT_MM_LOC_UPD_REQUEST:
Holger Freyther429e7762008-12-30 13:28:30 +00001530 DEBUGP(DMM, "LOCATION UPDATING REQUEST\n");
Harald Welte231ad4f2008-12-27 11:15:38 +00001531 rc = mm_rx_loc_upd_req(msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001532 break;
1533 case GSM48_MT_MM_ID_RESP:
Harald Welte231ad4f2008-12-27 11:15:38 +00001534 rc = mm_rx_id_resp(msg);
1535 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001536 case GSM48_MT_MM_CM_SERV_REQ:
Harald Welte4b634542008-12-27 01:55:51 +00001537 rc = gsm48_rx_mm_serv_req(msg);
1538 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001539 case GSM48_MT_MM_STATUS:
Harald Welted2a7f5a2009-06-05 20:08:20 +00001540 rc = gsm48_rx_mm_status(msg);
Harald Welte231ad4f2008-12-27 11:15:38 +00001541 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001542 case GSM48_MT_MM_TMSI_REALL_COMPL:
Harald Welte69b2af22009-01-06 19:47:00 +00001543 DEBUGP(DMM, "TMSI Reallocation Completed. Subscriber: %s\n",
1544 msg->lchan->subscr ?
1545 msg->lchan->subscr->imsi :
1546 "unknown subscriber");
1547 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001548 case GSM48_MT_MM_IMSI_DETACH_IND:
Harald Welte2a139372009-02-22 21:14:55 +00001549 rc = gsm48_rx_mm_imsi_detach_ind(msg);
1550 break;
1551 case GSM48_MT_MM_CM_REEST_REQ:
1552 DEBUGP(DMM, "CM REESTABLISH REQUEST: Not implemented\n");
1553 break;
1554 case GSM48_MT_MM_AUTH_RESP:
1555 DEBUGP(DMM, "AUTHENTICATION RESPONSE: Not implemented\n");
Harald Welte52b1f982008-12-23 20:25:15 +00001556 break;
1557 default:
1558 fprintf(stderr, "Unknown GSM 04.08 MM msg type 0x%02x\n",
1559 gh->msg_type);
1560 break;
1561 }
1562
1563 return rc;
1564}
Harald Weltebf5e8df2009-02-03 12:59:45 +00001565
Harald Welte2d35ae62009-02-06 12:02:13 +00001566/* Receive a PAGING RESPONSE message from the MS */
1567static int gsm48_rr_rx_pag_resp(struct msgb *msg)
1568{
1569 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte61548982009-02-22 21:26:29 +00001570 u_int8_t *classmark2_lv = gh->data + 1;
1571 u_int8_t *mi_lv = gh->data + 2 + *classmark2_lv;
1572 u_int8_t mi_type = mi_lv[1] & GSM_MI_TYPE_MASK;
Harald Welte2d35ae62009-02-06 12:02:13 +00001573 char mi_string[MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +08001574 struct gsm_subscriber *subscr = NULL;
Harald Welte595ad7b2009-02-16 22:05:44 +00001575 struct paging_signal_data sig_data;
Harald Welte2d35ae62009-02-06 12:02:13 +00001576 int rc = 0;
1577
Harald Welte61548982009-02-22 21:26:29 +00001578 mi_to_string(mi_string, sizeof(mi_string), mi_lv+1, *mi_lv);
Harald Welte2d35ae62009-02-06 12:02:13 +00001579 DEBUGP(DRR, "PAGING RESPONSE: mi_type=0x%02x MI(%s)\n",
1580 mi_type, mi_string);
Harald Weltefe18d8f2009-02-22 21:14:24 +00001581 switch (mi_type) {
1582 case GSM_MI_TYPE_TMSI:
1583 subscr = subscr_get_by_tmsi(mi_string);
1584 break;
1585 case GSM_MI_TYPE_IMSI:
1586 subscr = subscr_get_by_imsi(mi_string);
1587 break;
1588 }
Harald Welte2d35ae62009-02-06 12:02:13 +00001589
1590 if (!subscr) {
1591 DEBUGP(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte09e38af2009-02-16 22:52:23 +00001592 /* FIXME: request id? close channel? */
Harald Welte2d35ae62009-02-06 12:02:13 +00001593 return -EINVAL;
1594 }
1595 DEBUGP(DRR, "<- Channel was requested by %s\n",
1596 subscr->name ? subscr->name : subscr->imsi);
Holger Freyther053e09d2009-02-14 22:51:06 +00001597
Harald Weltef7c43522009-06-09 20:24:21 +00001598 subscr->classmark2_len = *classmark2_lv;
1599 memcpy(subscr->classmark2, classmark2_lv+1, *classmark2_lv);
1600
Holger Freytherc21cfbc2009-06-02 02:54:57 +00001601 if (!msg->lchan->subscr) {
Holger Freyther2fa4cb52009-02-14 23:53:15 +00001602 msg->lchan->subscr = subscr;
Holger Freytherc21cfbc2009-06-02 02:54:57 +00001603 } else if (msg->lchan->subscr != subscr) {
Holger Freyther2fa4cb52009-02-14 23:53:15 +00001604 DEBUGP(DRR, "<- Channel already owned by someone else?\n");
1605 subscr_put(subscr);
Holger Freytherc21cfbc2009-06-02 02:54:57 +00001606 return -EINVAL;
1607 } else {
1608 DEBUGP(DRR, "<- Channel already owned by us\n");
1609 subscr_put(subscr);
1610 subscr = msg->lchan->subscr;
Holger Freyther2fa4cb52009-02-14 23:53:15 +00001611 }
1612
Harald Welte595ad7b2009-02-16 22:05:44 +00001613 sig_data.subscr = subscr;
1614 sig_data.bts = msg->lchan->ts->trx->bts;
1615 sig_data.lchan = msg->lchan;
1616
1617 dispatch_signal(SS_PAGING, S_PAGING_COMPLETED, &sig_data);
Harald Weltebe143102009-06-10 11:21:55 +08001618
1619 /* Stop paging on the bts we received the paging response */
Harald Welte7ccf7782009-02-17 01:43:01 +00001620 paging_request_stop(msg->trx->bts, subscr, msg->lchan);
Harald Welte2d35ae62009-02-06 12:02:13 +00001621
Harald Welte7584aea2009-02-11 11:44:12 +00001622 /* FIXME: somehow signal the completion of the PAGING to
1623 * the entity that requested the paging */
1624
Harald Welte2d35ae62009-02-06 12:02:13 +00001625 return rc;
1626}
1627
Harald Weltef7c43522009-06-09 20:24:21 +00001628static int gsm48_rx_rr_classmark(struct msgb *msg)
1629{
1630 struct gsm48_hdr *gh = msgb_l3(msg);
1631 struct gsm_subscriber *subscr = msg->lchan->subscr;
1632 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1633 u_int8_t cm2_len, cm3_len = 0;
1634 u_int8_t *cm2, *cm3 = NULL;
1635
1636 DEBUGP(DRR, "CLASSMARK CHANGE ");
1637
1638 /* classmark 2 */
1639 cm2_len = gh->data[0];
1640 cm2 = &gh->data[1];
1641 DEBUGPC(DRR, "CM2(len=%u) ", cm2_len);
1642
1643 if (payload_len > cm2_len + 1) {
1644 /* we must have a classmark3 */
1645 if (gh->data[cm2_len+1] != 0x20) {
1646 DEBUGPC(DRR, "ERR CM3 TAG\n");
1647 return -EINVAL;
1648 }
1649 if (cm2_len > 3) {
1650 DEBUGPC(DRR, "CM2 too long!\n");
1651 return -EINVAL;
1652 }
1653
1654 cm3_len = gh->data[cm2_len+2];
1655 cm3 = &gh->data[cm2_len+3];
1656 if (cm3_len > 14) {
1657 DEBUGPC(DRR, "CM3 len %u too long!\n", cm3_len);
1658 return -EINVAL;
1659 }
1660 DEBUGPC(DRR, "CM3(len=%u)\n", cm3_len);
1661 }
1662 if (subscr) {
1663 subscr->classmark2_len = cm2_len;
1664 memcpy(subscr->classmark2, cm2, cm2_len);
1665 if (cm3) {
1666 subscr->classmark3_len = cm3_len;
1667 memcpy(subscr->classmark3, cm3, cm3_len);
1668 }
1669 }
1670
1671 /* FIXME: store the classmark2/3 values with the equipment register */
1672
1673 return 0;
1674}
1675
Harald Weltecf5b3592009-05-01 18:28:42 +00001676static int gsm48_rx_rr_status(struct msgb *msg)
1677{
1678 struct gsm48_hdr *gh = msgb_l3(msg);
1679
1680 DEBUGP(DRR, "STATUS rr_cause = %s\n",
1681 rr_cause_name(gh->data[0]));
1682
1683 return 0;
1684}
1685
Harald Weltef7c43522009-06-09 20:24:21 +00001686static int gsm48_rx_rr_meas_rep(struct msgb *msg)
1687{
1688 struct gsm48_hdr *gh = msgb_l3(msg);
1689 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1690 static struct gsm_meas_rep meas_rep;
1691
1692 DEBUGP(DRR, "MEASUREMENT REPORT ");
1693 parse_meas_rep(&meas_rep, gh->data, payload_len);
1694 if (meas_rep.flags & MEAS_REP_F_DTX)
1695 DEBUGPC(DRR, "DTX ");
1696 if (meas_rep.flags & MEAS_REP_F_BA1)
1697 DEBUGPC(DRR, "BA1 ");
1698 if (!(meas_rep.flags & MEAS_REP_F_VALID))
1699 DEBUGPC(DRR, "NOT VALID ");
1700 else
1701 DEBUGPC(DRR, "FULL(lev=%u, qual=%u) SUB(lev=%u, qual=%u) ",
1702 meas_rep.rxlev_full, meas_rep.rxqual_full, meas_rep.rxlev_sub,
1703 meas_rep.rxqual_sub);
1704
1705 DEBUGPC(DRR, "NUM_NEIGH=%u\n", meas_rep.num_cell);
1706
1707 /* FIXME: put the results somwhere */
1708
1709 return 0;
1710}
1711
Harald Weltebf5e8df2009-02-03 12:59:45 +00001712/* Receive a GSM 04.08 Radio Resource (RR) message */
Harald Welte52b1f982008-12-23 20:25:15 +00001713static int gsm0408_rcv_rr(struct msgb *msg)
1714{
1715 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001716 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001717
1718 switch (gh->msg_type) {
1719 case GSM48_MT_RR_CLSM_CHG:
Harald Weltef7c43522009-06-09 20:24:21 +00001720 rc = gsm48_rx_rr_classmark(msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001721 break;
Harald Weltefc977a82008-12-27 10:19:37 +00001722 case GSM48_MT_RR_GPRS_SUSP_REQ:
1723 DEBUGP(DRR, "GRPS SUSPEND REQUEST\n");
1724 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001725 case GSM48_MT_RR_PAG_RESP:
Harald Welte2d35ae62009-02-06 12:02:13 +00001726 rc = gsm48_rr_rx_pag_resp(msg);
1727 break;
Harald Welte7ccf7782009-02-17 01:43:01 +00001728 case GSM48_MT_RR_CHAN_MODE_MODIF_ACK:
1729 DEBUGP(DRR, "CHANNEL MODE MODIFY ACK\n");
Harald Welte2c38aa82009-02-18 03:44:24 +00001730 rc = rsl_chan_mode_modify_req(msg->lchan);
Harald Welte7ccf7782009-02-17 01:43:01 +00001731 break;
Harald Weltecf5b3592009-05-01 18:28:42 +00001732 case GSM48_MT_RR_STATUS:
1733 rc = gsm48_rx_rr_status(msg);
1734 break;
Harald Weltef7c43522009-06-09 20:24:21 +00001735 case GSM48_MT_RR_MEAS_REP:
1736 rc = gsm48_rx_rr_meas_rep(msg);
1737 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001738 default:
Harald Welte2d35ae62009-02-06 12:02:13 +00001739 fprintf(stderr, "Unimplemented GSM 04.08 RR msg type 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +00001740 gh->msg_type);
1741 break;
1742 }
1743
Harald Welte2d35ae62009-02-06 12:02:13 +00001744 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +00001745}
1746
Holger Freythere64a7a32009-02-06 21:55:37 +00001747/* 7.1.7 and 9.1.7 Channel release*/
1748int gsm48_send_rr_release(struct gsm_lchan *lchan)
1749{
1750 struct msgb *msg = gsm48_msgb_alloc();
1751 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1752 u_int8_t *cause;
1753
1754 msg->lchan = lchan;
1755 gh->proto_discr = GSM48_PDISC_RR;
1756 gh->msg_type = GSM48_MT_RR_CHAN_REL;
1757
1758 cause = msgb_put(msg, 1);
1759 cause[0] = GSM48_RR_CAUSE_NORMAL;
1760
1761 DEBUGP(DRR, "Sending Channel Release: Chan: Number: %d Type: %d\n",
1762 lchan->nr, lchan->type);
1763
1764 return gsm48_sendmsg(msg);
1765}
1766
Harald Welte4bc90a12008-12-27 16:32:52 +00001767/* Call Control */
1768
Harald Welte7584aea2009-02-11 11:44:12 +00001769/* The entire call control code is written in accordance with Figure 7.10c
1770 * for 'very early assignment', i.e. we allocate a TCH/F during IMMEDIATE
1771 * ASSIGN, then first use that TCH/F for signalling and later MODE MODIFY
1772 * it for voice */
1773
Harald Welte4bfdfe72009-06-10 23:11:52 +08001774static void new_cc_state(struct gsm_trans *trans, int state)
1775{
1776 if (state > 31 || state < 0)
1777 return;
1778
1779 DEBUGP(DCC, "new state %s -> %s\n",
1780 cc_state_names[trans->state], cc_state_names[state]);
1781
1782 trans->state = state;
1783}
1784
1785static int gsm48_cc_tx_status(struct gsm_trans *trans, void *arg)
Harald Welte4bc90a12008-12-27 16:32:52 +00001786{
1787 struct msgb *msg = gsm48_msgb_alloc();
1788 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1789 u_int8_t *cause, *call_state;
1790
Harald Welte4bfdfe72009-06-10 23:11:52 +08001791 gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id;
1792 msg->lchan = trans->lchan;
Harald Welte4bc90a12008-12-27 16:32:52 +00001793 gh->msg_type = GSM48_MT_CC_STATUS;
1794
1795 cause = msgb_put(msg, 3);
1796 cause[0] = 2;
1797 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
1798 cause[2] = 0x80 | 30; /* response to status inquiry */
1799
1800 call_state = msgb_put(msg, 1);
1801 call_state[0] = 0xc0 | 0x00;
1802
Harald Welte65e74cc2008-12-29 01:55:35 +00001803 return gsm48_sendmsg(msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00001804}
1805
Harald Welte6f4b7532008-12-29 00:39:37 +00001806static int gsm48_tx_simple(struct gsm_lchan *lchan,
1807 u_int8_t pdisc, u_int8_t msg_type)
Harald Welte4bc90a12008-12-27 16:32:52 +00001808{
1809 struct msgb *msg = gsm48_msgb_alloc();
1810 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1811
1812 msg->lchan = lchan;
1813
Harald Welte6f4b7532008-12-29 00:39:37 +00001814 gh->proto_discr = pdisc;
Harald Welte4bc90a12008-12-27 16:32:52 +00001815 gh->msg_type = msg_type;
1816
Harald Welte65e74cc2008-12-29 01:55:35 +00001817 return gsm48_sendmsg(msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00001818}
1819
Harald Welte4bfdfe72009-06-10 23:11:52 +08001820static void gsm48_stop_cc_timer(struct gsm_trans *trans)
1821{
1822 if (bsc_timer_pending(&trans->cc_timer)) {
1823 DEBUGP(DCC, "stopping pending timer T%x\n", trans->Tcurrent);
1824 bsc_del_timer(&trans->cc_timer);
1825 trans->Tcurrent = 0;
1826 }
1827}
1828
1829static int mncc_recvmsg(struct gsm_network *net, struct gsm_trans *trans,
1830 int msg_type, struct gsm_mncc *mncc)
1831{
1832 struct msgb *msg;
1833
1834 if (trans)
1835 if (trans->lchan)
1836 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x sub %s) "
1837 "Sending '%s' to MNCC.\n",
1838 trans->lchan->ts->trx->bts->nr,
1839 trans->lchan->ts->trx->nr,
1840 trans->lchan->ts->nr, trans->transaction_id,
1841 (trans->subscr)?(trans->subscr->extension):"-",
1842 get_mncc_name(msg_type));
1843 else
1844 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
1845 "Sending '%s' to MNCC.\n",
1846 (trans->subscr)?(trans->subscr->extension):"-",
1847 get_mncc_name(msg_type));
1848 else
1849 DEBUGP(DCC, "(bts - trx - ts - ti -- sub -) "
1850 "Sending '%s' to MNCC.\n", get_mncc_name(msg_type));
1851
1852 mncc->msg_type = msg_type;
1853
1854 msg = msgb_alloc(sizeof(struct gsm_mncc));
1855 if (!msg)
1856 return -ENOMEM;
1857 memcpy(msg->data, mncc, sizeof(struct gsm_mncc));
1858 msgb_enqueue(&net->upqueue, msg);
1859
1860 return 0;
1861}
1862
1863int mncc_release_ind(struct gsm_network *net, struct gsm_trans *trans,
1864 u_int32_t callref, int location, int value)
1865{
1866 struct gsm_mncc rel;
1867
1868 memset(&rel, 0, sizeof(&rel));
1869 rel.callref = callref;
1870 mncc_set_cause(&rel, 1, 1); /* Unknown subscriber */
1871 return mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
1872}
1873
1874void free_trans(struct gsm_trans *trans)
1875{
1876 struct gsm_bts *bts;
1877
1878 gsm48_stop_cc_timer(trans);
1879
1880 /* send release to L4, if callref still exists */
1881 if (trans->callref) {
1882 /* Ressource unavailable */
1883 mncc_release_ind(trans->network, trans, trans->callref, 1, 47);
1884 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,
1964 1, 27);
1965 transt->callref = 0;
1966 free_trans(transt);
1967 break;
1968 }
1969 }
Harald Welte09e38af2009-02-16 22:52:23 +00001970 return 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00001971}
Harald Welte7584aea2009-02-11 11:44:12 +00001972
Harald Welte49f48b82009-02-17 15:29:33 +00001973/* map two ipaccess RTP streams onto each other */
Harald Welte11fa29c2009-02-19 17:24:39 +00001974static int tch_map(struct gsm_lchan *lchan, struct gsm_lchan *remote_lchan)
Harald Welte49f48b82009-02-17 15:29:33 +00001975{
Harald Welte11fa29c2009-02-19 17:24:39 +00001976 struct gsm_bts *bts = lchan->ts->trx->bts;
1977 struct gsm_bts *remote_bts = remote_lchan->ts->trx->bts;
Harald Welte49f48b82009-02-17 15:29:33 +00001978 struct gsm_bts_trx_ts *ts;
1979
Harald Welte11fa29c2009-02-19 17:24:39 +00001980 DEBUGP(DCC, "Setting up TCH map between (bts=%u,trx=%u,ts=%u) and (bts=%u,trx=%u,ts=%u)\n",
1981 bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
1982 remote_bts->nr, remote_lchan->ts->trx->nr, remote_lchan->ts->nr);
1983
1984 if (bts->type != remote_bts->type) {
1985 DEBUGP(DCC, "Cannot switch calls between different BTS types yet\n");
1986 return -EINVAL;
1987 }
Harald Welte49f48b82009-02-17 15:29:33 +00001988
Harald Welte11fa29c2009-02-19 17:24:39 +00001989 switch (bts->type) {
1990 case GSM_BTS_TYPE_NANOBTS_900:
1991 case GSM_BTS_TYPE_NANOBTS_1800:
1992 ts = remote_lchan->ts;
1993 rsl_ipacc_connect(lchan, ts->abis_ip.bound_ip, ts->abis_ip.bound_port,
1994 lchan->ts->abis_ip.attr_f8, ts->abis_ip.attr_fc);
1995
1996 ts = lchan->ts;
1997 rsl_ipacc_connect(remote_lchan, ts->abis_ip.bound_ip, ts->abis_ip.bound_port,
1998 remote_lchan->ts->abis_ip.attr_f8, ts->abis_ip.attr_fc);
1999 break;
2000 case GSM_BTS_TYPE_BS11:
2001 trau_mux_map_lchan(lchan, remote_lchan);
2002 break;
2003 default:
2004 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
2005 break;
2006 }
Harald Welte49f48b82009-02-17 15:29:33 +00002007
2008 return 0;
2009}
2010
Harald Welte4bfdfe72009-06-10 23:11:52 +08002011static struct gsm_trans *get_trans_ref(struct gsm_network *net, u_int32_t callref)
Harald Welte7ccf7782009-02-17 01:43:01 +00002012{
Harald Welte4bfdfe72009-06-10 23:11:52 +08002013 struct gsm_trans *trans;
2014 llist_for_each_entry(trans, &net->trans_list, entry) {
2015 if (trans->callref == callref)
2016 return trans;
2017 }
2018 return NULL;
Harald Welte7ccf7782009-02-17 01:43:01 +00002019}
2020
Harald Welte4bfdfe72009-06-10 23:11:52 +08002021/* bridge channels of two transactions */
2022static int tch_bridge(struct gsm_network *net, u_int32_t *refs)
Harald Welte7ccf7782009-02-17 01:43:01 +00002023{
Harald Welte4bfdfe72009-06-10 23:11:52 +08002024 struct gsm_trans *trans1 = get_trans_ref(net, refs[0]);
2025 struct gsm_trans *trans2 = get_trans_ref(net, refs[1]);
Harald Welte7ccf7782009-02-17 01:43:01 +00002026
Harald Welte4bfdfe72009-06-10 23:11:52 +08002027 if (!trans1 || !trans2)
Harald Welte7ccf7782009-02-17 01:43:01 +00002028 return -EIO;
2029
Harald Welte4bfdfe72009-06-10 23:11:52 +08002030 if (!trans1->lchan || !trans2->lchan)
2031 return -EIO;
2032
2033 /* through-connect channel */
2034 return tch_map(trans1->lchan, trans2->lchan);
Harald Welte7ccf7782009-02-17 01:43:01 +00002035}
2036
Harald Welte4bfdfe72009-06-10 23:11:52 +08002037/* enable receive of channels to upqueue */
2038static int tch_recv(struct gsm_network *net, struct gsm_mncc *data, int enable)
2039{
2040 struct gsm_trans *trans;
Harald Welte7ccf7782009-02-17 01:43:01 +00002041
Harald Welte4bfdfe72009-06-10 23:11:52 +08002042 /* Find callref */
2043 trans = get_trans_ref(net, data->callref);
2044 if (!trans)
2045 return -EIO;
2046 if (!trans->lchan)
2047 return 0;
2048
2049 // todo IPACCESS
2050 if (enable)
2051 return trau_recv_lchan(trans->lchan, data->callref);
2052 return trau_mux_unmap(NULL, data->callref);
2053}
2054
2055/* send a frame to channel */
2056static int tch_frame(struct gsm_network *net, struct gsm_trau_frame *frame)
2057{
2058 struct gsm_trans *trans;
2059
2060 /* Find callref */
2061 trans = get_trans_ref(net, frame->callref);
2062 if (!trans)
2063 return -EIO;
2064 if (!trans->lchan)
2065 return 0;
2066 if (trans->lchan->type != GSM_LCHAN_TCH_F &&
2067 trans->lchan->type != GSM_LCHAN_TCH_H)
2068 return 0;
2069
2070 // todo IPACCESS
2071 return trau_send_lchan(trans->lchan,
2072 (struct decoded_trau_frame *)frame->data);
2073}
2074
2075
2076static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
2077{
2078 DEBUGP(DCC, "-> STATUS ENQ\n");
2079 return gsm48_cc_tx_status(trans, msg);
2080}
2081
2082static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg);
2083static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg);
2084
2085static void gsm48_cc_timeout(void *arg)
2086{
2087 struct gsm_trans *trans = arg;
2088 int disconnect = 0, release = 0;
2089 int mo_cause = 102, mo_location = 0;
2090 int l4_cause = 31, l4_location = 1;
2091 struct gsm_mncc mo_rel, l4_rel;
2092
2093 memset(&mo_rel, 0, sizeof(struct gsm_mncc));
2094 mo_rel.callref = trans->callref;
2095 memset(&l4_rel, 0, sizeof(struct gsm_mncc));
2096 l4_rel.callref = trans->callref;
2097
2098 switch(trans->Tcurrent) {
2099 case 0x303:
2100 release = 1;
2101 l4_cause = 18;
2102 break;
2103 case 0x310:
2104 disconnect = 1;
2105 l4_cause = 18;
2106 break;
2107 case 0x313:
2108 disconnect = 1;
2109 /* unknown, did not find it in the specs */
2110 break;
2111 case 0x301:
2112 disconnect = 1;
2113 l4_cause = 19;
2114 break;
2115 case 0x308:
2116 if (!trans->T308_second) {
2117 /* restart T308 a second time */
2118 gsm48_cc_tx_release(trans, &trans->cc_msg);
2119 trans->T308_second = 1;
2120 break; /* stay in release state */
2121 }
2122 free_trans(trans);
2123 return;
2124// release = 1;
2125// l4_cause = 14;
2126// break;
2127 case 0x306:
2128 release = 1;
2129 mo_cause = trans->cc_msg.cause.value;
2130 mo_location = trans->cc_msg.cause.location;
2131 break;
2132 case 0x323:
2133 disconnect = 1;
2134 break;
2135 default:
2136 release = 1;
2137 }
2138
2139 if (release && trans->callref) {
2140 /* process release towards layer 4 */
2141 mncc_release_ind(trans->network, trans, trans->callref,
2142 l4_location, l4_cause);
2143 trans->callref = 0;
2144 }
2145
2146 if (disconnect && trans->callref) {
2147 /* process disconnect towards layer 4 */
2148 mncc_set_cause(&l4_rel, l4_location, l4_cause);
2149 mncc_recvmsg(trans->network, trans, MNCC_DISC_IND, &l4_rel);
2150 }
2151
2152 /* process disconnect towards mobile station */
2153 if (disconnect || release) {
2154 mncc_set_cause(&mo_rel, mo_location, mo_cause);
2155 mo_rel.cause.diag[0] = ((trans->Tcurrent & 0xf00) >> 8) + '0';
2156 mo_rel.cause.diag[1] = ((trans->Tcurrent & 0x0f0) >> 4) + '0';
2157 mo_rel.cause.diag[2] = (trans->Tcurrent & 0x00f) + '0';
2158 mo_rel.cause.diag_len = 3;
2159
2160 if (disconnect)
2161 gsm48_cc_tx_disconnect(trans, &mo_rel);
2162 if (release)
2163 gsm48_cc_tx_release(trans, &mo_rel);
2164 }
2165
2166}
2167
2168static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
2169 int sec, int micro)
2170{
2171 DEBUGP(DCC, "starting timer T%x with %d seconds\n", current, sec);
2172 trans->cc_timer.cb = gsm48_cc_timeout;
2173 trans->cc_timer.data = trans;
2174 bsc_schedule_timer(&trans->cc_timer, sec, micro);
2175 trans->Tcurrent = current;
2176}
2177
2178static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
2179{
2180 struct gsm48_hdr *gh = msgb_l3(msg);
2181 u_int8_t msg_type = gh->msg_type & 0xbf;
2182 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2183 struct tlv_parsed tp;
2184 struct gsm_mncc setup;
2185
2186 memset(&setup, 0, sizeof(struct gsm_mncc));
2187 setup.callref = trans->callref;
2188 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2189 /* emergency setup is identified by msg_type */
2190 if (msg_type == GSM48_MT_CC_EMERG_SETUP)
2191 setup.emergency = 1;
2192
2193 /* use subscriber as calling party number */
2194 if (trans->subscr) {
2195 setup.fields |= MNCC_F_CALLING;
2196 strncpy(setup.calling.number, trans->subscr->extension,
2197 sizeof(setup.calling.number)-1);
2198 }
2199 /* bearer capability */
2200 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2201 setup.fields |= MNCC_F_BEARER_CAP;
2202 decode_bearer_cap(&setup.bearer_cap,
2203 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2204 }
2205 /* facility */
2206 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2207 setup.fields |= MNCC_F_FACILITY;
2208 decode_facility(&setup.facility,
2209 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2210 }
2211 /* called party bcd number */
2212 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
2213 setup.fields |= MNCC_F_CALLED;
2214 decode_called(&setup.called,
2215 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
2216 }
2217 /* user-user */
2218 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2219 setup.fields |= MNCC_F_USERUSER;
2220 decode_useruser(&setup.useruser,
2221 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2222 }
2223 /* ss-version */
2224 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2225 setup.fields |= MNCC_F_SSVERSION;
2226 decode_ssversion(&setup.ssversion,
2227 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2228 }
2229 /* CLIR suppression */
2230 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
2231 setup.clir.sup = 1;
2232 /* CLIR invocation */
2233 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
2234 setup.clir.inv = 1;
2235 /* cc cap */
2236 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
2237 setup.fields |= MNCC_F_CCCAP;
2238 decode_cccap(&setup.cccap,
2239 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
2240 }
2241
2242 if (is_ipaccess_bts(msg->trx->bts))
2243 rsl_ipacc_bind(msg->lchan);
2244
2245 new_cc_state(trans, GSM_CSTATE_INITIATED);
2246
2247 /* indicate setup to MNCC */
2248 mncc_recvmsg(trans->network, trans, MNCC_SETUP_IND, &setup);
2249
2250 return 0;
2251}
2252
2253static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
Harald Welte65e74cc2008-12-29 01:55:35 +00002254{
2255 struct msgb *msg = gsm48_msgb_alloc();
2256 struct gsm48_hdr *gh;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002257 struct gsm_mncc *setup = arg;
2258 struct gsm_trans *transt;
2259 u_int16_t trans_id_mask = 0;
2260 int rc, i;
Harald Welte65e74cc2008-12-29 01:55:35 +00002261
Harald Welte7ccf7782009-02-17 01:43:01 +00002262 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte65e74cc2008-12-29 01:55:35 +00002263
Harald Welte4bfdfe72009-06-10 23:11:52 +08002264 /* transaction id must not be assigned */
2265 if (trans->transaction_id != 0xff) { /* unasssigned */
2266 DEBUGP(DCC, "TX Setup with assigned transaction. "
2267 "This is not allowed!\n");
2268 /* Temporarily out of order */
2269 rc = mncc_release_ind(trans->network, trans, trans->callref,
2270 1, 47);
2271 trans->callref = 0;
2272 free_trans(trans);
2273 return rc;
2274 }
2275
2276 /* Get free transaction_id */
2277 llist_for_each_entry(transt, &trans->network->trans_list, entry) {
2278 /* Transaction of our lchan? */
2279 if (transt->lchan == trans->lchan &&
2280 transt->transaction_id != 0xff)
2281 trans_id_mask |= (1 << (transt->transaction_id >> 4));
2282 }
2283 /* Assign free transaction ID */
2284 if ((trans_id_mask & 0x007f) == 0x7f) {
2285 /* no free transaction ID */
2286 rc = mncc_release_ind(trans->network, trans, trans->callref,
2287 1, 47);
2288 trans->callref = 0;
2289 free_trans(trans);
2290 return rc;
2291 }
2292 for (i = 0; i < 7; i++) {
2293 if ((trans_id_mask & (1 << i)) == 0) {
2294 trans->transaction_id = i << 4; /* flag = 0 */
2295 break;
2296 }
2297 }
Harald Welte49f48b82009-02-17 15:29:33 +00002298
Harald Welte4bfdfe72009-06-10 23:11:52 +08002299 gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id;
2300 msg->lchan = trans->lchan;
Harald Welte65e74cc2008-12-29 01:55:35 +00002301 gh->msg_type = GSM48_MT_CC_SETUP;
Harald Welte09e38af2009-02-16 22:52:23 +00002302
Harald Welte4bfdfe72009-06-10 23:11:52 +08002303 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
Harald Welte65e74cc2008-12-29 01:55:35 +00002304
Harald Welte4bfdfe72009-06-10 23:11:52 +08002305 /* bearer capability */
2306 if (setup->fields & MNCC_F_BEARER_CAP)
2307 encode_bearer_cap(msg, 0, &setup->bearer_cap);
2308 /* facility */
2309 if (setup->fields & MNCC_F_FACILITY)
2310 encode_facility(msg, 0, &setup->facility);
2311 /* progress */
2312 if (setup->fields & MNCC_F_PROGRESS)
2313 encode_progress(msg, 0, &setup->progress);
2314 /* calling party BCD number */
2315 if (setup->fields & MNCC_F_CALLING)
2316 encode_calling(msg, &setup->calling);
2317 /* called party BCD number */
2318 if (setup->fields & MNCC_F_CALLED)
2319 encode_called(msg, &setup->called);
2320 /* user-user */
2321 if (setup->fields & MNCC_F_USERUSER)
2322 encode_useruser(msg, 0, &setup->useruser);
2323 /* redirecting party BCD number */
2324 if (setup->fields & MNCC_F_REDIRECTING)
2325 encode_redirecting(msg, &setup->redirecting);
2326 /* signal */
2327 if (setup->fields & MNCC_F_SIGNAL)
2328 encode_signal(msg, setup->signal);
2329
2330 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
Harald Welte65e74cc2008-12-29 01:55:35 +00002331
2332 return gsm48_sendmsg(msg);
2333}
2334
Harald Welte4bfdfe72009-06-10 23:11:52 +08002335static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
2336{
2337 struct gsm48_hdr *gh = msgb_l3(msg);
2338 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2339 struct tlv_parsed tp;
2340 struct gsm_mncc call_conf;
2341
2342 gsm48_stop_cc_timer(trans);
2343 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
2344
2345 memset(&call_conf, 0, sizeof(struct gsm_mncc));
2346 call_conf.callref = trans->callref;
2347 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2348#if 0
2349 /* repeat */
2350 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
2351 call_conf.repeat = 1;
2352 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
2353 call_conf.repeat = 2;
2354#endif
2355 /* bearer capability */
2356 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2357 call_conf.fields |= MNCC_F_BEARER_CAP;
2358 decode_bearer_cap(&call_conf.bearer_cap,
2359 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2360 }
2361 /* cause */
2362 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2363 call_conf.fields |= MNCC_F_CAUSE;
2364 decode_cause(&call_conf.cause,
2365 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2366 }
2367 /* cc cap */
2368 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
2369 call_conf.fields |= MNCC_F_CCCAP;
2370 decode_cccap(&call_conf.cccap,
2371 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
2372 }
2373
2374 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
2375
2376 return mncc_recvmsg(trans->network, trans, MNCC_CALL_CONF_IND, &call_conf);
2377}
2378
2379static int gsm48_cc_tx_call_proc(struct gsm_trans *trans, void *arg)
2380{
2381 struct gsm_mncc *proceeding = arg;
2382 struct msgb *msg = gsm48_msgb_alloc();
2383 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2384
2385 gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id;
2386 msg->lchan = trans->lchan;
2387 gh->msg_type = GSM48_MT_CC_CALL_PROC;
2388
2389 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
2390
2391 /* bearer capability */
2392 if (proceeding->fields & MNCC_F_BEARER_CAP)
2393 encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
2394 /* facility */
2395 if (proceeding->fields & MNCC_F_FACILITY)
2396 encode_facility(msg, 0, &proceeding->facility);
2397 /* progress */
2398 if (proceeding->fields & MNCC_F_PROGRESS)
2399 encode_progress(msg, 0, &proceeding->progress);
2400
2401 return gsm48_sendmsg(msg);
2402}
2403
2404static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
2405{
2406 struct gsm48_hdr *gh = msgb_l3(msg);
2407 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2408 struct tlv_parsed tp;
2409 struct gsm_mncc alerting;
2410
2411 gsm48_stop_cc_timer(trans);
2412 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
2413
2414 memset(&alerting, 0, sizeof(struct gsm_mncc));
2415 alerting.callref = trans->callref;
2416 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2417 /* facility */
2418 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2419 alerting.fields |= MNCC_F_FACILITY;
2420 decode_facility(&alerting.facility,
2421 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2422 }
2423
2424 /* progress */
2425 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
2426 alerting.fields |= MNCC_F_PROGRESS;
2427 decode_progress(&alerting.progress,
2428 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
2429 }
2430 /* ss-version */
2431 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2432 alerting.fields |= MNCC_F_SSVERSION;
2433 decode_ssversion(&alerting.ssversion,
2434 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2435 }
2436
2437 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
2438
2439 return mncc_recvmsg(trans->network, trans, MNCC_ALERT_IND, &alerting);
2440}
2441
2442static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
2443{
2444 struct gsm_mncc *alerting = arg;
2445 struct msgb *msg = gsm48_msgb_alloc();
2446 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2447
2448 gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id;
2449 msg->lchan = trans->lchan;
2450 gh->msg_type = GSM48_MT_CC_ALERTING;
2451
2452 /* facility */
2453 if (alerting->fields & MNCC_F_FACILITY)
2454 encode_facility(msg, 0, &alerting->facility);
2455 /* progress */
2456 if (alerting->fields & MNCC_F_PROGRESS)
2457 encode_progress(msg, 0, &alerting->progress);
2458 /* user-user */
2459 if (alerting->fields & MNCC_F_USERUSER)
2460 encode_useruser(msg, 0, &alerting->useruser);
2461
2462 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
2463
2464 return gsm48_sendmsg(msg);
2465}
2466
2467static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
2468{
2469 struct gsm_mncc *progress = arg;
2470 struct msgb *msg = gsm48_msgb_alloc();
2471 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2472
2473 gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id;
2474 msg->lchan = trans->lchan;
2475 gh->msg_type = GSM48_MT_CC_PROGRESS;
2476
2477 /* progress */
2478 encode_progress(msg, 1, &progress->progress);
2479 /* user-user */
2480 if (progress->fields & MNCC_F_USERUSER)
2481 encode_useruser(msg, 0, &progress->useruser);
2482
2483 return gsm48_sendmsg(msg);
2484}
2485
2486static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
2487{
2488 struct gsm_mncc *connect = arg;
2489 struct msgb *msg = gsm48_msgb_alloc();
2490 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2491
2492 gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id;
2493 msg->lchan = trans->lchan;
2494 gh->msg_type = GSM48_MT_CC_CONNECT;
2495
2496 gsm48_stop_cc_timer(trans);
2497 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
2498
2499 /* facility */
2500 if (connect->fields & MNCC_F_FACILITY)
2501 encode_facility(msg, 0, &connect->facility);
2502 /* progress */
2503 if (connect->fields & MNCC_F_PROGRESS)
2504 encode_progress(msg, 0, &connect->progress);
2505 /* connected number */
2506 if (connect->fields & MNCC_F_CONNECTED)
2507 encode_connected(msg, &connect->connected);
2508 /* user-user */
2509 if (connect->fields & MNCC_F_USERUSER)
2510 encode_useruser(msg, 0, &connect->useruser);
2511
2512 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
2513
2514 return gsm48_sendmsg(msg);
2515}
2516
2517static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
2518{
2519 struct gsm48_hdr *gh = msgb_l3(msg);
2520 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2521 struct tlv_parsed tp;
2522 struct gsm_mncc connect;
2523
2524 gsm48_stop_cc_timer(trans);
2525
2526 memset(&connect, 0, sizeof(struct gsm_mncc));
2527 connect.callref = trans->callref;
2528 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2529 /* use subscriber as connected party number */
2530 if (trans->subscr) {
2531 connect.fields |= MNCC_F_CONNECTED;
2532 strncpy(connect.connected.number, trans->subscr->extension,
2533 sizeof(connect.connected.number)-1);
2534 }
2535 /* facility */
2536 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2537 connect.fields |= MNCC_F_FACILITY;
2538 decode_facility(&connect.facility,
2539 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2540 }
2541 /* user-user */
2542 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2543 connect.fields |= MNCC_F_USERUSER;
2544 decode_useruser(&connect.useruser,
2545 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2546 }
2547 /* ss-version */
2548 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2549 connect.fields |= MNCC_F_SSVERSION;
2550 decode_ssversion(&connect.ssversion,
2551 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2552 }
2553
2554 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
2555
2556 return mncc_recvmsg(trans->network, trans, MNCC_SETUP_CNF, &connect);
2557}
2558
2559
2560static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
2561{
2562 struct gsm_mncc connect_ack;
2563
2564 gsm48_stop_cc_timer(trans);
2565
2566 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2567
2568 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
2569 connect_ack.callref = trans->callref;
2570 return mncc_recvmsg(trans->network, trans, MNCC_SETUP_COMPL_IND,
2571 &connect_ack);
2572}
2573
2574static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
2575{
2576 struct msgb *msg = gsm48_msgb_alloc();
2577 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2578
2579 gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id;
2580 msg->lchan = trans->lchan;
2581 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
2582
2583 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2584
2585 return gsm48_sendmsg(msg);
2586}
2587
2588static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
2589{
2590 struct gsm48_hdr *gh = msgb_l3(msg);
2591 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2592 struct tlv_parsed tp;
2593 struct gsm_mncc disc;
2594
2595 gsm48_stop_cc_timer(trans);
2596
2597 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
2598
2599 memset(&disc, 0, sizeof(struct gsm_mncc));
2600 disc.callref = trans->callref;
2601 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
2602 /* cause */
2603 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2604 disc.fields |= MNCC_F_CAUSE;
2605 decode_cause(&disc.cause,
2606 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2607 }
2608 /* facility */
2609 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2610 disc.fields |= MNCC_F_FACILITY;
2611 decode_facility(&disc.facility,
2612 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2613 }
2614 /* user-user */
2615 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2616 disc.fields |= MNCC_F_USERUSER;
2617 decode_useruser(&disc.useruser,
2618 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2619 }
2620 /* ss-version */
2621 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2622 disc.fields |= MNCC_F_SSVERSION;
2623 decode_ssversion(&disc.ssversion,
2624 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2625 }
2626
2627 return mncc_recvmsg(trans->network, trans, MNCC_DISC_IND, &disc);
2628
2629}
2630
2631static struct gsm_mncc_cause default_cause = { 1, 0, 0, 0, 31, 0, { 0 } };
2632
2633static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
2634{
2635 struct gsm_mncc *disc = arg;
2636 struct msgb *msg = gsm48_msgb_alloc();
2637 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2638
2639 gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id;
2640 msg->lchan = trans->lchan;
2641 gh->msg_type = GSM48_MT_CC_DISCONNECT;
2642
2643 gsm48_stop_cc_timer(trans);
2644 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
2645
2646 /* cause */
2647 if (disc->fields & MNCC_F_CAUSE)
2648 encode_cause(msg, 1, &disc->cause);
2649 else
2650 encode_cause(msg, 1, &default_cause);
2651
2652 /* facility */
2653 if (disc->fields & MNCC_F_FACILITY)
2654 encode_facility(msg, 0, &disc->facility);
2655 /* progress */
2656 if (disc->fields & MNCC_F_PROGRESS)
2657 encode_progress(msg, 0, &disc->progress);
2658 /* user-user */
2659 if (disc->fields & MNCC_F_USERUSER)
2660 encode_useruser(msg, 0, &disc->useruser);
2661
2662 /* store disconnect cause for T306 expiry */
2663 memcpy(&trans->cc_msg, disc, sizeof(struct gsm_mncc));
2664
2665 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
2666
2667 return gsm48_sendmsg(msg);
2668}
2669
2670static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
2671{
2672 struct gsm48_hdr *gh = msgb_l3(msg);
2673 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2674 struct tlv_parsed tp;
2675 struct gsm_mncc rel;
2676 int rc;
2677
2678 gsm48_stop_cc_timer(trans);
2679
2680 memset(&rel, 0, sizeof(struct gsm_mncc));
2681 rel.callref = trans->callref;
2682 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2683 /* cause */
2684 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2685 rel.fields |= MNCC_F_CAUSE;
2686 decode_cause(&rel.cause,
2687 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2688 }
2689 /* facility */
2690 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2691 rel.fields |= MNCC_F_FACILITY;
2692 decode_facility(&rel.facility,
2693 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2694 }
2695 /* user-user */
2696 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2697 rel.fields |= MNCC_F_USERUSER;
2698 decode_useruser(&rel.useruser,
2699 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2700 }
2701 /* ss-version */
2702 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2703 rel.fields |= MNCC_F_SSVERSION;
2704 decode_ssversion(&rel.ssversion,
2705 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2706 }
2707
2708 if (trans->state == GSM_CSTATE_RELEASE_REQ) {
2709 /* release collision 5.4.5 */
2710 rc = mncc_recvmsg(trans->network, trans, MNCC_REL_CNF, &rel);
2711 } else {
2712 rc = gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC | trans->transaction_id,
2713 GSM48_MT_CC_RELEASE_COMPL);
2714 rc = mncc_recvmsg(trans->network, trans, MNCC_REL_IND, &rel);
2715 }
2716
2717 new_cc_state(trans, GSM_CSTATE_NULL);
2718
2719 trans->callref = 0;
2720 free_trans(trans);
2721
2722 return rc;
2723}
2724
2725static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
2726{
2727 struct gsm_mncc *rel = arg;
2728 struct msgb *msg = gsm48_msgb_alloc();
2729 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2730
2731 gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id;
2732 msg->lchan = trans->lchan;
2733 gh->msg_type = GSM48_MT_CC_RELEASE;
2734
2735 trans->callref = 0;
2736
2737 gsm48_stop_cc_timer(trans);
2738 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
2739
2740 /* cause */
2741 if (rel->fields & MNCC_F_CAUSE)
2742 encode_cause(msg, 0, &rel->cause);
2743 /* facility */
2744 if (rel->fields & MNCC_F_FACILITY)
2745 encode_facility(msg, 0, &rel->facility);
2746 /* user-user */
2747 if (rel->fields & MNCC_F_USERUSER)
2748 encode_useruser(msg, 0, &rel->useruser);
2749
2750 trans->T308_second = 0;
2751 memcpy(&trans->cc_msg, rel, sizeof(struct gsm_mncc));
2752
2753 if (trans->state != GSM_CSTATE_RELEASE_REQ)
2754 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
2755
2756 return gsm48_sendmsg(msg);
2757}
2758
2759static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
2760{
2761 struct gsm48_hdr *gh = msgb_l3(msg);
2762 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2763 struct tlv_parsed tp;
2764 struct gsm_mncc rel;
2765 int rc = 0;
2766
2767 gsm48_stop_cc_timer(trans);
2768
2769 memset(&rel, 0, sizeof(struct gsm_mncc));
2770 rel.callref = trans->callref;
2771 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2772 /* cause */
2773 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2774 rel.fields |= MNCC_F_CAUSE;
2775 decode_cause(&rel.cause,
2776 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2777 }
2778 /* facility */
2779 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2780 rel.fields |= MNCC_F_FACILITY;
2781 decode_facility(&rel.facility,
2782 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2783 }
2784 /* user-user */
2785 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2786 rel.fields |= MNCC_F_USERUSER;
2787 decode_useruser(&rel.useruser,
2788 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2789 }
2790 /* ss-version */
2791 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2792 rel.fields |= MNCC_F_SSVERSION;
2793 decode_ssversion(&rel.ssversion,
2794 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2795 }
2796
2797 if (trans->callref) {
2798 switch (trans->state) {
2799 case GSM_CSTATE_CALL_PRESENT:
2800 rc = mncc_recvmsg(trans->network, trans,
2801 MNCC_REJ_IND, &rel);
2802 break;
2803 case GSM_CSTATE_RELEASE_REQ:
2804 rc = mncc_recvmsg(trans->network, trans,
2805 MNCC_REL_CNF, &rel);
2806 break;
2807 default:
2808 rc = mncc_recvmsg(trans->network, trans,
2809 MNCC_REL_IND, &rel);
2810 }
2811 }
2812
2813 trans->callref = 0;
2814 free_trans(trans);
2815
2816 return rc;
2817}
2818
2819static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
2820{
2821 struct gsm_mncc *rel = arg;
2822 struct msgb *msg = gsm48_msgb_alloc();
2823 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2824
2825 gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id;
2826 msg->lchan = trans->lchan;
2827 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
2828
2829 trans->callref = 0;
2830
2831 gsm48_stop_cc_timer(trans);
2832
2833 /* cause */
2834 if (rel->fields & MNCC_F_CAUSE)
2835 encode_cause(msg, 0, &rel->cause);
2836 /* facility */
2837 if (rel->fields & MNCC_F_FACILITY)
2838 encode_facility(msg, 0, &rel->facility);
2839 /* user-user */
2840 if (rel->fields & MNCC_F_USERUSER)
2841 encode_useruser(msg, 0, &rel->useruser);
2842
2843 free_trans(trans);
2844
2845 return gsm48_sendmsg(msg);
2846}
2847
2848static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
2849{
2850 struct gsm48_hdr *gh = msgb_l3(msg);
2851 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2852 struct tlv_parsed tp;
2853 struct gsm_mncc fac;
2854
2855 memset(&fac, 0, sizeof(struct gsm_mncc));
2856 fac.callref = trans->callref;
2857 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
2858 /* facility */
2859 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2860 fac.fields |= MNCC_F_FACILITY;
2861 decode_facility(&fac.facility,
2862 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2863 }
2864 /* ss-version */
2865 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2866 fac.fields |= MNCC_F_SSVERSION;
2867 decode_ssversion(&fac.ssversion,
2868 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2869 }
2870
2871 return mncc_recvmsg(trans->network, trans, MNCC_FACILITY_IND, &fac);
2872}
2873
2874static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
2875{
2876 struct gsm_mncc *fac = arg;
2877 struct msgb *msg = gsm48_msgb_alloc();
2878 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2879
2880 gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id;
2881 msg->lchan = trans->lchan;
2882 gh->msg_type = GSM48_MT_CC_FACILITY;
2883
2884 /* facility */
2885 encode_facility(msg, 1, &fac->facility);
2886
2887 return gsm48_sendmsg(msg);
2888}
2889
2890static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
2891{
2892 struct gsm_mncc hold;
2893
2894 memset(&hold, 0, sizeof(struct gsm_mncc));
2895 hold.callref = trans->callref;
2896 return mncc_recvmsg(trans->network, trans, MNCC_HOLD_IND, &hold);
2897}
2898
2899static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
2900{
2901 struct msgb *msg = gsm48_msgb_alloc();
2902 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2903
2904 gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id;
2905 msg->lchan = trans->lchan;
2906 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
2907
2908 return gsm48_sendmsg(msg);
2909}
2910
2911static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
2912{
2913 struct gsm_mncc *hold_rej = arg;
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_REJ;
2920
2921 /* cause */
2922 if (hold_rej->fields & MNCC_F_CAUSE)
2923 encode_cause(msg, 1, &hold_rej->cause);
2924 else
2925 encode_cause(msg, 1, &default_cause);
2926
2927 return gsm48_sendmsg(msg);
2928}
2929
2930static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
2931{
2932 struct gsm_mncc retrieve;
2933
2934 memset(&retrieve, 0, sizeof(struct gsm_mncc));
2935 retrieve.callref = trans->callref;
2936 return mncc_recvmsg(trans->network, trans, MNCC_RETRIEVE_IND, &retrieve);
2937}
2938
2939static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
2940{
2941 struct msgb *msg = gsm48_msgb_alloc();
2942 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2943
2944 gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id;
2945 msg->lchan = trans->lchan;
2946 gh->msg_type = GSM48_MT_CC_RETR_ACK;
2947
2948 return gsm48_sendmsg(msg);
2949}
2950
2951static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
2952{
2953 struct gsm_mncc *retrieve_rej = arg;
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_REJ;
2960
2961 /* cause */
2962 if (retrieve_rej->fields & MNCC_F_CAUSE)
2963 encode_cause(msg, 1, &retrieve_rej->cause);
2964 else
2965 encode_cause(msg, 1, &default_cause);
2966
2967 return gsm48_sendmsg(msg);
2968}
2969
2970static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
2971{
2972 struct gsm48_hdr *gh = msgb_l3(msg);
2973 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2974 struct tlv_parsed tp;
2975 struct gsm_mncc dtmf;
2976
2977 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2978 dtmf.callref = trans->callref;
2979 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2980 /* keypad facility */
2981 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
2982 dtmf.fields |= MNCC_F_KEYPAD;
2983 decode_keypad(&dtmf.keypad,
2984 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
2985 }
2986
2987 return mncc_recvmsg(trans->network, trans, MNCC_START_DTMF_IND, &dtmf);
2988}
2989
2990static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
2991{
2992 struct gsm_mncc *dtmf = arg;
2993 struct msgb *msg = gsm48_msgb_alloc();
2994 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2995
2996 gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id;
2997 msg->lchan = trans->lchan;
2998 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
2999
3000 /* keypad */
3001 if (dtmf->fields & MNCC_F_KEYPAD)
3002 encode_keypad(msg, dtmf->keypad);
3003
3004 return gsm48_sendmsg(msg);
3005}
3006
3007static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
3008{
3009 struct gsm_mncc *dtmf = arg;
3010 struct msgb *msg = gsm48_msgb_alloc();
3011 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3012
3013 gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id;
3014 msg->lchan = trans->lchan;
3015 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
3016
3017 /* cause */
3018 if (dtmf->fields & MNCC_F_CAUSE)
3019 encode_cause(msg, 1, &dtmf->cause);
3020 else
3021 encode_cause(msg, 1, &default_cause);
3022
3023 return gsm48_sendmsg(msg);
3024}
3025
3026static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
3027{
3028 struct msgb *msg = gsm48_msgb_alloc();
3029 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3030
3031 gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id;
3032 msg->lchan = trans->lchan;
3033 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
3034
3035 return gsm48_sendmsg(msg);
3036}
3037
3038static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
3039{
3040 struct gsm_mncc dtmf;
3041
3042 memset(&dtmf, 0, sizeof(struct gsm_mncc));
3043 dtmf.callref = trans->callref;
3044
3045 return mncc_recvmsg(trans->network, trans, MNCC_STOP_DTMF_IND, &dtmf);
3046}
3047
3048static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
3049{
3050 struct gsm48_hdr *gh = msgb_l3(msg);
3051 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
3052 struct tlv_parsed tp;
3053 struct gsm_mncc modify;
3054
3055 memset(&modify, 0, sizeof(struct gsm_mncc));
3056 modify.callref = trans->callref;
3057 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
3058 /* bearer capability */
3059 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
3060 modify.fields |= MNCC_F_BEARER_CAP;
3061 decode_bearer_cap(&modify.bearer_cap,
3062 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
3063 }
3064
3065 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
3066
3067 return mncc_recvmsg(trans->network, trans, MNCC_MODIFY_IND, &modify);
3068}
3069
3070static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
3071{
3072 struct gsm_mncc *modify = arg;
3073 struct msgb *msg = gsm48_msgb_alloc();
3074 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3075
3076 gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id;
3077 msg->lchan = trans->lchan;
3078 gh->msg_type = GSM48_MT_CC_MODIFY;
3079
3080 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
3081
3082 /* bearer capability */
3083 encode_bearer_cap(msg, 1, &modify->bearer_cap);
3084
3085 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
3086
3087 return gsm48_sendmsg(msg);
3088}
3089
3090static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
3091{
3092 struct gsm48_hdr *gh = msgb_l3(msg);
3093 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
3094 struct tlv_parsed tp;
3095 struct gsm_mncc modify;
3096
3097 gsm48_stop_cc_timer(trans);
3098
3099 memset(&modify, 0, sizeof(struct gsm_mncc));
3100 modify.callref = trans->callref;
3101 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
3102 /* bearer capability */
3103 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
3104 modify.fields |= MNCC_F_BEARER_CAP;
3105 decode_bearer_cap(&modify.bearer_cap,
3106 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
3107 }
3108
3109 new_cc_state(trans, GSM_CSTATE_ACTIVE);
3110
3111 return mncc_recvmsg(trans->network, trans, MNCC_MODIFY_CNF, &modify);
3112}
3113
3114static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
3115{
3116 struct gsm_mncc *modify = arg;
3117 struct msgb *msg = gsm48_msgb_alloc();
3118 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3119
3120 gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id;
3121 msg->lchan = trans->lchan;
3122 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
3123
3124 /* bearer capability */
3125 encode_bearer_cap(msg, 1, &modify->bearer_cap);
3126
3127 new_cc_state(trans, GSM_CSTATE_ACTIVE);
3128
3129 return gsm48_sendmsg(msg);
3130}
3131
3132static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
3133{
3134 struct gsm48_hdr *gh = msgb_l3(msg);
3135 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
3136 struct tlv_parsed tp;
3137 struct gsm_mncc modify;
3138
3139 gsm48_stop_cc_timer(trans);
3140
3141 memset(&modify, 0, sizeof(struct gsm_mncc));
3142 modify.callref = trans->callref;
3143 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
3144 /* bearer capability */
3145 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
3146 modify.fields |= GSM48_IE_BEARER_CAP;
3147 decode_bearer_cap(&modify.bearer_cap,
3148 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
3149 }
3150 /* cause */
3151 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
3152 modify.fields |= MNCC_F_CAUSE;
3153 decode_cause(&modify.cause,
3154 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
3155 }
3156
3157 new_cc_state(trans, GSM_CSTATE_ACTIVE);
3158
3159 return mncc_recvmsg(trans->network, trans, MNCC_MODIFY_REJ, &modify);
3160}
3161
3162static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
3163{
3164 struct gsm_mncc *modify = arg;
3165 struct msgb *msg = gsm48_msgb_alloc();
3166 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3167
3168 gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id;
3169 msg->lchan = trans->lchan;
3170 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
3171
3172 /* bearer capability */
3173 encode_bearer_cap(msg, 1, &modify->bearer_cap);
3174 /* cause */
3175 encode_cause(msg, 1, &modify->cause);
3176
3177 new_cc_state(trans, GSM_CSTATE_ACTIVE);
3178
3179 return gsm48_sendmsg(msg);
3180}
3181
3182static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
3183{
3184 struct gsm_mncc *notify = arg;
3185 struct msgb *msg = gsm48_msgb_alloc();
3186 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3187
3188 gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id;
3189 msg->lchan = trans->lchan;
3190 gh->msg_type = GSM48_MT_CC_NOTIFY;
3191
3192 /* notify */
3193 encode_notify(msg, notify->notify);
3194
3195 return gsm48_sendmsg(msg);
3196}
3197
3198static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
3199{
3200 struct gsm48_hdr *gh = msgb_l3(msg);
3201 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
3202// struct tlv_parsed tp;
3203 struct gsm_mncc notify;
3204
3205 memset(&notify, 0, sizeof(struct gsm_mncc));
3206 notify.callref = trans->callref;
3207// tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len);
3208 if (payload_len >= 1)
3209 decode_notify(&notify.notify, gh->data);
3210
3211 return mncc_recvmsg(trans->network, trans, MNCC_NOTIFY_IND, &notify);
3212}
3213
3214static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
3215{
3216 struct gsm_mncc *user = arg;
3217 struct msgb *msg = gsm48_msgb_alloc();
3218 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3219
3220 gh->proto_discr = GSM48_PDISC_CC | trans->transaction_id;
3221 msg->lchan = trans->lchan;
3222 gh->msg_type = GSM48_MT_CC_USER_INFO;
3223
3224 /* user-user */
3225 if (user->fields & MNCC_F_USERUSER)
3226 encode_useruser(msg, 1, &user->useruser);
3227 /* more data */
3228 if (user->more)
3229 encode_more(msg);
3230
3231 return gsm48_sendmsg(msg);
3232}
3233
3234static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
3235{
3236 struct gsm48_hdr *gh = msgb_l3(msg);
3237 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
3238 struct tlv_parsed tp;
3239 struct gsm_mncc user;
3240
3241 memset(&user, 0, sizeof(struct gsm_mncc));
3242 user.callref = trans->callref;
3243 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
3244 /* user-user */
3245 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
3246 user.fields |= MNCC_F_USERUSER;
3247 decode_useruser(&user.useruser,
3248 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
3249 }
3250 /* more data */
3251 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
3252 user.more = 1;
3253
3254 return mncc_recvmsg(trans->network, trans, MNCC_USERINFO_IND, &user);
3255}
3256
3257static int gsm48_lchan_modify(struct gsm_trans *trans, void *arg)
3258{
3259 struct gsm_mncc *mode = arg;
3260
3261 return gsm48_tx_chan_mode_modify(trans->lchan, mode->lchan_mode);
3262}
3263
3264static struct downstate {
3265 u_int32_t states;
3266 int type;
3267 int (*rout) (struct gsm_trans *trans, void *arg);
3268} downstatelist[] = {
3269 /* mobile originating call establishment */
3270 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
3271 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc},
3272 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
3273 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
3274 {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 */
3275 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
3276 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
3277 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
3278 /* mobile terminating call establishment */
3279 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
3280 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
3281 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
3282 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
3283 /* signalling during call */
3284 {SBIT(GSM_CSTATE_ACTIVE),
3285 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
3286 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
3287 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
3288 {ALL_STATES,
3289 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
3290 {ALL_STATES,
3291 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
3292 {ALL_STATES,
3293 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
3294 {SBIT(GSM_CSTATE_ACTIVE),
3295 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
3296 {SBIT(GSM_CSTATE_ACTIVE),
3297 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
3298 {SBIT(GSM_CSTATE_ACTIVE),
3299 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
3300 {SBIT(GSM_CSTATE_ACTIVE),
3301 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
3302 {SBIT(GSM_CSTATE_ACTIVE),
3303 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
3304 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
3305 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
3306 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
3307 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
3308 {SBIT(GSM_CSTATE_ACTIVE),
3309 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
3310 /* clearing */
3311 {SBIT(GSM_CSTATE_INITIATED),
3312 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
3313 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
3314 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
3315 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
3316 MNCC_REL_REQ, gsm48_cc_tx_release},
3317 /* special */
3318 {ALL_STATES,
3319 MNCC_LCHAN_MODIFY, gsm48_lchan_modify},
3320};
3321
3322#define DOWNSLLEN \
3323 (sizeof(downstatelist) / sizeof(struct downstate))
3324
3325
3326int mncc_send(struct gsm_network *net, int msg_type, void *arg)
3327{
3328 int i, j, k, l, rc = 0;
3329 struct gsm_trans *trans = NULL, *transt;
3330 struct gsm_subscriber *subscr;
3331 struct gsm_lchan *lchan = NULL, *lchant;
3332 struct gsm_bts *bts = NULL;
3333 struct gsm_bts_trx *trx;
3334 struct gsm_bts_trx_ts *ts;
3335 struct gsm_mncc *data = arg, rel;
3336
3337 /* handle special messages */
3338 switch(msg_type) {
3339 case MNCC_BRIDGE:
3340 return tch_bridge(net, arg);
3341 case MNCC_FRAME_DROP:
3342 return tch_recv(net, arg, 0);
3343 case MNCC_FRAME_RECV:
3344 return tch_recv(net, arg, 1);
3345 case GSM_TRAU_FRAME:
3346 return tch_frame(net, arg);
3347 }
3348
3349 memset(&rel, 0, sizeof(struct gsm_mncc));
3350 rel.callref = data->callref;
3351
3352 /* Find callref */
3353 trans = get_trans_ref(net, data->callref);
3354
3355 /* Callref unknown */
3356 if (!trans) {
3357 if (msg_type != MNCC_SETUP_REQ || !data->called.number[0]) {
3358 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3359 "Received '%s' from MNCC with "
3360 "unknown callref %d\n", data->called.number,
3361 get_mncc_name(msg_type), data->callref);
3362 /* Invalid call reference */
3363 return mncc_release_ind(net, NULL, data->callref, 1, 81);
3364 }
3365 /* New transaction due to setup, find subscriber */
3366 subscr = subscr_get_by_extension(data->called.number);
3367 /* If subscriber is not found */
3368 if (!subscr) {
3369 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3370 "Received '%s' from MNCC with "
3371 "unknown subscriber %s\n", data->called.number,
3372 get_mncc_name(msg_type), data->called.number);
3373 /* Unknown subscriber */
3374 return mncc_release_ind(net, NULL, data->callref, 1, 1);
3375 }
3376 /* If subscriber is not "attached" */
3377 if (!subscr->lac) {
3378 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3379 "Received '%s' from MNCC with "
3380 "detached subscriber %s\n", data->called.number,
3381 get_mncc_name(msg_type), data->called.number);
3382 subscr_put(subscr);
3383 /* Temporarily out of order */
3384 return mncc_release_ind(net, NULL, data->callref, 1, 27);
3385 }
3386 /* Create transaction */
3387 if (!(trans = calloc(1, sizeof(struct gsm_trans)))) {
3388 DEBUGP(DCC, "No memory for trans.\n");
3389 subscr_put(subscr);
3390 /* Ressource unavailable */
3391 mncc_release_ind(net, NULL, data->callref, 1, 47);
3392 return -ENOMEM;
3393 }
3394 trans->callref = data->callref;
3395 trans->network = net;
3396 trans->transaction_id = 0xff; /* unassigned */
3397 llist_add_tail(&trans->entry, &net->trans_list);
3398 /* Assign subscriber to transaction */
3399 trans->subscr = subscr;
3400 /* Find lchan */
3401 for (i = 0; i < net->num_bts; i++) {
3402 bts = &net->bts[i];
3403 for (j = 0; j < bts->num_trx; j++) {
3404 trx = &bts->trx[j];
3405 for (k = 0; k < TRX_NR_TS; k++) {
3406 ts = &trx->ts[k];
3407 for (l = 0; l < TS_MAX_LCHAN; l++) {
3408 lchant = &ts->lchan[l];
3409 if (lchant->subscr == subscr) {
3410 lchan = lchant;
3411 break;
3412 }
3413 }
3414 }
3415 }
3416 }
3417
3418 /* If subscriber has no lchan */
3419 if (!lchan) {
3420 /* find transaction with this subscriber already paging */
3421 llist_for_each_entry(transt, &net->trans_list, entry) {
3422 /* Transaction of our lchan? */
3423 if (transt == trans ||
3424 transt->subscr != subscr)
3425 continue;
3426 DEBUGP(DCC, "(bts %d trx - ts - ti -- sub %s) "
3427 "Received '%s' from MNCC with "
3428 "unallocated channel, paging already "
3429 "started.\n", bts->nr,
3430 data->called.number,
3431 get_mncc_name(msg_type));
3432 return 0;
3433 }
3434 /* store setup informations until paging was successfull */
3435 memcpy(&trans->cc_msg, data, sizeof(struct gsm_mncc));
3436 /* start paging subscriber on all BTS with her location */
3437 subscr->net = net;
3438 bts = NULL;
3439 do {
3440 bts = gsm_bts_by_lac(net, subscr->lac, bts);
3441 if (!bts)
3442 break;
3443 DEBUGP(DCC, "(bts %d trx - ts - ti -- sub %s) "
3444 "Received '%s' from MNCC with "
3445 "unallocated channel, paging.\n",
3446 bts->nr, data->called.number,
3447 get_mncc_name(msg_type));
3448 /* Trigger paging */
3449 paging_request(bts, subscr, RSL_CHANNEED_TCH_F,
3450 setup_trig_pag_evt, subscr);
3451 } while (1);
3452 return 0;
3453 }
3454 /* Assign lchan */
3455 trans->lchan = lchan;
3456 use_lchan(lchan);
3457 }
3458 lchan = trans->lchan;
3459
3460 /* if paging did not respond yet */
3461 if (!lchan) {
3462 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3463 "Received '%s' from MNCC in paging state\n",
3464 (trans->subscr)?(trans->subscr->extension):"-",
3465 get_mncc_name(msg_type));
3466 mncc_set_cause(&rel, 1, 16); /* Normal call clearing */
3467 if (msg_type == MNCC_REL_REQ)
3468 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
3469 else
3470 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
3471 trans->callref = 0;
3472 free_trans(trans);
3473 return rc;
3474 }
3475
3476 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x sub %s) "
3477 "Received '%s' from MNCC in state %d (%s)\n",
3478 lchan->ts->trx->bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
3479 trans->transaction_id,
3480 (lchan->subscr)?(lchan->subscr->extension):"-",
3481 get_mncc_name(msg_type), trans->state,
3482 cc_state_names[trans->state]);
3483
3484 /* Find function for current state and message */
3485 for (i = 0; i < DOWNSLLEN; i++)
3486 if ((msg_type == downstatelist[i].type)
3487 && ((1 << trans->state) & downstatelist[i].states))
3488 break;
3489 if (i == DOWNSLLEN) {
3490 DEBUGP(DCC, "Message unhandled at this state.\n");
3491 return 0;
3492 }
3493
3494 rc = downstatelist[i].rout(trans, arg);
3495
3496 return rc;
3497}
3498
3499
3500static struct datastate {
3501 u_int32_t states;
3502 int type;
3503 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
3504} datastatelist[] = {
3505 /* mobile originating call establishment */
3506 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3507 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
3508 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3509 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
3510 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
3511 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
3512 /* mobile terminating call establishment */
3513 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
3514 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
3515 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
3516 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
3517 {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 */
3518 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
3519 /* signalling during call */
3520 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
3521 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
3522 {SBIT(GSM_CSTATE_ACTIVE),
3523 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
3524 {ALL_STATES,
3525 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
3526 {ALL_STATES,
3527 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
3528 {ALL_STATES,
3529 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
3530 {SBIT(GSM_CSTATE_ACTIVE),
3531 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
3532 {SBIT(GSM_CSTATE_ACTIVE),
3533 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
3534 {SBIT(GSM_CSTATE_ACTIVE),
3535 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
3536 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3537 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
3538 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3539 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
3540 {SBIT(GSM_CSTATE_ACTIVE),
3541 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
3542 /* clearing */
3543 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
3544 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
3545 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
3546 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
3547 {ALL_STATES, /* 5.4.3.4 */
3548 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
3549};
3550
3551#define DATASLLEN \
3552 (sizeof(datastatelist) / sizeof(struct datastate))
3553
Harald Welte4bc90a12008-12-27 16:32:52 +00003554static int gsm0408_rcv_cc(struct msgb *msg)
3555{
3556 struct gsm48_hdr *gh = msgb_l3(msg);
3557 u_int8_t msg_type = gh->msg_type & 0xbf;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003558 u_int8_t transaction_id = (gh->proto_discr & 0xf0) ^ 0x80; /* flip */
3559 struct gsm_lchan *lchan = msg->lchan;
3560 struct gsm_trans *trans = NULL, *transt;
3561 struct gsm_network *net = lchan->ts->trx->bts->network;
3562 int i, rc = 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00003563
Harald Welte4bfdfe72009-06-10 23:11:52 +08003564 if (msg_type & 0x80) {
3565 DEBUGP(DCC, "MSG 0x%2x not defined for PD error\n", msg_type);
3566 return -EINVAL;
Harald Welte4bc90a12008-12-27 16:32:52 +00003567 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003568
3569 /* Find transaction */
3570 llist_for_each_entry(transt, &net->trans_list, entry) {
3571 /* Transaction of our lchan? */
3572 if (transt->lchan == lchan
3573 && transt->transaction_id == transaction_id) {
3574 trans = transt;
3575 }
3576 }
3577
3578 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x sub %s) "
3579 "Received '%s' from MS in state %d (%s)\n",
3580 lchan->ts->trx->bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
3581 transaction_id, (lchan->subscr)?(lchan->subscr->extension):"-",
3582 cc_msg_names[msg_type], trans?(trans->state):0,
3583 cc_state_names[trans?(trans->state):0]);
3584
3585 /* Create transaction */
3586 if (!trans) {
3587 DEBUGP(DCC, "Unknown transaction ID %02x, "
3588 "creating new trans.\n", transaction_id);
3589 /* Create transaction */
3590 if (!(trans = calloc(1, sizeof(struct gsm_trans)))) {
3591 DEBUGP(DCC, "No memory for trans.\n");
3592 rc = gsm48_tx_simple(msg->lchan,
3593 GSM48_PDISC_CC | transaction_id,
3594 GSM48_MT_CC_RELEASE_COMPL);
3595 return -ENOMEM;
3596 }
3597 llist_add_tail(&trans->entry, &net->trans_list);
3598 /* Assign transaction */
3599 trans->callref = new_callref++;
3600 trans->network = net;
3601 trans->transaction_id = transaction_id;
3602 trans->lchan = lchan;
3603 use_lchan(lchan);
3604 if (lchan->subscr) {
3605 trans->subscr = lchan->subscr;
3606 subscr_get(trans->subscr);
3607 }
3608 }
3609
3610 /* find function for current state and message */
3611 for (i = 0; i < DATASLLEN; i++)
3612 if ((msg_type == datastatelist[i].type)
3613 && ((1 << trans->state) & datastatelist[i].states))
3614 break;
3615 if (i == DATASLLEN) {
3616 DEBUGP(DCC, "Message unhandled at this state.\n");
3617 return 0;
3618 }
3619
3620 rc = datastatelist[i].rout(trans, msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00003621
3622 return rc;
3623}
3624
Harald Welte52b1f982008-12-23 20:25:15 +00003625/* here we pass in a msgb from the RSL->RLL. We expect the l3 pointer to be set */
3626int gsm0408_rcvmsg(struct msgb *msg)
3627{
3628 struct gsm48_hdr *gh = msgb_l3(msg);
3629 u_int8_t pdisc = gh->proto_discr & 0x0f;
Harald Welte8470bf22008-12-25 23:28:35 +00003630 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00003631
3632 switch (pdisc) {
3633 case GSM48_PDISC_CC:
3634 rc = gsm0408_rcv_cc(msg);
3635 break;
3636 case GSM48_PDISC_MM:
3637 rc = gsm0408_rcv_mm(msg);
3638 break;
3639 case GSM48_PDISC_RR:
3640 rc = gsm0408_rcv_rr(msg);
3641 break;
Harald Weltebcae43f2008-12-27 21:45:37 +00003642 case GSM48_PDISC_SMS:
Daniel Willmann8b3390e2008-12-28 00:31:09 +00003643 rc = gsm0411_rcv_sms(msg);
Harald Weltebcae43f2008-12-27 21:45:37 +00003644 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003645 case GSM48_PDISC_MM_GPRS:
Harald Weltebcae43f2008-12-27 21:45:37 +00003646 case GSM48_PDISC_SM_GPRS:
Harald Welte52b1f982008-12-23 20:25:15 +00003647 fprintf(stderr, "Unimplemented GSM 04.08 discriminator 0x%02d\n",
3648 pdisc);
3649 break;
3650 default:
3651 fprintf(stderr, "Unknown GSM 04.08 discriminator 0x%02d\n",
3652 pdisc);
3653 break;
3654 }
3655
3656 return rc;
3657}
Harald Welte8470bf22008-12-25 23:28:35 +00003658
Harald Welte8470bf22008-12-25 23:28:35 +00003659/* Section 9.1.8 / Table 9.9 */
3660struct chreq {
3661 u_int8_t val;
3662 u_int8_t mask;
3663 enum chreq_type type;
3664};
3665
3666/* If SYSTEM INFORMATION TYPE 4 NECI bit == 1 */
3667static const struct chreq chreq_type_neci1[] = {
3668 { 0xa0, 0xe0, CHREQ_T_EMERG_CALL },
3669 { 0xc0, 0xe0, CHREQ_T_CALL_REEST_TCH_F },
3670 { 0x68, 0xfc, CHREQ_T_CALL_REEST_TCH_H },
3671 { 0x6c, 0xfc, CHREQ_T_CALL_REEST_TCH_H_DBL },
3672 { 0xe0, 0xe0, CHREQ_T_SDCCH },
3673 { 0x40, 0xf0, CHREQ_T_VOICE_CALL_TCH_H },
3674 { 0x50, 0xf0, CHREQ_T_DATA_CALL_TCH_H },
3675 { 0x00, 0xf0, CHREQ_T_LOCATION_UPD },
3676 { 0x10, 0xf0, CHREQ_T_SDCCH },
3677 { 0x80, 0xe0, CHREQ_T_PAG_R_ANY },
3678 { 0x20, 0xf0, CHREQ_T_PAG_R_TCH_F },
3679 { 0x30, 0xf0, CHREQ_T_PAG_R_TCH_FH },
3680};
3681
3682/* If SYSTEM INFORMATION TYPE 4 NECI bit == 0 */
3683static const struct chreq chreq_type_neci0[] = {
3684 { 0xa0, 0xe0, CHREQ_T_EMERG_CALL },
3685 { 0xc0, 0xe0, CHREQ_T_CALL_REEST_TCH_H },
3686 { 0xe0, 0xe0, CHREQ_T_TCH_F },
3687 { 0x50, 0xf0, CHREQ_T_DATA_CALL_TCH_H },
3688 { 0x00, 0xe0, CHREQ_T_LOCATION_UPD },
3689 { 0x80, 0xe0, CHREQ_T_PAG_R_ANY },
3690 { 0x20, 0xf0, CHREQ_T_PAG_R_TCH_F },
3691 { 0x30, 0xf0, CHREQ_T_PAG_R_TCH_FH },
3692};
3693
3694static const enum gsm_chan_t ctype_by_chreq[] = {
3695 [CHREQ_T_EMERG_CALL] = GSM_LCHAN_TCH_F,
3696 [CHREQ_T_CALL_REEST_TCH_F] = GSM_LCHAN_TCH_F,
3697 [CHREQ_T_CALL_REEST_TCH_H] = GSM_LCHAN_TCH_H,
3698 [CHREQ_T_CALL_REEST_TCH_H_DBL] = GSM_LCHAN_TCH_H,
3699 [CHREQ_T_SDCCH] = GSM_LCHAN_SDCCH,
3700 [CHREQ_T_TCH_F] = GSM_LCHAN_TCH_F,
3701 [CHREQ_T_VOICE_CALL_TCH_H] = GSM_LCHAN_TCH_H,
3702 [CHREQ_T_DATA_CALL_TCH_H] = GSM_LCHAN_TCH_H,
3703 [CHREQ_T_LOCATION_UPD] = GSM_LCHAN_SDCCH,
3704 [CHREQ_T_PAG_R_ANY] = GSM_LCHAN_SDCCH,
3705 [CHREQ_T_PAG_R_TCH_F] = GSM_LCHAN_TCH_F,
3706 [CHREQ_T_PAG_R_TCH_FH] = GSM_LCHAN_TCH_F,
3707};
3708
Harald Weltee14a57c2008-12-29 04:08:28 +00003709static const enum gsm_chreq_reason_t reason_by_chreq[] = {
3710 [CHREQ_T_EMERG_CALL] = GSM_CHREQ_REASON_EMERG,
3711 [CHREQ_T_CALL_REEST_TCH_F] = GSM_CHREQ_REASON_CALL,
3712 [CHREQ_T_CALL_REEST_TCH_H] = GSM_CHREQ_REASON_CALL,
3713 [CHREQ_T_CALL_REEST_TCH_H_DBL] = GSM_CHREQ_REASON_CALL,
3714 [CHREQ_T_SDCCH] = GSM_CHREQ_REASON_OTHER,
3715 [CHREQ_T_TCH_F] = GSM_CHREQ_REASON_OTHER,
3716 [CHREQ_T_VOICE_CALL_TCH_H] = GSM_CHREQ_REASON_OTHER,
3717 [CHREQ_T_DATA_CALL_TCH_H] = GSM_CHREQ_REASON_OTHER,
3718 [CHREQ_T_LOCATION_UPD] = GSM_CHREQ_REASON_LOCATION_UPD,
3719 [CHREQ_T_PAG_R_ANY] = GSM_CHREQ_REASON_PAG,
3720 [CHREQ_T_PAG_R_TCH_F] = GSM_CHREQ_REASON_PAG,
3721 [CHREQ_T_PAG_R_TCH_FH] = GSM_CHREQ_REASON_PAG,
3722};
3723
Harald Welte8470bf22008-12-25 23:28:35 +00003724enum gsm_chan_t get_ctype_by_chreq(struct gsm_bts *bts, u_int8_t ra)
3725{
3726 int i;
3727 /* FIXME: determine if we set NECI = 0 in the BTS SI4 */
3728
3729 for (i = 0; i < ARRAY_SIZE(chreq_type_neci0); i++) {
3730 const struct chreq *chr = &chreq_type_neci0[i];
3731 if ((ra & chr->mask) == chr->val)
3732 return ctype_by_chreq[chr->type];
3733 }
3734 fprintf(stderr, "Unknown CHANNEL REQUEST RQD 0x%02x\n", ra);
3735 return GSM_LCHAN_SDCCH;
3736}
Harald Weltee14a57c2008-12-29 04:08:28 +00003737
3738enum gsm_chreq_reason_t get_reason_by_chreq(struct gsm_bts *bts, u_int8_t ra)
3739{
3740 int i;
3741 /* FIXME: determine if we set NECI = 0 in the BTS SI4 */
3742
3743 for (i = 0; i < ARRAY_SIZE(chreq_type_neci0); i++) {
3744 const struct chreq *chr = &chreq_type_neci0[i];
3745 if ((ra & chr->mask) == chr->val)
3746 return reason_by_chreq[chr->type];
3747 }
3748 fprintf(stderr, "Unknown CHANNEL REQUEST REASON 0x%02x\n", ra);
3749 return GSM_CHREQ_REASON_OTHER;
3750}
Harald Welte4bfdfe72009-06-10 23:11:52 +08003751
3752/* dequeue messages to layer 4 */
3753int bsc_upqueue(struct gsm_network *net)
3754{
3755 struct gsm_mncc *mncc;
3756 struct msgb *msg;
3757 int work = 0;
3758
3759 if (net)
3760 while ((msg = msgb_dequeue(&net->upqueue))) {
3761 mncc = (struct gsm_mncc *)msg->data;
3762 if (net->mncc_recv)
3763 net->mncc_recv(net, mncc->msg_type, mncc);
3764 work = 1; /* work done */
3765 }
3766
3767 return work;
3768}