blob: a6ed3e83e711ef1213886ff75da5e667de411daa [file] [log] [blame]
Jonathan Santos03fd8d02011-05-25 13:54:02 -04001/* 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
4/* (C) 2008-2009 by Harald Welte <laforge@gnumonks.org>
5 * (C) 2008-2010 by Holger Hans Peter Freyther <zecke@selfish.org>
6 *
7 * 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 Affero General Public License as published by
11 * the Free Software Foundation; either version 3 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 Affero General Public License for more details.
18 *
19 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 *
22 */
23
24
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <errno.h>
29#include <time.h>
30#include <netinet/in.h>
31
32#include <openbsc/auth.h>
33#include <openbsc/db.h>
34#include <openbsc/debug.h>
35#include <openbsc/gsm_data.h>
36#include <openbsc/gsm_subscriber.h>
37#include <openbsc/gsm_04_11.h>
38#include <openbsc/gsm_04_08.h>
39#include <openbsc/gsm_04_80.h>
40#include <openbsc/abis_rsl.h>
41#include <openbsc/chan_alloc.h>
42#include <openbsc/paging.h>
43#include <openbsc/signal.h>
44#include <openbsc/trau_frame.h>
45#include <openbsc/trau_mux.h>
46#include <openbsc/rtp_proxy.h>
47#include <openbsc/transaction.h>
48#include <openbsc/ussd.h>
49#include <openbsc/silent_call.h>
50#include <openbsc/bsc_api.h>
51#include <openbsc/osmo_msc.h>
Jonathan Santos5a45b152011-08-17 15:33:57 -040052#include <osmocom/core/bitvec.h>
Jonathan Santos03fd8d02011-05-25 13:54:02 -040053
Jonathan Santos5a45b152011-08-17 15:33:57 -040054#include <osmocom/gsm/gsm48.h>
55#include <osmocom/gsm/gsm0480.h>
56#include <osmocom/gsm/gsm_utils.h>
57#include <osmocom/core/msgb.h>
58#include <osmocom/core/talloc.h>
59#include <osmocom/gsm/tlv.h>
Jonathan Santos03fd8d02011-05-25 13:54:02 -040060
61void *tall_locop_ctx;
62void *tall_authciphop_ctx;
63
Jonathan Santos5a45b152011-08-17 15:33:57 -040064int gsm0408_loc_upd_acc(struct gsm_subscriber_connection *conn, uint32_t tmsi);
Jonathan Santos03fd8d02011-05-25 13:54:02 -040065static int gsm48_tx_simple(struct gsm_subscriber_connection *conn,
Jonathan Santos5a45b152011-08-17 15:33:57 -040066 uint8_t pdisc, uint8_t msg_type);
Jonathan Santos03fd8d02011-05-25 13:54:02 -040067static void schedule_reject(struct gsm_subscriber_connection *conn);
68static void release_anchor(struct gsm_subscriber_connection *conn);
69
70struct gsm_lai {
Jonathan Santos5a45b152011-08-17 15:33:57 -040071 uint16_t mcc;
72 uint16_t mnc;
73 uint16_t lac;
Jonathan Santos03fd8d02011-05-25 13:54:02 -040074};
75
Jonathan Santos5a45b152011-08-17 15:33:57 -040076static uint32_t new_callref = 0x80000001;
Jonathan Santos03fd8d02011-05-25 13:54:02 -040077
78void cc_tx_to_mncc(struct gsm_network *net, struct msgb *msg)
79{
80 net->mncc_recv(net, msg);
81}
82
83static int gsm48_conn_sendmsg(struct msgb *msg, struct gsm_subscriber_connection *conn,
84 struct gsm_trans *trans)
85{
86 struct gsm48_hdr *gh = (struct gsm48_hdr *) msg->data;
87
88 /* if we get passed a transaction reference, do some common
89 * work that the caller no longer has to do */
90 if (trans) {
91 gh->proto_discr = trans->protocol | (trans->transaction_id << 4);
92 msg->lchan = trans->conn->lchan;
93 }
94
95
96 if (msg->lchan) {
97 msg->trx = msg->lchan->ts->trx;
98 if ((gh->proto_discr & GSM48_PDISC_MASK) == GSM48_PDISC_CC)
99 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x) "
100 "Sending '%s' to MS.\n", msg->trx->bts->nr,
101 msg->trx->nr, msg->lchan->ts->nr,
102 gh->proto_discr & 0xf0,
103 gsm48_cc_msg_name(gh->msg_type));
104 else
105 DEBUGP(DCC, "(bts %d trx %d ts %d pd %02x) "
106 "Sending 0x%02x to MS.\n", msg->trx->bts->nr,
107 msg->trx->nr, msg->lchan->ts->nr,
108 gh->proto_discr, gh->msg_type);
109 }
110
111 return gsm0808_submit_dtap(conn, msg, 0, 0);
112}
113
114int gsm48_cc_tx_notify_ss(struct gsm_trans *trans, const char *message)
115{
116 struct gsm48_hdr *gh;
117 struct msgb *ss_notify;
118
119 ss_notify = gsm0480_create_notifySS(message);
120 if (!ss_notify)
121 return -1;
122
123 gsm0480_wrap_invoke(ss_notify, GSM0480_OP_CODE_NOTIFY_SS, 0);
124 uint8_t *data = msgb_push(ss_notify, 1);
125 data[0] = ss_notify->len - 1;
126 gh = (struct gsm48_hdr *) msgb_push(ss_notify, sizeof(*gh));
127 gh->msg_type = GSM48_MT_CC_FACILITY;
128 return gsm48_conn_sendmsg(ss_notify, trans->conn, trans);
129}
130
131static void release_security_operation(struct gsm_subscriber_connection *conn)
132{
133 if (!conn->sec_operation)
134 return;
135
136 talloc_free(conn->sec_operation);
137 conn->sec_operation = NULL;
138 msc_release_connection(conn);
139}
140
141static void allocate_security_operation(struct gsm_subscriber_connection *conn)
142{
143 conn->sec_operation = talloc_zero(tall_authciphop_ctx,
144 struct gsm_security_operation);
145}
146
147int gsm48_secure_channel(struct gsm_subscriber_connection *conn, int key_seq,
148 gsm_cbfn *cb, void *cb_data)
149{
150 struct gsm_network *net = conn->bts->network;
151 struct gsm_subscriber *subscr = conn->subscr;
152 struct gsm_security_operation *op;
153 struct gsm_auth_tuple atuple;
154 int status = -1, rc;
155
156 /* Check if we _can_ enable encryption. Cases where we can't:
157 * - Encryption disabled in config
158 * - Channel already secured (nothing to do)
159 * - Subscriber equipment doesn't support configured encryption
160 */
161 if (!net->a5_encryption) {
162 status = GSM_SECURITY_NOAVAIL;
163 } else if (conn->lchan->encr.alg_id > RSL_ENC_ALG_A5(0)) {
164 DEBUGP(DMM, "Requesting to secure an already secure channel");
165 status = GSM_SECURITY_SUCCEEDED;
166 } else if (!ms_cm2_a5n_support(subscr->equipment.classmark2,
167 net->a5_encryption)) {
168 DEBUGP(DMM, "Subscriber equipment doesn't support requested encryption");
169 status = GSM_SECURITY_NOAVAIL;
170 }
171
172 /* If not done yet, try to get info for this user */
173 if (status < 0) {
174 rc = auth_get_tuple_for_subscr(&atuple, subscr, key_seq);
175 if (rc <= 0)
176 status = GSM_SECURITY_NOAVAIL;
177 }
178
179 /* Are we done yet ? */
180 if (status >= 0)
181 return cb ?
182 cb(GSM_HOOK_RR_SECURITY, status, NULL, conn, cb_data) :
183 0;
184
185 /* Start an operation (can't have more than one pending !!!) */
186 if (conn->sec_operation)
187 return -EBUSY;
188
189 allocate_security_operation(conn);
190 op = conn->sec_operation;
191 op->cb = cb;
192 op->cb_data = cb_data;
193 memcpy(&op->atuple, &atuple, sizeof(struct gsm_auth_tuple));
194
195 /* FIXME: Should start a timer for completion ... */
196
197 /* Then do whatever is needed ... */
198 if (rc == AUTH_DO_AUTH_THAN_CIPH) {
199 /* Start authentication */
200 return gsm48_tx_mm_auth_req(conn, op->atuple.rand, op->atuple.key_seq);
201 } else if (rc == AUTH_DO_CIPH) {
202 /* Start ciphering directly */
203 return gsm0808_cipher_mode(conn, net->a5_encryption,
204 op->atuple.kc, 8, 0);
205 }
206
207 return -EINVAL; /* not reached */
208}
209
210static int authorize_subscriber(struct gsm_loc_updating_operation *loc,
211 struct gsm_subscriber *subscriber)
212{
213 if (!subscriber)
214 return 0;
215
216 /*
217 * Do not send accept yet as more information should arrive. Some
218 * phones will not send us the information and we will have to check
219 * what we want to do with that.
220 */
221 if (loc && (loc->waiting_for_imsi || loc->waiting_for_imei))
222 return 0;
223
224 switch (subscriber->net->auth_policy) {
225 case GSM_AUTH_POLICY_CLOSED:
226 return subscriber->authorized;
227 case GSM_AUTH_POLICY_TOKEN:
228 if (subscriber->authorized)
229 return subscriber->authorized;
230 return (subscriber->flags & GSM_SUBSCRIBER_FIRST_CONTACT);
231 case GSM_AUTH_POLICY_ACCEPT_ALL:
232 return 1;
233 default:
234 return 0;
235 }
236}
237
238static void release_loc_updating_req(struct gsm_subscriber_connection *conn)
239{
240 if (!conn->loc_operation)
241 return;
242
243 /* No need to keep the connection up */
244 release_anchor(conn);
245
Jonathan Santos5a45b152011-08-17 15:33:57 -0400246 osmo_timer_del(&conn->loc_operation->updating_timer);
Jonathan Santos03fd8d02011-05-25 13:54:02 -0400247 talloc_free(conn->loc_operation);
248 conn->loc_operation = NULL;
249 msc_release_connection(conn);
250}
251
252static void allocate_loc_updating_req(struct gsm_subscriber_connection *conn)
253{
254 if (conn->loc_operation)
255 LOGP(DMM, LOGL_ERROR, "Connection already had operation.\n");
256 release_loc_updating_req(conn);
257
258 conn->loc_operation = talloc_zero(tall_locop_ctx,
259 struct gsm_loc_updating_operation);
260}
261
262static int _gsm0408_authorize_sec_cb(unsigned int hooknum, unsigned int event,
263 struct msgb *msg, void *data, void *param)
264{
265 struct gsm_subscriber_connection *conn = data;
266 int rc = 0;
267
268 switch (event) {
269 case GSM_SECURITY_AUTH_FAILED:
270 release_loc_updating_req(conn);
271 break;
272
273 case GSM_SECURITY_NOAVAIL:
274 case GSM_SECURITY_SUCCEEDED:
275 /* We're all good */
276 db_subscriber_alloc_tmsi(conn->subscr);
277 rc = gsm0408_loc_upd_acc(conn, conn->subscr->tmsi);
278 if (conn->bts->network->send_mm_info) {
279 /* send MM INFO with network name */
280 rc = gsm48_tx_mm_info(conn);
281 }
282
283 /* call subscr_update after putting the loc_upd_acc
284 * in the transmit queue, since S_SUBSCR_ATTACHED might
285 * trigger further action like SMS delivery */
286 subscr_update(conn->subscr, conn->bts,
287 GSM_SUBSCRIBER_UPDATE_ATTACHED);
288
289 /*
290 * The gsm0408_loc_upd_acc sends a MI with the TMSI. The
291 * MS needs to respond with a TMSI REALLOCATION COMPLETE
292 * (even if the TMSI is the same).
293 */
294 break;
295
296 default:
297 rc = -EINVAL;
298 };
299
300 return rc;
301}
302
303static int gsm0408_authorize(struct gsm_subscriber_connection *conn, struct msgb *msg)
304{
305 if (authorize_subscriber(conn->loc_operation, conn->subscr))
306 return gsm48_secure_channel(conn,
307 conn->loc_operation->key_seq,
308 _gsm0408_authorize_sec_cb, NULL);
309 return 0;
310}
311
312void gsm0408_clear_request(struct gsm_subscriber_connection *conn, uint32_t cause)
313{
314 struct gsm_trans *trans, *temp;
315
316 /* avoid someone issuing a clear */
317 conn->in_release = 1;
318
319 /*
320 * Cancel any outstanding location updating request
321 * operation taking place on the subscriber connection.
322 */
323 release_loc_updating_req(conn);
324
325 /* We might need to cancel the paging response or such. */
326 if (conn->sec_operation && conn->sec_operation->cb) {
327 conn->sec_operation->cb(GSM_HOOK_RR_SECURITY, GSM_SECURITY_AUTH_FAILED,
328 NULL, conn, conn->sec_operation->cb_data);
329 }
330
331 release_security_operation(conn);
332 release_anchor(conn);
333
334 /* Free all transactions that are associated with the released lchan */
335 /* FIXME: this is not neccessarily the right thing to do, we should
336 * only set trans->lchan to NULL and wait for another lchan to be
337 * established to the same MM entity (phone/subscriber) */
338 llist_for_each_entry_safe(trans, temp, &conn->bts->network->trans_list, entry) {
339 if (trans->conn == conn)
340 trans_free(trans);
341 }
342}
343
344void gsm0408_clear_all_trans(struct gsm_network *net, int protocol)
345{
346 struct gsm_trans *trans, *temp;
347
348 LOGP(DCC, LOGL_NOTICE, "Clearing all currently active transactions!!!\n");
349
350 llist_for_each_entry_safe(trans, temp, &net->trans_list, entry) {
351 if (trans->protocol == protocol) {
352 trans->callref = 0;
353 trans_free(trans);
354 }
355 }
356}
357
358/* Chapter 9.2.14 : Send LOCATION UPDATING REJECT */
Jonathan Santos5a45b152011-08-17 15:33:57 -0400359int gsm0408_loc_upd_rej(struct gsm_subscriber_connection *conn, uint8_t cause)
Jonathan Santos03fd8d02011-05-25 13:54:02 -0400360{
361 struct gsm_bts *bts = conn->bts;
362 struct msgb *msg;
363
Jonathan Santos5a45b152011-08-17 15:33:57 -0400364 osmo_counter_inc(bts->network->stats.loc_upd_resp.reject);
Jonathan Santos03fd8d02011-05-25 13:54:02 -0400365
366 msg = gsm48_create_loc_upd_rej(cause);
367 if (!msg) {
368 LOGP(DMM, LOGL_ERROR, "Failed to create msg for LOCATION UPDATING REJECT.\n");
369 return -1;
370 }
371
372 msg->lchan = conn->lchan;
373
374 LOGP(DMM, LOGL_INFO, "Subscriber %s: LOCATION UPDATING REJECT "
375 "LAC=%u BTS=%u\n", conn->subscr ?
376 subscr_name(conn->subscr) : "unknown",
377 bts->location_area_code, bts->nr);
378
379 return gsm48_conn_sendmsg(msg, conn, NULL);
380}
381
382/* Chapter 9.2.13 : Send LOCATION UPDATE ACCEPT */
Jonathan Santos5a45b152011-08-17 15:33:57 -0400383int gsm0408_loc_upd_acc(struct gsm_subscriber_connection *conn, uint32_t tmsi)
Jonathan Santos03fd8d02011-05-25 13:54:02 -0400384{
385 struct gsm_bts *bts = conn->bts;
386 struct msgb *msg = gsm48_msgb_alloc();
387 struct gsm48_hdr *gh;
388 struct gsm48_loc_area_id *lai;
Jonathan Santos5a45b152011-08-17 15:33:57 -0400389 uint8_t *mid;
Jonathan Santos03fd8d02011-05-25 13:54:02 -0400390
391 msg->lchan = conn->lchan;
392
393 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
394 gh->proto_discr = GSM48_PDISC_MM;
395 gh->msg_type = GSM48_MT_MM_LOC_UPD_ACCEPT;
396
397 lai = (struct gsm48_loc_area_id *) msgb_put(msg, sizeof(*lai));
398 gsm48_generate_lai(lai, bts->network->country_code,
399 bts->network->network_code, bts->location_area_code);
400
401 mid = msgb_put(msg, GSM48_MID_TMSI_LEN);
402 gsm48_generate_mid_from_tmsi(mid, tmsi);
403
404 DEBUGP(DMM, "-> LOCATION UPDATE ACCEPT\n");
405
Jonathan Santos5a45b152011-08-17 15:33:57 -0400406 osmo_counter_inc(bts->network->stats.loc_upd_resp.accept);
Jonathan Santos03fd8d02011-05-25 13:54:02 -0400407
408 return gsm48_conn_sendmsg(msg, conn, NULL);
409}
410
411/* Transmit Chapter 9.2.10 Identity Request */
Jonathan Santos5a45b152011-08-17 15:33:57 -0400412static int mm_tx_identity_req(struct gsm_subscriber_connection *conn, uint8_t id_type)
Jonathan Santos03fd8d02011-05-25 13:54:02 -0400413{
414 struct msgb *msg = gsm48_msgb_alloc();
415 struct gsm48_hdr *gh;
416
417 msg->lchan = conn->lchan;
418
419 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
420 gh->proto_discr = GSM48_PDISC_MM;
421 gh->msg_type = GSM48_MT_MM_ID_REQ;
422 gh->data[0] = id_type;
423
424 return gsm48_conn_sendmsg(msg, conn, NULL);
425}
426
427
428/* Parse Chapter 9.2.11 Identity Response */
429static int mm_rx_id_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
430{
431 struct gsm48_hdr *gh = msgb_l3(msg);
432 struct gsm_lchan *lchan = msg->lchan;
433 struct gsm_bts *bts = lchan->ts->trx->bts;
434 struct gsm_network *net = bts->network;
Jonathan Santos5a45b152011-08-17 15:33:57 -0400435 uint8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
Jonathan Santos03fd8d02011-05-25 13:54:02 -0400436 char mi_string[GSM48_MI_SIZE];
437
438 gsm48_mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
439 DEBUGP(DMM, "IDENTITY RESPONSE: mi_type=0x%02x MI(%s)\n",
440 mi_type, mi_string);
441
Jonathan Santos5a45b152011-08-17 15:33:57 -0400442 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, gh->data);
Jonathan Santos03fd8d02011-05-25 13:54:02 -0400443
444 switch (mi_type) {
445 case GSM_MI_TYPE_IMSI:
446 /* look up subscriber based on IMSI, create if not found */
447 if (!conn->subscr) {
448 conn->subscr = subscr_get_by_imsi(net, mi_string);
449 if (!conn->subscr)
450 conn->subscr = db_create_subscriber(net, mi_string);
451 }
452 if (conn->loc_operation)
453 conn->loc_operation->waiting_for_imsi = 0;
454 break;
455 case GSM_MI_TYPE_IMEI:
456 case GSM_MI_TYPE_IMEISV:
457 /* update subscribe <-> IMEI mapping */
458 if (conn->subscr) {
459 db_subscriber_assoc_imei(conn->subscr, mi_string);
460 db_sync_equipment(&conn->subscr->equipment);
461 }
462 if (conn->loc_operation)
463 conn->loc_operation->waiting_for_imei = 0;
464 break;
465 }
466
467 /* Check if we can let the mobile station enter */
468 return gsm0408_authorize(conn, msg);
469}
470
471
472static void loc_upd_rej_cb(void *data)
473{
474 struct gsm_subscriber_connection *conn = data;
475 struct gsm_lchan *lchan = conn->lchan;
476 struct gsm_bts *bts = lchan->ts->trx->bts;
477
478 gsm0408_loc_upd_rej(conn, bts->network->reject_cause);
479 release_loc_updating_req(conn);
480}
481
482static void schedule_reject(struct gsm_subscriber_connection *conn)
483{
484 conn->loc_operation->updating_timer.cb = loc_upd_rej_cb;
485 conn->loc_operation->updating_timer.data = conn;
Jonathan Santos5a45b152011-08-17 15:33:57 -0400486 osmo_timer_schedule(&conn->loc_operation->updating_timer, 5, 0);
Jonathan Santos03fd8d02011-05-25 13:54:02 -0400487}
488
Jonathan Santos5a45b152011-08-17 15:33:57 -0400489static const char *lupd_name(uint8_t type)
Jonathan Santos03fd8d02011-05-25 13:54:02 -0400490{
491 switch (type) {
492 case GSM48_LUPD_NORMAL:
493 return "NORMAL";
494 case GSM48_LUPD_PERIODIC:
495 return "PEROIDOC";
496 case GSM48_LUPD_IMSI_ATT:
497 return "IMSI ATTACH";
498 default:
499 return "UNKNOWN";
500 }
501}
502
503/* Chapter 9.2.15: Receive Location Updating Request */
504static int mm_rx_loc_upd_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
505{
506 struct gsm48_hdr *gh = msgb_l3(msg);
507 struct gsm48_loc_upd_req *lu;
508 struct gsm_subscriber *subscr = NULL;
509 struct gsm_bts *bts = conn->bts;
Jonathan Santos5a45b152011-08-17 15:33:57 -0400510 uint8_t mi_type;
Jonathan Santos03fd8d02011-05-25 13:54:02 -0400511 char mi_string[GSM48_MI_SIZE];
512 int rc;
513
514 lu = (struct gsm48_loc_upd_req *) gh->data;
515
516 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
517
518 gsm48_mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
519
520 DEBUGPC(DMM, "mi_type=0x%02x MI(%s) type=%s ", mi_type, mi_string,
521 lupd_name(lu->type));
522
Jonathan Santos5a45b152011-08-17 15:33:57 -0400523 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, &lu->mi_len);
Jonathan Santos03fd8d02011-05-25 13:54:02 -0400524
525 switch (lu->type) {
526 case GSM48_LUPD_NORMAL:
Jonathan Santos5a45b152011-08-17 15:33:57 -0400527 osmo_counter_inc(bts->network->stats.loc_upd_type.normal);
Jonathan Santos03fd8d02011-05-25 13:54:02 -0400528 break;
529 case GSM48_LUPD_IMSI_ATT:
Jonathan Santos5a45b152011-08-17 15:33:57 -0400530 osmo_counter_inc(bts->network->stats.loc_upd_type.attach);
Jonathan Santos03fd8d02011-05-25 13:54:02 -0400531 break;
532 case GSM48_LUPD_PERIODIC:
Jonathan Santos5a45b152011-08-17 15:33:57 -0400533 osmo_counter_inc(bts->network->stats.loc_upd_type.periodic);
Jonathan Santos03fd8d02011-05-25 13:54:02 -0400534 break;
535 }
536
537 /*
538 * Pseudo Spoof detection: Just drop a second/concurrent
539 * location updating request.
540 */
541 if (conn->loc_operation) {
542 DEBUGPC(DMM, "ignoring request due an existing one: %p.\n",
543 conn->loc_operation);
544 gsm0408_loc_upd_rej(conn, GSM48_REJECT_PROTOCOL_ERROR);
545 return 0;
546 }
547
548 allocate_loc_updating_req(conn);
549
550 conn->loc_operation->key_seq = lu->key_seq;
551
552 switch (mi_type) {
553 case GSM_MI_TYPE_IMSI:
554 DEBUGPC(DMM, "\n");
555 /* we always want the IMEI, too */
556 rc = mm_tx_identity_req(conn, GSM_MI_TYPE_IMEI);
557 conn->loc_operation->waiting_for_imei = 1;
558
559 /* look up subscriber based on IMSI, create if not found */
560 subscr = subscr_get_by_imsi(bts->network, mi_string);
561 if (!subscr) {
562 subscr = db_create_subscriber(bts->network, mi_string);
563 }
564 break;
565 case GSM_MI_TYPE_TMSI:
566 DEBUGPC(DMM, "\n");
567 /* look up the subscriber based on TMSI, request IMSI if it fails */
568 subscr = subscr_get_by_tmsi(bts->network,
569 tmsi_from_string(mi_string));
570 if (!subscr) {
571 /* send IDENTITY REQUEST message to get IMSI */
572 rc = mm_tx_identity_req(conn, GSM_MI_TYPE_IMSI);
573 conn->loc_operation->waiting_for_imsi = 1;
574 }
575 /* we always want the IMEI, too */
576 rc = mm_tx_identity_req(conn, GSM_MI_TYPE_IMEI);
577 conn->loc_operation->waiting_for_imei = 1;
578 break;
579 case GSM_MI_TYPE_IMEI:
580 case GSM_MI_TYPE_IMEISV:
581 /* no sim card... FIXME: what to do ? */
582 DEBUGPC(DMM, "unimplemented mobile identity type\n");
583 break;
584 default:
585 DEBUGPC(DMM, "unknown mobile identity type\n");
586 break;
587 }
588
589 /* schedule the reject timer */
590 schedule_reject(conn);
591
592 if (!subscr) {
593 DEBUGPC(DRR, "<- Can't find any subscriber for this ID\n");
594 /* FIXME: request id? close channel? */
595 return -EINVAL;
596 }
597
598 conn->subscr = subscr;
599 conn->subscr->equipment.classmark1 = lu->classmark1;
600
601 /* check if we can let the subscriber into our network immediately
602 * or if we need to wait for identity responses. */
603 return gsm0408_authorize(conn, msg);
604}
605
606#if 0
Jonathan Santos5a45b152011-08-17 15:33:57 -0400607static uint8_t to_bcd8(uint8_t val)
Jonathan Santos03fd8d02011-05-25 13:54:02 -0400608{
609 return ((val / 10) << 4) | (val % 10);
610}
611#endif
612
613/* Section 9.2.15a */
614int gsm48_tx_mm_info(struct gsm_subscriber_connection *conn)
615{
616 struct msgb *msg = gsm48_msgb_alloc();
617 struct gsm48_hdr *gh;
618 struct gsm_network *net = conn->bts->network;
Jonathan Santos5a45b152011-08-17 15:33:57 -0400619 uint8_t *ptr8;
Jonathan Santos03fd8d02011-05-25 13:54:02 -0400620 int name_len, name_pad;
621#if 0
622 time_t cur_t;
623 struct tm* cur_time;
624 int tz15min;
625#endif
626
627 msg->lchan = conn->lchan;
628
629 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
630 gh->proto_discr = GSM48_PDISC_MM;
631 gh->msg_type = GSM48_MT_MM_INFO;
632
633 if (net->name_long) {
634#if 0
635 name_len = strlen(net->name_long);
636 /* 10.5.3.5a */
637 ptr8 = msgb_put(msg, 3);
638 ptr8[0] = GSM48_IE_NAME_LONG;
639 ptr8[1] = name_len*2 +1;
640 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
641
Jonathan Santos5a45b152011-08-17 15:33:57 -0400642 ptr16 = (uint16_t *) msgb_put(msg, name_len*2);
Jonathan Santos03fd8d02011-05-25 13:54:02 -0400643 for (i = 0; i < name_len; i++)
644 ptr16[i] = htons(net->name_long[i]);
645
646 /* FIXME: Use Cell Broadcast, not UCS-2, since
647 * UCS-2 is only supported by later revisions of the spec */
648#endif
649 name_len = (strlen(net->name_long)*7)/8;
650 name_pad = (8 - strlen(net->name_long)*7)%8;
651 if (name_pad > 0)
652 name_len++;
653 /* 10.5.3.5a */
654 ptr8 = msgb_put(msg, 3);
655 ptr8[0] = GSM48_IE_NAME_LONG;
656 ptr8[1] = name_len +1;
657 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
658
659 ptr8 = msgb_put(msg, name_len);
660 gsm_7bit_encode(ptr8, net->name_long);
661
662 }
663
664 if (net->name_short) {
665#if 0
666 name_len = strlen(net->name_short);
667 /* 10.5.3.5a */
Jonathan Santos5a45b152011-08-17 15:33:57 -0400668 ptr8 = (uint8_t *) msgb_put(msg, 3);
Jonathan Santos03fd8d02011-05-25 13:54:02 -0400669 ptr8[0] = GSM48_IE_NAME_SHORT;
670 ptr8[1] = name_len*2 + 1;
671 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
672
Jonathan Santos5a45b152011-08-17 15:33:57 -0400673 ptr16 = (uint16_t *) msgb_put(msg, name_len*2);
Jonathan Santos03fd8d02011-05-25 13:54:02 -0400674 for (i = 0; i < name_len; i++)
675 ptr16[i] = htons(net->name_short[i]);
676#endif
677 name_len = (strlen(net->name_short)*7)/8;
678 name_pad = (8 - strlen(net->name_short)*7)%8;
679 if (name_pad > 0)
680 name_len++;
681 /* 10.5.3.5a */
Jonathan Santos5a45b152011-08-17 15:33:57 -0400682 ptr8 = (uint8_t *) msgb_put(msg, 3);
Jonathan Santos03fd8d02011-05-25 13:54:02 -0400683 ptr8[0] = GSM48_IE_NAME_SHORT;
684 ptr8[1] = name_len +1;
685 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
686
687 ptr8 = msgb_put(msg, name_len);
688 gsm_7bit_encode(ptr8, net->name_short);
689
690 }
691
692#if 0
693 /* Section 10.5.3.9 */
694 cur_t = time(NULL);
695 cur_time = gmtime(&cur_t);
696 ptr8 = msgb_put(msg, 8);
697 ptr8[0] = GSM48_IE_NET_TIME_TZ;
698 ptr8[1] = to_bcd8(cur_time->tm_year % 100);
699 ptr8[2] = to_bcd8(cur_time->tm_mon);
700 ptr8[3] = to_bcd8(cur_time->tm_mday);
701 ptr8[4] = to_bcd8(cur_time->tm_hour);
702 ptr8[5] = to_bcd8(cur_time->tm_min);
703 ptr8[6] = to_bcd8(cur_time->tm_sec);
704 /* 02.42: coded as BCD encoded signed value in units of 15 minutes */
705 tz15min = (cur_time->tm_gmtoff)/(60*15);
706 ptr8[7] = to_bcd8(tz15min);
707 if (tz15min < 0)
708 ptr8[7] |= 0x80;
709#endif
710
711 DEBUGP(DMM, "-> MM INFO\n");
712
713 return gsm48_conn_sendmsg(msg, conn, NULL);
714}
715
716/* Section 9.2.2 */
Jonathan Santos5a45b152011-08-17 15:33:57 -0400717int gsm48_tx_mm_auth_req(struct gsm_subscriber_connection *conn, uint8_t *rand, int key_seq)
Jonathan Santos03fd8d02011-05-25 13:54:02 -0400718{
719 struct msgb *msg = gsm48_msgb_alloc();
720 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
721 struct gsm48_auth_req *ar = (struct gsm48_auth_req *) msgb_put(msg, sizeof(*ar));
722
Jonathan Santos5a45b152011-08-17 15:33:57 -0400723 DEBUGP(DMM, "-> AUTH REQ (rand = %s)\n", osmo_hexdump(rand, 16));
Jonathan Santos03fd8d02011-05-25 13:54:02 -0400724
725 msg->lchan = conn->lchan;
726 gh->proto_discr = GSM48_PDISC_MM;
727 gh->msg_type = GSM48_MT_MM_AUTH_REQ;
728
729 ar->key_seq = key_seq;
730
731 /* 16 bytes RAND parameters */
732 if (rand)
733 memcpy(ar->rand, rand, 16);
734
735 return gsm48_conn_sendmsg(msg, conn, NULL);
736}
737
738/* Section 9.2.1 */
739int gsm48_tx_mm_auth_rej(struct gsm_subscriber_connection *conn)
740{
741 DEBUGP(DMM, "-> AUTH REJECT\n");
742 return gsm48_tx_simple(conn, GSM48_PDISC_MM, GSM48_MT_MM_AUTH_REJ);
743}
744
745static int gsm48_tx_mm_serv_ack(struct gsm_subscriber_connection *conn)
746{
747 DEBUGP(DMM, "-> CM SERVICE ACK\n");
748 return gsm48_tx_simple(conn, GSM48_PDISC_MM, GSM48_MT_MM_CM_SERV_ACC);
749}
750
751/* 9.2.6 CM service reject */
752static int gsm48_tx_mm_serv_rej(struct gsm_subscriber_connection *conn,
753 enum gsm48_reject_value value)
754{
755 struct msgb *msg;
756
757 msg = gsm48_create_mm_serv_rej(value);
758 if (!msg) {
759 LOGP(DMM, LOGL_ERROR, "Failed to allocate CM Service Reject.\n");
760 return -1;
761 }
762
763 DEBUGP(DMM, "-> CM SERVICE Reject cause: %d\n", value);
764 msg->lchan = conn->lchan;
765 return gsm48_conn_sendmsg(msg, conn, NULL);
766}
767
768static int _gsm48_rx_mm_serv_req_sec_cb(
769 unsigned int hooknum, unsigned int event,
770 struct msgb *msg, void *data, void *param)
771{
772 struct gsm_subscriber_connection *conn = data;
773 int rc = 0;
774
775 switch (event) {
776 case GSM_SECURITY_AUTH_FAILED:
777 /* Nothing to do */
778 break;
779
780 case GSM_SECURITY_NOAVAIL:
781 rc = gsm48_tx_mm_serv_ack(conn);
782 break;
783
784 case GSM_SECURITY_SUCCEEDED:
785 /* nothing to do. CIPHER MODE COMMAND is
786 * implicit CM SERV ACK */
787 break;
788
789 default:
790 rc = -EINVAL;
791 };
792
793 return rc;
794}
795
796/*
797 * Handle CM Service Requests
798 * a) Verify that the packet is long enough to contain the information
799 * we require otherwsie reject with INCORRECT_MESSAGE
800 * b) Try to parse the TMSI. If we do not have one reject
801 * c) Check that we know the subscriber with the TMSI otherwise reject
802 * with a HLR cause
803 * d) Set the subscriber on the gsm_lchan and accept
804 */
805static int gsm48_rx_mm_serv_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
806{
Jonathan Santos5a45b152011-08-17 15:33:57 -0400807 uint8_t mi_type;
Jonathan Santos03fd8d02011-05-25 13:54:02 -0400808 char mi_string[GSM48_MI_SIZE];
809
810 struct gsm_bts *bts = conn->bts;
811 struct gsm_subscriber *subscr;
812 struct gsm48_hdr *gh = msgb_l3(msg);
813 struct gsm48_service_request *req =
814 (struct gsm48_service_request *)gh->data;
815 /* unfortunately in Phase1 the classmark2 length is variable */
Jonathan Santos5a45b152011-08-17 15:33:57 -0400816 uint8_t classmark2_len = gh->data[1];
817 uint8_t *classmark2 = gh->data+2;
818 uint8_t mi_len = *(classmark2 + classmark2_len);
819 uint8_t *mi = (classmark2 + classmark2_len + 1);
Jonathan Santos03fd8d02011-05-25 13:54:02 -0400820
821 DEBUGP(DMM, "<- CM SERVICE REQUEST ");
822 if (msg->data_len < sizeof(struct gsm48_service_request*)) {
823 DEBUGPC(DMM, "wrong sized message\n");
824 return gsm48_tx_mm_serv_rej(conn,
825 GSM48_REJECT_INCORRECT_MESSAGE);
826 }
827
828 if (msg->data_len < req->mi_len + 6) {
829 DEBUGPC(DMM, "does not fit in packet\n");
830 return gsm48_tx_mm_serv_rej(conn,
831 GSM48_REJECT_INCORRECT_MESSAGE);
832 }
833
834 mi_type = mi[0] & GSM_MI_TYPE_MASK;
835 if (mi_type != GSM_MI_TYPE_TMSI) {
836 DEBUGPC(DMM, "mi_type is not TMSI: %d\n", mi_type);
837 return gsm48_tx_mm_serv_rej(conn,
838 GSM48_REJECT_INCORRECT_MESSAGE);
839 }
840
841 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
842 DEBUGPC(DMM, "serv_type=0x%02x mi_type=0x%02x M(%s)\n",
843 req->cm_service_type, mi_type, mi_string);
844
Jonathan Santos5a45b152011-08-17 15:33:57 -0400845 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, (classmark2 + classmark2_len));
Jonathan Santos03fd8d02011-05-25 13:54:02 -0400846
847 if (is_siemens_bts(bts))
848 send_siemens_mrpci(msg->lchan, classmark2-1);
849
850 subscr = subscr_get_by_tmsi(bts->network,
851 tmsi_from_string(mi_string));
852
853 /* FIXME: if we don't know the TMSI, inquire abit IMSI and allocate new TMSI */
854 if (!subscr)
855 return gsm48_tx_mm_serv_rej(conn,
856 GSM48_REJECT_IMSI_UNKNOWN_IN_HLR);
857
858 if (!conn->subscr)
859 conn->subscr = subscr;
860 else if (conn->subscr == subscr)
861 subscr_put(subscr); /* lchan already has a ref, don't need another one */
862 else {
863 DEBUGP(DMM, "<- CM Channel already owned by someone else?\n");
864 subscr_put(subscr);
865 }
866
867 subscr->equipment.classmark2_len = classmark2_len;
868 memcpy(subscr->equipment.classmark2, classmark2, classmark2_len);
869 db_sync_equipment(&subscr->equipment);
870
871 return gsm48_secure_channel(conn, req->cipher_key_seq,
872 _gsm48_rx_mm_serv_req_sec_cb, NULL);
873}
874
875static int gsm48_rx_mm_imsi_detach_ind(struct msgb *msg)
876{
877 struct gsm_bts *bts = msg->lchan->ts->trx->bts;
878 struct gsm48_hdr *gh = msgb_l3(msg);
879 struct gsm48_imsi_detach_ind *idi =
880 (struct gsm48_imsi_detach_ind *) gh->data;
Jonathan Santos5a45b152011-08-17 15:33:57 -0400881 uint8_t mi_type = idi->mi[0] & GSM_MI_TYPE_MASK;
Jonathan Santos03fd8d02011-05-25 13:54:02 -0400882 char mi_string[GSM48_MI_SIZE];
883 struct gsm_subscriber *subscr = NULL;
884
885 gsm48_mi_to_string(mi_string, sizeof(mi_string), idi->mi, idi->mi_len);
886 DEBUGP(DMM, "IMSI DETACH INDICATION: mi_type=0x%02x MI(%s): ",
887 mi_type, mi_string);
888
Jonathan Santos5a45b152011-08-17 15:33:57 -0400889 osmo_counter_inc(bts->network->stats.loc_upd_type.detach);
Jonathan Santos03fd8d02011-05-25 13:54:02 -0400890
891 switch (mi_type) {
892 case GSM_MI_TYPE_TMSI:
893 subscr = subscr_get_by_tmsi(bts->network,
894 tmsi_from_string(mi_string));
895 break;
896 case GSM_MI_TYPE_IMSI:
897 subscr = subscr_get_by_imsi(bts->network, mi_string);
898 break;
899 case GSM_MI_TYPE_IMEI:
900 case GSM_MI_TYPE_IMEISV:
901 /* no sim card... FIXME: what to do ? */
902 DEBUGPC(DMM, "unimplemented mobile identity type\n");
903 break;
904 default:
905 DEBUGPC(DMM, "unknown mobile identity type\n");
906 break;
907 }
908
909 if (subscr) {
910 subscr_update(subscr, msg->trx->bts,
911 GSM_SUBSCRIBER_UPDATE_DETACHED);
912 DEBUGP(DMM, "Subscriber: %s\n", subscr_name(subscr));
913
914 subscr->equipment.classmark1 = idi->classmark1;
915 db_sync_equipment(&subscr->equipment);
916
917 subscr_put(subscr);
918 } else
919 DEBUGP(DMM, "Unknown Subscriber ?!?\n");
920
921 /* FIXME: iterate over all transactions and release them,
922 * imagine an IMSI DETACH happening during an active call! */
923
924 /* subscriber is detached: should we release lchan? */
925 return 0;
926}
927
928static int gsm48_rx_mm_status(struct msgb *msg)
929{
930 struct gsm48_hdr *gh = msgb_l3(msg);
931
932 DEBUGP(DMM, "MM STATUS (reject cause 0x%02x)\n", gh->data[0]);
933
934 return 0;
935}
936
937/* Chapter 9.2.3: Authentication Response */
938static int gsm48_rx_mm_auth_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
939{
940 struct gsm48_hdr *gh = msgb_l3(msg);
941 struct gsm48_auth_resp *ar = (struct gsm48_auth_resp*) gh->data;
942 struct gsm_network *net = conn->bts->network;
943
944 DEBUGP(DMM, "MM AUTHENTICATION RESPONSE (sres = %s): ",
Jonathan Santos5a45b152011-08-17 15:33:57 -0400945 osmo_hexdump(ar->sres, 4));
Jonathan Santos03fd8d02011-05-25 13:54:02 -0400946
947 /* Safety check */
948 if (!conn->sec_operation) {
949 DEBUGP(DMM, "No authentication/cipher operation in progress !!!\n");
950 return -EIO;
951 }
952
953 /* Validate SRES */
954 if (memcmp(conn->sec_operation->atuple.sres, ar->sres,4)) {
955 int rc;
956 gsm_cbfn *cb = conn->sec_operation->cb;
957
958 DEBUGPC(DMM, "Invalid (expected %s)\n",
Jonathan Santos5a45b152011-08-17 15:33:57 -0400959 osmo_hexdump(conn->sec_operation->atuple.sres, 4));
Jonathan Santos03fd8d02011-05-25 13:54:02 -0400960
961 if (cb)
962 cb(GSM_HOOK_RR_SECURITY, GSM_SECURITY_AUTH_FAILED,
963 NULL, conn, conn->sec_operation->cb_data);
964
965 rc = gsm48_tx_mm_auth_rej(conn);
966 release_security_operation(conn);
967 return rc;
968 }
969
970 DEBUGPC(DMM, "OK\n");
971
972 /* Start ciphering */
973 return gsm0808_cipher_mode(conn, net->a5_encryption,
974 conn->sec_operation->atuple.kc, 8, 0);
975}
976
977/* Receive a GSM 04.08 Mobility Management (MM) message */
978static int gsm0408_rcv_mm(struct gsm_subscriber_connection *conn, struct msgb *msg)
979{
980 struct gsm48_hdr *gh = msgb_l3(msg);
981 int rc = 0;
982
983 switch (gh->msg_type & 0xbf) {
984 case GSM48_MT_MM_LOC_UPD_REQUEST:
985 DEBUGP(DMM, "LOCATION UPDATING REQUEST: ");
986 rc = mm_rx_loc_upd_req(conn, msg);
987 break;
988 case GSM48_MT_MM_ID_RESP:
989 rc = mm_rx_id_resp(conn, msg);
990 break;
991 case GSM48_MT_MM_CM_SERV_REQ:
992 rc = gsm48_rx_mm_serv_req(conn, msg);
993 break;
994 case GSM48_MT_MM_STATUS:
995 rc = gsm48_rx_mm_status(msg);
996 break;
997 case GSM48_MT_MM_TMSI_REALL_COMPL:
998 DEBUGP(DMM, "TMSI Reallocation Completed. Subscriber: %s\n",
999 conn->subscr ?
1000 subscr_name(conn->subscr) :
1001 "unknown subscriber");
1002 release_loc_updating_req(conn);
1003 break;
1004 case GSM48_MT_MM_IMSI_DETACH_IND:
1005 rc = gsm48_rx_mm_imsi_detach_ind(msg);
1006 break;
1007 case GSM48_MT_MM_CM_REEST_REQ:
1008 DEBUGP(DMM, "CM REESTABLISH REQUEST: Not implemented\n");
1009 break;
1010 case GSM48_MT_MM_AUTH_RESP:
1011 rc = gsm48_rx_mm_auth_resp(conn, msg);
1012 break;
1013 default:
1014 LOGP(DMM, LOGL_NOTICE, "Unknown GSM 04.08 MM msg type 0x%02x\n",
1015 gh->msg_type);
1016 break;
1017 }
1018
1019 return rc;
1020}
1021
1022/* Receive a PAGING RESPONSE message from the MS */
1023static int gsm48_rx_rr_pag_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
1024{
1025 struct gsm_bts *bts = conn->bts;
1026 struct gsm48_hdr *gh = msgb_l3(msg);
1027 struct gsm48_pag_resp *resp;
Jonathan Santos5a45b152011-08-17 15:33:57 -04001028 uint8_t *classmark2_lv = gh->data + 1;
1029 uint8_t mi_type;
Jonathan Santos03fd8d02011-05-25 13:54:02 -04001030 char mi_string[GSM48_MI_SIZE];
1031 struct gsm_subscriber *subscr = NULL;
1032 int rc = 0;
1033
1034 resp = (struct gsm48_pag_resp *) &gh->data[0];
1035 gsm48_paging_extract_mi(resp, msgb_l3len(msg) - sizeof(*gh),
1036 mi_string, &mi_type);
1037 DEBUGP(DRR, "PAGING RESPONSE: mi_type=0x%02x MI(%s)\n",
1038 mi_type, mi_string);
1039
1040 switch (mi_type) {
1041 case GSM_MI_TYPE_TMSI:
1042 subscr = subscr_get_by_tmsi(bts->network,
1043 tmsi_from_string(mi_string));
1044 break;
1045 case GSM_MI_TYPE_IMSI:
1046 subscr = subscr_get_by_imsi(bts->network, mi_string);
1047 break;
1048 }
1049
1050 if (!subscr) {
1051 DEBUGP(DRR, "<- Can't find any subscriber for this ID\n");
1052 /* FIXME: request id? close channel? */
1053 return -EINVAL;
1054 }
1055 DEBUGP(DRR, "<- Channel was requested by %s\n",
1056 subscr->name && strlen(subscr->name) ? subscr->name : subscr->imsi);
1057
1058 subscr->equipment.classmark2_len = *classmark2_lv;
1059 memcpy(subscr->equipment.classmark2, classmark2_lv+1, *classmark2_lv);
1060 db_sync_equipment(&subscr->equipment);
1061
1062 rc = gsm48_handle_paging_resp(conn, msg, subscr);
1063 return rc;
1064}
1065
1066static int gsm48_rx_rr_classmark(struct gsm_subscriber_connection *conn, struct msgb *msg)
1067{
1068 struct gsm48_hdr *gh = msgb_l3(msg);
1069 struct gsm_subscriber *subscr = conn->subscr;
1070 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
Jonathan Santos5a45b152011-08-17 15:33:57 -04001071 uint8_t cm2_len, cm3_len = 0;
1072 uint8_t *cm2, *cm3 = NULL;
Jonathan Santos03fd8d02011-05-25 13:54:02 -04001073
1074 DEBUGP(DRR, "CLASSMARK CHANGE ");
1075
1076 /* classmark 2 */
1077 cm2_len = gh->data[0];
1078 cm2 = &gh->data[1];
1079 DEBUGPC(DRR, "CM2(len=%u) ", cm2_len);
1080
1081 if (payload_len > cm2_len + 1) {
1082 /* we must have a classmark3 */
1083 if (gh->data[cm2_len+1] != 0x20) {
1084 DEBUGPC(DRR, "ERR CM3 TAG\n");
1085 return -EINVAL;
1086 }
1087 if (cm2_len > 3) {
1088 DEBUGPC(DRR, "CM2 too long!\n");
1089 return -EINVAL;
1090 }
1091
1092 cm3_len = gh->data[cm2_len+2];
1093 cm3 = &gh->data[cm2_len+3];
1094 if (cm3_len > 14) {
1095 DEBUGPC(DRR, "CM3 len %u too long!\n", cm3_len);
1096 return -EINVAL;
1097 }
1098 DEBUGPC(DRR, "CM3(len=%u)\n", cm3_len);
1099 }
1100 if (subscr) {
1101 subscr->equipment.classmark2_len = cm2_len;
1102 memcpy(subscr->equipment.classmark2, cm2, cm2_len);
1103 if (cm3) {
1104 subscr->equipment.classmark3_len = cm3_len;
1105 memcpy(subscr->equipment.classmark3, cm3, cm3_len);
1106 }
1107 db_sync_equipment(&subscr->equipment);
1108 }
1109
1110 return 0;
1111}
1112
1113static int gsm48_rx_rr_status(struct msgb *msg)
1114{
1115 struct gsm48_hdr *gh = msgb_l3(msg);
1116
1117 DEBUGP(DRR, "STATUS rr_cause = %s\n",
1118 rr_cause_name(gh->data[0]));
1119
1120 return 0;
1121}
1122
1123static int gsm48_rx_rr_meas_rep(struct msgb *msg)
1124{
1125 struct gsm_meas_rep *meas_rep = lchan_next_meas_rep(msg->lchan);
1126
1127 /* This shouldn't actually end up here, as RSL treats
1128 * L3 Info of 08.58 MEASUREMENT REPORT different by calling
1129 * directly into gsm48_parse_meas_rep */
1130 DEBUGP(DMEAS, "DIRECT GSM48 MEASUREMENT REPORT ?!? ");
1131 gsm48_parse_meas_rep(meas_rep, msg);
1132
1133 return 0;
1134}
1135
1136static int gsm48_rx_rr_app_info(struct gsm_subscriber_connection *conn, struct msgb *msg)
1137{
1138 struct gsm48_hdr *gh = msgb_l3(msg);
Jonathan Santos5a45b152011-08-17 15:33:57 -04001139 uint8_t apdu_id_flags;
1140 uint8_t apdu_len;
1141 uint8_t *apdu_data;
Jonathan Santos03fd8d02011-05-25 13:54:02 -04001142
1143 apdu_id_flags = gh->data[0];
1144 apdu_len = gh->data[1];
1145 apdu_data = gh->data+2;
1146
1147 DEBUGP(DNM, "RX APPLICATION INFO id/flags=0x%02x apdu_len=%u apdu=%s",
Jonathan Santos5a45b152011-08-17 15:33:57 -04001148 apdu_id_flags, apdu_len, osmo_hexdump(apdu_data, apdu_len));
Jonathan Santos03fd8d02011-05-25 13:54:02 -04001149
1150 return db_apdu_blob_store(conn->subscr, apdu_id_flags, apdu_len, apdu_data);
1151}
1152
1153/* Chapter 9.1.10 Ciphering Mode Complete */
1154static int gsm48_rx_rr_ciph_m_compl(struct gsm_subscriber_connection *conn, struct msgb *msg)
1155{
1156 gsm_cbfn *cb;
1157 int rc = 0;
1158
1159 DEBUGP(DRR, "CIPHERING MODE COMPLETE\n");
1160
1161 /* Safety check */
1162 if (!conn->sec_operation) {
1163 DEBUGP(DRR, "No authentication/cipher operation in progress !!!\n");
1164 return -EIO;
1165 }
1166
1167 /* FIXME: check for MI (if any) */
1168
1169 /* Call back whatever was in progress (if anything) ... */
1170 cb = conn->sec_operation->cb;
1171 if (cb) {
1172 rc = cb(GSM_HOOK_RR_SECURITY, GSM_SECURITY_SUCCEEDED,
1173 NULL, conn, conn->sec_operation->cb_data);
1174 }
1175
1176 /* Complete the operation */
1177 release_security_operation(conn);
1178
1179 return rc;
1180}
1181
1182/* Chapter 9.1.16 Handover complete */
1183static int gsm48_rx_rr_ho_compl(struct msgb *msg)
1184{
1185 struct lchan_signal_data sig;
1186 struct gsm48_hdr *gh = msgb_l3(msg);
1187
1188 DEBUGP(DRR, "HANDOVER COMPLETE cause = %s\n",
1189 rr_cause_name(gh->data[0]));
1190
1191 sig.lchan = msg->lchan;
1192 sig.mr = NULL;
Jonathan Santos5a45b152011-08-17 15:33:57 -04001193 osmo_signal_dispatch(SS_LCHAN, S_LCHAN_HANDOVER_COMPL, &sig);
Jonathan Santos03fd8d02011-05-25 13:54:02 -04001194 /* FIXME: release old channel */
1195
1196 return 0;
1197}
1198
1199/* Chapter 9.1.17 Handover Failure */
1200static int gsm48_rx_rr_ho_fail(struct msgb *msg)
1201{
1202 struct lchan_signal_data sig;
1203 struct gsm48_hdr *gh = msgb_l3(msg);
1204
1205 DEBUGP(DRR, "HANDOVER FAILED cause = %s\n",
1206 rr_cause_name(gh->data[0]));
1207
1208 sig.lchan = msg->lchan;
1209 sig.mr = NULL;
Jonathan Santos5a45b152011-08-17 15:33:57 -04001210 osmo_signal_dispatch(SS_LCHAN, S_LCHAN_HANDOVER_FAIL, &sig);
Jonathan Santos03fd8d02011-05-25 13:54:02 -04001211 /* FIXME: release allocated new channel */
1212
1213 return 0;
1214}
1215
1216/* Receive a GSM 04.08 Radio Resource (RR) message */
1217static int gsm0408_rcv_rr(struct gsm_subscriber_connection *conn, struct msgb *msg)
1218{
1219 struct gsm48_hdr *gh = msgb_l3(msg);
1220 int rc = 0;
1221
1222 switch (gh->msg_type) {
1223 case GSM48_MT_RR_CLSM_CHG:
1224 rc = gsm48_rx_rr_classmark(conn, msg);
1225 break;
1226 case GSM48_MT_RR_GPRS_SUSP_REQ:
1227 DEBUGP(DRR, "GRPS SUSPEND REQUEST\n");
1228 break;
1229 case GSM48_MT_RR_PAG_RESP:
1230 rc = gsm48_rx_rr_pag_resp(conn, msg);
1231 break;
1232 case GSM48_MT_RR_STATUS:
1233 rc = gsm48_rx_rr_status(msg);
1234 break;
1235 case GSM48_MT_RR_MEAS_REP:
1236 rc = gsm48_rx_rr_meas_rep(msg);
1237 break;
1238 case GSM48_MT_RR_APP_INFO:
1239 rc = gsm48_rx_rr_app_info(conn, msg);
1240 break;
1241 case GSM48_MT_RR_CIPH_M_COMPL:
1242 rc = gsm48_rx_rr_ciph_m_compl(conn, msg);
1243 break;
1244 case GSM48_MT_RR_HANDO_COMPL:
1245 rc = gsm48_rx_rr_ho_compl(msg);
1246 break;
1247 case GSM48_MT_RR_HANDO_FAIL:
1248 rc = gsm48_rx_rr_ho_fail(msg);
1249 break;
1250 default:
1251 LOGP(DRR, LOGL_NOTICE, "Unimplemented "
1252 "GSM 04.08 RR msg type 0x%02x\n", gh->msg_type);
1253 break;
1254 }
1255
1256 return rc;
1257}
1258
Jonathan Santos5a45b152011-08-17 15:33:57 -04001259int gsm48_send_rr_app_info(struct gsm_subscriber_connection *conn, uint8_t apdu_id,
1260 uint8_t apdu_len, const uint8_t *apdu)
Jonathan Santos03fd8d02011-05-25 13:54:02 -04001261{
1262 struct msgb *msg = gsm48_msgb_alloc();
1263 struct gsm48_hdr *gh;
1264
1265 msg->lchan = conn->lchan;
1266
1267 DEBUGP(DRR, "TX APPLICATION INFO id=0x%02x, len=%u\n",
1268 apdu_id, apdu_len);
1269
1270 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 2 + apdu_len);
1271 gh->proto_discr = GSM48_PDISC_RR;
1272 gh->msg_type = GSM48_MT_RR_APP_INFO;
1273 gh->data[0] = apdu_id;
1274 gh->data[1] = apdu_len;
1275 memcpy(gh->data+2, apdu, apdu_len);
1276
1277 return gsm48_conn_sendmsg(msg, conn, NULL);
1278}
1279
1280/* Call Control */
1281
1282/* The entire call control code is written in accordance with Figure 7.10c
1283 * for 'very early assignment', i.e. we allocate a TCH/F during IMMEDIATE
1284 * ASSIGN, then first use that TCH/F for signalling and later MODE MODIFY
1285 * it for voice */
1286
1287static void new_cc_state(struct gsm_trans *trans, int state)
1288{
1289 if (state > 31 || state < 0)
1290 return;
1291
1292 DEBUGP(DCC, "new state %s -> %s\n",
1293 gsm48_cc_state_name(trans->cc.state),
1294 gsm48_cc_state_name(state));
1295
1296 trans->cc.state = state;
1297}
1298
1299static int gsm48_cc_tx_status(struct gsm_trans *trans, void *arg)
1300{
1301 struct msgb *msg = gsm48_msgb_alloc();
1302 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Jonathan Santos5a45b152011-08-17 15:33:57 -04001303 uint8_t *cause, *call_state;
Jonathan Santos03fd8d02011-05-25 13:54:02 -04001304
1305 gh->msg_type = GSM48_MT_CC_STATUS;
1306
1307 cause = msgb_put(msg, 3);
1308 cause[0] = 2;
1309 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
1310 cause[2] = 0x80 | 30; /* response to status inquiry */
1311
1312 call_state = msgb_put(msg, 1);
1313 call_state[0] = 0xc0 | 0x00;
1314
1315 return gsm48_conn_sendmsg(msg, trans->conn, trans);
1316}
1317
1318static int gsm48_tx_simple(struct gsm_subscriber_connection *conn,
Jonathan Santos5a45b152011-08-17 15:33:57 -04001319 uint8_t pdisc, uint8_t msg_type)
Jonathan Santos03fd8d02011-05-25 13:54:02 -04001320{
1321 struct msgb *msg = gsm48_msgb_alloc();
1322 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1323
1324 msg->lchan = conn->lchan;
1325
1326 gh->proto_discr = pdisc;
1327 gh->msg_type = msg_type;
1328
1329 return gsm48_conn_sendmsg(msg, conn, NULL);
1330}
1331
1332static void gsm48_stop_cc_timer(struct gsm_trans *trans)
1333{
Jonathan Santos5a45b152011-08-17 15:33:57 -04001334 if (osmo_timer_pending(&trans->cc.timer)) {
Jonathan Santos03fd8d02011-05-25 13:54:02 -04001335 DEBUGP(DCC, "stopping pending timer T%x\n", trans->cc.Tcurrent);
Jonathan Santos5a45b152011-08-17 15:33:57 -04001336 osmo_timer_del(&trans->cc.timer);
Jonathan Santos03fd8d02011-05-25 13:54:02 -04001337 trans->cc.Tcurrent = 0;
1338 }
1339}
1340
1341static int mncc_recvmsg(struct gsm_network *net, struct gsm_trans *trans,
1342 int msg_type, struct gsm_mncc *mncc)
1343{
1344 struct msgb *msg;
1345 unsigned char *data;
1346
1347 if (trans)
1348 if (trans->conn && trans->conn->lchan)
1349 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
1350 "Sending '%s' to MNCC.\n",
1351 trans->conn->lchan->ts->trx->bts->nr,
1352 trans->conn->lchan->ts->trx->nr,
1353 trans->conn->lchan->ts->nr, trans->transaction_id,
1354 (trans->subscr)?(trans->subscr->extension):"-",
1355 get_mncc_name(msg_type));
1356 else
1357 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
1358 "Sending '%s' to MNCC.\n",
1359 (trans->subscr)?(trans->subscr->extension):"-",
1360 get_mncc_name(msg_type));
1361 else
1362 DEBUGP(DCC, "(bts - trx - ts - ti -- sub -) "
1363 "Sending '%s' to MNCC.\n", get_mncc_name(msg_type));
1364
1365 mncc->msg_type = msg_type;
1366
1367 msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
1368 if (!msg)
1369 return -ENOMEM;
1370
1371 data = msgb_put(msg, sizeof(struct gsm_mncc));
1372 memcpy(data, mncc, sizeof(struct gsm_mncc));
1373
1374 cc_tx_to_mncc(net, msg);
1375
1376 return 0;
1377}
1378
1379int mncc_release_ind(struct gsm_network *net, struct gsm_trans *trans,
Jonathan Santos5a45b152011-08-17 15:33:57 -04001380 uint32_t callref, int location, int value)
Jonathan Santos03fd8d02011-05-25 13:54:02 -04001381{
1382 struct gsm_mncc rel;
1383
1384 memset(&rel, 0, sizeof(rel));
1385 rel.callref = callref;
1386 mncc_set_cause(&rel, location, value);
1387 return mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
1388}
1389
1390/* Call Control Specific transaction release.
1391 * gets called by trans_free, DO NOT CALL YOURSELF! */
1392void _gsm48_cc_trans_free(struct gsm_trans *trans)
1393{
1394 gsm48_stop_cc_timer(trans);
1395
1396 /* send release to L4, if callref still exists */
1397 if (trans->callref) {
1398 /* Ressource unavailable */
1399 mncc_release_ind(trans->subscr->net, trans, trans->callref,
1400 GSM48_CAUSE_LOC_PRN_S_LU,
1401 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
1402 }
1403 if (trans->cc.state != GSM_CSTATE_NULL)
1404 new_cc_state(trans, GSM_CSTATE_NULL);
1405 if (trans->conn)
1406 trau_mux_unmap(&trans->conn->lchan->ts->e1_link, trans->callref);
1407}
1408
1409static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg);
1410
1411/* call-back from paging the B-end of the connection */
1412static int setup_trig_pag_evt(unsigned int hooknum, unsigned int event,
1413 struct msgb *msg, void *_conn, void *param)
1414{
1415 int found = 0;
1416 struct gsm_subscriber_connection *conn = _conn;
1417 struct gsm_network **paging_request = param, *net;
1418 struct gsm_trans *transt, *tmp;
1419
1420 if (hooknum != GSM_HOOK_RR_PAGING)
1421 return -EINVAL;
1422
1423 net = *paging_request;
1424 if (!net) {
1425 DEBUGP(DCC, "Error Network not set!\n");
1426 return -EINVAL;
1427 }
1428
1429 /* check all tranactions (without lchan) for subscriber */
1430 llist_for_each_entry_safe(transt, tmp, &net->trans_list, entry) {
1431 if (transt->paging_request != paging_request || transt->conn)
1432 continue;
1433 switch (event) {
1434 case GSM_PAGING_SUCCEEDED:
1435 if (!conn) // paranoid
1436 break;
1437 DEBUGP(DCC, "Paging subscr %s succeeded!\n",
1438 transt->subscr->extension);
1439 found = 1;
1440 /* Assign lchan */
1441 if (!transt->conn) {
1442 transt->paging_request = NULL;
1443 transt->conn = conn;
1444 conn->put_channel = 1;
1445 }
1446 /* send SETUP request to called party */
1447 gsm48_cc_tx_setup(transt, &transt->cc.msg);
1448 break;
1449 case GSM_PAGING_EXPIRED:
1450 case GSM_PAGING_BUSY:
1451 DEBUGP(DCC, "Paging subscr %s expired!\n",
1452 transt->subscr->extension);
1453 /* Temporarily out of order */
1454 found = 1;
1455 mncc_release_ind(transt->subscr->net, transt,
1456 transt->callref,
1457 GSM48_CAUSE_LOC_PRN_S_LU,
1458 GSM48_CC_CAUSE_DEST_OOO);
1459 transt->callref = 0;
1460 transt->paging_request = NULL;
1461 trans_free(transt);
1462 break;
1463 }
1464 }
1465
1466 talloc_free(paging_request);
1467
1468 /*
1469 * FIXME: The queue needs to be kicked. This is likely to go through a RF
1470 * failure and then the subscr will be poke again. This needs a lot of fixing
1471 * in the subscriber queue code.
1472 */
1473 if (!found && conn)
1474 conn->put_channel = 1;
1475 return 0;
1476}
1477
Jonathan Santos5a45b152011-08-17 15:33:57 -04001478static int tch_recv_mncc(struct gsm_network *net, uint32_t callref, int enable);
Jonathan Santos03fd8d02011-05-25 13:54:02 -04001479
1480/* handle audio path for handover */
1481static int handle_ho_signal(unsigned int subsys, unsigned int signal,
1482 void *handler_data, void *signal_data)
1483{
1484 struct rtp_socket *old_rs, *new_rs, *other_rs;
1485 struct ho_signal_data *sig = signal_data;
1486
1487 if (subsys != SS_HO || signal != S_HANDOVER_ACK)
1488 return 0;
1489
1490 if (ipacc_rtp_direct) {
1491 LOGP(DHO, LOGL_ERROR, "unable to handover in direct RTP mode\n");
1492 return 0;
1493 }
1494
1495 /* RTP Proxy mode */
1496 new_rs = sig->new_lchan->abis_ip.rtp_socket;
1497 old_rs = sig->old_lchan->abis_ip.rtp_socket;
1498
1499 if (!new_rs) {
1500 LOGP(DHO, LOGL_ERROR, "no RTP socket for new_lchan\n");
1501 return -EIO;
1502 }
1503
1504 rsl_ipacc_mdcx_to_rtpsock(sig->new_lchan);
1505
1506 if (!old_rs) {
1507 LOGP(DHO, LOGL_ERROR, "no RTP socket for old_lchan\n");
1508 return -EIO;
1509 }
1510
1511 /* copy rx_action and reference to other sock */
1512 new_rs->rx_action = old_rs->rx_action;
1513 new_rs->tx_action = old_rs->tx_action;
1514 new_rs->transmit = old_rs->transmit;
1515
1516 switch (sig->old_lchan->abis_ip.rtp_socket->rx_action) {
1517 case RTP_PROXY:
1518 other_rs = old_rs->proxy.other_sock;
1519 rtp_socket_proxy(new_rs, other_rs);
1520 /* delete reference to other end socket to prevent
1521 * rtp_socket_free() from removing the inverse reference */
1522 old_rs->proxy.other_sock = NULL;
1523 break;
1524 case RTP_RECV_UPSTREAM:
1525 new_rs->receive = old_rs->receive;
1526 break;
1527 case RTP_NONE:
1528 break;
1529 }
1530
1531 return 0;
1532}
1533
1534/* some other part of the code sends us a signal */
1535static int handle_abisip_signal(unsigned int subsys, unsigned int signal,
1536 void *handler_data, void *signal_data)
1537{
1538 struct gsm_lchan *lchan = signal_data;
1539 int rc;
1540 struct gsm_network *net;
1541 struct gsm_trans *trans;
1542
1543 if (subsys != SS_ABISIP)
1544 return 0;
1545
1546 /* in case we use direct BTS-to-BTS RTP */
1547 if (ipacc_rtp_direct)
1548 return 0;
1549
1550 switch (signal) {
1551 case S_ABISIP_CRCX_ACK:
1552 /* in case we don't use direct BTS-to-BTS RTP */
1553 /* the BTS has successfully bound a TCH to a local ip/port,
1554 * which means we can connect our UDP socket to it */
1555 if (lchan->abis_ip.rtp_socket) {
1556 rtp_socket_free(lchan->abis_ip.rtp_socket);
1557 lchan->abis_ip.rtp_socket = NULL;
1558 }
1559
1560 lchan->abis_ip.rtp_socket = rtp_socket_create();
1561 if (!lchan->abis_ip.rtp_socket)
1562 return -EIO;
1563
1564 rc = rtp_socket_connect(lchan->abis_ip.rtp_socket,
1565 lchan->abis_ip.bound_ip,
1566 lchan->abis_ip.bound_port);
1567 if (rc < 0)
1568 return -EIO;
1569
1570 /* check if any transactions on this lchan still have
1571 * a tch_recv_mncc request pending */
1572 net = lchan->ts->trx->bts->network;
1573 llist_for_each_entry(trans, &net->trans_list, entry) {
1574 if (trans->conn && trans->conn->lchan == lchan && trans->tch_recv) {
1575 DEBUGP(DCC, "pending tch_recv_mncc request\n");
1576 tch_recv_mncc(net, trans->callref, 1);
1577 }
1578 }
1579 break;
1580 case S_ABISIP_DLCX_IND:
1581 /* the BTS tells us a RTP stream has been disconnected */
1582 if (lchan->abis_ip.rtp_socket) {
1583 rtp_socket_free(lchan->abis_ip.rtp_socket);
1584 lchan->abis_ip.rtp_socket = NULL;
1585 }
1586
1587 break;
1588 }
1589
1590 return 0;
1591}
1592
1593/* map two ipaccess RTP streams onto each other */
1594static int tch_map(struct gsm_lchan *lchan, struct gsm_lchan *remote_lchan)
1595{
1596 struct gsm_bts *bts = lchan->ts->trx->bts;
1597 struct gsm_bts *remote_bts = remote_lchan->ts->trx->bts;
1598 int rc;
1599
1600 DEBUGP(DCC, "Setting up TCH map between (bts=%u,trx=%u,ts=%u) and (bts=%u,trx=%u,ts=%u)\n",
1601 bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
1602 remote_bts->nr, remote_lchan->ts->trx->nr, remote_lchan->ts->nr);
1603
1604 if (bts->type != remote_bts->type) {
1605 DEBUGP(DCC, "Cannot switch calls between different BTS types yet\n");
1606 return -EINVAL;
1607 }
1608
1609 // todo: map between different bts types
1610 switch (bts->type) {
1611 case GSM_BTS_TYPE_NANOBTS:
1612 if (!ipacc_rtp_direct) {
1613 /* connect the TCH's to our RTP proxy */
1614 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
1615 if (rc < 0)
1616 return rc;
1617 rc = rsl_ipacc_mdcx_to_rtpsock(remote_lchan);
1618 if (rc < 0)
1619 return rc;
1620 /* connect them with each other */
1621 rtp_socket_proxy(lchan->abis_ip.rtp_socket,
1622 remote_lchan->abis_ip.rtp_socket);
1623 } else {
1624 /* directly connect TCH RTP streams to each other */
1625 rc = rsl_ipacc_mdcx(lchan, remote_lchan->abis_ip.bound_ip,
1626 remote_lchan->abis_ip.bound_port,
1627 remote_lchan->abis_ip.rtp_payload2);
1628 if (rc < 0)
1629 return rc;
1630 rc = rsl_ipacc_mdcx(remote_lchan, lchan->abis_ip.bound_ip,
1631 lchan->abis_ip.bound_port,
1632 lchan->abis_ip.rtp_payload2);
1633 }
1634 break;
1635 case GSM_BTS_TYPE_BS11:
Jonathan Santos5a45b152011-08-17 15:33:57 -04001636 case GSM_BTS_TYPE_RBS2000:
Jonathan Santos03fd8d02011-05-25 13:54:02 -04001637 trau_mux_map_lchan(lchan, remote_lchan);
1638 break;
1639 default:
1640 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
1641 return -EINVAL;
1642 }
1643
1644 return 0;
1645}
1646
1647/* bridge channels of two transactions */
Jonathan Santos5a45b152011-08-17 15:33:57 -04001648static int tch_bridge(struct gsm_network *net, uint32_t *refs)
Jonathan Santos03fd8d02011-05-25 13:54:02 -04001649{
1650 struct gsm_trans *trans1 = trans_find_by_callref(net, refs[0]);
1651 struct gsm_trans *trans2 = trans_find_by_callref(net, refs[1]);
1652
1653 if (!trans1 || !trans2)
1654 return -EIO;
1655
1656 if (!trans1->conn || !trans2->conn)
1657 return -EIO;
1658
1659 /* through-connect channel */
1660 return tch_map(trans1->conn->lchan, trans2->conn->lchan);
1661}
1662
1663/* enable receive of channels to MNCC upqueue */
Jonathan Santos5a45b152011-08-17 15:33:57 -04001664static int tch_recv_mncc(struct gsm_network *net, uint32_t callref, int enable)
Jonathan Santos03fd8d02011-05-25 13:54:02 -04001665{
1666 struct gsm_trans *trans;
1667 struct gsm_lchan *lchan;
1668 struct gsm_bts *bts;
1669 int rc;
1670
1671 /* Find callref */
1672 trans = trans_find_by_callref(net, callref);
1673 if (!trans)
1674 return -EIO;
1675 if (!trans->conn)
1676 return 0;
1677 lchan = trans->conn->lchan;
1678 bts = lchan->ts->trx->bts;
1679
1680 switch (bts->type) {
1681 case GSM_BTS_TYPE_NANOBTS:
1682 if (ipacc_rtp_direct) {
1683 DEBUGP(DCC, "Error: RTP proxy is disabled\n");
1684 return -EINVAL;
1685 }
1686 /* in case, we don't have a RTP socket yet, we note this
1687 * in the transaction and try later */
1688 if (!lchan->abis_ip.rtp_socket) {
1689 trans->tch_recv = enable;
1690 DEBUGP(DCC, "queue tch_recv_mncc request (%d)\n", enable);
1691 return 0;
1692 }
1693 if (enable) {
1694 /* connect the TCH's to our RTP proxy */
1695 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
1696 if (rc < 0)
1697 return rc;
1698 /* assign socket to application interface */
1699 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
1700 net, callref);
1701 } else
1702 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
1703 net, 0);
1704 break;
1705 case GSM_BTS_TYPE_BS11:
Jonathan Santos5a45b152011-08-17 15:33:57 -04001706 case GSM_BTS_TYPE_RBS2000:
Jonathan Santos03fd8d02011-05-25 13:54:02 -04001707 if (enable)
1708 return trau_recv_lchan(lchan, callref);
1709 return trau_mux_unmap(NULL, callref);
1710 break;
1711 default:
1712 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
1713 return -EINVAL;
1714 }
1715
1716 return 0;
1717}
1718
1719static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
1720{
1721 DEBUGP(DCC, "-> STATUS ENQ\n");
1722 return gsm48_cc_tx_status(trans, msg);
1723}
1724
1725static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg);
1726static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg);
1727
1728static void gsm48_cc_timeout(void *arg)
1729{
1730 struct gsm_trans *trans = arg;
1731 int disconnect = 0, release = 0;
1732 int mo_cause = GSM48_CC_CAUSE_RECOVERY_TIMER;
1733 int mo_location = GSM48_CAUSE_LOC_USER;
1734 int l4_cause = GSM48_CC_CAUSE_NORMAL_UNSPEC;
1735 int l4_location = GSM48_CAUSE_LOC_PRN_S_LU;
1736 struct gsm_mncc mo_rel, l4_rel;
1737
1738 memset(&mo_rel, 0, sizeof(struct gsm_mncc));
1739 mo_rel.callref = trans->callref;
1740 memset(&l4_rel, 0, sizeof(struct gsm_mncc));
1741 l4_rel.callref = trans->callref;
1742
1743 switch(trans->cc.Tcurrent) {
1744 case 0x303:
1745 release = 1;
1746 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
1747 break;
1748 case 0x310:
1749 disconnect = 1;
1750 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
1751 break;
1752 case 0x313:
1753 disconnect = 1;
1754 /* unknown, did not find it in the specs */
1755 break;
1756 case 0x301:
1757 disconnect = 1;
1758 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
1759 break;
1760 case 0x308:
1761 if (!trans->cc.T308_second) {
1762 /* restart T308 a second time */
1763 gsm48_cc_tx_release(trans, &trans->cc.msg);
1764 trans->cc.T308_second = 1;
1765 break; /* stay in release state */
1766 }
1767 trans_free(trans);
1768 return;
1769// release = 1;
1770// l4_cause = 14;
1771// break;
1772 case 0x306:
1773 release = 1;
1774 mo_cause = trans->cc.msg.cause.value;
1775 mo_location = trans->cc.msg.cause.location;
1776 break;
1777 case 0x323:
1778 disconnect = 1;
1779 break;
1780 default:
1781 release = 1;
1782 }
1783
1784 if (release && trans->callref) {
1785 /* process release towards layer 4 */
1786 mncc_release_ind(trans->subscr->net, trans, trans->callref,
1787 l4_location, l4_cause);
1788 trans->callref = 0;
1789 }
1790
1791 if (disconnect && trans->callref) {
1792 /* process disconnect towards layer 4 */
1793 mncc_set_cause(&l4_rel, l4_location, l4_cause);
1794 mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &l4_rel);
1795 }
1796
1797 /* process disconnect towards mobile station */
1798 if (disconnect || release) {
1799 mncc_set_cause(&mo_rel, mo_location, mo_cause);
1800 mo_rel.cause.diag[0] = ((trans->cc.Tcurrent & 0xf00) >> 8) + '0';
1801 mo_rel.cause.diag[1] = ((trans->cc.Tcurrent & 0x0f0) >> 4) + '0';
1802 mo_rel.cause.diag[2] = (trans->cc.Tcurrent & 0x00f) + '0';
1803 mo_rel.cause.diag_len = 3;
1804
1805 if (disconnect)
1806 gsm48_cc_tx_disconnect(trans, &mo_rel);
1807 if (release)
1808 gsm48_cc_tx_release(trans, &mo_rel);
1809 }
1810
1811}
1812
1813static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
1814 int sec, int micro)
1815{
1816 DEBUGP(DCC, "starting timer T%x with %d seconds\n", current, sec);
1817 trans->cc.timer.cb = gsm48_cc_timeout;
1818 trans->cc.timer.data = trans;
Jonathan Santos5a45b152011-08-17 15:33:57 -04001819 osmo_timer_schedule(&trans->cc.timer, sec, micro);
Jonathan Santos03fd8d02011-05-25 13:54:02 -04001820 trans->cc.Tcurrent = current;
1821}
1822
1823static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
1824{
1825 struct gsm48_hdr *gh = msgb_l3(msg);
Jonathan Santos5a45b152011-08-17 15:33:57 -04001826 uint8_t msg_type = gh->msg_type & 0xbf;
Jonathan Santos03fd8d02011-05-25 13:54:02 -04001827 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1828 struct tlv_parsed tp;
1829 struct gsm_mncc setup;
1830
1831 memset(&setup, 0, sizeof(struct gsm_mncc));
1832 setup.callref = trans->callref;
1833 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
1834 /* emergency setup is identified by msg_type */
1835 if (msg_type == GSM48_MT_CC_EMERG_SETUP)
1836 setup.emergency = 1;
1837
1838 /* use subscriber as calling party number */
1839 if (trans->subscr) {
1840 setup.fields |= MNCC_F_CALLING;
1841 strncpy(setup.calling.number, trans->subscr->extension,
1842 sizeof(setup.calling.number)-1);
1843 strncpy(setup.imsi, trans->subscr->imsi,
1844 sizeof(setup.imsi)-1);
1845 }
1846 /* bearer capability */
1847 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1848 setup.fields |= MNCC_F_BEARER_CAP;
1849 gsm48_decode_bearer_cap(&setup.bearer_cap,
1850 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1851 }
1852 /* facility */
1853 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1854 setup.fields |= MNCC_F_FACILITY;
1855 gsm48_decode_facility(&setup.facility,
1856 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1857 }
1858 /* called party bcd number */
1859 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
1860 setup.fields |= MNCC_F_CALLED;
1861 gsm48_decode_called(&setup.called,
1862 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
1863 }
1864 /* user-user */
1865 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1866 setup.fields |= MNCC_F_USERUSER;
1867 gsm48_decode_useruser(&setup.useruser,
1868 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1869 }
1870 /* ss-version */
1871 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1872 setup.fields |= MNCC_F_SSVERSION;
1873 gsm48_decode_ssversion(&setup.ssversion,
1874 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1875 }
1876 /* CLIR suppression */
1877 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
1878 setup.clir.sup = 1;
1879 /* CLIR invocation */
1880 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
1881 setup.clir.inv = 1;
1882 /* cc cap */
1883 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1884 setup.fields |= MNCC_F_CCCAP;
1885 gsm48_decode_cccap(&setup.cccap,
1886 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1887 }
1888
1889 new_cc_state(trans, GSM_CSTATE_INITIATED);
1890
1891 LOGP(DCC, LOGL_INFO, "Subscriber %s (%s) sends SETUP to %s\n",
1892 subscr_name(trans->subscr), trans->subscr->extension,
1893 setup.called.number);
1894
Jonathan Santos5a45b152011-08-17 15:33:57 -04001895 osmo_counter_inc(trans->subscr->net->stats.call.mo_setup);
Jonathan Santos03fd8d02011-05-25 13:54:02 -04001896
1897 /* indicate setup to MNCC */
1898 mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_IND, &setup);
1899
1900 /* MNCC code will modify the channel asynchronously, we should
1901 * ipaccess-bind only after the modification has been made to the
1902 * lchan->tch_mode */
1903 return 0;
1904}
1905
1906static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
1907{
1908 struct msgb *msg = gsm48_msgb_alloc();
1909 struct gsm48_hdr *gh;
1910 struct gsm_mncc *setup = arg;
1911 int rc, trans_id;
1912
1913 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1914
1915 /* transaction id must not be assigned */
1916 if (trans->transaction_id != 0xff) { /* unasssigned */
1917 DEBUGP(DCC, "TX Setup with assigned transaction. "
1918 "This is not allowed!\n");
1919 /* Temporarily out of order */
1920 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
1921 GSM48_CAUSE_LOC_PRN_S_LU,
1922 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
1923 trans->callref = 0;
1924 trans_free(trans);
1925 return rc;
1926 }
1927
1928 /* Get free transaction_id */
1929 trans_id = trans_assign_trans_id(trans->subscr, GSM48_PDISC_CC, 0);
1930 if (trans_id < 0) {
1931 /* no free transaction ID */
1932 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
1933 GSM48_CAUSE_LOC_PRN_S_LU,
1934 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
1935 trans->callref = 0;
1936 trans_free(trans);
1937 return rc;
1938 }
1939 trans->transaction_id = trans_id;
1940
1941 gh->msg_type = GSM48_MT_CC_SETUP;
1942
1943 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
1944
1945 /* bearer capability */
1946 if (setup->fields & MNCC_F_BEARER_CAP)
1947 gsm48_encode_bearer_cap(msg, 0, &setup->bearer_cap);
1948 /* facility */
1949 if (setup->fields & MNCC_F_FACILITY)
1950 gsm48_encode_facility(msg, 0, &setup->facility);
1951 /* progress */
1952 if (setup->fields & MNCC_F_PROGRESS)
1953 gsm48_encode_progress(msg, 0, &setup->progress);
1954 /* calling party BCD number */
1955 if (setup->fields & MNCC_F_CALLING)
1956 gsm48_encode_calling(msg, &setup->calling);
1957 /* called party BCD number */
1958 if (setup->fields & MNCC_F_CALLED)
1959 gsm48_encode_called(msg, &setup->called);
1960 /* user-user */
1961 if (setup->fields & MNCC_F_USERUSER)
1962 gsm48_encode_useruser(msg, 0, &setup->useruser);
1963 /* redirecting party BCD number */
1964 if (setup->fields & MNCC_F_REDIRECTING)
1965 gsm48_encode_redirecting(msg, &setup->redirecting);
1966 /* signal */
1967 if (setup->fields & MNCC_F_SIGNAL)
1968 gsm48_encode_signal(msg, setup->signal);
1969
1970 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
1971
Jonathan Santos5a45b152011-08-17 15:33:57 -04001972 osmo_counter_inc(trans->subscr->net->stats.call.mt_setup);
Jonathan Santos03fd8d02011-05-25 13:54:02 -04001973
1974 return gsm48_conn_sendmsg(msg, trans->conn, trans);
1975}
1976
1977static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
1978{
1979 struct gsm48_hdr *gh = msgb_l3(msg);
1980 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1981 struct tlv_parsed tp;
1982 struct gsm_mncc call_conf;
1983
1984 gsm48_stop_cc_timer(trans);
1985 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
1986
1987 memset(&call_conf, 0, sizeof(struct gsm_mncc));
1988 call_conf.callref = trans->callref;
1989 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
1990#if 0
1991 /* repeat */
1992 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
1993 call_conf.repeat = 1;
1994 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
1995 call_conf.repeat = 2;
1996#endif
1997 /* bearer capability */
1998 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1999 call_conf.fields |= MNCC_F_BEARER_CAP;
2000 gsm48_decode_bearer_cap(&call_conf.bearer_cap,
2001 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2002 }
2003 /* cause */
2004 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2005 call_conf.fields |= MNCC_F_CAUSE;
2006 gsm48_decode_cause(&call_conf.cause,
2007 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2008 }
2009 /* cc cap */
2010 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
2011 call_conf.fields |= MNCC_F_CCCAP;
2012 gsm48_decode_cccap(&call_conf.cccap,
2013 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
2014 }
2015
2016 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
2017
2018 return mncc_recvmsg(trans->subscr->net, trans, MNCC_CALL_CONF_IND,
2019 &call_conf);
2020}
2021
2022static int gsm48_cc_tx_call_proc(struct gsm_trans *trans, void *arg)
2023{
2024 struct gsm_mncc *proceeding = arg;
2025 struct msgb *msg = gsm48_msgb_alloc();
2026 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2027
2028 gh->msg_type = GSM48_MT_CC_CALL_PROC;
2029
2030 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
2031
2032 /* bearer capability */
2033 if (proceeding->fields & MNCC_F_BEARER_CAP)
2034 gsm48_encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
2035 /* facility */
2036 if (proceeding->fields & MNCC_F_FACILITY)
2037 gsm48_encode_facility(msg, 0, &proceeding->facility);
2038 /* progress */
2039 if (proceeding->fields & MNCC_F_PROGRESS)
2040 gsm48_encode_progress(msg, 0, &proceeding->progress);
2041
2042 return gsm48_conn_sendmsg(msg, trans->conn, trans);
2043}
2044
2045static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
2046{
2047 struct gsm48_hdr *gh = msgb_l3(msg);
2048 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2049 struct tlv_parsed tp;
2050 struct gsm_mncc alerting;
2051
2052 gsm48_stop_cc_timer(trans);
2053 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
2054
2055 memset(&alerting, 0, sizeof(struct gsm_mncc));
2056 alerting.callref = trans->callref;
2057 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
2058 /* facility */
2059 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2060 alerting.fields |= MNCC_F_FACILITY;
2061 gsm48_decode_facility(&alerting.facility,
2062 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2063 }
2064
2065 /* progress */
2066 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
2067 alerting.fields |= MNCC_F_PROGRESS;
2068 gsm48_decode_progress(&alerting.progress,
2069 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
2070 }
2071 /* ss-version */
2072 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2073 alerting.fields |= MNCC_F_SSVERSION;
2074 gsm48_decode_ssversion(&alerting.ssversion,
2075 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2076 }
2077
2078 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
2079
2080 return mncc_recvmsg(trans->subscr->net, trans, MNCC_ALERT_IND,
2081 &alerting);
2082}
2083
2084static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
2085{
2086 struct gsm_mncc *alerting = arg;
2087 struct msgb *msg = gsm48_msgb_alloc();
2088 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2089
2090 gh->msg_type = GSM48_MT_CC_ALERTING;
2091
2092 /* facility */
2093 if (alerting->fields & MNCC_F_FACILITY)
2094 gsm48_encode_facility(msg, 0, &alerting->facility);
2095 /* progress */
2096 if (alerting->fields & MNCC_F_PROGRESS)
2097 gsm48_encode_progress(msg, 0, &alerting->progress);
2098 /* user-user */
2099 if (alerting->fields & MNCC_F_USERUSER)
2100 gsm48_encode_useruser(msg, 0, &alerting->useruser);
2101
2102 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
2103
2104 return gsm48_conn_sendmsg(msg, trans->conn, trans);
2105}
2106
2107static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
2108{
2109 struct gsm_mncc *progress = arg;
2110 struct msgb *msg = gsm48_msgb_alloc();
2111 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2112
2113 gh->msg_type = GSM48_MT_CC_PROGRESS;
2114
2115 /* progress */
2116 gsm48_encode_progress(msg, 1, &progress->progress);
2117 /* user-user */
2118 if (progress->fields & MNCC_F_USERUSER)
2119 gsm48_encode_useruser(msg, 0, &progress->useruser);
2120
2121 return gsm48_conn_sendmsg(msg, trans->conn, trans);
2122}
2123
2124static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
2125{
2126 struct gsm_mncc *connect = arg;
2127 struct msgb *msg = gsm48_msgb_alloc();
2128 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2129
2130 gh->msg_type = GSM48_MT_CC_CONNECT;
2131
2132 gsm48_stop_cc_timer(trans);
2133 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
2134
2135 /* facility */
2136 if (connect->fields & MNCC_F_FACILITY)
2137 gsm48_encode_facility(msg, 0, &connect->facility);
2138 /* progress */
2139 if (connect->fields & MNCC_F_PROGRESS)
2140 gsm48_encode_progress(msg, 0, &connect->progress);
2141 /* connected number */
2142 if (connect->fields & MNCC_F_CONNECTED)
2143 gsm48_encode_connected(msg, &connect->connected);
2144 /* user-user */
2145 if (connect->fields & MNCC_F_USERUSER)
2146 gsm48_encode_useruser(msg, 0, &connect->useruser);
2147
2148 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
2149
2150 return gsm48_conn_sendmsg(msg, trans->conn, trans);
2151}
2152
2153static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
2154{
2155 struct gsm48_hdr *gh = msgb_l3(msg);
2156 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2157 struct tlv_parsed tp;
2158 struct gsm_mncc connect;
2159
2160 gsm48_stop_cc_timer(trans);
2161
2162 memset(&connect, 0, sizeof(struct gsm_mncc));
2163 connect.callref = trans->callref;
2164 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
2165 /* use subscriber as connected party number */
2166 if (trans->subscr) {
2167 connect.fields |= MNCC_F_CONNECTED;
2168 strncpy(connect.connected.number, trans->subscr->extension,
2169 sizeof(connect.connected.number)-1);
2170 strncpy(connect.imsi, trans->subscr->imsi,
2171 sizeof(connect.imsi)-1);
2172 }
2173 /* facility */
2174 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2175 connect.fields |= MNCC_F_FACILITY;
2176 gsm48_decode_facility(&connect.facility,
2177 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2178 }
2179 /* user-user */
2180 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2181 connect.fields |= MNCC_F_USERUSER;
2182 gsm48_decode_useruser(&connect.useruser,
2183 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2184 }
2185 /* ss-version */
2186 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2187 connect.fields |= MNCC_F_SSVERSION;
2188 gsm48_decode_ssversion(&connect.ssversion,
2189 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2190 }
2191
2192 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
Jonathan Santos5a45b152011-08-17 15:33:57 -04002193 osmo_counter_inc(trans->subscr->net->stats.call.mt_connect);
Jonathan Santos03fd8d02011-05-25 13:54:02 -04002194
2195 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_CNF, &connect);
2196}
2197
2198
2199static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
2200{
2201 struct gsm_mncc connect_ack;
2202
2203 gsm48_stop_cc_timer(trans);
2204
2205 new_cc_state(trans, GSM_CSTATE_ACTIVE);
Jonathan Santos5a45b152011-08-17 15:33:57 -04002206 osmo_counter_inc(trans->subscr->net->stats.call.mo_connect_ack);
Jonathan Santos03fd8d02011-05-25 13:54:02 -04002207
2208 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
2209 connect_ack.callref = trans->callref;
2210
2211 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_COMPL_IND,
2212 &connect_ack);
2213}
2214
2215static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
2216{
2217 struct msgb *msg = gsm48_msgb_alloc();
2218 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2219
2220 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
2221
2222 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2223
2224 return gsm48_conn_sendmsg(msg, trans->conn, trans);
2225}
2226
2227static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
2228{
2229 struct gsm48_hdr *gh = msgb_l3(msg);
2230 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2231 struct tlv_parsed tp;
2232 struct gsm_mncc disc;
2233
2234 gsm48_stop_cc_timer(trans);
2235
2236 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
2237
2238 memset(&disc, 0, sizeof(struct gsm_mncc));
2239 disc.callref = trans->callref;
2240 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
2241 /* cause */
2242 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2243 disc.fields |= MNCC_F_CAUSE;
2244 gsm48_decode_cause(&disc.cause,
2245 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2246 }
2247 /* facility */
2248 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2249 disc.fields |= MNCC_F_FACILITY;
2250 gsm48_decode_facility(&disc.facility,
2251 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2252 }
2253 /* user-user */
2254 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2255 disc.fields |= MNCC_F_USERUSER;
2256 gsm48_decode_useruser(&disc.useruser,
2257 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2258 }
2259 /* ss-version */
2260 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2261 disc.fields |= MNCC_F_SSVERSION;
2262 gsm48_decode_ssversion(&disc.ssversion,
2263 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2264 }
2265
2266 return mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &disc);
2267
2268}
2269
2270static struct gsm_mncc_cause default_cause = {
2271 .location = GSM48_CAUSE_LOC_PRN_S_LU,
2272 .coding = 0,
2273 .rec = 0,
2274 .rec_val = 0,
2275 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
2276 .diag_len = 0,
2277 .diag = { 0 },
2278};
2279
2280static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
2281{
2282 struct gsm_mncc *disc = arg;
2283 struct msgb *msg = gsm48_msgb_alloc();
2284 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2285
2286 gh->msg_type = GSM48_MT_CC_DISCONNECT;
2287
2288 gsm48_stop_cc_timer(trans);
2289 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
2290
2291 /* cause */
2292 if (disc->fields & MNCC_F_CAUSE)
2293 gsm48_encode_cause(msg, 1, &disc->cause);
2294 else
2295 gsm48_encode_cause(msg, 1, &default_cause);
2296
2297 /* facility */
2298 if (disc->fields & MNCC_F_FACILITY)
2299 gsm48_encode_facility(msg, 0, &disc->facility);
2300 /* progress */
2301 if (disc->fields & MNCC_F_PROGRESS)
2302 gsm48_encode_progress(msg, 0, &disc->progress);
2303 /* user-user */
2304 if (disc->fields & MNCC_F_USERUSER)
2305 gsm48_encode_useruser(msg, 0, &disc->useruser);
2306
2307 /* store disconnect cause for T306 expiry */
2308 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
2309
2310 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
2311
2312 return gsm48_conn_sendmsg(msg, trans->conn, trans);
2313}
2314
2315static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
2316{
2317 struct gsm48_hdr *gh = msgb_l3(msg);
2318 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2319 struct tlv_parsed tp;
2320 struct gsm_mncc rel;
2321 int rc;
2322
2323 gsm48_stop_cc_timer(trans);
2324
2325 memset(&rel, 0, sizeof(struct gsm_mncc));
2326 rel.callref = trans->callref;
2327 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
2328 /* cause */
2329 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2330 rel.fields |= MNCC_F_CAUSE;
2331 gsm48_decode_cause(&rel.cause,
2332 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2333 }
2334 /* facility */
2335 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2336 rel.fields |= MNCC_F_FACILITY;
2337 gsm48_decode_facility(&rel.facility,
2338 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2339 }
2340 /* user-user */
2341 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2342 rel.fields |= MNCC_F_USERUSER;
2343 gsm48_decode_useruser(&rel.useruser,
2344 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2345 }
2346 /* ss-version */
2347 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2348 rel.fields |= MNCC_F_SSVERSION;
2349 gsm48_decode_ssversion(&rel.ssversion,
2350 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2351 }
2352
2353 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
2354 /* release collision 5.4.5 */
2355 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_CNF, &rel);
2356 } else {
2357 rc = gsm48_tx_simple(trans->conn,
2358 GSM48_PDISC_CC | (trans->transaction_id << 4),
2359 GSM48_MT_CC_RELEASE_COMPL);
2360 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_IND, &rel);
2361 }
2362
2363 new_cc_state(trans, GSM_CSTATE_NULL);
2364
2365 trans->callref = 0;
2366 trans_free(trans);
2367
2368 return rc;
2369}
2370
2371static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
2372{
2373 struct gsm_mncc *rel = arg;
2374 struct msgb *msg = gsm48_msgb_alloc();
2375 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2376
2377 gh->msg_type = GSM48_MT_CC_RELEASE;
2378
2379 trans->callref = 0;
2380
2381 gsm48_stop_cc_timer(trans);
2382 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
2383
2384 /* cause */
2385 if (rel->fields & MNCC_F_CAUSE)
2386 gsm48_encode_cause(msg, 0, &rel->cause);
2387 /* facility */
2388 if (rel->fields & MNCC_F_FACILITY)
2389 gsm48_encode_facility(msg, 0, &rel->facility);
2390 /* user-user */
2391 if (rel->fields & MNCC_F_USERUSER)
2392 gsm48_encode_useruser(msg, 0, &rel->useruser);
2393
2394 trans->cc.T308_second = 0;
2395 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
2396
2397 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
2398 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
2399
2400 return gsm48_conn_sendmsg(msg, trans->conn, trans);
2401}
2402
2403static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
2404{
2405 struct gsm48_hdr *gh = msgb_l3(msg);
2406 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2407 struct tlv_parsed tp;
2408 struct gsm_mncc rel;
2409 int rc = 0;
2410
2411 gsm48_stop_cc_timer(trans);
2412
2413 memset(&rel, 0, sizeof(struct gsm_mncc));
2414 rel.callref = trans->callref;
2415 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
2416 /* cause */
2417 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2418 rel.fields |= MNCC_F_CAUSE;
2419 gsm48_decode_cause(&rel.cause,
2420 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2421 }
2422 /* facility */
2423 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2424 rel.fields |= MNCC_F_FACILITY;
2425 gsm48_decode_facility(&rel.facility,
2426 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2427 }
2428 /* user-user */
2429 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2430 rel.fields |= MNCC_F_USERUSER;
2431 gsm48_decode_useruser(&rel.useruser,
2432 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2433 }
2434 /* ss-version */
2435 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2436 rel.fields |= MNCC_F_SSVERSION;
2437 gsm48_decode_ssversion(&rel.ssversion,
2438 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2439 }
2440
2441 if (trans->callref) {
2442 switch (trans->cc.state) {
2443 case GSM_CSTATE_CALL_PRESENT:
2444 rc = mncc_recvmsg(trans->subscr->net, trans,
2445 MNCC_REJ_IND, &rel);
2446 break;
2447 case GSM_CSTATE_RELEASE_REQ:
2448 rc = mncc_recvmsg(trans->subscr->net, trans,
2449 MNCC_REL_CNF, &rel);
2450 break;
2451 default:
2452 rc = mncc_recvmsg(trans->subscr->net, trans,
2453 MNCC_REL_IND, &rel);
2454 }
2455 }
2456
2457 trans->callref = 0;
2458 trans_free(trans);
2459
2460 return rc;
2461}
2462
2463static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
2464{
2465 struct gsm_mncc *rel = arg;
2466 struct msgb *msg = gsm48_msgb_alloc();
2467 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2468 int ret;
2469
2470 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
2471
2472 trans->callref = 0;
2473
2474 gsm48_stop_cc_timer(trans);
2475
2476 /* cause */
2477 if (rel->fields & MNCC_F_CAUSE)
2478 gsm48_encode_cause(msg, 0, &rel->cause);
2479 /* facility */
2480 if (rel->fields & MNCC_F_FACILITY)
2481 gsm48_encode_facility(msg, 0, &rel->facility);
2482 /* user-user */
2483 if (rel->fields & MNCC_F_USERUSER)
2484 gsm48_encode_useruser(msg, 0, &rel->useruser);
2485
2486 ret = gsm48_conn_sendmsg(msg, trans->conn, trans);
2487
2488 trans_free(trans);
2489
2490 return ret;
2491}
2492
2493static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
2494{
2495 struct gsm48_hdr *gh = msgb_l3(msg);
2496 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2497 struct tlv_parsed tp;
2498 struct gsm_mncc fac;
2499
2500 memset(&fac, 0, sizeof(struct gsm_mncc));
2501 fac.callref = trans->callref;
2502 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
2503 /* facility */
2504 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2505 fac.fields |= MNCC_F_FACILITY;
2506 gsm48_decode_facility(&fac.facility,
2507 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2508 }
2509 /* ss-version */
2510 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2511 fac.fields |= MNCC_F_SSVERSION;
2512 gsm48_decode_ssversion(&fac.ssversion,
2513 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2514 }
2515
2516 return mncc_recvmsg(trans->subscr->net, trans, MNCC_FACILITY_IND, &fac);
2517}
2518
2519static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
2520{
2521 struct gsm_mncc *fac = arg;
2522 struct msgb *msg = gsm48_msgb_alloc();
2523 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2524
2525 gh->msg_type = GSM48_MT_CC_FACILITY;
2526
2527 /* facility */
2528 gsm48_encode_facility(msg, 1, &fac->facility);
2529
2530 return gsm48_conn_sendmsg(msg, trans->conn, trans);
2531}
2532
2533static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
2534{
2535 struct gsm_mncc hold;
2536
2537 memset(&hold, 0, sizeof(struct gsm_mncc));
2538 hold.callref = trans->callref;
2539 return mncc_recvmsg(trans->subscr->net, trans, MNCC_HOLD_IND, &hold);
2540}
2541
2542static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
2543{
2544 struct msgb *msg = gsm48_msgb_alloc();
2545 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2546
2547 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
2548
2549 return gsm48_conn_sendmsg(msg, trans->conn, trans);
2550}
2551
2552static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
2553{
2554 struct gsm_mncc *hold_rej = arg;
2555 struct msgb *msg = gsm48_msgb_alloc();
2556 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2557
2558 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
2559
2560 /* cause */
2561 if (hold_rej->fields & MNCC_F_CAUSE)
2562 gsm48_encode_cause(msg, 1, &hold_rej->cause);
2563 else
2564 gsm48_encode_cause(msg, 1, &default_cause);
2565
2566 return gsm48_conn_sendmsg(msg, trans->conn, trans);
2567}
2568
2569static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
2570{
2571 struct gsm_mncc retrieve;
2572
2573 memset(&retrieve, 0, sizeof(struct gsm_mncc));
2574 retrieve.callref = trans->callref;
2575 return mncc_recvmsg(trans->subscr->net, trans, MNCC_RETRIEVE_IND,
2576 &retrieve);
2577}
2578
2579static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
2580{
2581 struct msgb *msg = gsm48_msgb_alloc();
2582 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2583
2584 gh->msg_type = GSM48_MT_CC_RETR_ACK;
2585
2586 return gsm48_conn_sendmsg(msg, trans->conn, trans);
2587}
2588
2589static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
2590{
2591 struct gsm_mncc *retrieve_rej = arg;
2592 struct msgb *msg = gsm48_msgb_alloc();
2593 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2594
2595 gh->msg_type = GSM48_MT_CC_RETR_REJ;
2596
2597 /* cause */
2598 if (retrieve_rej->fields & MNCC_F_CAUSE)
2599 gsm48_encode_cause(msg, 1, &retrieve_rej->cause);
2600 else
2601 gsm48_encode_cause(msg, 1, &default_cause);
2602
2603 return gsm48_conn_sendmsg(msg, trans->conn, trans);
2604}
2605
2606static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
2607{
2608 struct gsm48_hdr *gh = msgb_l3(msg);
2609 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2610 struct tlv_parsed tp;
2611 struct gsm_mncc dtmf;
2612
2613 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2614 dtmf.callref = trans->callref;
2615 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
2616 /* keypad facility */
2617 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
2618 dtmf.fields |= MNCC_F_KEYPAD;
2619 gsm48_decode_keypad(&dtmf.keypad,
2620 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
2621 }
2622
2623 return mncc_recvmsg(trans->subscr->net, trans, MNCC_START_DTMF_IND, &dtmf);
2624}
2625
2626static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
2627{
2628 struct gsm_mncc *dtmf = arg;
2629 struct msgb *msg = gsm48_msgb_alloc();
2630 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2631
2632 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
2633
2634 /* keypad */
2635 if (dtmf->fields & MNCC_F_KEYPAD)
2636 gsm48_encode_keypad(msg, dtmf->keypad);
2637
2638 return gsm48_conn_sendmsg(msg, trans->conn, trans);
2639}
2640
2641static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
2642{
2643 struct gsm_mncc *dtmf = arg;
2644 struct msgb *msg = gsm48_msgb_alloc();
2645 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2646
2647 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
2648
2649 /* cause */
2650 if (dtmf->fields & MNCC_F_CAUSE)
2651 gsm48_encode_cause(msg, 1, &dtmf->cause);
2652 else
2653 gsm48_encode_cause(msg, 1, &default_cause);
2654
2655 return gsm48_conn_sendmsg(msg, trans->conn, trans);
2656}
2657
2658static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
2659{
2660 struct msgb *msg = gsm48_msgb_alloc();
2661 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2662
2663 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
2664
2665 return gsm48_conn_sendmsg(msg, trans->conn, trans);
2666}
2667
2668static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
2669{
2670 struct gsm_mncc dtmf;
2671
2672 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2673 dtmf.callref = trans->callref;
2674
2675 return mncc_recvmsg(trans->subscr->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
2676}
2677
2678static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
2679{
2680 struct gsm48_hdr *gh = msgb_l3(msg);
2681 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2682 struct tlv_parsed tp;
2683 struct gsm_mncc modify;
2684
2685 memset(&modify, 0, sizeof(struct gsm_mncc));
2686 modify.callref = trans->callref;
2687 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
2688 /* bearer capability */
2689 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2690 modify.fields |= MNCC_F_BEARER_CAP;
2691 gsm48_decode_bearer_cap(&modify.bearer_cap,
2692 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2693 }
2694
2695 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
2696
2697 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_IND, &modify);
2698}
2699
2700static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
2701{
2702 struct gsm_mncc *modify = arg;
2703 struct msgb *msg = gsm48_msgb_alloc();
2704 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2705
2706 gh->msg_type = GSM48_MT_CC_MODIFY;
2707
2708 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
2709
2710 /* bearer capability */
2711 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
2712
2713 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
2714
2715 return gsm48_conn_sendmsg(msg, trans->conn, trans);
2716}
2717
2718static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
2719{
2720 struct gsm48_hdr *gh = msgb_l3(msg);
2721 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2722 struct tlv_parsed tp;
2723 struct gsm_mncc modify;
2724
2725 gsm48_stop_cc_timer(trans);
2726
2727 memset(&modify, 0, sizeof(struct gsm_mncc));
2728 modify.callref = trans->callref;
2729 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
2730 /* bearer capability */
2731 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2732 modify.fields |= MNCC_F_BEARER_CAP;
2733 gsm48_decode_bearer_cap(&modify.bearer_cap,
2734 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2735 }
2736
2737 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2738
2739 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_CNF, &modify);
2740}
2741
2742static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
2743{
2744 struct gsm_mncc *modify = arg;
2745 struct msgb *msg = gsm48_msgb_alloc();
2746 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2747
2748 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
2749
2750 /* bearer capability */
2751 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
2752
2753 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2754
2755 return gsm48_conn_sendmsg(msg, trans->conn, trans);
2756}
2757
2758static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
2759{
2760 struct gsm48_hdr *gh = msgb_l3(msg);
2761 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2762 struct tlv_parsed tp;
2763 struct gsm_mncc modify;
2764
2765 gsm48_stop_cc_timer(trans);
2766
2767 memset(&modify, 0, sizeof(struct gsm_mncc));
2768 modify.callref = trans->callref;
2769 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
2770 /* bearer capability */
2771 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2772 modify.fields |= GSM48_IE_BEARER_CAP;
2773 gsm48_decode_bearer_cap(&modify.bearer_cap,
2774 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2775 }
2776 /* cause */
2777 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2778 modify.fields |= MNCC_F_CAUSE;
2779 gsm48_decode_cause(&modify.cause,
2780 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2781 }
2782
2783 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2784
2785 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_REJ, &modify);
2786}
2787
2788static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
2789{
2790 struct gsm_mncc *modify = arg;
2791 struct msgb *msg = gsm48_msgb_alloc();
2792 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2793
2794 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
2795
2796 /* bearer capability */
2797 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
2798 /* cause */
2799 gsm48_encode_cause(msg, 1, &modify->cause);
2800
2801 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2802
2803 return gsm48_conn_sendmsg(msg, trans->conn, trans);
2804}
2805
2806static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
2807{
2808 struct gsm_mncc *notify = arg;
2809 struct msgb *msg = gsm48_msgb_alloc();
2810 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2811
2812 gh->msg_type = GSM48_MT_CC_NOTIFY;
2813
2814 /* notify */
2815 gsm48_encode_notify(msg, notify->notify);
2816
2817 return gsm48_conn_sendmsg(msg, trans->conn, trans);
2818}
2819
2820static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
2821{
2822 struct gsm48_hdr *gh = msgb_l3(msg);
2823 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2824// struct tlv_parsed tp;
2825 struct gsm_mncc notify;
2826
2827 memset(&notify, 0, sizeof(struct gsm_mncc));
2828 notify.callref = trans->callref;
2829// tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len);
2830 if (payload_len >= 1)
2831 gsm48_decode_notify(&notify.notify, gh->data);
2832
2833 return mncc_recvmsg(trans->subscr->net, trans, MNCC_NOTIFY_IND, &notify);
2834}
2835
2836static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
2837{
2838 struct gsm_mncc *user = arg;
2839 struct msgb *msg = gsm48_msgb_alloc();
2840 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2841
2842 gh->msg_type = GSM48_MT_CC_USER_INFO;
2843
2844 /* user-user */
2845 if (user->fields & MNCC_F_USERUSER)
2846 gsm48_encode_useruser(msg, 1, &user->useruser);
2847 /* more data */
2848 if (user->more)
2849 gsm48_encode_more(msg);
2850
2851 return gsm48_conn_sendmsg(msg, trans->conn, trans);
2852}
2853
2854static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
2855{
2856 struct gsm48_hdr *gh = msgb_l3(msg);
2857 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2858 struct tlv_parsed tp;
2859 struct gsm_mncc user;
2860
2861 memset(&user, 0, sizeof(struct gsm_mncc));
2862 user.callref = trans->callref;
2863 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
2864 /* user-user */
2865 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2866 user.fields |= MNCC_F_USERUSER;
2867 gsm48_decode_useruser(&user.useruser,
2868 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2869 }
2870 /* more data */
2871 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
2872 user.more = 1;
2873
2874 return mncc_recvmsg(trans->subscr->net, trans, MNCC_USERINFO_IND, &user);
2875}
2876
2877static int _gsm48_lchan_modify(struct gsm_trans *trans, void *arg)
2878{
2879 struct gsm_mncc *mode = arg;
2880
2881 return gsm0808_assign_req(trans->conn, mode->lchan_mode, 1);
2882}
2883
2884static struct downstate {
Jonathan Santos5a45b152011-08-17 15:33:57 -04002885 uint32_t states;
Jonathan Santos03fd8d02011-05-25 13:54:02 -04002886 int type;
2887 int (*rout) (struct gsm_trans *trans, void *arg);
2888} downstatelist[] = {
2889 /* mobile originating call establishment */
2890 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
2891 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc},
2892 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
2893 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
2894 {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 */
2895 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
2896 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
2897 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
2898 /* mobile terminating call establishment */
2899 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
2900 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
2901 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
2902 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
2903 /* signalling during call */
2904 {SBIT(GSM_CSTATE_ACTIVE),
2905 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
2906 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
2907 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
2908 {ALL_STATES,
2909 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
2910 {ALL_STATES,
2911 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
2912 {ALL_STATES,
2913 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
2914 {SBIT(GSM_CSTATE_ACTIVE),
2915 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
2916 {SBIT(GSM_CSTATE_ACTIVE),
2917 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
2918 {SBIT(GSM_CSTATE_ACTIVE),
2919 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
2920 {SBIT(GSM_CSTATE_ACTIVE),
2921 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
2922 {SBIT(GSM_CSTATE_ACTIVE),
2923 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
2924 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2925 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
2926 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2927 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
2928 {SBIT(GSM_CSTATE_ACTIVE),
2929 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
2930 /* clearing */
2931 {SBIT(GSM_CSTATE_INITIATED),
2932 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
2933 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
2934 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
2935 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
2936 MNCC_REL_REQ, gsm48_cc_tx_release},
2937 /* special */
2938 {ALL_STATES,
2939 MNCC_LCHAN_MODIFY, _gsm48_lchan_modify},
2940};
2941
2942#define DOWNSLLEN \
2943 (sizeof(downstatelist) / sizeof(struct downstate))
2944
2945
2946int mncc_tx_to_cc(struct gsm_network *net, int msg_type, void *arg)
2947{
2948 int i, rc = 0;
2949 struct gsm_trans *trans = NULL, *transt;
2950 struct gsm_subscriber_connection *conn = NULL;
2951 struct gsm_bts *bts = NULL;
2952 struct gsm_mncc *data = arg, rel;
2953
2954 DEBUGP(DMNCC, "receive message %s\n", get_mncc_name(msg_type));
2955
2956 /* handle special messages */
2957 switch(msg_type) {
2958 case MNCC_BRIDGE:
2959 return tch_bridge(net, arg);
2960 case MNCC_FRAME_DROP:
2961 return tch_recv_mncc(net, data->callref, 0);
2962 case MNCC_FRAME_RECV:
2963 return tch_recv_mncc(net, data->callref, 1);
2964 case GSM_TCHF_FRAME:
2965 /* Find callref */
2966 trans = trans_find_by_callref(net, data->callref);
2967 if (!trans) {
2968 LOGP(DMNCC, LOGL_ERROR, "TCH frame for non-existing trans\n");
2969 return -EIO;
2970 }
2971 if (!trans->conn) {
2972 LOGP(DMNCC, LOGL_NOTICE, "TCH frame for trans without conn\n");
2973 return 0;
2974 }
2975 if (trans->conn->lchan->type != GSM_LCHAN_TCH_F) {
2976 /* This should be LOGL_ERROR or NOTICE, but
2977 * unfortuantely it happens for a couple of frames at
2978 * the beginning of every RTP connection */
2979 LOGP(DMNCC, LOGL_DEBUG, "TCH frame for lchan != TCH_F\n");
2980 return 0;
2981 }
2982 bts = trans->conn->lchan->ts->trx->bts;
2983 switch (bts->type) {
2984 case GSM_BTS_TYPE_NANOBTS:
2985 if (!trans->conn->lchan->abis_ip.rtp_socket) {
2986 DEBUGP(DMNCC, "TCH frame to lchan without RTP connection\n");
2987 return 0;
2988 }
2989 return rtp_send_frame(trans->conn->lchan->abis_ip.rtp_socket, arg);
2990 case GSM_BTS_TYPE_BS11:
Jonathan Santos5a45b152011-08-17 15:33:57 -04002991 case GSM_BTS_TYPE_RBS2000:
Jonathan Santos03fd8d02011-05-25 13:54:02 -04002992 return trau_send_frame(trans->conn->lchan, arg);
2993 default:
2994 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
2995 }
2996 return -EINVAL;
2997 }
2998
2999 memset(&rel, 0, sizeof(struct gsm_mncc));
3000 rel.callref = data->callref;
3001
3002 /* Find callref */
3003 trans = trans_find_by_callref(net, data->callref);
3004
3005 /* Callref unknown */
3006 if (!trans) {
3007 struct gsm_subscriber *subscr;
3008
3009 if (msg_type != MNCC_SETUP_REQ) {
3010 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3011 "Received '%s' from MNCC with "
3012 "unknown callref %d\n", data->called.number,
3013 get_mncc_name(msg_type), data->callref);
3014 /* Invalid call reference */
3015 return mncc_release_ind(net, NULL, data->callref,
3016 GSM48_CAUSE_LOC_PRN_S_LU,
3017 GSM48_CC_CAUSE_INVAL_TRANS_ID);
3018 }
3019 if (!data->called.number[0] && !data->imsi[0]) {
3020 DEBUGP(DCC, "(bts - trx - ts - ti) "
3021 "Received '%s' from MNCC with "
3022 "no number or IMSI\n", get_mncc_name(msg_type));
3023 /* Invalid number */
3024 return mncc_release_ind(net, NULL, data->callref,
3025 GSM48_CAUSE_LOC_PRN_S_LU,
3026 GSM48_CC_CAUSE_INV_NR_FORMAT);
3027 }
3028 /* New transaction due to setup, find subscriber */
3029 if (data->called.number[0])
3030 subscr = subscr_get_by_extension(net,
3031 data->called.number);
3032 else
3033 subscr = subscr_get_by_imsi(net, data->imsi);
3034 /* If subscriber is not found */
3035 if (!subscr) {
3036 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3037 "Received '%s' from MNCC with "
3038 "unknown subscriber %s\n", data->called.number,
3039 get_mncc_name(msg_type), data->called.number);
3040 /* Unknown subscriber */
3041 return mncc_release_ind(net, NULL, data->callref,
3042 GSM48_CAUSE_LOC_PRN_S_LU,
3043 GSM48_CC_CAUSE_UNASSIGNED_NR);
3044 }
3045 /* If subscriber is not "attached" */
3046 if (!subscr->lac) {
3047 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3048 "Received '%s' from MNCC with "
3049 "detached subscriber %s\n", data->called.number,
3050 get_mncc_name(msg_type), data->called.number);
3051 subscr_put(subscr);
3052 /* Temporarily out of order */
3053 return mncc_release_ind(net, NULL, data->callref,
3054 GSM48_CAUSE_LOC_PRN_S_LU,
3055 GSM48_CC_CAUSE_DEST_OOO);
3056 }
3057 /* Create transaction */
3058 trans = trans_alloc(subscr, GSM48_PDISC_CC, 0xff, data->callref);
3059 if (!trans) {
3060 DEBUGP(DCC, "No memory for trans.\n");
3061 subscr_put(subscr);
3062 /* Ressource unavailable */
3063 mncc_release_ind(net, NULL, data->callref,
3064 GSM48_CAUSE_LOC_PRN_S_LU,
3065 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
3066 return -ENOMEM;
3067 }
3068 /* Find lchan */
3069 conn = connection_for_subscr(subscr);
3070
3071 /* If subscriber has no lchan */
3072 if (!conn) {
3073 /* find transaction with this subscriber already paging */
3074 llist_for_each_entry(transt, &net->trans_list, entry) {
3075 /* Transaction of our lchan? */
3076 if (transt == trans ||
3077 transt->subscr != subscr)
3078 continue;
3079 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3080 "Received '%s' from MNCC with "
3081 "unallocated channel, paging already "
3082 "started for lac %d.\n",
3083 data->called.number,
3084 get_mncc_name(msg_type), subscr->lac);
3085 subscr_put(subscr);
3086 trans_free(trans);
3087 return 0;
3088 }
3089 /* store setup informations until paging was successfull */
3090 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
3091
3092 /* Get a channel */
3093 trans->paging_request = talloc_zero(subscr->net, struct gsm_network*);
3094 if (!trans->paging_request) {
3095 LOGP(DCC, LOGL_ERROR, "Failed to allocate paging token.\n");
3096 subscr_put(subscr);
3097 trans_free(trans);
3098 return 0;
3099 }
3100
3101 *trans->paging_request = subscr->net;
3102 subscr_get_channel(subscr, RSL_CHANNEED_TCH_F, setup_trig_pag_evt, trans->paging_request);
3103
3104 subscr_put(subscr);
3105 return 0;
3106 }
3107 /* Assign lchan */
3108 trans->conn = conn;
3109 subscr_put(subscr);
3110 }
3111
3112 if (trans->conn)
3113 conn = trans->conn;
3114
3115 /* if paging did not respond yet */
3116 if (!conn) {
3117 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3118 "Received '%s' from MNCC in paging state\n",
3119 (trans->subscr)?(trans->subscr->extension):"-",
3120 get_mncc_name(msg_type));
3121 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
3122 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
3123 if (msg_type == MNCC_REL_REQ)
3124 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
3125 else
3126 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
3127 trans->callref = 0;
3128 trans_free(trans);
3129 return rc;
3130 }
3131
3132 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x sub %s) "
3133 "Received '%s' from MNCC in state %d (%s)\n",
3134 conn->bts->nr, conn->lchan->ts->trx->nr, conn->lchan->ts->nr,
3135 trans->transaction_id,
3136 (trans->conn->subscr)?(trans->conn->subscr->extension):"-",
3137 get_mncc_name(msg_type), trans->cc.state,
3138 gsm48_cc_state_name(trans->cc.state));
3139
3140 /* Find function for current state and message */
3141 for (i = 0; i < DOWNSLLEN; i++)
3142 if ((msg_type == downstatelist[i].type)
3143 && ((1 << trans->cc.state) & downstatelist[i].states))
3144 break;
3145 if (i == DOWNSLLEN) {
3146 DEBUGP(DCC, "Message unhandled at this state.\n");
3147 return 0;
3148 }
3149
3150 rc = downstatelist[i].rout(trans, arg);
3151
3152 return rc;
3153}
3154
3155
3156static struct datastate {
Jonathan Santos5a45b152011-08-17 15:33:57 -04003157 uint32_t states;
Jonathan Santos03fd8d02011-05-25 13:54:02 -04003158 int type;
3159 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
3160} datastatelist[] = {
3161 /* mobile originating call establishment */
3162 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3163 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
3164 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3165 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
3166 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
3167 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
3168 /* mobile terminating call establishment */
3169 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
3170 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
3171 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
3172 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
3173 {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 */
3174 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
3175 /* signalling during call */
3176 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
3177 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
3178 {SBIT(GSM_CSTATE_ACTIVE),
3179 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
3180 {ALL_STATES,
3181 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
3182 {ALL_STATES,
3183 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
3184 {ALL_STATES,
3185 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
3186 {SBIT(GSM_CSTATE_ACTIVE),
3187 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
3188 {SBIT(GSM_CSTATE_ACTIVE),
3189 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
3190 {SBIT(GSM_CSTATE_ACTIVE),
3191 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
3192 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3193 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
3194 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3195 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
3196 {SBIT(GSM_CSTATE_ACTIVE),
3197 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
3198 /* clearing */
3199 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
3200 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
3201 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
3202 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
3203 {ALL_STATES, /* 5.4.3.4 */
3204 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
3205};
3206
3207#define DATASLLEN \
3208 (sizeof(datastatelist) / sizeof(struct datastate))
3209
3210static int gsm0408_rcv_cc(struct gsm_subscriber_connection *conn, struct msgb *msg)
3211{
3212 struct gsm48_hdr *gh = msgb_l3(msg);
Jonathan Santos5a45b152011-08-17 15:33:57 -04003213 uint8_t msg_type = gh->msg_type & 0xbf;
3214 uint8_t transaction_id = ((gh->proto_discr & 0xf0) ^ 0x80) >> 4; /* flip */
Jonathan Santos03fd8d02011-05-25 13:54:02 -04003215 struct gsm_trans *trans = NULL;
3216 int i, rc = 0;
3217
3218 if (msg_type & 0x80) {
3219 DEBUGP(DCC, "MSG 0x%2x not defined for PD error\n", msg_type);
3220 return -EINVAL;
3221 }
3222
3223 /* Find transaction */
3224 trans = trans_find_by_id(conn->subscr, GSM48_PDISC_CC, transaction_id);
3225
3226 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
3227 "Received '%s' from MS in state %d (%s)\n",
3228 conn->bts->nr, conn->lchan->ts->trx->nr, conn->lchan->ts->nr,
3229 transaction_id, (conn->subscr)?(conn->subscr->extension):"-",
3230 gsm48_cc_msg_name(msg_type), trans?(trans->cc.state):0,
3231 gsm48_cc_state_name(trans?(trans->cc.state):0));
3232
3233 /* Create transaction */
3234 if (!trans) {
3235 DEBUGP(DCC, "Unknown transaction ID %x, "
3236 "creating new trans.\n", transaction_id);
3237 /* Create transaction */
3238 trans = trans_alloc(conn->subscr, GSM48_PDISC_CC,
3239 transaction_id, new_callref++);
3240 if (!trans) {
3241 DEBUGP(DCC, "No memory for trans.\n");
3242 rc = gsm48_tx_simple(conn,
3243 GSM48_PDISC_CC | (transaction_id << 4),
3244 GSM48_MT_CC_RELEASE_COMPL);
3245 return -ENOMEM;
3246 }
3247 /* Assign transaction */
3248 trans->conn = conn;
3249 }
3250
3251 /* find function for current state and message */
3252 for (i = 0; i < DATASLLEN; i++)
3253 if ((msg_type == datastatelist[i].type)
3254 && ((1 << trans->cc.state) & datastatelist[i].states))
3255 break;
3256 if (i == DATASLLEN) {
3257 DEBUGP(DCC, "Message unhandled at this state.\n");
3258 return 0;
3259 }
3260
3261 rc = datastatelist[i].rout(trans, msg);
3262
3263 return rc;
3264}
3265
3266/* Create a dummy to wait five seconds */
3267static void release_anchor(struct gsm_subscriber_connection *conn)
3268{
3269 if (!conn->anch_operation)
3270 return;
3271
Jonathan Santos5a45b152011-08-17 15:33:57 -04003272 osmo_timer_del(&conn->anch_operation->timeout);
Jonathan Santos03fd8d02011-05-25 13:54:02 -04003273 talloc_free(conn->anch_operation);
3274 conn->anch_operation = NULL;
3275}
3276
3277static void anchor_timeout(void *_data)
3278{
3279 struct gsm_subscriber_connection *con = _data;
3280
3281 release_anchor(con);
3282 msc_release_connection(con);
3283}
3284
3285int gsm0408_new_conn(struct gsm_subscriber_connection *conn)
3286{
3287 conn->anch_operation = talloc_zero(conn, struct gsm_anchor_operation);
3288 if (!conn->anch_operation)
3289 return -1;
3290
3291 conn->anch_operation->timeout.data = conn;
3292 conn->anch_operation->timeout.cb = anchor_timeout;
Jonathan Santos5a45b152011-08-17 15:33:57 -04003293 osmo_timer_schedule(&conn->anch_operation->timeout, 5, 0);
Jonathan Santos03fd8d02011-05-25 13:54:02 -04003294 return 0;
3295}
3296
3297/* here we get data from the BSC level... */
3298int gsm0408_dispatch(struct gsm_subscriber_connection *conn, struct msgb *msg)
3299{
3300 struct gsm48_hdr *gh = msgb_l3(msg);
Jonathan Santos5a45b152011-08-17 15:33:57 -04003301 uint8_t pdisc = gh->proto_discr & 0x0f;
Jonathan Santos03fd8d02011-05-25 13:54:02 -04003302 int rc = 0;
3303
3304 if (silent_call_reroute(conn, msg))
3305 return silent_call_rx(conn, msg);
3306
3307 switch (pdisc) {
3308 case GSM48_PDISC_CC:
3309 release_anchor(conn);
3310 rc = gsm0408_rcv_cc(conn, msg);
3311 break;
3312 case GSM48_PDISC_MM:
3313 rc = gsm0408_rcv_mm(conn, msg);
3314 break;
3315 case GSM48_PDISC_RR:
3316 rc = gsm0408_rcv_rr(conn, msg);
3317 break;
3318 case GSM48_PDISC_SMS:
3319 release_anchor(conn);
3320 rc = gsm0411_rcv_sms(conn, msg);
3321 break;
3322 case GSM48_PDISC_MM_GPRS:
3323 case GSM48_PDISC_SM_GPRS:
3324 LOGP(DRLL, LOGL_NOTICE, "Unimplemented "
3325 "GSM 04.08 discriminator 0x%02x\n", pdisc);
3326 break;
3327 case GSM48_PDISC_NC_SS:
3328 release_anchor(conn);
3329 rc = handle_rcv_ussd(conn, msg);
3330 break;
3331 default:
3332 LOGP(DRLL, LOGL_NOTICE, "Unknown "
3333 "GSM 04.08 discriminator 0x%02x\n", pdisc);
3334 break;
3335 }
3336
3337 return rc;
3338}
3339
3340/*
3341 * This will be ran by the linker when loading the DSO. We use it to
3342 * do system initialization, e.g. registration of signal handlers.
3343 */
3344static __attribute__((constructor)) void on_dso_load_0408(void)
3345{
Jonathan Santos5a45b152011-08-17 15:33:57 -04003346 osmo_signal_register_handler(SS_HO, handle_ho_signal, NULL);
3347 osmo_signal_register_handler(SS_ABISIP, handle_abisip_signal, NULL);
Jonathan Santos03fd8d02011-05-25 13:54:02 -04003348}