sctp_eNB_task.c 22.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
/*******************************************************************************

  Eurecom OpenAirInterface
  Copyright(c) 1999 - 2013 Eurecom

  This program is free software; you can redistribute it and/or modify it
  under the terms and conditions of the GNU General Public License,
  version 2, as published by the Free Software Foundation.

  This program is distributed in the hope it will be useful, but WITHOUT
  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
  more details.

  You should have received a copy of the GNU General Public License along with
  this program; if not, write to the Free Software Foundation, Inc.,
  51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.

  The full GNU General Public License is included in this distribution in
  the file called "COPYING".

  Contact Information
  Openair Admin: openair_admin@eurecom.fr
  Openair Tech : openair_tech@eurecom.fr
  Forums       : http://forums.eurecom.fr/openairinterface
  Address      : EURECOM, Campus SophiaTech, 450 Route des Chappes
                 06410 Biot FRANCE

*******************************************************************************/

#include <pthread.h>
#include <stdint.h>
#include <errno.h>
#include <string.h>

#include <sys/types.h>
#include <sys/socket.h>
gauthier's avatar
gauthier committed
38 39 40 41 42
#include <netdb.h>
#include <arpa/inet.h>
#include <ifaddrs.h>
#include <sys/ioctl.h>
#include <net/if.h>
43 44 45 46 47 48 49 50 51 52 53

#include <netinet/in.h>
#include <netinet/sctp.h>

#include <arpa/inet.h>

#include "assertions.h"
#include "queue.h"

#include "intertask_interface.h"

54
#include "sctp_default_values.h"
55 56 57
#include "sctp_common.h"
#include "sctp_eNB_itti_messaging.h"

58 59 60 61 62 63
enum sctp_connection_type_e {
    SCTP_TYPE_CLIENT,
    SCTP_TYPE_SERVER,
    SCTP_TYPE_MAX
};

64 65 66
struct sctp_cnx_list_elm_s {
    STAILQ_ENTRY(sctp_cnx_list_elm_s) entries;

67 68 69 70
    /* Type of this association
     */
    enum sctp_connection_type_e connection_type;

71 72 73
    /* Socket descriptor of connection */
    int sd;

74 75 76
    /* local port used */
    uint16_t local_port;

77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
    /* IN/OUT streams */
    uint16_t in_streams;
    uint16_t out_streams;

    /* Configured PPID */
    uint16_t ppid;

    /* Association id */
    int32_t assoc_id;

    /* Nb of messages received on interface */
    uint32_t nb_messages;

    /* Task id of the task who asked for this connection */
    task_id_t task_id;

93 94 95
    /* Instance */
    instance_t instance;

96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
    /* Upper layer identifier */
    uint16_t cnx_id;
};

static STAILQ_HEAD(sctp_cnx_list_head, sctp_cnx_list_elm_s) sctp_cnx_list;
static uint16_t sctp_nb_cnx = 0;

struct sctp_cnx_list_elm_s *sctp_get_cnx(int32_t assoc_id, int sd)
{
    struct sctp_cnx_list_elm_s *elm;

    STAILQ_FOREACH(elm, &sctp_cnx_list, entries)
    {
        if (assoc_id != -1) {
            if (elm->assoc_id == assoc_id) {
                return elm;
            }
        } else {
            if (elm->sd == sd) {
                return elm;
            }
        }
    }

    return NULL;
}

void sctp_handle_new_association_req(
124
    const instance_t instance,
125 126 127
    const task_id_t requestor,
    const sctp_new_association_req_t * const sctp_new_association_req_p)
{
gauthier's avatar
gauthier committed
128 129
    int                           sd;
    int32_t                       assoc_id;
130

gauthier's avatar
gauthier committed
131
    struct sctp_event_subscribe   events;
132

gauthier's avatar
gauthier committed
133 134
    struct sctp_cnx_list_elm_s   *sctp_cnx = NULL;
    enum sctp_connection_type_e   connection_type = SCTP_TYPE_CLIENT;
135

gauthier's avatar
gauthier committed
136 137 138 139 140
    struct ifreq                  ifr;
    struct ifaddrs               *ifaddr, *ifa;
    int                           family, s;
    struct in_addr                in;
    struct in6_addr               in6;
141 142 143 144 145 146 147 148 149 150 151 152 153 154
    /* Prepare a new SCTP association as requested by upper layer and try to connect
     * to remote host.
     */
    DevAssert(sctp_new_association_req_p != NULL);

    /* Create new socket with IPv6 affinity */
    if ((sd = socket(AF_INET6, SOCK_STREAM, IPPROTO_SCTP)) < 0) {
        SCTP_ERROR("Socket creation failed: %s\n", strerror(errno));
        return;
    }

    /* Add the socket to list of fd monitored by ITTI */
    itti_subscribe_event_fd(TASK_SCTP, sd);

155 156 157
    if (sctp_set_init_opt(sd, SCTP_IN_STREAMS, SCTP_OUT_STREAMS,
        SCTP_MAX_ATTEMPTS, SCTP_TIMEOUT) != 0)
    {
158 159
        SCTP_ERROR("Setsockopt IPPROTO_SCTP_INITMSG failed: %s\n",
                   strerror(errno));
160
        itti_unsubscribe_event_fd(TASK_SCTP, sd);
161
        close(sd);
162 163 164 165 166 167 168 169 170
        return;
    }

    /* Subscribe to all events */
    memset((void *)&events, 1, sizeof(struct sctp_event_subscribe));
    if (setsockopt(sd, IPPROTO_SCTP, SCTP_EVENTS, &events,
                   sizeof(struct sctp_event_subscribe)) < 0) {
        SCTP_ERROR("Setsockopt IPPROTO_SCTP_EVENTS failed: %s\n",
                   strerror(errno));
171 172 173 174
        close(sd);
        return;
    }

gauthier's avatar
gauthier committed
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
    // Bind to device ... or we could bind to address also
    if (getifaddrs(&ifaddr) == -1) {
        SCTP_ERROR("getifaddrs failed: %s\n", strerror(errno));
        close(sd);
    }
    /* Walk through linked list, maintaining head pointer so we
       can free list later */
    for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
        if (ifa->ifa_addr == NULL)
            continue;

        family = ifa->ifa_addr->sa_family;

        /* For an AF_INET* interface address, display the address */
        if (sctp_new_association_req_p->local_address.ipv4 && family == AF_INET) {
            // compare address
            s = inet_aton(sctp_new_association_req_p->local_address.ipv4_address,
                    &in);
            if (s > 0 ) {
                if (((struct sockaddr_in*)ifa->ifa_addr)->sin_addr.s_addr == in.s_addr) {
                    memset(&ifr, 0, sizeof(ifr));
                    snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), ifa->ifa_name);
                    if (setsockopt(sd, SOL_SOCKET, SO_BINDTODEVICE, (void *)&ifr, sizeof(ifr)) < 0) {
                        SCTP_ERROR("Setsockopt SOL_SOCKET failed: %s\n",
                                   strerror(errno));
                    } else {
                        SCTP_DEBUG("Setsockopt SOL_SOCKET socket bound to : %s\n",
                                ifa->ifa_name);
                    }
                    break;
                }
            }
        } else if (sctp_new_association_req_p->local_address.ipv6 && family == AF_INET6) {
            // compare address
            s = inet_pton(AF_INET6,
                    sctp_new_association_req_p->local_address.ipv6_address,
                    &in6);
            if (s == 1 ) {
                if (memcmp(&((struct sockaddr_in6*)ifa->ifa_addr)->sin6_addr,
                        &in6, sizeof(in6)) == 0) {
                    memset(&ifr, 0, sizeof(ifr));
                    snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), ifa->ifa_name);
                    if (setsockopt(sd, SOL_SOCKET, SO_BINDTODEVICE, (void *)&ifr, sizeof(ifr)) < 0) {
                        SCTP_ERROR("Setsockopt SOL_SOCKET failed: %s\n",
                                   strerror(errno));
                    } else {
                        SCTP_DEBUG("Setsockopt SOL_SOCKET socket bound to : %s\n",
                                ifa->ifa_name);
                    }
                    break;
                }
            }
        }
    }

    freeifaddrs(ifaddr);

232 233 234 235 236
    /* Mark the socket as non-blocking */
    if (fcntl(sd, F_SETFL, O_NONBLOCK) < 0) {
        SCTP_ERROR("fcntl F_SETFL O_NONBLOCK failed: %s\n",
                   strerror(errno));
        close(sd);
237 238 239 240 241 242 243
        return;
    }

    /* SOCK_STREAM socket type requires an explicit connect to the remote host
     * address and port.
     * Only use IPv4 for the first connection attempt
     */
244 245
    if ((sctp_new_association_req_p->remote_address.ipv6 != 0) ||
        (sctp_new_association_req_p->remote_address.ipv4 != 0))
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
    {
        uint8_t address_index = 0;
        uint8_t used_address  = sctp_new_association_req_p->remote_address.ipv6 +
                                sctp_new_association_req_p->remote_address.ipv4;
        struct sockaddr_in addr[used_address];

        memset(addr, 0, used_address * sizeof(struct sockaddr_in));

        if (sctp_new_association_req_p->remote_address.ipv6 == 1) {
            if (inet_pton(AF_INET6, sctp_new_association_req_p->remote_address.ipv6_address,
                &addr[address_index].sin_addr.s_addr) != 1)
            {
                SCTP_ERROR("Failed to convert ipv6 address %*s to network type\n",
                           strlen(sctp_new_association_req_p->remote_address.ipv6_address),
                           sctp_new_association_req_p->remote_address.ipv6_address);
261
                close(sd);
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
                return;
            }

            SCTP_DEBUG("Converted ipv6 address %*s to network type\n",
                       strlen(sctp_new_association_req_p->remote_address.ipv6_address),
                       sctp_new_association_req_p->remote_address.ipv6_address);

            addr[address_index].sin_family = AF_INET6;
            addr[address_index].sin_port   = htons(sctp_new_association_req_p->port);
            address_index++;
        }
        if (sctp_new_association_req_p->remote_address.ipv4 == 1) {
            if (inet_pton(AF_INET, sctp_new_association_req_p->remote_address.ipv4_address,
                &addr[address_index].sin_addr.s_addr) != 1)
            {
                SCTP_ERROR("Failed to convert ipv4 address %*s to network type\n",
                           strlen(sctp_new_association_req_p->remote_address.ipv4_address),
                           sctp_new_association_req_p->remote_address.ipv4_address);
280
                close(sd);
281 282 283 284 285 286 287 288 289 290 291 292 293
                return;
            }

            SCTP_DEBUG("Converted ipv4 address %*s to network type\n",
                       strlen(sctp_new_association_req_p->remote_address.ipv4_address),
                       sctp_new_association_req_p->remote_address.ipv4_address);

            addr[address_index].sin_family = AF_INET;
            addr[address_index].sin_port   = htons(sctp_new_association_req_p->port);
            address_index++;
        }

        /* Connect to remote host and port */
294 295 296 297 298 299 300 301
        if (sctp_connectx(sd, (struct sockaddr *)addr, used_address, &assoc_id) < 0)
        {
            /* sctp_connectx on non-blocking socket return EINPROGRESS */
            if (errno != EINPROGRESS) {
                SCTP_ERROR("Connect failed: %s\n", strerror(errno));
                sctp_itti_send_association_resp(
                    requestor, instance, -1, sctp_new_association_req_p->ulp_cnx_id,
                    SCTP_STATE_UNREACHABLE, 0, 0);
302 303
                /* Add the socket to list of fd monitored by ITTI */
                itti_unsubscribe_event_fd(TASK_SCTP, sd);
304 305
                close(sd);
                return;
306
            } else {
307
                SCTP_DEBUG("connectx %d in progress...\n", assoc_id);
308
            }
309
        }
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
    } else {
        /* No remote address provided -> only bind the socket for now.
         * Connection will be accepted in the main event loop
         */
        struct sockaddr_in6 addr6;

        connection_type = SCTP_TYPE_SERVER;

        /* For now bind to any interface */
        addr6.sin6_family = AF_INET6;
        addr6.sin6_addr = in6addr_any;
        addr6.sin6_port = htons(sctp_new_association_req_p->port);

        if (bind(sd, (struct sockaddr*)&addr6, sizeof(addr6)) < 0) {
            SCTP_ERROR("Failed to bind the socket %d to address any (v4/v6): %s\n",
                       strerror(errno));
            close(sd);
            return;
        }
329 330
    }

331
    sctp_cnx = calloc(1, sizeof(*sctp_cnx));
332

333 334
    sctp_cnx->connection_type = connection_type;

335 336 337 338 339
    sctp_cnx->sd       = sd;
    sctp_cnx->task_id  = requestor;
    sctp_cnx->cnx_id   = sctp_new_association_req_p->ulp_cnx_id;
    sctp_cnx->ppid     = sctp_new_association_req_p->ppid;
    sctp_cnx->instance = instance;
340
    sctp_cnx->assoc_id = assoc_id;
341

342 343 344
    /* Insert new element at end of list */
    STAILQ_INSERT_TAIL(&sctp_cnx_list, sctp_cnx, entries);
    sctp_nb_cnx++;
345

346 347
    SCTP_DEBUG("Inserted new descriptor for sd %d in list, nb elements %u, assoc_id %d\n",
                sd, sctp_nb_cnx, assoc_id);
348 349
}

350
void sctp_send_data(instance_t instance, task_id_t task_id, sctp_data_req_t *sctp_data_req_p)
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
{
    struct sctp_cnx_list_elm_s *sctp_cnx = NULL;

    DevAssert(sctp_data_req_p != NULL);

    sctp_cnx = sctp_get_cnx(sctp_data_req_p->assoc_id, 0);

    if (sctp_cnx == NULL) {
        SCTP_ERROR("Failed to find SCTP description for assoc_id %d\n",
                   sctp_data_req_p->assoc_id);
        /* TODO: notify upper layer */
        return;
    }

    if (sctp_data_req_p->stream >= sctp_cnx->out_streams) {
        SCTP_ERROR("Requested stream (%u) >= nb out streams\n",
                   sctp_data_req_p->stream, sctp_cnx->out_streams);
        return;
    }

    /* Send message on specified stream of the sd association
     * NOTE: PPID should be defined in network order
     */
    if (sctp_sendmsg(sctp_cnx->sd, sctp_data_req_p->buffer,
                     sctp_data_req_p->buffer_length, NULL, 0,
                     htonl(sctp_cnx->ppid), 0, sctp_data_req_p->stream, 0, 0) < 0) {
        SCTP_ERROR("Sctp_sendmsg failed: %s\n", strerror(errno));
        /* TODO: notify upper lkayer */
        return;
    }

    SCTP_DEBUG("Successfully sent %u bytes on stream %d for assoc_id %u\n",
               sctp_data_req_p->buffer_length, sctp_data_req_p->stream,
               sctp_cnx->assoc_id);
}

387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
static
inline void sctp_eNB_accept_associations(struct sctp_cnx_list_elm_s *sctp_cnx)
{
    int client_sd;

    struct sockaddr saddr;
    socklen_t       saddr_size;

    DevAssert(sctp_cnx != NULL);

    saddr_size = sizeof(saddr);

    /* There is a new client connecting. Accept it...
     */
    if ((client_sd = accept(sctp_cnx->sd, &saddr, &saddr_size)) < 0) {
        SCTP_ERROR("[%d] accept failed: %s:%d\n", sctp_cnx->sd, strerror(errno), errno);
    } else {
        struct sctp_cnx_list_elm_s *new_cnx;
        uint16_t port;

        /* This is an ipv6 socket */
        port = ((struct sockaddr_in6*)&saddr)->sin6_port;

        /* Contrary to BSD, client socket does not inherit O_NONBLOCK option */
        if (fcntl(client_sd, F_SETFL, O_NONBLOCK) < 0) {
            SCTP_ERROR("fcntl F_SETFL O_NONBLOCK failed: %s\n",
                       strerror(errno));
            close(client_sd);
            return;
        }

        new_cnx = calloc(1, sizeof(*sctp_cnx));

        DevAssert(new_cnx != NULL);

        new_cnx->connection_type = SCTP_TYPE_CLIENT;

        new_cnx->sd         = client_sd;
        new_cnx->task_id    = sctp_cnx->task_id;
        new_cnx->cnx_id     = 0;
        new_cnx->ppid       = sctp_cnx->ppid;
        new_cnx->instance   = sctp_cnx->instance;
        new_cnx->local_port = sctp_cnx->local_port;

        if (sctp_get_sockinfo(client_sd, &new_cnx->in_streams, &new_cnx->out_streams,
            &new_cnx->assoc_id) != 0)
        {
            SCTP_ERROR("sctp_get_sockinfo failed\n");
            close(client_sd);
            free(new_cnx);
            return;
        }

        /* Insert new element at end of list */
        STAILQ_INSERT_TAIL(&sctp_cnx_list, new_cnx, entries);
        sctp_nb_cnx++;

        /* Add the socket to list of fd monitored by ITTI */
        itti_subscribe_event_fd(TASK_SCTP, client_sd);

        sctp_itti_send_association_ind(new_cnx->task_id, new_cnx->instance,
                                       new_cnx->assoc_id, port,
                                       new_cnx->out_streams, new_cnx->in_streams);
    }
}

453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
static
inline void sctp_eNB_read_from_socket(struct sctp_cnx_list_elm_s *sctp_cnx)
{
    int flags = 0, n;
    socklen_t from_len;
    struct sctp_sndrcvinfo sinfo;

    struct sockaddr_in addr;
    uint8_t buffer[SCTP_RECV_BUFFER_SIZE];

    DevAssert(sctp_cnx != NULL);

    memset((void *)&addr, 0, sizeof(struct sockaddr_in));
    from_len = (socklen_t)sizeof(struct sockaddr_in);
    memset((void *)&sinfo, 0, sizeof(struct sctp_sndrcvinfo));

    n = sctp_recvmsg(sctp_cnx->sd, (void *)buffer, SCTP_RECV_BUFFER_SIZE,
                     (struct sockaddr *)&addr, &from_len,
                     &sinfo, &flags);
    if (n < 0) {
473 474 475
        if (errno == ENOTCONN) {
            itti_unsubscribe_event_fd(TASK_SCTP, sctp_cnx->sd);

476 477
            SCTP_DEBUG("Received not connected for sd %d\n", sctp_cnx->sd);

478 479 480 481 482
            sctp_itti_send_association_resp(
                sctp_cnx->task_id, sctp_cnx->instance, -1,
                sctp_cnx->cnx_id, SCTP_STATE_UNREACHABLE, 0, 0);

            close(sctp_cnx->sd);
483
            STAILQ_REMOVE(&sctp_cnx_list, sctp_cnx, sctp_cnx_list_elm_s, entries);
484 485 486 487 488 489 490 491 492
            sctp_nb_cnx--;
            free(sctp_cnx);
        } else {
            SCTP_DEBUG("An error occured during read\n");
            SCTP_ERROR("sctp_recvmsg: %s:%d\n", strerror(errno), errno);
        }
        return;
    } else if (n == 0) {
        SCTP_DEBUG("return of sctp_recvmsg is 0...\n");
493 494 495 496 497 498
        return;
    }
    if (flags & MSG_NOTIFICATION) {
        union sctp_notification *snp;
        snp = (union sctp_notification *)buffer;

499 500 501
        SCTP_DEBUG("Received notification for sd %d, type %u\n",
                   sctp_cnx->sd, snp->sn_header.sn_type);

502 503 504
        /* Client deconnection */
        if (SCTP_SHUTDOWN_EVENT == snp->sn_header.sn_type) {
            itti_unsubscribe_event_fd(TASK_SCTP, sctp_cnx->sd);
505 506 507 508 509 510 511 512 513 514 515 516

            close(sctp_cnx->sd);

            sctp_itti_send_association_resp(
                sctp_cnx->task_id, sctp_cnx->instance, sctp_cnx->assoc_id,
                sctp_cnx->cnx_id, SCTP_STATE_SHUTDOWN,
                0, 0);

            STAILQ_REMOVE(&sctp_cnx_list, sctp_cnx, sctp_cnx_list_elm_s, entries);
            sctp_nb_cnx--;

            free(sctp_cnx);
517 518 519 520 521 522 523 524 525 526 527
        }
        /* Association has changed. */
        else if (SCTP_ASSOC_CHANGE == snp->sn_header.sn_type) {
            struct sctp_assoc_change *sctp_assoc_changed;
            sctp_assoc_changed = &snp->sn_assoc_change;

            SCTP_DEBUG("Client association changed: %d\n", sctp_assoc_changed->sac_state);

            /* New physical association requested by a peer */
            switch (sctp_assoc_changed->sac_state) {
                case SCTP_COMM_UP: {
528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543
                    if (sctp_get_peeraddresses(sctp_cnx->sd, NULL, NULL) != 0)
                    {
                        /* TODO Failure -> notify upper layer */
                    } else {
                        sctp_get_sockinfo(sctp_cnx->sd, &sctp_cnx->in_streams,
                                          &sctp_cnx->out_streams, &sctp_cnx->assoc_id);
                    }

                    SCTP_DEBUG("Comm up notified for sd %d, assigned assoc_id %d\n",
                               sctp_cnx->sd, sctp_cnx->assoc_id);

                    sctp_itti_send_association_resp(
                        sctp_cnx->task_id, sctp_cnx->instance, sctp_cnx->assoc_id,
                        sctp_cnx->cnx_id, SCTP_STATE_ESTABLISHED,
                        sctp_cnx->out_streams, sctp_cnx->in_streams);

544 545 546 547 548 549 550 551
                } break;
                default:
                    break;
            }
        }
    } else {
        sctp_cnx->nb_messages++;

nikaeinn's avatar
nikaeinn committed
552
        if (sinfo.sinfo_ppid != sctp_cnx->ppid) {
553 554 555 556
            /* Mismatch in Payload Protocol Identifier,
             * may be we received unsollicited traffic from stack other than S1AP.
             */
            SCTP_ERROR("Received data from peer with unsollicited PPID %d, expecting %d\n",
nikaeinn's avatar
nikaeinn committed
557
                sinfo.sinfo_ppid, sctp_cnx->ppid);
558 559 560 561
        }

        SCTP_DEBUG("[%d][%d] Msg of length %d received from port %u, on stream %d, PPID %d\n",
                   sinfo.sinfo_assoc_id, sctp_cnx->sd, n, ntohs(addr.sin_port),
562
                   sinfo.sinfo_stream, ntohl(sinfo.sinfo_ppid));
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584

        sctp_itti_send_new_message_ind(sctp_cnx->task_id,
                                       sinfo.sinfo_assoc_id,
                                       buffer, n, sinfo.sinfo_stream);
    }
}

void sctp_eNB_flush_sockets(struct epoll_event *events, int nb_events)
{
    int i;
    struct sctp_cnx_list_elm_s *sctp_cnx = NULL;

    if (events == NULL) {
        return;
    }

    for (i = 0; i < nb_events; i++) {
        sctp_cnx = sctp_get_cnx(-1, events[i].data.fd);
        if (sctp_cnx == NULL) {
            continue;
        }
        SCTP_DEBUG("Found data for descriptor %d\n", events[i].data.fd);
585 586 587 588 589
        if (sctp_cnx->connection_type == SCTP_TYPE_CLIENT) {
            sctp_eNB_read_from_socket(sctp_cnx);
        } else {
            sctp_eNB_accept_associations(sctp_cnx);
        }
590 591 592 593 594 595 596 597
    }
}

void *sctp_eNB_task(void *arg)
{
    int                 nb_events;
    struct epoll_event *events;
    MessageDef         *received_msg = NULL;
598
    int                 result;
599 600 601 602 603 604 605 606 607 608 609 610

    SCTP_DEBUG("Starting SCTP layer\n");

    STAILQ_INIT(&sctp_cnx_list);

    itti_mark_task_ready(TASK_SCTP);

    while (1) {
        itti_receive_msg(TASK_SCTP, &received_msg);

        /* Check if there is a packet to handle */
        if (received_msg != NULL) {
611 612
            switch (ITTI_MSG_ID(received_msg))
            {
613 614 615 616
                case TERMINATE_MESSAGE:
                    itti_exit_task();
                    break;
                case SCTP_NEW_ASSOCIATION_REQ: {
617
                    sctp_handle_new_association_req(ITTI_MESSAGE_GET_INSTANCE(received_msg),
Cedric Roux's avatar
Cedric Roux committed
618
                                                    ITTI_MSG_ORIGIN_ID(received_msg),
619
                                                    &received_msg->ittiMsg.sctp_new_association_req);
620 621
                } break;
                case SCTP_DATA_REQ: {
622
                    sctp_send_data(ITTI_MESSAGE_GET_INSTANCE(received_msg),
Cedric Roux's avatar
Cedric Roux committed
623
                                   ITTI_MSG_ORIGIN_ID(received_msg),
624
                                   &received_msg->ittiMsg.sctp_data_req);
625 626
                } break;
                default:
627 628
                    SCTP_ERROR("Received unhandled message %d:%s\n",
                               ITTI_MSG_ID(received_msg), ITTI_MSG_NAME(received_msg));
629 630
                    break;
            }
631 632
            result = itti_free(ITTI_MSG_ORIGIN_ID(received_msg), received_msg);
            AssertFatal (result == EXIT_SUCCESS, "Failed to free memory (%d)!\n", result);
633
            received_msg = NULL;
634 635 636 637 638 639 640 641
        }

        nb_events = itti_get_events(TASK_SCTP, &events);
        /* Now handle notifications for other sockets */
        sctp_eNB_flush_sockets(events, nb_events);
    }
    return NULL;
}