layer23: Added VTY command to display current states.
[osmocom-bb.git] / src / host / layer23 / src / 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/logging.h>
34 #include <osmocom/osmocom_data.h>
35 #include <osmocom/mncc.h>
36 #include <osmocom/transaction.h>
37 #include <osmocom/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.location = loc;
403         data->cause.value = val;
404 }
405
406 /* send release indication to upper layer */
407 int mncc_release_ind(struct osmocom_ms *ms, struct gsm_trans *trans,
408                      u_int32_t callref, int location, int value)
409 {
410         struct gsm_mncc rel;
411
412         memset(&rel, 0, sizeof(rel));
413         rel.callref = callref;
414         mncc_set_cause(&rel, location, value);
415         return mncc_recvmsg(ms, trans, MNCC_REL_IND, &rel);
416 }
417
418 /* sending status message in response to unknown message */
419 static int gsm48_cc_tx_status(struct gsm_trans *trans, int cause)
420 {
421         struct msgb *nmsg;
422         struct gsm48_hdr *gh;
423         uint8_t *cause_ie, *call_state_ie;
424
425         LOGP(DCC, LOGL_INFO, "sending STATUS (cause %d)\n", cause);
426
427         nmsg = gsm48_l3_msgb_alloc();
428         if (!nmsg)
429                 return -ENOMEM;
430         gh = (struct gsm48_hdr *) msgb_put(nmsg, sizeof(*gh));
431
432         gh->msg_type = GSM48_MT_CC_STATUS;
433
434         cause_ie = msgb_put(nmsg, 3);
435         cause_ie[0] = 2;
436         cause_ie[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_PRN_S_LU;
437         cause_ie[2] = 0x80 | cause;
438
439         call_state_ie = msgb_put(nmsg, 1);
440         call_state_ie[0] = 0xc0 | trans->cc.state;
441
442         return gsm48_cc_to_mm(nmsg, trans, GSM48_MMCC_DATA_REQ);
443 }
444
445 /* reply status enquiry */
446 static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
447 {
448         LOGP(DCC, LOGL_INFO, "received STATUS ENQUIREY\n");
449
450         return gsm48_cc_tx_status(trans, GSM48_CC_CAUSE_RESP_STATUS_INQ);
451 }
452
453 /*
454  * process handlers (mobile originating call establish)
455  */
456
457 /* on SETUP request from L4, init MM connection */
458 static int gsm48_cc_init_mm(struct gsm_trans *trans, void *arg)
459 {
460         struct msgb *nmsg;
461         struct gsm_mncc *data = arg;
462         struct gsm48_mmxx_hdr *nmmh;
463
464         /* store setup message */
465         memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
466
467         new_cc_state(trans, GSM_CSTATE_MM_CONNECTION_PEND);
468
469         /* establish MM connection */
470         nmsg = gsm48_mmxx_msgb_alloc(GSM48_MMCC_EST_REQ, trans->callref,
471                                         trans->transaction_id);
472         if (!nmsg)
473                 return -ENOMEM;
474         nmmh = (struct gsm48_mmxx_hdr *) nmsg->data;
475         if (data->emergency)
476                 nmmh->emergency = 1;
477         LOGP(DCC, LOGL_INFO, "Sending MMCC_EST_REQ\n");
478         return gsm48_mmxx_downmsg(trans->ms, nmsg);
479 }
480
481 /* abort connection prior SETUP */
482 static int gsm48_cc_abort_mm(struct gsm_trans *trans, void *arg)
483 {
484         struct msgb *nmsg;
485
486         /* abort MM connection */
487         nmsg = gsm48_mmxx_msgb_alloc(GSM48_MMCC_REL_REQ, trans->callref,
488                         trans->transaction_id);
489         if (!nmsg)
490                 return -ENOMEM;
491         LOGP(DCC, LOGL_INFO, "Sending MMCC_REL_REQ\n");
492         gsm48_mmxx_downmsg(trans->ms, nmsg);
493
494         new_cc_state(trans, GSM_CSTATE_NULL);
495
496         trans->callref = 0;
497         trans_free(trans);
498
499         return 0;
500 }
501
502 /* setup message from upper layer */
503 static int gsm48_cc_tx_setup(struct gsm_trans *trans)
504 {
505         struct msgb *nmsg;
506         struct gsm48_hdr *gh;
507         struct gsm_mncc *setup = &trans->cc.msg;
508         int rc, transaction_id;
509         uint8_t *ie;
510
511         LOGP(DCC, LOGL_INFO, "sending SETUP\n");
512
513         nmsg = gsm48_l3_msgb_alloc();
514         if (!nmsg)
515                 return -ENOMEM;
516         gh = (struct gsm48_hdr *) msgb_put(nmsg, sizeof(*gh));
517
518         /* transaction id must not be assigned */
519         if (trans->transaction_id != 0xff) { /* unasssigned */
520                 LOGP(DCC, LOGL_NOTICE, "TX Setup with assigned transaction. "
521                         "This is not allowed!\n");
522                 /* Temporarily out of order */
523                 rc = mncc_release_ind(trans->ms, trans, trans->callref,
524                                       GSM48_CAUSE_LOC_PRN_S_LU,
525                                       GSM48_CC_CAUSE_NORMAL_UNSPEC);
526                 trans->callref = 0;
527                 trans_free(trans);
528                 return rc;
529         }
530         
531         /* Get free transaction_id */
532         transaction_id = trans_assign_trans_id(trans->ms, GSM48_PDISC_CC, 0);
533         if (transaction_id < 0) {
534                 /* no free transaction ID */
535                 rc = mncc_release_ind(trans->ms, trans, trans->callref,
536                                       GSM48_CAUSE_LOC_PRN_S_LU,
537                                       GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
538                 trans->callref = 0;
539                 trans_free(trans);
540                 return rc;
541         }
542         trans->transaction_id = transaction_id;
543
544         gh->msg_type = (setup->emergency) ? GSM48_MT_CC_EMERG_SETUP :
545                                                 GSM48_MT_CC_SETUP;
546
547         /* actually we have to start it when CM SERVICE REQUEST has been sent,
548          * but there is no primitive for that defined. i think it is ok to
549          * do it here rather than inventing MMCC-NOTIFY-IND.
550          */
551         gsm48_start_cc_timer(trans, 0x303, GSM48_T303_MS);
552
553         if (!setup->emergency) {
554                 /* bearer capability */
555                 gsm48_encode_bearer_cap(nmsg, 0, &setup->bearer_cap);
556                 /* facility */
557                 if (setup->fields & MNCC_F_FACILITY)
558                         gsm48_encode_facility(nmsg, 0, &setup->facility);
559                 /* called party BCD number */
560                 if (setup->fields & MNCC_F_CALLED)
561                         gsm48_encode_called(nmsg, &setup->called);
562                 /* user-user */
563                 if (setup->fields & MNCC_F_USERUSER)
564                         gsm48_encode_useruser(nmsg, 0, &setup->useruser);
565                 /* ss version */
566                 if (setup->fields & MNCC_F_SSVERSION)
567                         gsm48_encode_ssversion(nmsg, &setup->ssversion);
568                 /* CLIR suppression */
569                 if (setup->clir.sup) {
570                         ie = msgb_put(nmsg, 1);
571                         ie[0] = GSM48_IE_CLIR_SUPP;
572                 }
573                 /* CLIR invocation */
574                 if (setup->clir.inv) {
575                         ie = msgb_put(nmsg, 1);
576                         ie[0] = GSM48_IE_CLIR_INVOC;
577                 }
578                 /* cc cap */
579                 if (setup->fields & MNCC_F_CCCAP)
580                         gsm48_encode_cccap(nmsg, &setup->cccap);
581         }
582
583         /* actually MM CONNECTION PENDING */
584         new_cc_state(trans, GSM_CSTATE_INITIATED);
585
586         return gsm48_cc_to_mm(nmsg, trans, GSM48_MMCC_DATA_REQ);
587 }
588
589 /* progress is received from lower layer */
590 static int gsm48_cc_rx_progress(struct gsm_trans *trans, struct msgb *msg)
591 {
592         struct gsm48_hdr *gh = msgb_l3(msg);
593         unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
594         struct tlv_parsed tp;
595         struct gsm_mncc progress;
596
597         LOGP(DCC, LOGL_INFO, "received PROGRESS\n");
598
599         memset(&progress, 0, sizeof(struct gsm_mncc));
600         progress.callref = trans->callref;
601         tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len,
602                 GSM48_IE_PROGR_IND, 0);
603         /* progress */
604         if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
605                 progress.fields |= MNCC_F_PROGRESS;
606                 gsm48_decode_progress(&progress.progress,
607                                 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
608                 /* store last progress indicator */
609                 trans->cc.prog_ind = progress.progress.descr;
610         }
611         /* user-user */
612         if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
613                 progress.fields |= MNCC_F_USERUSER;
614                 gsm48_decode_useruser(&progress.useruser,
615                                 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
616         }
617
618         return mncc_recvmsg(trans->ms, trans, MNCC_PROGRESS_IND, &progress);
619 }
620
621 /* call proceeding is received from lower layer */
622 static int gsm48_cc_rx_call_proceeding(struct gsm_trans *trans,
623         struct msgb *msg)
624 {
625         struct gsm48_hdr *gh = msgb_l3(msg);
626         unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
627         struct tlv_parsed tp;
628         struct gsm_mncc call_proc;
629
630         LOGP(DCC, LOGL_INFO, "sending CALL PROCEEDING\n");
631
632         gsm48_stop_cc_timer(trans);
633
634         memset(&call_proc, 0, sizeof(struct gsm_mncc));
635         call_proc.callref = trans->callref;
636         tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
637 #if 0
638         /* repeat */
639         if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
640                 call_conf.repeat = 1;
641         if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
642                 call_conf.repeat = 2;
643 #endif
644         /* bearer capability */
645         if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
646                 call_proc.fields |= MNCC_F_BEARER_CAP;
647                 gsm48_decode_bearer_cap(&call_proc.bearer_cap,
648                                   TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
649         }
650         /* facility */
651         if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
652                 call_proc.fields |= MNCC_F_FACILITY;
653                 gsm48_decode_facility(&call_proc.facility,
654                                 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
655         }
656
657         /* progress */
658         if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
659                 call_proc.fields |= MNCC_F_PROGRESS;
660                 gsm48_decode_progress(&call_proc.progress,
661                                 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
662                 /* store last progress indicator */
663                 trans->cc.prog_ind = call_proc.progress.descr;
664         }
665
666         /* start T310, if last progress indicator was 1 or 2 or 64 */
667         if (trans->cc.prog_ind == 1
668          || trans->cc.prog_ind == 2
669          || trans->cc.prog_ind == 64)
670                 gsm48_start_cc_timer(trans, 0x310, GSM48_T310_MS);
671
672         new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
673
674         return mncc_recvmsg(trans->ms, trans, MNCC_CALL_PROC_IND,
675                             &call_proc);
676 }
677
678 /* alerting is received by the lower layer */
679 static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
680 {
681         struct gsm48_hdr *gh = msgb_l3(msg);
682         unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
683         struct tlv_parsed tp;
684         struct gsm_mncc alerting;
685         
686         LOGP(DCC, LOGL_INFO, "sending ALERTING\n");
687
688         gsm48_stop_cc_timer(trans);
689         /* no T301 in MS call control */
690
691         memset(&alerting, 0, sizeof(struct gsm_mncc));
692         alerting.callref = trans->callref;
693         tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
694         /* facility */
695         if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
696                 alerting.fields |= MNCC_F_FACILITY;
697                 gsm48_decode_facility(&alerting.facility,
698                                 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
699         }
700
701         /* progress */
702         if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
703                 alerting.fields |= MNCC_F_PROGRESS;
704                 gsm48_decode_progress(&alerting.progress,
705                                 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
706         }
707         /* user-user */
708         if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
709                 alerting.fields |= MNCC_F_USERUSER;
710                 gsm48_decode_useruser(&alerting.useruser,
711                                 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
712         }
713
714         new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
715
716         return mncc_recvmsg(trans->ms, trans, MNCC_ALERT_IND,
717                             &alerting);
718 }
719
720 /* connect is received from lower layer */
721 static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
722 {
723         struct gsm48_hdr *gh = msgb_l3(msg);
724         unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
725         struct tlv_parsed tp;
726         struct gsm_mncc connect;
727
728         LOGP(DCC, LOGL_INFO, "received CONNECT\n");
729
730         gsm48_stop_cc_timer(trans);
731
732         memset(&connect, 0, sizeof(struct gsm_mncc));
733         connect.callref = trans->callref;
734         tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
735         /* facility */
736         if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
737                 connect.fields |= MNCC_F_FACILITY;
738                 gsm48_decode_facility(&connect.facility,
739                                 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
740         }
741         /* connected */
742         if (TLVP_PRESENT(&tp, GSM48_IE_CONN_BCD)) {
743                 connect.fields |= MNCC_F_CONNECTED;
744                 gsm48_decode_connected(&connect.connected,
745                                 TLVP_VAL(&tp, GSM48_IE_CONN_BCD)-1);
746         }
747         /* progress */
748         if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
749                 connect.fields |= MNCC_F_PROGRESS;
750                 gsm48_decode_progress(&connect.progress,
751                                 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
752         }
753         /* user-user */
754         if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
755                 connect.fields |= MNCC_F_USERUSER;
756                 gsm48_decode_useruser(&connect.useruser,
757                                 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
758         }
759
760         /* ACTIVE state is set during this: */
761         gsm48_cc_tx_connect_ack(trans, NULL);
762
763         return mncc_recvmsg(trans->ms, trans, MNCC_SETUP_CNF, &connect);
764 }
765
766 /* connect ack message from upper layer */
767 static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
768 {
769         struct msgb *nmsg;
770         struct gsm48_hdr *gh;
771
772         LOGP(DCC, LOGL_INFO, "sending CONNECT ACKNOWLEDGE\n");
773
774         nmsg = gsm48_l3_msgb_alloc();
775         if (!nmsg)
776                 return -ENOMEM;
777         gh = (struct gsm48_hdr *) msgb_put(nmsg, sizeof(*gh));
778
779         gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
780
781         new_cc_state(trans, GSM_CSTATE_ACTIVE);
782
783         return gsm48_cc_to_mm(nmsg, trans, GSM48_MMCC_DATA_REQ);
784 }
785
786 /*
787  * process handlers (mobile terminating call establish)
788  */
789
790 /* setup is received from lower layer */
791 static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
792 {
793         struct gsm48_hdr *gh = msgb_l3(msg);
794         unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
795         struct tlv_parsed tp;
796         struct gsm_mncc setup;
797
798         LOGP(DCC, LOGL_INFO, "sending SETUP\n");
799
800         memset(&setup, 0, sizeof(struct gsm_mncc));
801         setup.callref = trans->callref;
802         tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
803
804         /* bearer capability */
805         if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
806                 setup.fields |= MNCC_F_BEARER_CAP;
807                 gsm48_decode_bearer_cap(&setup.bearer_cap,
808                                   TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
809         }
810         /* facility */
811         if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
812                 setup.fields |= MNCC_F_FACILITY;
813                 gsm48_decode_facility(&setup.facility,
814                                 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
815         }
816         /* progress */
817         if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
818                 setup.fields |= MNCC_F_PROGRESS;
819                 gsm48_decode_progress(&setup.progress,
820                                 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
821         }
822         /* signal */
823         if (TLVP_PRESENT(&tp, GSM48_IE_SIGNAL)) {
824                 setup.fields |= MNCC_F_SIGNAL;
825                 gsm48_decode_signal(&setup.signal,
826                                 TLVP_VAL(&tp, GSM48_IE_SIGNAL)-1);
827         }
828         /* calling party bcd number */
829         if (TLVP_PRESENT(&tp, GSM48_IE_CALLING_BCD)) {
830                 setup.fields |= MNCC_F_CALLING;
831                 gsm48_decode_calling(&setup.calling,
832                               TLVP_VAL(&tp, GSM48_IE_CALLING_BCD)-1);
833         }
834         /* called party bcd number */
835         if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
836                 setup.fields |= MNCC_F_CALLED;
837                 gsm48_decode_called(&setup.called,
838                               TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
839         }
840         /* redirecting party bcd number */
841         if (TLVP_PRESENT(&tp, GSM48_IE_REDIR_BCD)) {
842                 setup.fields |= MNCC_F_REDIRECTING;
843                 gsm48_decode_redirecting(&setup.redirecting,
844                               TLVP_VAL(&tp, GSM48_IE_REDIR_BCD)-1);
845         }
846         /* user-user */
847         if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
848                 setup.fields |= MNCC_F_USERUSER;
849                 gsm48_decode_useruser(&setup.useruser,
850                                 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
851         }
852
853         new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
854
855         /* indicate setup to MNCC */
856         mncc_recvmsg(trans->ms, trans, MNCC_SETUP_IND, &setup);
857
858         return 0;
859 }
860
861 /* call conf message from upper layer */
862 static int gsm48_cc_tx_call_conf(struct gsm_trans *trans, void *arg)
863 {
864         struct gsm_mncc *confirm = arg;
865         struct msgb *nmsg;
866         struct gsm48_hdr *gh;
867
868         LOGP(DCC, LOGL_INFO, "sending CALL CONFIRMED (proceeding)\n");
869
870         nmsg = gsm48_l3_msgb_alloc();
871         if (!nmsg)
872                 return -ENOMEM;
873         gh = (struct gsm48_hdr *) msgb_put(nmsg, sizeof(*gh));
874
875         gh->msg_type = GSM48_MT_CC_CALL_CONF;
876
877         new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
878
879         /* bearer capability */
880         if (confirm->fields & MNCC_F_BEARER_CAP)
881                 gsm48_encode_bearer_cap(nmsg, 0, &confirm->bearer_cap);
882         /* cause */
883         if (confirm->fields & MNCC_F_CAUSE)
884                 gsm48_encode_cause(nmsg, 0, &confirm->cause);
885         /* cc cap */
886         if (confirm->fields & MNCC_F_CCCAP)
887                 gsm48_encode_cccap(nmsg, &confirm->cccap);
888
889         return gsm48_cc_to_mm(nmsg, trans, GSM48_MMCC_DATA_REQ);
890 }
891
892 /* alerting message from upper layer */
893 static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
894 {
895         struct gsm_mncc *alerting = arg;
896         struct msgb *nmsg;
897         struct gsm48_hdr *gh;
898
899         LOGP(DCC, LOGL_INFO, "sending ALERTING\n");
900
901         nmsg = gsm48_l3_msgb_alloc();
902         if (!nmsg)
903                 return -ENOMEM;
904         gh = (struct gsm48_hdr *) msgb_put(nmsg, sizeof(*gh));
905
906         gh->msg_type = GSM48_MT_CC_ALERTING;
907
908         /* facility */
909         if (alerting->fields & MNCC_F_FACILITY)
910                 gsm48_encode_facility(nmsg, 0, &alerting->facility);
911         /* user-user */
912         if (alerting->fields & MNCC_F_USERUSER)
913                 gsm48_encode_useruser(nmsg, 0, &alerting->useruser);
914         /* ss version */
915         if (alerting->fields & MNCC_F_SSVERSION)
916                 gsm48_encode_ssversion(nmsg, &alerting->ssversion);
917
918         new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
919         
920         return gsm48_cc_to_mm(nmsg, trans, GSM48_MMCC_DATA_REQ);
921 }
922
923 /* connect message from upper layer */
924 static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
925 {
926         struct gsm_mncc *connect = arg;
927         struct msgb *nmsg;
928         struct gsm48_hdr *gh;
929
930         LOGP(DCC, LOGL_INFO, "sending CONNECT\n");
931
932         nmsg = gsm48_l3_msgb_alloc();
933         if (!nmsg)
934                 return -ENOMEM;
935         gh = (struct gsm48_hdr *) msgb_put(nmsg, sizeof(*gh));
936
937         gh->msg_type = GSM48_MT_CC_CONNECT;
938
939         gsm48_stop_cc_timer(trans);
940         gsm48_start_cc_timer(trans, 0x313, GSM48_T313_MS);
941
942         /* facility */
943         if (connect->fields & MNCC_F_FACILITY)
944                 gsm48_encode_facility(nmsg, 0, &connect->facility);
945         /* user-user */
946         if (connect->fields & MNCC_F_USERUSER)
947                 gsm48_encode_useruser(nmsg, 0, &connect->useruser);
948         /* ss version */
949         if (connect->fields & MNCC_F_SSVERSION)
950                 gsm48_encode_ssversion(nmsg, &connect->ssversion);
951
952         new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
953
954         return gsm48_cc_to_mm(nmsg, trans, GSM48_MMCC_DATA_REQ);
955 }
956
957 /* connect ack is received from lower layer */
958 static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
959 {
960         struct gsm_mncc connect_ack;
961
962         LOGP(DCC, LOGL_INFO, "received CONNECT ACKNOWLEDGE\n");
963
964         gsm48_stop_cc_timer(trans);
965
966         new_cc_state(trans, GSM_CSTATE_ACTIVE);
967         
968         memset(&connect_ack, 0, sizeof(struct gsm_mncc));
969         connect_ack.callref = trans->callref;
970         return mncc_recvmsg(trans->ms, trans, MNCC_SETUP_COMPL_IND,
971                             &connect_ack);
972 }
973
974 /*
975  * process handlers (during active state)
976  */
977
978 /* notify message from upper layer */
979 static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
980 {
981         struct gsm_mncc *notify = arg;
982         struct msgb *nmsg;
983         struct gsm48_hdr *gh;
984
985         LOGP(DCC, LOGL_INFO, "sending NOTIFY\n");
986
987         nmsg = gsm48_l3_msgb_alloc();
988         if (!nmsg)
989                 return -ENOMEM;
990         gh = (struct gsm48_hdr *) msgb_put(nmsg, sizeof(*gh));
991
992         gh->msg_type = GSM48_MT_CC_NOTIFY;
993
994         /* notify */
995         gsm48_encode_notify(nmsg, notify->notify);
996
997         return gsm48_cc_to_mm(nmsg, trans, GSM48_MMCC_DATA_REQ);
998 }
999
1000 /* notify is received from lower layer */
1001 static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
1002 {
1003         struct gsm48_hdr *gh = msgb_l3(msg);
1004         unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1005         struct gsm_mncc notify;
1006
1007         LOGP(DCC, LOGL_INFO, "received NOTIFY\n");
1008
1009         memset(&notify, 0, sizeof(struct gsm_mncc));
1010         notify.callref = trans->callref;
1011         /* notify */
1012         if (payload_len < 1) {
1013                 LOGP(DCC, LOGL_NOTICE, "Short read of notify message error.\n");
1014                 return -EINVAL;
1015         }
1016         gsm48_decode_notify(&notify.notify, gh->data);
1017
1018         return mncc_recvmsg(trans->ms, trans, MNCC_NOTIFY_IND, &notify);
1019 }
1020
1021 /* start dtmf message from upper layer */
1022 static int gsm48_cc_tx_start_dtmf(struct gsm_trans *trans, void *arg)
1023 {
1024         struct gsm_mncc *dtmf = arg;
1025         struct msgb *nmsg;
1026         struct gsm48_hdr *gh;
1027
1028         LOGP(DCC, LOGL_INFO, "sending START DTMF\n");
1029
1030         nmsg = gsm48_l3_msgb_alloc();
1031         if (!nmsg)
1032                 return -ENOMEM;
1033         gh = (struct gsm48_hdr *) msgb_put(nmsg, sizeof(*gh));
1034
1035         gh->msg_type = GSM48_MT_CC_START_DTMF;
1036
1037         /* keypad */
1038         gsm48_encode_keypad(nmsg, dtmf->keypad);
1039
1040         return gsm48_cc_to_mm(nmsg, trans, GSM48_MMCC_DATA_REQ);
1041 }
1042
1043 /* start dtmf ack is received from lower layer */
1044 static int gsm48_cc_rx_start_dtmf_ack(struct gsm_trans *trans, struct msgb *msg)
1045 {
1046         struct gsm48_hdr *gh = msgb_l3(msg);
1047         unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1048         struct tlv_parsed tp;
1049         struct gsm_mncc dtmf;
1050
1051         LOGP(DCC, LOGL_INFO, "received START DTMF ACKNOWLEDGE\n");
1052
1053         memset(&dtmf, 0, sizeof(struct gsm_mncc));
1054         dtmf.callref = trans->callref;
1055         tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
1056         /* keypad facility */
1057         if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
1058                 dtmf.fields |= MNCC_F_KEYPAD;
1059                 gsm48_decode_keypad(&dtmf.keypad,
1060                               TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
1061         }
1062
1063         return mncc_recvmsg(trans->ms, trans, MNCC_START_DTMF_RSP, &dtmf);
1064 }
1065
1066 /* start dtmf rej is received from lower layer */
1067 static int gsm48_cc_rx_start_dtmf_rej(struct gsm_trans *trans, struct msgb *msg)
1068 {
1069         struct gsm48_hdr *gh = msgb_l3(msg);
1070         unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1071         struct gsm_mncc dtmf;
1072
1073         LOGP(DCC, LOGL_INFO, "received START DTMF REJECT\n");
1074
1075         memset(&dtmf, 0, sizeof(struct gsm_mncc));
1076         dtmf.callref = trans->callref;
1077         /* cause */
1078         if (payload_len < 1) {
1079                 LOGP(DCC, LOGL_NOTICE, "Short read of dtmf reject message "
1080                         "error.\n");
1081                 return -EINVAL;
1082         }
1083         gsm48_decode_cause(&dtmf.cause, gh->data);
1084
1085         return mncc_recvmsg(trans->ms, trans, MNCC_START_DTMF_REJ, &dtmf);
1086 }
1087
1088 /* stop dtmf message from upper layer */
1089 static int gsm48_cc_tx_stop_dtmf(struct gsm_trans *trans, void *arg)
1090 {
1091         struct msgb *nmsg;
1092         struct gsm48_hdr *gh;
1093
1094         LOGP(DCC, LOGL_INFO, "sending STOP DTMF\n");
1095
1096         nmsg = gsm48_l3_msgb_alloc();
1097         if (!nmsg)
1098                 return -ENOMEM;
1099         gh = (struct gsm48_hdr *) msgb_put(nmsg, sizeof(*gh));
1100
1101         gh->msg_type = GSM48_MT_CC_STOP_DTMF;
1102
1103         return gsm48_cc_to_mm(nmsg, trans, GSM48_MMCC_DATA_REQ);
1104 }
1105
1106 /* stop dtmf ack is received from lower layer */
1107 static int gsm48_cc_rx_stop_dtmf_ack(struct gsm_trans *trans, struct msgb *msg)
1108 {
1109         struct gsm48_hdr *gh = msgb_l3(msg);
1110         unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1111         struct tlv_parsed tp;
1112         struct gsm_mncc dtmf;
1113
1114         LOGP(DCC, LOGL_INFO, "received STOP DTMF ACKNOWLEDGE\n");
1115
1116         memset(&dtmf, 0, sizeof(struct gsm_mncc));
1117         dtmf.callref = trans->callref;
1118         tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
1119
1120         return mncc_recvmsg(trans->ms, trans, MNCC_STOP_DTMF_RSP, &dtmf);
1121 }
1122
1123 /* hold message from upper layer */
1124 static int gsm48_cc_tx_hold(struct gsm_trans *trans, void *arg)
1125 {
1126         struct msgb *nmsg;
1127         struct gsm48_hdr *gh;
1128
1129         LOGP(DCC, LOGL_INFO, "sending HOLD\n");
1130
1131         nmsg = gsm48_l3_msgb_alloc();
1132         if (!nmsg)
1133                 return -ENOMEM;
1134         gh = (struct gsm48_hdr *) msgb_put(nmsg, sizeof(*gh));
1135
1136         gh->msg_type = GSM48_MT_CC_HOLD;
1137
1138         return gsm48_cc_to_mm(nmsg, trans, GSM48_MMCC_DATA_REQ);
1139 }
1140
1141 /* hold ack is received from lower layer */
1142 static int gsm48_cc_rx_hold_ack(struct gsm_trans *trans, struct msgb *msg)
1143 {
1144         struct gsm_mncc hold;
1145
1146         LOGP(DCC, LOGL_INFO, "received HOLD ACKNOWLEDGE\n");
1147
1148         memset(&hold, 0, sizeof(struct gsm_mncc));
1149         hold.callref = trans->callref;
1150
1151         return mncc_recvmsg(trans->ms, trans, MNCC_HOLD_CNF, &hold);
1152 }
1153
1154 /* hold rej is received from lower layer */
1155 static int gsm48_cc_rx_hold_rej(struct gsm_trans *trans, struct msgb *msg)
1156 {
1157         struct gsm48_hdr *gh = msgb_l3(msg);
1158         unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1159         struct gsm_mncc hold;
1160
1161         LOGP(DCC, LOGL_INFO, "received HOLD REJECT\n");
1162
1163         memset(&hold, 0, sizeof(struct gsm_mncc));
1164         hold.callref = trans->callref;
1165         /* cause */
1166         if (payload_len < 1) {
1167                 LOGP(DCC, LOGL_NOTICE, "Short read of hold reject message "
1168                         "error.\n");
1169                 return -EINVAL;
1170         }
1171         gsm48_decode_cause(&hold.cause, gh->data);
1172
1173         return mncc_recvmsg(trans->ms, trans, MNCC_HOLD_REJ, &hold);
1174 }
1175
1176 /* retrieve message from upper layer */
1177 static int gsm48_cc_tx_retrieve(struct gsm_trans *trans, void *arg)
1178 {
1179         struct msgb *nmsg;
1180         struct gsm48_hdr *gh;
1181
1182         LOGP(DCC, LOGL_INFO, "sending RETRIEVE\n");
1183
1184         nmsg = gsm48_l3_msgb_alloc();
1185         if (!nmsg)
1186                 return -ENOMEM;
1187         gh = (struct gsm48_hdr *) msgb_put(nmsg, sizeof(*gh));
1188
1189         gh->msg_type = GSM48_MT_CC_RETR;
1190
1191         return gsm48_cc_to_mm(nmsg, trans, GSM48_MMCC_DATA_REQ);
1192 }
1193
1194 /* retrieve ack is received from lower layer */
1195 static int gsm48_cc_rx_retrieve_ack(struct gsm_trans *trans, struct msgb *msg)
1196 {
1197         struct gsm_mncc retrieve;
1198
1199         LOGP(DCC, LOGL_INFO, "received RETRIEVE ACKNOWLEDGE\n");
1200
1201         memset(&retrieve, 0, sizeof(struct gsm_mncc));
1202         retrieve.callref = trans->callref;
1203
1204         return mncc_recvmsg(trans->ms, trans, MNCC_RETRIEVE_CNF, &retrieve);
1205 }
1206
1207 /* retrieve rej is received from lower layer */
1208 static int gsm48_cc_rx_retrieve_rej(struct gsm_trans *trans, struct msgb *msg)
1209 {
1210         struct gsm48_hdr *gh = msgb_l3(msg);
1211         unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1212         struct gsm_mncc retrieve;
1213
1214         LOGP(DCC, LOGL_INFO, "received RETRIEVE REJECT\n");
1215
1216         memset(&retrieve, 0, sizeof(struct gsm_mncc));
1217         retrieve.callref = trans->callref;
1218         /* cause */
1219         if (payload_len < 1) {
1220                 LOGP(DCC, LOGL_NOTICE, "Short read of retrieve reject message "
1221                         "error.\n");
1222                 return -EINVAL;
1223         }
1224         gsm48_decode_cause(&retrieve.cause, gh->data);
1225
1226         return mncc_recvmsg(trans->ms, trans, MNCC_RETRIEVE_REJ, &retrieve);
1227 }
1228
1229 /* facility message from upper layer or from timer event */
1230 static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
1231 {
1232         struct gsm_mncc *fac = arg;
1233         struct msgb *nmsg;
1234         struct gsm48_hdr *gh;
1235
1236         LOGP(DCC, LOGL_INFO, "sending FACILITY\n");
1237
1238         nmsg = gsm48_l3_msgb_alloc();
1239         if (!nmsg)
1240                 return -ENOMEM;
1241         gh = (struct gsm48_hdr *) msgb_put(nmsg, sizeof(*gh));
1242
1243         gh->msg_type = GSM48_MT_CC_FACILITY;
1244
1245         /* facility */
1246         gsm48_encode_facility(nmsg, 1, &fac->facility);
1247         /* ss version */
1248         if (fac->fields & MNCC_F_SSVERSION)
1249                 gsm48_encode_ssversion(nmsg, &fac->ssversion);
1250
1251         return gsm48_cc_to_mm(nmsg, trans, GSM48_MMCC_DATA_REQ);
1252 }
1253
1254 /* facility is received from lower layer */
1255 static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
1256 {
1257         struct gsm48_hdr *gh = msgb_l3(msg);
1258         unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1259         struct gsm_mncc fac;
1260
1261         LOGP(DCC, LOGL_INFO, "received FACILITY\n");
1262
1263         memset(&fac, 0, sizeof(struct gsm_mncc));
1264         fac.callref = trans->callref;
1265         if (payload_len < 1) {
1266                 LOGP(DCC, LOGL_NOTICE, "Short read of facility message "
1267                         "error.\n");
1268                 return -EINVAL;
1269         }
1270         /* facility */
1271         gsm48_decode_facility(&fac.facility, gh->data);
1272
1273         return mncc_recvmsg(trans->ms, trans, MNCC_FACILITY_IND, &fac);
1274 }
1275
1276 /* user info message from upper layer or from timer event */
1277 static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
1278 {
1279         struct gsm_mncc *user = arg;
1280         struct msgb *nmsg;
1281         struct gsm48_hdr *gh;
1282
1283         LOGP(DCC, LOGL_INFO, "sending USERINFO\n");
1284
1285         nmsg = gsm48_l3_msgb_alloc();
1286         if (!nmsg)
1287                 return -ENOMEM;
1288         gh = (struct gsm48_hdr *) msgb_put(nmsg, sizeof(*gh));
1289
1290         gh->msg_type = GSM48_MT_CC_USER_INFO;
1291
1292         /* user-user */
1293         if (user->fields & MNCC_F_USERUSER)
1294                 gsm48_encode_useruser(nmsg, 1, &user->useruser);
1295         /* more data */
1296         if (user->more)
1297                 gsm48_encode_more(nmsg);
1298
1299         return gsm48_cc_to_mm(nmsg, trans, GSM48_MMCC_DATA_REQ);
1300 }
1301
1302 /* user info is received from lower layer */
1303 static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
1304 {
1305         struct gsm48_hdr *gh = msgb_l3(msg);
1306         unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1307         struct tlv_parsed tp;
1308         struct gsm_mncc user;
1309
1310         LOGP(DCC, LOGL_INFO, "received USERINFO\n");
1311
1312         memset(&user, 0, sizeof(struct gsm_mncc));
1313         user.callref = trans->callref;
1314         if (payload_len < 1) {
1315                 LOGP(DCC, LOGL_NOTICE, "Short read of userinfo message "
1316                         "error.\n");
1317                 return -EINVAL;
1318         }
1319         tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len,
1320                 GSM48_IE_USER_USER, 0);
1321         /* user-user */
1322         gsm48_decode_useruser(&user.useruser,
1323                         TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1324         /* more data */
1325         if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
1326                 user.more = 1;
1327
1328         return mncc_recvmsg(trans->ms, trans, MNCC_USERINFO_IND, &user);
1329 }
1330
1331 /* modify message from upper layer or from timer event */
1332 static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
1333 {
1334         struct gsm_mncc *modify = arg;
1335         struct msgb *nmsg;
1336         struct gsm48_hdr *gh;
1337
1338         LOGP(DCC, LOGL_INFO, "sending MODIFY\n");
1339
1340         nmsg = gsm48_l3_msgb_alloc();
1341         if (!nmsg)
1342                 return -ENOMEM;
1343         gh = (struct gsm48_hdr *) msgb_put(nmsg, sizeof(*gh));
1344
1345         gh->msg_type = GSM48_MT_CC_MODIFY;
1346
1347         gsm48_start_cc_timer(trans, 0x323, GSM48_T323_MS);
1348
1349         /* bearer capability */
1350         gsm48_encode_bearer_cap(nmsg, 1, &modify->bearer_cap);
1351
1352         new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
1353
1354         return gsm48_cc_to_mm(nmsg, trans, GSM48_MMCC_DATA_REQ);
1355 }
1356
1357 /* modify complete is received from lower layer */
1358 static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans,
1359         struct msgb *msg)
1360 {
1361         struct gsm48_hdr *gh = msgb_l3(msg);
1362         unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1363         struct gsm_mncc modify;
1364
1365         LOGP(DCC, LOGL_INFO, "received MODIFY COMPLETE\n");
1366
1367         gsm48_stop_cc_timer(trans);
1368
1369         memset(&modify, 0, sizeof(struct gsm_mncc));
1370         modify.callref = trans->callref;
1371         if (payload_len < 1) {
1372                 LOGP(DCC, LOGL_NOTICE, "Short read of modify complete message "
1373                         "error.\n");
1374                 return -EINVAL;
1375         }
1376         /* bearer capability */
1377         gsm48_decode_bearer_cap(&modify.bearer_cap, gh->data);
1378
1379         new_cc_state(trans, GSM_CSTATE_ACTIVE);
1380
1381         return mncc_recvmsg(trans->ms, trans, MNCC_MODIFY_CNF, &modify);
1382 }
1383
1384 /* modify reject is received from lower layer */
1385 static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
1386 {
1387         struct gsm48_hdr *gh = msgb_l3(msg);
1388         unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1389         struct tlv_parsed tp;
1390         struct gsm_mncc modify;
1391
1392         LOGP(DCC, LOGL_INFO, "received MODIFY REJECT\n");
1393
1394         gsm48_stop_cc_timer(trans);
1395
1396         memset(&modify, 0, sizeof(struct gsm_mncc));
1397         modify.callref = trans->callref;
1398         if (payload_len < 1) {
1399                 LOGP(DCC, LOGL_NOTICE, "Short read of modify reject message "
1400                         "error.\n");
1401                 return -EINVAL;
1402         }
1403         tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len,
1404                 GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
1405         /* bearer capability */
1406         if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1407                 modify.fields |= MNCC_F_BEARER_CAP;
1408                 gsm48_decode_bearer_cap(&modify.bearer_cap,
1409                                   TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1410         }
1411         /* cause */
1412         if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1413                 modify.fields |= MNCC_F_CAUSE;
1414                 gsm48_decode_cause(&modify.cause,
1415                              TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1416         }
1417
1418         new_cc_state(trans, GSM_CSTATE_ACTIVE);
1419
1420         return mncc_recvmsg(trans->ms, trans, MNCC_MODIFY_REJ, &modify);
1421 }
1422
1423 /* modify is received from lower layer */
1424 static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
1425 {
1426         struct gsm48_hdr *gh = msgb_l3(msg);
1427         unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1428         struct gsm_mncc modify;
1429
1430         LOGP(DCC, LOGL_INFO, "received MODIFY\n");
1431
1432         memset(&modify, 0, sizeof(struct gsm_mncc));
1433         modify.callref = trans->callref;
1434         if (payload_len < 1) {
1435                 LOGP(DCC, LOGL_NOTICE, "Short read of modify message error.\n");
1436                 return -EINVAL;
1437         }
1438         /* bearer capability */
1439         gsm48_decode_bearer_cap(&modify.bearer_cap, gh->data);
1440
1441         new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
1442
1443         return mncc_recvmsg(trans->ms, trans, MNCC_MODIFY_IND, &modify);
1444 }
1445
1446 /* modify complete message from upper layer or from timer event */
1447 static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
1448 {
1449         struct gsm_mncc *modify = arg;
1450         struct msgb *nmsg;
1451         struct gsm48_hdr *gh;
1452
1453         LOGP(DCC, LOGL_INFO, "sending MODIFY COMPLETE\n");
1454
1455         nmsg = gsm48_l3_msgb_alloc();
1456         if (!nmsg)
1457                 return -ENOMEM;
1458         gh = (struct gsm48_hdr *) msgb_put(nmsg, sizeof(*gh));
1459
1460         gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
1461
1462         /* bearer capability */
1463         gsm48_encode_bearer_cap(nmsg, 1, &modify->bearer_cap);
1464
1465         new_cc_state(trans, GSM_CSTATE_ACTIVE);
1466
1467         return gsm48_cc_to_mm(nmsg, trans, GSM48_MMCC_DATA_REQ);
1468 }
1469
1470 /* modify reject message from upper layer or from timer event */
1471 static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
1472 {
1473         struct gsm_mncc *modify = arg;
1474         struct msgb *nmsg;
1475         struct gsm48_hdr *gh;
1476
1477         LOGP(DCC, LOGL_INFO, "sending MODIFY REJECT\n");
1478
1479         nmsg = gsm48_l3_msgb_alloc();
1480         if (!nmsg)
1481                 return -ENOMEM;
1482         gh = (struct gsm48_hdr *) msgb_put(nmsg, sizeof(*gh));
1483
1484         gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
1485
1486         /* bearer capability */
1487         gsm48_encode_bearer_cap(nmsg, 1, &modify->bearer_cap);
1488         /* cause */
1489         gsm48_encode_cause(nmsg, 1, &modify->cause);
1490
1491         new_cc_state(trans, GSM_CSTATE_ACTIVE);
1492
1493         return gsm48_cc_to_mm(nmsg, trans, GSM48_MMCC_DATA_REQ);
1494 }
1495
1496 /*
1497  * process handlers (call clearing)
1498  */
1499
1500 static struct gsm_mncc_cause default_cause = {
1501         .location       = GSM48_CAUSE_LOC_PRN_S_LU,
1502         .coding         = 0,
1503         .rec            = 0,
1504         .rec_val        = 0,
1505         .value          = GSM48_CC_CAUSE_NORMAL_UNSPEC,
1506         .diag_len       = 0,
1507         .diag           = { 0 },
1508 };
1509
1510 /* disconnect message from upper layer or from timer event */
1511 static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
1512 {
1513         struct gsm_mncc *disc = arg;
1514         struct msgb *nmsg;
1515         struct gsm48_hdr *gh;
1516
1517         LOGP(DCC, LOGL_INFO, "sending DISCONNECT\n");
1518
1519         nmsg = gsm48_l3_msgb_alloc();
1520         if (!nmsg)
1521                 return -ENOMEM;
1522         gh = (struct gsm48_hdr *) msgb_put(nmsg, sizeof(*gh));
1523
1524         gh->msg_type = GSM48_MT_CC_DISCONNECT;
1525
1526         gsm48_stop_cc_timer(trans);
1527         gsm48_start_cc_timer(trans, 0x305, GSM48_T305_MS);
1528
1529         /* cause */
1530         if (disc->fields & MNCC_F_CAUSE)
1531                 gsm48_encode_cause(nmsg, 1, &disc->cause);
1532         else
1533                 gsm48_encode_cause(nmsg, 1, &default_cause);
1534
1535         /* facility */
1536         if (disc->fields & MNCC_F_FACILITY)
1537                 gsm48_encode_facility(nmsg, 0, &disc->facility);
1538         /* progress */
1539         if (disc->fields & MNCC_F_PROGRESS)
1540                 gsm48_encode_progress(nmsg, 0, &disc->progress);
1541         /* user-user */
1542         if (disc->fields & MNCC_F_USERUSER)
1543                 gsm48_encode_useruser(nmsg, 0, &disc->useruser);
1544         /* ss version */
1545         if (disc->fields & MNCC_F_SSVERSION)
1546                 gsm48_encode_ssversion(nmsg, &disc->ssversion);
1547
1548         new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
1549
1550         return gsm48_cc_to_mm(nmsg, trans, GSM48_MMCC_DATA_REQ);
1551 }
1552
1553 /* release message from upper layer or from timer event */
1554 static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
1555 {
1556         struct gsm_mncc *rel = arg;
1557         struct msgb *nmsg;
1558         struct gsm48_hdr *gh;
1559
1560         LOGP(DCC, LOGL_INFO, "sending RELEASE\n");
1561
1562         nmsg = gsm48_l3_msgb_alloc();
1563         if (!nmsg)
1564                 return -ENOMEM;
1565         gh = (struct gsm48_hdr *) msgb_put(nmsg, sizeof(*gh));
1566
1567         gh->msg_type = GSM48_MT_CC_RELEASE;
1568
1569         gsm48_stop_cc_timer(trans);
1570         gsm48_start_cc_timer(trans, 0x308, GSM48_T308_MS);
1571
1572         /* cause */
1573         if (rel->fields & MNCC_F_CAUSE)
1574                 gsm48_encode_cause(nmsg, 0, &rel->cause);
1575         /* facility */
1576         if (rel->fields & MNCC_F_FACILITY)
1577                 gsm48_encode_facility(nmsg, 0, &rel->facility);
1578         /* user-user */
1579         if (rel->fields & MNCC_F_USERUSER)
1580                 gsm48_encode_useruser(nmsg, 0, &rel->useruser);
1581         /* ss version */
1582         if (rel->fields & MNCC_F_SSVERSION)
1583                 gsm48_encode_ssversion(nmsg, &rel->ssversion);
1584
1585         trans->cc.T308_second = 0;
1586         memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
1587
1588         if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
1589                 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
1590
1591         gsm48_cc_to_mm(nmsg, trans, GSM48_MMCC_DATA_REQ);
1592
1593         /* release without sending MMCC_REL_REQ */
1594         new_cc_state(trans, GSM_CSTATE_NULL);
1595         trans->callref = 0;
1596         trans_free(trans);
1597
1598         return 0;
1599 }
1600
1601 /* reject message from upper layer */
1602 static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
1603 {
1604         struct gsm_mncc *rel = arg;
1605         struct msgb *nmsg;
1606         struct gsm48_hdr *gh;
1607
1608         LOGP(DCC, LOGL_INFO, "sending RELEASE COMPLETE\n");
1609
1610         nmsg = gsm48_l3_msgb_alloc();
1611         if (!nmsg)
1612                 return -ENOMEM;
1613         gh = (struct gsm48_hdr *) msgb_put(nmsg, sizeof(*gh));
1614
1615         gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
1616
1617         gsm48_stop_cc_timer(trans);
1618
1619         /* cause */
1620         if (rel->fields & MNCC_F_CAUSE)
1621                 gsm48_encode_cause(nmsg, 0, &rel->cause);
1622         /* facility */
1623         if (rel->fields & MNCC_F_FACILITY)
1624                 gsm48_encode_facility(nmsg, 0, &rel->facility);
1625         /* user-user */
1626         if (rel->fields & MNCC_F_USERUSER)
1627                 gsm48_encode_useruser(nmsg, 0, &rel->useruser);
1628         /* ss version */
1629         if (rel->fields & MNCC_F_SSVERSION)
1630                 gsm48_encode_ssversion(nmsg, &rel->ssversion);
1631
1632         /* release without sending MMCC_REL_REQ */
1633         new_cc_state(trans, GSM_CSTATE_NULL);
1634         trans->callref = 0;
1635         trans_free(trans);
1636
1637         return 0;
1638 }
1639
1640 /* disconnect is received from lower layer */
1641 static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
1642 {
1643         struct gsm48_hdr *gh = msgb_l3(msg);
1644         unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1645         struct tlv_parsed tp;
1646         struct gsm_mncc disc;
1647
1648         LOGP(DCC, LOGL_INFO, "received DISCONNECT\n");
1649
1650         gsm48_stop_cc_timer(trans);
1651
1652         new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
1653
1654         memset(&disc, 0, sizeof(struct gsm_mncc));
1655         disc.callref = trans->callref;
1656         tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len,
1657                 GSM48_IE_CAUSE, 0);
1658         /* cause */
1659         if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1660                 disc.fields |= MNCC_F_CAUSE;
1661                 gsm48_decode_cause(&disc.cause,
1662                              TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1663         }
1664         /* facility */
1665         if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1666                 disc.fields |= MNCC_F_FACILITY;
1667                 gsm48_decode_facility(&disc.facility,
1668                                 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1669         }
1670         /* progress */
1671         if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
1672                 disc.fields |= MNCC_F_PROGRESS;
1673                 gsm48_decode_progress(&disc.progress,
1674                                 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
1675         }
1676         /* user-user */
1677         if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1678                 disc.fields |= MNCC_F_USERUSER;
1679                 gsm48_decode_useruser(&disc.useruser,
1680                                 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1681         }
1682
1683         /* store disconnect cause for T305 expiry */
1684         memcpy(&trans->cc.msg, &disc, sizeof(struct gsm_mncc));
1685
1686         return mncc_recvmsg(trans->ms, trans, MNCC_DISC_IND, &disc);
1687 }
1688
1689 /* release is received from lower layer */
1690 static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
1691 {
1692         struct gsm48_hdr *gh = msgb_l3(msg);
1693         unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1694         struct tlv_parsed tp;
1695         struct gsm_mncc rel;
1696
1697         LOGP(DCC, LOGL_INFO, "received RELEASE\n");
1698
1699         gsm48_stop_cc_timer(trans);
1700
1701         memset(&rel, 0, sizeof(struct gsm_mncc));
1702         rel.callref = trans->callref;
1703         tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
1704         /* cause */
1705         if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1706                 rel.fields |= MNCC_F_CAUSE;
1707                 gsm48_decode_cause(&rel.cause,
1708                              TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1709         }
1710         /* facility */
1711         if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1712                 rel.fields |= MNCC_F_FACILITY;
1713                 gsm48_decode_facility(&rel.facility,
1714                                 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1715         }
1716         /* user-user */
1717         if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1718                 rel.fields |= MNCC_F_USERUSER;
1719                 gsm48_decode_useruser(&rel.useruser,
1720                                 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1721         }
1722
1723         if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
1724                 /* release collision 5.4.5 */
1725                 mncc_recvmsg(trans->ms, trans, MNCC_REL_CNF, &rel);
1726         } else {
1727                 struct msgb *nmsg;
1728
1729                 /* forward cause only */
1730                 LOGP(DCC, LOGL_INFO, "sending RELEASE COMPLETE\n");
1731
1732                 nmsg = gsm48_l3_msgb_alloc();
1733                 if (!nmsg)
1734                         return -ENOMEM;
1735                 gh = (struct gsm48_hdr *) msgb_put(nmsg, sizeof(*gh));
1736
1737                 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
1738
1739                 if (rel.fields & MNCC_F_CAUSE)
1740                         gsm48_encode_cause(nmsg, 0, &rel.cause);
1741
1742                 gsm48_cc_to_mm(nmsg, trans, GSM48_MMCC_DATA_REQ);
1743
1744                 /* release indication */
1745                 mncc_recvmsg(trans->ms, trans, MNCC_REL_IND, &rel);
1746         }
1747
1748         /* release MM conn, got NULL state, free trans */
1749         return gsm48_rel_null_free(trans);
1750 }
1751
1752 /* release complete is received from lower layer */
1753 static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
1754 {
1755         struct gsm48_hdr *gh = msgb_l3(msg);
1756         unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1757         struct tlv_parsed tp;
1758         struct gsm_mncc rel;
1759
1760         LOGP(DCC, LOGL_INFO, "received RELEASE COMPLETE\n");
1761
1762         gsm48_stop_cc_timer(trans);
1763
1764         memset(&rel, 0, sizeof(struct gsm_mncc));
1765         rel.callref = trans->callref;
1766         tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
1767         /* cause */
1768         if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1769                 rel.fields |= MNCC_F_CAUSE;
1770                 gsm48_decode_cause(&rel.cause,
1771                              TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1772         }
1773         /* facility */
1774         if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1775                 rel.fields |= MNCC_F_FACILITY;
1776                 gsm48_decode_facility(&rel.facility,
1777                                 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1778         }
1779         /* user-user */
1780         if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1781                 rel.fields |= MNCC_F_USERUSER;
1782                 gsm48_decode_useruser(&rel.useruser,
1783                                 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1784         }
1785
1786         if (trans->callref) {
1787                 switch (trans->cc.state) {
1788                 case GSM_CSTATE_CALL_PRESENT:
1789                         mncc_recvmsg(trans->ms, trans,
1790                                           MNCC_REJ_IND, &rel);
1791                         break;
1792                 case GSM_CSTATE_RELEASE_REQ:
1793                         mncc_recvmsg(trans->ms, trans,
1794                                           MNCC_REL_CNF, &rel);
1795                         break;
1796                 default:
1797                         mncc_recvmsg(trans->ms, trans,
1798                                           MNCC_REL_IND, &rel);
1799                 }
1800         }
1801
1802         /* release MM conn, got NULL state, free trans */
1803         return gsm48_rel_null_free(trans);
1804 }
1805
1806 /*
1807  * state machines
1808  */
1809
1810 /* state trasitions for MNCC messages (upper layer) */
1811 static struct downstate {
1812         u_int32_t       states;
1813         int             type;
1814         int             (*rout) (struct gsm_trans *trans, void *arg);
1815 } downstatelist[] = {
1816         /* mobile originating call establishment */
1817         {SBIT(GSM_CSTATE_NULL), /* 5.2.1 */
1818          MNCC_SETUP_REQ, gsm48_cc_init_mm},
1819
1820         {SBIT(GSM_CSTATE_MM_CONNECTION_PEND), /* 5.2.1 */
1821          MNCC_REL_REQ, gsm48_cc_abort_mm},
1822
1823         /* mobile terminating call establishment */
1824         {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.1 */
1825          MNCC_CALL_CONF_REQ, gsm48_cc_tx_call_conf},
1826
1827         {SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* 5.2.2.3.2 */
1828          MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
1829
1830         {SBIT(GSM_CSTATE_MO_TERM_CALL_CONF) |
1831          SBIT(GSM_CSTATE_CALL_RECEIVED), /* 5.2.2.5 */
1832          MNCC_SETUP_RSP, gsm48_cc_tx_connect},
1833
1834          /* signalling during call */
1835         {SBIT(GSM_CSTATE_ACTIVE), /* 5.3.1 */
1836          MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
1837
1838         {ALL_STATES, /* 5.5.7.1 */
1839          MNCC_START_DTMF_REQ, gsm48_cc_tx_start_dtmf},
1840
1841         {ALL_STATES, /* 5.5.7.3 */
1842          MNCC_STOP_DTMF_REQ, gsm48_cc_tx_stop_dtmf},
1843
1844         {SBIT(GSM_CSTATE_ACTIVE),
1845          MNCC_HOLD_REQ, gsm48_cc_tx_hold},
1846
1847         {SBIT(GSM_CSTATE_ACTIVE),
1848          MNCC_RETRIEVE_REQ, gsm48_cc_tx_retrieve},
1849
1850         {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
1851          MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
1852
1853         {SBIT(GSM_CSTATE_ACTIVE),
1854          MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
1855
1856         /* clearing */
1857         {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) -
1858          SBIT(GSM_CSTATE_RELEASE_REQ) -
1859          SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.3.1 */
1860          MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
1861
1862         {SBIT(GSM_CSTATE_INITIATED),
1863          MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
1864
1865         {ALL_STATES - SBIT(GSM_CSTATE_NULL) -
1866          SBIT(GSM_CSTATE_RELEASE_REQ), /* ??? */
1867          MNCC_REL_REQ, gsm48_cc_tx_release},
1868
1869         /* modify */
1870         {SBIT(GSM_CSTATE_ACTIVE),
1871          MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
1872
1873         {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
1874          MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
1875
1876         {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
1877          MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
1878 };
1879
1880 #define DOWNSLLEN \
1881         (sizeof(downstatelist) / sizeof(struct downstate))
1882
1883 int mncc_send(struct osmocom_ms *ms, int msg_type, void *arg)
1884 {
1885         struct gsm_mncc *data = arg;
1886         struct gsm_trans *trans;
1887         int i, rc;
1888
1889         /* Find callref */
1890         trans = trans_find_by_callref(ms, data->callref);
1891
1892         if (!trans) {
1893                 /* check for SETUP message */
1894                 if (msg_type != MNCC_SETUP_REQ) {
1895                         /* Invalid call reference */
1896                         return mncc_release_ind(ms, NULL, data->callref,
1897                                 GSM48_CAUSE_LOC_PRN_S_LU,
1898                                 GSM48_CC_CAUSE_INVAL_TRANS_ID);
1899                 }
1900                 if (data->callref >= 0x40000000) {
1901                         LOGP(DCC, LOGL_FATAL, "MNCC ref wrong.\n");
1902                         return mncc_release_ind(ms, NULL, data->callref,
1903                                 GSM48_CAUSE_LOC_PRN_S_LU,
1904                                 GSM48_CC_CAUSE_INVAL_TRANS_ID);
1905                 }
1906
1907                 /* Create transaction */
1908                 trans = trans_alloc(ms, GSM48_PDISC_CC, 0xff, data->callref);
1909                 if (!trans) {
1910                         /* No memory or whatever */
1911                         return mncc_release_ind(ms, NULL, data->callref,
1912                                 GSM48_CAUSE_LOC_PRN_S_LU,
1913                                 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
1914                 }
1915         }
1916
1917         /* Find function for current state and message */
1918         for (i = 0; i < DOWNSLLEN; i++)
1919                 if ((msg_type == downstatelist[i].type)
1920                  && ((1 << trans->cc.state) & downstatelist[i].states))
1921                         break;
1922                 if (i == DOWNSLLEN) {
1923                         LOGP(DCC, LOGL_NOTICE, "Message unhandled at this "
1924                                 "state.\n");
1925                         return 0;
1926                 }
1927                 
1928         rc = downstatelist[i].rout(trans, arg);
1929
1930         return rc;
1931 }
1932
1933 /* state trasitions for call control messages (lower layer) */
1934 static struct datastate {
1935         u_int32_t       states;
1936         int             type;
1937         int             (*rout) (struct gsm_trans *trans, struct msgb *msg);
1938 } datastatelist[] = {
1939         /* mobile originating call establishment */
1940         {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.3 */
1941          GSM48_MT_CC_CALL_PROC, gsm48_cc_rx_call_proceeding},
1942
1943         {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC) |
1944          SBIT(GSM_CSTATE_CALL_DELIVERED), /* 5.2.1.4.1 */
1945          MNCC_PROGRESS_REQ, gsm48_cc_rx_progress},
1946
1947         {SBIT(GSM_CSTATE_INITIATED) |
1948          SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.5 */
1949          GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
1950
1951         {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC) |
1952          SBIT(GSM_CSTATE_CALL_DELIVERED), /* 5.2.1.6 */  
1953          GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
1954
1955         /* mobile terminating call establishment */
1956         {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
1957          GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
1958
1959         {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.6 */
1960          GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
1961
1962          /* signalling during call */
1963         {SBIT(GSM_CSTATE_ACTIVE), /* 5.3.1 */
1964          GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
1965
1966         {ALL_STATES, /* 8.4 */
1967          GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
1968
1969         {ALL_STATES, /* 5.5.7.2 */
1970          GSM48_MT_CC_START_DTMF_ACK, gsm48_cc_rx_start_dtmf_ack},
1971
1972         {ALL_STATES, /* 5.5.7.2 */
1973          GSM48_MT_CC_START_DTMF_REJ, gsm48_cc_rx_start_dtmf_rej},
1974
1975         {ALL_STATES, /* 5.5.7.4 */
1976          GSM48_MT_CC_STOP_DTMF_ACK, gsm48_cc_rx_stop_dtmf_ack},
1977
1978         {SBIT(GSM_CSTATE_ACTIVE),
1979          GSM48_MT_CC_HOLD_ACK, gsm48_cc_rx_hold_ack},
1980
1981         {SBIT(GSM_CSTATE_ACTIVE),
1982          GSM48_MT_CC_HOLD_REJ, gsm48_cc_rx_hold_rej},
1983
1984         {SBIT(GSM_CSTATE_ACTIVE),
1985          GSM48_MT_CC_RETR_ACK, gsm48_cc_rx_retrieve_ack},
1986
1987         {SBIT(GSM_CSTATE_ACTIVE),
1988          GSM48_MT_CC_RETR_REJ, gsm48_cc_rx_retrieve_rej},
1989
1990         {ALL_STATES - SBIT(GSM_CSTATE_NULL),
1991          GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
1992
1993         {SBIT(GSM_CSTATE_ACTIVE),
1994          GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
1995
1996         /* clearing */
1997         {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ) -
1998          SBIT(GSM_CSTATE_DISCONNECT_IND), /* 5.4.4.1.1 */
1999          GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
2000
2001         {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.3.3 & 5.4.5!!!*/
2002          GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
2003
2004         {ALL_STATES, /* 5.4.4.1.3 */
2005          GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
2006
2007         /* modify */
2008         {SBIT(GSM_CSTATE_ACTIVE),
2009          GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
2010
2011         {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
2012          GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
2013
2014         {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
2015          GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
2016 };
2017
2018 #define DATASLLEN \
2019         (sizeof(datastatelist) / sizeof(struct datastate))
2020
2021 static int gsm48_cc_data_ind(struct gsm_trans *trans, struct msgb *msg)
2022 {
2023         struct osmocom_ms *ms = trans->ms;
2024         struct gsm48_hdr *gh = msgb_l3(msg);
2025         int msg_type = gh->msg_type & 0xbf;
2026         uint8_t transaction_id = ((gh->proto_discr & 0xf0) ^ 0x80) >> 4;
2027                 /* flip */
2028         int msg_supported = 0; /* determine, if message is supported at all */
2029         int i, rc;
2030
2031         /* set transaction ID, if not already */
2032         trans->transaction_id = transaction_id;
2033
2034         /* pull the MMCC header */
2035         msgb_pull(msg, sizeof(struct gsm48_mmxx_hdr));
2036
2037         LOGP(DCC, LOGL_INFO, "(ms %s) Received '%s' in CC state %s\n", ms->name,
2038                 gsm48_cc_msg_name(msg_type),
2039                 gsm48_cc_state_name(trans->cc.state));
2040
2041         /* find function for current state and message */
2042         for (i = 0; i < DATASLLEN; i++) {
2043                 if (msg_type == datastatelist[i].type)
2044                         msg_supported = 1;
2045                 if ((msg_type == datastatelist[i].type)
2046                  && ((1 << trans->cc.state) & datastatelist[i].states))
2047                         break;
2048         }
2049         if (i == DATASLLEN) {
2050                 if (msg_supported) {
2051                         LOGP(DCC, LOGL_NOTICE, "Message unhandled at this "
2052                                 "state.\n");
2053                         return gsm48_cc_tx_status(trans,
2054                                 GSM48_REJECT_MSG_TYPE_NOT_COMPATIBLE);
2055                 } else {
2056                         LOGP(DCC, LOGL_NOTICE, "Message not supported.\n");
2057                         return gsm48_cc_tx_status(trans,
2058                                 GSM48_REJECT_MSG_TYPE_NOT_IMPLEMENTED);
2059                 }
2060         }
2061
2062         rc = datastatelist[i].rout(trans, msg);
2063
2064         return rc;
2065 }
2066
2067 /* receive message from MM layer */
2068 int gsm48_rcv_cc(struct osmocom_ms *ms, struct msgb *msg)
2069 {
2070         struct gsm48_mmxx_hdr *mmh = (struct gsm48_mmxx_hdr *)msg->data;
2071         int msg_type = mmh->msg_type;
2072         struct gsm_trans *trans;
2073         int rc = 0;
2074
2075         trans = trans_find_by_callref(ms, mmh->ref);
2076         if (!trans) {
2077                 trans = trans_alloc(ms, GSM48_PDISC_CC, mmh->transaction_id,
2078                                         mmh->ref);
2079                 if (!trans)
2080                         return -ENOMEM;
2081         }
2082
2083         LOGP(DCC, LOGL_INFO, "(ms %s) Received '%s' in CC state %s\n", ms->name,
2084                 get_mmxx_name(msg_type),
2085                 gsm48_cc_state_name(trans->cc.state));
2086
2087         switch (msg_type) {
2088         case GSM48_MMCC_EST_IND:
2089                 /* data included */
2090                 rc = gsm48_cc_data_ind(trans, msg);
2091                 break;
2092         case GSM48_MMCC_EST_CNF:
2093                 /* send setup after confirm */
2094                 if (trans->cc.state == GSM_CSTATE_MM_CONNECTION_PEND)
2095                         rc = gsm48_cc_tx_setup(trans);
2096                 else
2097                         LOGP(DCC, LOGL_ERROR, "Oops, MMCC-EST-CONF in state "
2098                                 "%d?\n", trans->cc.state);
2099                 break;
2100         case GSM48_MMCC_ERR_IND: /* no supporting re-establishment */
2101         case GSM48_MMCC_REL_IND:
2102                 /* release L4, release transaction */
2103                 mncc_release_ind(trans->ms, trans, trans->callref,
2104                          GSM48_CAUSE_LOC_PRN_S_LU, mmh->cause);
2105                 /* release without sending MMCC_REL_REQ */
2106                 new_cc_state(trans, GSM_CSTATE_NULL);
2107                 trans->callref = 0;
2108                 trans_free(trans);
2109                 break;
2110         case GSM48_MMCC_DATA_IND:
2111                 rc = gsm48_cc_data_ind(trans, msg);
2112                 break;
2113         case GSM48_MMCC_UNIT_DATA_IND:
2114                 break;
2115         case GSM48_MMCC_SYNC_IND:
2116                 break;
2117         default:
2118                 LOGP(DCC, LOGL_NOTICE, "Message unhandled.\n");
2119                 rc = -ENOTSUP;
2120         }
2121
2122         return rc;
2123 }
2124