Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 1 | /* |
| 2 | * SCCP management code |
| 3 | * |
Holger Hans Peter Freyther | a1b5980 | 2010-01-27 12:12:46 +0100 | [diff] [blame] | 4 | * (C) 2009, 2010 by Holger Hans Peter Freyther <zecke@selfish.org> |
| 5 | * (C) 2009, 2010 by on-waves.com |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 6 | * |
| 7 | * All Rights Reserved |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License along |
| 20 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 21 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 22 | * |
| 23 | */ |
| 24 | |
| 25 | #include <string.h> |
| 26 | |
| 27 | #include <sccp/sccp.h> |
| 28 | |
| 29 | #include <openbsc/debug.h> |
| 30 | #include <openbsc/talloc.h> |
| 31 | #include <openbsc/linuxlist.h> |
| 32 | |
| 33 | static void *tall_sccp_ctx; |
| 34 | static LLIST_HEAD(sccp_connections); |
| 35 | |
| 36 | #define SCCP_MSG_SIZE 4096 |
| 37 | #define SCCP_MSG_HEADROOM 128 |
| 38 | |
| 39 | /* global data */ |
| 40 | const struct sockaddr_sccp sccp_ssn_bssap = { |
| 41 | .sccp_family = 0, |
| 42 | .sccp_ssn = SCCP_SSN_BSSAP, |
| 43 | }; |
| 44 | |
| 45 | struct sccp_system { |
| 46 | /* layer3 -> layer2 */ |
| 47 | int (*write_data)(struct msgb *data, void *context); |
| 48 | void *write_context; |
| 49 | }; |
| 50 | |
| 51 | |
| 52 | static struct sccp_system sccp_system = { |
| 53 | .write_data = NULL, |
| 54 | }; |
| 55 | |
| 56 | struct sccp_data_callback { |
| 57 | /* connection based */ |
| 58 | int (*accept_cb)(struct sccp_connection *, void *); |
| 59 | void *accept_context; |
| 60 | |
| 61 | /* connection less */ |
| 62 | int (*read_cb)(struct msgb *, unsigned int, void *); |
| 63 | void *read_context; |
| 64 | |
| 65 | u_int8_t ssn; |
| 66 | struct llist_head callback; |
| 67 | }; |
| 68 | |
| 69 | static LLIST_HEAD(sccp_callbacks); |
| 70 | |
| 71 | static struct sccp_data_callback *_find_ssn(u_int8_t ssn) |
| 72 | { |
| 73 | struct sccp_data_callback *cb; |
| 74 | |
| 75 | llist_for_each_entry(cb, &sccp_callbacks, callback) { |
| 76 | if (cb->ssn == ssn) |
| 77 | return cb; |
| 78 | } |
| 79 | |
| 80 | /* need to add one */ |
| 81 | cb = talloc_zero(tall_sccp_ctx, struct sccp_data_callback); |
| 82 | if (!cb) { |
| 83 | DEBUGP(DSCCP, "Failed to allocate sccp callback.\n"); |
| 84 | return NULL; |
| 85 | } |
| 86 | |
| 87 | cb->ssn = ssn; |
| 88 | llist_add_tail(&cb->callback, &sccp_callbacks); |
| 89 | return cb; |
| 90 | } |
| 91 | |
| 92 | |
| 93 | static int _send_msg(struct msgb *msg) |
| 94 | { |
| 95 | return sccp_system.write_data(msg, sccp_system.write_context); |
| 96 | } |
| 97 | |
| 98 | /* |
| 99 | * parsing routines |
| 100 | */ |
| 101 | static int copy_address(struct sccp_address *addr, u_int8_t offset, struct msgb *msgb) |
| 102 | { |
| 103 | struct sccp_called_party_address *party; |
| 104 | |
| 105 | int room = msgb_l2len(msgb) - offset; |
| 106 | u_int8_t read = 0; |
| 107 | u_int8_t length; |
| 108 | |
| 109 | if (room <= 0) { |
| 110 | DEBUGP(DSCCP, "Not enough room for an address: %u\n", room); |
| 111 | return -1; |
| 112 | } |
| 113 | |
| 114 | length = msgb->l2h[offset]; |
| 115 | if (room <= length) { |
| 116 | DEBUGP(DSCCP, "Not enough room for optional data %u %u\n", room, length); |
| 117 | return -1; |
| 118 | } |
| 119 | |
| 120 | |
| 121 | party = (struct sccp_called_party_address *)(msgb->l2h + offset + 1); |
| 122 | if (party->point_code_indicator) { |
| 123 | if (length <= read + 2) { |
| 124 | DEBUGP(DSCCP, "POI does not fit %u\n", length); |
| 125 | return -1; |
| 126 | } |
| 127 | |
| 128 | |
| 129 | memcpy(&addr->poi, &party->data[read], 2); |
| 130 | read += 2; |
| 131 | } |
| 132 | |
| 133 | if (party->ssn_indicator) { |
| 134 | if (length <= read + 1) { |
| 135 | DEBUGP(DSCCP, "SSN does not fit %u\n", length); |
| 136 | return -1; |
| 137 | } |
| 138 | |
| 139 | addr->ssn = party->data[read]; |
| 140 | read += 1; |
| 141 | } |
| 142 | |
| 143 | if (party->global_title_indicator) { |
| 144 | DEBUGP(DSCCP, "GTI not supported %u\n", *(u_int8_t *)party); |
| 145 | return -1; |
| 146 | } |
| 147 | |
| 148 | addr->address = *party; |
| 149 | return 0; |
| 150 | } |
| 151 | |
| 152 | static int check_address(struct sccp_address *addr) |
| 153 | { |
| 154 | /* ignore point_code_indicator... it should be zero... but */ |
| 155 | if (addr->address.ssn_indicator != 1 |
| 156 | || addr->address.global_title_indicator == 1 |
| 157 | || addr->address.routing_indicator != 1) { |
| 158 | DEBUGP(DSCCP, "Invalid called address according to 08.06: 0x%x 0x%x\n", |
| 159 | *(u_int8_t *)&addr->address, addr->ssn); |
| 160 | return -1; |
| 161 | } |
| 162 | |
| 163 | return 0; |
| 164 | } |
| 165 | |
| 166 | static int _sccp_parse_optional_data(const int offset, |
| 167 | struct msgb *msgb, struct sccp_optional_data *data) |
| 168 | { |
| 169 | u_int16_t room = msgb_l2len(msgb) - offset; |
| 170 | u_int16_t read = 0; |
| 171 | |
| 172 | while (room > read) { |
| 173 | u_int8_t type = msgb->l2h[offset + read]; |
| 174 | if (type == SCCP_PNC_END_OF_OPTIONAL) |
| 175 | return 0; |
| 176 | |
| 177 | if (read + 1 >= room) { |
| 178 | DEBUGP(DSCCP, "no place for length\n"); |
| 179 | return 0; |
| 180 | } |
| 181 | |
| 182 | u_int8_t length = msgb->l2h[offset + read + 1]; |
| 183 | read += 2 + length; |
| 184 | |
| 185 | |
| 186 | if (room <= read) { |
| 187 | DEBUGP(DSCCP, "no space for the data: type: %d read: %d room: %d l2: %d\n", |
| 188 | type, read, room, msgb_l2len(msgb)); |
| 189 | return 0; |
| 190 | } |
| 191 | |
| 192 | if (type == SCCP_PNC_DATA) { |
| 193 | data->data_len = length; |
| 194 | data->data_start = offset + read - length; |
| 195 | } |
| 196 | |
| 197 | } |
| 198 | |
| 199 | return -1; |
| 200 | } |
| 201 | |
Holger Hans Peter Freyther | 867d836 | 2010-01-27 12:25:13 +0100 | [diff] [blame] | 202 | int _sccp_parse_connection_request(struct msgb *msgb, struct sccp_parse_result *result) |
Holger Hans Peter Freyther | a1b5980 | 2010-01-27 12:12:46 +0100 | [diff] [blame] | 203 | { |
Holger Hans Peter Freyther | 867d836 | 2010-01-27 12:25:13 +0100 | [diff] [blame] | 204 | static const u_int32_t header_size = |
| 205 | sizeof(struct sccp_connection_request); |
| 206 | static const u_int32_t optional_offset = |
| 207 | offsetof(struct sccp_connection_request, optional_start); |
| 208 | static const u_int32_t called_offset = |
| 209 | offsetof(struct sccp_connection_request, variable_called); |
| 210 | |
| 211 | struct sccp_connection_request *req = (struct sccp_connection_request *)msgb->data; |
| 212 | struct sccp_optional_data optional_data; |
| 213 | |
| 214 | /* header check */ |
| 215 | if (msgb_l2len(msgb) < header_size) { |
| 216 | DEBUGP(DSCCP, "msgb < header_size %u %u\n", |
| 217 | msgb_l2len(msgb), header_size); |
| 218 | return -1; |
| 219 | } |
| 220 | |
| 221 | /* copy out the calling and called address. Add the offset */ |
| 222 | if (copy_address(&result->called, called_offset + req->variable_called, msgb) != 0) |
| 223 | return -1; |
| 224 | |
| 225 | if (check_address(&result->called) != 0) { |
| 226 | DEBUGP(DSCCP, "Invalid called address according to 08.06: 0x%x 0x%x\n", |
| 227 | *(u_int8_t *)&result->called.address, result->called.ssn); |
| 228 | return -1; |
| 229 | } |
| 230 | |
| 231 | result->source_local_reference = &req->source_local_reference; |
| 232 | |
| 233 | /* |
| 234 | * parse optional data. |
| 235 | */ |
| 236 | memset(&optional_data, 0, sizeof(optional_data)); |
| 237 | if (_sccp_parse_optional_data(optional_offset + req->optional_start, msgb, &optional_data) != 0) { |
| 238 | DEBUGP(DSCCP, "parsing of optional data failed.\n"); |
| 239 | return -1; |
| 240 | } |
| 241 | |
| 242 | if (optional_data.data_len != 0) { |
| 243 | msgb->l3h = &msgb->l2h[optional_data.data_start]; |
| 244 | result->data_len = optional_data.data_len; |
| 245 | } else { |
| 246 | result->data_len = 0; |
| 247 | } |
| 248 | |
| 249 | return 0; |
Holger Hans Peter Freyther | a1b5980 | 2010-01-27 12:12:46 +0100 | [diff] [blame] | 250 | } |
| 251 | |
Holger Hans Peter Freyther | 5defc56 | 2010-01-29 03:49:32 +0100 | [diff] [blame] | 252 | int _sccp_parse_connection_released(struct msgb *msgb, struct sccp_parse_result *result) |
Holger Hans Peter Freyther | a1b5980 | 2010-01-27 12:12:46 +0100 | [diff] [blame] | 253 | { |
Holger Hans Peter Freyther | 5defc56 | 2010-01-29 03:49:32 +0100 | [diff] [blame] | 254 | static int header_size = sizeof(struct sccp_connection_released); |
| 255 | static int optional_offset = offsetof(struct sccp_connection_released, optional_start); |
| 256 | |
| 257 | struct sccp_optional_data optional_data; |
| 258 | struct sccp_connection_released *rls = (struct sccp_connection_released *) msgb->l2h; |
| 259 | |
| 260 | /* we don't have enough size for the struct */ |
| 261 | if (msgb_l2len(msgb) < header_size) { |
| 262 | DEBUGP(DSCCP, "msgb > header_size %u %u\n", |
| 263 | msgb_l2len(msgb), header_size); |
| 264 | return -1; |
| 265 | } |
| 266 | |
| 267 | memset(&optional_data, 0, sizeof(optional_data)); |
| 268 | if (_sccp_parse_optional_data(optional_offset + rls->optional_start, msgb, &optional_data) != 0) { |
| 269 | DEBUGP(DSCCP, "parsing of optional data failed.\n"); |
| 270 | return -1; |
| 271 | } |
| 272 | |
| 273 | result->source_local_reference = &rls->source_local_reference; |
| 274 | result->destination_local_reference = &rls->destination_local_reference; |
| 275 | |
| 276 | if (optional_data.data_len != 0) { |
| 277 | msgb->l3h = &msgb->l2h[optional_data.data_start]; |
| 278 | result->data_len = optional_data.data_len; |
| 279 | } else { |
| 280 | result->data_len = 0; |
| 281 | } |
| 282 | |
| 283 | return 0; |
Holger Hans Peter Freyther | a1b5980 | 2010-01-27 12:12:46 +0100 | [diff] [blame] | 284 | } |
| 285 | |
Holger Hans Peter Freyther | 6233c58 | 2010-01-29 03:58:12 +0100 | [diff] [blame] | 286 | int _sccp_parse_connection_refused(struct msgb *msgb, struct sccp_parse_result *result) |
Holger Hans Peter Freyther | a1b5980 | 2010-01-27 12:12:46 +0100 | [diff] [blame] | 287 | { |
Holger Hans Peter Freyther | 6233c58 | 2010-01-29 03:58:12 +0100 | [diff] [blame] | 288 | static const u_int32_t header_size = |
| 289 | sizeof(struct sccp_connection_refused); |
| 290 | static int optional_offset = offsetof(struct sccp_connection_refused, optional_start); |
| 291 | |
| 292 | struct sccp_optional_data optional_data; |
| 293 | struct sccp_connection_refused *ref; |
| 294 | |
| 295 | /* header check */ |
| 296 | if (msgb_l2len(msgb) < header_size) { |
| 297 | DEBUGP(DSCCP, "msgb < header_size %u %u\n", |
| 298 | msgb_l2len(msgb), header_size); |
| 299 | return -1; |
| 300 | } |
| 301 | |
| 302 | ref = (struct sccp_connection_refused *) msgb->l2h; |
| 303 | |
| 304 | result->destination_local_reference = &ref->destination_local_reference; |
| 305 | |
| 306 | memset(&optional_data, 0, sizeof(optional_data)); |
| 307 | if (_sccp_parse_optional_data(optional_offset + ref->optional_start, msgb, &optional_data) != 0) { |
| 308 | DEBUGP(DSCCP, "parsing of optional data failed.\n"); |
| 309 | return -1; |
| 310 | } |
| 311 | |
| 312 | /* optional data */ |
| 313 | if (optional_data.data_len != 0) { |
| 314 | msgb->l3h = &msgb->l2h[optional_data.data_start]; |
| 315 | result->data_len = optional_data.data_len; |
| 316 | } else { |
| 317 | result->data_len = 0; |
| 318 | } |
| 319 | |
| 320 | return 0; |
Holger Hans Peter Freyther | a1b5980 | 2010-01-27 12:12:46 +0100 | [diff] [blame] | 321 | } |
| 322 | |
Holger Hans Peter Freyther | 1f21f34 | 2010-01-29 04:03:00 +0100 | [diff] [blame] | 323 | int _sccp_parse_connection_confirm(struct msgb *msgb, struct sccp_parse_result *result) |
Holger Hans Peter Freyther | a1b5980 | 2010-01-27 12:12:46 +0100 | [diff] [blame] | 324 | { |
Holger Hans Peter Freyther | 1f21f34 | 2010-01-29 04:03:00 +0100 | [diff] [blame] | 325 | static u_int32_t header_size = |
| 326 | sizeof(struct sccp_connection_confirm); |
| 327 | static const u_int32_t optional_offset = |
| 328 | offsetof(struct sccp_connection_confirm, optional_start); |
| 329 | |
| 330 | struct sccp_optional_data optional_data; |
| 331 | struct sccp_connection_confirm *con; |
| 332 | |
| 333 | /* header check */ |
| 334 | if (msgb_l2len(msgb) < header_size) { |
| 335 | DEBUGP(DSCCP, "msgb < header_size %u %u\n", |
| 336 | msgb_l2len(msgb), header_size); |
| 337 | return -1; |
| 338 | } |
| 339 | |
| 340 | con = (struct sccp_connection_confirm *) msgb->l2h; |
| 341 | result->destination_local_reference = &con->destination_local_reference; |
| 342 | result->source_local_reference = &con->source_local_reference; |
| 343 | |
| 344 | memset(&optional_data, 0, sizeof(optional_data)); |
| 345 | if (_sccp_parse_optional_data(optional_offset + con->optional_start, msgb, &optional_data) != 0) { |
| 346 | DEBUGP(DSCCP, "parsing of optional data failed.\n"); |
| 347 | return -1; |
| 348 | } |
| 349 | |
| 350 | if (optional_data.data_len != 0) { |
| 351 | msgb->l3h = &msgb->l2h[optional_data.data_start]; |
| 352 | result->data_len = optional_data.data_len; |
| 353 | } else { |
| 354 | result->data_len = 0; |
| 355 | } |
| 356 | |
| 357 | return 0; |
Holger Hans Peter Freyther | a1b5980 | 2010-01-27 12:12:46 +0100 | [diff] [blame] | 358 | } |
| 359 | |
Holger Hans Peter Freyther | db56e38 | 2010-01-29 04:19:56 +0100 | [diff] [blame] | 360 | int _sccp_parse_connection_release_complete(struct msgb *msgb, struct sccp_parse_result *result) |
Holger Hans Peter Freyther | a1b5980 | 2010-01-27 12:12:46 +0100 | [diff] [blame] | 361 | { |
Holger Hans Peter Freyther | db56e38 | 2010-01-29 04:19:56 +0100 | [diff] [blame] | 362 | static int header_size = sizeof(struct sccp_connection_release_complete); |
| 363 | |
| 364 | struct sccp_connection_release_complete *cmpl; |
| 365 | |
| 366 | /* header check */ |
| 367 | if (msgb_l2len(msgb) < header_size) { |
| 368 | DEBUGP(DSCCP, "msgb < header_size %u %u\n", |
| 369 | msgb_l2len(msgb), header_size); |
| 370 | return -1; |
| 371 | } |
| 372 | |
| 373 | cmpl = (struct sccp_connection_release_complete *) msgb->l2h; |
| 374 | result->source_local_reference = &cmpl->source_local_reference; |
| 375 | result->destination_local_reference = &cmpl->destination_local_reference; |
| 376 | |
| 377 | return 0; |
Holger Hans Peter Freyther | a1b5980 | 2010-01-27 12:12:46 +0100 | [diff] [blame] | 378 | } |
| 379 | |
Holger Hans Peter Freyther | 8edb961 | 2010-01-29 04:31:00 +0100 | [diff] [blame^] | 380 | int _sccp_parse_connection_dt1(struct msgb *msgb, struct sccp_parse_result *result) |
Holger Hans Peter Freyther | a1b5980 | 2010-01-27 12:12:46 +0100 | [diff] [blame] | 381 | { |
Holger Hans Peter Freyther | 8edb961 | 2010-01-29 04:31:00 +0100 | [diff] [blame^] | 382 | static int header_size = sizeof(struct sccp_data_form1); |
| 383 | static int variable_offset = offsetof(struct sccp_data_form1, variable_start); |
| 384 | |
| 385 | struct sccp_data_form1 *dt1 = (struct sccp_data_form1 *)msgb->l2h; |
| 386 | |
| 387 | /* we don't have enough size for the struct */ |
| 388 | if (msgb_l2len(msgb) < header_size) { |
| 389 | DEBUGP(DSCCP, "msgb > header_size %u %u\n", |
| 390 | msgb_l2len(msgb), header_size); |
| 391 | return -1; |
| 392 | } |
| 393 | |
| 394 | if (dt1->segmenting != 0) { |
| 395 | DEBUGP(DSCCP, "This packet has segmenting, not supported: %d\n", dt1->segmenting); |
| 396 | return -1; |
| 397 | } |
| 398 | |
| 399 | result->destination_local_reference = &dt1->destination_local_reference; |
| 400 | |
| 401 | /* some more size checks in here */ |
| 402 | if (msgb_l2len(msgb) < variable_offset + dt1->variable_start + 1) { |
| 403 | DEBUGP(DSCCP, "Not enough space for variable start: %u %u\n", |
| 404 | msgb_l2len(msgb), dt1->variable_start); |
| 405 | return -1; |
| 406 | } |
| 407 | |
| 408 | result->data_len = msgb->l2h[variable_offset + dt1->variable_start]; |
| 409 | msgb->l3h = &msgb->l2h[dt1->variable_start + variable_offset + 1]; |
| 410 | |
| 411 | if (msgb_l3len(msgb) < result->data_len) { |
| 412 | DEBUGP(DSCCP, "Not enough room for the payload: %u %u\n", |
| 413 | msgb_l3len(msgb), result->data_len); |
| 414 | return -1; |
| 415 | } |
| 416 | |
| 417 | return 0; |
Holger Hans Peter Freyther | a1b5980 | 2010-01-27 12:12:46 +0100 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | int _sccp_parse_udt(struct msgb *msgb, struct sccp_parse_result *result) |
| 421 | { |
| 422 | static const u_int32_t header_size = sizeof(struct sccp_data_unitdata); |
| 423 | static const u_int32_t called_offset = offsetof(struct sccp_data_unitdata, variable_called); |
| 424 | static const u_int32_t calling_offset = offsetof(struct sccp_data_unitdata, variable_calling); |
| 425 | static const u_int32_t data_offset = offsetof(struct sccp_data_unitdata, variable_data); |
| 426 | |
| 427 | struct sccp_data_unitdata *udt = (struct sccp_data_unitdata *)msgb->l2h; |
| 428 | |
| 429 | if (msgb_l2len(msgb) < header_size) { |
| 430 | DEBUGP(DSCCP, "msgb < header_size %u %u\n", |
| 431 | msgb_l2len(msgb), header_size); |
| 432 | return -1; |
| 433 | } |
| 434 | |
| 435 | /* copy out the calling and called address. Add the off */ |
| 436 | if (copy_address(&result->called, called_offset + udt->variable_called, msgb) != 0) |
| 437 | return -1; |
| 438 | |
| 439 | if (check_address(&result->called) != 0) { |
| 440 | DEBUGP(DSCCP, "Invalid called address according to 08.06: 0x%x 0x%x\n", |
| 441 | *(u_int8_t *)&result->called.address, result->called.ssn); |
| 442 | return -1; |
| 443 | } |
| 444 | |
| 445 | if (copy_address(&result->calling, calling_offset + udt->variable_calling, msgb) != 0) |
| 446 | return -1; |
| 447 | |
| 448 | if (check_address(&result->calling) != 0) { |
| 449 | DEBUGP(DSCCP, "Invalid called address according to 08.06: 0x%x 0x%x\n", |
| 450 | *(u_int8_t *)&result->called.address, result->called.ssn); |
| 451 | } |
| 452 | |
| 453 | /* we don't have enough size for the data */ |
| 454 | if (msgb_l2len(msgb) < data_offset + udt->variable_data + 1) { |
| 455 | DEBUGP(DSCCP, "msgb < header + offset %u %u %u\n", |
| 456 | msgb_l2len(msgb), header_size, udt->variable_data); |
| 457 | return -1; |
| 458 | } |
| 459 | |
| 460 | |
| 461 | msgb->l3h = &udt->data[udt->variable_data]; |
| 462 | |
| 463 | if (msgb_l3len(msgb) != msgb->l3h[-1]) { |
| 464 | DEBUGP(DSCCP, "msgb is truncated %u %u\n", |
| 465 | msgb_l3len(msgb), msgb->l3h[-1]); |
| 466 | return -1; |
| 467 | } |
| 468 | |
| 469 | return 0; |
| 470 | } |
| 471 | |
| 472 | |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 473 | /* |
| 474 | * Send UDT. Currently we have a fixed address... |
| 475 | */ |
| 476 | static int _sccp_send_data(int class, const struct sockaddr_sccp *in, |
| 477 | const struct sockaddr_sccp *out, struct msgb *payload) |
| 478 | { |
| 479 | struct sccp_data_unitdata *udt; |
| 480 | u_int8_t *data; |
| 481 | int ret; |
| 482 | |
| 483 | if (msgb_l3len(payload) > 256) { |
| 484 | DEBUGP(DSCCP, "The payload is too big for one udt\n"); |
| 485 | return -1; |
| 486 | } |
| 487 | |
| 488 | struct msgb *msg = msgb_alloc_headroom(SCCP_MSG_SIZE, |
| 489 | SCCP_MSG_HEADROOM, "sccp: udt"); |
| 490 | msg->l2h = &msg->data[0]; |
| 491 | udt = (struct sccp_data_unitdata *)msgb_put(msg, sizeof(*udt)); |
| 492 | |
| 493 | udt->type = SCCP_MSG_TYPE_UDT; |
| 494 | udt->proto_class = class; |
| 495 | udt->variable_called = 3; |
| 496 | udt->variable_calling = 5; |
| 497 | udt->variable_data = 7; |
| 498 | |
| 499 | /* for variable data we start with a size and the data */ |
| 500 | data = msgb_put(msg, 1 + 2); |
| 501 | data[0] = 2; |
| 502 | data[1] = 0x42; |
| 503 | data[2] = out->sccp_ssn; |
| 504 | |
| 505 | data = msgb_put(msg, 1 + 2); |
| 506 | data[0] = 2; |
| 507 | data[1] = 0x42; |
| 508 | data[2] = in->sccp_ssn; |
| 509 | |
| 510 | /* copy the payload */ |
| 511 | data = msgb_put(msg, 1 + msgb_l3len(payload)); |
| 512 | data[0] = msgb_l3len(payload); |
| 513 | memcpy(&data[1], payload->l3h, msgb_l3len(payload)); |
| 514 | |
| 515 | ret = _send_msg(msg); |
| 516 | msgb_free(msg); |
| 517 | |
| 518 | return ret; |
| 519 | } |
| 520 | |
| 521 | static int _sccp_handle_read(struct msgb *msgb) |
| 522 | { |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 523 | struct sccp_data_callback *cb; |
Holger Hans Peter Freyther | a1b5980 | 2010-01-27 12:12:46 +0100 | [diff] [blame] | 524 | struct sccp_parse_result result; |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 525 | |
Holger Hans Peter Freyther | a1b5980 | 2010-01-27 12:12:46 +0100 | [diff] [blame] | 526 | if (_sccp_parse_udt(msgb, &result) != 0) |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 527 | return -1; |
| 528 | |
Holger Hans Peter Freyther | a1b5980 | 2010-01-27 12:12:46 +0100 | [diff] [blame] | 529 | cb = _find_ssn(result.called.ssn); |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 530 | if (!cb || !cb->read_cb) { |
Holger Hans Peter Freyther | a1b5980 | 2010-01-27 12:12:46 +0100 | [diff] [blame] | 531 | DEBUGP(DSCCP, "No routing for UDT for called SSN: %u\n", result.called.ssn); |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 532 | return -1; |
| 533 | } |
| 534 | |
| 535 | /* sanity check */ |
| 536 | return cb->read_cb(msgb, msgb_l3len(msgb), cb->read_context); |
| 537 | } |
| 538 | |
| 539 | /* |
| 540 | * handle connection orientated methods |
| 541 | */ |
| 542 | static int source_local_reference_is_free(struct sccp_source_reference *reference) |
| 543 | { |
| 544 | struct sccp_connection *connection; |
| 545 | |
| 546 | llist_for_each_entry(connection, &sccp_connections, list) { |
| 547 | if (memcmp(reference, &connection->source_local_reference, sizeof(*reference)) == 0) |
| 548 | return -1; |
| 549 | } |
| 550 | |
| 551 | return 0; |
| 552 | } |
| 553 | |
| 554 | static int destination_local_reference_is_free(struct sccp_source_reference *reference) |
| 555 | { |
| 556 | struct sccp_connection *connection; |
| 557 | |
| 558 | llist_for_each_entry(connection, &sccp_connections, list) { |
| 559 | if (memcmp(reference, &connection->destination_local_reference, sizeof(*reference)) == 0) |
| 560 | return -1; |
| 561 | } |
| 562 | |
| 563 | return 0; |
| 564 | } |
| 565 | |
| 566 | static int assign_source_local_reference(struct sccp_connection *connection) |
| 567 | { |
| 568 | static u_int32_t last_ref = 0x30000; |
| 569 | int wrapped = 0; |
| 570 | |
| 571 | do { |
| 572 | struct sccp_source_reference reference; |
| 573 | reference.octet1 = (last_ref >> 0) & 0xff; |
| 574 | reference.octet2 = (last_ref >> 8) & 0xff; |
| 575 | reference.octet3 = (last_ref >> 16) & 0xff; |
| 576 | |
| 577 | ++last_ref; |
| 578 | /* do not use the reversed word and wrap around */ |
| 579 | if ((last_ref & 0x00FFFFFF) == 0x00FFFFFF) { |
| 580 | DEBUGP(DSCCP, "Wrapped searching for a free code\n"); |
| 581 | last_ref = 0; |
| 582 | ++wrapped; |
| 583 | } |
| 584 | |
| 585 | if (source_local_reference_is_free(&reference) == 0) { |
| 586 | connection->source_local_reference = reference; |
| 587 | return 0; |
| 588 | } |
| 589 | } while (wrapped != 2); |
| 590 | |
| 591 | DEBUGP(DSCCP, "Finding a free reference failed\n"); |
| 592 | return -1; |
| 593 | } |
| 594 | |
| 595 | static void _sccp_set_connection_state(struct sccp_connection *connection, int new_state) |
| 596 | { |
| 597 | int old_state = connection->connection_state; |
| 598 | |
| 599 | connection->connection_state = new_state; |
| 600 | if (connection->state_cb) |
| 601 | connection->state_cb(connection, old_state); |
| 602 | } |
| 603 | |
Holger Hans Peter Freyther | 867d836 | 2010-01-27 12:25:13 +0100 | [diff] [blame] | 604 | static int _sccp_send_refuse(struct sccp_source_reference *src_ref, int cause) |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 605 | { |
| 606 | struct msgb *msgb; |
| 607 | struct sccp_connection_refused *ref; |
| 608 | u_int8_t *data; |
| 609 | int ret; |
| 610 | |
| 611 | msgb = msgb_alloc_headroom(SCCP_MSG_SIZE, |
| 612 | SCCP_MSG_HEADROOM, "sccp ref"); |
| 613 | msgb->l2h = &msgb->data[0]; |
| 614 | |
| 615 | ref = (struct sccp_connection_refused *) msgb_put(msgb, sizeof(*ref)); |
| 616 | ref->type = SCCP_MSG_TYPE_CREF; |
Holger Hans Peter Freyther | 867d836 | 2010-01-27 12:25:13 +0100 | [diff] [blame] | 617 | memcpy(&ref->destination_local_reference, src_ref, |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 618 | sizeof(struct sccp_source_reference)); |
| 619 | ref->cause = cause; |
| 620 | ref->optional_start = 1; |
| 621 | |
| 622 | data = msgb_put(msgb, 1); |
| 623 | data[0] = SCCP_PNC_END_OF_OPTIONAL; |
| 624 | |
| 625 | ret = _send_msg(msgb); |
| 626 | msgb_free(msgb); |
| 627 | return ret; |
| 628 | } |
| 629 | |
| 630 | static int _sccp_send_connection_confirm(struct sccp_connection *connection) |
| 631 | { |
| 632 | struct msgb *response; |
| 633 | struct sccp_connection_confirm *confirm; |
| 634 | u_int8_t *optional_data; |
| 635 | int ret; |
| 636 | |
| 637 | if (assign_source_local_reference(connection) != 0) |
| 638 | return -1; |
| 639 | |
| 640 | response = msgb_alloc_headroom(SCCP_MSG_SIZE, |
| 641 | SCCP_MSG_HEADROOM, "sccp confirm"); |
| 642 | response->l2h = &response->data[0]; |
| 643 | |
| 644 | confirm = (struct sccp_connection_confirm *) msgb_put(response, sizeof(*confirm)); |
| 645 | |
| 646 | confirm->type = SCCP_MSG_TYPE_CC; |
| 647 | memcpy(&confirm->destination_local_reference, |
| 648 | &connection->destination_local_reference, |
| 649 | sizeof(connection->destination_local_reference)); |
| 650 | memcpy(&confirm->source_local_reference, |
| 651 | &connection->source_local_reference, |
| 652 | sizeof(connection->source_local_reference)); |
| 653 | confirm->proto_class = 2; |
| 654 | confirm->optional_start = 1; |
| 655 | |
| 656 | optional_data = (u_int8_t *) msgb_put(response, 1); |
| 657 | optional_data[0] = SCCP_PNC_END_OF_OPTIONAL; |
| 658 | |
| 659 | ret = _send_msg(response); |
| 660 | msgb_free(response); |
| 661 | |
| 662 | _sccp_set_connection_state(connection, SCCP_CONNECTION_STATE_ESTABLISHED); |
| 663 | return ret; |
| 664 | } |
| 665 | |
| 666 | static int _sccp_send_connection_request(struct sccp_connection *connection, |
| 667 | const struct sockaddr_sccp *called, struct msgb *msg) |
| 668 | { |
| 669 | struct msgb *request; |
| 670 | struct sccp_connection_request *req; |
| 671 | u_int8_t *data; |
| 672 | u_int8_t extra_size = 3 + 1; |
| 673 | int ret; |
| 674 | |
| 675 | |
| 676 | if (msg && (msgb_l3len(msg) < 3 || msgb_l3len(msg) > 130)) { |
| 677 | DEBUGP(DSCCP, "Invalid amount of data... %d\n", msgb_l3len(msg)); |
| 678 | return -1; |
| 679 | } |
| 680 | |
| 681 | /* try to find a id */ |
| 682 | if (assign_source_local_reference(connection) != 0) { |
| 683 | DEBUGP(DSCCP, "Assigning a local reference failed.\n"); |
| 684 | _sccp_set_connection_state(connection, SCCP_CONNECTION_STATE_SETUP_ERROR); |
| 685 | return -1; |
| 686 | } |
| 687 | |
| 688 | |
| 689 | if (msg) |
| 690 | extra_size += 2 + msgb_l3len(msg); |
| 691 | request = msgb_alloc_headroom(SCCP_MSG_SIZE, |
| 692 | SCCP_MSG_HEADROOM, "sccp connection request"); |
| 693 | request->l2h = &request->data[0]; |
| 694 | req = (struct sccp_connection_request *) msgb_put(request, sizeof(*req)); |
| 695 | |
| 696 | req->type = SCCP_MSG_TYPE_CR; |
| 697 | memcpy(&req->source_local_reference, &connection->source_local_reference, |
| 698 | sizeof(connection->source_local_reference)); |
| 699 | req->proto_class = 2; |
| 700 | req->variable_called = 2; |
| 701 | req->optional_start = 4; |
| 702 | |
| 703 | /* write the called party address */ |
| 704 | data = msgb_put(request, 1 + 2); |
| 705 | data[0] = 2; |
| 706 | data[1] = 0x42; |
| 707 | data[2] = called->sccp_ssn; |
| 708 | |
| 709 | /* write the payload */ |
| 710 | if (msg) { |
| 711 | data = msgb_put(request, 2 + msgb_l3len(msg)); |
| 712 | data[0] = SCCP_PNC_DATA; |
| 713 | data[1] = msgb_l3len(msg); |
| 714 | memcpy(&data[2], msg->l3h, msgb_l3len(msg)); |
| 715 | } |
| 716 | |
| 717 | data = msgb_put(request, 1); |
| 718 | data[0] = SCCP_PNC_END_OF_OPTIONAL; |
| 719 | |
| 720 | llist_add_tail(&connection->list, &sccp_connections); |
| 721 | _sccp_set_connection_state(connection, SCCP_CONNECTION_STATE_REQUEST); |
| 722 | |
| 723 | ret = _send_msg(request); |
| 724 | msgb_free(request); |
| 725 | |
| 726 | return ret; |
| 727 | } |
| 728 | |
| 729 | static int _sccp_send_connection_data(struct sccp_connection *conn, struct msgb *_data) |
| 730 | { |
| 731 | struct msgb *msgb; |
| 732 | struct sccp_data_form1 *dt1; |
| 733 | u_int8_t *data; |
| 734 | int extra_size; |
| 735 | int ret; |
| 736 | |
| 737 | if (msgb_l3len(_data) < 2 || msgb_l3len(_data) > 256) { |
| 738 | DEBUGP(DSCCP, "data size too big, segmenting unimplemented.\n"); |
| 739 | return -1; |
| 740 | } |
| 741 | |
| 742 | extra_size = 1 + msgb_l3len(_data); |
| 743 | msgb = msgb_alloc_headroom(SCCP_MSG_SIZE, |
| 744 | SCCP_MSG_HEADROOM, "sccp dt1"); |
| 745 | msgb->l2h = &msgb->data[0]; |
| 746 | |
| 747 | dt1 = (struct sccp_data_form1 *) msgb_put(msgb, sizeof(*dt1)); |
| 748 | dt1->type = SCCP_MSG_TYPE_DT1; |
| 749 | memcpy(&dt1->destination_local_reference, &conn->destination_local_reference, |
| 750 | sizeof(struct sccp_source_reference)); |
| 751 | dt1->segmenting = 0; |
| 752 | |
| 753 | /* copy the data */ |
| 754 | dt1->variable_start = 1; |
| 755 | data = msgb_put(msgb, extra_size); |
| 756 | data[0] = extra_size - 1; |
| 757 | memcpy(&data[1], _data->l3h, extra_size - 1); |
| 758 | |
| 759 | ret = _send_msg(msgb); |
| 760 | msgb_free(msgb); |
| 761 | |
| 762 | return ret; |
| 763 | } |
| 764 | |
Holger Hans Peter Freyther | 9343821 | 2009-11-18 22:11:28 +0100 | [diff] [blame] | 765 | static int _sccp_send_connection_it(struct sccp_connection *conn) |
| 766 | { |
| 767 | struct msgb *msgb; |
| 768 | struct sccp_data_it *it; |
| 769 | int ret; |
| 770 | |
| 771 | msgb = msgb_alloc_headroom(SCCP_MSG_SIZE, |
| 772 | SCCP_MSG_HEADROOM, "sccp it"); |
| 773 | msgb->l2h = &msgb->data[0]; |
| 774 | it = (struct sccp_data_it *) msgb_put(msgb, sizeof(*it)); |
| 775 | it->type = SCCP_MSG_TYPE_IT; |
| 776 | memcpy(&it->destination_local_reference, &conn->destination_local_reference, |
| 777 | sizeof(struct sccp_source_reference)); |
| 778 | memcpy(&it->source_local_reference, &conn->source_local_reference, |
| 779 | sizeof(struct sccp_source_reference)); |
| 780 | |
| 781 | it->proto_class = 0x2; |
| 782 | it->sequencing[0] = it->sequencing[1] = 0; |
| 783 | it->credit = 0; |
| 784 | |
| 785 | ret = _send_msg(msgb); |
| 786 | msgb_free(msgb); |
| 787 | return ret; |
| 788 | } |
| 789 | |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 790 | static int _sccp_send_connection_released(struct sccp_connection *conn, int cause) |
| 791 | { |
| 792 | struct msgb *msg; |
| 793 | struct sccp_connection_released *rel; |
| 794 | u_int8_t *data; |
| 795 | int ret; |
| 796 | |
| 797 | msg = msgb_alloc_headroom(SCCP_MSG_SIZE, SCCP_MSG_HEADROOM, |
| 798 | "sccp: connection released"); |
| 799 | msg->l2h = &msg->data[0]; |
| 800 | rel = (struct sccp_connection_released *) msgb_put(msg, sizeof(*rel)); |
| 801 | rel->type = SCCP_MSG_TYPE_RLSD; |
| 802 | rel->release_cause = cause; |
| 803 | |
| 804 | /* copy the source references */ |
| 805 | memcpy(&rel->destination_local_reference, &conn->destination_local_reference, |
| 806 | sizeof(struct sccp_source_reference)); |
| 807 | memcpy(&rel->source_local_reference, &conn->source_local_reference, |
| 808 | sizeof(struct sccp_source_reference)); |
| 809 | |
| 810 | data = msgb_put(msg, 1); |
| 811 | data[0] = SCCP_PNC_END_OF_OPTIONAL; |
| 812 | |
| 813 | _sccp_set_connection_state(conn, SCCP_CONNECTION_STATE_RELEASE); |
| 814 | ret = _send_msg(msg); |
| 815 | msgb_free(msg); |
| 816 | |
| 817 | return ret; |
| 818 | } |
| 819 | |
| 820 | /* |
| 821 | * Open a connection. The following is going to happen: |
| 822 | * |
| 823 | * - Verify the packet, e.g. that we have no other connection |
| 824 | * that id. |
| 825 | * - Ask the user if he wants to accept the connection |
| 826 | * - Try to open the connection by assigning a source local reference |
| 827 | * and sending the packet |
| 828 | */ |
| 829 | static int _sccp_handle_connection_request(struct msgb *msgb) |
| 830 | { |
Holger Hans Peter Freyther | 867d836 | 2010-01-27 12:25:13 +0100 | [diff] [blame] | 831 | struct sccp_parse_result result; |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 832 | |
| 833 | struct sccp_data_callback *cb; |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 834 | struct sccp_connection *connection; |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 835 | |
Holger Hans Peter Freyther | 867d836 | 2010-01-27 12:25:13 +0100 | [diff] [blame] | 836 | if (_sccp_parse_connection_request(msgb, &result) != 0) |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 837 | return -1; |
| 838 | |
Holger Hans Peter Freyther | 867d836 | 2010-01-27 12:25:13 +0100 | [diff] [blame] | 839 | cb = _find_ssn(result.called.ssn); |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 840 | if (!cb || !cb->accept_cb) { |
Holger Hans Peter Freyther | 867d836 | 2010-01-27 12:25:13 +0100 | [diff] [blame] | 841 | DEBUGP(DSCCP, "No routing for CR for called SSN: %u\n", result.called.ssn); |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 842 | return -1; |
| 843 | } |
| 844 | |
| 845 | /* check if the system wants this connection */ |
| 846 | connection = talloc_zero(tall_sccp_ctx, struct sccp_connection); |
| 847 | if (!connection) { |
| 848 | DEBUGP(DSCCP, "Allocation failed\n"); |
| 849 | return -1; |
| 850 | } |
| 851 | |
| 852 | /* |
| 853 | * sanity checks: |
| 854 | * - Is the source_local_reference in any other connection? |
| 855 | * then will call accept, assign a "destination" local reference |
| 856 | * and send a connection confirm, otherwise we will send a refuseed |
| 857 | * one.... |
| 858 | */ |
Holger Hans Peter Freyther | 867d836 | 2010-01-27 12:25:13 +0100 | [diff] [blame] | 859 | if (destination_local_reference_is_free(result.source_local_reference) != 0) { |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 860 | DEBUGP(DSCCP, "Need to reject connection with existing reference\n"); |
Holger Hans Peter Freyther | 867d836 | 2010-01-27 12:25:13 +0100 | [diff] [blame] | 861 | _sccp_send_refuse(result.source_local_reference, SCCP_REFUSAL_SCCP_FAILURE); |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 862 | talloc_free(connection); |
| 863 | return -1; |
| 864 | } |
| 865 | |
| 866 | connection->incoming = 1; |
Holger Hans Peter Freyther | 867d836 | 2010-01-27 12:25:13 +0100 | [diff] [blame] | 867 | connection->destination_local_reference = *result.source_local_reference; |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 868 | |
| 869 | if (cb->accept_cb(connection, cb->accept_context) != 0) { |
Holger Hans Peter Freyther | 867d836 | 2010-01-27 12:25:13 +0100 | [diff] [blame] | 870 | _sccp_send_refuse(result.source_local_reference, SCCP_REFUSAL_END_USER_ORIGINATED); |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 871 | _sccp_set_connection_state(connection, SCCP_CONNECTION_STATE_REFUSED); |
| 872 | talloc_free(connection); |
| 873 | return 0; |
| 874 | } |
| 875 | |
| 876 | |
| 877 | llist_add_tail(&connection->list, &sccp_connections); |
| 878 | |
| 879 | if (_sccp_send_connection_confirm(connection) != 0) { |
| 880 | DEBUGP(DSCCP, "Sending confirm failed... no available source reference?\n"); |
| 881 | |
Holger Hans Peter Freyther | 867d836 | 2010-01-27 12:25:13 +0100 | [diff] [blame] | 882 | _sccp_send_refuse(result.source_local_reference, SCCP_REFUSAL_SCCP_FAILURE); |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 883 | _sccp_set_connection_state(connection, SCCP_CONNECTION_STATE_REFUSED); |
| 884 | llist_del(&connection->list); |
| 885 | talloc_free(connection); |
| 886 | |
| 887 | return -1; |
| 888 | } |
| 889 | |
| 890 | /* |
| 891 | * If we have data let us forward things. |
| 892 | */ |
Holger Hans Peter Freyther | 867d836 | 2010-01-27 12:25:13 +0100 | [diff] [blame] | 893 | if (result.data_len != 0 && connection->data_cb) { |
| 894 | connection->data_cb(connection, msgb, result.data_len); |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 895 | } |
| 896 | |
| 897 | return 0; |
| 898 | } |
| 899 | |
| 900 | /* Handle the release confirmed */ |
Holger Hans Peter Freyther | db56e38 | 2010-01-29 04:19:56 +0100 | [diff] [blame] | 901 | static int _sccp_handle_connection_release_complete(struct msgb *msgb) |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 902 | { |
Holger Hans Peter Freyther | db56e38 | 2010-01-29 04:19:56 +0100 | [diff] [blame] | 903 | struct sccp_parse_result result; |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 904 | struct sccp_connection *conn; |
| 905 | |
Holger Hans Peter Freyther | db56e38 | 2010-01-29 04:19:56 +0100 | [diff] [blame] | 906 | if (_sccp_parse_connection_release_complete(msgb, &result) != 0) |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 907 | return -1; |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 908 | |
| 909 | /* find the connection */ |
| 910 | llist_for_each_entry(conn, &sccp_connections, list) { |
| 911 | if (conn->data_cb |
| 912 | && memcmp(&conn->source_local_reference, |
Holger Hans Peter Freyther | db56e38 | 2010-01-29 04:19:56 +0100 | [diff] [blame] | 913 | result.destination_local_reference, |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 914 | sizeof(conn->source_local_reference)) == 0 |
| 915 | && memcmp(&conn->destination_local_reference, |
Holger Hans Peter Freyther | db56e38 | 2010-01-29 04:19:56 +0100 | [diff] [blame] | 916 | result.source_local_reference, |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 917 | sizeof(conn->destination_local_reference)) == 0) { |
| 918 | goto found; |
| 919 | } |
| 920 | } |
| 921 | |
| 922 | |
| 923 | DEBUGP(DSCCP, "Release complete of unknown connection\n"); |
| 924 | return -1; |
| 925 | |
| 926 | found: |
| 927 | llist_del(&conn->list); |
| 928 | _sccp_set_connection_state(conn, SCCP_CONNECTION_STATE_RELEASE_COMPLETE); |
| 929 | return 0; |
| 930 | } |
| 931 | |
| 932 | /* Handle the Data Form 1 message */ |
Holger Hans Peter Freyther | 8edb961 | 2010-01-29 04:31:00 +0100 | [diff] [blame^] | 933 | static int _sccp_handle_connection_dt1(struct msgb *msgb) |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 934 | { |
Holger Hans Peter Freyther | 8edb961 | 2010-01-29 04:31:00 +0100 | [diff] [blame^] | 935 | struct sccp_parse_result result; |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 936 | struct sccp_connection *conn; |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 937 | |
Holger Hans Peter Freyther | 8edb961 | 2010-01-29 04:31:00 +0100 | [diff] [blame^] | 938 | if (_sccp_parse_connection_dt1(msgb, &result) != 0) |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 939 | return -1; |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 940 | |
| 941 | /* lookup if we have a connection with the given reference */ |
| 942 | llist_for_each_entry(conn, &sccp_connections, list) { |
| 943 | if (conn->data_cb |
| 944 | && memcmp(&conn->source_local_reference, |
Holger Hans Peter Freyther | 8edb961 | 2010-01-29 04:31:00 +0100 | [diff] [blame^] | 945 | result.destination_local_reference, |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 946 | sizeof(conn->source_local_reference)) == 0) { |
Holger Hans Peter Freyther | 8edb961 | 2010-01-29 04:31:00 +0100 | [diff] [blame^] | 947 | goto found; |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 948 | } |
| 949 | } |
| 950 | |
| 951 | DEBUGP(DSCCP, "No connection found for dt1 data\n"); |
| 952 | return -1; |
Holger Hans Peter Freyther | 8edb961 | 2010-01-29 04:31:00 +0100 | [diff] [blame^] | 953 | |
| 954 | found: |
| 955 | conn->data_cb(conn, msgb, result.data_len); |
| 956 | return 0; |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 957 | } |
| 958 | |
| 959 | /* confirm a connection release */ |
| 960 | static int _sccp_send_connection_release_complete(struct sccp_connection *connection) |
| 961 | { |
| 962 | struct msgb *msgb; |
| 963 | struct sccp_connection_release_complete *rlc; |
| 964 | int ret; |
| 965 | |
| 966 | msgb = msgb_alloc_headroom(SCCP_MSG_SIZE, |
| 967 | SCCP_MSG_HEADROOM, "sccp rlc"); |
| 968 | msgb->l2h = &msgb->data[0]; |
| 969 | |
| 970 | rlc = (struct sccp_connection_release_complete *) msgb_put(msgb, sizeof(*rlc)); |
| 971 | rlc->type = SCCP_MSG_TYPE_RLC; |
| 972 | memcpy(&rlc->destination_local_reference, |
| 973 | &connection->destination_local_reference, sizeof(struct sccp_source_reference)); |
| 974 | memcpy(&rlc->source_local_reference, |
| 975 | &connection->source_local_reference, sizeof(struct sccp_source_reference)); |
| 976 | |
| 977 | ret = _send_msg(msgb); |
| 978 | msgb_free(msgb); |
| 979 | |
| 980 | /* |
| 981 | * Remove from the list of active connections and set the state. User code |
| 982 | * should now free the entry. |
| 983 | */ |
| 984 | llist_del(&connection->list); |
| 985 | _sccp_set_connection_state(connection, SCCP_CONNECTION_STATE_RELEASE_COMPLETE); |
| 986 | |
| 987 | return ret; |
| 988 | } |
| 989 | |
| 990 | /* connection released, send a released confirm */ |
Holger Hans Peter Freyther | 5defc56 | 2010-01-29 03:49:32 +0100 | [diff] [blame] | 991 | static int _sccp_handle_connection_released(struct msgb *msgb) |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 992 | { |
Holger Hans Peter Freyther | 5defc56 | 2010-01-29 03:49:32 +0100 | [diff] [blame] | 993 | struct sccp_parse_result result; |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 994 | struct sccp_connection *conn; |
| 995 | |
Holger Hans Peter Freyther | 5defc56 | 2010-01-29 03:49:32 +0100 | [diff] [blame] | 996 | if (_sccp_parse_connection_released(msgb, &result) == -1) |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 997 | return -1; |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 998 | |
| 999 | /* lookup if we have a connection with the given reference */ |
| 1000 | llist_for_each_entry(conn, &sccp_connections, list) { |
| 1001 | if (conn->data_cb |
| 1002 | && memcmp(&conn->source_local_reference, |
Holger Hans Peter Freyther | 5defc56 | 2010-01-29 03:49:32 +0100 | [diff] [blame] | 1003 | result.destination_local_reference, |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 1004 | sizeof(conn->source_local_reference)) == 0 |
| 1005 | && memcmp(&conn->destination_local_reference, |
Holger Hans Peter Freyther | 5defc56 | 2010-01-29 03:49:32 +0100 | [diff] [blame] | 1006 | result.source_local_reference, |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 1007 | sizeof(conn->destination_local_reference)) == 0) { |
| 1008 | goto found; |
| 1009 | } |
| 1010 | } |
| 1011 | |
| 1012 | |
| 1013 | DEBUGP(DSCCP, "Unknown connection was released.\n"); |
| 1014 | return -1; |
| 1015 | |
| 1016 | /* we have found a connection */ |
| 1017 | found: |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 1018 | /* optional data */ |
Holger Hans Peter Freyther | 5defc56 | 2010-01-29 03:49:32 +0100 | [diff] [blame] | 1019 | if (result.data_len != 0 && conn->data_cb) { |
| 1020 | conn->data_cb(conn, msgb, result.data_len); |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 1021 | } |
| 1022 | |
| 1023 | /* generate a response */ |
| 1024 | if (_sccp_send_connection_release_complete(conn) != 0) { |
| 1025 | DEBUGP(DSCCP, "Sending release confirmed failed\n"); |
| 1026 | return -1; |
| 1027 | } |
| 1028 | |
| 1029 | return 0; |
| 1030 | } |
| 1031 | |
| 1032 | static int _sccp_handle_connection_refused(struct msgb *msgb) |
| 1033 | { |
Holger Hans Peter Freyther | 6233c58 | 2010-01-29 03:58:12 +0100 | [diff] [blame] | 1034 | struct sccp_parse_result result; |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 1035 | struct sccp_connection *conn; |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 1036 | |
Holger Hans Peter Freyther | 6233c58 | 2010-01-29 03:58:12 +0100 | [diff] [blame] | 1037 | if (_sccp_parse_connection_refused(msgb, &result) != 0) |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 1038 | return -1; |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 1039 | |
| 1040 | /* lookup if we have a connection with the given reference */ |
| 1041 | llist_for_each_entry(conn, &sccp_connections, list) { |
| 1042 | if (conn->incoming == 0 && conn->data_cb |
| 1043 | && memcmp(&conn->source_local_reference, |
Holger Hans Peter Freyther | 6233c58 | 2010-01-29 03:58:12 +0100 | [diff] [blame] | 1044 | result.destination_local_reference, |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 1045 | sizeof(conn->source_local_reference)) == 0) { |
| 1046 | goto found; |
| 1047 | } |
| 1048 | } |
| 1049 | |
| 1050 | DEBUGP(DSCCP, "Refused but no connection found\n"); |
| 1051 | return -1; |
| 1052 | |
| 1053 | found: |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 1054 | /* optional data */ |
Holger Hans Peter Freyther | 6233c58 | 2010-01-29 03:58:12 +0100 | [diff] [blame] | 1055 | if (result.data_len != 0 && conn->data_cb) { |
| 1056 | conn->data_cb(conn, msgb, result.data_len); |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 1057 | } |
| 1058 | |
| 1059 | |
| 1060 | llist_del(&conn->list); |
| 1061 | _sccp_set_connection_state(conn, SCCP_CONNECTION_STATE_REFUSED); |
| 1062 | return 0; |
| 1063 | } |
| 1064 | |
| 1065 | static int _sccp_handle_connection_confirm(struct msgb *msgb) |
| 1066 | { |
Holger Hans Peter Freyther | 1f21f34 | 2010-01-29 04:03:00 +0100 | [diff] [blame] | 1067 | struct sccp_parse_result result; |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 1068 | struct sccp_connection *conn; |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 1069 | |
Holger Hans Peter Freyther | 1f21f34 | 2010-01-29 04:03:00 +0100 | [diff] [blame] | 1070 | if (_sccp_parse_connection_confirm(msgb, &result) != 0) |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 1071 | return -1; |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 1072 | |
| 1073 | /* lookup if we have a connection with the given reference */ |
| 1074 | llist_for_each_entry(conn, &sccp_connections, list) { |
| 1075 | if (conn->incoming == 0 && conn->data_cb |
| 1076 | && memcmp(&conn->source_local_reference, |
Holger Hans Peter Freyther | 1f21f34 | 2010-01-29 04:03:00 +0100 | [diff] [blame] | 1077 | result.destination_local_reference, |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 1078 | sizeof(conn->source_local_reference)) == 0) { |
| 1079 | goto found; |
| 1080 | } |
| 1081 | } |
| 1082 | |
| 1083 | DEBUGP(DSCCP, "Confirmed but no connection found\n"); |
| 1084 | return -1; |
| 1085 | |
| 1086 | found: |
| 1087 | /* copy the addresses of the connection */ |
Holger Hans Peter Freyther | 1f21f34 | 2010-01-29 04:03:00 +0100 | [diff] [blame] | 1088 | conn->destination_local_reference = *result.source_local_reference; |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 1089 | _sccp_set_connection_state(conn, SCCP_CONNECTION_STATE_ESTABLISHED); |
| 1090 | |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 1091 | /* optional data */ |
Holger Hans Peter Freyther | 1f21f34 | 2010-01-29 04:03:00 +0100 | [diff] [blame] | 1092 | if (result.data_len != 0 && conn->data_cb) { |
| 1093 | conn->data_cb(conn, msgb, result.data_len); |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 1094 | } |
| 1095 | |
| 1096 | return 0; |
| 1097 | } |
| 1098 | |
| 1099 | |
| 1100 | int sccp_system_init(int (*outgoing)(struct msgb *data, void *ctx), void *ctx) |
| 1101 | { |
| 1102 | sccp_system.write_data = outgoing; |
| 1103 | sccp_system.write_context = ctx; |
| 1104 | |
| 1105 | return 0; |
| 1106 | } |
| 1107 | |
| 1108 | /* oh my god a real SCCP packet. need to dispatch it now */ |
| 1109 | int sccp_system_incoming(struct msgb *msgb) |
| 1110 | { |
| 1111 | if (msgb_l2len(msgb) < 1 ) { |
| 1112 | DEBUGP(DSCCP, "Too short packet\n"); |
| 1113 | return -1; |
| 1114 | } |
| 1115 | |
| 1116 | int type = msgb->l2h[0]; |
| 1117 | |
| 1118 | switch(type) { |
| 1119 | case SCCP_MSG_TYPE_CR: |
| 1120 | return _sccp_handle_connection_request(msgb); |
| 1121 | break; |
| 1122 | case SCCP_MSG_TYPE_RLSD: |
| 1123 | return _sccp_handle_connection_released(msgb); |
| 1124 | break; |
| 1125 | case SCCP_MSG_TYPE_CREF: |
| 1126 | return _sccp_handle_connection_refused(msgb); |
| 1127 | break; |
| 1128 | case SCCP_MSG_TYPE_CC: |
| 1129 | return _sccp_handle_connection_confirm(msgb); |
| 1130 | break; |
| 1131 | case SCCP_MSG_TYPE_RLC: |
| 1132 | return _sccp_handle_connection_release_complete(msgb); |
| 1133 | break; |
| 1134 | case SCCP_MSG_TYPE_DT1: |
| 1135 | return _sccp_handle_connection_dt1(msgb); |
| 1136 | break; |
| 1137 | case SCCP_MSG_TYPE_UDT: |
| 1138 | return _sccp_handle_read(msgb); |
| 1139 | break; |
| 1140 | default: |
| 1141 | DEBUGP(DSCCP, "unimplemented msg type: %d\n", type); |
| 1142 | }; |
| 1143 | |
| 1144 | return -1; |
| 1145 | } |
| 1146 | |
| 1147 | /* create a packet from the data */ |
| 1148 | int sccp_connection_write(struct sccp_connection *connection, struct msgb *data) |
| 1149 | { |
| 1150 | if (connection->connection_state < SCCP_CONNECTION_STATE_CONFIRM |
| 1151 | || connection->connection_state > SCCP_CONNECTION_STATE_ESTABLISHED) { |
| 1152 | DEBUGP(DSCCP, "sccp_connection_write: Wrong connection state: %p %d\n", |
| 1153 | connection, connection->connection_state); |
| 1154 | return -1; |
| 1155 | } |
| 1156 | |
| 1157 | return _sccp_send_connection_data(connection, data); |
| 1158 | } |
| 1159 | |
Holger Hans Peter Freyther | 9343821 | 2009-11-18 22:11:28 +0100 | [diff] [blame] | 1160 | /* |
| 1161 | * Send a Inactivity Test message. The owner of the connection |
| 1162 | * should start a timer and call this method regularily. Calling |
| 1163 | * this every 60 seconds should be good enough. |
| 1164 | */ |
| 1165 | int sccp_connection_send_it(struct sccp_connection *connection) |
| 1166 | { |
| 1167 | if (connection->connection_state < SCCP_CONNECTION_STATE_CONFIRM |
| 1168 | || connection->connection_state > SCCP_CONNECTION_STATE_ESTABLISHED) { |
| 1169 | DEBUGP(DSCCP, "sccp_connection_write: Wrong connection state: %p %d\n", |
| 1170 | connection, connection->connection_state); |
| 1171 | return -1; |
| 1172 | } |
| 1173 | |
| 1174 | return _sccp_send_connection_it(connection); |
| 1175 | } |
| 1176 | |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 1177 | /* send a connection release and wait for the connection released */ |
| 1178 | int sccp_connection_close(struct sccp_connection *connection, int cause) |
| 1179 | { |
| 1180 | if (connection->connection_state < SCCP_CONNECTION_STATE_CONFIRM |
| 1181 | || connection->connection_state > SCCP_CONNECTION_STATE_ESTABLISHED) { |
| 1182 | DEBUGPC(DSCCP, "Can not close the connection. It was never opened: %p %d\n", |
| 1183 | connection, connection->connection_state); |
| 1184 | return -1; |
| 1185 | } |
| 1186 | |
| 1187 | return _sccp_send_connection_released(connection, cause); |
| 1188 | } |
| 1189 | |
| 1190 | int sccp_connection_free(struct sccp_connection *connection) |
| 1191 | { |
| 1192 | if (connection->connection_state > SCCP_CONNECTION_STATE_NONE |
| 1193 | && connection->connection_state < SCCP_CONNECTION_STATE_RELEASE_COMPLETE) { |
| 1194 | DEBUGP(DSCCP, "The connection needs to be released before it is freed"); |
| 1195 | return -1; |
| 1196 | } |
| 1197 | |
| 1198 | talloc_free(connection); |
| 1199 | return 0; |
| 1200 | } |
| 1201 | |
| 1202 | struct sccp_connection *sccp_connection_socket(void) |
| 1203 | { |
| 1204 | return talloc_zero(tall_sccp_ctx, struct sccp_connection); |
| 1205 | } |
| 1206 | |
| 1207 | int sccp_connection_connect(struct sccp_connection *conn, |
| 1208 | const struct sockaddr_sccp *local, |
| 1209 | struct msgb *data) |
| 1210 | { |
| 1211 | return _sccp_send_connection_request(conn, local, data); |
| 1212 | } |
| 1213 | |
| 1214 | int sccp_connection_set_incoming(const struct sockaddr_sccp *sock, |
| 1215 | int (*accept_cb)(struct sccp_connection *, void *), void *context) |
| 1216 | { |
| 1217 | struct sccp_data_callback *cb; |
| 1218 | |
| 1219 | if (!sock) |
| 1220 | return -2; |
| 1221 | |
| 1222 | cb = _find_ssn(sock->sccp_ssn); |
| 1223 | if (!cb) |
| 1224 | return -1; |
| 1225 | |
| 1226 | cb->accept_cb = accept_cb; |
| 1227 | cb->accept_context = context; |
| 1228 | return 0; |
| 1229 | } |
| 1230 | |
| 1231 | int sccp_write(struct msgb *data, const struct sockaddr_sccp *in, |
| 1232 | const struct sockaddr_sccp *out, int class) |
| 1233 | { |
| 1234 | return _sccp_send_data(class, in, out, data); |
| 1235 | } |
| 1236 | |
| 1237 | int sccp_set_read(const struct sockaddr_sccp *sock, |
| 1238 | int (*read_cb)(struct msgb *, unsigned int, void *), void *context) |
| 1239 | { |
| 1240 | struct sccp_data_callback *cb; |
| 1241 | |
| 1242 | if (!sock) |
| 1243 | return -2; |
| 1244 | |
| 1245 | cb = _find_ssn(sock->sccp_ssn); |
| 1246 | if (!cb) |
| 1247 | return -1; |
| 1248 | |
| 1249 | cb->read_cb = read_cb; |
| 1250 | cb->read_context = context; |
| 1251 | return 0; |
| 1252 | } |
| 1253 | |
| 1254 | static_assert(sizeof(struct sccp_source_reference) <= sizeof(u_int32_t), enough_space); |
| 1255 | |
| 1256 | u_int32_t sccp_src_ref_to_int(struct sccp_source_reference *ref) |
| 1257 | { |
| 1258 | u_int32_t src_ref = 0; |
| 1259 | memcpy(&src_ref, ref, sizeof(*ref)); |
| 1260 | return src_ref; |
| 1261 | } |
| 1262 | |
| 1263 | struct sccp_source_reference sccp_src_ref_from_int(u_int32_t int_ref) |
| 1264 | { |
| 1265 | struct sccp_source_reference ref; |
| 1266 | memcpy(&ref, &int_ref, sizeof(ref)); |
| 1267 | return ref; |
| 1268 | } |
| 1269 | |
Holger Hans Peter Freyther | 481e10b | 2010-01-13 09:55:43 +0100 | [diff] [blame] | 1270 | int sccp_determine_msg_type(struct msgb *msg) |
| 1271 | { |
| 1272 | if (msgb_l2len(msg) < 1) |
| 1273 | return -1; |
| 1274 | |
| 1275 | return msg->l2h[0]; |
| 1276 | } |
| 1277 | |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 1278 | static __attribute__((constructor)) void on_dso_load(void) |
| 1279 | { |
| 1280 | tall_sccp_ctx = talloc_named_const(NULL, 1, "sccp"); |
| 1281 | } |
| 1282 | |
| 1283 | static __attribute__((destructor)) void on_dso_unload(void) |
| 1284 | { |
| 1285 | talloc_report_full(tall_sccp_ctx, stderr); |
| 1286 | } |