linux/net/sctp/input.c
<<
>>
Prefs
   1/* SCTP kernel implementation
   2 * Copyright (c) 1999-2000 Cisco, Inc.
   3 * Copyright (c) 1999-2001 Motorola, Inc.
   4 * Copyright (c) 2001-2003 International Business Machines, Corp.
   5 * Copyright (c) 2001 Intel Corp.
   6 * Copyright (c) 2001 Nokia, Inc.
   7 * Copyright (c) 2001 La Monte H.P. Yarroll
   8 *
   9 * This file is part of the SCTP kernel implementation
  10 *
  11 * These functions handle all input from the IP layer into SCTP.
  12 *
  13 * This SCTP implementation is free software;
  14 * you can redistribute it and/or modify it under the terms of
  15 * the GNU General Public License as published by
  16 * the Free Software Foundation; either version 2, or (at your option)
  17 * any later version.
  18 *
  19 * This SCTP implementation is distributed in the hope that it
  20 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
  21 *                 ************************
  22 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  23 * See the GNU General Public License for more details.
  24 *
  25 * You should have received a copy of the GNU General Public License
  26 * along with GNU CC; see the file COPYING.  If not, see
  27 * <http://www.gnu.org/licenses/>.
  28 *
  29 * Please send any bug reports or fixes you make to the
  30 * email address(es):
  31 *    lksctp developers <linux-sctp@vger.kernel.org>
  32 *
  33 * Written or modified by:
  34 *    La Monte H.P. Yarroll <piggy@acm.org>
  35 *    Karl Knutson <karl@athena.chicago.il.us>
  36 *    Xingang Guo <xingang.guo@intel.com>
  37 *    Jon Grimm <jgrimm@us.ibm.com>
  38 *    Hui Huang <hui.huang@nokia.com>
  39 *    Daisy Chang <daisyc@us.ibm.com>
  40 *    Sridhar Samudrala <sri@us.ibm.com>
  41 *    Ardelle Fan <ardelle.fan@intel.com>
  42 */
  43
  44#include <linux/types.h>
  45#include <linux/list.h> /* For struct list_head */
  46#include <linux/socket.h>
  47#include <linux/ip.h>
  48#include <linux/time.h> /* For struct timeval */
  49#include <linux/slab.h>
  50#include <net/ip.h>
  51#include <net/icmp.h>
  52#include <net/snmp.h>
  53#include <net/sock.h>
  54#include <net/xfrm.h>
  55#include <net/sctp/sctp.h>
  56#include <net/sctp/sm.h>
  57#include <net/sctp/checksum.h>
  58#include <net/net_namespace.h>
  59#include <linux/rhashtable.h>
  60#include <net/sock_reuseport.h>
  61
  62/* Forward declarations for internal helpers. */
  63static int sctp_rcv_ootb(struct sk_buff *);
  64static struct sctp_association *__sctp_rcv_lookup(struct net *net,
  65                                      struct sk_buff *skb,
  66                                      const union sctp_addr *paddr,
  67                                      const union sctp_addr *laddr,
  68                                      struct sctp_transport **transportp);
  69static struct sctp_endpoint *__sctp_rcv_lookup_endpoint(
  70                                        struct net *net, struct sk_buff *skb,
  71                                        const union sctp_addr *laddr,
  72                                        const union sctp_addr *daddr);
  73static struct sctp_association *__sctp_lookup_association(
  74                                        struct net *net,
  75                                        const union sctp_addr *local,
  76                                        const union sctp_addr *peer,
  77                                        struct sctp_transport **pt);
  78
  79static int sctp_add_backlog(struct sock *sk, struct sk_buff *skb);
  80
  81
  82/* Calculate the SCTP checksum of an SCTP packet.  */
  83static inline int sctp_rcv_checksum(struct net *net, struct sk_buff *skb)
  84{
  85        struct sctphdr *sh = sctp_hdr(skb);
  86        __le32 cmp = sh->checksum;
  87        __le32 val = sctp_compute_cksum(skb, 0);
  88
  89        if (val != cmp) {
  90                /* CRC failure, dump it. */
  91                __SCTP_INC_STATS(net, SCTP_MIB_CHECKSUMERRORS);
  92                return -1;
  93        }
  94        return 0;
  95}
  96
  97/*
  98 * This is the routine which IP calls when receiving an SCTP packet.
  99 */
 100int sctp_rcv(struct sk_buff *skb)
 101{
 102        struct sock *sk;
 103        struct sctp_association *asoc;
 104        struct sctp_endpoint *ep = NULL;
 105        struct sctp_ep_common *rcvr;
 106        struct sctp_transport *transport = NULL;
 107        struct sctp_chunk *chunk;
 108        union sctp_addr src;
 109        union sctp_addr dest;
 110        int family;
 111        struct sctp_af *af;
 112        struct net *net = dev_net(skb->dev);
 113        bool is_gso = skb_is_gso(skb) && skb_is_gso_sctp(skb);
 114
 115        if (skb->pkt_type != PACKET_HOST)
 116                goto discard_it;
 117
 118        __SCTP_INC_STATS(net, SCTP_MIB_INSCTPPACKS);
 119
 120        /* If packet is too small to contain a single chunk, let's not
 121         * waste time on it anymore.
 122         */
 123        if (skb->len < sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr) +
 124                       skb_transport_offset(skb))
 125                goto discard_it;
 126
 127        /* If the packet is fragmented and we need to do crc checking,
 128         * it's better to just linearize it otherwise crc computing
 129         * takes longer.
 130         */
 131        if ((!is_gso && skb_linearize(skb)) ||
 132            !pskb_may_pull(skb, sizeof(struct sctphdr)))
 133                goto discard_it;
 134
 135        /* Pull up the IP header. */
 136        __skb_pull(skb, skb_transport_offset(skb));
 137
 138        skb->csum_valid = 0; /* Previous value not applicable */
 139        if (skb_csum_unnecessary(skb))
 140                __skb_decr_checksum_unnecessary(skb);
 141        else if (!sctp_checksum_disable &&
 142                 !is_gso &&
 143                 sctp_rcv_checksum(net, skb) < 0)
 144                goto discard_it;
 145        skb->csum_valid = 1;
 146
 147        __skb_pull(skb, sizeof(struct sctphdr));
 148
 149        family = ipver2af(ip_hdr(skb)->version);
 150        af = sctp_get_af_specific(family);
 151        if (unlikely(!af))
 152                goto discard_it;
 153        SCTP_INPUT_CB(skb)->af = af;
 154
 155        /* Initialize local addresses for lookups. */
 156        af->from_skb(&src, skb, 1);
 157        af->from_skb(&dest, skb, 0);
 158
 159        /* If the packet is to or from a non-unicast address,
 160         * silently discard the packet.
 161         *
 162         * This is not clearly defined in the RFC except in section
 163         * 8.4 - OOTB handling.  However, based on the book "Stream Control
 164         * Transmission Protocol" 2.1, "It is important to note that the
 165         * IP address of an SCTP transport address must be a routable
 166         * unicast address.  In other words, IP multicast addresses and
 167         * IP broadcast addresses cannot be used in an SCTP transport
 168         * address."
 169         */
 170        if (!af->addr_valid(&src, NULL, skb) ||
 171            !af->addr_valid(&dest, NULL, skb))
 172                goto discard_it;
 173
 174        asoc = __sctp_rcv_lookup(net, skb, &src, &dest, &transport);
 175
 176        if (!asoc)
 177                ep = __sctp_rcv_lookup_endpoint(net, skb, &dest, &src);
 178
 179        /* Retrieve the common input handling substructure. */
 180        rcvr = asoc ? &asoc->base : &ep->base;
 181        sk = rcvr->sk;
 182
 183        /*
 184         * If a frame arrives on an interface and the receiving socket is
 185         * bound to another interface, via SO_BINDTODEVICE, treat it as OOTB
 186         */
 187        if (sk->sk_bound_dev_if && (sk->sk_bound_dev_if != af->skb_iif(skb))) {
 188                if (transport) {
 189                        sctp_transport_put(transport);
 190                        asoc = NULL;
 191                        transport = NULL;
 192                } else {
 193                        sctp_endpoint_put(ep);
 194                        ep = NULL;
 195                }
 196                sk = net->sctp.ctl_sock;
 197                ep = sctp_sk(sk)->ep;
 198                sctp_endpoint_hold(ep);
 199                rcvr = &ep->base;
 200        }
 201
 202        /*
 203         * RFC 2960, 8.4 - Handle "Out of the blue" Packets.
 204         * An SCTP packet is called an "out of the blue" (OOTB)
 205         * packet if it is correctly formed, i.e., passed the
 206         * receiver's checksum check, but the receiver is not
 207         * able to identify the association to which this
 208         * packet belongs.
 209         */
 210        if (!asoc) {
 211                if (sctp_rcv_ootb(skb)) {
 212                        __SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
 213                        goto discard_release;
 214                }
 215        }
 216
 217        if (!xfrm_policy_check(sk, XFRM_POLICY_IN, skb, family))
 218                goto discard_release;
 219        nf_reset(skb);
 220
 221        if (sk_filter(sk, skb))
 222                goto discard_release;
 223
 224        /* Create an SCTP packet structure. */
 225        chunk = sctp_chunkify(skb, asoc, sk, GFP_ATOMIC);
 226        if (!chunk)
 227                goto discard_release;
 228        SCTP_INPUT_CB(skb)->chunk = chunk;
 229
 230        /* Remember what endpoint is to handle this packet. */
 231        chunk->rcvr = rcvr;
 232
 233        /* Remember the SCTP header. */
 234        chunk->sctp_hdr = sctp_hdr(skb);
 235
 236        /* Set the source and destination addresses of the incoming chunk.  */
 237        sctp_init_addrs(chunk, &src, &dest);
 238
 239        /* Remember where we came from.  */
 240        chunk->transport = transport;
 241
 242        /* Acquire access to the sock lock. Note: We are safe from other
 243         * bottom halves on this lock, but a user may be in the lock too,
 244         * so check if it is busy.
 245         */
 246        bh_lock_sock(sk);
 247
 248        if (sk != rcvr->sk) {
 249                /* Our cached sk is different from the rcvr->sk.  This is
 250                 * because migrate()/accept() may have moved the association
 251                 * to a new socket and released all the sockets.  So now we
 252                 * are holding a lock on the old socket while the user may
 253                 * be doing something with the new socket.  Switch our veiw
 254                 * of the current sk.
 255                 */
 256                bh_unlock_sock(sk);
 257                sk = rcvr->sk;
 258                bh_lock_sock(sk);
 259        }
 260
 261        if (sock_owned_by_user(sk) || !sctp_newsk_ready(sk)) {
 262                if (sctp_add_backlog(sk, skb)) {
 263                        bh_unlock_sock(sk);
 264                        sctp_chunk_free(chunk);
 265                        skb = NULL; /* sctp_chunk_free already freed the skb */
 266                        goto discard_release;
 267                }
 268                __SCTP_INC_STATS(net, SCTP_MIB_IN_PKT_BACKLOG);
 269        } else {
 270                __SCTP_INC_STATS(net, SCTP_MIB_IN_PKT_SOFTIRQ);
 271                sctp_inq_push(&chunk->rcvr->inqueue, chunk);
 272        }
 273
 274        bh_unlock_sock(sk);
 275
 276        /* Release the asoc/ep ref we took in the lookup calls. */
 277        if (transport)
 278                sctp_transport_put(transport);
 279        else
 280                sctp_endpoint_put(ep);
 281
 282        return 0;
 283
 284discard_it:
 285        __SCTP_INC_STATS(net, SCTP_MIB_IN_PKT_DISCARDS);
 286        kfree_skb(skb);
 287        return 0;
 288
 289discard_release:
 290        /* Release the asoc/ep ref we took in the lookup calls. */
 291        if (transport)
 292                sctp_transport_put(transport);
 293        else
 294                sctp_endpoint_put(ep);
 295
 296        goto discard_it;
 297}
 298
 299/* Process the backlog queue of the socket.  Every skb on
 300 * the backlog holds a ref on an association or endpoint.
 301 * We hold this ref throughout the state machine to make
 302 * sure that the structure we need is still around.
 303 */
 304int sctp_backlog_rcv(struct sock *sk, struct sk_buff *skb)
 305{
 306        struct sctp_chunk *chunk = SCTP_INPUT_CB(skb)->chunk;
 307        struct sctp_inq *inqueue = &chunk->rcvr->inqueue;
 308        struct sctp_transport *t = chunk->transport;
 309        struct sctp_ep_common *rcvr = NULL;
 310        int backloged = 0;
 311
 312        rcvr = chunk->rcvr;
 313
 314        /* If the rcvr is dead then the association or endpoint
 315         * has been deleted and we can safely drop the chunk
 316         * and refs that we are holding.
 317         */
 318        if (rcvr->dead) {
 319                sctp_chunk_free(chunk);
 320                goto done;
 321        }
 322
 323        if (unlikely(rcvr->sk != sk)) {
 324                /* In this case, the association moved from one socket to
 325                 * another.  We are currently sitting on the backlog of the
 326                 * old socket, so we need to move.
 327                 * However, since we are here in the process context we
 328                 * need to take make sure that the user doesn't own
 329                 * the new socket when we process the packet.
 330                 * If the new socket is user-owned, queue the chunk to the
 331                 * backlog of the new socket without dropping any refs.
 332                 * Otherwise, we can safely push the chunk on the inqueue.
 333                 */
 334
 335                sk = rcvr->sk;
 336                local_bh_disable();
 337                bh_lock_sock(sk);
 338
 339                if (sock_owned_by_user(sk) || !sctp_newsk_ready(sk)) {
 340                        if (sk_add_backlog(sk, skb, sk->sk_rcvbuf))
 341                                sctp_chunk_free(chunk);
 342                        else
 343                                backloged = 1;
 344                } else
 345                        sctp_inq_push(inqueue, chunk);
 346
 347                bh_unlock_sock(sk);
 348                local_bh_enable();
 349
 350                /* If the chunk was backloged again, don't drop refs */
 351                if (backloged)
 352                        return 0;
 353        } else {
 354                if (!sctp_newsk_ready(sk)) {
 355                        if (!sk_add_backlog(sk, skb, sk->sk_rcvbuf))
 356                                return 0;
 357                        sctp_chunk_free(chunk);
 358                } else {
 359                        sctp_inq_push(inqueue, chunk);
 360                }
 361        }
 362
 363done:
 364        /* Release the refs we took in sctp_add_backlog */
 365        if (SCTP_EP_TYPE_ASSOCIATION == rcvr->type)
 366                sctp_transport_put(t);
 367        else if (SCTP_EP_TYPE_SOCKET == rcvr->type)
 368                sctp_endpoint_put(sctp_ep(rcvr));
 369        else
 370                BUG();
 371
 372        return 0;
 373}
 374
 375static int sctp_add_backlog(struct sock *sk, struct sk_buff *skb)
 376{
 377        struct sctp_chunk *chunk = SCTP_INPUT_CB(skb)->chunk;
 378        struct sctp_transport *t = chunk->transport;
 379        struct sctp_ep_common *rcvr = chunk->rcvr;
 380        int ret;
 381
 382        ret = sk_add_backlog(sk, skb, sk->sk_rcvbuf);
 383        if (!ret) {
 384                /* Hold the assoc/ep while hanging on the backlog queue.
 385                 * This way, we know structures we need will not disappear
 386                 * from us
 387                 */
 388                if (SCTP_EP_TYPE_ASSOCIATION == rcvr->type)
 389                        sctp_transport_hold(t);
 390                else if (SCTP_EP_TYPE_SOCKET == rcvr->type)
 391                        sctp_endpoint_hold(sctp_ep(rcvr));
 392                else
 393                        BUG();
 394        }
 395        return ret;
 396
 397}
 398
 399/* Handle icmp frag needed error. */
 400void sctp_icmp_frag_needed(struct sock *sk, struct sctp_association *asoc,
 401                           struct sctp_transport *t, __u32 pmtu)
 402{
 403        if (!t ||
 404            (t->pathmtu <= pmtu &&
 405             t->pl.probe_size + sctp_transport_pl_hlen(t) <= pmtu))
 406                return;
 407
 408        if (sock_owned_by_user(sk)) {
 409                atomic_set(&t->mtu_info, pmtu);
 410                asoc->pmtu_pending = 1;
 411                t->pmtu_pending = 1;
 412                return;
 413        }
 414
 415        if (!(t->param_flags & SPP_PMTUD_ENABLE))
 416                /* We can't allow retransmitting in such case, as the
 417                 * retransmission would be sized just as before, and thus we
 418                 * would get another icmp, and retransmit again.
 419                 */
 420                return;
 421
 422        /* Update transports view of the MTU. Return if no update was needed.
 423         * If an update wasn't needed/possible, it also doesn't make sense to
 424         * try to retransmit now.
 425         */
 426        if (!sctp_transport_update_pmtu(t, pmtu))
 427                return;
 428
 429        /* Update association pmtu. */
 430        sctp_assoc_sync_pmtu(asoc);
 431
 432        /* Retransmit with the new pmtu setting. */
 433        sctp_retransmit(&asoc->outqueue, t, SCTP_RTXR_PMTUD);
 434}
 435
 436void sctp_icmp_redirect(struct sock *sk, struct sctp_transport *t,
 437                        struct sk_buff *skb)
 438{
 439        struct dst_entry *dst;
 440
 441        if (sock_owned_by_user(sk) || !t)
 442                return;
 443        dst = sctp_transport_dst_check(t);
 444        if (dst)
 445                dst->ops->redirect(dst, sk, skb);
 446}
 447
 448/*
 449 * SCTP Implementer's Guide, 2.37 ICMP handling procedures
 450 *
 451 * ICMP8) If the ICMP code is a "Unrecognized next header type encountered"
 452 *        or a "Protocol Unreachable" treat this message as an abort
 453 *        with the T bit set.
 454 *
 455 * This function sends an event to the state machine, which will abort the
 456 * association.
 457 *
 458 */
 459void sctp_icmp_proto_unreachable(struct sock *sk,
 460                           struct sctp_association *asoc,
 461                           struct sctp_transport *t)
 462{
 463        if (sock_owned_by_user(sk)) {
 464                if (timer_pending(&t->proto_unreach_timer))
 465                        return;
 466                else {
 467                        if (!mod_timer(&t->proto_unreach_timer,
 468                                                jiffies + (HZ/20)))
 469                                sctp_transport_hold(t);
 470                }
 471        } else {
 472                struct net *net = sock_net(sk);
 473
 474                pr_debug("%s: unrecognized next header type "
 475                         "encountered!\n", __func__);
 476
 477                if (del_timer(&t->proto_unreach_timer))
 478                        sctp_transport_put(t);
 479
 480                sctp_do_sm(net, SCTP_EVENT_T_OTHER,
 481                           SCTP_ST_OTHER(SCTP_EVENT_ICMP_PROTO_UNREACH),
 482                           asoc->state, asoc->ep, asoc, t,
 483                           GFP_ATOMIC);
 484        }
 485}
 486
 487/* Common lookup code for icmp/icmpv6 error handler. */
 488struct sock *sctp_err_lookup(struct net *net, int family, struct sk_buff *skb,
 489                             struct sctphdr *sctphdr,
 490                             struct sctp_association **app,
 491                             struct sctp_transport **tpp)
 492{
 493        struct sctp_init_chunk *chunkhdr, _chunkhdr;
 494        union sctp_addr saddr;
 495        union sctp_addr daddr;
 496        struct sctp_af *af;
 497        struct sock *sk = NULL;
 498        struct sctp_association *asoc;
 499        struct sctp_transport *transport = NULL;
 500        __u32 vtag = ntohl(sctphdr->vtag);
 501
 502        *app = NULL; *tpp = NULL;
 503
 504        af = sctp_get_af_specific(family);
 505        if (unlikely(!af)) {
 506                return NULL;
 507        }
 508
 509        /* Initialize local addresses for lookups. */
 510        af->from_skb(&saddr, skb, 1);
 511        af->from_skb(&daddr, skb, 0);
 512
 513        /* Look for an association that matches the incoming ICMP error
 514         * packet.
 515         */
 516        asoc = __sctp_lookup_association(net, &saddr, &daddr, &transport);
 517        if (!asoc)
 518                return NULL;
 519
 520        sk = asoc->base.sk;
 521
 522        /* RFC 4960, Appendix C. ICMP Handling
 523         *
 524         * ICMP6) An implementation MUST validate that the Verification Tag
 525         * contained in the ICMP message matches the Verification Tag of
 526         * the peer.  If the Verification Tag is not 0 and does NOT
 527         * match, discard the ICMP message.  If it is 0 and the ICMP
 528         * message contains enough bytes to verify that the chunk type is
 529         * an INIT chunk and that the Initiate Tag matches the tag of the
 530         * peer, continue with ICMP7.  If the ICMP message is too short
 531         * or the chunk type or the Initiate Tag does not match, silently
 532         * discard the packet.
 533         */
 534        if (vtag == 0) {
 535                /* chunk header + first 4 octects of init header */
 536                chunkhdr = skb_header_pointer(skb, skb_transport_offset(skb) +
 537                                              sizeof(struct sctphdr),
 538                                              sizeof(struct sctp_chunkhdr) +
 539                                              sizeof(__be32), &_chunkhdr);
 540                if (!chunkhdr ||
 541                    chunkhdr->chunk_hdr.type != SCTP_CID_INIT ||
 542                    ntohl(chunkhdr->init_hdr.init_tag) != asoc->c.my_vtag)
 543                        goto out;
 544
 545        } else if (vtag != asoc->c.peer_vtag) {
 546                goto out;
 547        }
 548
 549        bh_lock_sock(sk);
 550
 551        /* If too many ICMPs get dropped on busy
 552         * servers this needs to be solved differently.
 553         */
 554        if (sock_owned_by_user(sk))
 555                __NET_INC_STATS(net, LINUX_MIB_LOCKDROPPEDICMPS);
 556
 557        *app = asoc;
 558        *tpp = transport;
 559        return sk;
 560
 561out:
 562        sctp_transport_put(transport);
 563        return NULL;
 564}
 565
 566/* Common cleanup code for icmp/icmpv6 error handler. */
 567void sctp_err_finish(struct sock *sk, struct sctp_transport *t)
 568{
 569        bh_unlock_sock(sk);
 570        sctp_transport_put(t);
 571}
 572
 573static void sctp_v4_err_handle(struct sctp_transport *t, struct sk_buff *skb,
 574                               __u8 type, __u8 code, __u32 info)
 575{
 576        struct sctp_association *asoc = t->asoc;
 577        struct sock *sk = asoc->base.sk;
 578        int err = 0;
 579
 580        switch (type) {
 581        case ICMP_PARAMETERPROB:
 582                err = EPROTO;
 583                break;
 584        case ICMP_DEST_UNREACH:
 585                if (code > NR_ICMP_UNREACH)
 586                        return;
 587                if (code == ICMP_FRAG_NEEDED) {
 588                        sctp_icmp_frag_needed(sk, asoc, t, SCTP_TRUNC4(info));
 589                        return;
 590                }
 591                if (code == ICMP_PROT_UNREACH) {
 592                        sctp_icmp_proto_unreachable(sk, asoc, t);
 593                        return;
 594                }
 595                err = icmp_err_convert[code].errno;
 596                break;
 597        case ICMP_TIME_EXCEEDED:
 598                if (code == ICMP_EXC_FRAGTIME)
 599                        return;
 600
 601                err = EHOSTUNREACH;
 602                break;
 603        case ICMP_REDIRECT:
 604                sctp_icmp_redirect(sk, t, skb);
 605        default:
 606                return;
 607        }
 608        if (!sock_owned_by_user(sk) && inet_sk(sk)->recverr) {
 609                sk->sk_err = err;
 610                sk_error_report(sk);
 611        } else {  /* Only an error on timeout */
 612                sk->sk_err_soft = err;
 613        }
 614}
 615
 616/*
 617 * This routine is called by the ICMP module when it gets some
 618 * sort of error condition.  If err < 0 then the socket should
 619 * be closed and the error returned to the user.  If err > 0
 620 * it's just the icmp type << 8 | icmp code.  After adjustment
 621 * header points to the first 8 bytes of the sctp header.  We need
 622 * to find the appropriate port.
 623 *
 624 * The locking strategy used here is very "optimistic". When
 625 * someone else accesses the socket the ICMP is just dropped
 626 * and for some paths there is no check at all.
 627 * A more general error queue to queue errors for later handling
 628 * is probably better.
 629 *
 630 */
 631int sctp_v4_err(struct sk_buff *skb, __u32 info)
 632{
 633        const struct iphdr *iph = (const struct iphdr *)skb->data;
 634        const int type = icmp_hdr(skb)->type;
 635        const int code = icmp_hdr(skb)->code;
 636        struct net *net = dev_net(skb->dev);
 637        struct sctp_transport *transport;
 638        struct sctp_association *asoc;
 639        __u16 saveip, savesctp;
 640        struct sock *sk;
 641
 642        /* Fix up skb to look at the embedded net header. */
 643        saveip = skb->network_header;
 644        savesctp = skb->transport_header;
 645        skb_reset_network_header(skb);
 646        skb_set_transport_header(skb, iph->ihl * 4);
 647        sk = sctp_err_lookup(net, AF_INET, skb, sctp_hdr(skb), &asoc, &transport);
 648        /* Put back, the original values. */
 649        skb->network_header = saveip;
 650        skb->transport_header = savesctp;
 651        if (!sk) {
 652                __ICMP_INC_STATS(net, ICMP_MIB_INERRORS);
 653                return -ENOENT;
 654        }
 655
 656        sctp_v4_err_handle(transport, skb, type, code, info);
 657        sctp_err_finish(sk, transport);
 658
 659        return 0;
 660}
 661
 662int sctp_udp_v4_err(struct sock *sk, struct sk_buff *skb)
 663{
 664        struct net *net = dev_net(skb->dev);
 665        struct sctp_association *asoc;
 666        struct sctp_transport *t;
 667        struct icmphdr *hdr;
 668        __u32 info = 0;
 669
 670        skb->transport_header += sizeof(struct udphdr);
 671        sk = sctp_err_lookup(net, AF_INET, skb, sctp_hdr(skb), &asoc, &t);
 672        if (!sk) {
 673                __ICMP_INC_STATS(net, ICMP_MIB_INERRORS);
 674                return -ENOENT;
 675        }
 676
 677        skb->transport_header -= sizeof(struct udphdr);
 678        hdr = (struct icmphdr *)(skb_network_header(skb) - sizeof(struct icmphdr));
 679        if (hdr->type == ICMP_REDIRECT) {
 680                /* can't be handled without outer iphdr known, leave it to udp_err */
 681                sctp_err_finish(sk, t);
 682                return 0;
 683        }
 684        if (hdr->type == ICMP_DEST_UNREACH && hdr->code == ICMP_FRAG_NEEDED)
 685                info = ntohs(hdr->un.frag.mtu);
 686        sctp_v4_err_handle(t, skb, hdr->type, hdr->code, info);
 687
 688        sctp_err_finish(sk, t);
 689        return 1;
 690}
 691
 692/*
 693 * RFC 2960, 8.4 - Handle "Out of the blue" Packets.
 694 *
 695 * This function scans all the chunks in the OOTB packet to determine if
 696 * the packet should be discarded right away.  If a response might be needed
 697 * for this packet, or, if further processing is possible, the packet will
 698 * be queued to a proper inqueue for the next phase of handling.
 699 *
 700 * Output:
 701 * Return 0 - If further processing is needed.
 702 * Return 1 - If the packet can be discarded right away.
 703 */
 704static int sctp_rcv_ootb(struct sk_buff *skb)
 705{
 706        struct sctp_chunkhdr *ch, _ch;
 707        int ch_end, offset = 0;
 708
 709        /* Scan through all the chunks in the packet.  */
 710        do {
 711                /* Make sure we have at least the header there */
 712                if (offset + sizeof(_ch) > skb->len)
 713                        break;
 714
 715                ch = skb_header_pointer(skb, offset, sizeof(*ch), &_ch);
 716
 717                /* Break out if chunk length is less then minimal. */
 718                if (!ch || ntohs(ch->length) < sizeof(_ch))
 719                        break;
 720
 721                ch_end = offset + SCTP_PAD4(ntohs(ch->length));
 722                if (ch_end > skb->len)
 723                        break;
 724
 725                /* RFC 8.4, 2) If the OOTB packet contains an ABORT chunk, the
 726                 * receiver MUST silently discard the OOTB packet and take no
 727                 * further action.
 728                 */
 729                if (SCTP_CID_ABORT == ch->type)
 730                        goto discard;
 731
 732                /* RFC 8.4, 6) If the packet contains a SHUTDOWN COMPLETE
 733                 * chunk, the receiver should silently discard the packet
 734                 * and take no further action.
 735                 */
 736                if (SCTP_CID_SHUTDOWN_COMPLETE == ch->type)
 737                        goto discard;
 738
 739                /* RFC 4460, 2.11.2
 740                 * This will discard packets with INIT chunk bundled as
 741                 * subsequent chunks in the packet.  When INIT is first,
 742                 * the normal INIT processing will discard the chunk.
 743                 */
 744                if (SCTP_CID_INIT == ch->type && (void *)ch != skb->data)
 745                        goto discard;
 746
 747                offset = ch_end;
 748        } while (ch_end < skb->len);
 749
 750        return 0;
 751
 752discard:
 753        return 1;
 754}
 755
 756/* Insert endpoint into the hash table.  */
 757static int __sctp_hash_endpoint(struct sctp_endpoint *ep)
 758{
 759        struct sock *sk = ep->base.sk;
 760        struct net *net = sock_net(sk);
 761        struct sctp_hashbucket *head;
 762
 763        ep->hashent = sctp_ep_hashfn(net, ep->base.bind_addr.port);
 764        head = &sctp_ep_hashtable[ep->hashent];
 765
 766        if (sk->sk_reuseport) {
 767                bool any = sctp_is_ep_boundall(sk);
 768                struct sctp_endpoint *ep2;
 769                struct list_head *list;
 770                int cnt = 0, err = 1;
 771
 772                list_for_each(list, &ep->base.bind_addr.address_list)
 773                        cnt++;
 774
 775                sctp_for_each_hentry(ep2, &head->chain) {
 776                        struct sock *sk2 = ep2->base.sk;
 777
 778                        if (!net_eq(sock_net(sk2), net) || sk2 == sk ||
 779                            !uid_eq(sock_i_uid(sk2), sock_i_uid(sk)) ||
 780                            !sk2->sk_reuseport)
 781                                continue;
 782
 783                        err = sctp_bind_addrs_check(sctp_sk(sk2),
 784                                                    sctp_sk(sk), cnt);
 785                        if (!err) {
 786                                err = reuseport_add_sock(sk, sk2, any);
 787                                if (err)
 788                                        return err;
 789                                break;
 790                        } else if (err < 0) {
 791                                return err;
 792                        }
 793                }
 794
 795                if (err) {
 796                        err = reuseport_alloc(sk, any);
 797                        if (err)
 798                                return err;
 799                }
 800        }
 801
 802        write_lock(&head->lock);
 803        hlist_add_head(&ep->node, &head->chain);
 804        write_unlock(&head->lock);
 805        return 0;
 806}
 807
 808/* Add an endpoint to the hash. Local BH-safe. */
 809int sctp_hash_endpoint(struct sctp_endpoint *ep)
 810{
 811        int err;
 812
 813        local_bh_disable();
 814        err = __sctp_hash_endpoint(ep);
 815        local_bh_enable();
 816
 817        return err;
 818}
 819
 820/* Remove endpoint from the hash table.  */
 821static void __sctp_unhash_endpoint(struct sctp_endpoint *ep)
 822{
 823        struct sock *sk = ep->base.sk;
 824        struct sctp_hashbucket *head;
 825
 826        ep->hashent = sctp_ep_hashfn(sock_net(sk), ep->base.bind_addr.port);
 827
 828        head = &sctp_ep_hashtable[ep->hashent];
 829
 830        if (rcu_access_pointer(sk->sk_reuseport_cb))
 831                reuseport_detach_sock(sk);
 832
 833        write_lock(&head->lock);
 834        hlist_del_init(&ep->node);
 835        write_unlock(&head->lock);
 836}
 837
 838/* Remove endpoint from the hash.  Local BH-safe. */
 839void sctp_unhash_endpoint(struct sctp_endpoint *ep)
 840{
 841        local_bh_disable();
 842        __sctp_unhash_endpoint(ep);
 843        local_bh_enable();
 844}
 845
 846static inline __u32 sctp_hashfn(const struct net *net, __be16 lport,
 847                                const union sctp_addr *paddr, __u32 seed)
 848{
 849        __u32 addr;
 850
 851        if (paddr->sa.sa_family == AF_INET6)
 852                addr = jhash(&paddr->v6.sin6_addr, 16, seed);
 853        else
 854                addr = (__force __u32)paddr->v4.sin_addr.s_addr;
 855
 856        return  jhash_3words(addr, ((__force __u32)paddr->v4.sin_port) << 16 |
 857                             (__force __u32)lport, net_hash_mix(net), seed);
 858}
 859
 860/* Look up an endpoint. */
 861static struct sctp_endpoint *__sctp_rcv_lookup_endpoint(
 862                                        struct net *net, struct sk_buff *skb,
 863                                        const union sctp_addr *laddr,
 864                                        const union sctp_addr *paddr)
 865{
 866        struct sctp_hashbucket *head;
 867        struct sctp_endpoint *ep;
 868        struct sock *sk;
 869        __be16 lport;
 870        int hash;
 871
 872        lport = laddr->v4.sin_port;
 873        hash = sctp_ep_hashfn(net, ntohs(lport));
 874        head = &sctp_ep_hashtable[hash];
 875        read_lock(&head->lock);
 876        sctp_for_each_hentry(ep, &head->chain) {
 877                if (sctp_endpoint_is_match(ep, net, laddr))
 878                        goto hit;
 879        }
 880
 881        ep = sctp_sk(net->sctp.ctl_sock)->ep;
 882
 883hit:
 884        sk = ep->base.sk;
 885        if (sk->sk_reuseport) {
 886                __u32 phash = sctp_hashfn(net, lport, paddr, 0);
 887
 888                sk = reuseport_select_sock(sk, phash, skb,
 889                                           sizeof(struct sctphdr));
 890                if (sk)
 891                        ep = sctp_sk(sk)->ep;
 892        }
 893        sctp_endpoint_hold(ep);
 894        read_unlock(&head->lock);
 895        return ep;
 896}
 897
 898/* rhashtable for transport */
 899struct sctp_hash_cmp_arg {
 900        const union sctp_addr   *paddr;
 901        const struct net        *net;
 902        __be16                  lport;
 903};
 904
 905static inline int sctp_hash_cmp(struct rhashtable_compare_arg *arg,
 906                                const void *ptr)
 907{
 908        struct sctp_transport *t = (struct sctp_transport *)ptr;
 909        const struct sctp_hash_cmp_arg *x = arg->key;
 910        int err = 1;
 911
 912        if (!sctp_cmp_addr_exact(&t->ipaddr, x->paddr))
 913                return err;
 914        if (!sctp_transport_hold(t))
 915                return err;
 916
 917        if (!net_eq(t->asoc->base.net, x->net))
 918                goto out;
 919        if (x->lport != htons(t->asoc->base.bind_addr.port))
 920                goto out;
 921
 922        err = 0;
 923out:
 924        sctp_transport_put(t);
 925        return err;
 926}
 927
 928static inline __u32 sctp_hash_obj(const void *data, u32 len, u32 seed)
 929{
 930        const struct sctp_transport *t = data;
 931
 932        return sctp_hashfn(t->asoc->base.net,
 933                           htons(t->asoc->base.bind_addr.port),
 934                           &t->ipaddr, seed);
 935}
 936
 937static inline __u32 sctp_hash_key(const void *data, u32 len, u32 seed)
 938{
 939        const struct sctp_hash_cmp_arg *x = data;
 940
 941        return sctp_hashfn(x->net, x->lport, x->paddr, seed);
 942}
 943
 944static const struct rhashtable_params sctp_hash_params = {
 945        .head_offset            = offsetof(struct sctp_transport, node),
 946        .hashfn                 = sctp_hash_key,
 947        .obj_hashfn             = sctp_hash_obj,
 948        .obj_cmpfn              = sctp_hash_cmp,
 949        .automatic_shrinking    = true,
 950};
 951
 952int sctp_transport_hashtable_init(void)
 953{
 954        return rhltable_init(&sctp_transport_hashtable, &sctp_hash_params);
 955}
 956
 957void sctp_transport_hashtable_destroy(void)
 958{
 959        rhltable_destroy(&sctp_transport_hashtable);
 960}
 961
 962int sctp_hash_transport(struct sctp_transport *t)
 963{
 964        struct sctp_transport *transport;
 965        struct rhlist_head *tmp, *list;
 966        struct sctp_hash_cmp_arg arg;
 967        int err;
 968
 969        if (t->asoc->temp)
 970                return 0;
 971
 972        arg.net   = sock_net(t->asoc->base.sk);
 973        arg.paddr = &t->ipaddr;
 974        arg.lport = htons(t->asoc->base.bind_addr.port);
 975
 976        rcu_read_lock();
 977        list = rhltable_lookup(&sctp_transport_hashtable, &arg,
 978                               sctp_hash_params);
 979
 980        rhl_for_each_entry_rcu(transport, tmp, list, node)
 981                if (transport->asoc->ep == t->asoc->ep) {
 982                        rcu_read_unlock();
 983                        return -EEXIST;
 984                }
 985        rcu_read_unlock();
 986
 987        err = rhltable_insert_key(&sctp_transport_hashtable, &arg,
 988                                  &t->node, sctp_hash_params);
 989        if (err)
 990                pr_err_once("insert transport fail, errno %d\n", err);
 991
 992        return err;
 993}
 994
 995void sctp_unhash_transport(struct sctp_transport *t)
 996{
 997        if (t->asoc->temp)
 998                return;
 999
1000        rhltable_remove(&sctp_transport_hashtable, &t->node,
1001                        sctp_hash_params);
1002}
1003
1004/* return a transport with holding it */
1005struct sctp_transport *sctp_addrs_lookup_transport(
1006                                struct net *net,
1007                                const union sctp_addr *laddr,
1008                                const union sctp_addr *paddr)
1009{
1010        struct rhlist_head *tmp, *list;
1011        struct sctp_transport *t;
1012        struct sctp_hash_cmp_arg arg = {
1013                .paddr = paddr,
1014                .net   = net,
1015                .lport = laddr->v4.sin_port,
1016        };
1017
1018        list = rhltable_lookup(&sctp_transport_hashtable, &arg,
1019                               sctp_hash_params);
1020
1021        rhl_for_each_entry_rcu(t, tmp, list, node) {
1022                if (!sctp_transport_hold(t))
1023                        continue;
1024
1025                if (sctp_bind_addr_match(&t->asoc->base.bind_addr,
1026                                         laddr, sctp_sk(t->asoc->base.sk)))
1027                        return t;
1028                sctp_transport_put(t);
1029        }
1030
1031        return NULL;
1032}
1033
1034/* return a transport without holding it, as it's only used under sock lock */
1035struct sctp_transport *sctp_epaddr_lookup_transport(
1036                                const struct sctp_endpoint *ep,
1037                                const union sctp_addr *paddr)
1038{
1039        struct net *net = sock_net(ep->base.sk);
1040        struct rhlist_head *tmp, *list;
1041        struct sctp_transport *t;
1042        struct sctp_hash_cmp_arg arg = {
1043                .paddr = paddr,
1044                .net   = net,
1045                .lport = htons(ep->base.bind_addr.port),
1046        };
1047
1048        list = rhltable_lookup(&sctp_transport_hashtable, &arg,
1049                               sctp_hash_params);
1050
1051        rhl_for_each_entry_rcu(t, tmp, list, node)
1052                if (ep == t->asoc->ep)
1053                        return t;
1054
1055        return NULL;
1056}
1057
1058/* Look up an association. */
1059static struct sctp_association *__sctp_lookup_association(
1060                                        struct net *net,
1061                                        const union sctp_addr *local,
1062                                        const union sctp_addr *peer,
1063                                        struct sctp_transport **pt)
1064{
1065        struct sctp_transport *t;
1066        struct sctp_association *asoc = NULL;
1067
1068        t = sctp_addrs_lookup_transport(net, local, peer);
1069        if (!t)
1070                goto out;
1071
1072        asoc = t->asoc;
1073        *pt = t;
1074
1075out:
1076        return asoc;
1077}
1078
1079/* Look up an association. protected by RCU read lock */
1080static
1081struct sctp_association *sctp_lookup_association(struct net *net,
1082                                                 const union sctp_addr *laddr,
1083                                                 const union sctp_addr *paddr,
1084                                                 struct sctp_transport **transportp)
1085{
1086        struct sctp_association *asoc;
1087
1088        rcu_read_lock();
1089        asoc = __sctp_lookup_association(net, laddr, paddr, transportp);
1090        rcu_read_unlock();
1091
1092        return asoc;
1093}
1094
1095/* Is there an association matching the given local and peer addresses? */
1096bool sctp_has_association(struct net *net,
1097                          const union sctp_addr *laddr,
1098                          const union sctp_addr *paddr)
1099{
1100        struct sctp_transport *transport;
1101
1102        if (sctp_lookup_association(net, laddr, paddr, &transport)) {
1103                sctp_transport_put(transport);
1104                return true;
1105        }
1106
1107        return false;
1108}
1109
1110/*
1111 * SCTP Implementors Guide, 2.18 Handling of address
1112 * parameters within the INIT or INIT-ACK.
1113 *
1114 * D) When searching for a matching TCB upon reception of an INIT
1115 *    or INIT-ACK chunk the receiver SHOULD use not only the
1116 *    source address of the packet (containing the INIT or
1117 *    INIT-ACK) but the receiver SHOULD also use all valid
1118 *    address parameters contained within the chunk.
1119 *
1120 * 2.18.3 Solution description
1121 *
1122 * This new text clearly specifies to an implementor the need
1123 * to look within the INIT or INIT-ACK. Any implementation that
1124 * does not do this, may not be able to establish associations
1125 * in certain circumstances.
1126 *
1127 */
1128static struct sctp_association *__sctp_rcv_init_lookup(struct net *net,
1129        struct sk_buff *skb,
1130        const union sctp_addr *laddr, struct sctp_transport **transportp)
1131{
1132        struct sctp_association *asoc;
1133        union sctp_addr addr;
1134        union sctp_addr *paddr = &addr;
1135        struct sctphdr *sh = sctp_hdr(skb);
1136        union sctp_params params;
1137        struct sctp_init_chunk *init;
1138        struct sctp_af *af;
1139
1140        /*
1141         * This code will NOT touch anything inside the chunk--it is
1142         * strictly READ-ONLY.
1143         *
1144         * RFC 2960 3  SCTP packet Format
1145         *
1146         * Multiple chunks can be bundled into one SCTP packet up to
1147         * the MTU size, except for the INIT, INIT ACK, and SHUTDOWN
1148         * COMPLETE chunks.  These chunks MUST NOT be bundled with any
1149         * other chunk in a packet.  See Section 6.10 for more details
1150         * on chunk bundling.
1151         */
1152
1153        /* Find the start of the TLVs and the end of the chunk.  This is
1154         * the region we search for address parameters.
1155         */
1156        init = (struct sctp_init_chunk *)skb->data;
1157
1158        /* Walk the parameters looking for embedded addresses. */
1159        sctp_walk_params(params, init, init_hdr.params) {
1160
1161                /* Note: Ignoring hostname addresses. */
1162                af = sctp_get_af_specific(param_type2af(params.p->type));
1163                if (!af)
1164                        continue;
1165
1166                if (!af->from_addr_param(paddr, params.addr, sh->source, 0))
1167                        continue;
1168
1169                asoc = __sctp_lookup_association(net, laddr, paddr, transportp);
1170                if (asoc)
1171                        return asoc;
1172        }
1173
1174        return NULL;
1175}
1176
1177/* ADD-IP, Section 5.2
1178 * When an endpoint receives an ASCONF Chunk from the remote peer
1179 * special procedures may be needed to identify the association the
1180 * ASCONF Chunk is associated with. To properly find the association
1181 * the following procedures SHOULD be followed:
1182 *
1183 * D2) If the association is not found, use the address found in the
1184 * Address Parameter TLV combined with the port number found in the
1185 * SCTP common header. If found proceed to rule D4.
1186 *
1187 * D2-ext) If more than one ASCONF Chunks are packed together, use the
1188 * address found in the ASCONF Address Parameter TLV of each of the
1189 * subsequent ASCONF Chunks. If found, proceed to rule D4.
1190 */
1191static struct sctp_association *__sctp_rcv_asconf_lookup(
1192                                        struct net *net,
1193                                        struct sctp_chunkhdr *ch,
1194                                        const union sctp_addr *laddr,
1195                                        __be16 peer_port,
1196                                        struct sctp_transport **transportp)
1197{
1198        struct sctp_addip_chunk *asconf = (struct sctp_addip_chunk *)ch;
1199        struct sctp_af *af;
1200        union sctp_addr_param *param;
1201        union sctp_addr paddr;
1202
1203        if (ntohs(ch->length) < sizeof(*asconf) + sizeof(struct sctp_paramhdr))
1204                return NULL;
1205
1206        /* Skip over the ADDIP header and find the Address parameter */
1207        param = (union sctp_addr_param *)(asconf + 1);
1208
1209        af = sctp_get_af_specific(param_type2af(param->p.type));
1210        if (unlikely(!af))
1211                return NULL;
1212
1213        if (!af->from_addr_param(&paddr, param, peer_port, 0))
1214                return NULL;
1215
1216        return __sctp_lookup_association(net, laddr, &paddr, transportp);
1217}
1218
1219
1220/* SCTP-AUTH, Section 6.3:
1221*    If the receiver does not find a STCB for a packet containing an AUTH
1222*    chunk as the first chunk and not a COOKIE-ECHO chunk as the second
1223*    chunk, it MUST use the chunks after the AUTH chunk to look up an existing
1224*    association.
1225*
1226* This means that any chunks that can help us identify the association need
1227* to be looked at to find this association.
1228*/
1229static struct sctp_association *__sctp_rcv_walk_lookup(struct net *net,
1230                                      struct sk_buff *skb,
1231                                      const union sctp_addr *laddr,
1232                                      struct sctp_transport **transportp)
1233{
1234        struct sctp_association *asoc = NULL;
1235        struct sctp_chunkhdr *ch;
1236        int have_auth = 0;
1237        unsigned int chunk_num = 1;
1238        __u8 *ch_end;
1239
1240        /* Walk through the chunks looking for AUTH or ASCONF chunks
1241         * to help us find the association.
1242         */
1243        ch = (struct sctp_chunkhdr *)skb->data;
1244        do {
1245                /* Break out if chunk length is less then minimal. */
1246                if (ntohs(ch->length) < sizeof(*ch))
1247                        break;
1248
1249                ch_end = ((__u8 *)ch) + SCTP_PAD4(ntohs(ch->length));
1250                if (ch_end > skb_tail_pointer(skb))
1251                        break;
1252
1253                switch (ch->type) {
1254                case SCTP_CID_AUTH:
1255                        have_auth = chunk_num;
1256                        break;
1257
1258                case SCTP_CID_COOKIE_ECHO:
1259                        /* If a packet arrives containing an AUTH chunk as
1260                         * a first chunk, a COOKIE-ECHO chunk as the second
1261                         * chunk, and possibly more chunks after them, and
1262                         * the receiver does not have an STCB for that
1263                         * packet, then authentication is based on
1264                         * the contents of the COOKIE- ECHO chunk.
1265                         */
1266                        if (have_auth == 1 && chunk_num == 2)
1267                                return NULL;
1268                        break;
1269
1270                case SCTP_CID_ASCONF:
1271                        if (have_auth || net->sctp.addip_noauth)
1272                                asoc = __sctp_rcv_asconf_lookup(
1273                                                net, ch, laddr,
1274                                                sctp_hdr(skb)->source,
1275                                                transportp);
1276                default:
1277                        break;
1278                }
1279
1280                if (asoc)
1281                        break;
1282
1283                ch = (struct sctp_chunkhdr *)ch_end;
1284                chunk_num++;
1285        } while (ch_end + sizeof(*ch) < skb_tail_pointer(skb));
1286
1287        return asoc;
1288}
1289
1290/*
1291 * There are circumstances when we need to look inside the SCTP packet
1292 * for information to help us find the association.   Examples
1293 * include looking inside of INIT/INIT-ACK chunks or after the AUTH
1294 * chunks.
1295 */
1296static struct sctp_association *__sctp_rcv_lookup_harder(struct net *net,
1297                                      struct sk_buff *skb,
1298                                      const union sctp_addr *laddr,
1299                                      struct sctp_transport **transportp)
1300{
1301        struct sctp_chunkhdr *ch;
1302
1303        /* We do not allow GSO frames here as we need to linearize and
1304         * then cannot guarantee frame boundaries. This shouldn't be an
1305         * issue as packets hitting this are mostly INIT or INIT-ACK and
1306         * those cannot be on GSO-style anyway.
1307         */
1308        if (skb_is_gso(skb) && skb_is_gso_sctp(skb))
1309                return NULL;
1310
1311        ch = (struct sctp_chunkhdr *)skb->data;
1312
1313        /* The code below will attempt to walk the chunk and extract
1314         * parameter information.  Before we do that, we need to verify
1315         * that the chunk length doesn't cause overflow.  Otherwise, we'll
1316         * walk off the end.
1317         */
1318        if (SCTP_PAD4(ntohs(ch->length)) > skb->len)
1319                return NULL;
1320
1321        /* If this is INIT/INIT-ACK look inside the chunk too. */
1322        if (ch->type == SCTP_CID_INIT || ch->type == SCTP_CID_INIT_ACK)
1323                return __sctp_rcv_init_lookup(net, skb, laddr, transportp);
1324
1325        return __sctp_rcv_walk_lookup(net, skb, laddr, transportp);
1326}
1327
1328/* Lookup an association for an inbound skb. */
1329static struct sctp_association *__sctp_rcv_lookup(struct net *net,
1330                                      struct sk_buff *skb,
1331                                      const union sctp_addr *paddr,
1332                                      const union sctp_addr *laddr,
1333                                      struct sctp_transport **transportp)
1334{
1335        struct sctp_association *asoc;
1336
1337        asoc = __sctp_lookup_association(net, laddr, paddr, transportp);
1338        if (asoc)
1339                goto out;
1340
1341        /* Further lookup for INIT/INIT-ACK packets.
1342         * SCTP Implementors Guide, 2.18 Handling of address
1343         * parameters within the INIT or INIT-ACK.
1344         */
1345        asoc = __sctp_rcv_lookup_harder(net, skb, laddr, transportp);
1346        if (asoc)
1347                goto out;
1348
1349        if (paddr->sa.sa_family == AF_INET)
1350                pr_debug("sctp: asoc not found for src:%pI4:%d dst:%pI4:%d\n",
1351                         &laddr->v4.sin_addr, ntohs(laddr->v4.sin_port),
1352                         &paddr->v4.sin_addr, ntohs(paddr->v4.sin_port));
1353        else
1354                pr_debug("sctp: asoc not found for src:%pI6:%d dst:%pI6:%d\n",
1355                         &laddr->v6.sin6_addr, ntohs(laddr->v6.sin6_port),
1356                         &paddr->v6.sin6_addr, ntohs(paddr->v6.sin6_port));
1357
1358out:
1359        return asoc;
1360}
1361