blob: 9ad9e15392f4f897217e64faeb5593ee03ef8688 [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>
Daniel Willmanneea93372009-08-13 03:42:07 +020038#include <openbsc/gsm_utils.h>
Harald Welte8470bf22008-12-25 23:28:35 +000039#include <openbsc/gsm_subscriber.h>
Daniel Willmann8b3390e2008-12-28 00:31:09 +000040#include <openbsc/gsm_04_11.h>
Harald Welte8470bf22008-12-25 23:28:35 +000041#include <openbsc/gsm_04_08.h>
42#include <openbsc/abis_rsl.h>
Holger Freytherca362a62009-01-04 21:05:01 +000043#include <openbsc/chan_alloc.h>
Harald Welte0b4c34e2009-02-09 17:54:43 +000044#include <openbsc/paging.h>
Holger Freyther053e09d2009-02-14 22:51:06 +000045#include <openbsc/signal.h>
Harald Welte45b407a2009-05-23 15:51:12 +000046#include <openbsc/trau_frame.h>
Harald Welte11fa29c2009-02-19 17:24:39 +000047#include <openbsc/trau_mux.h>
Harald Welte805f6442009-07-28 18:25:29 +020048#include <openbsc/rtp_proxy.h>
Harald Welte2cf161b2009-06-20 22:36:41 +020049#include <openbsc/talloc.h>
Harald Weltedcaf5652009-07-23 18:56:43 +020050#include <openbsc/transaction.h>
Harald Welte52b1f982008-12-23 20:25:15 +000051
Harald Welte0c389302009-06-10 12:08:54 +080052#define GSM_MAX_FACILITY 128
53#define GSM_MAX_SSVERSION 128
54#define GSM_MAX_USERUSER 128
55
Harald Welte (local)d19e58b2009-08-15 02:30:58 +020056void *tall_locop_ctx;
Harald Welte2cf161b2009-06-20 22:36:41 +020057
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +020058extern int ipacc_rtp_direct;
Harald Welte805f6442009-07-28 18:25:29 +020059
Harald Welte09e38af2009-02-16 22:52:23 +000060static const struct tlv_definition rsl_att_tlvdef = {
61 .def = {
62 [GSM48_IE_MOBILE_ID] = { TLV_TYPE_TLV },
63 [GSM48_IE_NAME_LONG] = { TLV_TYPE_TLV },
64 [GSM48_IE_NAME_SHORT] = { TLV_TYPE_TLV },
65 [GSM48_IE_UTC] = { TLV_TYPE_TV },
66 [GSM48_IE_NET_TIME_TZ] = { TLV_TYPE_FIXED, 7 },
67 [GSM48_IE_LSA_IDENT] = { TLV_TYPE_TLV },
68
69 [GSM48_IE_BEARER_CAP] = { TLV_TYPE_TLV },
70 [GSM48_IE_CAUSE] = { TLV_TYPE_TLV },
71 [GSM48_IE_CC_CAP] = { TLV_TYPE_TLV },
72 [GSM48_IE_ALERT] = { TLV_TYPE_TLV },
73 [GSM48_IE_FACILITY] = { TLV_TYPE_TLV },
74 [GSM48_IE_PROGR_IND] = { TLV_TYPE_TLV },
75 [GSM48_IE_AUX_STATUS] = { TLV_TYPE_TLV },
Harald Welte0c389302009-06-10 12:08:54 +080076 [GSM48_IE_NOTIFY] = { TLV_TYPE_TV },
Harald Welte09e38af2009-02-16 22:52:23 +000077 [GSM48_IE_KPD_FACILITY] = { TLV_TYPE_TV },
78 [GSM48_IE_SIGNAL] = { TLV_TYPE_TV },
Harald Welte0c389302009-06-10 12:08:54 +080079 [GSM48_IE_CONN_BCD] = { TLV_TYPE_TLV },
80 [GSM48_IE_CONN_SUB] = { TLV_TYPE_TLV },
Harald Welte09e38af2009-02-16 22:52:23 +000081 [GSM48_IE_CALLING_BCD] = { TLV_TYPE_TLV },
82 [GSM48_IE_CALLING_SUB] = { TLV_TYPE_TLV },
83 [GSM48_IE_CALLED_BCD] = { TLV_TYPE_TLV },
84 [GSM48_IE_CALLED_SUB] = { TLV_TYPE_TLV },
85 [GSM48_IE_REDIR_BCD] = { TLV_TYPE_TLV },
86 [GSM48_IE_REDIR_SUB] = { TLV_TYPE_TLV },
87 [GSM48_IE_LOWL_COMPAT] = { TLV_TYPE_TLV },
88 [GSM48_IE_HIGHL_COMPAT] = { TLV_TYPE_TLV },
89 [GSM48_IE_USER_USER] = { TLV_TYPE_TLV },
90 [GSM48_IE_SS_VERS] = { TLV_TYPE_TLV },
91 [GSM48_IE_MORE_DATA] = { TLV_TYPE_T },
92 [GSM48_IE_CLIR_SUPP] = { TLV_TYPE_T },
93 [GSM48_IE_CLIR_INVOC] = { TLV_TYPE_T },
94 [GSM48_IE_REV_C_SETUP] = { TLV_TYPE_T },
Harald Welte0c389302009-06-10 12:08:54 +080095 [GSM48_IE_REPEAT_CIR] = { TLV_TYPE_T },
96 [GSM48_IE_REPEAT_SEQ] = { TLV_TYPE_T },
Harald Welte09e38af2009-02-16 22:52:23 +000097 /* FIXME: more elements */
98 },
99};
Harald Weltecf5b3592009-05-01 18:28:42 +0000100
101static const char *rr_cause_names[] = {
102 [GSM48_RR_CAUSE_NORMAL] = "Normal event",
103 [GSM48_RR_CAUSE_ABNORMAL_UNSPEC] = "Abnormal release, unspecified",
104 [GSM48_RR_CAUSE_ABNORMAL_UNACCT] = "Abnormal release, channel unacceptable",
105 [GSM48_RR_CAUSE_ABNORMAL_TIMER] = "Abnormal release, timer expired",
106 [GSM48_RR_CAUSE_ABNORMAL_NOACT] = "Abnormal release, no activity on radio path",
107 [GSM48_RR_CAUSE_PREMPTIVE_REL] = "Preemptive release",
108 [GSM48_RR_CAUSE_HNDOVER_IMP] = "Handover impossible, timing advance out of range",
109 [GSM48_RR_CAUSE_CHAN_MODE_UNACCT] = "Channel mode unacceptable",
110 [GSM48_RR_CAUSE_FREQ_NOT_IMPL] = "Frequency not implemented",
111 [GSM48_RR_CAUSE_CALL_CLEARED] = "Call already cleared",
112 [GSM48_RR_CAUSE_SEMANT_INCORR] = "Semantically incorrect message",
113 [GSM48_RR_CAUSE_INVALID_MAND_INF] = "Invalid mandatory information",
114 [GSM48_RR_CAUSE_MSG_TYPE_N] = "Message type non-existant or not implemented",
115 [GSM48_RR_CAUSE_MSG_TYPE_N_COMPAT] = "Message type not compatible with protocol state",
116 [GSM48_RR_CAUSE_COND_IE_ERROR] = "Conditional IE error",
117 [GSM48_RR_CAUSE_NO_CELL_ALLOC_A] = "No cell allocation available",
118 [GSM48_RR_CAUSE_PROT_ERROR_UNSPC] = "Protocol error unspecified",
119};
120
Harald Welte4bfdfe72009-06-10 23:11:52 +0800121static const char *cc_state_names[] = {
122 "NULL",
123 "INITIATED",
124 "illegal state 2",
125 "MO_CALL_PROC",
126 "CALL_DELIVERED",
127 "illegal state 5",
128 "CALL_PRESENT",
129 "CALL_RECEIVED",
130 "CONNECT_REQUEST",
131 "MO_TERM_CALL_CONF",
132 "ACTIVE",
133 "DISCONNECT_REQ",
134 "DISCONNECT_IND",
135 "illegal state 13",
136 "illegal state 14",
137 "illegal state 15",
138 "illegal state 16",
139 "illegal state 17",
140 "illegal state 18",
141 "RELEASE_REQ",
142 "illegal state 20",
143 "illegal state 21",
144 "illegal state 22",
145 "illegal state 23",
146 "illegal state 24",
147 "illegal state 25",
148 "MO_ORIG_MODIFY",
149 "MO_TERM_MODIFY",
150 "CONNECT_IND",
151 "illegal state 29",
152 "illegal state 30",
153 "illegal state 31",
154};
155
Harald Weltecf5b3592009-05-01 18:28:42 +0000156static char strbuf[64];
157
158static const char *rr_cause_name(u_int8_t cause)
159{
160 if (cause < ARRAY_SIZE(rr_cause_names) &&
161 rr_cause_names[cause])
162 return rr_cause_names[cause];
163
164 snprintf(strbuf, sizeof(strbuf), "0x%02x", cause);
165 return strbuf;
166}
167
Harald Weltef7c43522009-06-09 20:24:21 +0000168static void parse_meas_rep(struct gsm_meas_rep *rep, const u_int8_t *data,
169 int len)
170{
171 memset(rep, 0, sizeof(*rep));
172
173 if (data[0] & 0x80)
174 rep->flags |= MEAS_REP_F_BA1;
175 if (data[0] & 0x40)
176 rep->flags |= MEAS_REP_F_DTX;
Harald Welte5a691b52009-07-05 04:05:44 +0200177 if ((data[1] & 0x40) == 0x00)
Harald Weltef7c43522009-06-09 20:24:21 +0000178 rep->flags |= MEAS_REP_F_VALID;
179
180 rep->rxlev_full = data[0] & 0x3f;
181 rep->rxlev_sub = data[1] & 0x3f;
182 rep->rxqual_full = (data[3] >> 4) & 0x7;
183 rep->rxqual_sub = (data[3] >> 1) & 0x7;
184 rep->num_cell = data[4] >> 6 | ((data[3] & 0x01) << 2);
185 if (rep->num_cell < 1)
186 return;
187
188 /* an encoding nightmare in perfection */
189
190 rep->cell[0].rxlev = data[4] & 0x3f;
191 rep->cell[0].bcch_freq = data[5] >> 2;
192 rep->cell[0].bsic = ((data[5] & 0x03) << 3) | (data[6] >> 5);
193 if (rep->num_cell < 2)
194 return;
195
196 rep->cell[1].rxlev = ((data[6] & 0x1f) << 1) | (data[7] >> 7);
197 rep->cell[1].bcch_freq = (data[7] >> 2) & 0x1f;
198 rep->cell[1].bsic = ((data[7] & 0x03) << 4) | (data[8] >> 4);
199 if (rep->num_cell < 3)
200 return;
201
202 rep->cell[2].rxlev = ((data[8] & 0x0f) << 2) | (data[9] >> 6);
203 rep->cell[2].bcch_freq = (data[9] >> 1) & 0x1f;
204 rep->cell[2].bsic = ((data[9] & 0x01) << 6) | (data[10] >> 3);
205 if (rep->num_cell < 4)
206 return;
207
208 rep->cell[3].rxlev = ((data[10] & 0x07) << 3) | (data[11] >> 5);
209 rep->cell[3].bcch_freq = data[11] & 0x1f;
210 rep->cell[3].bsic = data[12] >> 2;
211 if (rep->num_cell < 5)
212 return;
213
214 rep->cell[4].rxlev = ((data[12] & 0x03) << 4) | (data[13] >> 4);
215 rep->cell[4].bcch_freq = ((data[13] & 0xf) << 1) | (data[14] >> 7);
216 rep->cell[4].bsic = (data[14] >> 1) & 0x3f;
217 if (rep->num_cell < 6)
218 return;
219
220 rep->cell[5].rxlev = ((data[14] & 0x01) << 5) | (data[15] >> 3);
221 rep->cell[5].bcch_freq = ((data[15] & 0x07) << 2) | (data[16] >> 6);
222 rep->cell[5].bsic = data[16] & 0x3f;
223}
224
Holger Freytherd51524f2009-06-09 08:27:07 +0000225int gsm0408_loc_upd_acc(struct gsm_lchan *lchan, u_int32_t tmsi);
Harald Welte65e74cc2008-12-29 01:55:35 +0000226static int gsm48_tx_simple(struct gsm_lchan *lchan,
227 u_int8_t pdisc, u_int8_t msg_type);
Holger Freytherb7193e42008-12-29 17:44:08 +0000228static void schedule_reject(struct gsm_lchan *lchan);
Harald Welte65e74cc2008-12-29 01:55:35 +0000229
Harald Welte52b1f982008-12-23 20:25:15 +0000230struct gsm_lai {
231 u_int16_t mcc;
232 u_int16_t mnc;
233 u_int16_t lac;
234};
235
Holger Freythere97f7fb2008-12-31 18:52:11 +0000236static int reject_cause = 0;
237void gsm0408_set_reject_cause(int cause)
238{
239 reject_cause = cause;
240}
241
Harald Welte4bfdfe72009-06-10 23:11:52 +0800242static u_int32_t new_callref = 0x80000001;
243
Holger Freyther73487a22008-12-31 18:53:57 +0000244static int authorize_subscriber(struct gsm_loc_updating_operation *loc,
245 struct gsm_subscriber *subscriber)
Holger Freyther89824fc2008-12-30 16:18:18 +0000246{
247 if (!subscriber)
248 return 0;
249
Holger Freyther73487a22008-12-31 18:53:57 +0000250 /*
251 * Do not send accept yet as more information should arrive. Some
252 * phones will not send us the information and we will have to check
253 * what we want to do with that.
254 */
255 if (loc && (loc->waiting_for_imsi || loc->waiting_for_imei))
256 return 0;
257
Jan Luebbe06513f22009-08-12 12:48:00 +0200258 switch (subscriber->net->auth_policy) {
259 case GSM_AUTH_POLICY_CLOSED:
260 return subscriber->authorized;
Harald Welte (local)aa9dc192009-08-13 13:49:51 +0200261 case GSM_AUTH_POLICY_TOKEN:
Harald Welte (local)ee9afe32009-08-13 20:44:23 +0200262 if (subscriber->authorized)
263 return subscriber->authorized;
Harald Welte (local)aa9dc192009-08-13 13:49:51 +0200264 return (subscriber->flags & GSM_SUBSCRIBER_FIRST_CONTACT);
Jan Luebbe06513f22009-08-12 12:48:00 +0200265 case GSM_AUTH_POLICY_ACCEPT_ALL:
Holger Freyther89824fc2008-12-30 16:18:18 +0000266 return 1;
Jan Luebbe06513f22009-08-12 12:48:00 +0200267 default:
268 return 0;
269 }
Holger Freyther89824fc2008-12-30 16:18:18 +0000270}
Holger Freyther07cc8d82008-12-29 06:23:46 +0000271
Holger Freyther73487a22008-12-31 18:53:57 +0000272static void release_loc_updating_req(struct gsm_lchan *lchan)
273{
Harald Welte179f0642008-12-31 23:59:18 +0000274 if (!lchan->loc_operation)
Holger Freyther73487a22008-12-31 18:53:57 +0000275 return;
276
Harald Welteff117a82009-05-23 05:22:08 +0000277 bsc_del_timer(&lchan->loc_operation->updating_timer);
Harald Welte2cf161b2009-06-20 22:36:41 +0200278 talloc_free(lchan->loc_operation);
Holger Freyther73487a22008-12-31 18:53:57 +0000279 lchan->loc_operation = 0;
Holger Freyther3eaa7922009-01-01 02:59:03 +0000280 put_lchan(lchan);
Holger Freyther73487a22008-12-31 18:53:57 +0000281}
282
283static void allocate_loc_updating_req(struct gsm_lchan *lchan)
284{
Holger Freyther67b4b9a2009-01-01 03:46:11 +0000285 use_lchan(lchan);
Holger Freyther73487a22008-12-31 18:53:57 +0000286 release_loc_updating_req(lchan);
287
Harald Welte470ec292009-06-26 20:25:23 +0200288 lchan->loc_operation = talloc_zero(tall_locop_ctx,
289 struct gsm_loc_updating_operation);
Holger Freyther73487a22008-12-31 18:53:57 +0000290}
Holger Freyther07cc8d82008-12-29 06:23:46 +0000291
Holger Freytherd51524f2009-06-09 08:27:07 +0000292static int gsm0408_authorize(struct gsm_lchan *lchan, struct msgb *msg)
293{
Holger Freytherd51524f2009-06-09 08:27:07 +0000294 if (authorize_subscriber(lchan->loc_operation, lchan->subscr)) {
Harald Welteee5ad162009-08-09 19:07:00 +0200295 int rc;
296
Holger Freytherd51524f2009-06-09 08:27:07 +0000297 db_subscriber_alloc_tmsi(lchan->subscr);
Holger Freytherd51524f2009-06-09 08:27:07 +0000298 release_loc_updating_req(lchan);
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200299 rc = gsm0408_loc_upd_acc(msg->lchan, lchan->subscr->tmsi);
Harald Welteee5ad162009-08-09 19:07:00 +0200300 /* call subscr_update after putting the loc_upd_acc
301 * in the transmit queue, since S_SUBSCR_ATTACHED might
302 * trigger further action like SMS delivery */
303 subscr_update(lchan->subscr, msg->trx->bts,
304 GSM_SUBSCRIBER_UPDATE_ATTACHED);
305 return rc;
Holger Freytherd51524f2009-06-09 08:27:07 +0000306 }
307
308 return 0;
309}
310
Holger Freyther7c19f742009-06-06 13:54:35 +0000311static int gsm0408_handle_lchan_signal(unsigned int subsys, unsigned int signal,
312 void *handler_data, void *signal_data)
313{
Harald Welte4bfdfe72009-06-10 23:11:52 +0800314 struct gsm_trans *trans, *temp;
315
Holger Freyther7c19f742009-06-06 13:54:35 +0000316 if (subsys != SS_LCHAN || signal != S_LCHAN_UNEXPECTED_RELEASE)
317 return 0;
318
319 /*
320 * Cancel any outstanding location updating request
321 * operation taking place on the lchan.
322 */
Harald Welte1a5c6bd2009-07-04 09:35:21 +0200323 struct gsm_lchan *lchan = (struct gsm_lchan *)signal_data;
Harald Weltec05677b2009-06-26 20:17:06 +0200324 if (!lchan)
325 return 0;
326
Holger Freyther7c19f742009-06-06 13:54:35 +0000327 release_loc_updating_req(lchan);
328
Harald Welte4bfdfe72009-06-10 23:11:52 +0800329 /* Free all transactions that are associated with the released lchan */
Harald Weltedcaf5652009-07-23 18:56:43 +0200330 /* FIXME: this is not neccessarily the right thing to do, we should
331 * only set trans->lchan to NULL and wait for another lchan to be
332 * established to the same MM entity (phone/subscriber) */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800333 llist_for_each_entry_safe(trans, temp, &lchan->ts->trx->bts->network->trans_list, entry) {
334 if (trans->lchan == lchan)
Harald Weltedcaf5652009-07-23 18:56:43 +0200335 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +0800336 }
337
Holger Freyther7c19f742009-06-06 13:54:35 +0000338 return 0;
339}
340
Harald Welte09e38af2009-02-16 22:52:23 +0000341static const char bcd_num_digits[] = {
342 '0', '1', '2', '3', '4', '5', '6', '7',
343 '8', '9', '*', '#', 'a', 'b', 'c', '\0'
344};
345
Harald Welte0c389302009-06-10 12:08:54 +0800346/* decode a 'called/calling/connect party BCD number' as in 10.5.4.7 */
347int decode_bcd_number(char *output, int output_len, const u_int8_t *bcd_lv,
348 int h_len)
Harald Welte09e38af2009-02-16 22:52:23 +0000349{
350 u_int8_t in_len = bcd_lv[0];
351 int i;
352
Harald Welte0c389302009-06-10 12:08:54 +0800353 for (i = 1 + h_len; i <= in_len; i++) {
Harald Welte09e38af2009-02-16 22:52:23 +0000354 /* lower nibble */
355 output_len--;
356 if (output_len <= 1)
357 break;
358 *output++ = bcd_num_digits[bcd_lv[i] & 0xf];
359
360 /* higher nibble */
361 output_len--;
362 if (output_len <= 1)
363 break;
364 *output++ = bcd_num_digits[bcd_lv[i] >> 4];
365 }
366 if (output_len >= 1)
367 *output++ = '\0';
368
Harald Welte0c389302009-06-10 12:08:54 +0800369 return 0;
Harald Welte09e38af2009-02-16 22:52:23 +0000370}
371
372/* convert a single ASCII character to call-control BCD */
373static int asc_to_bcd(const char asc)
374{
375 int i;
376
377 for (i = 0; i < ARRAY_SIZE(bcd_num_digits); i++) {
378 if (bcd_num_digits[i] == asc)
379 return i;
380 }
381 return -EINVAL;
382}
383
Harald Welte0c389302009-06-10 12:08:54 +0800384/* convert a ASCII phone number to 'called/calling/connect party BCD number' */
Harald Welte09e38af2009-02-16 22:52:23 +0000385int encode_bcd_number(u_int8_t *bcd_lv, u_int8_t max_len,
Harald Welte0c389302009-06-10 12:08:54 +0800386 int h_len, const char *input)
Harald Welte09e38af2009-02-16 22:52:23 +0000387{
388 int in_len = strlen(input);
389 int i;
Harald Welte0c389302009-06-10 12:08:54 +0800390 u_int8_t *bcd_cur = bcd_lv + 1 + h_len;
Harald Welte09e38af2009-02-16 22:52:23 +0000391
392 /* two digits per byte, plus type byte */
Harald Welte0c389302009-06-10 12:08:54 +0800393 bcd_lv[0] = in_len/2 + h_len;
Harald Welte09e38af2009-02-16 22:52:23 +0000394 if (in_len % 2)
395 bcd_lv[0]++;
396
Harald Welte0c389302009-06-10 12:08:54 +0800397 if (bcd_lv[0] > max_len)
398 return -EIO;
Harald Welte09e38af2009-02-16 22:52:23 +0000399
400 for (i = 0; i < in_len; i++) {
401 int rc = asc_to_bcd(input[i]);
402 if (rc < 0)
403 return rc;
404 if (i % 2 == 0)
405 *bcd_cur = rc;
406 else
407 *bcd_cur++ |= (rc << 4);
408 }
409 /* append padding nibble in case of odd length */
410 if (i % 2)
411 *bcd_cur++ |= 0xf0;
412
413 /* return how many bytes we used */
414 return (bcd_cur - bcd_lv);
415}
416
Harald Welte0c389302009-06-10 12:08:54 +0800417/* decode 'bearer capability' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800418static int decode_bearer_cap(struct gsm_mncc_bearer_cap *bcap,
Harald Welte0c389302009-06-10 12:08:54 +0800419 const u_int8_t *lv)
420{
421 u_int8_t in_len = lv[0];
422 int i, s;
423
424 if (in_len < 1)
425 return -EINVAL;
426
Harald Welte4bfdfe72009-06-10 23:11:52 +0800427 bcap->speech_ver[0] = -1; /* end of list, of maximum 7 values */
Harald Welte0c389302009-06-10 12:08:54 +0800428
429 /* octet 3 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800430 bcap->transfer = lv[1] & 0x07;
431 bcap->mode = (lv[1] & 0x08) >> 3;
432 bcap->coding = (lv[1] & 0x10) >> 4;
433 bcap->radio = (lv[1] & 0x60) >> 5;
Harald Welte0c389302009-06-10 12:08:54 +0800434
435 i = 1;
436 s = 0;
437 while(!(lv[i] & 0x80)) {
438 i++; /* octet 3a etc */
439 if (in_len < i)
440 return 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800441 bcap->speech_ver[s++] = lv[i] & 0x0f;
442 bcap->speech_ver[s] = -1; /* end of list */
Harald Welte0c389302009-06-10 12:08:54 +0800443 if (i == 2) /* octet 3a */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800444 bcap->speech_ctm = (lv[i] & 0x20) >> 5;
Harald Welte0c389302009-06-10 12:08:54 +0800445 if (s == 7) /* maximum speech versions + end of list */
446 return 0;
447 }
448
449 return 0;
450}
451
452/* encode 'bearer capability' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800453static int encode_bearer_cap(struct msgb *msg, int lv_only,
454 const struct gsm_mncc_bearer_cap *bcap)
Harald Welte0c389302009-06-10 12:08:54 +0800455{
456 u_int8_t lv[32 + 1];
457 int i, s;
458
Harald Welte4bfdfe72009-06-10 23:11:52 +0800459 lv[1] = bcap->transfer;
460 lv[1] |= bcap->mode << 3;
461 lv[1] |= bcap->coding << 4;
462 lv[1] |= bcap->radio << 5;
Harald Welte0c389302009-06-10 12:08:54 +0800463
464 i = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800465 for (s = 0; bcap->speech_ver[s] >= 0; s++) {
Harald Welte0c389302009-06-10 12:08:54 +0800466 i++; /* octet 3a etc */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800467 lv[i] = bcap->speech_ver[s];
Harald Welte0c389302009-06-10 12:08:54 +0800468 if (i == 2) /* octet 3a */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800469 lv[i] |= bcap->speech_ctm << 5;
Harald Welte0c389302009-06-10 12:08:54 +0800470 }
471 lv[i] |= 0x80; /* last IE of octet 3 etc */
472
473 lv[0] = i;
474 if (lv_only)
475 msgb_lv_put(msg, lv[0], lv+1);
476 else
477 msgb_tlv_put(msg, GSM48_IE_BEARER_CAP, lv[0], lv+1);
478
479 return 0;
480}
481
482/* decode 'call control cap' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800483static int decode_cccap(struct gsm_mncc_cccap *ccap, const u_int8_t *lv)
Harald Welte0c389302009-06-10 12:08:54 +0800484{
485 u_int8_t in_len = lv[0];
486
487 if (in_len < 1)
488 return -EINVAL;
489
490 /* octet 3 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800491 ccap->dtmf = lv[1] & 0x01;
492 ccap->pcp = (lv[1] & 0x02) >> 1;
Harald Welte0c389302009-06-10 12:08:54 +0800493
494 return 0;
495}
496
497/* decode 'called party BCD number' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800498static int decode_called(struct gsm_mncc_number *called,
499 const u_int8_t *lv)
Harald Welte0c389302009-06-10 12:08:54 +0800500{
501 u_int8_t in_len = lv[0];
502
503 if (in_len < 1)
504 return -EINVAL;
505
506 /* octet 3 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800507 called->plan = lv[1] & 0x0f;
508 called->type = (lv[1] & 0x70) >> 4;
Harald Welte0c389302009-06-10 12:08:54 +0800509
510 /* octet 4..N */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800511 decode_bcd_number(called->number, sizeof(called->number), lv, 1);
Harald Welte0c389302009-06-10 12:08:54 +0800512
513 return 0;
514}
515
516/* encode 'called party BCD number' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800517static int encode_called(struct msgb *msg,
518 const struct gsm_mncc_number *called)
Harald Welte0c389302009-06-10 12:08:54 +0800519{
520 u_int8_t lv[18];
521 int ret;
522
523 /* octet 3 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800524 lv[1] = called->plan;
525 lv[1] |= called->type << 4;
Harald Welte0c389302009-06-10 12:08:54 +0800526
527 /* octet 4..N, octet 2 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800528 ret = encode_bcd_number(lv, sizeof(lv), 1, called->number);
Harald Welte0c389302009-06-10 12:08:54 +0800529 if (ret < 0)
530 return ret;
531
532 msgb_tlv_put(msg, GSM48_IE_CALLED_BCD, lv[0], lv+1);
533
534 return 0;
535}
536
537/* encode callerid of various IEs */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800538static int encode_callerid(struct msgb *msg, int ie,
539 const struct gsm_mncc_number *callerid)
Harald Welte0c389302009-06-10 12:08:54 +0800540{
541 u_int8_t lv[13];
542 int h_len = 1;
543 int ret;
544
545 /* octet 3 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800546 lv[1] = callerid->plan;
547 lv[1] |= callerid->type << 4;
Harald Welte0c389302009-06-10 12:08:54 +0800548
Harald Welte4bfdfe72009-06-10 23:11:52 +0800549 if (callerid->present || callerid->screen) {
Harald Welte0c389302009-06-10 12:08:54 +0800550 /* octet 3a */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800551 lv[2] = callerid->screen;
552 lv[2] |= callerid->present << 5;
Harald Welte0c389302009-06-10 12:08:54 +0800553 lv[2] |= 0x80;
554 h_len++;
555 } else
556 lv[1] |= 0x80;
557
558 /* octet 4..N, octet 2 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800559 ret = encode_bcd_number(lv, sizeof(lv), h_len, callerid->number);
Harald Welte0c389302009-06-10 12:08:54 +0800560 if (ret < 0)
561 return ret;
562
563 msgb_tlv_put(msg, ie, lv[0], lv+1);
564
565 return 0;
566}
567
568/* decode 'cause' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800569static int decode_cause(struct gsm_mncc_cause *cause,
Harald Welte0c389302009-06-10 12:08:54 +0800570 const u_int8_t *lv)
571{
572 u_int8_t in_len = lv[0];
573 int i;
574
575 if (in_len < 2)
576 return -EINVAL;
577
Harald Welte4bfdfe72009-06-10 23:11:52 +0800578 cause->diag_len = 0;
Harald Welte0c389302009-06-10 12:08:54 +0800579
580 /* octet 3 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800581 cause->location = lv[1] & 0x0f;
582 cause->coding = (lv[1] & 0x60) >> 5;
Harald Welte0c389302009-06-10 12:08:54 +0800583
584 i = 1;
585 if (!(lv[i] & 0x80)) {
586 i++; /* octet 3a */
587 if (in_len < i+1)
588 return 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800589 cause->rec = 1;
590 cause->rec_val = lv[i] & 0x7f;
Harald Welte0c389302009-06-10 12:08:54 +0800591
592 }
593 i++;
594
595 /* octet 4 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800596 cause->value = lv[i] & 0x7f;
Harald Welte0c389302009-06-10 12:08:54 +0800597 i++;
598
599 if (in_len < i) /* no diag */
600 return 0;
601
602 if (in_len - (i-1) > 32) /* maximum 32 octets */
603 return 0;
604
605 /* octet 5-N */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800606 memcpy(cause->diag, lv + i, in_len - (i-1));
607 cause->diag_len = in_len - (i-1);
Harald Welte0c389302009-06-10 12:08:54 +0800608
609 return 0;
610}
611
612/* encode 'cause' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800613static int encode_cause(struct msgb *msg, int lv_only,
614 const struct gsm_mncc_cause *cause)
Harald Welte0c389302009-06-10 12:08:54 +0800615{
616 u_int8_t lv[32+4];
617 int i;
618
Harald Welte4bfdfe72009-06-10 23:11:52 +0800619 if (cause->diag_len > 32)
Harald Welte0c389302009-06-10 12:08:54 +0800620 return -EINVAL;
621
622 /* octet 3 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800623 lv[1] = cause->location;
624 lv[1] |= cause->coding << 5;
Harald Welte0c389302009-06-10 12:08:54 +0800625
626 i = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800627 if (cause->rec) {
Harald Welte0c389302009-06-10 12:08:54 +0800628 i++; /* octet 3a */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800629 lv[i] = cause->rec_val;
Harald Welte0c389302009-06-10 12:08:54 +0800630 }
631 lv[i] |= 0x80; /* end of octet 3 */
632
633 /* octet 4 */
634 i++;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800635 lv[i] = 0x80 | cause->value;
Harald Welte0c389302009-06-10 12:08:54 +0800636
637 /* octet 5-N */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800638 if (cause->diag_len) {
639 memcpy(lv + i, cause->diag, cause->diag_len);
640 i += cause->diag_len;
Harald Welte0c389302009-06-10 12:08:54 +0800641 }
642
643 lv[0] = i;
644 if (lv_only)
645 msgb_lv_put(msg, lv[0], lv+1);
646 else
647 msgb_tlv_put(msg, GSM48_IE_CAUSE, lv[0], lv+1);
648
649 return 0;
650}
651
652/* encode 'calling number' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800653static int encode_calling(struct msgb *msg,
654 const struct gsm_mncc_number *calling)
Harald Welte0c389302009-06-10 12:08:54 +0800655{
Harald Welte4bfdfe72009-06-10 23:11:52 +0800656 return encode_callerid(msg, GSM48_IE_CALLING_BCD, calling);
Harald Welte0c389302009-06-10 12:08:54 +0800657}
658
659/* encode 'connected number' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800660static int encode_connected(struct msgb *msg,
661 const struct gsm_mncc_number *connected)
Harald Welte0c389302009-06-10 12:08:54 +0800662{
Harald Welte4bfdfe72009-06-10 23:11:52 +0800663 return encode_callerid(msg, GSM48_IE_CONN_BCD, connected);
Harald Welte0c389302009-06-10 12:08:54 +0800664}
665
666/* encode 'redirecting number' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800667static int encode_redirecting(struct msgb *msg,
668 const struct gsm_mncc_number *redirecting)
Harald Welte0c389302009-06-10 12:08:54 +0800669{
Harald Welte4bfdfe72009-06-10 23:11:52 +0800670 return encode_callerid(msg, GSM48_IE_REDIR_BCD, redirecting);
Harald Welte0c389302009-06-10 12:08:54 +0800671}
672
673/* decode 'facility' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800674static int decode_facility(struct gsm_mncc_facility *facility,
Harald Welte0c389302009-06-10 12:08:54 +0800675 const u_int8_t *lv)
676{
677 u_int8_t in_len = lv[0];
678
679 if (in_len < 1)
680 return -EINVAL;
681
Harald Welte4bfdfe72009-06-10 23:11:52 +0800682 if (in_len > sizeof(facility->info))
Harald Welte0c389302009-06-10 12:08:54 +0800683 return -EINVAL;
684
Harald Welte4bfdfe72009-06-10 23:11:52 +0800685 memcpy(facility->info, lv+1, in_len);
686 facility->len = in_len;
Harald Welte0c389302009-06-10 12:08:54 +0800687
688 return 0;
689}
690
691/* encode 'facility' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800692static int encode_facility(struct msgb *msg, int lv_only,
693 const struct gsm_mncc_facility *facility)
Harald Welte0c389302009-06-10 12:08:54 +0800694{
695 u_int8_t lv[GSM_MAX_FACILITY + 1];
696
Harald Welte4bfdfe72009-06-10 23:11:52 +0800697 if (facility->len < 1 || facility->len > GSM_MAX_FACILITY)
Harald Welte0c389302009-06-10 12:08:54 +0800698 return -EINVAL;
699
Harald Welte4bfdfe72009-06-10 23:11:52 +0800700 memcpy(lv+1, facility->info, facility->len);
701 lv[0] = facility->len;
Harald Welte0c389302009-06-10 12:08:54 +0800702 if (lv_only)
703 msgb_lv_put(msg, lv[0], lv+1);
704 else
705 msgb_tlv_put(msg, GSM48_IE_FACILITY, lv[0], lv+1);
706
707 return 0;
708}
709
710/* decode 'notify' */
711static int decode_notify(int *notify, const u_int8_t *v)
712{
713 *notify = v[0] & 0x7f;
714
715 return 0;
716}
717
718/* encode 'notify' */
719static int encode_notify(struct msgb *msg, int notify)
720{
721 msgb_v_put(msg, notify | 0x80);
722
723 return 0;
724}
725
726/* encode 'signal' */
727static int encode_signal(struct msgb *msg, int signal)
728{
729 msgb_tv_put(msg, GSM48_IE_SIGNAL, signal);
730
731 return 0;
732}
733
734/* decode 'keypad' */
735static int decode_keypad(int *keypad, const u_int8_t *lv)
736{
737 u_int8_t in_len = lv[0];
738
739 if (in_len < 1)
740 return -EINVAL;
741
742 *keypad = lv[1] & 0x7f;
743
744 return 0;
745}
746
747/* encode 'keypad' */
748static int encode_keypad(struct msgb *msg, int keypad)
749{
750 msgb_tv_put(msg, GSM48_IE_KPD_FACILITY, keypad);
751
752 return 0;
753}
754
755/* decode 'progress' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800756static int decode_progress(struct gsm_mncc_progress *progress,
Harald Welte0c389302009-06-10 12:08:54 +0800757 const u_int8_t *lv)
758{
759 u_int8_t in_len = lv[0];
760
761 if (in_len < 2)
762 return -EINVAL;
763
Harald Welte4bfdfe72009-06-10 23:11:52 +0800764 progress->coding = (lv[1] & 0x60) >> 5;
765 progress->location = lv[1] & 0x0f;
766 progress->descr = lv[2] & 0x7f;
Harald Welte0c389302009-06-10 12:08:54 +0800767
768 return 0;
769}
770
771/* encode 'progress' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800772static int encode_progress(struct msgb *msg, int lv_only,
773 const struct gsm_mncc_progress *p)
Harald Welte0c389302009-06-10 12:08:54 +0800774{
775 u_int8_t lv[3];
776
777 lv[0] = 2;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800778 lv[1] = 0x80 | ((p->coding & 0x3) << 5) | (p->location & 0xf);
779 lv[2] = 0x80 | (p->descr & 0x7f);
Harald Welte0c389302009-06-10 12:08:54 +0800780 if (lv_only)
781 msgb_lv_put(msg, lv[0], lv+1);
782 else
783 msgb_tlv_put(msg, GSM48_IE_PROGR_IND, lv[0], lv+1);
784
785 return 0;
786}
787
788/* decode 'user-user' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800789static int decode_useruser(struct gsm_mncc_useruser *uu,
Harald Welte0c389302009-06-10 12:08:54 +0800790 const u_int8_t *lv)
791{
792 u_int8_t in_len = lv[0];
Harald Welte4bfdfe72009-06-10 23:11:52 +0800793 char *info = uu->info;
794 int info_len = sizeof(uu->info);
Harald Welte0c389302009-06-10 12:08:54 +0800795 int i;
796
797 if (in_len < 1)
798 return -EINVAL;
799
Harald Welte4bfdfe72009-06-10 23:11:52 +0800800 uu->proto = lv[1];
Harald Welte0c389302009-06-10 12:08:54 +0800801
802 for (i = 2; i <= in_len; i++) {
803 info_len--;
804 if (info_len <= 1)
805 break;
806 *info++ = lv[i];
807 }
808 if (info_len >= 1)
809 *info++ = '\0';
810
811 return 0;
812}
813
814/* encode 'useruser' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800815static int encode_useruser(struct msgb *msg, int lv_only,
816 const struct gsm_mncc_useruser *uu)
Harald Welte0c389302009-06-10 12:08:54 +0800817{
818 u_int8_t lv[GSM_MAX_USERUSER + 2];
819
Harald Welte4bfdfe72009-06-10 23:11:52 +0800820 if (strlen(uu->info) > GSM_MAX_USERUSER)
Harald Welte0c389302009-06-10 12:08:54 +0800821 return -EINVAL;
822
Harald Welte4bfdfe72009-06-10 23:11:52 +0800823 lv[0] = 1 + strlen(uu->info);
824 lv[1] = uu->proto;
825 memcpy(lv + 2, uu->info, strlen(uu->info));
Harald Welte0c389302009-06-10 12:08:54 +0800826 if (lv_only)
827 msgb_lv_put(msg, lv[0], lv+1);
828 else
829 msgb_tlv_put(msg, GSM48_IE_USER_USER, lv[0], lv+1);
830
831 return 0;
832}
833
834/* decode 'ss version' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800835static int decode_ssversion(struct gsm_mncc_ssversion *ssv,
Harald Welte0c389302009-06-10 12:08:54 +0800836 const u_int8_t *lv)
837{
838 u_int8_t in_len = lv[0];
839
Harald Welte4bfdfe72009-06-10 23:11:52 +0800840 if (in_len < 1 || in_len < sizeof(ssv->info))
Harald Welte0c389302009-06-10 12:08:54 +0800841 return -EINVAL;
842
Harald Welte4bfdfe72009-06-10 23:11:52 +0800843 memcpy(ssv->info, lv + 1, in_len);
844 ssv->len = in_len;
Harald Welte0c389302009-06-10 12:08:54 +0800845
846 return 0;
847}
848
849/* encode 'more data' */
850static int encode_more(struct msgb *msg)
851{
852 u_int8_t *ie;
853
854 ie = msgb_put(msg, 1);
855 ie[0] = GSM48_IE_MORE_DATA;
856
857 return 0;
858}
859
Holger Freyther429e7762008-12-30 13:28:30 +0000860/* Chapter 9.2.14 : Send LOCATION UPDATING REJECT */
Harald Welte8470bf22008-12-25 23:28:35 +0000861int gsm0408_loc_upd_rej(struct gsm_lchan *lchan, u_int8_t cause)
Harald Welte52b1f982008-12-23 20:25:15 +0000862{
Harald Welte8470bf22008-12-25 23:28:35 +0000863 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000864 struct gsm48_hdr *gh;
865
Harald Welte8470bf22008-12-25 23:28:35 +0000866 msg->lchan = lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000867
868 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
869 gh->proto_discr = GSM48_PDISC_MM;
Harald Welte10b487b2008-12-27 19:53:37 +0000870 gh->msg_type = GSM48_MT_MM_LOC_UPD_REJECT;
Harald Welte52b1f982008-12-23 20:25:15 +0000871 gh->data[0] = cause;
872
Harald Weltedb253af2008-12-30 17:56:55 +0000873 DEBUGP(DMM, "-> LOCATION UPDATING REJECT on channel: %d\n", lchan->nr);
874
Harald Welte39e2ead2009-07-23 21:13:03 +0200875 return gsm48_sendmsg(msg, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000876}
877
878/* Chapter 9.2.13 : Send LOCATION UPDATE ACCEPT */
Harald Welte75a983f2008-12-27 21:34:06 +0000879int gsm0408_loc_upd_acc(struct gsm_lchan *lchan, u_int32_t tmsi)
Harald Welte52b1f982008-12-23 20:25:15 +0000880{
Harald Welte8470bf22008-12-25 23:28:35 +0000881 struct gsm_bts *bts = lchan->ts->trx->bts;
882 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000883 struct gsm48_hdr *gh;
884 struct gsm48_loc_area_id *lai;
885 u_int8_t *mid;
Holger Freyther07cc8d82008-12-29 06:23:46 +0000886 int ret;
Harald Welte52b1f982008-12-23 20:25:15 +0000887
Harald Welte8470bf22008-12-25 23:28:35 +0000888 msg->lchan = lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000889
890 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
891 gh->proto_discr = GSM48_PDISC_MM;
892 gh->msg_type = GSM48_MT_MM_LOC_UPD_ACCEPT;
893
894 lai = (struct gsm48_loc_area_id *) msgb_put(msg, sizeof(*lai));
Holger Freyther17746612008-12-28 16:32:44 +0000895 gsm0408_generate_lai(lai, bts->network->country_code,
Harald Welte52b1f982008-12-23 20:25:15 +0000896 bts->network->network_code, bts->location_area_code);
897
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +0200898 mid = msgb_put(msg, GSM48_MID_TMSI_LEN);
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +0200899 gsm48_generate_mid_from_tmsi(mid, tmsi);
Harald Welte52b1f982008-12-23 20:25:15 +0000900
901 DEBUGP(DMM, "-> LOCATION UPDATE ACCEPT\n");
902
Harald Welte39e2ead2009-07-23 21:13:03 +0200903 ret = gsm48_sendmsg(msg, NULL);
Harald Weltedb253af2008-12-30 17:56:55 +0000904
Harald Welteee5ad162009-08-09 19:07:00 +0200905 /* send MM INFO with network name */
Harald Weltedb253af2008-12-30 17:56:55 +0000906 ret = gsm48_tx_mm_info(lchan);
Harald Weltedb253af2008-12-30 17:56:55 +0000907
Holger Freyther07cc8d82008-12-29 06:23:46 +0000908 return ret;
Harald Welte52b1f982008-12-23 20:25:15 +0000909}
910
Harald Weltebf5e8df2009-02-03 12:59:45 +0000911/* Transmit Chapter 9.2.10 Identity Request */
Harald Welte231ad4f2008-12-27 11:15:38 +0000912static int mm_tx_identity_req(struct gsm_lchan *lchan, u_int8_t id_type)
913{
914 struct msgb *msg = gsm48_msgb_alloc();
915 struct gsm48_hdr *gh;
Harald Weltefc977a82008-12-27 10:19:37 +0000916
Harald Welte231ad4f2008-12-27 11:15:38 +0000917 msg->lchan = lchan;
918
919 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
920 gh->proto_discr = GSM48_PDISC_MM;
921 gh->msg_type = GSM48_MT_MM_ID_REQ;
922 gh->data[0] = id_type;
923
Harald Welte39e2ead2009-07-23 21:13:03 +0200924 return gsm48_sendmsg(msg, NULL);
Harald Welte231ad4f2008-12-27 11:15:38 +0000925}
926
Harald Welte231ad4f2008-12-27 11:15:38 +0000927
Harald Weltebf5e8df2009-02-03 12:59:45 +0000928/* Parse Chapter 9.2.11 Identity Response */
Harald Welte231ad4f2008-12-27 11:15:38 +0000929static int mm_rx_id_resp(struct msgb *msg)
930{
931 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte75a983f2008-12-27 21:34:06 +0000932 struct gsm_lchan *lchan = msg->lchan;
Harald Welte9176bd42009-07-23 18:46:00 +0200933 struct gsm_bts *bts = lchan->ts->trx->bts;
934 struct gsm_network *net = bts->network;
Harald Welte231ad4f2008-12-27 11:15:38 +0000935 u_int8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200936 char mi_string[GSM48_MI_SIZE];
Harald Welte231ad4f2008-12-27 11:15:38 +0000937
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200938 gsm48_mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
Harald Welte61253062008-12-27 11:25:50 +0000939 DEBUGP(DMM, "IDENTITY RESPONSE: mi_type=0x%02x MI(%s)\n",
Harald Welte231ad4f2008-12-27 11:15:38 +0000940 mi_type, mi_string);
941
Harald Welte75a983f2008-12-27 21:34:06 +0000942 switch (mi_type) {
943 case GSM_MI_TYPE_IMSI:
Jan Luebbe370b41d2009-08-12 10:19:34 +0200944 /* look up subscriber based on IMSI, create if not found */
945 if (!lchan->subscr) {
946 lchan->subscr = subscr_get_by_imsi(net, mi_string);
Harald Welte (local)aa9dc192009-08-13 13:49:51 +0200947 if (!lchan->subscr)
948 lchan->subscr = db_create_subscriber(net, mi_string);
Jan Luebbeb0dfc312009-08-12 10:12:52 +0200949 }
Holger Freyther73487a22008-12-31 18:53:57 +0000950 if (lchan->loc_operation)
951 lchan->loc_operation->waiting_for_imsi = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000952 break;
953 case GSM_MI_TYPE_IMEI:
Harald Welte255539c2008-12-28 02:26:27 +0000954 case GSM_MI_TYPE_IMEISV:
Harald Welte75a983f2008-12-27 21:34:06 +0000955 /* update subscribe <-> IMEI mapping */
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200956 if (lchan->subscr) {
Harald Welte75a983f2008-12-27 21:34:06 +0000957 db_subscriber_assoc_imei(lchan->subscr, mi_string);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200958 db_sync_equipment(&lchan->subscr->equipment);
959 }
Holger Freyther73487a22008-12-31 18:53:57 +0000960 if (lchan->loc_operation)
961 lchan->loc_operation->waiting_for_imei = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000962 break;
963 }
Holger Freyther73487a22008-12-31 18:53:57 +0000964
965 /* Check if we can let the mobile station enter */
Holger Freytherd51524f2009-06-09 08:27:07 +0000966 return gsm0408_authorize(lchan, msg);
Harald Welte231ad4f2008-12-27 11:15:38 +0000967}
968
Harald Welte255539c2008-12-28 02:26:27 +0000969
970static void loc_upd_rej_cb(void *data)
971{
972 struct gsm_lchan *lchan = data;
973
Holger Freyther73487a22008-12-31 18:53:57 +0000974 release_loc_updating_req(lchan);
Holger Freythere97f7fb2008-12-31 18:52:11 +0000975 gsm0408_loc_upd_rej(lchan, reject_cause);
Holger Freyther67b4b9a2009-01-01 03:46:11 +0000976 lchan_auto_release(lchan);
Harald Welte255539c2008-12-28 02:26:27 +0000977}
978
Holger Freytherb7193e42008-12-29 17:44:08 +0000979static void schedule_reject(struct gsm_lchan *lchan)
980{
Holger Freyther73487a22008-12-31 18:53:57 +0000981 lchan->loc_operation->updating_timer.cb = loc_upd_rej_cb;
982 lchan->loc_operation->updating_timer.data = lchan;
Harald Welteff117a82009-05-23 05:22:08 +0000983 bsc_schedule_timer(&lchan->loc_operation->updating_timer, 5, 0);
Holger Freytherb7193e42008-12-29 17:44:08 +0000984}
985
Harald Welte2a139372009-02-22 21:14:55 +0000986static const char *lupd_name(u_int8_t type)
987{
988 switch (type) {
989 case GSM48_LUPD_NORMAL:
990 return "NORMAL";
991 case GSM48_LUPD_PERIODIC:
992 return "PEROIDOC";
993 case GSM48_LUPD_IMSI_ATT:
994 return "IMSI ATTACH";
995 default:
996 return "UNKNOWN";
997 }
998}
999
Harald Weltebf5e8df2009-02-03 12:59:45 +00001000/* Chapter 9.2.15: Receive Location Updating Request */
Harald Welte231ad4f2008-12-27 11:15:38 +00001001static int mm_rx_loc_upd_req(struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001002{
Harald Welte8470bf22008-12-25 23:28:35 +00001003 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001004 struct gsm48_loc_upd_req *lu;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001005 struct gsm_subscriber *subscr = NULL;
Harald Welte255539c2008-12-28 02:26:27 +00001006 struct gsm_lchan *lchan = msg->lchan;
Harald Welte9176bd42009-07-23 18:46:00 +02001007 struct gsm_bts *bts = lchan->ts->trx->bts;
Harald Welte8470bf22008-12-25 23:28:35 +00001008 u_int8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001009 char mi_string[GSM48_MI_SIZE];
Harald Welte231ad4f2008-12-27 11:15:38 +00001010 int rc;
Harald Welte52b1f982008-12-23 20:25:15 +00001011
Harald Welte8470bf22008-12-25 23:28:35 +00001012 lu = (struct gsm48_loc_upd_req *) gh->data;
1013
1014 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
Harald Welte52b1f982008-12-23 20:25:15 +00001015
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001016 gsm48_mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
Harald Weltefc977a82008-12-27 10:19:37 +00001017
Harald Weltea0368542009-06-27 02:58:43 +02001018 DEBUGPC(DMM, "mi_type=0x%02x MI(%s) type=%s ", mi_type, mi_string,
Harald Welte2a139372009-02-22 21:14:55 +00001019 lupd_name(lu->type));
Holger Freyther73487a22008-12-31 18:53:57 +00001020
Holger Freythereaf04692009-06-06 13:54:44 +00001021 /*
1022 * Pseudo Spoof detection: Just drop a second/concurrent
1023 * location updating request.
1024 */
1025 if (lchan->loc_operation) {
Harald Weltea0368542009-06-27 02:58:43 +02001026 DEBUGPC(DMM, "ignoring request due an existing one: %p.\n",
Holger Freythereaf04692009-06-06 13:54:44 +00001027 lchan->loc_operation);
1028 gsm0408_loc_upd_rej(lchan, GSM48_REJECT_PROTOCOL_ERROR);
1029 return 0;
1030 }
1031
Holger Freyther73487a22008-12-31 18:53:57 +00001032 allocate_loc_updating_req(lchan);
1033
Harald Welte52b1f982008-12-23 20:25:15 +00001034 switch (mi_type) {
1035 case GSM_MI_TYPE_IMSI:
Harald Weltea0368542009-06-27 02:58:43 +02001036 DEBUGPC(DMM, "\n");
Harald Welte231ad4f2008-12-27 11:15:38 +00001037 /* we always want the IMEI, too */
Harald Welte015b9ad2009-02-28 18:22:03 +00001038 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMEI);
Holger Freyther73487a22008-12-31 18:53:57 +00001039 lchan->loc_operation->waiting_for_imei = 1;
Holger Freytherc6ea9db2008-12-30 19:18:21 +00001040
Jan Luebbe370b41d2009-08-12 10:19:34 +02001041 /* look up subscriber based on IMSI, create if not found */
1042 subscr = subscr_get_by_imsi(bts->network, mi_string);
1043 if (!subscr) {
1044 subscr = db_create_subscriber(bts->network, mi_string);
1045 }
Harald Welte4b634542008-12-27 01:55:51 +00001046 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001047 case GSM_MI_TYPE_TMSI:
Harald Weltea0368542009-06-27 02:58:43 +02001048 DEBUGPC(DMM, "\n");
Harald Welte231ad4f2008-12-27 11:15:38 +00001049 /* we always want the IMEI, too */
Harald Welte015b9ad2009-02-28 18:22:03 +00001050 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMEI);
Holger Freyther73487a22008-12-31 18:53:57 +00001051 lchan->loc_operation->waiting_for_imei = 1;
Holger Freytherc6ea9db2008-12-30 19:18:21 +00001052
Harald Welte52b1f982008-12-23 20:25:15 +00001053 /* look up the subscriber based on TMSI, request IMSI if it fails */
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +02001054 subscr = subscr_get_by_tmsi(bts->network,
1055 tmsi_from_string(mi_string));
Harald Welte52b1f982008-12-23 20:25:15 +00001056 if (!subscr) {
Harald Welte231ad4f2008-12-27 11:15:38 +00001057 /* send IDENTITY REQUEST message to get IMSI */
Harald Welte255539c2008-12-28 02:26:27 +00001058 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMSI);
Holger Freyther73487a22008-12-31 18:53:57 +00001059 lchan->loc_operation->waiting_for_imsi = 1;
Harald Welte52b1f982008-12-23 20:25:15 +00001060 }
1061 break;
1062 case GSM_MI_TYPE_IMEI:
1063 case GSM_MI_TYPE_IMEISV:
1064 /* no sim card... FIXME: what to do ? */
Harald Weltea0368542009-06-27 02:58:43 +02001065 DEBUGPC(DMM, "unimplemented mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +00001066 break;
1067 default:
Harald Weltea0368542009-06-27 02:58:43 +02001068 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +00001069 break;
1070 }
1071
Harald Welte24516ea2009-07-04 10:18:00 +02001072 /* schedule the reject timer */
1073 schedule_reject(lchan);
1074
Harald Welte4bfdfe72009-06-10 23:11:52 +08001075 if (!subscr) {
Harald Weltea0368542009-06-27 02:58:43 +02001076 DEBUGPC(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001077 /* FIXME: request id? close channel? */
1078 return -EINVAL;
1079 }
1080
Harald Welte255539c2008-12-28 02:26:27 +00001081 lchan->subscr = subscr;
Harald Welte (local)ee4410a2009-08-17 09:39:55 +02001082 lchan->subscr->equipment.classmark1 = lu->classmark1;
Harald Welte255539c2008-12-28 02:26:27 +00001083
Harald Welte24516ea2009-07-04 10:18:00 +02001084 /* check if we can let the subscriber into our network immediately
1085 * or if we need to wait for identity responses. */
Holger Freytherd51524f2009-06-09 08:27:07 +00001086 return gsm0408_authorize(lchan, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001087}
1088
Harald Welte13cac662009-07-29 12:10:35 +02001089/* 9.1.5 Channel mode modify: Modify the mode on the MS side */
Harald Welte7584aea2009-02-11 11:44:12 +00001090int gsm48_tx_chan_mode_modify(struct gsm_lchan *lchan, u_int8_t mode)
1091{
1092 struct msgb *msg = gsm48_msgb_alloc();
1093 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1094 struct gsm48_chan_mode_modify *cmm =
1095 (struct gsm48_chan_mode_modify *) msgb_put(msg, sizeof(*cmm));
Harald Welte4a543e82009-02-28 13:17:55 +00001096 u_int16_t arfcn = lchan->ts->trx->arfcn & 0x3ff;
Harald Welte7584aea2009-02-11 11:44:12 +00001097
Harald Welte4a543e82009-02-28 13:17:55 +00001098 DEBUGP(DRR, "-> CHANNEL MODE MODIFY mode=0x%02x\n", mode);
Harald Welte7ccf7782009-02-17 01:43:01 +00001099
Harald Welte45b407a2009-05-23 15:51:12 +00001100 lchan->tch_mode = mode;
Harald Welte7584aea2009-02-11 11:44:12 +00001101 msg->lchan = lchan;
1102 gh->proto_discr = GSM48_PDISC_RR;
1103 gh->msg_type = GSM48_MT_RR_CHAN_MODE_MODIF;
1104
1105 /* fill the channel information element, this code
1106 * should probably be shared with rsl_rx_chan_rqd() */
1107 cmm->chan_desc.chan_nr = lchan2chan_nr(lchan);
Harald Welte02b0e092009-02-28 13:11:07 +00001108 cmm->chan_desc.h0.tsc = lchan->ts->trx->bts->tsc;
Harald Welte7584aea2009-02-11 11:44:12 +00001109 cmm->chan_desc.h0.h = 0;
1110 cmm->chan_desc.h0.arfcn_high = arfcn >> 8;
1111 cmm->chan_desc.h0.arfcn_low = arfcn & 0xff;
1112 cmm->mode = mode;
1113
Harald Welte39e2ead2009-07-23 21:13:03 +02001114 return gsm48_sendmsg(msg, NULL);
Harald Welte7584aea2009-02-11 11:44:12 +00001115}
1116
Harald Welte4bfdfe72009-06-10 23:11:52 +08001117#if 0
1118static u_int8_t to_bcd8(u_int8_t val)
1119{
1120 return ((val / 10) << 4) | (val % 10);
1121}
1122#endif
1123
Harald Weltedb253af2008-12-30 17:56:55 +00001124/* Section 9.2.15a */
1125int gsm48_tx_mm_info(struct gsm_lchan *lchan)
1126{
1127 struct msgb *msg = gsm48_msgb_alloc();
1128 struct gsm48_hdr *gh;
1129 struct gsm_network *net = lchan->ts->trx->bts->network;
Harald Weltedb253af2008-12-30 17:56:55 +00001130 u_int8_t *ptr8;
1131 u_int16_t *ptr16;
Daniel Willmanneea93372009-08-13 03:42:07 +02001132 int name_len, name_pad;
Harald Weltedb253af2008-12-30 17:56:55 +00001133 int i;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001134#if 0
1135 time_t cur_t;
1136 struct tm* cur_time;
1137 int tz15min;
1138#endif
Harald Weltedb253af2008-12-30 17:56:55 +00001139
1140 msg->lchan = lchan;
1141
1142 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1143 gh->proto_discr = GSM48_PDISC_MM;
1144 gh->msg_type = GSM48_MT_MM_INFO;
1145
1146 if (net->name_long) {
Daniel Willmanneea93372009-08-13 03:42:07 +02001147#if 0
Harald Weltedb253af2008-12-30 17:56:55 +00001148 name_len = strlen(net->name_long);
1149 /* 10.5.3.5a */
1150 ptr8 = msgb_put(msg, 3);
1151 ptr8[0] = GSM48_IE_NAME_LONG;
1152 ptr8[1] = name_len*2 +1;
1153 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
1154
1155 ptr16 = (u_int16_t *) msgb_put(msg, name_len*2);
1156 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +00001157 ptr16[i] = htons(net->name_long[i]);
Harald Weltedb253af2008-12-30 17:56:55 +00001158
1159 /* FIXME: Use Cell Broadcast, not UCS-2, since
1160 * UCS-2 is only supported by later revisions of the spec */
Daniel Willmanneea93372009-08-13 03:42:07 +02001161#endif
1162 name_len = (strlen(net->name_long)*7)/8;
1163 name_pad = (8 - strlen(net->name_long)*7)%8;
1164 if (name_pad > 0)
1165 name_len++;
1166 /* 10.5.3.5a */
1167 ptr8 = msgb_put(msg, 3);
1168 ptr8[0] = GSM48_IE_NAME_LONG;
1169 ptr8[1] = name_len +1;
1170 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
1171
1172 ptr8 = msgb_put(msg, name_len);
1173 gsm_7bit_encode(ptr8, net->name_long);
1174
Harald Weltedb253af2008-12-30 17:56:55 +00001175 }
1176
1177 if (net->name_short) {
Daniel Willmanneea93372009-08-13 03:42:07 +02001178#if 0
Harald Weltedb253af2008-12-30 17:56:55 +00001179 name_len = strlen(net->name_short);
1180 /* 10.5.3.5a */
1181 ptr8 = (u_int8_t *) msgb_put(msg, 3);
Harald Welte7543eb72009-07-19 17:51:36 +02001182 ptr8[0] = GSM48_IE_NAME_SHORT;
Harald Weltedb253af2008-12-30 17:56:55 +00001183 ptr8[1] = name_len*2 + 1;
1184 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
1185
Harald Weltee872cb12009-01-01 00:33:37 +00001186 ptr16 = (u_int16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +00001187 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +00001188 ptr16[i] = htons(net->name_short[i]);
Daniel Willmanneea93372009-08-13 03:42:07 +02001189#endif
1190 name_len = (strlen(net->name_short)*7)/8;
1191 name_pad = (8 - strlen(net->name_short)*7)%8;
1192 if (name_pad > 0)
1193 name_len++;
1194 /* 10.5.3.5a */
1195 ptr8 = (u_int8_t *) msgb_put(msg, 3);
1196 ptr8[0] = GSM48_IE_NAME_SHORT;
1197 ptr8[1] = name_len +1;
1198 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
1199
1200 ptr8 = msgb_put(msg, name_len);
1201 gsm_7bit_encode(ptr8, net->name_short);
1202
Harald Weltedb253af2008-12-30 17:56:55 +00001203 }
1204
1205#if 0
1206 /* Section 10.5.3.9 */
1207 cur_t = time(NULL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001208 cur_time = gmtime(&cur_t);
Harald Weltedb253af2008-12-30 17:56:55 +00001209 ptr8 = msgb_put(msg, 8);
1210 ptr8[0] = GSM48_IE_NET_TIME_TZ;
1211 ptr8[1] = to_bcd8(cur_time->tm_year % 100);
1212 ptr8[2] = to_bcd8(cur_time->tm_mon);
1213 ptr8[3] = to_bcd8(cur_time->tm_mday);
1214 ptr8[4] = to_bcd8(cur_time->tm_hour);
1215 ptr8[5] = to_bcd8(cur_time->tm_min);
1216 ptr8[6] = to_bcd8(cur_time->tm_sec);
1217 /* 02.42: coded as BCD encoded signed value in units of 15 minutes */
1218 tz15min = (cur_time->tm_gmtoff)/(60*15);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001219 ptr8[7] = to_bcd8(tz15min);
Harald Weltedb253af2008-12-30 17:56:55 +00001220 if (tz15min < 0)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001221 ptr8[7] |= 0x80;
Harald Weltedb253af2008-12-30 17:56:55 +00001222#endif
1223
Daniel Willmanneea93372009-08-13 03:42:07 +02001224 DEBUGP(DMM, "-> MM INFO\n");
1225
Harald Welte39e2ead2009-07-23 21:13:03 +02001226 return gsm48_sendmsg(msg, NULL);
Harald Weltedb253af2008-12-30 17:56:55 +00001227}
1228
Harald Welte7984d5c2009-08-12 22:56:50 +02001229/* Section 9.2.2 */
1230int gsm48_tx_mm_auth_req(struct gsm_lchan *lchan, u_int8_t *rand)
1231{
1232 struct msgb *msg = gsm48_msgb_alloc();
1233 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Sylvain Munaut849f5542009-09-27 11:10:17 +02001234 struct gsm48_auth_req *ar = (struct gsm48_auth_req *) msgb_put(msg, sizeof(*ar));
Harald Welte7984d5c2009-08-12 22:56:50 +02001235
1236 DEBUGP(DMM, "-> AUTH REQ\n");
1237
1238 msg->lchan = lchan;
1239 gh->proto_discr = GSM48_PDISC_MM;
1240 gh->msg_type = GSM48_MT_MM_AUTH_REQ;
1241
Sylvain Munaut849f5542009-09-27 11:10:17 +02001242 /* Key Sequence: FIXME fixed to 0 */
1243 ar->key_seq = 0;
1244
Harald Welte7984d5c2009-08-12 22:56:50 +02001245 /* 16 bytes RAND parameters */
Harald Welte7984d5c2009-08-12 22:56:50 +02001246 if (rand)
Sylvain Munaut849f5542009-09-27 11:10:17 +02001247 memcpy(ar->rand, rand, 16);
Harald Welte7984d5c2009-08-12 22:56:50 +02001248
1249 return gsm48_sendmsg(msg, NULL);
1250}
1251
1252/* Section 9.2.1 */
1253int gsm48_tx_mm_auth_rej(struct gsm_lchan *lchan)
1254{
1255 DEBUGP(DMM, "-> AUTH REJECT\n");
1256 return gsm48_tx_simple(lchan, GSM48_PDISC_MM, GSM48_MT_MM_AUTH_REJ);
1257}
1258
Harald Welte4b634542008-12-27 01:55:51 +00001259static int gsm48_tx_mm_serv_ack(struct gsm_lchan *lchan)
1260{
Harald Welte4b634542008-12-27 01:55:51 +00001261 DEBUGP(DMM, "-> CM SERVICE ACK\n");
Harald Welte65e74cc2008-12-29 01:55:35 +00001262 return gsm48_tx_simple(lchan, GSM48_PDISC_MM, GSM48_MT_MM_CM_SERV_ACC);
Harald Welte4b634542008-12-27 01:55:51 +00001263}
Harald Welteba4cf162009-01-10 01:49:35 +00001264
1265/* 9.2.6 CM service reject */
1266static int gsm48_tx_mm_serv_rej(struct gsm_lchan *lchan,
1267 enum gsm48_reject_value value)
1268{
1269 struct msgb *msg = gsm48_msgb_alloc();
1270 struct gsm48_hdr *gh;
1271
1272 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
1273
1274 msg->lchan = lchan;
1275 use_lchan(lchan);
1276
1277 gh->proto_discr = GSM48_PDISC_MM;
1278 gh->msg_type = GSM48_MT_MM_CM_SERV_REJ;
1279 gh->data[0] = value;
1280 DEBUGP(DMM, "-> CM SERVICE Reject cause: %d\n", value);
1281
Harald Welte39e2ead2009-07-23 21:13:03 +02001282 return gsm48_sendmsg(msg, NULL);
Harald Welteba4cf162009-01-10 01:49:35 +00001283}
1284
Harald Welte4ed0e922009-01-10 03:17:30 +00001285/*
1286 * Handle CM Service Requests
1287 * a) Verify that the packet is long enough to contain the information
1288 * we require otherwsie reject with INCORRECT_MESSAGE
1289 * b) Try to parse the TMSI. If we do not have one reject
1290 * c) Check that we know the subscriber with the TMSI otherwise reject
1291 * with a HLR cause
1292 * d) Set the subscriber on the gsm_lchan and accept
1293 */
Harald Welte4b634542008-12-27 01:55:51 +00001294static int gsm48_rx_mm_serv_req(struct msgb *msg)
1295{
Harald Welteba4cf162009-01-10 01:49:35 +00001296 u_int8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001297 char mi_string[GSM48_MI_SIZE];
Harald Welte4b634542008-12-27 01:55:51 +00001298
Harald Welte9176bd42009-07-23 18:46:00 +02001299 struct gsm_bts *bts = msg->lchan->ts->trx->bts;
Harald Welteba4cf162009-01-10 01:49:35 +00001300 struct gsm_subscriber *subscr;
1301 struct gsm48_hdr *gh = msgb_l3(msg);
1302 struct gsm48_service_request *req =
1303 (struct gsm48_service_request *)gh->data;
Harald Weltec9e02182009-05-01 19:07:53 +00001304 /* unfortunately in Phase1 the classmar2 length is variable */
1305 u_int8_t classmark2_len = gh->data[1];
1306 u_int8_t *classmark2 = gh->data+2;
1307 u_int8_t mi_len = *(classmark2 + classmark2_len);
1308 u_int8_t *mi = (classmark2 + classmark2_len + 1);
Harald Welteba4cf162009-01-10 01:49:35 +00001309
Harald Weltec9e02182009-05-01 19:07:53 +00001310 DEBUGP(DMM, "<- CM SERVICE REQUEST ");
Harald Welteba4cf162009-01-10 01:49:35 +00001311 if (msg->data_len < sizeof(struct gsm48_service_request*)) {
Harald Weltec9e02182009-05-01 19:07:53 +00001312 DEBUGPC(DMM, "wrong sized message\n");
Harald Welteba4cf162009-01-10 01:49:35 +00001313 return gsm48_tx_mm_serv_rej(msg->lchan,
1314 GSM48_REJECT_INCORRECT_MESSAGE);
1315 }
1316
1317 if (msg->data_len < req->mi_len + 6) {
Harald Weltec9e02182009-05-01 19:07:53 +00001318 DEBUGPC(DMM, "does not fit in packet\n");
Harald Welteba4cf162009-01-10 01:49:35 +00001319 return gsm48_tx_mm_serv_rej(msg->lchan,
1320 GSM48_REJECT_INCORRECT_MESSAGE);
1321 }
1322
Harald Weltec9e02182009-05-01 19:07:53 +00001323 mi_type = mi[0] & GSM_MI_TYPE_MASK;
Harald Welteba4cf162009-01-10 01:49:35 +00001324 if (mi_type != GSM_MI_TYPE_TMSI) {
Harald Weltec9e02182009-05-01 19:07:53 +00001325 DEBUGPC(DMM, "mi_type is not TMSI: %d\n", mi_type);
Harald Welteba4cf162009-01-10 01:49:35 +00001326 return gsm48_tx_mm_serv_rej(msg->lchan,
1327 GSM48_REJECT_INCORRECT_MESSAGE);
1328 }
1329
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001330 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
Harald Weltec9e02182009-05-01 19:07:53 +00001331 DEBUGPC(DMM, "serv_type=0x%02x mi_type=0x%02x M(%s)\n",
Harald Welte4ed0e922009-01-10 03:17:30 +00001332 req->cm_service_type, mi_type, mi_string);
Harald Weltebcae43f2008-12-27 21:45:37 +00001333
Harald Welte3ac7f102009-08-10 10:12:45 +02001334 if (is_siemens_bts(bts))
1335 send_siemens_mrpci(msg->lchan, classmark2-1);
1336
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +02001337 subscr = subscr_get_by_tmsi(bts->network,
1338 tmsi_from_string(mi_string));
Holger Freythereb443982009-06-04 13:58:42 +00001339
Harald Welte2a139372009-02-22 21:14:55 +00001340 /* FIXME: if we don't know the TMSI, inquire abit IMSI and allocate new TMSI */
Harald Welte4ed0e922009-01-10 03:17:30 +00001341 if (!subscr)
1342 return gsm48_tx_mm_serv_rej(msg->lchan,
1343 GSM48_REJECT_IMSI_UNKNOWN_IN_HLR);
1344
1345 if (!msg->lchan->subscr)
1346 msg->lchan->subscr = subscr;
Harald Welte9bb7c702009-01-10 03:21:41 +00001347 else if (msg->lchan->subscr != subscr) {
1348 DEBUGP(DMM, "<- CM Channel already owned by someone else?\n");
1349 subscr_put(subscr);
1350 }
1351
Harald Weltec2e302d2009-07-05 14:08:13 +02001352 subscr->equipment.classmark2_len = classmark2_len;
1353 memcpy(subscr->equipment.classmark2, classmark2, classmark2_len);
1354 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +00001355
Harald Welte4b634542008-12-27 01:55:51 +00001356 return gsm48_tx_mm_serv_ack(msg->lchan);
1357}
1358
Harald Welte2a139372009-02-22 21:14:55 +00001359static int gsm48_rx_mm_imsi_detach_ind(struct msgb *msg)
1360{
Harald Welte9176bd42009-07-23 18:46:00 +02001361 struct gsm_bts *bts = msg->lchan->ts->trx->bts;
Harald Welte2a139372009-02-22 21:14:55 +00001362 struct gsm48_hdr *gh = msgb_l3(msg);
1363 struct gsm48_imsi_detach_ind *idi =
1364 (struct gsm48_imsi_detach_ind *) gh->data;
1365 u_int8_t mi_type = idi->mi[0] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001366 char mi_string[GSM48_MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +08001367 struct gsm_subscriber *subscr = NULL;
Harald Welte2a139372009-02-22 21:14:55 +00001368
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001369 gsm48_mi_to_string(mi_string, sizeof(mi_string), idi->mi, idi->mi_len);
Harald Welte2a139372009-02-22 21:14:55 +00001370 DEBUGP(DMM, "IMSI DETACH INDICATION: mi_type=0x%02x MI(%s): ",
1371 mi_type, mi_string);
1372
1373 switch (mi_type) {
1374 case GSM_MI_TYPE_TMSI:
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +02001375 subscr = subscr_get_by_tmsi(bts->network,
1376 tmsi_from_string(mi_string));
Harald Welte2a139372009-02-22 21:14:55 +00001377 break;
1378 case GSM_MI_TYPE_IMSI:
Harald Welte9176bd42009-07-23 18:46:00 +02001379 subscr = subscr_get_by_imsi(bts->network, mi_string);
Harald Welte2a139372009-02-22 21:14:55 +00001380 break;
1381 case GSM_MI_TYPE_IMEI:
1382 case GSM_MI_TYPE_IMEISV:
1383 /* no sim card... FIXME: what to do ? */
Holger Freyther79f4ae62009-06-02 03:25:04 +00001384 DEBUGPC(DMM, "unimplemented mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +00001385 break;
1386 default:
Holger Freyther79f4ae62009-06-02 03:25:04 +00001387 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +00001388 break;
1389 }
1390
Holger Freyther4a49e772009-04-12 05:37:29 +00001391 if (subscr) {
1392 subscr_update(subscr, msg->trx->bts,
1393 GSM_SUBSCRIBER_UPDATE_DETACHED);
Harald Welte2a139372009-02-22 21:14:55 +00001394 DEBUGP(DMM, "Subscriber: %s\n",
1395 subscr->name ? subscr->name : subscr->imsi);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +02001396
1397 subscr->equipment.classmark1 = idi->classmark1;
1398 db_sync_equipment(&subscr->equipment);
1399
Holger Freytherc21cfbc2009-06-02 02:54:57 +00001400 subscr_put(subscr);
Holger Freyther4a49e772009-04-12 05:37:29 +00001401 } else
Harald Welte2a139372009-02-22 21:14:55 +00001402 DEBUGP(DMM, "Unknown Subscriber ?!?\n");
1403
Harald Welte2a139372009-02-22 21:14:55 +00001404 return 0;
1405}
1406
Harald Welted2a7f5a2009-06-05 20:08:20 +00001407static int gsm48_rx_mm_status(struct msgb *msg)
1408{
1409 struct gsm48_hdr *gh = msgb_l3(msg);
1410
1411 DEBUGP(DMM, "MM STATUS (reject cause 0x%02x)\n", gh->data[0]);
1412
1413 return 0;
1414}
1415
Harald Weltebf5e8df2009-02-03 12:59:45 +00001416/* Receive a GSM 04.08 Mobility Management (MM) message */
Harald Welte52b1f982008-12-23 20:25:15 +00001417static int gsm0408_rcv_mm(struct msgb *msg)
1418{
1419 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001420 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001421
1422 switch (gh->msg_type & 0xbf) {
1423 case GSM48_MT_MM_LOC_UPD_REQUEST:
Harald Weltea0368542009-06-27 02:58:43 +02001424 DEBUGP(DMM, "LOCATION UPDATING REQUEST: ");
Harald Welte231ad4f2008-12-27 11:15:38 +00001425 rc = mm_rx_loc_upd_req(msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001426 break;
1427 case GSM48_MT_MM_ID_RESP:
Harald Welte231ad4f2008-12-27 11:15:38 +00001428 rc = mm_rx_id_resp(msg);
1429 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001430 case GSM48_MT_MM_CM_SERV_REQ:
Harald Welte4b634542008-12-27 01:55:51 +00001431 rc = gsm48_rx_mm_serv_req(msg);
1432 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001433 case GSM48_MT_MM_STATUS:
Harald Welted2a7f5a2009-06-05 20:08:20 +00001434 rc = gsm48_rx_mm_status(msg);
Harald Welte231ad4f2008-12-27 11:15:38 +00001435 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001436 case GSM48_MT_MM_TMSI_REALL_COMPL:
Harald Welte69b2af22009-01-06 19:47:00 +00001437 DEBUGP(DMM, "TMSI Reallocation Completed. Subscriber: %s\n",
1438 msg->lchan->subscr ?
1439 msg->lchan->subscr->imsi :
1440 "unknown subscriber");
1441 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001442 case GSM48_MT_MM_IMSI_DETACH_IND:
Harald Welte2a139372009-02-22 21:14:55 +00001443 rc = gsm48_rx_mm_imsi_detach_ind(msg);
1444 break;
1445 case GSM48_MT_MM_CM_REEST_REQ:
1446 DEBUGP(DMM, "CM REESTABLISH REQUEST: Not implemented\n");
1447 break;
1448 case GSM48_MT_MM_AUTH_RESP:
1449 DEBUGP(DMM, "AUTHENTICATION RESPONSE: Not implemented\n");
Harald Welte52b1f982008-12-23 20:25:15 +00001450 break;
1451 default:
1452 fprintf(stderr, "Unknown GSM 04.08 MM msg type 0x%02x\n",
1453 gh->msg_type);
1454 break;
1455 }
1456
1457 return rc;
1458}
Harald Weltebf5e8df2009-02-03 12:59:45 +00001459
Harald Welte2d35ae62009-02-06 12:02:13 +00001460/* Receive a PAGING RESPONSE message from the MS */
1461static int gsm48_rr_rx_pag_resp(struct msgb *msg)
1462{
Harald Welte9176bd42009-07-23 18:46:00 +02001463 struct gsm_bts *bts = msg->lchan->ts->trx->bts;
Harald Welte2d35ae62009-02-06 12:02:13 +00001464 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte61548982009-02-22 21:26:29 +00001465 u_int8_t *classmark2_lv = gh->data + 1;
Holger Hans Peter Freyther3ee5d3e2009-08-21 05:18:21 +02001466 u_int8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001467 char mi_string[GSM48_MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +08001468 struct gsm_subscriber *subscr = NULL;
Harald Welte2d35ae62009-02-06 12:02:13 +00001469 int rc = 0;
1470
Holger Hans Peter Freyther3ee5d3e2009-08-21 05:18:21 +02001471 gsm48_paging_extract_mi(msg, mi_string, &mi_type);
Harald Welte2d35ae62009-02-06 12:02:13 +00001472 DEBUGP(DRR, "PAGING RESPONSE: mi_type=0x%02x MI(%s)\n",
1473 mi_type, mi_string);
Harald Welte3ac7f102009-08-10 10:12:45 +02001474
Harald Weltefe18d8f2009-02-22 21:14:24 +00001475 switch (mi_type) {
1476 case GSM_MI_TYPE_TMSI:
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +02001477 subscr = subscr_get_by_tmsi(bts->network,
1478 tmsi_from_string(mi_string));
Harald Weltefe18d8f2009-02-22 21:14:24 +00001479 break;
1480 case GSM_MI_TYPE_IMSI:
Harald Welte9176bd42009-07-23 18:46:00 +02001481 subscr = subscr_get_by_imsi(bts->network, mi_string);
Harald Weltefe18d8f2009-02-22 21:14:24 +00001482 break;
1483 }
Harald Welte2d35ae62009-02-06 12:02:13 +00001484
1485 if (!subscr) {
1486 DEBUGP(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte09e38af2009-02-16 22:52:23 +00001487 /* FIXME: request id? close channel? */
Harald Welte2d35ae62009-02-06 12:02:13 +00001488 return -EINVAL;
1489 }
1490 DEBUGP(DRR, "<- Channel was requested by %s\n",
Harald Welte76042182009-08-08 16:03:15 +02001491 subscr->name && strlen(subscr->name) ? subscr->name : subscr->imsi);
Holger Freyther053e09d2009-02-14 22:51:06 +00001492
Harald Weltec2e302d2009-07-05 14:08:13 +02001493 subscr->equipment.classmark2_len = *classmark2_lv;
1494 memcpy(subscr->equipment.classmark2, classmark2_lv+1, *classmark2_lv);
1495 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +00001496
Holger Hans Peter Freyther3ee5d3e2009-08-21 05:18:21 +02001497 rc = gsm48_handle_paging_resp(msg, subscr);
Harald Welte2d35ae62009-02-06 12:02:13 +00001498 return rc;
1499}
1500
Harald Weltef7c43522009-06-09 20:24:21 +00001501static int gsm48_rx_rr_classmark(struct msgb *msg)
1502{
1503 struct gsm48_hdr *gh = msgb_l3(msg);
1504 struct gsm_subscriber *subscr = msg->lchan->subscr;
1505 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1506 u_int8_t cm2_len, cm3_len = 0;
1507 u_int8_t *cm2, *cm3 = NULL;
1508
1509 DEBUGP(DRR, "CLASSMARK CHANGE ");
1510
1511 /* classmark 2 */
1512 cm2_len = gh->data[0];
1513 cm2 = &gh->data[1];
1514 DEBUGPC(DRR, "CM2(len=%u) ", cm2_len);
1515
1516 if (payload_len > cm2_len + 1) {
1517 /* we must have a classmark3 */
1518 if (gh->data[cm2_len+1] != 0x20) {
1519 DEBUGPC(DRR, "ERR CM3 TAG\n");
1520 return -EINVAL;
1521 }
1522 if (cm2_len > 3) {
1523 DEBUGPC(DRR, "CM2 too long!\n");
1524 return -EINVAL;
1525 }
1526
1527 cm3_len = gh->data[cm2_len+2];
1528 cm3 = &gh->data[cm2_len+3];
1529 if (cm3_len > 14) {
1530 DEBUGPC(DRR, "CM3 len %u too long!\n", cm3_len);
1531 return -EINVAL;
1532 }
1533 DEBUGPC(DRR, "CM3(len=%u)\n", cm3_len);
1534 }
1535 if (subscr) {
Harald Weltec2e302d2009-07-05 14:08:13 +02001536 subscr->equipment.classmark2_len = cm2_len;
1537 memcpy(subscr->equipment.classmark2, cm2, cm2_len);
Harald Weltef7c43522009-06-09 20:24:21 +00001538 if (cm3) {
Harald Weltec2e302d2009-07-05 14:08:13 +02001539 subscr->equipment.classmark3_len = cm3_len;
1540 memcpy(subscr->equipment.classmark3, cm3, cm3_len);
Harald Weltef7c43522009-06-09 20:24:21 +00001541 }
Harald Weltec2e302d2009-07-05 14:08:13 +02001542 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +00001543 }
1544
Harald Weltef7c43522009-06-09 20:24:21 +00001545 return 0;
1546}
1547
Harald Weltecf5b3592009-05-01 18:28:42 +00001548static int gsm48_rx_rr_status(struct msgb *msg)
1549{
1550 struct gsm48_hdr *gh = msgb_l3(msg);
1551
1552 DEBUGP(DRR, "STATUS rr_cause = %s\n",
1553 rr_cause_name(gh->data[0]));
1554
1555 return 0;
1556}
1557
Harald Weltef7c43522009-06-09 20:24:21 +00001558static int gsm48_rx_rr_meas_rep(struct msgb *msg)
1559{
1560 struct gsm48_hdr *gh = msgb_l3(msg);
1561 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1562 static struct gsm_meas_rep meas_rep;
1563
Harald Welte10d0e672009-06-27 02:53:10 +02001564 DEBUGP(DMEAS, "MEASUREMENT REPORT ");
Harald Weltef7c43522009-06-09 20:24:21 +00001565 parse_meas_rep(&meas_rep, gh->data, payload_len);
1566 if (meas_rep.flags & MEAS_REP_F_DTX)
Harald Welte10d0e672009-06-27 02:53:10 +02001567 DEBUGPC(DMEAS, "DTX ");
Harald Weltef7c43522009-06-09 20:24:21 +00001568 if (meas_rep.flags & MEAS_REP_F_BA1)
Harald Welte10d0e672009-06-27 02:53:10 +02001569 DEBUGPC(DMEAS, "BA1 ");
Harald Weltef7c43522009-06-09 20:24:21 +00001570 if (!(meas_rep.flags & MEAS_REP_F_VALID))
Harald Welte10d0e672009-06-27 02:53:10 +02001571 DEBUGPC(DMEAS, "NOT VALID ");
Harald Weltef7c43522009-06-09 20:24:21 +00001572 else
Harald Welte10d0e672009-06-27 02:53:10 +02001573 DEBUGPC(DMEAS, "FULL(lev=%u, qual=%u) SUB(lev=%u, qual=%u) ",
Harald Weltef7c43522009-06-09 20:24:21 +00001574 meas_rep.rxlev_full, meas_rep.rxqual_full, meas_rep.rxlev_sub,
1575 meas_rep.rxqual_sub);
1576
Harald Welte10d0e672009-06-27 02:53:10 +02001577 DEBUGPC(DMEAS, "NUM_NEIGH=%u\n", meas_rep.num_cell);
Harald Weltef7c43522009-06-09 20:24:21 +00001578
1579 /* FIXME: put the results somwhere */
1580
1581 return 0;
1582}
1583
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001584static int gsm48_rx_rr_app_info(struct msgb *msg)
1585{
1586 struct gsm48_hdr *gh = msgb_l3(msg);
1587 u_int8_t apdu_id_flags;
1588 u_int8_t apdu_len;
1589 u_int8_t *apdu_data;
1590
1591 apdu_id_flags = gh->data[0];
1592 apdu_len = gh->data[1];
1593 apdu_data = gh->data+2;
1594
1595 DEBUGP(DNM, "RX APPLICATION INFO id/flags=0x%02x apdu_len=%u apdu=%s",
1596 apdu_id_flags, apdu_len, hexdump(apdu_data, apdu_len));
1597
Harald Welte (local)026531e2009-08-16 10:40:10 +02001598 return db_apdu_blob_store(msg->lchan->subscr, apdu_id_flags, apdu_len, apdu_data);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001599}
1600
1601
Harald Weltebf5e8df2009-02-03 12:59:45 +00001602/* Receive a GSM 04.08 Radio Resource (RR) message */
Harald Welte52b1f982008-12-23 20:25:15 +00001603static int gsm0408_rcv_rr(struct msgb *msg)
1604{
1605 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001606 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001607
1608 switch (gh->msg_type) {
1609 case GSM48_MT_RR_CLSM_CHG:
Harald Weltef7c43522009-06-09 20:24:21 +00001610 rc = gsm48_rx_rr_classmark(msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001611 break;
Harald Weltefc977a82008-12-27 10:19:37 +00001612 case GSM48_MT_RR_GPRS_SUSP_REQ:
1613 DEBUGP(DRR, "GRPS SUSPEND REQUEST\n");
1614 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001615 case GSM48_MT_RR_PAG_RESP:
Harald Welte2d35ae62009-02-06 12:02:13 +00001616 rc = gsm48_rr_rx_pag_resp(msg);
1617 break;
Harald Welte7ccf7782009-02-17 01:43:01 +00001618 case GSM48_MT_RR_CHAN_MODE_MODIF_ACK:
1619 DEBUGP(DRR, "CHANNEL MODE MODIFY ACK\n");
Harald Welte13cac662009-07-29 12:10:35 +02001620 /* We've successfully modified the MS side of the channel,
1621 * now go on to modify the BTS side of the channel */
Harald Welte9943c5b2009-07-29 15:41:29 +02001622 msg->lchan->rsl_cmode = RSL_CMOD_SPD_SPEECH;
Harald Welte2c38aa82009-02-18 03:44:24 +00001623 rc = rsl_chan_mode_modify_req(msg->lchan);
Harald Welte7ccf7782009-02-17 01:43:01 +00001624 break;
Harald Weltecf5b3592009-05-01 18:28:42 +00001625 case GSM48_MT_RR_STATUS:
1626 rc = gsm48_rx_rr_status(msg);
1627 break;
Harald Weltef7c43522009-06-09 20:24:21 +00001628 case GSM48_MT_RR_MEAS_REP:
1629 rc = gsm48_rx_rr_meas_rep(msg);
1630 break;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001631 case GSM48_MT_RR_APP_INFO:
1632 rc = gsm48_rx_rr_app_info(msg);
1633 break;
Harald Welte08d91a52009-08-30 15:37:11 +09001634 case GSM48_MT_RR_CIPH_M_COMPL:
1635 DEBUGP(DRR, "CIPHERING MODE COMPLETE\n");
1636 /* FIXME: check for MI (if any) */
1637 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001638 default:
Harald Welte2d35ae62009-02-06 12:02:13 +00001639 fprintf(stderr, "Unimplemented GSM 04.08 RR msg type 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +00001640 gh->msg_type);
1641 break;
1642 }
1643
Harald Welte2d35ae62009-02-06 12:02:13 +00001644 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +00001645}
1646
Harald Welte08d91a52009-08-30 15:37:11 +09001647/* Chapter 9.1.9: Ciphering Mode Command */
1648int gsm48_send_rr_ciph_mode(struct gsm_lchan *lchan)
1649{
1650 struct msgb *msg = gsm48_msgb_alloc();
1651 struct gsm48_hdr *gh;
1652 u_int8_t ciph_mod_set;
1653
1654 msg->lchan = lchan;
1655
1656 DEBUGP(DRR, "TX CIPHERING MODE CMD\n");
1657
1658 if (lchan->encr.alg_id <= RSL_ENC_ALG_A5(0))
1659 ciph_mod_set = 0;
1660 else
Sylvain Munaut1dcfc002009-09-27 11:11:41 +02001661 ciph_mod_set = (lchan->encr.alg_id-2)<<1 | 1;
Harald Welte08d91a52009-08-30 15:37:11 +09001662
1663 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
1664 gh->proto_discr = GSM48_PDISC_RR;
1665 gh->msg_type = GSM48_MT_RR_CIPH_M_CMD;
1666 gh->data[0] = 0x10 | (ciph_mod_set & 0xf);
1667
1668 return rsl_encryption_cmd(msg);
1669}
Holger Freythere64a7a32009-02-06 21:55:37 +00001670
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001671int gsm48_send_rr_app_info(struct gsm_lchan *lchan, u_int8_t apdu_id,
1672 u_int8_t apdu_len, u_int8_t *apdu)
1673{
1674 struct msgb *msg = gsm48_msgb_alloc();
1675 struct gsm48_hdr *gh;
1676
1677 msg->lchan = lchan;
1678
1679 DEBUGP(DRR, "TX APPLICATION INFO id=0x%02x, len=%u\n",
1680 apdu_id, apdu_len);
1681
1682 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 2 + apdu_len);
1683 gh->proto_discr = GSM48_PDISC_RR;
1684 gh->msg_type = GSM48_MT_RR_APP_INFO;
1685 gh->data[0] = apdu_id;
1686 gh->data[1] = apdu_len;
1687 memcpy(gh->data+2, apdu, apdu_len);
1688
1689 return gsm48_sendmsg(msg, NULL);
1690}
1691
Harald Welte4bc90a12008-12-27 16:32:52 +00001692/* Call Control */
1693
Harald Welte7584aea2009-02-11 11:44:12 +00001694/* The entire call control code is written in accordance with Figure 7.10c
1695 * for 'very early assignment', i.e. we allocate a TCH/F during IMMEDIATE
1696 * ASSIGN, then first use that TCH/F for signalling and later MODE MODIFY
1697 * it for voice */
1698
Harald Welte4bfdfe72009-06-10 23:11:52 +08001699static void new_cc_state(struct gsm_trans *trans, int state)
1700{
1701 if (state > 31 || state < 0)
1702 return;
1703
1704 DEBUGP(DCC, "new state %s -> %s\n",
Harald Weltedcaf5652009-07-23 18:56:43 +02001705 cc_state_names[trans->cc.state], cc_state_names[state]);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001706
Harald Weltedcaf5652009-07-23 18:56:43 +02001707 trans->cc.state = state;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001708}
1709
1710static int gsm48_cc_tx_status(struct gsm_trans *trans, void *arg)
Harald Welte4bc90a12008-12-27 16:32:52 +00001711{
1712 struct msgb *msg = gsm48_msgb_alloc();
1713 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1714 u_int8_t *cause, *call_state;
1715
Harald Welte4bc90a12008-12-27 16:32:52 +00001716 gh->msg_type = GSM48_MT_CC_STATUS;
1717
1718 cause = msgb_put(msg, 3);
1719 cause[0] = 2;
1720 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
1721 cause[2] = 0x80 | 30; /* response to status inquiry */
1722
1723 call_state = msgb_put(msg, 1);
1724 call_state[0] = 0xc0 | 0x00;
1725
Harald Welte39e2ead2009-07-23 21:13:03 +02001726 return gsm48_sendmsg(msg, trans);
Harald Welte4bc90a12008-12-27 16:32:52 +00001727}
1728
Harald Welte6f4b7532008-12-29 00:39:37 +00001729static int gsm48_tx_simple(struct gsm_lchan *lchan,
1730 u_int8_t pdisc, u_int8_t msg_type)
Harald Welte4bc90a12008-12-27 16:32:52 +00001731{
1732 struct msgb *msg = gsm48_msgb_alloc();
1733 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1734
1735 msg->lchan = lchan;
1736
Harald Welte6f4b7532008-12-29 00:39:37 +00001737 gh->proto_discr = pdisc;
Harald Welte4bc90a12008-12-27 16:32:52 +00001738 gh->msg_type = msg_type;
1739
Harald Welte39e2ead2009-07-23 21:13:03 +02001740 return gsm48_sendmsg(msg, NULL);
Harald Welte4bc90a12008-12-27 16:32:52 +00001741}
1742
Harald Welte4bfdfe72009-06-10 23:11:52 +08001743static void gsm48_stop_cc_timer(struct gsm_trans *trans)
1744{
Harald Weltedcaf5652009-07-23 18:56:43 +02001745 if (bsc_timer_pending(&trans->cc.timer)) {
1746 DEBUGP(DCC, "stopping pending timer T%x\n", trans->cc.Tcurrent);
1747 bsc_del_timer(&trans->cc.timer);
1748 trans->cc.Tcurrent = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001749 }
1750}
1751
1752static int mncc_recvmsg(struct gsm_network *net, struct gsm_trans *trans,
1753 int msg_type, struct gsm_mncc *mncc)
1754{
1755 struct msgb *msg;
1756
1757 if (trans)
1758 if (trans->lchan)
Harald Welte6f5aee02009-07-23 21:21:14 +02001759 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08001760 "Sending '%s' to MNCC.\n",
1761 trans->lchan->ts->trx->bts->nr,
1762 trans->lchan->ts->trx->nr,
1763 trans->lchan->ts->nr, trans->transaction_id,
1764 (trans->subscr)?(trans->subscr->extension):"-",
1765 get_mncc_name(msg_type));
1766 else
1767 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
1768 "Sending '%s' to MNCC.\n",
1769 (trans->subscr)?(trans->subscr->extension):"-",
1770 get_mncc_name(msg_type));
1771 else
1772 DEBUGP(DCC, "(bts - trx - ts - ti -- sub -) "
1773 "Sending '%s' to MNCC.\n", get_mncc_name(msg_type));
1774
1775 mncc->msg_type = msg_type;
1776
Harald Welte966636f2009-06-26 19:39:35 +02001777 msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001778 if (!msg)
1779 return -ENOMEM;
1780 memcpy(msg->data, mncc, sizeof(struct gsm_mncc));
1781 msgb_enqueue(&net->upqueue, msg);
1782
1783 return 0;
1784}
1785
1786int mncc_release_ind(struct gsm_network *net, struct gsm_trans *trans,
1787 u_int32_t callref, int location, int value)
1788{
1789 struct gsm_mncc rel;
1790
Harald Welte92f70c52009-06-12 01:54:08 +08001791 memset(&rel, 0, sizeof(rel));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001792 rel.callref = callref;
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001793 mncc_set_cause(&rel, location, value);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001794 return mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
1795}
1796
Harald Weltedcaf5652009-07-23 18:56:43 +02001797/* Call Control Specific transaction release.
1798 * gets called by trans_free, DO NOT CALL YOURSELF! */
1799void _gsm48_cc_trans_free(struct gsm_trans *trans)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001800{
Harald Welte4bfdfe72009-06-10 23:11:52 +08001801 gsm48_stop_cc_timer(trans);
1802
1803 /* send release to L4, if callref still exists */
1804 if (trans->callref) {
1805 /* Ressource unavailable */
Harald Welte596fed42009-07-23 19:06:52 +02001806 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001807 GSM48_CAUSE_LOC_PRN_S_LU,
1808 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001809 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001810 if (trans->cc.state != GSM_CSTATE_NULL)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001811 new_cc_state(trans, GSM_CSTATE_NULL);
Harald Weltedcaf5652009-07-23 18:56:43 +02001812 if (trans->lchan)
1813 trau_mux_unmap(&trans->lchan->ts->e1_link, trans->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001814}
1815
1816static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg);
1817
Harald Welte09e38af2009-02-16 22:52:23 +00001818/* call-back from paging the B-end of the connection */
1819static int setup_trig_pag_evt(unsigned int hooknum, unsigned int event,
Harald Welte7ccf7782009-02-17 01:43:01 +00001820 struct msgb *msg, void *_lchan, void *param)
Harald Welte09e38af2009-02-16 22:52:23 +00001821{
Harald Welte7ccf7782009-02-17 01:43:01 +00001822 struct gsm_lchan *lchan = _lchan;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001823 struct gsm_subscriber *subscr = param;
1824 struct gsm_trans *transt, *tmp;
1825 struct gsm_network *net;
Harald Weltec05677b2009-06-26 20:17:06 +02001826
Harald Welte09e38af2009-02-16 22:52:23 +00001827 if (hooknum != GSM_HOOK_RR_PAGING)
1828 return -EINVAL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001829
1830 if (!subscr)
1831 return -EINVAL;
1832 net = subscr->net;
1833 if (!net) {
1834 DEBUGP(DCC, "Error Network not set!\n");
1835 return -EINVAL;
Harald Welte5a065df2009-02-22 21:13:18 +00001836 }
Harald Welte7584aea2009-02-11 11:44:12 +00001837
Harald Welte4bfdfe72009-06-10 23:11:52 +08001838 /* check all tranactions (without lchan) for subscriber */
1839 llist_for_each_entry_safe(transt, tmp, &net->trans_list, entry) {
1840 if (transt->subscr != subscr || transt->lchan)
1841 continue;
1842 switch (event) {
1843 case GSM_PAGING_SUCCEEDED:
1844 if (!lchan) // paranoid
1845 break;
1846 DEBUGP(DCC, "Paging subscr %s succeeded!\n",
1847 subscr->extension);
1848 /* Assign lchan */
1849 if (!transt->lchan) {
1850 transt->lchan = lchan;
1851 use_lchan(lchan);
1852 }
1853 /* send SETUP request to called party */
Harald Weltedcaf5652009-07-23 18:56:43 +02001854 gsm48_cc_tx_setup(transt, &transt->cc.msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001855 break;
1856 case GSM_PAGING_EXPIRED:
1857 DEBUGP(DCC, "Paging subscr %s expired!\n",
1858 subscr->extension);
1859 /* Temporarily out of order */
Harald Welte596fed42009-07-23 19:06:52 +02001860 mncc_release_ind(transt->subscr->net, transt,
1861 transt->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001862 GSM48_CAUSE_LOC_PRN_S_LU,
1863 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001864 transt->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001865 trans_free(transt);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001866 break;
1867 }
1868 }
Harald Welte09e38af2009-02-16 22:52:23 +00001869 return 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00001870}
Harald Welte7584aea2009-02-11 11:44:12 +00001871
Harald Welte805f6442009-07-28 18:25:29 +02001872/* some other part of the code sends us a signal */
1873static int handle_abisip_signal(unsigned int subsys, unsigned int signal,
1874 void *handler_data, void *signal_data)
1875{
1876 struct gsm_lchan *lchan = signal_data;
1877 struct gsm_bts_trx_ts *ts;
1878 int rc;
1879
1880 if (subsys != SS_ABISIP)
1881 return 0;
1882
1883 /* in case we use direct BTS-to-BTS RTP */
1884 if (ipacc_rtp_direct)
1885 return 0;
1886
1887 ts = lchan->ts;
1888
1889 switch (signal) {
1890 case S_ABISIP_BIND_ACK:
1891 /* the BTS has successfully bound a TCH to a local ip/port,
1892 * which means we can connect our UDP socket to it */
1893 if (ts->abis_ip.rtp_socket) {
1894 rtp_socket_free(ts->abis_ip.rtp_socket);
1895 ts->abis_ip.rtp_socket = NULL;
1896 }
1897
1898 ts->abis_ip.rtp_socket = rtp_socket_create();
1899 if (!ts->abis_ip.rtp_socket)
1900 goto out_err;
1901
1902 rc = rtp_socket_connect(ts->abis_ip.rtp_socket,
1903 ts->abis_ip.bound_ip,
1904 ts->abis_ip.bound_port);
1905 if (rc < 0)
1906 goto out_err;
1907 break;
1908 case S_ABISIP_DISC_IND:
1909 /* the BTS tells us a RTP stream has been disconnected */
1910 if (ts->abis_ip.rtp_socket) {
1911 rtp_socket_free(ts->abis_ip.rtp_socket);
1912 ts->abis_ip.rtp_socket = NULL;
1913 }
1914 break;
1915 }
1916
1917 return 0;
1918out_err:
1919 /* FIXME: do something */
1920 return 0;
1921}
1922
1923/* bind rtp proxy to local IP/port and tell BTS to connect to it */
1924static int ipacc_connect_proxy_bind(struct gsm_lchan *lchan)
1925{
1926 struct gsm_bts_trx_ts *ts = lchan->ts;
1927 struct rtp_socket *rs = ts->abis_ip.rtp_socket;
1928 int rc;
1929
1930 rc = rsl_ipacc_connect(lchan, ntohl(rs->rtp.sin_local.sin_addr.s_addr),
1931 ntohs(rs->rtp.sin_local.sin_port),
1932 ts->abis_ip.conn_id,
1933 /* FIXME: use RTP payload of bound socket, not BTS*/
1934 ts->abis_ip.rtp_payload2);
1935
1936 return rc;
1937}
1938
Harald Welte49f48b82009-02-17 15:29:33 +00001939/* map two ipaccess RTP streams onto each other */
Harald Welte11fa29c2009-02-19 17:24:39 +00001940static int tch_map(struct gsm_lchan *lchan, struct gsm_lchan *remote_lchan)
Harald Welte49f48b82009-02-17 15:29:33 +00001941{
Harald Welte11fa29c2009-02-19 17:24:39 +00001942 struct gsm_bts *bts = lchan->ts->trx->bts;
1943 struct gsm_bts *remote_bts = remote_lchan->ts->trx->bts;
Harald Welte49f48b82009-02-17 15:29:33 +00001944 struct gsm_bts_trx_ts *ts;
Harald Welte805f6442009-07-28 18:25:29 +02001945 int rc;
Harald Welte49f48b82009-02-17 15:29:33 +00001946
Harald Welte11fa29c2009-02-19 17:24:39 +00001947 DEBUGP(DCC, "Setting up TCH map between (bts=%u,trx=%u,ts=%u) and (bts=%u,trx=%u,ts=%u)\n",
1948 bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
1949 remote_bts->nr, remote_lchan->ts->trx->nr, remote_lchan->ts->nr);
1950
1951 if (bts->type != remote_bts->type) {
1952 DEBUGP(DCC, "Cannot switch calls between different BTS types yet\n");
1953 return -EINVAL;
1954 }
Harald Welte49f48b82009-02-17 15:29:33 +00001955
Harald Welte11fa29c2009-02-19 17:24:39 +00001956 switch (bts->type) {
Mike Habene2d82272009-10-02 12:19:34 +01001957 case GSM_BTS_TYPE_NANOBTS:
Harald Welte805f6442009-07-28 18:25:29 +02001958 if (!ipacc_rtp_direct) {
1959 /* connect the TCH's to our RTP proxy */
1960 rc = ipacc_connect_proxy_bind(lchan);
1961 if (rc < 0)
1962 return rc;
1963 rc = ipacc_connect_proxy_bind(remote_lchan);
1964
1965 /* connect them with each other */
1966 rtp_socket_proxy(lchan->ts->abis_ip.rtp_socket,
1967 remote_lchan->ts->abis_ip.rtp_socket);
1968 } else {
1969 /* directly connect TCH RTP streams to each other */
1970 ts = remote_lchan->ts;
1971 rc = rsl_ipacc_connect(lchan, ts->abis_ip.bound_ip,
1972 ts->abis_ip.bound_port,
1973 lchan->ts->abis_ip.conn_id,
1974 ts->abis_ip.rtp_payload2);
1975 if (rc < 0)
1976 return rc;
1977 ts = lchan->ts;
1978 rc = rsl_ipacc_connect(remote_lchan, ts->abis_ip.bound_ip,
1979 ts->abis_ip.bound_port,
1980 remote_lchan->ts->abis_ip.conn_id,
1981 ts->abis_ip.rtp_payload2);
1982 }
Harald Welte11fa29c2009-02-19 17:24:39 +00001983 break;
1984 case GSM_BTS_TYPE_BS11:
1985 trau_mux_map_lchan(lchan, remote_lchan);
1986 break;
1987 default:
1988 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
Harald Welte805f6442009-07-28 18:25:29 +02001989 rc = -EINVAL;
Harald Welte11fa29c2009-02-19 17:24:39 +00001990 break;
1991 }
Harald Welte49f48b82009-02-17 15:29:33 +00001992
1993 return 0;
1994}
1995
Harald Welte4bfdfe72009-06-10 23:11:52 +08001996/* bridge channels of two transactions */
1997static int tch_bridge(struct gsm_network *net, u_int32_t *refs)
Harald Welte7ccf7782009-02-17 01:43:01 +00001998{
Harald Weltedcaf5652009-07-23 18:56:43 +02001999 struct gsm_trans *trans1 = trans_find_by_callref(net, refs[0]);
2000 struct gsm_trans *trans2 = trans_find_by_callref(net, refs[1]);
Harald Welte7ccf7782009-02-17 01:43:01 +00002001
Harald Welte4bfdfe72009-06-10 23:11:52 +08002002 if (!trans1 || !trans2)
Harald Welte7ccf7782009-02-17 01:43:01 +00002003 return -EIO;
2004
Harald Welte4bfdfe72009-06-10 23:11:52 +08002005 if (!trans1->lchan || !trans2->lchan)
2006 return -EIO;
2007
2008 /* through-connect channel */
2009 return tch_map(trans1->lchan, trans2->lchan);
Harald Welte7ccf7782009-02-17 01:43:01 +00002010}
2011
Harald Welte4bfdfe72009-06-10 23:11:52 +08002012/* enable receive of channels to upqueue */
2013static int tch_recv(struct gsm_network *net, struct gsm_mncc *data, int enable)
2014{
2015 struct gsm_trans *trans;
Harald Welte7ccf7782009-02-17 01:43:01 +00002016
Harald Welte4bfdfe72009-06-10 23:11:52 +08002017 /* Find callref */
Harald Weltedcaf5652009-07-23 18:56:43 +02002018 trans = trans_find_by_callref(net, data->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002019 if (!trans)
2020 return -EIO;
2021 if (!trans->lchan)
2022 return 0;
2023
2024 // todo IPACCESS
2025 if (enable)
2026 return trau_recv_lchan(trans->lchan, data->callref);
2027 return trau_mux_unmap(NULL, data->callref);
2028}
2029
2030/* send a frame to channel */
2031static int tch_frame(struct gsm_network *net, struct gsm_trau_frame *frame)
2032{
2033 struct gsm_trans *trans;
2034
2035 /* Find callref */
Harald Weltedcaf5652009-07-23 18:56:43 +02002036 trans = trans_find_by_callref(net, frame->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002037 if (!trans)
2038 return -EIO;
2039 if (!trans->lchan)
2040 return 0;
2041 if (trans->lchan->type != GSM_LCHAN_TCH_F &&
2042 trans->lchan->type != GSM_LCHAN_TCH_H)
2043 return 0;
2044
2045 // todo IPACCESS
2046 return trau_send_lchan(trans->lchan,
2047 (struct decoded_trau_frame *)frame->data);
2048}
2049
2050
2051static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
2052{
2053 DEBUGP(DCC, "-> STATUS ENQ\n");
2054 return gsm48_cc_tx_status(trans, msg);
2055}
2056
2057static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg);
2058static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg);
2059
2060static void gsm48_cc_timeout(void *arg)
2061{
2062 struct gsm_trans *trans = arg;
2063 int disconnect = 0, release = 0;
Harald Weltec66b71c2009-06-11 14:23:20 +08002064 int mo_cause = GSM48_CC_CAUSE_RECOVERY_TIMER;
2065 int mo_location = GSM48_CAUSE_LOC_USER;
2066 int l4_cause = GSM48_CC_CAUSE_NORMAL_UNSPEC;
2067 int l4_location = GSM48_CAUSE_LOC_PRN_S_LU;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002068 struct gsm_mncc mo_rel, l4_rel;
2069
2070 memset(&mo_rel, 0, sizeof(struct gsm_mncc));
2071 mo_rel.callref = trans->callref;
2072 memset(&l4_rel, 0, sizeof(struct gsm_mncc));
2073 l4_rel.callref = trans->callref;
2074
Harald Weltedcaf5652009-07-23 18:56:43 +02002075 switch(trans->cc.Tcurrent) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002076 case 0x303:
2077 release = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08002078 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002079 break;
2080 case 0x310:
2081 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08002082 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002083 break;
2084 case 0x313:
2085 disconnect = 1;
2086 /* unknown, did not find it in the specs */
2087 break;
2088 case 0x301:
2089 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08002090 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002091 break;
2092 case 0x308:
Harald Weltedcaf5652009-07-23 18:56:43 +02002093 if (!trans->cc.T308_second) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002094 /* restart T308 a second time */
Harald Weltedcaf5652009-07-23 18:56:43 +02002095 gsm48_cc_tx_release(trans, &trans->cc.msg);
2096 trans->cc.T308_second = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002097 break; /* stay in release state */
2098 }
Harald Weltedcaf5652009-07-23 18:56:43 +02002099 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002100 return;
2101// release = 1;
2102// l4_cause = 14;
2103// break;
2104 case 0x306:
2105 release = 1;
Harald Weltedcaf5652009-07-23 18:56:43 +02002106 mo_cause = trans->cc.msg.cause.value;
2107 mo_location = trans->cc.msg.cause.location;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002108 break;
2109 case 0x323:
2110 disconnect = 1;
2111 break;
2112 default:
2113 release = 1;
2114 }
2115
2116 if (release && trans->callref) {
2117 /* process release towards layer 4 */
Harald Welte596fed42009-07-23 19:06:52 +02002118 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002119 l4_location, l4_cause);
2120 trans->callref = 0;
2121 }
2122
2123 if (disconnect && trans->callref) {
2124 /* process disconnect towards layer 4 */
2125 mncc_set_cause(&l4_rel, l4_location, l4_cause);
Harald Welte596fed42009-07-23 19:06:52 +02002126 mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &l4_rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002127 }
2128
2129 /* process disconnect towards mobile station */
2130 if (disconnect || release) {
2131 mncc_set_cause(&mo_rel, mo_location, mo_cause);
Harald Weltedcaf5652009-07-23 18:56:43 +02002132 mo_rel.cause.diag[0] = ((trans->cc.Tcurrent & 0xf00) >> 8) + '0';
2133 mo_rel.cause.diag[1] = ((trans->cc.Tcurrent & 0x0f0) >> 4) + '0';
2134 mo_rel.cause.diag[2] = (trans->cc.Tcurrent & 0x00f) + '0';
Harald Welte4bfdfe72009-06-10 23:11:52 +08002135 mo_rel.cause.diag_len = 3;
2136
2137 if (disconnect)
2138 gsm48_cc_tx_disconnect(trans, &mo_rel);
2139 if (release)
2140 gsm48_cc_tx_release(trans, &mo_rel);
2141 }
2142
2143}
2144
2145static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
2146 int sec, int micro)
2147{
2148 DEBUGP(DCC, "starting timer T%x with %d seconds\n", current, sec);
Harald Weltedcaf5652009-07-23 18:56:43 +02002149 trans->cc.timer.cb = gsm48_cc_timeout;
2150 trans->cc.timer.data = trans;
2151 bsc_schedule_timer(&trans->cc.timer, sec, micro);
2152 trans->cc.Tcurrent = current;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002153}
2154
2155static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
2156{
2157 struct gsm48_hdr *gh = msgb_l3(msg);
2158 u_int8_t msg_type = gh->msg_type & 0xbf;
2159 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2160 struct tlv_parsed tp;
2161 struct gsm_mncc setup;
2162
2163 memset(&setup, 0, sizeof(struct gsm_mncc));
2164 setup.callref = trans->callref;
2165 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2166 /* emergency setup is identified by msg_type */
2167 if (msg_type == GSM48_MT_CC_EMERG_SETUP)
2168 setup.emergency = 1;
2169
2170 /* use subscriber as calling party number */
2171 if (trans->subscr) {
2172 setup.fields |= MNCC_F_CALLING;
2173 strncpy(setup.calling.number, trans->subscr->extension,
2174 sizeof(setup.calling.number)-1);
Andreas Eversbergc079be42009-06-15 23:22:09 +02002175 strncpy(setup.imsi, trans->subscr->imsi,
2176 sizeof(setup.imsi)-1);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002177 }
2178 /* bearer capability */
2179 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2180 setup.fields |= MNCC_F_BEARER_CAP;
2181 decode_bearer_cap(&setup.bearer_cap,
2182 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2183 }
2184 /* facility */
2185 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2186 setup.fields |= MNCC_F_FACILITY;
2187 decode_facility(&setup.facility,
2188 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2189 }
2190 /* called party bcd number */
2191 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
2192 setup.fields |= MNCC_F_CALLED;
2193 decode_called(&setup.called,
2194 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
2195 }
2196 /* user-user */
2197 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2198 setup.fields |= MNCC_F_USERUSER;
2199 decode_useruser(&setup.useruser,
2200 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2201 }
2202 /* ss-version */
2203 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2204 setup.fields |= MNCC_F_SSVERSION;
2205 decode_ssversion(&setup.ssversion,
2206 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2207 }
2208 /* CLIR suppression */
2209 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
2210 setup.clir.sup = 1;
2211 /* CLIR invocation */
2212 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
2213 setup.clir.inv = 1;
2214 /* cc cap */
2215 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
2216 setup.fields |= MNCC_F_CCCAP;
2217 decode_cccap(&setup.cccap,
2218 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
2219 }
2220
Harald Welte4bfdfe72009-06-10 23:11:52 +08002221 new_cc_state(trans, GSM_CSTATE_INITIATED);
2222
2223 /* indicate setup to MNCC */
Harald Welte596fed42009-07-23 19:06:52 +02002224 mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_IND, &setup);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002225
Harald Welte13cac662009-07-29 12:10:35 +02002226 /* MNCC code will modify the channel asynchronously, we should
2227 * ipaccess-bind only after the modification has been made to the
2228 * lchan->tch_mode */
Harald Welte4bfdfe72009-06-10 23:11:52 +08002229 return 0;
2230}
2231
2232static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
Harald Welte65e74cc2008-12-29 01:55:35 +00002233{
2234 struct msgb *msg = gsm48_msgb_alloc();
2235 struct gsm48_hdr *gh;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002236 struct gsm_mncc *setup = arg;
Harald Welte78283ef2009-07-23 21:36:44 +02002237 int rc, trans_id;
Harald Welte65e74cc2008-12-29 01:55:35 +00002238
Harald Welte7ccf7782009-02-17 01:43:01 +00002239 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte65e74cc2008-12-29 01:55:35 +00002240
Harald Welte4bfdfe72009-06-10 23:11:52 +08002241 /* transaction id must not be assigned */
2242 if (trans->transaction_id != 0xff) { /* unasssigned */
2243 DEBUGP(DCC, "TX Setup with assigned transaction. "
2244 "This is not allowed!\n");
2245 /* Temporarily out of order */
Harald Welte596fed42009-07-23 19:06:52 +02002246 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002247 GSM48_CAUSE_LOC_PRN_S_LU,
2248 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002249 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002250 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002251 return rc;
2252 }
2253
2254 /* Get free transaction_id */
Harald Welte78283ef2009-07-23 21:36:44 +02002255 trans_id = trans_assign_trans_id(trans->subscr, GSM48_PDISC_CC, 0);
2256 if (trans_id < 0) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002257 /* no free transaction ID */
Harald Welte596fed42009-07-23 19:06:52 +02002258 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002259 GSM48_CAUSE_LOC_PRN_S_LU,
2260 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002261 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002262 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002263 return rc;
2264 }
Harald Welte78283ef2009-07-23 21:36:44 +02002265 trans->transaction_id = trans_id;
Harald Welte49f48b82009-02-17 15:29:33 +00002266
Harald Welte65e74cc2008-12-29 01:55:35 +00002267 gh->msg_type = GSM48_MT_CC_SETUP;
Harald Welte09e38af2009-02-16 22:52:23 +00002268
Harald Welte4bfdfe72009-06-10 23:11:52 +08002269 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
Harald Welte65e74cc2008-12-29 01:55:35 +00002270
Harald Welte4bfdfe72009-06-10 23:11:52 +08002271 /* bearer capability */
2272 if (setup->fields & MNCC_F_BEARER_CAP)
2273 encode_bearer_cap(msg, 0, &setup->bearer_cap);
2274 /* facility */
2275 if (setup->fields & MNCC_F_FACILITY)
2276 encode_facility(msg, 0, &setup->facility);
2277 /* progress */
2278 if (setup->fields & MNCC_F_PROGRESS)
2279 encode_progress(msg, 0, &setup->progress);
2280 /* calling party BCD number */
2281 if (setup->fields & MNCC_F_CALLING)
2282 encode_calling(msg, &setup->calling);
2283 /* called party BCD number */
2284 if (setup->fields & MNCC_F_CALLED)
2285 encode_called(msg, &setup->called);
2286 /* user-user */
2287 if (setup->fields & MNCC_F_USERUSER)
2288 encode_useruser(msg, 0, &setup->useruser);
2289 /* redirecting party BCD number */
2290 if (setup->fields & MNCC_F_REDIRECTING)
2291 encode_redirecting(msg, &setup->redirecting);
2292 /* signal */
2293 if (setup->fields & MNCC_F_SIGNAL)
2294 encode_signal(msg, setup->signal);
2295
2296 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
Harald Welte65e74cc2008-12-29 01:55:35 +00002297
Harald Welte39e2ead2009-07-23 21:13:03 +02002298 return gsm48_sendmsg(msg, trans);
Harald Welte65e74cc2008-12-29 01:55:35 +00002299}
2300
Harald Welte4bfdfe72009-06-10 23:11:52 +08002301static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
2302{
2303 struct gsm48_hdr *gh = msgb_l3(msg);
2304 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2305 struct tlv_parsed tp;
2306 struct gsm_mncc call_conf;
2307
2308 gsm48_stop_cc_timer(trans);
2309 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
2310
2311 memset(&call_conf, 0, sizeof(struct gsm_mncc));
2312 call_conf.callref = trans->callref;
2313 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2314#if 0
2315 /* repeat */
2316 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
2317 call_conf.repeat = 1;
2318 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
2319 call_conf.repeat = 2;
2320#endif
2321 /* bearer capability */
2322 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2323 call_conf.fields |= MNCC_F_BEARER_CAP;
2324 decode_bearer_cap(&call_conf.bearer_cap,
2325 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2326 }
2327 /* cause */
2328 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2329 call_conf.fields |= MNCC_F_CAUSE;
2330 decode_cause(&call_conf.cause,
2331 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2332 }
2333 /* cc cap */
2334 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
2335 call_conf.fields |= MNCC_F_CCCAP;
2336 decode_cccap(&call_conf.cccap,
2337 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
2338 }
2339
2340 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
2341
Harald Welte596fed42009-07-23 19:06:52 +02002342 return mncc_recvmsg(trans->subscr->net, trans, MNCC_CALL_CONF_IND,
2343 &call_conf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002344}
2345
2346static int gsm48_cc_tx_call_proc(struct gsm_trans *trans, void *arg)
2347{
2348 struct gsm_mncc *proceeding = arg;
2349 struct msgb *msg = gsm48_msgb_alloc();
2350 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2351
Harald Welte4bfdfe72009-06-10 23:11:52 +08002352 gh->msg_type = GSM48_MT_CC_CALL_PROC;
2353
2354 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
2355
2356 /* bearer capability */
2357 if (proceeding->fields & MNCC_F_BEARER_CAP)
2358 encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
2359 /* facility */
2360 if (proceeding->fields & MNCC_F_FACILITY)
2361 encode_facility(msg, 0, &proceeding->facility);
2362 /* progress */
2363 if (proceeding->fields & MNCC_F_PROGRESS)
2364 encode_progress(msg, 0, &proceeding->progress);
2365
Harald Welte39e2ead2009-07-23 21:13:03 +02002366 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002367}
2368
2369static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
2370{
2371 struct gsm48_hdr *gh = msgb_l3(msg);
2372 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2373 struct tlv_parsed tp;
2374 struct gsm_mncc alerting;
2375
2376 gsm48_stop_cc_timer(trans);
2377 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
2378
2379 memset(&alerting, 0, sizeof(struct gsm_mncc));
2380 alerting.callref = trans->callref;
2381 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2382 /* facility */
2383 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2384 alerting.fields |= MNCC_F_FACILITY;
2385 decode_facility(&alerting.facility,
2386 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2387 }
2388
2389 /* progress */
2390 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
2391 alerting.fields |= MNCC_F_PROGRESS;
2392 decode_progress(&alerting.progress,
2393 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
2394 }
2395 /* ss-version */
2396 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2397 alerting.fields |= MNCC_F_SSVERSION;
2398 decode_ssversion(&alerting.ssversion,
2399 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2400 }
2401
2402 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
2403
Harald Welte596fed42009-07-23 19:06:52 +02002404 return mncc_recvmsg(trans->subscr->net, trans, MNCC_ALERT_IND,
2405 &alerting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002406}
2407
2408static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
2409{
2410 struct gsm_mncc *alerting = arg;
2411 struct msgb *msg = gsm48_msgb_alloc();
2412 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2413
Harald Welte4bfdfe72009-06-10 23:11:52 +08002414 gh->msg_type = GSM48_MT_CC_ALERTING;
2415
2416 /* facility */
2417 if (alerting->fields & MNCC_F_FACILITY)
2418 encode_facility(msg, 0, &alerting->facility);
2419 /* progress */
2420 if (alerting->fields & MNCC_F_PROGRESS)
2421 encode_progress(msg, 0, &alerting->progress);
2422 /* user-user */
2423 if (alerting->fields & MNCC_F_USERUSER)
2424 encode_useruser(msg, 0, &alerting->useruser);
2425
2426 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
2427
Harald Welte39e2ead2009-07-23 21:13:03 +02002428 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002429}
2430
2431static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
2432{
2433 struct gsm_mncc *progress = arg;
2434 struct msgb *msg = gsm48_msgb_alloc();
2435 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2436
Harald Welte4bfdfe72009-06-10 23:11:52 +08002437 gh->msg_type = GSM48_MT_CC_PROGRESS;
2438
2439 /* progress */
2440 encode_progress(msg, 1, &progress->progress);
2441 /* user-user */
2442 if (progress->fields & MNCC_F_USERUSER)
2443 encode_useruser(msg, 0, &progress->useruser);
2444
Harald Welte39e2ead2009-07-23 21:13:03 +02002445 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002446}
2447
2448static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
2449{
2450 struct gsm_mncc *connect = arg;
2451 struct msgb *msg = gsm48_msgb_alloc();
2452 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2453
Harald Welte4bfdfe72009-06-10 23:11:52 +08002454 gh->msg_type = GSM48_MT_CC_CONNECT;
2455
2456 gsm48_stop_cc_timer(trans);
2457 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
2458
2459 /* facility */
2460 if (connect->fields & MNCC_F_FACILITY)
2461 encode_facility(msg, 0, &connect->facility);
2462 /* progress */
2463 if (connect->fields & MNCC_F_PROGRESS)
2464 encode_progress(msg, 0, &connect->progress);
2465 /* connected number */
2466 if (connect->fields & MNCC_F_CONNECTED)
2467 encode_connected(msg, &connect->connected);
2468 /* user-user */
2469 if (connect->fields & MNCC_F_USERUSER)
2470 encode_useruser(msg, 0, &connect->useruser);
2471
2472 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
2473
Harald Welte39e2ead2009-07-23 21:13:03 +02002474 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002475}
2476
2477static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
2478{
2479 struct gsm48_hdr *gh = msgb_l3(msg);
2480 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2481 struct tlv_parsed tp;
2482 struct gsm_mncc connect;
2483
2484 gsm48_stop_cc_timer(trans);
2485
2486 memset(&connect, 0, sizeof(struct gsm_mncc));
2487 connect.callref = trans->callref;
2488 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2489 /* use subscriber as connected party number */
2490 if (trans->subscr) {
2491 connect.fields |= MNCC_F_CONNECTED;
2492 strncpy(connect.connected.number, trans->subscr->extension,
2493 sizeof(connect.connected.number)-1);
Andreas Eversbergc079be42009-06-15 23:22:09 +02002494 strncpy(connect.imsi, trans->subscr->imsi,
2495 sizeof(connect.imsi)-1);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002496 }
2497 /* facility */
2498 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2499 connect.fields |= MNCC_F_FACILITY;
2500 decode_facility(&connect.facility,
2501 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2502 }
2503 /* user-user */
2504 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2505 connect.fields |= MNCC_F_USERUSER;
2506 decode_useruser(&connect.useruser,
2507 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2508 }
2509 /* ss-version */
2510 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2511 connect.fields |= MNCC_F_SSVERSION;
2512 decode_ssversion(&connect.ssversion,
2513 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2514 }
2515
2516 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
2517
Harald Welte596fed42009-07-23 19:06:52 +02002518 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_CNF, &connect);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002519}
2520
2521
2522static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
2523{
2524 struct gsm_mncc connect_ack;
2525
2526 gsm48_stop_cc_timer(trans);
2527
2528 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2529
2530 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
2531 connect_ack.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02002532 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_COMPL_IND,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002533 &connect_ack);
2534}
2535
2536static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
2537{
2538 struct msgb *msg = gsm48_msgb_alloc();
2539 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2540
Harald Welte4bfdfe72009-06-10 23:11:52 +08002541 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
2542
2543 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2544
Harald Welte39e2ead2009-07-23 21:13:03 +02002545 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002546}
2547
2548static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
2549{
2550 struct gsm48_hdr *gh = msgb_l3(msg);
2551 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2552 struct tlv_parsed tp;
2553 struct gsm_mncc disc;
2554
2555 gsm48_stop_cc_timer(trans);
2556
2557 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
2558
2559 memset(&disc, 0, sizeof(struct gsm_mncc));
2560 disc.callref = trans->callref;
2561 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
2562 /* cause */
2563 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2564 disc.fields |= MNCC_F_CAUSE;
2565 decode_cause(&disc.cause,
2566 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2567 }
2568 /* facility */
2569 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2570 disc.fields |= MNCC_F_FACILITY;
2571 decode_facility(&disc.facility,
2572 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2573 }
2574 /* user-user */
2575 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2576 disc.fields |= MNCC_F_USERUSER;
2577 decode_useruser(&disc.useruser,
2578 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2579 }
2580 /* ss-version */
2581 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2582 disc.fields |= MNCC_F_SSVERSION;
2583 decode_ssversion(&disc.ssversion,
2584 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2585 }
2586
Harald Welte596fed42009-07-23 19:06:52 +02002587 return mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &disc);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002588
2589}
2590
Harald Weltec66b71c2009-06-11 14:23:20 +08002591static struct gsm_mncc_cause default_cause = {
2592 .location = GSM48_CAUSE_LOC_PRN_S_LU,
2593 .coding = 0,
2594 .rec = 0,
2595 .rec_val = 0,
2596 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
2597 .diag_len = 0,
2598 .diag = { 0 },
2599};
Harald Welte4bfdfe72009-06-10 23:11:52 +08002600
2601static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
2602{
2603 struct gsm_mncc *disc = arg;
2604 struct msgb *msg = gsm48_msgb_alloc();
2605 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2606
Harald Welte4bfdfe72009-06-10 23:11:52 +08002607 gh->msg_type = GSM48_MT_CC_DISCONNECT;
2608
2609 gsm48_stop_cc_timer(trans);
2610 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
2611
2612 /* cause */
2613 if (disc->fields & MNCC_F_CAUSE)
2614 encode_cause(msg, 1, &disc->cause);
2615 else
2616 encode_cause(msg, 1, &default_cause);
2617
2618 /* facility */
2619 if (disc->fields & MNCC_F_FACILITY)
2620 encode_facility(msg, 0, &disc->facility);
2621 /* progress */
2622 if (disc->fields & MNCC_F_PROGRESS)
2623 encode_progress(msg, 0, &disc->progress);
2624 /* user-user */
2625 if (disc->fields & MNCC_F_USERUSER)
2626 encode_useruser(msg, 0, &disc->useruser);
2627
2628 /* store disconnect cause for T306 expiry */
Harald Weltedcaf5652009-07-23 18:56:43 +02002629 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002630
2631 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
2632
Harald Welte39e2ead2009-07-23 21:13:03 +02002633 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002634}
2635
2636static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
2637{
2638 struct gsm48_hdr *gh = msgb_l3(msg);
2639 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2640 struct tlv_parsed tp;
2641 struct gsm_mncc rel;
2642 int rc;
2643
2644 gsm48_stop_cc_timer(trans);
2645
2646 memset(&rel, 0, sizeof(struct gsm_mncc));
2647 rel.callref = trans->callref;
2648 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2649 /* cause */
2650 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2651 rel.fields |= MNCC_F_CAUSE;
2652 decode_cause(&rel.cause,
2653 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2654 }
2655 /* facility */
2656 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2657 rel.fields |= MNCC_F_FACILITY;
2658 decode_facility(&rel.facility,
2659 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2660 }
2661 /* user-user */
2662 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2663 rel.fields |= MNCC_F_USERUSER;
2664 decode_useruser(&rel.useruser,
2665 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2666 }
2667 /* ss-version */
2668 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2669 rel.fields |= MNCC_F_SSVERSION;
2670 decode_ssversion(&rel.ssversion,
2671 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2672 }
2673
Harald Weltedcaf5652009-07-23 18:56:43 +02002674 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002675 /* release collision 5.4.5 */
Harald Welte596fed42009-07-23 19:06:52 +02002676 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_CNF, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002677 } else {
Harald Welte596fed42009-07-23 19:06:52 +02002678 rc = gsm48_tx_simple(msg->lchan,
Harald Welte6f5aee02009-07-23 21:21:14 +02002679 GSM48_PDISC_CC | (trans->transaction_id << 4),
Harald Welte596fed42009-07-23 19:06:52 +02002680 GSM48_MT_CC_RELEASE_COMPL);
2681 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_IND, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002682 }
2683
2684 new_cc_state(trans, GSM_CSTATE_NULL);
2685
2686 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002687 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002688
2689 return rc;
2690}
2691
2692static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
2693{
2694 struct gsm_mncc *rel = arg;
2695 struct msgb *msg = gsm48_msgb_alloc();
2696 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2697
Harald Welte4bfdfe72009-06-10 23:11:52 +08002698 gh->msg_type = GSM48_MT_CC_RELEASE;
2699
2700 trans->callref = 0;
2701
2702 gsm48_stop_cc_timer(trans);
2703 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
2704
2705 /* cause */
2706 if (rel->fields & MNCC_F_CAUSE)
2707 encode_cause(msg, 0, &rel->cause);
2708 /* facility */
2709 if (rel->fields & MNCC_F_FACILITY)
2710 encode_facility(msg, 0, &rel->facility);
2711 /* user-user */
2712 if (rel->fields & MNCC_F_USERUSER)
2713 encode_useruser(msg, 0, &rel->useruser);
2714
Harald Weltedcaf5652009-07-23 18:56:43 +02002715 trans->cc.T308_second = 0;
2716 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002717
Harald Weltedcaf5652009-07-23 18:56:43 +02002718 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002719 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
2720
Harald Welte39e2ead2009-07-23 21:13:03 +02002721 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002722}
2723
2724static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
2725{
2726 struct gsm48_hdr *gh = msgb_l3(msg);
2727 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2728 struct tlv_parsed tp;
2729 struct gsm_mncc rel;
2730 int rc = 0;
2731
2732 gsm48_stop_cc_timer(trans);
2733
2734 memset(&rel, 0, sizeof(struct gsm_mncc));
2735 rel.callref = trans->callref;
2736 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2737 /* cause */
2738 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2739 rel.fields |= MNCC_F_CAUSE;
2740 decode_cause(&rel.cause,
2741 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2742 }
2743 /* facility */
2744 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2745 rel.fields |= MNCC_F_FACILITY;
2746 decode_facility(&rel.facility,
2747 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2748 }
2749 /* user-user */
2750 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2751 rel.fields |= MNCC_F_USERUSER;
2752 decode_useruser(&rel.useruser,
2753 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2754 }
2755 /* ss-version */
2756 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2757 rel.fields |= MNCC_F_SSVERSION;
2758 decode_ssversion(&rel.ssversion,
2759 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2760 }
2761
2762 if (trans->callref) {
Harald Weltedcaf5652009-07-23 18:56:43 +02002763 switch (trans->cc.state) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002764 case GSM_CSTATE_CALL_PRESENT:
Harald Welte596fed42009-07-23 19:06:52 +02002765 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002766 MNCC_REJ_IND, &rel);
2767 break;
2768 case GSM_CSTATE_RELEASE_REQ:
Harald Welte596fed42009-07-23 19:06:52 +02002769 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002770 MNCC_REL_CNF, &rel);
2771 break;
2772 default:
Harald Welte596fed42009-07-23 19:06:52 +02002773 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002774 MNCC_REL_IND, &rel);
2775 }
2776 }
2777
2778 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002779 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002780
2781 return rc;
2782}
2783
2784static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
2785{
2786 struct gsm_mncc *rel = arg;
2787 struct msgb *msg = gsm48_msgb_alloc();
2788 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2789
Harald Welte4bfdfe72009-06-10 23:11:52 +08002790 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
2791
2792 trans->callref = 0;
2793
2794 gsm48_stop_cc_timer(trans);
2795
2796 /* cause */
2797 if (rel->fields & MNCC_F_CAUSE)
2798 encode_cause(msg, 0, &rel->cause);
2799 /* facility */
2800 if (rel->fields & MNCC_F_FACILITY)
2801 encode_facility(msg, 0, &rel->facility);
2802 /* user-user */
2803 if (rel->fields & MNCC_F_USERUSER)
2804 encode_useruser(msg, 0, &rel->useruser);
2805
Harald Weltedcaf5652009-07-23 18:56:43 +02002806 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002807
Harald Welte39e2ead2009-07-23 21:13:03 +02002808 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002809}
2810
2811static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
2812{
2813 struct gsm48_hdr *gh = msgb_l3(msg);
2814 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2815 struct tlv_parsed tp;
2816 struct gsm_mncc fac;
2817
2818 memset(&fac, 0, sizeof(struct gsm_mncc));
2819 fac.callref = trans->callref;
2820 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
2821 /* facility */
2822 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2823 fac.fields |= MNCC_F_FACILITY;
2824 decode_facility(&fac.facility,
2825 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2826 }
2827 /* ss-version */
2828 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2829 fac.fields |= MNCC_F_SSVERSION;
2830 decode_ssversion(&fac.ssversion,
2831 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2832 }
2833
Harald Welte596fed42009-07-23 19:06:52 +02002834 return mncc_recvmsg(trans->subscr->net, trans, MNCC_FACILITY_IND, &fac);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002835}
2836
2837static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
2838{
2839 struct gsm_mncc *fac = arg;
2840 struct msgb *msg = gsm48_msgb_alloc();
2841 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2842
Harald Welte4bfdfe72009-06-10 23:11:52 +08002843 gh->msg_type = GSM48_MT_CC_FACILITY;
2844
2845 /* facility */
2846 encode_facility(msg, 1, &fac->facility);
2847
Harald Welte39e2ead2009-07-23 21:13:03 +02002848 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002849}
2850
2851static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
2852{
2853 struct gsm_mncc hold;
2854
2855 memset(&hold, 0, sizeof(struct gsm_mncc));
2856 hold.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02002857 return mncc_recvmsg(trans->subscr->net, trans, MNCC_HOLD_IND, &hold);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002858}
2859
2860static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
2861{
2862 struct msgb *msg = gsm48_msgb_alloc();
2863 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2864
Harald Welte4bfdfe72009-06-10 23:11:52 +08002865 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
2866
Harald Welte39e2ead2009-07-23 21:13:03 +02002867 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002868}
2869
2870static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
2871{
2872 struct gsm_mncc *hold_rej = arg;
2873 struct msgb *msg = gsm48_msgb_alloc();
2874 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2875
Harald Welte4bfdfe72009-06-10 23:11:52 +08002876 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
2877
2878 /* cause */
2879 if (hold_rej->fields & MNCC_F_CAUSE)
2880 encode_cause(msg, 1, &hold_rej->cause);
2881 else
2882 encode_cause(msg, 1, &default_cause);
2883
Harald Welte39e2ead2009-07-23 21:13:03 +02002884 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002885}
2886
2887static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
2888{
2889 struct gsm_mncc retrieve;
2890
2891 memset(&retrieve, 0, sizeof(struct gsm_mncc));
2892 retrieve.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02002893 return mncc_recvmsg(trans->subscr->net, trans, MNCC_RETRIEVE_IND,
2894 &retrieve);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002895}
2896
2897static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
2898{
2899 struct msgb *msg = gsm48_msgb_alloc();
2900 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2901
Harald Welte4bfdfe72009-06-10 23:11:52 +08002902 gh->msg_type = GSM48_MT_CC_RETR_ACK;
2903
Harald Welte39e2ead2009-07-23 21:13:03 +02002904 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002905}
2906
2907static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
2908{
2909 struct gsm_mncc *retrieve_rej = arg;
2910 struct msgb *msg = gsm48_msgb_alloc();
2911 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2912
Harald Welte4bfdfe72009-06-10 23:11:52 +08002913 gh->msg_type = GSM48_MT_CC_RETR_REJ;
2914
2915 /* cause */
2916 if (retrieve_rej->fields & MNCC_F_CAUSE)
2917 encode_cause(msg, 1, &retrieve_rej->cause);
2918 else
2919 encode_cause(msg, 1, &default_cause);
2920
Harald Welte39e2ead2009-07-23 21:13:03 +02002921 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002922}
2923
2924static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
2925{
2926 struct gsm48_hdr *gh = msgb_l3(msg);
2927 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2928 struct tlv_parsed tp;
2929 struct gsm_mncc dtmf;
2930
2931 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2932 dtmf.callref = trans->callref;
2933 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2934 /* keypad facility */
2935 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
2936 dtmf.fields |= MNCC_F_KEYPAD;
2937 decode_keypad(&dtmf.keypad,
2938 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
2939 }
2940
Harald Welte596fed42009-07-23 19:06:52 +02002941 return mncc_recvmsg(trans->subscr->net, trans, MNCC_START_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002942}
2943
2944static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
2945{
2946 struct gsm_mncc *dtmf = arg;
2947 struct msgb *msg = gsm48_msgb_alloc();
2948 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2949
Harald Welte4bfdfe72009-06-10 23:11:52 +08002950 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
2951
2952 /* keypad */
2953 if (dtmf->fields & MNCC_F_KEYPAD)
2954 encode_keypad(msg, dtmf->keypad);
2955
Harald Welte39e2ead2009-07-23 21:13:03 +02002956 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002957}
2958
2959static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
2960{
2961 struct gsm_mncc *dtmf = arg;
2962 struct msgb *msg = gsm48_msgb_alloc();
2963 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2964
Harald Welte4bfdfe72009-06-10 23:11:52 +08002965 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
2966
2967 /* cause */
2968 if (dtmf->fields & MNCC_F_CAUSE)
2969 encode_cause(msg, 1, &dtmf->cause);
2970 else
2971 encode_cause(msg, 1, &default_cause);
2972
Harald Welte39e2ead2009-07-23 21:13:03 +02002973 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002974}
2975
2976static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
2977{
2978 struct msgb *msg = gsm48_msgb_alloc();
2979 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2980
Harald Welte4bfdfe72009-06-10 23:11:52 +08002981 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
2982
Harald Welte39e2ead2009-07-23 21:13:03 +02002983 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002984}
2985
2986static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
2987{
2988 struct gsm_mncc dtmf;
2989
2990 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2991 dtmf.callref = trans->callref;
2992
Harald Welte596fed42009-07-23 19:06:52 +02002993 return mncc_recvmsg(trans->subscr->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002994}
2995
2996static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
2997{
2998 struct gsm48_hdr *gh = msgb_l3(msg);
2999 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
3000 struct tlv_parsed tp;
3001 struct gsm_mncc modify;
3002
3003 memset(&modify, 0, sizeof(struct gsm_mncc));
3004 modify.callref = trans->callref;
3005 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
3006 /* bearer capability */
3007 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
3008 modify.fields |= MNCC_F_BEARER_CAP;
3009 decode_bearer_cap(&modify.bearer_cap,
3010 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
3011 }
3012
3013 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
3014
Harald Welte596fed42009-07-23 19:06:52 +02003015 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_IND, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003016}
3017
3018static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
3019{
3020 struct gsm_mncc *modify = arg;
3021 struct msgb *msg = gsm48_msgb_alloc();
3022 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3023
Harald Welte4bfdfe72009-06-10 23:11:52 +08003024 gh->msg_type = GSM48_MT_CC_MODIFY;
3025
3026 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
3027
3028 /* bearer capability */
3029 encode_bearer_cap(msg, 1, &modify->bearer_cap);
3030
3031 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
3032
Harald Welte39e2ead2009-07-23 21:13:03 +02003033 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003034}
3035
3036static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
3037{
3038 struct gsm48_hdr *gh = msgb_l3(msg);
3039 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
3040 struct tlv_parsed tp;
3041 struct gsm_mncc modify;
3042
3043 gsm48_stop_cc_timer(trans);
3044
3045 memset(&modify, 0, sizeof(struct gsm_mncc));
3046 modify.callref = trans->callref;
3047 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
3048 /* bearer capability */
3049 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
3050 modify.fields |= MNCC_F_BEARER_CAP;
3051 decode_bearer_cap(&modify.bearer_cap,
3052 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
3053 }
3054
3055 new_cc_state(trans, GSM_CSTATE_ACTIVE);
3056
Harald Welte596fed42009-07-23 19:06:52 +02003057 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_CNF, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003058}
3059
3060static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
3061{
3062 struct gsm_mncc *modify = arg;
3063 struct msgb *msg = gsm48_msgb_alloc();
3064 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3065
Harald Welte4bfdfe72009-06-10 23:11:52 +08003066 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
3067
3068 /* bearer capability */
3069 encode_bearer_cap(msg, 1, &modify->bearer_cap);
3070
3071 new_cc_state(trans, GSM_CSTATE_ACTIVE);
3072
Harald Welte39e2ead2009-07-23 21:13:03 +02003073 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003074}
3075
3076static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
3077{
3078 struct gsm48_hdr *gh = msgb_l3(msg);
3079 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
3080 struct tlv_parsed tp;
3081 struct gsm_mncc modify;
3082
3083 gsm48_stop_cc_timer(trans);
3084
3085 memset(&modify, 0, sizeof(struct gsm_mncc));
3086 modify.callref = trans->callref;
3087 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
3088 /* bearer capability */
3089 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
3090 modify.fields |= GSM48_IE_BEARER_CAP;
3091 decode_bearer_cap(&modify.bearer_cap,
3092 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
3093 }
3094 /* cause */
3095 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
3096 modify.fields |= MNCC_F_CAUSE;
3097 decode_cause(&modify.cause,
3098 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
3099 }
3100
3101 new_cc_state(trans, GSM_CSTATE_ACTIVE);
3102
Harald Welte596fed42009-07-23 19:06:52 +02003103 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_REJ, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003104}
3105
3106static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
3107{
3108 struct gsm_mncc *modify = arg;
3109 struct msgb *msg = gsm48_msgb_alloc();
3110 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3111
Harald Welte4bfdfe72009-06-10 23:11:52 +08003112 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
3113
3114 /* bearer capability */
3115 encode_bearer_cap(msg, 1, &modify->bearer_cap);
3116 /* cause */
3117 encode_cause(msg, 1, &modify->cause);
3118
3119 new_cc_state(trans, GSM_CSTATE_ACTIVE);
3120
Harald Welte39e2ead2009-07-23 21:13:03 +02003121 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003122}
3123
3124static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
3125{
3126 struct gsm_mncc *notify = arg;
3127 struct msgb *msg = gsm48_msgb_alloc();
3128 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3129
Harald Welte4bfdfe72009-06-10 23:11:52 +08003130 gh->msg_type = GSM48_MT_CC_NOTIFY;
3131
3132 /* notify */
3133 encode_notify(msg, notify->notify);
3134
Harald Welte39e2ead2009-07-23 21:13:03 +02003135 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003136}
3137
3138static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
3139{
3140 struct gsm48_hdr *gh = msgb_l3(msg);
3141 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
3142// struct tlv_parsed tp;
3143 struct gsm_mncc notify;
3144
3145 memset(&notify, 0, sizeof(struct gsm_mncc));
3146 notify.callref = trans->callref;
3147// tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len);
3148 if (payload_len >= 1)
3149 decode_notify(&notify.notify, gh->data);
3150
Harald Welte596fed42009-07-23 19:06:52 +02003151 return mncc_recvmsg(trans->subscr->net, trans, MNCC_NOTIFY_IND, &notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003152}
3153
3154static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
3155{
3156 struct gsm_mncc *user = arg;
3157 struct msgb *msg = gsm48_msgb_alloc();
3158 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3159
Harald Welte4bfdfe72009-06-10 23:11:52 +08003160 gh->msg_type = GSM48_MT_CC_USER_INFO;
3161
3162 /* user-user */
3163 if (user->fields & MNCC_F_USERUSER)
3164 encode_useruser(msg, 1, &user->useruser);
3165 /* more data */
3166 if (user->more)
3167 encode_more(msg);
3168
Harald Welte39e2ead2009-07-23 21:13:03 +02003169 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003170}
3171
3172static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
3173{
3174 struct gsm48_hdr *gh = msgb_l3(msg);
3175 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
3176 struct tlv_parsed tp;
3177 struct gsm_mncc user;
3178
3179 memset(&user, 0, sizeof(struct gsm_mncc));
3180 user.callref = trans->callref;
3181 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
3182 /* user-user */
3183 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
3184 user.fields |= MNCC_F_USERUSER;
3185 decode_useruser(&user.useruser,
3186 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
3187 }
3188 /* more data */
3189 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
3190 user.more = 1;
3191
Harald Welte596fed42009-07-23 19:06:52 +02003192 return mncc_recvmsg(trans->subscr->net, trans, MNCC_USERINFO_IND, &user);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003193}
3194
3195static int gsm48_lchan_modify(struct gsm_trans *trans, void *arg)
3196{
3197 struct gsm_mncc *mode = arg;
Harald Welte13cac662009-07-29 12:10:35 +02003198 int rc;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003199
Harald Welte13cac662009-07-29 12:10:35 +02003200 rc = gsm48_tx_chan_mode_modify(trans->lchan, mode->lchan_mode);
3201 if (rc < 0)
3202 return rc;
3203
3204 /* FIXME: we not only need to do this after mode modify, but
3205 * also after channel activation */
3206 if (is_ipaccess_bts(trans->lchan->ts->trx->bts) &&
3207 mode->lchan_mode != GSM48_CMODE_SIGN)
3208 rc = rsl_ipacc_bind(trans->lchan);
3209
3210 return rc;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003211}
3212
3213static struct downstate {
3214 u_int32_t states;
3215 int type;
3216 int (*rout) (struct gsm_trans *trans, void *arg);
3217} downstatelist[] = {
3218 /* mobile originating call establishment */
3219 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
3220 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc},
3221 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
3222 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
3223 {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 */
3224 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
3225 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
3226 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
3227 /* mobile terminating call establishment */
3228 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
3229 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
3230 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
3231 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
3232 /* signalling during call */
3233 {SBIT(GSM_CSTATE_ACTIVE),
3234 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
3235 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
3236 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
3237 {ALL_STATES,
3238 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
3239 {ALL_STATES,
3240 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
3241 {ALL_STATES,
3242 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
3243 {SBIT(GSM_CSTATE_ACTIVE),
3244 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
3245 {SBIT(GSM_CSTATE_ACTIVE),
3246 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
3247 {SBIT(GSM_CSTATE_ACTIVE),
3248 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
3249 {SBIT(GSM_CSTATE_ACTIVE),
3250 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
3251 {SBIT(GSM_CSTATE_ACTIVE),
3252 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
3253 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
3254 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
3255 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
3256 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
3257 {SBIT(GSM_CSTATE_ACTIVE),
3258 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
3259 /* clearing */
3260 {SBIT(GSM_CSTATE_INITIATED),
3261 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
3262 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
3263 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
3264 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
3265 MNCC_REL_REQ, gsm48_cc_tx_release},
3266 /* special */
3267 {ALL_STATES,
3268 MNCC_LCHAN_MODIFY, gsm48_lchan_modify},
3269};
3270
3271#define DOWNSLLEN \
3272 (sizeof(downstatelist) / sizeof(struct downstate))
3273
3274
3275int mncc_send(struct gsm_network *net, int msg_type, void *arg)
3276{
Harald Welte1a6f7982009-08-09 18:52:33 +02003277 int i, rc = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003278 struct gsm_trans *trans = NULL, *transt;
3279 struct gsm_subscriber *subscr;
Harald Welte1a6f7982009-08-09 18:52:33 +02003280 struct gsm_lchan *lchan = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003281 struct gsm_bts *bts = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003282 struct gsm_mncc *data = arg, rel;
3283
3284 /* handle special messages */
3285 switch(msg_type) {
3286 case MNCC_BRIDGE:
3287 return tch_bridge(net, arg);
3288 case MNCC_FRAME_DROP:
3289 return tch_recv(net, arg, 0);
3290 case MNCC_FRAME_RECV:
3291 return tch_recv(net, arg, 1);
3292 case GSM_TRAU_FRAME:
3293 return tch_frame(net, arg);
3294 }
3295
3296 memset(&rel, 0, sizeof(struct gsm_mncc));
3297 rel.callref = data->callref;
3298
3299 /* Find callref */
Harald Weltedcaf5652009-07-23 18:56:43 +02003300 trans = trans_find_by_callref(net, data->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003301
3302 /* Callref unknown */
3303 if (!trans) {
Harald Welte4a3464c2009-07-04 10:11:24 +02003304 if (msg_type != MNCC_SETUP_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003305 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3306 "Received '%s' from MNCC with "
3307 "unknown callref %d\n", data->called.number,
3308 get_mncc_name(msg_type), data->callref);
3309 /* Invalid call reference */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003310 return mncc_release_ind(net, NULL, data->callref,
3311 GSM48_CAUSE_LOC_PRN_S_LU,
3312 GSM48_CC_CAUSE_INVAL_TRANS_ID);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003313 }
Andreas Eversbergc079be42009-06-15 23:22:09 +02003314 if (!data->called.number[0] && !data->imsi[0]) {
3315 DEBUGP(DCC, "(bts - trx - ts - ti) "
3316 "Received '%s' from MNCC with "
3317 "no number or IMSI\n", get_mncc_name(msg_type));
3318 /* Invalid number */
3319 return mncc_release_ind(net, NULL, data->callref,
3320 GSM48_CAUSE_LOC_PRN_S_LU,
3321 GSM48_CC_CAUSE_INV_NR_FORMAT);
3322 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003323 /* New transaction due to setup, find subscriber */
Andreas Eversbergc079be42009-06-15 23:22:09 +02003324 if (data->called.number[0])
Harald Welte9176bd42009-07-23 18:46:00 +02003325 subscr = subscr_get_by_extension(net,
3326 data->called.number);
Andreas Eversbergc079be42009-06-15 23:22:09 +02003327 else
Harald Welte9176bd42009-07-23 18:46:00 +02003328 subscr = subscr_get_by_imsi(net, data->imsi);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003329 /* If subscriber is not found */
3330 if (!subscr) {
3331 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3332 "Received '%s' from MNCC with "
3333 "unknown subscriber %s\n", data->called.number,
3334 get_mncc_name(msg_type), data->called.number);
3335 /* Unknown subscriber */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003336 return mncc_release_ind(net, NULL, data->callref,
3337 GSM48_CAUSE_LOC_PRN_S_LU,
3338 GSM48_CC_CAUSE_UNASSIGNED_NR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003339 }
3340 /* If subscriber is not "attached" */
3341 if (!subscr->lac) {
3342 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3343 "Received '%s' from MNCC with "
3344 "detached subscriber %s\n", data->called.number,
3345 get_mncc_name(msg_type), data->called.number);
3346 subscr_put(subscr);
3347 /* Temporarily out of order */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003348 return mncc_release_ind(net, NULL, data->callref,
3349 GSM48_CAUSE_LOC_PRN_S_LU,
3350 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003351 }
3352 /* Create transaction */
Harald Weltedcaf5652009-07-23 18:56:43 +02003353 trans = trans_alloc(subscr, GSM48_PDISC_CC, 0xff, data->callref);
3354 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003355 DEBUGP(DCC, "No memory for trans.\n");
3356 subscr_put(subscr);
3357 /* Ressource unavailable */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003358 mncc_release_ind(net, NULL, data->callref,
3359 GSM48_CAUSE_LOC_PRN_S_LU,
3360 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003361 return -ENOMEM;
3362 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003363 /* Find lchan */
Harald Welte1a6f7982009-08-09 18:52:33 +02003364 lchan = lchan_for_subscr(subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003365 /* If subscriber has no lchan */
3366 if (!lchan) {
3367 /* find transaction with this subscriber already paging */
3368 llist_for_each_entry(transt, &net->trans_list, entry) {
3369 /* Transaction of our lchan? */
3370 if (transt == trans ||
3371 transt->subscr != subscr)
3372 continue;
3373 DEBUGP(DCC, "(bts %d trx - ts - ti -- sub %s) "
3374 "Received '%s' from MNCC with "
3375 "unallocated channel, paging already "
3376 "started.\n", bts->nr,
3377 data->called.number,
3378 get_mncc_name(msg_type));
3379 return 0;
3380 }
3381 /* store setup informations until paging was successfull */
Harald Weltedcaf5652009-07-23 18:56:43 +02003382 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
Harald Weltea1b28582009-08-01 19:31:47 +02003383 /* Trigger paging */
3384 paging_request(net, subscr, RSL_CHANNEED_TCH_F,
3385 setup_trig_pag_evt, subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003386 return 0;
3387 }
3388 /* Assign lchan */
3389 trans->lchan = lchan;
3390 use_lchan(lchan);
3391 }
3392 lchan = trans->lchan;
3393
3394 /* if paging did not respond yet */
3395 if (!lchan) {
3396 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3397 "Received '%s' from MNCC in paging state\n",
3398 (trans->subscr)?(trans->subscr->extension):"-",
3399 get_mncc_name(msg_type));
Harald Weltec66b71c2009-06-11 14:23:20 +08003400 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
3401 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003402 if (msg_type == MNCC_REL_REQ)
3403 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
3404 else
3405 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
3406 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02003407 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003408 return rc;
3409 }
3410
3411 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x sub %s) "
3412 "Received '%s' from MNCC in state %d (%s)\n",
3413 lchan->ts->trx->bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
3414 trans->transaction_id,
3415 (lchan->subscr)?(lchan->subscr->extension):"-",
Harald Weltedcaf5652009-07-23 18:56:43 +02003416 get_mncc_name(msg_type), trans->cc.state,
3417 cc_state_names[trans->cc.state]);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003418
3419 /* Find function for current state and message */
3420 for (i = 0; i < DOWNSLLEN; i++)
3421 if ((msg_type == downstatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003422 && ((1 << trans->cc.state) & downstatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003423 break;
3424 if (i == DOWNSLLEN) {
3425 DEBUGP(DCC, "Message unhandled at this state.\n");
3426 return 0;
3427 }
3428
3429 rc = downstatelist[i].rout(trans, arg);
3430
3431 return rc;
3432}
3433
3434
3435static struct datastate {
3436 u_int32_t states;
3437 int type;
3438 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
3439} datastatelist[] = {
3440 /* mobile originating call establishment */
3441 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3442 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
3443 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3444 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
3445 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
3446 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
3447 /* mobile terminating call establishment */
3448 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
3449 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
3450 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
3451 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
3452 {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 */
3453 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
3454 /* signalling during call */
3455 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
3456 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
3457 {SBIT(GSM_CSTATE_ACTIVE),
3458 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
3459 {ALL_STATES,
3460 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
3461 {ALL_STATES,
3462 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
3463 {ALL_STATES,
3464 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
3465 {SBIT(GSM_CSTATE_ACTIVE),
3466 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
3467 {SBIT(GSM_CSTATE_ACTIVE),
3468 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
3469 {SBIT(GSM_CSTATE_ACTIVE),
3470 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
3471 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3472 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
3473 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3474 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
3475 {SBIT(GSM_CSTATE_ACTIVE),
3476 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
3477 /* clearing */
3478 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
3479 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
3480 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
3481 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
3482 {ALL_STATES, /* 5.4.3.4 */
3483 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
3484};
3485
3486#define DATASLLEN \
3487 (sizeof(datastatelist) / sizeof(struct datastate))
3488
Harald Welte4bc90a12008-12-27 16:32:52 +00003489static int gsm0408_rcv_cc(struct msgb *msg)
3490{
3491 struct gsm48_hdr *gh = msgb_l3(msg);
3492 u_int8_t msg_type = gh->msg_type & 0xbf;
Harald Welte6f5aee02009-07-23 21:21:14 +02003493 u_int8_t transaction_id = ((gh->proto_discr & 0xf0) ^ 0x80) >> 4; /* flip */
Harald Welte4bfdfe72009-06-10 23:11:52 +08003494 struct gsm_lchan *lchan = msg->lchan;
Harald Weltedcaf5652009-07-23 18:56:43 +02003495 struct gsm_trans *trans = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003496 int i, rc = 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00003497
Harald Welte4bfdfe72009-06-10 23:11:52 +08003498 if (msg_type & 0x80) {
3499 DEBUGP(DCC, "MSG 0x%2x not defined for PD error\n", msg_type);
3500 return -EINVAL;
Harald Welte4bc90a12008-12-27 16:32:52 +00003501 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003502
3503 /* Find transaction */
Harald Welteb8b40732009-07-23 21:58:40 +02003504 trans = trans_find_by_id(lchan->subscr, GSM48_PDISC_CC, transaction_id);
Harald Weltedcaf5652009-07-23 18:56:43 +02003505
Harald Welte6f5aee02009-07-23 21:21:14 +02003506 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003507 "Received '%s' from MS in state %d (%s)\n",
3508 lchan->ts->trx->bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
3509 transaction_id, (lchan->subscr)?(lchan->subscr->extension):"-",
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +02003510 gsm0408_cc_msg_names[msg_type], trans?(trans->cc.state):0,
Harald Weltedcaf5652009-07-23 18:56:43 +02003511 cc_state_names[trans?(trans->cc.state):0]);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003512
3513 /* Create transaction */
3514 if (!trans) {
Harald Welte6f5aee02009-07-23 21:21:14 +02003515 DEBUGP(DCC, "Unknown transaction ID %x, "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003516 "creating new trans.\n", transaction_id);
3517 /* Create transaction */
Harald Weltedcaf5652009-07-23 18:56:43 +02003518 trans = trans_alloc(lchan->subscr, GSM48_PDISC_CC,
3519 transaction_id, new_callref++);
3520 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003521 DEBUGP(DCC, "No memory for trans.\n");
3522 rc = gsm48_tx_simple(msg->lchan,
Harald Welte6f5aee02009-07-23 21:21:14 +02003523 GSM48_PDISC_CC | (transaction_id << 4),
Harald Welte4bfdfe72009-06-10 23:11:52 +08003524 GSM48_MT_CC_RELEASE_COMPL);
3525 return -ENOMEM;
3526 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003527 /* Assign transaction */
Harald Welte4bfdfe72009-06-10 23:11:52 +08003528 trans->lchan = lchan;
3529 use_lchan(lchan);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003530 }
3531
3532 /* find function for current state and message */
3533 for (i = 0; i < DATASLLEN; i++)
3534 if ((msg_type == datastatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003535 && ((1 << trans->cc.state) & datastatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003536 break;
3537 if (i == DATASLLEN) {
3538 DEBUGP(DCC, "Message unhandled at this state.\n");
3539 return 0;
3540 }
3541
3542 rc = datastatelist[i].rout(trans, msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00003543
3544 return rc;
3545}
3546
Harald Welte52b1f982008-12-23 20:25:15 +00003547/* here we pass in a msgb from the RSL->RLL. We expect the l3 pointer to be set */
Harald Welte (local)daef6062009-08-14 11:41:12 +02003548int gsm0408_rcvmsg(struct msgb *msg, u_int8_t link_id)
Harald Welte52b1f982008-12-23 20:25:15 +00003549{
3550 struct gsm48_hdr *gh = msgb_l3(msg);
3551 u_int8_t pdisc = gh->proto_discr & 0x0f;
Harald Welte8470bf22008-12-25 23:28:35 +00003552 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00003553
3554 switch (pdisc) {
3555 case GSM48_PDISC_CC:
3556 rc = gsm0408_rcv_cc(msg);
3557 break;
3558 case GSM48_PDISC_MM:
3559 rc = gsm0408_rcv_mm(msg);
3560 break;
3561 case GSM48_PDISC_RR:
3562 rc = gsm0408_rcv_rr(msg);
3563 break;
Harald Weltebcae43f2008-12-27 21:45:37 +00003564 case GSM48_PDISC_SMS:
Harald Welte (local)daef6062009-08-14 11:41:12 +02003565 rc = gsm0411_rcv_sms(msg, link_id);
Harald Weltebcae43f2008-12-27 21:45:37 +00003566 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003567 case GSM48_PDISC_MM_GPRS:
Harald Weltebcae43f2008-12-27 21:45:37 +00003568 case GSM48_PDISC_SM_GPRS:
Mike Haben59d284e2009-09-26 19:00:03 +02003569 case GSM48_PDISC_NC_SS: /* mobile-originated USSD */
3570 fprintf(stderr, "Unimplemented GSM 04.08 discriminator 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +00003571 pdisc);
3572 break;
3573 default:
Mike Haben59d284e2009-09-26 19:00:03 +02003574 fprintf(stderr, "Unknown GSM 04.08 discriminator 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +00003575 pdisc);
3576 break;
3577 }
3578
3579 return rc;
3580}
Harald Welte8470bf22008-12-25 23:28:35 +00003581
Harald Welte4bfdfe72009-06-10 23:11:52 +08003582/* dequeue messages to layer 4 */
3583int bsc_upqueue(struct gsm_network *net)
3584{
3585 struct gsm_mncc *mncc;
3586 struct msgb *msg;
3587 int work = 0;
3588
3589 if (net)
3590 while ((msg = msgb_dequeue(&net->upqueue))) {
3591 mncc = (struct gsm_mncc *)msg->data;
3592 if (net->mncc_recv)
3593 net->mncc_recv(net, mncc->msg_type, mncc);
3594 work = 1; /* work done */
Harald Welte316c8252009-06-26 19:40:48 +02003595 talloc_free(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003596 }
3597
3598 return work;
3599}
Harald Weltedcaf5652009-07-23 18:56:43 +02003600
Harald Welte805f6442009-07-28 18:25:29 +02003601/*
3602 * This will be ran by the linker when loading the DSO. We use it to
3603 * do system initialization, e.g. registration of signal handlers.
3604 */
3605static __attribute__((constructor)) void on_dso_load_0408(void)
3606{
Harald Welte805f6442009-07-28 18:25:29 +02003607 register_signal_handler(SS_LCHAN, gsm0408_handle_lchan_signal, NULL);
3608 register_signal_handler(SS_ABISIP, handle_abisip_signal, NULL);
3609}