blob: 80f86c4faebc2fad2e7a98e22e502d1563c1152a [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
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
Holger Hans Peter Freythere2c50282010-02-20 00:36:03 +0100211 struct sccp_connection_request *req = (struct sccp_connection_request *)msgb->l2h;
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100212 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];
Holger Hans Peter Freyther8fabf512010-02-12 23:08:21 +0100462 result->data_len = msgb_l3len(msgb);
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +0100463
464 if (msgb_l3len(msgb) != msgb->l3h[-1]) {
Holger Hans Peter Freyther3cb28902010-01-30 10:34:18 +0100465 DEBUGP(DSCCP, "msgb is truncated is: %u should: %u\n",
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +0100466 msgb_l3len(msgb), msgb->l3h[-1]);
467 return -1;
468 }
469
470 return 0;
471}
472
Holger Hans Peter Freythere1d50672010-02-26 19:26:35 +0100473static int _sccp_parse_it(struct msgb *msgb, struct sccp_parse_result *result)
474{
475 static const u_int32_t header_size = sizeof(struct sccp_data_it);
476
477 struct sccp_data_it *it;
478
479 if (msgb_l2len(msgb) < header_size) {
480 DEBUGP(DSCCP, "msgb < header_size %u %u\n",
481 msgb_l2len(msgb), header_size);
482 return -1;
483 }
484
485 it = (struct sccp_data_it *) msgb->l2h;
486 result->data_len = 0;
487 result->source_local_reference = &it->source_local_reference;
488 result->destination_local_reference = &it->destination_local_reference;
489 return 0;
490}
491
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +0100492
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200493/*
494 * Send UDT. Currently we have a fixed address...
495 */
496static int _sccp_send_data(int class, const struct sockaddr_sccp *in,
497 const struct sockaddr_sccp *out, struct msgb *payload)
498{
499 struct sccp_data_unitdata *udt;
500 u_int8_t *data;
501 int ret;
502
503 if (msgb_l3len(payload) > 256) {
504 DEBUGP(DSCCP, "The payload is too big for one udt\n");
505 return -1;
506 }
507
508 struct msgb *msg = msgb_alloc_headroom(SCCP_MSG_SIZE,
509 SCCP_MSG_HEADROOM, "sccp: udt");
510 msg->l2h = &msg->data[0];
511 udt = (struct sccp_data_unitdata *)msgb_put(msg, sizeof(*udt));
512
513 udt->type = SCCP_MSG_TYPE_UDT;
514 udt->proto_class = class;
515 udt->variable_called = 3;
516 udt->variable_calling = 5;
517 udt->variable_data = 7;
518
519 /* for variable data we start with a size and the data */
520 data = msgb_put(msg, 1 + 2);
521 data[0] = 2;
522 data[1] = 0x42;
523 data[2] = out->sccp_ssn;
524
525 data = msgb_put(msg, 1 + 2);
526 data[0] = 2;
527 data[1] = 0x42;
528 data[2] = in->sccp_ssn;
529
530 /* copy the payload */
531 data = msgb_put(msg, 1 + msgb_l3len(payload));
532 data[0] = msgb_l3len(payload);
533 memcpy(&data[1], payload->l3h, msgb_l3len(payload));
534
535 ret = _send_msg(msg);
536 msgb_free(msg);
537
538 return ret;
539}
540
541static int _sccp_handle_read(struct msgb *msgb)
542{
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200543 struct sccp_data_callback *cb;
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +0100544 struct sccp_parse_result result;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200545
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +0100546 if (_sccp_parse_udt(msgb, &result) != 0)
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200547 return -1;
548
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +0100549 cb = _find_ssn(result.called.ssn);
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200550 if (!cb || !cb->read_cb) {
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +0100551 DEBUGP(DSCCP, "No routing for UDT for called SSN: %u\n", result.called.ssn);
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200552 return -1;
553 }
554
555 /* sanity check */
556 return cb->read_cb(msgb, msgb_l3len(msgb), cb->read_context);
557}
558
559/*
560 * handle connection orientated methods
561 */
562static int source_local_reference_is_free(struct sccp_source_reference *reference)
563{
564 struct sccp_connection *connection;
565
566 llist_for_each_entry(connection, &sccp_connections, list) {
567 if (memcmp(reference, &connection->source_local_reference, sizeof(*reference)) == 0)
568 return -1;
569 }
570
571 return 0;
572}
573
574static int destination_local_reference_is_free(struct sccp_source_reference *reference)
575{
576 struct sccp_connection *connection;
577
578 llist_for_each_entry(connection, &sccp_connections, list) {
579 if (memcmp(reference, &connection->destination_local_reference, sizeof(*reference)) == 0)
580 return -1;
581 }
582
583 return 0;
584}
585
586static int assign_source_local_reference(struct sccp_connection *connection)
587{
588 static u_int32_t last_ref = 0x30000;
589 int wrapped = 0;
590
591 do {
592 struct sccp_source_reference reference;
593 reference.octet1 = (last_ref >> 0) & 0xff;
594 reference.octet2 = (last_ref >> 8) & 0xff;
595 reference.octet3 = (last_ref >> 16) & 0xff;
596
597 ++last_ref;
598 /* do not use the reversed word and wrap around */
599 if ((last_ref & 0x00FFFFFF) == 0x00FFFFFF) {
600 DEBUGP(DSCCP, "Wrapped searching for a free code\n");
601 last_ref = 0;
602 ++wrapped;
603 }
604
605 if (source_local_reference_is_free(&reference) == 0) {
606 connection->source_local_reference = reference;
607 return 0;
608 }
609 } while (wrapped != 2);
610
611 DEBUGP(DSCCP, "Finding a free reference failed\n");
612 return -1;
613}
614
615static void _sccp_set_connection_state(struct sccp_connection *connection, int new_state)
616{
617 int old_state = connection->connection_state;
618
619 connection->connection_state = new_state;
620 if (connection->state_cb)
621 connection->state_cb(connection, old_state);
622}
623
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100624static int _sccp_send_refuse(struct sccp_source_reference *src_ref, int cause)
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200625{
626 struct msgb *msgb;
627 struct sccp_connection_refused *ref;
628 u_int8_t *data;
629 int ret;
630
631 msgb = msgb_alloc_headroom(SCCP_MSG_SIZE,
632 SCCP_MSG_HEADROOM, "sccp ref");
633 msgb->l2h = &msgb->data[0];
634
635 ref = (struct sccp_connection_refused *) msgb_put(msgb, sizeof(*ref));
636 ref->type = SCCP_MSG_TYPE_CREF;
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100637 memcpy(&ref->destination_local_reference, src_ref,
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200638 sizeof(struct sccp_source_reference));
639 ref->cause = cause;
640 ref->optional_start = 1;
641
642 data = msgb_put(msgb, 1);
643 data[0] = SCCP_PNC_END_OF_OPTIONAL;
644
645 ret = _send_msg(msgb);
646 msgb_free(msgb);
647 return ret;
648}
649
650static int _sccp_send_connection_confirm(struct sccp_connection *connection)
651{
652 struct msgb *response;
653 struct sccp_connection_confirm *confirm;
654 u_int8_t *optional_data;
655 int ret;
656
657 if (assign_source_local_reference(connection) != 0)
658 return -1;
659
660 response = msgb_alloc_headroom(SCCP_MSG_SIZE,
661 SCCP_MSG_HEADROOM, "sccp confirm");
662 response->l2h = &response->data[0];
663
664 confirm = (struct sccp_connection_confirm *) msgb_put(response, sizeof(*confirm));
665
666 confirm->type = SCCP_MSG_TYPE_CC;
667 memcpy(&confirm->destination_local_reference,
668 &connection->destination_local_reference,
669 sizeof(connection->destination_local_reference));
670 memcpy(&confirm->source_local_reference,
671 &connection->source_local_reference,
672 sizeof(connection->source_local_reference));
673 confirm->proto_class = 2;
674 confirm->optional_start = 1;
675
676 optional_data = (u_int8_t *) msgb_put(response, 1);
677 optional_data[0] = SCCP_PNC_END_OF_OPTIONAL;
678
679 ret = _send_msg(response);
680 msgb_free(response);
681
682 _sccp_set_connection_state(connection, SCCP_CONNECTION_STATE_ESTABLISHED);
683 return ret;
684}
685
686static int _sccp_send_connection_request(struct sccp_connection *connection,
687 const struct sockaddr_sccp *called, struct msgb *msg)
688{
689 struct msgb *request;
690 struct sccp_connection_request *req;
691 u_int8_t *data;
692 u_int8_t extra_size = 3 + 1;
693 int ret;
694
695
696 if (msg && (msgb_l3len(msg) < 3 || msgb_l3len(msg) > 130)) {
697 DEBUGP(DSCCP, "Invalid amount of data... %d\n", msgb_l3len(msg));
698 return -1;
699 }
700
701 /* try to find a id */
702 if (assign_source_local_reference(connection) != 0) {
703 DEBUGP(DSCCP, "Assigning a local reference failed.\n");
704 _sccp_set_connection_state(connection, SCCP_CONNECTION_STATE_SETUP_ERROR);
705 return -1;
706 }
707
708
709 if (msg)
710 extra_size += 2 + msgb_l3len(msg);
711 request = msgb_alloc_headroom(SCCP_MSG_SIZE,
712 SCCP_MSG_HEADROOM, "sccp connection request");
713 request->l2h = &request->data[0];
714 req = (struct sccp_connection_request *) msgb_put(request, sizeof(*req));
715
716 req->type = SCCP_MSG_TYPE_CR;
717 memcpy(&req->source_local_reference, &connection->source_local_reference,
718 sizeof(connection->source_local_reference));
719 req->proto_class = 2;
720 req->variable_called = 2;
721 req->optional_start = 4;
722
723 /* write the called party address */
724 data = msgb_put(request, 1 + 2);
725 data[0] = 2;
726 data[1] = 0x42;
727 data[2] = called->sccp_ssn;
728
729 /* write the payload */
730 if (msg) {
731 data = msgb_put(request, 2 + msgb_l3len(msg));
732 data[0] = SCCP_PNC_DATA;
733 data[1] = msgb_l3len(msg);
734 memcpy(&data[2], msg->l3h, msgb_l3len(msg));
735 }
736
737 data = msgb_put(request, 1);
738 data[0] = SCCP_PNC_END_OF_OPTIONAL;
739
740 llist_add_tail(&connection->list, &sccp_connections);
741 _sccp_set_connection_state(connection, SCCP_CONNECTION_STATE_REQUEST);
742
743 ret = _send_msg(request);
744 msgb_free(request);
745
746 return ret;
747}
748
749static int _sccp_send_connection_data(struct sccp_connection *conn, struct msgb *_data)
750{
751 struct msgb *msgb;
752 struct sccp_data_form1 *dt1;
753 u_int8_t *data;
754 int extra_size;
755 int ret;
756
757 if (msgb_l3len(_data) < 2 || msgb_l3len(_data) > 256) {
758 DEBUGP(DSCCP, "data size too big, segmenting unimplemented.\n");
759 return -1;
760 }
761
762 extra_size = 1 + msgb_l3len(_data);
763 msgb = msgb_alloc_headroom(SCCP_MSG_SIZE,
764 SCCP_MSG_HEADROOM, "sccp dt1");
765 msgb->l2h = &msgb->data[0];
766
767 dt1 = (struct sccp_data_form1 *) msgb_put(msgb, sizeof(*dt1));
768 dt1->type = SCCP_MSG_TYPE_DT1;
769 memcpy(&dt1->destination_local_reference, &conn->destination_local_reference,
770 sizeof(struct sccp_source_reference));
771 dt1->segmenting = 0;
772
773 /* copy the data */
774 dt1->variable_start = 1;
775 data = msgb_put(msgb, extra_size);
776 data[0] = extra_size - 1;
777 memcpy(&data[1], _data->l3h, extra_size - 1);
778
779 ret = _send_msg(msgb);
780 msgb_free(msgb);
781
782 return ret;
783}
784
Holger Hans Peter Freyther3b9516e2009-11-18 22:11:28 +0100785static int _sccp_send_connection_it(struct sccp_connection *conn)
786{
787 struct msgb *msgb;
788 struct sccp_data_it *it;
789 int ret;
790
791 msgb = msgb_alloc_headroom(SCCP_MSG_SIZE,
792 SCCP_MSG_HEADROOM, "sccp it");
793 msgb->l2h = &msgb->data[0];
794 it = (struct sccp_data_it *) msgb_put(msgb, sizeof(*it));
795 it->type = SCCP_MSG_TYPE_IT;
796 memcpy(&it->destination_local_reference, &conn->destination_local_reference,
797 sizeof(struct sccp_source_reference));
798 memcpy(&it->source_local_reference, &conn->source_local_reference,
799 sizeof(struct sccp_source_reference));
800
801 it->proto_class = 0x2;
802 it->sequencing[0] = it->sequencing[1] = 0;
803 it->credit = 0;
804
805 ret = _send_msg(msgb);
806 msgb_free(msgb);
807 return ret;
808}
809
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200810static int _sccp_send_connection_released(struct sccp_connection *conn, int cause)
811{
812 struct msgb *msg;
813 struct sccp_connection_released *rel;
814 u_int8_t *data;
815 int ret;
816
817 msg = msgb_alloc_headroom(SCCP_MSG_SIZE, SCCP_MSG_HEADROOM,
818 "sccp: connection released");
819 msg->l2h = &msg->data[0];
820 rel = (struct sccp_connection_released *) msgb_put(msg, sizeof(*rel));
821 rel->type = SCCP_MSG_TYPE_RLSD;
822 rel->release_cause = cause;
823
824 /* copy the source references */
825 memcpy(&rel->destination_local_reference, &conn->destination_local_reference,
826 sizeof(struct sccp_source_reference));
827 memcpy(&rel->source_local_reference, &conn->source_local_reference,
828 sizeof(struct sccp_source_reference));
829
830 data = msgb_put(msg, 1);
831 data[0] = SCCP_PNC_END_OF_OPTIONAL;
832
833 _sccp_set_connection_state(conn, SCCP_CONNECTION_STATE_RELEASE);
834 ret = _send_msg(msg);
835 msgb_free(msg);
836
837 return ret;
838}
839
840/*
841 * Open a connection. The following is going to happen:
842 *
843 * - Verify the packet, e.g. that we have no other connection
844 * that id.
845 * - Ask the user if he wants to accept the connection
846 * - Try to open the connection by assigning a source local reference
847 * and sending the packet
848 */
849static int _sccp_handle_connection_request(struct msgb *msgb)
850{
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100851 struct sccp_parse_result result;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200852
853 struct sccp_data_callback *cb;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200854 struct sccp_connection *connection;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200855
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100856 if (_sccp_parse_connection_request(msgb, &result) != 0)
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200857 return -1;
858
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100859 cb = _find_ssn(result.called.ssn);
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200860 if (!cb || !cb->accept_cb) {
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100861 DEBUGP(DSCCP, "No routing for CR for called SSN: %u\n", result.called.ssn);
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200862 return -1;
863 }
864
865 /* check if the system wants this connection */
866 connection = talloc_zero(tall_sccp_ctx, struct sccp_connection);
867 if (!connection) {
868 DEBUGP(DSCCP, "Allocation failed\n");
869 return -1;
870 }
871
872 /*
873 * sanity checks:
874 * - Is the source_local_reference in any other connection?
875 * then will call accept, assign a "destination" local reference
876 * and send a connection confirm, otherwise we will send a refuseed
877 * one....
878 */
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100879 if (destination_local_reference_is_free(result.source_local_reference) != 0) {
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200880 DEBUGP(DSCCP, "Need to reject connection with existing reference\n");
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100881 _sccp_send_refuse(result.source_local_reference, SCCP_REFUSAL_SCCP_FAILURE);
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200882 talloc_free(connection);
883 return -1;
884 }
885
886 connection->incoming = 1;
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100887 connection->destination_local_reference = *result.source_local_reference;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200888
889 if (cb->accept_cb(connection, cb->accept_context) != 0) {
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100890 _sccp_send_refuse(result.source_local_reference, SCCP_REFUSAL_END_USER_ORIGINATED);
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200891 _sccp_set_connection_state(connection, SCCP_CONNECTION_STATE_REFUSED);
892 talloc_free(connection);
893 return 0;
894 }
895
896
897 llist_add_tail(&connection->list, &sccp_connections);
898
899 if (_sccp_send_connection_confirm(connection) != 0) {
900 DEBUGP(DSCCP, "Sending confirm failed... no available source reference?\n");
901
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100902 _sccp_send_refuse(result.source_local_reference, SCCP_REFUSAL_SCCP_FAILURE);
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200903 _sccp_set_connection_state(connection, SCCP_CONNECTION_STATE_REFUSED);
904 llist_del(&connection->list);
905 talloc_free(connection);
906
907 return -1;
908 }
909
910 /*
911 * If we have data let us forward things.
912 */
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100913 if (result.data_len != 0 && connection->data_cb) {
914 connection->data_cb(connection, msgb, result.data_len);
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200915 }
916
917 return 0;
918}
919
920/* Handle the release confirmed */
Holger Hans Peter Freyther18c5cad2010-01-29 04:19:56 +0100921static int _sccp_handle_connection_release_complete(struct msgb *msgb)
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200922{
Holger Hans Peter Freyther18c5cad2010-01-29 04:19:56 +0100923 struct sccp_parse_result result;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200924 struct sccp_connection *conn;
925
Holger Hans Peter Freyther18c5cad2010-01-29 04:19:56 +0100926 if (_sccp_parse_connection_release_complete(msgb, &result) != 0)
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200927 return -1;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200928
929 /* find the connection */
930 llist_for_each_entry(conn, &sccp_connections, list) {
931 if (conn->data_cb
932 && memcmp(&conn->source_local_reference,
Holger Hans Peter Freyther18c5cad2010-01-29 04:19:56 +0100933 result.destination_local_reference,
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200934 sizeof(conn->source_local_reference)) == 0
935 && memcmp(&conn->destination_local_reference,
Holger Hans Peter Freyther18c5cad2010-01-29 04:19:56 +0100936 result.source_local_reference,
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200937 sizeof(conn->destination_local_reference)) == 0) {
938 goto found;
939 }
940 }
941
942
943 DEBUGP(DSCCP, "Release complete of unknown connection\n");
944 return -1;
945
946found:
947 llist_del(&conn->list);
948 _sccp_set_connection_state(conn, SCCP_CONNECTION_STATE_RELEASE_COMPLETE);
949 return 0;
950}
951
952/* Handle the Data Form 1 message */
Holger Hans Peter Freytheref845392010-01-29 04:31:00 +0100953static int _sccp_handle_connection_dt1(struct msgb *msgb)
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200954{
Holger Hans Peter Freytheref845392010-01-29 04:31:00 +0100955 struct sccp_parse_result result;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200956 struct sccp_connection *conn;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200957
Holger Hans Peter Freytheref845392010-01-29 04:31:00 +0100958 if (_sccp_parse_connection_dt1(msgb, &result) != 0)
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200959 return -1;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200960
961 /* lookup if we have a connection with the given reference */
962 llist_for_each_entry(conn, &sccp_connections, list) {
963 if (conn->data_cb
964 && memcmp(&conn->source_local_reference,
Holger Hans Peter Freytheref845392010-01-29 04:31:00 +0100965 result.destination_local_reference,
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200966 sizeof(conn->source_local_reference)) == 0) {
Holger Hans Peter Freytheref845392010-01-29 04:31:00 +0100967 goto found;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200968 }
969 }
970
971 DEBUGP(DSCCP, "No connection found for dt1 data\n");
972 return -1;
Holger Hans Peter Freytheref845392010-01-29 04:31:00 +0100973
974found:
975 conn->data_cb(conn, msgb, result.data_len);
976 return 0;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200977}
978
979/* confirm a connection release */
980static int _sccp_send_connection_release_complete(struct sccp_connection *connection)
981{
982 struct msgb *msgb;
983 struct sccp_connection_release_complete *rlc;
984 int ret;
985
986 msgb = msgb_alloc_headroom(SCCP_MSG_SIZE,
987 SCCP_MSG_HEADROOM, "sccp rlc");
988 msgb->l2h = &msgb->data[0];
989
990 rlc = (struct sccp_connection_release_complete *) msgb_put(msgb, sizeof(*rlc));
991 rlc->type = SCCP_MSG_TYPE_RLC;
992 memcpy(&rlc->destination_local_reference,
993 &connection->destination_local_reference, sizeof(struct sccp_source_reference));
994 memcpy(&rlc->source_local_reference,
995 &connection->source_local_reference, sizeof(struct sccp_source_reference));
996
997 ret = _send_msg(msgb);
998 msgb_free(msgb);
999
1000 /*
1001 * Remove from the list of active connections and set the state. User code
1002 * should now free the entry.
1003 */
1004 llist_del(&connection->list);
1005 _sccp_set_connection_state(connection, SCCP_CONNECTION_STATE_RELEASE_COMPLETE);
1006
1007 return ret;
1008}
1009
1010/* connection released, send a released confirm */
Holger Hans Peter Freyther88fe6ee2010-01-29 03:49:32 +01001011static int _sccp_handle_connection_released(struct msgb *msgb)
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001012{
Holger Hans Peter Freyther88fe6ee2010-01-29 03:49:32 +01001013 struct sccp_parse_result result;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001014 struct sccp_connection *conn;
1015
Holger Hans Peter Freyther88fe6ee2010-01-29 03:49:32 +01001016 if (_sccp_parse_connection_released(msgb, &result) == -1)
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001017 return -1;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001018
1019 /* lookup if we have a connection with the given reference */
1020 llist_for_each_entry(conn, &sccp_connections, list) {
1021 if (conn->data_cb
1022 && memcmp(&conn->source_local_reference,
Holger Hans Peter Freyther88fe6ee2010-01-29 03:49:32 +01001023 result.destination_local_reference,
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001024 sizeof(conn->source_local_reference)) == 0
1025 && memcmp(&conn->destination_local_reference,
Holger Hans Peter Freyther88fe6ee2010-01-29 03:49:32 +01001026 result.source_local_reference,
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001027 sizeof(conn->destination_local_reference)) == 0) {
1028 goto found;
1029 }
1030 }
1031
1032
1033 DEBUGP(DSCCP, "Unknown connection was released.\n");
1034 return -1;
1035
1036 /* we have found a connection */
1037found:
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001038 /* optional data */
Holger Hans Peter Freyther88fe6ee2010-01-29 03:49:32 +01001039 if (result.data_len != 0 && conn->data_cb) {
1040 conn->data_cb(conn, msgb, result.data_len);
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001041 }
1042
1043 /* generate a response */
1044 if (_sccp_send_connection_release_complete(conn) != 0) {
1045 DEBUGP(DSCCP, "Sending release confirmed failed\n");
1046 return -1;
1047 }
1048
1049 return 0;
1050}
1051
1052static int _sccp_handle_connection_refused(struct msgb *msgb)
1053{
Holger Hans Peter Freytherfe5de4e2010-01-29 03:58:12 +01001054 struct sccp_parse_result result;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001055 struct sccp_connection *conn;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001056
Holger Hans Peter Freytherfe5de4e2010-01-29 03:58:12 +01001057 if (_sccp_parse_connection_refused(msgb, &result) != 0)
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001058 return -1;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001059
1060 /* lookup if we have a connection with the given reference */
1061 llist_for_each_entry(conn, &sccp_connections, list) {
1062 if (conn->incoming == 0 && conn->data_cb
1063 && memcmp(&conn->source_local_reference,
Holger Hans Peter Freytherfe5de4e2010-01-29 03:58:12 +01001064 result.destination_local_reference,
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001065 sizeof(conn->source_local_reference)) == 0) {
1066 goto found;
1067 }
1068 }
1069
1070 DEBUGP(DSCCP, "Refused but no connection found\n");
1071 return -1;
1072
1073found:
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001074 /* optional data */
Holger Hans Peter Freytherfe5de4e2010-01-29 03:58:12 +01001075 if (result.data_len != 0 && conn->data_cb) {
1076 conn->data_cb(conn, msgb, result.data_len);
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001077 }
1078
1079
1080 llist_del(&conn->list);
1081 _sccp_set_connection_state(conn, SCCP_CONNECTION_STATE_REFUSED);
1082 return 0;
1083}
1084
1085static int _sccp_handle_connection_confirm(struct msgb *msgb)
1086{
Holger Hans Peter Freytherca1d1d12010-01-29 04:03:00 +01001087 struct sccp_parse_result result;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001088 struct sccp_connection *conn;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001089
Holger Hans Peter Freytherca1d1d12010-01-29 04:03:00 +01001090 if (_sccp_parse_connection_confirm(msgb, &result) != 0)
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001091 return -1;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001092
1093 /* lookup if we have a connection with the given reference */
1094 llist_for_each_entry(conn, &sccp_connections, list) {
1095 if (conn->incoming == 0 && conn->data_cb
1096 && memcmp(&conn->source_local_reference,
Holger Hans Peter Freytherca1d1d12010-01-29 04:03:00 +01001097 result.destination_local_reference,
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001098 sizeof(conn->source_local_reference)) == 0) {
1099 goto found;
1100 }
1101 }
1102
1103 DEBUGP(DSCCP, "Confirmed but no connection found\n");
1104 return -1;
1105
1106found:
1107 /* copy the addresses of the connection */
Holger Hans Peter Freytherca1d1d12010-01-29 04:03:00 +01001108 conn->destination_local_reference = *result.source_local_reference;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001109 _sccp_set_connection_state(conn, SCCP_CONNECTION_STATE_ESTABLISHED);
1110
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001111 /* optional data */
Holger Hans Peter Freytherca1d1d12010-01-29 04:03:00 +01001112 if (result.data_len != 0 && conn->data_cb) {
1113 conn->data_cb(conn, msgb, result.data_len);
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001114 }
1115
1116 return 0;
1117}
1118
1119
1120int sccp_system_init(int (*outgoing)(struct msgb *data, void *ctx), void *ctx)
1121{
1122 sccp_system.write_data = outgoing;
1123 sccp_system.write_context = ctx;
1124
1125 return 0;
1126}
1127
1128/* oh my god a real SCCP packet. need to dispatch it now */
1129int sccp_system_incoming(struct msgb *msgb)
1130{
1131 if (msgb_l2len(msgb) < 1 ) {
1132 DEBUGP(DSCCP, "Too short packet\n");
1133 return -1;
1134 }
1135
1136 int type = msgb->l2h[0];
1137
1138 switch(type) {
1139 case SCCP_MSG_TYPE_CR:
1140 return _sccp_handle_connection_request(msgb);
1141 break;
1142 case SCCP_MSG_TYPE_RLSD:
1143 return _sccp_handle_connection_released(msgb);
1144 break;
1145 case SCCP_MSG_TYPE_CREF:
1146 return _sccp_handle_connection_refused(msgb);
1147 break;
1148 case SCCP_MSG_TYPE_CC:
1149 return _sccp_handle_connection_confirm(msgb);
1150 break;
1151 case SCCP_MSG_TYPE_RLC:
1152 return _sccp_handle_connection_release_complete(msgb);
1153 break;
1154 case SCCP_MSG_TYPE_DT1:
1155 return _sccp_handle_connection_dt1(msgb);
1156 break;
1157 case SCCP_MSG_TYPE_UDT:
1158 return _sccp_handle_read(msgb);
1159 break;
1160 default:
1161 DEBUGP(DSCCP, "unimplemented msg type: %d\n", type);
1162 };
1163
1164 return -1;
1165}
1166
1167/* create a packet from the data */
1168int sccp_connection_write(struct sccp_connection *connection, struct msgb *data)
1169{
1170 if (connection->connection_state < SCCP_CONNECTION_STATE_CONFIRM
1171 || connection->connection_state > SCCP_CONNECTION_STATE_ESTABLISHED) {
1172 DEBUGP(DSCCP, "sccp_connection_write: Wrong connection state: %p %d\n",
1173 connection, connection->connection_state);
1174 return -1;
1175 }
1176
1177 return _sccp_send_connection_data(connection, data);
1178}
1179
Holger Hans Peter Freyther3b9516e2009-11-18 22:11:28 +01001180/*
1181 * Send a Inactivity Test message. The owner of the connection
1182 * should start a timer and call this method regularily. Calling
1183 * this every 60 seconds should be good enough.
1184 */
1185int sccp_connection_send_it(struct sccp_connection *connection)
1186{
1187 if (connection->connection_state < SCCP_CONNECTION_STATE_CONFIRM
1188 || connection->connection_state > SCCP_CONNECTION_STATE_ESTABLISHED) {
1189 DEBUGP(DSCCP, "sccp_connection_write: Wrong connection state: %p %d\n",
1190 connection, connection->connection_state);
1191 return -1;
1192 }
1193
1194 return _sccp_send_connection_it(connection);
1195}
1196
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001197/* send a connection release and wait for the connection released */
1198int sccp_connection_close(struct sccp_connection *connection, int cause)
1199{
1200 if (connection->connection_state < SCCP_CONNECTION_STATE_CONFIRM
1201 || connection->connection_state > SCCP_CONNECTION_STATE_ESTABLISHED) {
1202 DEBUGPC(DSCCP, "Can not close the connection. It was never opened: %p %d\n",
1203 connection, connection->connection_state);
1204 return -1;
1205 }
1206
1207 return _sccp_send_connection_released(connection, cause);
1208}
1209
1210int sccp_connection_free(struct sccp_connection *connection)
1211{
1212 if (connection->connection_state > SCCP_CONNECTION_STATE_NONE
1213 && connection->connection_state < SCCP_CONNECTION_STATE_RELEASE_COMPLETE) {
1214 DEBUGP(DSCCP, "The connection needs to be released before it is freed");
1215 return -1;
1216 }
1217
1218 talloc_free(connection);
1219 return 0;
1220}
1221
1222struct sccp_connection *sccp_connection_socket(void)
1223{
1224 return talloc_zero(tall_sccp_ctx, struct sccp_connection);
1225}
1226
1227int sccp_connection_connect(struct sccp_connection *conn,
1228 const struct sockaddr_sccp *local,
1229 struct msgb *data)
1230{
1231 return _sccp_send_connection_request(conn, local, data);
1232}
1233
1234int sccp_connection_set_incoming(const struct sockaddr_sccp *sock,
1235 int (*accept_cb)(struct sccp_connection *, void *), void *context)
1236{
1237 struct sccp_data_callback *cb;
1238
1239 if (!sock)
1240 return -2;
1241
1242 cb = _find_ssn(sock->sccp_ssn);
1243 if (!cb)
1244 return -1;
1245
1246 cb->accept_cb = accept_cb;
1247 cb->accept_context = context;
1248 return 0;
1249}
1250
1251int sccp_write(struct msgb *data, const struct sockaddr_sccp *in,
1252 const struct sockaddr_sccp *out, int class)
1253{
1254 return _sccp_send_data(class, in, out, data);
1255}
1256
1257int sccp_set_read(const struct sockaddr_sccp *sock,
1258 int (*read_cb)(struct msgb *, unsigned int, void *), void *context)
1259{
1260 struct sccp_data_callback *cb;
1261
1262 if (!sock)
1263 return -2;
1264
1265 cb = _find_ssn(sock->sccp_ssn);
1266 if (!cb)
1267 return -1;
1268
1269 cb->read_cb = read_cb;
1270 cb->read_context = context;
1271 return 0;
1272}
1273
1274static_assert(sizeof(struct sccp_source_reference) <= sizeof(u_int32_t), enough_space);
1275
1276u_int32_t sccp_src_ref_to_int(struct sccp_source_reference *ref)
1277{
1278 u_int32_t src_ref = 0;
1279 memcpy(&src_ref, ref, sizeof(*ref));
1280 return src_ref;
1281}
1282
1283struct sccp_source_reference sccp_src_ref_from_int(u_int32_t int_ref)
1284{
1285 struct sccp_source_reference ref;
1286 memcpy(&ref, &int_ref, sizeof(ref));
1287 return ref;
1288}
1289
Holger Hans Peter Freythera692fbc2010-01-13 09:55:43 +01001290int sccp_determine_msg_type(struct msgb *msg)
1291{
1292 if (msgb_l2len(msg) < 1)
1293 return -1;
1294
1295 return msg->l2h[0];
1296}
1297
Holger Hans Peter Freythercaf49b42010-01-29 04:31:51 +01001298int sccp_parse_header(struct msgb *msg, struct sccp_parse_result *result)
1299{
1300 int type;
1301
1302 if (msgb_l2len(msg) < 1)
1303 return -1;
1304
1305 type = msg->l2h[0];
1306 switch(type) {
1307 case SCCP_MSG_TYPE_CR:
1308 return _sccp_parse_connection_request(msg, result);
1309 break;
1310 case SCCP_MSG_TYPE_RLSD:
1311 return _sccp_parse_connection_released(msg, result);
1312 break;
1313 case SCCP_MSG_TYPE_CREF:
1314 return _sccp_parse_connection_refused(msg, result);
1315 break;
1316 case SCCP_MSG_TYPE_CC:
1317 return _sccp_parse_connection_confirm(msg, result);
1318 break;
1319 case SCCP_MSG_TYPE_RLC:
1320 return _sccp_parse_connection_release_complete(msg, result);
1321 break;
1322 case SCCP_MSG_TYPE_DT1:
1323 return _sccp_parse_connection_dt1(msg, result);
1324 break;
1325 case SCCP_MSG_TYPE_UDT:
1326 return _sccp_parse_udt(msg, result);
1327 break;
Holger Hans Peter Freythere1d50672010-02-26 19:26:35 +01001328 case SCCP_MSG_TYPE_IT:
1329 return _sccp_parse_it(msg, result);
1330 break;
Holger Hans Peter Freythercaf49b42010-01-29 04:31:51 +01001331 };
1332
Holger Hans Peter Freythere1d50672010-02-26 19:26:35 +01001333 LOGP(DSCCP, LOGL_ERROR, "Unimplemented MSG Type: 0x%x\n", type);
Holger Hans Peter Freythercaf49b42010-01-29 04:31:51 +01001334 return -1;
1335}
1336
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001337static __attribute__((constructor)) void on_dso_load(void)
1338{
1339 tall_sccp_ctx = talloc_named_const(NULL, 1, "sccp");
1340}
1341
1342static __attribute__((destructor)) void on_dso_unload(void)
1343{
1344 talloc_report_full(tall_sccp_ctx, stderr);
1345}