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