Holger Hans Peter Freyther | d70a7e8 | 2011-01-17 14:13:29 +0100 | [diff] [blame] | 1 | /* Run M2UA over SCTP here */ |
| 2 | /* (C) 2011 by Holger Hans Peter Freyther <zecke@selfish.org> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU Affero General Public License as published by |
| 6 | * the Free Software Foundation; either version 3 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU Affero General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Affero General Public License |
| 15 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | */ |
| 17 | |
| 18 | #include <sctp_m2ua.h> |
| 19 | #include <bsc_data.h> |
| 20 | #include <cellmgr_debug.h> |
| 21 | #include <mtp_data.h> |
Holger Hans Peter Freyther | c17852e | 2011-01-17 22:23:24 +0100 | [diff] [blame] | 22 | #include <mtp_pcap.h> |
Holger Hans Peter Freyther | d70a7e8 | 2011-01-17 14:13:29 +0100 | [diff] [blame] | 23 | |
| 24 | #include <osmocore/talloc.h> |
| 25 | |
| 26 | #include <sys/socket.h> |
| 27 | #include <arpa/inet.h> |
| 28 | |
| 29 | #include <string.h> |
| 30 | #include <unistd.h> |
| 31 | |
| 32 | extern struct bsc_data bsc; |
| 33 | |
| 34 | static void m2ua_conn_destroy(struct sctp_m2ua_conn *conn) |
| 35 | { |
| 36 | close(conn->queue.bfd.fd); |
| 37 | bsc_unregister_fd(&conn->queue.bfd); |
| 38 | write_queue_clear(&conn->queue); |
| 39 | llist_del(&conn->entry); |
| 40 | |
| 41 | if (conn->asp_up && conn->asp_active && conn->established) |
| 42 | mtp_link_down(&conn->trans->base); |
| 43 | talloc_free(conn); |
| 44 | |
| 45 | #warning "Notify any other AS(P) for failover scenario" |
| 46 | } |
| 47 | |
| 48 | static int m2ua_conn_send(struct sctp_m2ua_conn *conn, |
| 49 | struct m2ua_msg *m2ua, |
| 50 | struct sctp_sndrcvinfo *info) |
| 51 | { |
| 52 | struct msgb *msg; |
| 53 | msg = m2ua_to_msg(m2ua); |
| 54 | if (!msg) |
| 55 | return -1; |
| 56 | |
| 57 | /* save the OOB data in front of the message */ |
| 58 | msg->l2h = msg->data; |
| 59 | msgb_push(msg, sizeof(*info)); |
| 60 | memcpy(msg->data, info, sizeof(*info)); |
| 61 | |
| 62 | if (write_queue_enqueue(&conn->queue, msg) != 0) { |
| 63 | LOGP(DINP, LOGL_ERROR, "Failed to enqueue.\n"); |
| 64 | msgb_free(msg); |
| 65 | return -1; |
| 66 | } |
| 67 | |
| 68 | return 0; |
| 69 | } |
| 70 | |
| 71 | static int m2ua_conn_send_ntfy(struct sctp_m2ua_conn *conn, |
| 72 | struct sctp_sndrcvinfo *info) |
| 73 | { |
| 74 | struct m2ua_msg *msg; |
| 75 | uint16_t state[2]; |
| 76 | int rc; |
| 77 | |
| 78 | msg = m2ua_msg_alloc(); |
| 79 | if (!msg) |
| 80 | return -1; |
| 81 | msg->hdr.msg_class = M2UA_CLS_MGMT; |
| 82 | msg->hdr.msg_type = M2UA_MGMT_NTFY; |
| 83 | |
| 84 | /* state change */ |
| 85 | state[0] = ntohs(M2UA_STP_AS_STATE_CHG); |
| 86 | |
| 87 | if (conn->asp_active) |
| 88 | state[1] = ntohs(M2UA_STP_AS_ACTIVE); |
| 89 | else |
| 90 | state[1] = ntohs(M2UA_STP_AS_INACTIVE); |
| 91 | |
| 92 | m2ua_msg_add_data(msg, MUA_TAG_STATUS, 4, (uint8_t *) state); |
| 93 | m2ua_msg_add_data(msg, MUA_TAG_ASP_IDENT, 4, conn->asp_ident); |
| 94 | rc = m2ua_conn_send(conn, msg, info); |
| 95 | m2ua_msg_free(msg); |
| 96 | |
| 97 | return rc; |
| 98 | } |
| 99 | |
| 100 | static int m2ua_handle_asp_ack(struct sctp_m2ua_conn *conn, |
| 101 | struct m2ua_msg *m2ua, |
| 102 | struct sctp_sndrcvinfo *info) |
| 103 | { |
| 104 | struct m2ua_msg_part *asp_ident; |
| 105 | struct m2ua_msg *ack; |
| 106 | |
| 107 | asp_ident = m2ua_msg_find_tag(m2ua, MUA_TAG_ASP_IDENT); |
| 108 | if (!asp_ident) { |
| 109 | LOGP(DINP, LOGL_ERROR, "ASP UP lacks ASP IDENT\n"); |
| 110 | return -1; |
| 111 | } |
| 112 | if (asp_ident->len != 4) { |
| 113 | LOGP(DINP, LOGL_ERROR, "ASP Ident needs to be four byte.\n"); |
| 114 | return -1; |
| 115 | } |
| 116 | |
| 117 | /* TODO: Better handling for fail over is needed here */ |
| 118 | ack = m2ua_msg_alloc(); |
| 119 | if (!ack) { |
| 120 | LOGP(DINP, LOGL_ERROR, "Failed to create response\n"); |
| 121 | return -1; |
| 122 | } |
| 123 | |
| 124 | ack->hdr.msg_class = M2UA_CLS_ASPSM; |
| 125 | ack->hdr.msg_type = M2UA_ASPSM_UP_ACK; |
| 126 | if (m2ua_conn_send(conn, ack, info) != 0) { |
| 127 | m2ua_msg_free(ack); |
| 128 | return -1; |
| 129 | } |
| 130 | |
| 131 | memcpy(conn->asp_ident, asp_ident->dat, 4); |
| 132 | conn->asp_up = 1; |
| 133 | |
| 134 | m2ua_conn_send_ntfy(conn, info); |
| 135 | m2ua_msg_free(ack); |
| 136 | return 0; |
| 137 | } |
| 138 | |
| 139 | static int m2ua_handle_asp(struct sctp_m2ua_conn *conn, |
| 140 | struct m2ua_msg *m2ua, struct sctp_sndrcvinfo *info) |
| 141 | { |
| 142 | switch (m2ua->hdr.msg_type) { |
| 143 | case M2UA_ASPSM_UP: |
| 144 | m2ua_handle_asp_ack(conn, m2ua, info); |
| 145 | break; |
| 146 | default: |
| 147 | LOGP(DINP, LOGL_ERROR, "Unhandled msg_type %d\n", |
| 148 | m2ua->hdr.msg_type); |
| 149 | break; |
| 150 | } |
| 151 | |
| 152 | return 0; |
| 153 | } |
| 154 | |
| 155 | static int m2ua_handle_asptm_act(struct sctp_m2ua_conn *conn, |
| 156 | struct m2ua_msg *m2ua, |
| 157 | struct sctp_sndrcvinfo *info) |
| 158 | { |
| 159 | struct m2ua_msg *ack; |
| 160 | |
| 161 | /* TODO: parse the interface identifiers. This is plural */ |
| 162 | ack = m2ua_msg_alloc(); |
| 163 | if (!ack) |
| 164 | return -1; |
| 165 | |
| 166 | ack->hdr.msg_class = M2UA_CLS_ASPTM; |
| 167 | ack->hdr.msg_type = M2UA_ASPTM_ACTIV_ACK; |
| 168 | |
| 169 | if (m2ua_conn_send(conn, ack, info) != 0) { |
| 170 | m2ua_msg_free(ack); |
| 171 | return -1; |
| 172 | } |
| 173 | |
| 174 | conn->asp_active = 1; |
| 175 | m2ua_conn_send_ntfy(conn, info); |
| 176 | m2ua_msg_free(ack); |
| 177 | return 0; |
| 178 | } |
| 179 | |
| 180 | static int m2ua_handle_asptm(struct sctp_m2ua_conn *conn, |
| 181 | struct m2ua_msg *m2ua, |
| 182 | struct sctp_sndrcvinfo *info) |
| 183 | { |
| 184 | switch (m2ua->hdr.msg_type) { |
| 185 | case M2UA_ASPTM_ACTIV: |
| 186 | m2ua_handle_asptm_act(conn, m2ua, info); |
| 187 | break; |
| 188 | default: |
| 189 | LOGP(DINP, LOGL_ERROR, "Unhandled msg_type %d\n", |
| 190 | m2ua->hdr.msg_type); |
| 191 | break; |
| 192 | } |
| 193 | |
| 194 | return 0; |
| 195 | } |
| 196 | |
| 197 | static int m2ua_handle_state_req(struct sctp_m2ua_conn *conn, |
| 198 | struct m2ua_msg *m2ua, |
| 199 | struct sctp_sndrcvinfo *info) |
| 200 | { |
| 201 | struct m2ua_msg_part *ident, *state; |
| 202 | struct m2ua_msg *conf; |
| 203 | int interface = 0, req; |
| 204 | |
| 205 | state = m2ua_msg_find_tag(m2ua, M2UA_TAG_STATE_REQ); |
| 206 | if (!state || state->len != 4) { |
| 207 | LOGP(DINP, LOGL_ERROR, "Mandantory state request not present.\n"); |
| 208 | return -1; |
| 209 | } |
| 210 | |
| 211 | ident = m2ua_msg_find_tag(m2ua, MUA_TAG_IDENT_INT); |
| 212 | if (ident && ident->len == 4) { |
| 213 | memcpy(&interface, ident->dat, 4); |
| 214 | interface = ntohl(interface); |
| 215 | } |
| 216 | |
| 217 | memcpy(&req, state->dat, 4); |
| 218 | req = ntohl(req); |
| 219 | |
| 220 | switch (req) { |
| 221 | case M2UA_STATUS_EMER_SET: |
| 222 | conf = m2ua_msg_alloc(); |
| 223 | if (!conf) |
| 224 | return -1; |
| 225 | |
| 226 | conf->hdr.msg_class = M2UA_CLS_MAUP; |
| 227 | conf->hdr.msg_type = M2UA_MAUP_STATE_CON; |
| 228 | m2ua_msg_add_data(conf, MUA_TAG_IDENT_INT, 4, (uint8_t *) &interface); |
| 229 | m2ua_msg_add_data(conf, M2UA_TAG_STATE_REQ, 4, (uint8_t *) &req); |
| 230 | if (m2ua_conn_send(conn, conf, info) != 0) { |
| 231 | m2ua_msg_free(conf); |
| 232 | return -1; |
| 233 | } |
| 234 | m2ua_msg_free(conf); |
| 235 | break; |
| 236 | default: |
| 237 | LOGP(DINP, LOGL_ERROR, "Unknown STATE Request: %d\n", req); |
| 238 | break; |
| 239 | } |
| 240 | |
| 241 | return 0; |
| 242 | } |
| 243 | |
| 244 | static int m2ua_handle_est_req(struct sctp_m2ua_conn *conn, |
| 245 | struct m2ua_msg *m2ua, |
| 246 | struct sctp_sndrcvinfo *info) |
| 247 | { |
| 248 | struct m2ua_msg *conf; |
| 249 | |
| 250 | conf = m2ua_msg_alloc(); |
| 251 | if (!conf) |
| 252 | return -1; |
| 253 | |
| 254 | conf->hdr.msg_class = M2UA_CLS_MAUP; |
| 255 | conf->hdr.msg_type = M2UA_MAUP_EST_CON; |
| 256 | |
| 257 | if (m2ua_conn_send(conn, conf, info) != 0) { |
| 258 | m2ua_msg_free(conf); |
| 259 | return -1; |
| 260 | } |
| 261 | |
| 262 | conn->established = 1; |
| 263 | LOGP(DINP, LOGL_NOTICE, "M2UA/Link is established.\n"); |
| 264 | mtp_link_up(&conn->trans->base); |
| 265 | m2ua_msg_free(conf); |
| 266 | return 0; |
| 267 | } |
| 268 | |
| 269 | static int m2ua_handle_rel_req(struct sctp_m2ua_conn *conn, |
| 270 | struct m2ua_msg *m2ua, |
| 271 | struct sctp_sndrcvinfo *info) |
| 272 | { |
| 273 | struct m2ua_msg *conf; |
| 274 | |
| 275 | conf = m2ua_msg_alloc(); |
| 276 | if (!conf) |
| 277 | return -1; |
| 278 | |
| 279 | conf->hdr.msg_class = M2UA_CLS_MAUP; |
| 280 | conf->hdr.msg_type = M2UA_MAUP_REL_CON; |
| 281 | |
| 282 | if (m2ua_conn_send(conn, conf, info) != 0) { |
| 283 | m2ua_msg_free(conf); |
| 284 | return -1; |
| 285 | } |
| 286 | |
| 287 | conn->established = 0; |
| 288 | LOGP(DINP, LOGL_NOTICE, "M2UA/Link is released.\n"); |
| 289 | mtp_link_down(&conn->trans->base); |
| 290 | m2ua_msg_free(conf); |
| 291 | return 0; |
| 292 | } |
| 293 | |
| 294 | static int m2ua_handle_data(struct sctp_m2ua_conn *conn, |
| 295 | struct m2ua_msg *m2ua, |
| 296 | struct sctp_sndrcvinfo *info) |
| 297 | { |
| 298 | struct msgb *msg; |
| 299 | struct m2ua_msg_part *data; |
Holger Hans Peter Freyther | c17852e | 2011-01-17 22:23:24 +0100 | [diff] [blame] | 300 | struct mtp_link *link; |
Holger Hans Peter Freyther | d70a7e8 | 2011-01-17 14:13:29 +0100 | [diff] [blame] | 301 | |
| 302 | data = m2ua_msg_find_tag(m2ua, M2UA_TAG_DATA); |
| 303 | if (!data) { |
| 304 | LOGP(DINP, LOGL_ERROR, "No DATA in DATA message.\n"); |
| 305 | return -1; |
| 306 | } |
| 307 | |
| 308 | if (data->len > 2048) { |
| 309 | LOGP(DINP, LOGL_ERROR, "TOO much data for us to handle.\n"); |
| 310 | return -1; |
| 311 | } |
| 312 | |
| 313 | msg = msgb_alloc(2048, "m2ua-data"); |
| 314 | if (!msg) { |
| 315 | LOGP(DINP, LOGL_ERROR, "Failed to allocate storage.\n"); |
| 316 | return -1; |
| 317 | } |
| 318 | |
| 319 | msg->l2h = msgb_put(msg, data->len); |
| 320 | memcpy(msg->l2h, data->dat, data->len); |
Holger Hans Peter Freyther | c17852e | 2011-01-17 22:23:24 +0100 | [diff] [blame] | 321 | |
| 322 | link = &conn->trans->base; |
| 323 | if (link->pcap_fd >= 0) |
| 324 | mtp_pcap_write_msu(link->pcap_fd, msg->l2h, msgb_l2len(msg)); |
| 325 | mtp_link_set_data(link->the_link, msg); |
Holger Hans Peter Freyther | d70a7e8 | 2011-01-17 14:13:29 +0100 | [diff] [blame] | 326 | msgb_free(msg); |
| 327 | |
| 328 | return 0; |
| 329 | } |
| 330 | |
| 331 | static int m2ua_handle_maup(struct sctp_m2ua_conn *conn, |
| 332 | struct m2ua_msg *m2ua, |
| 333 | struct sctp_sndrcvinfo *info) |
| 334 | { |
| 335 | switch (m2ua->hdr.msg_type) { |
| 336 | case M2UA_MAUP_STATE_REQ: |
| 337 | m2ua_handle_state_req(conn, m2ua, info); |
| 338 | break; |
| 339 | case M2UA_MAUP_EST_REQ: |
| 340 | m2ua_handle_est_req(conn, m2ua, info); |
| 341 | break; |
| 342 | case M2UA_MAUP_REL_REQ: |
| 343 | m2ua_handle_rel_req(conn, m2ua, info); |
| 344 | break; |
| 345 | case M2UA_MAUP_DATA: |
| 346 | m2ua_handle_data(conn, m2ua, info); |
| 347 | break; |
| 348 | default: |
| 349 | LOGP(DINP, LOGL_ERROR, "Unhandled msg_type %d\n", |
| 350 | m2ua->hdr.msg_type); |
| 351 | break; |
| 352 | } |
| 353 | |
| 354 | return 0; |
| 355 | } |
| 356 | |
| 357 | static int m2ua_handle_mgmt(struct sctp_m2ua_conn *conn, |
| 358 | struct m2ua_msg *m2ua, struct sctp_sndrcvinfo *info) |
| 359 | { |
| 360 | switch (m2ua->hdr.msg_type) { |
| 361 | case M2UA_MGMT_ERROR: |
| 362 | LOGP(DINP, LOGL_ERROR, "We did something wrong. Error...\n"); |
| 363 | break; |
| 364 | case M2UA_MGMT_NTFY: |
| 365 | LOGP(DINP, LOGL_NOTICE, "There was a notiy.. but we should only send it.\n"); |
| 366 | break; |
| 367 | } |
| 368 | |
| 369 | return 0; |
| 370 | } |
| 371 | |
| 372 | static int m2ua_conn_handle(struct sctp_m2ua_conn *conn, |
| 373 | struct msgb *msg, struct sctp_sndrcvinfo *info) |
| 374 | { |
| 375 | struct m2ua_msg *m2ua; |
| 376 | m2ua = m2ua_from_msg(msg->len, msg->data); |
| 377 | if (!m2ua) { |
| 378 | LOGP(DINP, LOGL_ERROR, "Failed to parse the message.\n"); |
| 379 | return -1; |
| 380 | } |
| 381 | |
| 382 | switch (m2ua->hdr.msg_class) { |
| 383 | case M2UA_CLS_MGMT: |
| 384 | m2ua_handle_mgmt(conn, m2ua, info); |
| 385 | break; |
| 386 | case M2UA_CLS_ASPSM: |
| 387 | m2ua_handle_asp(conn, m2ua, info); |
| 388 | break; |
| 389 | case M2UA_CLS_ASPTM: |
| 390 | m2ua_handle_asptm(conn, m2ua, info); |
| 391 | break; |
| 392 | case M2UA_CLS_MAUP: |
| 393 | m2ua_handle_maup(conn, m2ua, info); |
| 394 | break; |
| 395 | default: |
| 396 | LOGP(DINP, LOGL_ERROR, "Unhandled msg_class %d\n", |
| 397 | m2ua->hdr.msg_class); |
| 398 | break; |
| 399 | } |
| 400 | |
| 401 | m2ua_msg_free(m2ua); |
| 402 | return 0; |
| 403 | } |
| 404 | |
| 405 | static int m2ua_conn_read(struct bsc_fd *fd) |
| 406 | { |
| 407 | struct sockaddr_in addr; |
| 408 | struct sctp_sndrcvinfo info; |
| 409 | socklen_t len = sizeof(addr); |
| 410 | struct msgb *msg; |
| 411 | int rc; |
| 412 | |
| 413 | msg = msgb_alloc(2048, "m2ua buffer"); |
| 414 | if (!msg) { |
| 415 | LOGP(DINP, LOGL_ERROR, "Failed to allocate buffer.\n"); |
| 416 | m2ua_conn_destroy(fd->data); |
| 417 | return -1; |
| 418 | } |
| 419 | |
| 420 | memset(&info, 0, sizeof(info)); |
| 421 | memset(&addr, 0, sizeof(addr)); |
| 422 | rc = sctp_recvmsg(fd->fd, msg->data, msg->data_len, |
| 423 | (struct sockaddr *) &addr, &len, &info, NULL); |
| 424 | if (rc < 0) { |
| 425 | LOGP(DINP, LOGL_ERROR, "Failed to read.\n"); |
| 426 | m2ua_conn_destroy(fd->data); |
| 427 | return -1; |
| 428 | } |
| 429 | |
| 430 | msgb_put(msg, rc); |
| 431 | LOGP(DINP, LOGL_NOTICE, "Read %d on stream: %d ssn: %d assoc: %d\n", |
| 432 | rc, info.sinfo_stream, info.sinfo_ssn, info.sinfo_assoc_id); |
| 433 | m2ua_conn_handle(fd->data, msg, &info); |
| 434 | msgb_free(msg); |
| 435 | return 0; |
| 436 | } |
| 437 | |
| 438 | static int sctp_m2ua_write(struct mtp_link *link, struct msgb *msg) |
| 439 | { |
| 440 | struct mtp_m2ua_link *trans; |
| 441 | struct sctp_m2ua_conn *conn = NULL, *tmp; |
| 442 | struct sctp_sndrcvinfo info; |
| 443 | struct m2ua_msg *m2ua; |
| 444 | uint32_t interface; |
| 445 | |
| 446 | trans = (struct mtp_m2ua_link *) link; |
| 447 | |
| 448 | if (llist_empty(&trans->conns)) |
| 449 | return -1; |
| 450 | |
| 451 | llist_for_each_entry(tmp, &trans->conns, entry) |
| 452 | if (tmp->established && tmp->asp_active && tmp->asp_up) { |
| 453 | conn = tmp; |
| 454 | break; |
| 455 | } |
| 456 | |
| 457 | if (!conn) { |
| 458 | LOGP(DINP, LOGL_ERROR, "No active ASP?\n"); |
| 459 | return -1; |
| 460 | } |
| 461 | |
| 462 | m2ua = m2ua_msg_alloc(); |
| 463 | if (!m2ua) |
| 464 | return -1; |
| 465 | |
Holger Hans Peter Freyther | c17852e | 2011-01-17 22:23:24 +0100 | [diff] [blame] | 466 | if (link->pcap_fd >= 0) |
| 467 | mtp_pcap_write_msu(link->pcap_fd, msg->data, msg->len); |
| 468 | |
Holger Hans Peter Freyther | d70a7e8 | 2011-01-17 14:13:29 +0100 | [diff] [blame] | 469 | m2ua->hdr.msg_class = M2UA_CLS_MAUP; |
| 470 | m2ua->hdr.msg_type = M2UA_MAUP_DATA; |
| 471 | |
| 472 | interface = htonl(0); |
| 473 | m2ua_msg_add_data(m2ua, MUA_TAG_IDENT_INT, 4, (uint8_t *) &interface); |
| 474 | m2ua_msg_add_data(m2ua, M2UA_TAG_DATA, msg->len, msg->data); |
| 475 | |
| 476 | memset(&info, 0, sizeof(info)); |
| 477 | info.sinfo_stream = 1; |
| 478 | info.sinfo_assoc_id = 1; |
| 479 | info.sinfo_ppid = htonl(2); |
| 480 | |
| 481 | m2ua_conn_send(conn, m2ua, &info); |
| 482 | m2ua_msg_free(m2ua); |
| 483 | return 0; |
| 484 | } |
| 485 | |
| 486 | static int m2ua_conn_write(struct bsc_fd *fd, struct msgb *msg) |
| 487 | { |
| 488 | int ret; |
| 489 | struct sctp_sndrcvinfo info; |
| 490 | memcpy(&info, msg->data, sizeof(info)); |
| 491 | |
| 492 | ret = sctp_send(fd->fd, msg->l2h, msgb_l2len(msg), |
| 493 | &info, 0); |
| 494 | |
| 495 | if (ret != msgb_l2len(msg)) |
| 496 | LOGP(DINP, LOGL_ERROR, "Failed to send %d.\n", ret); |
| 497 | |
| 498 | return 0; |
| 499 | } |
| 500 | |
| 501 | static int sctp_trans_accept(struct bsc_fd *fd, unsigned int what) |
| 502 | { |
| 503 | struct sctp_event_subscribe events; |
| 504 | struct mtp_m2ua_link *trans; |
| 505 | struct sctp_m2ua_conn *conn; |
| 506 | struct sockaddr_in addr; |
| 507 | socklen_t len; |
| 508 | int s; |
| 509 | |
| 510 | len = sizeof(addr); |
| 511 | s = accept(fd->fd, (struct sockaddr *) &addr, &len); |
| 512 | if (s < 0) { |
| 513 | LOGP(DINP, LOGL_ERROR, "Failed to accept.\n"); |
| 514 | return -1; |
| 515 | } |
| 516 | |
| 517 | trans = fd->data; |
| 518 | if (!trans->started) { |
| 519 | LOGP(DINP, LOGL_NOTICE, "The link is not started.\n"); |
| 520 | close(s); |
| 521 | return -1; |
| 522 | } |
| 523 | |
| 524 | LOGP(DINP, LOGL_NOTICE, "Got a new SCTP connection.\n"); |
| 525 | conn = talloc_zero(fd->data, struct sctp_m2ua_conn); |
| 526 | if (!conn) { |
| 527 | LOGP(DINP, LOGL_ERROR, "Failed to create.\n"); |
| 528 | close(s); |
| 529 | return -1; |
| 530 | } |
| 531 | |
| 532 | conn->trans = trans; |
| 533 | |
| 534 | write_queue_init(&conn->queue, 10); |
| 535 | conn->queue.bfd.fd = s; |
| 536 | conn->queue.bfd.data = conn; |
| 537 | conn->queue.bfd.when = BSC_FD_READ; |
| 538 | conn->queue.read_cb = m2ua_conn_read; |
| 539 | conn->queue.write_cb = m2ua_conn_write; |
| 540 | |
| 541 | if (bsc_register_fd(&conn->queue.bfd) != 0) { |
| 542 | LOGP(DINP, LOGL_ERROR, "Failed to register.\n"); |
| 543 | close(s); |
| 544 | talloc_free(conn); |
| 545 | return -1; |
| 546 | } |
| 547 | |
| 548 | memset(&events, 0, sizeof(events)); |
| 549 | events.sctp_data_io_event = 1; |
| 550 | setsockopt(s, SOL_SCTP, SCTP_EVENTS, &events, sizeof(events)); |
| 551 | |
| 552 | llist_add_tail(&conn->entry, &trans->conns); |
| 553 | return 0; |
| 554 | } |
| 555 | |
| 556 | static int sctp_m2ua_dummy(struct mtp_link *link) |
| 557 | { |
| 558 | return 0; |
| 559 | } |
| 560 | |
| 561 | static int sctp_m2ua_start(struct mtp_link *link) |
| 562 | { |
| 563 | struct mtp_m2ua_link *trans = (struct mtp_m2ua_link *) link; |
| 564 | |
| 565 | trans->started = 1; |
| 566 | return 0; |
| 567 | } |
| 568 | |
| 569 | static int sctp_m2ua_reset(struct mtp_link *link) |
| 570 | { |
| 571 | struct sctp_m2ua_conn *conn, *tmp; |
| 572 | struct mtp_m2ua_link *transp = (struct mtp_m2ua_link *) link; |
| 573 | |
| 574 | llist_for_each_entry_safe(conn, tmp, &transp->conns, entry) |
| 575 | m2ua_conn_destroy(conn); |
| 576 | |
| 577 | return 0; |
| 578 | } |
| 579 | |
| 580 | struct mtp_m2ua_link *sctp_m2ua_transp_create(const char *ip, int port) |
| 581 | { |
| 582 | int sctp; |
| 583 | struct sockaddr_in addr; |
| 584 | struct mtp_m2ua_link *trans; |
| 585 | |
| 586 | sctp = socket(PF_INET, SOCK_STREAM, IPPROTO_SCTP); |
| 587 | if (!sctp) { |
| 588 | LOGP(DINP, LOGL_ERROR, "Failed to create socket.\n"); |
| 589 | return NULL; |
| 590 | } |
| 591 | |
| 592 | memset(&addr, 0, sizeof(addr)); |
| 593 | addr.sin_family = AF_INET; |
| 594 | addr.sin_port = htons(port); |
| 595 | addr.sin_addr.s_addr = inet_addr(ip); |
| 596 | |
| 597 | if (bind(sctp, (struct sockaddr *) &addr, sizeof(addr)) != 0) { |
| 598 | LOGP(DINP, LOGL_ERROR, "Failed to bind.\n"); |
| 599 | close(sctp); |
| 600 | return NULL; |
| 601 | } |
| 602 | |
| 603 | if (listen(sctp, 1) != 0) { |
| 604 | LOGP(DINP, LOGL_ERROR, "Failed to listen.\n"); |
| 605 | close(sctp); |
| 606 | return NULL; |
| 607 | } |
| 608 | |
| 609 | int on = 1; |
| 610 | setsockopt(sctp, SOL_SCTP, 112, &on, sizeof(on)); |
| 611 | |
| 612 | trans = talloc_zero(NULL, struct mtp_m2ua_link); |
| 613 | if (!trans) { |
| 614 | LOGP(DINP, LOGL_ERROR, "Remove the talloc.\n"); |
| 615 | close(sctp); |
| 616 | return NULL; |
| 617 | } |
| 618 | |
| 619 | trans->base.shutdown = sctp_m2ua_dummy; |
| 620 | trans->base.clear_queue = sctp_m2ua_dummy; |
| 621 | trans->base.reset = sctp_m2ua_reset; |
| 622 | trans->base.start = sctp_m2ua_start; |
| 623 | trans->base.write = sctp_m2ua_write; |
| 624 | |
| 625 | trans->bsc.fd = sctp; |
| 626 | trans->bsc.data = trans; |
| 627 | trans->bsc.cb = sctp_trans_accept; |
| 628 | trans->bsc.when = BSC_FD_READ; |
| 629 | |
| 630 | if (bsc_register_fd(&trans->bsc) != 0) { |
| 631 | LOGP(DINP, LOGL_ERROR, "Failed to register the fd.\n"); |
| 632 | talloc_free(trans); |
| 633 | close(sctp); |
| 634 | return NULL; |
| 635 | } |
| 636 | |
| 637 | INIT_LLIST_HEAD(&trans->conns); |
| 638 | return trans; |
| 639 | } |
| 640 | |