blob: e435ecf2399633c3dbfc9d8bc4457abb0271de7b [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
Holger Hans Peter Freytheref845392010-01-29 04:31:00 +0100380int _sccp_parse_connection_dt1(struct msgb *msgb, struct sccp_parse_result *result)
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +0100381{
Holger Hans Peter Freytheref845392010-01-29 04:31:00 +0100382 static int header_size = sizeof(struct sccp_data_form1);
383 static int variable_offset = offsetof(struct sccp_data_form1, variable_start);
384
385 struct sccp_data_form1 *dt1 = (struct sccp_data_form1 *)msgb->l2h;
386
387 /* we don't have enough size for the struct */
388 if (msgb_l2len(msgb) < header_size) {
389 DEBUGP(DSCCP, "msgb > header_size %u %u\n",
390 msgb_l2len(msgb), header_size);
391 return -1;
392 }
393
394 if (dt1->segmenting != 0) {
395 DEBUGP(DSCCP, "This packet has segmenting, not supported: %d\n", dt1->segmenting);
396 return -1;
397 }
398
399 result->destination_local_reference = &dt1->destination_local_reference;
400
401 /* some more size checks in here */
402 if (msgb_l2len(msgb) < variable_offset + dt1->variable_start + 1) {
403 DEBUGP(DSCCP, "Not enough space for variable start: %u %u\n",
404 msgb_l2len(msgb), dt1->variable_start);
405 return -1;
406 }
407
408 result->data_len = msgb->l2h[variable_offset + dt1->variable_start];
409 msgb->l3h = &msgb->l2h[dt1->variable_start + variable_offset + 1];
410
411 if (msgb_l3len(msgb) < result->data_len) {
412 DEBUGP(DSCCP, "Not enough room for the payload: %u %u\n",
413 msgb_l3len(msgb), result->data_len);
414 return -1;
415 }
416
417 return 0;
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +0100418}
419
420int _sccp_parse_udt(struct msgb *msgb, struct sccp_parse_result *result)
421{
422 static const u_int32_t header_size = sizeof(struct sccp_data_unitdata);
423 static const u_int32_t called_offset = offsetof(struct sccp_data_unitdata, variable_called);
424 static const u_int32_t calling_offset = offsetof(struct sccp_data_unitdata, variable_calling);
425 static const u_int32_t data_offset = offsetof(struct sccp_data_unitdata, variable_data);
426
427 struct sccp_data_unitdata *udt = (struct sccp_data_unitdata *)msgb->l2h;
428
429 if (msgb_l2len(msgb) < header_size) {
430 DEBUGP(DSCCP, "msgb < header_size %u %u\n",
431 msgb_l2len(msgb), header_size);
432 return -1;
433 }
434
435 /* copy out the calling and called address. Add the off */
436 if (copy_address(&result->called, called_offset + udt->variable_called, msgb) != 0)
437 return -1;
438
439 if (check_address(&result->called) != 0) {
440 DEBUGP(DSCCP, "Invalid called address according to 08.06: 0x%x 0x%x\n",
441 *(u_int8_t *)&result->called.address, result->called.ssn);
442 return -1;
443 }
444
445 if (copy_address(&result->calling, calling_offset + udt->variable_calling, msgb) != 0)
446 return -1;
447
448 if (check_address(&result->calling) != 0) {
449 DEBUGP(DSCCP, "Invalid called address according to 08.06: 0x%x 0x%x\n",
450 *(u_int8_t *)&result->called.address, result->called.ssn);
451 }
452
453 /* we don't have enough size for the data */
454 if (msgb_l2len(msgb) < data_offset + udt->variable_data + 1) {
455 DEBUGP(DSCCP, "msgb < header + offset %u %u %u\n",
456 msgb_l2len(msgb), header_size, udt->variable_data);
457 return -1;
458 }
459
460
461 msgb->l3h = &udt->data[udt->variable_data];
462
463 if (msgb_l3len(msgb) != msgb->l3h[-1]) {
464 DEBUGP(DSCCP, "msgb is truncated %u %u\n",
465 msgb_l3len(msgb), msgb->l3h[-1]);
466 return -1;
467 }
468
469 return 0;
470}
471
472
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200473/*
474 * Send UDT. Currently we have a fixed address...
475 */
476static int _sccp_send_data(int class, const struct sockaddr_sccp *in,
477 const struct sockaddr_sccp *out, struct msgb *payload)
478{
479 struct sccp_data_unitdata *udt;
480 u_int8_t *data;
481 int ret;
482
483 if (msgb_l3len(payload) > 256) {
484 DEBUGP(DSCCP, "The payload is too big for one udt\n");
485 return -1;
486 }
487
488 struct msgb *msg = msgb_alloc_headroom(SCCP_MSG_SIZE,
489 SCCP_MSG_HEADROOM, "sccp: udt");
490 msg->l2h = &msg->data[0];
491 udt = (struct sccp_data_unitdata *)msgb_put(msg, sizeof(*udt));
492
493 udt->type = SCCP_MSG_TYPE_UDT;
494 udt->proto_class = class;
495 udt->variable_called = 3;
496 udt->variable_calling = 5;
497 udt->variable_data = 7;
498
499 /* for variable data we start with a size and the data */
500 data = msgb_put(msg, 1 + 2);
501 data[0] = 2;
502 data[1] = 0x42;
503 data[2] = out->sccp_ssn;
504
505 data = msgb_put(msg, 1 + 2);
506 data[0] = 2;
507 data[1] = 0x42;
508 data[2] = in->sccp_ssn;
509
510 /* copy the payload */
511 data = msgb_put(msg, 1 + msgb_l3len(payload));
512 data[0] = msgb_l3len(payload);
513 memcpy(&data[1], payload->l3h, msgb_l3len(payload));
514
515 ret = _send_msg(msg);
516 msgb_free(msg);
517
518 return ret;
519}
520
521static int _sccp_handle_read(struct msgb *msgb)
522{
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200523 struct sccp_data_callback *cb;
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +0100524 struct sccp_parse_result result;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200525
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +0100526 if (_sccp_parse_udt(msgb, &result) != 0)
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200527 return -1;
528
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +0100529 cb = _find_ssn(result.called.ssn);
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200530 if (!cb || !cb->read_cb) {
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +0100531 DEBUGP(DSCCP, "No routing for UDT for called SSN: %u\n", result.called.ssn);
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200532 return -1;
533 }
534
535 /* sanity check */
536 return cb->read_cb(msgb, msgb_l3len(msgb), cb->read_context);
537}
538
539/*
540 * handle connection orientated methods
541 */
542static int source_local_reference_is_free(struct sccp_source_reference *reference)
543{
544 struct sccp_connection *connection;
545
546 llist_for_each_entry(connection, &sccp_connections, list) {
547 if (memcmp(reference, &connection->source_local_reference, sizeof(*reference)) == 0)
548 return -1;
549 }
550
551 return 0;
552}
553
554static int destination_local_reference_is_free(struct sccp_source_reference *reference)
555{
556 struct sccp_connection *connection;
557
558 llist_for_each_entry(connection, &sccp_connections, list) {
559 if (memcmp(reference, &connection->destination_local_reference, sizeof(*reference)) == 0)
560 return -1;
561 }
562
563 return 0;
564}
565
566static int assign_source_local_reference(struct sccp_connection *connection)
567{
568 static u_int32_t last_ref = 0x30000;
569 int wrapped = 0;
570
571 do {
572 struct sccp_source_reference reference;
573 reference.octet1 = (last_ref >> 0) & 0xff;
574 reference.octet2 = (last_ref >> 8) & 0xff;
575 reference.octet3 = (last_ref >> 16) & 0xff;
576
577 ++last_ref;
578 /* do not use the reversed word and wrap around */
579 if ((last_ref & 0x00FFFFFF) == 0x00FFFFFF) {
580 DEBUGP(DSCCP, "Wrapped searching for a free code\n");
581 last_ref = 0;
582 ++wrapped;
583 }
584
585 if (source_local_reference_is_free(&reference) == 0) {
586 connection->source_local_reference = reference;
587 return 0;
588 }
589 } while (wrapped != 2);
590
591 DEBUGP(DSCCP, "Finding a free reference failed\n");
592 return -1;
593}
594
595static void _sccp_set_connection_state(struct sccp_connection *connection, int new_state)
596{
597 int old_state = connection->connection_state;
598
599 connection->connection_state = new_state;
600 if (connection->state_cb)
601 connection->state_cb(connection, old_state);
602}
603
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100604static int _sccp_send_refuse(struct sccp_source_reference *src_ref, int cause)
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200605{
606 struct msgb *msgb;
607 struct sccp_connection_refused *ref;
608 u_int8_t *data;
609 int ret;
610
611 msgb = msgb_alloc_headroom(SCCP_MSG_SIZE,
612 SCCP_MSG_HEADROOM, "sccp ref");
613 msgb->l2h = &msgb->data[0];
614
615 ref = (struct sccp_connection_refused *) msgb_put(msgb, sizeof(*ref));
616 ref->type = SCCP_MSG_TYPE_CREF;
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100617 memcpy(&ref->destination_local_reference, src_ref,
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200618 sizeof(struct sccp_source_reference));
619 ref->cause = cause;
620 ref->optional_start = 1;
621
622 data = msgb_put(msgb, 1);
623 data[0] = SCCP_PNC_END_OF_OPTIONAL;
624
625 ret = _send_msg(msgb);
626 msgb_free(msgb);
627 return ret;
628}
629
630static int _sccp_send_connection_confirm(struct sccp_connection *connection)
631{
632 struct msgb *response;
633 struct sccp_connection_confirm *confirm;
634 u_int8_t *optional_data;
635 int ret;
636
637 if (assign_source_local_reference(connection) != 0)
638 return -1;
639
640 response = msgb_alloc_headroom(SCCP_MSG_SIZE,
641 SCCP_MSG_HEADROOM, "sccp confirm");
642 response->l2h = &response->data[0];
643
644 confirm = (struct sccp_connection_confirm *) msgb_put(response, sizeof(*confirm));
645
646 confirm->type = SCCP_MSG_TYPE_CC;
647 memcpy(&confirm->destination_local_reference,
648 &connection->destination_local_reference,
649 sizeof(connection->destination_local_reference));
650 memcpy(&confirm->source_local_reference,
651 &connection->source_local_reference,
652 sizeof(connection->source_local_reference));
653 confirm->proto_class = 2;
654 confirm->optional_start = 1;
655
656 optional_data = (u_int8_t *) msgb_put(response, 1);
657 optional_data[0] = SCCP_PNC_END_OF_OPTIONAL;
658
659 ret = _send_msg(response);
660 msgb_free(response);
661
662 _sccp_set_connection_state(connection, SCCP_CONNECTION_STATE_ESTABLISHED);
663 return ret;
664}
665
666static int _sccp_send_connection_request(struct sccp_connection *connection,
667 const struct sockaddr_sccp *called, struct msgb *msg)
668{
669 struct msgb *request;
670 struct sccp_connection_request *req;
671 u_int8_t *data;
672 u_int8_t extra_size = 3 + 1;
673 int ret;
674
675
676 if (msg && (msgb_l3len(msg) < 3 || msgb_l3len(msg) > 130)) {
677 DEBUGP(DSCCP, "Invalid amount of data... %d\n", msgb_l3len(msg));
678 return -1;
679 }
680
681 /* try to find a id */
682 if (assign_source_local_reference(connection) != 0) {
683 DEBUGP(DSCCP, "Assigning a local reference failed.\n");
684 _sccp_set_connection_state(connection, SCCP_CONNECTION_STATE_SETUP_ERROR);
685 return -1;
686 }
687
688
689 if (msg)
690 extra_size += 2 + msgb_l3len(msg);
691 request = msgb_alloc_headroom(SCCP_MSG_SIZE,
692 SCCP_MSG_HEADROOM, "sccp connection request");
693 request->l2h = &request->data[0];
694 req = (struct sccp_connection_request *) msgb_put(request, sizeof(*req));
695
696 req->type = SCCP_MSG_TYPE_CR;
697 memcpy(&req->source_local_reference, &connection->source_local_reference,
698 sizeof(connection->source_local_reference));
699 req->proto_class = 2;
700 req->variable_called = 2;
701 req->optional_start = 4;
702
703 /* write the called party address */
704 data = msgb_put(request, 1 + 2);
705 data[0] = 2;
706 data[1] = 0x42;
707 data[2] = called->sccp_ssn;
708
709 /* write the payload */
710 if (msg) {
711 data = msgb_put(request, 2 + msgb_l3len(msg));
712 data[0] = SCCP_PNC_DATA;
713 data[1] = msgb_l3len(msg);
714 memcpy(&data[2], msg->l3h, msgb_l3len(msg));
715 }
716
717 data = msgb_put(request, 1);
718 data[0] = SCCP_PNC_END_OF_OPTIONAL;
719
720 llist_add_tail(&connection->list, &sccp_connections);
721 _sccp_set_connection_state(connection, SCCP_CONNECTION_STATE_REQUEST);
722
723 ret = _send_msg(request);
724 msgb_free(request);
725
726 return ret;
727}
728
729static int _sccp_send_connection_data(struct sccp_connection *conn, struct msgb *_data)
730{
731 struct msgb *msgb;
732 struct sccp_data_form1 *dt1;
733 u_int8_t *data;
734 int extra_size;
735 int ret;
736
737 if (msgb_l3len(_data) < 2 || msgb_l3len(_data) > 256) {
738 DEBUGP(DSCCP, "data size too big, segmenting unimplemented.\n");
739 return -1;
740 }
741
742 extra_size = 1 + msgb_l3len(_data);
743 msgb = msgb_alloc_headroom(SCCP_MSG_SIZE,
744 SCCP_MSG_HEADROOM, "sccp dt1");
745 msgb->l2h = &msgb->data[0];
746
747 dt1 = (struct sccp_data_form1 *) msgb_put(msgb, sizeof(*dt1));
748 dt1->type = SCCP_MSG_TYPE_DT1;
749 memcpy(&dt1->destination_local_reference, &conn->destination_local_reference,
750 sizeof(struct sccp_source_reference));
751 dt1->segmenting = 0;
752
753 /* copy the data */
754 dt1->variable_start = 1;
755 data = msgb_put(msgb, extra_size);
756 data[0] = extra_size - 1;
757 memcpy(&data[1], _data->l3h, extra_size - 1);
758
759 ret = _send_msg(msgb);
760 msgb_free(msgb);
761
762 return ret;
763}
764
Holger Hans Peter Freyther3b9516e2009-11-18 22:11:28 +0100765static int _sccp_send_connection_it(struct sccp_connection *conn)
766{
767 struct msgb *msgb;
768 struct sccp_data_it *it;
769 int ret;
770
771 msgb = msgb_alloc_headroom(SCCP_MSG_SIZE,
772 SCCP_MSG_HEADROOM, "sccp it");
773 msgb->l2h = &msgb->data[0];
774 it = (struct sccp_data_it *) msgb_put(msgb, sizeof(*it));
775 it->type = SCCP_MSG_TYPE_IT;
776 memcpy(&it->destination_local_reference, &conn->destination_local_reference,
777 sizeof(struct sccp_source_reference));
778 memcpy(&it->source_local_reference, &conn->source_local_reference,
779 sizeof(struct sccp_source_reference));
780
781 it->proto_class = 0x2;
782 it->sequencing[0] = it->sequencing[1] = 0;
783 it->credit = 0;
784
785 ret = _send_msg(msgb);
786 msgb_free(msgb);
787 return ret;
788}
789
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200790static int _sccp_send_connection_released(struct sccp_connection *conn, int cause)
791{
792 struct msgb *msg;
793 struct sccp_connection_released *rel;
794 u_int8_t *data;
795 int ret;
796
797 msg = msgb_alloc_headroom(SCCP_MSG_SIZE, SCCP_MSG_HEADROOM,
798 "sccp: connection released");
799 msg->l2h = &msg->data[0];
800 rel = (struct sccp_connection_released *) msgb_put(msg, sizeof(*rel));
801 rel->type = SCCP_MSG_TYPE_RLSD;
802 rel->release_cause = cause;
803
804 /* copy the source references */
805 memcpy(&rel->destination_local_reference, &conn->destination_local_reference,
806 sizeof(struct sccp_source_reference));
807 memcpy(&rel->source_local_reference, &conn->source_local_reference,
808 sizeof(struct sccp_source_reference));
809
810 data = msgb_put(msg, 1);
811 data[0] = SCCP_PNC_END_OF_OPTIONAL;
812
813 _sccp_set_connection_state(conn, SCCP_CONNECTION_STATE_RELEASE);
814 ret = _send_msg(msg);
815 msgb_free(msg);
816
817 return ret;
818}
819
820/*
821 * Open a connection. The following is going to happen:
822 *
823 * - Verify the packet, e.g. that we have no other connection
824 * that id.
825 * - Ask the user if he wants to accept the connection
826 * - Try to open the connection by assigning a source local reference
827 * and sending the packet
828 */
829static int _sccp_handle_connection_request(struct msgb *msgb)
830{
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100831 struct sccp_parse_result result;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200832
833 struct sccp_data_callback *cb;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200834 struct sccp_connection *connection;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200835
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100836 if (_sccp_parse_connection_request(msgb, &result) != 0)
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200837 return -1;
838
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100839 cb = _find_ssn(result.called.ssn);
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200840 if (!cb || !cb->accept_cb) {
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100841 DEBUGP(DSCCP, "No routing for CR for called SSN: %u\n", result.called.ssn);
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200842 return -1;
843 }
844
845 /* check if the system wants this connection */
846 connection = talloc_zero(tall_sccp_ctx, struct sccp_connection);
847 if (!connection) {
848 DEBUGP(DSCCP, "Allocation failed\n");
849 return -1;
850 }
851
852 /*
853 * sanity checks:
854 * - Is the source_local_reference in any other connection?
855 * then will call accept, assign a "destination" local reference
856 * and send a connection confirm, otherwise we will send a refuseed
857 * one....
858 */
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100859 if (destination_local_reference_is_free(result.source_local_reference) != 0) {
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200860 DEBUGP(DSCCP, "Need to reject connection with existing reference\n");
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100861 _sccp_send_refuse(result.source_local_reference, SCCP_REFUSAL_SCCP_FAILURE);
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200862 talloc_free(connection);
863 return -1;
864 }
865
866 connection->incoming = 1;
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100867 connection->destination_local_reference = *result.source_local_reference;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200868
869 if (cb->accept_cb(connection, cb->accept_context) != 0) {
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100870 _sccp_send_refuse(result.source_local_reference, SCCP_REFUSAL_END_USER_ORIGINATED);
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200871 _sccp_set_connection_state(connection, SCCP_CONNECTION_STATE_REFUSED);
872 talloc_free(connection);
873 return 0;
874 }
875
876
877 llist_add_tail(&connection->list, &sccp_connections);
878
879 if (_sccp_send_connection_confirm(connection) != 0) {
880 DEBUGP(DSCCP, "Sending confirm failed... no available source reference?\n");
881
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100882 _sccp_send_refuse(result.source_local_reference, SCCP_REFUSAL_SCCP_FAILURE);
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200883 _sccp_set_connection_state(connection, SCCP_CONNECTION_STATE_REFUSED);
884 llist_del(&connection->list);
885 talloc_free(connection);
886
887 return -1;
888 }
889
890 /*
891 * If we have data let us forward things.
892 */
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100893 if (result.data_len != 0 && connection->data_cb) {
894 connection->data_cb(connection, msgb, result.data_len);
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200895 }
896
897 return 0;
898}
899
900/* Handle the release confirmed */
Holger Hans Peter Freyther18c5cad2010-01-29 04:19:56 +0100901static int _sccp_handle_connection_release_complete(struct msgb *msgb)
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200902{
Holger Hans Peter Freyther18c5cad2010-01-29 04:19:56 +0100903 struct sccp_parse_result result;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200904 struct sccp_connection *conn;
905
Holger Hans Peter Freyther18c5cad2010-01-29 04:19:56 +0100906 if (_sccp_parse_connection_release_complete(msgb, &result) != 0)
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200907 return -1;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200908
909 /* find the connection */
910 llist_for_each_entry(conn, &sccp_connections, list) {
911 if (conn->data_cb
912 && memcmp(&conn->source_local_reference,
Holger Hans Peter Freyther18c5cad2010-01-29 04:19:56 +0100913 result.destination_local_reference,
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200914 sizeof(conn->source_local_reference)) == 0
915 && memcmp(&conn->destination_local_reference,
Holger Hans Peter Freyther18c5cad2010-01-29 04:19:56 +0100916 result.source_local_reference,
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200917 sizeof(conn->destination_local_reference)) == 0) {
918 goto found;
919 }
920 }
921
922
923 DEBUGP(DSCCP, "Release complete of unknown connection\n");
924 return -1;
925
926found:
927 llist_del(&conn->list);
928 _sccp_set_connection_state(conn, SCCP_CONNECTION_STATE_RELEASE_COMPLETE);
929 return 0;
930}
931
932/* Handle the Data Form 1 message */
Holger Hans Peter Freytheref845392010-01-29 04:31:00 +0100933static int _sccp_handle_connection_dt1(struct msgb *msgb)
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200934{
Holger Hans Peter Freytheref845392010-01-29 04:31:00 +0100935 struct sccp_parse_result result;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200936 struct sccp_connection *conn;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200937
Holger Hans Peter Freytheref845392010-01-29 04:31:00 +0100938 if (_sccp_parse_connection_dt1(msgb, &result) != 0)
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200939 return -1;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200940
941 /* lookup if we have a connection with the given reference */
942 llist_for_each_entry(conn, &sccp_connections, list) {
943 if (conn->data_cb
944 && memcmp(&conn->source_local_reference,
Holger Hans Peter Freytheref845392010-01-29 04:31:00 +0100945 result.destination_local_reference,
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200946 sizeof(conn->source_local_reference)) == 0) {
Holger Hans Peter Freytheref845392010-01-29 04:31:00 +0100947 goto found;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200948 }
949 }
950
951 DEBUGP(DSCCP, "No connection found for dt1 data\n");
952 return -1;
Holger Hans Peter Freytheref845392010-01-29 04:31:00 +0100953
954found:
955 conn->data_cb(conn, msgb, result.data_len);
956 return 0;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200957}
958
959/* confirm a connection release */
960static int _sccp_send_connection_release_complete(struct sccp_connection *connection)
961{
962 struct msgb *msgb;
963 struct sccp_connection_release_complete *rlc;
964 int ret;
965
966 msgb = msgb_alloc_headroom(SCCP_MSG_SIZE,
967 SCCP_MSG_HEADROOM, "sccp rlc");
968 msgb->l2h = &msgb->data[0];
969
970 rlc = (struct sccp_connection_release_complete *) msgb_put(msgb, sizeof(*rlc));
971 rlc->type = SCCP_MSG_TYPE_RLC;
972 memcpy(&rlc->destination_local_reference,
973 &connection->destination_local_reference, sizeof(struct sccp_source_reference));
974 memcpy(&rlc->source_local_reference,
975 &connection->source_local_reference, sizeof(struct sccp_source_reference));
976
977 ret = _send_msg(msgb);
978 msgb_free(msgb);
979
980 /*
981 * Remove from the list of active connections and set the state. User code
982 * should now free the entry.
983 */
984 llist_del(&connection->list);
985 _sccp_set_connection_state(connection, SCCP_CONNECTION_STATE_RELEASE_COMPLETE);
986
987 return ret;
988}
989
990/* connection released, send a released confirm */
Holger Hans Peter Freyther88fe6ee2010-01-29 03:49:32 +0100991static int _sccp_handle_connection_released(struct msgb *msgb)
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200992{
Holger Hans Peter Freyther88fe6ee2010-01-29 03:49:32 +0100993 struct sccp_parse_result result;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200994 struct sccp_connection *conn;
995
Holger Hans Peter Freyther88fe6ee2010-01-29 03:49:32 +0100996 if (_sccp_parse_connection_released(msgb, &result) == -1)
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200997 return -1;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200998
999 /* lookup if we have a connection with the given reference */
1000 llist_for_each_entry(conn, &sccp_connections, list) {
1001 if (conn->data_cb
1002 && memcmp(&conn->source_local_reference,
Holger Hans Peter Freyther88fe6ee2010-01-29 03:49:32 +01001003 result.destination_local_reference,
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001004 sizeof(conn->source_local_reference)) == 0
1005 && memcmp(&conn->destination_local_reference,
Holger Hans Peter Freyther88fe6ee2010-01-29 03:49:32 +01001006 result.source_local_reference,
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001007 sizeof(conn->destination_local_reference)) == 0) {
1008 goto found;
1009 }
1010 }
1011
1012
1013 DEBUGP(DSCCP, "Unknown connection was released.\n");
1014 return -1;
1015
1016 /* we have found a connection */
1017found:
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001018 /* optional data */
Holger Hans Peter Freyther88fe6ee2010-01-29 03:49:32 +01001019 if (result.data_len != 0 && conn->data_cb) {
1020 conn->data_cb(conn, msgb, result.data_len);
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001021 }
1022
1023 /* generate a response */
1024 if (_sccp_send_connection_release_complete(conn) != 0) {
1025 DEBUGP(DSCCP, "Sending release confirmed failed\n");
1026 return -1;
1027 }
1028
1029 return 0;
1030}
1031
1032static int _sccp_handle_connection_refused(struct msgb *msgb)
1033{
Holger Hans Peter Freytherfe5de4e2010-01-29 03:58:12 +01001034 struct sccp_parse_result result;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001035 struct sccp_connection *conn;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001036
Holger Hans Peter Freytherfe5de4e2010-01-29 03:58:12 +01001037 if (_sccp_parse_connection_refused(msgb, &result) != 0)
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001038 return -1;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001039
1040 /* lookup if we have a connection with the given reference */
1041 llist_for_each_entry(conn, &sccp_connections, list) {
1042 if (conn->incoming == 0 && conn->data_cb
1043 && memcmp(&conn->source_local_reference,
Holger Hans Peter Freytherfe5de4e2010-01-29 03:58:12 +01001044 result.destination_local_reference,
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001045 sizeof(conn->source_local_reference)) == 0) {
1046 goto found;
1047 }
1048 }
1049
1050 DEBUGP(DSCCP, "Refused but no connection found\n");
1051 return -1;
1052
1053found:
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001054 /* optional data */
Holger Hans Peter Freytherfe5de4e2010-01-29 03:58:12 +01001055 if (result.data_len != 0 && conn->data_cb) {
1056 conn->data_cb(conn, msgb, result.data_len);
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001057 }
1058
1059
1060 llist_del(&conn->list);
1061 _sccp_set_connection_state(conn, SCCP_CONNECTION_STATE_REFUSED);
1062 return 0;
1063}
1064
1065static int _sccp_handle_connection_confirm(struct msgb *msgb)
1066{
Holger Hans Peter Freytherca1d1d12010-01-29 04:03:00 +01001067 struct sccp_parse_result result;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001068 struct sccp_connection *conn;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001069
Holger Hans Peter Freytherca1d1d12010-01-29 04:03:00 +01001070 if (_sccp_parse_connection_confirm(msgb, &result) != 0)
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001071 return -1;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001072
1073 /* lookup if we have a connection with the given reference */
1074 llist_for_each_entry(conn, &sccp_connections, list) {
1075 if (conn->incoming == 0 && conn->data_cb
1076 && memcmp(&conn->source_local_reference,
Holger Hans Peter Freytherca1d1d12010-01-29 04:03:00 +01001077 result.destination_local_reference,
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001078 sizeof(conn->source_local_reference)) == 0) {
1079 goto found;
1080 }
1081 }
1082
1083 DEBUGP(DSCCP, "Confirmed but no connection found\n");
1084 return -1;
1085
1086found:
1087 /* copy the addresses of the connection */
Holger Hans Peter Freytherca1d1d12010-01-29 04:03:00 +01001088 conn->destination_local_reference = *result.source_local_reference;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001089 _sccp_set_connection_state(conn, SCCP_CONNECTION_STATE_ESTABLISHED);
1090
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001091 /* optional data */
Holger Hans Peter Freytherca1d1d12010-01-29 04:03:00 +01001092 if (result.data_len != 0 && conn->data_cb) {
1093 conn->data_cb(conn, msgb, result.data_len);
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001094 }
1095
1096 return 0;
1097}
1098
1099
1100int sccp_system_init(int (*outgoing)(struct msgb *data, void *ctx), void *ctx)
1101{
1102 sccp_system.write_data = outgoing;
1103 sccp_system.write_context = ctx;
1104
1105 return 0;
1106}
1107
1108/* oh my god a real SCCP packet. need to dispatch it now */
1109int sccp_system_incoming(struct msgb *msgb)
1110{
1111 if (msgb_l2len(msgb) < 1 ) {
1112 DEBUGP(DSCCP, "Too short packet\n");
1113 return -1;
1114 }
1115
1116 int type = msgb->l2h[0];
1117
1118 switch(type) {
1119 case SCCP_MSG_TYPE_CR:
1120 return _sccp_handle_connection_request(msgb);
1121 break;
1122 case SCCP_MSG_TYPE_RLSD:
1123 return _sccp_handle_connection_released(msgb);
1124 break;
1125 case SCCP_MSG_TYPE_CREF:
1126 return _sccp_handle_connection_refused(msgb);
1127 break;
1128 case SCCP_MSG_TYPE_CC:
1129 return _sccp_handle_connection_confirm(msgb);
1130 break;
1131 case SCCP_MSG_TYPE_RLC:
1132 return _sccp_handle_connection_release_complete(msgb);
1133 break;
1134 case SCCP_MSG_TYPE_DT1:
1135 return _sccp_handle_connection_dt1(msgb);
1136 break;
1137 case SCCP_MSG_TYPE_UDT:
1138 return _sccp_handle_read(msgb);
1139 break;
1140 default:
1141 DEBUGP(DSCCP, "unimplemented msg type: %d\n", type);
1142 };
1143
1144 return -1;
1145}
1146
1147/* create a packet from the data */
1148int sccp_connection_write(struct sccp_connection *connection, struct msgb *data)
1149{
1150 if (connection->connection_state < SCCP_CONNECTION_STATE_CONFIRM
1151 || connection->connection_state > SCCP_CONNECTION_STATE_ESTABLISHED) {
1152 DEBUGP(DSCCP, "sccp_connection_write: Wrong connection state: %p %d\n",
1153 connection, connection->connection_state);
1154 return -1;
1155 }
1156
1157 return _sccp_send_connection_data(connection, data);
1158}
1159
Holger Hans Peter Freyther3b9516e2009-11-18 22:11:28 +01001160/*
1161 * Send a Inactivity Test message. The owner of the connection
1162 * should start a timer and call this method regularily. Calling
1163 * this every 60 seconds should be good enough.
1164 */
1165int sccp_connection_send_it(struct sccp_connection *connection)
1166{
1167 if (connection->connection_state < SCCP_CONNECTION_STATE_CONFIRM
1168 || connection->connection_state > SCCP_CONNECTION_STATE_ESTABLISHED) {
1169 DEBUGP(DSCCP, "sccp_connection_write: Wrong connection state: %p %d\n",
1170 connection, connection->connection_state);
1171 return -1;
1172 }
1173
1174 return _sccp_send_connection_it(connection);
1175}
1176
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001177/* send a connection release and wait for the connection released */
1178int sccp_connection_close(struct sccp_connection *connection, int cause)
1179{
1180 if (connection->connection_state < SCCP_CONNECTION_STATE_CONFIRM
1181 || connection->connection_state > SCCP_CONNECTION_STATE_ESTABLISHED) {
1182 DEBUGPC(DSCCP, "Can not close the connection. It was never opened: %p %d\n",
1183 connection, connection->connection_state);
1184 return -1;
1185 }
1186
1187 return _sccp_send_connection_released(connection, cause);
1188}
1189
1190int sccp_connection_free(struct sccp_connection *connection)
1191{
1192 if (connection->connection_state > SCCP_CONNECTION_STATE_NONE
1193 && connection->connection_state < SCCP_CONNECTION_STATE_RELEASE_COMPLETE) {
1194 DEBUGP(DSCCP, "The connection needs to be released before it is freed");
1195 return -1;
1196 }
1197
1198 talloc_free(connection);
1199 return 0;
1200}
1201
1202struct sccp_connection *sccp_connection_socket(void)
1203{
1204 return talloc_zero(tall_sccp_ctx, struct sccp_connection);
1205}
1206
1207int sccp_connection_connect(struct sccp_connection *conn,
1208 const struct sockaddr_sccp *local,
1209 struct msgb *data)
1210{
1211 return _sccp_send_connection_request(conn, local, data);
1212}
1213
1214int sccp_connection_set_incoming(const struct sockaddr_sccp *sock,
1215 int (*accept_cb)(struct sccp_connection *, void *), void *context)
1216{
1217 struct sccp_data_callback *cb;
1218
1219 if (!sock)
1220 return -2;
1221
1222 cb = _find_ssn(sock->sccp_ssn);
1223 if (!cb)
1224 return -1;
1225
1226 cb->accept_cb = accept_cb;
1227 cb->accept_context = context;
1228 return 0;
1229}
1230
1231int sccp_write(struct msgb *data, const struct sockaddr_sccp *in,
1232 const struct sockaddr_sccp *out, int class)
1233{
1234 return _sccp_send_data(class, in, out, data);
1235}
1236
1237int sccp_set_read(const struct sockaddr_sccp *sock,
1238 int (*read_cb)(struct msgb *, unsigned int, void *), void *context)
1239{
1240 struct sccp_data_callback *cb;
1241
1242 if (!sock)
1243 return -2;
1244
1245 cb = _find_ssn(sock->sccp_ssn);
1246 if (!cb)
1247 return -1;
1248
1249 cb->read_cb = read_cb;
1250 cb->read_context = context;
1251 return 0;
1252}
1253
1254static_assert(sizeof(struct sccp_source_reference) <= sizeof(u_int32_t), enough_space);
1255
1256u_int32_t sccp_src_ref_to_int(struct sccp_source_reference *ref)
1257{
1258 u_int32_t src_ref = 0;
1259 memcpy(&src_ref, ref, sizeof(*ref));
1260 return src_ref;
1261}
1262
1263struct sccp_source_reference sccp_src_ref_from_int(u_int32_t int_ref)
1264{
1265 struct sccp_source_reference ref;
1266 memcpy(&ref, &int_ref, sizeof(ref));
1267 return ref;
1268}
1269
Holger Hans Peter Freythera692fbc2010-01-13 09:55:43 +01001270int sccp_determine_msg_type(struct msgb *msg)
1271{
1272 if (msgb_l2len(msg) < 1)
1273 return -1;
1274
1275 return msg->l2h[0];
1276}
1277
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001278static __attribute__((constructor)) void on_dso_load(void)
1279{
1280 tall_sccp_ctx = talloc_named_const(NULL, 1, "sccp");
1281}
1282
1283static __attribute__((destructor)) void on_dso_unload(void)
1284{
1285 talloc_report_full(tall_sccp_ctx, stderr);
1286}