blob: ae08b5db2897c7be2169a5dd5f49198c6834b606 [file] [log] [blame]
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001/*
2 * SCCP management code
3 *
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +01004 * (C) 2009, 2010 by Holger Hans Peter Freyther <zecke@selfish.org>
5 * (C) 2009, 2010 by on-waves.com
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02006 *
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
33static void *tall_sccp_ctx;
34static LLIST_HEAD(sccp_connections);
35
36#define SCCP_MSG_SIZE 4096
37#define SCCP_MSG_HEADROOM 128
38
39/* global data */
40const struct sockaddr_sccp sccp_ssn_bssap = {
41 .sccp_family = 0,
42 .sccp_ssn = SCCP_SSN_BSSAP,
43};
44
45struct sccp_system {
46 /* layer3 -> layer2 */
47 int (*write_data)(struct msgb *data, void *context);
48 void *write_context;
49};
50
51
52static struct sccp_system sccp_system = {
53 .write_data = NULL,
54};
55
56struct 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
69static LLIST_HEAD(sccp_callbacks);
70
71static 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
93static 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 */
101static 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
152static 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
166static 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 Freythera8cd2e62010-01-27 12:25:13 +0100202int _sccp_parse_connection_request(struct msgb *msgb, struct sccp_parse_result *result)
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +0100203{
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100204 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 Freytherefca5412010-01-27 12:12:46 +0100250}
251
Holger Hans Peter Freyther88fe6ee2010-01-29 03:49:32 +0100252int _sccp_parse_connection_released(struct msgb *msgb, struct sccp_parse_result *result)
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +0100253{
Holger Hans Peter Freyther88fe6ee2010-01-29 03:49:32 +0100254 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 Freytherefca5412010-01-27 12:12:46 +0100284}
285
Holger Hans Peter Freytherfe5de4e2010-01-29 03:58:12 +0100286int _sccp_parse_connection_refused(struct msgb *msgb, struct sccp_parse_result *result)
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +0100287{
Holger Hans Peter Freytherfe5de4e2010-01-29 03:58:12 +0100288 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 Freytherefca5412010-01-27 12:12:46 +0100321}
322
Holger Hans Peter Freytherca1d1d12010-01-29 04:03:00 +0100323int _sccp_parse_connection_confirm(struct msgb *msgb, struct sccp_parse_result *result)
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +0100324{
Holger Hans Peter Freytherca1d1d12010-01-29 04:03:00 +0100325 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 Freytherefca5412010-01-27 12:12:46 +0100358}
359
Holger Hans Peter Freyther18c5cad2010-01-29 04:19:56 +0100360int _sccp_parse_connection_release_complete(struct msgb *msgb, struct sccp_parse_result *result)
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +0100361{
Holger Hans Peter Freyther18c5cad2010-01-29 04:19:56 +0100362 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 Freytherefca5412010-01-27 12:12:46 +0100378}
379
380int _sccp_parse_connection_dt1(struct msgb *msg, struct sccp_parse_result *result)
381{
382 return -1;
383}
384
385int _sccp_parse_udt(struct msgb *msgb, struct sccp_parse_result *result)
386{
387 static const u_int32_t header_size = sizeof(struct sccp_data_unitdata);
388 static const u_int32_t called_offset = offsetof(struct sccp_data_unitdata, variable_called);
389 static const u_int32_t calling_offset = offsetof(struct sccp_data_unitdata, variable_calling);
390 static const u_int32_t data_offset = offsetof(struct sccp_data_unitdata, variable_data);
391
392 struct sccp_data_unitdata *udt = (struct sccp_data_unitdata *)msgb->l2h;
393
394 if (msgb_l2len(msgb) < header_size) {
395 DEBUGP(DSCCP, "msgb < header_size %u %u\n",
396 msgb_l2len(msgb), header_size);
397 return -1;
398 }
399
400 /* copy out the calling and called address. Add the off */
401 if (copy_address(&result->called, called_offset + udt->variable_called, msgb) != 0)
402 return -1;
403
404 if (check_address(&result->called) != 0) {
405 DEBUGP(DSCCP, "Invalid called address according to 08.06: 0x%x 0x%x\n",
406 *(u_int8_t *)&result->called.address, result->called.ssn);
407 return -1;
408 }
409
410 if (copy_address(&result->calling, calling_offset + udt->variable_calling, msgb) != 0)
411 return -1;
412
413 if (check_address(&result->calling) != 0) {
414 DEBUGP(DSCCP, "Invalid called address according to 08.06: 0x%x 0x%x\n",
415 *(u_int8_t *)&result->called.address, result->called.ssn);
416 }
417
418 /* we don't have enough size for the data */
419 if (msgb_l2len(msgb) < data_offset + udt->variable_data + 1) {
420 DEBUGP(DSCCP, "msgb < header + offset %u %u %u\n",
421 msgb_l2len(msgb), header_size, udt->variable_data);
422 return -1;
423 }
424
425
426 msgb->l3h = &udt->data[udt->variable_data];
427
428 if (msgb_l3len(msgb) != msgb->l3h[-1]) {
429 DEBUGP(DSCCP, "msgb is truncated %u %u\n",
430 msgb_l3len(msgb), msgb->l3h[-1]);
431 return -1;
432 }
433
434 return 0;
435}
436
437
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200438/*
439 * Send UDT. Currently we have a fixed address...
440 */
441static int _sccp_send_data(int class, const struct sockaddr_sccp *in,
442 const struct sockaddr_sccp *out, struct msgb *payload)
443{
444 struct sccp_data_unitdata *udt;
445 u_int8_t *data;
446 int ret;
447
448 if (msgb_l3len(payload) > 256) {
449 DEBUGP(DSCCP, "The payload is too big for one udt\n");
450 return -1;
451 }
452
453 struct msgb *msg = msgb_alloc_headroom(SCCP_MSG_SIZE,
454 SCCP_MSG_HEADROOM, "sccp: udt");
455 msg->l2h = &msg->data[0];
456 udt = (struct sccp_data_unitdata *)msgb_put(msg, sizeof(*udt));
457
458 udt->type = SCCP_MSG_TYPE_UDT;
459 udt->proto_class = class;
460 udt->variable_called = 3;
461 udt->variable_calling = 5;
462 udt->variable_data = 7;
463
464 /* for variable data we start with a size and the data */
465 data = msgb_put(msg, 1 + 2);
466 data[0] = 2;
467 data[1] = 0x42;
468 data[2] = out->sccp_ssn;
469
470 data = msgb_put(msg, 1 + 2);
471 data[0] = 2;
472 data[1] = 0x42;
473 data[2] = in->sccp_ssn;
474
475 /* copy the payload */
476 data = msgb_put(msg, 1 + msgb_l3len(payload));
477 data[0] = msgb_l3len(payload);
478 memcpy(&data[1], payload->l3h, msgb_l3len(payload));
479
480 ret = _send_msg(msg);
481 msgb_free(msg);
482
483 return ret;
484}
485
486static int _sccp_handle_read(struct msgb *msgb)
487{
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200488 struct sccp_data_callback *cb;
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +0100489 struct sccp_parse_result result;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200490
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +0100491 if (_sccp_parse_udt(msgb, &result) != 0)
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200492 return -1;
493
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +0100494 cb = _find_ssn(result.called.ssn);
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200495 if (!cb || !cb->read_cb) {
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +0100496 DEBUGP(DSCCP, "No routing for UDT for called SSN: %u\n", result.called.ssn);
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200497 return -1;
498 }
499
500 /* sanity check */
501 return cb->read_cb(msgb, msgb_l3len(msgb), cb->read_context);
502}
503
504/*
505 * handle connection orientated methods
506 */
507static int source_local_reference_is_free(struct sccp_source_reference *reference)
508{
509 struct sccp_connection *connection;
510
511 llist_for_each_entry(connection, &sccp_connections, list) {
512 if (memcmp(reference, &connection->source_local_reference, sizeof(*reference)) == 0)
513 return -1;
514 }
515
516 return 0;
517}
518
519static int destination_local_reference_is_free(struct sccp_source_reference *reference)
520{
521 struct sccp_connection *connection;
522
523 llist_for_each_entry(connection, &sccp_connections, list) {
524 if (memcmp(reference, &connection->destination_local_reference, sizeof(*reference)) == 0)
525 return -1;
526 }
527
528 return 0;
529}
530
531static int assign_source_local_reference(struct sccp_connection *connection)
532{
533 static u_int32_t last_ref = 0x30000;
534 int wrapped = 0;
535
536 do {
537 struct sccp_source_reference reference;
538 reference.octet1 = (last_ref >> 0) & 0xff;
539 reference.octet2 = (last_ref >> 8) & 0xff;
540 reference.octet3 = (last_ref >> 16) & 0xff;
541
542 ++last_ref;
543 /* do not use the reversed word and wrap around */
544 if ((last_ref & 0x00FFFFFF) == 0x00FFFFFF) {
545 DEBUGP(DSCCP, "Wrapped searching for a free code\n");
546 last_ref = 0;
547 ++wrapped;
548 }
549
550 if (source_local_reference_is_free(&reference) == 0) {
551 connection->source_local_reference = reference;
552 return 0;
553 }
554 } while (wrapped != 2);
555
556 DEBUGP(DSCCP, "Finding a free reference failed\n");
557 return -1;
558}
559
560static void _sccp_set_connection_state(struct sccp_connection *connection, int new_state)
561{
562 int old_state = connection->connection_state;
563
564 connection->connection_state = new_state;
565 if (connection->state_cb)
566 connection->state_cb(connection, old_state);
567}
568
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100569static int _sccp_send_refuse(struct sccp_source_reference *src_ref, int cause)
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200570{
571 struct msgb *msgb;
572 struct sccp_connection_refused *ref;
573 u_int8_t *data;
574 int ret;
575
576 msgb = msgb_alloc_headroom(SCCP_MSG_SIZE,
577 SCCP_MSG_HEADROOM, "sccp ref");
578 msgb->l2h = &msgb->data[0];
579
580 ref = (struct sccp_connection_refused *) msgb_put(msgb, sizeof(*ref));
581 ref->type = SCCP_MSG_TYPE_CREF;
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100582 memcpy(&ref->destination_local_reference, src_ref,
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200583 sizeof(struct sccp_source_reference));
584 ref->cause = cause;
585 ref->optional_start = 1;
586
587 data = msgb_put(msgb, 1);
588 data[0] = SCCP_PNC_END_OF_OPTIONAL;
589
590 ret = _send_msg(msgb);
591 msgb_free(msgb);
592 return ret;
593}
594
595static int _sccp_send_connection_confirm(struct sccp_connection *connection)
596{
597 struct msgb *response;
598 struct sccp_connection_confirm *confirm;
599 u_int8_t *optional_data;
600 int ret;
601
602 if (assign_source_local_reference(connection) != 0)
603 return -1;
604
605 response = msgb_alloc_headroom(SCCP_MSG_SIZE,
606 SCCP_MSG_HEADROOM, "sccp confirm");
607 response->l2h = &response->data[0];
608
609 confirm = (struct sccp_connection_confirm *) msgb_put(response, sizeof(*confirm));
610
611 confirm->type = SCCP_MSG_TYPE_CC;
612 memcpy(&confirm->destination_local_reference,
613 &connection->destination_local_reference,
614 sizeof(connection->destination_local_reference));
615 memcpy(&confirm->source_local_reference,
616 &connection->source_local_reference,
617 sizeof(connection->source_local_reference));
618 confirm->proto_class = 2;
619 confirm->optional_start = 1;
620
621 optional_data = (u_int8_t *) msgb_put(response, 1);
622 optional_data[0] = SCCP_PNC_END_OF_OPTIONAL;
623
624 ret = _send_msg(response);
625 msgb_free(response);
626
627 _sccp_set_connection_state(connection, SCCP_CONNECTION_STATE_ESTABLISHED);
628 return ret;
629}
630
631static int _sccp_send_connection_request(struct sccp_connection *connection,
632 const struct sockaddr_sccp *called, struct msgb *msg)
633{
634 struct msgb *request;
635 struct sccp_connection_request *req;
636 u_int8_t *data;
637 u_int8_t extra_size = 3 + 1;
638 int ret;
639
640
641 if (msg && (msgb_l3len(msg) < 3 || msgb_l3len(msg) > 130)) {
642 DEBUGP(DSCCP, "Invalid amount of data... %d\n", msgb_l3len(msg));
643 return -1;
644 }
645
646 /* try to find a id */
647 if (assign_source_local_reference(connection) != 0) {
648 DEBUGP(DSCCP, "Assigning a local reference failed.\n");
649 _sccp_set_connection_state(connection, SCCP_CONNECTION_STATE_SETUP_ERROR);
650 return -1;
651 }
652
653
654 if (msg)
655 extra_size += 2 + msgb_l3len(msg);
656 request = msgb_alloc_headroom(SCCP_MSG_SIZE,
657 SCCP_MSG_HEADROOM, "sccp connection request");
658 request->l2h = &request->data[0];
659 req = (struct sccp_connection_request *) msgb_put(request, sizeof(*req));
660
661 req->type = SCCP_MSG_TYPE_CR;
662 memcpy(&req->source_local_reference, &connection->source_local_reference,
663 sizeof(connection->source_local_reference));
664 req->proto_class = 2;
665 req->variable_called = 2;
666 req->optional_start = 4;
667
668 /* write the called party address */
669 data = msgb_put(request, 1 + 2);
670 data[0] = 2;
671 data[1] = 0x42;
672 data[2] = called->sccp_ssn;
673
674 /* write the payload */
675 if (msg) {
676 data = msgb_put(request, 2 + msgb_l3len(msg));
677 data[0] = SCCP_PNC_DATA;
678 data[1] = msgb_l3len(msg);
679 memcpy(&data[2], msg->l3h, msgb_l3len(msg));
680 }
681
682 data = msgb_put(request, 1);
683 data[0] = SCCP_PNC_END_OF_OPTIONAL;
684
685 llist_add_tail(&connection->list, &sccp_connections);
686 _sccp_set_connection_state(connection, SCCP_CONNECTION_STATE_REQUEST);
687
688 ret = _send_msg(request);
689 msgb_free(request);
690
691 return ret;
692}
693
694static int _sccp_send_connection_data(struct sccp_connection *conn, struct msgb *_data)
695{
696 struct msgb *msgb;
697 struct sccp_data_form1 *dt1;
698 u_int8_t *data;
699 int extra_size;
700 int ret;
701
702 if (msgb_l3len(_data) < 2 || msgb_l3len(_data) > 256) {
703 DEBUGP(DSCCP, "data size too big, segmenting unimplemented.\n");
704 return -1;
705 }
706
707 extra_size = 1 + msgb_l3len(_data);
708 msgb = msgb_alloc_headroom(SCCP_MSG_SIZE,
709 SCCP_MSG_HEADROOM, "sccp dt1");
710 msgb->l2h = &msgb->data[0];
711
712 dt1 = (struct sccp_data_form1 *) msgb_put(msgb, sizeof(*dt1));
713 dt1->type = SCCP_MSG_TYPE_DT1;
714 memcpy(&dt1->destination_local_reference, &conn->destination_local_reference,
715 sizeof(struct sccp_source_reference));
716 dt1->segmenting = 0;
717
718 /* copy the data */
719 dt1->variable_start = 1;
720 data = msgb_put(msgb, extra_size);
721 data[0] = extra_size - 1;
722 memcpy(&data[1], _data->l3h, extra_size - 1);
723
724 ret = _send_msg(msgb);
725 msgb_free(msgb);
726
727 return ret;
728}
729
Holger Hans Peter Freyther3b9516e2009-11-18 22:11:28 +0100730static int _sccp_send_connection_it(struct sccp_connection *conn)
731{
732 struct msgb *msgb;
733 struct sccp_data_it *it;
734 int ret;
735
736 msgb = msgb_alloc_headroom(SCCP_MSG_SIZE,
737 SCCP_MSG_HEADROOM, "sccp it");
738 msgb->l2h = &msgb->data[0];
739 it = (struct sccp_data_it *) msgb_put(msgb, sizeof(*it));
740 it->type = SCCP_MSG_TYPE_IT;
741 memcpy(&it->destination_local_reference, &conn->destination_local_reference,
742 sizeof(struct sccp_source_reference));
743 memcpy(&it->source_local_reference, &conn->source_local_reference,
744 sizeof(struct sccp_source_reference));
745
746 it->proto_class = 0x2;
747 it->sequencing[0] = it->sequencing[1] = 0;
748 it->credit = 0;
749
750 ret = _send_msg(msgb);
751 msgb_free(msgb);
752 return ret;
753}
754
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200755static int _sccp_send_connection_released(struct sccp_connection *conn, int cause)
756{
757 struct msgb *msg;
758 struct sccp_connection_released *rel;
759 u_int8_t *data;
760 int ret;
761
762 msg = msgb_alloc_headroom(SCCP_MSG_SIZE, SCCP_MSG_HEADROOM,
763 "sccp: connection released");
764 msg->l2h = &msg->data[0];
765 rel = (struct sccp_connection_released *) msgb_put(msg, sizeof(*rel));
766 rel->type = SCCP_MSG_TYPE_RLSD;
767 rel->release_cause = cause;
768
769 /* copy the source references */
770 memcpy(&rel->destination_local_reference, &conn->destination_local_reference,
771 sizeof(struct sccp_source_reference));
772 memcpy(&rel->source_local_reference, &conn->source_local_reference,
773 sizeof(struct sccp_source_reference));
774
775 data = msgb_put(msg, 1);
776 data[0] = SCCP_PNC_END_OF_OPTIONAL;
777
778 _sccp_set_connection_state(conn, SCCP_CONNECTION_STATE_RELEASE);
779 ret = _send_msg(msg);
780 msgb_free(msg);
781
782 return ret;
783}
784
785/*
786 * Open a connection. The following is going to happen:
787 *
788 * - Verify the packet, e.g. that we have no other connection
789 * that id.
790 * - Ask the user if he wants to accept the connection
791 * - Try to open the connection by assigning a source local reference
792 * and sending the packet
793 */
794static int _sccp_handle_connection_request(struct msgb *msgb)
795{
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100796 struct sccp_parse_result result;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200797
798 struct sccp_data_callback *cb;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200799 struct sccp_connection *connection;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200800
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100801 if (_sccp_parse_connection_request(msgb, &result) != 0)
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200802 return -1;
803
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100804 cb = _find_ssn(result.called.ssn);
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200805 if (!cb || !cb->accept_cb) {
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100806 DEBUGP(DSCCP, "No routing for CR for called SSN: %u\n", result.called.ssn);
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200807 return -1;
808 }
809
810 /* check if the system wants this connection */
811 connection = talloc_zero(tall_sccp_ctx, struct sccp_connection);
812 if (!connection) {
813 DEBUGP(DSCCP, "Allocation failed\n");
814 return -1;
815 }
816
817 /*
818 * sanity checks:
819 * - Is the source_local_reference in any other connection?
820 * then will call accept, assign a "destination" local reference
821 * and send a connection confirm, otherwise we will send a refuseed
822 * one....
823 */
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100824 if (destination_local_reference_is_free(result.source_local_reference) != 0) {
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200825 DEBUGP(DSCCP, "Need to reject connection with existing reference\n");
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100826 _sccp_send_refuse(result.source_local_reference, SCCP_REFUSAL_SCCP_FAILURE);
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200827 talloc_free(connection);
828 return -1;
829 }
830
831 connection->incoming = 1;
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100832 connection->destination_local_reference = *result.source_local_reference;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200833
834 if (cb->accept_cb(connection, cb->accept_context) != 0) {
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100835 _sccp_send_refuse(result.source_local_reference, SCCP_REFUSAL_END_USER_ORIGINATED);
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200836 _sccp_set_connection_state(connection, SCCP_CONNECTION_STATE_REFUSED);
837 talloc_free(connection);
838 return 0;
839 }
840
841
842 llist_add_tail(&connection->list, &sccp_connections);
843
844 if (_sccp_send_connection_confirm(connection) != 0) {
845 DEBUGP(DSCCP, "Sending confirm failed... no available source reference?\n");
846
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100847 _sccp_send_refuse(result.source_local_reference, SCCP_REFUSAL_SCCP_FAILURE);
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200848 _sccp_set_connection_state(connection, SCCP_CONNECTION_STATE_REFUSED);
849 llist_del(&connection->list);
850 talloc_free(connection);
851
852 return -1;
853 }
854
855 /*
856 * If we have data let us forward things.
857 */
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100858 if (result.data_len != 0 && connection->data_cb) {
859 connection->data_cb(connection, msgb, result.data_len);
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200860 }
861
862 return 0;
863}
864
865/* Handle the release confirmed */
Holger Hans Peter Freyther18c5cad2010-01-29 04:19:56 +0100866static int _sccp_handle_connection_release_complete(struct msgb *msgb)
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200867{
Holger Hans Peter Freyther18c5cad2010-01-29 04:19:56 +0100868 struct sccp_parse_result result;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200869 struct sccp_connection *conn;
870
Holger Hans Peter Freyther18c5cad2010-01-29 04:19:56 +0100871 if (_sccp_parse_connection_release_complete(msgb, &result) != 0)
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200872 return -1;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200873
874 /* find the connection */
875 llist_for_each_entry(conn, &sccp_connections, list) {
876 if (conn->data_cb
877 && memcmp(&conn->source_local_reference,
Holger Hans Peter Freyther18c5cad2010-01-29 04:19:56 +0100878 result.destination_local_reference,
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200879 sizeof(conn->source_local_reference)) == 0
880 && memcmp(&conn->destination_local_reference,
Holger Hans Peter Freyther18c5cad2010-01-29 04:19:56 +0100881 result.source_local_reference,
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200882 sizeof(conn->destination_local_reference)) == 0) {
883 goto found;
884 }
885 }
886
887
888 DEBUGP(DSCCP, "Release complete of unknown connection\n");
889 return -1;
890
891found:
892 llist_del(&conn->list);
893 _sccp_set_connection_state(conn, SCCP_CONNECTION_STATE_RELEASE_COMPLETE);
894 return 0;
895}
896
897/* Handle the Data Form 1 message */
898static int _sccp_handle_connection_dt1(struct msgb *data)
899{
900 static int variable_offset = offsetof(struct sccp_data_form1, variable_start);
901 static int header_size = sizeof(struct sccp_data_form1);
902
903 struct sccp_data_form1 *dt1 = (struct sccp_data_form1 *)data->l2h;
904 struct sccp_connection *conn;
905 int size;
906
907 /* we don't have enough size for the struct */
908 if (msgb_l2len(data) < header_size) {
909 DEBUGP(DSCCP, "msgb > header_size %u %u\n",
910 msgb_l2len(data), header_size);
911 return -1;
912 }
913
914 if (dt1->segmenting != 0) {
915 DEBUGP(DSCCP, "This packet has segmenting, not supported: %d\n", dt1->segmenting);
916 return -1;
917 }
918
919 /* lookup if we have a connection with the given reference */
920 llist_for_each_entry(conn, &sccp_connections, list) {
921 if (conn->data_cb
922 && memcmp(&conn->source_local_reference,
923 &dt1->destination_local_reference,
924 sizeof(conn->source_local_reference)) == 0) {
925
926 /* some more size checks in here */
927 if (msgb_l2len(data) < variable_offset + dt1->variable_start + 1) {
928 DEBUGP(DSCCP, "Not enough space for variable start: %u %u\n",
929 msgb_l2len(data), dt1->variable_start);
930 return -1;
931 }
932
933 size = data->l2h[variable_offset + dt1->variable_start];
934 data->l3h = &data->l2h[dt1->variable_start + variable_offset + 1];
935
936 if (msgb_l3len(data) < size) {
937 DEBUGP(DSCCP, "Not enough room for the payload: %u %u\n",
938 msgb_l3len(data), size);
939 return -1;
940 }
941
942 conn->data_cb(conn, data, size);
943 return 0;
944 }
945 }
946
947 DEBUGP(DSCCP, "No connection found for dt1 data\n");
948 return -1;
949}
950
951/* confirm a connection release */
952static int _sccp_send_connection_release_complete(struct sccp_connection *connection)
953{
954 struct msgb *msgb;
955 struct sccp_connection_release_complete *rlc;
956 int ret;
957
958 msgb = msgb_alloc_headroom(SCCP_MSG_SIZE,
959 SCCP_MSG_HEADROOM, "sccp rlc");
960 msgb->l2h = &msgb->data[0];
961
962 rlc = (struct sccp_connection_release_complete *) msgb_put(msgb, sizeof(*rlc));
963 rlc->type = SCCP_MSG_TYPE_RLC;
964 memcpy(&rlc->destination_local_reference,
965 &connection->destination_local_reference, sizeof(struct sccp_source_reference));
966 memcpy(&rlc->source_local_reference,
967 &connection->source_local_reference, sizeof(struct sccp_source_reference));
968
969 ret = _send_msg(msgb);
970 msgb_free(msgb);
971
972 /*
973 * Remove from the list of active connections and set the state. User code
974 * should now free the entry.
975 */
976 llist_del(&connection->list);
977 _sccp_set_connection_state(connection, SCCP_CONNECTION_STATE_RELEASE_COMPLETE);
978
979 return ret;
980}
981
982/* connection released, send a released confirm */
Holger Hans Peter Freyther88fe6ee2010-01-29 03:49:32 +0100983static int _sccp_handle_connection_released(struct msgb *msgb)
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200984{
Holger Hans Peter Freyther88fe6ee2010-01-29 03:49:32 +0100985 struct sccp_parse_result result;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200986 struct sccp_connection *conn;
987
Holger Hans Peter Freyther88fe6ee2010-01-29 03:49:32 +0100988 if (_sccp_parse_connection_released(msgb, &result) == -1)
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200989 return -1;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200990
991 /* lookup if we have a connection with the given reference */
992 llist_for_each_entry(conn, &sccp_connections, list) {
993 if (conn->data_cb
994 && memcmp(&conn->source_local_reference,
Holger Hans Peter Freyther88fe6ee2010-01-29 03:49:32 +0100995 result.destination_local_reference,
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200996 sizeof(conn->source_local_reference)) == 0
997 && memcmp(&conn->destination_local_reference,
Holger Hans Peter Freyther88fe6ee2010-01-29 03:49:32 +0100998 result.source_local_reference,
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200999 sizeof(conn->destination_local_reference)) == 0) {
1000 goto found;
1001 }
1002 }
1003
1004
1005 DEBUGP(DSCCP, "Unknown connection was released.\n");
1006 return -1;
1007
1008 /* we have found a connection */
1009found:
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001010 /* optional data */
Holger Hans Peter Freyther88fe6ee2010-01-29 03:49:32 +01001011 if (result.data_len != 0 && conn->data_cb) {
1012 conn->data_cb(conn, msgb, result.data_len);
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001013 }
1014
1015 /* generate a response */
1016 if (_sccp_send_connection_release_complete(conn) != 0) {
1017 DEBUGP(DSCCP, "Sending release confirmed failed\n");
1018 return -1;
1019 }
1020
1021 return 0;
1022}
1023
1024static int _sccp_handle_connection_refused(struct msgb *msgb)
1025{
Holger Hans Peter Freytherfe5de4e2010-01-29 03:58:12 +01001026 struct sccp_parse_result result;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001027 struct sccp_connection *conn;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001028
Holger Hans Peter Freytherfe5de4e2010-01-29 03:58:12 +01001029 if (_sccp_parse_connection_refused(msgb, &result) != 0)
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001030 return -1;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001031
1032 /* lookup if we have a connection with the given reference */
1033 llist_for_each_entry(conn, &sccp_connections, list) {
1034 if (conn->incoming == 0 && conn->data_cb
1035 && memcmp(&conn->source_local_reference,
Holger Hans Peter Freytherfe5de4e2010-01-29 03:58:12 +01001036 result.destination_local_reference,
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001037 sizeof(conn->source_local_reference)) == 0) {
1038 goto found;
1039 }
1040 }
1041
1042 DEBUGP(DSCCP, "Refused but no connection found\n");
1043 return -1;
1044
1045found:
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001046 /* optional data */
Holger Hans Peter Freytherfe5de4e2010-01-29 03:58:12 +01001047 if (result.data_len != 0 && conn->data_cb) {
1048 conn->data_cb(conn, msgb, result.data_len);
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001049 }
1050
1051
1052 llist_del(&conn->list);
1053 _sccp_set_connection_state(conn, SCCP_CONNECTION_STATE_REFUSED);
1054 return 0;
1055}
1056
1057static int _sccp_handle_connection_confirm(struct msgb *msgb)
1058{
Holger Hans Peter Freytherca1d1d12010-01-29 04:03:00 +01001059 struct sccp_parse_result result;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001060 struct sccp_connection *conn;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001061
Holger Hans Peter Freytherca1d1d12010-01-29 04:03:00 +01001062 if (_sccp_parse_connection_confirm(msgb, &result) != 0)
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001063 return -1;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001064
1065 /* lookup if we have a connection with the given reference */
1066 llist_for_each_entry(conn, &sccp_connections, list) {
1067 if (conn->incoming == 0 && conn->data_cb
1068 && memcmp(&conn->source_local_reference,
Holger Hans Peter Freytherca1d1d12010-01-29 04:03:00 +01001069 result.destination_local_reference,
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001070 sizeof(conn->source_local_reference)) == 0) {
1071 goto found;
1072 }
1073 }
1074
1075 DEBUGP(DSCCP, "Confirmed but no connection found\n");
1076 return -1;
1077
1078found:
1079 /* copy the addresses of the connection */
Holger Hans Peter Freytherca1d1d12010-01-29 04:03:00 +01001080 conn->destination_local_reference = *result.source_local_reference;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001081 _sccp_set_connection_state(conn, SCCP_CONNECTION_STATE_ESTABLISHED);
1082
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001083 /* optional data */
Holger Hans Peter Freytherca1d1d12010-01-29 04:03:00 +01001084 if (result.data_len != 0 && conn->data_cb) {
1085 conn->data_cb(conn, msgb, result.data_len);
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001086 }
1087
1088 return 0;
1089}
1090
1091
1092int sccp_system_init(int (*outgoing)(struct msgb *data, void *ctx), void *ctx)
1093{
1094 sccp_system.write_data = outgoing;
1095 sccp_system.write_context = ctx;
1096
1097 return 0;
1098}
1099
1100/* oh my god a real SCCP packet. need to dispatch it now */
1101int sccp_system_incoming(struct msgb *msgb)
1102{
1103 if (msgb_l2len(msgb) < 1 ) {
1104 DEBUGP(DSCCP, "Too short packet\n");
1105 return -1;
1106 }
1107
1108 int type = msgb->l2h[0];
1109
1110 switch(type) {
1111 case SCCP_MSG_TYPE_CR:
1112 return _sccp_handle_connection_request(msgb);
1113 break;
1114 case SCCP_MSG_TYPE_RLSD:
1115 return _sccp_handle_connection_released(msgb);
1116 break;
1117 case SCCP_MSG_TYPE_CREF:
1118 return _sccp_handle_connection_refused(msgb);
1119 break;
1120 case SCCP_MSG_TYPE_CC:
1121 return _sccp_handle_connection_confirm(msgb);
1122 break;
1123 case SCCP_MSG_TYPE_RLC:
1124 return _sccp_handle_connection_release_complete(msgb);
1125 break;
1126 case SCCP_MSG_TYPE_DT1:
1127 return _sccp_handle_connection_dt1(msgb);
1128 break;
1129 case SCCP_MSG_TYPE_UDT:
1130 return _sccp_handle_read(msgb);
1131 break;
1132 default:
1133 DEBUGP(DSCCP, "unimplemented msg type: %d\n", type);
1134 };
1135
1136 return -1;
1137}
1138
1139/* create a packet from the data */
1140int sccp_connection_write(struct sccp_connection *connection, struct msgb *data)
1141{
1142 if (connection->connection_state < SCCP_CONNECTION_STATE_CONFIRM
1143 || connection->connection_state > SCCP_CONNECTION_STATE_ESTABLISHED) {
1144 DEBUGP(DSCCP, "sccp_connection_write: Wrong connection state: %p %d\n",
1145 connection, connection->connection_state);
1146 return -1;
1147 }
1148
1149 return _sccp_send_connection_data(connection, data);
1150}
1151
Holger Hans Peter Freyther3b9516e2009-11-18 22:11:28 +01001152/*
1153 * Send a Inactivity Test message. The owner of the connection
1154 * should start a timer and call this method regularily. Calling
1155 * this every 60 seconds should be good enough.
1156 */
1157int sccp_connection_send_it(struct sccp_connection *connection)
1158{
1159 if (connection->connection_state < SCCP_CONNECTION_STATE_CONFIRM
1160 || connection->connection_state > SCCP_CONNECTION_STATE_ESTABLISHED) {
1161 DEBUGP(DSCCP, "sccp_connection_write: Wrong connection state: %p %d\n",
1162 connection, connection->connection_state);
1163 return -1;
1164 }
1165
1166 return _sccp_send_connection_it(connection);
1167}
1168
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001169/* send a connection release and wait for the connection released */
1170int sccp_connection_close(struct sccp_connection *connection, int cause)
1171{
1172 if (connection->connection_state < SCCP_CONNECTION_STATE_CONFIRM
1173 || connection->connection_state > SCCP_CONNECTION_STATE_ESTABLISHED) {
1174 DEBUGPC(DSCCP, "Can not close the connection. It was never opened: %p %d\n",
1175 connection, connection->connection_state);
1176 return -1;
1177 }
1178
1179 return _sccp_send_connection_released(connection, cause);
1180}
1181
1182int sccp_connection_free(struct sccp_connection *connection)
1183{
1184 if (connection->connection_state > SCCP_CONNECTION_STATE_NONE
1185 && connection->connection_state < SCCP_CONNECTION_STATE_RELEASE_COMPLETE) {
1186 DEBUGP(DSCCP, "The connection needs to be released before it is freed");
1187 return -1;
1188 }
1189
1190 talloc_free(connection);
1191 return 0;
1192}
1193
1194struct sccp_connection *sccp_connection_socket(void)
1195{
1196 return talloc_zero(tall_sccp_ctx, struct sccp_connection);
1197}
1198
1199int sccp_connection_connect(struct sccp_connection *conn,
1200 const struct sockaddr_sccp *local,
1201 struct msgb *data)
1202{
1203 return _sccp_send_connection_request(conn, local, data);
1204}
1205
1206int sccp_connection_set_incoming(const struct sockaddr_sccp *sock,
1207 int (*accept_cb)(struct sccp_connection *, void *), void *context)
1208{
1209 struct sccp_data_callback *cb;
1210
1211 if (!sock)
1212 return -2;
1213
1214 cb = _find_ssn(sock->sccp_ssn);
1215 if (!cb)
1216 return -1;
1217
1218 cb->accept_cb = accept_cb;
1219 cb->accept_context = context;
1220 return 0;
1221}
1222
1223int sccp_write(struct msgb *data, const struct sockaddr_sccp *in,
1224 const struct sockaddr_sccp *out, int class)
1225{
1226 return _sccp_send_data(class, in, out, data);
1227}
1228
1229int sccp_set_read(const struct sockaddr_sccp *sock,
1230 int (*read_cb)(struct msgb *, unsigned int, void *), void *context)
1231{
1232 struct sccp_data_callback *cb;
1233
1234 if (!sock)
1235 return -2;
1236
1237 cb = _find_ssn(sock->sccp_ssn);
1238 if (!cb)
1239 return -1;
1240
1241 cb->read_cb = read_cb;
1242 cb->read_context = context;
1243 return 0;
1244}
1245
1246static_assert(sizeof(struct sccp_source_reference) <= sizeof(u_int32_t), enough_space);
1247
1248u_int32_t sccp_src_ref_to_int(struct sccp_source_reference *ref)
1249{
1250 u_int32_t src_ref = 0;
1251 memcpy(&src_ref, ref, sizeof(*ref));
1252 return src_ref;
1253}
1254
1255struct sccp_source_reference sccp_src_ref_from_int(u_int32_t int_ref)
1256{
1257 struct sccp_source_reference ref;
1258 memcpy(&ref, &int_ref, sizeof(ref));
1259 return ref;
1260}
1261
Holger Hans Peter Freythera692fbc2010-01-13 09:55:43 +01001262int sccp_determine_msg_type(struct msgb *msg)
1263{
1264 if (msgb_l2len(msg) < 1)
1265 return -1;
1266
1267 return msg->l2h[0];
1268}
1269
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001270static __attribute__((constructor)) void on_dso_load(void)
1271{
1272 tall_sccp_ctx = talloc_named_const(NULL, 1, "sccp");
1273}
1274
1275static __attribute__((destructor)) void on_dso_unload(void)
1276{
1277 talloc_report_full(tall_sccp_ctx, stderr);
1278}