blob: ac1a0a18d90e865df7fdb018a64dc881dc599e73 [file] [log] [blame]
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001/*
2 * SCCP management code
3 *
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +01004 * (C) 2009, 2010 by Holger Hans Peter Freyther <zecke@selfish.org>
5 * (C) 2009, 2010 by on-waves.com
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02006 *
7 * All Rights Reserved
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 *
23 */
24
25#include <string.h>
26
27#include <sccp/sccp.h>
28
29#include <openbsc/debug.h>
30#include <openbsc/talloc.h>
31#include <openbsc/linuxlist.h>
32
33static void *tall_sccp_ctx;
34static LLIST_HEAD(sccp_connections);
35
36#define SCCP_MSG_SIZE 4096
37#define SCCP_MSG_HEADROOM 128
38
39/* global data */
40const struct sockaddr_sccp sccp_ssn_bssap = {
41 .sccp_family = 0,
42 .sccp_ssn = SCCP_SSN_BSSAP,
43};
44
45struct sccp_system {
46 /* layer3 -> layer2 */
47 int (*write_data)(struct msgb *data, void *context);
48 void *write_context;
49};
50
51
52static struct sccp_system sccp_system = {
53 .write_data = NULL,
54};
55
56struct sccp_data_callback {
57 /* connection based */
58 int (*accept_cb)(struct sccp_connection *, void *);
59 void *accept_context;
60
61 /* connection less */
62 int (*read_cb)(struct msgb *, unsigned int, void *);
63 void *read_context;
64
65 u_int8_t ssn;
66 struct llist_head callback;
67};
68
69static LLIST_HEAD(sccp_callbacks);
70
71static struct sccp_data_callback *_find_ssn(u_int8_t ssn)
72{
73 struct sccp_data_callback *cb;
74
75 llist_for_each_entry(cb, &sccp_callbacks, callback) {
76 if (cb->ssn == ssn)
77 return cb;
78 }
79
80 /* need to add one */
81 cb = talloc_zero(tall_sccp_ctx, struct sccp_data_callback);
82 if (!cb) {
83 DEBUGP(DSCCP, "Failed to allocate sccp callback.\n");
84 return NULL;
85 }
86
87 cb->ssn = ssn;
88 llist_add_tail(&cb->callback, &sccp_callbacks);
89 return cb;
90}
91
92
93static int _send_msg(struct msgb *msg)
94{
95 return sccp_system.write_data(msg, sccp_system.write_context);
96}
97
98/*
99 * parsing routines
100 */
101static int copy_address(struct sccp_address *addr, u_int8_t offset, struct msgb *msgb)
102{
103 struct sccp_called_party_address *party;
104
105 int room = msgb_l2len(msgb) - offset;
106 u_int8_t read = 0;
107 u_int8_t length;
108
109 if (room <= 0) {
110 DEBUGP(DSCCP, "Not enough room for an address: %u\n", room);
111 return -1;
112 }
113
114 length = msgb->l2h[offset];
115 if (room <= length) {
116 DEBUGP(DSCCP, "Not enough room for optional data %u %u\n", room, length);
117 return -1;
118 }
119
120
121 party = (struct sccp_called_party_address *)(msgb->l2h + offset + 1);
122 if (party->point_code_indicator) {
123 if (length <= read + 2) {
124 DEBUGP(DSCCP, "POI does not fit %u\n", length);
125 return -1;
126 }
127
128
129 memcpy(&addr->poi, &party->data[read], 2);
130 read += 2;
131 }
132
133 if (party->ssn_indicator) {
134 if (length <= read + 1) {
135 DEBUGP(DSCCP, "SSN does not fit %u\n", length);
136 return -1;
137 }
138
139 addr->ssn = party->data[read];
140 read += 1;
141 }
142
143 if (party->global_title_indicator) {
144 DEBUGP(DSCCP, "GTI not supported %u\n", *(u_int8_t *)party);
145 return -1;
146 }
147
148 addr->address = *party;
149 return 0;
150}
151
152static int check_address(struct sccp_address *addr)
153{
154 /* ignore point_code_indicator... it should be zero... but */
155 if (addr->address.ssn_indicator != 1
156 || addr->address.global_title_indicator == 1
157 || addr->address.routing_indicator != 1) {
158 DEBUGP(DSCCP, "Invalid called address according to 08.06: 0x%x 0x%x\n",
159 *(u_int8_t *)&addr->address, addr->ssn);
160 return -1;
161 }
162
163 return 0;
164}
165
166static int _sccp_parse_optional_data(const int offset,
167 struct msgb *msgb, struct sccp_optional_data *data)
168{
169 u_int16_t room = msgb_l2len(msgb) - offset;
170 u_int16_t read = 0;
171
172 while (room > read) {
173 u_int8_t type = msgb->l2h[offset + read];
174 if (type == SCCP_PNC_END_OF_OPTIONAL)
175 return 0;
176
177 if (read + 1 >= room) {
178 DEBUGP(DSCCP, "no place for length\n");
179 return 0;
180 }
181
182 u_int8_t length = msgb->l2h[offset + read + 1];
183 read += 2 + length;
184
185
186 if (room <= read) {
187 DEBUGP(DSCCP, "no space for the data: type: %d read: %d room: %d l2: %d\n",
188 type, read, room, msgb_l2len(msgb));
189 return 0;
190 }
191
192 if (type == SCCP_PNC_DATA) {
193 data->data_len = length;
194 data->data_start = offset + read - length;
195 }
196
197 }
198
199 return -1;
200}
201
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100202int _sccp_parse_connection_request(struct msgb *msgb, struct sccp_parse_result *result)
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +0100203{
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100204 static const u_int32_t header_size =
205 sizeof(struct sccp_connection_request);
206 static const u_int32_t optional_offset =
207 offsetof(struct sccp_connection_request, optional_start);
208 static const u_int32_t called_offset =
209 offsetof(struct sccp_connection_request, variable_called);
210
211 struct sccp_connection_request *req = (struct sccp_connection_request *)msgb->data;
212 struct sccp_optional_data optional_data;
213
214 /* header check */
215 if (msgb_l2len(msgb) < header_size) {
216 DEBUGP(DSCCP, "msgb < header_size %u %u\n",
217 msgb_l2len(msgb), header_size);
218 return -1;
219 }
220
221 /* copy out the calling and called address. Add the offset */
222 if (copy_address(&result->called, called_offset + req->variable_called, msgb) != 0)
223 return -1;
224
225 if (check_address(&result->called) != 0) {
226 DEBUGP(DSCCP, "Invalid called address according to 08.06: 0x%x 0x%x\n",
227 *(u_int8_t *)&result->called.address, result->called.ssn);
228 return -1;
229 }
230
231 result->source_local_reference = &req->source_local_reference;
232
233 /*
234 * parse optional data.
235 */
236 memset(&optional_data, 0, sizeof(optional_data));
237 if (_sccp_parse_optional_data(optional_offset + req->optional_start, msgb, &optional_data) != 0) {
238 DEBUGP(DSCCP, "parsing of optional data failed.\n");
239 return -1;
240 }
241
242 if (optional_data.data_len != 0) {
243 msgb->l3h = &msgb->l2h[optional_data.data_start];
244 result->data_len = optional_data.data_len;
245 } else {
246 result->data_len = 0;
247 }
248
249 return 0;
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +0100250}
251
Holger Hans Peter Freyther88fe6ee2010-01-29 03:49:32 +0100252int _sccp_parse_connection_released(struct msgb *msgb, struct sccp_parse_result *result)
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +0100253{
Holger Hans Peter Freyther88fe6ee2010-01-29 03:49:32 +0100254 static int header_size = sizeof(struct sccp_connection_released);
255 static int optional_offset = offsetof(struct sccp_connection_released, optional_start);
256
257 struct sccp_optional_data optional_data;
258 struct sccp_connection_released *rls = (struct sccp_connection_released *) msgb->l2h;
259
260 /* we don't have enough size for the struct */
261 if (msgb_l2len(msgb) < header_size) {
262 DEBUGP(DSCCP, "msgb > header_size %u %u\n",
263 msgb_l2len(msgb), header_size);
264 return -1;
265 }
266
267 memset(&optional_data, 0, sizeof(optional_data));
268 if (_sccp_parse_optional_data(optional_offset + rls->optional_start, msgb, &optional_data) != 0) {
269 DEBUGP(DSCCP, "parsing of optional data failed.\n");
270 return -1;
271 }
272
273 result->source_local_reference = &rls->source_local_reference;
274 result->destination_local_reference = &rls->destination_local_reference;
275
276 if (optional_data.data_len != 0) {
277 msgb->l3h = &msgb->l2h[optional_data.data_start];
278 result->data_len = optional_data.data_len;
279 } else {
280 result->data_len = 0;
281 }
282
283 return 0;
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +0100284}
285
286int _sccp_parse_connection_refused(struct msgb *msg, struct sccp_parse_result *result)
287{
288 return -1;
289}
290
291int _sccp_parse_connection_confirm(struct msgb *msg, struct sccp_parse_result *result)
292{
293 return -1;
294}
295
296int _sccp_parse_connection_release_complete(struct msgb *msg, struct sccp_parse_result *result)
297{
298 return -1;
299}
300
301int _sccp_parse_connection_dt1(struct msgb *msg, struct sccp_parse_result *result)
302{
303 return -1;
304}
305
306int _sccp_parse_udt(struct msgb *msgb, struct sccp_parse_result *result)
307{
308 static const u_int32_t header_size = sizeof(struct sccp_data_unitdata);
309 static const u_int32_t called_offset = offsetof(struct sccp_data_unitdata, variable_called);
310 static const u_int32_t calling_offset = offsetof(struct sccp_data_unitdata, variable_calling);
311 static const u_int32_t data_offset = offsetof(struct sccp_data_unitdata, variable_data);
312
313 struct sccp_data_unitdata *udt = (struct sccp_data_unitdata *)msgb->l2h;
314
315 if (msgb_l2len(msgb) < header_size) {
316 DEBUGP(DSCCP, "msgb < header_size %u %u\n",
317 msgb_l2len(msgb), header_size);
318 return -1;
319 }
320
321 /* copy out the calling and called address. Add the off */
322 if (copy_address(&result->called, called_offset + udt->variable_called, msgb) != 0)
323 return -1;
324
325 if (check_address(&result->called) != 0) {
326 DEBUGP(DSCCP, "Invalid called address according to 08.06: 0x%x 0x%x\n",
327 *(u_int8_t *)&result->called.address, result->called.ssn);
328 return -1;
329 }
330
331 if (copy_address(&result->calling, calling_offset + udt->variable_calling, msgb) != 0)
332 return -1;
333
334 if (check_address(&result->calling) != 0) {
335 DEBUGP(DSCCP, "Invalid called address according to 08.06: 0x%x 0x%x\n",
336 *(u_int8_t *)&result->called.address, result->called.ssn);
337 }
338
339 /* we don't have enough size for the data */
340 if (msgb_l2len(msgb) < data_offset + udt->variable_data + 1) {
341 DEBUGP(DSCCP, "msgb < header + offset %u %u %u\n",
342 msgb_l2len(msgb), header_size, udt->variable_data);
343 return -1;
344 }
345
346
347 msgb->l3h = &udt->data[udt->variable_data];
348
349 if (msgb_l3len(msgb) != msgb->l3h[-1]) {
350 DEBUGP(DSCCP, "msgb is truncated %u %u\n",
351 msgb_l3len(msgb), msgb->l3h[-1]);
352 return -1;
353 }
354
355 return 0;
356}
357
358
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200359/*
360 * Send UDT. Currently we have a fixed address...
361 */
362static int _sccp_send_data(int class, const struct sockaddr_sccp *in,
363 const struct sockaddr_sccp *out, struct msgb *payload)
364{
365 struct sccp_data_unitdata *udt;
366 u_int8_t *data;
367 int ret;
368
369 if (msgb_l3len(payload) > 256) {
370 DEBUGP(DSCCP, "The payload is too big for one udt\n");
371 return -1;
372 }
373
374 struct msgb *msg = msgb_alloc_headroom(SCCP_MSG_SIZE,
375 SCCP_MSG_HEADROOM, "sccp: udt");
376 msg->l2h = &msg->data[0];
377 udt = (struct sccp_data_unitdata *)msgb_put(msg, sizeof(*udt));
378
379 udt->type = SCCP_MSG_TYPE_UDT;
380 udt->proto_class = class;
381 udt->variable_called = 3;
382 udt->variable_calling = 5;
383 udt->variable_data = 7;
384
385 /* for variable data we start with a size and the data */
386 data = msgb_put(msg, 1 + 2);
387 data[0] = 2;
388 data[1] = 0x42;
389 data[2] = out->sccp_ssn;
390
391 data = msgb_put(msg, 1 + 2);
392 data[0] = 2;
393 data[1] = 0x42;
394 data[2] = in->sccp_ssn;
395
396 /* copy the payload */
397 data = msgb_put(msg, 1 + msgb_l3len(payload));
398 data[0] = msgb_l3len(payload);
399 memcpy(&data[1], payload->l3h, msgb_l3len(payload));
400
401 ret = _send_msg(msg);
402 msgb_free(msg);
403
404 return ret;
405}
406
407static int _sccp_handle_read(struct msgb *msgb)
408{
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200409 struct sccp_data_callback *cb;
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +0100410 struct sccp_parse_result result;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200411
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +0100412 if (_sccp_parse_udt(msgb, &result) != 0)
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200413 return -1;
414
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +0100415 cb = _find_ssn(result.called.ssn);
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200416 if (!cb || !cb->read_cb) {
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +0100417 DEBUGP(DSCCP, "No routing for UDT for called SSN: %u\n", result.called.ssn);
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200418 return -1;
419 }
420
421 /* sanity check */
422 return cb->read_cb(msgb, msgb_l3len(msgb), cb->read_context);
423}
424
425/*
426 * handle connection orientated methods
427 */
428static int source_local_reference_is_free(struct sccp_source_reference *reference)
429{
430 struct sccp_connection *connection;
431
432 llist_for_each_entry(connection, &sccp_connections, list) {
433 if (memcmp(reference, &connection->source_local_reference, sizeof(*reference)) == 0)
434 return -1;
435 }
436
437 return 0;
438}
439
440static int destination_local_reference_is_free(struct sccp_source_reference *reference)
441{
442 struct sccp_connection *connection;
443
444 llist_for_each_entry(connection, &sccp_connections, list) {
445 if (memcmp(reference, &connection->destination_local_reference, sizeof(*reference)) == 0)
446 return -1;
447 }
448
449 return 0;
450}
451
452static int assign_source_local_reference(struct sccp_connection *connection)
453{
454 static u_int32_t last_ref = 0x30000;
455 int wrapped = 0;
456
457 do {
458 struct sccp_source_reference reference;
459 reference.octet1 = (last_ref >> 0) & 0xff;
460 reference.octet2 = (last_ref >> 8) & 0xff;
461 reference.octet3 = (last_ref >> 16) & 0xff;
462
463 ++last_ref;
464 /* do not use the reversed word and wrap around */
465 if ((last_ref & 0x00FFFFFF) == 0x00FFFFFF) {
466 DEBUGP(DSCCP, "Wrapped searching for a free code\n");
467 last_ref = 0;
468 ++wrapped;
469 }
470
471 if (source_local_reference_is_free(&reference) == 0) {
472 connection->source_local_reference = reference;
473 return 0;
474 }
475 } while (wrapped != 2);
476
477 DEBUGP(DSCCP, "Finding a free reference failed\n");
478 return -1;
479}
480
481static void _sccp_set_connection_state(struct sccp_connection *connection, int new_state)
482{
483 int old_state = connection->connection_state;
484
485 connection->connection_state = new_state;
486 if (connection->state_cb)
487 connection->state_cb(connection, old_state);
488}
489
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100490static int _sccp_send_refuse(struct sccp_source_reference *src_ref, int cause)
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200491{
492 struct msgb *msgb;
493 struct sccp_connection_refused *ref;
494 u_int8_t *data;
495 int ret;
496
497 msgb = msgb_alloc_headroom(SCCP_MSG_SIZE,
498 SCCP_MSG_HEADROOM, "sccp ref");
499 msgb->l2h = &msgb->data[0];
500
501 ref = (struct sccp_connection_refused *) msgb_put(msgb, sizeof(*ref));
502 ref->type = SCCP_MSG_TYPE_CREF;
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100503 memcpy(&ref->destination_local_reference, src_ref,
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200504 sizeof(struct sccp_source_reference));
505 ref->cause = cause;
506 ref->optional_start = 1;
507
508 data = msgb_put(msgb, 1);
509 data[0] = SCCP_PNC_END_OF_OPTIONAL;
510
511 ret = _send_msg(msgb);
512 msgb_free(msgb);
513 return ret;
514}
515
516static int _sccp_send_connection_confirm(struct sccp_connection *connection)
517{
518 struct msgb *response;
519 struct sccp_connection_confirm *confirm;
520 u_int8_t *optional_data;
521 int ret;
522
523 if (assign_source_local_reference(connection) != 0)
524 return -1;
525
526 response = msgb_alloc_headroom(SCCP_MSG_SIZE,
527 SCCP_MSG_HEADROOM, "sccp confirm");
528 response->l2h = &response->data[0];
529
530 confirm = (struct sccp_connection_confirm *) msgb_put(response, sizeof(*confirm));
531
532 confirm->type = SCCP_MSG_TYPE_CC;
533 memcpy(&confirm->destination_local_reference,
534 &connection->destination_local_reference,
535 sizeof(connection->destination_local_reference));
536 memcpy(&confirm->source_local_reference,
537 &connection->source_local_reference,
538 sizeof(connection->source_local_reference));
539 confirm->proto_class = 2;
540 confirm->optional_start = 1;
541
542 optional_data = (u_int8_t *) msgb_put(response, 1);
543 optional_data[0] = SCCP_PNC_END_OF_OPTIONAL;
544
545 ret = _send_msg(response);
546 msgb_free(response);
547
548 _sccp_set_connection_state(connection, SCCP_CONNECTION_STATE_ESTABLISHED);
549 return ret;
550}
551
552static int _sccp_send_connection_request(struct sccp_connection *connection,
553 const struct sockaddr_sccp *called, struct msgb *msg)
554{
555 struct msgb *request;
556 struct sccp_connection_request *req;
557 u_int8_t *data;
558 u_int8_t extra_size = 3 + 1;
559 int ret;
560
561
562 if (msg && (msgb_l3len(msg) < 3 || msgb_l3len(msg) > 130)) {
563 DEBUGP(DSCCP, "Invalid amount of data... %d\n", msgb_l3len(msg));
564 return -1;
565 }
566
567 /* try to find a id */
568 if (assign_source_local_reference(connection) != 0) {
569 DEBUGP(DSCCP, "Assigning a local reference failed.\n");
570 _sccp_set_connection_state(connection, SCCP_CONNECTION_STATE_SETUP_ERROR);
571 return -1;
572 }
573
574
575 if (msg)
576 extra_size += 2 + msgb_l3len(msg);
577 request = msgb_alloc_headroom(SCCP_MSG_SIZE,
578 SCCP_MSG_HEADROOM, "sccp connection request");
579 request->l2h = &request->data[0];
580 req = (struct sccp_connection_request *) msgb_put(request, sizeof(*req));
581
582 req->type = SCCP_MSG_TYPE_CR;
583 memcpy(&req->source_local_reference, &connection->source_local_reference,
584 sizeof(connection->source_local_reference));
585 req->proto_class = 2;
586 req->variable_called = 2;
587 req->optional_start = 4;
588
589 /* write the called party address */
590 data = msgb_put(request, 1 + 2);
591 data[0] = 2;
592 data[1] = 0x42;
593 data[2] = called->sccp_ssn;
594
595 /* write the payload */
596 if (msg) {
597 data = msgb_put(request, 2 + msgb_l3len(msg));
598 data[0] = SCCP_PNC_DATA;
599 data[1] = msgb_l3len(msg);
600 memcpy(&data[2], msg->l3h, msgb_l3len(msg));
601 }
602
603 data = msgb_put(request, 1);
604 data[0] = SCCP_PNC_END_OF_OPTIONAL;
605
606 llist_add_tail(&connection->list, &sccp_connections);
607 _sccp_set_connection_state(connection, SCCP_CONNECTION_STATE_REQUEST);
608
609 ret = _send_msg(request);
610 msgb_free(request);
611
612 return ret;
613}
614
615static int _sccp_send_connection_data(struct sccp_connection *conn, struct msgb *_data)
616{
617 struct msgb *msgb;
618 struct sccp_data_form1 *dt1;
619 u_int8_t *data;
620 int extra_size;
621 int ret;
622
623 if (msgb_l3len(_data) < 2 || msgb_l3len(_data) > 256) {
624 DEBUGP(DSCCP, "data size too big, segmenting unimplemented.\n");
625 return -1;
626 }
627
628 extra_size = 1 + msgb_l3len(_data);
629 msgb = msgb_alloc_headroom(SCCP_MSG_SIZE,
630 SCCP_MSG_HEADROOM, "sccp dt1");
631 msgb->l2h = &msgb->data[0];
632
633 dt1 = (struct sccp_data_form1 *) msgb_put(msgb, sizeof(*dt1));
634 dt1->type = SCCP_MSG_TYPE_DT1;
635 memcpy(&dt1->destination_local_reference, &conn->destination_local_reference,
636 sizeof(struct sccp_source_reference));
637 dt1->segmenting = 0;
638
639 /* copy the data */
640 dt1->variable_start = 1;
641 data = msgb_put(msgb, extra_size);
642 data[0] = extra_size - 1;
643 memcpy(&data[1], _data->l3h, extra_size - 1);
644
645 ret = _send_msg(msgb);
646 msgb_free(msgb);
647
648 return ret;
649}
650
Holger Hans Peter Freyther3b9516e2009-11-18 22:11:28 +0100651static int _sccp_send_connection_it(struct sccp_connection *conn)
652{
653 struct msgb *msgb;
654 struct sccp_data_it *it;
655 int ret;
656
657 msgb = msgb_alloc_headroom(SCCP_MSG_SIZE,
658 SCCP_MSG_HEADROOM, "sccp it");
659 msgb->l2h = &msgb->data[0];
660 it = (struct sccp_data_it *) msgb_put(msgb, sizeof(*it));
661 it->type = SCCP_MSG_TYPE_IT;
662 memcpy(&it->destination_local_reference, &conn->destination_local_reference,
663 sizeof(struct sccp_source_reference));
664 memcpy(&it->source_local_reference, &conn->source_local_reference,
665 sizeof(struct sccp_source_reference));
666
667 it->proto_class = 0x2;
668 it->sequencing[0] = it->sequencing[1] = 0;
669 it->credit = 0;
670
671 ret = _send_msg(msgb);
672 msgb_free(msgb);
673 return ret;
674}
675
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200676static int _sccp_send_connection_released(struct sccp_connection *conn, int cause)
677{
678 struct msgb *msg;
679 struct sccp_connection_released *rel;
680 u_int8_t *data;
681 int ret;
682
683 msg = msgb_alloc_headroom(SCCP_MSG_SIZE, SCCP_MSG_HEADROOM,
684 "sccp: connection released");
685 msg->l2h = &msg->data[0];
686 rel = (struct sccp_connection_released *) msgb_put(msg, sizeof(*rel));
687 rel->type = SCCP_MSG_TYPE_RLSD;
688 rel->release_cause = cause;
689
690 /* copy the source references */
691 memcpy(&rel->destination_local_reference, &conn->destination_local_reference,
692 sizeof(struct sccp_source_reference));
693 memcpy(&rel->source_local_reference, &conn->source_local_reference,
694 sizeof(struct sccp_source_reference));
695
696 data = msgb_put(msg, 1);
697 data[0] = SCCP_PNC_END_OF_OPTIONAL;
698
699 _sccp_set_connection_state(conn, SCCP_CONNECTION_STATE_RELEASE);
700 ret = _send_msg(msg);
701 msgb_free(msg);
702
703 return ret;
704}
705
706/*
707 * Open a connection. The following is going to happen:
708 *
709 * - Verify the packet, e.g. that we have no other connection
710 * that id.
711 * - Ask the user if he wants to accept the connection
712 * - Try to open the connection by assigning a source local reference
713 * and sending the packet
714 */
715static int _sccp_handle_connection_request(struct msgb *msgb)
716{
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100717 struct sccp_parse_result result;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200718
719 struct sccp_data_callback *cb;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200720 struct sccp_connection *connection;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200721
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100722 if (_sccp_parse_connection_request(msgb, &result) != 0)
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200723 return -1;
724
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100725 cb = _find_ssn(result.called.ssn);
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200726 if (!cb || !cb->accept_cb) {
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100727 DEBUGP(DSCCP, "No routing for CR for called SSN: %u\n", result.called.ssn);
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200728 return -1;
729 }
730
731 /* check if the system wants this connection */
732 connection = talloc_zero(tall_sccp_ctx, struct sccp_connection);
733 if (!connection) {
734 DEBUGP(DSCCP, "Allocation failed\n");
735 return -1;
736 }
737
738 /*
739 * sanity checks:
740 * - Is the source_local_reference in any other connection?
741 * then will call accept, assign a "destination" local reference
742 * and send a connection confirm, otherwise we will send a refuseed
743 * one....
744 */
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100745 if (destination_local_reference_is_free(result.source_local_reference) != 0) {
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200746 DEBUGP(DSCCP, "Need to reject connection with existing reference\n");
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100747 _sccp_send_refuse(result.source_local_reference, SCCP_REFUSAL_SCCP_FAILURE);
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200748 talloc_free(connection);
749 return -1;
750 }
751
752 connection->incoming = 1;
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100753 connection->destination_local_reference = *result.source_local_reference;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200754
755 if (cb->accept_cb(connection, cb->accept_context) != 0) {
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100756 _sccp_send_refuse(result.source_local_reference, SCCP_REFUSAL_END_USER_ORIGINATED);
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200757 _sccp_set_connection_state(connection, SCCP_CONNECTION_STATE_REFUSED);
758 talloc_free(connection);
759 return 0;
760 }
761
762
763 llist_add_tail(&connection->list, &sccp_connections);
764
765 if (_sccp_send_connection_confirm(connection) != 0) {
766 DEBUGP(DSCCP, "Sending confirm failed... no available source reference?\n");
767
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100768 _sccp_send_refuse(result.source_local_reference, SCCP_REFUSAL_SCCP_FAILURE);
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200769 _sccp_set_connection_state(connection, SCCP_CONNECTION_STATE_REFUSED);
770 llist_del(&connection->list);
771 talloc_free(connection);
772
773 return -1;
774 }
775
776 /*
777 * If we have data let us forward things.
778 */
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100779 if (result.data_len != 0 && connection->data_cb) {
780 connection->data_cb(connection, msgb, result.data_len);
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200781 }
782
783 return 0;
784}
785
786/* Handle the release confirmed */
787static int _sccp_handle_connection_release_complete(struct msgb *data)
788{
789 static int header_size = sizeof(struct sccp_connection_release_complete);
790
791 struct sccp_connection_release_complete *cmpl;
792 struct sccp_connection *conn;
793
794 /* header check */
795 if (msgb_l2len(data) < header_size) {
796 DEBUGP(DSCCP, "msgb < header_size %u %u\n",
797 msgb_l2len(data), header_size);
798 return -1;
799 }
800
801 cmpl = (struct sccp_connection_release_complete *) data->l2h;
802
803 /* find the connection */
804 llist_for_each_entry(conn, &sccp_connections, list) {
805 if (conn->data_cb
806 && memcmp(&conn->source_local_reference,
807 &cmpl->destination_local_reference,
808 sizeof(conn->source_local_reference)) == 0
809 && memcmp(&conn->destination_local_reference,
810 &cmpl->source_local_reference,
811 sizeof(conn->destination_local_reference)) == 0) {
812 goto found;
813 }
814 }
815
816
817 DEBUGP(DSCCP, "Release complete of unknown connection\n");
818 return -1;
819
820found:
821 llist_del(&conn->list);
822 _sccp_set_connection_state(conn, SCCP_CONNECTION_STATE_RELEASE_COMPLETE);
823 return 0;
824}
825
826/* Handle the Data Form 1 message */
827static int _sccp_handle_connection_dt1(struct msgb *data)
828{
829 static int variable_offset = offsetof(struct sccp_data_form1, variable_start);
830 static int header_size = sizeof(struct sccp_data_form1);
831
832 struct sccp_data_form1 *dt1 = (struct sccp_data_form1 *)data->l2h;
833 struct sccp_connection *conn;
834 int size;
835
836 /* we don't have enough size for the struct */
837 if (msgb_l2len(data) < header_size) {
838 DEBUGP(DSCCP, "msgb > header_size %u %u\n",
839 msgb_l2len(data), header_size);
840 return -1;
841 }
842
843 if (dt1->segmenting != 0) {
844 DEBUGP(DSCCP, "This packet has segmenting, not supported: %d\n", dt1->segmenting);
845 return -1;
846 }
847
848 /* lookup if we have a connection with the given reference */
849 llist_for_each_entry(conn, &sccp_connections, list) {
850 if (conn->data_cb
851 && memcmp(&conn->source_local_reference,
852 &dt1->destination_local_reference,
853 sizeof(conn->source_local_reference)) == 0) {
854
855 /* some more size checks in here */
856 if (msgb_l2len(data) < variable_offset + dt1->variable_start + 1) {
857 DEBUGP(DSCCP, "Not enough space for variable start: %u %u\n",
858 msgb_l2len(data), dt1->variable_start);
859 return -1;
860 }
861
862 size = data->l2h[variable_offset + dt1->variable_start];
863 data->l3h = &data->l2h[dt1->variable_start + variable_offset + 1];
864
865 if (msgb_l3len(data) < size) {
866 DEBUGP(DSCCP, "Not enough room for the payload: %u %u\n",
867 msgb_l3len(data), size);
868 return -1;
869 }
870
871 conn->data_cb(conn, data, size);
872 return 0;
873 }
874 }
875
876 DEBUGP(DSCCP, "No connection found for dt1 data\n");
877 return -1;
878}
879
880/* confirm a connection release */
881static int _sccp_send_connection_release_complete(struct sccp_connection *connection)
882{
883 struct msgb *msgb;
884 struct sccp_connection_release_complete *rlc;
885 int ret;
886
887 msgb = msgb_alloc_headroom(SCCP_MSG_SIZE,
888 SCCP_MSG_HEADROOM, "sccp rlc");
889 msgb->l2h = &msgb->data[0];
890
891 rlc = (struct sccp_connection_release_complete *) msgb_put(msgb, sizeof(*rlc));
892 rlc->type = SCCP_MSG_TYPE_RLC;
893 memcpy(&rlc->destination_local_reference,
894 &connection->destination_local_reference, sizeof(struct sccp_source_reference));
895 memcpy(&rlc->source_local_reference,
896 &connection->source_local_reference, sizeof(struct sccp_source_reference));
897
898 ret = _send_msg(msgb);
899 msgb_free(msgb);
900
901 /*
902 * Remove from the list of active connections and set the state. User code
903 * should now free the entry.
904 */
905 llist_del(&connection->list);
906 _sccp_set_connection_state(connection, SCCP_CONNECTION_STATE_RELEASE_COMPLETE);
907
908 return ret;
909}
910
911/* connection released, send a released confirm */
Holger Hans Peter Freyther88fe6ee2010-01-29 03:49:32 +0100912static int _sccp_handle_connection_released(struct msgb *msgb)
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200913{
Holger Hans Peter Freyther88fe6ee2010-01-29 03:49:32 +0100914 struct sccp_parse_result result;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200915 struct sccp_connection *conn;
916
Holger Hans Peter Freyther88fe6ee2010-01-29 03:49:32 +0100917 if (_sccp_parse_connection_released(msgb, &result) == -1)
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200918 return -1;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200919
920 /* lookup if we have a connection with the given reference */
921 llist_for_each_entry(conn, &sccp_connections, list) {
922 if (conn->data_cb
923 && memcmp(&conn->source_local_reference,
Holger Hans Peter Freyther88fe6ee2010-01-29 03:49:32 +0100924 result.destination_local_reference,
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200925 sizeof(conn->source_local_reference)) == 0
926 && memcmp(&conn->destination_local_reference,
Holger Hans Peter Freyther88fe6ee2010-01-29 03:49:32 +0100927 result.source_local_reference,
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200928 sizeof(conn->destination_local_reference)) == 0) {
929 goto found;
930 }
931 }
932
933
934 DEBUGP(DSCCP, "Unknown connection was released.\n");
935 return -1;
936
937 /* we have found a connection */
938found:
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200939 /* optional data */
Holger Hans Peter Freyther88fe6ee2010-01-29 03:49:32 +0100940 if (result.data_len != 0 && conn->data_cb) {
941 conn->data_cb(conn, msgb, result.data_len);
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200942 }
943
944 /* generate a response */
945 if (_sccp_send_connection_release_complete(conn) != 0) {
946 DEBUGP(DSCCP, "Sending release confirmed failed\n");
947 return -1;
948 }
949
950 return 0;
951}
952
953static int _sccp_handle_connection_refused(struct msgb *msgb)
954{
955 static const u_int32_t header_size =
956 sizeof(struct sccp_connection_refused);
957 static int optional_offset = offsetof(struct sccp_connection_refused, optional_start);
958
959 struct sccp_optional_data optional_data;
960 struct sccp_connection *conn;
961 struct sccp_connection_refused *ref;
962
963 /* header check */
964 if (msgb_l2len(msgb) < header_size) {
965 DEBUGP(DSCCP, "msgb < header_size %u %u\n",
966 msgb_l2len(msgb), header_size);
967 return -1;
968 }
969
970 ref = (struct sccp_connection_refused *) msgb->l2h;
971
972 /* lookup if we have a connection with the given reference */
973 llist_for_each_entry(conn, &sccp_connections, list) {
974 if (conn->incoming == 0 && conn->data_cb
975 && memcmp(&conn->source_local_reference,
976 &ref->destination_local_reference,
977 sizeof(conn->source_local_reference)) == 0) {
978 goto found;
979 }
980 }
981
982 DEBUGP(DSCCP, "Refused but no connection found\n");
983 return -1;
984
985found:
986 memset(&optional_data, 0, sizeof(optional_data));
987 if (_sccp_parse_optional_data(optional_offset + ref->optional_start, msgb, &optional_data) != 0) {
988 DEBUGP(DSCCP, "parsing of optional data failed.\n");
989 return -1;
990 }
991
992 /* optional data */
993 if (optional_data.data_len != 0 && conn->data_cb) {
994 msgb->l3h = &msgb->l2h[optional_data.data_start];
995 conn->data_cb(conn, msgb, optional_data.data_len);
996 }
997
998
999 llist_del(&conn->list);
1000 _sccp_set_connection_state(conn, SCCP_CONNECTION_STATE_REFUSED);
1001 return 0;
1002}
1003
1004static int _sccp_handle_connection_confirm(struct msgb *msgb)
1005{
1006 static u_int32_t header_size =
1007 sizeof(struct sccp_connection_confirm);
1008 static const u_int32_t optional_offset =
1009 offsetof(struct sccp_connection_confirm, optional_start);
1010
1011 struct sccp_optional_data optional_data;
1012 struct sccp_connection *conn;
1013 struct sccp_connection_confirm *con;
1014
1015 /* header check */
1016 if (msgb_l2len(msgb) < header_size) {
1017 DEBUGP(DSCCP, "msgb < header_size %u %u\n",
1018 msgb_l2len(msgb), header_size);
1019 return -1;
1020 }
1021
1022 con = (struct sccp_connection_confirm *) msgb->l2h;
1023
1024 /* lookup if we have a connection with the given reference */
1025 llist_for_each_entry(conn, &sccp_connections, list) {
1026 if (conn->incoming == 0 && conn->data_cb
1027 && memcmp(&conn->source_local_reference,
1028 &con->destination_local_reference,
1029 sizeof(conn->source_local_reference)) == 0) {
1030 goto found;
1031 }
1032 }
1033
1034 DEBUGP(DSCCP, "Confirmed but no connection found\n");
1035 return -1;
1036
1037found:
1038 /* copy the addresses of the connection */
1039 conn->destination_local_reference = con->source_local_reference;
1040 _sccp_set_connection_state(conn, SCCP_CONNECTION_STATE_ESTABLISHED);
1041
1042 memset(&optional_data, 0, sizeof(optional_data));
1043 if (_sccp_parse_optional_data(optional_offset + con->optional_start, msgb, &optional_data) != 0) {
1044 DEBUGP(DSCCP, "parsing of optional data failed.\n");
1045 return -1;
1046 }
1047
1048 /* optional data */
1049 if (optional_data.data_len != 0 && conn->data_cb) {
1050 msgb->l3h = &msgb->l2h[optional_data.data_start];
1051 conn->data_cb(conn, msgb, optional_data.data_len);
1052 }
1053
1054 return 0;
1055}
1056
1057
1058int sccp_system_init(int (*outgoing)(struct msgb *data, void *ctx), void *ctx)
1059{
1060 sccp_system.write_data = outgoing;
1061 sccp_system.write_context = ctx;
1062
1063 return 0;
1064}
1065
1066/* oh my god a real SCCP packet. need to dispatch it now */
1067int sccp_system_incoming(struct msgb *msgb)
1068{
1069 if (msgb_l2len(msgb) < 1 ) {
1070 DEBUGP(DSCCP, "Too short packet\n");
1071 return -1;
1072 }
1073
1074 int type = msgb->l2h[0];
1075
1076 switch(type) {
1077 case SCCP_MSG_TYPE_CR:
1078 return _sccp_handle_connection_request(msgb);
1079 break;
1080 case SCCP_MSG_TYPE_RLSD:
1081 return _sccp_handle_connection_released(msgb);
1082 break;
1083 case SCCP_MSG_TYPE_CREF:
1084 return _sccp_handle_connection_refused(msgb);
1085 break;
1086 case SCCP_MSG_TYPE_CC:
1087 return _sccp_handle_connection_confirm(msgb);
1088 break;
1089 case SCCP_MSG_TYPE_RLC:
1090 return _sccp_handle_connection_release_complete(msgb);
1091 break;
1092 case SCCP_MSG_TYPE_DT1:
1093 return _sccp_handle_connection_dt1(msgb);
1094 break;
1095 case SCCP_MSG_TYPE_UDT:
1096 return _sccp_handle_read(msgb);
1097 break;
1098 default:
1099 DEBUGP(DSCCP, "unimplemented msg type: %d\n", type);
1100 };
1101
1102 return -1;
1103}
1104
1105/* create a packet from the data */
1106int sccp_connection_write(struct sccp_connection *connection, struct msgb *data)
1107{
1108 if (connection->connection_state < SCCP_CONNECTION_STATE_CONFIRM
1109 || connection->connection_state > SCCP_CONNECTION_STATE_ESTABLISHED) {
1110 DEBUGP(DSCCP, "sccp_connection_write: Wrong connection state: %p %d\n",
1111 connection, connection->connection_state);
1112 return -1;
1113 }
1114
1115 return _sccp_send_connection_data(connection, data);
1116}
1117
Holger Hans Peter Freyther3b9516e2009-11-18 22:11:28 +01001118/*
1119 * Send a Inactivity Test message. The owner of the connection
1120 * should start a timer and call this method regularily. Calling
1121 * this every 60 seconds should be good enough.
1122 */
1123int sccp_connection_send_it(struct sccp_connection *connection)
1124{
1125 if (connection->connection_state < SCCP_CONNECTION_STATE_CONFIRM
1126 || connection->connection_state > SCCP_CONNECTION_STATE_ESTABLISHED) {
1127 DEBUGP(DSCCP, "sccp_connection_write: Wrong connection state: %p %d\n",
1128 connection, connection->connection_state);
1129 return -1;
1130 }
1131
1132 return _sccp_send_connection_it(connection);
1133}
1134
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001135/* send a connection release and wait for the connection released */
1136int sccp_connection_close(struct sccp_connection *connection, int cause)
1137{
1138 if (connection->connection_state < SCCP_CONNECTION_STATE_CONFIRM
1139 || connection->connection_state > SCCP_CONNECTION_STATE_ESTABLISHED) {
1140 DEBUGPC(DSCCP, "Can not close the connection. It was never opened: %p %d\n",
1141 connection, connection->connection_state);
1142 return -1;
1143 }
1144
1145 return _sccp_send_connection_released(connection, cause);
1146}
1147
1148int sccp_connection_free(struct sccp_connection *connection)
1149{
1150 if (connection->connection_state > SCCP_CONNECTION_STATE_NONE
1151 && connection->connection_state < SCCP_CONNECTION_STATE_RELEASE_COMPLETE) {
1152 DEBUGP(DSCCP, "The connection needs to be released before it is freed");
1153 return -1;
1154 }
1155
1156 talloc_free(connection);
1157 return 0;
1158}
1159
1160struct sccp_connection *sccp_connection_socket(void)
1161{
1162 return talloc_zero(tall_sccp_ctx, struct sccp_connection);
1163}
1164
1165int sccp_connection_connect(struct sccp_connection *conn,
1166 const struct sockaddr_sccp *local,
1167 struct msgb *data)
1168{
1169 return _sccp_send_connection_request(conn, local, data);
1170}
1171
1172int sccp_connection_set_incoming(const struct sockaddr_sccp *sock,
1173 int (*accept_cb)(struct sccp_connection *, void *), void *context)
1174{
1175 struct sccp_data_callback *cb;
1176
1177 if (!sock)
1178 return -2;
1179
1180 cb = _find_ssn(sock->sccp_ssn);
1181 if (!cb)
1182 return -1;
1183
1184 cb->accept_cb = accept_cb;
1185 cb->accept_context = context;
1186 return 0;
1187}
1188
1189int sccp_write(struct msgb *data, const struct sockaddr_sccp *in,
1190 const struct sockaddr_sccp *out, int class)
1191{
1192 return _sccp_send_data(class, in, out, data);
1193}
1194
1195int sccp_set_read(const struct sockaddr_sccp *sock,
1196 int (*read_cb)(struct msgb *, unsigned int, void *), void *context)
1197{
1198 struct sccp_data_callback *cb;
1199
1200 if (!sock)
1201 return -2;
1202
1203 cb = _find_ssn(sock->sccp_ssn);
1204 if (!cb)
1205 return -1;
1206
1207 cb->read_cb = read_cb;
1208 cb->read_context = context;
1209 return 0;
1210}
1211
1212static_assert(sizeof(struct sccp_source_reference) <= sizeof(u_int32_t), enough_space);
1213
1214u_int32_t sccp_src_ref_to_int(struct sccp_source_reference *ref)
1215{
1216 u_int32_t src_ref = 0;
1217 memcpy(&src_ref, ref, sizeof(*ref));
1218 return src_ref;
1219}
1220
1221struct sccp_source_reference sccp_src_ref_from_int(u_int32_t int_ref)
1222{
1223 struct sccp_source_reference ref;
1224 memcpy(&ref, &int_ref, sizeof(ref));
1225 return ref;
1226}
1227
Holger Hans Peter Freythera692fbc2010-01-13 09:55:43 +01001228int sccp_determine_msg_type(struct msgb *msg)
1229{
1230 if (msgb_l2len(msg) < 1)
1231 return -1;
1232
1233 return msg->l2h[0];
1234}
1235
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001236static __attribute__((constructor)) void on_dso_load(void)
1237{
1238 tall_sccp_ctx = talloc_named_const(NULL, 1, "sccp");
1239}
1240
1241static __attribute__((destructor)) void on_dso_unload(void)
1242{
1243 talloc_report_full(tall_sccp_ctx, stderr);
1244}