blob: f7c31e8a7ea2a751b5c74b3aefc2e7dc81c70914 [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) {
Holger Hans Peter Freyther277f0352010-04-21 21:08:58 +080084 LOGP(DSCCP, LOGL_ERROR, "Failed to allocate sccp callback.\n");
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +020085 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) {
Holger Hans Peter Freyther277f0352010-04-21 21:08:58 +0800111 LOGP(DSCCP, LOGL_ERROR, "Not enough room for an address: %u\n", room);
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200112 return -1;
113 }
114
115 length = msgb->l2h[offset];
116 if (room <= length) {
Holger Hans Peter Freyther277f0352010-04-21 21:08:58 +0800117 LOGP(DSCCP, LOGL_ERROR, "Not enough room for optional data %u %u\n", room, length);
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200118 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) {
Holger Hans Peter Freyther277f0352010-04-21 21:08:58 +0800125 LOGP(DSCCP, LOGL_ERROR, "POI does not fit %u\n", length);
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200126 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) {
Holger Hans Peter Freyther277f0352010-04-21 21:08:58 +0800136 LOGP(DSCCP, LOGL_ERROR, "SSN does not fit %u\n", length);
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200137 return -1;
138 }
139
140 addr->ssn = party->data[read];
141 read += 1;
142 }
143
144 if (party->global_title_indicator) {
Holger Hans Peter Freyther277f0352010-04-21 21:08:58 +0800145 LOGP(DSCCP, LOGL_ERROR, "GTI not supported %u\n", *(u_int8_t *)party);
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200146 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) {
Holger Hans Peter Freyther277f0352010-04-21 21:08:58 +0800159 LOGP(DSCCP, LOGL_ERROR,
160 "Invalid called address according to 08.06: 0x%x 0x%x\n",
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200161 *(u_int8_t *)&addr->address, addr->ssn);
162 return -1;
163 }
164
165 return 0;
166}
167
168static int _sccp_parse_optional_data(const int offset,
169 struct msgb *msgb, struct sccp_optional_data *data)
170{
171 u_int16_t room = msgb_l2len(msgb) - offset;
172 u_int16_t read = 0;
173
174 while (room > read) {
175 u_int8_t type = msgb->l2h[offset + read];
176 if (type == SCCP_PNC_END_OF_OPTIONAL)
177 return 0;
178
179 if (read + 1 >= room) {
Holger Hans Peter Freyther277f0352010-04-21 21:08:58 +0800180 LOGP(DSCCP, LOGL_ERROR, "no place for length\n");
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200181 return 0;
182 }
183
184 u_int8_t length = msgb->l2h[offset + read + 1];
185 read += 2 + length;
186
187
188 if (room <= read) {
Holger Hans Peter Freyther277f0352010-04-21 21:08:58 +0800189 LOGP(DSCCP, LOGL_ERROR,
190 "no space for the data: type: %d read: %d room: %d l2: %d\n",
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200191 type, read, room, msgb_l2len(msgb));
192 return 0;
193 }
194
195 if (type == SCCP_PNC_DATA) {
196 data->data_len = length;
197 data->data_start = offset + read - length;
198 }
199
200 }
201
202 return -1;
203}
204
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100205int _sccp_parse_connection_request(struct msgb *msgb, struct sccp_parse_result *result)
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +0100206{
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100207 static const u_int32_t header_size =
208 sizeof(struct sccp_connection_request);
209 static const u_int32_t optional_offset =
210 offsetof(struct sccp_connection_request, optional_start);
211 static const u_int32_t called_offset =
212 offsetof(struct sccp_connection_request, variable_called);
213
Holger Hans Peter Freythere2c50282010-02-20 00:36:03 +0100214 struct sccp_connection_request *req = (struct sccp_connection_request *)msgb->l2h;
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100215 struct sccp_optional_data optional_data;
216
217 /* header check */
218 if (msgb_l2len(msgb) < header_size) {
Holger Hans Peter Freyther277f0352010-04-21 21:08:58 +0800219 LOGP(DSCCP, LOGL_ERROR, "msgb < header_size %u %u\n",
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100220 msgb_l2len(msgb), header_size);
221 return -1;
222 }
223
224 /* copy out the calling and called address. Add the offset */
225 if (copy_address(&result->called, called_offset + req->variable_called, msgb) != 0)
226 return -1;
227
228 if (check_address(&result->called) != 0) {
Holger Hans Peter Freyther277f0352010-04-21 21:08:58 +0800229 LOGP(DSCCP, LOGL_ERROR, "Invalid called address according to 08.06: 0x%x 0x%x\n",
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100230 *(u_int8_t *)&result->called.address, result->called.ssn);
231 return -1;
232 }
233
234 result->source_local_reference = &req->source_local_reference;
235
236 /*
237 * parse optional data.
238 */
239 memset(&optional_data, 0, sizeof(optional_data));
240 if (_sccp_parse_optional_data(optional_offset + req->optional_start, msgb, &optional_data) != 0) {
Holger Hans Peter Freyther277f0352010-04-21 21:08:58 +0800241 LOGP(DSCCP, LOGL_ERROR, "parsing of optional data failed.\n");
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100242 return -1;
243 }
244
245 if (optional_data.data_len != 0) {
246 msgb->l3h = &msgb->l2h[optional_data.data_start];
247 result->data_len = optional_data.data_len;
248 } else {
249 result->data_len = 0;
250 }
251
252 return 0;
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +0100253}
254
Holger Hans Peter Freyther88fe6ee2010-01-29 03:49:32 +0100255int _sccp_parse_connection_released(struct msgb *msgb, struct sccp_parse_result *result)
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +0100256{
Holger Hans Peter Freyther88fe6ee2010-01-29 03:49:32 +0100257 static int header_size = sizeof(struct sccp_connection_released);
258 static int optional_offset = offsetof(struct sccp_connection_released, optional_start);
259
260 struct sccp_optional_data optional_data;
261 struct sccp_connection_released *rls = (struct sccp_connection_released *) msgb->l2h;
262
263 /* we don't have enough size for the struct */
264 if (msgb_l2len(msgb) < header_size) {
Holger Hans Peter Freyther277f0352010-04-21 21:08:58 +0800265 LOGP(DSCCP, LOGL_ERROR, "msgb > header_size %u %u\n",
Holger Hans Peter Freyther88fe6ee2010-01-29 03:49:32 +0100266 msgb_l2len(msgb), header_size);
267 return -1;
268 }
269
270 memset(&optional_data, 0, sizeof(optional_data));
271 if (_sccp_parse_optional_data(optional_offset + rls->optional_start, msgb, &optional_data) != 0) {
Holger Hans Peter Freyther277f0352010-04-21 21:08:58 +0800272 LOGP(DSCCP, LOGL_ERROR, "parsing of optional data failed.\n");
Holger Hans Peter Freyther88fe6ee2010-01-29 03:49:32 +0100273 return -1;
274 }
275
276 result->source_local_reference = &rls->source_local_reference;
277 result->destination_local_reference = &rls->destination_local_reference;
278
279 if (optional_data.data_len != 0) {
280 msgb->l3h = &msgb->l2h[optional_data.data_start];
281 result->data_len = optional_data.data_len;
282 } else {
283 result->data_len = 0;
284 }
285
286 return 0;
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +0100287}
288
Holger Hans Peter Freytherfe5de4e2010-01-29 03:58:12 +0100289int _sccp_parse_connection_refused(struct msgb *msgb, struct sccp_parse_result *result)
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +0100290{
Holger Hans Peter Freytherfe5de4e2010-01-29 03:58:12 +0100291 static const u_int32_t header_size =
292 sizeof(struct sccp_connection_refused);
293 static int optional_offset = offsetof(struct sccp_connection_refused, optional_start);
294
295 struct sccp_optional_data optional_data;
296 struct sccp_connection_refused *ref;
297
298 /* header check */
299 if (msgb_l2len(msgb) < header_size) {
Holger Hans Peter Freyther277f0352010-04-21 21:08:58 +0800300 LOGP(DSCCP, LOGL_ERROR, "msgb < header_size %u %u\n",
Holger Hans Peter Freytherfe5de4e2010-01-29 03:58:12 +0100301 msgb_l2len(msgb), header_size);
302 return -1;
303 }
304
305 ref = (struct sccp_connection_refused *) msgb->l2h;
306
307 result->destination_local_reference = &ref->destination_local_reference;
308
309 memset(&optional_data, 0, sizeof(optional_data));
310 if (_sccp_parse_optional_data(optional_offset + ref->optional_start, msgb, &optional_data) != 0) {
Holger Hans Peter Freyther277f0352010-04-21 21:08:58 +0800311 LOGP(DSCCP, LOGL_ERROR, "parsing of optional data failed.\n");
Holger Hans Peter Freytherfe5de4e2010-01-29 03:58:12 +0100312 return -1;
313 }
314
315 /* optional data */
316 if (optional_data.data_len != 0) {
317 msgb->l3h = &msgb->l2h[optional_data.data_start];
318 result->data_len = optional_data.data_len;
319 } else {
320 result->data_len = 0;
321 }
322
323 return 0;
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +0100324}
325
Holger Hans Peter Freytherca1d1d12010-01-29 04:03:00 +0100326int _sccp_parse_connection_confirm(struct msgb *msgb, struct sccp_parse_result *result)
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +0100327{
Holger Hans Peter Freytherca1d1d12010-01-29 04:03:00 +0100328 static u_int32_t header_size =
329 sizeof(struct sccp_connection_confirm);
330 static const u_int32_t optional_offset =
331 offsetof(struct sccp_connection_confirm, optional_start);
332
333 struct sccp_optional_data optional_data;
334 struct sccp_connection_confirm *con;
335
336 /* header check */
337 if (msgb_l2len(msgb) < header_size) {
Holger Hans Peter Freyther277f0352010-04-21 21:08:58 +0800338 LOGP(DSCCP, LOGL_ERROR, "msgb < header_size %u %u\n",
Holger Hans Peter Freytherca1d1d12010-01-29 04:03:00 +0100339 msgb_l2len(msgb), header_size);
340 return -1;
341 }
342
343 con = (struct sccp_connection_confirm *) msgb->l2h;
344 result->destination_local_reference = &con->destination_local_reference;
345 result->source_local_reference = &con->source_local_reference;
346
347 memset(&optional_data, 0, sizeof(optional_data));
348 if (_sccp_parse_optional_data(optional_offset + con->optional_start, msgb, &optional_data) != 0) {
Holger Hans Peter Freyther277f0352010-04-21 21:08:58 +0800349 LOGP(DSCCP, LOGL_ERROR, "parsing of optional data failed.\n");
Holger Hans Peter Freytherca1d1d12010-01-29 04:03:00 +0100350 return -1;
351 }
352
353 if (optional_data.data_len != 0) {
354 msgb->l3h = &msgb->l2h[optional_data.data_start];
355 result->data_len = optional_data.data_len;
356 } else {
357 result->data_len = 0;
358 }
359
360 return 0;
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +0100361}
362
Holger Hans Peter Freyther18c5cad2010-01-29 04:19:56 +0100363int _sccp_parse_connection_release_complete(struct msgb *msgb, struct sccp_parse_result *result)
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +0100364{
Holger Hans Peter Freyther18c5cad2010-01-29 04:19:56 +0100365 static int header_size = sizeof(struct sccp_connection_release_complete);
366
367 struct sccp_connection_release_complete *cmpl;
368
369 /* header check */
370 if (msgb_l2len(msgb) < header_size) {
Holger Hans Peter Freyther277f0352010-04-21 21:08:58 +0800371 LOGP(DSCCP, LOGL_ERROR, "msgb < header_size %u %u\n",
Holger Hans Peter Freyther18c5cad2010-01-29 04:19:56 +0100372 msgb_l2len(msgb), header_size);
373 return -1;
374 }
375
376 cmpl = (struct sccp_connection_release_complete *) msgb->l2h;
377 result->source_local_reference = &cmpl->source_local_reference;
378 result->destination_local_reference = &cmpl->destination_local_reference;
379
380 return 0;
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +0100381}
382
Holger Hans Peter Freytheref845392010-01-29 04:31:00 +0100383int _sccp_parse_connection_dt1(struct msgb *msgb, struct sccp_parse_result *result)
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +0100384{
Holger Hans Peter Freytheref845392010-01-29 04:31:00 +0100385 static int header_size = sizeof(struct sccp_data_form1);
386 static int variable_offset = offsetof(struct sccp_data_form1, variable_start);
387
388 struct sccp_data_form1 *dt1 = (struct sccp_data_form1 *)msgb->l2h;
389
390 /* we don't have enough size for the struct */
391 if (msgb_l2len(msgb) < header_size) {
Holger Hans Peter Freyther277f0352010-04-21 21:08:58 +0800392 LOGP(DSCCP, LOGL_ERROR, "msgb > header_size %u %u\n",
Holger Hans Peter Freytheref845392010-01-29 04:31:00 +0100393 msgb_l2len(msgb), header_size);
394 return -1;
395 }
396
397 if (dt1->segmenting != 0) {
Holger Hans Peter Freyther277f0352010-04-21 21:08:58 +0800398 LOGP(DSCCP, LOGL_ERROR, "This packet has segmenting, not supported: %d\n", dt1->segmenting);
Holger Hans Peter Freytheref845392010-01-29 04:31:00 +0100399 return -1;
400 }
401
402 result->destination_local_reference = &dt1->destination_local_reference;
403
404 /* some more size checks in here */
405 if (msgb_l2len(msgb) < variable_offset + dt1->variable_start + 1) {
Holger Hans Peter Freyther277f0352010-04-21 21:08:58 +0800406 LOGP(DSCCP, LOGL_ERROR, "Not enough space for variable start: %u %u\n",
Holger Hans Peter Freytheref845392010-01-29 04:31:00 +0100407 msgb_l2len(msgb), dt1->variable_start);
408 return -1;
409 }
410
411 result->data_len = msgb->l2h[variable_offset + dt1->variable_start];
412 msgb->l3h = &msgb->l2h[dt1->variable_start + variable_offset + 1];
413
414 if (msgb_l3len(msgb) < result->data_len) {
Holger Hans Peter Freyther277f0352010-04-21 21:08:58 +0800415 LOGP(DSCCP, LOGL_ERROR, "Not enough room for the payload: %u %u\n",
Holger Hans Peter Freytheref845392010-01-29 04:31:00 +0100416 msgb_l3len(msgb), result->data_len);
417 return -1;
418 }
419
420 return 0;
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +0100421}
422
423int _sccp_parse_udt(struct msgb *msgb, struct sccp_parse_result *result)
424{
425 static const u_int32_t header_size = sizeof(struct sccp_data_unitdata);
426 static const u_int32_t called_offset = offsetof(struct sccp_data_unitdata, variable_called);
427 static const u_int32_t calling_offset = offsetof(struct sccp_data_unitdata, variable_calling);
428 static const u_int32_t data_offset = offsetof(struct sccp_data_unitdata, variable_data);
429
430 struct sccp_data_unitdata *udt = (struct sccp_data_unitdata *)msgb->l2h;
431
432 if (msgb_l2len(msgb) < header_size) {
Holger Hans Peter Freyther277f0352010-04-21 21:08:58 +0800433 LOGP(DSCCP, LOGL_ERROR, "msgb < header_size %u %u\n",
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +0100434 msgb_l2len(msgb), header_size);
435 return -1;
436 }
437
438 /* copy out the calling and called address. Add the off */
439 if (copy_address(&result->called, called_offset + udt->variable_called, msgb) != 0)
440 return -1;
441
442 if (check_address(&result->called) != 0) {
Holger Hans Peter Freyther277f0352010-04-21 21:08:58 +0800443 LOGP(DSCCP, LOGL_ERROR, "Invalid called address according to 08.06: 0x%x 0x%x\n",
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +0100444 *(u_int8_t *)&result->called.address, result->called.ssn);
445 return -1;
446 }
447
448 if (copy_address(&result->calling, calling_offset + udt->variable_calling, msgb) != 0)
449 return -1;
450
451 if (check_address(&result->calling) != 0) {
Holger Hans Peter Freyther277f0352010-04-21 21:08:58 +0800452 LOGP(DSCCP, LOGL_ERROR, "Invalid called address according to 08.06: 0x%x 0x%x\n",
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +0100453 *(u_int8_t *)&result->called.address, result->called.ssn);
454 }
455
456 /* we don't have enough size for the data */
457 if (msgb_l2len(msgb) < data_offset + udt->variable_data + 1) {
Holger Hans Peter Freyther277f0352010-04-21 21:08:58 +0800458 LOGP(DSCCP, LOGL_ERROR, "msgb < header + offset %u %u %u\n",
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +0100459 msgb_l2len(msgb), header_size, udt->variable_data);
460 return -1;
461 }
462
463
464 msgb->l3h = &udt->data[udt->variable_data];
Holger Hans Peter Freyther8fabf512010-02-12 23:08:21 +0100465 result->data_len = msgb_l3len(msgb);
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +0100466
467 if (msgb_l3len(msgb) != msgb->l3h[-1]) {
Holger Hans Peter Freyther277f0352010-04-21 21:08:58 +0800468 LOGP(DSCCP, LOGL_ERROR, "msgb is truncated is: %u should: %u\n",
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +0100469 msgb_l3len(msgb), msgb->l3h[-1]);
470 return -1;
471 }
472
473 return 0;
474}
475
Holger Hans Peter Freythere1d50672010-02-26 19:26:35 +0100476static int _sccp_parse_it(struct msgb *msgb, struct sccp_parse_result *result)
477{
478 static const u_int32_t header_size = sizeof(struct sccp_data_it);
479
480 struct sccp_data_it *it;
481
482 if (msgb_l2len(msgb) < header_size) {
Holger Hans Peter Freyther277f0352010-04-21 21:08:58 +0800483 LOGP(DSCCP, LOGL_ERROR, "msgb < header_size %u %u\n",
Holger Hans Peter Freythere1d50672010-02-26 19:26:35 +0100484 msgb_l2len(msgb), header_size);
485 return -1;
486 }
487
488 it = (struct sccp_data_it *) msgb->l2h;
489 result->data_len = 0;
490 result->source_local_reference = &it->source_local_reference;
491 result->destination_local_reference = &it->destination_local_reference;
492 return 0;
493}
494
Holger Hans Peter Freyther2cdda722010-04-21 15:38:16 +0800495static int _sccp_parse_err(struct msgb *msgb, struct sccp_parse_result *result)
496{
497 static const u_int32_t header_size = sizeof(struct sccp_proto_err);
498
499 struct sccp_proto_err *err;
500
501 if (msgb_l2len(msgb) < header_size) {
502 LOGP(DSCCP, LOGL_ERROR, "msgb < header_size %u %u\n",
503 msgb_l2len(msgb), header_size);
504 return -1;
505 }
506
507 err = (struct sccp_proto_err *) msgb->l2h;
508 result->data_len = 0;
509 result->destination_local_reference = &err->destination_local_reference;
510 return 0;
511}
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +0100512
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200513/*
514 * Send UDT. Currently we have a fixed address...
515 */
516static int _sccp_send_data(int class, const struct sockaddr_sccp *in,
517 const struct sockaddr_sccp *out, struct msgb *payload)
518{
519 struct sccp_data_unitdata *udt;
520 u_int8_t *data;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200521
522 if (msgb_l3len(payload) > 256) {
Holger Hans Peter Freyther277f0352010-04-21 21:08:58 +0800523 LOGP(DSCCP, LOGL_ERROR, "The payload is too big for one udt\n");
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200524 return -1;
525 }
526
527 struct msgb *msg = msgb_alloc_headroom(SCCP_MSG_SIZE,
528 SCCP_MSG_HEADROOM, "sccp: udt");
529 msg->l2h = &msg->data[0];
530 udt = (struct sccp_data_unitdata *)msgb_put(msg, sizeof(*udt));
531
532 udt->type = SCCP_MSG_TYPE_UDT;
533 udt->proto_class = class;
534 udt->variable_called = 3;
535 udt->variable_calling = 5;
536 udt->variable_data = 7;
537
538 /* for variable data we start with a size and the data */
539 data = msgb_put(msg, 1 + 2);
540 data[0] = 2;
541 data[1] = 0x42;
542 data[2] = out->sccp_ssn;
543
544 data = msgb_put(msg, 1 + 2);
545 data[0] = 2;
546 data[1] = 0x42;
547 data[2] = in->sccp_ssn;
548
549 /* copy the payload */
550 data = msgb_put(msg, 1 + msgb_l3len(payload));
551 data[0] = msgb_l3len(payload);
552 memcpy(&data[1], payload->l3h, msgb_l3len(payload));
553
Holger Hans Peter Freyther3c1221e2010-03-26 05:44:21 +0100554 _send_msg(msg);
555 return 0;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200556}
557
558static int _sccp_handle_read(struct msgb *msgb)
559{
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200560 struct sccp_data_callback *cb;
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +0100561 struct sccp_parse_result result;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200562
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +0100563 if (_sccp_parse_udt(msgb, &result) != 0)
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200564 return -1;
565
Holger Hans Peter Freytherefca5412010-01-27 12:12:46 +0100566 cb = _find_ssn(result.called.ssn);
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200567 if (!cb || !cb->read_cb) {
Holger Hans Peter Freyther277f0352010-04-21 21:08:58 +0800568 LOGP(DSCCP, LOGL_ERROR, "No routing for UDT for called SSN: %u\n", result.called.ssn);
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200569 return -1;
570 }
571
572 /* sanity check */
573 return cb->read_cb(msgb, msgb_l3len(msgb), cb->read_context);
574}
575
576/*
577 * handle connection orientated methods
578 */
579static int source_local_reference_is_free(struct sccp_source_reference *reference)
580{
581 struct sccp_connection *connection;
582
583 llist_for_each_entry(connection, &sccp_connections, list) {
584 if (memcmp(reference, &connection->source_local_reference, sizeof(*reference)) == 0)
585 return -1;
586 }
587
588 return 0;
589}
590
591static int destination_local_reference_is_free(struct sccp_source_reference *reference)
592{
593 struct sccp_connection *connection;
594
595 llist_for_each_entry(connection, &sccp_connections, list) {
596 if (memcmp(reference, &connection->destination_local_reference, sizeof(*reference)) == 0)
597 return -1;
598 }
599
600 return 0;
601}
602
603static int assign_source_local_reference(struct sccp_connection *connection)
604{
605 static u_int32_t last_ref = 0x30000;
606 int wrapped = 0;
607
608 do {
609 struct sccp_source_reference reference;
610 reference.octet1 = (last_ref >> 0) & 0xff;
611 reference.octet2 = (last_ref >> 8) & 0xff;
612 reference.octet3 = (last_ref >> 16) & 0xff;
613
614 ++last_ref;
615 /* do not use the reversed word and wrap around */
616 if ((last_ref & 0x00FFFFFF) == 0x00FFFFFF) {
Holger Hans Peter Freyther277f0352010-04-21 21:08:58 +0800617 LOGP(DSCCP, LOGL_DEBUG, "Wrapped searching for a free code\n");
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200618 last_ref = 0;
619 ++wrapped;
620 }
621
622 if (source_local_reference_is_free(&reference) == 0) {
623 connection->source_local_reference = reference;
624 return 0;
625 }
626 } while (wrapped != 2);
627
Holger Hans Peter Freyther277f0352010-04-21 21:08:58 +0800628 LOGP(DSCCP, LOGL_ERROR, "Finding a free reference failed\n");
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200629 return -1;
630}
631
632static void _sccp_set_connection_state(struct sccp_connection *connection, int new_state)
633{
634 int old_state = connection->connection_state;
635
636 connection->connection_state = new_state;
637 if (connection->state_cb)
638 connection->state_cb(connection, old_state);
639}
640
Holger Hans Peter Freytherea504e72010-05-15 05:35:48 +0800641struct msgb *sccp_create_refuse(struct sccp_source_reference *src_ref, int cause, uint8_t *inp, int length)
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200642{
643 struct msgb *msgb;
644 struct sccp_connection_refused *ref;
645 u_int8_t *data;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200646
647 msgb = msgb_alloc_headroom(SCCP_MSG_SIZE,
648 SCCP_MSG_HEADROOM, "sccp ref");
Holger Hans Peter Freyther7bf332a2010-05-14 03:34:35 +0800649 if (!msgb) {
650 LOGP(DSCCP, LOGL_ERROR, "Failed to allocate refusal msg.\n");
651 return NULL;
652 }
653
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200654 msgb->l2h = &msgb->data[0];
655
656 ref = (struct sccp_connection_refused *) msgb_put(msgb, sizeof(*ref));
657 ref->type = SCCP_MSG_TYPE_CREF;
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100658 memcpy(&ref->destination_local_reference, src_ref,
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200659 sizeof(struct sccp_source_reference));
660 ref->cause = cause;
661 ref->optional_start = 1;
662
Holger Hans Peter Freytherea504e72010-05-15 05:35:48 +0800663 if (inp) {
664 data = msgb_put(msgb, 1 + 1 + length);
665 data[0] = SCCP_PNC_DATA;
666 data[1] = length;
667 memcpy(&data[2], inp, length);
668 }
669
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200670 data = msgb_put(msgb, 1);
671 data[0] = SCCP_PNC_END_OF_OPTIONAL;
Holger Hans Peter Freyther7bf332a2010-05-14 03:34:35 +0800672 return msgb;
673}
674
675static int _sccp_send_refuse(struct sccp_source_reference *src_ref, int cause)
676{
Holger Hans Peter Freytherea504e72010-05-15 05:35:48 +0800677 struct msgb *msgb = sccp_create_refuse(src_ref, cause, NULL, 0);
Holger Hans Peter Freyther7bf332a2010-05-14 03:34:35 +0800678 if (!msgb)
679 return -1;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200680
Holger Hans Peter Freyther3c1221e2010-03-26 05:44:21 +0100681 _send_msg(msgb);
682 return 0;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200683}
684
685static int _sccp_send_connection_confirm(struct sccp_connection *connection)
686{
687 struct msgb *response;
688 struct sccp_connection_confirm *confirm;
689 u_int8_t *optional_data;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200690
691 if (assign_source_local_reference(connection) != 0)
692 return -1;
693
694 response = msgb_alloc_headroom(SCCP_MSG_SIZE,
695 SCCP_MSG_HEADROOM, "sccp confirm");
696 response->l2h = &response->data[0];
697
698 confirm = (struct sccp_connection_confirm *) msgb_put(response, sizeof(*confirm));
699
700 confirm->type = SCCP_MSG_TYPE_CC;
701 memcpy(&confirm->destination_local_reference,
702 &connection->destination_local_reference,
703 sizeof(connection->destination_local_reference));
704 memcpy(&confirm->source_local_reference,
705 &connection->source_local_reference,
706 sizeof(connection->source_local_reference));
707 confirm->proto_class = 2;
708 confirm->optional_start = 1;
709
710 optional_data = (u_int8_t *) msgb_put(response, 1);
711 optional_data[0] = SCCP_PNC_END_OF_OPTIONAL;
712
Holger Hans Peter Freyther3c1221e2010-03-26 05:44:21 +0100713 _send_msg(response);
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200714 _sccp_set_connection_state(connection, SCCP_CONNECTION_STATE_ESTABLISHED);
Holger Hans Peter Freyther3c1221e2010-03-26 05:44:21 +0100715 return 0;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200716}
717
718static int _sccp_send_connection_request(struct sccp_connection *connection,
719 const struct sockaddr_sccp *called, struct msgb *msg)
720{
721 struct msgb *request;
722 struct sccp_connection_request *req;
723 u_int8_t *data;
724 u_int8_t extra_size = 3 + 1;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200725
726
727 if (msg && (msgb_l3len(msg) < 3 || msgb_l3len(msg) > 130)) {
Holger Hans Peter Freyther277f0352010-04-21 21:08:58 +0800728 LOGP(DSCCP, LOGL_ERROR, "Invalid amount of data... %d\n", msgb_l3len(msg));
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200729 return -1;
730 }
731
732 /* try to find a id */
733 if (assign_source_local_reference(connection) != 0) {
Holger Hans Peter Freyther277f0352010-04-21 21:08:58 +0800734 LOGP(DSCCP, LOGL_ERROR, "Assigning a local reference failed.\n");
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200735 _sccp_set_connection_state(connection, SCCP_CONNECTION_STATE_SETUP_ERROR);
736 return -1;
737 }
738
739
740 if (msg)
741 extra_size += 2 + msgb_l3len(msg);
742 request = msgb_alloc_headroom(SCCP_MSG_SIZE,
743 SCCP_MSG_HEADROOM, "sccp connection request");
744 request->l2h = &request->data[0];
745 req = (struct sccp_connection_request *) msgb_put(request, sizeof(*req));
746
747 req->type = SCCP_MSG_TYPE_CR;
748 memcpy(&req->source_local_reference, &connection->source_local_reference,
749 sizeof(connection->source_local_reference));
750 req->proto_class = 2;
751 req->variable_called = 2;
752 req->optional_start = 4;
753
754 /* write the called party address */
755 data = msgb_put(request, 1 + 2);
756 data[0] = 2;
757 data[1] = 0x42;
758 data[2] = called->sccp_ssn;
759
760 /* write the payload */
761 if (msg) {
762 data = msgb_put(request, 2 + msgb_l3len(msg));
763 data[0] = SCCP_PNC_DATA;
764 data[1] = msgb_l3len(msg);
765 memcpy(&data[2], msg->l3h, msgb_l3len(msg));
766 }
767
768 data = msgb_put(request, 1);
769 data[0] = SCCP_PNC_END_OF_OPTIONAL;
770
771 llist_add_tail(&connection->list, &sccp_connections);
772 _sccp_set_connection_state(connection, SCCP_CONNECTION_STATE_REQUEST);
773
Holger Hans Peter Freyther3c1221e2010-03-26 05:44:21 +0100774 _send_msg(request);
775 return 0;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200776}
777
778static int _sccp_send_connection_data(struct sccp_connection *conn, struct msgb *_data)
779{
780 struct msgb *msgb;
781 struct sccp_data_form1 *dt1;
782 u_int8_t *data;
783 int extra_size;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200784
785 if (msgb_l3len(_data) < 2 || msgb_l3len(_data) > 256) {
Holger Hans Peter Freyther277f0352010-04-21 21:08:58 +0800786 LOGP(DSCCP, LOGL_ERROR, "data size too big, segmenting unimplemented.\n");
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200787 return -1;
788 }
789
790 extra_size = 1 + msgb_l3len(_data);
791 msgb = msgb_alloc_headroom(SCCP_MSG_SIZE,
792 SCCP_MSG_HEADROOM, "sccp dt1");
793 msgb->l2h = &msgb->data[0];
794
795 dt1 = (struct sccp_data_form1 *) msgb_put(msgb, sizeof(*dt1));
796 dt1->type = SCCP_MSG_TYPE_DT1;
797 memcpy(&dt1->destination_local_reference, &conn->destination_local_reference,
798 sizeof(struct sccp_source_reference));
799 dt1->segmenting = 0;
800
801 /* copy the data */
802 dt1->variable_start = 1;
803 data = msgb_put(msgb, extra_size);
804 data[0] = extra_size - 1;
805 memcpy(&data[1], _data->l3h, extra_size - 1);
806
Holger Hans Peter Freyther3c1221e2010-03-26 05:44:21 +0100807 _send_msg(msgb);
808 return 0;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200809}
810
Holger Hans Peter Freyther3b9516e2009-11-18 22:11:28 +0100811static int _sccp_send_connection_it(struct sccp_connection *conn)
812{
813 struct msgb *msgb;
814 struct sccp_data_it *it;
Holger Hans Peter Freyther3b9516e2009-11-18 22:11:28 +0100815
816 msgb = msgb_alloc_headroom(SCCP_MSG_SIZE,
817 SCCP_MSG_HEADROOM, "sccp it");
818 msgb->l2h = &msgb->data[0];
819 it = (struct sccp_data_it *) msgb_put(msgb, sizeof(*it));
820 it->type = SCCP_MSG_TYPE_IT;
821 memcpy(&it->destination_local_reference, &conn->destination_local_reference,
822 sizeof(struct sccp_source_reference));
823 memcpy(&it->source_local_reference, &conn->source_local_reference,
824 sizeof(struct sccp_source_reference));
825
826 it->proto_class = 0x2;
827 it->sequencing[0] = it->sequencing[1] = 0;
828 it->credit = 0;
829
Holger Hans Peter Freyther3c1221e2010-03-26 05:44:21 +0100830 _send_msg(msgb);
831 return 0;
Holger Hans Peter Freyther3b9516e2009-11-18 22:11:28 +0100832}
833
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200834static int _sccp_send_connection_released(struct sccp_connection *conn, int cause)
835{
836 struct msgb *msg;
837 struct sccp_connection_released *rel;
838 u_int8_t *data;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200839
840 msg = msgb_alloc_headroom(SCCP_MSG_SIZE, SCCP_MSG_HEADROOM,
841 "sccp: connection released");
842 msg->l2h = &msg->data[0];
843 rel = (struct sccp_connection_released *) msgb_put(msg, sizeof(*rel));
844 rel->type = SCCP_MSG_TYPE_RLSD;
845 rel->release_cause = cause;
846
847 /* copy the source references */
848 memcpy(&rel->destination_local_reference, &conn->destination_local_reference,
849 sizeof(struct sccp_source_reference));
850 memcpy(&rel->source_local_reference, &conn->source_local_reference,
851 sizeof(struct sccp_source_reference));
852
853 data = msgb_put(msg, 1);
854 data[0] = SCCP_PNC_END_OF_OPTIONAL;
855
856 _sccp_set_connection_state(conn, SCCP_CONNECTION_STATE_RELEASE);
Holger Hans Peter Freyther3c1221e2010-03-26 05:44:21 +0100857 _send_msg(msg);
858 return 0;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200859}
860
861/*
862 * Open a connection. The following is going to happen:
863 *
864 * - Verify the packet, e.g. that we have no other connection
865 * that id.
866 * - Ask the user if he wants to accept the connection
867 * - Try to open the connection by assigning a source local reference
868 * and sending the packet
869 */
870static int _sccp_handle_connection_request(struct msgb *msgb)
871{
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100872 struct sccp_parse_result result;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200873
874 struct sccp_data_callback *cb;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200875 struct sccp_connection *connection;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200876
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100877 if (_sccp_parse_connection_request(msgb, &result) != 0)
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200878 return -1;
879
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100880 cb = _find_ssn(result.called.ssn);
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200881 if (!cb || !cb->accept_cb) {
Holger Hans Peter Freyther277f0352010-04-21 21:08:58 +0800882 LOGP(DSCCP, LOGL_ERROR, "No routing for CR for called SSN: %u\n", result.called.ssn);
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200883 return -1;
884 }
885
886 /* check if the system wants this connection */
887 connection = talloc_zero(tall_sccp_ctx, struct sccp_connection);
888 if (!connection) {
Holger Hans Peter Freyther277f0352010-04-21 21:08:58 +0800889 LOGP(DSCCP, LOGL_ERROR, "Allocation failed\n");
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200890 return -1;
891 }
892
893 /*
894 * sanity checks:
895 * - Is the source_local_reference in any other connection?
896 * then will call accept, assign a "destination" local reference
897 * and send a connection confirm, otherwise we will send a refuseed
898 * one....
899 */
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100900 if (destination_local_reference_is_free(result.source_local_reference) != 0) {
Holger Hans Peter Freyther277f0352010-04-21 21:08:58 +0800901 LOGP(DSCCP, LOGL_ERROR, "Need to reject connection with existing reference\n");
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100902 _sccp_send_refuse(result.source_local_reference, SCCP_REFUSAL_SCCP_FAILURE);
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200903 talloc_free(connection);
904 return -1;
905 }
906
907 connection->incoming = 1;
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100908 connection->destination_local_reference = *result.source_local_reference;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200909
910 if (cb->accept_cb(connection, cb->accept_context) != 0) {
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100911 _sccp_send_refuse(result.source_local_reference, SCCP_REFUSAL_END_USER_ORIGINATED);
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200912 _sccp_set_connection_state(connection, SCCP_CONNECTION_STATE_REFUSED);
913 talloc_free(connection);
914 return 0;
915 }
916
917
918 llist_add_tail(&connection->list, &sccp_connections);
919
920 if (_sccp_send_connection_confirm(connection) != 0) {
Holger Hans Peter Freyther277f0352010-04-21 21:08:58 +0800921 LOGP(DSCCP, LOGL_ERROR, "Sending confirm failed... no available source reference?\n");
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200922
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100923 _sccp_send_refuse(result.source_local_reference, SCCP_REFUSAL_SCCP_FAILURE);
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200924 _sccp_set_connection_state(connection, SCCP_CONNECTION_STATE_REFUSED);
925 llist_del(&connection->list);
926 talloc_free(connection);
927
928 return -1;
929 }
930
931 /*
932 * If we have data let us forward things.
933 */
Holger Hans Peter Freythera8cd2e62010-01-27 12:25:13 +0100934 if (result.data_len != 0 && connection->data_cb) {
935 connection->data_cb(connection, msgb, result.data_len);
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200936 }
937
938 return 0;
939}
940
941/* Handle the release confirmed */
Holger Hans Peter Freyther18c5cad2010-01-29 04:19:56 +0100942static int _sccp_handle_connection_release_complete(struct msgb *msgb)
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200943{
Holger Hans Peter Freyther18c5cad2010-01-29 04:19:56 +0100944 struct sccp_parse_result result;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200945 struct sccp_connection *conn;
946
Holger Hans Peter Freyther18c5cad2010-01-29 04:19:56 +0100947 if (_sccp_parse_connection_release_complete(msgb, &result) != 0)
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200948 return -1;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200949
950 /* find the connection */
951 llist_for_each_entry(conn, &sccp_connections, list) {
952 if (conn->data_cb
953 && memcmp(&conn->source_local_reference,
Holger Hans Peter Freyther18c5cad2010-01-29 04:19:56 +0100954 result.destination_local_reference,
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200955 sizeof(conn->source_local_reference)) == 0
956 && memcmp(&conn->destination_local_reference,
Holger Hans Peter Freyther18c5cad2010-01-29 04:19:56 +0100957 result.source_local_reference,
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200958 sizeof(conn->destination_local_reference)) == 0) {
959 goto found;
960 }
961 }
962
963
Holger Hans Peter Freyther277f0352010-04-21 21:08:58 +0800964 LOGP(DSCCP, LOGL_ERROR, "Release complete of unknown connection\n");
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200965 return -1;
966
967found:
968 llist_del(&conn->list);
969 _sccp_set_connection_state(conn, SCCP_CONNECTION_STATE_RELEASE_COMPLETE);
970 return 0;
971}
972
973/* Handle the Data Form 1 message */
Holger Hans Peter Freytheref845392010-01-29 04:31:00 +0100974static int _sccp_handle_connection_dt1(struct msgb *msgb)
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200975{
Holger Hans Peter Freytheref845392010-01-29 04:31:00 +0100976 struct sccp_parse_result result;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200977 struct sccp_connection *conn;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200978
Holger Hans Peter Freytheref845392010-01-29 04:31:00 +0100979 if (_sccp_parse_connection_dt1(msgb, &result) != 0)
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200980 return -1;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200981
982 /* lookup if we have a connection with the given reference */
983 llist_for_each_entry(conn, &sccp_connections, list) {
984 if (conn->data_cb
985 && memcmp(&conn->source_local_reference,
Holger Hans Peter Freytheref845392010-01-29 04:31:00 +0100986 result.destination_local_reference,
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200987 sizeof(conn->source_local_reference)) == 0) {
Holger Hans Peter Freytheref845392010-01-29 04:31:00 +0100988 goto found;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200989 }
990 }
991
Holger Hans Peter Freyther277f0352010-04-21 21:08:58 +0800992 LOGP(DSCCP, LOGL_ERROR, "No connection found for dt1 data\n");
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200993 return -1;
Holger Hans Peter Freytheref845392010-01-29 04:31:00 +0100994
995found:
996 conn->data_cb(conn, msgb, result.data_len);
997 return 0;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +0200998}
999
1000/* confirm a connection release */
1001static int _sccp_send_connection_release_complete(struct sccp_connection *connection)
1002{
1003 struct msgb *msgb;
1004 struct sccp_connection_release_complete *rlc;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001005
1006 msgb = msgb_alloc_headroom(SCCP_MSG_SIZE,
1007 SCCP_MSG_HEADROOM, "sccp rlc");
1008 msgb->l2h = &msgb->data[0];
1009
1010 rlc = (struct sccp_connection_release_complete *) msgb_put(msgb, sizeof(*rlc));
1011 rlc->type = SCCP_MSG_TYPE_RLC;
1012 memcpy(&rlc->destination_local_reference,
1013 &connection->destination_local_reference, sizeof(struct sccp_source_reference));
1014 memcpy(&rlc->source_local_reference,
1015 &connection->source_local_reference, sizeof(struct sccp_source_reference));
1016
Holger Hans Peter Freyther3c1221e2010-03-26 05:44:21 +01001017 _send_msg(msgb);
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001018
1019 /*
1020 * Remove from the list of active connections and set the state. User code
1021 * should now free the entry.
1022 */
1023 llist_del(&connection->list);
1024 _sccp_set_connection_state(connection, SCCP_CONNECTION_STATE_RELEASE_COMPLETE);
Holger Hans Peter Freyther3c1221e2010-03-26 05:44:21 +01001025 return 0;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001026}
1027
1028/* connection released, send a released confirm */
Holger Hans Peter Freyther88fe6ee2010-01-29 03:49:32 +01001029static int _sccp_handle_connection_released(struct msgb *msgb)
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001030{
Holger Hans Peter Freyther88fe6ee2010-01-29 03:49:32 +01001031 struct sccp_parse_result result;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001032 struct sccp_connection *conn;
1033
Holger Hans Peter Freyther88fe6ee2010-01-29 03:49:32 +01001034 if (_sccp_parse_connection_released(msgb, &result) == -1)
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001035 return -1;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001036
1037 /* lookup if we have a connection with the given reference */
1038 llist_for_each_entry(conn, &sccp_connections, list) {
1039 if (conn->data_cb
1040 && memcmp(&conn->source_local_reference,
Holger Hans Peter Freyther88fe6ee2010-01-29 03:49:32 +01001041 result.destination_local_reference,
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001042 sizeof(conn->source_local_reference)) == 0
1043 && memcmp(&conn->destination_local_reference,
Holger Hans Peter Freyther88fe6ee2010-01-29 03:49:32 +01001044 result.source_local_reference,
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001045 sizeof(conn->destination_local_reference)) == 0) {
1046 goto found;
1047 }
1048 }
1049
1050
Holger Hans Peter Freyther277f0352010-04-21 21:08:58 +08001051 LOGP(DSCCP, LOGL_ERROR, "Unknown connection was released.\n");
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001052 return -1;
1053
1054 /* we have found a connection */
1055found:
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001056 /* optional data */
Holger Hans Peter Freyther88fe6ee2010-01-29 03:49:32 +01001057 if (result.data_len != 0 && conn->data_cb) {
1058 conn->data_cb(conn, msgb, result.data_len);
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001059 }
1060
1061 /* generate a response */
1062 if (_sccp_send_connection_release_complete(conn) != 0) {
Holger Hans Peter Freyther277f0352010-04-21 21:08:58 +08001063 LOGP(DSCCP, LOGL_ERROR, "Sending release confirmed failed\n");
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001064 return -1;
1065 }
1066
1067 return 0;
1068}
1069
1070static int _sccp_handle_connection_refused(struct msgb *msgb)
1071{
Holger Hans Peter Freytherfe5de4e2010-01-29 03:58:12 +01001072 struct sccp_parse_result result;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001073 struct sccp_connection *conn;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001074
Holger Hans Peter Freytherfe5de4e2010-01-29 03:58:12 +01001075 if (_sccp_parse_connection_refused(msgb, &result) != 0)
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001076 return -1;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001077
1078 /* lookup if we have a connection with the given reference */
1079 llist_for_each_entry(conn, &sccp_connections, list) {
1080 if (conn->incoming == 0 && conn->data_cb
1081 && memcmp(&conn->source_local_reference,
Holger Hans Peter Freytherfe5de4e2010-01-29 03:58:12 +01001082 result.destination_local_reference,
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001083 sizeof(conn->source_local_reference)) == 0) {
1084 goto found;
1085 }
1086 }
1087
Holger Hans Peter Freyther277f0352010-04-21 21:08:58 +08001088 LOGP(DSCCP, LOGL_ERROR, "Refused but no connection found\n");
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001089 return -1;
1090
1091found:
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001092 /* optional data */
Holger Hans Peter Freytherfe5de4e2010-01-29 03:58:12 +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
1098 llist_del(&conn->list);
1099 _sccp_set_connection_state(conn, SCCP_CONNECTION_STATE_REFUSED);
1100 return 0;
1101}
1102
1103static int _sccp_handle_connection_confirm(struct msgb *msgb)
1104{
Holger Hans Peter Freytherca1d1d12010-01-29 04:03:00 +01001105 struct sccp_parse_result result;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001106 struct sccp_connection *conn;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001107
Holger Hans Peter Freytherca1d1d12010-01-29 04:03:00 +01001108 if (_sccp_parse_connection_confirm(msgb, &result) != 0)
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001109 return -1;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001110
1111 /* lookup if we have a connection with the given reference */
1112 llist_for_each_entry(conn, &sccp_connections, list) {
1113 if (conn->incoming == 0 && conn->data_cb
1114 && memcmp(&conn->source_local_reference,
Holger Hans Peter Freytherca1d1d12010-01-29 04:03:00 +01001115 result.destination_local_reference,
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001116 sizeof(conn->source_local_reference)) == 0) {
1117 goto found;
1118 }
1119 }
1120
Holger Hans Peter Freyther277f0352010-04-21 21:08:58 +08001121 LOGP(DSCCP, LOGL_ERROR, "Confirmed but no connection found\n");
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001122 return -1;
1123
1124found:
1125 /* copy the addresses of the connection */
Holger Hans Peter Freytherca1d1d12010-01-29 04:03:00 +01001126 conn->destination_local_reference = *result.source_local_reference;
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001127 _sccp_set_connection_state(conn, SCCP_CONNECTION_STATE_ESTABLISHED);
1128
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001129 /* optional data */
Holger Hans Peter Freytherca1d1d12010-01-29 04:03:00 +01001130 if (result.data_len != 0 && conn->data_cb) {
1131 conn->data_cb(conn, msgb, result.data_len);
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001132 }
1133
1134 return 0;
1135}
1136
1137
Holger Hans Peter Freyther3c1221e2010-03-26 05:44:21 +01001138int sccp_system_init(void (*outgoing)(struct msgb *data, void *ctx), void *ctx)
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001139{
1140 sccp_system.write_data = outgoing;
1141 sccp_system.write_context = ctx;
1142
1143 return 0;
1144}
1145
1146/* oh my god a real SCCP packet. need to dispatch it now */
1147int sccp_system_incoming(struct msgb *msgb)
1148{
1149 if (msgb_l2len(msgb) < 1 ) {
Holger Hans Peter Freyther277f0352010-04-21 21:08:58 +08001150 LOGP(DSCCP, LOGL_ERROR, "Too short packet\n");
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001151 return -1;
1152 }
1153
1154 int type = msgb->l2h[0];
1155
1156 switch(type) {
1157 case SCCP_MSG_TYPE_CR:
1158 return _sccp_handle_connection_request(msgb);
1159 break;
1160 case SCCP_MSG_TYPE_RLSD:
1161 return _sccp_handle_connection_released(msgb);
1162 break;
1163 case SCCP_MSG_TYPE_CREF:
1164 return _sccp_handle_connection_refused(msgb);
1165 break;
1166 case SCCP_MSG_TYPE_CC:
1167 return _sccp_handle_connection_confirm(msgb);
1168 break;
1169 case SCCP_MSG_TYPE_RLC:
1170 return _sccp_handle_connection_release_complete(msgb);
1171 break;
1172 case SCCP_MSG_TYPE_DT1:
1173 return _sccp_handle_connection_dt1(msgb);
1174 break;
1175 case SCCP_MSG_TYPE_UDT:
1176 return _sccp_handle_read(msgb);
1177 break;
1178 default:
Holger Hans Peter Freyther277f0352010-04-21 21:08:58 +08001179 LOGP(DSCCP, LOGL_ERROR, "unimplemented msg type: %d\n", type);
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001180 };
1181
1182 return -1;
1183}
1184
1185/* create a packet from the data */
1186int sccp_connection_write(struct sccp_connection *connection, struct msgb *data)
1187{
1188 if (connection->connection_state < SCCP_CONNECTION_STATE_CONFIRM
1189 || connection->connection_state > SCCP_CONNECTION_STATE_ESTABLISHED) {
Holger Hans Peter Freyther277f0352010-04-21 21:08:58 +08001190 LOGP(DSCCP, LOGL_ERROR, "sccp_connection_write: Wrong connection state: %p %d\n",
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001191 connection, connection->connection_state);
1192 return -1;
1193 }
1194
1195 return _sccp_send_connection_data(connection, data);
1196}
1197
Holger Hans Peter Freyther3b9516e2009-11-18 22:11:28 +01001198/*
1199 * Send a Inactivity Test message. The owner of the connection
1200 * should start a timer and call this method regularily. Calling
1201 * this every 60 seconds should be good enough.
1202 */
1203int sccp_connection_send_it(struct sccp_connection *connection)
1204{
1205 if (connection->connection_state < SCCP_CONNECTION_STATE_CONFIRM
1206 || connection->connection_state > SCCP_CONNECTION_STATE_ESTABLISHED) {
Holger Hans Peter Freyther277f0352010-04-21 21:08:58 +08001207 LOGP(DSCCP, LOGL_ERROR, "sccp_connection_write: Wrong connection state: %p %d\n",
Holger Hans Peter Freyther3b9516e2009-11-18 22:11:28 +01001208 connection, connection->connection_state);
1209 return -1;
1210 }
1211
1212 return _sccp_send_connection_it(connection);
1213}
1214
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001215/* send a connection release and wait for the connection released */
1216int sccp_connection_close(struct sccp_connection *connection, int cause)
1217{
1218 if (connection->connection_state < SCCP_CONNECTION_STATE_CONFIRM
1219 || connection->connection_state > SCCP_CONNECTION_STATE_ESTABLISHED) {
Holger Hans Peter Freyther277f0352010-04-21 21:08:58 +08001220 LOGP(DSCCP, LOGL_ERROR, "Can not close the connection. It was never opened: %p %d\n",
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001221 connection, connection->connection_state);
1222 return -1;
1223 }
1224
1225 return _sccp_send_connection_released(connection, cause);
1226}
1227
1228int sccp_connection_free(struct sccp_connection *connection)
1229{
1230 if (connection->connection_state > SCCP_CONNECTION_STATE_NONE
1231 && connection->connection_state < SCCP_CONNECTION_STATE_RELEASE_COMPLETE) {
Holger Hans Peter Freyther277f0352010-04-21 21:08:58 +08001232 LOGP(DSCCP, LOGL_ERROR, "The connection needs to be released before it is freed");
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001233 return -1;
1234 }
1235
1236 talloc_free(connection);
1237 return 0;
1238}
1239
Holger Hans Peter Freytherb71517f2010-04-05 18:13:40 +02001240int sccp_connection_force_free(struct sccp_connection *con)
1241{
1242 if (con->connection_state > SCCP_CONNECTION_STATE_NONE &&
1243 con->connection_state < SCCP_CONNECTION_STATE_RELEASE_COMPLETE)
1244 llist_del(&con->list);
1245
1246 con->connection_state = SCCP_CONNECTION_STATE_REFUSED;
1247 sccp_connection_free(con);
1248 return 0;
1249}
1250
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001251struct sccp_connection *sccp_connection_socket(void)
1252{
1253 return talloc_zero(tall_sccp_ctx, struct sccp_connection);
1254}
1255
1256int sccp_connection_connect(struct sccp_connection *conn,
1257 const struct sockaddr_sccp *local,
1258 struct msgb *data)
1259{
1260 return _sccp_send_connection_request(conn, local, data);
1261}
1262
1263int sccp_connection_set_incoming(const struct sockaddr_sccp *sock,
1264 int (*accept_cb)(struct sccp_connection *, void *), void *context)
1265{
1266 struct sccp_data_callback *cb;
1267
1268 if (!sock)
1269 return -2;
1270
1271 cb = _find_ssn(sock->sccp_ssn);
1272 if (!cb)
1273 return -1;
1274
1275 cb->accept_cb = accept_cb;
1276 cb->accept_context = context;
1277 return 0;
1278}
1279
1280int sccp_write(struct msgb *data, const struct sockaddr_sccp *in,
1281 const struct sockaddr_sccp *out, int class)
1282{
1283 return _sccp_send_data(class, in, out, data);
1284}
1285
1286int sccp_set_read(const struct sockaddr_sccp *sock,
1287 int (*read_cb)(struct msgb *, unsigned int, void *), void *context)
1288{
1289 struct sccp_data_callback *cb;
1290
1291 if (!sock)
1292 return -2;
1293
1294 cb = _find_ssn(sock->sccp_ssn);
1295 if (!cb)
1296 return -1;
1297
1298 cb->read_cb = read_cb;
1299 cb->read_context = context;
1300 return 0;
1301}
1302
1303static_assert(sizeof(struct sccp_source_reference) <= sizeof(u_int32_t), enough_space);
1304
1305u_int32_t sccp_src_ref_to_int(struct sccp_source_reference *ref)
1306{
1307 u_int32_t src_ref = 0;
1308 memcpy(&src_ref, ref, sizeof(*ref));
1309 return src_ref;
1310}
1311
1312struct sccp_source_reference sccp_src_ref_from_int(u_int32_t int_ref)
1313{
1314 struct sccp_source_reference ref;
1315 memcpy(&ref, &int_ref, sizeof(ref));
1316 return ref;
1317}
1318
Holger Hans Peter Freythera692fbc2010-01-13 09:55:43 +01001319int sccp_determine_msg_type(struct msgb *msg)
1320{
1321 if (msgb_l2len(msg) < 1)
1322 return -1;
1323
1324 return msg->l2h[0];
1325}
1326
Holger Hans Peter Freythercaf49b42010-01-29 04:31:51 +01001327int sccp_parse_header(struct msgb *msg, struct sccp_parse_result *result)
1328{
1329 int type;
1330
1331 if (msgb_l2len(msg) < 1)
1332 return -1;
1333
1334 type = msg->l2h[0];
1335 switch(type) {
1336 case SCCP_MSG_TYPE_CR:
1337 return _sccp_parse_connection_request(msg, result);
1338 break;
1339 case SCCP_MSG_TYPE_RLSD:
1340 return _sccp_parse_connection_released(msg, result);
1341 break;
1342 case SCCP_MSG_TYPE_CREF:
1343 return _sccp_parse_connection_refused(msg, result);
1344 break;
1345 case SCCP_MSG_TYPE_CC:
1346 return _sccp_parse_connection_confirm(msg, result);
1347 break;
1348 case SCCP_MSG_TYPE_RLC:
1349 return _sccp_parse_connection_release_complete(msg, result);
1350 break;
1351 case SCCP_MSG_TYPE_DT1:
1352 return _sccp_parse_connection_dt1(msg, result);
1353 break;
1354 case SCCP_MSG_TYPE_UDT:
1355 return _sccp_parse_udt(msg, result);
1356 break;
Holger Hans Peter Freythere1d50672010-02-26 19:26:35 +01001357 case SCCP_MSG_TYPE_IT:
1358 return _sccp_parse_it(msg, result);
1359 break;
Holger Hans Peter Freyther2cdda722010-04-21 15:38:16 +08001360 case SCCP_MSG_TYPE_ERR:
1361 return _sccp_parse_err(msg, result);
1362 break;
Holger Hans Peter Freythercaf49b42010-01-29 04:31:51 +01001363 };
1364
Holger Hans Peter Freythere1d50672010-02-26 19:26:35 +01001365 LOGP(DSCCP, LOGL_ERROR, "Unimplemented MSG Type: 0x%x\n", type);
Holger Hans Peter Freythercaf49b42010-01-29 04:31:51 +01001366 return -1;
1367}
1368
Holger Hans Peter Freytherac967702009-07-29 07:37:48 +02001369static __attribute__((constructor)) void on_dso_load(void)
1370{
1371 tall_sccp_ctx = talloc_named_const(NULL, 1, "sccp");
1372}
1373
1374static __attribute__((destructor)) void on_dso_unload(void)
1375{
1376 talloc_report_full(tall_sccp_ctx, stderr);
1377}