Harald Welte | 4bfdfe7 | 2009-06-10 23:11:52 +0800 | [diff] [blame^] | 1 | /* (C) 2008-2009 by Harald Welte <laforge@gnumonks.org> |
| 2 | * (C) 2009 by Andreas Eversberg <Andreas.Eversberg@versatel.de> |
| 3 | * All Rights Reserved |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License along |
| 16 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | * |
| 19 | */ |
| 20 | |
| 21 | |
| 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | #include <sys/types.h> |
| 26 | |
| 27 | #include <openbsc/gsm_04_08.h> |
| 28 | #include <openbsc/debug.h> |
| 29 | #include <openbsc/mncc.h> |
| 30 | |
| 31 | static struct mncc_names { |
| 32 | char *name; |
| 33 | int value; |
| 34 | } mncc_names[] = { |
| 35 | {"MNCC_SETUP_REQ", 0x0101}, |
| 36 | {"MNCC_SETUP_IND", 0x0102}, |
| 37 | {"MNCC_SETUP_RSP", 0x0103}, |
| 38 | {"MNCC_SETUP_CNF", 0x0104}, |
| 39 | {"MNCC_SETUP_COMPL_REQ",0x0105}, |
| 40 | {"MNCC_SETUP_COMPL_IND",0x0106}, |
| 41 | {"MNCC_CALL_CONF_IND", 0x0107}, |
| 42 | {"MNCC_CALL_PROC_REQ", 0x0108}, |
| 43 | {"MNCC_PROGRESS_REQ", 0x0109}, |
| 44 | {"MNCC_ALERT_REQ", 0x010a}, |
| 45 | {"MNCC_ALERT_IND", 0x010b}, |
| 46 | {"MNCC_NOTIFY_REQ", 0x010c}, |
| 47 | {"MNCC_NOTIFY_IND", 0x010d}, |
| 48 | {"MNCC_DISC_REQ", 0x010e}, |
| 49 | {"MNCC_DISC_IND", 0x010f}, |
| 50 | {"MNCC_REL_REQ", 0x0110}, |
| 51 | {"MNCC_REL_IND", 0x0111}, |
| 52 | {"MNCC_REL_CNF", 0x0112}, |
| 53 | {"MNCC_FACILITY_REQ", 0x0113}, |
| 54 | {"MNCC_FACILITY_IND", 0x0114}, |
| 55 | {"MNCC_START_DTMF_IND", 0x0115}, |
| 56 | {"MNCC_START_DTMF_RSP", 0x0116}, |
| 57 | {"MNCC_START_DTMF_REJ", 0x0117}, |
| 58 | {"MNCC_STOP_DTMF_IND", 0x0118}, |
| 59 | {"MNCC_STOP_DTMF_RSP", 0x0119}, |
| 60 | {"MNCC_MODIFY_REQ", 0x011a}, |
| 61 | {"MNCC_MODIFY_IND", 0x011b}, |
| 62 | {"MNCC_MODIFY_RSP", 0x011c}, |
| 63 | {"MNCC_MODIFY_CNF", 0x011d}, |
| 64 | {"MNCC_MODIFY_REJ", 0x011e}, |
| 65 | {"MNCC_HOLD_IND", 0x011f}, |
| 66 | {"MNCC_HOLD_CNF", 0x0120}, |
| 67 | {"MNCC_HOLD_REJ", 0x0121}, |
| 68 | {"MNCC_RETRIEVE_IND", 0x0122}, |
| 69 | {"MNCC_RETRIEVE_CNF", 0x0123}, |
| 70 | {"MNCC_RETRIEVE_REJ", 0x0124}, |
| 71 | {"MNCC_USERINFO_REQ", 0x0125}, |
| 72 | {"MNCC_USERINFO_IND", 0x0126}, |
| 73 | {"MNCC_REJ_REQ", 0x0127}, |
| 74 | {"MNCC_REJ_IND", 0x0128}, |
| 75 | |
| 76 | {"MNCC_BRIDGE", 0x0200}, |
| 77 | {"MNCC_FRAME_RECV", 0x0201}, |
| 78 | {"MNCC_FRAME_DROP", 0x0202}, |
| 79 | {"MNCC_LCHAN_MODIFY", 0x0203}, |
| 80 | |
| 81 | {"GSM_TRAU_FRAME", 0x0300}, |
| 82 | |
| 83 | {NULL, 0} |
| 84 | }; |
| 85 | |
| 86 | static LLIST_HEAD(call_list); |
| 87 | |
| 88 | static u_int32_t new_callref = 0x00000001; |
| 89 | |
| 90 | char *get_mncc_name(int value) |
| 91 | { |
| 92 | int i; |
| 93 | |
| 94 | for (i = 0; mncc_names[i].name; i++) { |
| 95 | if (mncc_names[i].value == value) |
| 96 | return mncc_names[i].name; |
| 97 | } |
| 98 | |
| 99 | return "MNCC_Unknown"; |
| 100 | } |
| 101 | |
| 102 | static void free_call(struct gsm_call *call) |
| 103 | { |
| 104 | llist_del(&call->entry); |
| 105 | DEBUGP(DMNCC, "(call %x) Call removed.\n", call->callref); |
| 106 | free(call); |
| 107 | } |
| 108 | |
| 109 | |
| 110 | struct gsm_call *get_call_ref(u_int32_t callref) |
| 111 | { |
| 112 | struct gsm_call *callt; |
| 113 | |
| 114 | llist_for_each_entry(callt, &call_list, entry) { |
| 115 | if (callt->callref == callref) |
| 116 | return callt; |
| 117 | } |
| 118 | return NULL; |
| 119 | } |
| 120 | |
| 121 | void mncc_set_cause(struct gsm_mncc *data, int loc, int val) |
| 122 | { |
| 123 | data->fields |= MNCC_F_CAUSE; |
| 124 | data->cause.location = loc; |
| 125 | data->cause.value = val; |
| 126 | } |
| 127 | |
| 128 | /* on incoming call, look up database and send setup to remote subscr. */ |
| 129 | static int mncc_setup_ind(struct gsm_call *call, int msg_type, |
| 130 | struct gsm_mncc *setup) |
| 131 | { |
| 132 | struct gsm_mncc mncc; |
| 133 | struct gsm_call *remote; |
| 134 | |
| 135 | /* already have remote call */ |
| 136 | if (call->remote_ref) |
| 137 | return 0; |
| 138 | |
| 139 | /* create remote call */ |
| 140 | if (!(remote = calloc(1, sizeof(struct gsm_call)))) { |
| 141 | memset(&mncc, 0, sizeof(struct gsm_mncc)); |
| 142 | mncc.callref = call->callref; |
| 143 | mncc_set_cause(&mncc, 1, 47); |
| 144 | mncc_send(call->net, MNCC_REJ_REQ, &mncc); |
| 145 | free_call(call); |
| 146 | return 0; |
| 147 | } |
| 148 | llist_add_tail(&remote->entry, &call_list); |
| 149 | remote->net = call->net; |
| 150 | remote->callref = new_callref++; |
| 151 | DEBUGP(DMNCC, "(call %x) Creating new remote instance %x.\n", |
| 152 | call->callref, remote->callref); |
| 153 | |
| 154 | /* link remote call */ |
| 155 | call->remote_ref = remote->callref; |
| 156 | remote->remote_ref = call->callref; |
| 157 | |
| 158 | /* modify mode */ |
| 159 | memset(&mncc, 0, sizeof(struct gsm_mncc)); |
| 160 | mncc.callref = call->callref; |
| 161 | mncc.lchan_mode = GSM48_CMODE_SPEECH_EFR; |
| 162 | DEBUGP(DMNCC, "(call %x) Modify channel mode.\n", call->callref); |
| 163 | mncc_send(call->net, MNCC_LCHAN_MODIFY, &mncc); |
| 164 | |
| 165 | /* send call proceeding */ |
| 166 | memset(&mncc, 0, sizeof(struct gsm_mncc)); |
| 167 | mncc.callref = call->callref; |
| 168 | DEBUGP(DMNCC, "(call %x) Accepting call.\n", call->callref); |
| 169 | mncc_send(call->net, MNCC_CALL_PROC_REQ, &mncc); |
| 170 | |
| 171 | /* send setup to remote */ |
| 172 | // setup->fields |= MNCC_F_SIGNAL; |
| 173 | // setup->signal = GSM48_SIGNAL_DIALTONE; |
| 174 | setup->callref = remote->callref; |
| 175 | DEBUGP(DMNCC, "(call %x) Forwarding SETUP to remote.\n", call->callref); |
| 176 | return mncc_send(remote->net, MNCC_SETUP_REQ, setup); |
| 177 | } |
| 178 | |
| 179 | static int mncc_alert_ind(struct gsm_call *call, int msg_type, |
| 180 | struct gsm_mncc *alert) |
| 181 | { |
| 182 | struct gsm_call *remote; |
| 183 | |
| 184 | /* send alerting to remote */ |
| 185 | if (!(remote = get_call_ref(call->remote_ref))) |
| 186 | return 0; |
| 187 | alert->callref = remote->callref; |
| 188 | DEBUGP(DMNCC, "(call %x) Forwarding ALERT to remote.\n", call->callref); |
| 189 | return mncc_send(remote->net, MNCC_ALERT_REQ, alert); |
| 190 | } |
| 191 | |
| 192 | static int mncc_notify_ind(struct gsm_call *call, int msg_type, |
| 193 | struct gsm_mncc *notify) |
| 194 | { |
| 195 | struct gsm_call *remote; |
| 196 | |
| 197 | /* send notify to remote */ |
| 198 | if (!(remote = get_call_ref(call->remote_ref))) |
| 199 | return 0; |
| 200 | notify->callref = remote->callref; |
| 201 | DEBUGP(DMNCC, "(call %x) Forwarding NOTIF to remote.\n", call->callref); |
| 202 | return mncc_send(remote->net, MNCC_NOTIFY_REQ, notify); |
| 203 | } |
| 204 | |
| 205 | static int mncc_setup_cnf(struct gsm_call *call, int msg_type, |
| 206 | struct gsm_mncc *connect) |
| 207 | { |
| 208 | struct gsm_mncc connect_ack; |
| 209 | struct gsm_call *remote; |
| 210 | u_int32_t refs[2]; |
| 211 | |
| 212 | /* acknowledge connect */ |
| 213 | memset(&connect_ack, 0, sizeof(struct gsm_mncc)); |
| 214 | connect_ack.callref = call->callref; |
| 215 | DEBUGP(DMNCC, "(call %x) Acknowledge SETUP.\n", call->callref); |
| 216 | mncc_send(call->net, MNCC_SETUP_COMPL_REQ, &connect_ack); |
| 217 | |
| 218 | /* send connect message to remote */ |
| 219 | if (!(remote = get_call_ref(call->remote_ref))) |
| 220 | return 0; |
| 221 | connect->callref = remote->callref; |
| 222 | DEBUGP(DMNCC, "(call %x) Sending CONNECT to remote.\n", call->callref); |
| 223 | mncc_send(remote->net, MNCC_SETUP_RSP, connect); |
| 224 | |
| 225 | /* bridge tch */ |
| 226 | refs[0] = call->callref; |
| 227 | refs[1] = call->remote_ref; |
| 228 | DEBUGP(DMNCC, "(call %x) Bridging with remote.\n", call->callref); |
| 229 | return mncc_send(call->net, MNCC_BRIDGE, refs); |
| 230 | } |
| 231 | |
| 232 | static int mncc_disc_ind(struct gsm_call *call, int msg_type, |
| 233 | struct gsm_mncc *disc) |
| 234 | { |
| 235 | struct gsm_call *remote; |
| 236 | |
| 237 | /* send release */ |
| 238 | DEBUGP(DMNCC, "(call %x) Releasing call with cause %d\n", |
| 239 | call->callref, disc->cause.value); |
| 240 | mncc_send(call->net, MNCC_REL_REQ, disc); |
| 241 | |
| 242 | /* send disc to remote */ |
| 243 | if (!(remote = get_call_ref(call->remote_ref))) { |
| 244 | return 0; |
| 245 | } |
| 246 | disc->callref = remote->callref; |
| 247 | DEBUGP(DMNCC, "(call %x) Disconnecting remote with cause %d\n", |
| 248 | remote->callref, disc->cause.value); |
| 249 | return mncc_send(remote->net, MNCC_DISC_REQ, disc); |
| 250 | } |
| 251 | |
| 252 | static int mncc_rel_ind(struct gsm_call *call, int msg_type, struct gsm_mncc *rel) |
| 253 | { |
| 254 | struct gsm_call *remote; |
| 255 | |
| 256 | /* send release to remote */ |
| 257 | if (!(remote = get_call_ref(call->remote_ref))) { |
| 258 | free_call(call); |
| 259 | return 0; |
| 260 | } |
| 261 | rel->callref = remote->callref; |
| 262 | DEBUGP(DMNCC, "(call %x) Releasing remote with cause %d\n", |
| 263 | call->callref, rel->cause.value); |
| 264 | mncc_send(remote->net, MNCC_REL_REQ, rel); |
| 265 | |
| 266 | free_call(call); |
| 267 | |
| 268 | return 0; |
| 269 | } |
| 270 | |
| 271 | static int mncc_rel_cnf(struct gsm_call *call, int msg_type, struct gsm_mncc *rel) |
| 272 | { |
| 273 | free_call(call); |
| 274 | return 0; |
| 275 | } |
| 276 | |
| 277 | int mncc_recv(struct gsm_network *net, int msg_type, void *arg) |
| 278 | { |
| 279 | struct gsm_mncc *data = arg; |
| 280 | int callref; |
| 281 | struct gsm_call *call = NULL, *callt; |
| 282 | int rc = 0; |
| 283 | |
| 284 | /* Special messages */ |
| 285 | switch(msg_type) { |
| 286 | } |
| 287 | |
| 288 | /* find callref */ |
| 289 | callref = data->callref; |
| 290 | llist_for_each_entry(callt, &call_list, entry) { |
| 291 | if (callt->callref == callref) { |
| 292 | call = callt; |
| 293 | break; |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | /* create callref, if setup is received */ |
| 298 | if (!call) { |
| 299 | if (msg_type != MNCC_SETUP_IND) |
| 300 | return 0; /* drop */ |
| 301 | /* create call */ |
| 302 | if (!(call = calloc(1, sizeof(struct gsm_call)))) { |
| 303 | struct gsm_mncc rel; |
| 304 | |
| 305 | memset(&rel, 0, sizeof(struct gsm_mncc)); |
| 306 | rel.callref = callref; |
| 307 | mncc_set_cause(&rel, 1, 47); |
| 308 | mncc_send(net, MNCC_REL_REQ, &rel); |
| 309 | return 0; |
| 310 | } |
| 311 | llist_add_tail(&call->entry, &call_list); |
| 312 | call->net = net; |
| 313 | call->callref = callref; |
| 314 | DEBUGP(DMNCC, "(call %x) Call created.\n", call->callref); |
| 315 | } |
| 316 | |
| 317 | DEBUGP(DMNCC, "(call %x) Received message %s\n", call->callref, |
| 318 | get_mncc_name(msg_type)); |
| 319 | |
| 320 | switch(msg_type) { |
| 321 | case MNCC_SETUP_IND: |
| 322 | rc = mncc_setup_ind(call, msg_type, arg); |
| 323 | break; |
| 324 | case MNCC_SETUP_CNF: |
| 325 | rc = mncc_setup_cnf(call, msg_type, arg); |
| 326 | break; |
| 327 | case MNCC_SETUP_COMPL_IND: |
| 328 | break; |
| 329 | case MNCC_CALL_CONF_IND: |
| 330 | /* we now need to MODIFY the channel */ |
| 331 | data->lchan_mode = GSM48_CMODE_SPEECH_EFR; |
| 332 | mncc_send(call->net, MNCC_LCHAN_MODIFY, data); |
| 333 | break; |
| 334 | case MNCC_ALERT_IND: |
| 335 | rc = mncc_alert_ind(call, msg_type, arg); |
| 336 | break; |
| 337 | case MNCC_NOTIFY_IND: |
| 338 | rc = mncc_notify_ind(call, msg_type, arg); |
| 339 | break; |
| 340 | case MNCC_DISC_IND: |
| 341 | rc = mncc_disc_ind(call, msg_type, arg); |
| 342 | break; |
| 343 | case MNCC_REL_IND: |
| 344 | case MNCC_REJ_IND: |
| 345 | rc = mncc_rel_ind(call, msg_type, arg); |
| 346 | break; |
| 347 | case MNCC_REL_CNF: |
| 348 | rc = mncc_rel_cnf(call, msg_type, arg); |
| 349 | break; |
| 350 | case MNCC_FACILITY_IND: |
| 351 | break; |
| 352 | case MNCC_START_DTMF_IND: |
| 353 | break; |
| 354 | case MNCC_STOP_DTMF_IND: |
| 355 | break; |
| 356 | case MNCC_MODIFY_IND: |
| 357 | mncc_set_cause(data, 1, 79); |
| 358 | DEBUGP(DMNCC, "(call %x) Rejecting MODIFY with cause %d\n", |
| 359 | call->callref, data->cause.value); |
| 360 | rc = mncc_send(net, MNCC_MODIFY_REJ, data); |
| 361 | break; |
| 362 | case MNCC_MODIFY_CNF: |
| 363 | break; |
| 364 | case MNCC_HOLD_IND: |
| 365 | mncc_set_cause(data, 1, 79); |
| 366 | DEBUGP(DMNCC, "(call %x) Rejecting HOLD with cause %d\n", |
| 367 | call->callref, data->cause.value); |
| 368 | rc = mncc_send(net, MNCC_HOLD_REJ, data); |
| 369 | break; |
| 370 | case MNCC_RETRIEVE_IND: |
| 371 | mncc_set_cause(data, 1, 79); |
| 372 | DEBUGP(DMNCC, "(call %x) Rejecting RETRIEVE with cause %d\n", |
| 373 | call->callref, data->cause.value); |
| 374 | rc = mncc_send(net, MNCC_RETRIEVE_REJ, data); |
| 375 | break; |
| 376 | default: |
| 377 | DEBUGP(DMNCC, "(call %x) Message unhandled\n", callref); |
| 378 | break; |
| 379 | } |
| 380 | |
| 381 | return rc; |
| 382 | } |