blob: 94f9ef4d43b3f790c5b86d58b42960361e15db5e [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{
294 u_int32_t tmsi;
295
296 if (authorize_subscriber(lchan->loc_operation, lchan->subscr)) {
Harald Welteee5ad162009-08-09 19:07:00 +0200297 int rc;
298
Holger Freytherd51524f2009-06-09 08:27:07 +0000299 db_subscriber_alloc_tmsi(lchan->subscr);
Holger Freytherd51524f2009-06-09 08:27:07 +0000300 tmsi = strtoul(lchan->subscr->tmsi, NULL, 10);
301 release_loc_updating_req(lchan);
Harald Welteee5ad162009-08-09 19:07:00 +0200302 rc = gsm0408_loc_upd_acc(msg->lchan, tmsi);
303 /* call subscr_update after putting the loc_upd_acc
304 * in the transmit queue, since S_SUBSCR_ATTACHED might
305 * trigger further action like SMS delivery */
306 subscr_update(lchan->subscr, msg->trx->bts,
307 GSM_SUBSCRIBER_UPDATE_ATTACHED);
308 return rc;
Holger Freytherd51524f2009-06-09 08:27:07 +0000309 }
310
311 return 0;
312}
313
Holger Freyther7c19f742009-06-06 13:54:35 +0000314static int gsm0408_handle_lchan_signal(unsigned int subsys, unsigned int signal,
315 void *handler_data, void *signal_data)
316{
Harald Welte4bfdfe72009-06-10 23:11:52 +0800317 struct gsm_trans *trans, *temp;
318
Holger Freyther7c19f742009-06-06 13:54:35 +0000319 if (subsys != SS_LCHAN || signal != S_LCHAN_UNEXPECTED_RELEASE)
320 return 0;
321
322 /*
323 * Cancel any outstanding location updating request
324 * operation taking place on the lchan.
325 */
Harald Welte1a5c6bd2009-07-04 09:35:21 +0200326 struct gsm_lchan *lchan = (struct gsm_lchan *)signal_data;
Harald Weltec05677b2009-06-26 20:17:06 +0200327 if (!lchan)
328 return 0;
329
Holger Freyther7c19f742009-06-06 13:54:35 +0000330 release_loc_updating_req(lchan);
331
Harald Welte4bfdfe72009-06-10 23:11:52 +0800332 /* Free all transactions that are associated with the released lchan */
Harald Weltedcaf5652009-07-23 18:56:43 +0200333 /* FIXME: this is not neccessarily the right thing to do, we should
334 * only set trans->lchan to NULL and wait for another lchan to be
335 * established to the same MM entity (phone/subscriber) */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800336 llist_for_each_entry_safe(trans, temp, &lchan->ts->trx->bts->network->trans_list, entry) {
337 if (trans->lchan == lchan)
Harald Weltedcaf5652009-07-23 18:56:43 +0200338 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +0800339 }
340
Holger Freyther7c19f742009-06-06 13:54:35 +0000341 return 0;
342}
343
Harald Welte09e38af2009-02-16 22:52:23 +0000344static const char bcd_num_digits[] = {
345 '0', '1', '2', '3', '4', '5', '6', '7',
346 '8', '9', '*', '#', 'a', 'b', 'c', '\0'
347};
348
Harald Welte0c389302009-06-10 12:08:54 +0800349/* decode a 'called/calling/connect party BCD number' as in 10.5.4.7 */
350int decode_bcd_number(char *output, int output_len, const u_int8_t *bcd_lv,
351 int h_len)
Harald Welte09e38af2009-02-16 22:52:23 +0000352{
353 u_int8_t in_len = bcd_lv[0];
354 int i;
355
Harald Welte0c389302009-06-10 12:08:54 +0800356 for (i = 1 + h_len; i <= in_len; i++) {
Harald Welte09e38af2009-02-16 22:52:23 +0000357 /* lower nibble */
358 output_len--;
359 if (output_len <= 1)
360 break;
361 *output++ = bcd_num_digits[bcd_lv[i] & 0xf];
362
363 /* higher nibble */
364 output_len--;
365 if (output_len <= 1)
366 break;
367 *output++ = bcd_num_digits[bcd_lv[i] >> 4];
368 }
369 if (output_len >= 1)
370 *output++ = '\0';
371
Harald Welte0c389302009-06-10 12:08:54 +0800372 return 0;
Harald Welte09e38af2009-02-16 22:52:23 +0000373}
374
375/* convert a single ASCII character to call-control BCD */
376static int asc_to_bcd(const char asc)
377{
378 int i;
379
380 for (i = 0; i < ARRAY_SIZE(bcd_num_digits); i++) {
381 if (bcd_num_digits[i] == asc)
382 return i;
383 }
384 return -EINVAL;
385}
386
Harald Welte0c389302009-06-10 12:08:54 +0800387/* convert a ASCII phone number to 'called/calling/connect party BCD number' */
Harald Welte09e38af2009-02-16 22:52:23 +0000388int encode_bcd_number(u_int8_t *bcd_lv, u_int8_t max_len,
Harald Welte0c389302009-06-10 12:08:54 +0800389 int h_len, const char *input)
Harald Welte09e38af2009-02-16 22:52:23 +0000390{
391 int in_len = strlen(input);
392 int i;
Harald Welte0c389302009-06-10 12:08:54 +0800393 u_int8_t *bcd_cur = bcd_lv + 1 + h_len;
Harald Welte09e38af2009-02-16 22:52:23 +0000394
395 /* two digits per byte, plus type byte */
Harald Welte0c389302009-06-10 12:08:54 +0800396 bcd_lv[0] = in_len/2 + h_len;
Harald Welte09e38af2009-02-16 22:52:23 +0000397 if (in_len % 2)
398 bcd_lv[0]++;
399
Harald Welte0c389302009-06-10 12:08:54 +0800400 if (bcd_lv[0] > max_len)
401 return -EIO;
Harald Welte09e38af2009-02-16 22:52:23 +0000402
403 for (i = 0; i < in_len; i++) {
404 int rc = asc_to_bcd(input[i]);
405 if (rc < 0)
406 return rc;
407 if (i % 2 == 0)
408 *bcd_cur = rc;
409 else
410 *bcd_cur++ |= (rc << 4);
411 }
412 /* append padding nibble in case of odd length */
413 if (i % 2)
414 *bcd_cur++ |= 0xf0;
415
416 /* return how many bytes we used */
417 return (bcd_cur - bcd_lv);
418}
419
Harald Welte0c389302009-06-10 12:08:54 +0800420/* decode 'bearer capability' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800421static int decode_bearer_cap(struct gsm_mncc_bearer_cap *bcap,
Harald Welte0c389302009-06-10 12:08:54 +0800422 const u_int8_t *lv)
423{
424 u_int8_t in_len = lv[0];
425 int i, s;
426
427 if (in_len < 1)
428 return -EINVAL;
429
Harald Welte4bfdfe72009-06-10 23:11:52 +0800430 bcap->speech_ver[0] = -1; /* end of list, of maximum 7 values */
Harald Welte0c389302009-06-10 12:08:54 +0800431
432 /* octet 3 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800433 bcap->transfer = lv[1] & 0x07;
434 bcap->mode = (lv[1] & 0x08) >> 3;
435 bcap->coding = (lv[1] & 0x10) >> 4;
436 bcap->radio = (lv[1] & 0x60) >> 5;
Harald Welte0c389302009-06-10 12:08:54 +0800437
438 i = 1;
439 s = 0;
440 while(!(lv[i] & 0x80)) {
441 i++; /* octet 3a etc */
442 if (in_len < i)
443 return 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800444 bcap->speech_ver[s++] = lv[i] & 0x0f;
445 bcap->speech_ver[s] = -1; /* end of list */
Harald Welte0c389302009-06-10 12:08:54 +0800446 if (i == 2) /* octet 3a */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800447 bcap->speech_ctm = (lv[i] & 0x20) >> 5;
Harald Welte0c389302009-06-10 12:08:54 +0800448 if (s == 7) /* maximum speech versions + end of list */
449 return 0;
450 }
451
452 return 0;
453}
454
455/* encode 'bearer capability' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800456static int encode_bearer_cap(struct msgb *msg, int lv_only,
457 const struct gsm_mncc_bearer_cap *bcap)
Harald Welte0c389302009-06-10 12:08:54 +0800458{
459 u_int8_t lv[32 + 1];
460 int i, s;
461
Harald Welte4bfdfe72009-06-10 23:11:52 +0800462 lv[1] = bcap->transfer;
463 lv[1] |= bcap->mode << 3;
464 lv[1] |= bcap->coding << 4;
465 lv[1] |= bcap->radio << 5;
Harald Welte0c389302009-06-10 12:08:54 +0800466
467 i = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800468 for (s = 0; bcap->speech_ver[s] >= 0; s++) {
Harald Welte0c389302009-06-10 12:08:54 +0800469 i++; /* octet 3a etc */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800470 lv[i] = bcap->speech_ver[s];
Harald Welte0c389302009-06-10 12:08:54 +0800471 if (i == 2) /* octet 3a */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800472 lv[i] |= bcap->speech_ctm << 5;
Harald Welte0c389302009-06-10 12:08:54 +0800473 }
474 lv[i] |= 0x80; /* last IE of octet 3 etc */
475
476 lv[0] = i;
477 if (lv_only)
478 msgb_lv_put(msg, lv[0], lv+1);
479 else
480 msgb_tlv_put(msg, GSM48_IE_BEARER_CAP, lv[0], lv+1);
481
482 return 0;
483}
484
485/* decode 'call control cap' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800486static int decode_cccap(struct gsm_mncc_cccap *ccap, const u_int8_t *lv)
Harald Welte0c389302009-06-10 12:08:54 +0800487{
488 u_int8_t in_len = lv[0];
489
490 if (in_len < 1)
491 return -EINVAL;
492
493 /* octet 3 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800494 ccap->dtmf = lv[1] & 0x01;
495 ccap->pcp = (lv[1] & 0x02) >> 1;
Harald Welte0c389302009-06-10 12:08:54 +0800496
497 return 0;
498}
499
500/* decode 'called party BCD number' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800501static int decode_called(struct gsm_mncc_number *called,
502 const u_int8_t *lv)
Harald Welte0c389302009-06-10 12:08:54 +0800503{
504 u_int8_t in_len = lv[0];
505
506 if (in_len < 1)
507 return -EINVAL;
508
509 /* octet 3 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800510 called->plan = lv[1] & 0x0f;
511 called->type = (lv[1] & 0x70) >> 4;
Harald Welte0c389302009-06-10 12:08:54 +0800512
513 /* octet 4..N */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800514 decode_bcd_number(called->number, sizeof(called->number), lv, 1);
Harald Welte0c389302009-06-10 12:08:54 +0800515
516 return 0;
517}
518
519/* encode 'called party BCD number' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800520static int encode_called(struct msgb *msg,
521 const struct gsm_mncc_number *called)
Harald Welte0c389302009-06-10 12:08:54 +0800522{
523 u_int8_t lv[18];
524 int ret;
525
526 /* octet 3 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800527 lv[1] = called->plan;
528 lv[1] |= called->type << 4;
Harald Welte0c389302009-06-10 12:08:54 +0800529
530 /* octet 4..N, octet 2 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800531 ret = encode_bcd_number(lv, sizeof(lv), 1, called->number);
Harald Welte0c389302009-06-10 12:08:54 +0800532 if (ret < 0)
533 return ret;
534
535 msgb_tlv_put(msg, GSM48_IE_CALLED_BCD, lv[0], lv+1);
536
537 return 0;
538}
539
540/* encode callerid of various IEs */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800541static int encode_callerid(struct msgb *msg, int ie,
542 const struct gsm_mncc_number *callerid)
Harald Welte0c389302009-06-10 12:08:54 +0800543{
544 u_int8_t lv[13];
545 int h_len = 1;
546 int ret;
547
548 /* octet 3 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800549 lv[1] = callerid->plan;
550 lv[1] |= callerid->type << 4;
Harald Welte0c389302009-06-10 12:08:54 +0800551
Harald Welte4bfdfe72009-06-10 23:11:52 +0800552 if (callerid->present || callerid->screen) {
Harald Welte0c389302009-06-10 12:08:54 +0800553 /* octet 3a */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800554 lv[2] = callerid->screen;
555 lv[2] |= callerid->present << 5;
Harald Welte0c389302009-06-10 12:08:54 +0800556 lv[2] |= 0x80;
557 h_len++;
558 } else
559 lv[1] |= 0x80;
560
561 /* octet 4..N, octet 2 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800562 ret = encode_bcd_number(lv, sizeof(lv), h_len, callerid->number);
Harald Welte0c389302009-06-10 12:08:54 +0800563 if (ret < 0)
564 return ret;
565
566 msgb_tlv_put(msg, ie, lv[0], lv+1);
567
568 return 0;
569}
570
571/* decode 'cause' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800572static int decode_cause(struct gsm_mncc_cause *cause,
Harald Welte0c389302009-06-10 12:08:54 +0800573 const u_int8_t *lv)
574{
575 u_int8_t in_len = lv[0];
576 int i;
577
578 if (in_len < 2)
579 return -EINVAL;
580
Harald Welte4bfdfe72009-06-10 23:11:52 +0800581 cause->diag_len = 0;
Harald Welte0c389302009-06-10 12:08:54 +0800582
583 /* octet 3 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800584 cause->location = lv[1] & 0x0f;
585 cause->coding = (lv[1] & 0x60) >> 5;
Harald Welte0c389302009-06-10 12:08:54 +0800586
587 i = 1;
588 if (!(lv[i] & 0x80)) {
589 i++; /* octet 3a */
590 if (in_len < i+1)
591 return 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800592 cause->rec = 1;
593 cause->rec_val = lv[i] & 0x7f;
Harald Welte0c389302009-06-10 12:08:54 +0800594
595 }
596 i++;
597
598 /* octet 4 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800599 cause->value = lv[i] & 0x7f;
Harald Welte0c389302009-06-10 12:08:54 +0800600 i++;
601
602 if (in_len < i) /* no diag */
603 return 0;
604
605 if (in_len - (i-1) > 32) /* maximum 32 octets */
606 return 0;
607
608 /* octet 5-N */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800609 memcpy(cause->diag, lv + i, in_len - (i-1));
610 cause->diag_len = in_len - (i-1);
Harald Welte0c389302009-06-10 12:08:54 +0800611
612 return 0;
613}
614
615/* encode 'cause' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800616static int encode_cause(struct msgb *msg, int lv_only,
617 const struct gsm_mncc_cause *cause)
Harald Welte0c389302009-06-10 12:08:54 +0800618{
619 u_int8_t lv[32+4];
620 int i;
621
Harald Welte4bfdfe72009-06-10 23:11:52 +0800622 if (cause->diag_len > 32)
Harald Welte0c389302009-06-10 12:08:54 +0800623 return -EINVAL;
624
625 /* octet 3 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800626 lv[1] = cause->location;
627 lv[1] |= cause->coding << 5;
Harald Welte0c389302009-06-10 12:08:54 +0800628
629 i = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800630 if (cause->rec) {
Harald Welte0c389302009-06-10 12:08:54 +0800631 i++; /* octet 3a */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800632 lv[i] = cause->rec_val;
Harald Welte0c389302009-06-10 12:08:54 +0800633 }
634 lv[i] |= 0x80; /* end of octet 3 */
635
636 /* octet 4 */
637 i++;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800638 lv[i] = 0x80 | cause->value;
Harald Welte0c389302009-06-10 12:08:54 +0800639
640 /* octet 5-N */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800641 if (cause->diag_len) {
642 memcpy(lv + i, cause->diag, cause->diag_len);
643 i += cause->diag_len;
Harald Welte0c389302009-06-10 12:08:54 +0800644 }
645
646 lv[0] = i;
647 if (lv_only)
648 msgb_lv_put(msg, lv[0], lv+1);
649 else
650 msgb_tlv_put(msg, GSM48_IE_CAUSE, lv[0], lv+1);
651
652 return 0;
653}
654
655/* encode 'calling number' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800656static int encode_calling(struct msgb *msg,
657 const struct gsm_mncc_number *calling)
Harald Welte0c389302009-06-10 12:08:54 +0800658{
Harald Welte4bfdfe72009-06-10 23:11:52 +0800659 return encode_callerid(msg, GSM48_IE_CALLING_BCD, calling);
Harald Welte0c389302009-06-10 12:08:54 +0800660}
661
662/* encode 'connected number' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800663static int encode_connected(struct msgb *msg,
664 const struct gsm_mncc_number *connected)
Harald Welte0c389302009-06-10 12:08:54 +0800665{
Harald Welte4bfdfe72009-06-10 23:11:52 +0800666 return encode_callerid(msg, GSM48_IE_CONN_BCD, connected);
Harald Welte0c389302009-06-10 12:08:54 +0800667}
668
669/* encode 'redirecting number' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800670static int encode_redirecting(struct msgb *msg,
671 const struct gsm_mncc_number *redirecting)
Harald Welte0c389302009-06-10 12:08:54 +0800672{
Harald Welte4bfdfe72009-06-10 23:11:52 +0800673 return encode_callerid(msg, GSM48_IE_REDIR_BCD, redirecting);
Harald Welte0c389302009-06-10 12:08:54 +0800674}
675
676/* decode 'facility' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800677static int decode_facility(struct gsm_mncc_facility *facility,
Harald Welte0c389302009-06-10 12:08:54 +0800678 const u_int8_t *lv)
679{
680 u_int8_t in_len = lv[0];
681
682 if (in_len < 1)
683 return -EINVAL;
684
Harald Welte4bfdfe72009-06-10 23:11:52 +0800685 if (in_len > sizeof(facility->info))
Harald Welte0c389302009-06-10 12:08:54 +0800686 return -EINVAL;
687
Harald Welte4bfdfe72009-06-10 23:11:52 +0800688 memcpy(facility->info, lv+1, in_len);
689 facility->len = in_len;
Harald Welte0c389302009-06-10 12:08:54 +0800690
691 return 0;
692}
693
694/* encode 'facility' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800695static int encode_facility(struct msgb *msg, int lv_only,
696 const struct gsm_mncc_facility *facility)
Harald Welte0c389302009-06-10 12:08:54 +0800697{
698 u_int8_t lv[GSM_MAX_FACILITY + 1];
699
Harald Welte4bfdfe72009-06-10 23:11:52 +0800700 if (facility->len < 1 || facility->len > GSM_MAX_FACILITY)
Harald Welte0c389302009-06-10 12:08:54 +0800701 return -EINVAL;
702
Harald Welte4bfdfe72009-06-10 23:11:52 +0800703 memcpy(lv+1, facility->info, facility->len);
704 lv[0] = facility->len;
Harald Welte0c389302009-06-10 12:08:54 +0800705 if (lv_only)
706 msgb_lv_put(msg, lv[0], lv+1);
707 else
708 msgb_tlv_put(msg, GSM48_IE_FACILITY, lv[0], lv+1);
709
710 return 0;
711}
712
713/* decode 'notify' */
714static int decode_notify(int *notify, const u_int8_t *v)
715{
716 *notify = v[0] & 0x7f;
717
718 return 0;
719}
720
721/* encode 'notify' */
722static int encode_notify(struct msgb *msg, int notify)
723{
724 msgb_v_put(msg, notify | 0x80);
725
726 return 0;
727}
728
729/* encode 'signal' */
730static int encode_signal(struct msgb *msg, int signal)
731{
732 msgb_tv_put(msg, GSM48_IE_SIGNAL, signal);
733
734 return 0;
735}
736
737/* decode 'keypad' */
738static int decode_keypad(int *keypad, const u_int8_t *lv)
739{
740 u_int8_t in_len = lv[0];
741
742 if (in_len < 1)
743 return -EINVAL;
744
745 *keypad = lv[1] & 0x7f;
746
747 return 0;
748}
749
750/* encode 'keypad' */
751static int encode_keypad(struct msgb *msg, int keypad)
752{
753 msgb_tv_put(msg, GSM48_IE_KPD_FACILITY, keypad);
754
755 return 0;
756}
757
758/* decode 'progress' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800759static int decode_progress(struct gsm_mncc_progress *progress,
Harald Welte0c389302009-06-10 12:08:54 +0800760 const u_int8_t *lv)
761{
762 u_int8_t in_len = lv[0];
763
764 if (in_len < 2)
765 return -EINVAL;
766
Harald Welte4bfdfe72009-06-10 23:11:52 +0800767 progress->coding = (lv[1] & 0x60) >> 5;
768 progress->location = lv[1] & 0x0f;
769 progress->descr = lv[2] & 0x7f;
Harald Welte0c389302009-06-10 12:08:54 +0800770
771 return 0;
772}
773
774/* encode 'progress' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800775static int encode_progress(struct msgb *msg, int lv_only,
776 const struct gsm_mncc_progress *p)
Harald Welte0c389302009-06-10 12:08:54 +0800777{
778 u_int8_t lv[3];
779
780 lv[0] = 2;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800781 lv[1] = 0x80 | ((p->coding & 0x3) << 5) | (p->location & 0xf);
782 lv[2] = 0x80 | (p->descr & 0x7f);
Harald Welte0c389302009-06-10 12:08:54 +0800783 if (lv_only)
784 msgb_lv_put(msg, lv[0], lv+1);
785 else
786 msgb_tlv_put(msg, GSM48_IE_PROGR_IND, lv[0], lv+1);
787
788 return 0;
789}
790
791/* decode 'user-user' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800792static int decode_useruser(struct gsm_mncc_useruser *uu,
Harald Welte0c389302009-06-10 12:08:54 +0800793 const u_int8_t *lv)
794{
795 u_int8_t in_len = lv[0];
Harald Welte4bfdfe72009-06-10 23:11:52 +0800796 char *info = uu->info;
797 int info_len = sizeof(uu->info);
Harald Welte0c389302009-06-10 12:08:54 +0800798 int i;
799
800 if (in_len < 1)
801 return -EINVAL;
802
Harald Welte4bfdfe72009-06-10 23:11:52 +0800803 uu->proto = lv[1];
Harald Welte0c389302009-06-10 12:08:54 +0800804
805 for (i = 2; i <= in_len; i++) {
806 info_len--;
807 if (info_len <= 1)
808 break;
809 *info++ = lv[i];
810 }
811 if (info_len >= 1)
812 *info++ = '\0';
813
814 return 0;
815}
816
817/* encode 'useruser' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800818static int encode_useruser(struct msgb *msg, int lv_only,
819 const struct gsm_mncc_useruser *uu)
Harald Welte0c389302009-06-10 12:08:54 +0800820{
821 u_int8_t lv[GSM_MAX_USERUSER + 2];
822
Harald Welte4bfdfe72009-06-10 23:11:52 +0800823 if (strlen(uu->info) > GSM_MAX_USERUSER)
Harald Welte0c389302009-06-10 12:08:54 +0800824 return -EINVAL;
825
Harald Welte4bfdfe72009-06-10 23:11:52 +0800826 lv[0] = 1 + strlen(uu->info);
827 lv[1] = uu->proto;
828 memcpy(lv + 2, uu->info, strlen(uu->info));
Harald Welte0c389302009-06-10 12:08:54 +0800829 if (lv_only)
830 msgb_lv_put(msg, lv[0], lv+1);
831 else
832 msgb_tlv_put(msg, GSM48_IE_USER_USER, lv[0], lv+1);
833
834 return 0;
835}
836
837/* decode 'ss version' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800838static int decode_ssversion(struct gsm_mncc_ssversion *ssv,
Harald Welte0c389302009-06-10 12:08:54 +0800839 const u_int8_t *lv)
840{
841 u_int8_t in_len = lv[0];
842
Harald Welte4bfdfe72009-06-10 23:11:52 +0800843 if (in_len < 1 || in_len < sizeof(ssv->info))
Harald Welte0c389302009-06-10 12:08:54 +0800844 return -EINVAL;
845
Harald Welte4bfdfe72009-06-10 23:11:52 +0800846 memcpy(ssv->info, lv + 1, in_len);
847 ssv->len = in_len;
Harald Welte0c389302009-06-10 12:08:54 +0800848
849 return 0;
850}
851
852/* encode 'more data' */
853static int encode_more(struct msgb *msg)
854{
855 u_int8_t *ie;
856
857 ie = msgb_put(msg, 1);
858 ie[0] = GSM48_IE_MORE_DATA;
859
860 return 0;
861}
862
Holger Freyther429e7762008-12-30 13:28:30 +0000863/* Chapter 9.2.14 : Send LOCATION UPDATING REJECT */
Harald Welte8470bf22008-12-25 23:28:35 +0000864int gsm0408_loc_upd_rej(struct gsm_lchan *lchan, u_int8_t cause)
Harald Welte52b1f982008-12-23 20:25:15 +0000865{
Harald Welte8470bf22008-12-25 23:28:35 +0000866 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000867 struct gsm48_hdr *gh;
868
Harald Welte8470bf22008-12-25 23:28:35 +0000869 msg->lchan = lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000870
871 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
872 gh->proto_discr = GSM48_PDISC_MM;
Harald Welte10b487b2008-12-27 19:53:37 +0000873 gh->msg_type = GSM48_MT_MM_LOC_UPD_REJECT;
Harald Welte52b1f982008-12-23 20:25:15 +0000874 gh->data[0] = cause;
875
Harald Weltedb253af2008-12-30 17:56:55 +0000876 DEBUGP(DMM, "-> LOCATION UPDATING REJECT on channel: %d\n", lchan->nr);
877
Harald Welte39e2ead2009-07-23 21:13:03 +0200878 return gsm48_sendmsg(msg, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000879}
880
881/* Chapter 9.2.13 : Send LOCATION UPDATE ACCEPT */
Harald Welte75a983f2008-12-27 21:34:06 +0000882int gsm0408_loc_upd_acc(struct gsm_lchan *lchan, u_int32_t tmsi)
Harald Welte52b1f982008-12-23 20:25:15 +0000883{
Harald Welte8470bf22008-12-25 23:28:35 +0000884 struct gsm_bts *bts = lchan->ts->trx->bts;
885 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000886 struct gsm48_hdr *gh;
887 struct gsm48_loc_area_id *lai;
888 u_int8_t *mid;
Holger Freyther07cc8d82008-12-29 06:23:46 +0000889 int ret;
Harald Welte52b1f982008-12-23 20:25:15 +0000890
Harald Welte8470bf22008-12-25 23:28:35 +0000891 msg->lchan = lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000892
893 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
894 gh->proto_discr = GSM48_PDISC_MM;
895 gh->msg_type = GSM48_MT_MM_LOC_UPD_ACCEPT;
896
897 lai = (struct gsm48_loc_area_id *) msgb_put(msg, sizeof(*lai));
Holger Freyther17746612008-12-28 16:32:44 +0000898 gsm0408_generate_lai(lai, bts->network->country_code,
Harald Welte52b1f982008-12-23 20:25:15 +0000899 bts->network->network_code, bts->location_area_code);
900
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +0200901 mid = msgb_put(msg, GSM48_MID_TMSI_LEN);
Harald Welte52b1f982008-12-23 20:25:15 +0000902 generate_mid_from_tmsi(mid, tmsi);
903
904 DEBUGP(DMM, "-> LOCATION UPDATE ACCEPT\n");
905
Harald Welte39e2ead2009-07-23 21:13:03 +0200906 ret = gsm48_sendmsg(msg, NULL);
Harald Weltedb253af2008-12-30 17:56:55 +0000907
Harald Welteee5ad162009-08-09 19:07:00 +0200908 /* send MM INFO with network name */
Harald Weltedb253af2008-12-30 17:56:55 +0000909 ret = gsm48_tx_mm_info(lchan);
Harald Weltedb253af2008-12-30 17:56:55 +0000910
Holger Freyther07cc8d82008-12-29 06:23:46 +0000911 return ret;
Harald Welte52b1f982008-12-23 20:25:15 +0000912}
913
Harald Weltefc977a82008-12-27 10:19:37 +0000914static char bcd2char(u_int8_t bcd)
915{
916 if (bcd < 0xa)
917 return '0' + bcd;
918 else
919 return 'A' + (bcd - 0xa);
920}
921
Harald Weltebf5e8df2009-02-03 12:59:45 +0000922/* Convert Mobile Identity (10.5.1.4) to string */
Harald Weltefc977a82008-12-27 10:19:37 +0000923static int mi_to_string(char *string, int str_len, u_int8_t *mi, int mi_len)
924{
925 int i;
926 u_int8_t mi_type;
927 char *str_cur = string;
Harald Welte4ed0e922009-01-10 03:17:30 +0000928 u_int32_t tmsi;
Harald Weltefc977a82008-12-27 10:19:37 +0000929
930 mi_type = mi[0] & GSM_MI_TYPE_MASK;
931
932 switch (mi_type) {
933 case GSM_MI_TYPE_NONE:
934 break;
935 case GSM_MI_TYPE_TMSI:
Harald Welte4ed0e922009-01-10 03:17:30 +0000936 /* Table 10.5.4.3, reverse generate_mid_from_tmsi */
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +0200937 if (mi_len == GSM48_TMSI_LEN && mi[0] == (0xf0 | GSM_MI_TYPE_TMSI)) {
Harald Welte4ed0e922009-01-10 03:17:30 +0000938 memcpy(&tmsi, &mi[1], 4);
939 tmsi = ntohl(tmsi);
940 return snprintf(string, str_len, "%u", tmsi);
Harald Weltefc977a82008-12-27 10:19:37 +0000941 }
942 break;
943 case GSM_MI_TYPE_IMSI:
944 case GSM_MI_TYPE_IMEI:
945 case GSM_MI_TYPE_IMEISV:
Harald Weltedb253af2008-12-30 17:56:55 +0000946 *str_cur++ = bcd2char(mi[0] >> 4);
947
948 for (i = 1; i < mi_len; i++) {
Harald Weltefc977a82008-12-27 10:19:37 +0000949 if (str_cur + 2 >= string + str_len)
950 return str_cur - string;
951 *str_cur++ = bcd2char(mi[i] & 0xf);
Harald Weltedb253af2008-12-30 17:56:55 +0000952 /* skip last nibble in last input byte when GSM_EVEN */
953 if( (i != mi_len-1) || (mi[0] & GSM_MI_ODD))
954 *str_cur++ = bcd2char(mi[i] >> 4);
Harald Weltefc977a82008-12-27 10:19:37 +0000955 }
956 break;
957 default:
958 break;
959 }
Harald Weltefc977a82008-12-27 10:19:37 +0000960 *str_cur++ = '\0';
Harald Weltedb253af2008-12-30 17:56:55 +0000961
Harald Weltefc977a82008-12-27 10:19:37 +0000962 return str_cur - string;
963}
964
Harald Weltebf5e8df2009-02-03 12:59:45 +0000965/* Transmit Chapter 9.2.10 Identity Request */
Harald Welte231ad4f2008-12-27 11:15:38 +0000966static int mm_tx_identity_req(struct gsm_lchan *lchan, u_int8_t id_type)
967{
968 struct msgb *msg = gsm48_msgb_alloc();
969 struct gsm48_hdr *gh;
Harald Weltefc977a82008-12-27 10:19:37 +0000970
Harald Welte231ad4f2008-12-27 11:15:38 +0000971 msg->lchan = lchan;
972
973 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
974 gh->proto_discr = GSM48_PDISC_MM;
975 gh->msg_type = GSM48_MT_MM_ID_REQ;
976 gh->data[0] = id_type;
977
Harald Welte39e2ead2009-07-23 21:13:03 +0200978 return gsm48_sendmsg(msg, NULL);
Harald Welte231ad4f2008-12-27 11:15:38 +0000979}
980
981#define MI_SIZE 32
982
Harald Weltebf5e8df2009-02-03 12:59:45 +0000983/* Parse Chapter 9.2.11 Identity Response */
Harald Welte231ad4f2008-12-27 11:15:38 +0000984static int mm_rx_id_resp(struct msgb *msg)
985{
986 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte75a983f2008-12-27 21:34:06 +0000987 struct gsm_lchan *lchan = msg->lchan;
Harald Welte9176bd42009-07-23 18:46:00 +0200988 struct gsm_bts *bts = lchan->ts->trx->bts;
989 struct gsm_network *net = bts->network;
Harald Welte231ad4f2008-12-27 11:15:38 +0000990 u_int8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
991 char mi_string[MI_SIZE];
992
993 mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
Harald Welte61253062008-12-27 11:25:50 +0000994 DEBUGP(DMM, "IDENTITY RESPONSE: mi_type=0x%02x MI(%s)\n",
Harald Welte231ad4f2008-12-27 11:15:38 +0000995 mi_type, mi_string);
996
Harald Welte75a983f2008-12-27 21:34:06 +0000997 switch (mi_type) {
998 case GSM_MI_TYPE_IMSI:
Jan Luebbe370b41d2009-08-12 10:19:34 +0200999 /* look up subscriber based on IMSI, create if not found */
1000 if (!lchan->subscr) {
1001 lchan->subscr = subscr_get_by_imsi(net, mi_string);
Harald Welte (local)aa9dc192009-08-13 13:49:51 +02001002 if (!lchan->subscr)
1003 lchan->subscr = db_create_subscriber(net, mi_string);
Jan Luebbeb0dfc312009-08-12 10:12:52 +02001004 }
Holger Freyther73487a22008-12-31 18:53:57 +00001005 if (lchan->loc_operation)
1006 lchan->loc_operation->waiting_for_imsi = 0;
Harald Welte75a983f2008-12-27 21:34:06 +00001007 break;
1008 case GSM_MI_TYPE_IMEI:
Harald Welte255539c2008-12-28 02:26:27 +00001009 case GSM_MI_TYPE_IMEISV:
Harald Welte75a983f2008-12-27 21:34:06 +00001010 /* update subscribe <-> IMEI mapping */
Harald Welte (local)ee4410a2009-08-17 09:39:55 +02001011 if (lchan->subscr) {
Harald Welte75a983f2008-12-27 21:34:06 +00001012 db_subscriber_assoc_imei(lchan->subscr, mi_string);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +02001013 db_sync_equipment(&lchan->subscr->equipment);
1014 }
Holger Freyther73487a22008-12-31 18:53:57 +00001015 if (lchan->loc_operation)
1016 lchan->loc_operation->waiting_for_imei = 0;
Harald Welte75a983f2008-12-27 21:34:06 +00001017 break;
1018 }
Holger Freyther73487a22008-12-31 18:53:57 +00001019
1020 /* Check if we can let the mobile station enter */
Holger Freytherd51524f2009-06-09 08:27:07 +00001021 return gsm0408_authorize(lchan, msg);
Harald Welte231ad4f2008-12-27 11:15:38 +00001022}
1023
Harald Welte255539c2008-12-28 02:26:27 +00001024
1025static void loc_upd_rej_cb(void *data)
1026{
1027 struct gsm_lchan *lchan = data;
1028
Holger Freyther73487a22008-12-31 18:53:57 +00001029 release_loc_updating_req(lchan);
Holger Freythere97f7fb2008-12-31 18:52:11 +00001030 gsm0408_loc_upd_rej(lchan, reject_cause);
Holger Freyther67b4b9a2009-01-01 03:46:11 +00001031 lchan_auto_release(lchan);
Harald Welte255539c2008-12-28 02:26:27 +00001032}
1033
Holger Freytherb7193e42008-12-29 17:44:08 +00001034static void schedule_reject(struct gsm_lchan *lchan)
1035{
Holger Freyther73487a22008-12-31 18:53:57 +00001036 lchan->loc_operation->updating_timer.cb = loc_upd_rej_cb;
1037 lchan->loc_operation->updating_timer.data = lchan;
Harald Welteff117a82009-05-23 05:22:08 +00001038 bsc_schedule_timer(&lchan->loc_operation->updating_timer, 5, 0);
Holger Freytherb7193e42008-12-29 17:44:08 +00001039}
1040
Harald Welte2a139372009-02-22 21:14:55 +00001041static const char *lupd_name(u_int8_t type)
1042{
1043 switch (type) {
1044 case GSM48_LUPD_NORMAL:
1045 return "NORMAL";
1046 case GSM48_LUPD_PERIODIC:
1047 return "PEROIDOC";
1048 case GSM48_LUPD_IMSI_ATT:
1049 return "IMSI ATTACH";
1050 default:
1051 return "UNKNOWN";
1052 }
1053}
1054
Harald Welte231ad4f2008-12-27 11:15:38 +00001055#define MI_SIZE 32
Harald Weltebf5e8df2009-02-03 12:59:45 +00001056/* Chapter 9.2.15: Receive Location Updating Request */
Harald Welte231ad4f2008-12-27 11:15:38 +00001057static int mm_rx_loc_upd_req(struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001058{
Harald Welte8470bf22008-12-25 23:28:35 +00001059 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001060 struct gsm48_loc_upd_req *lu;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001061 struct gsm_subscriber *subscr = NULL;
Harald Welte255539c2008-12-28 02:26:27 +00001062 struct gsm_lchan *lchan = msg->lchan;
Harald Welte9176bd42009-07-23 18:46:00 +02001063 struct gsm_bts *bts = lchan->ts->trx->bts;
Harald Welte8470bf22008-12-25 23:28:35 +00001064 u_int8_t mi_type;
Harald Welte231ad4f2008-12-27 11:15:38 +00001065 char mi_string[MI_SIZE];
1066 int rc;
Harald Welte52b1f982008-12-23 20:25:15 +00001067
Harald Welte8470bf22008-12-25 23:28:35 +00001068 lu = (struct gsm48_loc_upd_req *) gh->data;
1069
1070 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
Harald Welte52b1f982008-12-23 20:25:15 +00001071
Harald Weltefc977a82008-12-27 10:19:37 +00001072 mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
1073
Harald Weltea0368542009-06-27 02:58:43 +02001074 DEBUGPC(DMM, "mi_type=0x%02x MI(%s) type=%s ", mi_type, mi_string,
Harald Welte2a139372009-02-22 21:14:55 +00001075 lupd_name(lu->type));
Holger Freyther73487a22008-12-31 18:53:57 +00001076
Holger Freythereaf04692009-06-06 13:54:44 +00001077 /*
1078 * Pseudo Spoof detection: Just drop a second/concurrent
1079 * location updating request.
1080 */
1081 if (lchan->loc_operation) {
Harald Weltea0368542009-06-27 02:58:43 +02001082 DEBUGPC(DMM, "ignoring request due an existing one: %p.\n",
Holger Freythereaf04692009-06-06 13:54:44 +00001083 lchan->loc_operation);
1084 gsm0408_loc_upd_rej(lchan, GSM48_REJECT_PROTOCOL_ERROR);
1085 return 0;
1086 }
1087
Holger Freyther73487a22008-12-31 18:53:57 +00001088 allocate_loc_updating_req(lchan);
1089
Harald Welte52b1f982008-12-23 20:25:15 +00001090 switch (mi_type) {
1091 case GSM_MI_TYPE_IMSI:
Harald Weltea0368542009-06-27 02:58:43 +02001092 DEBUGPC(DMM, "\n");
Harald Welte231ad4f2008-12-27 11:15:38 +00001093 /* we always want the IMEI, too */
Harald Welte015b9ad2009-02-28 18:22:03 +00001094 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMEI);
Holger Freyther73487a22008-12-31 18:53:57 +00001095 lchan->loc_operation->waiting_for_imei = 1;
Holger Freytherc6ea9db2008-12-30 19:18:21 +00001096
Jan Luebbe370b41d2009-08-12 10:19:34 +02001097 /* look up subscriber based on IMSI, create if not found */
1098 subscr = subscr_get_by_imsi(bts->network, mi_string);
1099 if (!subscr) {
1100 subscr = db_create_subscriber(bts->network, mi_string);
1101 }
Harald Welte4b634542008-12-27 01:55:51 +00001102 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001103 case GSM_MI_TYPE_TMSI:
Harald Weltea0368542009-06-27 02:58:43 +02001104 DEBUGPC(DMM, "\n");
Harald Welte231ad4f2008-12-27 11:15:38 +00001105 /* we always want the IMEI, too */
Harald Welte015b9ad2009-02-28 18:22:03 +00001106 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMEI);
Holger Freyther73487a22008-12-31 18:53:57 +00001107 lchan->loc_operation->waiting_for_imei = 1;
Holger Freytherc6ea9db2008-12-30 19:18:21 +00001108
Harald Welte52b1f982008-12-23 20:25:15 +00001109 /* look up the subscriber based on TMSI, request IMSI if it fails */
Harald Welte9176bd42009-07-23 18:46:00 +02001110 subscr = subscr_get_by_tmsi(bts->network, mi_string);
Harald Welte52b1f982008-12-23 20:25:15 +00001111 if (!subscr) {
Harald Welte231ad4f2008-12-27 11:15:38 +00001112 /* send IDENTITY REQUEST message to get IMSI */
Harald Welte255539c2008-12-28 02:26:27 +00001113 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMSI);
Holger Freyther73487a22008-12-31 18:53:57 +00001114 lchan->loc_operation->waiting_for_imsi = 1;
Harald Welte52b1f982008-12-23 20:25:15 +00001115 }
1116 break;
1117 case GSM_MI_TYPE_IMEI:
1118 case GSM_MI_TYPE_IMEISV:
1119 /* no sim card... FIXME: what to do ? */
Harald Weltea0368542009-06-27 02:58:43 +02001120 DEBUGPC(DMM, "unimplemented mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +00001121 break;
1122 default:
Harald Weltea0368542009-06-27 02:58:43 +02001123 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +00001124 break;
1125 }
1126
Harald Welte24516ea2009-07-04 10:18:00 +02001127 /* schedule the reject timer */
1128 schedule_reject(lchan);
1129
Harald Welte4bfdfe72009-06-10 23:11:52 +08001130 if (!subscr) {
Harald Weltea0368542009-06-27 02:58:43 +02001131 DEBUGPC(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001132 /* FIXME: request id? close channel? */
1133 return -EINVAL;
1134 }
1135
Harald Welte255539c2008-12-28 02:26:27 +00001136 lchan->subscr = subscr;
Harald Welte (local)ee4410a2009-08-17 09:39:55 +02001137 lchan->subscr->equipment.classmark1 = lu->classmark1;
Harald Welte255539c2008-12-28 02:26:27 +00001138
Harald Welte24516ea2009-07-04 10:18:00 +02001139 /* check if we can let the subscriber into our network immediately
1140 * or if we need to wait for identity responses. */
Holger Freytherd51524f2009-06-09 08:27:07 +00001141 return gsm0408_authorize(lchan, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001142}
1143
Harald Welte13cac662009-07-29 12:10:35 +02001144/* 9.1.5 Channel mode modify: Modify the mode on the MS side */
Harald Welte7584aea2009-02-11 11:44:12 +00001145int gsm48_tx_chan_mode_modify(struct gsm_lchan *lchan, u_int8_t mode)
1146{
1147 struct msgb *msg = gsm48_msgb_alloc();
1148 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1149 struct gsm48_chan_mode_modify *cmm =
1150 (struct gsm48_chan_mode_modify *) msgb_put(msg, sizeof(*cmm));
Harald Welte4a543e82009-02-28 13:17:55 +00001151 u_int16_t arfcn = lchan->ts->trx->arfcn & 0x3ff;
Harald Welte7584aea2009-02-11 11:44:12 +00001152
Harald Welte4a543e82009-02-28 13:17:55 +00001153 DEBUGP(DRR, "-> CHANNEL MODE MODIFY mode=0x%02x\n", mode);
Harald Welte7ccf7782009-02-17 01:43:01 +00001154
Harald Welte45b407a2009-05-23 15:51:12 +00001155 lchan->tch_mode = mode;
Harald Welte7584aea2009-02-11 11:44:12 +00001156 msg->lchan = lchan;
1157 gh->proto_discr = GSM48_PDISC_RR;
1158 gh->msg_type = GSM48_MT_RR_CHAN_MODE_MODIF;
1159
1160 /* fill the channel information element, this code
1161 * should probably be shared with rsl_rx_chan_rqd() */
1162 cmm->chan_desc.chan_nr = lchan2chan_nr(lchan);
Harald Welte02b0e092009-02-28 13:11:07 +00001163 cmm->chan_desc.h0.tsc = lchan->ts->trx->bts->tsc;
Harald Welte7584aea2009-02-11 11:44:12 +00001164 cmm->chan_desc.h0.h = 0;
1165 cmm->chan_desc.h0.arfcn_high = arfcn >> 8;
1166 cmm->chan_desc.h0.arfcn_low = arfcn & 0xff;
1167 cmm->mode = mode;
1168
Harald Welte39e2ead2009-07-23 21:13:03 +02001169 return gsm48_sendmsg(msg, NULL);
Harald Welte7584aea2009-02-11 11:44:12 +00001170}
1171
Harald Welte4bfdfe72009-06-10 23:11:52 +08001172#if 0
1173static u_int8_t to_bcd8(u_int8_t val)
1174{
1175 return ((val / 10) << 4) | (val % 10);
1176}
1177#endif
1178
Harald Weltedb253af2008-12-30 17:56:55 +00001179/* Section 9.2.15a */
1180int gsm48_tx_mm_info(struct gsm_lchan *lchan)
1181{
1182 struct msgb *msg = gsm48_msgb_alloc();
1183 struct gsm48_hdr *gh;
1184 struct gsm_network *net = lchan->ts->trx->bts->network;
Harald Weltedb253af2008-12-30 17:56:55 +00001185 u_int8_t *ptr8;
1186 u_int16_t *ptr16;
Daniel Willmanneea93372009-08-13 03:42:07 +02001187 int name_len, name_pad;
Harald Weltedb253af2008-12-30 17:56:55 +00001188 int i;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001189#if 0
1190 time_t cur_t;
1191 struct tm* cur_time;
1192 int tz15min;
1193#endif
Harald Weltedb253af2008-12-30 17:56:55 +00001194
1195 msg->lchan = lchan;
1196
1197 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1198 gh->proto_discr = GSM48_PDISC_MM;
1199 gh->msg_type = GSM48_MT_MM_INFO;
1200
1201 if (net->name_long) {
Daniel Willmanneea93372009-08-13 03:42:07 +02001202#if 0
Harald Weltedb253af2008-12-30 17:56:55 +00001203 name_len = strlen(net->name_long);
1204 /* 10.5.3.5a */
1205 ptr8 = msgb_put(msg, 3);
1206 ptr8[0] = GSM48_IE_NAME_LONG;
1207 ptr8[1] = name_len*2 +1;
1208 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
1209
1210 ptr16 = (u_int16_t *) msgb_put(msg, name_len*2);
1211 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +00001212 ptr16[i] = htons(net->name_long[i]);
Harald Weltedb253af2008-12-30 17:56:55 +00001213
1214 /* FIXME: Use Cell Broadcast, not UCS-2, since
1215 * UCS-2 is only supported by later revisions of the spec */
Daniel Willmanneea93372009-08-13 03:42:07 +02001216#endif
1217 name_len = (strlen(net->name_long)*7)/8;
1218 name_pad = (8 - strlen(net->name_long)*7)%8;
1219 if (name_pad > 0)
1220 name_len++;
1221 /* 10.5.3.5a */
1222 ptr8 = msgb_put(msg, 3);
1223 ptr8[0] = GSM48_IE_NAME_LONG;
1224 ptr8[1] = name_len +1;
1225 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
1226
1227 ptr8 = msgb_put(msg, name_len);
1228 gsm_7bit_encode(ptr8, net->name_long);
1229
Harald Weltedb253af2008-12-30 17:56:55 +00001230 }
1231
1232 if (net->name_short) {
Daniel Willmanneea93372009-08-13 03:42:07 +02001233#if 0
Harald Weltedb253af2008-12-30 17:56:55 +00001234 name_len = strlen(net->name_short);
1235 /* 10.5.3.5a */
1236 ptr8 = (u_int8_t *) msgb_put(msg, 3);
Harald Welte7543eb72009-07-19 17:51:36 +02001237 ptr8[0] = GSM48_IE_NAME_SHORT;
Harald Weltedb253af2008-12-30 17:56:55 +00001238 ptr8[1] = name_len*2 + 1;
1239 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
1240
Harald Weltee872cb12009-01-01 00:33:37 +00001241 ptr16 = (u_int16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +00001242 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +00001243 ptr16[i] = htons(net->name_short[i]);
Daniel Willmanneea93372009-08-13 03:42:07 +02001244#endif
1245 name_len = (strlen(net->name_short)*7)/8;
1246 name_pad = (8 - strlen(net->name_short)*7)%8;
1247 if (name_pad > 0)
1248 name_len++;
1249 /* 10.5.3.5a */
1250 ptr8 = (u_int8_t *) msgb_put(msg, 3);
1251 ptr8[0] = GSM48_IE_NAME_SHORT;
1252 ptr8[1] = name_len +1;
1253 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
1254
1255 ptr8 = msgb_put(msg, name_len);
1256 gsm_7bit_encode(ptr8, net->name_short);
1257
Harald Weltedb253af2008-12-30 17:56:55 +00001258 }
1259
1260#if 0
1261 /* Section 10.5.3.9 */
1262 cur_t = time(NULL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001263 cur_time = gmtime(&cur_t);
Harald Weltedb253af2008-12-30 17:56:55 +00001264 ptr8 = msgb_put(msg, 8);
1265 ptr8[0] = GSM48_IE_NET_TIME_TZ;
1266 ptr8[1] = to_bcd8(cur_time->tm_year % 100);
1267 ptr8[2] = to_bcd8(cur_time->tm_mon);
1268 ptr8[3] = to_bcd8(cur_time->tm_mday);
1269 ptr8[4] = to_bcd8(cur_time->tm_hour);
1270 ptr8[5] = to_bcd8(cur_time->tm_min);
1271 ptr8[6] = to_bcd8(cur_time->tm_sec);
1272 /* 02.42: coded as BCD encoded signed value in units of 15 minutes */
1273 tz15min = (cur_time->tm_gmtoff)/(60*15);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001274 ptr8[7] = to_bcd8(tz15min);
Harald Weltedb253af2008-12-30 17:56:55 +00001275 if (tz15min < 0)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001276 ptr8[7] |= 0x80;
Harald Weltedb253af2008-12-30 17:56:55 +00001277#endif
1278
Daniel Willmanneea93372009-08-13 03:42:07 +02001279 DEBUGP(DMM, "-> MM INFO\n");
1280
Harald Welte39e2ead2009-07-23 21:13:03 +02001281 return gsm48_sendmsg(msg, NULL);
Harald Weltedb253af2008-12-30 17:56:55 +00001282}
1283
Harald Welte7984d5c2009-08-12 22:56:50 +02001284/* Section 9.2.2 */
1285int gsm48_tx_mm_auth_req(struct gsm_lchan *lchan, u_int8_t *rand)
1286{
1287 struct msgb *msg = gsm48_msgb_alloc();
1288 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1289 u_int8_t *r;
1290
1291 DEBUGP(DMM, "-> AUTH REQ\n");
1292
1293 msg->lchan = lchan;
1294 gh->proto_discr = GSM48_PDISC_MM;
1295 gh->msg_type = GSM48_MT_MM_AUTH_REQ;
1296
1297 /* 16 bytes RAND parameters */
1298 r = msgb_put(msg, 16);
1299 if (rand)
1300 memcpy(r, rand, 16);
1301
1302 return gsm48_sendmsg(msg, NULL);
1303}
1304
1305/* Section 9.2.1 */
1306int gsm48_tx_mm_auth_rej(struct gsm_lchan *lchan)
1307{
1308 DEBUGP(DMM, "-> AUTH REJECT\n");
1309 return gsm48_tx_simple(lchan, GSM48_PDISC_MM, GSM48_MT_MM_AUTH_REJ);
1310}
1311
Harald Welte4b634542008-12-27 01:55:51 +00001312static int gsm48_tx_mm_serv_ack(struct gsm_lchan *lchan)
1313{
Harald Welte4b634542008-12-27 01:55:51 +00001314 DEBUGP(DMM, "-> CM SERVICE ACK\n");
Harald Welte65e74cc2008-12-29 01:55:35 +00001315 return gsm48_tx_simple(lchan, GSM48_PDISC_MM, GSM48_MT_MM_CM_SERV_ACC);
Harald Welte4b634542008-12-27 01:55:51 +00001316}
Harald Welteba4cf162009-01-10 01:49:35 +00001317
1318/* 9.2.6 CM service reject */
1319static int gsm48_tx_mm_serv_rej(struct gsm_lchan *lchan,
1320 enum gsm48_reject_value value)
1321{
1322 struct msgb *msg = gsm48_msgb_alloc();
1323 struct gsm48_hdr *gh;
1324
1325 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
1326
1327 msg->lchan = lchan;
1328 use_lchan(lchan);
1329
1330 gh->proto_discr = GSM48_PDISC_MM;
1331 gh->msg_type = GSM48_MT_MM_CM_SERV_REJ;
1332 gh->data[0] = value;
1333 DEBUGP(DMM, "-> CM SERVICE Reject cause: %d\n", value);
1334
Harald Welte39e2ead2009-07-23 21:13:03 +02001335 return gsm48_sendmsg(msg, NULL);
Harald Welteba4cf162009-01-10 01:49:35 +00001336}
1337
Harald Welte3ac7f102009-08-10 10:12:45 +02001338static int send_siemens_mrpci(struct gsm_lchan *lchan,
1339 u_int8_t *classmark2_lv)
1340{
1341 struct rsl_mrpci mrpci;
1342
1343 if (classmark2_lv[0] < 2)
1344 return -EINVAL;
1345
1346 mrpci.power_class = classmark2_lv[1] & 0x7;
1347 mrpci.vgcs_capable = classmark2_lv[2] & (1 << 1);
1348 mrpci.vbs_capable = classmark2_lv[2] & (1 <<2);
1349 mrpci.gsm_phase = (classmark2_lv[1]) >> 5 & 0x3;
1350
1351 return rsl_siemens_mrpci(lchan, &mrpci);
1352}
Harald Welte4ed0e922009-01-10 03:17:30 +00001353
1354/*
1355 * Handle CM Service Requests
1356 * a) Verify that the packet is long enough to contain the information
1357 * we require otherwsie reject with INCORRECT_MESSAGE
1358 * b) Try to parse the TMSI. If we do not have one reject
1359 * c) Check that we know the subscriber with the TMSI otherwise reject
1360 * with a HLR cause
1361 * d) Set the subscriber on the gsm_lchan and accept
1362 */
Harald Welte4b634542008-12-27 01:55:51 +00001363static int gsm48_rx_mm_serv_req(struct msgb *msg)
1364{
Harald Welteba4cf162009-01-10 01:49:35 +00001365 u_int8_t mi_type;
Harald Welte4ed0e922009-01-10 03:17:30 +00001366 char mi_string[MI_SIZE];
Harald Welte4b634542008-12-27 01:55:51 +00001367
Harald Welte9176bd42009-07-23 18:46:00 +02001368 struct gsm_bts *bts = msg->lchan->ts->trx->bts;
Harald Welteba4cf162009-01-10 01:49:35 +00001369 struct gsm_subscriber *subscr;
1370 struct gsm48_hdr *gh = msgb_l3(msg);
1371 struct gsm48_service_request *req =
1372 (struct gsm48_service_request *)gh->data;
Harald Weltec9e02182009-05-01 19:07:53 +00001373 /* unfortunately in Phase1 the classmar2 length is variable */
1374 u_int8_t classmark2_len = gh->data[1];
1375 u_int8_t *classmark2 = gh->data+2;
1376 u_int8_t mi_len = *(classmark2 + classmark2_len);
1377 u_int8_t *mi = (classmark2 + classmark2_len + 1);
Harald Welteba4cf162009-01-10 01:49:35 +00001378
Harald Weltec9e02182009-05-01 19:07:53 +00001379 DEBUGP(DMM, "<- CM SERVICE REQUEST ");
Harald Welteba4cf162009-01-10 01:49:35 +00001380 if (msg->data_len < sizeof(struct gsm48_service_request*)) {
Harald Weltec9e02182009-05-01 19:07:53 +00001381 DEBUGPC(DMM, "wrong sized message\n");
Harald Welteba4cf162009-01-10 01:49:35 +00001382 return gsm48_tx_mm_serv_rej(msg->lchan,
1383 GSM48_REJECT_INCORRECT_MESSAGE);
1384 }
1385
1386 if (msg->data_len < req->mi_len + 6) {
Harald Weltec9e02182009-05-01 19:07:53 +00001387 DEBUGPC(DMM, "does not fit in packet\n");
Harald Welteba4cf162009-01-10 01:49:35 +00001388 return gsm48_tx_mm_serv_rej(msg->lchan,
1389 GSM48_REJECT_INCORRECT_MESSAGE);
1390 }
1391
Harald Weltec9e02182009-05-01 19:07:53 +00001392 mi_type = mi[0] & GSM_MI_TYPE_MASK;
Harald Welteba4cf162009-01-10 01:49:35 +00001393 if (mi_type != GSM_MI_TYPE_TMSI) {
Harald Weltec9e02182009-05-01 19:07:53 +00001394 DEBUGPC(DMM, "mi_type is not TMSI: %d\n", mi_type);
Harald Welteba4cf162009-01-10 01:49:35 +00001395 return gsm48_tx_mm_serv_rej(msg->lchan,
1396 GSM48_REJECT_INCORRECT_MESSAGE);
1397 }
1398
Harald Weltec9e02182009-05-01 19:07:53 +00001399 mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
Harald Weltec9e02182009-05-01 19:07:53 +00001400 DEBUGPC(DMM, "serv_type=0x%02x mi_type=0x%02x M(%s)\n",
Harald Welte4ed0e922009-01-10 03:17:30 +00001401 req->cm_service_type, mi_type, mi_string);
Harald Weltebcae43f2008-12-27 21:45:37 +00001402
Harald Welte3ac7f102009-08-10 10:12:45 +02001403 if (is_siemens_bts(bts))
1404 send_siemens_mrpci(msg->lchan, classmark2-1);
1405
Harald Welte9176bd42009-07-23 18:46:00 +02001406 subscr = subscr_get_by_tmsi(bts->network, mi_string);
Holger Freythereb443982009-06-04 13:58:42 +00001407
Harald Welte2a139372009-02-22 21:14:55 +00001408 /* FIXME: if we don't know the TMSI, inquire abit IMSI and allocate new TMSI */
Harald Welte4ed0e922009-01-10 03:17:30 +00001409 if (!subscr)
1410 return gsm48_tx_mm_serv_rej(msg->lchan,
1411 GSM48_REJECT_IMSI_UNKNOWN_IN_HLR);
1412
1413 if (!msg->lchan->subscr)
1414 msg->lchan->subscr = subscr;
Harald Welte9bb7c702009-01-10 03:21:41 +00001415 else if (msg->lchan->subscr != subscr) {
1416 DEBUGP(DMM, "<- CM Channel already owned by someone else?\n");
1417 subscr_put(subscr);
1418 }
1419
Harald Weltec2e302d2009-07-05 14:08:13 +02001420 subscr->equipment.classmark2_len = classmark2_len;
1421 memcpy(subscr->equipment.classmark2, classmark2, classmark2_len);
1422 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +00001423
Harald Welte4b634542008-12-27 01:55:51 +00001424 return gsm48_tx_mm_serv_ack(msg->lchan);
1425}
1426
Harald Welte2a139372009-02-22 21:14:55 +00001427static int gsm48_rx_mm_imsi_detach_ind(struct msgb *msg)
1428{
Harald Welte9176bd42009-07-23 18:46:00 +02001429 struct gsm_bts *bts = msg->lchan->ts->trx->bts;
Harald Welte2a139372009-02-22 21:14:55 +00001430 struct gsm48_hdr *gh = msgb_l3(msg);
1431 struct gsm48_imsi_detach_ind *idi =
1432 (struct gsm48_imsi_detach_ind *) gh->data;
1433 u_int8_t mi_type = idi->mi[0] & GSM_MI_TYPE_MASK;
1434 char mi_string[MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +08001435 struct gsm_subscriber *subscr = NULL;
Harald Welte2a139372009-02-22 21:14:55 +00001436
1437 mi_to_string(mi_string, sizeof(mi_string), idi->mi, idi->mi_len);
1438 DEBUGP(DMM, "IMSI DETACH INDICATION: mi_type=0x%02x MI(%s): ",
1439 mi_type, mi_string);
1440
1441 switch (mi_type) {
1442 case GSM_MI_TYPE_TMSI:
Harald Welte9176bd42009-07-23 18:46:00 +02001443 subscr = subscr_get_by_tmsi(bts->network, mi_string);
Harald Welte2a139372009-02-22 21:14:55 +00001444 break;
1445 case GSM_MI_TYPE_IMSI:
Harald Welte9176bd42009-07-23 18:46:00 +02001446 subscr = subscr_get_by_imsi(bts->network, mi_string);
Harald Welte2a139372009-02-22 21:14:55 +00001447 break;
1448 case GSM_MI_TYPE_IMEI:
1449 case GSM_MI_TYPE_IMEISV:
1450 /* no sim card... FIXME: what to do ? */
Holger Freyther79f4ae62009-06-02 03:25:04 +00001451 DEBUGPC(DMM, "unimplemented mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +00001452 break;
1453 default:
Holger Freyther79f4ae62009-06-02 03:25:04 +00001454 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +00001455 break;
1456 }
1457
Holger Freyther4a49e772009-04-12 05:37:29 +00001458 if (subscr) {
1459 subscr_update(subscr, msg->trx->bts,
1460 GSM_SUBSCRIBER_UPDATE_DETACHED);
Harald Welte2a139372009-02-22 21:14:55 +00001461 DEBUGP(DMM, "Subscriber: %s\n",
1462 subscr->name ? subscr->name : subscr->imsi);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +02001463
1464 subscr->equipment.classmark1 = idi->classmark1;
1465 db_sync_equipment(&subscr->equipment);
1466
Holger Freytherc21cfbc2009-06-02 02:54:57 +00001467 subscr_put(subscr);
Holger Freyther4a49e772009-04-12 05:37:29 +00001468 } else
Harald Welte2a139372009-02-22 21:14:55 +00001469 DEBUGP(DMM, "Unknown Subscriber ?!?\n");
1470
Harald Welte2a139372009-02-22 21:14:55 +00001471 return 0;
1472}
1473
Harald Welted2a7f5a2009-06-05 20:08:20 +00001474static int gsm48_rx_mm_status(struct msgb *msg)
1475{
1476 struct gsm48_hdr *gh = msgb_l3(msg);
1477
1478 DEBUGP(DMM, "MM STATUS (reject cause 0x%02x)\n", gh->data[0]);
1479
1480 return 0;
1481}
1482
Harald Weltebf5e8df2009-02-03 12:59:45 +00001483/* Receive a GSM 04.08 Mobility Management (MM) message */
Harald Welte52b1f982008-12-23 20:25:15 +00001484static int gsm0408_rcv_mm(struct msgb *msg)
1485{
1486 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001487 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001488
1489 switch (gh->msg_type & 0xbf) {
1490 case GSM48_MT_MM_LOC_UPD_REQUEST:
Harald Weltea0368542009-06-27 02:58:43 +02001491 DEBUGP(DMM, "LOCATION UPDATING REQUEST: ");
Harald Welte231ad4f2008-12-27 11:15:38 +00001492 rc = mm_rx_loc_upd_req(msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001493 break;
1494 case GSM48_MT_MM_ID_RESP:
Harald Welte231ad4f2008-12-27 11:15:38 +00001495 rc = mm_rx_id_resp(msg);
1496 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001497 case GSM48_MT_MM_CM_SERV_REQ:
Harald Welte4b634542008-12-27 01:55:51 +00001498 rc = gsm48_rx_mm_serv_req(msg);
1499 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001500 case GSM48_MT_MM_STATUS:
Harald Welted2a7f5a2009-06-05 20:08:20 +00001501 rc = gsm48_rx_mm_status(msg);
Harald Welte231ad4f2008-12-27 11:15:38 +00001502 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001503 case GSM48_MT_MM_TMSI_REALL_COMPL:
Harald Welte69b2af22009-01-06 19:47:00 +00001504 DEBUGP(DMM, "TMSI Reallocation Completed. Subscriber: %s\n",
1505 msg->lchan->subscr ?
1506 msg->lchan->subscr->imsi :
1507 "unknown subscriber");
1508 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001509 case GSM48_MT_MM_IMSI_DETACH_IND:
Harald Welte2a139372009-02-22 21:14:55 +00001510 rc = gsm48_rx_mm_imsi_detach_ind(msg);
1511 break;
1512 case GSM48_MT_MM_CM_REEST_REQ:
1513 DEBUGP(DMM, "CM REESTABLISH REQUEST: Not implemented\n");
1514 break;
1515 case GSM48_MT_MM_AUTH_RESP:
1516 DEBUGP(DMM, "AUTHENTICATION RESPONSE: Not implemented\n");
Harald Welte52b1f982008-12-23 20:25:15 +00001517 break;
1518 default:
1519 fprintf(stderr, "Unknown GSM 04.08 MM msg type 0x%02x\n",
1520 gh->msg_type);
1521 break;
1522 }
1523
1524 return rc;
1525}
Harald Weltebf5e8df2009-02-03 12:59:45 +00001526
Harald Welte2d35ae62009-02-06 12:02:13 +00001527/* Receive a PAGING RESPONSE message from the MS */
1528static int gsm48_rr_rx_pag_resp(struct msgb *msg)
1529{
Harald Welte9176bd42009-07-23 18:46:00 +02001530 struct gsm_bts *bts = msg->lchan->ts->trx->bts;
Harald Welte2d35ae62009-02-06 12:02:13 +00001531 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte61548982009-02-22 21:26:29 +00001532 u_int8_t *classmark2_lv = gh->data + 1;
1533 u_int8_t *mi_lv = gh->data + 2 + *classmark2_lv;
1534 u_int8_t mi_type = mi_lv[1] & GSM_MI_TYPE_MASK;
Harald Welte2d35ae62009-02-06 12:02:13 +00001535 char mi_string[MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +08001536 struct gsm_subscriber *subscr = NULL;
Harald Welte595ad7b2009-02-16 22:05:44 +00001537 struct paging_signal_data sig_data;
Harald Welte2d35ae62009-02-06 12:02:13 +00001538 int rc = 0;
1539
Harald Welte61548982009-02-22 21:26:29 +00001540 mi_to_string(mi_string, sizeof(mi_string), mi_lv+1, *mi_lv);
Harald Welte2d35ae62009-02-06 12:02:13 +00001541 DEBUGP(DRR, "PAGING RESPONSE: mi_type=0x%02x MI(%s)\n",
1542 mi_type, mi_string);
Harald Welte3ac7f102009-08-10 10:12:45 +02001543
1544 if (is_siemens_bts(bts))
1545 send_siemens_mrpci(msg->lchan, classmark2_lv);
1546
Harald Weltefe18d8f2009-02-22 21:14:24 +00001547 switch (mi_type) {
1548 case GSM_MI_TYPE_TMSI:
Harald Welte9176bd42009-07-23 18:46:00 +02001549 subscr = subscr_get_by_tmsi(bts->network, mi_string);
Harald Weltefe18d8f2009-02-22 21:14:24 +00001550 break;
1551 case GSM_MI_TYPE_IMSI:
Harald Welte9176bd42009-07-23 18:46:00 +02001552 subscr = subscr_get_by_imsi(bts->network, mi_string);
Harald Weltefe18d8f2009-02-22 21:14:24 +00001553 break;
1554 }
Harald Welte2d35ae62009-02-06 12:02:13 +00001555
1556 if (!subscr) {
1557 DEBUGP(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte09e38af2009-02-16 22:52:23 +00001558 /* FIXME: request id? close channel? */
Harald Welte2d35ae62009-02-06 12:02:13 +00001559 return -EINVAL;
1560 }
1561 DEBUGP(DRR, "<- Channel was requested by %s\n",
Harald Welte76042182009-08-08 16:03:15 +02001562 subscr->name && strlen(subscr->name) ? subscr->name : subscr->imsi);
Holger Freyther053e09d2009-02-14 22:51:06 +00001563
Harald Weltec2e302d2009-07-05 14:08:13 +02001564 subscr->equipment.classmark2_len = *classmark2_lv;
1565 memcpy(subscr->equipment.classmark2, classmark2_lv+1, *classmark2_lv);
1566 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +00001567
Holger Freytherc21cfbc2009-06-02 02:54:57 +00001568 if (!msg->lchan->subscr) {
Holger Freyther2fa4cb52009-02-14 23:53:15 +00001569 msg->lchan->subscr = subscr;
Holger Freytherc21cfbc2009-06-02 02:54:57 +00001570 } else if (msg->lchan->subscr != subscr) {
Holger Freyther2fa4cb52009-02-14 23:53:15 +00001571 DEBUGP(DRR, "<- Channel already owned by someone else?\n");
1572 subscr_put(subscr);
Holger Freytherc21cfbc2009-06-02 02:54:57 +00001573 return -EINVAL;
1574 } else {
1575 DEBUGP(DRR, "<- Channel already owned by us\n");
1576 subscr_put(subscr);
1577 subscr = msg->lchan->subscr;
Holger Freyther2fa4cb52009-02-14 23:53:15 +00001578 }
1579
Harald Welte595ad7b2009-02-16 22:05:44 +00001580 sig_data.subscr = subscr;
1581 sig_data.bts = msg->lchan->ts->trx->bts;
1582 sig_data.lchan = msg->lchan;
1583
1584 dispatch_signal(SS_PAGING, S_PAGING_COMPLETED, &sig_data);
Harald Weltebe143102009-06-10 11:21:55 +08001585
1586 /* Stop paging on the bts we received the paging response */
Harald Welte7ccf7782009-02-17 01:43:01 +00001587 paging_request_stop(msg->trx->bts, subscr, msg->lchan);
Harald Welte2d35ae62009-02-06 12:02:13 +00001588
Harald Welte7584aea2009-02-11 11:44:12 +00001589 /* FIXME: somehow signal the completion of the PAGING to
1590 * the entity that requested the paging */
1591
Harald Welte2d35ae62009-02-06 12:02:13 +00001592 return rc;
1593}
1594
Harald Weltef7c43522009-06-09 20:24:21 +00001595static int gsm48_rx_rr_classmark(struct msgb *msg)
1596{
1597 struct gsm48_hdr *gh = msgb_l3(msg);
1598 struct gsm_subscriber *subscr = msg->lchan->subscr;
1599 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1600 u_int8_t cm2_len, cm3_len = 0;
1601 u_int8_t *cm2, *cm3 = NULL;
1602
1603 DEBUGP(DRR, "CLASSMARK CHANGE ");
1604
1605 /* classmark 2 */
1606 cm2_len = gh->data[0];
1607 cm2 = &gh->data[1];
1608 DEBUGPC(DRR, "CM2(len=%u) ", cm2_len);
1609
1610 if (payload_len > cm2_len + 1) {
1611 /* we must have a classmark3 */
1612 if (gh->data[cm2_len+1] != 0x20) {
1613 DEBUGPC(DRR, "ERR CM3 TAG\n");
1614 return -EINVAL;
1615 }
1616 if (cm2_len > 3) {
1617 DEBUGPC(DRR, "CM2 too long!\n");
1618 return -EINVAL;
1619 }
1620
1621 cm3_len = gh->data[cm2_len+2];
1622 cm3 = &gh->data[cm2_len+3];
1623 if (cm3_len > 14) {
1624 DEBUGPC(DRR, "CM3 len %u too long!\n", cm3_len);
1625 return -EINVAL;
1626 }
1627 DEBUGPC(DRR, "CM3(len=%u)\n", cm3_len);
1628 }
1629 if (subscr) {
Harald Weltec2e302d2009-07-05 14:08:13 +02001630 subscr->equipment.classmark2_len = cm2_len;
1631 memcpy(subscr->equipment.classmark2, cm2, cm2_len);
Harald Weltef7c43522009-06-09 20:24:21 +00001632 if (cm3) {
Harald Weltec2e302d2009-07-05 14:08:13 +02001633 subscr->equipment.classmark3_len = cm3_len;
1634 memcpy(subscr->equipment.classmark3, cm3, cm3_len);
Harald Weltef7c43522009-06-09 20:24:21 +00001635 }
Harald Weltec2e302d2009-07-05 14:08:13 +02001636 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +00001637 }
1638
Harald Weltef7c43522009-06-09 20:24:21 +00001639 return 0;
1640}
1641
Harald Weltecf5b3592009-05-01 18:28:42 +00001642static int gsm48_rx_rr_status(struct msgb *msg)
1643{
1644 struct gsm48_hdr *gh = msgb_l3(msg);
1645
1646 DEBUGP(DRR, "STATUS rr_cause = %s\n",
1647 rr_cause_name(gh->data[0]));
1648
1649 return 0;
1650}
1651
Harald Weltef7c43522009-06-09 20:24:21 +00001652static int gsm48_rx_rr_meas_rep(struct msgb *msg)
1653{
1654 struct gsm48_hdr *gh = msgb_l3(msg);
1655 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1656 static struct gsm_meas_rep meas_rep;
1657
Harald Welte10d0e672009-06-27 02:53:10 +02001658 DEBUGP(DMEAS, "MEASUREMENT REPORT ");
Harald Weltef7c43522009-06-09 20:24:21 +00001659 parse_meas_rep(&meas_rep, gh->data, payload_len);
1660 if (meas_rep.flags & MEAS_REP_F_DTX)
Harald Welte10d0e672009-06-27 02:53:10 +02001661 DEBUGPC(DMEAS, "DTX ");
Harald Weltef7c43522009-06-09 20:24:21 +00001662 if (meas_rep.flags & MEAS_REP_F_BA1)
Harald Welte10d0e672009-06-27 02:53:10 +02001663 DEBUGPC(DMEAS, "BA1 ");
Harald Weltef7c43522009-06-09 20:24:21 +00001664 if (!(meas_rep.flags & MEAS_REP_F_VALID))
Harald Welte10d0e672009-06-27 02:53:10 +02001665 DEBUGPC(DMEAS, "NOT VALID ");
Harald Weltef7c43522009-06-09 20:24:21 +00001666 else
Harald Welte10d0e672009-06-27 02:53:10 +02001667 DEBUGPC(DMEAS, "FULL(lev=%u, qual=%u) SUB(lev=%u, qual=%u) ",
Harald Weltef7c43522009-06-09 20:24:21 +00001668 meas_rep.rxlev_full, meas_rep.rxqual_full, meas_rep.rxlev_sub,
1669 meas_rep.rxqual_sub);
1670
Harald Welte10d0e672009-06-27 02:53:10 +02001671 DEBUGPC(DMEAS, "NUM_NEIGH=%u\n", meas_rep.num_cell);
Harald Weltef7c43522009-06-09 20:24:21 +00001672
1673 /* FIXME: put the results somwhere */
1674
1675 return 0;
1676}
1677
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001678static int gsm48_rx_rr_app_info(struct msgb *msg)
1679{
1680 struct gsm48_hdr *gh = msgb_l3(msg);
1681 u_int8_t apdu_id_flags;
1682 u_int8_t apdu_len;
1683 u_int8_t *apdu_data;
1684
1685 apdu_id_flags = gh->data[0];
1686 apdu_len = gh->data[1];
1687 apdu_data = gh->data+2;
1688
1689 DEBUGP(DNM, "RX APPLICATION INFO id/flags=0x%02x apdu_len=%u apdu=%s",
1690 apdu_id_flags, apdu_len, hexdump(apdu_data, apdu_len));
1691
Harald Welte (local)026531e2009-08-16 10:40:10 +02001692 return db_apdu_blob_store(msg->lchan->subscr, apdu_id_flags, apdu_len, apdu_data);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001693}
1694
1695
Harald Weltebf5e8df2009-02-03 12:59:45 +00001696/* Receive a GSM 04.08 Radio Resource (RR) message */
Harald Welte52b1f982008-12-23 20:25:15 +00001697static int gsm0408_rcv_rr(struct msgb *msg)
1698{
1699 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001700 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001701
1702 switch (gh->msg_type) {
1703 case GSM48_MT_RR_CLSM_CHG:
Harald Weltef7c43522009-06-09 20:24:21 +00001704 rc = gsm48_rx_rr_classmark(msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001705 break;
Harald Weltefc977a82008-12-27 10:19:37 +00001706 case GSM48_MT_RR_GPRS_SUSP_REQ:
1707 DEBUGP(DRR, "GRPS SUSPEND REQUEST\n");
1708 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001709 case GSM48_MT_RR_PAG_RESP:
Harald Welte2d35ae62009-02-06 12:02:13 +00001710 rc = gsm48_rr_rx_pag_resp(msg);
1711 break;
Harald Welte7ccf7782009-02-17 01:43:01 +00001712 case GSM48_MT_RR_CHAN_MODE_MODIF_ACK:
1713 DEBUGP(DRR, "CHANNEL MODE MODIFY ACK\n");
Harald Welte13cac662009-07-29 12:10:35 +02001714 /* We've successfully modified the MS side of the channel,
1715 * now go on to modify the BTS side of the channel */
Harald Welte9943c5b2009-07-29 15:41:29 +02001716 msg->lchan->rsl_cmode = RSL_CMOD_SPD_SPEECH;
Harald Welte2c38aa82009-02-18 03:44:24 +00001717 rc = rsl_chan_mode_modify_req(msg->lchan);
Harald Welte7ccf7782009-02-17 01:43:01 +00001718 break;
Harald Weltecf5b3592009-05-01 18:28:42 +00001719 case GSM48_MT_RR_STATUS:
1720 rc = gsm48_rx_rr_status(msg);
1721 break;
Harald Weltef7c43522009-06-09 20:24:21 +00001722 case GSM48_MT_RR_MEAS_REP:
1723 rc = gsm48_rx_rr_meas_rep(msg);
1724 break;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001725 case GSM48_MT_RR_APP_INFO:
1726 rc = gsm48_rx_rr_app_info(msg);
1727 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001728 default:
Harald Welte2d35ae62009-02-06 12:02:13 +00001729 fprintf(stderr, "Unimplemented GSM 04.08 RR msg type 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +00001730 gh->msg_type);
1731 break;
1732 }
1733
Harald Welte2d35ae62009-02-06 12:02:13 +00001734 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +00001735}
1736
Holger Freythere64a7a32009-02-06 21:55:37 +00001737
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001738int gsm48_send_rr_app_info(struct gsm_lchan *lchan, u_int8_t apdu_id,
1739 u_int8_t apdu_len, u_int8_t *apdu)
1740{
1741 struct msgb *msg = gsm48_msgb_alloc();
1742 struct gsm48_hdr *gh;
1743
1744 msg->lchan = lchan;
1745
1746 DEBUGP(DRR, "TX APPLICATION INFO id=0x%02x, len=%u\n",
1747 apdu_id, apdu_len);
1748
1749 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 2 + apdu_len);
1750 gh->proto_discr = GSM48_PDISC_RR;
1751 gh->msg_type = GSM48_MT_RR_APP_INFO;
1752 gh->data[0] = apdu_id;
1753 gh->data[1] = apdu_len;
1754 memcpy(gh->data+2, apdu, apdu_len);
1755
1756 return gsm48_sendmsg(msg, NULL);
1757}
1758
Harald Welte4bc90a12008-12-27 16:32:52 +00001759/* Call Control */
1760
Harald Welte7584aea2009-02-11 11:44:12 +00001761/* The entire call control code is written in accordance with Figure 7.10c
1762 * for 'very early assignment', i.e. we allocate a TCH/F during IMMEDIATE
1763 * ASSIGN, then first use that TCH/F for signalling and later MODE MODIFY
1764 * it for voice */
1765
Harald Welte4bfdfe72009-06-10 23:11:52 +08001766static void new_cc_state(struct gsm_trans *trans, int state)
1767{
1768 if (state > 31 || state < 0)
1769 return;
1770
1771 DEBUGP(DCC, "new state %s -> %s\n",
Harald Weltedcaf5652009-07-23 18:56:43 +02001772 cc_state_names[trans->cc.state], cc_state_names[state]);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001773
Harald Weltedcaf5652009-07-23 18:56:43 +02001774 trans->cc.state = state;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001775}
1776
1777static int gsm48_cc_tx_status(struct gsm_trans *trans, void *arg)
Harald Welte4bc90a12008-12-27 16:32:52 +00001778{
1779 struct msgb *msg = gsm48_msgb_alloc();
1780 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1781 u_int8_t *cause, *call_state;
1782
Harald Welte4bc90a12008-12-27 16:32:52 +00001783 gh->msg_type = GSM48_MT_CC_STATUS;
1784
1785 cause = msgb_put(msg, 3);
1786 cause[0] = 2;
1787 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
1788 cause[2] = 0x80 | 30; /* response to status inquiry */
1789
1790 call_state = msgb_put(msg, 1);
1791 call_state[0] = 0xc0 | 0x00;
1792
Harald Welte39e2ead2009-07-23 21:13:03 +02001793 return gsm48_sendmsg(msg, trans);
Harald Welte4bc90a12008-12-27 16:32:52 +00001794}
1795
Harald Welte6f4b7532008-12-29 00:39:37 +00001796static int gsm48_tx_simple(struct gsm_lchan *lchan,
1797 u_int8_t pdisc, u_int8_t msg_type)
Harald Welte4bc90a12008-12-27 16:32:52 +00001798{
1799 struct msgb *msg = gsm48_msgb_alloc();
1800 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1801
1802 msg->lchan = lchan;
1803
Harald Welte6f4b7532008-12-29 00:39:37 +00001804 gh->proto_discr = pdisc;
Harald Welte4bc90a12008-12-27 16:32:52 +00001805 gh->msg_type = msg_type;
1806
Harald Welte39e2ead2009-07-23 21:13:03 +02001807 return gsm48_sendmsg(msg, NULL);
Harald Welte4bc90a12008-12-27 16:32:52 +00001808}
1809
Harald Welte4bfdfe72009-06-10 23:11:52 +08001810static void gsm48_stop_cc_timer(struct gsm_trans *trans)
1811{
Harald Weltedcaf5652009-07-23 18:56:43 +02001812 if (bsc_timer_pending(&trans->cc.timer)) {
1813 DEBUGP(DCC, "stopping pending timer T%x\n", trans->cc.Tcurrent);
1814 bsc_del_timer(&trans->cc.timer);
1815 trans->cc.Tcurrent = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001816 }
1817}
1818
1819static int mncc_recvmsg(struct gsm_network *net, struct gsm_trans *trans,
1820 int msg_type, struct gsm_mncc *mncc)
1821{
1822 struct msgb *msg;
1823
1824 if (trans)
1825 if (trans->lchan)
Harald Welte6f5aee02009-07-23 21:21:14 +02001826 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08001827 "Sending '%s' to MNCC.\n",
1828 trans->lchan->ts->trx->bts->nr,
1829 trans->lchan->ts->trx->nr,
1830 trans->lchan->ts->nr, trans->transaction_id,
1831 (trans->subscr)?(trans->subscr->extension):"-",
1832 get_mncc_name(msg_type));
1833 else
1834 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
1835 "Sending '%s' to MNCC.\n",
1836 (trans->subscr)?(trans->subscr->extension):"-",
1837 get_mncc_name(msg_type));
1838 else
1839 DEBUGP(DCC, "(bts - trx - ts - ti -- sub -) "
1840 "Sending '%s' to MNCC.\n", get_mncc_name(msg_type));
1841
1842 mncc->msg_type = msg_type;
1843
Harald Welte966636f2009-06-26 19:39:35 +02001844 msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001845 if (!msg)
1846 return -ENOMEM;
1847 memcpy(msg->data, mncc, sizeof(struct gsm_mncc));
1848 msgb_enqueue(&net->upqueue, msg);
1849
1850 return 0;
1851}
1852
1853int mncc_release_ind(struct gsm_network *net, struct gsm_trans *trans,
1854 u_int32_t callref, int location, int value)
1855{
1856 struct gsm_mncc rel;
1857
Harald Welte92f70c52009-06-12 01:54:08 +08001858 memset(&rel, 0, sizeof(rel));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001859 rel.callref = callref;
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001860 mncc_set_cause(&rel, location, value);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001861 return mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
1862}
1863
Harald Weltedcaf5652009-07-23 18:56:43 +02001864/* Call Control Specific transaction release.
1865 * gets called by trans_free, DO NOT CALL YOURSELF! */
1866void _gsm48_cc_trans_free(struct gsm_trans *trans)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001867{
Harald Welte4bfdfe72009-06-10 23:11:52 +08001868 gsm48_stop_cc_timer(trans);
1869
1870 /* send release to L4, if callref still exists */
1871 if (trans->callref) {
1872 /* Ressource unavailable */
Harald Welte596fed42009-07-23 19:06:52 +02001873 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001874 GSM48_CAUSE_LOC_PRN_S_LU,
1875 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001876 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001877 if (trans->cc.state != GSM_CSTATE_NULL)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001878 new_cc_state(trans, GSM_CSTATE_NULL);
Harald Weltedcaf5652009-07-23 18:56:43 +02001879 if (trans->lchan)
1880 trau_mux_unmap(&trans->lchan->ts->e1_link, trans->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001881}
1882
1883static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg);
1884
Harald Welte09e38af2009-02-16 22:52:23 +00001885/* call-back from paging the B-end of the connection */
1886static int setup_trig_pag_evt(unsigned int hooknum, unsigned int event,
Harald Welte7ccf7782009-02-17 01:43:01 +00001887 struct msgb *msg, void *_lchan, void *param)
Harald Welte09e38af2009-02-16 22:52:23 +00001888{
Harald Welte7ccf7782009-02-17 01:43:01 +00001889 struct gsm_lchan *lchan = _lchan;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001890 struct gsm_subscriber *subscr = param;
1891 struct gsm_trans *transt, *tmp;
1892 struct gsm_network *net;
Harald Weltec05677b2009-06-26 20:17:06 +02001893
Harald Welte09e38af2009-02-16 22:52:23 +00001894 if (hooknum != GSM_HOOK_RR_PAGING)
1895 return -EINVAL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001896
1897 if (!subscr)
1898 return -EINVAL;
1899 net = subscr->net;
1900 if (!net) {
1901 DEBUGP(DCC, "Error Network not set!\n");
1902 return -EINVAL;
Harald Welte5a065df2009-02-22 21:13:18 +00001903 }
Harald Welte7584aea2009-02-11 11:44:12 +00001904
Harald Welte4bfdfe72009-06-10 23:11:52 +08001905 /* check all tranactions (without lchan) for subscriber */
1906 llist_for_each_entry_safe(transt, tmp, &net->trans_list, entry) {
1907 if (transt->subscr != subscr || transt->lchan)
1908 continue;
1909 switch (event) {
1910 case GSM_PAGING_SUCCEEDED:
1911 if (!lchan) // paranoid
1912 break;
1913 DEBUGP(DCC, "Paging subscr %s succeeded!\n",
1914 subscr->extension);
1915 /* Assign lchan */
1916 if (!transt->lchan) {
1917 transt->lchan = lchan;
1918 use_lchan(lchan);
1919 }
1920 /* send SETUP request to called party */
Harald Weltedcaf5652009-07-23 18:56:43 +02001921 gsm48_cc_tx_setup(transt, &transt->cc.msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001922 break;
1923 case GSM_PAGING_EXPIRED:
1924 DEBUGP(DCC, "Paging subscr %s expired!\n",
1925 subscr->extension);
1926 /* Temporarily out of order */
Harald Welte596fed42009-07-23 19:06:52 +02001927 mncc_release_ind(transt->subscr->net, transt,
1928 transt->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001929 GSM48_CAUSE_LOC_PRN_S_LU,
1930 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001931 transt->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001932 trans_free(transt);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001933 break;
1934 }
1935 }
Harald Welte09e38af2009-02-16 22:52:23 +00001936 return 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00001937}
Harald Welte7584aea2009-02-11 11:44:12 +00001938
Harald Welte805f6442009-07-28 18:25:29 +02001939/* some other part of the code sends us a signal */
1940static int handle_abisip_signal(unsigned int subsys, unsigned int signal,
1941 void *handler_data, void *signal_data)
1942{
1943 struct gsm_lchan *lchan = signal_data;
1944 struct gsm_bts_trx_ts *ts;
1945 int rc;
1946
1947 if (subsys != SS_ABISIP)
1948 return 0;
1949
1950 /* in case we use direct BTS-to-BTS RTP */
1951 if (ipacc_rtp_direct)
1952 return 0;
1953
1954 ts = lchan->ts;
1955
1956 switch (signal) {
1957 case S_ABISIP_BIND_ACK:
1958 /* the BTS has successfully bound a TCH to a local ip/port,
1959 * which means we can connect our UDP socket to it */
1960 if (ts->abis_ip.rtp_socket) {
1961 rtp_socket_free(ts->abis_ip.rtp_socket);
1962 ts->abis_ip.rtp_socket = NULL;
1963 }
1964
1965 ts->abis_ip.rtp_socket = rtp_socket_create();
1966 if (!ts->abis_ip.rtp_socket)
1967 goto out_err;
1968
1969 rc = rtp_socket_connect(ts->abis_ip.rtp_socket,
1970 ts->abis_ip.bound_ip,
1971 ts->abis_ip.bound_port);
1972 if (rc < 0)
1973 goto out_err;
1974 break;
1975 case S_ABISIP_DISC_IND:
1976 /* the BTS tells us a RTP stream has been disconnected */
1977 if (ts->abis_ip.rtp_socket) {
1978 rtp_socket_free(ts->abis_ip.rtp_socket);
1979 ts->abis_ip.rtp_socket = NULL;
1980 }
1981 break;
1982 }
1983
1984 return 0;
1985out_err:
1986 /* FIXME: do something */
1987 return 0;
1988}
1989
1990/* bind rtp proxy to local IP/port and tell BTS to connect to it */
1991static int ipacc_connect_proxy_bind(struct gsm_lchan *lchan)
1992{
1993 struct gsm_bts_trx_ts *ts = lchan->ts;
1994 struct rtp_socket *rs = ts->abis_ip.rtp_socket;
1995 int rc;
1996
1997 rc = rsl_ipacc_connect(lchan, ntohl(rs->rtp.sin_local.sin_addr.s_addr),
1998 ntohs(rs->rtp.sin_local.sin_port),
1999 ts->abis_ip.conn_id,
2000 /* FIXME: use RTP payload of bound socket, not BTS*/
2001 ts->abis_ip.rtp_payload2);
2002
2003 return rc;
2004}
2005
Harald Welte49f48b82009-02-17 15:29:33 +00002006/* map two ipaccess RTP streams onto each other */
Harald Welte11fa29c2009-02-19 17:24:39 +00002007static int tch_map(struct gsm_lchan *lchan, struct gsm_lchan *remote_lchan)
Harald Welte49f48b82009-02-17 15:29:33 +00002008{
Harald Welte11fa29c2009-02-19 17:24:39 +00002009 struct gsm_bts *bts = lchan->ts->trx->bts;
2010 struct gsm_bts *remote_bts = remote_lchan->ts->trx->bts;
Harald Welte49f48b82009-02-17 15:29:33 +00002011 struct gsm_bts_trx_ts *ts;
Harald Welte805f6442009-07-28 18:25:29 +02002012 int rc;
Harald Welte49f48b82009-02-17 15:29:33 +00002013
Harald Welte11fa29c2009-02-19 17:24:39 +00002014 DEBUGP(DCC, "Setting up TCH map between (bts=%u,trx=%u,ts=%u) and (bts=%u,trx=%u,ts=%u)\n",
2015 bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
2016 remote_bts->nr, remote_lchan->ts->trx->nr, remote_lchan->ts->nr);
2017
2018 if (bts->type != remote_bts->type) {
2019 DEBUGP(DCC, "Cannot switch calls between different BTS types yet\n");
2020 return -EINVAL;
2021 }
Harald Welte49f48b82009-02-17 15:29:33 +00002022
Harald Welte11fa29c2009-02-19 17:24:39 +00002023 switch (bts->type) {
2024 case GSM_BTS_TYPE_NANOBTS_900:
2025 case GSM_BTS_TYPE_NANOBTS_1800:
Harald Welte805f6442009-07-28 18:25:29 +02002026 if (!ipacc_rtp_direct) {
2027 /* connect the TCH's to our RTP proxy */
2028 rc = ipacc_connect_proxy_bind(lchan);
2029 if (rc < 0)
2030 return rc;
2031 rc = ipacc_connect_proxy_bind(remote_lchan);
2032
2033 /* connect them with each other */
2034 rtp_socket_proxy(lchan->ts->abis_ip.rtp_socket,
2035 remote_lchan->ts->abis_ip.rtp_socket);
2036 } else {
2037 /* directly connect TCH RTP streams to each other */
2038 ts = remote_lchan->ts;
2039 rc = rsl_ipacc_connect(lchan, ts->abis_ip.bound_ip,
2040 ts->abis_ip.bound_port,
2041 lchan->ts->abis_ip.conn_id,
2042 ts->abis_ip.rtp_payload2);
2043 if (rc < 0)
2044 return rc;
2045 ts = lchan->ts;
2046 rc = rsl_ipacc_connect(remote_lchan, ts->abis_ip.bound_ip,
2047 ts->abis_ip.bound_port,
2048 remote_lchan->ts->abis_ip.conn_id,
2049 ts->abis_ip.rtp_payload2);
2050 }
Harald Welte11fa29c2009-02-19 17:24:39 +00002051 break;
2052 case GSM_BTS_TYPE_BS11:
2053 trau_mux_map_lchan(lchan, remote_lchan);
2054 break;
2055 default:
2056 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
Harald Welte805f6442009-07-28 18:25:29 +02002057 rc = -EINVAL;
Harald Welte11fa29c2009-02-19 17:24:39 +00002058 break;
2059 }
Harald Welte49f48b82009-02-17 15:29:33 +00002060
2061 return 0;
2062}
2063
Harald Welte4bfdfe72009-06-10 23:11:52 +08002064/* bridge channels of two transactions */
2065static int tch_bridge(struct gsm_network *net, u_int32_t *refs)
Harald Welte7ccf7782009-02-17 01:43:01 +00002066{
Harald Weltedcaf5652009-07-23 18:56:43 +02002067 struct gsm_trans *trans1 = trans_find_by_callref(net, refs[0]);
2068 struct gsm_trans *trans2 = trans_find_by_callref(net, refs[1]);
Harald Welte7ccf7782009-02-17 01:43:01 +00002069
Harald Welte4bfdfe72009-06-10 23:11:52 +08002070 if (!trans1 || !trans2)
Harald Welte7ccf7782009-02-17 01:43:01 +00002071 return -EIO;
2072
Harald Welte4bfdfe72009-06-10 23:11:52 +08002073 if (!trans1->lchan || !trans2->lchan)
2074 return -EIO;
2075
2076 /* through-connect channel */
2077 return tch_map(trans1->lchan, trans2->lchan);
Harald Welte7ccf7782009-02-17 01:43:01 +00002078}
2079
Harald Welte4bfdfe72009-06-10 23:11:52 +08002080/* enable receive of channels to upqueue */
2081static int tch_recv(struct gsm_network *net, struct gsm_mncc *data, int enable)
2082{
2083 struct gsm_trans *trans;
Harald Welte7ccf7782009-02-17 01:43:01 +00002084
Harald Welte4bfdfe72009-06-10 23:11:52 +08002085 /* Find callref */
Harald Weltedcaf5652009-07-23 18:56:43 +02002086 trans = trans_find_by_callref(net, data->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002087 if (!trans)
2088 return -EIO;
2089 if (!trans->lchan)
2090 return 0;
2091
2092 // todo IPACCESS
2093 if (enable)
2094 return trau_recv_lchan(trans->lchan, data->callref);
2095 return trau_mux_unmap(NULL, data->callref);
2096}
2097
2098/* send a frame to channel */
2099static int tch_frame(struct gsm_network *net, struct gsm_trau_frame *frame)
2100{
2101 struct gsm_trans *trans;
2102
2103 /* Find callref */
Harald Weltedcaf5652009-07-23 18:56:43 +02002104 trans = trans_find_by_callref(net, frame->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002105 if (!trans)
2106 return -EIO;
2107 if (!trans->lchan)
2108 return 0;
2109 if (trans->lchan->type != GSM_LCHAN_TCH_F &&
2110 trans->lchan->type != GSM_LCHAN_TCH_H)
2111 return 0;
2112
2113 // todo IPACCESS
2114 return trau_send_lchan(trans->lchan,
2115 (struct decoded_trau_frame *)frame->data);
2116}
2117
2118
2119static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
2120{
2121 DEBUGP(DCC, "-> STATUS ENQ\n");
2122 return gsm48_cc_tx_status(trans, msg);
2123}
2124
2125static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg);
2126static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg);
2127
2128static void gsm48_cc_timeout(void *arg)
2129{
2130 struct gsm_trans *trans = arg;
2131 int disconnect = 0, release = 0;
Harald Weltec66b71c2009-06-11 14:23:20 +08002132 int mo_cause = GSM48_CC_CAUSE_RECOVERY_TIMER;
2133 int mo_location = GSM48_CAUSE_LOC_USER;
2134 int l4_cause = GSM48_CC_CAUSE_NORMAL_UNSPEC;
2135 int l4_location = GSM48_CAUSE_LOC_PRN_S_LU;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002136 struct gsm_mncc mo_rel, l4_rel;
2137
2138 memset(&mo_rel, 0, sizeof(struct gsm_mncc));
2139 mo_rel.callref = trans->callref;
2140 memset(&l4_rel, 0, sizeof(struct gsm_mncc));
2141 l4_rel.callref = trans->callref;
2142
Harald Weltedcaf5652009-07-23 18:56:43 +02002143 switch(trans->cc.Tcurrent) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002144 case 0x303:
2145 release = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08002146 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002147 break;
2148 case 0x310:
2149 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08002150 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002151 break;
2152 case 0x313:
2153 disconnect = 1;
2154 /* unknown, did not find it in the specs */
2155 break;
2156 case 0x301:
2157 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08002158 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002159 break;
2160 case 0x308:
Harald Weltedcaf5652009-07-23 18:56:43 +02002161 if (!trans->cc.T308_second) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002162 /* restart T308 a second time */
Harald Weltedcaf5652009-07-23 18:56:43 +02002163 gsm48_cc_tx_release(trans, &trans->cc.msg);
2164 trans->cc.T308_second = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002165 break; /* stay in release state */
2166 }
Harald Weltedcaf5652009-07-23 18:56:43 +02002167 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002168 return;
2169// release = 1;
2170// l4_cause = 14;
2171// break;
2172 case 0x306:
2173 release = 1;
Harald Weltedcaf5652009-07-23 18:56:43 +02002174 mo_cause = trans->cc.msg.cause.value;
2175 mo_location = trans->cc.msg.cause.location;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002176 break;
2177 case 0x323:
2178 disconnect = 1;
2179 break;
2180 default:
2181 release = 1;
2182 }
2183
2184 if (release && trans->callref) {
2185 /* process release towards layer 4 */
Harald Welte596fed42009-07-23 19:06:52 +02002186 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002187 l4_location, l4_cause);
2188 trans->callref = 0;
2189 }
2190
2191 if (disconnect && trans->callref) {
2192 /* process disconnect towards layer 4 */
2193 mncc_set_cause(&l4_rel, l4_location, l4_cause);
Harald Welte596fed42009-07-23 19:06:52 +02002194 mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &l4_rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002195 }
2196
2197 /* process disconnect towards mobile station */
2198 if (disconnect || release) {
2199 mncc_set_cause(&mo_rel, mo_location, mo_cause);
Harald Weltedcaf5652009-07-23 18:56:43 +02002200 mo_rel.cause.diag[0] = ((trans->cc.Tcurrent & 0xf00) >> 8) + '0';
2201 mo_rel.cause.diag[1] = ((trans->cc.Tcurrent & 0x0f0) >> 4) + '0';
2202 mo_rel.cause.diag[2] = (trans->cc.Tcurrent & 0x00f) + '0';
Harald Welte4bfdfe72009-06-10 23:11:52 +08002203 mo_rel.cause.diag_len = 3;
2204
2205 if (disconnect)
2206 gsm48_cc_tx_disconnect(trans, &mo_rel);
2207 if (release)
2208 gsm48_cc_tx_release(trans, &mo_rel);
2209 }
2210
2211}
2212
2213static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
2214 int sec, int micro)
2215{
2216 DEBUGP(DCC, "starting timer T%x with %d seconds\n", current, sec);
Harald Weltedcaf5652009-07-23 18:56:43 +02002217 trans->cc.timer.cb = gsm48_cc_timeout;
2218 trans->cc.timer.data = trans;
2219 bsc_schedule_timer(&trans->cc.timer, sec, micro);
2220 trans->cc.Tcurrent = current;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002221}
2222
2223static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
2224{
2225 struct gsm48_hdr *gh = msgb_l3(msg);
2226 u_int8_t msg_type = gh->msg_type & 0xbf;
2227 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2228 struct tlv_parsed tp;
2229 struct gsm_mncc setup;
2230
2231 memset(&setup, 0, sizeof(struct gsm_mncc));
2232 setup.callref = trans->callref;
2233 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2234 /* emergency setup is identified by msg_type */
2235 if (msg_type == GSM48_MT_CC_EMERG_SETUP)
2236 setup.emergency = 1;
2237
2238 /* use subscriber as calling party number */
2239 if (trans->subscr) {
2240 setup.fields |= MNCC_F_CALLING;
2241 strncpy(setup.calling.number, trans->subscr->extension,
2242 sizeof(setup.calling.number)-1);
Andreas Eversbergc079be42009-06-15 23:22:09 +02002243 strncpy(setup.imsi, trans->subscr->imsi,
2244 sizeof(setup.imsi)-1);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002245 }
2246 /* bearer capability */
2247 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2248 setup.fields |= MNCC_F_BEARER_CAP;
2249 decode_bearer_cap(&setup.bearer_cap,
2250 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2251 }
2252 /* facility */
2253 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2254 setup.fields |= MNCC_F_FACILITY;
2255 decode_facility(&setup.facility,
2256 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2257 }
2258 /* called party bcd number */
2259 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
2260 setup.fields |= MNCC_F_CALLED;
2261 decode_called(&setup.called,
2262 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
2263 }
2264 /* user-user */
2265 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2266 setup.fields |= MNCC_F_USERUSER;
2267 decode_useruser(&setup.useruser,
2268 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2269 }
2270 /* ss-version */
2271 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2272 setup.fields |= MNCC_F_SSVERSION;
2273 decode_ssversion(&setup.ssversion,
2274 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2275 }
2276 /* CLIR suppression */
2277 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
2278 setup.clir.sup = 1;
2279 /* CLIR invocation */
2280 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
2281 setup.clir.inv = 1;
2282 /* cc cap */
2283 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
2284 setup.fields |= MNCC_F_CCCAP;
2285 decode_cccap(&setup.cccap,
2286 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
2287 }
2288
Harald Welte4bfdfe72009-06-10 23:11:52 +08002289 new_cc_state(trans, GSM_CSTATE_INITIATED);
2290
2291 /* indicate setup to MNCC */
Harald Welte596fed42009-07-23 19:06:52 +02002292 mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_IND, &setup);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002293
Harald Welte13cac662009-07-29 12:10:35 +02002294 /* MNCC code will modify the channel asynchronously, we should
2295 * ipaccess-bind only after the modification has been made to the
2296 * lchan->tch_mode */
Harald Welte4bfdfe72009-06-10 23:11:52 +08002297 return 0;
2298}
2299
2300static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
Harald Welte65e74cc2008-12-29 01:55:35 +00002301{
2302 struct msgb *msg = gsm48_msgb_alloc();
2303 struct gsm48_hdr *gh;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002304 struct gsm_mncc *setup = arg;
Harald Welte78283ef2009-07-23 21:36:44 +02002305 int rc, trans_id;
Harald Welte65e74cc2008-12-29 01:55:35 +00002306
Harald Welte7ccf7782009-02-17 01:43:01 +00002307 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte65e74cc2008-12-29 01:55:35 +00002308
Harald Welte4bfdfe72009-06-10 23:11:52 +08002309 /* transaction id must not be assigned */
2310 if (trans->transaction_id != 0xff) { /* unasssigned */
2311 DEBUGP(DCC, "TX Setup with assigned transaction. "
2312 "This is not allowed!\n");
2313 /* Temporarily out of order */
Harald Welte596fed42009-07-23 19:06:52 +02002314 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002315 GSM48_CAUSE_LOC_PRN_S_LU,
2316 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002317 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002318 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002319 return rc;
2320 }
2321
2322 /* Get free transaction_id */
Harald Welte78283ef2009-07-23 21:36:44 +02002323 trans_id = trans_assign_trans_id(trans->subscr, GSM48_PDISC_CC, 0);
2324 if (trans_id < 0) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002325 /* no free transaction ID */
Harald Welte596fed42009-07-23 19:06:52 +02002326 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002327 GSM48_CAUSE_LOC_PRN_S_LU,
2328 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002329 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002330 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002331 return rc;
2332 }
Harald Welte78283ef2009-07-23 21:36:44 +02002333 trans->transaction_id = trans_id;
Harald Welte49f48b82009-02-17 15:29:33 +00002334
Harald Welte65e74cc2008-12-29 01:55:35 +00002335 gh->msg_type = GSM48_MT_CC_SETUP;
Harald Welte09e38af2009-02-16 22:52:23 +00002336
Harald Welte4bfdfe72009-06-10 23:11:52 +08002337 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
Harald Welte65e74cc2008-12-29 01:55:35 +00002338
Harald Welte4bfdfe72009-06-10 23:11:52 +08002339 /* bearer capability */
2340 if (setup->fields & MNCC_F_BEARER_CAP)
2341 encode_bearer_cap(msg, 0, &setup->bearer_cap);
2342 /* facility */
2343 if (setup->fields & MNCC_F_FACILITY)
2344 encode_facility(msg, 0, &setup->facility);
2345 /* progress */
2346 if (setup->fields & MNCC_F_PROGRESS)
2347 encode_progress(msg, 0, &setup->progress);
2348 /* calling party BCD number */
2349 if (setup->fields & MNCC_F_CALLING)
2350 encode_calling(msg, &setup->calling);
2351 /* called party BCD number */
2352 if (setup->fields & MNCC_F_CALLED)
2353 encode_called(msg, &setup->called);
2354 /* user-user */
2355 if (setup->fields & MNCC_F_USERUSER)
2356 encode_useruser(msg, 0, &setup->useruser);
2357 /* redirecting party BCD number */
2358 if (setup->fields & MNCC_F_REDIRECTING)
2359 encode_redirecting(msg, &setup->redirecting);
2360 /* signal */
2361 if (setup->fields & MNCC_F_SIGNAL)
2362 encode_signal(msg, setup->signal);
2363
2364 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
Harald Welte65e74cc2008-12-29 01:55:35 +00002365
Harald Welte39e2ead2009-07-23 21:13:03 +02002366 return gsm48_sendmsg(msg, trans);
Harald Welte65e74cc2008-12-29 01:55:35 +00002367}
2368
Harald Welte4bfdfe72009-06-10 23:11:52 +08002369static int gsm48_cc_rx_call_conf(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 call_conf;
2375
2376 gsm48_stop_cc_timer(trans);
2377 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
2378
2379 memset(&call_conf, 0, sizeof(struct gsm_mncc));
2380 call_conf.callref = trans->callref;
2381 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2382#if 0
2383 /* repeat */
2384 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
2385 call_conf.repeat = 1;
2386 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
2387 call_conf.repeat = 2;
2388#endif
2389 /* bearer capability */
2390 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2391 call_conf.fields |= MNCC_F_BEARER_CAP;
2392 decode_bearer_cap(&call_conf.bearer_cap,
2393 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2394 }
2395 /* cause */
2396 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2397 call_conf.fields |= MNCC_F_CAUSE;
2398 decode_cause(&call_conf.cause,
2399 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2400 }
2401 /* cc cap */
2402 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
2403 call_conf.fields |= MNCC_F_CCCAP;
2404 decode_cccap(&call_conf.cccap,
2405 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
2406 }
2407
2408 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
2409
Harald Welte596fed42009-07-23 19:06:52 +02002410 return mncc_recvmsg(trans->subscr->net, trans, MNCC_CALL_CONF_IND,
2411 &call_conf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002412}
2413
2414static int gsm48_cc_tx_call_proc(struct gsm_trans *trans, void *arg)
2415{
2416 struct gsm_mncc *proceeding = arg;
2417 struct msgb *msg = gsm48_msgb_alloc();
2418 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2419
Harald Welte4bfdfe72009-06-10 23:11:52 +08002420 gh->msg_type = GSM48_MT_CC_CALL_PROC;
2421
2422 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
2423
2424 /* bearer capability */
2425 if (proceeding->fields & MNCC_F_BEARER_CAP)
2426 encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
2427 /* facility */
2428 if (proceeding->fields & MNCC_F_FACILITY)
2429 encode_facility(msg, 0, &proceeding->facility);
2430 /* progress */
2431 if (proceeding->fields & MNCC_F_PROGRESS)
2432 encode_progress(msg, 0, &proceeding->progress);
2433
Harald Welte39e2ead2009-07-23 21:13:03 +02002434 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002435}
2436
2437static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
2438{
2439 struct gsm48_hdr *gh = msgb_l3(msg);
2440 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2441 struct tlv_parsed tp;
2442 struct gsm_mncc alerting;
2443
2444 gsm48_stop_cc_timer(trans);
2445 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
2446
2447 memset(&alerting, 0, sizeof(struct gsm_mncc));
2448 alerting.callref = trans->callref;
2449 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2450 /* facility */
2451 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2452 alerting.fields |= MNCC_F_FACILITY;
2453 decode_facility(&alerting.facility,
2454 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2455 }
2456
2457 /* progress */
2458 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
2459 alerting.fields |= MNCC_F_PROGRESS;
2460 decode_progress(&alerting.progress,
2461 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
2462 }
2463 /* ss-version */
2464 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2465 alerting.fields |= MNCC_F_SSVERSION;
2466 decode_ssversion(&alerting.ssversion,
2467 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2468 }
2469
2470 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
2471
Harald Welte596fed42009-07-23 19:06:52 +02002472 return mncc_recvmsg(trans->subscr->net, trans, MNCC_ALERT_IND,
2473 &alerting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002474}
2475
2476static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
2477{
2478 struct gsm_mncc *alerting = arg;
2479 struct msgb *msg = gsm48_msgb_alloc();
2480 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2481
Harald Welte4bfdfe72009-06-10 23:11:52 +08002482 gh->msg_type = GSM48_MT_CC_ALERTING;
2483
2484 /* facility */
2485 if (alerting->fields & MNCC_F_FACILITY)
2486 encode_facility(msg, 0, &alerting->facility);
2487 /* progress */
2488 if (alerting->fields & MNCC_F_PROGRESS)
2489 encode_progress(msg, 0, &alerting->progress);
2490 /* user-user */
2491 if (alerting->fields & MNCC_F_USERUSER)
2492 encode_useruser(msg, 0, &alerting->useruser);
2493
2494 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
2495
Harald Welte39e2ead2009-07-23 21:13:03 +02002496 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002497}
2498
2499static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
2500{
2501 struct gsm_mncc *progress = arg;
2502 struct msgb *msg = gsm48_msgb_alloc();
2503 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2504
Harald Welte4bfdfe72009-06-10 23:11:52 +08002505 gh->msg_type = GSM48_MT_CC_PROGRESS;
2506
2507 /* progress */
2508 encode_progress(msg, 1, &progress->progress);
2509 /* user-user */
2510 if (progress->fields & MNCC_F_USERUSER)
2511 encode_useruser(msg, 0, &progress->useruser);
2512
Harald Welte39e2ead2009-07-23 21:13:03 +02002513 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002514}
2515
2516static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
2517{
2518 struct gsm_mncc *connect = arg;
2519 struct msgb *msg = gsm48_msgb_alloc();
2520 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2521
Harald Welte4bfdfe72009-06-10 23:11:52 +08002522 gh->msg_type = GSM48_MT_CC_CONNECT;
2523
2524 gsm48_stop_cc_timer(trans);
2525 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
2526
2527 /* facility */
2528 if (connect->fields & MNCC_F_FACILITY)
2529 encode_facility(msg, 0, &connect->facility);
2530 /* progress */
2531 if (connect->fields & MNCC_F_PROGRESS)
2532 encode_progress(msg, 0, &connect->progress);
2533 /* connected number */
2534 if (connect->fields & MNCC_F_CONNECTED)
2535 encode_connected(msg, &connect->connected);
2536 /* user-user */
2537 if (connect->fields & MNCC_F_USERUSER)
2538 encode_useruser(msg, 0, &connect->useruser);
2539
2540 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
2541
Harald Welte39e2ead2009-07-23 21:13:03 +02002542 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002543}
2544
2545static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
2546{
2547 struct gsm48_hdr *gh = msgb_l3(msg);
2548 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2549 struct tlv_parsed tp;
2550 struct gsm_mncc connect;
2551
2552 gsm48_stop_cc_timer(trans);
2553
2554 memset(&connect, 0, sizeof(struct gsm_mncc));
2555 connect.callref = trans->callref;
2556 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2557 /* use subscriber as connected party number */
2558 if (trans->subscr) {
2559 connect.fields |= MNCC_F_CONNECTED;
2560 strncpy(connect.connected.number, trans->subscr->extension,
2561 sizeof(connect.connected.number)-1);
Andreas Eversbergc079be42009-06-15 23:22:09 +02002562 strncpy(connect.imsi, trans->subscr->imsi,
2563 sizeof(connect.imsi)-1);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002564 }
2565 /* facility */
2566 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2567 connect.fields |= MNCC_F_FACILITY;
2568 decode_facility(&connect.facility,
2569 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2570 }
2571 /* user-user */
2572 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2573 connect.fields |= MNCC_F_USERUSER;
2574 decode_useruser(&connect.useruser,
2575 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2576 }
2577 /* ss-version */
2578 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2579 connect.fields |= MNCC_F_SSVERSION;
2580 decode_ssversion(&connect.ssversion,
2581 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2582 }
2583
2584 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
2585
Harald Welte596fed42009-07-23 19:06:52 +02002586 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_CNF, &connect);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002587}
2588
2589
2590static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
2591{
2592 struct gsm_mncc connect_ack;
2593
2594 gsm48_stop_cc_timer(trans);
2595
2596 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2597
2598 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
2599 connect_ack.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02002600 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_COMPL_IND,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002601 &connect_ack);
2602}
2603
2604static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
2605{
2606 struct msgb *msg = gsm48_msgb_alloc();
2607 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2608
Harald Welte4bfdfe72009-06-10 23:11:52 +08002609 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
2610
2611 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2612
Harald Welte39e2ead2009-07-23 21:13:03 +02002613 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002614}
2615
2616static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
2617{
2618 struct gsm48_hdr *gh = msgb_l3(msg);
2619 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2620 struct tlv_parsed tp;
2621 struct gsm_mncc disc;
2622
2623 gsm48_stop_cc_timer(trans);
2624
2625 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
2626
2627 memset(&disc, 0, sizeof(struct gsm_mncc));
2628 disc.callref = trans->callref;
2629 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
2630 /* cause */
2631 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2632 disc.fields |= MNCC_F_CAUSE;
2633 decode_cause(&disc.cause,
2634 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2635 }
2636 /* facility */
2637 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2638 disc.fields |= MNCC_F_FACILITY;
2639 decode_facility(&disc.facility,
2640 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2641 }
2642 /* user-user */
2643 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2644 disc.fields |= MNCC_F_USERUSER;
2645 decode_useruser(&disc.useruser,
2646 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2647 }
2648 /* ss-version */
2649 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2650 disc.fields |= MNCC_F_SSVERSION;
2651 decode_ssversion(&disc.ssversion,
2652 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2653 }
2654
Harald Welte596fed42009-07-23 19:06:52 +02002655 return mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &disc);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002656
2657}
2658
Harald Weltec66b71c2009-06-11 14:23:20 +08002659static struct gsm_mncc_cause default_cause = {
2660 .location = GSM48_CAUSE_LOC_PRN_S_LU,
2661 .coding = 0,
2662 .rec = 0,
2663 .rec_val = 0,
2664 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
2665 .diag_len = 0,
2666 .diag = { 0 },
2667};
Harald Welte4bfdfe72009-06-10 23:11:52 +08002668
2669static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
2670{
2671 struct gsm_mncc *disc = arg;
2672 struct msgb *msg = gsm48_msgb_alloc();
2673 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2674
Harald Welte4bfdfe72009-06-10 23:11:52 +08002675 gh->msg_type = GSM48_MT_CC_DISCONNECT;
2676
2677 gsm48_stop_cc_timer(trans);
2678 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
2679
2680 /* cause */
2681 if (disc->fields & MNCC_F_CAUSE)
2682 encode_cause(msg, 1, &disc->cause);
2683 else
2684 encode_cause(msg, 1, &default_cause);
2685
2686 /* facility */
2687 if (disc->fields & MNCC_F_FACILITY)
2688 encode_facility(msg, 0, &disc->facility);
2689 /* progress */
2690 if (disc->fields & MNCC_F_PROGRESS)
2691 encode_progress(msg, 0, &disc->progress);
2692 /* user-user */
2693 if (disc->fields & MNCC_F_USERUSER)
2694 encode_useruser(msg, 0, &disc->useruser);
2695
2696 /* store disconnect cause for T306 expiry */
Harald Weltedcaf5652009-07-23 18:56:43 +02002697 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002698
2699 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
2700
Harald Welte39e2ead2009-07-23 21:13:03 +02002701 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002702}
2703
2704static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
2705{
2706 struct gsm48_hdr *gh = msgb_l3(msg);
2707 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2708 struct tlv_parsed tp;
2709 struct gsm_mncc rel;
2710 int rc;
2711
2712 gsm48_stop_cc_timer(trans);
2713
2714 memset(&rel, 0, sizeof(struct gsm_mncc));
2715 rel.callref = trans->callref;
2716 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2717 /* cause */
2718 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2719 rel.fields |= MNCC_F_CAUSE;
2720 decode_cause(&rel.cause,
2721 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2722 }
2723 /* facility */
2724 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2725 rel.fields |= MNCC_F_FACILITY;
2726 decode_facility(&rel.facility,
2727 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2728 }
2729 /* user-user */
2730 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2731 rel.fields |= MNCC_F_USERUSER;
2732 decode_useruser(&rel.useruser,
2733 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2734 }
2735 /* ss-version */
2736 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2737 rel.fields |= MNCC_F_SSVERSION;
2738 decode_ssversion(&rel.ssversion,
2739 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2740 }
2741
Harald Weltedcaf5652009-07-23 18:56:43 +02002742 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002743 /* release collision 5.4.5 */
Harald Welte596fed42009-07-23 19:06:52 +02002744 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_CNF, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002745 } else {
Harald Welte596fed42009-07-23 19:06:52 +02002746 rc = gsm48_tx_simple(msg->lchan,
Harald Welte6f5aee02009-07-23 21:21:14 +02002747 GSM48_PDISC_CC | (trans->transaction_id << 4),
Harald Welte596fed42009-07-23 19:06:52 +02002748 GSM48_MT_CC_RELEASE_COMPL);
2749 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_IND, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002750 }
2751
2752 new_cc_state(trans, GSM_CSTATE_NULL);
2753
2754 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002755 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002756
2757 return rc;
2758}
2759
2760static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
2761{
2762 struct gsm_mncc *rel = arg;
2763 struct msgb *msg = gsm48_msgb_alloc();
2764 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2765
Harald Welte4bfdfe72009-06-10 23:11:52 +08002766 gh->msg_type = GSM48_MT_CC_RELEASE;
2767
2768 trans->callref = 0;
2769
2770 gsm48_stop_cc_timer(trans);
2771 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
2772
2773 /* cause */
2774 if (rel->fields & MNCC_F_CAUSE)
2775 encode_cause(msg, 0, &rel->cause);
2776 /* facility */
2777 if (rel->fields & MNCC_F_FACILITY)
2778 encode_facility(msg, 0, &rel->facility);
2779 /* user-user */
2780 if (rel->fields & MNCC_F_USERUSER)
2781 encode_useruser(msg, 0, &rel->useruser);
2782
Harald Weltedcaf5652009-07-23 18:56:43 +02002783 trans->cc.T308_second = 0;
2784 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002785
Harald Weltedcaf5652009-07-23 18:56:43 +02002786 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002787 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
2788
Harald Welte39e2ead2009-07-23 21:13:03 +02002789 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002790}
2791
2792static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
2793{
2794 struct gsm48_hdr *gh = msgb_l3(msg);
2795 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2796 struct tlv_parsed tp;
2797 struct gsm_mncc rel;
2798 int rc = 0;
2799
2800 gsm48_stop_cc_timer(trans);
2801
2802 memset(&rel, 0, sizeof(struct gsm_mncc));
2803 rel.callref = trans->callref;
2804 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2805 /* cause */
2806 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2807 rel.fields |= MNCC_F_CAUSE;
2808 decode_cause(&rel.cause,
2809 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2810 }
2811 /* facility */
2812 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2813 rel.fields |= MNCC_F_FACILITY;
2814 decode_facility(&rel.facility,
2815 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2816 }
2817 /* user-user */
2818 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2819 rel.fields |= MNCC_F_USERUSER;
2820 decode_useruser(&rel.useruser,
2821 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2822 }
2823 /* ss-version */
2824 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2825 rel.fields |= MNCC_F_SSVERSION;
2826 decode_ssversion(&rel.ssversion,
2827 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2828 }
2829
2830 if (trans->callref) {
Harald Weltedcaf5652009-07-23 18:56:43 +02002831 switch (trans->cc.state) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002832 case GSM_CSTATE_CALL_PRESENT:
Harald Welte596fed42009-07-23 19:06:52 +02002833 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002834 MNCC_REJ_IND, &rel);
2835 break;
2836 case GSM_CSTATE_RELEASE_REQ:
Harald Welte596fed42009-07-23 19:06:52 +02002837 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002838 MNCC_REL_CNF, &rel);
2839 break;
2840 default:
Harald Welte596fed42009-07-23 19:06:52 +02002841 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002842 MNCC_REL_IND, &rel);
2843 }
2844 }
2845
2846 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002847 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002848
2849 return rc;
2850}
2851
2852static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
2853{
2854 struct gsm_mncc *rel = arg;
2855 struct msgb *msg = gsm48_msgb_alloc();
2856 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2857
Harald Welte4bfdfe72009-06-10 23:11:52 +08002858 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
2859
2860 trans->callref = 0;
2861
2862 gsm48_stop_cc_timer(trans);
2863
2864 /* cause */
2865 if (rel->fields & MNCC_F_CAUSE)
2866 encode_cause(msg, 0, &rel->cause);
2867 /* facility */
2868 if (rel->fields & MNCC_F_FACILITY)
2869 encode_facility(msg, 0, &rel->facility);
2870 /* user-user */
2871 if (rel->fields & MNCC_F_USERUSER)
2872 encode_useruser(msg, 0, &rel->useruser);
2873
Harald Weltedcaf5652009-07-23 18:56:43 +02002874 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002875
Harald Welte39e2ead2009-07-23 21:13:03 +02002876 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002877}
2878
2879static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
2880{
2881 struct gsm48_hdr *gh = msgb_l3(msg);
2882 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2883 struct tlv_parsed tp;
2884 struct gsm_mncc fac;
2885
2886 memset(&fac, 0, sizeof(struct gsm_mncc));
2887 fac.callref = trans->callref;
2888 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
2889 /* facility */
2890 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2891 fac.fields |= MNCC_F_FACILITY;
2892 decode_facility(&fac.facility,
2893 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2894 }
2895 /* ss-version */
2896 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2897 fac.fields |= MNCC_F_SSVERSION;
2898 decode_ssversion(&fac.ssversion,
2899 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2900 }
2901
Harald Welte596fed42009-07-23 19:06:52 +02002902 return mncc_recvmsg(trans->subscr->net, trans, MNCC_FACILITY_IND, &fac);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002903}
2904
2905static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
2906{
2907 struct gsm_mncc *fac = arg;
2908 struct msgb *msg = gsm48_msgb_alloc();
2909 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2910
Harald Welte4bfdfe72009-06-10 23:11:52 +08002911 gh->msg_type = GSM48_MT_CC_FACILITY;
2912
2913 /* facility */
2914 encode_facility(msg, 1, &fac->facility);
2915
Harald Welte39e2ead2009-07-23 21:13:03 +02002916 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002917}
2918
2919static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
2920{
2921 struct gsm_mncc hold;
2922
2923 memset(&hold, 0, sizeof(struct gsm_mncc));
2924 hold.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02002925 return mncc_recvmsg(trans->subscr->net, trans, MNCC_HOLD_IND, &hold);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002926}
2927
2928static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
2929{
2930 struct msgb *msg = gsm48_msgb_alloc();
2931 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2932
Harald Welte4bfdfe72009-06-10 23:11:52 +08002933 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
2934
Harald Welte39e2ead2009-07-23 21:13:03 +02002935 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002936}
2937
2938static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
2939{
2940 struct gsm_mncc *hold_rej = arg;
2941 struct msgb *msg = gsm48_msgb_alloc();
2942 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2943
Harald Welte4bfdfe72009-06-10 23:11:52 +08002944 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
2945
2946 /* cause */
2947 if (hold_rej->fields & MNCC_F_CAUSE)
2948 encode_cause(msg, 1, &hold_rej->cause);
2949 else
2950 encode_cause(msg, 1, &default_cause);
2951
Harald Welte39e2ead2009-07-23 21:13:03 +02002952 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002953}
2954
2955static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
2956{
2957 struct gsm_mncc retrieve;
2958
2959 memset(&retrieve, 0, sizeof(struct gsm_mncc));
2960 retrieve.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02002961 return mncc_recvmsg(trans->subscr->net, trans, MNCC_RETRIEVE_IND,
2962 &retrieve);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002963}
2964
2965static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
2966{
2967 struct msgb *msg = gsm48_msgb_alloc();
2968 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2969
Harald Welte4bfdfe72009-06-10 23:11:52 +08002970 gh->msg_type = GSM48_MT_CC_RETR_ACK;
2971
Harald Welte39e2ead2009-07-23 21:13:03 +02002972 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002973}
2974
2975static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
2976{
2977 struct gsm_mncc *retrieve_rej = arg;
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_RETR_REJ;
2982
2983 /* cause */
2984 if (retrieve_rej->fields & MNCC_F_CAUSE)
2985 encode_cause(msg, 1, &retrieve_rej->cause);
2986 else
2987 encode_cause(msg, 1, &default_cause);
2988
Harald Welte39e2ead2009-07-23 21:13:03 +02002989 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002990}
2991
2992static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
2993{
2994 struct gsm48_hdr *gh = msgb_l3(msg);
2995 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2996 struct tlv_parsed tp;
2997 struct gsm_mncc dtmf;
2998
2999 memset(&dtmf, 0, sizeof(struct gsm_mncc));
3000 dtmf.callref = trans->callref;
3001 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
3002 /* keypad facility */
3003 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
3004 dtmf.fields |= MNCC_F_KEYPAD;
3005 decode_keypad(&dtmf.keypad,
3006 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
3007 }
3008
Harald Welte596fed42009-07-23 19:06:52 +02003009 return mncc_recvmsg(trans->subscr->net, trans, MNCC_START_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003010}
3011
3012static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
3013{
3014 struct gsm_mncc *dtmf = arg;
3015 struct msgb *msg = gsm48_msgb_alloc();
3016 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3017
Harald Welte4bfdfe72009-06-10 23:11:52 +08003018 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
3019
3020 /* keypad */
3021 if (dtmf->fields & MNCC_F_KEYPAD)
3022 encode_keypad(msg, dtmf->keypad);
3023
Harald Welte39e2ead2009-07-23 21:13:03 +02003024 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003025}
3026
3027static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
3028{
3029 struct gsm_mncc *dtmf = arg;
3030 struct msgb *msg = gsm48_msgb_alloc();
3031 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3032
Harald Welte4bfdfe72009-06-10 23:11:52 +08003033 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
3034
3035 /* cause */
3036 if (dtmf->fields & MNCC_F_CAUSE)
3037 encode_cause(msg, 1, &dtmf->cause);
3038 else
3039 encode_cause(msg, 1, &default_cause);
3040
Harald Welte39e2ead2009-07-23 21:13:03 +02003041 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003042}
3043
3044static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
3045{
3046 struct msgb *msg = gsm48_msgb_alloc();
3047 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3048
Harald Welte4bfdfe72009-06-10 23:11:52 +08003049 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
3050
Harald Welte39e2ead2009-07-23 21:13:03 +02003051 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003052}
3053
3054static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
3055{
3056 struct gsm_mncc dtmf;
3057
3058 memset(&dtmf, 0, sizeof(struct gsm_mncc));
3059 dtmf.callref = trans->callref;
3060
Harald Welte596fed42009-07-23 19:06:52 +02003061 return mncc_recvmsg(trans->subscr->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003062}
3063
3064static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
3065{
3066 struct gsm48_hdr *gh = msgb_l3(msg);
3067 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
3068 struct tlv_parsed tp;
3069 struct gsm_mncc modify;
3070
3071 memset(&modify, 0, sizeof(struct gsm_mncc));
3072 modify.callref = trans->callref;
3073 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
3074 /* bearer capability */
3075 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
3076 modify.fields |= MNCC_F_BEARER_CAP;
3077 decode_bearer_cap(&modify.bearer_cap,
3078 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
3079 }
3080
3081 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
3082
Harald Welte596fed42009-07-23 19:06:52 +02003083 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_IND, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003084}
3085
3086static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
3087{
3088 struct gsm_mncc *modify = arg;
3089 struct msgb *msg = gsm48_msgb_alloc();
3090 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3091
Harald Welte4bfdfe72009-06-10 23:11:52 +08003092 gh->msg_type = GSM48_MT_CC_MODIFY;
3093
3094 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
3095
3096 /* bearer capability */
3097 encode_bearer_cap(msg, 1, &modify->bearer_cap);
3098
3099 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
3100
Harald Welte39e2ead2009-07-23 21:13:03 +02003101 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003102}
3103
3104static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
3105{
3106 struct gsm48_hdr *gh = msgb_l3(msg);
3107 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
3108 struct tlv_parsed tp;
3109 struct gsm_mncc modify;
3110
3111 gsm48_stop_cc_timer(trans);
3112
3113 memset(&modify, 0, sizeof(struct gsm_mncc));
3114 modify.callref = trans->callref;
3115 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
3116 /* bearer capability */
3117 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
3118 modify.fields |= MNCC_F_BEARER_CAP;
3119 decode_bearer_cap(&modify.bearer_cap,
3120 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
3121 }
3122
3123 new_cc_state(trans, GSM_CSTATE_ACTIVE);
3124
Harald Welte596fed42009-07-23 19:06:52 +02003125 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_CNF, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003126}
3127
3128static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
3129{
3130 struct gsm_mncc *modify = arg;
3131 struct msgb *msg = gsm48_msgb_alloc();
3132 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3133
Harald Welte4bfdfe72009-06-10 23:11:52 +08003134 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
3135
3136 /* bearer capability */
3137 encode_bearer_cap(msg, 1, &modify->bearer_cap);
3138
3139 new_cc_state(trans, GSM_CSTATE_ACTIVE);
3140
Harald Welte39e2ead2009-07-23 21:13:03 +02003141 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003142}
3143
3144static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
3145{
3146 struct gsm48_hdr *gh = msgb_l3(msg);
3147 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
3148 struct tlv_parsed tp;
3149 struct gsm_mncc modify;
3150
3151 gsm48_stop_cc_timer(trans);
3152
3153 memset(&modify, 0, sizeof(struct gsm_mncc));
3154 modify.callref = trans->callref;
3155 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
3156 /* bearer capability */
3157 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
3158 modify.fields |= GSM48_IE_BEARER_CAP;
3159 decode_bearer_cap(&modify.bearer_cap,
3160 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
3161 }
3162 /* cause */
3163 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
3164 modify.fields |= MNCC_F_CAUSE;
3165 decode_cause(&modify.cause,
3166 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
3167 }
3168
3169 new_cc_state(trans, GSM_CSTATE_ACTIVE);
3170
Harald Welte596fed42009-07-23 19:06:52 +02003171 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_REJ, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003172}
3173
3174static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
3175{
3176 struct gsm_mncc *modify = arg;
3177 struct msgb *msg = gsm48_msgb_alloc();
3178 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3179
Harald Welte4bfdfe72009-06-10 23:11:52 +08003180 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
3181
3182 /* bearer capability */
3183 encode_bearer_cap(msg, 1, &modify->bearer_cap);
3184 /* cause */
3185 encode_cause(msg, 1, &modify->cause);
3186
3187 new_cc_state(trans, GSM_CSTATE_ACTIVE);
3188
Harald Welte39e2ead2009-07-23 21:13:03 +02003189 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003190}
3191
3192static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
3193{
3194 struct gsm_mncc *notify = arg;
3195 struct msgb *msg = gsm48_msgb_alloc();
3196 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3197
Harald Welte4bfdfe72009-06-10 23:11:52 +08003198 gh->msg_type = GSM48_MT_CC_NOTIFY;
3199
3200 /* notify */
3201 encode_notify(msg, notify->notify);
3202
Harald Welte39e2ead2009-07-23 21:13:03 +02003203 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003204}
3205
3206static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
3207{
3208 struct gsm48_hdr *gh = msgb_l3(msg);
3209 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
3210// struct tlv_parsed tp;
3211 struct gsm_mncc notify;
3212
3213 memset(&notify, 0, sizeof(struct gsm_mncc));
3214 notify.callref = trans->callref;
3215// tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len);
3216 if (payload_len >= 1)
3217 decode_notify(&notify.notify, gh->data);
3218
Harald Welte596fed42009-07-23 19:06:52 +02003219 return mncc_recvmsg(trans->subscr->net, trans, MNCC_NOTIFY_IND, &notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003220}
3221
3222static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
3223{
3224 struct gsm_mncc *user = arg;
3225 struct msgb *msg = gsm48_msgb_alloc();
3226 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3227
Harald Welte4bfdfe72009-06-10 23:11:52 +08003228 gh->msg_type = GSM48_MT_CC_USER_INFO;
3229
3230 /* user-user */
3231 if (user->fields & MNCC_F_USERUSER)
3232 encode_useruser(msg, 1, &user->useruser);
3233 /* more data */
3234 if (user->more)
3235 encode_more(msg);
3236
Harald Welte39e2ead2009-07-23 21:13:03 +02003237 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003238}
3239
3240static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
3241{
3242 struct gsm48_hdr *gh = msgb_l3(msg);
3243 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
3244 struct tlv_parsed tp;
3245 struct gsm_mncc user;
3246
3247 memset(&user, 0, sizeof(struct gsm_mncc));
3248 user.callref = trans->callref;
3249 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
3250 /* user-user */
3251 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
3252 user.fields |= MNCC_F_USERUSER;
3253 decode_useruser(&user.useruser,
3254 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
3255 }
3256 /* more data */
3257 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
3258 user.more = 1;
3259
Harald Welte596fed42009-07-23 19:06:52 +02003260 return mncc_recvmsg(trans->subscr->net, trans, MNCC_USERINFO_IND, &user);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003261}
3262
3263static int gsm48_lchan_modify(struct gsm_trans *trans, void *arg)
3264{
3265 struct gsm_mncc *mode = arg;
Harald Welte13cac662009-07-29 12:10:35 +02003266 int rc;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003267
Harald Welte13cac662009-07-29 12:10:35 +02003268 rc = gsm48_tx_chan_mode_modify(trans->lchan, mode->lchan_mode);
3269 if (rc < 0)
3270 return rc;
3271
3272 /* FIXME: we not only need to do this after mode modify, but
3273 * also after channel activation */
3274 if (is_ipaccess_bts(trans->lchan->ts->trx->bts) &&
3275 mode->lchan_mode != GSM48_CMODE_SIGN)
3276 rc = rsl_ipacc_bind(trans->lchan);
3277
3278 return rc;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003279}
3280
3281static struct downstate {
3282 u_int32_t states;
3283 int type;
3284 int (*rout) (struct gsm_trans *trans, void *arg);
3285} downstatelist[] = {
3286 /* mobile originating call establishment */
3287 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
3288 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc},
3289 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
3290 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
3291 {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 */
3292 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
3293 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
3294 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
3295 /* mobile terminating call establishment */
3296 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
3297 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
3298 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
3299 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
3300 /* signalling during call */
3301 {SBIT(GSM_CSTATE_ACTIVE),
3302 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
3303 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
3304 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
3305 {ALL_STATES,
3306 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
3307 {ALL_STATES,
3308 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
3309 {ALL_STATES,
3310 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
3311 {SBIT(GSM_CSTATE_ACTIVE),
3312 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
3313 {SBIT(GSM_CSTATE_ACTIVE),
3314 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
3315 {SBIT(GSM_CSTATE_ACTIVE),
3316 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
3317 {SBIT(GSM_CSTATE_ACTIVE),
3318 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
3319 {SBIT(GSM_CSTATE_ACTIVE),
3320 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
3321 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
3322 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
3323 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
3324 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
3325 {SBIT(GSM_CSTATE_ACTIVE),
3326 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
3327 /* clearing */
3328 {SBIT(GSM_CSTATE_INITIATED),
3329 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
3330 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
3331 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
3332 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
3333 MNCC_REL_REQ, gsm48_cc_tx_release},
3334 /* special */
3335 {ALL_STATES,
3336 MNCC_LCHAN_MODIFY, gsm48_lchan_modify},
3337};
3338
3339#define DOWNSLLEN \
3340 (sizeof(downstatelist) / sizeof(struct downstate))
3341
3342
3343int mncc_send(struct gsm_network *net, int msg_type, void *arg)
3344{
Harald Welte1a6f7982009-08-09 18:52:33 +02003345 int i, rc = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003346 struct gsm_trans *trans = NULL, *transt;
3347 struct gsm_subscriber *subscr;
Harald Welte1a6f7982009-08-09 18:52:33 +02003348 struct gsm_lchan *lchan = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003349 struct gsm_bts *bts = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003350 struct gsm_mncc *data = arg, rel;
3351
3352 /* handle special messages */
3353 switch(msg_type) {
3354 case MNCC_BRIDGE:
3355 return tch_bridge(net, arg);
3356 case MNCC_FRAME_DROP:
3357 return tch_recv(net, arg, 0);
3358 case MNCC_FRAME_RECV:
3359 return tch_recv(net, arg, 1);
3360 case GSM_TRAU_FRAME:
3361 return tch_frame(net, arg);
3362 }
3363
3364 memset(&rel, 0, sizeof(struct gsm_mncc));
3365 rel.callref = data->callref;
3366
3367 /* Find callref */
Harald Weltedcaf5652009-07-23 18:56:43 +02003368 trans = trans_find_by_callref(net, data->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003369
3370 /* Callref unknown */
3371 if (!trans) {
Harald Welte4a3464c2009-07-04 10:11:24 +02003372 if (msg_type != MNCC_SETUP_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003373 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3374 "Received '%s' from MNCC with "
3375 "unknown callref %d\n", data->called.number,
3376 get_mncc_name(msg_type), data->callref);
3377 /* Invalid call reference */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003378 return mncc_release_ind(net, NULL, data->callref,
3379 GSM48_CAUSE_LOC_PRN_S_LU,
3380 GSM48_CC_CAUSE_INVAL_TRANS_ID);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003381 }
Andreas Eversbergc079be42009-06-15 23:22:09 +02003382 if (!data->called.number[0] && !data->imsi[0]) {
3383 DEBUGP(DCC, "(bts - trx - ts - ti) "
3384 "Received '%s' from MNCC with "
3385 "no number or IMSI\n", get_mncc_name(msg_type));
3386 /* Invalid number */
3387 return mncc_release_ind(net, NULL, data->callref,
3388 GSM48_CAUSE_LOC_PRN_S_LU,
3389 GSM48_CC_CAUSE_INV_NR_FORMAT);
3390 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003391 /* New transaction due to setup, find subscriber */
Andreas Eversbergc079be42009-06-15 23:22:09 +02003392 if (data->called.number[0])
Harald Welte9176bd42009-07-23 18:46:00 +02003393 subscr = subscr_get_by_extension(net,
3394 data->called.number);
Andreas Eversbergc079be42009-06-15 23:22:09 +02003395 else
Harald Welte9176bd42009-07-23 18:46:00 +02003396 subscr = subscr_get_by_imsi(net, data->imsi);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003397 /* If subscriber is not found */
3398 if (!subscr) {
3399 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3400 "Received '%s' from MNCC with "
3401 "unknown subscriber %s\n", data->called.number,
3402 get_mncc_name(msg_type), data->called.number);
3403 /* Unknown subscriber */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003404 return mncc_release_ind(net, NULL, data->callref,
3405 GSM48_CAUSE_LOC_PRN_S_LU,
3406 GSM48_CC_CAUSE_UNASSIGNED_NR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003407 }
3408 /* If subscriber is not "attached" */
3409 if (!subscr->lac) {
3410 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3411 "Received '%s' from MNCC with "
3412 "detached subscriber %s\n", data->called.number,
3413 get_mncc_name(msg_type), data->called.number);
3414 subscr_put(subscr);
3415 /* Temporarily out of order */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003416 return mncc_release_ind(net, NULL, data->callref,
3417 GSM48_CAUSE_LOC_PRN_S_LU,
3418 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003419 }
3420 /* Create transaction */
Harald Weltedcaf5652009-07-23 18:56:43 +02003421 trans = trans_alloc(subscr, GSM48_PDISC_CC, 0xff, data->callref);
3422 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003423 DEBUGP(DCC, "No memory for trans.\n");
3424 subscr_put(subscr);
3425 /* Ressource unavailable */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003426 mncc_release_ind(net, NULL, data->callref,
3427 GSM48_CAUSE_LOC_PRN_S_LU,
3428 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003429 return -ENOMEM;
3430 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003431 /* Find lchan */
Harald Welte1a6f7982009-08-09 18:52:33 +02003432 lchan = lchan_for_subscr(subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003433 /* If subscriber has no lchan */
3434 if (!lchan) {
3435 /* find transaction with this subscriber already paging */
3436 llist_for_each_entry(transt, &net->trans_list, entry) {
3437 /* Transaction of our lchan? */
3438 if (transt == trans ||
3439 transt->subscr != subscr)
3440 continue;
3441 DEBUGP(DCC, "(bts %d trx - ts - ti -- sub %s) "
3442 "Received '%s' from MNCC with "
3443 "unallocated channel, paging already "
3444 "started.\n", bts->nr,
3445 data->called.number,
3446 get_mncc_name(msg_type));
3447 return 0;
3448 }
3449 /* store setup informations until paging was successfull */
Harald Weltedcaf5652009-07-23 18:56:43 +02003450 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
Harald Weltea1b28582009-08-01 19:31:47 +02003451 /* Trigger paging */
3452 paging_request(net, subscr, RSL_CHANNEED_TCH_F,
3453 setup_trig_pag_evt, subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003454 return 0;
3455 }
3456 /* Assign lchan */
3457 trans->lchan = lchan;
3458 use_lchan(lchan);
3459 }
3460 lchan = trans->lchan;
3461
3462 /* if paging did not respond yet */
3463 if (!lchan) {
3464 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3465 "Received '%s' from MNCC in paging state\n",
3466 (trans->subscr)?(trans->subscr->extension):"-",
3467 get_mncc_name(msg_type));
Harald Weltec66b71c2009-06-11 14:23:20 +08003468 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
3469 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003470 if (msg_type == MNCC_REL_REQ)
3471 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
3472 else
3473 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
3474 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02003475 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003476 return rc;
3477 }
3478
3479 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x sub %s) "
3480 "Received '%s' from MNCC in state %d (%s)\n",
3481 lchan->ts->trx->bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
3482 trans->transaction_id,
3483 (lchan->subscr)?(lchan->subscr->extension):"-",
Harald Weltedcaf5652009-07-23 18:56:43 +02003484 get_mncc_name(msg_type), trans->cc.state,
3485 cc_state_names[trans->cc.state]);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003486
3487 /* Find function for current state and message */
3488 for (i = 0; i < DOWNSLLEN; i++)
3489 if ((msg_type == downstatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003490 && ((1 << trans->cc.state) & downstatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003491 break;
3492 if (i == DOWNSLLEN) {
3493 DEBUGP(DCC, "Message unhandled at this state.\n");
3494 return 0;
3495 }
3496
3497 rc = downstatelist[i].rout(trans, arg);
3498
3499 return rc;
3500}
3501
3502
3503static struct datastate {
3504 u_int32_t states;
3505 int type;
3506 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
3507} datastatelist[] = {
3508 /* mobile originating call establishment */
3509 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3510 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
3511 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3512 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
3513 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
3514 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
3515 /* mobile terminating call establishment */
3516 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
3517 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
3518 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
3519 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
3520 {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 */
3521 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
3522 /* signalling during call */
3523 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
3524 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
3525 {SBIT(GSM_CSTATE_ACTIVE),
3526 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
3527 {ALL_STATES,
3528 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
3529 {ALL_STATES,
3530 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
3531 {ALL_STATES,
3532 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
3533 {SBIT(GSM_CSTATE_ACTIVE),
3534 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
3535 {SBIT(GSM_CSTATE_ACTIVE),
3536 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
3537 {SBIT(GSM_CSTATE_ACTIVE),
3538 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
3539 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3540 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
3541 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3542 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
3543 {SBIT(GSM_CSTATE_ACTIVE),
3544 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
3545 /* clearing */
3546 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
3547 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
3548 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
3549 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
3550 {ALL_STATES, /* 5.4.3.4 */
3551 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
3552};
3553
3554#define DATASLLEN \
3555 (sizeof(datastatelist) / sizeof(struct datastate))
3556
Harald Welte4bc90a12008-12-27 16:32:52 +00003557static int gsm0408_rcv_cc(struct msgb *msg)
3558{
3559 struct gsm48_hdr *gh = msgb_l3(msg);
3560 u_int8_t msg_type = gh->msg_type & 0xbf;
Harald Welte6f5aee02009-07-23 21:21:14 +02003561 u_int8_t transaction_id = ((gh->proto_discr & 0xf0) ^ 0x80) >> 4; /* flip */
Harald Welte4bfdfe72009-06-10 23:11:52 +08003562 struct gsm_lchan *lchan = msg->lchan;
Harald Weltedcaf5652009-07-23 18:56:43 +02003563 struct gsm_trans *trans = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003564 int i, rc = 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00003565
Harald Welte4bfdfe72009-06-10 23:11:52 +08003566 if (msg_type & 0x80) {
3567 DEBUGP(DCC, "MSG 0x%2x not defined for PD error\n", msg_type);
3568 return -EINVAL;
Harald Welte4bc90a12008-12-27 16:32:52 +00003569 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003570
3571 /* Find transaction */
Harald Welteb8b40732009-07-23 21:58:40 +02003572 trans = trans_find_by_id(lchan->subscr, GSM48_PDISC_CC, transaction_id);
Harald Weltedcaf5652009-07-23 18:56:43 +02003573
Harald Welte6f5aee02009-07-23 21:21:14 +02003574 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003575 "Received '%s' from MS in state %d (%s)\n",
3576 lchan->ts->trx->bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
3577 transaction_id, (lchan->subscr)?(lchan->subscr->extension):"-",
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +02003578 gsm0408_cc_msg_names[msg_type], trans?(trans->cc.state):0,
Harald Weltedcaf5652009-07-23 18:56:43 +02003579 cc_state_names[trans?(trans->cc.state):0]);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003580
3581 /* Create transaction */
3582 if (!trans) {
Harald Welte6f5aee02009-07-23 21:21:14 +02003583 DEBUGP(DCC, "Unknown transaction ID %x, "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003584 "creating new trans.\n", transaction_id);
3585 /* Create transaction */
Harald Weltedcaf5652009-07-23 18:56:43 +02003586 trans = trans_alloc(lchan->subscr, GSM48_PDISC_CC,
3587 transaction_id, new_callref++);
3588 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003589 DEBUGP(DCC, "No memory for trans.\n");
3590 rc = gsm48_tx_simple(msg->lchan,
Harald Welte6f5aee02009-07-23 21:21:14 +02003591 GSM48_PDISC_CC | (transaction_id << 4),
Harald Welte4bfdfe72009-06-10 23:11:52 +08003592 GSM48_MT_CC_RELEASE_COMPL);
3593 return -ENOMEM;
3594 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003595 /* Assign transaction */
Harald Welte4bfdfe72009-06-10 23:11:52 +08003596 trans->lchan = lchan;
3597 use_lchan(lchan);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003598 }
3599
3600 /* find function for current state and message */
3601 for (i = 0; i < DATASLLEN; i++)
3602 if ((msg_type == datastatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003603 && ((1 << trans->cc.state) & datastatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003604 break;
3605 if (i == DATASLLEN) {
3606 DEBUGP(DCC, "Message unhandled at this state.\n");
3607 return 0;
3608 }
3609
3610 rc = datastatelist[i].rout(trans, msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00003611
3612 return rc;
3613}
3614
Harald Welte52b1f982008-12-23 20:25:15 +00003615/* 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 +02003616int gsm0408_rcvmsg(struct msgb *msg, u_int8_t link_id)
Harald Welte52b1f982008-12-23 20:25:15 +00003617{
3618 struct gsm48_hdr *gh = msgb_l3(msg);
3619 u_int8_t pdisc = gh->proto_discr & 0x0f;
Harald Welte8470bf22008-12-25 23:28:35 +00003620 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00003621
3622 switch (pdisc) {
3623 case GSM48_PDISC_CC:
3624 rc = gsm0408_rcv_cc(msg);
3625 break;
3626 case GSM48_PDISC_MM:
3627 rc = gsm0408_rcv_mm(msg);
3628 break;
3629 case GSM48_PDISC_RR:
3630 rc = gsm0408_rcv_rr(msg);
3631 break;
Harald Weltebcae43f2008-12-27 21:45:37 +00003632 case GSM48_PDISC_SMS:
Harald Welte (local)daef6062009-08-14 11:41:12 +02003633 rc = gsm0411_rcv_sms(msg, link_id);
Harald Weltebcae43f2008-12-27 21:45:37 +00003634 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003635 case GSM48_PDISC_MM_GPRS:
Harald Weltebcae43f2008-12-27 21:45:37 +00003636 case GSM48_PDISC_SM_GPRS:
Harald Welte52b1f982008-12-23 20:25:15 +00003637 fprintf(stderr, "Unimplemented GSM 04.08 discriminator 0x%02d\n",
3638 pdisc);
3639 break;
3640 default:
3641 fprintf(stderr, "Unknown GSM 04.08 discriminator 0x%02d\n",
3642 pdisc);
3643 break;
3644 }
3645
3646 return rc;
3647}
Harald Welte8470bf22008-12-25 23:28:35 +00003648
Harald Welte4bfdfe72009-06-10 23:11:52 +08003649/* dequeue messages to layer 4 */
3650int bsc_upqueue(struct gsm_network *net)
3651{
3652 struct gsm_mncc *mncc;
3653 struct msgb *msg;
3654 int work = 0;
3655
3656 if (net)
3657 while ((msg = msgb_dequeue(&net->upqueue))) {
3658 mncc = (struct gsm_mncc *)msg->data;
3659 if (net->mncc_recv)
3660 net->mncc_recv(net, mncc->msg_type, mncc);
3661 work = 1; /* work done */
Harald Welte316c8252009-06-26 19:40:48 +02003662 talloc_free(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003663 }
3664
3665 return work;
3666}
Harald Weltedcaf5652009-07-23 18:56:43 +02003667
Harald Welte805f6442009-07-28 18:25:29 +02003668/*
3669 * This will be ran by the linker when loading the DSO. We use it to
3670 * do system initialization, e.g. registration of signal handlers.
3671 */
3672static __attribute__((constructor)) void on_dso_load_0408(void)
3673{
Harald Welte805f6442009-07-28 18:25:29 +02003674 register_signal_handler(SS_LCHAN, gsm0408_handle_lchan_signal, NULL);
3675 register_signal_handler(SS_ABISIP, handle_abisip_signal, NULL);
3676}