blob: 22713167181c385bbe77a337bb14f54e8c0a02ef [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 Weltef1dae192009-12-14 22:24:28 +010035#include <openbsc/bitvec.h>
Harald Welte7584aea2009-02-11 11:44:12 +000036#include <openbsc/tlv.h>
Harald Welte8470bf22008-12-25 23:28:35 +000037#include <openbsc/debug.h>
38#include <openbsc/gsm_data.h>
Daniel Willmanneea93372009-08-13 03:42:07 +020039#include <openbsc/gsm_utils.h>
Harald Welte8470bf22008-12-25 23:28:35 +000040#include <openbsc/gsm_subscriber.h>
Daniel Willmann8b3390e2008-12-28 00:31:09 +000041#include <openbsc/gsm_04_11.h>
Harald Welte8470bf22008-12-25 23:28:35 +000042#include <openbsc/gsm_04_08.h>
43#include <openbsc/abis_rsl.h>
Holger Freytherca362a62009-01-04 21:05:01 +000044#include <openbsc/chan_alloc.h>
Harald Welte0b4c34e2009-02-09 17:54:43 +000045#include <openbsc/paging.h>
Holger Freyther053e09d2009-02-14 22:51:06 +000046#include <openbsc/signal.h>
Harald Welte45b407a2009-05-23 15:51:12 +000047#include <openbsc/trau_frame.h>
Harald Welte11fa29c2009-02-19 17:24:39 +000048#include <openbsc/trau_mux.h>
Harald Welte805f6442009-07-28 18:25:29 +020049#include <openbsc/rtp_proxy.h>
Harald Welte2cf161b2009-06-20 22:36:41 +020050#include <openbsc/talloc.h>
Harald Weltedcaf5652009-07-23 18:56:43 +020051#include <openbsc/transaction.h>
Harald Welte6eafe912009-10-16 08:32:58 +020052#include <openbsc/ussd.h>
Harald Welte52b1f982008-12-23 20:25:15 +000053
Harald Welte0c389302009-06-10 12:08:54 +080054#define GSM_MAX_FACILITY 128
55#define GSM_MAX_SSVERSION 128
56#define GSM_MAX_USERUSER 128
57
Harald Welte (local)d19e58b2009-08-15 02:30:58 +020058void *tall_locop_ctx;
Harald Welte2cf161b2009-06-20 22:36:41 +020059
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +020060extern int ipacc_rtp_direct;
Harald Welte805f6442009-07-28 18:25:29 +020061
Harald Welte09e38af2009-02-16 22:52:23 +000062static const struct tlv_definition rsl_att_tlvdef = {
63 .def = {
64 [GSM48_IE_MOBILE_ID] = { TLV_TYPE_TLV },
65 [GSM48_IE_NAME_LONG] = { TLV_TYPE_TLV },
66 [GSM48_IE_NAME_SHORT] = { TLV_TYPE_TLV },
67 [GSM48_IE_UTC] = { TLV_TYPE_TV },
68 [GSM48_IE_NET_TIME_TZ] = { TLV_TYPE_FIXED, 7 },
69 [GSM48_IE_LSA_IDENT] = { TLV_TYPE_TLV },
70
71 [GSM48_IE_BEARER_CAP] = { TLV_TYPE_TLV },
72 [GSM48_IE_CAUSE] = { TLV_TYPE_TLV },
73 [GSM48_IE_CC_CAP] = { TLV_TYPE_TLV },
74 [GSM48_IE_ALERT] = { TLV_TYPE_TLV },
75 [GSM48_IE_FACILITY] = { TLV_TYPE_TLV },
76 [GSM48_IE_PROGR_IND] = { TLV_TYPE_TLV },
77 [GSM48_IE_AUX_STATUS] = { TLV_TYPE_TLV },
Harald Welte0c389302009-06-10 12:08:54 +080078 [GSM48_IE_NOTIFY] = { TLV_TYPE_TV },
Harald Welte09e38af2009-02-16 22:52:23 +000079 [GSM48_IE_KPD_FACILITY] = { TLV_TYPE_TV },
80 [GSM48_IE_SIGNAL] = { TLV_TYPE_TV },
Harald Welte0c389302009-06-10 12:08:54 +080081 [GSM48_IE_CONN_BCD] = { TLV_TYPE_TLV },
82 [GSM48_IE_CONN_SUB] = { TLV_TYPE_TLV },
Harald Welte09e38af2009-02-16 22:52:23 +000083 [GSM48_IE_CALLING_BCD] = { TLV_TYPE_TLV },
84 [GSM48_IE_CALLING_SUB] = { TLV_TYPE_TLV },
85 [GSM48_IE_CALLED_BCD] = { TLV_TYPE_TLV },
86 [GSM48_IE_CALLED_SUB] = { TLV_TYPE_TLV },
87 [GSM48_IE_REDIR_BCD] = { TLV_TYPE_TLV },
88 [GSM48_IE_REDIR_SUB] = { TLV_TYPE_TLV },
89 [GSM48_IE_LOWL_COMPAT] = { TLV_TYPE_TLV },
90 [GSM48_IE_HIGHL_COMPAT] = { TLV_TYPE_TLV },
91 [GSM48_IE_USER_USER] = { TLV_TYPE_TLV },
92 [GSM48_IE_SS_VERS] = { TLV_TYPE_TLV },
93 [GSM48_IE_MORE_DATA] = { TLV_TYPE_T },
94 [GSM48_IE_CLIR_SUPP] = { TLV_TYPE_T },
95 [GSM48_IE_CLIR_INVOC] = { TLV_TYPE_T },
96 [GSM48_IE_REV_C_SETUP] = { TLV_TYPE_T },
Harald Welte0c389302009-06-10 12:08:54 +080097 [GSM48_IE_REPEAT_CIR] = { TLV_TYPE_T },
98 [GSM48_IE_REPEAT_SEQ] = { TLV_TYPE_T },
Harald Welte09e38af2009-02-16 22:52:23 +000099 /* FIXME: more elements */
100 },
101};
Harald Weltecf5b3592009-05-01 18:28:42 +0000102
103static const char *rr_cause_names[] = {
104 [GSM48_RR_CAUSE_NORMAL] = "Normal event",
105 [GSM48_RR_CAUSE_ABNORMAL_UNSPEC] = "Abnormal release, unspecified",
106 [GSM48_RR_CAUSE_ABNORMAL_UNACCT] = "Abnormal release, channel unacceptable",
107 [GSM48_RR_CAUSE_ABNORMAL_TIMER] = "Abnormal release, timer expired",
108 [GSM48_RR_CAUSE_ABNORMAL_NOACT] = "Abnormal release, no activity on radio path",
109 [GSM48_RR_CAUSE_PREMPTIVE_REL] = "Preemptive release",
110 [GSM48_RR_CAUSE_HNDOVER_IMP] = "Handover impossible, timing advance out of range",
111 [GSM48_RR_CAUSE_CHAN_MODE_UNACCT] = "Channel mode unacceptable",
112 [GSM48_RR_CAUSE_FREQ_NOT_IMPL] = "Frequency not implemented",
113 [GSM48_RR_CAUSE_CALL_CLEARED] = "Call already cleared",
114 [GSM48_RR_CAUSE_SEMANT_INCORR] = "Semantically incorrect message",
115 [GSM48_RR_CAUSE_INVALID_MAND_INF] = "Invalid mandatory information",
116 [GSM48_RR_CAUSE_MSG_TYPE_N] = "Message type non-existant or not implemented",
117 [GSM48_RR_CAUSE_MSG_TYPE_N_COMPAT] = "Message type not compatible with protocol state",
118 [GSM48_RR_CAUSE_COND_IE_ERROR] = "Conditional IE error",
119 [GSM48_RR_CAUSE_NO_CELL_ALLOC_A] = "No cell allocation available",
120 [GSM48_RR_CAUSE_PROT_ERROR_UNSPC] = "Protocol error unspecified",
121};
122
Harald Welte4bfdfe72009-06-10 23:11:52 +0800123static const char *cc_state_names[] = {
124 "NULL",
125 "INITIATED",
126 "illegal state 2",
127 "MO_CALL_PROC",
128 "CALL_DELIVERED",
129 "illegal state 5",
130 "CALL_PRESENT",
131 "CALL_RECEIVED",
132 "CONNECT_REQUEST",
133 "MO_TERM_CALL_CONF",
134 "ACTIVE",
135 "DISCONNECT_REQ",
136 "DISCONNECT_IND",
137 "illegal state 13",
138 "illegal state 14",
139 "illegal state 15",
140 "illegal state 16",
141 "illegal state 17",
142 "illegal state 18",
143 "RELEASE_REQ",
144 "illegal state 20",
145 "illegal state 21",
146 "illegal state 22",
147 "illegal state 23",
148 "illegal state 24",
149 "illegal state 25",
150 "MO_ORIG_MODIFY",
151 "MO_TERM_MODIFY",
152 "CONNECT_IND",
153 "illegal state 29",
154 "illegal state 30",
155 "illegal state 31",
156};
157
Harald Weltecf5b3592009-05-01 18:28:42 +0000158static char strbuf[64];
159
160static const char *rr_cause_name(u_int8_t cause)
161{
162 if (cause < ARRAY_SIZE(rr_cause_names) &&
163 rr_cause_names[cause])
164 return rr_cause_names[cause];
165
166 snprintf(strbuf, sizeof(strbuf), "0x%02x", cause);
167 return strbuf;
168}
169
Harald Welte3c7dc6e2009-11-29 19:07:28 +0100170int gsm48_parse_meas_rep(struct gsm_meas_rep *rep, struct msgb *msg)
Harald Weltef7c43522009-06-09 20:24:21 +0000171{
Harald Welte3c7dc6e2009-11-29 19:07:28 +0100172 struct gsm48_hdr *gh = msgb_l3(msg);
173 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
174 u_int8_t *data = gh->data;
Harald Weltef1dae192009-12-14 22:24:28 +0100175 struct gsm_bts *bts = msg->lchan->ts->trx->bts;
176 struct bitvec *nbv = &bts->si_common.neigh_list;
Harald Welte3c7dc6e2009-11-29 19:07:28 +0100177
178 if (gh->msg_type != GSM48_MT_RR_MEAS_REP)
179 return -EINVAL;
Harald Weltef7c43522009-06-09 20:24:21 +0000180
181 if (data[0] & 0x80)
182 rep->flags |= MEAS_REP_F_BA1;
183 if (data[0] & 0x40)
Harald Welte3c7dc6e2009-11-29 19:07:28 +0100184 rep->flags |= MEAS_REP_F_UL_DTX;
Harald Welte5a691b52009-07-05 04:05:44 +0200185 if ((data[1] & 0x40) == 0x00)
Harald Welte3c7dc6e2009-11-29 19:07:28 +0100186 rep->flags |= MEAS_REP_F_DL_VALID;
Harald Weltef7c43522009-06-09 20:24:21 +0000187
Harald Welte3c7dc6e2009-11-29 19:07:28 +0100188 rep->dl.full.rx_lev = data[0] & 0x3f;
189 rep->dl.sub.rx_lev = data[1] & 0x3f;
190 rep->dl.full.rx_qual = (data[3] >> 4) & 0x7;
191 rep->dl.sub.rx_qual = (data[3] >> 1) & 0x7;
192
Harald Weltea2f74b82009-12-16 16:49:21 +0100193 rep->num_cell = ((data[3] >> 6) & 0x3) | ((data[2] & 0x01) << 2);
Harald Welte51cec5f2009-12-19 16:45:29 +0100194 if (rep->num_cell < 1 || rep->num_cell > 6)
Harald Welte3c7dc6e2009-11-29 19:07:28 +0100195 return 0;
Harald Weltef7c43522009-06-09 20:24:21 +0000196
197 /* an encoding nightmare in perfection */
198
Harald Weltea2f74b82009-12-16 16:49:21 +0100199 rep->cell[0].rxlev = data[3] & 0x3f;
200 rep->cell[0].arfcn = bitvec_get_nth_set_bit(nbv, data[4] >> 2);
Harald Welte73ddaed2009-12-16 23:29:12 +0100201 rep->cell[0].bsic = ((data[4] & 0x07) << 3) | (data[5] >> 5);
Harald Weltef7c43522009-06-09 20:24:21 +0000202 if (rep->num_cell < 2)
Harald Welte3c7dc6e2009-11-29 19:07:28 +0100203 return 0;
Harald Weltef7c43522009-06-09 20:24:21 +0000204
Harald Weltea2f74b82009-12-16 16:49:21 +0100205 rep->cell[1].rxlev = ((data[5] & 0x1f) << 1) | (data[6] >> 7);
206 rep->cell[1].arfcn = bitvec_get_nth_set_bit(nbv, (data[6] >> 2) & 0x1f);
207 rep->cell[1].bsic = ((data[6] & 0x03) << 4) | (data[7] >> 4);
Harald Weltef7c43522009-06-09 20:24:21 +0000208 if (rep->num_cell < 3)
Harald Welte3c7dc6e2009-11-29 19:07:28 +0100209 return 0;
Harald Weltef7c43522009-06-09 20:24:21 +0000210
Harald Weltea2f74b82009-12-16 16:49:21 +0100211 rep->cell[2].rxlev = ((data[7] & 0x0f) << 2) | (data[8] >> 6);
212 rep->cell[2].arfcn = bitvec_get_nth_set_bit(nbv, (data[8] >> 1) & 0x1f);
213 rep->cell[2].bsic = ((data[8] & 0x01) << 6) | (data[9] >> 3);
Harald Weltef7c43522009-06-09 20:24:21 +0000214 if (rep->num_cell < 4)
Harald Welte3c7dc6e2009-11-29 19:07:28 +0100215 return 0;
Harald Weltef7c43522009-06-09 20:24:21 +0000216
Harald Weltea2f74b82009-12-16 16:49:21 +0100217 rep->cell[3].rxlev = ((data[9] & 0x07) << 3) | (data[10] >> 5);
218 rep->cell[3].arfcn = bitvec_get_nth_set_bit(nbv, data[10] & 0x1f);
219 rep->cell[3].bsic = data[11] >> 2;
Harald Weltef7c43522009-06-09 20:24:21 +0000220 if (rep->num_cell < 5)
Harald Welte3c7dc6e2009-11-29 19:07:28 +0100221 return 0;
Harald Weltef7c43522009-06-09 20:24:21 +0000222
Harald Weltea2f74b82009-12-16 16:49:21 +0100223 rep->cell[4].rxlev = ((data[11] & 0x03) << 4) | (data[12] >> 4);
Harald Weltef1dae192009-12-14 22:24:28 +0100224 rep->cell[4].arfcn = bitvec_get_nth_set_bit(nbv,
Harald Weltea2f74b82009-12-16 16:49:21 +0100225 ((data[12] & 0xf) << 1) | (data[13] >> 7));
226 rep->cell[4].bsic = (data[13] >> 1) & 0x3f;
Harald Weltef7c43522009-06-09 20:24:21 +0000227 if (rep->num_cell < 6)
Harald Welte3c7dc6e2009-11-29 19:07:28 +0100228 return 0;
Harald Weltef7c43522009-06-09 20:24:21 +0000229
Harald Weltea2f74b82009-12-16 16:49:21 +0100230 rep->cell[5].rxlev = ((data[13] & 0x01) << 5) | (data[14] >> 3);
Harald Weltef1dae192009-12-14 22:24:28 +0100231 rep->cell[5].arfcn = bitvec_get_nth_set_bit(nbv,
Harald Weltea2f74b82009-12-16 16:49:21 +0100232 ((data[14] & 0x07) << 2) | (data[15] >> 6));
233 rep->cell[5].bsic = data[15] & 0x3f;
Harald Welte3c7dc6e2009-11-29 19:07:28 +0100234
235 return 0;
Harald Weltef7c43522009-06-09 20:24:21 +0000236}
237
Holger Freytherd51524f2009-06-09 08:27:07 +0000238int gsm0408_loc_upd_acc(struct gsm_lchan *lchan, u_int32_t tmsi);
Harald Welte65e74cc2008-12-29 01:55:35 +0000239static int gsm48_tx_simple(struct gsm_lchan *lchan,
240 u_int8_t pdisc, u_int8_t msg_type);
Holger Freytherb7193e42008-12-29 17:44:08 +0000241static void schedule_reject(struct gsm_lchan *lchan);
Harald Welte65e74cc2008-12-29 01:55:35 +0000242
Harald Welte52b1f982008-12-23 20:25:15 +0000243struct gsm_lai {
244 u_int16_t mcc;
245 u_int16_t mnc;
246 u_int16_t lac;
247};
248
Harald Welte4bfdfe72009-06-10 23:11:52 +0800249static u_int32_t new_callref = 0x80000001;
250
Holger Freyther73487a22008-12-31 18:53:57 +0000251static int authorize_subscriber(struct gsm_loc_updating_operation *loc,
252 struct gsm_subscriber *subscriber)
Holger Freyther89824fc2008-12-30 16:18:18 +0000253{
254 if (!subscriber)
255 return 0;
256
Holger Freyther73487a22008-12-31 18:53:57 +0000257 /*
258 * Do not send accept yet as more information should arrive. Some
259 * phones will not send us the information and we will have to check
260 * what we want to do with that.
261 */
262 if (loc && (loc->waiting_for_imsi || loc->waiting_for_imei))
263 return 0;
264
Jan Luebbe06513f22009-08-12 12:48:00 +0200265 switch (subscriber->net->auth_policy) {
266 case GSM_AUTH_POLICY_CLOSED:
267 return subscriber->authorized;
Harald Welte (local)aa9dc192009-08-13 13:49:51 +0200268 case GSM_AUTH_POLICY_TOKEN:
Harald Welte (local)ee9afe32009-08-13 20:44:23 +0200269 if (subscriber->authorized)
270 return subscriber->authorized;
Harald Welte (local)aa9dc192009-08-13 13:49:51 +0200271 return (subscriber->flags & GSM_SUBSCRIBER_FIRST_CONTACT);
Jan Luebbe06513f22009-08-12 12:48:00 +0200272 case GSM_AUTH_POLICY_ACCEPT_ALL:
Holger Freyther89824fc2008-12-30 16:18:18 +0000273 return 1;
Jan Luebbe06513f22009-08-12 12:48:00 +0200274 default:
275 return 0;
276 }
Holger Freyther89824fc2008-12-30 16:18:18 +0000277}
Holger Freyther07cc8d82008-12-29 06:23:46 +0000278
Holger Freyther73487a22008-12-31 18:53:57 +0000279static void release_loc_updating_req(struct gsm_lchan *lchan)
280{
Harald Welte179f0642008-12-31 23:59:18 +0000281 if (!lchan->loc_operation)
Holger Freyther73487a22008-12-31 18:53:57 +0000282 return;
283
Harald Welteff117a82009-05-23 05:22:08 +0000284 bsc_del_timer(&lchan->loc_operation->updating_timer);
Harald Welte2cf161b2009-06-20 22:36:41 +0200285 talloc_free(lchan->loc_operation);
Holger Freyther73487a22008-12-31 18:53:57 +0000286 lchan->loc_operation = 0;
Holger Freyther3eaa7922009-01-01 02:59:03 +0000287 put_lchan(lchan);
Holger Freyther73487a22008-12-31 18:53:57 +0000288}
289
290static void allocate_loc_updating_req(struct gsm_lchan *lchan)
291{
Holger Freyther67b4b9a2009-01-01 03:46:11 +0000292 use_lchan(lchan);
Holger Freyther73487a22008-12-31 18:53:57 +0000293 release_loc_updating_req(lchan);
294
Harald Welte470ec292009-06-26 20:25:23 +0200295 lchan->loc_operation = talloc_zero(tall_locop_ctx,
296 struct gsm_loc_updating_operation);
Holger Freyther73487a22008-12-31 18:53:57 +0000297}
Holger Freyther07cc8d82008-12-29 06:23:46 +0000298
Holger Freytherd51524f2009-06-09 08:27:07 +0000299static int gsm0408_authorize(struct gsm_lchan *lchan, struct msgb *msg)
300{
Holger Freytherd51524f2009-06-09 08:27:07 +0000301 if (authorize_subscriber(lchan->loc_operation, lchan->subscr)) {
Harald Welteee5ad162009-08-09 19:07:00 +0200302 int rc;
303
Holger Freytherd51524f2009-06-09 08:27:07 +0000304 db_subscriber_alloc_tmsi(lchan->subscr);
Holger Freytherd51524f2009-06-09 08:27:07 +0000305 release_loc_updating_req(lchan);
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200306 rc = gsm0408_loc_upd_acc(msg->lchan, lchan->subscr->tmsi);
Harald Welte648b6ce2009-12-14 09:00:24 +0100307 if (lchan->ts->trx->bts->network->send_mm_info) {
308 /* send MM INFO with network name */
309 rc = gsm48_tx_mm_info(msg->lchan);
310 }
Harald Welteb83d9382009-12-12 21:00:48 +0100311
Harald Welteee5ad162009-08-09 19:07:00 +0200312 /* call subscr_update after putting the loc_upd_acc
313 * in the transmit queue, since S_SUBSCR_ATTACHED might
314 * trigger further action like SMS delivery */
315 subscr_update(lchan->subscr, msg->trx->bts,
316 GSM_SUBSCRIBER_UPDATE_ATTACHED);
Harald Welteb83d9382009-12-12 21:00:48 +0100317 /* try to close channel ASAP */
318 lchan_auto_release(lchan);
Harald Welteee5ad162009-08-09 19:07:00 +0200319 return rc;
Holger Freytherd51524f2009-06-09 08:27:07 +0000320 }
321
322 return 0;
323}
324
Holger Freyther7c19f742009-06-06 13:54:35 +0000325static int gsm0408_handle_lchan_signal(unsigned int subsys, unsigned int signal,
326 void *handler_data, void *signal_data)
327{
Harald Welte4bfdfe72009-06-10 23:11:52 +0800328 struct gsm_trans *trans, *temp;
329
Holger Freyther7c19f742009-06-06 13:54:35 +0000330 if (subsys != SS_LCHAN || signal != S_LCHAN_UNEXPECTED_RELEASE)
331 return 0;
332
333 /*
334 * Cancel any outstanding location updating request
335 * operation taking place on the lchan.
336 */
Harald Welte1a5c6bd2009-07-04 09:35:21 +0200337 struct gsm_lchan *lchan = (struct gsm_lchan *)signal_data;
Harald Weltec05677b2009-06-26 20:17:06 +0200338 if (!lchan)
339 return 0;
340
Holger Freyther7c19f742009-06-06 13:54:35 +0000341 release_loc_updating_req(lchan);
342
Harald Welte4bfdfe72009-06-10 23:11:52 +0800343 /* Free all transactions that are associated with the released lchan */
Harald Weltedcaf5652009-07-23 18:56:43 +0200344 /* FIXME: this is not neccessarily the right thing to do, we should
345 * only set trans->lchan to NULL and wait for another lchan to be
346 * established to the same MM entity (phone/subscriber) */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800347 llist_for_each_entry_safe(trans, temp, &lchan->ts->trx->bts->network->trans_list, entry) {
348 if (trans->lchan == lchan)
Harald Weltedcaf5652009-07-23 18:56:43 +0200349 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +0800350 }
351
Holger Freyther7c19f742009-06-06 13:54:35 +0000352 return 0;
353}
354
Harald Welte09e38af2009-02-16 22:52:23 +0000355static const char bcd_num_digits[] = {
356 '0', '1', '2', '3', '4', '5', '6', '7',
357 '8', '9', '*', '#', 'a', 'b', 'c', '\0'
358};
359
Harald Welte0c389302009-06-10 12:08:54 +0800360/* decode a 'called/calling/connect party BCD number' as in 10.5.4.7 */
361int decode_bcd_number(char *output, int output_len, const u_int8_t *bcd_lv,
362 int h_len)
Harald Welte09e38af2009-02-16 22:52:23 +0000363{
364 u_int8_t in_len = bcd_lv[0];
365 int i;
366
Harald Welte0c389302009-06-10 12:08:54 +0800367 for (i = 1 + h_len; i <= in_len; i++) {
Harald Welte09e38af2009-02-16 22:52:23 +0000368 /* lower nibble */
369 output_len--;
370 if (output_len <= 1)
371 break;
372 *output++ = bcd_num_digits[bcd_lv[i] & 0xf];
373
374 /* higher nibble */
375 output_len--;
376 if (output_len <= 1)
377 break;
378 *output++ = bcd_num_digits[bcd_lv[i] >> 4];
379 }
380 if (output_len >= 1)
381 *output++ = '\0';
382
Harald Welte0c389302009-06-10 12:08:54 +0800383 return 0;
Harald Welte09e38af2009-02-16 22:52:23 +0000384}
385
386/* convert a single ASCII character to call-control BCD */
387static int asc_to_bcd(const char asc)
388{
389 int i;
390
391 for (i = 0; i < ARRAY_SIZE(bcd_num_digits); i++) {
392 if (bcd_num_digits[i] == asc)
393 return i;
394 }
395 return -EINVAL;
396}
397
Harald Welte0c389302009-06-10 12:08:54 +0800398/* convert a ASCII phone number to 'called/calling/connect party BCD number' */
Harald Welte09e38af2009-02-16 22:52:23 +0000399int encode_bcd_number(u_int8_t *bcd_lv, u_int8_t max_len,
Harald Welte0c389302009-06-10 12:08:54 +0800400 int h_len, const char *input)
Harald Welte09e38af2009-02-16 22:52:23 +0000401{
402 int in_len = strlen(input);
403 int i;
Harald Welte0c389302009-06-10 12:08:54 +0800404 u_int8_t *bcd_cur = bcd_lv + 1 + h_len;
Harald Welte09e38af2009-02-16 22:52:23 +0000405
406 /* two digits per byte, plus type byte */
Harald Welte0c389302009-06-10 12:08:54 +0800407 bcd_lv[0] = in_len/2 + h_len;
Harald Welte09e38af2009-02-16 22:52:23 +0000408 if (in_len % 2)
409 bcd_lv[0]++;
410
Harald Welte0c389302009-06-10 12:08:54 +0800411 if (bcd_lv[0] > max_len)
412 return -EIO;
Harald Welte09e38af2009-02-16 22:52:23 +0000413
414 for (i = 0; i < in_len; i++) {
415 int rc = asc_to_bcd(input[i]);
416 if (rc < 0)
417 return rc;
418 if (i % 2 == 0)
419 *bcd_cur = rc;
420 else
421 *bcd_cur++ |= (rc << 4);
422 }
423 /* append padding nibble in case of odd length */
424 if (i % 2)
425 *bcd_cur++ |= 0xf0;
426
427 /* return how many bytes we used */
428 return (bcd_cur - bcd_lv);
429}
430
Harald Welte0c389302009-06-10 12:08:54 +0800431/* decode 'bearer capability' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800432static int decode_bearer_cap(struct gsm_mncc_bearer_cap *bcap,
Harald Welte0c389302009-06-10 12:08:54 +0800433 const u_int8_t *lv)
434{
435 u_int8_t in_len = lv[0];
436 int i, s;
437
438 if (in_len < 1)
439 return -EINVAL;
440
Harald Welte4bfdfe72009-06-10 23:11:52 +0800441 bcap->speech_ver[0] = -1; /* end of list, of maximum 7 values */
Harald Welte0c389302009-06-10 12:08:54 +0800442
443 /* octet 3 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800444 bcap->transfer = lv[1] & 0x07;
445 bcap->mode = (lv[1] & 0x08) >> 3;
446 bcap->coding = (lv[1] & 0x10) >> 4;
447 bcap->radio = (lv[1] & 0x60) >> 5;
Harald Welte0c389302009-06-10 12:08:54 +0800448
Harald Welte09b7e7f2009-12-12 21:36:53 +0100449 if (bcap->transfer == GSM_MNCC_BCAP_SPEECH) {
450 i = 1;
451 s = 0;
452 while(!(lv[i] & 0x80)) {
453 i++; /* octet 3a etc */
454 if (in_len < i)
455 return 0;
456 bcap->speech_ver[s++] = lv[i] & 0x0f;
457 bcap->speech_ver[s] = -1; /* end of list */
458 if (i == 2) /* octet 3a */
459 bcap->speech_ctm = (lv[i] & 0x20) >> 5;
460 if (s == 7) /* maximum speech versions + end of list */
461 return 0;
462 }
463 } else {
464 i = 1;
465 while (!(lv[i] & 0x80)) {
466 i++; /* octet 3a etc */
467 if (in_len < i)
468 return 0;
469 /* ignore them */
470 }
471 /* FIXME: implement OCTET 4+ parsing */
Harald Welte0c389302009-06-10 12:08:54 +0800472 }
473
474 return 0;
475}
476
477/* encode 'bearer capability' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800478static int encode_bearer_cap(struct msgb *msg, int lv_only,
479 const struct gsm_mncc_bearer_cap *bcap)
Harald Welte0c389302009-06-10 12:08:54 +0800480{
481 u_int8_t lv[32 + 1];
Harald Welte09b7e7f2009-12-12 21:36:53 +0100482 int i = 1, s;
Harald Welte0c389302009-06-10 12:08:54 +0800483
Harald Welte4bfdfe72009-06-10 23:11:52 +0800484 lv[1] = bcap->transfer;
485 lv[1] |= bcap->mode << 3;
486 lv[1] |= bcap->coding << 4;
487 lv[1] |= bcap->radio << 5;
Harald Welte0c389302009-06-10 12:08:54 +0800488
Harald Welte09b7e7f2009-12-12 21:36:53 +0100489 if (bcap->transfer == GSM_MNCC_BCAP_SPEECH) {
490 for (s = 0; bcap->speech_ver[s] >= 0; s++) {
491 i++; /* octet 3a etc */
492 lv[i] = bcap->speech_ver[s];
493 if (i == 2) /* octet 3a */
494 lv[i] |= bcap->speech_ctm << 5;
495 }
496 lv[i] |= 0x80; /* last IE of octet 3 etc */
497 } else {
498 /* FIXME: implement OCTET 4+ encoding */
Harald Welte0c389302009-06-10 12:08:54 +0800499 }
Harald Welte0c389302009-06-10 12:08:54 +0800500
501 lv[0] = i;
502 if (lv_only)
503 msgb_lv_put(msg, lv[0], lv+1);
504 else
505 msgb_tlv_put(msg, GSM48_IE_BEARER_CAP, lv[0], lv+1);
506
507 return 0;
508}
509
510/* decode 'call control cap' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800511static int decode_cccap(struct gsm_mncc_cccap *ccap, const u_int8_t *lv)
Harald Welte0c389302009-06-10 12:08:54 +0800512{
513 u_int8_t in_len = lv[0];
514
515 if (in_len < 1)
516 return -EINVAL;
517
518 /* octet 3 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800519 ccap->dtmf = lv[1] & 0x01;
520 ccap->pcp = (lv[1] & 0x02) >> 1;
Harald Welte0c389302009-06-10 12:08:54 +0800521
522 return 0;
523}
524
525/* decode 'called party BCD number' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800526static int decode_called(struct gsm_mncc_number *called,
527 const u_int8_t *lv)
Harald Welte0c389302009-06-10 12:08:54 +0800528{
529 u_int8_t in_len = lv[0];
530
531 if (in_len < 1)
532 return -EINVAL;
533
534 /* octet 3 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800535 called->plan = lv[1] & 0x0f;
536 called->type = (lv[1] & 0x70) >> 4;
Harald Welte0c389302009-06-10 12:08:54 +0800537
538 /* octet 4..N */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800539 decode_bcd_number(called->number, sizeof(called->number), lv, 1);
Harald Welte0c389302009-06-10 12:08:54 +0800540
541 return 0;
542}
543
544/* encode 'called party BCD number' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800545static int encode_called(struct msgb *msg,
546 const struct gsm_mncc_number *called)
Harald Welte0c389302009-06-10 12:08:54 +0800547{
548 u_int8_t lv[18];
549 int ret;
550
551 /* octet 3 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800552 lv[1] = called->plan;
553 lv[1] |= called->type << 4;
Harald Welte0c389302009-06-10 12:08:54 +0800554
555 /* octet 4..N, octet 2 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800556 ret = encode_bcd_number(lv, sizeof(lv), 1, called->number);
Harald Welte0c389302009-06-10 12:08:54 +0800557 if (ret < 0)
558 return ret;
559
560 msgb_tlv_put(msg, GSM48_IE_CALLED_BCD, lv[0], lv+1);
561
562 return 0;
563}
564
565/* encode callerid of various IEs */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800566static int encode_callerid(struct msgb *msg, int ie,
567 const struct gsm_mncc_number *callerid)
Harald Welte0c389302009-06-10 12:08:54 +0800568{
569 u_int8_t lv[13];
570 int h_len = 1;
571 int ret;
572
573 /* octet 3 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800574 lv[1] = callerid->plan;
575 lv[1] |= callerid->type << 4;
Harald Welte0c389302009-06-10 12:08:54 +0800576
Harald Welte4bfdfe72009-06-10 23:11:52 +0800577 if (callerid->present || callerid->screen) {
Harald Welte0c389302009-06-10 12:08:54 +0800578 /* octet 3a */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800579 lv[2] = callerid->screen;
580 lv[2] |= callerid->present << 5;
Harald Welte0c389302009-06-10 12:08:54 +0800581 lv[2] |= 0x80;
582 h_len++;
583 } else
584 lv[1] |= 0x80;
585
586 /* octet 4..N, octet 2 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800587 ret = encode_bcd_number(lv, sizeof(lv), h_len, callerid->number);
Harald Welte0c389302009-06-10 12:08:54 +0800588 if (ret < 0)
589 return ret;
590
591 msgb_tlv_put(msg, ie, lv[0], lv+1);
592
593 return 0;
594}
595
596/* decode 'cause' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800597static int decode_cause(struct gsm_mncc_cause *cause,
Harald Welte0c389302009-06-10 12:08:54 +0800598 const u_int8_t *lv)
599{
600 u_int8_t in_len = lv[0];
601 int i;
602
603 if (in_len < 2)
604 return -EINVAL;
605
Harald Welte4bfdfe72009-06-10 23:11:52 +0800606 cause->diag_len = 0;
Harald Welte0c389302009-06-10 12:08:54 +0800607
608 /* octet 3 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800609 cause->location = lv[1] & 0x0f;
610 cause->coding = (lv[1] & 0x60) >> 5;
Harald Welte0c389302009-06-10 12:08:54 +0800611
612 i = 1;
613 if (!(lv[i] & 0x80)) {
614 i++; /* octet 3a */
615 if (in_len < i+1)
616 return 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800617 cause->rec = 1;
618 cause->rec_val = lv[i] & 0x7f;
Harald Welte0c389302009-06-10 12:08:54 +0800619
620 }
621 i++;
622
623 /* octet 4 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800624 cause->value = lv[i] & 0x7f;
Harald Welte0c389302009-06-10 12:08:54 +0800625 i++;
626
627 if (in_len < i) /* no diag */
628 return 0;
629
630 if (in_len - (i-1) > 32) /* maximum 32 octets */
631 return 0;
632
633 /* octet 5-N */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800634 memcpy(cause->diag, lv + i, in_len - (i-1));
635 cause->diag_len = in_len - (i-1);
Harald Welte0c389302009-06-10 12:08:54 +0800636
637 return 0;
638}
639
640/* encode 'cause' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800641static int encode_cause(struct msgb *msg, int lv_only,
642 const struct gsm_mncc_cause *cause)
Harald Welte0c389302009-06-10 12:08:54 +0800643{
644 u_int8_t lv[32+4];
645 int i;
646
Harald Welte4bfdfe72009-06-10 23:11:52 +0800647 if (cause->diag_len > 32)
Harald Welte0c389302009-06-10 12:08:54 +0800648 return -EINVAL;
649
650 /* octet 3 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800651 lv[1] = cause->location;
652 lv[1] |= cause->coding << 5;
Harald Welte0c389302009-06-10 12:08:54 +0800653
654 i = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800655 if (cause->rec) {
Harald Welte0c389302009-06-10 12:08:54 +0800656 i++; /* octet 3a */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800657 lv[i] = cause->rec_val;
Harald Welte0c389302009-06-10 12:08:54 +0800658 }
659 lv[i] |= 0x80; /* end of octet 3 */
660
661 /* octet 4 */
662 i++;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800663 lv[i] = 0x80 | cause->value;
Harald Welte0c389302009-06-10 12:08:54 +0800664
665 /* octet 5-N */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800666 if (cause->diag_len) {
667 memcpy(lv + i, cause->diag, cause->diag_len);
668 i += cause->diag_len;
Harald Welte0c389302009-06-10 12:08:54 +0800669 }
670
671 lv[0] = i;
672 if (lv_only)
673 msgb_lv_put(msg, lv[0], lv+1);
674 else
675 msgb_tlv_put(msg, GSM48_IE_CAUSE, lv[0], lv+1);
676
677 return 0;
678}
679
680/* encode 'calling number' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800681static int encode_calling(struct msgb *msg,
682 const struct gsm_mncc_number *calling)
Harald Welte0c389302009-06-10 12:08:54 +0800683{
Harald Welte4bfdfe72009-06-10 23:11:52 +0800684 return encode_callerid(msg, GSM48_IE_CALLING_BCD, calling);
Harald Welte0c389302009-06-10 12:08:54 +0800685}
686
687/* encode 'connected number' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800688static int encode_connected(struct msgb *msg,
689 const struct gsm_mncc_number *connected)
Harald Welte0c389302009-06-10 12:08:54 +0800690{
Harald Welte4bfdfe72009-06-10 23:11:52 +0800691 return encode_callerid(msg, GSM48_IE_CONN_BCD, connected);
Harald Welte0c389302009-06-10 12:08:54 +0800692}
693
694/* encode 'redirecting number' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800695static int encode_redirecting(struct msgb *msg,
696 const struct gsm_mncc_number *redirecting)
Harald Welte0c389302009-06-10 12:08:54 +0800697{
Harald Welte4bfdfe72009-06-10 23:11:52 +0800698 return encode_callerid(msg, GSM48_IE_REDIR_BCD, redirecting);
Harald Welte0c389302009-06-10 12:08:54 +0800699}
700
701/* decode 'facility' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800702static int decode_facility(struct gsm_mncc_facility *facility,
Harald Welte0c389302009-06-10 12:08:54 +0800703 const u_int8_t *lv)
704{
705 u_int8_t in_len = lv[0];
706
707 if (in_len < 1)
708 return -EINVAL;
709
Harald Welte4bfdfe72009-06-10 23:11:52 +0800710 if (in_len > sizeof(facility->info))
Harald Welte0c389302009-06-10 12:08:54 +0800711 return -EINVAL;
712
Harald Welte4bfdfe72009-06-10 23:11:52 +0800713 memcpy(facility->info, lv+1, in_len);
714 facility->len = in_len;
Harald Welte0c389302009-06-10 12:08:54 +0800715
716 return 0;
717}
718
719/* encode 'facility' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800720static int encode_facility(struct msgb *msg, int lv_only,
721 const struct gsm_mncc_facility *facility)
Harald Welte0c389302009-06-10 12:08:54 +0800722{
723 u_int8_t lv[GSM_MAX_FACILITY + 1];
724
Harald Welte4bfdfe72009-06-10 23:11:52 +0800725 if (facility->len < 1 || facility->len > GSM_MAX_FACILITY)
Harald Welte0c389302009-06-10 12:08:54 +0800726 return -EINVAL;
727
Harald Welte4bfdfe72009-06-10 23:11:52 +0800728 memcpy(lv+1, facility->info, facility->len);
729 lv[0] = facility->len;
Harald Welte0c389302009-06-10 12:08:54 +0800730 if (lv_only)
731 msgb_lv_put(msg, lv[0], lv+1);
732 else
733 msgb_tlv_put(msg, GSM48_IE_FACILITY, lv[0], lv+1);
734
735 return 0;
736}
737
738/* decode 'notify' */
739static int decode_notify(int *notify, const u_int8_t *v)
740{
741 *notify = v[0] & 0x7f;
742
743 return 0;
744}
745
746/* encode 'notify' */
747static int encode_notify(struct msgb *msg, int notify)
748{
749 msgb_v_put(msg, notify | 0x80);
750
751 return 0;
752}
753
754/* encode 'signal' */
755static int encode_signal(struct msgb *msg, int signal)
756{
757 msgb_tv_put(msg, GSM48_IE_SIGNAL, signal);
758
759 return 0;
760}
761
762/* decode 'keypad' */
763static int decode_keypad(int *keypad, const u_int8_t *lv)
764{
765 u_int8_t in_len = lv[0];
766
767 if (in_len < 1)
768 return -EINVAL;
769
770 *keypad = lv[1] & 0x7f;
771
772 return 0;
773}
774
775/* encode 'keypad' */
776static int encode_keypad(struct msgb *msg, int keypad)
777{
778 msgb_tv_put(msg, GSM48_IE_KPD_FACILITY, keypad);
779
780 return 0;
781}
782
783/* decode 'progress' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800784static int decode_progress(struct gsm_mncc_progress *progress,
Harald Welte0c389302009-06-10 12:08:54 +0800785 const u_int8_t *lv)
786{
787 u_int8_t in_len = lv[0];
788
789 if (in_len < 2)
790 return -EINVAL;
791
Harald Welte4bfdfe72009-06-10 23:11:52 +0800792 progress->coding = (lv[1] & 0x60) >> 5;
793 progress->location = lv[1] & 0x0f;
794 progress->descr = lv[2] & 0x7f;
Harald Welte0c389302009-06-10 12:08:54 +0800795
796 return 0;
797}
798
799/* encode 'progress' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800800static int encode_progress(struct msgb *msg, int lv_only,
801 const struct gsm_mncc_progress *p)
Harald Welte0c389302009-06-10 12:08:54 +0800802{
803 u_int8_t lv[3];
804
805 lv[0] = 2;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800806 lv[1] = 0x80 | ((p->coding & 0x3) << 5) | (p->location & 0xf);
807 lv[2] = 0x80 | (p->descr & 0x7f);
Harald Welte0c389302009-06-10 12:08:54 +0800808 if (lv_only)
809 msgb_lv_put(msg, lv[0], lv+1);
810 else
811 msgb_tlv_put(msg, GSM48_IE_PROGR_IND, lv[0], lv+1);
812
813 return 0;
814}
815
816/* decode 'user-user' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800817static int decode_useruser(struct gsm_mncc_useruser *uu,
Harald Welte0c389302009-06-10 12:08:54 +0800818 const u_int8_t *lv)
819{
820 u_int8_t in_len = lv[0];
Harald Welte4bfdfe72009-06-10 23:11:52 +0800821 char *info = uu->info;
822 int info_len = sizeof(uu->info);
Harald Welte0c389302009-06-10 12:08:54 +0800823 int i;
824
825 if (in_len < 1)
826 return -EINVAL;
827
Harald Welte4bfdfe72009-06-10 23:11:52 +0800828 uu->proto = lv[1];
Harald Welte0c389302009-06-10 12:08:54 +0800829
830 for (i = 2; i <= in_len; i++) {
831 info_len--;
832 if (info_len <= 1)
833 break;
834 *info++ = lv[i];
835 }
836 if (info_len >= 1)
837 *info++ = '\0';
838
839 return 0;
840}
841
842/* encode 'useruser' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800843static int encode_useruser(struct msgb *msg, int lv_only,
844 const struct gsm_mncc_useruser *uu)
Harald Welte0c389302009-06-10 12:08:54 +0800845{
846 u_int8_t lv[GSM_MAX_USERUSER + 2];
847
Harald Welte4bfdfe72009-06-10 23:11:52 +0800848 if (strlen(uu->info) > GSM_MAX_USERUSER)
Harald Welte0c389302009-06-10 12:08:54 +0800849 return -EINVAL;
850
Harald Welte4bfdfe72009-06-10 23:11:52 +0800851 lv[0] = 1 + strlen(uu->info);
852 lv[1] = uu->proto;
853 memcpy(lv + 2, uu->info, strlen(uu->info));
Harald Welte0c389302009-06-10 12:08:54 +0800854 if (lv_only)
855 msgb_lv_put(msg, lv[0], lv+1);
856 else
857 msgb_tlv_put(msg, GSM48_IE_USER_USER, lv[0], lv+1);
858
859 return 0;
860}
861
862/* decode 'ss version' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800863static int decode_ssversion(struct gsm_mncc_ssversion *ssv,
Harald Welte0c389302009-06-10 12:08:54 +0800864 const u_int8_t *lv)
865{
866 u_int8_t in_len = lv[0];
867
Harald Welte4bfdfe72009-06-10 23:11:52 +0800868 if (in_len < 1 || in_len < sizeof(ssv->info))
Harald Welte0c389302009-06-10 12:08:54 +0800869 return -EINVAL;
870
Harald Welte4bfdfe72009-06-10 23:11:52 +0800871 memcpy(ssv->info, lv + 1, in_len);
872 ssv->len = in_len;
Harald Welte0c389302009-06-10 12:08:54 +0800873
874 return 0;
875}
876
877/* encode 'more data' */
878static int encode_more(struct msgb *msg)
879{
880 u_int8_t *ie;
881
882 ie = msgb_put(msg, 1);
883 ie[0] = GSM48_IE_MORE_DATA;
884
885 return 0;
886}
887
Holger Freyther429e7762008-12-30 13:28:30 +0000888/* Chapter 9.2.14 : Send LOCATION UPDATING REJECT */
Harald Welte8470bf22008-12-25 23:28:35 +0000889int gsm0408_loc_upd_rej(struct gsm_lchan *lchan, u_int8_t cause)
Harald Welte52b1f982008-12-23 20:25:15 +0000890{
Harald Welte8470bf22008-12-25 23:28:35 +0000891 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000892 struct gsm48_hdr *gh;
893
Harald Welte8470bf22008-12-25 23:28:35 +0000894 msg->lchan = lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000895
896 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
897 gh->proto_discr = GSM48_PDISC_MM;
Harald Welte10b487b2008-12-27 19:53:37 +0000898 gh->msg_type = GSM48_MT_MM_LOC_UPD_REJECT;
Harald Welte52b1f982008-12-23 20:25:15 +0000899 gh->data[0] = cause;
900
Harald Weltedb253af2008-12-30 17:56:55 +0000901 DEBUGP(DMM, "-> LOCATION UPDATING REJECT on channel: %d\n", lchan->nr);
902
Harald Welte39e2ead2009-07-23 21:13:03 +0200903 return gsm48_sendmsg(msg, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000904}
905
906/* Chapter 9.2.13 : Send LOCATION UPDATE ACCEPT */
Harald Welte75a983f2008-12-27 21:34:06 +0000907int gsm0408_loc_upd_acc(struct gsm_lchan *lchan, u_int32_t tmsi)
Harald Welte52b1f982008-12-23 20:25:15 +0000908{
Harald Welte8470bf22008-12-25 23:28:35 +0000909 struct gsm_bts *bts = lchan->ts->trx->bts;
910 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000911 struct gsm48_hdr *gh;
912 struct gsm48_loc_area_id *lai;
913 u_int8_t *mid;
914
Harald Welte8470bf22008-12-25 23:28:35 +0000915 msg->lchan = lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000916
917 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
918 gh->proto_discr = GSM48_PDISC_MM;
919 gh->msg_type = GSM48_MT_MM_LOC_UPD_ACCEPT;
920
921 lai = (struct gsm48_loc_area_id *) msgb_put(msg, sizeof(*lai));
Holger Freyther17746612008-12-28 16:32:44 +0000922 gsm0408_generate_lai(lai, bts->network->country_code,
Harald Welte52b1f982008-12-23 20:25:15 +0000923 bts->network->network_code, bts->location_area_code);
924
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +0200925 mid = msgb_put(msg, GSM48_MID_TMSI_LEN);
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +0200926 gsm48_generate_mid_from_tmsi(mid, tmsi);
Harald Welte52b1f982008-12-23 20:25:15 +0000927
928 DEBUGP(DMM, "-> LOCATION UPDATE ACCEPT\n");
929
Harald Welteb83d9382009-12-12 21:00:48 +0100930 return gsm48_sendmsg(msg, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000931}
932
Harald Weltebf5e8df2009-02-03 12:59:45 +0000933/* Transmit Chapter 9.2.10 Identity Request */
Harald Welte231ad4f2008-12-27 11:15:38 +0000934static int mm_tx_identity_req(struct gsm_lchan *lchan, u_int8_t id_type)
935{
936 struct msgb *msg = gsm48_msgb_alloc();
937 struct gsm48_hdr *gh;
Harald Weltefc977a82008-12-27 10:19:37 +0000938
Harald Welte231ad4f2008-12-27 11:15:38 +0000939 msg->lchan = lchan;
940
941 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
942 gh->proto_discr = GSM48_PDISC_MM;
943 gh->msg_type = GSM48_MT_MM_ID_REQ;
944 gh->data[0] = id_type;
945
Harald Welte39e2ead2009-07-23 21:13:03 +0200946 return gsm48_sendmsg(msg, NULL);
Harald Welte231ad4f2008-12-27 11:15:38 +0000947}
948
Harald Welte231ad4f2008-12-27 11:15:38 +0000949
Harald Weltebf5e8df2009-02-03 12:59:45 +0000950/* Parse Chapter 9.2.11 Identity Response */
Harald Welte231ad4f2008-12-27 11:15:38 +0000951static int mm_rx_id_resp(struct msgb *msg)
952{
953 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte75a983f2008-12-27 21:34:06 +0000954 struct gsm_lchan *lchan = msg->lchan;
Harald Welte9176bd42009-07-23 18:46:00 +0200955 struct gsm_bts *bts = lchan->ts->trx->bts;
956 struct gsm_network *net = bts->network;
Harald Welte231ad4f2008-12-27 11:15:38 +0000957 u_int8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200958 char mi_string[GSM48_MI_SIZE];
Harald Welte231ad4f2008-12-27 11:15:38 +0000959
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200960 gsm48_mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
Harald Welte61253062008-12-27 11:25:50 +0000961 DEBUGP(DMM, "IDENTITY RESPONSE: mi_type=0x%02x MI(%s)\n",
Harald Welte231ad4f2008-12-27 11:15:38 +0000962 mi_type, mi_string);
963
Harald Welte7659de12009-12-13 12:39:18 +0100964 dispatch_signal(SS_SUBSCR, S_SUBSCR_IDENTITY, gh->data);
965
Harald Welte75a983f2008-12-27 21:34:06 +0000966 switch (mi_type) {
967 case GSM_MI_TYPE_IMSI:
Jan Luebbe370b41d2009-08-12 10:19:34 +0200968 /* look up subscriber based on IMSI, create if not found */
969 if (!lchan->subscr) {
970 lchan->subscr = subscr_get_by_imsi(net, mi_string);
Harald Welte (local)aa9dc192009-08-13 13:49:51 +0200971 if (!lchan->subscr)
972 lchan->subscr = db_create_subscriber(net, mi_string);
Jan Luebbeb0dfc312009-08-12 10:12:52 +0200973 }
Holger Freyther73487a22008-12-31 18:53:57 +0000974 if (lchan->loc_operation)
975 lchan->loc_operation->waiting_for_imsi = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000976 break;
977 case GSM_MI_TYPE_IMEI:
Harald Welte255539c2008-12-28 02:26:27 +0000978 case GSM_MI_TYPE_IMEISV:
Harald Welte75a983f2008-12-27 21:34:06 +0000979 /* update subscribe <-> IMEI mapping */
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200980 if (lchan->subscr) {
Harald Welte75a983f2008-12-27 21:34:06 +0000981 db_subscriber_assoc_imei(lchan->subscr, mi_string);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200982 db_sync_equipment(&lchan->subscr->equipment);
983 }
Holger Freyther73487a22008-12-31 18:53:57 +0000984 if (lchan->loc_operation)
985 lchan->loc_operation->waiting_for_imei = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000986 break;
987 }
Holger Freyther73487a22008-12-31 18:53:57 +0000988
989 /* Check if we can let the mobile station enter */
Holger Freytherd51524f2009-06-09 08:27:07 +0000990 return gsm0408_authorize(lchan, msg);
Harald Welte231ad4f2008-12-27 11:15:38 +0000991}
992
Harald Welte255539c2008-12-28 02:26:27 +0000993
994static void loc_upd_rej_cb(void *data)
995{
996 struct gsm_lchan *lchan = data;
Harald Welte1085c092009-11-18 20:33:19 +0100997 struct gsm_bts *bts = lchan->ts->trx->bts;
Harald Welte255539c2008-12-28 02:26:27 +0000998
Holger Freyther73487a22008-12-31 18:53:57 +0000999 release_loc_updating_req(lchan);
Harald Welte1085c092009-11-18 20:33:19 +01001000 gsm0408_loc_upd_rej(lchan, bts->network->reject_cause);
Holger Freyther67b4b9a2009-01-01 03:46:11 +00001001 lchan_auto_release(lchan);
Harald Welte255539c2008-12-28 02:26:27 +00001002}
1003
Holger Freytherb7193e42008-12-29 17:44:08 +00001004static void schedule_reject(struct gsm_lchan *lchan)
1005{
Holger Freyther73487a22008-12-31 18:53:57 +00001006 lchan->loc_operation->updating_timer.cb = loc_upd_rej_cb;
1007 lchan->loc_operation->updating_timer.data = lchan;
Harald Welteff117a82009-05-23 05:22:08 +00001008 bsc_schedule_timer(&lchan->loc_operation->updating_timer, 5, 0);
Holger Freytherb7193e42008-12-29 17:44:08 +00001009}
1010
Harald Welte2a139372009-02-22 21:14:55 +00001011static const char *lupd_name(u_int8_t type)
1012{
1013 switch (type) {
1014 case GSM48_LUPD_NORMAL:
1015 return "NORMAL";
1016 case GSM48_LUPD_PERIODIC:
1017 return "PEROIDOC";
1018 case GSM48_LUPD_IMSI_ATT:
1019 return "IMSI ATTACH";
1020 default:
1021 return "UNKNOWN";
1022 }
1023}
1024
Harald Weltebf5e8df2009-02-03 12:59:45 +00001025/* Chapter 9.2.15: Receive Location Updating Request */
Harald Welte231ad4f2008-12-27 11:15:38 +00001026static int mm_rx_loc_upd_req(struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001027{
Harald Welte8470bf22008-12-25 23:28:35 +00001028 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001029 struct gsm48_loc_upd_req *lu;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001030 struct gsm_subscriber *subscr = NULL;
Harald Welte255539c2008-12-28 02:26:27 +00001031 struct gsm_lchan *lchan = msg->lchan;
Harald Welte9176bd42009-07-23 18:46:00 +02001032 struct gsm_bts *bts = lchan->ts->trx->bts;
Harald Welte8470bf22008-12-25 23:28:35 +00001033 u_int8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001034 char mi_string[GSM48_MI_SIZE];
Harald Welte231ad4f2008-12-27 11:15:38 +00001035 int rc;
Harald Welte52b1f982008-12-23 20:25:15 +00001036
Harald Welte8470bf22008-12-25 23:28:35 +00001037 lu = (struct gsm48_loc_upd_req *) gh->data;
1038
1039 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
Harald Welte52b1f982008-12-23 20:25:15 +00001040
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001041 gsm48_mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
Harald Weltefc977a82008-12-27 10:19:37 +00001042
Harald Weltea0368542009-06-27 02:58:43 +02001043 DEBUGPC(DMM, "mi_type=0x%02x MI(%s) type=%s ", mi_type, mi_string,
Harald Welte2a139372009-02-22 21:14:55 +00001044 lupd_name(lu->type));
Holger Freyther73487a22008-12-31 18:53:57 +00001045
Harald Welte7659de12009-12-13 12:39:18 +01001046 dispatch_signal(SS_SUBSCR, S_SUBSCR_IDENTITY, &lu->mi_len);
1047
Holger Freythereaf04692009-06-06 13:54:44 +00001048 /*
1049 * Pseudo Spoof detection: Just drop a second/concurrent
1050 * location updating request.
1051 */
1052 if (lchan->loc_operation) {
Harald Weltea0368542009-06-27 02:58:43 +02001053 DEBUGPC(DMM, "ignoring request due an existing one: %p.\n",
Holger Freythereaf04692009-06-06 13:54:44 +00001054 lchan->loc_operation);
1055 gsm0408_loc_upd_rej(lchan, GSM48_REJECT_PROTOCOL_ERROR);
1056 return 0;
1057 }
1058
Holger Freyther73487a22008-12-31 18:53:57 +00001059 allocate_loc_updating_req(lchan);
1060
Harald Welte52b1f982008-12-23 20:25:15 +00001061 switch (mi_type) {
1062 case GSM_MI_TYPE_IMSI:
Harald Weltea0368542009-06-27 02:58:43 +02001063 DEBUGPC(DMM, "\n");
Harald Welte231ad4f2008-12-27 11:15:38 +00001064 /* we always want the IMEI, too */
Harald Welte015b9ad2009-02-28 18:22:03 +00001065 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMEI);
Holger Freyther73487a22008-12-31 18:53:57 +00001066 lchan->loc_operation->waiting_for_imei = 1;
Holger Freytherc6ea9db2008-12-30 19:18:21 +00001067
Jan Luebbe370b41d2009-08-12 10:19:34 +02001068 /* look up subscriber based on IMSI, create if not found */
1069 subscr = subscr_get_by_imsi(bts->network, mi_string);
1070 if (!subscr) {
1071 subscr = db_create_subscriber(bts->network, mi_string);
1072 }
Harald Welte4b634542008-12-27 01:55:51 +00001073 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001074 case GSM_MI_TYPE_TMSI:
Harald Weltea0368542009-06-27 02:58:43 +02001075 DEBUGPC(DMM, "\n");
Harald Welte231ad4f2008-12-27 11:15:38 +00001076 /* we always want the IMEI, too */
Harald Welte015b9ad2009-02-28 18:22:03 +00001077 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMEI);
Holger Freyther73487a22008-12-31 18:53:57 +00001078 lchan->loc_operation->waiting_for_imei = 1;
Holger Freytherc6ea9db2008-12-30 19:18:21 +00001079
Harald Welte52b1f982008-12-23 20:25:15 +00001080 /* look up the subscriber based on TMSI, request IMSI if it fails */
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +02001081 subscr = subscr_get_by_tmsi(bts->network,
1082 tmsi_from_string(mi_string));
Harald Welte52b1f982008-12-23 20:25:15 +00001083 if (!subscr) {
Harald Welte231ad4f2008-12-27 11:15:38 +00001084 /* send IDENTITY REQUEST message to get IMSI */
Harald Welte255539c2008-12-28 02:26:27 +00001085 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMSI);
Holger Freyther73487a22008-12-31 18:53:57 +00001086 lchan->loc_operation->waiting_for_imsi = 1;
Harald Welte52b1f982008-12-23 20:25:15 +00001087 }
1088 break;
1089 case GSM_MI_TYPE_IMEI:
1090 case GSM_MI_TYPE_IMEISV:
1091 /* no sim card... FIXME: what to do ? */
Harald Weltea0368542009-06-27 02:58:43 +02001092 DEBUGPC(DMM, "unimplemented mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +00001093 break;
1094 default:
Harald Weltea0368542009-06-27 02:58:43 +02001095 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +00001096 break;
1097 }
1098
Harald Welte24516ea2009-07-04 10:18:00 +02001099 /* schedule the reject timer */
1100 schedule_reject(lchan);
1101
Harald Welte4bfdfe72009-06-10 23:11:52 +08001102 if (!subscr) {
Harald Weltea0368542009-06-27 02:58:43 +02001103 DEBUGPC(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001104 /* FIXME: request id? close channel? */
1105 return -EINVAL;
1106 }
1107
Harald Welte255539c2008-12-28 02:26:27 +00001108 lchan->subscr = subscr;
Harald Welte (local)ee4410a2009-08-17 09:39:55 +02001109 lchan->subscr->equipment.classmark1 = lu->classmark1;
Harald Welte255539c2008-12-28 02:26:27 +00001110
Harald Welte24516ea2009-07-04 10:18:00 +02001111 /* check if we can let the subscriber into our network immediately
1112 * or if we need to wait for identity responses. */
Holger Freytherd51524f2009-06-09 08:27:07 +00001113 return gsm0408_authorize(lchan, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001114}
1115
Harald Welte4bfdfe72009-06-10 23:11:52 +08001116#if 0
1117static u_int8_t to_bcd8(u_int8_t val)
1118{
1119 return ((val / 10) << 4) | (val % 10);
1120}
1121#endif
1122
Harald Weltedb253af2008-12-30 17:56:55 +00001123/* Section 9.2.15a */
1124int gsm48_tx_mm_info(struct gsm_lchan *lchan)
1125{
1126 struct msgb *msg = gsm48_msgb_alloc();
1127 struct gsm48_hdr *gh;
1128 struct gsm_network *net = lchan->ts->trx->bts->network;
Harald Weltedb253af2008-12-30 17:56:55 +00001129 u_int8_t *ptr8;
Daniel Willmanneea93372009-08-13 03:42:07 +02001130 int name_len, name_pad;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001131#if 0
1132 time_t cur_t;
1133 struct tm* cur_time;
1134 int tz15min;
1135#endif
Harald Weltedb253af2008-12-30 17:56:55 +00001136
1137 msg->lchan = lchan;
1138
1139 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1140 gh->proto_discr = GSM48_PDISC_MM;
1141 gh->msg_type = GSM48_MT_MM_INFO;
1142
1143 if (net->name_long) {
Daniel Willmanneea93372009-08-13 03:42:07 +02001144#if 0
Harald Weltedb253af2008-12-30 17:56:55 +00001145 name_len = strlen(net->name_long);
1146 /* 10.5.3.5a */
1147 ptr8 = msgb_put(msg, 3);
1148 ptr8[0] = GSM48_IE_NAME_LONG;
1149 ptr8[1] = name_len*2 +1;
1150 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
1151
1152 ptr16 = (u_int16_t *) msgb_put(msg, name_len*2);
1153 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +00001154 ptr16[i] = htons(net->name_long[i]);
Harald Weltedb253af2008-12-30 17:56:55 +00001155
1156 /* FIXME: Use Cell Broadcast, not UCS-2, since
1157 * UCS-2 is only supported by later revisions of the spec */
Daniel Willmanneea93372009-08-13 03:42:07 +02001158#endif
1159 name_len = (strlen(net->name_long)*7)/8;
1160 name_pad = (8 - strlen(net->name_long)*7)%8;
1161 if (name_pad > 0)
1162 name_len++;
1163 /* 10.5.3.5a */
1164 ptr8 = msgb_put(msg, 3);
1165 ptr8[0] = GSM48_IE_NAME_LONG;
1166 ptr8[1] = name_len +1;
1167 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
1168
1169 ptr8 = msgb_put(msg, name_len);
1170 gsm_7bit_encode(ptr8, net->name_long);
1171
Harald Weltedb253af2008-12-30 17:56:55 +00001172 }
1173
1174 if (net->name_short) {
Daniel Willmanneea93372009-08-13 03:42:07 +02001175#if 0
Harald Weltedb253af2008-12-30 17:56:55 +00001176 name_len = strlen(net->name_short);
1177 /* 10.5.3.5a */
1178 ptr8 = (u_int8_t *) msgb_put(msg, 3);
Harald Welte7543eb72009-07-19 17:51:36 +02001179 ptr8[0] = GSM48_IE_NAME_SHORT;
Harald Weltedb253af2008-12-30 17:56:55 +00001180 ptr8[1] = name_len*2 + 1;
1181 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
1182
Harald Weltee872cb12009-01-01 00:33:37 +00001183 ptr16 = (u_int16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +00001184 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +00001185 ptr16[i] = htons(net->name_short[i]);
Daniel Willmanneea93372009-08-13 03:42:07 +02001186#endif
1187 name_len = (strlen(net->name_short)*7)/8;
1188 name_pad = (8 - strlen(net->name_short)*7)%8;
1189 if (name_pad > 0)
1190 name_len++;
1191 /* 10.5.3.5a */
1192 ptr8 = (u_int8_t *) msgb_put(msg, 3);
1193 ptr8[0] = GSM48_IE_NAME_SHORT;
1194 ptr8[1] = name_len +1;
1195 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
1196
1197 ptr8 = msgb_put(msg, name_len);
1198 gsm_7bit_encode(ptr8, net->name_short);
1199
Harald Weltedb253af2008-12-30 17:56:55 +00001200 }
1201
1202#if 0
1203 /* Section 10.5.3.9 */
1204 cur_t = time(NULL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001205 cur_time = gmtime(&cur_t);
Harald Weltedb253af2008-12-30 17:56:55 +00001206 ptr8 = msgb_put(msg, 8);
1207 ptr8[0] = GSM48_IE_NET_TIME_TZ;
1208 ptr8[1] = to_bcd8(cur_time->tm_year % 100);
1209 ptr8[2] = to_bcd8(cur_time->tm_mon);
1210 ptr8[3] = to_bcd8(cur_time->tm_mday);
1211 ptr8[4] = to_bcd8(cur_time->tm_hour);
1212 ptr8[5] = to_bcd8(cur_time->tm_min);
1213 ptr8[6] = to_bcd8(cur_time->tm_sec);
1214 /* 02.42: coded as BCD encoded signed value in units of 15 minutes */
1215 tz15min = (cur_time->tm_gmtoff)/(60*15);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001216 ptr8[7] = to_bcd8(tz15min);
Harald Weltedb253af2008-12-30 17:56:55 +00001217 if (tz15min < 0)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001218 ptr8[7] |= 0x80;
Harald Weltedb253af2008-12-30 17:56:55 +00001219#endif
1220
Daniel Willmanneea93372009-08-13 03:42:07 +02001221 DEBUGP(DMM, "-> MM INFO\n");
1222
Harald Welte39e2ead2009-07-23 21:13:03 +02001223 return gsm48_sendmsg(msg, NULL);
Harald Weltedb253af2008-12-30 17:56:55 +00001224}
1225
Harald Welte7984d5c2009-08-12 22:56:50 +02001226/* Section 9.2.2 */
1227int gsm48_tx_mm_auth_req(struct gsm_lchan *lchan, u_int8_t *rand)
1228{
1229 struct msgb *msg = gsm48_msgb_alloc();
1230 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Sylvain Munaut849f5542009-09-27 11:10:17 +02001231 struct gsm48_auth_req *ar = (struct gsm48_auth_req *) msgb_put(msg, sizeof(*ar));
Harald Welte7984d5c2009-08-12 22:56:50 +02001232
1233 DEBUGP(DMM, "-> AUTH REQ\n");
1234
1235 msg->lchan = lchan;
1236 gh->proto_discr = GSM48_PDISC_MM;
1237 gh->msg_type = GSM48_MT_MM_AUTH_REQ;
1238
Sylvain Munaut849f5542009-09-27 11:10:17 +02001239 /* Key Sequence: FIXME fixed to 0 */
1240 ar->key_seq = 0;
1241
Harald Welte7984d5c2009-08-12 22:56:50 +02001242 /* 16 bytes RAND parameters */
Harald Welte7984d5c2009-08-12 22:56:50 +02001243 if (rand)
Sylvain Munaut849f5542009-09-27 11:10:17 +02001244 memcpy(ar->rand, rand, 16);
Harald Welte7984d5c2009-08-12 22:56:50 +02001245
1246 return gsm48_sendmsg(msg, NULL);
1247}
1248
1249/* Section 9.2.1 */
1250int gsm48_tx_mm_auth_rej(struct gsm_lchan *lchan)
1251{
1252 DEBUGP(DMM, "-> AUTH REJECT\n");
1253 return gsm48_tx_simple(lchan, GSM48_PDISC_MM, GSM48_MT_MM_AUTH_REJ);
1254}
1255
Harald Welte4b634542008-12-27 01:55:51 +00001256static int gsm48_tx_mm_serv_ack(struct gsm_lchan *lchan)
1257{
Harald Welte4b634542008-12-27 01:55:51 +00001258 DEBUGP(DMM, "-> CM SERVICE ACK\n");
Harald Welte65e74cc2008-12-29 01:55:35 +00001259 return gsm48_tx_simple(lchan, GSM48_PDISC_MM, GSM48_MT_MM_CM_SERV_ACC);
Harald Welte4b634542008-12-27 01:55:51 +00001260}
Harald Welteba4cf162009-01-10 01:49:35 +00001261
1262/* 9.2.6 CM service reject */
1263static int gsm48_tx_mm_serv_rej(struct gsm_lchan *lchan,
1264 enum gsm48_reject_value value)
1265{
1266 struct msgb *msg = gsm48_msgb_alloc();
1267 struct gsm48_hdr *gh;
1268
1269 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
1270
1271 msg->lchan = lchan;
1272 use_lchan(lchan);
1273
1274 gh->proto_discr = GSM48_PDISC_MM;
1275 gh->msg_type = GSM48_MT_MM_CM_SERV_REJ;
1276 gh->data[0] = value;
1277 DEBUGP(DMM, "-> CM SERVICE Reject cause: %d\n", value);
1278
Harald Welte39e2ead2009-07-23 21:13:03 +02001279 return gsm48_sendmsg(msg, NULL);
Harald Welteba4cf162009-01-10 01:49:35 +00001280}
1281
Harald Welte4ed0e922009-01-10 03:17:30 +00001282/*
1283 * Handle CM Service Requests
1284 * a) Verify that the packet is long enough to contain the information
1285 * we require otherwsie reject with INCORRECT_MESSAGE
1286 * b) Try to parse the TMSI. If we do not have one reject
1287 * c) Check that we know the subscriber with the TMSI otherwise reject
1288 * with a HLR cause
1289 * d) Set the subscriber on the gsm_lchan and accept
1290 */
Harald Welte4b634542008-12-27 01:55:51 +00001291static int gsm48_rx_mm_serv_req(struct msgb *msg)
1292{
Harald Welteba4cf162009-01-10 01:49:35 +00001293 u_int8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001294 char mi_string[GSM48_MI_SIZE];
Harald Welte4b634542008-12-27 01:55:51 +00001295
Harald Welte9176bd42009-07-23 18:46:00 +02001296 struct gsm_bts *bts = msg->lchan->ts->trx->bts;
Harald Welteba4cf162009-01-10 01:49:35 +00001297 struct gsm_subscriber *subscr;
1298 struct gsm48_hdr *gh = msgb_l3(msg);
1299 struct gsm48_service_request *req =
1300 (struct gsm48_service_request *)gh->data;
Harald Weltec9e02182009-05-01 19:07:53 +00001301 /* unfortunately in Phase1 the classmar2 length is variable */
1302 u_int8_t classmark2_len = gh->data[1];
1303 u_int8_t *classmark2 = gh->data+2;
1304 u_int8_t mi_len = *(classmark2 + classmark2_len);
1305 u_int8_t *mi = (classmark2 + classmark2_len + 1);
Harald Welteba4cf162009-01-10 01:49:35 +00001306
Harald Weltec9e02182009-05-01 19:07:53 +00001307 DEBUGP(DMM, "<- CM SERVICE REQUEST ");
Harald Welteba4cf162009-01-10 01:49:35 +00001308 if (msg->data_len < sizeof(struct gsm48_service_request*)) {
Harald Weltec9e02182009-05-01 19:07:53 +00001309 DEBUGPC(DMM, "wrong sized message\n");
Harald Welteba4cf162009-01-10 01:49:35 +00001310 return gsm48_tx_mm_serv_rej(msg->lchan,
1311 GSM48_REJECT_INCORRECT_MESSAGE);
1312 }
1313
1314 if (msg->data_len < req->mi_len + 6) {
Harald Weltec9e02182009-05-01 19:07:53 +00001315 DEBUGPC(DMM, "does not fit in packet\n");
Harald Welteba4cf162009-01-10 01:49:35 +00001316 return gsm48_tx_mm_serv_rej(msg->lchan,
1317 GSM48_REJECT_INCORRECT_MESSAGE);
1318 }
1319
Harald Weltec9e02182009-05-01 19:07:53 +00001320 mi_type = mi[0] & GSM_MI_TYPE_MASK;
Harald Welteba4cf162009-01-10 01:49:35 +00001321 if (mi_type != GSM_MI_TYPE_TMSI) {
Harald Weltec9e02182009-05-01 19:07:53 +00001322 DEBUGPC(DMM, "mi_type is not TMSI: %d\n", mi_type);
Harald Welteba4cf162009-01-10 01:49:35 +00001323 return gsm48_tx_mm_serv_rej(msg->lchan,
1324 GSM48_REJECT_INCORRECT_MESSAGE);
1325 }
1326
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001327 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
Harald Weltec9e02182009-05-01 19:07:53 +00001328 DEBUGPC(DMM, "serv_type=0x%02x mi_type=0x%02x M(%s)\n",
Harald Welte4ed0e922009-01-10 03:17:30 +00001329 req->cm_service_type, mi_type, mi_string);
Harald Weltebcae43f2008-12-27 21:45:37 +00001330
Harald Welte7659de12009-12-13 12:39:18 +01001331 dispatch_signal(SS_SUBSCR, S_SUBSCR_IDENTITY, (classmark2 + classmark2_len));
1332
Harald Welte3ac7f102009-08-10 10:12:45 +02001333 if (is_siemens_bts(bts))
1334 send_siemens_mrpci(msg->lchan, classmark2-1);
1335
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +02001336 subscr = subscr_get_by_tmsi(bts->network,
1337 tmsi_from_string(mi_string));
Holger Freythereb443982009-06-04 13:58:42 +00001338
Harald Welte2a139372009-02-22 21:14:55 +00001339 /* FIXME: if we don't know the TMSI, inquire abit IMSI and allocate new TMSI */
Harald Welte4ed0e922009-01-10 03:17:30 +00001340 if (!subscr)
1341 return gsm48_tx_mm_serv_rej(msg->lchan,
1342 GSM48_REJECT_IMSI_UNKNOWN_IN_HLR);
1343
1344 if (!msg->lchan->subscr)
1345 msg->lchan->subscr = subscr;
Sylvain Munautea3f6742009-12-18 18:28:10 +01001346 else if (msg->lchan->subscr == subscr)
1347 subscr_put(subscr); /* lchan already has a ref, don't need another one */
1348 else {
Harald Welte9bb7c702009-01-10 03:21:41 +00001349 DEBUGP(DMM, "<- CM Channel already owned by someone else?\n");
1350 subscr_put(subscr);
1351 }
1352
Harald Weltec2e302d2009-07-05 14:08:13 +02001353 subscr->equipment.classmark2_len = classmark2_len;
1354 memcpy(subscr->equipment.classmark2, classmark2, classmark2_len);
1355 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +00001356
Harald Welte4b634542008-12-27 01:55:51 +00001357 return gsm48_tx_mm_serv_ack(msg->lchan);
1358}
1359
Harald Welte2a139372009-02-22 21:14:55 +00001360static int gsm48_rx_mm_imsi_detach_ind(struct msgb *msg)
1361{
Harald Welte9176bd42009-07-23 18:46:00 +02001362 struct gsm_bts *bts = msg->lchan->ts->trx->bts;
Harald Welte2a139372009-02-22 21:14:55 +00001363 struct gsm48_hdr *gh = msgb_l3(msg);
1364 struct gsm48_imsi_detach_ind *idi =
1365 (struct gsm48_imsi_detach_ind *) gh->data;
1366 u_int8_t mi_type = idi->mi[0] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001367 char mi_string[GSM48_MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +08001368 struct gsm_subscriber *subscr = NULL;
Harald Welte2a139372009-02-22 21:14:55 +00001369
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001370 gsm48_mi_to_string(mi_string, sizeof(mi_string), idi->mi, idi->mi_len);
Harald Welte2a139372009-02-22 21:14:55 +00001371 DEBUGP(DMM, "IMSI DETACH INDICATION: mi_type=0x%02x MI(%s): ",
1372 mi_type, mi_string);
1373
1374 switch (mi_type) {
1375 case GSM_MI_TYPE_TMSI:
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +02001376 subscr = subscr_get_by_tmsi(bts->network,
1377 tmsi_from_string(mi_string));
Harald Welte2a139372009-02-22 21:14:55 +00001378 break;
1379 case GSM_MI_TYPE_IMSI:
Harald Welte9176bd42009-07-23 18:46:00 +02001380 subscr = subscr_get_by_imsi(bts->network, mi_string);
Harald Welte2a139372009-02-22 21:14:55 +00001381 break;
1382 case GSM_MI_TYPE_IMEI:
1383 case GSM_MI_TYPE_IMEISV:
1384 /* no sim card... FIXME: what to do ? */
Holger Freyther79f4ae62009-06-02 03:25:04 +00001385 DEBUGPC(DMM, "unimplemented mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +00001386 break;
1387 default:
Holger Freyther79f4ae62009-06-02 03:25:04 +00001388 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +00001389 break;
1390 }
1391
Holger Freyther4a49e772009-04-12 05:37:29 +00001392 if (subscr) {
1393 subscr_update(subscr, msg->trx->bts,
1394 GSM_SUBSCRIBER_UPDATE_DETACHED);
Harald Welte2a139372009-02-22 21:14:55 +00001395 DEBUGP(DMM, "Subscriber: %s\n",
1396 subscr->name ? subscr->name : subscr->imsi);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +02001397
1398 subscr->equipment.classmark1 = idi->classmark1;
1399 db_sync_equipment(&subscr->equipment);
1400
Holger Freytherc21cfbc2009-06-02 02:54:57 +00001401 subscr_put(subscr);
Holger Freyther4a49e772009-04-12 05:37:29 +00001402 } else
Harald Welte2a139372009-02-22 21:14:55 +00001403 DEBUGP(DMM, "Unknown Subscriber ?!?\n");
1404
Harald Welte31981a02009-12-20 09:58:40 +01001405 /* FIXME: iterate over all transactions and release them,
1406 * imagine an IMSI DETACH happening during an active call! */
1407
Harald Welteb83d9382009-12-12 21:00:48 +01001408 /* subscriber is detached: should we release lchan? */
1409
Harald Welte2a139372009-02-22 21:14:55 +00001410 return 0;
1411}
1412
Harald Welted2a7f5a2009-06-05 20:08:20 +00001413static int gsm48_rx_mm_status(struct msgb *msg)
1414{
1415 struct gsm48_hdr *gh = msgb_l3(msg);
1416
1417 DEBUGP(DMM, "MM STATUS (reject cause 0x%02x)\n", gh->data[0]);
1418
1419 return 0;
1420}
1421
Harald Weltebf5e8df2009-02-03 12:59:45 +00001422/* Receive a GSM 04.08 Mobility Management (MM) message */
Harald Welte52b1f982008-12-23 20:25:15 +00001423static int gsm0408_rcv_mm(struct msgb *msg)
1424{
1425 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001426 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001427
1428 switch (gh->msg_type & 0xbf) {
1429 case GSM48_MT_MM_LOC_UPD_REQUEST:
Harald Weltea0368542009-06-27 02:58:43 +02001430 DEBUGP(DMM, "LOCATION UPDATING REQUEST: ");
Harald Welte231ad4f2008-12-27 11:15:38 +00001431 rc = mm_rx_loc_upd_req(msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001432 break;
1433 case GSM48_MT_MM_ID_RESP:
Harald Welte231ad4f2008-12-27 11:15:38 +00001434 rc = mm_rx_id_resp(msg);
1435 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001436 case GSM48_MT_MM_CM_SERV_REQ:
Harald Welte4b634542008-12-27 01:55:51 +00001437 rc = gsm48_rx_mm_serv_req(msg);
1438 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001439 case GSM48_MT_MM_STATUS:
Harald Welted2a7f5a2009-06-05 20:08:20 +00001440 rc = gsm48_rx_mm_status(msg);
Harald Welte231ad4f2008-12-27 11:15:38 +00001441 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001442 case GSM48_MT_MM_TMSI_REALL_COMPL:
Harald Welte69b2af22009-01-06 19:47:00 +00001443 DEBUGP(DMM, "TMSI Reallocation Completed. Subscriber: %s\n",
1444 msg->lchan->subscr ?
1445 msg->lchan->subscr->imsi :
1446 "unknown subscriber");
1447 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001448 case GSM48_MT_MM_IMSI_DETACH_IND:
Harald Welte2a139372009-02-22 21:14:55 +00001449 rc = gsm48_rx_mm_imsi_detach_ind(msg);
1450 break;
1451 case GSM48_MT_MM_CM_REEST_REQ:
1452 DEBUGP(DMM, "CM REESTABLISH REQUEST: Not implemented\n");
1453 break;
1454 case GSM48_MT_MM_AUTH_RESP:
1455 DEBUGP(DMM, "AUTHENTICATION RESPONSE: Not implemented\n");
Harald Welte52b1f982008-12-23 20:25:15 +00001456 break;
1457 default:
1458 fprintf(stderr, "Unknown GSM 04.08 MM msg type 0x%02x\n",
1459 gh->msg_type);
1460 break;
1461 }
1462
1463 return rc;
1464}
Harald Weltebf5e8df2009-02-03 12:59:45 +00001465
Harald Welte2d35ae62009-02-06 12:02:13 +00001466/* Receive a PAGING RESPONSE message from the MS */
Holger Hans Peter Freytherca0fcbe2009-10-23 13:48:54 +02001467static int gsm48_rx_rr_pag_resp(struct msgb *msg)
Harald Welte2d35ae62009-02-06 12:02:13 +00001468{
Harald Welte9176bd42009-07-23 18:46:00 +02001469 struct gsm_bts *bts = msg->lchan->ts->trx->bts;
Harald Welte2d35ae62009-02-06 12:02:13 +00001470 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte61548982009-02-22 21:26:29 +00001471 u_int8_t *classmark2_lv = gh->data + 1;
Holger Hans Peter Freyther3ee5d3e2009-08-21 05:18:21 +02001472 u_int8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001473 char mi_string[GSM48_MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +08001474 struct gsm_subscriber *subscr = NULL;
Harald Welte2d35ae62009-02-06 12:02:13 +00001475 int rc = 0;
1476
Holger Hans Peter Freyther3ee5d3e2009-08-21 05:18:21 +02001477 gsm48_paging_extract_mi(msg, mi_string, &mi_type);
Harald Welte2d35ae62009-02-06 12:02:13 +00001478 DEBUGP(DRR, "PAGING RESPONSE: mi_type=0x%02x MI(%s)\n",
1479 mi_type, mi_string);
Harald Welte3ac7f102009-08-10 10:12:45 +02001480
Harald Weltefe18d8f2009-02-22 21:14:24 +00001481 switch (mi_type) {
1482 case GSM_MI_TYPE_TMSI:
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +02001483 subscr = subscr_get_by_tmsi(bts->network,
1484 tmsi_from_string(mi_string));
Harald Weltefe18d8f2009-02-22 21:14:24 +00001485 break;
1486 case GSM_MI_TYPE_IMSI:
Harald Welte9176bd42009-07-23 18:46:00 +02001487 subscr = subscr_get_by_imsi(bts->network, mi_string);
Harald Weltefe18d8f2009-02-22 21:14:24 +00001488 break;
1489 }
Harald Welte2d35ae62009-02-06 12:02:13 +00001490
1491 if (!subscr) {
1492 DEBUGP(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte09e38af2009-02-16 22:52:23 +00001493 /* FIXME: request id? close channel? */
Harald Welte2d35ae62009-02-06 12:02:13 +00001494 return -EINVAL;
1495 }
1496 DEBUGP(DRR, "<- Channel was requested by %s\n",
Harald Welte76042182009-08-08 16:03:15 +02001497 subscr->name && strlen(subscr->name) ? subscr->name : subscr->imsi);
Holger Freyther053e09d2009-02-14 22:51:06 +00001498
Harald Weltec2e302d2009-07-05 14:08:13 +02001499 subscr->equipment.classmark2_len = *classmark2_lv;
1500 memcpy(subscr->equipment.classmark2, classmark2_lv+1, *classmark2_lv);
1501 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +00001502
Holger Hans Peter Freyther3ee5d3e2009-08-21 05:18:21 +02001503 rc = gsm48_handle_paging_resp(msg, subscr);
Harald Welte2d35ae62009-02-06 12:02:13 +00001504 return rc;
1505}
1506
Harald Weltef7c43522009-06-09 20:24:21 +00001507static int gsm48_rx_rr_classmark(struct msgb *msg)
1508{
1509 struct gsm48_hdr *gh = msgb_l3(msg);
1510 struct gsm_subscriber *subscr = msg->lchan->subscr;
1511 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1512 u_int8_t cm2_len, cm3_len = 0;
1513 u_int8_t *cm2, *cm3 = NULL;
1514
1515 DEBUGP(DRR, "CLASSMARK CHANGE ");
1516
1517 /* classmark 2 */
1518 cm2_len = gh->data[0];
1519 cm2 = &gh->data[1];
1520 DEBUGPC(DRR, "CM2(len=%u) ", cm2_len);
1521
1522 if (payload_len > cm2_len + 1) {
1523 /* we must have a classmark3 */
1524 if (gh->data[cm2_len+1] != 0x20) {
1525 DEBUGPC(DRR, "ERR CM3 TAG\n");
1526 return -EINVAL;
1527 }
1528 if (cm2_len > 3) {
1529 DEBUGPC(DRR, "CM2 too long!\n");
1530 return -EINVAL;
1531 }
1532
1533 cm3_len = gh->data[cm2_len+2];
1534 cm3 = &gh->data[cm2_len+3];
1535 if (cm3_len > 14) {
1536 DEBUGPC(DRR, "CM3 len %u too long!\n", cm3_len);
1537 return -EINVAL;
1538 }
1539 DEBUGPC(DRR, "CM3(len=%u)\n", cm3_len);
1540 }
1541 if (subscr) {
Harald Weltec2e302d2009-07-05 14:08:13 +02001542 subscr->equipment.classmark2_len = cm2_len;
1543 memcpy(subscr->equipment.classmark2, cm2, cm2_len);
Harald Weltef7c43522009-06-09 20:24:21 +00001544 if (cm3) {
Harald Weltec2e302d2009-07-05 14:08:13 +02001545 subscr->equipment.classmark3_len = cm3_len;
1546 memcpy(subscr->equipment.classmark3, cm3, cm3_len);
Harald Weltef7c43522009-06-09 20:24:21 +00001547 }
Harald Weltec2e302d2009-07-05 14:08:13 +02001548 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +00001549 }
1550
Harald Weltef7c43522009-06-09 20:24:21 +00001551 return 0;
1552}
1553
Harald Weltecf5b3592009-05-01 18:28:42 +00001554static int gsm48_rx_rr_status(struct msgb *msg)
1555{
1556 struct gsm48_hdr *gh = msgb_l3(msg);
1557
1558 DEBUGP(DRR, "STATUS rr_cause = %s\n",
1559 rr_cause_name(gh->data[0]));
1560
1561 return 0;
1562}
1563
Harald Weltef7c43522009-06-09 20:24:21 +00001564static int gsm48_rx_rr_meas_rep(struct msgb *msg)
1565{
Harald Welted12b0fd2009-12-15 21:36:05 +01001566 struct gsm_meas_rep *meas_rep = lchan_next_meas_rep(msg->lchan);
Harald Weltef7c43522009-06-09 20:24:21 +00001567
Harald Welte3c7dc6e2009-11-29 19:07:28 +01001568 /* This shouldn't actually end up here, as RSL treats
1569 * L3 Info of 08.58 MEASUREMENT REPORT different by calling
1570 * directly into gsm48_parse_meas_rep */
1571 DEBUGP(DMEAS, "DIRECT GSM48 MEASUREMENT REPORT ?!? ");
Harald Welted12b0fd2009-12-15 21:36:05 +01001572 gsm48_parse_meas_rep(meas_rep, msg);
Harald Weltef7c43522009-06-09 20:24:21 +00001573
1574 return 0;
1575}
1576
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001577static int gsm48_rx_rr_app_info(struct msgb *msg)
1578{
1579 struct gsm48_hdr *gh = msgb_l3(msg);
1580 u_int8_t apdu_id_flags;
1581 u_int8_t apdu_len;
1582 u_int8_t *apdu_data;
1583
1584 apdu_id_flags = gh->data[0];
1585 apdu_len = gh->data[1];
1586 apdu_data = gh->data+2;
1587
1588 DEBUGP(DNM, "RX APPLICATION INFO id/flags=0x%02x apdu_len=%u apdu=%s",
1589 apdu_id_flags, apdu_len, hexdump(apdu_data, apdu_len));
1590
Harald Welte (local)026531e2009-08-16 10:40:10 +02001591 return db_apdu_blob_store(msg->lchan->subscr, apdu_id_flags, apdu_len, apdu_data);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001592}
1593
Harald Welted011e8b2009-11-29 22:45:52 +01001594/* Chapter 9.1.16 Handover complete */
Harald Welte1e191c52009-12-14 17:51:15 +01001595static int gsm48_rx_rr_ho_compl(struct msgb *msg)
Harald Welted011e8b2009-11-29 22:45:52 +01001596{
1597 struct gsm48_hdr *gh = msgb_l3(msg);
1598
1599 DEBUGP(DRR, "HANDOVER COMPLETE cause = %s\n",
1600 rr_cause_name(gh->data[0]));
1601
1602 dispatch_signal(SS_LCHAN, S_LCHAN_HANDOVER_COMPL, msg->lchan);
1603 /* FIXME: release old channel */
1604
1605 return 0;
1606}
1607
1608/* Chapter 9.1.17 Handover Failure */
Harald Welte1e191c52009-12-14 17:51:15 +01001609static int gsm48_rx_rr_ho_fail(struct msgb *msg)
Harald Welted011e8b2009-11-29 22:45:52 +01001610{
1611 struct gsm48_hdr *gh = msgb_l3(msg);
1612
1613 DEBUGP(DRR, "HANDOVER FAILED cause = %s\n",
1614 rr_cause_name(gh->data[0]));
1615
1616 dispatch_signal(SS_LCHAN, S_LCHAN_HANDOVER_FAIL, msg->lchan);
1617 /* FIXME: release allocated new channel */
1618
1619 return 0;
1620}
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001621
Harald Weltebf5e8df2009-02-03 12:59:45 +00001622/* Receive a GSM 04.08 Radio Resource (RR) message */
Harald Welte52b1f982008-12-23 20:25:15 +00001623static int gsm0408_rcv_rr(struct msgb *msg)
1624{
1625 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001626 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001627
1628 switch (gh->msg_type) {
1629 case GSM48_MT_RR_CLSM_CHG:
Harald Weltef7c43522009-06-09 20:24:21 +00001630 rc = gsm48_rx_rr_classmark(msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001631 break;
Harald Weltefc977a82008-12-27 10:19:37 +00001632 case GSM48_MT_RR_GPRS_SUSP_REQ:
1633 DEBUGP(DRR, "GRPS SUSPEND REQUEST\n");
1634 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001635 case GSM48_MT_RR_PAG_RESP:
Holger Hans Peter Freytherca0fcbe2009-10-23 13:48:54 +02001636 rc = gsm48_rx_rr_pag_resp(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001637 break;
Harald Welte7ccf7782009-02-17 01:43:01 +00001638 case GSM48_MT_RR_CHAN_MODE_MODIF_ACK:
Holger Hans Peter Freytherf520e642009-10-22 15:23:11 +02001639 rc = gsm48_rx_rr_modif_ack(msg);
Harald Welte7ccf7782009-02-17 01:43:01 +00001640 break;
Harald Weltecf5b3592009-05-01 18:28:42 +00001641 case GSM48_MT_RR_STATUS:
1642 rc = gsm48_rx_rr_status(msg);
1643 break;
Harald Weltef7c43522009-06-09 20:24:21 +00001644 case GSM48_MT_RR_MEAS_REP:
1645 rc = gsm48_rx_rr_meas_rep(msg);
1646 break;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001647 case GSM48_MT_RR_APP_INFO:
1648 rc = gsm48_rx_rr_app_info(msg);
1649 break;
Harald Welte08d91a52009-08-30 15:37:11 +09001650 case GSM48_MT_RR_CIPH_M_COMPL:
1651 DEBUGP(DRR, "CIPHERING MODE COMPLETE\n");
1652 /* FIXME: check for MI (if any) */
1653 break;
Harald Welteccd5a882009-11-29 20:02:20 +01001654 case GSM48_MT_RR_HANDO_COMPL:
Harald Welted011e8b2009-11-29 22:45:52 +01001655 rc = gsm48_rx_rr_ho_compl(msg);
Harald Welteccd5a882009-11-29 20:02:20 +01001656 break;
1657 case GSM48_MT_RR_HANDO_FAIL:
Harald Welted011e8b2009-11-29 22:45:52 +01001658 rc = gsm48_rx_rr_ho_fail(msg);
Harald Welteccd5a882009-11-29 20:02:20 +01001659 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001660 default:
Harald Welte2d35ae62009-02-06 12:02:13 +00001661 fprintf(stderr, "Unimplemented GSM 04.08 RR msg type 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +00001662 gh->msg_type);
1663 break;
1664 }
1665
Harald Welte2d35ae62009-02-06 12:02:13 +00001666 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +00001667}
1668
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001669int gsm48_send_rr_app_info(struct gsm_lchan *lchan, u_int8_t apdu_id,
Holger Hans Peter Freyther8b77a342009-10-22 15:42:19 +02001670 u_int8_t apdu_len, const u_int8_t *apdu)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001671{
1672 struct msgb *msg = gsm48_msgb_alloc();
1673 struct gsm48_hdr *gh;
1674
1675 msg->lchan = lchan;
1676
1677 DEBUGP(DRR, "TX APPLICATION INFO id=0x%02x, len=%u\n",
1678 apdu_id, apdu_len);
1679
1680 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 2 + apdu_len);
1681 gh->proto_discr = GSM48_PDISC_RR;
1682 gh->msg_type = GSM48_MT_RR_APP_INFO;
1683 gh->data[0] = apdu_id;
1684 gh->data[1] = apdu_len;
1685 memcpy(gh->data+2, apdu, apdu_len);
1686
1687 return gsm48_sendmsg(msg, NULL);
1688}
1689
Harald Welte4bc90a12008-12-27 16:32:52 +00001690/* Call Control */
1691
Harald Welte7584aea2009-02-11 11:44:12 +00001692/* The entire call control code is written in accordance with Figure 7.10c
1693 * for 'very early assignment', i.e. we allocate a TCH/F during IMMEDIATE
1694 * ASSIGN, then first use that TCH/F for signalling and later MODE MODIFY
1695 * it for voice */
1696
Harald Welte4bfdfe72009-06-10 23:11:52 +08001697static void new_cc_state(struct gsm_trans *trans, int state)
1698{
1699 if (state > 31 || state < 0)
1700 return;
1701
1702 DEBUGP(DCC, "new state %s -> %s\n",
Harald Weltedcaf5652009-07-23 18:56:43 +02001703 cc_state_names[trans->cc.state], cc_state_names[state]);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001704
Harald Weltedcaf5652009-07-23 18:56:43 +02001705 trans->cc.state = state;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001706}
1707
1708static int gsm48_cc_tx_status(struct gsm_trans *trans, void *arg)
Harald Welte4bc90a12008-12-27 16:32:52 +00001709{
1710 struct msgb *msg = gsm48_msgb_alloc();
1711 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1712 u_int8_t *cause, *call_state;
1713
Harald Welte4bc90a12008-12-27 16:32:52 +00001714 gh->msg_type = GSM48_MT_CC_STATUS;
1715
1716 cause = msgb_put(msg, 3);
1717 cause[0] = 2;
1718 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
1719 cause[2] = 0x80 | 30; /* response to status inquiry */
1720
1721 call_state = msgb_put(msg, 1);
1722 call_state[0] = 0xc0 | 0x00;
1723
Harald Welte39e2ead2009-07-23 21:13:03 +02001724 return gsm48_sendmsg(msg, trans);
Harald Welte4bc90a12008-12-27 16:32:52 +00001725}
1726
Harald Welte6f4b7532008-12-29 00:39:37 +00001727static int gsm48_tx_simple(struct gsm_lchan *lchan,
1728 u_int8_t pdisc, u_int8_t msg_type)
Harald Welte4bc90a12008-12-27 16:32:52 +00001729{
1730 struct msgb *msg = gsm48_msgb_alloc();
1731 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1732
1733 msg->lchan = lchan;
1734
Harald Welte6f4b7532008-12-29 00:39:37 +00001735 gh->proto_discr = pdisc;
Harald Welte4bc90a12008-12-27 16:32:52 +00001736 gh->msg_type = msg_type;
1737
Harald Welte39e2ead2009-07-23 21:13:03 +02001738 return gsm48_sendmsg(msg, NULL);
Harald Welte4bc90a12008-12-27 16:32:52 +00001739}
1740
Harald Welte4bfdfe72009-06-10 23:11:52 +08001741static void gsm48_stop_cc_timer(struct gsm_trans *trans)
1742{
Harald Weltedcaf5652009-07-23 18:56:43 +02001743 if (bsc_timer_pending(&trans->cc.timer)) {
1744 DEBUGP(DCC, "stopping pending timer T%x\n", trans->cc.Tcurrent);
1745 bsc_del_timer(&trans->cc.timer);
1746 trans->cc.Tcurrent = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001747 }
1748}
1749
1750static int mncc_recvmsg(struct gsm_network *net, struct gsm_trans *trans,
1751 int msg_type, struct gsm_mncc *mncc)
1752{
1753 struct msgb *msg;
1754
1755 if (trans)
1756 if (trans->lchan)
Harald Welte6f5aee02009-07-23 21:21:14 +02001757 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08001758 "Sending '%s' to MNCC.\n",
1759 trans->lchan->ts->trx->bts->nr,
1760 trans->lchan->ts->trx->nr,
1761 trans->lchan->ts->nr, trans->transaction_id,
1762 (trans->subscr)?(trans->subscr->extension):"-",
1763 get_mncc_name(msg_type));
1764 else
1765 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
1766 "Sending '%s' to MNCC.\n",
1767 (trans->subscr)?(trans->subscr->extension):"-",
1768 get_mncc_name(msg_type));
1769 else
1770 DEBUGP(DCC, "(bts - trx - ts - ti -- sub -) "
1771 "Sending '%s' to MNCC.\n", get_mncc_name(msg_type));
1772
1773 mncc->msg_type = msg_type;
1774
Harald Welte966636f2009-06-26 19:39:35 +02001775 msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001776 if (!msg)
1777 return -ENOMEM;
1778 memcpy(msg->data, mncc, sizeof(struct gsm_mncc));
1779 msgb_enqueue(&net->upqueue, msg);
1780
1781 return 0;
1782}
1783
1784int mncc_release_ind(struct gsm_network *net, struct gsm_trans *trans,
1785 u_int32_t callref, int location, int value)
1786{
1787 struct gsm_mncc rel;
1788
Harald Welte92f70c52009-06-12 01:54:08 +08001789 memset(&rel, 0, sizeof(rel));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001790 rel.callref = callref;
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001791 mncc_set_cause(&rel, location, value);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001792 return mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
1793}
1794
Harald Weltedcaf5652009-07-23 18:56:43 +02001795/* Call Control Specific transaction release.
1796 * gets called by trans_free, DO NOT CALL YOURSELF! */
1797void _gsm48_cc_trans_free(struct gsm_trans *trans)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001798{
Harald Welte4bfdfe72009-06-10 23:11:52 +08001799 gsm48_stop_cc_timer(trans);
1800
1801 /* send release to L4, if callref still exists */
1802 if (trans->callref) {
1803 /* Ressource unavailable */
Harald Welte596fed42009-07-23 19:06:52 +02001804 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001805 GSM48_CAUSE_LOC_PRN_S_LU,
1806 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001807 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001808 if (trans->cc.state != GSM_CSTATE_NULL)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001809 new_cc_state(trans, GSM_CSTATE_NULL);
Harald Weltedcaf5652009-07-23 18:56:43 +02001810 if (trans->lchan)
1811 trau_mux_unmap(&trans->lchan->ts->e1_link, trans->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001812}
1813
1814static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg);
1815
Harald Welte09e38af2009-02-16 22:52:23 +00001816/* call-back from paging the B-end of the connection */
1817static int setup_trig_pag_evt(unsigned int hooknum, unsigned int event,
Harald Welte7ccf7782009-02-17 01:43:01 +00001818 struct msgb *msg, void *_lchan, void *param)
Harald Welte09e38af2009-02-16 22:52:23 +00001819{
Harald Welte7ccf7782009-02-17 01:43:01 +00001820 struct gsm_lchan *lchan = _lchan;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001821 struct gsm_subscriber *subscr = param;
1822 struct gsm_trans *transt, *tmp;
1823 struct gsm_network *net;
Harald Weltec05677b2009-06-26 20:17:06 +02001824
Harald Welte09e38af2009-02-16 22:52:23 +00001825 if (hooknum != GSM_HOOK_RR_PAGING)
1826 return -EINVAL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001827
1828 if (!subscr)
1829 return -EINVAL;
1830 net = subscr->net;
1831 if (!net) {
1832 DEBUGP(DCC, "Error Network not set!\n");
1833 return -EINVAL;
Harald Welte5a065df2009-02-22 21:13:18 +00001834 }
Harald Welte7584aea2009-02-11 11:44:12 +00001835
Harald Welte4bfdfe72009-06-10 23:11:52 +08001836 /* check all tranactions (without lchan) for subscriber */
1837 llist_for_each_entry_safe(transt, tmp, &net->trans_list, entry) {
1838 if (transt->subscr != subscr || transt->lchan)
1839 continue;
1840 switch (event) {
1841 case GSM_PAGING_SUCCEEDED:
1842 if (!lchan) // paranoid
1843 break;
1844 DEBUGP(DCC, "Paging subscr %s succeeded!\n",
1845 subscr->extension);
1846 /* Assign lchan */
1847 if (!transt->lchan) {
1848 transt->lchan = lchan;
1849 use_lchan(lchan);
1850 }
1851 /* send SETUP request to called party */
Harald Weltedcaf5652009-07-23 18:56:43 +02001852 gsm48_cc_tx_setup(transt, &transt->cc.msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001853 break;
1854 case GSM_PAGING_EXPIRED:
1855 DEBUGP(DCC, "Paging subscr %s expired!\n",
1856 subscr->extension);
1857 /* Temporarily out of order */
Harald Welte596fed42009-07-23 19:06:52 +02001858 mncc_release_ind(transt->subscr->net, transt,
1859 transt->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001860 GSM48_CAUSE_LOC_PRN_S_LU,
1861 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001862 transt->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001863 trans_free(transt);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001864 break;
1865 }
1866 }
Harald Welte09e38af2009-02-16 22:52:23 +00001867 return 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00001868}
Harald Welte7584aea2009-02-11 11:44:12 +00001869
Harald Welteda7ab742009-12-19 22:23:05 +01001870static int tch_recv_mncc(struct gsm_network *net, u_int32_t callref, int enable);
1871
Harald Welte805f6442009-07-28 18:25:29 +02001872/* some other part of the code sends us a signal */
1873static int handle_abisip_signal(unsigned int subsys, unsigned int signal,
1874 void *handler_data, void *signal_data)
1875{
1876 struct gsm_lchan *lchan = signal_data;
Harald Welte805f6442009-07-28 18:25:29 +02001877 int rc;
Harald Welteda7ab742009-12-19 22:23:05 +01001878 struct gsm_network *net;
1879 struct gsm_trans *trans;
Harald Welte805f6442009-07-28 18:25:29 +02001880
1881 if (subsys != SS_ABISIP)
1882 return 0;
1883
1884 /* in case we use direct BTS-to-BTS RTP */
1885 if (ipacc_rtp_direct)
1886 return 0;
1887
Harald Welte805f6442009-07-28 18:25:29 +02001888 switch (signal) {
Holger Hans Peter Freyther231163d2009-11-18 21:06:12 +01001889 case S_ABISIP_CRCX_ACK:
Harald Welte805f6442009-07-28 18:25:29 +02001890 /* the BTS has successfully bound a TCH to a local ip/port,
1891 * which means we can connect our UDP socket to it */
Harald Welte2c828992009-12-02 01:56:49 +05301892 if (lchan->abis_ip.rtp_socket) {
1893 rtp_socket_free(lchan->abis_ip.rtp_socket);
1894 lchan->abis_ip.rtp_socket = NULL;
Harald Welte805f6442009-07-28 18:25:29 +02001895 }
1896
Harald Welte2c828992009-12-02 01:56:49 +05301897 lchan->abis_ip.rtp_socket = rtp_socket_create();
1898 if (!lchan->abis_ip.rtp_socket)
Harald Welte805f6442009-07-28 18:25:29 +02001899 goto out_err;
1900
Harald Welte2c828992009-12-02 01:56:49 +05301901 rc = rtp_socket_connect(lchan->abis_ip.rtp_socket,
1902 lchan->abis_ip.bound_ip,
1903 lchan->abis_ip.bound_port);
Harald Welte805f6442009-07-28 18:25:29 +02001904 if (rc < 0)
1905 goto out_err;
Harald Welteda7ab742009-12-19 22:23:05 +01001906 /* check if any transactions on this lchan still have
1907 * a tch_recv_mncc request pending */
1908 net = lchan->ts->trx->bts->network;
1909 llist_for_each_entry(trans, &net->trans_list, entry) {
1910 if (trans->lchan == lchan && trans->tch_recv) {
1911 DEBUGP(DCC, "pending tch_recv_mncc request\n");
1912 tch_recv_mncc(net, trans->callref, 1);
1913 }
1914 }
Harald Welte805f6442009-07-28 18:25:29 +02001915 break;
Holger Hans Peter Freyther231163d2009-11-18 21:06:12 +01001916 case S_ABISIP_DLCX_IND:
Harald Welte805f6442009-07-28 18:25:29 +02001917 /* the BTS tells us a RTP stream has been disconnected */
Harald Welte2c828992009-12-02 01:56:49 +05301918 if (lchan->abis_ip.rtp_socket) {
1919 rtp_socket_free(lchan->abis_ip.rtp_socket);
1920 lchan->abis_ip.rtp_socket = NULL;
Harald Welte805f6442009-07-28 18:25:29 +02001921 }
1922 break;
1923 }
1924
1925 return 0;
1926out_err:
1927 /* FIXME: do something */
1928 return 0;
1929}
1930
1931/* bind rtp proxy to local IP/port and tell BTS to connect to it */
1932static int ipacc_connect_proxy_bind(struct gsm_lchan *lchan)
1933{
Harald Welte2c828992009-12-02 01:56:49 +05301934 struct rtp_socket *rs = lchan->abis_ip.rtp_socket;
Harald Welte805f6442009-07-28 18:25:29 +02001935 int rc;
1936
Holger Hans Peter Freyther231163d2009-11-18 21:06:12 +01001937 rc = rsl_ipacc_mdcx(lchan, ntohl(rs->rtp.sin_local.sin_addr.s_addr),
Harald Welte805f6442009-07-28 18:25:29 +02001938 ntohs(rs->rtp.sin_local.sin_port),
Harald Welte805f6442009-07-28 18:25:29 +02001939 /* FIXME: use RTP payload of bound socket, not BTS*/
Harald Welte2c828992009-12-02 01:56:49 +05301940 lchan->abis_ip.rtp_payload2);
Harald Welte805f6442009-07-28 18:25:29 +02001941
1942 return rc;
1943}
1944
Harald Welte49f48b82009-02-17 15:29:33 +00001945/* map two ipaccess RTP streams onto each other */
Harald Welte11fa29c2009-02-19 17:24:39 +00001946static int tch_map(struct gsm_lchan *lchan, struct gsm_lchan *remote_lchan)
Harald Welte49f48b82009-02-17 15:29:33 +00001947{
Harald Welte11fa29c2009-02-19 17:24:39 +00001948 struct gsm_bts *bts = lchan->ts->trx->bts;
1949 struct gsm_bts *remote_bts = remote_lchan->ts->trx->bts;
Harald Welte805f6442009-07-28 18:25:29 +02001950 int rc;
Harald Welte49f48b82009-02-17 15:29:33 +00001951
Harald Welte11fa29c2009-02-19 17:24:39 +00001952 DEBUGP(DCC, "Setting up TCH map between (bts=%u,trx=%u,ts=%u) and (bts=%u,trx=%u,ts=%u)\n",
1953 bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
1954 remote_bts->nr, remote_lchan->ts->trx->nr, remote_lchan->ts->nr);
1955
1956 if (bts->type != remote_bts->type) {
1957 DEBUGP(DCC, "Cannot switch calls between different BTS types yet\n");
1958 return -EINVAL;
1959 }
Harald Welteda7ab742009-12-19 22:23:05 +01001960
1961 // todo: map between different bts types
Harald Welte11fa29c2009-02-19 17:24:39 +00001962 switch (bts->type) {
Mike Habene2d82272009-10-02 12:19:34 +01001963 case GSM_BTS_TYPE_NANOBTS:
Harald Welte805f6442009-07-28 18:25:29 +02001964 if (!ipacc_rtp_direct) {
1965 /* connect the TCH's to our RTP proxy */
1966 rc = ipacc_connect_proxy_bind(lchan);
1967 if (rc < 0)
1968 return rc;
1969 rc = ipacc_connect_proxy_bind(remote_lchan);
Harald Welteda7ab742009-12-19 22:23:05 +01001970#warning do we need a check of rc here?
Harald Welte805f6442009-07-28 18:25:29 +02001971
1972 /* connect them with each other */
Harald Welte2c828992009-12-02 01:56:49 +05301973 rtp_socket_proxy(lchan->abis_ip.rtp_socket,
1974 remote_lchan->abis_ip.rtp_socket);
Harald Welte805f6442009-07-28 18:25:29 +02001975 } else {
1976 /* directly connect TCH RTP streams to each other */
Harald Welte2c828992009-12-02 01:56:49 +05301977 rc = rsl_ipacc_mdcx(lchan, remote_lchan->abis_ip.bound_ip,
1978 remote_lchan->abis_ip.bound_port,
Harald Welte2c828992009-12-02 01:56:49 +05301979 remote_lchan->abis_ip.rtp_payload2);
Harald Welte805f6442009-07-28 18:25:29 +02001980 if (rc < 0)
1981 return rc;
Harald Welte2c828992009-12-02 01:56:49 +05301982 rc = rsl_ipacc_mdcx(remote_lchan, lchan->abis_ip.bound_ip,
1983 lchan->abis_ip.bound_port,
Harald Welte2c828992009-12-02 01:56:49 +05301984 lchan->abis_ip.rtp_payload2);
Harald Welte805f6442009-07-28 18:25:29 +02001985 }
Harald Welte11fa29c2009-02-19 17:24:39 +00001986 break;
1987 case GSM_BTS_TYPE_BS11:
1988 trau_mux_map_lchan(lchan, remote_lchan);
1989 break;
1990 default:
1991 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
Harald Welteda7ab742009-12-19 22:23:05 +01001992 return -EINVAL;
Harald Welte11fa29c2009-02-19 17:24:39 +00001993 }
Harald Welte49f48b82009-02-17 15:29:33 +00001994
1995 return 0;
1996}
1997
Harald Welte4bfdfe72009-06-10 23:11:52 +08001998/* bridge channels of two transactions */
1999static int tch_bridge(struct gsm_network *net, u_int32_t *refs)
Harald Welte7ccf7782009-02-17 01:43:01 +00002000{
Harald Weltedcaf5652009-07-23 18:56:43 +02002001 struct gsm_trans *trans1 = trans_find_by_callref(net, refs[0]);
2002 struct gsm_trans *trans2 = trans_find_by_callref(net, refs[1]);
Harald Welte7ccf7782009-02-17 01:43:01 +00002003
Harald Welte4bfdfe72009-06-10 23:11:52 +08002004 if (!trans1 || !trans2)
Harald Welte7ccf7782009-02-17 01:43:01 +00002005 return -EIO;
2006
Harald Welte4bfdfe72009-06-10 23:11:52 +08002007 if (!trans1->lchan || !trans2->lchan)
2008 return -EIO;
2009
2010 /* through-connect channel */
2011 return tch_map(trans1->lchan, trans2->lchan);
Harald Welte7ccf7782009-02-17 01:43:01 +00002012}
2013
Harald Welteda7ab742009-12-19 22:23:05 +01002014/* enable receive of channels to MNCC upqueue */
2015static int tch_recv_mncc(struct gsm_network *net, u_int32_t callref, int enable)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002016{
2017 struct gsm_trans *trans;
Harald Welteda7ab742009-12-19 22:23:05 +01002018 struct gsm_lchan *lchan;
2019 struct gsm_bts *bts;
2020 int rc;
Harald Welte7ccf7782009-02-17 01:43:01 +00002021
Harald Welte4bfdfe72009-06-10 23:11:52 +08002022 /* Find callref */
Harald Welteda7ab742009-12-19 22:23:05 +01002023 trans = trans_find_by_callref(net, callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002024 if (!trans)
2025 return -EIO;
2026 if (!trans->lchan)
2027 return 0;
Harald Welteda7ab742009-12-19 22:23:05 +01002028 lchan = trans->lchan;
2029 bts = lchan->ts->trx->bts;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002030
Harald Welteda7ab742009-12-19 22:23:05 +01002031 switch (bts->type) {
2032 case GSM_BTS_TYPE_NANOBTS:
2033 if (ipacc_rtp_direct) {
2034 DEBUGP(DCC, "Error: RTP proxy is disabled\n");
2035 return -EINVAL;
2036 }
2037 /* in case, we don't have a RTP socket yet, we note this
2038 * in the transaction and try later */
2039 if (!lchan->abis_ip.rtp_socket) {
2040 trans->tch_recv = enable;
2041 DEBUGP(DCC, "queue tch_recv_mncc request (%d)\n", enable);
2042 return 0;
2043 }
2044 if (enable) {
2045 /* connect the TCH's to our RTP proxy */
2046 rc = ipacc_connect_proxy_bind(lchan);
2047 if (rc < 0)
2048 return rc;
2049 /* assign socket to application interface */
2050 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
2051 net, callref);
2052 } else
2053 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
2054 net, 0);
2055 break;
2056 case GSM_BTS_TYPE_BS11:
2057 if (enable)
2058 return trau_recv_lchan(lchan, callref);
2059 return trau_mux_unmap(NULL, callref);
2060 break;
2061 default:
2062 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
2063 return -EINVAL;
2064 }
2065
2066 return 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002067}
2068
Harald Welte4bfdfe72009-06-10 23:11:52 +08002069static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
2070{
2071 DEBUGP(DCC, "-> STATUS ENQ\n");
2072 return gsm48_cc_tx_status(trans, msg);
2073}
2074
2075static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg);
2076static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg);
2077
2078static void gsm48_cc_timeout(void *arg)
2079{
2080 struct gsm_trans *trans = arg;
2081 int disconnect = 0, release = 0;
Harald Weltec66b71c2009-06-11 14:23:20 +08002082 int mo_cause = GSM48_CC_CAUSE_RECOVERY_TIMER;
2083 int mo_location = GSM48_CAUSE_LOC_USER;
2084 int l4_cause = GSM48_CC_CAUSE_NORMAL_UNSPEC;
2085 int l4_location = GSM48_CAUSE_LOC_PRN_S_LU;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002086 struct gsm_mncc mo_rel, l4_rel;
2087
2088 memset(&mo_rel, 0, sizeof(struct gsm_mncc));
2089 mo_rel.callref = trans->callref;
2090 memset(&l4_rel, 0, sizeof(struct gsm_mncc));
2091 l4_rel.callref = trans->callref;
2092
Harald Weltedcaf5652009-07-23 18:56:43 +02002093 switch(trans->cc.Tcurrent) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002094 case 0x303:
2095 release = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08002096 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002097 break;
2098 case 0x310:
2099 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08002100 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002101 break;
2102 case 0x313:
2103 disconnect = 1;
2104 /* unknown, did not find it in the specs */
2105 break;
2106 case 0x301:
2107 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08002108 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002109 break;
2110 case 0x308:
Harald Weltedcaf5652009-07-23 18:56:43 +02002111 if (!trans->cc.T308_second) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002112 /* restart T308 a second time */
Harald Weltedcaf5652009-07-23 18:56:43 +02002113 gsm48_cc_tx_release(trans, &trans->cc.msg);
2114 trans->cc.T308_second = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002115 break; /* stay in release state */
2116 }
Harald Weltedcaf5652009-07-23 18:56:43 +02002117 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002118 return;
2119// release = 1;
2120// l4_cause = 14;
2121// break;
2122 case 0x306:
2123 release = 1;
Harald Weltedcaf5652009-07-23 18:56:43 +02002124 mo_cause = trans->cc.msg.cause.value;
2125 mo_location = trans->cc.msg.cause.location;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002126 break;
2127 case 0x323:
2128 disconnect = 1;
2129 break;
2130 default:
2131 release = 1;
2132 }
2133
2134 if (release && trans->callref) {
2135 /* process release towards layer 4 */
Harald Welte596fed42009-07-23 19:06:52 +02002136 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002137 l4_location, l4_cause);
2138 trans->callref = 0;
2139 }
2140
2141 if (disconnect && trans->callref) {
2142 /* process disconnect towards layer 4 */
2143 mncc_set_cause(&l4_rel, l4_location, l4_cause);
Harald Welte596fed42009-07-23 19:06:52 +02002144 mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &l4_rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002145 }
2146
2147 /* process disconnect towards mobile station */
2148 if (disconnect || release) {
2149 mncc_set_cause(&mo_rel, mo_location, mo_cause);
Harald Weltedcaf5652009-07-23 18:56:43 +02002150 mo_rel.cause.diag[0] = ((trans->cc.Tcurrent & 0xf00) >> 8) + '0';
2151 mo_rel.cause.diag[1] = ((trans->cc.Tcurrent & 0x0f0) >> 4) + '0';
2152 mo_rel.cause.diag[2] = (trans->cc.Tcurrent & 0x00f) + '0';
Harald Welte4bfdfe72009-06-10 23:11:52 +08002153 mo_rel.cause.diag_len = 3;
2154
2155 if (disconnect)
2156 gsm48_cc_tx_disconnect(trans, &mo_rel);
2157 if (release)
2158 gsm48_cc_tx_release(trans, &mo_rel);
2159 }
2160
2161}
2162
2163static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
2164 int sec, int micro)
2165{
2166 DEBUGP(DCC, "starting timer T%x with %d seconds\n", current, sec);
Harald Weltedcaf5652009-07-23 18:56:43 +02002167 trans->cc.timer.cb = gsm48_cc_timeout;
2168 trans->cc.timer.data = trans;
2169 bsc_schedule_timer(&trans->cc.timer, sec, micro);
2170 trans->cc.Tcurrent = current;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002171}
2172
2173static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
2174{
2175 struct gsm48_hdr *gh = msgb_l3(msg);
2176 u_int8_t msg_type = gh->msg_type & 0xbf;
2177 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2178 struct tlv_parsed tp;
2179 struct gsm_mncc setup;
2180
2181 memset(&setup, 0, sizeof(struct gsm_mncc));
2182 setup.callref = trans->callref;
2183 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2184 /* emergency setup is identified by msg_type */
2185 if (msg_type == GSM48_MT_CC_EMERG_SETUP)
2186 setup.emergency = 1;
2187
2188 /* use subscriber as calling party number */
2189 if (trans->subscr) {
2190 setup.fields |= MNCC_F_CALLING;
2191 strncpy(setup.calling.number, trans->subscr->extension,
2192 sizeof(setup.calling.number)-1);
Andreas Eversbergc079be42009-06-15 23:22:09 +02002193 strncpy(setup.imsi, trans->subscr->imsi,
2194 sizeof(setup.imsi)-1);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002195 }
2196 /* bearer capability */
2197 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2198 setup.fields |= MNCC_F_BEARER_CAP;
2199 decode_bearer_cap(&setup.bearer_cap,
2200 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2201 }
2202 /* facility */
2203 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2204 setup.fields |= MNCC_F_FACILITY;
2205 decode_facility(&setup.facility,
2206 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2207 }
2208 /* called party bcd number */
2209 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
2210 setup.fields |= MNCC_F_CALLED;
2211 decode_called(&setup.called,
2212 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
2213 }
2214 /* user-user */
2215 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2216 setup.fields |= MNCC_F_USERUSER;
2217 decode_useruser(&setup.useruser,
2218 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2219 }
2220 /* ss-version */
2221 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2222 setup.fields |= MNCC_F_SSVERSION;
2223 decode_ssversion(&setup.ssversion,
2224 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2225 }
2226 /* CLIR suppression */
2227 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
2228 setup.clir.sup = 1;
2229 /* CLIR invocation */
2230 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
2231 setup.clir.inv = 1;
2232 /* cc cap */
2233 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
2234 setup.fields |= MNCC_F_CCCAP;
2235 decode_cccap(&setup.cccap,
2236 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
2237 }
2238
Harald Welte4bfdfe72009-06-10 23:11:52 +08002239 new_cc_state(trans, GSM_CSTATE_INITIATED);
2240
2241 /* indicate setup to MNCC */
Harald Welte596fed42009-07-23 19:06:52 +02002242 mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_IND, &setup);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002243
Harald Welte13cac662009-07-29 12:10:35 +02002244 /* MNCC code will modify the channel asynchronously, we should
2245 * ipaccess-bind only after the modification has been made to the
2246 * lchan->tch_mode */
Harald Welte4bfdfe72009-06-10 23:11:52 +08002247 return 0;
2248}
2249
2250static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
Harald Welte65e74cc2008-12-29 01:55:35 +00002251{
2252 struct msgb *msg = gsm48_msgb_alloc();
2253 struct gsm48_hdr *gh;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002254 struct gsm_mncc *setup = arg;
Harald Welte78283ef2009-07-23 21:36:44 +02002255 int rc, trans_id;
Harald Welte65e74cc2008-12-29 01:55:35 +00002256
Harald Welte7ccf7782009-02-17 01:43:01 +00002257 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte65e74cc2008-12-29 01:55:35 +00002258
Harald Welte4bfdfe72009-06-10 23:11:52 +08002259 /* transaction id must not be assigned */
2260 if (trans->transaction_id != 0xff) { /* unasssigned */
2261 DEBUGP(DCC, "TX Setup with assigned transaction. "
2262 "This is not allowed!\n");
2263 /* Temporarily out of order */
Harald Welte596fed42009-07-23 19:06:52 +02002264 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002265 GSM48_CAUSE_LOC_PRN_S_LU,
2266 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002267 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002268 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002269 return rc;
2270 }
2271
2272 /* Get free transaction_id */
Harald Welte78283ef2009-07-23 21:36:44 +02002273 trans_id = trans_assign_trans_id(trans->subscr, GSM48_PDISC_CC, 0);
2274 if (trans_id < 0) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002275 /* no free transaction ID */
Harald Welte596fed42009-07-23 19:06:52 +02002276 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002277 GSM48_CAUSE_LOC_PRN_S_LU,
2278 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002279 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002280 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002281 return rc;
2282 }
Harald Welte78283ef2009-07-23 21:36:44 +02002283 trans->transaction_id = trans_id;
Harald Welte49f48b82009-02-17 15:29:33 +00002284
Harald Welte65e74cc2008-12-29 01:55:35 +00002285 gh->msg_type = GSM48_MT_CC_SETUP;
Harald Welte09e38af2009-02-16 22:52:23 +00002286
Harald Welte4bfdfe72009-06-10 23:11:52 +08002287 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
Harald Welte65e74cc2008-12-29 01:55:35 +00002288
Harald Welte4bfdfe72009-06-10 23:11:52 +08002289 /* bearer capability */
2290 if (setup->fields & MNCC_F_BEARER_CAP)
2291 encode_bearer_cap(msg, 0, &setup->bearer_cap);
2292 /* facility */
2293 if (setup->fields & MNCC_F_FACILITY)
2294 encode_facility(msg, 0, &setup->facility);
2295 /* progress */
2296 if (setup->fields & MNCC_F_PROGRESS)
2297 encode_progress(msg, 0, &setup->progress);
2298 /* calling party BCD number */
2299 if (setup->fields & MNCC_F_CALLING)
2300 encode_calling(msg, &setup->calling);
2301 /* called party BCD number */
2302 if (setup->fields & MNCC_F_CALLED)
2303 encode_called(msg, &setup->called);
2304 /* user-user */
2305 if (setup->fields & MNCC_F_USERUSER)
2306 encode_useruser(msg, 0, &setup->useruser);
2307 /* redirecting party BCD number */
2308 if (setup->fields & MNCC_F_REDIRECTING)
2309 encode_redirecting(msg, &setup->redirecting);
2310 /* signal */
2311 if (setup->fields & MNCC_F_SIGNAL)
2312 encode_signal(msg, setup->signal);
2313
2314 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
Harald Welte65e74cc2008-12-29 01:55:35 +00002315
Harald Welte39e2ead2009-07-23 21:13:03 +02002316 return gsm48_sendmsg(msg, trans);
Harald Welte65e74cc2008-12-29 01:55:35 +00002317}
2318
Harald Welte4bfdfe72009-06-10 23:11:52 +08002319static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
2320{
2321 struct gsm48_hdr *gh = msgb_l3(msg);
2322 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2323 struct tlv_parsed tp;
2324 struct gsm_mncc call_conf;
2325
2326 gsm48_stop_cc_timer(trans);
2327 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
2328
2329 memset(&call_conf, 0, sizeof(struct gsm_mncc));
2330 call_conf.callref = trans->callref;
2331 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2332#if 0
2333 /* repeat */
2334 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
2335 call_conf.repeat = 1;
2336 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
2337 call_conf.repeat = 2;
2338#endif
2339 /* bearer capability */
2340 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2341 call_conf.fields |= MNCC_F_BEARER_CAP;
2342 decode_bearer_cap(&call_conf.bearer_cap,
2343 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2344 }
2345 /* cause */
2346 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2347 call_conf.fields |= MNCC_F_CAUSE;
2348 decode_cause(&call_conf.cause,
2349 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2350 }
2351 /* cc cap */
2352 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
2353 call_conf.fields |= MNCC_F_CCCAP;
2354 decode_cccap(&call_conf.cccap,
2355 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
2356 }
2357
2358 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
2359
Harald Welte596fed42009-07-23 19:06:52 +02002360 return mncc_recvmsg(trans->subscr->net, trans, MNCC_CALL_CONF_IND,
2361 &call_conf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002362}
2363
2364static int gsm48_cc_tx_call_proc(struct gsm_trans *trans, void *arg)
2365{
2366 struct gsm_mncc *proceeding = arg;
2367 struct msgb *msg = gsm48_msgb_alloc();
2368 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2369
Harald Welte4bfdfe72009-06-10 23:11:52 +08002370 gh->msg_type = GSM48_MT_CC_CALL_PROC;
2371
2372 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
2373
2374 /* bearer capability */
2375 if (proceeding->fields & MNCC_F_BEARER_CAP)
2376 encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
2377 /* facility */
2378 if (proceeding->fields & MNCC_F_FACILITY)
2379 encode_facility(msg, 0, &proceeding->facility);
2380 /* progress */
2381 if (proceeding->fields & MNCC_F_PROGRESS)
2382 encode_progress(msg, 0, &proceeding->progress);
2383
Harald Welte39e2ead2009-07-23 21:13:03 +02002384 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002385}
2386
2387static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
2388{
2389 struct gsm48_hdr *gh = msgb_l3(msg);
2390 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2391 struct tlv_parsed tp;
2392 struct gsm_mncc alerting;
2393
2394 gsm48_stop_cc_timer(trans);
2395 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
2396
2397 memset(&alerting, 0, sizeof(struct gsm_mncc));
2398 alerting.callref = trans->callref;
2399 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2400 /* facility */
2401 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2402 alerting.fields |= MNCC_F_FACILITY;
2403 decode_facility(&alerting.facility,
2404 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2405 }
2406
2407 /* progress */
2408 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
2409 alerting.fields |= MNCC_F_PROGRESS;
2410 decode_progress(&alerting.progress,
2411 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
2412 }
2413 /* ss-version */
2414 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2415 alerting.fields |= MNCC_F_SSVERSION;
2416 decode_ssversion(&alerting.ssversion,
2417 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2418 }
2419
2420 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
2421
Harald Welte596fed42009-07-23 19:06:52 +02002422 return mncc_recvmsg(trans->subscr->net, trans, MNCC_ALERT_IND,
2423 &alerting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002424}
2425
2426static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
2427{
2428 struct gsm_mncc *alerting = arg;
2429 struct msgb *msg = gsm48_msgb_alloc();
2430 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2431
Harald Welte4bfdfe72009-06-10 23:11:52 +08002432 gh->msg_type = GSM48_MT_CC_ALERTING;
2433
2434 /* facility */
2435 if (alerting->fields & MNCC_F_FACILITY)
2436 encode_facility(msg, 0, &alerting->facility);
2437 /* progress */
2438 if (alerting->fields & MNCC_F_PROGRESS)
2439 encode_progress(msg, 0, &alerting->progress);
2440 /* user-user */
2441 if (alerting->fields & MNCC_F_USERUSER)
2442 encode_useruser(msg, 0, &alerting->useruser);
2443
2444 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
2445
Harald Welte39e2ead2009-07-23 21:13:03 +02002446 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002447}
2448
2449static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
2450{
2451 struct gsm_mncc *progress = arg;
2452 struct msgb *msg = gsm48_msgb_alloc();
2453 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2454
Harald Welte4bfdfe72009-06-10 23:11:52 +08002455 gh->msg_type = GSM48_MT_CC_PROGRESS;
2456
2457 /* progress */
2458 encode_progress(msg, 1, &progress->progress);
2459 /* user-user */
2460 if (progress->fields & MNCC_F_USERUSER)
2461 encode_useruser(msg, 0, &progress->useruser);
2462
Harald Welte39e2ead2009-07-23 21:13:03 +02002463 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002464}
2465
2466static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
2467{
2468 struct gsm_mncc *connect = arg;
2469 struct msgb *msg = gsm48_msgb_alloc();
2470 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2471
Harald Welte4bfdfe72009-06-10 23:11:52 +08002472 gh->msg_type = GSM48_MT_CC_CONNECT;
2473
2474 gsm48_stop_cc_timer(trans);
2475 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
2476
2477 /* facility */
2478 if (connect->fields & MNCC_F_FACILITY)
2479 encode_facility(msg, 0, &connect->facility);
2480 /* progress */
2481 if (connect->fields & MNCC_F_PROGRESS)
2482 encode_progress(msg, 0, &connect->progress);
2483 /* connected number */
2484 if (connect->fields & MNCC_F_CONNECTED)
2485 encode_connected(msg, &connect->connected);
2486 /* user-user */
2487 if (connect->fields & MNCC_F_USERUSER)
2488 encode_useruser(msg, 0, &connect->useruser);
2489
2490 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
2491
Harald Welte39e2ead2009-07-23 21:13:03 +02002492 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002493}
2494
2495static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
2496{
2497 struct gsm48_hdr *gh = msgb_l3(msg);
2498 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2499 struct tlv_parsed tp;
2500 struct gsm_mncc connect;
2501
2502 gsm48_stop_cc_timer(trans);
2503
2504 memset(&connect, 0, sizeof(struct gsm_mncc));
2505 connect.callref = trans->callref;
2506 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2507 /* use subscriber as connected party number */
2508 if (trans->subscr) {
2509 connect.fields |= MNCC_F_CONNECTED;
2510 strncpy(connect.connected.number, trans->subscr->extension,
2511 sizeof(connect.connected.number)-1);
Andreas Eversbergc079be42009-06-15 23:22:09 +02002512 strncpy(connect.imsi, trans->subscr->imsi,
2513 sizeof(connect.imsi)-1);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002514 }
2515 /* facility */
2516 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2517 connect.fields |= MNCC_F_FACILITY;
2518 decode_facility(&connect.facility,
2519 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2520 }
2521 /* user-user */
2522 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2523 connect.fields |= MNCC_F_USERUSER;
2524 decode_useruser(&connect.useruser,
2525 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2526 }
2527 /* ss-version */
2528 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2529 connect.fields |= MNCC_F_SSVERSION;
2530 decode_ssversion(&connect.ssversion,
2531 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2532 }
2533
2534 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
2535
Harald Welte596fed42009-07-23 19:06:52 +02002536 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_CNF, &connect);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002537}
2538
2539
2540static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
2541{
2542 struct gsm_mncc connect_ack;
2543
2544 gsm48_stop_cc_timer(trans);
2545
2546 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2547
2548 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
2549 connect_ack.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02002550 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_COMPL_IND,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002551 &connect_ack);
2552}
2553
2554static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
2555{
2556 struct msgb *msg = gsm48_msgb_alloc();
2557 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2558
Harald Welte4bfdfe72009-06-10 23:11:52 +08002559 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
2560
2561 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2562
Harald Welte39e2ead2009-07-23 21:13:03 +02002563 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002564}
2565
2566static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
2567{
2568 struct gsm48_hdr *gh = msgb_l3(msg);
2569 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2570 struct tlv_parsed tp;
2571 struct gsm_mncc disc;
2572
2573 gsm48_stop_cc_timer(trans);
2574
2575 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
2576
2577 memset(&disc, 0, sizeof(struct gsm_mncc));
2578 disc.callref = trans->callref;
2579 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
2580 /* cause */
2581 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2582 disc.fields |= MNCC_F_CAUSE;
2583 decode_cause(&disc.cause,
2584 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2585 }
2586 /* facility */
2587 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2588 disc.fields |= MNCC_F_FACILITY;
2589 decode_facility(&disc.facility,
2590 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2591 }
2592 /* user-user */
2593 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2594 disc.fields |= MNCC_F_USERUSER;
2595 decode_useruser(&disc.useruser,
2596 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2597 }
2598 /* ss-version */
2599 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2600 disc.fields |= MNCC_F_SSVERSION;
2601 decode_ssversion(&disc.ssversion,
2602 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2603 }
2604
Harald Welte596fed42009-07-23 19:06:52 +02002605 return mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &disc);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002606
2607}
2608
Harald Weltec66b71c2009-06-11 14:23:20 +08002609static struct gsm_mncc_cause default_cause = {
2610 .location = GSM48_CAUSE_LOC_PRN_S_LU,
2611 .coding = 0,
2612 .rec = 0,
2613 .rec_val = 0,
2614 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
2615 .diag_len = 0,
2616 .diag = { 0 },
2617};
Harald Welte4bfdfe72009-06-10 23:11:52 +08002618
2619static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
2620{
2621 struct gsm_mncc *disc = arg;
2622 struct msgb *msg = gsm48_msgb_alloc();
2623 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2624
Harald Welte4bfdfe72009-06-10 23:11:52 +08002625 gh->msg_type = GSM48_MT_CC_DISCONNECT;
2626
2627 gsm48_stop_cc_timer(trans);
2628 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
2629
2630 /* cause */
2631 if (disc->fields & MNCC_F_CAUSE)
2632 encode_cause(msg, 1, &disc->cause);
2633 else
2634 encode_cause(msg, 1, &default_cause);
2635
2636 /* facility */
2637 if (disc->fields & MNCC_F_FACILITY)
2638 encode_facility(msg, 0, &disc->facility);
2639 /* progress */
2640 if (disc->fields & MNCC_F_PROGRESS)
2641 encode_progress(msg, 0, &disc->progress);
2642 /* user-user */
2643 if (disc->fields & MNCC_F_USERUSER)
2644 encode_useruser(msg, 0, &disc->useruser);
2645
2646 /* store disconnect cause for T306 expiry */
Harald Weltedcaf5652009-07-23 18:56:43 +02002647 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002648
2649 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
2650
Harald Welte39e2ead2009-07-23 21:13:03 +02002651 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002652}
2653
2654static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
2655{
2656 struct gsm48_hdr *gh = msgb_l3(msg);
2657 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2658 struct tlv_parsed tp;
2659 struct gsm_mncc rel;
2660 int rc;
2661
2662 gsm48_stop_cc_timer(trans);
2663
2664 memset(&rel, 0, sizeof(struct gsm_mncc));
2665 rel.callref = trans->callref;
2666 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2667 /* cause */
2668 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2669 rel.fields |= MNCC_F_CAUSE;
2670 decode_cause(&rel.cause,
2671 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2672 }
2673 /* facility */
2674 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2675 rel.fields |= MNCC_F_FACILITY;
2676 decode_facility(&rel.facility,
2677 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2678 }
2679 /* user-user */
2680 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2681 rel.fields |= MNCC_F_USERUSER;
2682 decode_useruser(&rel.useruser,
2683 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2684 }
2685 /* ss-version */
2686 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2687 rel.fields |= MNCC_F_SSVERSION;
2688 decode_ssversion(&rel.ssversion,
2689 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2690 }
2691
Harald Weltedcaf5652009-07-23 18:56:43 +02002692 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002693 /* release collision 5.4.5 */
Harald Welte596fed42009-07-23 19:06:52 +02002694 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_CNF, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002695 } else {
Harald Welte596fed42009-07-23 19:06:52 +02002696 rc = gsm48_tx_simple(msg->lchan,
Harald Welte6f5aee02009-07-23 21:21:14 +02002697 GSM48_PDISC_CC | (trans->transaction_id << 4),
Harald Welte596fed42009-07-23 19:06:52 +02002698 GSM48_MT_CC_RELEASE_COMPL);
2699 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_IND, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002700 }
2701
2702 new_cc_state(trans, GSM_CSTATE_NULL);
2703
2704 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002705 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002706
2707 return rc;
2708}
2709
2710static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
2711{
2712 struct gsm_mncc *rel = arg;
2713 struct msgb *msg = gsm48_msgb_alloc();
2714 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2715
Harald Welte4bfdfe72009-06-10 23:11:52 +08002716 gh->msg_type = GSM48_MT_CC_RELEASE;
2717
2718 trans->callref = 0;
2719
2720 gsm48_stop_cc_timer(trans);
2721 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
2722
2723 /* cause */
2724 if (rel->fields & MNCC_F_CAUSE)
2725 encode_cause(msg, 0, &rel->cause);
2726 /* facility */
2727 if (rel->fields & MNCC_F_FACILITY)
2728 encode_facility(msg, 0, &rel->facility);
2729 /* user-user */
2730 if (rel->fields & MNCC_F_USERUSER)
2731 encode_useruser(msg, 0, &rel->useruser);
2732
Harald Weltedcaf5652009-07-23 18:56:43 +02002733 trans->cc.T308_second = 0;
2734 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002735
Harald Weltedcaf5652009-07-23 18:56:43 +02002736 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002737 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
2738
Harald Welte39e2ead2009-07-23 21:13:03 +02002739 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002740}
2741
2742static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
2743{
2744 struct gsm48_hdr *gh = msgb_l3(msg);
2745 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2746 struct tlv_parsed tp;
2747 struct gsm_mncc rel;
2748 int rc = 0;
2749
2750 gsm48_stop_cc_timer(trans);
2751
2752 memset(&rel, 0, sizeof(struct gsm_mncc));
2753 rel.callref = trans->callref;
2754 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2755 /* cause */
2756 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2757 rel.fields |= MNCC_F_CAUSE;
2758 decode_cause(&rel.cause,
2759 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2760 }
2761 /* facility */
2762 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2763 rel.fields |= MNCC_F_FACILITY;
2764 decode_facility(&rel.facility,
2765 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2766 }
2767 /* user-user */
2768 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2769 rel.fields |= MNCC_F_USERUSER;
2770 decode_useruser(&rel.useruser,
2771 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2772 }
2773 /* ss-version */
2774 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2775 rel.fields |= MNCC_F_SSVERSION;
2776 decode_ssversion(&rel.ssversion,
2777 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2778 }
2779
2780 if (trans->callref) {
Harald Weltedcaf5652009-07-23 18:56:43 +02002781 switch (trans->cc.state) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002782 case GSM_CSTATE_CALL_PRESENT:
Harald Welte596fed42009-07-23 19:06:52 +02002783 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002784 MNCC_REJ_IND, &rel);
2785 break;
2786 case GSM_CSTATE_RELEASE_REQ:
Harald Welte596fed42009-07-23 19:06:52 +02002787 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002788 MNCC_REL_CNF, &rel);
Harald Welteb83d9382009-12-12 21:00:48 +01002789 /* FIXME: in case of multiple calls, we can't simply
2790 * hang up here ! */
2791 lchan_auto_release(msg->lchan);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002792 break;
2793 default:
Harald Welte596fed42009-07-23 19:06:52 +02002794 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002795 MNCC_REL_IND, &rel);
2796 }
2797 }
2798
2799 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002800 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002801
2802 return rc;
2803}
2804
2805static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
2806{
2807 struct gsm_mncc *rel = arg;
2808 struct msgb *msg = gsm48_msgb_alloc();
2809 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2810
Harald Welte4bfdfe72009-06-10 23:11:52 +08002811 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
2812
2813 trans->callref = 0;
2814
2815 gsm48_stop_cc_timer(trans);
2816
2817 /* cause */
2818 if (rel->fields & MNCC_F_CAUSE)
2819 encode_cause(msg, 0, &rel->cause);
2820 /* facility */
2821 if (rel->fields & MNCC_F_FACILITY)
2822 encode_facility(msg, 0, &rel->facility);
2823 /* user-user */
2824 if (rel->fields & MNCC_F_USERUSER)
2825 encode_useruser(msg, 0, &rel->useruser);
2826
Harald Weltedcaf5652009-07-23 18:56:43 +02002827 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002828
Harald Welte39e2ead2009-07-23 21:13:03 +02002829 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002830}
2831
2832static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
2833{
2834 struct gsm48_hdr *gh = msgb_l3(msg);
2835 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2836 struct tlv_parsed tp;
2837 struct gsm_mncc fac;
2838
2839 memset(&fac, 0, sizeof(struct gsm_mncc));
2840 fac.callref = trans->callref;
2841 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
2842 /* facility */
2843 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2844 fac.fields |= MNCC_F_FACILITY;
2845 decode_facility(&fac.facility,
2846 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2847 }
2848 /* ss-version */
2849 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2850 fac.fields |= MNCC_F_SSVERSION;
2851 decode_ssversion(&fac.ssversion,
2852 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2853 }
2854
Harald Welte596fed42009-07-23 19:06:52 +02002855 return mncc_recvmsg(trans->subscr->net, trans, MNCC_FACILITY_IND, &fac);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002856}
2857
2858static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
2859{
2860 struct gsm_mncc *fac = arg;
2861 struct msgb *msg = gsm48_msgb_alloc();
2862 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2863
Harald Welte4bfdfe72009-06-10 23:11:52 +08002864 gh->msg_type = GSM48_MT_CC_FACILITY;
2865
2866 /* facility */
2867 encode_facility(msg, 1, &fac->facility);
2868
Harald Welte39e2ead2009-07-23 21:13:03 +02002869 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002870}
2871
2872static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
2873{
2874 struct gsm_mncc hold;
2875
2876 memset(&hold, 0, sizeof(struct gsm_mncc));
2877 hold.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02002878 return mncc_recvmsg(trans->subscr->net, trans, MNCC_HOLD_IND, &hold);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002879}
2880
2881static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
2882{
2883 struct msgb *msg = gsm48_msgb_alloc();
2884 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2885
Harald Welte4bfdfe72009-06-10 23:11:52 +08002886 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
2887
Harald Welte39e2ead2009-07-23 21:13:03 +02002888 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002889}
2890
2891static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
2892{
2893 struct gsm_mncc *hold_rej = arg;
2894 struct msgb *msg = gsm48_msgb_alloc();
2895 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2896
Harald Welte4bfdfe72009-06-10 23:11:52 +08002897 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
2898
2899 /* cause */
2900 if (hold_rej->fields & MNCC_F_CAUSE)
2901 encode_cause(msg, 1, &hold_rej->cause);
2902 else
2903 encode_cause(msg, 1, &default_cause);
2904
Harald Welte39e2ead2009-07-23 21:13:03 +02002905 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002906}
2907
2908static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
2909{
2910 struct gsm_mncc retrieve;
2911
2912 memset(&retrieve, 0, sizeof(struct gsm_mncc));
2913 retrieve.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02002914 return mncc_recvmsg(trans->subscr->net, trans, MNCC_RETRIEVE_IND,
2915 &retrieve);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002916}
2917
2918static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
2919{
2920 struct msgb *msg = gsm48_msgb_alloc();
2921 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2922
Harald Welte4bfdfe72009-06-10 23:11:52 +08002923 gh->msg_type = GSM48_MT_CC_RETR_ACK;
2924
Harald Welte39e2ead2009-07-23 21:13:03 +02002925 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002926}
2927
2928static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
2929{
2930 struct gsm_mncc *retrieve_rej = arg;
2931 struct msgb *msg = gsm48_msgb_alloc();
2932 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2933
Harald Welte4bfdfe72009-06-10 23:11:52 +08002934 gh->msg_type = GSM48_MT_CC_RETR_REJ;
2935
2936 /* cause */
2937 if (retrieve_rej->fields & MNCC_F_CAUSE)
2938 encode_cause(msg, 1, &retrieve_rej->cause);
2939 else
2940 encode_cause(msg, 1, &default_cause);
2941
Harald Welte39e2ead2009-07-23 21:13:03 +02002942 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002943}
2944
2945static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
2946{
2947 struct gsm48_hdr *gh = msgb_l3(msg);
2948 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2949 struct tlv_parsed tp;
2950 struct gsm_mncc dtmf;
2951
2952 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2953 dtmf.callref = trans->callref;
2954 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, 0, 0);
2955 /* keypad facility */
2956 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
2957 dtmf.fields |= MNCC_F_KEYPAD;
2958 decode_keypad(&dtmf.keypad,
2959 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
2960 }
2961
Harald Welte596fed42009-07-23 19:06:52 +02002962 return mncc_recvmsg(trans->subscr->net, trans, MNCC_START_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002963}
2964
2965static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
2966{
2967 struct gsm_mncc *dtmf = arg;
2968 struct msgb *msg = gsm48_msgb_alloc();
2969 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2970
Harald Welte4bfdfe72009-06-10 23:11:52 +08002971 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
2972
2973 /* keypad */
2974 if (dtmf->fields & MNCC_F_KEYPAD)
2975 encode_keypad(msg, dtmf->keypad);
2976
Harald Welte39e2ead2009-07-23 21:13:03 +02002977 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002978}
2979
2980static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
2981{
2982 struct gsm_mncc *dtmf = arg;
2983 struct msgb *msg = gsm48_msgb_alloc();
2984 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2985
Harald Welte4bfdfe72009-06-10 23:11:52 +08002986 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
2987
2988 /* cause */
2989 if (dtmf->fields & MNCC_F_CAUSE)
2990 encode_cause(msg, 1, &dtmf->cause);
2991 else
2992 encode_cause(msg, 1, &default_cause);
2993
Harald Welte39e2ead2009-07-23 21:13:03 +02002994 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002995}
2996
2997static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
2998{
2999 struct msgb *msg = gsm48_msgb_alloc();
3000 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3001
Harald Welte4bfdfe72009-06-10 23:11:52 +08003002 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
3003
Harald Welte39e2ead2009-07-23 21:13:03 +02003004 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003005}
3006
3007static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
3008{
3009 struct gsm_mncc dtmf;
3010
3011 memset(&dtmf, 0, sizeof(struct gsm_mncc));
3012 dtmf.callref = trans->callref;
3013
Harald Welte596fed42009-07-23 19:06:52 +02003014 return mncc_recvmsg(trans->subscr->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003015}
3016
3017static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
3018{
3019 struct gsm48_hdr *gh = msgb_l3(msg);
3020 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
3021 struct tlv_parsed tp;
3022 struct gsm_mncc modify;
3023
3024 memset(&modify, 0, sizeof(struct gsm_mncc));
3025 modify.callref = trans->callref;
3026 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
3027 /* bearer capability */
3028 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
3029 modify.fields |= MNCC_F_BEARER_CAP;
3030 decode_bearer_cap(&modify.bearer_cap,
3031 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
3032 }
3033
3034 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
3035
Harald Welte596fed42009-07-23 19:06:52 +02003036 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_IND, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003037}
3038
3039static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
3040{
3041 struct gsm_mncc *modify = arg;
3042 struct msgb *msg = gsm48_msgb_alloc();
3043 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3044
Harald Welte4bfdfe72009-06-10 23:11:52 +08003045 gh->msg_type = GSM48_MT_CC_MODIFY;
3046
3047 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
3048
3049 /* bearer capability */
3050 encode_bearer_cap(msg, 1, &modify->bearer_cap);
3051
3052 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
3053
Harald Welte39e2ead2009-07-23 21:13:03 +02003054 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003055}
3056
3057static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
3058{
3059 struct gsm48_hdr *gh = msgb_l3(msg);
3060 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
3061 struct tlv_parsed tp;
3062 struct gsm_mncc modify;
3063
3064 gsm48_stop_cc_timer(trans);
3065
3066 memset(&modify, 0, sizeof(struct gsm_mncc));
3067 modify.callref = trans->callref;
3068 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
3069 /* bearer capability */
3070 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
3071 modify.fields |= MNCC_F_BEARER_CAP;
3072 decode_bearer_cap(&modify.bearer_cap,
3073 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
3074 }
3075
3076 new_cc_state(trans, GSM_CSTATE_ACTIVE);
3077
Harald Welte596fed42009-07-23 19:06:52 +02003078 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_CNF, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003079}
3080
3081static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
3082{
3083 struct gsm_mncc *modify = arg;
3084 struct msgb *msg = gsm48_msgb_alloc();
3085 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3086
Harald Welte4bfdfe72009-06-10 23:11:52 +08003087 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
3088
3089 /* bearer capability */
3090 encode_bearer_cap(msg, 1, &modify->bearer_cap);
3091
3092 new_cc_state(trans, GSM_CSTATE_ACTIVE);
3093
Harald Welte39e2ead2009-07-23 21:13:03 +02003094 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003095}
3096
3097static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
3098{
3099 struct gsm48_hdr *gh = msgb_l3(msg);
3100 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
3101 struct tlv_parsed tp;
3102 struct gsm_mncc modify;
3103
3104 gsm48_stop_cc_timer(trans);
3105
3106 memset(&modify, 0, sizeof(struct gsm_mncc));
3107 modify.callref = trans->callref;
3108 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
3109 /* bearer capability */
3110 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
3111 modify.fields |= GSM48_IE_BEARER_CAP;
3112 decode_bearer_cap(&modify.bearer_cap,
3113 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
3114 }
3115 /* cause */
3116 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
3117 modify.fields |= MNCC_F_CAUSE;
3118 decode_cause(&modify.cause,
3119 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
3120 }
3121
3122 new_cc_state(trans, GSM_CSTATE_ACTIVE);
3123
Harald Welte596fed42009-07-23 19:06:52 +02003124 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_REJ, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003125}
3126
3127static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
3128{
3129 struct gsm_mncc *modify = arg;
3130 struct msgb *msg = gsm48_msgb_alloc();
3131 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3132
Harald Welte4bfdfe72009-06-10 23:11:52 +08003133 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
3134
3135 /* bearer capability */
3136 encode_bearer_cap(msg, 1, &modify->bearer_cap);
3137 /* cause */
3138 encode_cause(msg, 1, &modify->cause);
3139
3140 new_cc_state(trans, GSM_CSTATE_ACTIVE);
3141
Harald Welte39e2ead2009-07-23 21:13:03 +02003142 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003143}
3144
3145static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
3146{
3147 struct gsm_mncc *notify = arg;
3148 struct msgb *msg = gsm48_msgb_alloc();
3149 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3150
Harald Welte4bfdfe72009-06-10 23:11:52 +08003151 gh->msg_type = GSM48_MT_CC_NOTIFY;
3152
3153 /* notify */
3154 encode_notify(msg, notify->notify);
3155
Harald Welte39e2ead2009-07-23 21:13:03 +02003156 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003157}
3158
3159static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
3160{
3161 struct gsm48_hdr *gh = msgb_l3(msg);
3162 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
3163// struct tlv_parsed tp;
3164 struct gsm_mncc notify;
3165
3166 memset(&notify, 0, sizeof(struct gsm_mncc));
3167 notify.callref = trans->callref;
3168// tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len);
3169 if (payload_len >= 1)
3170 decode_notify(&notify.notify, gh->data);
3171
Harald Welte596fed42009-07-23 19:06:52 +02003172 return mncc_recvmsg(trans->subscr->net, trans, MNCC_NOTIFY_IND, &notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003173}
3174
3175static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
3176{
3177 struct gsm_mncc *user = arg;
3178 struct msgb *msg = gsm48_msgb_alloc();
3179 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3180
Harald Welte4bfdfe72009-06-10 23:11:52 +08003181 gh->msg_type = GSM48_MT_CC_USER_INFO;
3182
3183 /* user-user */
3184 if (user->fields & MNCC_F_USERUSER)
3185 encode_useruser(msg, 1, &user->useruser);
3186 /* more data */
3187 if (user->more)
3188 encode_more(msg);
3189
Harald Welte39e2ead2009-07-23 21:13:03 +02003190 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003191}
3192
3193static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
3194{
3195 struct gsm48_hdr *gh = msgb_l3(msg);
3196 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
3197 struct tlv_parsed tp;
3198 struct gsm_mncc user;
3199
3200 memset(&user, 0, sizeof(struct gsm_mncc));
3201 user.callref = trans->callref;
3202 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
3203 /* user-user */
3204 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
3205 user.fields |= MNCC_F_USERUSER;
3206 decode_useruser(&user.useruser,
3207 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
3208 }
3209 /* more data */
3210 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
3211 user.more = 1;
3212
Harald Welte596fed42009-07-23 19:06:52 +02003213 return mncc_recvmsg(trans->subscr->net, trans, MNCC_USERINFO_IND, &user);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003214}
3215
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +02003216static int _gsm48_lchan_modify(struct gsm_trans *trans, void *arg)
Harald Welte4bfdfe72009-06-10 23:11:52 +08003217{
3218 struct gsm_mncc *mode = arg;
3219
Holger Hans Peter Freytherea528022009-11-18 22:57:02 +01003220 return gsm48_lchan_modify(trans->lchan, mode->lchan_mode);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003221}
3222
3223static struct downstate {
3224 u_int32_t states;
3225 int type;
3226 int (*rout) (struct gsm_trans *trans, void *arg);
3227} downstatelist[] = {
3228 /* mobile originating call establishment */
3229 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
3230 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc},
3231 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
3232 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
3233 {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 */
3234 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
3235 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
3236 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
3237 /* mobile terminating call establishment */
3238 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
3239 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
3240 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
3241 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
3242 /* signalling during call */
3243 {SBIT(GSM_CSTATE_ACTIVE),
3244 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
3245 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
3246 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
3247 {ALL_STATES,
3248 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
3249 {ALL_STATES,
3250 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
3251 {ALL_STATES,
3252 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
3253 {SBIT(GSM_CSTATE_ACTIVE),
3254 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
3255 {SBIT(GSM_CSTATE_ACTIVE),
3256 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
3257 {SBIT(GSM_CSTATE_ACTIVE),
3258 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
3259 {SBIT(GSM_CSTATE_ACTIVE),
3260 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
3261 {SBIT(GSM_CSTATE_ACTIVE),
3262 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
3263 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
3264 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
3265 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
3266 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
3267 {SBIT(GSM_CSTATE_ACTIVE),
3268 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
3269 /* clearing */
3270 {SBIT(GSM_CSTATE_INITIATED),
3271 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
3272 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
3273 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
3274 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
3275 MNCC_REL_REQ, gsm48_cc_tx_release},
3276 /* special */
3277 {ALL_STATES,
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +02003278 MNCC_LCHAN_MODIFY, _gsm48_lchan_modify},
Harald Welte4bfdfe72009-06-10 23:11:52 +08003279};
3280
3281#define DOWNSLLEN \
3282 (sizeof(downstatelist) / sizeof(struct downstate))
3283
3284
3285int mncc_send(struct gsm_network *net, int msg_type, void *arg)
3286{
Harald Welte1a6f7982009-08-09 18:52:33 +02003287 int i, rc = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003288 struct gsm_trans *trans = NULL, *transt;
Harald Welte1a6f7982009-08-09 18:52:33 +02003289 struct gsm_lchan *lchan = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003290 struct gsm_bts *bts = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003291 struct gsm_mncc *data = arg, rel;
3292
3293 /* handle special messages */
3294 switch(msg_type) {
3295 case MNCC_BRIDGE:
3296 return tch_bridge(net, arg);
3297 case MNCC_FRAME_DROP:
Harald Welteda7ab742009-12-19 22:23:05 +01003298 return tch_recv_mncc(net, data->callref, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003299 case MNCC_FRAME_RECV:
Harald Welteda7ab742009-12-19 22:23:05 +01003300 return tch_recv_mncc(net, data->callref, 1);
3301 case GSM_TCHF_FRAME:
3302 /* Find callref */
3303 trans = trans_find_by_callref(net, data->callref);
3304 if (!trans)
3305 return -EIO;
3306 if (!trans->lchan)
3307 return 0;
3308 if (trans->lchan->type != GSM_LCHAN_TCH_F)
3309 return 0;
3310 bts = trans->lchan->ts->trx->bts;
3311 switch (bts->type) {
3312 case GSM_BTS_TYPE_NANOBTS:
3313 if (!trans->lchan->abis_ip.rtp_socket)
3314 return 0;
3315 return rtp_send_frame(trans->lchan->abis_ip.rtp_socket, arg);
3316 case GSM_BTS_TYPE_BS11:
3317 return trau_send_frame(trans->lchan, arg);
3318 default:
3319 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
3320 }
3321 return -EINVAL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003322 }
3323
3324 memset(&rel, 0, sizeof(struct gsm_mncc));
3325 rel.callref = data->callref;
3326
3327 /* Find callref */
Harald Weltedcaf5652009-07-23 18:56:43 +02003328 trans = trans_find_by_callref(net, data->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003329
3330 /* Callref unknown */
3331 if (!trans) {
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003332 struct gsm_subscriber *subscr;
3333
Harald Welte4a3464c2009-07-04 10:11:24 +02003334 if (msg_type != MNCC_SETUP_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003335 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3336 "Received '%s' from MNCC with "
3337 "unknown callref %d\n", data->called.number,
3338 get_mncc_name(msg_type), data->callref);
3339 /* Invalid call reference */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003340 return mncc_release_ind(net, NULL, data->callref,
3341 GSM48_CAUSE_LOC_PRN_S_LU,
3342 GSM48_CC_CAUSE_INVAL_TRANS_ID);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003343 }
Andreas Eversbergc079be42009-06-15 23:22:09 +02003344 if (!data->called.number[0] && !data->imsi[0]) {
3345 DEBUGP(DCC, "(bts - trx - ts - ti) "
3346 "Received '%s' from MNCC with "
3347 "no number or IMSI\n", get_mncc_name(msg_type));
3348 /* Invalid number */
3349 return mncc_release_ind(net, NULL, data->callref,
3350 GSM48_CAUSE_LOC_PRN_S_LU,
3351 GSM48_CC_CAUSE_INV_NR_FORMAT);
3352 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003353 /* New transaction due to setup, find subscriber */
Andreas Eversbergc079be42009-06-15 23:22:09 +02003354 if (data->called.number[0])
Harald Welte9176bd42009-07-23 18:46:00 +02003355 subscr = subscr_get_by_extension(net,
3356 data->called.number);
Andreas Eversbergc079be42009-06-15 23:22:09 +02003357 else
Harald Welte9176bd42009-07-23 18:46:00 +02003358 subscr = subscr_get_by_imsi(net, data->imsi);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003359 /* If subscriber is not found */
3360 if (!subscr) {
3361 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3362 "Received '%s' from MNCC with "
3363 "unknown subscriber %s\n", data->called.number,
3364 get_mncc_name(msg_type), data->called.number);
3365 /* Unknown subscriber */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003366 return mncc_release_ind(net, NULL, data->callref,
3367 GSM48_CAUSE_LOC_PRN_S_LU,
3368 GSM48_CC_CAUSE_UNASSIGNED_NR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003369 }
3370 /* If subscriber is not "attached" */
3371 if (!subscr->lac) {
3372 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3373 "Received '%s' from MNCC with "
3374 "detached subscriber %s\n", data->called.number,
3375 get_mncc_name(msg_type), data->called.number);
3376 subscr_put(subscr);
3377 /* Temporarily out of order */
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_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003381 }
3382 /* Create transaction */
Harald Weltedcaf5652009-07-23 18:56:43 +02003383 trans = trans_alloc(subscr, GSM48_PDISC_CC, 0xff, data->callref);
3384 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003385 DEBUGP(DCC, "No memory for trans.\n");
3386 subscr_put(subscr);
3387 /* Ressource unavailable */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003388 mncc_release_ind(net, NULL, data->callref,
3389 GSM48_CAUSE_LOC_PRN_S_LU,
3390 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003391 return -ENOMEM;
3392 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003393 /* Find lchan */
Harald Welte1a6f7982009-08-09 18:52:33 +02003394 lchan = lchan_for_subscr(subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003395 /* If subscriber has no lchan */
3396 if (!lchan) {
3397 /* find transaction with this subscriber already paging */
3398 llist_for_each_entry(transt, &net->trans_list, entry) {
3399 /* Transaction of our lchan? */
3400 if (transt == trans ||
3401 transt->subscr != subscr)
3402 continue;
3403 DEBUGP(DCC, "(bts %d trx - ts - ti -- sub %s) "
3404 "Received '%s' from MNCC with "
3405 "unallocated channel, paging already "
3406 "started.\n", bts->nr,
3407 data->called.number,
3408 get_mncc_name(msg_type));
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003409 subscr_put(subscr);
3410 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003411 return 0;
3412 }
3413 /* store setup informations until paging was successfull */
Harald Weltedcaf5652009-07-23 18:56:43 +02003414 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
Harald Weltea1b28582009-08-01 19:31:47 +02003415 /* Trigger paging */
3416 paging_request(net, subscr, RSL_CHANNEED_TCH_F,
3417 setup_trig_pag_evt, subscr);
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003418 subscr_put(subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003419 return 0;
3420 }
3421 /* Assign lchan */
3422 trans->lchan = lchan;
3423 use_lchan(lchan);
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003424 subscr_put(subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003425 }
3426 lchan = trans->lchan;
3427
3428 /* if paging did not respond yet */
3429 if (!lchan) {
3430 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3431 "Received '%s' from MNCC in paging state\n",
3432 (trans->subscr)?(trans->subscr->extension):"-",
3433 get_mncc_name(msg_type));
Harald Weltec66b71c2009-06-11 14:23:20 +08003434 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
3435 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003436 if (msg_type == MNCC_REL_REQ)
3437 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
3438 else
3439 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
3440 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02003441 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003442 return rc;
3443 }
3444
3445 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x sub %s) "
3446 "Received '%s' from MNCC in state %d (%s)\n",
3447 lchan->ts->trx->bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
3448 trans->transaction_id,
3449 (lchan->subscr)?(lchan->subscr->extension):"-",
Harald Weltedcaf5652009-07-23 18:56:43 +02003450 get_mncc_name(msg_type), trans->cc.state,
3451 cc_state_names[trans->cc.state]);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003452
3453 /* Find function for current state and message */
3454 for (i = 0; i < DOWNSLLEN; i++)
3455 if ((msg_type == downstatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003456 && ((1 << trans->cc.state) & downstatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003457 break;
3458 if (i == DOWNSLLEN) {
3459 DEBUGP(DCC, "Message unhandled at this state.\n");
3460 return 0;
3461 }
3462
3463 rc = downstatelist[i].rout(trans, arg);
3464
3465 return rc;
3466}
3467
3468
3469static struct datastate {
3470 u_int32_t states;
3471 int type;
3472 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
3473} datastatelist[] = {
3474 /* mobile originating call establishment */
3475 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3476 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
3477 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3478 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
3479 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
3480 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
3481 /* mobile terminating call establishment */
3482 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
3483 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
3484 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
3485 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
3486 {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 */
3487 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
3488 /* signalling during call */
3489 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
3490 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
3491 {SBIT(GSM_CSTATE_ACTIVE),
3492 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
3493 {ALL_STATES,
3494 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
3495 {ALL_STATES,
3496 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
3497 {ALL_STATES,
3498 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
3499 {SBIT(GSM_CSTATE_ACTIVE),
3500 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
3501 {SBIT(GSM_CSTATE_ACTIVE),
3502 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
3503 {SBIT(GSM_CSTATE_ACTIVE),
3504 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
3505 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3506 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
3507 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3508 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
3509 {SBIT(GSM_CSTATE_ACTIVE),
3510 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
3511 /* clearing */
3512 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
3513 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
3514 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
3515 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
3516 {ALL_STATES, /* 5.4.3.4 */
3517 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
3518};
3519
3520#define DATASLLEN \
3521 (sizeof(datastatelist) / sizeof(struct datastate))
3522
Harald Welte4bc90a12008-12-27 16:32:52 +00003523static int gsm0408_rcv_cc(struct msgb *msg)
3524{
3525 struct gsm48_hdr *gh = msgb_l3(msg);
3526 u_int8_t msg_type = gh->msg_type & 0xbf;
Harald Welte6f5aee02009-07-23 21:21:14 +02003527 u_int8_t transaction_id = ((gh->proto_discr & 0xf0) ^ 0x80) >> 4; /* flip */
Harald Welte4bfdfe72009-06-10 23:11:52 +08003528 struct gsm_lchan *lchan = msg->lchan;
Harald Weltedcaf5652009-07-23 18:56:43 +02003529 struct gsm_trans *trans = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003530 int i, rc = 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00003531
Harald Welte4bfdfe72009-06-10 23:11:52 +08003532 if (msg_type & 0x80) {
3533 DEBUGP(DCC, "MSG 0x%2x not defined for PD error\n", msg_type);
3534 return -EINVAL;
Harald Welte4bc90a12008-12-27 16:32:52 +00003535 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003536
3537 /* Find transaction */
Harald Welteb8b40732009-07-23 21:58:40 +02003538 trans = trans_find_by_id(lchan->subscr, GSM48_PDISC_CC, transaction_id);
Harald Weltedcaf5652009-07-23 18:56:43 +02003539
Harald Welte6f5aee02009-07-23 21:21:14 +02003540 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003541 "Received '%s' from MS in state %d (%s)\n",
3542 lchan->ts->trx->bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
3543 transaction_id, (lchan->subscr)?(lchan->subscr->extension):"-",
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +02003544 gsm0408_cc_msg_names[msg_type], trans?(trans->cc.state):0,
Harald Weltedcaf5652009-07-23 18:56:43 +02003545 cc_state_names[trans?(trans->cc.state):0]);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003546
3547 /* Create transaction */
3548 if (!trans) {
Harald Welte6f5aee02009-07-23 21:21:14 +02003549 DEBUGP(DCC, "Unknown transaction ID %x, "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003550 "creating new trans.\n", transaction_id);
3551 /* Create transaction */
Harald Weltedcaf5652009-07-23 18:56:43 +02003552 trans = trans_alloc(lchan->subscr, GSM48_PDISC_CC,
3553 transaction_id, new_callref++);
3554 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003555 DEBUGP(DCC, "No memory for trans.\n");
3556 rc = gsm48_tx_simple(msg->lchan,
Harald Welte6f5aee02009-07-23 21:21:14 +02003557 GSM48_PDISC_CC | (transaction_id << 4),
Harald Welte4bfdfe72009-06-10 23:11:52 +08003558 GSM48_MT_CC_RELEASE_COMPL);
3559 return -ENOMEM;
3560 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003561 /* Assign transaction */
Harald Welte4bfdfe72009-06-10 23:11:52 +08003562 trans->lchan = lchan;
3563 use_lchan(lchan);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003564 }
3565
3566 /* find function for current state and message */
3567 for (i = 0; i < DATASLLEN; i++)
3568 if ((msg_type == datastatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003569 && ((1 << trans->cc.state) & datastatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003570 break;
3571 if (i == DATASLLEN) {
3572 DEBUGP(DCC, "Message unhandled at this state.\n");
3573 return 0;
3574 }
3575
3576 rc = datastatelist[i].rout(trans, msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00003577
3578 return rc;
3579}
3580
Harald Welte52b1f982008-12-23 20:25:15 +00003581/* 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 +02003582int gsm0408_rcvmsg(struct msgb *msg, u_int8_t link_id)
Harald Welte52b1f982008-12-23 20:25:15 +00003583{
3584 struct gsm48_hdr *gh = msgb_l3(msg);
3585 u_int8_t pdisc = gh->proto_discr & 0x0f;
Harald Welte8470bf22008-12-25 23:28:35 +00003586 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00003587
3588 switch (pdisc) {
3589 case GSM48_PDISC_CC:
3590 rc = gsm0408_rcv_cc(msg);
3591 break;
3592 case GSM48_PDISC_MM:
3593 rc = gsm0408_rcv_mm(msg);
3594 break;
3595 case GSM48_PDISC_RR:
3596 rc = gsm0408_rcv_rr(msg);
3597 break;
Harald Weltebcae43f2008-12-27 21:45:37 +00003598 case GSM48_PDISC_SMS:
Harald Welte (local)daef6062009-08-14 11:41:12 +02003599 rc = gsm0411_rcv_sms(msg, link_id);
Harald Weltebcae43f2008-12-27 21:45:37 +00003600 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003601 case GSM48_PDISC_MM_GPRS:
Harald Weltebcae43f2008-12-27 21:45:37 +00003602 case GSM48_PDISC_SM_GPRS:
Mike Haben59d284e2009-09-26 19:00:03 +02003603 fprintf(stderr, "Unimplemented GSM 04.08 discriminator 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +00003604 pdisc);
3605 break;
Harald Welte6eafe912009-10-16 08:32:58 +02003606 case GSM48_PDISC_NC_SS:
3607 rc = handle_rcv_ussd(msg);
3608 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003609 default:
Mike Haben59d284e2009-09-26 19:00:03 +02003610 fprintf(stderr, "Unknown GSM 04.08 discriminator 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +00003611 pdisc);
3612 break;
3613 }
3614
3615 return rc;
3616}
Harald Welte8470bf22008-12-25 23:28:35 +00003617
Harald Welte4bfdfe72009-06-10 23:11:52 +08003618/* dequeue messages to layer 4 */
3619int bsc_upqueue(struct gsm_network *net)
3620{
3621 struct gsm_mncc *mncc;
3622 struct msgb *msg;
3623 int work = 0;
3624
3625 if (net)
3626 while ((msg = msgb_dequeue(&net->upqueue))) {
3627 mncc = (struct gsm_mncc *)msg->data;
3628 if (net->mncc_recv)
3629 net->mncc_recv(net, mncc->msg_type, mncc);
3630 work = 1; /* work done */
Harald Welte316c8252009-06-26 19:40:48 +02003631 talloc_free(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003632 }
3633
3634 return work;
3635}
Harald Weltedcaf5652009-07-23 18:56:43 +02003636
Harald Welte805f6442009-07-28 18:25:29 +02003637/*
3638 * This will be ran by the linker when loading the DSO. We use it to
3639 * do system initialization, e.g. registration of signal handlers.
3640 */
3641static __attribute__((constructor)) void on_dso_load_0408(void)
3642{
Harald Welte805f6442009-07-28 18:25:29 +02003643 register_signal_handler(SS_LCHAN, gsm0408_handle_lchan_signal, NULL);
3644 register_signal_handler(SS_ABISIP, handle_abisip_signal, NULL);
3645}