12dc3be4018a5d618dd3dd78b23a26f864ee9006
[osmocom-bb.git] / src / host / layer23 / src / mobile / gsm48_cc.c
1 /*
2  * (C) 2010 by Andreas Eversberg <jolly@eversberg.eu>
3  *
4  * All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19  *
20  */
21
22 #include <stdint.h>
23 #include <errno.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <stdlib.h>
27
28 #include <osmocore/msgb.h>
29 #include <osmocore/utils.h>
30 #include <osmocore/gsm48.h>
31 #include <osmocore/talloc.h>
32
33 #include <osmocom/bb/common/logging.h>
34 #include <osmocom/bb/common/osmocom_data.h>
35 #include <osmocom/bb/mobile/mncc.h>
36 #include <osmocom/bb/mobile/transaction.h>
37 #include <osmocom/bb/mobile/gsm48_cc.h>
38
39 extern void *l23_ctx;
40
41 static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg);
42 static int gsm48_rel_null_free(struct gsm_trans *trans);
43 int mncc_release_ind(struct osmocom_ms *ms, struct gsm_trans *trans,
44                      u_int32_t callref, int location, int value);
45 static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg);
46 static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg);
47
48 /*
49  * init
50  */
51
52 int gsm48_cc_init(struct osmocom_ms *ms)
53 {
54         struct gsm48_cclayer *cc = &ms->cclayer;
55
56         cc->ms = ms;
57
58         if (!cc->mncc_upqueue.next == 0)
59                 return 0;
60
61         LOGP(DCC, LOGL_INFO, "init Call Control\n");
62
63         INIT_LLIST_HEAD(&cc->mncc_upqueue);
64
65         return 0;
66 }
67
68 int gsm48_cc_exit(struct osmocom_ms *ms)
69 {
70         struct gsm48_cclayer *cc = &ms->cclayer;
71         struct gsm_trans *trans, *trans2;
72         struct msgb *msg;
73
74         LOGP(DCC, LOGL_INFO, "exit Call Control processes for %s\n", ms->name);
75
76         llist_for_each_entry_safe(trans, trans2, &ms->trans_list, entry) {
77                 if (trans->protocol == GSM48_PDISC_CC)
78                         LOGP(DCC, LOGL_NOTICE, "Free pendig CC-transaction.\n");
79                         trans_free(trans);
80         }
81
82         while ((msg = msgb_dequeue(&cc->mncc_upqueue)))
83                 msgb_free(msg);
84
85         return 0;
86 }
87
88 /*
89  * messages
90  */
91
92 /* names of MNCC-SAP */
93 static const struct value_string gsm_mncc_names[] = {
94         { MNCC_SETUP_REQ,       "MNCC_SETUP_REQ" },
95         { MNCC_SETUP_IND,       "MNCC_SETUP_IND" },
96         { MNCC_SETUP_RSP,       "MNCC_SETUP_RSP" },
97         { MNCC_SETUP_CNF,       "MNCC_SETUP_CNF" },
98         { MNCC_SETUP_COMPL_REQ, "MNCC_SETUP_COMPL_REQ" },
99         { MNCC_SETUP_COMPL_IND, "MNCC_SETUP_COMPL_IND" },
100         { MNCC_CALL_CONF_IND,   "MNCC_CALL_CONF_IND" },
101         { MNCC_CALL_PROC_REQ,   "MNCC_CALL_PROC_REQ" },
102         { MNCC_PROGRESS_REQ,    "MNCC_PROGRESS_REQ" },
103         { MNCC_ALERT_REQ,       "MNCC_ALERT_REQ" },
104         { MNCC_ALERT_IND,       "MNCC_ALERT_IND" },
105         { MNCC_NOTIFY_REQ,      "MNCC_NOTIFY_REQ" },
106         { MNCC_NOTIFY_IND,      "MNCC_NOTIFY_IND" },
107         { MNCC_DISC_REQ,        "MNCC_DISC_REQ" },
108         { MNCC_DISC_IND,        "MNCC_DISC_IND" },
109         { MNCC_REL_REQ,         "MNCC_REL_REQ" },
110         { MNCC_REL_IND,         "MNCC_REL_IND" },
111         { MNCC_REL_CNF,         "MNCC_REL_CNF" },
112         { MNCC_FACILITY_REQ,    "MNCC_FACILITY_REQ" },
113         { MNCC_FACILITY_IND,    "MNCC_FACILITY_IND" },
114         { MNCC_START_DTMF_IND,  "MNCC_START_DTMF_IND" },
115         { MNCC_START_DTMF_RSP,  "MNCC_START_DTMF_RSP" },
116         { MNCC_START_DTMF_REJ,  "MNCC_START_DTMF_REJ" },
117         { MNCC_STOP_DTMF_IND,   "MNCC_STOP_DTMF_IND" },
118         { MNCC_STOP_DTMF_RSP,   "MNCC_STOP_DTMF_RSP" },
119         { MNCC_MODIFY_REQ,      "MNCC_MODIFY_REQ" },
120         { MNCC_MODIFY_IND,      "MNCC_MODIFY_IND" },
121         { MNCC_MODIFY_RSP,      "MNCC_MODIFY_RSP" },
122         { MNCC_MODIFY_CNF,      "MNCC_MODIFY_CNF" },
123         { MNCC_MODIFY_REJ,      "MNCC_MODIFY_REJ" },
124         { MNCC_HOLD_IND,        "MNCC_HOLD_IND" },
125         { MNCC_HOLD_CNF,        "MNCC_HOLD_CNF" },
126         { MNCC_HOLD_REJ,        "MNCC_HOLD_REJ" },
127         { MNCC_RETRIEVE_IND,    "MNCC_RETRIEVE_IND" },
128         { MNCC_RETRIEVE_CNF,    "MNCC_RETRIEVE_CNF" },
129         { MNCC_RETRIEVE_REJ,    "MNCC_RETRIEVE_REJ" },
130         { MNCC_USERINFO_REQ,    "MNCC_USERINFO_REQ" },
131         { MNCC_USERINFO_IND,    "MNCC_USERINFO_IND" },
132         { MNCC_REJ_REQ,         "MNCC_REJ_REQ" },
133         { MNCC_REJ_IND,         "MNCC_REJ_IND" },
134         { MNCC_PROGRESS_IND,    "MNCC_PROGRESS_IND" },
135         { MNCC_CALL_PROC_IND,   "MNCC_CALL_PROC_IND" },
136         { MNCC_CALL_CONF_REQ,   "MNCC_CALL_CONF_REQ" },
137         { MNCC_START_DTMF_REQ,  "MNCC_START_DTMF_REQ" },
138         { MNCC_STOP_DTMF_REQ,   "MNCC_STOP_DTMF_REQ" },
139         { MNCC_HOLD_REQ,        "MNCC_HOLD_REQ " },
140         { MNCC_RETRIEVE_REQ,    "MNCC_RETRIEVE_REQ" },
141         { 0,                    NULL }
142 };
143
144 const char *get_mncc_name(int value)
145 {
146         return get_value_string(gsm_mncc_names, value);
147 }
148
149 /* push MMCC header and send to MM */
150 static int gsm48_cc_to_mm(struct msgb *msg, struct gsm_trans *trans,
151         int msg_type)
152 {
153         struct gsm48_hdr *gh = msgb_l3(msg);
154         struct gsm48_mmxx_hdr *mmh;
155         int emergency = 0;
156
157         /* Add protocol type and transaction ID */
158         gh->proto_discr = trans->protocol | (trans->transaction_id << 4);
159
160         /* indicate emergency setup to MM layer */
161         if (gh->msg_type == GSM48_MT_CC_EMERG_SETUP)
162                 emergency = 1;
163
164         /* push RR header */
165         msgb_push(msg, sizeof(struct gsm48_mmxx_hdr));
166         mmh = (struct gsm48_mmxx_hdr *)msg->data;
167         mmh->msg_type = msg_type;
168         mmh->ref = trans->callref;
169         mmh->transaction_id = trans->transaction_id;
170         mmh->emergency = emergency;
171
172         /* send message to MM */
173         LOGP(DCC, LOGL_INFO, "Sending '%s' using %s (callref=%x, "
174                 "transaction_id=%d)\n", gsm48_cc_msg_name(gh->msg_type),
175                 get_mmxx_name(msg_type), trans->callref, trans->transaction_id);
176         return gsm48_mmxx_downmsg(trans->ms, msg);
177 }
178
179 /* enqueue message to application (MNCC-SAP) */
180 static int mncc_recvmsg(struct osmocom_ms *ms, struct gsm_trans *trans,
181         int msg_type, struct gsm_mncc *mncc)
182 {
183         struct gsm48_cclayer *cc = &ms->cclayer;
184         struct msgb *msg;
185
186         if (trans)
187                 LOGP(DCC, LOGL_INFO, "(ms %s ti %x) Sending '%s' to MNCC.\n",
188                         ms->name, trans->transaction_id,
189                         get_mncc_name(msg_type));
190         else
191                 LOGP(DCC, LOGL_INFO, "(ms %s ti -) Sending '%s' to MNCC.\n",
192                         ms->name, get_mncc_name(msg_type));
193
194         mncc->msg_type = msg_type;
195
196         msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
197         if (!msg)
198                 return -ENOMEM;
199         memcpy(msg->data, mncc, sizeof(struct gsm_mncc));
200         msgb_enqueue(&cc->mncc_upqueue, msg);
201
202         return 0;
203 }
204
205 /* dequeue messages to layer 4 */
206 int mncc_dequeue(struct osmocom_ms *ms)
207 {
208         struct gsm48_cclayer *cc = &ms->cclayer;
209         struct gsm_mncc *mncc;
210         struct msgb *msg;
211         int work = 0;
212         
213         while ((msg = msgb_dequeue(&cc->mncc_upqueue))) {
214                 mncc = (struct gsm_mncc *)msg->data;
215                 if (cc->mncc_recv)
216                         cc->mncc_recv(ms, mncc->msg_type, mncc);
217                 work = 1; /* work done */
218                 talloc_free(msg);
219         }
220         
221         return work;
222 }
223
224
225 /*
226  * state transition
227  */
228
229 static void new_cc_state(struct gsm_trans *trans, int state)
230 {
231         if (state > 31 || state < 0)
232                 return;
233
234         DEBUGP(DCC, "new state %s -> %s\n",
235                 gsm48_cc_state_name(trans->cc.state),
236                 gsm48_cc_state_name(state));
237
238         trans->cc.state = state;
239 }
240
241 /*
242  * timers
243  */
244
245 /* timeout events of all timers */
246 static void gsm48_cc_timeout(void *arg)
247 {
248         struct gsm_trans *trans = arg;
249         int disconnect = 0, release = 0, abort = 1;
250         int mo_cause = GSM48_CC_CAUSE_RECOVERY_TIMER;
251         int mo_location = GSM48_CAUSE_LOC_PRN_S_LU;
252         int l4_cause = GSM48_CC_CAUSE_NORMAL_UNSPEC;
253         int l4_location = GSM48_CAUSE_LOC_PRN_S_LU;
254         struct gsm_mncc mo_rel, l4_rel;
255
256         memset(&mo_rel, 0, sizeof(struct gsm_mncc));
257         mo_rel.callref = trans->callref;
258         memset(&l4_rel, 0, sizeof(struct gsm_mncc));
259         l4_rel.callref = trans->callref;
260
261         LOGP(DCC, LOGL_INFO, "Timer T%x has fired.\n", trans->cc.Tcurrent);
262
263         switch(trans->cc.Tcurrent) {
264         case 0x303:
265                 /* abort if connection is not already esablished */
266                 if (trans->cc.state == GSM_CSTATE_MM_CONNECTION_PEND)
267                         abort = 1;
268                 else
269                         release = 1;
270                 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
271                 break;
272         case 0x305:
273                 release = 1;
274                 mo_cause = trans->cc.msg.cause.value;
275                 mo_location = trans->cc.msg.cause.location;
276                 break;
277         case 0x308:
278                 if (!trans->cc.T308_second) {
279                         /* restart T308 a second time */
280                         gsm48_cc_tx_release(trans, &trans->cc.msg);
281                         trans->cc.T308_second = 1;
282                         break; /* stay in release state */
283                 }
284                 /* release MM conn, got NULL state, free trans */
285                 gsm48_rel_null_free(trans);
286
287                 return;
288         case 0x310:
289                 disconnect = 1;
290                 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
291                 break;
292         case 0x313:
293                 disconnect = 1;
294                 /* unknown, did not find it in the specs */
295                 break;
296         default:
297                 release = 1;
298         }
299
300         if ((release || abort) && trans->callref) {
301                 /* process release towards layer 4 */
302                 mncc_release_ind(trans->ms, trans, trans->callref,
303                                  l4_location, l4_cause);
304         }
305
306         if (disconnect && trans->callref) {
307                 /* process disconnect towards layer 4 */
308                 mncc_set_cause(&l4_rel, l4_location, l4_cause);
309                 mncc_recvmsg(trans->ms, trans, MNCC_DISC_IND, &l4_rel);
310         }
311
312         /* process disconnect towards mobile station */
313         if (disconnect || release || abort) {
314                 mncc_set_cause(&mo_rel, mo_location, mo_cause);
315                 mo_rel.cause.diag[0] =
316                         ((trans->cc.Tcurrent & 0xf00) >> 8) + '0';
317                 mo_rel.cause.diag[1] =
318                         ((trans->cc.Tcurrent & 0x0f0) >> 4) + '0';
319                 mo_rel.cause.diag[2] = (trans->cc.Tcurrent & 0x00f) + '0';
320                 mo_rel.cause.diag_len = 3;
321
322                 if (disconnect)
323                         gsm48_cc_tx_disconnect(trans, &mo_rel);
324                 if (release)
325                         gsm48_cc_tx_release(trans, &mo_rel);
326                 if (abort) {
327                         /* release MM conn, got NULL state, free trans */
328                         gsm48_rel_null_free(trans);
329                 }
330         }
331 }
332
333 /* start various timers */
334 static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
335                                  int sec, int micro)
336 {
337         LOGP(DCC, LOGL_INFO, "starting timer T%x with %d seconds\n", current,
338                 sec);
339         trans->cc.timer.cb = gsm48_cc_timeout;
340         trans->cc.timer.data = trans;
341         bsc_schedule_timer(&trans->cc.timer, sec, micro);
342         trans->cc.Tcurrent = current;
343 }
344
345 /* stop various timers */
346 static void gsm48_stop_cc_timer(struct gsm_trans *trans)
347 {
348         if (bsc_timer_pending(&trans->cc.timer)) {
349                 LOGP(DCC, LOGL_INFO, "stopping pending timer T%x\n",
350                         trans->cc.Tcurrent);
351                 bsc_del_timer(&trans->cc.timer);
352                 trans->cc.Tcurrent = 0;
353         }
354 }
355
356 /*
357  * process handlers (misc)
358  */
359
360 /* Call Control Specific transaction release.
361  * gets called by trans_free, DO NOT CALL YOURSELF!
362  */
363 void _gsm48_cc_trans_free(struct gsm_trans *trans)
364 {
365         gsm48_stop_cc_timer(trans);
366
367         /* send release to L4, if callref still exists */
368         if (trans->callref) {
369                 /* Ressource unavailable */
370                 mncc_release_ind(trans->ms, trans, trans->callref,
371                         GSM48_CAUSE_LOC_PRN_S_LU,
372                         GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
373         }
374         if (trans->cc.state != GSM_CSTATE_NULL)
375                 new_cc_state(trans, GSM_CSTATE_NULL);
376 }
377
378 /* release MM connection, go NULL state, free transaction */
379 static int gsm48_rel_null_free(struct gsm_trans *trans)
380 {
381         struct msgb *nmsg;
382
383         /* release MM connection */
384         nmsg = gsm48_mmxx_msgb_alloc(GSM48_MMCC_REL_REQ, trans->callref,
385                                         trans->transaction_id);
386         if (!nmsg)
387                 return -ENOMEM;
388         LOGP(DCC, LOGL_INFO, "Sending MMCC_REL_REQ\n");
389         gsm48_mmxx_downmsg(trans->ms, nmsg);
390
391         new_cc_state(trans, GSM_CSTATE_NULL);
392
393         trans->callref = 0;
394         trans_free(trans);
395
396         return 0;
397 }
398
399 void mncc_set_cause(struct gsm_mncc *data, int loc, int val)
400 {
401         data->fields |= MNCC_F_CAUSE;
402         data->cause.coding = 0x3;
403         data->cause.location = loc;
404         data->cause.value = val;
405 }
406
407 /* send release indication to upper layer */
408 int mncc_release_ind(struct osmocom_ms *ms, struct gsm_trans *trans,
409                      u_int32_t callref, int location, int value)
410 {
411         struct gsm_mncc rel;
412
413         memset(&rel, 0, sizeof(rel));
414         rel.callref = callref;
415         mncc_set_cause(&rel, location, value);
416         return mncc_recvmsg(ms, trans, MNCC_REL_IND, &rel);
417 }
418
419 /* sending status message in response to unknown message */
420 static int gsm48_cc_tx_status(struct gsm_trans *trans, int cause)
421 {
422         struct msgb *nmsg;
423         struct gsm48_hdr *gh;
424         uint8_t *cause_ie, *call_state_ie;
425
426         LOGP(DCC, LOGL_INFO, "sending STATUS (cause %d)\n", cause);
427
428         nmsg = gsm48_l3_msgb_alloc();
429         if (!nmsg)
430                 return -ENOMEM;
431         gh = (struct gsm48_hdr *) msgb_put(nmsg, sizeof(*gh));
432
433         gh->msg_type = GSM48_MT_CC_STATUS;
434
435         cause_ie = msgb_put(nmsg, 3);
436         cause_ie[0] = 2;
437         cause_ie[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_PRN_S_LU;
438         cause_ie[2] = 0x80 | cause;
439
440         call_state_ie = msgb_put(nmsg, 1);
441         call_state_ie[0] = 0xc0 | trans->cc.state;
442
443         return gsm48_cc_to_mm(nmsg, trans, GSM48_MMCC_DATA_REQ);
444 }
445
446 /* reply status enquiry */
447 static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
448 {
449         LOGP(DCC, LOGL_INFO, "received STATUS ENQUIREY\n");
450
451         return gsm48_cc_tx_status(trans, GSM48_CC_CAUSE_RESP_STATUS_INQ);
452 }
453
454 /*
455  * process handlers (mobile originating call establish)
456  */
457
458 /* on SETUP request from L4, init MM connection */
459 static int gsm48_cc_init_mm(struct gsm_trans *trans, void *arg)
460 {
461         struct msgb *nmsg;
462         struct gsm_mncc *data = arg;
463         struct gsm48_mmxx_hdr *nmmh;
464
465         /* store setup message */
466         memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
467
468         new_cc_state(trans, GSM_CSTATE_MM_CONNECTION_PEND);
469
470         /* establish MM connection */
471         nmsg = gsm48_mmxx_msgb_alloc(GSM48_MMCC_EST_REQ, trans->callref,
472                                         trans->transaction_id);
473         if (!nmsg)
474                 return -ENOMEM;
475         nmmh = (struct gsm48_mmxx_hdr *) nmsg->data;
476         if (data->emergency)
477                 nmmh->emergency = 1;
478         LOGP(DCC, LOGL_INFO, "Sending MMCC_EST_REQ\n");
479         return gsm48_mmxx_downmsg(trans->ms, nmsg);
480 }
481
482 /* abort connection prior SETUP */
483 static int gsm48_cc_abort_mm(struct gsm_trans *trans, void *arg)
484 {
485         struct msgb *nmsg;
486
487         /* abort MM connection */
488         nmsg = gsm48_mmxx_msgb_alloc(GSM48_MMCC_REL_REQ, trans->callref,
489                         trans->transaction_id);
490         if (!nmsg)
491                 return -ENOMEM;
492         LOGP(DCC, LOGL_INFO, "Sending MMCC_REL_REQ\n");
493         gsm48_mmxx_downmsg(trans->ms, nmsg);
494
495         new_cc_state(trans, GSM_CSTATE_NULL);
496
497         trans->callref = 0;
498         trans_free(trans);
499
500         return 0;
501 }
502
503 /* setup message from upper layer */
504 static int gsm48_cc_tx_setup(struct gsm_trans *trans)
505 {
506         struct msgb *nmsg;
507         struct gsm48_hdr *gh;
508         struct gsm_mncc *setup = &trans->cc.msg;
509         int rc, transaction_id;
510         uint8_t *ie;
511
512         LOGP(DCC, LOGL_INFO, "sending SETUP\n");
513
514         nmsg = gsm48_l3_msgb_alloc();
515         if (!nmsg)
516                 return -ENOMEM;
517         gh = (struct gsm48_hdr *) msgb_put(nmsg, sizeof(*gh));
518
519         /* transaction id must not be assigned */
520         if (trans->transaction_id != 0xff) { /* unasssigned */
521                 LOGP(DCC, LOGL_NOTICE, "TX Setup with assigned transaction. "
522                         "This is not allowed!\n");
523                 /* Temporarily out of order */
524                 rc = mncc_release_ind(trans->ms, trans, trans->callref,
525                                       GSM48_CAUSE_LOC_PRN_S_LU,
526                                       GSM48_CC_CAUSE_NORMAL_UNSPEC);
527                 trans->callref = 0;
528                 trans_free(trans);
529                 return rc;
530         }
531         
532         /* Get free transaction_id */
533         transaction_id = trans_assign_trans_id(trans->ms, GSM48_PDISC_CC, 0);
534         if (transaction_id < 0) {
535                 /* no free transaction ID */
536                 rc = mncc_release_ind(trans->ms, trans, trans->callref,
537                                       GSM48_CAUSE_LOC_PRN_S_LU,
538                                       GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
539                 trans->callref = 0;
540                 trans_free(trans);
541                 return rc;
542         }
543         trans->transaction_id = transaction_id;
544
545         gh->msg_type = (setup->emergency) ? GSM48_MT_CC_EMERG_SETUP :
546                                                 GSM48_MT_CC_SETUP;
547
548         /* actually we have to start it when CM SERVICE REQUEST has been sent,
549          * but there is no primitive for that defined. i think it is ok to
550          * do it here rather than inventing MMCC-NOTIFY-IND.
551          */
552         gsm48_start_cc_timer(trans, 0x303, GSM48_T303_MS);
553
554         /* bearer capability (optional for emergency calls only) */
555         if (setup->fields & MNCC_F_BEARER_CAP)
556                 gsm48_encode_bearer_cap(nmsg, 0, &setup->bearer_cap);
557         if (!setup->emergency) {
558                 /* facility */
559                 if (setup->fields & MNCC_F_FACILITY)
560                         gsm48_encode_facility(nmsg, 0, &setup->facility);
561                 /* called party BCD number */
562                 if (setup->fields & MNCC_F_CALLED)
563                         gsm48_encode_called(nmsg, &setup->called);
564                 /* user-user */
565                 if (setup->fields & MNCC_F_USERUSER)
566                         gsm48_encode_useruser(nmsg, 0, &setup->useruser);
567                 /* ss version */
568                 if (setup->fields & MNCC_F_SSVERSION)
569                         gsm48_encode_ssversion(nmsg, &setup->ssversion);
570                 /* CLIR suppression */
571                 if (setup->clir.sup) {
572                         ie = msgb_put(nmsg, 1);
573                         ie[0] = GSM48_IE_CLIR_SUPP;
574                 }
575                 /* CLIR invocation */
576                 if (setup->clir.inv) {
577                         ie = msgb_put(nmsg, 1);
578                         ie[0] = GSM48_IE_CLIR_INVOC;
579                 }
580                 /* cc cap */
581                 if (setup->fields & MNCC_F_CCCAP)
582                         gsm48_encode_cccap(nmsg, &setup->cccap);
583         }
584
585         /* actually MM CONNECTION PENDING */
586         new_cc_state(trans, GSM_CSTATE_INITIATED);
587
588         return gsm48_cc_to_mm(nmsg, trans, GSM48_MMCC_DATA_REQ);
589 }
590
591 /* progress is received from lower layer */
592 static int gsm48_cc_rx_progress(struct gsm_trans *trans, struct msgb *msg)
593 {
594         struct gsm48_hdr *gh = msgb_l3(msg);
595         unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
596         struct tlv_parsed tp;
597         struct gsm_mncc progress;
598
599         LOGP(DCC, LOGL_INFO, "received PROGRESS\n");
600
601         memset(&progress, 0, sizeof(struct gsm_mncc));
602         progress.callref = trans->callref;
603         tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len,
604                 GSM48_IE_PROGR_IND, 0);
605         /* progress */
606         if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
607                 progress.fields |= MNCC_F_PROGRESS;
608                 gsm48_decode_progress(&progress.progress,
609                                 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
610                 /* store last progress indicator */
611                 trans->cc.prog_ind = progress.progress.descr;
612         }
613         /* user-user */
614         if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
615                 progress.fields |= MNCC_F_USERUSER;
616                 gsm48_decode_useruser(&progress.useruser,
617                                 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
618         }
619
620         return mncc_recvmsg(trans->ms, trans, MNCC_PROGRESS_IND, &progress);
621 }
622
623 /* call proceeding is received from lower layer */
624 static int gsm48_cc_rx_call_proceeding(struct gsm_trans *trans,
625         struct msgb *msg)
626 {
627         struct gsm48_hdr *gh = msgb_l3(msg);
628         unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
629         struct tlv_parsed tp;
630         struct gsm_mncc call_proc;
631
632         LOGP(DCC, LOGL_INFO, "sending CALL PROCEEDING\n");
633
634         gsm48_stop_cc_timer(trans);
635
636         memset(&call_proc, 0, sizeof(struct gsm_mncc));
637         call_proc.callref = trans->callref;
638         tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
639 #if 0
640         /* repeat */
641         if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
642                 call_conf.repeat = 1;
643         if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
644                 call_conf.repeat = 2;
645 #endif
646         /* bearer capability */
647         if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
648                 call_proc.fields |= MNCC_F_BEARER_CAP;
649                 gsm48_decode_bearer_cap(&call_proc.bearer_cap,
650                                   TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
651         }
652         /* facility */
653         if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
654                 call_proc.fields |= MNCC_F_FACILITY;
655                 gsm48_decode_facility(&call_proc.facility,
656                                 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
657         }
658
659         /* progress */
660         if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
661                 call_proc.fields |= MNCC_F_PROGRESS;
662                 gsm48_decode_progress(&call_proc.progress,
663                                 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
664                 /* store last progress indicator */
665                 trans->cc.prog_ind = call_proc.progress.descr;
666         }
667
668         /* start T310, if last progress indicator was 1 or 2 or 64 */
669         if (trans->cc.prog_ind == 1
670          || trans->cc.prog_ind == 2
671          || trans->cc.prog_ind == 64)
672                 gsm48_start_cc_timer(trans, 0x310, GSM48_T310_MS);
673
674         new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
675
676         return mncc_recvmsg(trans->ms, trans, MNCC_CALL_PROC_IND,
677                             &call_proc);
678 }
679
680 /* alerting is received by the lower layer */
681 static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
682 {
683         struct gsm48_hdr *gh = msgb_l3(msg);
684         unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
685         struct tlv_parsed tp;
686         struct gsm_mncc alerting;
687         
688         LOGP(DCC, LOGL_INFO, "sending ALERTING\n");
689
690         gsm48_stop_cc_timer(trans);
691         /* no T301 in MS call control */
692
693         memset(&alerting, 0, sizeof(struct gsm_mncc));
694         alerting.callref = trans->callref;
695         tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
696         /* facility */
697         if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
698                 alerting.fields |= MNCC_F_FACILITY;
699                 gsm48_decode_facility(&alerting.facility,
700                                 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
701         }
702
703         /* progress */
704         if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
705                 alerting.fields |= MNCC_F_PROGRESS;
706                 gsm48_decode_progress(&alerting.progress,
707                                 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
708         }
709         /* user-user */
710         if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
711                 alerting.fields |= MNCC_F_USERUSER;
712                 gsm48_decode_useruser(&alerting.useruser,
713                                 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
714         }
715
716         new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
717
718         return mncc_recvmsg(trans->ms, trans, MNCC_ALERT_IND,
719                             &alerting);
720 }
721
722 /* connect is received from lower layer */
723 static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
724 {
725         struct gsm48_hdr *gh = msgb_l3(msg);
726         unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
727         struct tlv_parsed tp;
728         struct gsm_mncc connect;
729
730         LOGP(DCC, LOGL_INFO, "received CONNECT\n");
731
732         gsm48_stop_cc_timer(trans);
733
734         memset(&connect, 0, sizeof(struct gsm_mncc));
735         connect.callref = trans->callref;
736         tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
737         /* facility */
738         if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
739                 connect.fields |= MNCC_F_FACILITY;
740                 gsm48_decode_facility(&connect.facility,
741                                 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
742         }
743         /* connected */
744         if (TLVP_PRESENT(&tp, GSM48_IE_CONN_BCD)) {
745                 connect.fields |= MNCC_F_CONNECTED;
746                 gsm48_decode_connected(&connect.connected,
747                                 TLVP_VAL(&tp, GSM48_IE_CONN_BCD)-1);
748         }
749         /* progress */
750         if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
751                 connect.fields |= MNCC_F_PROGRESS;
752                 gsm48_decode_progress(&connect.progress,
753                                 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
754         }
755         /* user-user */
756         if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
757                 connect.fields |= MNCC_F_USERUSER;
758                 gsm48_decode_useruser(&connect.useruser,
759                                 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
760         }
761
762         /* ACTIVE state is set during this: */
763         gsm48_cc_tx_connect_ack(trans, NULL);
764
765         return mncc_recvmsg(trans->ms, trans, MNCC_SETUP_CNF, &connect);
766 }
767
768 /* connect ack message from upper layer */
769 static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
770 {
771         struct msgb *nmsg;
772         struct gsm48_hdr *gh;
773
774         LOGP(DCC, LOGL_INFO, "sending CONNECT ACKNOWLEDGE\n");
775
776         nmsg = gsm48_l3_msgb_alloc();
777         if (!nmsg)
778                 return -ENOMEM;
779         gh = (struct gsm48_hdr *) msgb_put(nmsg, sizeof(*gh));
780
781         gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
782
783         new_cc_state(trans, GSM_CSTATE_ACTIVE);
784
785         return gsm48_cc_to_mm(nmsg, trans, GSM48_MMCC_DATA_REQ);
786 }
787
788 /*
789  * process handlers (mobile terminating call establish)
790  */
791
792 /* setup is received from lower layer */
793 static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
794 {
795         struct gsm48_hdr *gh = msgb_l3(msg);
796         unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
797         struct tlv_parsed tp;
798         struct gsm_mncc setup;
799
800         LOGP(DCC, LOGL_INFO, "received SETUP\n");
801
802         memset(&setup, 0, sizeof(struct gsm_mncc));
803         setup.callref = trans->callref;
804         tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
805
806         /* bearer capability */
807         if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
808                 setup.fields |= MNCC_F_BEARER_CAP;
809                 gsm48_decode_bearer_cap(&setup.bearer_cap,
810                                   TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
811         }
812         /* facility */
813         if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
814                 setup.fields |= MNCC_F_FACILITY;
815                 gsm48_decode_facility(&setup.facility,
816                                 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
817         }
818         /* progress */
819         if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
820                 setup.fields |= MNCC_F_PROGRESS;
821                 gsm48_decode_progress(&setup.progress,
822                                 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
823         }
824         /* signal */
825         if (TLVP_PRESENT(&tp, GSM48_IE_SIGNAL)) {
826                 setup.fields |= MNCC_F_SIGNAL;
827                 gsm48_decode_signal(&setup.signal,
828                                 TLVP_VAL(&tp, GSM48_IE_SIGNAL)-1);
829         }
830         /* calling party bcd number */
831         if (TLVP_PRESENT(&tp, GSM48_IE_CALLING_BCD)) {
832                 setup.fields |= MNCC_F_CALLING;
833                 gsm48_decode_calling(&setup.calling,
834                               TLVP_VAL(&tp, GSM48_IE_CALLING_BCD)-1);
835         }
836         /* called party bcd number */
837         if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
838                 setup.fields |= MNCC_F_CALLED;
839                 gsm48_decode_called(&setup.called,
840                               TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
841         }
842         /* redirecting party bcd number */
843         if (TLVP_PRESENT(&tp, GSM48_IE_REDIR_BCD)) {
844                 setup.fields |= MNCC_F_REDIRECTING;
845                 gsm48_decode_redirecting(&setup.redirecting,
846                               TLVP_VAL(&tp, GSM48_IE_REDIR_BCD)-1);
847         }
848         /* user-user */
849         if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
850                 setup.fields |= MNCC_F_USERUSER;
851                 gsm48_decode_useruser(&setup.useruser,
852                                 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
853         }
854
855         new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
856
857         /* indicate setup to MNCC */
858         mncc_recvmsg(trans->ms, trans, MNCC_SETUP_IND, &setup);
859
860         return 0;
861 }
862
863 /* call conf message from upper layer */
864 static int gsm48_cc_tx_call_conf(struct gsm_trans *trans, void *arg)
865 {
866         struct gsm_mncc *confirm = arg;
867         struct msgb *nmsg;
868         struct gsm48_hdr *gh;
869
870         LOGP(DCC, LOGL_INFO, "sending CALL CONFIRMED (proceeding)\n");
871
872         nmsg = gsm48_l3_msgb_alloc();
873         if (!nmsg)
874                 return -ENOMEM;
875         gh = (struct gsm48_hdr *) msgb_put(nmsg, sizeof(*gh));
876
877         gh->msg_type = GSM48_MT_CC_CALL_CONF;
878
879         new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
880
881         /* bearer capability */
882         if (confirm->fields & MNCC_F_BEARER_CAP)
883                 gsm48_encode_bearer_cap(nmsg, 0, &confirm->bearer_cap);
884         /* cause */
885         if (confirm->fields & MNCC_F_CAUSE)
886                 gsm48_encode_cause(nmsg, 0, &confirm->cause);
887         /* cc cap */
888         if (confirm->fields & MNCC_F_CCCAP)
889                 gsm48_encode_cccap(nmsg, &confirm->cccap);
890
891         return gsm48_cc_to_mm(nmsg, trans, GSM48_MMCC_DATA_REQ);
892 }
893
894 /* alerting message from upper layer */
895 static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
896 {
897         struct gsm_mncc *alerting = arg;
898         struct msgb *nmsg;
899         struct gsm48_hdr *gh;
900
901         LOGP(DCC, LOGL_INFO, "sending ALERTING\n");
902
903         nmsg = gsm48_l3_msgb_alloc();
904         if (!nmsg)
905                 return -ENOMEM;
906         gh = (struct gsm48_hdr *) msgb_put(nmsg, sizeof(*gh));
907
908         gh->msg_type = GSM48_MT_CC_ALERTING;
909
910         /* facility */
911         if (alerting->fields & MNCC_F_FACILITY)
912                 gsm48_encode_facility(nmsg, 0, &alerting->facility);
913         /* user-user */
914         if (alerting->fields & MNCC_F_USERUSER)
915                 gsm48_encode_useruser(nmsg, 0, &alerting->useruser);
916         /* ss version */
917         if (alerting->fields & MNCC_F_SSVERSION)
918                 gsm48_encode_ssversion(nmsg, &alerting->ssversion);
919
920         new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
921         
922         return gsm48_cc_to_mm(nmsg, trans, GSM48_MMCC_DATA_REQ);
923 }
924
925 /* connect message from upper layer */
926 static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
927 {
928         struct gsm_mncc *connect = arg;
929         struct msgb *nmsg;
930         struct gsm48_hdr *gh;
931
932         LOGP(DCC, LOGL_INFO, "sending CONNECT\n");
933
934         nmsg = gsm48_l3_msgb_alloc();
935         if (!nmsg)
936                 return -ENOMEM;
937         gh = (struct gsm48_hdr *) msgb_put(nmsg, sizeof(*gh));
938
939         gh->msg_type = GSM48_MT_CC_CONNECT;
940
941         gsm48_stop_cc_timer(trans);
942         gsm48_start_cc_timer(trans, 0x313, GSM48_T313_MS);
943
944         /* facility */
945         if (connect->fields & MNCC_F_FACILITY)
946                 gsm48_encode_facility(nmsg, 0, &connect->facility);
947         /* user-user */
948         if (connect->fields & MNCC_F_USERUSER)
949                 gsm48_encode_useruser(nmsg, 0, &connect->useruser);
950         /* ss version */
951         if (connect->fields & MNCC_F_SSVERSION)
952                 gsm48_encode_ssversion(nmsg, &connect->ssversion);
953
954         new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
955
956         return gsm48_cc_to_mm(nmsg, trans, GSM48_MMCC_DATA_REQ);
957 }
958
959 /* connect ack is received from lower layer */
960 static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
961 {
962         struct gsm_mncc connect_ack;
963
964         LOGP(DCC, LOGL_INFO, "received CONNECT ACKNOWLEDGE\n");
965
966         gsm48_stop_cc_timer(trans);
967
968         new_cc_state(trans, GSM_CSTATE_ACTIVE);
969         
970         memset(&connect_ack, 0, sizeof(struct gsm_mncc));
971         connect_ack.callref = trans->callref;
972         return mncc_recvmsg(trans->ms, trans, MNCC_SETUP_COMPL_IND,
973                             &connect_ack);
974 }
975
976 /*
977  * process handlers (during active state)
978  */
979
980 /* notify message from upper layer */
981 static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
982 {
983         struct gsm_mncc *notify = arg;
984         struct msgb *nmsg;
985         struct gsm48_hdr *gh;
986
987         LOGP(DCC, LOGL_INFO, "sending NOTIFY\n");
988
989         nmsg = gsm48_l3_msgb_alloc();
990         if (!nmsg)
991                 return -ENOMEM;
992         gh = (struct gsm48_hdr *) msgb_put(nmsg, sizeof(*gh));
993
994         gh->msg_type = GSM48_MT_CC_NOTIFY;
995
996         /* notify */
997         gsm48_encode_notify(nmsg, notify->notify);
998
999         return gsm48_cc_to_mm(nmsg, trans, GSM48_MMCC_DATA_REQ);
1000 }
1001
1002 /* notify is received from lower layer */
1003 static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
1004 {
1005         struct gsm48_hdr *gh = msgb_l3(msg);
1006         unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1007         struct gsm_mncc notify;
1008
1009         LOGP(DCC, LOGL_INFO, "received NOTIFY\n");
1010
1011         memset(&notify, 0, sizeof(struct gsm_mncc));
1012         notify.callref = trans->callref;
1013         /* notify */
1014         if (payload_len < 1) {
1015                 LOGP(DCC, LOGL_NOTICE, "Short read of notify message error.\n");
1016                 return -EINVAL;
1017         }
1018         gsm48_decode_notify(&notify.notify, gh->data);
1019
1020         return mncc_recvmsg(trans->ms, trans, MNCC_NOTIFY_IND, &notify);
1021 }
1022
1023 /* start dtmf message from upper layer */
1024 static int gsm48_cc_tx_start_dtmf(struct gsm_trans *trans, void *arg)
1025 {
1026         struct gsm_mncc *dtmf = arg;
1027         struct msgb *nmsg;
1028         struct gsm48_hdr *gh;
1029
1030         LOGP(DCC, LOGL_INFO, "sending START DTMF\n");
1031
1032         nmsg = gsm48_l3_msgb_alloc();
1033         if (!nmsg)
1034                 return -ENOMEM;
1035         gh = (struct gsm48_hdr *) msgb_put(nmsg, sizeof(*gh));
1036
1037         gh->msg_type = GSM48_MT_CC_START_DTMF;
1038
1039         /* keypad */
1040         gsm48_encode_keypad(nmsg, dtmf->keypad);
1041
1042         return gsm48_cc_to_mm(nmsg, trans, GSM48_MMCC_DATA_REQ);
1043 }
1044
1045 /* start dtmf ack is received from lower layer */
1046 static int gsm48_cc_rx_start_dtmf_ack(struct gsm_trans *trans, struct msgb *msg)
1047 {
1048         struct gsm48_hdr *gh = msgb_l3(msg);
1049         unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1050         struct tlv_parsed tp;
1051         struct gsm_mncc dtmf;
1052
1053         LOGP(DCC, LOGL_INFO, "received START DTMF ACKNOWLEDGE\n");
1054
1055         memset(&dtmf, 0, sizeof(struct gsm_mncc));
1056         dtmf.callref = trans->callref;
1057         tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
1058         /* keypad facility */
1059         if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
1060                 dtmf.fields |= MNCC_F_KEYPAD;
1061                 gsm48_decode_keypad(&dtmf.keypad,
1062                               TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
1063         }
1064
1065         return mncc_recvmsg(trans->ms, trans, MNCC_START_DTMF_RSP, &dtmf);
1066 }
1067
1068 /* start dtmf rej is received from lower layer */
1069 static int gsm48_cc_rx_start_dtmf_rej(struct gsm_trans *trans, struct msgb *msg)
1070 {
1071         struct gsm48_hdr *gh = msgb_l3(msg);
1072         unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1073         struct gsm_mncc dtmf;
1074
1075         LOGP(DCC, LOGL_INFO, "received START DTMF REJECT\n");
1076
1077         memset(&dtmf, 0, sizeof(struct gsm_mncc));
1078         dtmf.callref = trans->callref;
1079         /* cause */
1080         if (payload_len < 1) {
1081                 LOGP(DCC, LOGL_NOTICE, "Short read of dtmf reject message "
1082                         "error.\n");
1083                 return -EINVAL;
1084         }
1085         gsm48_decode_cause(&dtmf.cause, gh->data);
1086
1087         return mncc_recvmsg(trans->ms, trans, MNCC_START_DTMF_REJ, &dtmf);
1088 }
1089
1090 /* stop dtmf message from upper layer */
1091 static int gsm48_cc_tx_stop_dtmf(struct gsm_trans *trans, void *arg)
1092 {
1093         struct msgb *nmsg;
1094         struct gsm48_hdr *gh;
1095
1096         LOGP(DCC, LOGL_INFO, "sending STOP DTMF\n");
1097
1098         nmsg = gsm48_l3_msgb_alloc();
1099         if (!nmsg)
1100                 return -ENOMEM;
1101         gh = (struct gsm48_hdr *) msgb_put(nmsg, sizeof(*gh));
1102
1103         gh->msg_type = GSM48_MT_CC_STOP_DTMF;
1104
1105         return gsm48_cc_to_mm(nmsg, trans, GSM48_MMCC_DATA_REQ);
1106 }
1107
1108 /* stop dtmf ack is received from lower layer */
1109 static int gsm48_cc_rx_stop_dtmf_ack(struct gsm_trans *trans, struct msgb *msg)
1110 {
1111         struct gsm48_hdr *gh = msgb_l3(msg);
1112         unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1113         struct tlv_parsed tp;
1114         struct gsm_mncc dtmf;
1115
1116         LOGP(DCC, LOGL_INFO, "received STOP DTMF ACKNOWLEDGE\n");
1117
1118         memset(&dtmf, 0, sizeof(struct gsm_mncc));
1119         dtmf.callref = trans->callref;
1120         tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
1121
1122         return mncc_recvmsg(trans->ms, trans, MNCC_STOP_DTMF_RSP, &dtmf);
1123 }
1124
1125 /* hold message from upper layer */
1126 static int gsm48_cc_tx_hold(struct gsm_trans *trans, void *arg)
1127 {
1128         struct msgb *nmsg;
1129         struct gsm48_hdr *gh;
1130
1131         LOGP(DCC, LOGL_INFO, "sending HOLD\n");
1132
1133         nmsg = gsm48_l3_msgb_alloc();
1134         if (!nmsg)
1135                 return -ENOMEM;
1136         gh = (struct gsm48_hdr *) msgb_put(nmsg, sizeof(*gh));
1137
1138         gh->msg_type = GSM48_MT_CC_HOLD;
1139
1140         return gsm48_cc_to_mm(nmsg, trans, GSM48_MMCC_DATA_REQ);
1141 }
1142
1143 /* hold ack is received from lower layer */
1144 static int gsm48_cc_rx_hold_ack(struct gsm_trans *trans, struct msgb *msg)
1145 {
1146         struct gsm_mncc hold;
1147
1148         LOGP(DCC, LOGL_INFO, "received HOLD ACKNOWLEDGE\n");
1149
1150         memset(&hold, 0, sizeof(struct gsm_mncc));
1151         hold.callref = trans->callref;
1152
1153         return mncc_recvmsg(trans->ms, trans, MNCC_HOLD_CNF, &hold);
1154 }
1155
1156 /* hold rej is received from lower layer */
1157 static int gsm48_cc_rx_hold_rej(struct gsm_trans *trans, struct msgb *msg)
1158 {
1159         struct gsm48_hdr *gh = msgb_l3(msg);
1160         unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1161         struct gsm_mncc hold;
1162
1163         LOGP(DCC, LOGL_INFO, "received HOLD REJECT\n");
1164
1165         memset(&hold, 0, sizeof(struct gsm_mncc));
1166         hold.callref = trans->callref;
1167         /* cause */
1168         if (payload_len < 1) {
1169                 LOGP(DCC, LOGL_NOTICE, "Short read of hold reject message "
1170                         "error.\n");
1171                 return -EINVAL;
1172         }
1173         gsm48_decode_cause(&hold.cause, gh->data);
1174
1175         return mncc_recvmsg(trans->ms, trans, MNCC_HOLD_REJ, &hold);
1176 }
1177
1178 /* retrieve message from upper layer */
1179 static int gsm48_cc_tx_retrieve(struct gsm_trans *trans, void *arg)
1180 {
1181         struct msgb *nmsg;
1182         struct gsm48_hdr *gh;
1183
1184         LOGP(DCC, LOGL_INFO, "sending RETRIEVE\n");
1185
1186         nmsg = gsm48_l3_msgb_alloc();
1187         if (!nmsg)
1188                 return -ENOMEM;
1189         gh = (struct gsm48_hdr *) msgb_put(nmsg, sizeof(*gh));
1190
1191         gh->msg_type = GSM48_MT_CC_RETR;
1192
1193         return gsm48_cc_to_mm(nmsg, trans, GSM48_MMCC_DATA_REQ);
1194 }
1195
1196 /* retrieve ack is received from lower layer */
1197 static int gsm48_cc_rx_retrieve_ack(struct gsm_trans *trans, struct msgb *msg)
1198 {
1199         struct gsm_mncc retrieve;
1200
1201         LOGP(DCC, LOGL_INFO, "received RETRIEVE ACKNOWLEDGE\n");
1202
1203         memset(&retrieve, 0, sizeof(struct gsm_mncc));
1204         retrieve.callref = trans->callref;
1205
1206         return mncc_recvmsg(trans->ms, trans, MNCC_RETRIEVE_CNF, &retrieve);
1207 }
1208
1209 /* retrieve rej is received from lower layer */
1210 static int gsm48_cc_rx_retrieve_rej(struct gsm_trans *trans, struct msgb *msg)
1211 {
1212         struct gsm48_hdr *gh = msgb_l3(msg);
1213         unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1214         struct gsm_mncc retrieve;
1215
1216         LOGP(DCC, LOGL_INFO, "received RETRIEVE REJECT\n");
1217
1218         memset(&retrieve, 0, sizeof(struct gsm_mncc));
1219         retrieve.callref = trans->callref;
1220         /* cause */
1221         if (payload_len < 1) {
1222                 LOGP(DCC, LOGL_NOTICE, "Short read of retrieve reject message "
1223                         "error.\n");
1224                 return -EINVAL;
1225         }
1226         gsm48_decode_cause(&retrieve.cause, gh->data);
1227
1228         return mncc_recvmsg(trans->ms, trans, MNCC_RETRIEVE_REJ, &retrieve);
1229 }
1230
1231 /* facility message from upper layer or from timer event */
1232 static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
1233 {
1234         struct gsm_mncc *fac = arg;
1235         struct msgb *nmsg;
1236         struct gsm48_hdr *gh;
1237
1238         LOGP(DCC, LOGL_INFO, "sending FACILITY\n");
1239
1240         nmsg = gsm48_l3_msgb_alloc();
1241         if (!nmsg)
1242                 return -ENOMEM;
1243         gh = (struct gsm48_hdr *) msgb_put(nmsg, sizeof(*gh));
1244
1245         gh->msg_type = GSM48_MT_CC_FACILITY;
1246
1247         /* facility */
1248         gsm48_encode_facility(nmsg, 1, &fac->facility);
1249         /* ss version */
1250         if (fac->fields & MNCC_F_SSVERSION)
1251                 gsm48_encode_ssversion(nmsg, &fac->ssversion);
1252
1253         return gsm48_cc_to_mm(nmsg, trans, GSM48_MMCC_DATA_REQ);
1254 }
1255
1256 /* facility is received from lower layer */
1257 static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
1258 {
1259         struct gsm48_hdr *gh = msgb_l3(msg);
1260         unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1261         struct gsm_mncc fac;
1262
1263         LOGP(DCC, LOGL_INFO, "received FACILITY\n");
1264
1265         memset(&fac, 0, sizeof(struct gsm_mncc));
1266         fac.callref = trans->callref;
1267         if (payload_len < 1) {
1268                 LOGP(DCC, LOGL_NOTICE, "Short read of facility message "
1269                         "error.\n");
1270                 return -EINVAL;
1271         }
1272         /* facility */
1273         gsm48_decode_facility(&fac.facility, gh->data);
1274
1275         return mncc_recvmsg(trans->ms, trans, MNCC_FACILITY_IND, &fac);
1276 }
1277
1278 /* user info message from upper layer or from timer event */
1279 static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
1280 {
1281         struct gsm_mncc *user = arg;
1282         struct msgb *nmsg;
1283         struct gsm48_hdr *gh;
1284
1285         LOGP(DCC, LOGL_INFO, "sending USERINFO\n");
1286
1287         nmsg = gsm48_l3_msgb_alloc();
1288         if (!nmsg)
1289                 return -ENOMEM;
1290         gh = (struct gsm48_hdr *) msgb_put(nmsg, sizeof(*gh));
1291
1292         gh->msg_type = GSM48_MT_CC_USER_INFO;
1293
1294         /* user-user */
1295         if (user->fields & MNCC_F_USERUSER)
1296                 gsm48_encode_useruser(nmsg, 1, &user->useruser);
1297         /* more data */
1298         if (user->more)
1299                 gsm48_encode_more(nmsg);
1300
1301         return gsm48_cc_to_mm(nmsg, trans, GSM48_MMCC_DATA_REQ);
1302 }
1303
1304 /* user info is received from lower layer */
1305 static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
1306 {
1307         struct gsm48_hdr *gh = msgb_l3(msg);
1308         unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1309         struct tlv_parsed tp;
1310         struct gsm_mncc user;
1311
1312         LOGP(DCC, LOGL_INFO, "received USERINFO\n");
1313
1314         memset(&user, 0, sizeof(struct gsm_mncc));
1315         user.callref = trans->callref;
1316         if (payload_len < 1) {
1317                 LOGP(DCC, LOGL_NOTICE, "Short read of userinfo message "
1318                         "error.\n");
1319                 return -EINVAL;
1320         }
1321         tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len,
1322                 GSM48_IE_USER_USER, 0);
1323         /* user-user */
1324         gsm48_decode_useruser(&user.useruser,
1325                         TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1326         /* more data */
1327         if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
1328                 user.more = 1;
1329
1330         return mncc_recvmsg(trans->ms, trans, MNCC_USERINFO_IND, &user);
1331 }
1332
1333 /* modify message from upper layer or from timer event */
1334 static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
1335 {
1336         struct gsm_mncc *modify = arg;
1337         struct msgb *nmsg;
1338         struct gsm48_hdr *gh;
1339
1340         LOGP(DCC, LOGL_INFO, "sending MODIFY\n");
1341
1342         nmsg = gsm48_l3_msgb_alloc();
1343         if (!nmsg)
1344                 return -ENOMEM;
1345         gh = (struct gsm48_hdr *) msgb_put(nmsg, sizeof(*gh));
1346
1347         gh->msg_type = GSM48_MT_CC_MODIFY;
1348
1349         gsm48_start_cc_timer(trans, 0x323, GSM48_T323_MS);
1350
1351         /* bearer capability */
1352         gsm48_encode_bearer_cap(nmsg, 1, &modify->bearer_cap);
1353
1354         new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
1355
1356         return gsm48_cc_to_mm(nmsg, trans, GSM48_MMCC_DATA_REQ);
1357 }
1358
1359 /* modify complete is received from lower layer */
1360 static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans,
1361         struct msgb *msg)
1362 {
1363         struct gsm48_hdr *gh = msgb_l3(msg);
1364         unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1365         struct gsm_mncc modify;
1366
1367         LOGP(DCC, LOGL_INFO, "received MODIFY COMPLETE\n");
1368
1369         gsm48_stop_cc_timer(trans);
1370
1371         memset(&modify, 0, sizeof(struct gsm_mncc));
1372         modify.callref = trans->callref;
1373         if (payload_len < 1) {
1374                 LOGP(DCC, LOGL_NOTICE, "Short read of modify complete message "
1375                         "error.\n");
1376                 return -EINVAL;
1377         }
1378         /* bearer capability */
1379         gsm48_decode_bearer_cap(&modify.bearer_cap, gh->data);
1380
1381         new_cc_state(trans, GSM_CSTATE_ACTIVE);
1382
1383         return mncc_recvmsg(trans->ms, trans, MNCC_MODIFY_CNF, &modify);
1384 }
1385
1386 /* modify reject is received from lower layer */
1387 static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
1388 {
1389         struct gsm48_hdr *gh = msgb_l3(msg);
1390         unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1391         struct tlv_parsed tp;
1392         struct gsm_mncc modify;
1393
1394         LOGP(DCC, LOGL_INFO, "received MODIFY REJECT\n");
1395
1396         gsm48_stop_cc_timer(trans);
1397
1398         memset(&modify, 0, sizeof(struct gsm_mncc));
1399         modify.callref = trans->callref;
1400         if (payload_len < 1) {
1401                 LOGP(DCC, LOGL_NOTICE, "Short read of modify reject message "
1402                         "error.\n");
1403                 return -EINVAL;
1404         }
1405         tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len,
1406                 GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
1407         /* bearer capability */
1408         if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1409                 modify.fields |= MNCC_F_BEARER_CAP;
1410                 gsm48_decode_bearer_cap(&modify.bearer_cap,
1411                                   TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1412         }
1413         /* cause */
1414         if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1415                 modify.fields |= MNCC_F_CAUSE;
1416                 gsm48_decode_cause(&modify.cause,
1417                              TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1418         }
1419
1420         new_cc_state(trans, GSM_CSTATE_ACTIVE);
1421
1422         return mncc_recvmsg(trans->ms, trans, MNCC_MODIFY_REJ, &modify);
1423 }
1424
1425 /* modify is received from lower layer */
1426 static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
1427 {
1428         struct gsm48_hdr *gh = msgb_l3(msg);
1429         unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1430         struct gsm_mncc modify;
1431
1432         LOGP(DCC, LOGL_INFO, "received MODIFY\n");
1433
1434         memset(&modify, 0, sizeof(struct gsm_mncc));
1435         modify.callref = trans->callref;
1436         if (payload_len < 1) {
1437                 LOGP(DCC, LOGL_NOTICE, "Short read of modify message error.\n");
1438                 return -EINVAL;
1439         }
1440         /* bearer capability */
1441         gsm48_decode_bearer_cap(&modify.bearer_cap, gh->data);
1442
1443         new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
1444
1445         return mncc_recvmsg(trans->ms, trans, MNCC_MODIFY_IND, &modify);
1446 }
1447
1448 /* modify complete message from upper layer or from timer event */
1449 static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
1450 {
1451         struct gsm_mncc *modify = arg;
1452         struct msgb *nmsg;
1453         struct gsm48_hdr *gh;
1454
1455         LOGP(DCC, LOGL_INFO, "sending MODIFY COMPLETE\n");
1456
1457         nmsg = gsm48_l3_msgb_alloc();
1458         if (!nmsg)
1459                 return -ENOMEM;
1460         gh = (struct gsm48_hdr *) msgb_put(nmsg, sizeof(*gh));
1461
1462         gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
1463
1464         /* bearer capability */
1465         gsm48_encode_bearer_cap(nmsg, 1, &modify->bearer_cap);
1466
1467         new_cc_state(trans, GSM_CSTATE_ACTIVE);
1468
1469         return gsm48_cc_to_mm(nmsg, trans, GSM48_MMCC_DATA_REQ);
1470 }
1471
1472 /* modify reject message from upper layer or from timer event */
1473 static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
1474 {
1475         struct gsm_mncc *modify = arg;
1476         struct msgb *nmsg;
1477         struct gsm48_hdr *gh;
1478
1479         LOGP(DCC, LOGL_INFO, "sending MODIFY REJECT\n");
1480
1481         nmsg = gsm48_l3_msgb_alloc();
1482         if (!nmsg)
1483                 return -ENOMEM;
1484         gh = (struct gsm48_hdr *) msgb_put(nmsg, sizeof(*gh));
1485
1486         gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
1487
1488         /* bearer capability */
1489         gsm48_encode_bearer_cap(nmsg, 1, &modify->bearer_cap);
1490         /* cause */
1491         gsm48_encode_cause(nmsg, 1, &modify->cause);
1492
1493         new_cc_state(trans, GSM_CSTATE_ACTIVE);
1494
1495         return gsm48_cc_to_mm(nmsg, trans, GSM48_MMCC_DATA_REQ);
1496 }
1497
1498 /*
1499  * process handlers (call clearing)
1500  */
1501
1502 static struct gsm_mncc_cause default_cause = {
1503         .location       = GSM48_CAUSE_LOC_PRN_S_LU,
1504         .coding         = 0,
1505         .rec            = 0,
1506         .rec_val        = 0,
1507         .value          = GSM48_CC_CAUSE_NORMAL_UNSPEC,
1508         .diag_len       = 0,
1509         .diag           = { 0 },
1510 };
1511
1512 /* disconnect message from upper layer or from timer event */
1513 static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
1514 {
1515         struct gsm_mncc *disc = arg;
1516         struct msgb *nmsg;
1517         struct gsm48_hdr *gh;
1518
1519         LOGP(DCC, LOGL_INFO, "sending DISCONNECT\n");
1520
1521         nmsg = gsm48_l3_msgb_alloc();
1522         if (!nmsg)
1523                 return -ENOMEM;
1524         gh = (struct gsm48_hdr *) msgb_put(nmsg, sizeof(*gh));
1525
1526         gh->msg_type = GSM48_MT_CC_DISCONNECT;
1527
1528         gsm48_stop_cc_timer(trans);
1529         gsm48_start_cc_timer(trans, 0x305, GSM48_T305_MS);
1530
1531         /* cause */
1532         if (disc->fields & MNCC_F_CAUSE)
1533                 gsm48_encode_cause(nmsg, 1, &disc->cause);
1534         else
1535                 gsm48_encode_cause(nmsg, 1, &default_cause);
1536
1537         /* facility */
1538         if (disc->fields & MNCC_F_FACILITY)
1539                 gsm48_encode_facility(nmsg, 0, &disc->facility);
1540         /* progress */
1541         if (disc->fields & MNCC_F_PROGRESS)
1542                 gsm48_encode_progress(nmsg, 0, &disc->progress);
1543         /* user-user */
1544         if (disc->fields & MNCC_F_USERUSER)
1545                 gsm48_encode_useruser(nmsg, 0, &disc->useruser);
1546         /* ss version */
1547         if (disc->fields & MNCC_F_SSVERSION)
1548                 gsm48_encode_ssversion(nmsg, &disc->ssversion);
1549
1550         new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
1551
1552         return gsm48_cc_to_mm(nmsg, trans, GSM48_MMCC_DATA_REQ);
1553 }
1554
1555 /* release message from upper layer or from timer event */
1556 static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
1557 {
1558         struct gsm_mncc *rel = arg;
1559         struct msgb *nmsg;
1560         struct gsm48_hdr *gh;
1561
1562         LOGP(DCC, LOGL_INFO, "sending RELEASE\n");
1563
1564         nmsg = gsm48_l3_msgb_alloc();
1565         if (!nmsg)
1566                 return -ENOMEM;
1567         gh = (struct gsm48_hdr *) msgb_put(nmsg, sizeof(*gh));
1568
1569         gh->msg_type = GSM48_MT_CC_RELEASE;
1570
1571         gsm48_stop_cc_timer(trans);
1572         gsm48_start_cc_timer(trans, 0x308, GSM48_T308_MS);
1573
1574         /* cause */
1575         if (rel->fields & MNCC_F_CAUSE)
1576                 gsm48_encode_cause(nmsg, 0, &rel->cause);
1577         /* facility */
1578         if (rel->fields & MNCC_F_FACILITY)
1579                 gsm48_encode_facility(nmsg, 0, &rel->facility);
1580         /* user-user */
1581         if (rel->fields & MNCC_F_USERUSER)
1582                 gsm48_encode_useruser(nmsg, 0, &rel->useruser);
1583         /* ss version */
1584         if (rel->fields & MNCC_F_SSVERSION)
1585                 gsm48_encode_ssversion(nmsg, &rel->ssversion);
1586
1587         trans->cc.T308_second = 0;
1588         memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
1589
1590         if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
1591                 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
1592
1593         gsm48_cc_to_mm(nmsg, trans, GSM48_MMCC_DATA_REQ);
1594
1595 #if 0
1596         /* release without sending MMCC_REL_REQ */
1597         new_cc_state(trans, GSM_CSTATE_NULL);
1598         trans->callref = 0;
1599         trans_free(trans);
1600 #endif
1601
1602         return 0;
1603 }
1604
1605 /* reject message from upper layer */
1606 static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
1607 {
1608         struct gsm_mncc *rel = arg;
1609         struct msgb *nmsg;
1610         struct gsm48_hdr *gh;
1611
1612         LOGP(DCC, LOGL_INFO, "sending RELEASE COMPLETE\n");
1613
1614         nmsg = gsm48_l3_msgb_alloc();
1615         if (!nmsg)
1616                 return -ENOMEM;
1617         gh = (struct gsm48_hdr *) msgb_put(nmsg, sizeof(*gh));
1618
1619         gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
1620
1621         gsm48_stop_cc_timer(trans);
1622
1623         /* cause */
1624         if (rel->fields & MNCC_F_CAUSE)
1625                 gsm48_encode_cause(nmsg, 0, &rel->cause);
1626         /* facility */
1627         if (rel->fields & MNCC_F_FACILITY)
1628                 gsm48_encode_facility(nmsg, 0, &rel->facility);
1629         /* user-user */
1630         if (rel->fields & MNCC_F_USERUSER)
1631                 gsm48_encode_useruser(nmsg, 0, &rel->useruser);
1632         /* ss version */
1633         if (rel->fields & MNCC_F_SSVERSION)
1634                 gsm48_encode_ssversion(nmsg, &rel->ssversion);
1635
1636         /* release without sending MMCC_REL_REQ */
1637         new_cc_state(trans, GSM_CSTATE_NULL);
1638         trans->callref = 0;
1639         trans_free(trans);
1640
1641         return 0;
1642 }
1643
1644 /* disconnect is received from lower layer */
1645 static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
1646 {
1647         struct gsm48_hdr *gh = msgb_l3(msg);
1648         unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1649         struct tlv_parsed tp;
1650         struct gsm_mncc disc;
1651
1652         LOGP(DCC, LOGL_INFO, "received DISCONNECT\n");
1653
1654         gsm48_stop_cc_timer(trans);
1655
1656         new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
1657
1658         memset(&disc, 0, sizeof(struct gsm_mncc));
1659         disc.callref = trans->callref;
1660         tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len,
1661                 GSM48_IE_CAUSE, 0);
1662         /* cause */
1663         if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1664                 disc.fields |= MNCC_F_CAUSE;
1665                 gsm48_decode_cause(&disc.cause,
1666                              TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1667         }
1668         /* facility */
1669         if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1670                 disc.fields |= MNCC_F_FACILITY;
1671                 gsm48_decode_facility(&disc.facility,
1672                                 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1673         }
1674         /* progress */
1675         if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
1676                 disc.fields |= MNCC_F_PROGRESS;
1677                 gsm48_decode_progress(&disc.progress,
1678                                 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
1679         }
1680         /* user-user */
1681         if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1682                 disc.fields |= MNCC_F_USERUSER;
1683                 gsm48_decode_useruser(&disc.useruser,
1684                                 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1685         }
1686
1687         /* store disconnect cause for T305 expiry */
1688         memcpy(&trans->cc.msg, &disc, sizeof(struct gsm_mncc));
1689
1690         return mncc_recvmsg(trans->ms, trans, MNCC_DISC_IND, &disc);
1691 }
1692
1693 /* release is received from lower layer */
1694 static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
1695 {
1696         struct gsm48_hdr *gh = msgb_l3(msg);
1697         unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1698         struct tlv_parsed tp;
1699         struct gsm_mncc rel;
1700
1701         LOGP(DCC, LOGL_INFO, "received RELEASE\n");
1702
1703         gsm48_stop_cc_timer(trans);
1704
1705         memset(&rel, 0, sizeof(struct gsm_mncc));
1706         rel.callref = trans->callref;
1707         tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
1708         /* cause */
1709         if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1710                 rel.fields |= MNCC_F_CAUSE;
1711                 gsm48_decode_cause(&rel.cause,
1712                              TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1713         }
1714         /* facility */
1715         if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1716                 rel.fields |= MNCC_F_FACILITY;
1717                 gsm48_decode_facility(&rel.facility,
1718                                 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1719         }
1720         /* user-user */
1721         if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1722                 rel.fields |= MNCC_F_USERUSER;
1723                 gsm48_decode_useruser(&rel.useruser,
1724                                 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1725         }
1726
1727         /* in case we receive a relase, when we are already in NULL state */
1728         if (trans->cc.state == GSM_CSTATE_NULL) {
1729                 LOGP(DCC, LOGL_INFO, "ignoring RELEASE in NULL state\n");
1730                 /* release MM conn, free trans */
1731                 return gsm48_rel_null_free(trans);
1732         }
1733         if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
1734                 /* release collision 5.4.5 */
1735                 mncc_recvmsg(trans->ms, trans, MNCC_REL_CNF, &rel);
1736         } else {
1737                 struct msgb *nmsg;
1738
1739                 /* forward cause only */
1740                 LOGP(DCC, LOGL_INFO, "sending RELEASE COMPLETE\n");
1741
1742                 nmsg = gsm48_l3_msgb_alloc();
1743                 if (!nmsg)
1744                         return -ENOMEM;
1745                 gh = (struct gsm48_hdr *) msgb_put(nmsg, sizeof(*gh));
1746
1747                 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
1748
1749                 if (rel.fields & MNCC_F_CAUSE)
1750                         gsm48_encode_cause(nmsg, 0, &rel.cause);
1751
1752                 gsm48_cc_to_mm(nmsg, trans, GSM48_MMCC_DATA_REQ);
1753
1754                 /* release indication */
1755                 mncc_recvmsg(trans->ms, trans, MNCC_REL_IND, &rel);
1756         }
1757
1758         /* release MM conn, got NULL state, free trans */
1759         return gsm48_rel_null_free(trans);
1760 }
1761
1762 /* release complete is received from lower layer */
1763 static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
1764 {
1765         struct gsm48_hdr *gh = msgb_l3(msg);
1766         unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1767         struct tlv_parsed tp;
1768         struct gsm_mncc rel;
1769
1770         LOGP(DCC, LOGL_INFO, "received RELEASE COMPLETE\n");
1771
1772         gsm48_stop_cc_timer(trans);
1773
1774         memset(&rel, 0, sizeof(struct gsm_mncc));
1775         rel.callref = trans->callref;
1776         tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
1777         /* cause */
1778         if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1779                 rel.fields |= MNCC_F_CAUSE;
1780                 gsm48_decode_cause(&rel.cause,
1781                              TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1782         }
1783         /* facility */
1784         if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1785                 rel.fields |= MNCC_F_FACILITY;
1786                 gsm48_decode_facility(&rel.facility,
1787                                 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1788         }
1789         /* user-user */
1790         if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1791                 rel.fields |= MNCC_F_USERUSER;
1792                 gsm48_decode_useruser(&rel.useruser,
1793                                 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1794         }
1795
1796         if (trans->callref) {
1797                 switch (trans->cc.state) {
1798                 case GSM_CSTATE_CALL_PRESENT:
1799                         mncc_recvmsg(trans->ms, trans,
1800                                           MNCC_REJ_IND, &rel);
1801                         break;
1802                 case GSM_CSTATE_RELEASE_REQ:
1803                         mncc_recvmsg(trans->ms, trans,
1804                                           MNCC_REL_CNF, &rel);
1805                         break;
1806                 default:
1807                         mncc_recvmsg(trans->ms, trans,
1808                                           MNCC_REL_IND, &rel);
1809                 }
1810         }
1811
1812         /* release MM conn, got NULL state, free trans */
1813         return gsm48_rel_null_free(trans);
1814 }
1815
1816 /*
1817  * state machines
1818  */
1819
1820 /* state trasitions for MNCC messages (upper layer) */
1821 static struct downstate {
1822         u_int32_t       states;
1823         int             type;
1824         int             (*rout) (struct gsm_trans *trans, void *arg);
1825 } downstatelist[] = {
1826         /* mobile originating call establishment */
1827         {SBIT(GSM_CSTATE_NULL), /* 5.2.1 */
1828          MNCC_SETUP_REQ, gsm48_cc_init_mm},
1829
1830         {SBIT(GSM_CSTATE_MM_CONNECTION_PEND), /* 5.2.1 */
1831          MNCC_REL_REQ, gsm48_cc_abort_mm},
1832
1833         /* mobile terminating call establishment */
1834         {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.1 */
1835          MNCC_CALL_CONF_REQ, gsm48_cc_tx_call_conf},
1836
1837         {SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* 5.2.2.3.2 */
1838          MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
1839
1840         {SBIT(GSM_CSTATE_MO_TERM_CALL_CONF) |
1841          SBIT(GSM_CSTATE_CALL_RECEIVED), /* 5.2.2.5 */
1842          MNCC_SETUP_RSP, gsm48_cc_tx_connect},
1843
1844          /* signalling during call */
1845         {SBIT(GSM_CSTATE_ACTIVE), /* 5.3.1 */
1846          MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
1847
1848         {ALL_STATES, /* 5.5.7.1 */
1849          MNCC_START_DTMF_REQ, gsm48_cc_tx_start_dtmf},
1850
1851         {ALL_STATES, /* 5.5.7.3 */
1852          MNCC_STOP_DTMF_REQ, gsm48_cc_tx_stop_dtmf},
1853
1854         {SBIT(GSM_CSTATE_ACTIVE),
1855          MNCC_HOLD_REQ, gsm48_cc_tx_hold},
1856
1857         {SBIT(GSM_CSTATE_ACTIVE),
1858          MNCC_RETRIEVE_REQ, gsm48_cc_tx_retrieve},
1859
1860         {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
1861          MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
1862
1863         {SBIT(GSM_CSTATE_ACTIVE),
1864          MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
1865
1866         /* clearing */
1867         {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) -
1868          SBIT(GSM_CSTATE_RELEASE_REQ) -
1869          SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.3.1 */
1870          MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
1871
1872         {SBIT(GSM_CSTATE_INITIATED),
1873          MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
1874
1875         {ALL_STATES - SBIT(GSM_CSTATE_NULL) -
1876          SBIT(GSM_CSTATE_RELEASE_REQ), /* ??? */
1877          MNCC_REL_REQ, gsm48_cc_tx_release},
1878
1879         /* modify */
1880         {SBIT(GSM_CSTATE_ACTIVE),
1881          MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
1882
1883         {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
1884          MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
1885
1886         {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
1887          MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
1888 };
1889
1890 #define DOWNSLLEN \
1891         (sizeof(downstatelist) / sizeof(struct downstate))
1892
1893 int mncc_send(struct osmocom_ms *ms, int msg_type, void *arg)
1894 {
1895         struct gsm_mncc *data = arg;
1896         struct gsm_trans *trans;
1897         int i, rc;
1898
1899         /* Find callref */
1900         trans = trans_find_by_callref(ms, data->callref);
1901
1902         if (!trans) {
1903                 /* check for SETUP message */
1904                 if (msg_type != MNCC_SETUP_REQ) {
1905                         /* Invalid call reference */
1906                         LOGP(DCC, LOGL_NOTICE, "transaction not found\n");
1907                         return mncc_release_ind(ms, NULL, data->callref,
1908                                 GSM48_CAUSE_LOC_PRN_S_LU,
1909                                 GSM48_CC_CAUSE_INVAL_TRANS_ID);
1910                 }
1911                 if (data->callref >= 0x40000000) {
1912                         LOGP(DCC, LOGL_FATAL, "MNCC ref wrong.\n");
1913                         return mncc_release_ind(ms, NULL, data->callref,
1914                                 GSM48_CAUSE_LOC_PRN_S_LU,
1915                                 GSM48_CC_CAUSE_INVAL_TRANS_ID);
1916                 }
1917
1918                 /* Create transaction */
1919                 trans = trans_alloc(ms, GSM48_PDISC_CC, 0xff, data->callref);
1920                 if (!trans) {
1921                         /* No memory or whatever */
1922                         return mncc_release_ind(ms, NULL, data->callref,
1923                                 GSM48_CAUSE_LOC_PRN_S_LU,
1924                                 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
1925                 }
1926         }
1927
1928         switch (msg_type) {
1929         case GSM_TCHF_FRAME:
1930                 printf("TCH/F frame ignored!\n");
1931                 return -EINVAL;
1932         }
1933
1934         /* Find function for current state and message */
1935         for (i = 0; i < DOWNSLLEN; i++)
1936                 if ((msg_type == downstatelist[i].type)
1937                  && ((1 << trans->cc.state) & downstatelist[i].states))
1938                         break;
1939                 if (i == DOWNSLLEN) {
1940                         LOGP(DCC, LOGL_NOTICE, "Message unhandled at this "
1941                                 "state.\n");
1942                         return 0;
1943                 }
1944                 
1945         rc = downstatelist[i].rout(trans, arg);
1946
1947         return rc;
1948 }
1949
1950 /* state trasitions for call control messages (lower layer) */
1951 static struct datastate {
1952         u_int32_t       states;
1953         int             type;
1954         int             (*rout) (struct gsm_trans *trans, struct msgb *msg);
1955 } datastatelist[] = {
1956         /* mobile originating call establishment */
1957         {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.3 */
1958          GSM48_MT_CC_CALL_PROC, gsm48_cc_rx_call_proceeding},
1959
1960         {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC) |
1961          SBIT(GSM_CSTATE_CALL_DELIVERED), /* 5.2.1.4.1 */
1962          GSM48_MT_CC_PROGRESS, gsm48_cc_rx_progress},
1963
1964         {SBIT(GSM_CSTATE_INITIATED) |
1965          SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.5 */
1966          GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
1967
1968         {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC) |
1969          SBIT(GSM_CSTATE_CALL_DELIVERED), /* 5.2.1.6 */  
1970          GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
1971
1972         /* mobile terminating call establishment */
1973         {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
1974          GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
1975
1976         {SBIT(GSM_CSTATE_CONNECT_REQUEST), /* 5.2.2.6 */
1977          GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
1978
1979          /* signalling during call */
1980         {SBIT(GSM_CSTATE_ACTIVE), /* 5.3.1 */
1981          GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
1982
1983         {ALL_STATES, /* 8.4 */
1984          GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
1985
1986         {ALL_STATES, /* 5.5.7.2 */
1987          GSM48_MT_CC_START_DTMF_ACK, gsm48_cc_rx_start_dtmf_ack},
1988
1989         {ALL_STATES, /* 5.5.7.2 */
1990          GSM48_MT_CC_START_DTMF_REJ, gsm48_cc_rx_start_dtmf_rej},
1991
1992         {ALL_STATES, /* 5.5.7.4 */
1993          GSM48_MT_CC_STOP_DTMF_ACK, gsm48_cc_rx_stop_dtmf_ack},
1994
1995         {SBIT(GSM_CSTATE_ACTIVE),
1996          GSM48_MT_CC_HOLD_ACK, gsm48_cc_rx_hold_ack},
1997
1998         {SBIT(GSM_CSTATE_ACTIVE),
1999          GSM48_MT_CC_HOLD_REJ, gsm48_cc_rx_hold_rej},
2000
2001         {SBIT(GSM_CSTATE_ACTIVE),
2002          GSM48_MT_CC_RETR_ACK, gsm48_cc_rx_retrieve_ack},
2003
2004         {SBIT(GSM_CSTATE_ACTIVE),
2005          GSM48_MT_CC_RETR_REJ, gsm48_cc_rx_retrieve_rej},
2006
2007         {ALL_STATES - SBIT(GSM_CSTATE_NULL),
2008          GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
2009
2010         {SBIT(GSM_CSTATE_ACTIVE),
2011          GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
2012
2013         /* clearing */
2014         {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ) -
2015          SBIT(GSM_CSTATE_DISCONNECT_IND), /* 5.4.4.1.1 */
2016          GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
2017
2018         {ALL_STATES, /* 5.4.3.3 & 5.4.5!!!*/
2019          GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
2020
2021         {ALL_STATES, /* 5.4.4.1.3 */
2022          GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
2023
2024         /* modify */
2025         {SBIT(GSM_CSTATE_ACTIVE),
2026          GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
2027
2028         {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
2029          GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
2030
2031         {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
2032          GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
2033 };
2034
2035 #define DATASLLEN \
2036         (sizeof(datastatelist) / sizeof(struct datastate))
2037
2038 static int gsm48_cc_data_ind(struct gsm_trans *trans, struct msgb *msg)
2039 {
2040         struct osmocom_ms *ms = trans->ms;
2041         struct gsm48_hdr *gh = msgb_l3(msg);
2042         int msg_type = gh->msg_type & 0xbf;
2043         uint8_t transaction_id = ((gh->proto_discr & 0xf0) ^ 0x80) >> 4;
2044                 /* flip */
2045         int msg_supported = 0; /* determine, if message is supported at all */
2046         int i, rc;
2047
2048         /* set transaction ID, if not already */
2049         trans->transaction_id = transaction_id;
2050
2051         /* pull the MMCC header */
2052         msgb_pull(msg, sizeof(struct gsm48_mmxx_hdr));
2053
2054         LOGP(DCC, LOGL_INFO, "(ms %s) Received '%s' in CC state %s\n", ms->name,
2055                 gsm48_cc_msg_name(msg_type),
2056                 gsm48_cc_state_name(trans->cc.state));
2057
2058         /* find function for current state and message */
2059         for (i = 0; i < DATASLLEN; i++) {
2060                 if (msg_type == datastatelist[i].type)
2061                         msg_supported = 1;
2062                 if ((msg_type == datastatelist[i].type)
2063                  && ((1 << trans->cc.state) & datastatelist[i].states))
2064                         break;
2065         }
2066         if (i == DATASLLEN) {
2067                 if (msg_supported) {
2068                         LOGP(DCC, LOGL_NOTICE, "Message unhandled at this "
2069                                 "state.\n");
2070                         return gsm48_cc_tx_status(trans,
2071                                 GSM48_REJECT_MSG_TYPE_NOT_COMPATIBLE);
2072                 } else {
2073                         LOGP(DCC, LOGL_NOTICE, "Message not supported.\n");
2074                         return gsm48_cc_tx_status(trans,
2075                                 GSM48_REJECT_MSG_TYPE_NOT_IMPLEMENTED);
2076                 }
2077         }
2078
2079         rc = datastatelist[i].rout(trans, msg);
2080
2081         return rc;
2082 }
2083
2084 /* receive message from MM layer */
2085 int gsm48_rcv_cc(struct osmocom_ms *ms, struct msgb *msg)
2086 {
2087         struct gsm48_mmxx_hdr *mmh = (struct gsm48_mmxx_hdr *)msg->data;
2088         int msg_type = mmh->msg_type;
2089         struct gsm_trans *trans;
2090         int rc = 0;
2091
2092         trans = trans_find_by_callref(ms, mmh->ref);
2093         if (!trans) {
2094                 trans = trans_alloc(ms, GSM48_PDISC_CC, mmh->transaction_id,
2095                                         mmh->ref);
2096                 if (!trans)
2097                         return -ENOMEM;
2098         }
2099
2100         LOGP(DCC, LOGL_INFO, "(ms %s) Received '%s' in CC state %s\n", ms->name,
2101                 get_mmxx_name(msg_type),
2102                 gsm48_cc_state_name(trans->cc.state));
2103
2104         switch (msg_type) {
2105         case GSM48_MMCC_EST_IND:
2106                 /* data included */
2107                 rc = gsm48_cc_data_ind(trans, msg);
2108                 break;
2109         case GSM48_MMCC_EST_CNF:
2110                 /* send setup after confirm */
2111                 if (trans->cc.state == GSM_CSTATE_MM_CONNECTION_PEND)
2112                         rc = gsm48_cc_tx_setup(trans);
2113                 else
2114                         LOGP(DCC, LOGL_ERROR, "Oops, MMCC-EST-CONF in state "
2115                                 "%d?\n", trans->cc.state);
2116                 break;
2117         case GSM48_MMCC_ERR_IND: /* no supporting re-establishment */
2118         case GSM48_MMCC_REL_IND:
2119                 /* release L4, release transaction */
2120                 mncc_release_ind(trans->ms, trans, trans->callref,
2121                          GSM48_CAUSE_LOC_PRN_S_LU, mmh->cause);
2122                 /* release without sending MMCC_REL_REQ */
2123                 new_cc_state(trans, GSM_CSTATE_NULL);
2124                 trans->callref = 0;
2125                 trans_free(trans);
2126                 break;
2127         case GSM48_MMCC_DATA_IND:
2128                 rc = gsm48_cc_data_ind(trans, msg);
2129                 break;
2130         case GSM48_MMCC_UNIT_DATA_IND:
2131                 break;
2132         case GSM48_MMCC_SYNC_IND:
2133                 break;
2134         default:
2135                 LOGP(DCC, LOGL_NOTICE, "Message unhandled.\n");
2136                 rc = -ENOTSUP;
2137         }
2138
2139         return rc;
2140 }
2141