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