blob: e0fd02e0e7b4f7cad8b06973b64e54f9e8630ee4 [file] [log] [blame]
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001/*
2 * SCCP management code
3 *
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +01004 * (C) 2009, 2010 by Holger Hans Peter Freyther <zecke@selfish.org>
Holger Hans Peter Freyther6ae65722010-02-03 18:10:07 +01005 * (C) 2009, 2010 by On-Waves
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02006 *
7 * All Rights Reserved
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 *
23 */
24
25#include <string.h>
26
Harald Weltedfe6c7d2010-02-20 16:24:02 +010027#include <osmocore/msgb.h>
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +020028#include <openbsc/debug.h>
Harald Weltedfe6c7d2010-02-20 16:24:02 +010029#include <osmocore/talloc.h>
Holger Hans Peter Freyther8a69cb22010-02-12 22:44:50 +010030
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +020031#include <sccp/sccp.h>
32
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +020033
34static void *tall_sccp_ctx;
35static LLIST_HEAD(sccp_connections);
36
37#define SCCP_MSG_SIZE 4096
38#define SCCP_MSG_HEADROOM 128
39
40/* global data */
41const struct sockaddr_sccp sccp_ssn_bssap = {
42 .sccp_family = 0,
43 .sccp_ssn = SCCP_SSN_BSSAP,
44};
45
46struct sccp_system {
47 /* layer3 -> layer2 */
Holger Hans Peter Freyther3c1221e2010-03-26 05:44:21 +010048 void (*write_data)(struct msgb *data, void *context);
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +020049 void *write_context;
50};
51
52
53static struct sccp_system sccp_system = {
54 .write_data = NULL,
55};
56
57struct sccp_data_callback {
58 /* connection based */
59 int (*accept_cb)(struct sccp_connection *, void *);
60 void *accept_context;
61
62 /* connection less */
63 int (*read_cb)(struct msgb *, unsigned int, void *);
64 void *read_context;
65
66 u_int8_t ssn;
67 struct llist_head callback;
68};
69
70static LLIST_HEAD(sccp_callbacks);
71
72static struct sccp_data_callback *_find_ssn(u_int8_t ssn)
73{
74 struct sccp_data_callback *cb;
75
76 llist_for_each_entry(cb, &sccp_callbacks, callback) {
77 if (cb->ssn == ssn)
78 return cb;
79 }
80
81 /* need to add one */
82 cb = talloc_zero(tall_sccp_ctx, struct sccp_data_callback);
83 if (!cb) {
84 DEBUGP(DSCCP, "Failed to allocate sccp callback.\n");
85 return NULL;
86 }
87
88 cb->ssn = ssn;
89 llist_add_tail(&cb->callback, &sccp_callbacks);
90 return cb;
91}
92
93
Holger Hans Peter Freyther3c1221e2010-03-26 05:44:21 +010094static void _send_msg(struct msgb *msg)
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +020095{
Holger Hans Peter Freyther3c1221e2010-03-26 05:44:21 +010096 sccp_system.write_data(msg, sccp_system.write_context);
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +020097}
98
99/*
100 * parsing routines
101 */
102static int copy_address(struct sccp_address *addr, u_int8_t offset, struct msgb *msgb)
103{
104 struct sccp_called_party_address *party;
105
106 int room = msgb_l2len(msgb) - offset;
107 u_int8_t read = 0;
108 u_int8_t length;
109
110 if (room <= 0) {
111 DEBUGP(DSCCP, "Not enough room for an address: %u\n", room);
112 return -1;
113 }
114
115 length = msgb->l2h[offset];
116 if (room <= length) {
117 DEBUGP(DSCCP, "Not enough room for optional data %u %u\n", room, length);
118 return -1;
119 }
120
121
122 party = (struct sccp_called_party_address *)(msgb->l2h + offset + 1);
123 if (party->point_code_indicator) {
124 if (length <= read + 2) {
125 DEBUGP(DSCCP, "POI does not fit %u\n", length);
126 return -1;
127 }
128
129
130 memcpy(&addr->poi, &party->data[read], 2);
131 read += 2;
132 }
133
134 if (party->ssn_indicator) {
135 if (length <= read + 1) {
136 DEBUGP(DSCCP, "SSN does not fit %u\n", length);
137 return -1;
138 }
139
140 addr->ssn = party->data[read];
141 read += 1;
142 }
143
144 if (party->global_title_indicator) {
145 DEBUGP(DSCCP, "GTI not supported %u\n", *(u_int8_t *)party);
146 return -1;
147 }
148
149 addr->address = *party;
150 return 0;
151}
152
153static int check_address(struct sccp_address *addr)
154{
155 /* ignore point_code_indicator... it should be zero... but */
156 if (addr->address.ssn_indicator != 1
157 || addr->address.global_title_indicator == 1
158 || addr->address.routing_indicator != 1) {
159 DEBUGP(DSCCP, "Invalid called address according to 08.06: 0x%x 0x%x\n",
160 *(u_int8_t *)&addr->address, addr->ssn);
161 return -1;
162 }
163
164 return 0;
165}
166
167static int _sccp_parse_optional_data(const int offset,
168 struct msgb *msgb, struct sccp_optional_data *data)
169{
170 u_int16_t room = msgb_l2len(msgb) - offset;
171 u_int16_t read = 0;
172
173 while (room > read) {
174 u_int8_t type = msgb->l2h[offset + read];
175 if (type == SCCP_PNC_END_OF_OPTIONAL)
176 return 0;
177
178 if (read + 1 >= room) {
179 DEBUGP(DSCCP, "no place for length\n");
180 return 0;
181 }
182
183 u_int8_t length = msgb->l2h[offset + read + 1];
184 read += 2 + length;
185
186
187 if (room <= read) {
188 DEBUGP(DSCCP, "no space for the data: type: %d read: %d room: %d l2: %d\n",
189 type, read, room, msgb_l2len(msgb));
190 return 0;
191 }
192
193 if (type == SCCP_PNC_DATA) {
194 data->data_len = length;
195 data->data_start = offset + read - length;
196 }
197
198 }
199
200 return -1;
201}
202
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100203int _sccp_parse_connection_request(struct msgb *msgb, struct sccp_parse_result *result)
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +0100204{
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100205 static const u_int32_t header_size =
206 sizeof(struct sccp_connection_request);
207 static const u_int32_t optional_offset =
208 offsetof(struct sccp_connection_request, optional_start);
209 static const u_int32_t called_offset =
210 offsetof(struct sccp_connection_request, variable_called);
211
Holger Hans Peter Freythere2c50282010-02-20 00:36:03 +0100212 struct sccp_connection_request *req = (struct sccp_connection_request *)msgb->l2h;
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100213 struct sccp_optional_data optional_data;
214
215 /* header check */
216 if (msgb_l2len(msgb) < header_size) {
217 DEBUGP(DSCCP, "msgb < header_size %u %u\n",
218 msgb_l2len(msgb), header_size);
219 return -1;
220 }
221
222 /* copy out the calling and called address. Add the offset */
223 if (copy_address(&result->called, called_offset + req->variable_called, msgb) != 0)
224 return -1;
225
226 if (check_address(&result->called) != 0) {
227 DEBUGP(DSCCP, "Invalid called address according to 08.06: 0x%x 0x%x\n",
228 *(u_int8_t *)&result->called.address, result->called.ssn);
229 return -1;
230 }
231
232 result->source_local_reference = &req->source_local_reference;
233
234 /*
235 * parse optional data.
236 */
237 memset(&optional_data, 0, sizeof(optional_data));
238 if (_sccp_parse_optional_data(optional_offset + req->optional_start, msgb, &optional_data) != 0) {
239 DEBUGP(DSCCP, "parsing of optional data failed.\n");
240 return -1;
241 }
242
243 if (optional_data.data_len != 0) {
244 msgb->l3h = &msgb->l2h[optional_data.data_start];
245 result->data_len = optional_data.data_len;
246 } else {
247 result->data_len = 0;
248 }
249
250 return 0;
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +0100251}
252
Holger Hans Peter Freyther88fe6ee2010-01-29 03:49:32 +0100253int _sccp_parse_connection_released(struct msgb *msgb, struct sccp_parse_result *result)
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +0100254{
Holger Hans Peter Freyther88fe6ee2010-01-29 03:49:32 +0100255 static int header_size = sizeof(struct sccp_connection_released);
256 static int optional_offset = offsetof(struct sccp_connection_released, optional_start);
257
258 struct sccp_optional_data optional_data;
259 struct sccp_connection_released *rls = (struct sccp_connection_released *) msgb->l2h;
260
261 /* we don't have enough size for the struct */
262 if (msgb_l2len(msgb) < header_size) {
263 DEBUGP(DSCCP, "msgb > header_size %u %u\n",
264 msgb_l2len(msgb), header_size);
265 return -1;
266 }
267
268 memset(&optional_data, 0, sizeof(optional_data));
269 if (_sccp_parse_optional_data(optional_offset + rls->optional_start, msgb, &optional_data) != 0) {
270 DEBUGP(DSCCP, "parsing of optional data failed.\n");
271 return -1;
272 }
273
274 result->source_local_reference = &rls->source_local_reference;
275 result->destination_local_reference = &rls->destination_local_reference;
276
277 if (optional_data.data_len != 0) {
278 msgb->l3h = &msgb->l2h[optional_data.data_start];
279 result->data_len = optional_data.data_len;
280 } else {
281 result->data_len = 0;
282 }
283
284 return 0;
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +0100285}
286
Holger Hans Peter Freytherfe5de4e2010-01-29 03:58:12 +0100287int _sccp_parse_connection_refused(struct msgb *msgb, struct sccp_parse_result *result)
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +0100288{
Holger Hans Peter Freytherfe5de4e2010-01-29 03:58:12 +0100289 static const u_int32_t header_size =
290 sizeof(struct sccp_connection_refused);
291 static int optional_offset = offsetof(struct sccp_connection_refused, optional_start);
292
293 struct sccp_optional_data optional_data;
294 struct sccp_connection_refused *ref;
295
296 /* header check */
297 if (msgb_l2len(msgb) < header_size) {
298 DEBUGP(DSCCP, "msgb < header_size %u %u\n",
299 msgb_l2len(msgb), header_size);
300 return -1;
301 }
302
303 ref = (struct sccp_connection_refused *) msgb->l2h;
304
305 result->destination_local_reference = &ref->destination_local_reference;
306
307 memset(&optional_data, 0, sizeof(optional_data));
308 if (_sccp_parse_optional_data(optional_offset + ref->optional_start, msgb, &optional_data) != 0) {
309 DEBUGP(DSCCP, "parsing of optional data failed.\n");
310 return -1;
311 }
312
313 /* optional data */
314 if (optional_data.data_len != 0) {
315 msgb->l3h = &msgb->l2h[optional_data.data_start];
316 result->data_len = optional_data.data_len;
317 } else {
318 result->data_len = 0;
319 }
320
321 return 0;
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +0100322}
323
Holger Hans Peter Freytherca1d1d12010-01-29 04:03:00 +0100324int _sccp_parse_connection_confirm(struct msgb *msgb, struct sccp_parse_result *result)
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +0100325{
Holger Hans Peter Freytherca1d1d12010-01-29 04:03:00 +0100326 static u_int32_t header_size =
327 sizeof(struct sccp_connection_confirm);
328 static const u_int32_t optional_offset =
329 offsetof(struct sccp_connection_confirm, optional_start);
330
331 struct sccp_optional_data optional_data;
332 struct sccp_connection_confirm *con;
333
334 /* header check */
335 if (msgb_l2len(msgb) < header_size) {
336 DEBUGP(DSCCP, "msgb < header_size %u %u\n",
337 msgb_l2len(msgb), header_size);
338 return -1;
339 }
340
341 con = (struct sccp_connection_confirm *) msgb->l2h;
342 result->destination_local_reference = &con->destination_local_reference;
343 result->source_local_reference = &con->source_local_reference;
344
345 memset(&optional_data, 0, sizeof(optional_data));
346 if (_sccp_parse_optional_data(optional_offset + con->optional_start, msgb, &optional_data) != 0) {
347 DEBUGP(DSCCP, "parsing of optional data failed.\n");
348 return -1;
349 }
350
351 if (optional_data.data_len != 0) {
352 msgb->l3h = &msgb->l2h[optional_data.data_start];
353 result->data_len = optional_data.data_len;
354 } else {
355 result->data_len = 0;
356 }
357
358 return 0;
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +0100359}
360
Holger Hans Peter Freyther18c5cad2010-01-29 04:19:56 +0100361int _sccp_parse_connection_release_complete(struct msgb *msgb, struct sccp_parse_result *result)
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +0100362{
Holger Hans Peter Freyther18c5cad2010-01-29 04:19:56 +0100363 static int header_size = sizeof(struct sccp_connection_release_complete);
364
365 struct sccp_connection_release_complete *cmpl;
366
367 /* header check */
368 if (msgb_l2len(msgb) < header_size) {
369 DEBUGP(DSCCP, "msgb < header_size %u %u\n",
370 msgb_l2len(msgb), header_size);
371 return -1;
372 }
373
374 cmpl = (struct sccp_connection_release_complete *) msgb->l2h;
375 result->source_local_reference = &cmpl->source_local_reference;
376 result->destination_local_reference = &cmpl->destination_local_reference;
377
378 return 0;
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +0100379}
380
Holger Hans Peter Freytheref845392010-01-29 04:31:00 +0100381int _sccp_parse_connection_dt1(struct msgb *msgb, struct sccp_parse_result *result)
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +0100382{
Holger Hans Peter Freytheref845392010-01-29 04:31:00 +0100383 static int header_size = sizeof(struct sccp_data_form1);
384 static int variable_offset = offsetof(struct sccp_data_form1, variable_start);
385
386 struct sccp_data_form1 *dt1 = (struct sccp_data_form1 *)msgb->l2h;
387
388 /* we don't have enough size for the struct */
389 if (msgb_l2len(msgb) < header_size) {
390 DEBUGP(DSCCP, "msgb > header_size %u %u\n",
391 msgb_l2len(msgb), header_size);
392 return -1;
393 }
394
395 if (dt1->segmenting != 0) {
396 DEBUGP(DSCCP, "This packet has segmenting, not supported: %d\n", dt1->segmenting);
397 return -1;
398 }
399
400 result->destination_local_reference = &dt1->destination_local_reference;
401
402 /* some more size checks in here */
403 if (msgb_l2len(msgb) < variable_offset + dt1->variable_start + 1) {
404 DEBUGP(DSCCP, "Not enough space for variable start: %u %u\n",
405 msgb_l2len(msgb), dt1->variable_start);
406 return -1;
407 }
408
409 result->data_len = msgb->l2h[variable_offset + dt1->variable_start];
410 msgb->l3h = &msgb->l2h[dt1->variable_start + variable_offset + 1];
411
412 if (msgb_l3len(msgb) < result->data_len) {
413 DEBUGP(DSCCP, "Not enough room for the payload: %u %u\n",
414 msgb_l3len(msgb), result->data_len);
415 return -1;
416 }
417
418 return 0;
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +0100419}
420
421int _sccp_parse_udt(struct msgb *msgb, struct sccp_parse_result *result)
422{
423 static const u_int32_t header_size = sizeof(struct sccp_data_unitdata);
424 static const u_int32_t called_offset = offsetof(struct sccp_data_unitdata, variable_called);
425 static const u_int32_t calling_offset = offsetof(struct sccp_data_unitdata, variable_calling);
426 static const u_int32_t data_offset = offsetof(struct sccp_data_unitdata, variable_data);
427
428 struct sccp_data_unitdata *udt = (struct sccp_data_unitdata *)msgb->l2h;
429
430 if (msgb_l2len(msgb) < header_size) {
431 DEBUGP(DSCCP, "msgb < header_size %u %u\n",
432 msgb_l2len(msgb), header_size);
433 return -1;
434 }
435
436 /* copy out the calling and called address. Add the off */
437 if (copy_address(&result->called, called_offset + udt->variable_called, msgb) != 0)
438 return -1;
439
440 if (check_address(&result->called) != 0) {
441 DEBUGP(DSCCP, "Invalid called address according to 08.06: 0x%x 0x%x\n",
442 *(u_int8_t *)&result->called.address, result->called.ssn);
443 return -1;
444 }
445
446 if (copy_address(&result->calling, calling_offset + udt->variable_calling, msgb) != 0)
447 return -1;
448
449 if (check_address(&result->calling) != 0) {
450 DEBUGP(DSCCP, "Invalid called address according to 08.06: 0x%x 0x%x\n",
451 *(u_int8_t *)&result->called.address, result->called.ssn);
452 }
453
454 /* we don't have enough size for the data */
455 if (msgb_l2len(msgb) < data_offset + udt->variable_data + 1) {
456 DEBUGP(DSCCP, "msgb < header + offset %u %u %u\n",
457 msgb_l2len(msgb), header_size, udt->variable_data);
458 return -1;
459 }
460
461
462 msgb->l3h = &udt->data[udt->variable_data];
Holger Hans Peter Freyther8fabf512010-02-12 23:08:21 +0100463 result->data_len = msgb_l3len(msgb);
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +0100464
465 if (msgb_l3len(msgb) != msgb->l3h[-1]) {
Holger Hans Peter Freyther3cb28902010-01-30 10:34:18 +0100466 DEBUGP(DSCCP, "msgb is truncated is: %u should: %u\n",
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +0100467 msgb_l3len(msgb), msgb->l3h[-1]);
468 return -1;
469 }
470
471 return 0;
472}
473
Holger Hans Peter Freythere1d50672010-02-26 19:26:35 +0100474static int _sccp_parse_it(struct msgb *msgb, struct sccp_parse_result *result)
475{
476 static const u_int32_t header_size = sizeof(struct sccp_data_it);
477
478 struct sccp_data_it *it;
479
480 if (msgb_l2len(msgb) < header_size) {
481 DEBUGP(DSCCP, "msgb < header_size %u %u\n",
482 msgb_l2len(msgb), header_size);
483 return -1;
484 }
485
486 it = (struct sccp_data_it *) msgb->l2h;
487 result->data_len = 0;
488 result->source_local_reference = &it->source_local_reference;
489 result->destination_local_reference = &it->destination_local_reference;
490 return 0;
491}
492
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +0100493
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200494/*
495 * Send UDT. Currently we have a fixed address...
496 */
497static int _sccp_send_data(int class, const struct sockaddr_sccp *in,
498 const struct sockaddr_sccp *out, struct msgb *payload)
499{
500 struct sccp_data_unitdata *udt;
501 u_int8_t *data;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200502
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
Holger Hans Peter Freyther3c1221e2010-03-26 05:44:21 +0100535 _send_msg(msg);
536 return 0;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200537}
538
539static int _sccp_handle_read(struct msgb *msgb)
540{
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200541 struct sccp_data_callback *cb;
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +0100542 struct sccp_parse_result result;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200543
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +0100544 if (_sccp_parse_udt(msgb, &result) != 0)
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200545 return -1;
546
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +0100547 cb = _find_ssn(result.called.ssn);
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200548 if (!cb || !cb->read_cb) {
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +0100549 DEBUGP(DSCCP, "No routing for UDT for called SSN: %u\n", result.called.ssn);
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200550 return -1;
551 }
552
553 /* sanity check */
554 return cb->read_cb(msgb, msgb_l3len(msgb), cb->read_context);
555}
556
557/*
558 * handle connection orientated methods
559 */
560static int source_local_reference_is_free(struct sccp_source_reference *reference)
561{
562 struct sccp_connection *connection;
563
564 llist_for_each_entry(connection, &sccp_connections, list) {
565 if (memcmp(reference, &connection->source_local_reference, sizeof(*reference)) == 0)
566 return -1;
567 }
568
569 return 0;
570}
571
572static int destination_local_reference_is_free(struct sccp_source_reference *reference)
573{
574 struct sccp_connection *connection;
575
576 llist_for_each_entry(connection, &sccp_connections, list) {
577 if (memcmp(reference, &connection->destination_local_reference, sizeof(*reference)) == 0)
578 return -1;
579 }
580
581 return 0;
582}
583
584static int assign_source_local_reference(struct sccp_connection *connection)
585{
586 static u_int32_t last_ref = 0x30000;
587 int wrapped = 0;
588
589 do {
590 struct sccp_source_reference reference;
591 reference.octet1 = (last_ref >> 0) & 0xff;
592 reference.octet2 = (last_ref >> 8) & 0xff;
593 reference.octet3 = (last_ref >> 16) & 0xff;
594
595 ++last_ref;
596 /* do not use the reversed word and wrap around */
597 if ((last_ref & 0x00FFFFFF) == 0x00FFFFFF) {
598 DEBUGP(DSCCP, "Wrapped searching for a free code\n");
599 last_ref = 0;
600 ++wrapped;
601 }
602
603 if (source_local_reference_is_free(&reference) == 0) {
604 connection->source_local_reference = reference;
605 return 0;
606 }
607 } while (wrapped != 2);
608
609 DEBUGP(DSCCP, "Finding a free reference failed\n");
610 return -1;
611}
612
613static void _sccp_set_connection_state(struct sccp_connection *connection, int new_state)
614{
615 int old_state = connection->connection_state;
616
617 connection->connection_state = new_state;
618 if (connection->state_cb)
619 connection->state_cb(connection, old_state);
620}
621
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100622static int _sccp_send_refuse(struct sccp_source_reference *src_ref, int cause)
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200623{
624 struct msgb *msgb;
625 struct sccp_connection_refused *ref;
626 u_int8_t *data;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200627
628 msgb = msgb_alloc_headroom(SCCP_MSG_SIZE,
629 SCCP_MSG_HEADROOM, "sccp ref");
630 msgb->l2h = &msgb->data[0];
631
632 ref = (struct sccp_connection_refused *) msgb_put(msgb, sizeof(*ref));
633 ref->type = SCCP_MSG_TYPE_CREF;
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100634 memcpy(&ref->destination_local_reference, src_ref,
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200635 sizeof(struct sccp_source_reference));
636 ref->cause = cause;
637 ref->optional_start = 1;
638
639 data = msgb_put(msgb, 1);
640 data[0] = SCCP_PNC_END_OF_OPTIONAL;
641
Holger Hans Peter Freyther3c1221e2010-03-26 05:44:21 +0100642 _send_msg(msgb);
643 return 0;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200644}
645
646static int _sccp_send_connection_confirm(struct sccp_connection *connection)
647{
648 struct msgb *response;
649 struct sccp_connection_confirm *confirm;
650 u_int8_t *optional_data;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200651
652 if (assign_source_local_reference(connection) != 0)
653 return -1;
654
655 response = msgb_alloc_headroom(SCCP_MSG_SIZE,
656 SCCP_MSG_HEADROOM, "sccp confirm");
657 response->l2h = &response->data[0];
658
659 confirm = (struct sccp_connection_confirm *) msgb_put(response, sizeof(*confirm));
660
661 confirm->type = SCCP_MSG_TYPE_CC;
662 memcpy(&confirm->destination_local_reference,
663 &connection->destination_local_reference,
664 sizeof(connection->destination_local_reference));
665 memcpy(&confirm->source_local_reference,
666 &connection->source_local_reference,
667 sizeof(connection->source_local_reference));
668 confirm->proto_class = 2;
669 confirm->optional_start = 1;
670
671 optional_data = (u_int8_t *) msgb_put(response, 1);
672 optional_data[0] = SCCP_PNC_END_OF_OPTIONAL;
673
Holger Hans Peter Freyther3c1221e2010-03-26 05:44:21 +0100674 _send_msg(response);
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200675 _sccp_set_connection_state(connection, SCCP_CONNECTION_STATE_ESTABLISHED);
Holger Hans Peter Freyther3c1221e2010-03-26 05:44:21 +0100676 return 0;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200677}
678
679static int _sccp_send_connection_request(struct sccp_connection *connection,
680 const struct sockaddr_sccp *called, struct msgb *msg)
681{
682 struct msgb *request;
683 struct sccp_connection_request *req;
684 u_int8_t *data;
685 u_int8_t extra_size = 3 + 1;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200686
687
688 if (msg && (msgb_l3len(msg) < 3 || msgb_l3len(msg) > 130)) {
689 DEBUGP(DSCCP, "Invalid amount of data... %d\n", msgb_l3len(msg));
690 return -1;
691 }
692
693 /* try to find a id */
694 if (assign_source_local_reference(connection) != 0) {
695 DEBUGP(DSCCP, "Assigning a local reference failed.\n");
696 _sccp_set_connection_state(connection, SCCP_CONNECTION_STATE_SETUP_ERROR);
697 return -1;
698 }
699
700
701 if (msg)
702 extra_size += 2 + msgb_l3len(msg);
703 request = msgb_alloc_headroom(SCCP_MSG_SIZE,
704 SCCP_MSG_HEADROOM, "sccp connection request");
705 request->l2h = &request->data[0];
706 req = (struct sccp_connection_request *) msgb_put(request, sizeof(*req));
707
708 req->type = SCCP_MSG_TYPE_CR;
709 memcpy(&req->source_local_reference, &connection->source_local_reference,
710 sizeof(connection->source_local_reference));
711 req->proto_class = 2;
712 req->variable_called = 2;
713 req->optional_start = 4;
714
715 /* write the called party address */
716 data = msgb_put(request, 1 + 2);
717 data[0] = 2;
718 data[1] = 0x42;
719 data[2] = called->sccp_ssn;
720
721 /* write the payload */
722 if (msg) {
723 data = msgb_put(request, 2 + msgb_l3len(msg));
724 data[0] = SCCP_PNC_DATA;
725 data[1] = msgb_l3len(msg);
726 memcpy(&data[2], msg->l3h, msgb_l3len(msg));
727 }
728
729 data = msgb_put(request, 1);
730 data[0] = SCCP_PNC_END_OF_OPTIONAL;
731
732 llist_add_tail(&connection->list, &sccp_connections);
733 _sccp_set_connection_state(connection, SCCP_CONNECTION_STATE_REQUEST);
734
Holger Hans Peter Freyther3c1221e2010-03-26 05:44:21 +0100735 _send_msg(request);
736 return 0;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200737}
738
739static int _sccp_send_connection_data(struct sccp_connection *conn, struct msgb *_data)
740{
741 struct msgb *msgb;
742 struct sccp_data_form1 *dt1;
743 u_int8_t *data;
744 int extra_size;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200745
746 if (msgb_l3len(_data) < 2 || msgb_l3len(_data) > 256) {
747 DEBUGP(DSCCP, "data size too big, segmenting unimplemented.\n");
748 return -1;
749 }
750
751 extra_size = 1 + msgb_l3len(_data);
752 msgb = msgb_alloc_headroom(SCCP_MSG_SIZE,
753 SCCP_MSG_HEADROOM, "sccp dt1");
754 msgb->l2h = &msgb->data[0];
755
756 dt1 = (struct sccp_data_form1 *) msgb_put(msgb, sizeof(*dt1));
757 dt1->type = SCCP_MSG_TYPE_DT1;
758 memcpy(&dt1->destination_local_reference, &conn->destination_local_reference,
759 sizeof(struct sccp_source_reference));
760 dt1->segmenting = 0;
761
762 /* copy the data */
763 dt1->variable_start = 1;
764 data = msgb_put(msgb, extra_size);
765 data[0] = extra_size - 1;
766 memcpy(&data[1], _data->l3h, extra_size - 1);
767
Holger Hans Peter Freyther3c1221e2010-03-26 05:44:21 +0100768 _send_msg(msgb);
769 return 0;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200770}
771
Holger Hans Peter Freyther3b9516e2009-11-18 22:11:28 +0100772static int _sccp_send_connection_it(struct sccp_connection *conn)
773{
774 struct msgb *msgb;
775 struct sccp_data_it *it;
Holger Hans Peter Freyther3b9516e2009-11-18 22:11:28 +0100776
777 msgb = msgb_alloc_headroom(SCCP_MSG_SIZE,
778 SCCP_MSG_HEADROOM, "sccp it");
779 msgb->l2h = &msgb->data[0];
780 it = (struct sccp_data_it *) msgb_put(msgb, sizeof(*it));
781 it->type = SCCP_MSG_TYPE_IT;
782 memcpy(&it->destination_local_reference, &conn->destination_local_reference,
783 sizeof(struct sccp_source_reference));
784 memcpy(&it->source_local_reference, &conn->source_local_reference,
785 sizeof(struct sccp_source_reference));
786
787 it->proto_class = 0x2;
788 it->sequencing[0] = it->sequencing[1] = 0;
789 it->credit = 0;
790
Holger Hans Peter Freyther3c1221e2010-03-26 05:44:21 +0100791 _send_msg(msgb);
792 return 0;
Holger Hans Peter Freyther3b9516e2009-11-18 22:11:28 +0100793}
794
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200795static int _sccp_send_connection_released(struct sccp_connection *conn, int cause)
796{
797 struct msgb *msg;
798 struct sccp_connection_released *rel;
799 u_int8_t *data;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200800
801 msg = msgb_alloc_headroom(SCCP_MSG_SIZE, SCCP_MSG_HEADROOM,
802 "sccp: connection released");
803 msg->l2h = &msg->data[0];
804 rel = (struct sccp_connection_released *) msgb_put(msg, sizeof(*rel));
805 rel->type = SCCP_MSG_TYPE_RLSD;
806 rel->release_cause = cause;
807
808 /* copy the source references */
809 memcpy(&rel->destination_local_reference, &conn->destination_local_reference,
810 sizeof(struct sccp_source_reference));
811 memcpy(&rel->source_local_reference, &conn->source_local_reference,
812 sizeof(struct sccp_source_reference));
813
814 data = msgb_put(msg, 1);
815 data[0] = SCCP_PNC_END_OF_OPTIONAL;
816
817 _sccp_set_connection_state(conn, SCCP_CONNECTION_STATE_RELEASE);
Holger Hans Peter Freyther3c1221e2010-03-26 05:44:21 +0100818 _send_msg(msg);
819 return 0;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200820}
821
822/*
823 * Open a connection. The following is going to happen:
824 *
825 * - Verify the packet, e.g. that we have no other connection
826 * that id.
827 * - Ask the user if he wants to accept the connection
828 * - Try to open the connection by assigning a source local reference
829 * and sending the packet
830 */
831static int _sccp_handle_connection_request(struct msgb *msgb)
832{
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100833 struct sccp_parse_result result;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200834
835 struct sccp_data_callback *cb;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200836 struct sccp_connection *connection;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200837
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100838 if (_sccp_parse_connection_request(msgb, &result) != 0)
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200839 return -1;
840
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100841 cb = _find_ssn(result.called.ssn);
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200842 if (!cb || !cb->accept_cb) {
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100843 DEBUGP(DSCCP, "No routing for CR for called SSN: %u\n", result.called.ssn);
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200844 return -1;
845 }
846
847 /* check if the system wants this connection */
848 connection = talloc_zero(tall_sccp_ctx, struct sccp_connection);
849 if (!connection) {
850 DEBUGP(DSCCP, "Allocation failed\n");
851 return -1;
852 }
853
854 /*
855 * sanity checks:
856 * - Is the source_local_reference in any other connection?
857 * then will call accept, assign a "destination" local reference
858 * and send a connection confirm, otherwise we will send a refuseed
859 * one....
860 */
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100861 if (destination_local_reference_is_free(result.source_local_reference) != 0) {
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200862 DEBUGP(DSCCP, "Need to reject connection with existing reference\n");
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100863 _sccp_send_refuse(result.source_local_reference, SCCP_REFUSAL_SCCP_FAILURE);
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200864 talloc_free(connection);
865 return -1;
866 }
867
868 connection->incoming = 1;
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100869 connection->destination_local_reference = *result.source_local_reference;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200870
871 if (cb->accept_cb(connection, cb->accept_context) != 0) {
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100872 _sccp_send_refuse(result.source_local_reference, SCCP_REFUSAL_END_USER_ORIGINATED);
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200873 _sccp_set_connection_state(connection, SCCP_CONNECTION_STATE_REFUSED);
874 talloc_free(connection);
875 return 0;
876 }
877
878
879 llist_add_tail(&connection->list, &sccp_connections);
880
881 if (_sccp_send_connection_confirm(connection) != 0) {
882 DEBUGP(DSCCP, "Sending confirm failed... no available source reference?\n");
883
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100884 _sccp_send_refuse(result.source_local_reference, SCCP_REFUSAL_SCCP_FAILURE);
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200885 _sccp_set_connection_state(connection, SCCP_CONNECTION_STATE_REFUSED);
886 llist_del(&connection->list);
887 talloc_free(connection);
888
889 return -1;
890 }
891
892 /*
893 * If we have data let us forward things.
894 */
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100895 if (result.data_len != 0 && connection->data_cb) {
896 connection->data_cb(connection, msgb, result.data_len);
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200897 }
898
899 return 0;
900}
901
902/* Handle the release confirmed */
Holger Hans Peter Freyther18c5cad2010-01-29 04:19:56 +0100903static int _sccp_handle_connection_release_complete(struct msgb *msgb)
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200904{
Holger Hans Peter Freyther18c5cad2010-01-29 04:19:56 +0100905 struct sccp_parse_result result;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200906 struct sccp_connection *conn;
907
Holger Hans Peter Freyther18c5cad2010-01-29 04:19:56 +0100908 if (_sccp_parse_connection_release_complete(msgb, &result) != 0)
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200909 return -1;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200910
911 /* find the connection */
912 llist_for_each_entry(conn, &sccp_connections, list) {
913 if (conn->data_cb
914 && memcmp(&conn->source_local_reference,
Holger Hans Peter Freyther18c5cad2010-01-29 04:19:56 +0100915 result.destination_local_reference,
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200916 sizeof(conn->source_local_reference)) == 0
917 && memcmp(&conn->destination_local_reference,
Holger Hans Peter Freyther18c5cad2010-01-29 04:19:56 +0100918 result.source_local_reference,
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200919 sizeof(conn->destination_local_reference)) == 0) {
920 goto found;
921 }
922 }
923
924
925 DEBUGP(DSCCP, "Release complete of unknown connection\n");
926 return -1;
927
928found:
929 llist_del(&conn->list);
930 _sccp_set_connection_state(conn, SCCP_CONNECTION_STATE_RELEASE_COMPLETE);
931 return 0;
932}
933
934/* Handle the Data Form 1 message */
Holger Hans Peter Freytheref845392010-01-29 04:31:00 +0100935static int _sccp_handle_connection_dt1(struct msgb *msgb)
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200936{
Holger Hans Peter Freytheref845392010-01-29 04:31:00 +0100937 struct sccp_parse_result result;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200938 struct sccp_connection *conn;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200939
Holger Hans Peter Freytheref845392010-01-29 04:31:00 +0100940 if (_sccp_parse_connection_dt1(msgb, &result) != 0)
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200941 return -1;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200942
943 /* lookup if we have a connection with the given reference */
944 llist_for_each_entry(conn, &sccp_connections, list) {
945 if (conn->data_cb
946 && memcmp(&conn->source_local_reference,
Holger Hans Peter Freytheref845392010-01-29 04:31:00 +0100947 result.destination_local_reference,
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200948 sizeof(conn->source_local_reference)) == 0) {
Holger Hans Peter Freytheref845392010-01-29 04:31:00 +0100949 goto found;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200950 }
951 }
952
953 DEBUGP(DSCCP, "No connection found for dt1 data\n");
954 return -1;
Holger Hans Peter Freytheref845392010-01-29 04:31:00 +0100955
956found:
957 conn->data_cb(conn, msgb, result.data_len);
958 return 0;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200959}
960
961/* confirm a connection release */
962static int _sccp_send_connection_release_complete(struct sccp_connection *connection)
963{
964 struct msgb *msgb;
965 struct sccp_connection_release_complete *rlc;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200966
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
Holger Hans Peter Freyther3c1221e2010-03-26 05:44:21 +0100978 _send_msg(msgb);
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200979
980 /*
981 * Remove from the list of active connections and set the state. User code
982 * should now free the entry.
983 */
984 llist_del(&connection->list);
985 _sccp_set_connection_state(connection, SCCP_CONNECTION_STATE_RELEASE_COMPLETE);
Holger Hans Peter Freyther3c1221e2010-03-26 05:44:21 +0100986 return 0;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200987}
988
989/* connection released, send a released confirm */
Holger Hans Peter Freyther88fe6ee2010-01-29 03:49:32 +0100990static int _sccp_handle_connection_released(struct msgb *msgb)
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200991{
Holger Hans Peter Freyther88fe6ee2010-01-29 03:49:32 +0100992 struct sccp_parse_result result;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200993 struct sccp_connection *conn;
994
Holger Hans Peter Freyther88fe6ee2010-01-29 03:49:32 +0100995 if (_sccp_parse_connection_released(msgb, &result) == -1)
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200996 return -1;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200997
998 /* lookup if we have a connection with the given reference */
999 llist_for_each_entry(conn, &sccp_connections, list) {
1000 if (conn->data_cb
1001 && memcmp(&conn->source_local_reference,
Holger Hans Peter Freyther88fe6ee2010-01-29 03:49:32 +01001002 result.destination_local_reference,
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001003 sizeof(conn->source_local_reference)) == 0
1004 && memcmp(&conn->destination_local_reference,
Holger Hans Peter Freyther88fe6ee2010-01-29 03:49:32 +01001005 result.source_local_reference,
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001006 sizeof(conn->destination_local_reference)) == 0) {
1007 goto found;
1008 }
1009 }
1010
1011
1012 DEBUGP(DSCCP, "Unknown connection was released.\n");
1013 return -1;
1014
1015 /* we have found a connection */
1016found:
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001017 /* optional data */
Holger Hans Peter Freyther88fe6ee2010-01-29 03:49:32 +01001018 if (result.data_len != 0 && conn->data_cb) {
1019 conn->data_cb(conn, msgb, result.data_len);
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001020 }
1021
1022 /* generate a response */
1023 if (_sccp_send_connection_release_complete(conn) != 0) {
1024 DEBUGP(DSCCP, "Sending release confirmed failed\n");
1025 return -1;
1026 }
1027
1028 return 0;
1029}
1030
1031static int _sccp_handle_connection_refused(struct msgb *msgb)
1032{
Holger Hans Peter Freytherfe5de4e2010-01-29 03:58:12 +01001033 struct sccp_parse_result result;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001034 struct sccp_connection *conn;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001035
Holger Hans Peter Freytherfe5de4e2010-01-29 03:58:12 +01001036 if (_sccp_parse_connection_refused(msgb, &result) != 0)
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001037 return -1;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001038
1039 /* lookup if we have a connection with the given reference */
1040 llist_for_each_entry(conn, &sccp_connections, list) {
1041 if (conn->incoming == 0 && conn->data_cb
1042 && memcmp(&conn->source_local_reference,
Holger Hans Peter Freytherfe5de4e2010-01-29 03:58:12 +01001043 result.destination_local_reference,
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001044 sizeof(conn->source_local_reference)) == 0) {
1045 goto found;
1046 }
1047 }
1048
1049 DEBUGP(DSCCP, "Refused but no connection found\n");
1050 return -1;
1051
1052found:
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001053 /* optional data */
Holger Hans Peter Freytherfe5de4e2010-01-29 03:58:12 +01001054 if (result.data_len != 0 && conn->data_cb) {
1055 conn->data_cb(conn, msgb, result.data_len);
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001056 }
1057
1058
1059 llist_del(&conn->list);
1060 _sccp_set_connection_state(conn, SCCP_CONNECTION_STATE_REFUSED);
1061 return 0;
1062}
1063
1064static int _sccp_handle_connection_confirm(struct msgb *msgb)
1065{
Holger Hans Peter Freytherca1d1d12010-01-29 04:03:00 +01001066 struct sccp_parse_result result;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001067 struct sccp_connection *conn;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001068
Holger Hans Peter Freytherca1d1d12010-01-29 04:03:00 +01001069 if (_sccp_parse_connection_confirm(msgb, &result) != 0)
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001070 return -1;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001071
1072 /* lookup if we have a connection with the given reference */
1073 llist_for_each_entry(conn, &sccp_connections, list) {
1074 if (conn->incoming == 0 && conn->data_cb
1075 && memcmp(&conn->source_local_reference,
Holger Hans Peter Freytherca1d1d12010-01-29 04:03:00 +01001076 result.destination_local_reference,
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001077 sizeof(conn->source_local_reference)) == 0) {
1078 goto found;
1079 }
1080 }
1081
1082 DEBUGP(DSCCP, "Confirmed but no connection found\n");
1083 return -1;
1084
1085found:
1086 /* copy the addresses of the connection */
Holger Hans Peter Freytherca1d1d12010-01-29 04:03:00 +01001087 conn->destination_local_reference = *result.source_local_reference;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001088 _sccp_set_connection_state(conn, SCCP_CONNECTION_STATE_ESTABLISHED);
1089
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001090 /* optional data */
Holger Hans Peter Freytherca1d1d12010-01-29 04:03:00 +01001091 if (result.data_len != 0 && conn->data_cb) {
1092 conn->data_cb(conn, msgb, result.data_len);
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001093 }
1094
1095 return 0;
1096}
1097
1098
Holger Hans Peter Freyther3c1221e2010-03-26 05:44:21 +01001099int sccp_system_init(void (*outgoing)(struct msgb *data, void *ctx), void *ctx)
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001100{
1101 sccp_system.write_data = outgoing;
1102 sccp_system.write_context = ctx;
1103
1104 return 0;
1105}
1106
1107/* oh my god a real SCCP packet. need to dispatch it now */
1108int sccp_system_incoming(struct msgb *msgb)
1109{
1110 if (msgb_l2len(msgb) < 1 ) {
1111 DEBUGP(DSCCP, "Too short packet\n");
1112 return -1;
1113 }
1114
1115 int type = msgb->l2h[0];
1116
1117 switch(type) {
1118 case SCCP_MSG_TYPE_CR:
1119 return _sccp_handle_connection_request(msgb);
1120 break;
1121 case SCCP_MSG_TYPE_RLSD:
1122 return _sccp_handle_connection_released(msgb);
1123 break;
1124 case SCCP_MSG_TYPE_CREF:
1125 return _sccp_handle_connection_refused(msgb);
1126 break;
1127 case SCCP_MSG_TYPE_CC:
1128 return _sccp_handle_connection_confirm(msgb);
1129 break;
1130 case SCCP_MSG_TYPE_RLC:
1131 return _sccp_handle_connection_release_complete(msgb);
1132 break;
1133 case SCCP_MSG_TYPE_DT1:
1134 return _sccp_handle_connection_dt1(msgb);
1135 break;
1136 case SCCP_MSG_TYPE_UDT:
1137 return _sccp_handle_read(msgb);
1138 break;
1139 default:
1140 DEBUGP(DSCCP, "unimplemented msg type: %d\n", type);
1141 };
1142
1143 return -1;
1144}
1145
1146/* create a packet from the data */
1147int sccp_connection_write(struct sccp_connection *connection, struct msgb *data)
1148{
1149 if (connection->connection_state < SCCP_CONNECTION_STATE_CONFIRM
1150 || connection->connection_state > SCCP_CONNECTION_STATE_ESTABLISHED) {
1151 DEBUGP(DSCCP, "sccp_connection_write: Wrong connection state: %p %d\n",
1152 connection, connection->connection_state);
1153 return -1;
1154 }
1155
1156 return _sccp_send_connection_data(connection, data);
1157}
1158
Holger Hans Peter Freyther3b9516e2009-11-18 22:11:28 +01001159/*
1160 * Send a Inactivity Test message. The owner of the connection
1161 * should start a timer and call this method regularily. Calling
1162 * this every 60 seconds should be good enough.
1163 */
1164int sccp_connection_send_it(struct sccp_connection *connection)
1165{
1166 if (connection->connection_state < SCCP_CONNECTION_STATE_CONFIRM
1167 || connection->connection_state > SCCP_CONNECTION_STATE_ESTABLISHED) {
1168 DEBUGP(DSCCP, "sccp_connection_write: Wrong connection state: %p %d\n",
1169 connection, connection->connection_state);
1170 return -1;
1171 }
1172
1173 return _sccp_send_connection_it(connection);
1174}
1175
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001176/* send a connection release and wait for the connection released */
1177int sccp_connection_close(struct sccp_connection *connection, int cause)
1178{
1179 if (connection->connection_state < SCCP_CONNECTION_STATE_CONFIRM
1180 || connection->connection_state > SCCP_CONNECTION_STATE_ESTABLISHED) {
1181 DEBUGPC(DSCCP, "Can not close the connection. It was never opened: %p %d\n",
1182 connection, connection->connection_state);
1183 return -1;
1184 }
1185
1186 return _sccp_send_connection_released(connection, cause);
1187}
1188
1189int sccp_connection_free(struct sccp_connection *connection)
1190{
1191 if (connection->connection_state > SCCP_CONNECTION_STATE_NONE
1192 && connection->connection_state < SCCP_CONNECTION_STATE_RELEASE_COMPLETE) {
1193 DEBUGP(DSCCP, "The connection needs to be released before it is freed");
1194 return -1;
1195 }
1196
1197 talloc_free(connection);
1198 return 0;
1199}
1200
Holger Hans Peter Freytherb71517f2010-04-05 18:13:40 +02001201int sccp_connection_force_free(struct sccp_connection *con)
1202{
1203 if (con->connection_state > SCCP_CONNECTION_STATE_NONE &&
1204 con->connection_state < SCCP_CONNECTION_STATE_RELEASE_COMPLETE)
1205 llist_del(&con->list);
1206
1207 con->connection_state = SCCP_CONNECTION_STATE_REFUSED;
1208 sccp_connection_free(con);
1209 return 0;
1210}
1211
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001212struct sccp_connection *sccp_connection_socket(void)
1213{
1214 return talloc_zero(tall_sccp_ctx, struct sccp_connection);
1215}
1216
1217int sccp_connection_connect(struct sccp_connection *conn,
1218 const struct sockaddr_sccp *local,
1219 struct msgb *data)
1220{
1221 return _sccp_send_connection_request(conn, local, data);
1222}
1223
1224int sccp_connection_set_incoming(const struct sockaddr_sccp *sock,
1225 int (*accept_cb)(struct sccp_connection *, void *), void *context)
1226{
1227 struct sccp_data_callback *cb;
1228
1229 if (!sock)
1230 return -2;
1231
1232 cb = _find_ssn(sock->sccp_ssn);
1233 if (!cb)
1234 return -1;
1235
1236 cb->accept_cb = accept_cb;
1237 cb->accept_context = context;
1238 return 0;
1239}
1240
1241int sccp_write(struct msgb *data, const struct sockaddr_sccp *in,
1242 const struct sockaddr_sccp *out, int class)
1243{
1244 return _sccp_send_data(class, in, out, data);
1245}
1246
1247int sccp_set_read(const struct sockaddr_sccp *sock,
1248 int (*read_cb)(struct msgb *, unsigned int, void *), void *context)
1249{
1250 struct sccp_data_callback *cb;
1251
1252 if (!sock)
1253 return -2;
1254
1255 cb = _find_ssn(sock->sccp_ssn);
1256 if (!cb)
1257 return -1;
1258
1259 cb->read_cb = read_cb;
1260 cb->read_context = context;
1261 return 0;
1262}
1263
1264static_assert(sizeof(struct sccp_source_reference) <= sizeof(u_int32_t), enough_space);
1265
1266u_int32_t sccp_src_ref_to_int(struct sccp_source_reference *ref)
1267{
1268 u_int32_t src_ref = 0;
1269 memcpy(&src_ref, ref, sizeof(*ref));
1270 return src_ref;
1271}
1272
1273struct sccp_source_reference sccp_src_ref_from_int(u_int32_t int_ref)
1274{
1275 struct sccp_source_reference ref;
1276 memcpy(&ref, &int_ref, sizeof(ref));
1277 return ref;
1278}
1279
Holger Hans Peter Freythera692fbc2010-01-13 09:55:43 +01001280int sccp_determine_msg_type(struct msgb *msg)
1281{
1282 if (msgb_l2len(msg) < 1)
1283 return -1;
1284
1285 return msg->l2h[0];
1286}
1287
Holger Hans Peter Freythercaf49b42010-01-29 04:31:51 +01001288int sccp_parse_header(struct msgb *msg, struct sccp_parse_result *result)
1289{
1290 int type;
1291
1292 if (msgb_l2len(msg) < 1)
1293 return -1;
1294
1295 type = msg->l2h[0];
1296 switch(type) {
1297 case SCCP_MSG_TYPE_CR:
1298 return _sccp_parse_connection_request(msg, result);
1299 break;
1300 case SCCP_MSG_TYPE_RLSD:
1301 return _sccp_parse_connection_released(msg, result);
1302 break;
1303 case SCCP_MSG_TYPE_CREF:
1304 return _sccp_parse_connection_refused(msg, result);
1305 break;
1306 case SCCP_MSG_TYPE_CC:
1307 return _sccp_parse_connection_confirm(msg, result);
1308 break;
1309 case SCCP_MSG_TYPE_RLC:
1310 return _sccp_parse_connection_release_complete(msg, result);
1311 break;
1312 case SCCP_MSG_TYPE_DT1:
1313 return _sccp_parse_connection_dt1(msg, result);
1314 break;
1315 case SCCP_MSG_TYPE_UDT:
1316 return _sccp_parse_udt(msg, result);
1317 break;
Holger Hans Peter Freythere1d50672010-02-26 19:26:35 +01001318 case SCCP_MSG_TYPE_IT:
1319 return _sccp_parse_it(msg, result);
1320 break;
Holger Hans Peter Freythercaf49b42010-01-29 04:31:51 +01001321 };
1322
Holger Hans Peter Freythere1d50672010-02-26 19:26:35 +01001323 LOGP(DSCCP, LOGL_ERROR, "Unimplemented MSG Type: 0x%x\n", type);
Holger Hans Peter Freythercaf49b42010-01-29 04:31:51 +01001324 return -1;
1325}
1326
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001327static __attribute__((constructor)) void on_dso_load(void)
1328{
1329 tall_sccp_ctx = talloc_named_const(NULL, 1, "sccp");
1330}
1331
1332static __attribute__((destructor)) void on_dso_unload(void)
1333{
1334 talloc_report_full(tall_sccp_ctx, stderr);
1335}