Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 1 | /* |
| 2 | * SCCP management code |
| 3 | * |
Holger Hans Peter Freyther | a1b5980 | 2010-01-27 12:12:46 +0100 | [diff] [blame] | 4 | * (C) 2009, 2010 by Holger Hans Peter Freyther <zecke@selfish.org> |
Holger Hans Peter Freyther | 31c5368 | 2010-02-03 18:10:07 +0100 | [diff] [blame] | 5 | * (C) 2009, 2010 by On-Waves |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 6 | * |
| 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 Welte | f4625b1 | 2010-02-20 16:24:02 +0100 | [diff] [blame] | 27 | #include <osmocore/msgb.h> |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 28 | #include <openbsc/debug.h> |
Harald Welte | f4625b1 | 2010-02-20 16:24:02 +0100 | [diff] [blame] | 29 | #include <osmocore/talloc.h> |
Holger Hans Peter Freyther | 7f301de | 2010-02-12 22:44:50 +0100 | [diff] [blame] | 30 | |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 31 | #include <sccp/sccp.h> |
| 32 | |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 33 | |
| 34 | static void *tall_sccp_ctx; |
| 35 | static LLIST_HEAD(sccp_connections); |
| 36 | |
| 37 | #define SCCP_MSG_SIZE 4096 |
| 38 | #define SCCP_MSG_HEADROOM 128 |
| 39 | |
| 40 | /* global data */ |
| 41 | const struct sockaddr_sccp sccp_ssn_bssap = { |
| 42 | .sccp_family = 0, |
| 43 | .sccp_ssn = SCCP_SSN_BSSAP, |
| 44 | }; |
| 45 | |
| 46 | struct sccp_system { |
| 47 | /* layer3 -> layer2 */ |
Holger Hans Peter Freyther | 9f2f471 | 2010-03-26 05:44:21 +0100 | [diff] [blame] | 48 | void (*write_data)(struct msgb *data, void *context); |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 49 | void *write_context; |
| 50 | }; |
| 51 | |
| 52 | |
| 53 | static struct sccp_system sccp_system = { |
| 54 | .write_data = NULL, |
| 55 | }; |
| 56 | |
| 57 | struct 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 | |
| 70 | static LLIST_HEAD(sccp_callbacks); |
| 71 | |
| 72 | static 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 Freyther | c749e8b | 2010-04-21 21:08:58 +0800 | [diff] [blame] | 84 | LOGP(DSCCP, LOGL_ERROR, "Failed to allocate sccp callback.\n"); |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 85 | return NULL; |
| 86 | } |
| 87 | |
| 88 | cb->ssn = ssn; |
| 89 | llist_add_tail(&cb->callback, &sccp_callbacks); |
| 90 | return cb; |
| 91 | } |
| 92 | |
| 93 | |
Holger Hans Peter Freyther | 9f2f471 | 2010-03-26 05:44:21 +0100 | [diff] [blame] | 94 | static void _send_msg(struct msgb *msg) |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 95 | { |
Holger Hans Peter Freyther | 9f2f471 | 2010-03-26 05:44:21 +0100 | [diff] [blame] | 96 | sccp_system.write_data(msg, sccp_system.write_context); |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | /* |
| 100 | * parsing routines |
| 101 | */ |
| 102 | static 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 Freyther | c749e8b | 2010-04-21 21:08:58 +0800 | [diff] [blame] | 111 | LOGP(DSCCP, LOGL_ERROR, "Not enough room for an address: %u\n", room); |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 112 | return -1; |
| 113 | } |
| 114 | |
| 115 | length = msgb->l2h[offset]; |
| 116 | if (room <= length) { |
Holger Hans Peter Freyther | c749e8b | 2010-04-21 21:08:58 +0800 | [diff] [blame] | 117 | LOGP(DSCCP, LOGL_ERROR, "Not enough room for optional data %u %u\n", room, length); |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 118 | return -1; |
| 119 | } |
| 120 | |
| 121 | |
| 122 | party = (struct sccp_called_party_address *)(msgb->l2h + offset + 1); |
| 123 | if (party->point_code_indicator) { |
| 124 | if (length <= read + 2) { |
Holger Hans Peter Freyther | c749e8b | 2010-04-21 21:08:58 +0800 | [diff] [blame] | 125 | LOGP(DSCCP, LOGL_ERROR, "POI does not fit %u\n", length); |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 126 | return -1; |
| 127 | } |
| 128 | |
| 129 | |
| 130 | memcpy(&addr->poi, &party->data[read], 2); |
| 131 | read += 2; |
| 132 | } |
| 133 | |
| 134 | if (party->ssn_indicator) { |
| 135 | if (length <= read + 1) { |
Holger Hans Peter Freyther | c749e8b | 2010-04-21 21:08:58 +0800 | [diff] [blame] | 136 | LOGP(DSCCP, LOGL_ERROR, "SSN does not fit %u\n", length); |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 137 | return -1; |
| 138 | } |
| 139 | |
| 140 | addr->ssn = party->data[read]; |
| 141 | read += 1; |
| 142 | } |
| 143 | |
| 144 | if (party->global_title_indicator) { |
Holger Hans Peter Freyther | c749e8b | 2010-04-21 21:08:58 +0800 | [diff] [blame] | 145 | LOGP(DSCCP, LOGL_ERROR, "GTI not supported %u\n", *(u_int8_t *)party); |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 146 | return -1; |
| 147 | } |
| 148 | |
| 149 | addr->address = *party; |
| 150 | return 0; |
| 151 | } |
| 152 | |
| 153 | static 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 Freyther | c749e8b | 2010-04-21 21:08:58 +0800 | [diff] [blame] | 159 | LOGP(DSCCP, LOGL_ERROR, |
| 160 | "Invalid called address according to 08.06: 0x%x 0x%x\n", |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 161 | *(u_int8_t *)&addr->address, addr->ssn); |
| 162 | return -1; |
| 163 | } |
| 164 | |
| 165 | return 0; |
| 166 | } |
| 167 | |
| 168 | static 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 Freyther | c749e8b | 2010-04-21 21:08:58 +0800 | [diff] [blame] | 180 | LOGP(DSCCP, LOGL_ERROR, "no place for length\n"); |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 181 | 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 Freyther | c749e8b | 2010-04-21 21:08:58 +0800 | [diff] [blame] | 189 | LOGP(DSCCP, LOGL_ERROR, |
| 190 | "no space for the data: type: %d read: %d room: %d l2: %d\n", |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 191 | 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 Freyther | 867d836 | 2010-01-27 12:25:13 +0100 | [diff] [blame] | 205 | int _sccp_parse_connection_request(struct msgb *msgb, struct sccp_parse_result *result) |
Holger Hans Peter Freyther | a1b5980 | 2010-01-27 12:12:46 +0100 | [diff] [blame] | 206 | { |
Holger Hans Peter Freyther | 867d836 | 2010-01-27 12:25:13 +0100 | [diff] [blame] | 207 | 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 Freyther | 3f61d6e | 2010-02-20 00:36:03 +0100 | [diff] [blame] | 214 | struct sccp_connection_request *req = (struct sccp_connection_request *)msgb->l2h; |
Holger Hans Peter Freyther | 867d836 | 2010-01-27 12:25:13 +0100 | [diff] [blame] | 215 | struct sccp_optional_data optional_data; |
| 216 | |
| 217 | /* header check */ |
| 218 | if (msgb_l2len(msgb) < header_size) { |
Holger Hans Peter Freyther | c749e8b | 2010-04-21 21:08:58 +0800 | [diff] [blame] | 219 | LOGP(DSCCP, LOGL_ERROR, "msgb < header_size %u %u\n", |
Holger Hans Peter Freyther | 867d836 | 2010-01-27 12:25:13 +0100 | [diff] [blame] | 220 | 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 Freyther | c749e8b | 2010-04-21 21:08:58 +0800 | [diff] [blame] | 229 | LOGP(DSCCP, LOGL_ERROR, "Invalid called address according to 08.06: 0x%x 0x%x\n", |
Holger Hans Peter Freyther | 867d836 | 2010-01-27 12:25:13 +0100 | [diff] [blame] | 230 | *(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 Freyther | c749e8b | 2010-04-21 21:08:58 +0800 | [diff] [blame] | 241 | LOGP(DSCCP, LOGL_ERROR, "parsing of optional data failed.\n"); |
Holger Hans Peter Freyther | 867d836 | 2010-01-27 12:25:13 +0100 | [diff] [blame] | 242 | 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 Freyther | a1b5980 | 2010-01-27 12:12:46 +0100 | [diff] [blame] | 253 | } |
| 254 | |
Holger Hans Peter Freyther | 5defc56 | 2010-01-29 03:49:32 +0100 | [diff] [blame] | 255 | int _sccp_parse_connection_released(struct msgb *msgb, struct sccp_parse_result *result) |
Holger Hans Peter Freyther | a1b5980 | 2010-01-27 12:12:46 +0100 | [diff] [blame] | 256 | { |
Holger Hans Peter Freyther | 5defc56 | 2010-01-29 03:49:32 +0100 | [diff] [blame] | 257 | 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 Freyther | c749e8b | 2010-04-21 21:08:58 +0800 | [diff] [blame] | 265 | LOGP(DSCCP, LOGL_ERROR, "msgb > header_size %u %u\n", |
Holger Hans Peter Freyther | 5defc56 | 2010-01-29 03:49:32 +0100 | [diff] [blame] | 266 | 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 Freyther | c749e8b | 2010-04-21 21:08:58 +0800 | [diff] [blame] | 272 | LOGP(DSCCP, LOGL_ERROR, "parsing of optional data failed.\n"); |
Holger Hans Peter Freyther | 5defc56 | 2010-01-29 03:49:32 +0100 | [diff] [blame] | 273 | 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 Freyther | a1b5980 | 2010-01-27 12:12:46 +0100 | [diff] [blame] | 287 | } |
| 288 | |
Holger Hans Peter Freyther | 6233c58 | 2010-01-29 03:58:12 +0100 | [diff] [blame] | 289 | int _sccp_parse_connection_refused(struct msgb *msgb, struct sccp_parse_result *result) |
Holger Hans Peter Freyther | a1b5980 | 2010-01-27 12:12:46 +0100 | [diff] [blame] | 290 | { |
Holger Hans Peter Freyther | 6233c58 | 2010-01-29 03:58:12 +0100 | [diff] [blame] | 291 | 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 Freyther | c749e8b | 2010-04-21 21:08:58 +0800 | [diff] [blame] | 300 | LOGP(DSCCP, LOGL_ERROR, "msgb < header_size %u %u\n", |
Holger Hans Peter Freyther | 6233c58 | 2010-01-29 03:58:12 +0100 | [diff] [blame] | 301 | 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 Freyther | c749e8b | 2010-04-21 21:08:58 +0800 | [diff] [blame] | 311 | LOGP(DSCCP, LOGL_ERROR, "parsing of optional data failed.\n"); |
Holger Hans Peter Freyther | 6233c58 | 2010-01-29 03:58:12 +0100 | [diff] [blame] | 312 | 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 Freyther | a1b5980 | 2010-01-27 12:12:46 +0100 | [diff] [blame] | 324 | } |
| 325 | |
Holger Hans Peter Freyther | 1f21f34 | 2010-01-29 04:03:00 +0100 | [diff] [blame] | 326 | int _sccp_parse_connection_confirm(struct msgb *msgb, struct sccp_parse_result *result) |
Holger Hans Peter Freyther | a1b5980 | 2010-01-27 12:12:46 +0100 | [diff] [blame] | 327 | { |
Holger Hans Peter Freyther | 1f21f34 | 2010-01-29 04:03:00 +0100 | [diff] [blame] | 328 | 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 Freyther | c749e8b | 2010-04-21 21:08:58 +0800 | [diff] [blame] | 338 | LOGP(DSCCP, LOGL_ERROR, "msgb < header_size %u %u\n", |
Holger Hans Peter Freyther | 1f21f34 | 2010-01-29 04:03:00 +0100 | [diff] [blame] | 339 | 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 Freyther | c749e8b | 2010-04-21 21:08:58 +0800 | [diff] [blame] | 349 | LOGP(DSCCP, LOGL_ERROR, "parsing of optional data failed.\n"); |
Holger Hans Peter Freyther | 1f21f34 | 2010-01-29 04:03:00 +0100 | [diff] [blame] | 350 | 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 Freyther | a1b5980 | 2010-01-27 12:12:46 +0100 | [diff] [blame] | 361 | } |
| 362 | |
Holger Hans Peter Freyther | db56e38 | 2010-01-29 04:19:56 +0100 | [diff] [blame] | 363 | int _sccp_parse_connection_release_complete(struct msgb *msgb, struct sccp_parse_result *result) |
Holger Hans Peter Freyther | a1b5980 | 2010-01-27 12:12:46 +0100 | [diff] [blame] | 364 | { |
Holger Hans Peter Freyther | db56e38 | 2010-01-29 04:19:56 +0100 | [diff] [blame] | 365 | 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 Freyther | c749e8b | 2010-04-21 21:08:58 +0800 | [diff] [blame] | 371 | LOGP(DSCCP, LOGL_ERROR, "msgb < header_size %u %u\n", |
Holger Hans Peter Freyther | db56e38 | 2010-01-29 04:19:56 +0100 | [diff] [blame] | 372 | 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 Freyther | a1b5980 | 2010-01-27 12:12:46 +0100 | [diff] [blame] | 381 | } |
| 382 | |
Holger Hans Peter Freyther | 8edb961 | 2010-01-29 04:31:00 +0100 | [diff] [blame] | 383 | int _sccp_parse_connection_dt1(struct msgb *msgb, struct sccp_parse_result *result) |
Holger Hans Peter Freyther | a1b5980 | 2010-01-27 12:12:46 +0100 | [diff] [blame] | 384 | { |
Holger Hans Peter Freyther | 8edb961 | 2010-01-29 04:31:00 +0100 | [diff] [blame] | 385 | 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 Freyther | c749e8b | 2010-04-21 21:08:58 +0800 | [diff] [blame] | 392 | LOGP(DSCCP, LOGL_ERROR, "msgb > header_size %u %u\n", |
Holger Hans Peter Freyther | 8edb961 | 2010-01-29 04:31:00 +0100 | [diff] [blame] | 393 | msgb_l2len(msgb), header_size); |
| 394 | return -1; |
| 395 | } |
| 396 | |
| 397 | if (dt1->segmenting != 0) { |
Holger Hans Peter Freyther | c749e8b | 2010-04-21 21:08:58 +0800 | [diff] [blame] | 398 | LOGP(DSCCP, LOGL_ERROR, "This packet has segmenting, not supported: %d\n", dt1->segmenting); |
Holger Hans Peter Freyther | 8edb961 | 2010-01-29 04:31:00 +0100 | [diff] [blame] | 399 | 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 Freyther | c749e8b | 2010-04-21 21:08:58 +0800 | [diff] [blame] | 406 | LOGP(DSCCP, LOGL_ERROR, "Not enough space for variable start: %u %u\n", |
Holger Hans Peter Freyther | 8edb961 | 2010-01-29 04:31:00 +0100 | [diff] [blame] | 407 | 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 Freyther | c749e8b | 2010-04-21 21:08:58 +0800 | [diff] [blame] | 415 | LOGP(DSCCP, LOGL_ERROR, "Not enough room for the payload: %u %u\n", |
Holger Hans Peter Freyther | 8edb961 | 2010-01-29 04:31:00 +0100 | [diff] [blame] | 416 | msgb_l3len(msgb), result->data_len); |
| 417 | return -1; |
| 418 | } |
| 419 | |
| 420 | return 0; |
Holger Hans Peter Freyther | a1b5980 | 2010-01-27 12:12:46 +0100 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | int _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 Freyther | c749e8b | 2010-04-21 21:08:58 +0800 | [diff] [blame] | 433 | LOGP(DSCCP, LOGL_ERROR, "msgb < header_size %u %u\n", |
Holger Hans Peter Freyther | a1b5980 | 2010-01-27 12:12:46 +0100 | [diff] [blame] | 434 | 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 Freyther | c749e8b | 2010-04-21 21:08:58 +0800 | [diff] [blame] | 443 | LOGP(DSCCP, LOGL_ERROR, "Invalid called address according to 08.06: 0x%x 0x%x\n", |
Holger Hans Peter Freyther | a1b5980 | 2010-01-27 12:12:46 +0100 | [diff] [blame] | 444 | *(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 Freyther | c749e8b | 2010-04-21 21:08:58 +0800 | [diff] [blame] | 452 | LOGP(DSCCP, LOGL_ERROR, "Invalid called address according to 08.06: 0x%x 0x%x\n", |
Holger Hans Peter Freyther | a1b5980 | 2010-01-27 12:12:46 +0100 | [diff] [blame] | 453 | *(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 Freyther | c749e8b | 2010-04-21 21:08:58 +0800 | [diff] [blame] | 458 | LOGP(DSCCP, LOGL_ERROR, "msgb < header + offset %u %u %u\n", |
Holger Hans Peter Freyther | a1b5980 | 2010-01-27 12:12:46 +0100 | [diff] [blame] | 459 | 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 Freyther | f058610 | 2010-02-12 23:08:21 +0100 | [diff] [blame] | 465 | result->data_len = msgb_l3len(msgb); |
Holger Hans Peter Freyther | a1b5980 | 2010-01-27 12:12:46 +0100 | [diff] [blame] | 466 | |
| 467 | if (msgb_l3len(msgb) != msgb->l3h[-1]) { |
Holger Hans Peter Freyther | c749e8b | 2010-04-21 21:08:58 +0800 | [diff] [blame] | 468 | LOGP(DSCCP, LOGL_ERROR, "msgb is truncated is: %u should: %u\n", |
Holger Hans Peter Freyther | a1b5980 | 2010-01-27 12:12:46 +0100 | [diff] [blame] | 469 | msgb_l3len(msgb), msgb->l3h[-1]); |
| 470 | return -1; |
| 471 | } |
| 472 | |
| 473 | return 0; |
| 474 | } |
| 475 | |
Holger Hans Peter Freyther | 685b039 | 2010-02-26 19:26:35 +0100 | [diff] [blame] | 476 | static 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 Freyther | c749e8b | 2010-04-21 21:08:58 +0800 | [diff] [blame] | 483 | LOGP(DSCCP, LOGL_ERROR, "msgb < header_size %u %u\n", |
Holger Hans Peter Freyther | 685b039 | 2010-02-26 19:26:35 +0100 | [diff] [blame] | 484 | 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 Freyther | 3067d72 | 2010-04-21 15:38:16 +0800 | [diff] [blame] | 495 | static 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 Freyther | a1b5980 | 2010-01-27 12:12:46 +0100 | [diff] [blame] | 512 | |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 513 | /* |
| 514 | * Send UDT. Currently we have a fixed address... |
| 515 | */ |
| 516 | static 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 Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 521 | |
| 522 | if (msgb_l3len(payload) > 256) { |
Holger Hans Peter Freyther | c749e8b | 2010-04-21 21:08:58 +0800 | [diff] [blame] | 523 | LOGP(DSCCP, LOGL_ERROR, "The payload is too big for one udt\n"); |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 524 | 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 Freyther | 9f2f471 | 2010-03-26 05:44:21 +0100 | [diff] [blame] | 554 | _send_msg(msg); |
| 555 | return 0; |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | static int _sccp_handle_read(struct msgb *msgb) |
| 559 | { |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 560 | struct sccp_data_callback *cb; |
Holger Hans Peter Freyther | a1b5980 | 2010-01-27 12:12:46 +0100 | [diff] [blame] | 561 | struct sccp_parse_result result; |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 562 | |
Holger Hans Peter Freyther | a1b5980 | 2010-01-27 12:12:46 +0100 | [diff] [blame] | 563 | if (_sccp_parse_udt(msgb, &result) != 0) |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 564 | return -1; |
| 565 | |
Holger Hans Peter Freyther | a1b5980 | 2010-01-27 12:12:46 +0100 | [diff] [blame] | 566 | cb = _find_ssn(result.called.ssn); |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 567 | if (!cb || !cb->read_cb) { |
Holger Hans Peter Freyther | c749e8b | 2010-04-21 21:08:58 +0800 | [diff] [blame] | 568 | LOGP(DSCCP, LOGL_ERROR, "No routing for UDT for called SSN: %u\n", result.called.ssn); |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 569 | 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 | */ |
| 579 | static 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 | |
| 591 | static 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 | |
| 603 | static 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 Freyther | c749e8b | 2010-04-21 21:08:58 +0800 | [diff] [blame] | 617 | LOGP(DSCCP, LOGL_DEBUG, "Wrapped searching for a free code\n"); |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 618 | 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 Freyther | c749e8b | 2010-04-21 21:08:58 +0800 | [diff] [blame] | 628 | LOGP(DSCCP, LOGL_ERROR, "Finding a free reference failed\n"); |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 629 | return -1; |
| 630 | } |
| 631 | |
| 632 | static 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 Freyther | 0b3011c | 2010-05-15 05:35:48 +0800 | [diff] [blame] | 641 | struct msgb *sccp_create_refuse(struct sccp_source_reference *src_ref, int cause, uint8_t *inp, int length) |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 642 | { |
| 643 | struct msgb *msgb; |
| 644 | struct sccp_connection_refused *ref; |
| 645 | u_int8_t *data; |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 646 | |
| 647 | msgb = msgb_alloc_headroom(SCCP_MSG_SIZE, |
| 648 | SCCP_MSG_HEADROOM, "sccp ref"); |
Holger Hans Peter Freyther | 34ec49d | 2010-05-14 03:34:35 +0800 | [diff] [blame] | 649 | if (!msgb) { |
| 650 | LOGP(DSCCP, LOGL_ERROR, "Failed to allocate refusal msg.\n"); |
| 651 | return NULL; |
| 652 | } |
| 653 | |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 654 | 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 Freyther | 867d836 | 2010-01-27 12:25:13 +0100 | [diff] [blame] | 658 | memcpy(&ref->destination_local_reference, src_ref, |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 659 | sizeof(struct sccp_source_reference)); |
| 660 | ref->cause = cause; |
| 661 | ref->optional_start = 1; |
| 662 | |
Holger Hans Peter Freyther | 0b3011c | 2010-05-15 05:35:48 +0800 | [diff] [blame] | 663 | 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 Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 670 | data = msgb_put(msgb, 1); |
| 671 | data[0] = SCCP_PNC_END_OF_OPTIONAL; |
Holger Hans Peter Freyther | 34ec49d | 2010-05-14 03:34:35 +0800 | [diff] [blame] | 672 | return msgb; |
| 673 | } |
| 674 | |
| 675 | static int _sccp_send_refuse(struct sccp_source_reference *src_ref, int cause) |
| 676 | { |
Holger Hans Peter Freyther | 0b3011c | 2010-05-15 05:35:48 +0800 | [diff] [blame] | 677 | struct msgb *msgb = sccp_create_refuse(src_ref, cause, NULL, 0); |
Holger Hans Peter Freyther | 34ec49d | 2010-05-14 03:34:35 +0800 | [diff] [blame] | 678 | if (!msgb) |
| 679 | return -1; |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 680 | |
Holger Hans Peter Freyther | 9f2f471 | 2010-03-26 05:44:21 +0100 | [diff] [blame] | 681 | _send_msg(msgb); |
| 682 | return 0; |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 683 | } |
| 684 | |
Holger Hans Peter Freyther | 3ca6065 | 2010-05-16 17:55:07 +0800 | [diff] [blame] | 685 | struct msgb *sccp_create_cc(struct sccp_source_reference *src_ref, |
| 686 | struct sccp_source_reference *dst_ref) |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 687 | { |
| 688 | struct msgb *response; |
| 689 | struct sccp_connection_confirm *confirm; |
| 690 | u_int8_t *optional_data; |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 691 | |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 692 | response = msgb_alloc_headroom(SCCP_MSG_SIZE, |
| 693 | SCCP_MSG_HEADROOM, "sccp confirm"); |
Holger Hans Peter Freyther | 3ca6065 | 2010-05-16 17:55:07 +0800 | [diff] [blame] | 694 | if (!response) { |
| 695 | LOGP(DSCCP, LOGL_ERROR, "Failed to create SCCP Confirm.\n"); |
| 696 | return NULL; |
| 697 | } |
| 698 | |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 699 | response->l2h = &response->data[0]; |
| 700 | |
| 701 | confirm = (struct sccp_connection_confirm *) msgb_put(response, sizeof(*confirm)); |
| 702 | |
| 703 | confirm->type = SCCP_MSG_TYPE_CC; |
| 704 | memcpy(&confirm->destination_local_reference, |
Holger Hans Peter Freyther | 3ca6065 | 2010-05-16 17:55:07 +0800 | [diff] [blame] | 705 | dst_ref, sizeof(*dst_ref)); |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 706 | memcpy(&confirm->source_local_reference, |
Holger Hans Peter Freyther | 3ca6065 | 2010-05-16 17:55:07 +0800 | [diff] [blame] | 707 | src_ref, sizeof(*src_ref)); |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 708 | confirm->proto_class = 2; |
| 709 | confirm->optional_start = 1; |
| 710 | |
| 711 | optional_data = (u_int8_t *) msgb_put(response, 1); |
| 712 | optional_data[0] = SCCP_PNC_END_OF_OPTIONAL; |
Holger Hans Peter Freyther | 3ca6065 | 2010-05-16 17:55:07 +0800 | [diff] [blame] | 713 | return response; |
| 714 | } |
| 715 | |
| 716 | static int _sccp_send_connection_confirm(struct sccp_connection *connection) |
| 717 | { |
| 718 | struct msgb *response; |
| 719 | |
| 720 | if (assign_source_local_reference(connection) != 0) |
| 721 | return -1; |
| 722 | |
| 723 | response = sccp_create_cc(&connection->source_local_reference, |
| 724 | &connection->destination_local_reference); |
| 725 | if (!response) |
| 726 | return -1; |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 727 | |
Holger Hans Peter Freyther | 9f2f471 | 2010-03-26 05:44:21 +0100 | [diff] [blame] | 728 | _send_msg(response); |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 729 | _sccp_set_connection_state(connection, SCCP_CONNECTION_STATE_ESTABLISHED); |
Holger Hans Peter Freyther | 9f2f471 | 2010-03-26 05:44:21 +0100 | [diff] [blame] | 730 | return 0; |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 731 | } |
| 732 | |
| 733 | static int _sccp_send_connection_request(struct sccp_connection *connection, |
| 734 | const struct sockaddr_sccp *called, struct msgb *msg) |
| 735 | { |
| 736 | struct msgb *request; |
| 737 | struct sccp_connection_request *req; |
| 738 | u_int8_t *data; |
| 739 | u_int8_t extra_size = 3 + 1; |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 740 | |
| 741 | |
| 742 | if (msg && (msgb_l3len(msg) < 3 || msgb_l3len(msg) > 130)) { |
Holger Hans Peter Freyther | c749e8b | 2010-04-21 21:08:58 +0800 | [diff] [blame] | 743 | LOGP(DSCCP, LOGL_ERROR, "Invalid amount of data... %d\n", msgb_l3len(msg)); |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 744 | return -1; |
| 745 | } |
| 746 | |
| 747 | /* try to find a id */ |
| 748 | if (assign_source_local_reference(connection) != 0) { |
Holger Hans Peter Freyther | c749e8b | 2010-04-21 21:08:58 +0800 | [diff] [blame] | 749 | LOGP(DSCCP, LOGL_ERROR, "Assigning a local reference failed.\n"); |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 750 | _sccp_set_connection_state(connection, SCCP_CONNECTION_STATE_SETUP_ERROR); |
| 751 | return -1; |
| 752 | } |
| 753 | |
| 754 | |
| 755 | if (msg) |
| 756 | extra_size += 2 + msgb_l3len(msg); |
| 757 | request = msgb_alloc_headroom(SCCP_MSG_SIZE, |
| 758 | SCCP_MSG_HEADROOM, "sccp connection request"); |
| 759 | request->l2h = &request->data[0]; |
| 760 | req = (struct sccp_connection_request *) msgb_put(request, sizeof(*req)); |
| 761 | |
| 762 | req->type = SCCP_MSG_TYPE_CR; |
| 763 | memcpy(&req->source_local_reference, &connection->source_local_reference, |
| 764 | sizeof(connection->source_local_reference)); |
| 765 | req->proto_class = 2; |
| 766 | req->variable_called = 2; |
| 767 | req->optional_start = 4; |
| 768 | |
| 769 | /* write the called party address */ |
| 770 | data = msgb_put(request, 1 + 2); |
| 771 | data[0] = 2; |
| 772 | data[1] = 0x42; |
| 773 | data[2] = called->sccp_ssn; |
| 774 | |
| 775 | /* write the payload */ |
| 776 | if (msg) { |
| 777 | data = msgb_put(request, 2 + msgb_l3len(msg)); |
| 778 | data[0] = SCCP_PNC_DATA; |
| 779 | data[1] = msgb_l3len(msg); |
| 780 | memcpy(&data[2], msg->l3h, msgb_l3len(msg)); |
| 781 | } |
| 782 | |
| 783 | data = msgb_put(request, 1); |
| 784 | data[0] = SCCP_PNC_END_OF_OPTIONAL; |
| 785 | |
| 786 | llist_add_tail(&connection->list, &sccp_connections); |
| 787 | _sccp_set_connection_state(connection, SCCP_CONNECTION_STATE_REQUEST); |
| 788 | |
Holger Hans Peter Freyther | 9f2f471 | 2010-03-26 05:44:21 +0100 | [diff] [blame] | 789 | _send_msg(request); |
| 790 | return 0; |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 791 | } |
| 792 | |
Holger Hans Peter Freyther | 6d464dd | 2010-05-16 20:41:48 +0800 | [diff] [blame] | 793 | struct msgb *sccp_create_dt1(struct sccp_source_reference *dst_ref, uint8_t *inp_data, uint8_t len) |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 794 | { |
| 795 | struct msgb *msgb; |
| 796 | struct sccp_data_form1 *dt1; |
| 797 | u_int8_t *data; |
Holger Hans Peter Freyther | 6d464dd | 2010-05-16 20:41:48 +0800 | [diff] [blame] | 798 | |
| 799 | msgb = msgb_alloc_headroom(SCCP_MSG_SIZE, |
| 800 | SCCP_MSG_HEADROOM, "sccp dt1"); |
| 801 | if (!msgb) { |
| 802 | LOGP(DSCCP, LOGL_ERROR, "Failed to create DT1 msg.\n"); |
| 803 | return NULL; |
| 804 | } |
| 805 | |
| 806 | msgb->l2h = &msgb->data[0]; |
| 807 | |
| 808 | dt1 = (struct sccp_data_form1 *) msgb_put(msgb, sizeof(*dt1)); |
| 809 | dt1->type = SCCP_MSG_TYPE_DT1; |
| 810 | memcpy(&dt1->destination_local_reference, dst_ref, |
| 811 | sizeof(struct sccp_source_reference)); |
| 812 | dt1->segmenting = 0; |
| 813 | |
| 814 | /* copy the data */ |
| 815 | dt1->variable_start = 1; |
| 816 | data = msgb_put(msgb, 1 + len); |
| 817 | data[0] = len; |
| 818 | memcpy(&data[1], inp_data, len); |
| 819 | |
| 820 | return msgb; |
| 821 | } |
| 822 | |
| 823 | static int _sccp_send_connection_data(struct sccp_connection *conn, struct msgb *_data) |
| 824 | { |
| 825 | struct msgb *msgb; |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 826 | |
| 827 | if (msgb_l3len(_data) < 2 || msgb_l3len(_data) > 256) { |
Holger Hans Peter Freyther | c749e8b | 2010-04-21 21:08:58 +0800 | [diff] [blame] | 828 | LOGP(DSCCP, LOGL_ERROR, "data size too big, segmenting unimplemented.\n"); |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 829 | return -1; |
| 830 | } |
| 831 | |
Holger Hans Peter Freyther | 6d464dd | 2010-05-16 20:41:48 +0800 | [diff] [blame] | 832 | msgb = sccp_create_dt1(&conn->destination_local_reference, |
| 833 | _data->l3h, msgb_l3len(_data)); |
| 834 | if (!msgb) |
| 835 | return -1; |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 836 | |
Holger Hans Peter Freyther | 9f2f471 | 2010-03-26 05:44:21 +0100 | [diff] [blame] | 837 | _send_msg(msgb); |
| 838 | return 0; |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 839 | } |
| 840 | |
Holger Hans Peter Freyther | 9343821 | 2009-11-18 22:11:28 +0100 | [diff] [blame] | 841 | static int _sccp_send_connection_it(struct sccp_connection *conn) |
| 842 | { |
| 843 | struct msgb *msgb; |
| 844 | struct sccp_data_it *it; |
Holger Hans Peter Freyther | 9343821 | 2009-11-18 22:11:28 +0100 | [diff] [blame] | 845 | |
| 846 | msgb = msgb_alloc_headroom(SCCP_MSG_SIZE, |
| 847 | SCCP_MSG_HEADROOM, "sccp it"); |
| 848 | msgb->l2h = &msgb->data[0]; |
| 849 | it = (struct sccp_data_it *) msgb_put(msgb, sizeof(*it)); |
| 850 | it->type = SCCP_MSG_TYPE_IT; |
| 851 | memcpy(&it->destination_local_reference, &conn->destination_local_reference, |
| 852 | sizeof(struct sccp_source_reference)); |
| 853 | memcpy(&it->source_local_reference, &conn->source_local_reference, |
| 854 | sizeof(struct sccp_source_reference)); |
| 855 | |
| 856 | it->proto_class = 0x2; |
| 857 | it->sequencing[0] = it->sequencing[1] = 0; |
| 858 | it->credit = 0; |
| 859 | |
Holger Hans Peter Freyther | 9f2f471 | 2010-03-26 05:44:21 +0100 | [diff] [blame] | 860 | _send_msg(msgb); |
| 861 | return 0; |
Holger Hans Peter Freyther | 9343821 | 2009-11-18 22:11:28 +0100 | [diff] [blame] | 862 | } |
| 863 | |
Holger Hans Peter Freyther | 656fe86 | 2010-05-16 18:33:26 +0800 | [diff] [blame] | 864 | struct msgb *sccp_create_rlsd(struct sccp_source_reference *src_ref, |
| 865 | struct sccp_source_reference *dst_ref, int cause) |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 866 | { |
| 867 | struct msgb *msg; |
| 868 | struct sccp_connection_released *rel; |
| 869 | u_int8_t *data; |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 870 | |
| 871 | msg = msgb_alloc_headroom(SCCP_MSG_SIZE, SCCP_MSG_HEADROOM, |
| 872 | "sccp: connection released"); |
Holger Hans Peter Freyther | 656fe86 | 2010-05-16 18:33:26 +0800 | [diff] [blame] | 873 | if (!msg) { |
| 874 | LOGP(DSCCP, LOGL_ERROR, "Failed to allocate RLSD.\n"); |
| 875 | return NULL; |
| 876 | } |
| 877 | |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 878 | msg->l2h = &msg->data[0]; |
| 879 | rel = (struct sccp_connection_released *) msgb_put(msg, sizeof(*rel)); |
| 880 | rel->type = SCCP_MSG_TYPE_RLSD; |
| 881 | rel->release_cause = cause; |
| 882 | |
| 883 | /* copy the source references */ |
Holger Hans Peter Freyther | 656fe86 | 2010-05-16 18:33:26 +0800 | [diff] [blame] | 884 | memcpy(&rel->destination_local_reference, dst_ref, |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 885 | sizeof(struct sccp_source_reference)); |
Holger Hans Peter Freyther | 656fe86 | 2010-05-16 18:33:26 +0800 | [diff] [blame] | 886 | memcpy(&rel->source_local_reference, src_ref, |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 887 | sizeof(struct sccp_source_reference)); |
| 888 | |
| 889 | data = msgb_put(msg, 1); |
| 890 | data[0] = SCCP_PNC_END_OF_OPTIONAL; |
Holger Hans Peter Freyther | 656fe86 | 2010-05-16 18:33:26 +0800 | [diff] [blame] | 891 | return msg; |
| 892 | } |
| 893 | |
| 894 | static int _sccp_send_connection_released(struct sccp_connection *conn, int cause) |
| 895 | { |
| 896 | struct msgb *msg; |
| 897 | |
| 898 | msg = sccp_create_rlsd(&conn->source_local_reference, |
| 899 | &conn->destination_local_reference, |
| 900 | cause); |
| 901 | if (!msg) |
| 902 | return -1; |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 903 | |
| 904 | _sccp_set_connection_state(conn, SCCP_CONNECTION_STATE_RELEASE); |
Holger Hans Peter Freyther | 9f2f471 | 2010-03-26 05:44:21 +0100 | [diff] [blame] | 905 | _send_msg(msg); |
| 906 | return 0; |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 907 | } |
| 908 | |
| 909 | /* |
| 910 | * Open a connection. The following is going to happen: |
| 911 | * |
| 912 | * - Verify the packet, e.g. that we have no other connection |
| 913 | * that id. |
| 914 | * - Ask the user if he wants to accept the connection |
| 915 | * - Try to open the connection by assigning a source local reference |
| 916 | * and sending the packet |
| 917 | */ |
| 918 | static int _sccp_handle_connection_request(struct msgb *msgb) |
| 919 | { |
Holger Hans Peter Freyther | 867d836 | 2010-01-27 12:25:13 +0100 | [diff] [blame] | 920 | struct sccp_parse_result result; |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 921 | |
| 922 | struct sccp_data_callback *cb; |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 923 | struct sccp_connection *connection; |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 924 | |
Holger Hans Peter Freyther | 867d836 | 2010-01-27 12:25:13 +0100 | [diff] [blame] | 925 | if (_sccp_parse_connection_request(msgb, &result) != 0) |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 926 | return -1; |
| 927 | |
Holger Hans Peter Freyther | 867d836 | 2010-01-27 12:25:13 +0100 | [diff] [blame] | 928 | cb = _find_ssn(result.called.ssn); |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 929 | if (!cb || !cb->accept_cb) { |
Holger Hans Peter Freyther | c749e8b | 2010-04-21 21:08:58 +0800 | [diff] [blame] | 930 | LOGP(DSCCP, LOGL_ERROR, "No routing for CR for called SSN: %u\n", result.called.ssn); |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 931 | return -1; |
| 932 | } |
| 933 | |
| 934 | /* check if the system wants this connection */ |
| 935 | connection = talloc_zero(tall_sccp_ctx, struct sccp_connection); |
| 936 | if (!connection) { |
Holger Hans Peter Freyther | c749e8b | 2010-04-21 21:08:58 +0800 | [diff] [blame] | 937 | LOGP(DSCCP, LOGL_ERROR, "Allocation failed\n"); |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 938 | return -1; |
| 939 | } |
| 940 | |
| 941 | /* |
| 942 | * sanity checks: |
| 943 | * - Is the source_local_reference in any other connection? |
| 944 | * then will call accept, assign a "destination" local reference |
| 945 | * and send a connection confirm, otherwise we will send a refuseed |
| 946 | * one.... |
| 947 | */ |
Holger Hans Peter Freyther | 867d836 | 2010-01-27 12:25:13 +0100 | [diff] [blame] | 948 | if (destination_local_reference_is_free(result.source_local_reference) != 0) { |
Holger Hans Peter Freyther | c749e8b | 2010-04-21 21:08:58 +0800 | [diff] [blame] | 949 | LOGP(DSCCP, LOGL_ERROR, "Need to reject connection with existing reference\n"); |
Holger Hans Peter Freyther | 867d836 | 2010-01-27 12:25:13 +0100 | [diff] [blame] | 950 | _sccp_send_refuse(result.source_local_reference, SCCP_REFUSAL_SCCP_FAILURE); |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 951 | talloc_free(connection); |
| 952 | return -1; |
| 953 | } |
| 954 | |
| 955 | connection->incoming = 1; |
Holger Hans Peter Freyther | 867d836 | 2010-01-27 12:25:13 +0100 | [diff] [blame] | 956 | connection->destination_local_reference = *result.source_local_reference; |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 957 | |
| 958 | if (cb->accept_cb(connection, cb->accept_context) != 0) { |
Holger Hans Peter Freyther | 867d836 | 2010-01-27 12:25:13 +0100 | [diff] [blame] | 959 | _sccp_send_refuse(result.source_local_reference, SCCP_REFUSAL_END_USER_ORIGINATED); |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 960 | _sccp_set_connection_state(connection, SCCP_CONNECTION_STATE_REFUSED); |
| 961 | talloc_free(connection); |
| 962 | return 0; |
| 963 | } |
| 964 | |
| 965 | |
| 966 | llist_add_tail(&connection->list, &sccp_connections); |
| 967 | |
| 968 | if (_sccp_send_connection_confirm(connection) != 0) { |
Holger Hans Peter Freyther | c749e8b | 2010-04-21 21:08:58 +0800 | [diff] [blame] | 969 | LOGP(DSCCP, LOGL_ERROR, "Sending confirm failed... no available source reference?\n"); |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 970 | |
Holger Hans Peter Freyther | 867d836 | 2010-01-27 12:25:13 +0100 | [diff] [blame] | 971 | _sccp_send_refuse(result.source_local_reference, SCCP_REFUSAL_SCCP_FAILURE); |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 972 | _sccp_set_connection_state(connection, SCCP_CONNECTION_STATE_REFUSED); |
| 973 | llist_del(&connection->list); |
| 974 | talloc_free(connection); |
| 975 | |
| 976 | return -1; |
| 977 | } |
| 978 | |
| 979 | /* |
| 980 | * If we have data let us forward things. |
| 981 | */ |
Holger Hans Peter Freyther | 867d836 | 2010-01-27 12:25:13 +0100 | [diff] [blame] | 982 | if (result.data_len != 0 && connection->data_cb) { |
| 983 | connection->data_cb(connection, msgb, result.data_len); |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 984 | } |
| 985 | |
| 986 | return 0; |
| 987 | } |
| 988 | |
| 989 | /* Handle the release confirmed */ |
Holger Hans Peter Freyther | db56e38 | 2010-01-29 04:19:56 +0100 | [diff] [blame] | 990 | static int _sccp_handle_connection_release_complete(struct msgb *msgb) |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 991 | { |
Holger Hans Peter Freyther | db56e38 | 2010-01-29 04:19:56 +0100 | [diff] [blame] | 992 | struct sccp_parse_result result; |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 993 | struct sccp_connection *conn; |
| 994 | |
Holger Hans Peter Freyther | db56e38 | 2010-01-29 04:19:56 +0100 | [diff] [blame] | 995 | if (_sccp_parse_connection_release_complete(msgb, &result) != 0) |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 996 | return -1; |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 997 | |
| 998 | /* find the connection */ |
| 999 | llist_for_each_entry(conn, &sccp_connections, list) { |
| 1000 | if (conn->data_cb |
| 1001 | && memcmp(&conn->source_local_reference, |
Holger Hans Peter Freyther | db56e38 | 2010-01-29 04:19:56 +0100 | [diff] [blame] | 1002 | result.destination_local_reference, |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 1003 | sizeof(conn->source_local_reference)) == 0 |
| 1004 | && memcmp(&conn->destination_local_reference, |
Holger Hans Peter Freyther | db56e38 | 2010-01-29 04:19:56 +0100 | [diff] [blame] | 1005 | result.source_local_reference, |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 1006 | sizeof(conn->destination_local_reference)) == 0) { |
| 1007 | goto found; |
| 1008 | } |
| 1009 | } |
| 1010 | |
| 1011 | |
Holger Hans Peter Freyther | c749e8b | 2010-04-21 21:08:58 +0800 | [diff] [blame] | 1012 | LOGP(DSCCP, LOGL_ERROR, "Release complete of unknown connection\n"); |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 1013 | return -1; |
| 1014 | |
| 1015 | found: |
| 1016 | llist_del(&conn->list); |
| 1017 | _sccp_set_connection_state(conn, SCCP_CONNECTION_STATE_RELEASE_COMPLETE); |
| 1018 | return 0; |
| 1019 | } |
| 1020 | |
| 1021 | /* Handle the Data Form 1 message */ |
Holger Hans Peter Freyther | 8edb961 | 2010-01-29 04:31:00 +0100 | [diff] [blame] | 1022 | static int _sccp_handle_connection_dt1(struct msgb *msgb) |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 1023 | { |
Holger Hans Peter Freyther | 8edb961 | 2010-01-29 04:31:00 +0100 | [diff] [blame] | 1024 | struct sccp_parse_result result; |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 1025 | struct sccp_connection *conn; |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 1026 | |
Holger Hans Peter Freyther | 8edb961 | 2010-01-29 04:31:00 +0100 | [diff] [blame] | 1027 | if (_sccp_parse_connection_dt1(msgb, &result) != 0) |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 1028 | return -1; |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 1029 | |
| 1030 | /* lookup if we have a connection with the given reference */ |
| 1031 | llist_for_each_entry(conn, &sccp_connections, list) { |
| 1032 | if (conn->data_cb |
| 1033 | && memcmp(&conn->source_local_reference, |
Holger Hans Peter Freyther | 8edb961 | 2010-01-29 04:31:00 +0100 | [diff] [blame] | 1034 | result.destination_local_reference, |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 1035 | sizeof(conn->source_local_reference)) == 0) { |
Holger Hans Peter Freyther | 8edb961 | 2010-01-29 04:31:00 +0100 | [diff] [blame] | 1036 | goto found; |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 1037 | } |
| 1038 | } |
| 1039 | |
Holger Hans Peter Freyther | c749e8b | 2010-04-21 21:08:58 +0800 | [diff] [blame] | 1040 | LOGP(DSCCP, LOGL_ERROR, "No connection found for dt1 data\n"); |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 1041 | return -1; |
Holger Hans Peter Freyther | 8edb961 | 2010-01-29 04:31:00 +0100 | [diff] [blame] | 1042 | |
| 1043 | found: |
| 1044 | conn->data_cb(conn, msgb, result.data_len); |
| 1045 | return 0; |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 1046 | } |
| 1047 | |
| 1048 | /* confirm a connection release */ |
| 1049 | static int _sccp_send_connection_release_complete(struct sccp_connection *connection) |
| 1050 | { |
| 1051 | struct msgb *msgb; |
| 1052 | struct sccp_connection_release_complete *rlc; |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 1053 | |
| 1054 | msgb = msgb_alloc_headroom(SCCP_MSG_SIZE, |
| 1055 | SCCP_MSG_HEADROOM, "sccp rlc"); |
| 1056 | msgb->l2h = &msgb->data[0]; |
| 1057 | |
| 1058 | rlc = (struct sccp_connection_release_complete *) msgb_put(msgb, sizeof(*rlc)); |
| 1059 | rlc->type = SCCP_MSG_TYPE_RLC; |
| 1060 | memcpy(&rlc->destination_local_reference, |
| 1061 | &connection->destination_local_reference, sizeof(struct sccp_source_reference)); |
| 1062 | memcpy(&rlc->source_local_reference, |
| 1063 | &connection->source_local_reference, sizeof(struct sccp_source_reference)); |
| 1064 | |
Holger Hans Peter Freyther | 9f2f471 | 2010-03-26 05:44:21 +0100 | [diff] [blame] | 1065 | _send_msg(msgb); |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 1066 | |
| 1067 | /* |
| 1068 | * Remove from the list of active connections and set the state. User code |
| 1069 | * should now free the entry. |
| 1070 | */ |
| 1071 | llist_del(&connection->list); |
| 1072 | _sccp_set_connection_state(connection, SCCP_CONNECTION_STATE_RELEASE_COMPLETE); |
Holger Hans Peter Freyther | 9f2f471 | 2010-03-26 05:44:21 +0100 | [diff] [blame] | 1073 | return 0; |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 1074 | } |
| 1075 | |
| 1076 | /* connection released, send a released confirm */ |
Holger Hans Peter Freyther | 5defc56 | 2010-01-29 03:49:32 +0100 | [diff] [blame] | 1077 | static int _sccp_handle_connection_released(struct msgb *msgb) |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 1078 | { |
Holger Hans Peter Freyther | 5defc56 | 2010-01-29 03:49:32 +0100 | [diff] [blame] | 1079 | struct sccp_parse_result result; |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 1080 | struct sccp_connection *conn; |
| 1081 | |
Holger Hans Peter Freyther | 5defc56 | 2010-01-29 03:49:32 +0100 | [diff] [blame] | 1082 | if (_sccp_parse_connection_released(msgb, &result) == -1) |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 1083 | return -1; |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 1084 | |
| 1085 | /* lookup if we have a connection with the given reference */ |
| 1086 | llist_for_each_entry(conn, &sccp_connections, list) { |
| 1087 | if (conn->data_cb |
| 1088 | && memcmp(&conn->source_local_reference, |
Holger Hans Peter Freyther | 5defc56 | 2010-01-29 03:49:32 +0100 | [diff] [blame] | 1089 | result.destination_local_reference, |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 1090 | sizeof(conn->source_local_reference)) == 0 |
| 1091 | && memcmp(&conn->destination_local_reference, |
Holger Hans Peter Freyther | 5defc56 | 2010-01-29 03:49:32 +0100 | [diff] [blame] | 1092 | result.source_local_reference, |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 1093 | sizeof(conn->destination_local_reference)) == 0) { |
| 1094 | goto found; |
| 1095 | } |
| 1096 | } |
| 1097 | |
| 1098 | |
Holger Hans Peter Freyther | c749e8b | 2010-04-21 21:08:58 +0800 | [diff] [blame] | 1099 | LOGP(DSCCP, LOGL_ERROR, "Unknown connection was released.\n"); |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 1100 | return -1; |
| 1101 | |
| 1102 | /* we have found a connection */ |
| 1103 | found: |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 1104 | /* optional data */ |
Holger Hans Peter Freyther | 5defc56 | 2010-01-29 03:49:32 +0100 | [diff] [blame] | 1105 | if (result.data_len != 0 && conn->data_cb) { |
| 1106 | conn->data_cb(conn, msgb, result.data_len); |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 1107 | } |
| 1108 | |
| 1109 | /* generate a response */ |
| 1110 | if (_sccp_send_connection_release_complete(conn) != 0) { |
Holger Hans Peter Freyther | c749e8b | 2010-04-21 21:08:58 +0800 | [diff] [blame] | 1111 | LOGP(DSCCP, LOGL_ERROR, "Sending release confirmed failed\n"); |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 1112 | return -1; |
| 1113 | } |
| 1114 | |
| 1115 | return 0; |
| 1116 | } |
| 1117 | |
| 1118 | static int _sccp_handle_connection_refused(struct msgb *msgb) |
| 1119 | { |
Holger Hans Peter Freyther | 6233c58 | 2010-01-29 03:58:12 +0100 | [diff] [blame] | 1120 | struct sccp_parse_result result; |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 1121 | struct sccp_connection *conn; |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 1122 | |
Holger Hans Peter Freyther | 6233c58 | 2010-01-29 03:58:12 +0100 | [diff] [blame] | 1123 | if (_sccp_parse_connection_refused(msgb, &result) != 0) |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 1124 | return -1; |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 1125 | |
| 1126 | /* lookup if we have a connection with the given reference */ |
| 1127 | llist_for_each_entry(conn, &sccp_connections, list) { |
| 1128 | if (conn->incoming == 0 && conn->data_cb |
| 1129 | && memcmp(&conn->source_local_reference, |
Holger Hans Peter Freyther | 6233c58 | 2010-01-29 03:58:12 +0100 | [diff] [blame] | 1130 | result.destination_local_reference, |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 1131 | sizeof(conn->source_local_reference)) == 0) { |
| 1132 | goto found; |
| 1133 | } |
| 1134 | } |
| 1135 | |
Holger Hans Peter Freyther | c749e8b | 2010-04-21 21:08:58 +0800 | [diff] [blame] | 1136 | LOGP(DSCCP, LOGL_ERROR, "Refused but no connection found\n"); |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 1137 | return -1; |
| 1138 | |
| 1139 | found: |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 1140 | /* optional data */ |
Holger Hans Peter Freyther | 6233c58 | 2010-01-29 03:58:12 +0100 | [diff] [blame] | 1141 | if (result.data_len != 0 && conn->data_cb) { |
| 1142 | conn->data_cb(conn, msgb, result.data_len); |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 1143 | } |
| 1144 | |
| 1145 | |
| 1146 | llist_del(&conn->list); |
| 1147 | _sccp_set_connection_state(conn, SCCP_CONNECTION_STATE_REFUSED); |
| 1148 | return 0; |
| 1149 | } |
| 1150 | |
| 1151 | static int _sccp_handle_connection_confirm(struct msgb *msgb) |
| 1152 | { |
Holger Hans Peter Freyther | 1f21f34 | 2010-01-29 04:03:00 +0100 | [diff] [blame] | 1153 | struct sccp_parse_result result; |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 1154 | struct sccp_connection *conn; |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 1155 | |
Holger Hans Peter Freyther | 1f21f34 | 2010-01-29 04:03:00 +0100 | [diff] [blame] | 1156 | if (_sccp_parse_connection_confirm(msgb, &result) != 0) |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 1157 | return -1; |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 1158 | |
| 1159 | /* lookup if we have a connection with the given reference */ |
| 1160 | llist_for_each_entry(conn, &sccp_connections, list) { |
| 1161 | if (conn->incoming == 0 && conn->data_cb |
| 1162 | && memcmp(&conn->source_local_reference, |
Holger Hans Peter Freyther | 1f21f34 | 2010-01-29 04:03:00 +0100 | [diff] [blame] | 1163 | result.destination_local_reference, |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 1164 | sizeof(conn->source_local_reference)) == 0) { |
| 1165 | goto found; |
| 1166 | } |
| 1167 | } |
| 1168 | |
Holger Hans Peter Freyther | c749e8b | 2010-04-21 21:08:58 +0800 | [diff] [blame] | 1169 | LOGP(DSCCP, LOGL_ERROR, "Confirmed but no connection found\n"); |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 1170 | return -1; |
| 1171 | |
| 1172 | found: |
| 1173 | /* copy the addresses of the connection */ |
Holger Hans Peter Freyther | 1f21f34 | 2010-01-29 04:03:00 +0100 | [diff] [blame] | 1174 | conn->destination_local_reference = *result.source_local_reference; |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 1175 | _sccp_set_connection_state(conn, SCCP_CONNECTION_STATE_ESTABLISHED); |
| 1176 | |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 1177 | /* optional data */ |
Holger Hans Peter Freyther | 1f21f34 | 2010-01-29 04:03:00 +0100 | [diff] [blame] | 1178 | if (result.data_len != 0 && conn->data_cb) { |
| 1179 | conn->data_cb(conn, msgb, result.data_len); |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 1180 | } |
| 1181 | |
| 1182 | return 0; |
| 1183 | } |
| 1184 | |
| 1185 | |
Holger Hans Peter Freyther | 9f2f471 | 2010-03-26 05:44:21 +0100 | [diff] [blame] | 1186 | int sccp_system_init(void (*outgoing)(struct msgb *data, void *ctx), void *ctx) |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 1187 | { |
| 1188 | sccp_system.write_data = outgoing; |
| 1189 | sccp_system.write_context = ctx; |
| 1190 | |
| 1191 | return 0; |
| 1192 | } |
| 1193 | |
| 1194 | /* oh my god a real SCCP packet. need to dispatch it now */ |
| 1195 | int sccp_system_incoming(struct msgb *msgb) |
| 1196 | { |
| 1197 | if (msgb_l2len(msgb) < 1 ) { |
Holger Hans Peter Freyther | c749e8b | 2010-04-21 21:08:58 +0800 | [diff] [blame] | 1198 | LOGP(DSCCP, LOGL_ERROR, "Too short packet\n"); |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 1199 | return -1; |
| 1200 | } |
| 1201 | |
| 1202 | int type = msgb->l2h[0]; |
| 1203 | |
| 1204 | switch(type) { |
| 1205 | case SCCP_MSG_TYPE_CR: |
| 1206 | return _sccp_handle_connection_request(msgb); |
| 1207 | break; |
| 1208 | case SCCP_MSG_TYPE_RLSD: |
| 1209 | return _sccp_handle_connection_released(msgb); |
| 1210 | break; |
| 1211 | case SCCP_MSG_TYPE_CREF: |
| 1212 | return _sccp_handle_connection_refused(msgb); |
| 1213 | break; |
| 1214 | case SCCP_MSG_TYPE_CC: |
| 1215 | return _sccp_handle_connection_confirm(msgb); |
| 1216 | break; |
| 1217 | case SCCP_MSG_TYPE_RLC: |
| 1218 | return _sccp_handle_connection_release_complete(msgb); |
| 1219 | break; |
| 1220 | case SCCP_MSG_TYPE_DT1: |
| 1221 | return _sccp_handle_connection_dt1(msgb); |
| 1222 | break; |
| 1223 | case SCCP_MSG_TYPE_UDT: |
| 1224 | return _sccp_handle_read(msgb); |
| 1225 | break; |
| 1226 | default: |
Holger Hans Peter Freyther | c749e8b | 2010-04-21 21:08:58 +0800 | [diff] [blame] | 1227 | LOGP(DSCCP, LOGL_ERROR, "unimplemented msg type: %d\n", type); |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 1228 | }; |
| 1229 | |
| 1230 | return -1; |
| 1231 | } |
| 1232 | |
| 1233 | /* create a packet from the data */ |
| 1234 | int sccp_connection_write(struct sccp_connection *connection, struct msgb *data) |
| 1235 | { |
| 1236 | if (connection->connection_state < SCCP_CONNECTION_STATE_CONFIRM |
| 1237 | || connection->connection_state > SCCP_CONNECTION_STATE_ESTABLISHED) { |
Holger Hans Peter Freyther | c749e8b | 2010-04-21 21:08:58 +0800 | [diff] [blame] | 1238 | LOGP(DSCCP, LOGL_ERROR, "sccp_connection_write: Wrong connection state: %p %d\n", |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 1239 | connection, connection->connection_state); |
| 1240 | return -1; |
| 1241 | } |
| 1242 | |
| 1243 | return _sccp_send_connection_data(connection, data); |
| 1244 | } |
| 1245 | |
Holger Hans Peter Freyther | 9343821 | 2009-11-18 22:11:28 +0100 | [diff] [blame] | 1246 | /* |
| 1247 | * Send a Inactivity Test message. The owner of the connection |
| 1248 | * should start a timer and call this method regularily. Calling |
| 1249 | * this every 60 seconds should be good enough. |
| 1250 | */ |
| 1251 | int sccp_connection_send_it(struct sccp_connection *connection) |
| 1252 | { |
| 1253 | if (connection->connection_state < SCCP_CONNECTION_STATE_CONFIRM |
| 1254 | || connection->connection_state > SCCP_CONNECTION_STATE_ESTABLISHED) { |
Holger Hans Peter Freyther | c749e8b | 2010-04-21 21:08:58 +0800 | [diff] [blame] | 1255 | LOGP(DSCCP, LOGL_ERROR, "sccp_connection_write: Wrong connection state: %p %d\n", |
Holger Hans Peter Freyther | 9343821 | 2009-11-18 22:11:28 +0100 | [diff] [blame] | 1256 | connection, connection->connection_state); |
| 1257 | return -1; |
| 1258 | } |
| 1259 | |
| 1260 | return _sccp_send_connection_it(connection); |
| 1261 | } |
| 1262 | |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 1263 | /* send a connection release and wait for the connection released */ |
| 1264 | int sccp_connection_close(struct sccp_connection *connection, int cause) |
| 1265 | { |
| 1266 | if (connection->connection_state < SCCP_CONNECTION_STATE_CONFIRM |
| 1267 | || connection->connection_state > SCCP_CONNECTION_STATE_ESTABLISHED) { |
Holger Hans Peter Freyther | c749e8b | 2010-04-21 21:08:58 +0800 | [diff] [blame] | 1268 | LOGP(DSCCP, LOGL_ERROR, "Can not close the connection. It was never opened: %p %d\n", |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 1269 | connection, connection->connection_state); |
| 1270 | return -1; |
| 1271 | } |
| 1272 | |
| 1273 | return _sccp_send_connection_released(connection, cause); |
| 1274 | } |
| 1275 | |
| 1276 | int sccp_connection_free(struct sccp_connection *connection) |
| 1277 | { |
| 1278 | if (connection->connection_state > SCCP_CONNECTION_STATE_NONE |
| 1279 | && connection->connection_state < SCCP_CONNECTION_STATE_RELEASE_COMPLETE) { |
Holger Hans Peter Freyther | c749e8b | 2010-04-21 21:08:58 +0800 | [diff] [blame] | 1280 | LOGP(DSCCP, LOGL_ERROR, "The connection needs to be released before it is freed"); |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 1281 | return -1; |
| 1282 | } |
| 1283 | |
| 1284 | talloc_free(connection); |
| 1285 | return 0; |
| 1286 | } |
| 1287 | |
Holger Hans Peter Freyther | c07274e | 2010-04-05 18:13:40 +0200 | [diff] [blame] | 1288 | int sccp_connection_force_free(struct sccp_connection *con) |
| 1289 | { |
| 1290 | if (con->connection_state > SCCP_CONNECTION_STATE_NONE && |
| 1291 | con->connection_state < SCCP_CONNECTION_STATE_RELEASE_COMPLETE) |
| 1292 | llist_del(&con->list); |
| 1293 | |
| 1294 | con->connection_state = SCCP_CONNECTION_STATE_REFUSED; |
| 1295 | sccp_connection_free(con); |
| 1296 | return 0; |
| 1297 | } |
| 1298 | |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 1299 | struct sccp_connection *sccp_connection_socket(void) |
| 1300 | { |
| 1301 | return talloc_zero(tall_sccp_ctx, struct sccp_connection); |
| 1302 | } |
| 1303 | |
| 1304 | int sccp_connection_connect(struct sccp_connection *conn, |
| 1305 | const struct sockaddr_sccp *local, |
| 1306 | struct msgb *data) |
| 1307 | { |
| 1308 | return _sccp_send_connection_request(conn, local, data); |
| 1309 | } |
| 1310 | |
| 1311 | int sccp_connection_set_incoming(const struct sockaddr_sccp *sock, |
| 1312 | int (*accept_cb)(struct sccp_connection *, void *), void *context) |
| 1313 | { |
| 1314 | struct sccp_data_callback *cb; |
| 1315 | |
| 1316 | if (!sock) |
| 1317 | return -2; |
| 1318 | |
| 1319 | cb = _find_ssn(sock->sccp_ssn); |
| 1320 | if (!cb) |
| 1321 | return -1; |
| 1322 | |
| 1323 | cb->accept_cb = accept_cb; |
| 1324 | cb->accept_context = context; |
| 1325 | return 0; |
| 1326 | } |
| 1327 | |
| 1328 | int sccp_write(struct msgb *data, const struct sockaddr_sccp *in, |
| 1329 | const struct sockaddr_sccp *out, int class) |
| 1330 | { |
| 1331 | return _sccp_send_data(class, in, out, data); |
| 1332 | } |
| 1333 | |
| 1334 | int sccp_set_read(const struct sockaddr_sccp *sock, |
| 1335 | int (*read_cb)(struct msgb *, unsigned int, void *), void *context) |
| 1336 | { |
| 1337 | struct sccp_data_callback *cb; |
| 1338 | |
| 1339 | if (!sock) |
| 1340 | return -2; |
| 1341 | |
| 1342 | cb = _find_ssn(sock->sccp_ssn); |
| 1343 | if (!cb) |
| 1344 | return -1; |
| 1345 | |
| 1346 | cb->read_cb = read_cb; |
| 1347 | cb->read_context = context; |
| 1348 | return 0; |
| 1349 | } |
| 1350 | |
| 1351 | static_assert(sizeof(struct sccp_source_reference) <= sizeof(u_int32_t), enough_space); |
| 1352 | |
| 1353 | u_int32_t sccp_src_ref_to_int(struct sccp_source_reference *ref) |
| 1354 | { |
| 1355 | u_int32_t src_ref = 0; |
| 1356 | memcpy(&src_ref, ref, sizeof(*ref)); |
| 1357 | return src_ref; |
| 1358 | } |
| 1359 | |
| 1360 | struct sccp_source_reference sccp_src_ref_from_int(u_int32_t int_ref) |
| 1361 | { |
| 1362 | struct sccp_source_reference ref; |
| 1363 | memcpy(&ref, &int_ref, sizeof(ref)); |
| 1364 | return ref; |
| 1365 | } |
| 1366 | |
Holger Hans Peter Freyther | 481e10b | 2010-01-13 09:55:43 +0100 | [diff] [blame] | 1367 | int sccp_determine_msg_type(struct msgb *msg) |
| 1368 | { |
| 1369 | if (msgb_l2len(msg) < 1) |
| 1370 | return -1; |
| 1371 | |
| 1372 | return msg->l2h[0]; |
| 1373 | } |
| 1374 | |
Holger Hans Peter Freyther | 8621774 | 2010-01-29 04:31:51 +0100 | [diff] [blame] | 1375 | int sccp_parse_header(struct msgb *msg, struct sccp_parse_result *result) |
| 1376 | { |
| 1377 | int type; |
| 1378 | |
| 1379 | if (msgb_l2len(msg) < 1) |
| 1380 | return -1; |
| 1381 | |
| 1382 | type = msg->l2h[0]; |
| 1383 | switch(type) { |
| 1384 | case SCCP_MSG_TYPE_CR: |
| 1385 | return _sccp_parse_connection_request(msg, result); |
| 1386 | break; |
| 1387 | case SCCP_MSG_TYPE_RLSD: |
| 1388 | return _sccp_parse_connection_released(msg, result); |
| 1389 | break; |
| 1390 | case SCCP_MSG_TYPE_CREF: |
| 1391 | return _sccp_parse_connection_refused(msg, result); |
| 1392 | break; |
| 1393 | case SCCP_MSG_TYPE_CC: |
| 1394 | return _sccp_parse_connection_confirm(msg, result); |
| 1395 | break; |
| 1396 | case SCCP_MSG_TYPE_RLC: |
| 1397 | return _sccp_parse_connection_release_complete(msg, result); |
| 1398 | break; |
| 1399 | case SCCP_MSG_TYPE_DT1: |
| 1400 | return _sccp_parse_connection_dt1(msg, result); |
| 1401 | break; |
| 1402 | case SCCP_MSG_TYPE_UDT: |
| 1403 | return _sccp_parse_udt(msg, result); |
| 1404 | break; |
Holger Hans Peter Freyther | 685b039 | 2010-02-26 19:26:35 +0100 | [diff] [blame] | 1405 | case SCCP_MSG_TYPE_IT: |
| 1406 | return _sccp_parse_it(msg, result); |
| 1407 | break; |
Holger Hans Peter Freyther | 3067d72 | 2010-04-21 15:38:16 +0800 | [diff] [blame] | 1408 | case SCCP_MSG_TYPE_ERR: |
| 1409 | return _sccp_parse_err(msg, result); |
| 1410 | break; |
Holger Hans Peter Freyther | 8621774 | 2010-01-29 04:31:51 +0100 | [diff] [blame] | 1411 | }; |
| 1412 | |
Holger Hans Peter Freyther | 685b039 | 2010-02-26 19:26:35 +0100 | [diff] [blame] | 1413 | LOGP(DSCCP, LOGL_ERROR, "Unimplemented MSG Type: 0x%x\n", type); |
Holger Hans Peter Freyther | 8621774 | 2010-01-29 04:31:51 +0100 | [diff] [blame] | 1414 | return -1; |
| 1415 | } |
| 1416 | |
Holger Hans Peter Freyther | 6094ef2 | 2009-07-29 07:37:48 +0200 | [diff] [blame] | 1417 | static __attribute__((constructor)) void on_dso_load(void) |
| 1418 | { |
| 1419 | tall_sccp_ctx = talloc_named_const(NULL, 1, "sccp"); |
| 1420 | } |
| 1421 | |
| 1422 | static __attribute__((destructor)) void on_dso_unload(void) |
| 1423 | { |
| 1424 | talloc_report_full(tall_sccp_ctx, stderr); |
| 1425 | } |