blob: b1da2c721e5fd7a17e47fc8bbe07401a206632ee [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>
Holger Hans Peter Freyther6ae65722010-02-03 18:10:07 +01005 * (C) 2009, 2010 by On-Waves
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
Harald Weltedfe6c7d2010-02-20 16:24:02 +010027#include <osmocore/msgb.h>
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +020028#include <openbsc/debug.h>
Harald Weltedfe6c7d2010-02-20 16:24:02 +010029#include <osmocore/talloc.h>
Holger Hans Peter Freyther8a69cb22010-02-12 22:44:50 +010030
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +020031#include <sccp/sccp.h>
32
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +020033
34static void *tall_sccp_ctx;
35static LLIST_HEAD(sccp_connections);
36
37#define SCCP_MSG_SIZE 4096
38#define SCCP_MSG_HEADROOM 128
39
40/* global data */
41const struct sockaddr_sccp sccp_ssn_bssap = {
42 .sccp_family = 0,
43 .sccp_ssn = SCCP_SSN_BSSAP,
44};
45
46struct sccp_system {
47 /* layer3 -> layer2 */
48 int (*write_data)(struct msgb *data, void *context);
49 void *write_context;
50};
51
52
53static struct sccp_system sccp_system = {
54 .write_data = NULL,
55};
56
57struct 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
70static LLIST_HEAD(sccp_callbacks);
71
72static 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
94static int _send_msg(struct msgb *msg)
95{
96 return sccp_system.write_data(msg, sccp_system.write_context);
97}
98
99/*
100 * parsing routines
101 */
102static 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
153static 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
167static 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 Freythera8cd2e62010-01-27 12:25:13 +0100203int _sccp_parse_connection_request(struct msgb *msgb, struct sccp_parse_result *result)
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +0100204{
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100205 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 Freythere2c50282010-02-20 00:36:03 +0100212 struct sccp_connection_request *req = (struct sccp_connection_request *)msgb->l2h;
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100213 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 Freytherefca5412010-01-27 12:12:46 +0100251}
252
Holger Hans Peter Freyther88fe6ee2010-01-29 03:49:32 +0100253int _sccp_parse_connection_released(struct msgb *msgb, struct sccp_parse_result *result)
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +0100254{
Holger Hans Peter Freyther88fe6ee2010-01-29 03:49:32 +0100255 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 Freytherefca5412010-01-27 12:12:46 +0100285}
286
Holger Hans Peter Freytherfe5de4e2010-01-29 03:58:12 +0100287int _sccp_parse_connection_refused(struct msgb *msgb, struct sccp_parse_result *result)
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +0100288{
Holger Hans Peter Freytherfe5de4e2010-01-29 03:58:12 +0100289 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 Freytherefca5412010-01-27 12:12:46 +0100322}
323
Holger Hans Peter Freytherca1d1d12010-01-29 04:03:00 +0100324int _sccp_parse_connection_confirm(struct msgb *msgb, struct sccp_parse_result *result)
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +0100325{
Holger Hans Peter Freytherca1d1d12010-01-29 04:03:00 +0100326 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 Freytherefca5412010-01-27 12:12:46 +0100359}
360
Holger Hans Peter Freyther18c5cad2010-01-29 04:19:56 +0100361int _sccp_parse_connection_release_complete(struct msgb *msgb, struct sccp_parse_result *result)
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +0100362{
Holger Hans Peter Freyther18c5cad2010-01-29 04:19:56 +0100363 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 Freytherefca5412010-01-27 12:12:46 +0100379}
380
Holger Hans Peter Freytheref845392010-01-29 04:31:00 +0100381int _sccp_parse_connection_dt1(struct msgb *msgb, struct sccp_parse_result *result)
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +0100382{
Holger Hans Peter Freytheref845392010-01-29 04:31:00 +0100383 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 Freytherefca5412010-01-27 12:12:46 +0100419}
420
421int _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 Freyther8fabf512010-02-12 23:08:21 +0100463 result->data_len = msgb_l3len(msgb);
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +0100464
465 if (msgb_l3len(msgb) != msgb->l3h[-1]) {
Holger Hans Peter Freyther3cb28902010-01-30 10:34:18 +0100466 DEBUGP(DSCCP, "msgb is truncated is: %u should: %u\n",
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +0100467 msgb_l3len(msgb), msgb->l3h[-1]);
468 return -1;
469 }
470
471 return 0;
472}
473
Holger Hans Peter Freythere1d50672010-02-26 19:26:35 +0100474static 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 Freytherefca5412010-01-27 12:12:46 +0100493
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200494/*
495 * Send UDT. Currently we have a fixed address...
496 */
497static int _sccp_send_data(int class, const struct sockaddr_sccp *in,
498 const struct sockaddr_sccp *out, struct msgb *payload)
499{
500 struct sccp_data_unitdata *udt;
501 u_int8_t *data;
502 int ret;
503
504 if (msgb_l3len(payload) > 256) {
505 DEBUGP(DSCCP, "The payload is too big for one udt\n");
506 return -1;
507 }
508
509 struct msgb *msg = msgb_alloc_headroom(SCCP_MSG_SIZE,
510 SCCP_MSG_HEADROOM, "sccp: udt");
511 msg->l2h = &msg->data[0];
512 udt = (struct sccp_data_unitdata *)msgb_put(msg, sizeof(*udt));
513
514 udt->type = SCCP_MSG_TYPE_UDT;
515 udt->proto_class = class;
516 udt->variable_called = 3;
517 udt->variable_calling = 5;
518 udt->variable_data = 7;
519
520 /* for variable data we start with a size and the data */
521 data = msgb_put(msg, 1 + 2);
522 data[0] = 2;
523 data[1] = 0x42;
524 data[2] = out->sccp_ssn;
525
526 data = msgb_put(msg, 1 + 2);
527 data[0] = 2;
528 data[1] = 0x42;
529 data[2] = in->sccp_ssn;
530
531 /* copy the payload */
532 data = msgb_put(msg, 1 + msgb_l3len(payload));
533 data[0] = msgb_l3len(payload);
534 memcpy(&data[1], payload->l3h, msgb_l3len(payload));
535
536 ret = _send_msg(msg);
537 msgb_free(msg);
538
539 return ret;
540}
541
542static int _sccp_handle_read(struct msgb *msgb)
543{
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200544 struct sccp_data_callback *cb;
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +0100545 struct sccp_parse_result result;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200546
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +0100547 if (_sccp_parse_udt(msgb, &result) != 0)
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200548 return -1;
549
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +0100550 cb = _find_ssn(result.called.ssn);
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200551 if (!cb || !cb->read_cb) {
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +0100552 DEBUGP(DSCCP, "No routing for UDT for called SSN: %u\n", result.called.ssn);
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200553 return -1;
554 }
555
556 /* sanity check */
557 return cb->read_cb(msgb, msgb_l3len(msgb), cb->read_context);
558}
559
560/*
561 * handle connection orientated methods
562 */
563static int source_local_reference_is_free(struct sccp_source_reference *reference)
564{
565 struct sccp_connection *connection;
566
567 llist_for_each_entry(connection, &sccp_connections, list) {
568 if (memcmp(reference, &connection->source_local_reference, sizeof(*reference)) == 0)
569 return -1;
570 }
571
572 return 0;
573}
574
575static int destination_local_reference_is_free(struct sccp_source_reference *reference)
576{
577 struct sccp_connection *connection;
578
579 llist_for_each_entry(connection, &sccp_connections, list) {
580 if (memcmp(reference, &connection->destination_local_reference, sizeof(*reference)) == 0)
581 return -1;
582 }
583
584 return 0;
585}
586
587static int assign_source_local_reference(struct sccp_connection *connection)
588{
589 static u_int32_t last_ref = 0x30000;
590 int wrapped = 0;
591
592 do {
593 struct sccp_source_reference reference;
594 reference.octet1 = (last_ref >> 0) & 0xff;
595 reference.octet2 = (last_ref >> 8) & 0xff;
596 reference.octet3 = (last_ref >> 16) & 0xff;
597
598 ++last_ref;
599 /* do not use the reversed word and wrap around */
600 if ((last_ref & 0x00FFFFFF) == 0x00FFFFFF) {
601 DEBUGP(DSCCP, "Wrapped searching for a free code\n");
602 last_ref = 0;
603 ++wrapped;
604 }
605
606 if (source_local_reference_is_free(&reference) == 0) {
607 connection->source_local_reference = reference;
608 return 0;
609 }
610 } while (wrapped != 2);
611
612 DEBUGP(DSCCP, "Finding a free reference failed\n");
613 return -1;
614}
615
616static void _sccp_set_connection_state(struct sccp_connection *connection, int new_state)
617{
618 int old_state = connection->connection_state;
619
620 connection->connection_state = new_state;
621 if (connection->state_cb)
622 connection->state_cb(connection, old_state);
623}
624
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100625static int _sccp_send_refuse(struct sccp_source_reference *src_ref, int cause)
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200626{
627 struct msgb *msgb;
628 struct sccp_connection_refused *ref;
629 u_int8_t *data;
630 int ret;
631
632 msgb = msgb_alloc_headroom(SCCP_MSG_SIZE,
633 SCCP_MSG_HEADROOM, "sccp ref");
634 msgb->l2h = &msgb->data[0];
635
636 ref = (struct sccp_connection_refused *) msgb_put(msgb, sizeof(*ref));
637 ref->type = SCCP_MSG_TYPE_CREF;
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100638 memcpy(&ref->destination_local_reference, src_ref,
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200639 sizeof(struct sccp_source_reference));
640 ref->cause = cause;
641 ref->optional_start = 1;
642
643 data = msgb_put(msgb, 1);
644 data[0] = SCCP_PNC_END_OF_OPTIONAL;
645
646 ret = _send_msg(msgb);
647 msgb_free(msgb);
648 return ret;
649}
650
651static int _sccp_send_connection_confirm(struct sccp_connection *connection)
652{
653 struct msgb *response;
654 struct sccp_connection_confirm *confirm;
655 u_int8_t *optional_data;
656 int ret;
657
658 if (assign_source_local_reference(connection) != 0)
659 return -1;
660
661 response = msgb_alloc_headroom(SCCP_MSG_SIZE,
662 SCCP_MSG_HEADROOM, "sccp confirm");
663 response->l2h = &response->data[0];
664
665 confirm = (struct sccp_connection_confirm *) msgb_put(response, sizeof(*confirm));
666
667 confirm->type = SCCP_MSG_TYPE_CC;
668 memcpy(&confirm->destination_local_reference,
669 &connection->destination_local_reference,
670 sizeof(connection->destination_local_reference));
671 memcpy(&confirm->source_local_reference,
672 &connection->source_local_reference,
673 sizeof(connection->source_local_reference));
674 confirm->proto_class = 2;
675 confirm->optional_start = 1;
676
677 optional_data = (u_int8_t *) msgb_put(response, 1);
678 optional_data[0] = SCCP_PNC_END_OF_OPTIONAL;
679
680 ret = _send_msg(response);
681 msgb_free(response);
682
683 _sccp_set_connection_state(connection, SCCP_CONNECTION_STATE_ESTABLISHED);
684 return ret;
685}
686
687static int _sccp_send_connection_request(struct sccp_connection *connection,
688 const struct sockaddr_sccp *called, struct msgb *msg)
689{
690 struct msgb *request;
691 struct sccp_connection_request *req;
692 u_int8_t *data;
693 u_int8_t extra_size = 3 + 1;
694 int ret;
695
696
697 if (msg && (msgb_l3len(msg) < 3 || msgb_l3len(msg) > 130)) {
698 DEBUGP(DSCCP, "Invalid amount of data... %d\n", msgb_l3len(msg));
699 return -1;
700 }
701
702 /* try to find a id */
703 if (assign_source_local_reference(connection) != 0) {
704 DEBUGP(DSCCP, "Assigning a local reference failed.\n");
705 _sccp_set_connection_state(connection, SCCP_CONNECTION_STATE_SETUP_ERROR);
706 return -1;
707 }
708
709
710 if (msg)
711 extra_size += 2 + msgb_l3len(msg);
712 request = msgb_alloc_headroom(SCCP_MSG_SIZE,
713 SCCP_MSG_HEADROOM, "sccp connection request");
714 request->l2h = &request->data[0];
715 req = (struct sccp_connection_request *) msgb_put(request, sizeof(*req));
716
717 req->type = SCCP_MSG_TYPE_CR;
718 memcpy(&req->source_local_reference, &connection->source_local_reference,
719 sizeof(connection->source_local_reference));
720 req->proto_class = 2;
721 req->variable_called = 2;
722 req->optional_start = 4;
723
724 /* write the called party address */
725 data = msgb_put(request, 1 + 2);
726 data[0] = 2;
727 data[1] = 0x42;
728 data[2] = called->sccp_ssn;
729
730 /* write the payload */
731 if (msg) {
732 data = msgb_put(request, 2 + msgb_l3len(msg));
733 data[0] = SCCP_PNC_DATA;
734 data[1] = msgb_l3len(msg);
735 memcpy(&data[2], msg->l3h, msgb_l3len(msg));
736 }
737
738 data = msgb_put(request, 1);
739 data[0] = SCCP_PNC_END_OF_OPTIONAL;
740
741 llist_add_tail(&connection->list, &sccp_connections);
742 _sccp_set_connection_state(connection, SCCP_CONNECTION_STATE_REQUEST);
743
744 ret = _send_msg(request);
745 msgb_free(request);
746
747 return ret;
748}
749
750static int _sccp_send_connection_data(struct sccp_connection *conn, struct msgb *_data)
751{
752 struct msgb *msgb;
753 struct sccp_data_form1 *dt1;
754 u_int8_t *data;
755 int extra_size;
756 int ret;
757
758 if (msgb_l3len(_data) < 2 || msgb_l3len(_data) > 256) {
759 DEBUGP(DSCCP, "data size too big, segmenting unimplemented.\n");
760 return -1;
761 }
762
763 extra_size = 1 + msgb_l3len(_data);
764 msgb = msgb_alloc_headroom(SCCP_MSG_SIZE,
765 SCCP_MSG_HEADROOM, "sccp dt1");
766 msgb->l2h = &msgb->data[0];
767
768 dt1 = (struct sccp_data_form1 *) msgb_put(msgb, sizeof(*dt1));
769 dt1->type = SCCP_MSG_TYPE_DT1;
770 memcpy(&dt1->destination_local_reference, &conn->destination_local_reference,
771 sizeof(struct sccp_source_reference));
772 dt1->segmenting = 0;
773
774 /* copy the data */
775 dt1->variable_start = 1;
776 data = msgb_put(msgb, extra_size);
777 data[0] = extra_size - 1;
778 memcpy(&data[1], _data->l3h, extra_size - 1);
779
780 ret = _send_msg(msgb);
781 msgb_free(msgb);
782
783 return ret;
784}
785
Holger Hans Peter Freyther3b9516e2009-11-18 22:11:28 +0100786static int _sccp_send_connection_it(struct sccp_connection *conn)
787{
788 struct msgb *msgb;
789 struct sccp_data_it *it;
790 int ret;
791
792 msgb = msgb_alloc_headroom(SCCP_MSG_SIZE,
793 SCCP_MSG_HEADROOM, "sccp it");
794 msgb->l2h = &msgb->data[0];
795 it = (struct sccp_data_it *) msgb_put(msgb, sizeof(*it));
796 it->type = SCCP_MSG_TYPE_IT;
797 memcpy(&it->destination_local_reference, &conn->destination_local_reference,
798 sizeof(struct sccp_source_reference));
799 memcpy(&it->source_local_reference, &conn->source_local_reference,
800 sizeof(struct sccp_source_reference));
801
802 it->proto_class = 0x2;
803 it->sequencing[0] = it->sequencing[1] = 0;
804 it->credit = 0;
805
806 ret = _send_msg(msgb);
807 msgb_free(msgb);
808 return ret;
809}
810
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200811static int _sccp_send_connection_released(struct sccp_connection *conn, int cause)
812{
813 struct msgb *msg;
814 struct sccp_connection_released *rel;
815 u_int8_t *data;
816 int ret;
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);
835 ret = _send_msg(msg);
836 msgb_free(msg);
837
838 return ret;
839}
840
841/*
842 * Open a connection. The following is going to happen:
843 *
844 * - Verify the packet, e.g. that we have no other connection
845 * that id.
846 * - Ask the user if he wants to accept the connection
847 * - Try to open the connection by assigning a source local reference
848 * and sending the packet
849 */
850static int _sccp_handle_connection_request(struct msgb *msgb)
851{
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100852 struct sccp_parse_result result;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200853
854 struct sccp_data_callback *cb;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200855 struct sccp_connection *connection;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200856
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100857 if (_sccp_parse_connection_request(msgb, &result) != 0)
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200858 return -1;
859
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100860 cb = _find_ssn(result.called.ssn);
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200861 if (!cb || !cb->accept_cb) {
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100862 DEBUGP(DSCCP, "No routing for CR for called SSN: %u\n", result.called.ssn);
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200863 return -1;
864 }
865
866 /* check if the system wants this connection */
867 connection = talloc_zero(tall_sccp_ctx, struct sccp_connection);
868 if (!connection) {
869 DEBUGP(DSCCP, "Allocation failed\n");
870 return -1;
871 }
872
873 /*
874 * sanity checks:
875 * - Is the source_local_reference in any other connection?
876 * then will call accept, assign a "destination" local reference
877 * and send a connection confirm, otherwise we will send a refuseed
878 * one....
879 */
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100880 if (destination_local_reference_is_free(result.source_local_reference) != 0) {
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200881 DEBUGP(DSCCP, "Need to reject connection with existing reference\n");
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 talloc_free(connection);
884 return -1;
885 }
886
887 connection->incoming = 1;
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100888 connection->destination_local_reference = *result.source_local_reference;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200889
890 if (cb->accept_cb(connection, cb->accept_context) != 0) {
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100891 _sccp_send_refuse(result.source_local_reference, SCCP_REFUSAL_END_USER_ORIGINATED);
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200892 _sccp_set_connection_state(connection, SCCP_CONNECTION_STATE_REFUSED);
893 talloc_free(connection);
894 return 0;
895 }
896
897
898 llist_add_tail(&connection->list, &sccp_connections);
899
900 if (_sccp_send_connection_confirm(connection) != 0) {
901 DEBUGP(DSCCP, "Sending confirm failed... no available source reference?\n");
902
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100903 _sccp_send_refuse(result.source_local_reference, SCCP_REFUSAL_SCCP_FAILURE);
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200904 _sccp_set_connection_state(connection, SCCP_CONNECTION_STATE_REFUSED);
905 llist_del(&connection->list);
906 talloc_free(connection);
907
908 return -1;
909 }
910
911 /*
912 * If we have data let us forward things.
913 */
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100914 if (result.data_len != 0 && connection->data_cb) {
915 connection->data_cb(connection, msgb, result.data_len);
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200916 }
917
918 return 0;
919}
920
921/* Handle the release confirmed */
Holger Hans Peter Freyther18c5cad2010-01-29 04:19:56 +0100922static int _sccp_handle_connection_release_complete(struct msgb *msgb)
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200923{
Holger Hans Peter Freyther18c5cad2010-01-29 04:19:56 +0100924 struct sccp_parse_result result;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200925 struct sccp_connection *conn;
926
Holger Hans Peter Freyther18c5cad2010-01-29 04:19:56 +0100927 if (_sccp_parse_connection_release_complete(msgb, &result) != 0)
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200928 return -1;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200929
930 /* find the connection */
931 llist_for_each_entry(conn, &sccp_connections, list) {
932 if (conn->data_cb
933 && memcmp(&conn->source_local_reference,
Holger Hans Peter Freyther18c5cad2010-01-29 04:19:56 +0100934 result.destination_local_reference,
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200935 sizeof(conn->source_local_reference)) == 0
936 && memcmp(&conn->destination_local_reference,
Holger Hans Peter Freyther18c5cad2010-01-29 04:19:56 +0100937 result.source_local_reference,
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200938 sizeof(conn->destination_local_reference)) == 0) {
939 goto found;
940 }
941 }
942
943
944 DEBUGP(DSCCP, "Release complete of unknown connection\n");
945 return -1;
946
947found:
948 llist_del(&conn->list);
949 _sccp_set_connection_state(conn, SCCP_CONNECTION_STATE_RELEASE_COMPLETE);
950 return 0;
951}
952
953/* Handle the Data Form 1 message */
Holger Hans Peter Freytheref845392010-01-29 04:31:00 +0100954static int _sccp_handle_connection_dt1(struct msgb *msgb)
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200955{
Holger Hans Peter Freytheref845392010-01-29 04:31:00 +0100956 struct sccp_parse_result result;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200957 struct sccp_connection *conn;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200958
Holger Hans Peter Freytheref845392010-01-29 04:31:00 +0100959 if (_sccp_parse_connection_dt1(msgb, &result) != 0)
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200960 return -1;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200961
962 /* lookup if we have a connection with the given reference */
963 llist_for_each_entry(conn, &sccp_connections, list) {
964 if (conn->data_cb
965 && memcmp(&conn->source_local_reference,
Holger Hans Peter Freytheref845392010-01-29 04:31:00 +0100966 result.destination_local_reference,
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200967 sizeof(conn->source_local_reference)) == 0) {
Holger Hans Peter Freytheref845392010-01-29 04:31:00 +0100968 goto found;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200969 }
970 }
971
972 DEBUGP(DSCCP, "No connection found for dt1 data\n");
973 return -1;
Holger Hans Peter Freytheref845392010-01-29 04:31:00 +0100974
975found:
976 conn->data_cb(conn, msgb, result.data_len);
977 return 0;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200978}
979
980/* confirm a connection release */
981static int _sccp_send_connection_release_complete(struct sccp_connection *connection)
982{
983 struct msgb *msgb;
984 struct sccp_connection_release_complete *rlc;
985 int ret;
986
987 msgb = msgb_alloc_headroom(SCCP_MSG_SIZE,
988 SCCP_MSG_HEADROOM, "sccp rlc");
989 msgb->l2h = &msgb->data[0];
990
991 rlc = (struct sccp_connection_release_complete *) msgb_put(msgb, sizeof(*rlc));
992 rlc->type = SCCP_MSG_TYPE_RLC;
993 memcpy(&rlc->destination_local_reference,
994 &connection->destination_local_reference, sizeof(struct sccp_source_reference));
995 memcpy(&rlc->source_local_reference,
996 &connection->source_local_reference, sizeof(struct sccp_source_reference));
997
998 ret = _send_msg(msgb);
999 msgb_free(msgb);
1000
1001 /*
1002 * Remove from the list of active connections and set the state. User code
1003 * should now free the entry.
1004 */
1005 llist_del(&connection->list);
1006 _sccp_set_connection_state(connection, SCCP_CONNECTION_STATE_RELEASE_COMPLETE);
1007
1008 return ret;
1009}
1010
1011/* connection released, send a released confirm */
Holger Hans Peter Freyther88fe6ee2010-01-29 03:49:32 +01001012static int _sccp_handle_connection_released(struct msgb *msgb)
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001013{
Holger Hans Peter Freyther88fe6ee2010-01-29 03:49:32 +01001014 struct sccp_parse_result result;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001015 struct sccp_connection *conn;
1016
Holger Hans Peter Freyther88fe6ee2010-01-29 03:49:32 +01001017 if (_sccp_parse_connection_released(msgb, &result) == -1)
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001018 return -1;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001019
1020 /* lookup if we have a connection with the given reference */
1021 llist_for_each_entry(conn, &sccp_connections, list) {
1022 if (conn->data_cb
1023 && memcmp(&conn->source_local_reference,
Holger Hans Peter Freyther88fe6ee2010-01-29 03:49:32 +01001024 result.destination_local_reference,
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001025 sizeof(conn->source_local_reference)) == 0
1026 && memcmp(&conn->destination_local_reference,
Holger Hans Peter Freyther88fe6ee2010-01-29 03:49:32 +01001027 result.source_local_reference,
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001028 sizeof(conn->destination_local_reference)) == 0) {
1029 goto found;
1030 }
1031 }
1032
1033
1034 DEBUGP(DSCCP, "Unknown connection was released.\n");
1035 return -1;
1036
1037 /* we have found a connection */
1038found:
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001039 /* optional data */
Holger Hans Peter Freyther88fe6ee2010-01-29 03:49:32 +01001040 if (result.data_len != 0 && conn->data_cb) {
1041 conn->data_cb(conn, msgb, result.data_len);
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001042 }
1043
1044 /* generate a response */
1045 if (_sccp_send_connection_release_complete(conn) != 0) {
1046 DEBUGP(DSCCP, "Sending release confirmed failed\n");
1047 return -1;
1048 }
1049
1050 return 0;
1051}
1052
1053static int _sccp_handle_connection_refused(struct msgb *msgb)
1054{
Holger Hans Peter Freytherfe5de4e2010-01-29 03:58:12 +01001055 struct sccp_parse_result result;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001056 struct sccp_connection *conn;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001057
Holger Hans Peter Freytherfe5de4e2010-01-29 03:58:12 +01001058 if (_sccp_parse_connection_refused(msgb, &result) != 0)
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001059 return -1;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001060
1061 /* lookup if we have a connection with the given reference */
1062 llist_for_each_entry(conn, &sccp_connections, list) {
1063 if (conn->incoming == 0 && conn->data_cb
1064 && memcmp(&conn->source_local_reference,
Holger Hans Peter Freytherfe5de4e2010-01-29 03:58:12 +01001065 result.destination_local_reference,
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001066 sizeof(conn->source_local_reference)) == 0) {
1067 goto found;
1068 }
1069 }
1070
1071 DEBUGP(DSCCP, "Refused but no connection found\n");
1072 return -1;
1073
1074found:
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001075 /* optional data */
Holger Hans Peter Freytherfe5de4e2010-01-29 03:58:12 +01001076 if (result.data_len != 0 && conn->data_cb) {
1077 conn->data_cb(conn, msgb, result.data_len);
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001078 }
1079
1080
1081 llist_del(&conn->list);
1082 _sccp_set_connection_state(conn, SCCP_CONNECTION_STATE_REFUSED);
1083 return 0;
1084}
1085
1086static int _sccp_handle_connection_confirm(struct msgb *msgb)
1087{
Holger Hans Peter Freytherca1d1d12010-01-29 04:03:00 +01001088 struct sccp_parse_result result;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001089 struct sccp_connection *conn;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001090
Holger Hans Peter Freytherca1d1d12010-01-29 04:03:00 +01001091 if (_sccp_parse_connection_confirm(msgb, &result) != 0)
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001092 return -1;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001093
1094 /* lookup if we have a connection with the given reference */
1095 llist_for_each_entry(conn, &sccp_connections, list) {
1096 if (conn->incoming == 0 && conn->data_cb
1097 && memcmp(&conn->source_local_reference,
Holger Hans Peter Freytherca1d1d12010-01-29 04:03:00 +01001098 result.destination_local_reference,
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001099 sizeof(conn->source_local_reference)) == 0) {
1100 goto found;
1101 }
1102 }
1103
1104 DEBUGP(DSCCP, "Confirmed but no connection found\n");
1105 return -1;
1106
1107found:
1108 /* copy the addresses of the connection */
Holger Hans Peter Freytherca1d1d12010-01-29 04:03:00 +01001109 conn->destination_local_reference = *result.source_local_reference;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001110 _sccp_set_connection_state(conn, SCCP_CONNECTION_STATE_ESTABLISHED);
1111
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001112 /* optional data */
Holger Hans Peter Freytherca1d1d12010-01-29 04:03:00 +01001113 if (result.data_len != 0 && conn->data_cb) {
1114 conn->data_cb(conn, msgb, result.data_len);
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001115 }
1116
1117 return 0;
1118}
1119
1120
1121int sccp_system_init(int (*outgoing)(struct msgb *data, void *ctx), void *ctx)
1122{
1123 sccp_system.write_data = outgoing;
1124 sccp_system.write_context = ctx;
1125
1126 return 0;
1127}
1128
1129/* oh my god a real SCCP packet. need to dispatch it now */
1130int sccp_system_incoming(struct msgb *msgb)
1131{
1132 if (msgb_l2len(msgb) < 1 ) {
1133 DEBUGP(DSCCP, "Too short packet\n");
1134 return -1;
1135 }
1136
1137 int type = msgb->l2h[0];
1138
1139 switch(type) {
1140 case SCCP_MSG_TYPE_CR:
1141 return _sccp_handle_connection_request(msgb);
1142 break;
1143 case SCCP_MSG_TYPE_RLSD:
1144 return _sccp_handle_connection_released(msgb);
1145 break;
1146 case SCCP_MSG_TYPE_CREF:
1147 return _sccp_handle_connection_refused(msgb);
1148 break;
1149 case SCCP_MSG_TYPE_CC:
1150 return _sccp_handle_connection_confirm(msgb);
1151 break;
1152 case SCCP_MSG_TYPE_RLC:
1153 return _sccp_handle_connection_release_complete(msgb);
1154 break;
1155 case SCCP_MSG_TYPE_DT1:
1156 return _sccp_handle_connection_dt1(msgb);
1157 break;
1158 case SCCP_MSG_TYPE_UDT:
1159 return _sccp_handle_read(msgb);
1160 break;
1161 default:
1162 DEBUGP(DSCCP, "unimplemented msg type: %d\n", type);
1163 };
1164
1165 return -1;
1166}
1167
1168/* create a packet from the data */
1169int sccp_connection_write(struct sccp_connection *connection, struct msgb *data)
1170{
1171 if (connection->connection_state < SCCP_CONNECTION_STATE_CONFIRM
1172 || connection->connection_state > SCCP_CONNECTION_STATE_ESTABLISHED) {
1173 DEBUGP(DSCCP, "sccp_connection_write: Wrong connection state: %p %d\n",
1174 connection, connection->connection_state);
1175 return -1;
1176 }
1177
1178 return _sccp_send_connection_data(connection, data);
1179}
1180
Holger Hans Peter Freyther3b9516e2009-11-18 22:11:28 +01001181/*
1182 * Send a Inactivity Test message. The owner of the connection
1183 * should start a timer and call this method regularily. Calling
1184 * this every 60 seconds should be good enough.
1185 */
1186int sccp_connection_send_it(struct sccp_connection *connection)
1187{
1188 if (connection->connection_state < SCCP_CONNECTION_STATE_CONFIRM
1189 || connection->connection_state > SCCP_CONNECTION_STATE_ESTABLISHED) {
1190 DEBUGP(DSCCP, "sccp_connection_write: Wrong connection state: %p %d\n",
1191 connection, connection->connection_state);
1192 return -1;
1193 }
1194
1195 return _sccp_send_connection_it(connection);
1196}
1197
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001198/* send a connection release and wait for the connection released */
1199int sccp_connection_close(struct sccp_connection *connection, int cause)
1200{
1201 if (connection->connection_state < SCCP_CONNECTION_STATE_CONFIRM
1202 || connection->connection_state > SCCP_CONNECTION_STATE_ESTABLISHED) {
1203 DEBUGPC(DSCCP, "Can not close the connection. It was never opened: %p %d\n",
1204 connection, connection->connection_state);
1205 return -1;
1206 }
1207
1208 return _sccp_send_connection_released(connection, cause);
1209}
1210
1211int sccp_connection_free(struct sccp_connection *connection)
1212{
1213 if (connection->connection_state > SCCP_CONNECTION_STATE_NONE
1214 && connection->connection_state < SCCP_CONNECTION_STATE_RELEASE_COMPLETE) {
1215 DEBUGP(DSCCP, "The connection needs to be released before it is freed");
1216 return -1;
1217 }
1218
1219 talloc_free(connection);
1220 return 0;
1221}
1222
1223struct sccp_connection *sccp_connection_socket(void)
1224{
1225 return talloc_zero(tall_sccp_ctx, struct sccp_connection);
1226}
1227
1228int sccp_connection_connect(struct sccp_connection *conn,
1229 const struct sockaddr_sccp *local,
1230 struct msgb *data)
1231{
1232 return _sccp_send_connection_request(conn, local, data);
1233}
1234
1235int sccp_connection_set_incoming(const struct sockaddr_sccp *sock,
1236 int (*accept_cb)(struct sccp_connection *, void *), void *context)
1237{
1238 struct sccp_data_callback *cb;
1239
1240 if (!sock)
1241 return -2;
1242
1243 cb = _find_ssn(sock->sccp_ssn);
1244 if (!cb)
1245 return -1;
1246
1247 cb->accept_cb = accept_cb;
1248 cb->accept_context = context;
1249 return 0;
1250}
1251
1252int sccp_write(struct msgb *data, const struct sockaddr_sccp *in,
1253 const struct sockaddr_sccp *out, int class)
1254{
1255 return _sccp_send_data(class, in, out, data);
1256}
1257
1258int sccp_set_read(const struct sockaddr_sccp *sock,
1259 int (*read_cb)(struct msgb *, unsigned int, void *), void *context)
1260{
1261 struct sccp_data_callback *cb;
1262
1263 if (!sock)
1264 return -2;
1265
1266 cb = _find_ssn(sock->sccp_ssn);
1267 if (!cb)
1268 return -1;
1269
1270 cb->read_cb = read_cb;
1271 cb->read_context = context;
1272 return 0;
1273}
1274
1275static_assert(sizeof(struct sccp_source_reference) <= sizeof(u_int32_t), enough_space);
1276
1277u_int32_t sccp_src_ref_to_int(struct sccp_source_reference *ref)
1278{
1279 u_int32_t src_ref = 0;
1280 memcpy(&src_ref, ref, sizeof(*ref));
1281 return src_ref;
1282}
1283
1284struct sccp_source_reference sccp_src_ref_from_int(u_int32_t int_ref)
1285{
1286 struct sccp_source_reference ref;
1287 memcpy(&ref, &int_ref, sizeof(ref));
1288 return ref;
1289}
1290
Holger Hans Peter Freythera692fbc2010-01-13 09:55:43 +01001291int sccp_determine_msg_type(struct msgb *msg)
1292{
1293 if (msgb_l2len(msg) < 1)
1294 return -1;
1295
1296 return msg->l2h[0];
1297}
1298
Holger Hans Peter Freythercaf49b42010-01-29 04:31:51 +01001299int sccp_parse_header(struct msgb *msg, struct sccp_parse_result *result)
1300{
1301 int type;
1302
1303 if (msgb_l2len(msg) < 1)
1304 return -1;
1305
1306 type = msg->l2h[0];
1307 switch(type) {
1308 case SCCP_MSG_TYPE_CR:
1309 return _sccp_parse_connection_request(msg, result);
1310 break;
1311 case SCCP_MSG_TYPE_RLSD:
1312 return _sccp_parse_connection_released(msg, result);
1313 break;
1314 case SCCP_MSG_TYPE_CREF:
1315 return _sccp_parse_connection_refused(msg, result);
1316 break;
1317 case SCCP_MSG_TYPE_CC:
1318 return _sccp_parse_connection_confirm(msg, result);
1319 break;
1320 case SCCP_MSG_TYPE_RLC:
1321 return _sccp_parse_connection_release_complete(msg, result);
1322 break;
1323 case SCCP_MSG_TYPE_DT1:
1324 return _sccp_parse_connection_dt1(msg, result);
1325 break;
1326 case SCCP_MSG_TYPE_UDT:
1327 return _sccp_parse_udt(msg, result);
1328 break;
Holger Hans Peter Freythere1d50672010-02-26 19:26:35 +01001329 case SCCP_MSG_TYPE_IT:
1330 return _sccp_parse_it(msg, result);
1331 break;
Holger Hans Peter Freythercaf49b42010-01-29 04:31:51 +01001332 };
1333
Holger Hans Peter Freythere1d50672010-02-26 19:26:35 +01001334 LOGP(DSCCP, LOGL_ERROR, "Unimplemented MSG Type: 0x%x\n", type);
Holger Hans Peter Freythercaf49b42010-01-29 04:31:51 +01001335 return -1;
1336}
1337
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001338static __attribute__((constructor)) void on_dso_load(void)
1339{
1340 tall_sccp_ctx = talloc_named_const(NULL, 1, "sccp");
1341}
1342
1343static __attribute__((destructor)) void on_dso_unload(void)
1344{
1345 talloc_report_full(tall_sccp_ctx, stderr);
1346}