blob: a19926630c5fc78c4e4ac673b00444ac4b29bbc7 [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
211 struct sccp_connection_request *req = (struct sccp_connection_request *)msgb->data;
212 struct sccp_optional_data optional_data;
213
214 /* header check */
215 if (msgb_l2len(msgb) < header_size) {
216 DEBUGP(DSCCP, "msgb < header_size %u %u\n",
217 msgb_l2len(msgb), header_size);
218 return -1;
219 }
220
221 /* copy out the calling and called address. Add the offset */
222 if (copy_address(&result->called, called_offset + req->variable_called, msgb) != 0)
223 return -1;
224
225 if (check_address(&result->called) != 0) {
226 DEBUGP(DSCCP, "Invalid called address according to 08.06: 0x%x 0x%x\n",
227 *(u_int8_t *)&result->called.address, result->called.ssn);
228 return -1;
229 }
230
231 result->source_local_reference = &req->source_local_reference;
232
233 /*
234 * parse optional data.
235 */
236 memset(&optional_data, 0, sizeof(optional_data));
237 if (_sccp_parse_optional_data(optional_offset + req->optional_start, msgb, &optional_data) != 0) {
238 DEBUGP(DSCCP, "parsing of optional data failed.\n");
239 return -1;
240 }
241
242 if (optional_data.data_len != 0) {
243 msgb->l3h = &msgb->l2h[optional_data.data_start];
244 result->data_len = optional_data.data_len;
245 } else {
246 result->data_len = 0;
247 }
248
249 return 0;
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +0100250}
251
Holger Hans Peter Freyther88fe6ee2010-01-29 03:49:32 +0100252int _sccp_parse_connection_released(struct msgb *msgb, struct sccp_parse_result *result)
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +0100253{
Holger Hans Peter Freyther88fe6ee2010-01-29 03:49:32 +0100254 static int header_size = sizeof(struct sccp_connection_released);
255 static int optional_offset = offsetof(struct sccp_connection_released, optional_start);
256
257 struct sccp_optional_data optional_data;
258 struct sccp_connection_released *rls = (struct sccp_connection_released *) msgb->l2h;
259
260 /* we don't have enough size for the struct */
261 if (msgb_l2len(msgb) < header_size) {
262 DEBUGP(DSCCP, "msgb > header_size %u %u\n",
263 msgb_l2len(msgb), header_size);
264 return -1;
265 }
266
267 memset(&optional_data, 0, sizeof(optional_data));
268 if (_sccp_parse_optional_data(optional_offset + rls->optional_start, msgb, &optional_data) != 0) {
269 DEBUGP(DSCCP, "parsing of optional data failed.\n");
270 return -1;
271 }
272
273 result->source_local_reference = &rls->source_local_reference;
274 result->destination_local_reference = &rls->destination_local_reference;
275
276 if (optional_data.data_len != 0) {
277 msgb->l3h = &msgb->l2h[optional_data.data_start];
278 result->data_len = optional_data.data_len;
279 } else {
280 result->data_len = 0;
281 }
282
283 return 0;
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +0100284}
285
Holger Hans Peter Freytherfe5de4e2010-01-29 03:58:12 +0100286int _sccp_parse_connection_refused(struct msgb *msgb, struct sccp_parse_result *result)
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +0100287{
Holger Hans Peter Freytherfe5de4e2010-01-29 03:58:12 +0100288 static const u_int32_t header_size =
289 sizeof(struct sccp_connection_refused);
290 static int optional_offset = offsetof(struct sccp_connection_refused, optional_start);
291
292 struct sccp_optional_data optional_data;
293 struct sccp_connection_refused *ref;
294
295 /* header check */
296 if (msgb_l2len(msgb) < header_size) {
297 DEBUGP(DSCCP, "msgb < header_size %u %u\n",
298 msgb_l2len(msgb), header_size);
299 return -1;
300 }
301
302 ref = (struct sccp_connection_refused *) msgb->l2h;
303
304 result->destination_local_reference = &ref->destination_local_reference;
305
306 memset(&optional_data, 0, sizeof(optional_data));
307 if (_sccp_parse_optional_data(optional_offset + ref->optional_start, msgb, &optional_data) != 0) {
308 DEBUGP(DSCCP, "parsing of optional data failed.\n");
309 return -1;
310 }
311
312 /* optional data */
313 if (optional_data.data_len != 0) {
314 msgb->l3h = &msgb->l2h[optional_data.data_start];
315 result->data_len = optional_data.data_len;
316 } else {
317 result->data_len = 0;
318 }
319
320 return 0;
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +0100321}
322
Holger Hans Peter Freytherca1d1d12010-01-29 04:03:00 +0100323int _sccp_parse_connection_confirm(struct msgb *msgb, struct sccp_parse_result *result)
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +0100324{
Holger Hans Peter Freytherca1d1d12010-01-29 04:03:00 +0100325 static u_int32_t header_size =
326 sizeof(struct sccp_connection_confirm);
327 static const u_int32_t optional_offset =
328 offsetof(struct sccp_connection_confirm, optional_start);
329
330 struct sccp_optional_data optional_data;
331 struct sccp_connection_confirm *con;
332
333 /* header check */
334 if (msgb_l2len(msgb) < header_size) {
335 DEBUGP(DSCCP, "msgb < header_size %u %u\n",
336 msgb_l2len(msgb), header_size);
337 return -1;
338 }
339
340 con = (struct sccp_connection_confirm *) msgb->l2h;
341 result->destination_local_reference = &con->destination_local_reference;
342 result->source_local_reference = &con->source_local_reference;
343
344 memset(&optional_data, 0, sizeof(optional_data));
345 if (_sccp_parse_optional_data(optional_offset + con->optional_start, msgb, &optional_data) != 0) {
346 DEBUGP(DSCCP, "parsing of optional data failed.\n");
347 return -1;
348 }
349
350 if (optional_data.data_len != 0) {
351 msgb->l3h = &msgb->l2h[optional_data.data_start];
352 result->data_len = optional_data.data_len;
353 } else {
354 result->data_len = 0;
355 }
356
357 return 0;
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +0100358}
359
Holger Hans Peter Freyther18c5cad2010-01-29 04:19:56 +0100360int _sccp_parse_connection_release_complete(struct msgb *msgb, struct sccp_parse_result *result)
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +0100361{
Holger Hans Peter Freyther18c5cad2010-01-29 04:19:56 +0100362 static int header_size = sizeof(struct sccp_connection_release_complete);
363
364 struct sccp_connection_release_complete *cmpl;
365
366 /* header check */
367 if (msgb_l2len(msgb) < header_size) {
368 DEBUGP(DSCCP, "msgb < header_size %u %u\n",
369 msgb_l2len(msgb), header_size);
370 return -1;
371 }
372
373 cmpl = (struct sccp_connection_release_complete *) msgb->l2h;
374 result->source_local_reference = &cmpl->source_local_reference;
375 result->destination_local_reference = &cmpl->destination_local_reference;
376
377 return 0;
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +0100378}
379
Holger Hans Peter Freytheref845392010-01-29 04:31:00 +0100380int _sccp_parse_connection_dt1(struct msgb *msgb, struct sccp_parse_result *result)
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +0100381{
Holger Hans Peter Freytheref845392010-01-29 04:31:00 +0100382 static int header_size = sizeof(struct sccp_data_form1);
383 static int variable_offset = offsetof(struct sccp_data_form1, variable_start);
384
385 struct sccp_data_form1 *dt1 = (struct sccp_data_form1 *)msgb->l2h;
386
387 /* we don't have enough size for the struct */
388 if (msgb_l2len(msgb) < header_size) {
389 DEBUGP(DSCCP, "msgb > header_size %u %u\n",
390 msgb_l2len(msgb), header_size);
391 return -1;
392 }
393
394 if (dt1->segmenting != 0) {
395 DEBUGP(DSCCP, "This packet has segmenting, not supported: %d\n", dt1->segmenting);
396 return -1;
397 }
398
399 result->destination_local_reference = &dt1->destination_local_reference;
400
401 /* some more size checks in here */
402 if (msgb_l2len(msgb) < variable_offset + dt1->variable_start + 1) {
403 DEBUGP(DSCCP, "Not enough space for variable start: %u %u\n",
404 msgb_l2len(msgb), dt1->variable_start);
405 return -1;
406 }
407
408 result->data_len = msgb->l2h[variable_offset + dt1->variable_start];
409 msgb->l3h = &msgb->l2h[dt1->variable_start + variable_offset + 1];
410
411 if (msgb_l3len(msgb) < result->data_len) {
412 DEBUGP(DSCCP, "Not enough room for the payload: %u %u\n",
413 msgb_l3len(msgb), result->data_len);
414 return -1;
415 }
416
417 return 0;
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +0100418}
419
420int _sccp_parse_udt(struct msgb *msgb, struct sccp_parse_result *result)
421{
422 static const u_int32_t header_size = sizeof(struct sccp_data_unitdata);
423 static const u_int32_t called_offset = offsetof(struct sccp_data_unitdata, variable_called);
424 static const u_int32_t calling_offset = offsetof(struct sccp_data_unitdata, variable_calling);
425 static const u_int32_t data_offset = offsetof(struct sccp_data_unitdata, variable_data);
426
427 struct sccp_data_unitdata *udt = (struct sccp_data_unitdata *)msgb->l2h;
428
429 if (msgb_l2len(msgb) < header_size) {
430 DEBUGP(DSCCP, "msgb < header_size %u %u\n",
431 msgb_l2len(msgb), header_size);
432 return -1;
433 }
434
435 /* copy out the calling and called address. Add the off */
436 if (copy_address(&result->called, called_offset + udt->variable_called, msgb) != 0)
437 return -1;
438
439 if (check_address(&result->called) != 0) {
440 DEBUGP(DSCCP, "Invalid called address according to 08.06: 0x%x 0x%x\n",
441 *(u_int8_t *)&result->called.address, result->called.ssn);
442 return -1;
443 }
444
445 if (copy_address(&result->calling, calling_offset + udt->variable_calling, msgb) != 0)
446 return -1;
447
448 if (check_address(&result->calling) != 0) {
449 DEBUGP(DSCCP, "Invalid called address according to 08.06: 0x%x 0x%x\n",
450 *(u_int8_t *)&result->called.address, result->called.ssn);
451 }
452
453 /* we don't have enough size for the data */
454 if (msgb_l2len(msgb) < data_offset + udt->variable_data + 1) {
455 DEBUGP(DSCCP, "msgb < header + offset %u %u %u\n",
456 msgb_l2len(msgb), header_size, udt->variable_data);
457 return -1;
458 }
459
460
461 msgb->l3h = &udt->data[udt->variable_data];
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
473
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200474/*
475 * Send UDT. Currently we have a fixed address...
476 */
477static int _sccp_send_data(int class, const struct sockaddr_sccp *in,
478 const struct sockaddr_sccp *out, struct msgb *payload)
479{
480 struct sccp_data_unitdata *udt;
481 u_int8_t *data;
482 int ret;
483
484 if (msgb_l3len(payload) > 256) {
485 DEBUGP(DSCCP, "The payload is too big for one udt\n");
486 return -1;
487 }
488
489 struct msgb *msg = msgb_alloc_headroom(SCCP_MSG_SIZE,
490 SCCP_MSG_HEADROOM, "sccp: udt");
491 msg->l2h = &msg->data[0];
492 udt = (struct sccp_data_unitdata *)msgb_put(msg, sizeof(*udt));
493
494 udt->type = SCCP_MSG_TYPE_UDT;
495 udt->proto_class = class;
496 udt->variable_called = 3;
497 udt->variable_calling = 5;
498 udt->variable_data = 7;
499
500 /* for variable data we start with a size and the data */
501 data = msgb_put(msg, 1 + 2);
502 data[0] = 2;
503 data[1] = 0x42;
504 data[2] = out->sccp_ssn;
505
506 data = msgb_put(msg, 1 + 2);
507 data[0] = 2;
508 data[1] = 0x42;
509 data[2] = in->sccp_ssn;
510
511 /* copy the payload */
512 data = msgb_put(msg, 1 + msgb_l3len(payload));
513 data[0] = msgb_l3len(payload);
514 memcpy(&data[1], payload->l3h, msgb_l3len(payload));
515
516 ret = _send_msg(msg);
517 msgb_free(msg);
518
519 return ret;
520}
521
522static int _sccp_handle_read(struct msgb *msgb)
523{
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200524 struct sccp_data_callback *cb;
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +0100525 struct sccp_parse_result result;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200526
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +0100527 if (_sccp_parse_udt(msgb, &result) != 0)
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200528 return -1;
529
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +0100530 cb = _find_ssn(result.called.ssn);
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200531 if (!cb || !cb->read_cb) {
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +0100532 DEBUGP(DSCCP, "No routing for UDT for called SSN: %u\n", result.called.ssn);
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200533 return -1;
534 }
535
536 /* sanity check */
537 return cb->read_cb(msgb, msgb_l3len(msgb), cb->read_context);
538}
539
540/*
541 * handle connection orientated methods
542 */
543static int source_local_reference_is_free(struct sccp_source_reference *reference)
544{
545 struct sccp_connection *connection;
546
547 llist_for_each_entry(connection, &sccp_connections, list) {
548 if (memcmp(reference, &connection->source_local_reference, sizeof(*reference)) == 0)
549 return -1;
550 }
551
552 return 0;
553}
554
555static int destination_local_reference_is_free(struct sccp_source_reference *reference)
556{
557 struct sccp_connection *connection;
558
559 llist_for_each_entry(connection, &sccp_connections, list) {
560 if (memcmp(reference, &connection->destination_local_reference, sizeof(*reference)) == 0)
561 return -1;
562 }
563
564 return 0;
565}
566
567static int assign_source_local_reference(struct sccp_connection *connection)
568{
569 static u_int32_t last_ref = 0x30000;
570 int wrapped = 0;
571
572 do {
573 struct sccp_source_reference reference;
574 reference.octet1 = (last_ref >> 0) & 0xff;
575 reference.octet2 = (last_ref >> 8) & 0xff;
576 reference.octet3 = (last_ref >> 16) & 0xff;
577
578 ++last_ref;
579 /* do not use the reversed word and wrap around */
580 if ((last_ref & 0x00FFFFFF) == 0x00FFFFFF) {
581 DEBUGP(DSCCP, "Wrapped searching for a free code\n");
582 last_ref = 0;
583 ++wrapped;
584 }
585
586 if (source_local_reference_is_free(&reference) == 0) {
587 connection->source_local_reference = reference;
588 return 0;
589 }
590 } while (wrapped != 2);
591
592 DEBUGP(DSCCP, "Finding a free reference failed\n");
593 return -1;
594}
595
596static void _sccp_set_connection_state(struct sccp_connection *connection, int new_state)
597{
598 int old_state = connection->connection_state;
599
600 connection->connection_state = new_state;
601 if (connection->state_cb)
602 connection->state_cb(connection, old_state);
603}
604
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100605static int _sccp_send_refuse(struct sccp_source_reference *src_ref, int cause)
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200606{
607 struct msgb *msgb;
608 struct sccp_connection_refused *ref;
609 u_int8_t *data;
610 int ret;
611
612 msgb = msgb_alloc_headroom(SCCP_MSG_SIZE,
613 SCCP_MSG_HEADROOM, "sccp ref");
614 msgb->l2h = &msgb->data[0];
615
616 ref = (struct sccp_connection_refused *) msgb_put(msgb, sizeof(*ref));
617 ref->type = SCCP_MSG_TYPE_CREF;
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100618 memcpy(&ref->destination_local_reference, src_ref,
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200619 sizeof(struct sccp_source_reference));
620 ref->cause = cause;
621 ref->optional_start = 1;
622
623 data = msgb_put(msgb, 1);
624 data[0] = SCCP_PNC_END_OF_OPTIONAL;
625
626 ret = _send_msg(msgb);
627 msgb_free(msgb);
628 return ret;
629}
630
631static int _sccp_send_connection_confirm(struct sccp_connection *connection)
632{
633 struct msgb *response;
634 struct sccp_connection_confirm *confirm;
635 u_int8_t *optional_data;
636 int ret;
637
638 if (assign_source_local_reference(connection) != 0)
639 return -1;
640
641 response = msgb_alloc_headroom(SCCP_MSG_SIZE,
642 SCCP_MSG_HEADROOM, "sccp confirm");
643 response->l2h = &response->data[0];
644
645 confirm = (struct sccp_connection_confirm *) msgb_put(response, sizeof(*confirm));
646
647 confirm->type = SCCP_MSG_TYPE_CC;
648 memcpy(&confirm->destination_local_reference,
649 &connection->destination_local_reference,
650 sizeof(connection->destination_local_reference));
651 memcpy(&confirm->source_local_reference,
652 &connection->source_local_reference,
653 sizeof(connection->source_local_reference));
654 confirm->proto_class = 2;
655 confirm->optional_start = 1;
656
657 optional_data = (u_int8_t *) msgb_put(response, 1);
658 optional_data[0] = SCCP_PNC_END_OF_OPTIONAL;
659
660 ret = _send_msg(response);
661 msgb_free(response);
662
663 _sccp_set_connection_state(connection, SCCP_CONNECTION_STATE_ESTABLISHED);
664 return ret;
665}
666
667static int _sccp_send_connection_request(struct sccp_connection *connection,
668 const struct sockaddr_sccp *called, struct msgb *msg)
669{
670 struct msgb *request;
671 struct sccp_connection_request *req;
672 u_int8_t *data;
673 u_int8_t extra_size = 3 + 1;
674 int ret;
675
676
677 if (msg && (msgb_l3len(msg) < 3 || msgb_l3len(msg) > 130)) {
678 DEBUGP(DSCCP, "Invalid amount of data... %d\n", msgb_l3len(msg));
679 return -1;
680 }
681
682 /* try to find a id */
683 if (assign_source_local_reference(connection) != 0) {
684 DEBUGP(DSCCP, "Assigning a local reference failed.\n");
685 _sccp_set_connection_state(connection, SCCP_CONNECTION_STATE_SETUP_ERROR);
686 return -1;
687 }
688
689
690 if (msg)
691 extra_size += 2 + msgb_l3len(msg);
692 request = msgb_alloc_headroom(SCCP_MSG_SIZE,
693 SCCP_MSG_HEADROOM, "sccp connection request");
694 request->l2h = &request->data[0];
695 req = (struct sccp_connection_request *) msgb_put(request, sizeof(*req));
696
697 req->type = SCCP_MSG_TYPE_CR;
698 memcpy(&req->source_local_reference, &connection->source_local_reference,
699 sizeof(connection->source_local_reference));
700 req->proto_class = 2;
701 req->variable_called = 2;
702 req->optional_start = 4;
703
704 /* write the called party address */
705 data = msgb_put(request, 1 + 2);
706 data[0] = 2;
707 data[1] = 0x42;
708 data[2] = called->sccp_ssn;
709
710 /* write the payload */
711 if (msg) {
712 data = msgb_put(request, 2 + msgb_l3len(msg));
713 data[0] = SCCP_PNC_DATA;
714 data[1] = msgb_l3len(msg);
715 memcpy(&data[2], msg->l3h, msgb_l3len(msg));
716 }
717
718 data = msgb_put(request, 1);
719 data[0] = SCCP_PNC_END_OF_OPTIONAL;
720
721 llist_add_tail(&connection->list, &sccp_connections);
722 _sccp_set_connection_state(connection, SCCP_CONNECTION_STATE_REQUEST);
723
724 ret = _send_msg(request);
725 msgb_free(request);
726
727 return ret;
728}
729
730static int _sccp_send_connection_data(struct sccp_connection *conn, struct msgb *_data)
731{
732 struct msgb *msgb;
733 struct sccp_data_form1 *dt1;
734 u_int8_t *data;
735 int extra_size;
736 int ret;
737
738 if (msgb_l3len(_data) < 2 || msgb_l3len(_data) > 256) {
739 DEBUGP(DSCCP, "data size too big, segmenting unimplemented.\n");
740 return -1;
741 }
742
743 extra_size = 1 + msgb_l3len(_data);
744 msgb = msgb_alloc_headroom(SCCP_MSG_SIZE,
745 SCCP_MSG_HEADROOM, "sccp dt1");
746 msgb->l2h = &msgb->data[0];
747
748 dt1 = (struct sccp_data_form1 *) msgb_put(msgb, sizeof(*dt1));
749 dt1->type = SCCP_MSG_TYPE_DT1;
750 memcpy(&dt1->destination_local_reference, &conn->destination_local_reference,
751 sizeof(struct sccp_source_reference));
752 dt1->segmenting = 0;
753
754 /* copy the data */
755 dt1->variable_start = 1;
756 data = msgb_put(msgb, extra_size);
757 data[0] = extra_size - 1;
758 memcpy(&data[1], _data->l3h, extra_size - 1);
759
760 ret = _send_msg(msgb);
761 msgb_free(msgb);
762
763 return ret;
764}
765
Holger Hans Peter Freyther3b9516e2009-11-18 22:11:28 +0100766static int _sccp_send_connection_it(struct sccp_connection *conn)
767{
768 struct msgb *msgb;
769 struct sccp_data_it *it;
770 int ret;
771
772 msgb = msgb_alloc_headroom(SCCP_MSG_SIZE,
773 SCCP_MSG_HEADROOM, "sccp it");
774 msgb->l2h = &msgb->data[0];
775 it = (struct sccp_data_it *) msgb_put(msgb, sizeof(*it));
776 it->type = SCCP_MSG_TYPE_IT;
777 memcpy(&it->destination_local_reference, &conn->destination_local_reference,
778 sizeof(struct sccp_source_reference));
779 memcpy(&it->source_local_reference, &conn->source_local_reference,
780 sizeof(struct sccp_source_reference));
781
782 it->proto_class = 0x2;
783 it->sequencing[0] = it->sequencing[1] = 0;
784 it->credit = 0;
785
786 ret = _send_msg(msgb);
787 msgb_free(msgb);
788 return ret;
789}
790
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200791static int _sccp_send_connection_released(struct sccp_connection *conn, int cause)
792{
793 struct msgb *msg;
794 struct sccp_connection_released *rel;
795 u_int8_t *data;
796 int ret;
797
798 msg = msgb_alloc_headroom(SCCP_MSG_SIZE, SCCP_MSG_HEADROOM,
799 "sccp: connection released");
800 msg->l2h = &msg->data[0];
801 rel = (struct sccp_connection_released *) msgb_put(msg, sizeof(*rel));
802 rel->type = SCCP_MSG_TYPE_RLSD;
803 rel->release_cause = cause;
804
805 /* copy the source references */
806 memcpy(&rel->destination_local_reference, &conn->destination_local_reference,
807 sizeof(struct sccp_source_reference));
808 memcpy(&rel->source_local_reference, &conn->source_local_reference,
809 sizeof(struct sccp_source_reference));
810
811 data = msgb_put(msg, 1);
812 data[0] = SCCP_PNC_END_OF_OPTIONAL;
813
814 _sccp_set_connection_state(conn, SCCP_CONNECTION_STATE_RELEASE);
815 ret = _send_msg(msg);
816 msgb_free(msg);
817
818 return ret;
819}
820
821/*
822 * Open a connection. The following is going to happen:
823 *
824 * - Verify the packet, e.g. that we have no other connection
825 * that id.
826 * - Ask the user if he wants to accept the connection
827 * - Try to open the connection by assigning a source local reference
828 * and sending the packet
829 */
830static int _sccp_handle_connection_request(struct msgb *msgb)
831{
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100832 struct sccp_parse_result result;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200833
834 struct sccp_data_callback *cb;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200835 struct sccp_connection *connection;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200836
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100837 if (_sccp_parse_connection_request(msgb, &result) != 0)
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200838 return -1;
839
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100840 cb = _find_ssn(result.called.ssn);
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200841 if (!cb || !cb->accept_cb) {
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100842 DEBUGP(DSCCP, "No routing for CR for called SSN: %u\n", result.called.ssn);
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200843 return -1;
844 }
845
846 /* check if the system wants this connection */
847 connection = talloc_zero(tall_sccp_ctx, struct sccp_connection);
848 if (!connection) {
849 DEBUGP(DSCCP, "Allocation failed\n");
850 return -1;
851 }
852
853 /*
854 * sanity checks:
855 * - Is the source_local_reference in any other connection?
856 * then will call accept, assign a "destination" local reference
857 * and send a connection confirm, otherwise we will send a refuseed
858 * one....
859 */
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100860 if (destination_local_reference_is_free(result.source_local_reference) != 0) {
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200861 DEBUGP(DSCCP, "Need to reject connection with existing reference\n");
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100862 _sccp_send_refuse(result.source_local_reference, SCCP_REFUSAL_SCCP_FAILURE);
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200863 talloc_free(connection);
864 return -1;
865 }
866
867 connection->incoming = 1;
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100868 connection->destination_local_reference = *result.source_local_reference;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200869
870 if (cb->accept_cb(connection, cb->accept_context) != 0) {
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100871 _sccp_send_refuse(result.source_local_reference, SCCP_REFUSAL_END_USER_ORIGINATED);
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200872 _sccp_set_connection_state(connection, SCCP_CONNECTION_STATE_REFUSED);
873 talloc_free(connection);
874 return 0;
875 }
876
877
878 llist_add_tail(&connection->list, &sccp_connections);
879
880 if (_sccp_send_connection_confirm(connection) != 0) {
881 DEBUGP(DSCCP, "Sending confirm failed... no available source reference?\n");
882
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100883 _sccp_send_refuse(result.source_local_reference, SCCP_REFUSAL_SCCP_FAILURE);
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200884 _sccp_set_connection_state(connection, SCCP_CONNECTION_STATE_REFUSED);
885 llist_del(&connection->list);
886 talloc_free(connection);
887
888 return -1;
889 }
890
891 /*
892 * If we have data let us forward things.
893 */
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100894 if (result.data_len != 0 && connection->data_cb) {
895 connection->data_cb(connection, msgb, result.data_len);
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200896 }
897
898 return 0;
899}
900
901/* Handle the release confirmed */
Holger Hans Peter Freyther18c5cad2010-01-29 04:19:56 +0100902static int _sccp_handle_connection_release_complete(struct msgb *msgb)
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200903{
Holger Hans Peter Freyther18c5cad2010-01-29 04:19:56 +0100904 struct sccp_parse_result result;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200905 struct sccp_connection *conn;
906
Holger Hans Peter Freyther18c5cad2010-01-29 04:19:56 +0100907 if (_sccp_parse_connection_release_complete(msgb, &result) != 0)
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200908 return -1;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200909
910 /* find the connection */
911 llist_for_each_entry(conn, &sccp_connections, list) {
912 if (conn->data_cb
913 && memcmp(&conn->source_local_reference,
Holger Hans Peter Freyther18c5cad2010-01-29 04:19:56 +0100914 result.destination_local_reference,
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200915 sizeof(conn->source_local_reference)) == 0
916 && memcmp(&conn->destination_local_reference,
Holger Hans Peter Freyther18c5cad2010-01-29 04:19:56 +0100917 result.source_local_reference,
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200918 sizeof(conn->destination_local_reference)) == 0) {
919 goto found;
920 }
921 }
922
923
924 DEBUGP(DSCCP, "Release complete of unknown connection\n");
925 return -1;
926
927found:
928 llist_del(&conn->list);
929 _sccp_set_connection_state(conn, SCCP_CONNECTION_STATE_RELEASE_COMPLETE);
930 return 0;
931}
932
933/* Handle the Data Form 1 message */
Holger Hans Peter Freytheref845392010-01-29 04:31:00 +0100934static int _sccp_handle_connection_dt1(struct msgb *msgb)
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200935{
Holger Hans Peter Freytheref845392010-01-29 04:31:00 +0100936 struct sccp_parse_result result;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200937 struct sccp_connection *conn;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200938
Holger Hans Peter Freytheref845392010-01-29 04:31:00 +0100939 if (_sccp_parse_connection_dt1(msgb, &result) != 0)
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200940 return -1;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200941
942 /* lookup if we have a connection with the given reference */
943 llist_for_each_entry(conn, &sccp_connections, list) {
944 if (conn->data_cb
945 && memcmp(&conn->source_local_reference,
Holger Hans Peter Freytheref845392010-01-29 04:31:00 +0100946 result.destination_local_reference,
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200947 sizeof(conn->source_local_reference)) == 0) {
Holger Hans Peter Freytheref845392010-01-29 04:31:00 +0100948 goto found;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200949 }
950 }
951
952 DEBUGP(DSCCP, "No connection found for dt1 data\n");
953 return -1;
Holger Hans Peter Freytheref845392010-01-29 04:31:00 +0100954
955found:
956 conn->data_cb(conn, msgb, result.data_len);
957 return 0;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200958}
959
960/* confirm a connection release */
961static int _sccp_send_connection_release_complete(struct sccp_connection *connection)
962{
963 struct msgb *msgb;
964 struct sccp_connection_release_complete *rlc;
965 int ret;
966
967 msgb = msgb_alloc_headroom(SCCP_MSG_SIZE,
968 SCCP_MSG_HEADROOM, "sccp rlc");
969 msgb->l2h = &msgb->data[0];
970
971 rlc = (struct sccp_connection_release_complete *) msgb_put(msgb, sizeof(*rlc));
972 rlc->type = SCCP_MSG_TYPE_RLC;
973 memcpy(&rlc->destination_local_reference,
974 &connection->destination_local_reference, sizeof(struct sccp_source_reference));
975 memcpy(&rlc->source_local_reference,
976 &connection->source_local_reference, sizeof(struct sccp_source_reference));
977
978 ret = _send_msg(msgb);
979 msgb_free(msgb);
980
981 /*
982 * Remove from the list of active connections and set the state. User code
983 * should now free the entry.
984 */
985 llist_del(&connection->list);
986 _sccp_set_connection_state(connection, SCCP_CONNECTION_STATE_RELEASE_COMPLETE);
987
988 return ret;
989}
990
991/* connection released, send a released confirm */
Holger Hans Peter Freyther88fe6ee2010-01-29 03:49:32 +0100992static int _sccp_handle_connection_released(struct msgb *msgb)
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200993{
Holger Hans Peter Freyther88fe6ee2010-01-29 03:49:32 +0100994 struct sccp_parse_result result;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200995 struct sccp_connection *conn;
996
Holger Hans Peter Freyther88fe6ee2010-01-29 03:49:32 +0100997 if (_sccp_parse_connection_released(msgb, &result) == -1)
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200998 return -1;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200999
1000 /* lookup if we have a connection with the given reference */
1001 llist_for_each_entry(conn, &sccp_connections, list) {
1002 if (conn->data_cb
1003 && memcmp(&conn->source_local_reference,
Holger Hans Peter Freyther88fe6ee2010-01-29 03:49:32 +01001004 result.destination_local_reference,
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001005 sizeof(conn->source_local_reference)) == 0
1006 && memcmp(&conn->destination_local_reference,
Holger Hans Peter Freyther88fe6ee2010-01-29 03:49:32 +01001007 result.source_local_reference,
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001008 sizeof(conn->destination_local_reference)) == 0) {
1009 goto found;
1010 }
1011 }
1012
1013
1014 DEBUGP(DSCCP, "Unknown connection was released.\n");
1015 return -1;
1016
1017 /* we have found a connection */
1018found:
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001019 /* optional data */
Holger Hans Peter Freyther88fe6ee2010-01-29 03:49:32 +01001020 if (result.data_len != 0 && conn->data_cb) {
1021 conn->data_cb(conn, msgb, result.data_len);
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001022 }
1023
1024 /* generate a response */
1025 if (_sccp_send_connection_release_complete(conn) != 0) {
1026 DEBUGP(DSCCP, "Sending release confirmed failed\n");
1027 return -1;
1028 }
1029
1030 return 0;
1031}
1032
1033static int _sccp_handle_connection_refused(struct msgb *msgb)
1034{
Holger Hans Peter Freytherfe5de4e2010-01-29 03:58:12 +01001035 struct sccp_parse_result result;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001036 struct sccp_connection *conn;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001037
Holger Hans Peter Freytherfe5de4e2010-01-29 03:58:12 +01001038 if (_sccp_parse_connection_refused(msgb, &result) != 0)
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001039 return -1;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001040
1041 /* lookup if we have a connection with the given reference */
1042 llist_for_each_entry(conn, &sccp_connections, list) {
1043 if (conn->incoming == 0 && conn->data_cb
1044 && memcmp(&conn->source_local_reference,
Holger Hans Peter Freytherfe5de4e2010-01-29 03:58:12 +01001045 result.destination_local_reference,
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001046 sizeof(conn->source_local_reference)) == 0) {
1047 goto found;
1048 }
1049 }
1050
1051 DEBUGP(DSCCP, "Refused but no connection found\n");
1052 return -1;
1053
1054found:
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001055 /* optional data */
Holger Hans Peter Freytherfe5de4e2010-01-29 03:58:12 +01001056 if (result.data_len != 0 && conn->data_cb) {
1057 conn->data_cb(conn, msgb, result.data_len);
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001058 }
1059
1060
1061 llist_del(&conn->list);
1062 _sccp_set_connection_state(conn, SCCP_CONNECTION_STATE_REFUSED);
1063 return 0;
1064}
1065
1066static int _sccp_handle_connection_confirm(struct msgb *msgb)
1067{
Holger Hans Peter Freytherca1d1d12010-01-29 04:03:00 +01001068 struct sccp_parse_result result;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001069 struct sccp_connection *conn;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001070
Holger Hans Peter Freytherca1d1d12010-01-29 04:03:00 +01001071 if (_sccp_parse_connection_confirm(msgb, &result) != 0)
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001072 return -1;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001073
1074 /* lookup if we have a connection with the given reference */
1075 llist_for_each_entry(conn, &sccp_connections, list) {
1076 if (conn->incoming == 0 && conn->data_cb
1077 && memcmp(&conn->source_local_reference,
Holger Hans Peter Freytherca1d1d12010-01-29 04:03:00 +01001078 result.destination_local_reference,
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001079 sizeof(conn->source_local_reference)) == 0) {
1080 goto found;
1081 }
1082 }
1083
1084 DEBUGP(DSCCP, "Confirmed but no connection found\n");
1085 return -1;
1086
1087found:
1088 /* copy the addresses of the connection */
Holger Hans Peter Freytherca1d1d12010-01-29 04:03:00 +01001089 conn->destination_local_reference = *result.source_local_reference;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001090 _sccp_set_connection_state(conn, SCCP_CONNECTION_STATE_ESTABLISHED);
1091
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001092 /* optional data */
Holger Hans Peter Freytherca1d1d12010-01-29 04:03:00 +01001093 if (result.data_len != 0 && conn->data_cb) {
1094 conn->data_cb(conn, msgb, result.data_len);
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001095 }
1096
1097 return 0;
1098}
1099
1100
1101int sccp_system_init(int (*outgoing)(struct msgb *data, void *ctx), void *ctx)
1102{
1103 sccp_system.write_data = outgoing;
1104 sccp_system.write_context = ctx;
1105
1106 return 0;
1107}
1108
1109/* oh my god a real SCCP packet. need to dispatch it now */
1110int sccp_system_incoming(struct msgb *msgb)
1111{
1112 if (msgb_l2len(msgb) < 1 ) {
1113 DEBUGP(DSCCP, "Too short packet\n");
1114 return -1;
1115 }
1116
1117 int type = msgb->l2h[0];
1118
1119 switch(type) {
1120 case SCCP_MSG_TYPE_CR:
1121 return _sccp_handle_connection_request(msgb);
1122 break;
1123 case SCCP_MSG_TYPE_RLSD:
1124 return _sccp_handle_connection_released(msgb);
1125 break;
1126 case SCCP_MSG_TYPE_CREF:
1127 return _sccp_handle_connection_refused(msgb);
1128 break;
1129 case SCCP_MSG_TYPE_CC:
1130 return _sccp_handle_connection_confirm(msgb);
1131 break;
1132 case SCCP_MSG_TYPE_RLC:
1133 return _sccp_handle_connection_release_complete(msgb);
1134 break;
1135 case SCCP_MSG_TYPE_DT1:
1136 return _sccp_handle_connection_dt1(msgb);
1137 break;
1138 case SCCP_MSG_TYPE_UDT:
1139 return _sccp_handle_read(msgb);
1140 break;
1141 default:
1142 DEBUGP(DSCCP, "unimplemented msg type: %d\n", type);
1143 };
1144
1145 return -1;
1146}
1147
1148/* create a packet from the data */
1149int sccp_connection_write(struct sccp_connection *connection, struct msgb *data)
1150{
1151 if (connection->connection_state < SCCP_CONNECTION_STATE_CONFIRM
1152 || connection->connection_state > SCCP_CONNECTION_STATE_ESTABLISHED) {
1153 DEBUGP(DSCCP, "sccp_connection_write: Wrong connection state: %p %d\n",
1154 connection, connection->connection_state);
1155 return -1;
1156 }
1157
1158 return _sccp_send_connection_data(connection, data);
1159}
1160
Holger Hans Peter Freyther3b9516e2009-11-18 22:11:28 +01001161/*
1162 * Send a Inactivity Test message. The owner of the connection
1163 * should start a timer and call this method regularily. Calling
1164 * this every 60 seconds should be good enough.
1165 */
1166int sccp_connection_send_it(struct sccp_connection *connection)
1167{
1168 if (connection->connection_state < SCCP_CONNECTION_STATE_CONFIRM
1169 || connection->connection_state > SCCP_CONNECTION_STATE_ESTABLISHED) {
1170 DEBUGP(DSCCP, "sccp_connection_write: Wrong connection state: %p %d\n",
1171 connection, connection->connection_state);
1172 return -1;
1173 }
1174
1175 return _sccp_send_connection_it(connection);
1176}
1177
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001178/* send a connection release and wait for the connection released */
1179int sccp_connection_close(struct sccp_connection *connection, int cause)
1180{
1181 if (connection->connection_state < SCCP_CONNECTION_STATE_CONFIRM
1182 || connection->connection_state > SCCP_CONNECTION_STATE_ESTABLISHED) {
1183 DEBUGPC(DSCCP, "Can not close the connection. It was never opened: %p %d\n",
1184 connection, connection->connection_state);
1185 return -1;
1186 }
1187
1188 return _sccp_send_connection_released(connection, cause);
1189}
1190
1191int sccp_connection_free(struct sccp_connection *connection)
1192{
1193 if (connection->connection_state > SCCP_CONNECTION_STATE_NONE
1194 && connection->connection_state < SCCP_CONNECTION_STATE_RELEASE_COMPLETE) {
1195 DEBUGP(DSCCP, "The connection needs to be released before it is freed");
1196 return -1;
1197 }
1198
1199 talloc_free(connection);
1200 return 0;
1201}
1202
1203struct sccp_connection *sccp_connection_socket(void)
1204{
1205 return talloc_zero(tall_sccp_ctx, struct sccp_connection);
1206}
1207
1208int sccp_connection_connect(struct sccp_connection *conn,
1209 const struct sockaddr_sccp *local,
1210 struct msgb *data)
1211{
1212 return _sccp_send_connection_request(conn, local, data);
1213}
1214
1215int sccp_connection_set_incoming(const struct sockaddr_sccp *sock,
1216 int (*accept_cb)(struct sccp_connection *, void *), void *context)
1217{
1218 struct sccp_data_callback *cb;
1219
1220 if (!sock)
1221 return -2;
1222
1223 cb = _find_ssn(sock->sccp_ssn);
1224 if (!cb)
1225 return -1;
1226
1227 cb->accept_cb = accept_cb;
1228 cb->accept_context = context;
1229 return 0;
1230}
1231
1232int sccp_write(struct msgb *data, const struct sockaddr_sccp *in,
1233 const struct sockaddr_sccp *out, int class)
1234{
1235 return _sccp_send_data(class, in, out, data);
1236}
1237
1238int sccp_set_read(const struct sockaddr_sccp *sock,
1239 int (*read_cb)(struct msgb *, unsigned int, void *), void *context)
1240{
1241 struct sccp_data_callback *cb;
1242
1243 if (!sock)
1244 return -2;
1245
1246 cb = _find_ssn(sock->sccp_ssn);
1247 if (!cb)
1248 return -1;
1249
1250 cb->read_cb = read_cb;
1251 cb->read_context = context;
1252 return 0;
1253}
1254
1255static_assert(sizeof(struct sccp_source_reference) <= sizeof(u_int32_t), enough_space);
1256
1257u_int32_t sccp_src_ref_to_int(struct sccp_source_reference *ref)
1258{
1259 u_int32_t src_ref = 0;
1260 memcpy(&src_ref, ref, sizeof(*ref));
1261 return src_ref;
1262}
1263
1264struct sccp_source_reference sccp_src_ref_from_int(u_int32_t int_ref)
1265{
1266 struct sccp_source_reference ref;
1267 memcpy(&ref, &int_ref, sizeof(ref));
1268 return ref;
1269}
1270
Holger Hans Peter Freythera692fbc2010-01-13 09:55:43 +01001271int sccp_determine_msg_type(struct msgb *msg)
1272{
1273 if (msgb_l2len(msg) < 1)
1274 return -1;
1275
1276 return msg->l2h[0];
1277}
1278
Holger Hans Peter Freythercaf49b42010-01-29 04:31:51 +01001279int sccp_parse_header(struct msgb *msg, struct sccp_parse_result *result)
1280{
1281 int type;
1282
1283 if (msgb_l2len(msg) < 1)
1284 return -1;
1285
1286 type = msg->l2h[0];
1287 switch(type) {
1288 case SCCP_MSG_TYPE_CR:
1289 return _sccp_parse_connection_request(msg, result);
1290 break;
1291 case SCCP_MSG_TYPE_RLSD:
1292 return _sccp_parse_connection_released(msg, result);
1293 break;
1294 case SCCP_MSG_TYPE_CREF:
1295 return _sccp_parse_connection_refused(msg, result);
1296 break;
1297 case SCCP_MSG_TYPE_CC:
1298 return _sccp_parse_connection_confirm(msg, result);
1299 break;
1300 case SCCP_MSG_TYPE_RLC:
1301 return _sccp_parse_connection_release_complete(msg, result);
1302 break;
1303 case SCCP_MSG_TYPE_DT1:
1304 return _sccp_parse_connection_dt1(msg, result);
1305 break;
1306 case SCCP_MSG_TYPE_UDT:
1307 return _sccp_parse_udt(msg, result);
1308 break;
1309 };
1310
1311 return -1;
1312}
1313
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001314static __attribute__((constructor)) void on_dso_load(void)
1315{
1316 tall_sccp_ctx = talloc_named_const(NULL, 1, "sccp");
1317}
1318
1319static __attribute__((destructor)) void on_dso_unload(void)
1320{
1321 talloc_report_full(tall_sccp_ctx, stderr);
1322}