/* SCTP kernel implementation1* Copyright (c) 1999-2000 Cisco, Inc.2* Copyright (c) 1999-2001 Motorola, Inc.3* Copyright (c) 2001-2003 International Business Machines, Corp.4* Copyright (c) 2001 Intel Corp.5* Copyright (c) 2001 Nokia, Inc.6* Copyright (c) 2001 La Monte H.P. Yarroll7*8* This file is part of the SCTP kernel implementation9*10* These functions handle all input from the IP layer into SCTP.11*12* This SCTP implementation is free software;13* you can redistribute it and/or modify it under the terms of14* the GNU General Public License as published by15* the Free Software Foundation; either version 2, or (at your option)16* any later version.17*18* This SCTP implementation is distributed in the hope that it19* will be useful, but WITHOUT ANY WARRANTY; without even the implied20* ************************21* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.22* See the GNU General Public License for more details.23*24* You should have received a copy of the GNU General Public License25* along with GNU CC; see the file COPYING. If not, write to26* the Free Software Foundation, 59 Temple Place - Suite 330,27* Boston, MA 02111-1307, USA.28*29* Please send any bug reports or fixes you make to the30* email address(es):31* lksctp developers <[email protected]>32*33* Or submit a bug report through the following website:34* http://www.sf.net/projects/lksctp35*36* Written or modified by:37* La Monte H.P. Yarroll <[email protected]>38* Karl Knutson <[email protected]>39* Xingang Guo <[email protected]>40* Jon Grimm <[email protected]>41* Hui Huang <[email protected]>42* Daisy Chang <[email protected]>43* Sridhar Samudrala <[email protected]>44* Ardelle Fan <[email protected]>45*46* Any bugs reported given to us we will try to fix... any fixes shared will47* be incorporated into the next SCTP release.48*/4950#include <linux/types.h>51#include <linux/list.h> /* For struct list_head */52#include <linux/socket.h>53#include <linux/ip.h>54#include <linux/time.h> /* For struct timeval */55#include <linux/slab.h>56#include <net/ip.h>57#include <net/icmp.h>58#include <net/snmp.h>59#include <net/sock.h>60#include <net/xfrm.h>61#include <net/sctp/sctp.h>62#include <net/sctp/sm.h>63#include <net/sctp/checksum.h>64#include <net/net_namespace.h>6566/* Forward declarations for internal helpers. */67static int sctp_rcv_ootb(struct sk_buff *);68static struct sctp_association *__sctp_rcv_lookup(struct sk_buff *skb,69const union sctp_addr *laddr,70const union sctp_addr *paddr,71struct sctp_transport **transportp);72static struct sctp_endpoint *__sctp_rcv_lookup_endpoint(const union sctp_addr *laddr);73static struct sctp_association *__sctp_lookup_association(74const union sctp_addr *local,75const union sctp_addr *peer,76struct sctp_transport **pt);7778static int sctp_add_backlog(struct sock *sk, struct sk_buff *skb);798081/* Calculate the SCTP checksum of an SCTP packet. */82static inline int sctp_rcv_checksum(struct sk_buff *skb)83{84struct sctphdr *sh = sctp_hdr(skb);85__le32 cmp = sh->checksum;86struct sk_buff *list;87__le32 val;88__u32 tmp = sctp_start_cksum((__u8 *)sh, skb_headlen(skb));8990skb_walk_frags(skb, list)91tmp = sctp_update_cksum((__u8 *)list->data, skb_headlen(list),92tmp);9394val = sctp_end_cksum(tmp);9596if (val != cmp) {97/* CRC failure, dump it. */98SCTP_INC_STATS_BH(SCTP_MIB_CHECKSUMERRORS);99return -1;100}101return 0;102}103104struct sctp_input_cb {105union {106struct inet_skb_parm h4;107#if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)108struct inet6_skb_parm h6;109#endif110} header;111struct sctp_chunk *chunk;112};113#define SCTP_INPUT_CB(__skb) ((struct sctp_input_cb *)&((__skb)->cb[0]))114115/*116* This is the routine which IP calls when receiving an SCTP packet.117*/118int sctp_rcv(struct sk_buff *skb)119{120struct sock *sk;121struct sctp_association *asoc;122struct sctp_endpoint *ep = NULL;123struct sctp_ep_common *rcvr;124struct sctp_transport *transport = NULL;125struct sctp_chunk *chunk;126struct sctphdr *sh;127union sctp_addr src;128union sctp_addr dest;129int family;130struct sctp_af *af;131132if (skb->pkt_type!=PACKET_HOST)133goto discard_it;134135SCTP_INC_STATS_BH(SCTP_MIB_INSCTPPACKS);136137if (skb_linearize(skb))138goto discard_it;139140sh = sctp_hdr(skb);141142/* Pull up the IP and SCTP headers. */143__skb_pull(skb, skb_transport_offset(skb));144if (skb->len < sizeof(struct sctphdr))145goto discard_it;146if (!sctp_checksum_disable && !skb_csum_unnecessary(skb) &&147sctp_rcv_checksum(skb) < 0)148goto discard_it;149150skb_pull(skb, sizeof(struct sctphdr));151152/* Make sure we at least have chunk headers worth of data left. */153if (skb->len < sizeof(struct sctp_chunkhdr))154goto discard_it;155156family = ipver2af(ip_hdr(skb)->version);157af = sctp_get_af_specific(family);158if (unlikely(!af))159goto discard_it;160161/* Initialize local addresses for lookups. */162af->from_skb(&src, skb, 1);163af->from_skb(&dest, skb, 0);164165/* If the packet is to or from a non-unicast address,166* silently discard the packet.167*168* This is not clearly defined in the RFC except in section169* 8.4 - OOTB handling. However, based on the book "Stream Control170* Transmission Protocol" 2.1, "It is important to note that the171* IP address of an SCTP transport address must be a routable172* unicast address. In other words, IP multicast addresses and173* IP broadcast addresses cannot be used in an SCTP transport174* address."175*/176if (!af->addr_valid(&src, NULL, skb) ||177!af->addr_valid(&dest, NULL, skb))178goto discard_it;179180asoc = __sctp_rcv_lookup(skb, &src, &dest, &transport);181182if (!asoc)183ep = __sctp_rcv_lookup_endpoint(&dest);184185/* Retrieve the common input handling substructure. */186rcvr = asoc ? &asoc->base : &ep->base;187sk = rcvr->sk;188189/*190* If a frame arrives on an interface and the receiving socket is191* bound to another interface, via SO_BINDTODEVICE, treat it as OOTB192*/193if (sk->sk_bound_dev_if && (sk->sk_bound_dev_if != af->skb_iif(skb)))194{195if (asoc) {196sctp_association_put(asoc);197asoc = NULL;198} else {199sctp_endpoint_put(ep);200ep = NULL;201}202sk = sctp_get_ctl_sock();203ep = sctp_sk(sk)->ep;204sctp_endpoint_hold(ep);205rcvr = &ep->base;206}207208/*209* RFC 2960, 8.4 - Handle "Out of the blue" Packets.210* An SCTP packet is called an "out of the blue" (OOTB)211* packet if it is correctly formed, i.e., passed the212* receiver's checksum check, but the receiver is not213* able to identify the association to which this214* packet belongs.215*/216if (!asoc) {217if (sctp_rcv_ootb(skb)) {218SCTP_INC_STATS_BH(SCTP_MIB_OUTOFBLUES);219goto discard_release;220}221}222223if (!xfrm_policy_check(sk, XFRM_POLICY_IN, skb, family))224goto discard_release;225nf_reset(skb);226227if (sk_filter(sk, skb))228goto discard_release;229230/* Create an SCTP packet structure. */231chunk = sctp_chunkify(skb, asoc, sk);232if (!chunk)233goto discard_release;234SCTP_INPUT_CB(skb)->chunk = chunk;235236/* Remember what endpoint is to handle this packet. */237chunk->rcvr = rcvr;238239/* Remember the SCTP header. */240chunk->sctp_hdr = sh;241242/* Set the source and destination addresses of the incoming chunk. */243sctp_init_addrs(chunk, &src, &dest);244245/* Remember where we came from. */246chunk->transport = transport;247248/* Acquire access to the sock lock. Note: We are safe from other249* bottom halves on this lock, but a user may be in the lock too,250* so check if it is busy.251*/252sctp_bh_lock_sock(sk);253254if (sk != rcvr->sk) {255/* Our cached sk is different from the rcvr->sk. This is256* because migrate()/accept() may have moved the association257* to a new socket and released all the sockets. So now we258* are holding a lock on the old socket while the user may259* be doing something with the new socket. Switch our veiw260* of the current sk.261*/262sctp_bh_unlock_sock(sk);263sk = rcvr->sk;264sctp_bh_lock_sock(sk);265}266267if (sock_owned_by_user(sk)) {268if (sctp_add_backlog(sk, skb)) {269sctp_bh_unlock_sock(sk);270sctp_chunk_free(chunk);271skb = NULL; /* sctp_chunk_free already freed the skb */272goto discard_release;273}274SCTP_INC_STATS_BH(SCTP_MIB_IN_PKT_BACKLOG);275} else {276SCTP_INC_STATS_BH(SCTP_MIB_IN_PKT_SOFTIRQ);277sctp_inq_push(&chunk->rcvr->inqueue, chunk);278}279280sctp_bh_unlock_sock(sk);281282/* Release the asoc/ep ref we took in the lookup calls. */283if (asoc)284sctp_association_put(asoc);285else286sctp_endpoint_put(ep);287288return 0;289290discard_it:291SCTP_INC_STATS_BH(SCTP_MIB_IN_PKT_DISCARDS);292kfree_skb(skb);293return 0;294295discard_release:296/* Release the asoc/ep ref we took in the lookup calls. */297if (asoc)298sctp_association_put(asoc);299else300sctp_endpoint_put(ep);301302goto discard_it;303}304305/* Process the backlog queue of the socket. Every skb on306* the backlog holds a ref on an association or endpoint.307* We hold this ref throughout the state machine to make308* sure that the structure we need is still around.309*/310int sctp_backlog_rcv(struct sock *sk, struct sk_buff *skb)311{312struct sctp_chunk *chunk = SCTP_INPUT_CB(skb)->chunk;313struct sctp_inq *inqueue = &chunk->rcvr->inqueue;314struct sctp_ep_common *rcvr = NULL;315int backloged = 0;316317rcvr = chunk->rcvr;318319/* If the rcvr is dead then the association or endpoint320* has been deleted and we can safely drop the chunk321* and refs that we are holding.322*/323if (rcvr->dead) {324sctp_chunk_free(chunk);325goto done;326}327328if (unlikely(rcvr->sk != sk)) {329/* In this case, the association moved from one socket to330* another. We are currently sitting on the backlog of the331* old socket, so we need to move.332* However, since we are here in the process context we333* need to take make sure that the user doesn't own334* the new socket when we process the packet.335* If the new socket is user-owned, queue the chunk to the336* backlog of the new socket without dropping any refs.337* Otherwise, we can safely push the chunk on the inqueue.338*/339340sk = rcvr->sk;341sctp_bh_lock_sock(sk);342343if (sock_owned_by_user(sk)) {344if (sk_add_backlog(sk, skb))345sctp_chunk_free(chunk);346else347backloged = 1;348} else349sctp_inq_push(inqueue, chunk);350351sctp_bh_unlock_sock(sk);352353/* If the chunk was backloged again, don't drop refs */354if (backloged)355return 0;356} else {357sctp_inq_push(inqueue, chunk);358}359360done:361/* Release the refs we took in sctp_add_backlog */362if (SCTP_EP_TYPE_ASSOCIATION == rcvr->type)363sctp_association_put(sctp_assoc(rcvr));364else if (SCTP_EP_TYPE_SOCKET == rcvr->type)365sctp_endpoint_put(sctp_ep(rcvr));366else367BUG();368369return 0;370}371372static int sctp_add_backlog(struct sock *sk, struct sk_buff *skb)373{374struct sctp_chunk *chunk = SCTP_INPUT_CB(skb)->chunk;375struct sctp_ep_common *rcvr = chunk->rcvr;376int ret;377378ret = sk_add_backlog(sk, skb);379if (!ret) {380/* Hold the assoc/ep while hanging on the backlog queue.381* This way, we know structures we need will not disappear382* from us383*/384if (SCTP_EP_TYPE_ASSOCIATION == rcvr->type)385sctp_association_hold(sctp_assoc(rcvr));386else if (SCTP_EP_TYPE_SOCKET == rcvr->type)387sctp_endpoint_hold(sctp_ep(rcvr));388else389BUG();390}391return ret;392393}394395/* Handle icmp frag needed error. */396void sctp_icmp_frag_needed(struct sock *sk, struct sctp_association *asoc,397struct sctp_transport *t, __u32 pmtu)398{399if (!t || (t->pathmtu <= pmtu))400return;401402if (sock_owned_by_user(sk)) {403asoc->pmtu_pending = 1;404t->pmtu_pending = 1;405return;406}407408if (t->param_flags & SPP_PMTUD_ENABLE) {409/* Update transports view of the MTU */410sctp_transport_update_pmtu(t, pmtu);411412/* Update association pmtu. */413sctp_assoc_sync_pmtu(asoc);414}415416/* Retransmit with the new pmtu setting.417* Normally, if PMTU discovery is disabled, an ICMP Fragmentation418* Needed will never be sent, but if a message was sent before419* PMTU discovery was disabled that was larger than the PMTU, it420* would not be fragmented, so it must be re-transmitted fragmented.421*/422sctp_retransmit(&asoc->outqueue, t, SCTP_RTXR_PMTUD);423}424425/*426* SCTP Implementer's Guide, 2.37 ICMP handling procedures427*428* ICMP8) If the ICMP code is a "Unrecognized next header type encountered"429* or a "Protocol Unreachable" treat this message as an abort430* with the T bit set.431*432* This function sends an event to the state machine, which will abort the433* association.434*435*/436void sctp_icmp_proto_unreachable(struct sock *sk,437struct sctp_association *asoc,438struct sctp_transport *t)439{440SCTP_DEBUG_PRINTK("%s\n", __func__);441442if (sock_owned_by_user(sk)) {443if (timer_pending(&t->proto_unreach_timer))444return;445else {446if (!mod_timer(&t->proto_unreach_timer,447jiffies + (HZ/20)))448sctp_association_hold(asoc);449}450451} else {452if (timer_pending(&t->proto_unreach_timer) &&453del_timer(&t->proto_unreach_timer))454sctp_association_put(asoc);455456sctp_do_sm(SCTP_EVENT_T_OTHER,457SCTP_ST_OTHER(SCTP_EVENT_ICMP_PROTO_UNREACH),458asoc->state, asoc->ep, asoc, t,459GFP_ATOMIC);460}461}462463/* Common lookup code for icmp/icmpv6 error handler. */464struct sock *sctp_err_lookup(int family, struct sk_buff *skb,465struct sctphdr *sctphdr,466struct sctp_association **app,467struct sctp_transport **tpp)468{469union sctp_addr saddr;470union sctp_addr daddr;471struct sctp_af *af;472struct sock *sk = NULL;473struct sctp_association *asoc;474struct sctp_transport *transport = NULL;475struct sctp_init_chunk *chunkhdr;476__u32 vtag = ntohl(sctphdr->vtag);477int len = skb->len - ((void *)sctphdr - (void *)skb->data);478479*app = NULL; *tpp = NULL;480481af = sctp_get_af_specific(family);482if (unlikely(!af)) {483return NULL;484}485486/* Initialize local addresses for lookups. */487af->from_skb(&saddr, skb, 1);488af->from_skb(&daddr, skb, 0);489490/* Look for an association that matches the incoming ICMP error491* packet.492*/493asoc = __sctp_lookup_association(&saddr, &daddr, &transport);494if (!asoc)495return NULL;496497sk = asoc->base.sk;498499/* RFC 4960, Appendix C. ICMP Handling500*501* ICMP6) An implementation MUST validate that the Verification Tag502* contained in the ICMP message matches the Verification Tag of503* the peer. If the Verification Tag is not 0 and does NOT504* match, discard the ICMP message. If it is 0 and the ICMP505* message contains enough bytes to verify that the chunk type is506* an INIT chunk and that the Initiate Tag matches the tag of the507* peer, continue with ICMP7. If the ICMP message is too short508* or the chunk type or the Initiate Tag does not match, silently509* discard the packet.510*/511if (vtag == 0) {512chunkhdr = (struct sctp_init_chunk *)((void *)sctphdr513+ sizeof(struct sctphdr));514if (len < sizeof(struct sctphdr) + sizeof(sctp_chunkhdr_t)515+ sizeof(__be32) ||516chunkhdr->chunk_hdr.type != SCTP_CID_INIT ||517ntohl(chunkhdr->init_hdr.init_tag) != asoc->c.my_vtag) {518goto out;519}520} else if (vtag != asoc->c.peer_vtag) {521goto out;522}523524sctp_bh_lock_sock(sk);525526/* If too many ICMPs get dropped on busy527* servers this needs to be solved differently.528*/529if (sock_owned_by_user(sk))530NET_INC_STATS_BH(&init_net, LINUX_MIB_LOCKDROPPEDICMPS);531532*app = asoc;533*tpp = transport;534return sk;535536out:537if (asoc)538sctp_association_put(asoc);539return NULL;540}541542/* Common cleanup code for icmp/icmpv6 error handler. */543void sctp_err_finish(struct sock *sk, struct sctp_association *asoc)544{545sctp_bh_unlock_sock(sk);546if (asoc)547sctp_association_put(asoc);548}549550/*551* This routine is called by the ICMP module when it gets some552* sort of error condition. If err < 0 then the socket should553* be closed and the error returned to the user. If err > 0554* it's just the icmp type << 8 | icmp code. After adjustment555* header points to the first 8 bytes of the sctp header. We need556* to find the appropriate port.557*558* The locking strategy used here is very "optimistic". When559* someone else accesses the socket the ICMP is just dropped560* and for some paths there is no check at all.561* A more general error queue to queue errors for later handling562* is probably better.563*564*/565void sctp_v4_err(struct sk_buff *skb, __u32 info)566{567const struct iphdr *iph = (const struct iphdr *)skb->data;568const int ihlen = iph->ihl * 4;569const int type = icmp_hdr(skb)->type;570const int code = icmp_hdr(skb)->code;571struct sock *sk;572struct sctp_association *asoc = NULL;573struct sctp_transport *transport;574struct inet_sock *inet;575sk_buff_data_t saveip, savesctp;576int err;577578if (skb->len < ihlen + 8) {579ICMP_INC_STATS_BH(&init_net, ICMP_MIB_INERRORS);580return;581}582583/* Fix up skb to look at the embedded net header. */584saveip = skb->network_header;585savesctp = skb->transport_header;586skb_reset_network_header(skb);587skb_set_transport_header(skb, ihlen);588sk = sctp_err_lookup(AF_INET, skb, sctp_hdr(skb), &asoc, &transport);589/* Put back, the original values. */590skb->network_header = saveip;591skb->transport_header = savesctp;592if (!sk) {593ICMP_INC_STATS_BH(&init_net, ICMP_MIB_INERRORS);594return;595}596/* Warning: The sock lock is held. Remember to call597* sctp_err_finish!598*/599600switch (type) {601case ICMP_PARAMETERPROB:602err = EPROTO;603break;604case ICMP_DEST_UNREACH:605if (code > NR_ICMP_UNREACH)606goto out_unlock;607608/* PMTU discovery (RFC1191) */609if (ICMP_FRAG_NEEDED == code) {610sctp_icmp_frag_needed(sk, asoc, transport, info);611goto out_unlock;612}613else {614if (ICMP_PROT_UNREACH == code) {615sctp_icmp_proto_unreachable(sk, asoc,616transport);617goto out_unlock;618}619}620err = icmp_err_convert[code].errno;621break;622case ICMP_TIME_EXCEEDED:623/* Ignore any time exceeded errors due to fragment reassembly624* timeouts.625*/626if (ICMP_EXC_FRAGTIME == code)627goto out_unlock;628629err = EHOSTUNREACH;630break;631default:632goto out_unlock;633}634635inet = inet_sk(sk);636if (!sock_owned_by_user(sk) && inet->recverr) {637sk->sk_err = err;638sk->sk_error_report(sk);639} else { /* Only an error on timeout */640sk->sk_err_soft = err;641}642643out_unlock:644sctp_err_finish(sk, asoc);645}646647/*648* RFC 2960, 8.4 - Handle "Out of the blue" Packets.649*650* This function scans all the chunks in the OOTB packet to determine if651* the packet should be discarded right away. If a response might be needed652* for this packet, or, if further processing is possible, the packet will653* be queued to a proper inqueue for the next phase of handling.654*655* Output:656* Return 0 - If further processing is needed.657* Return 1 - If the packet can be discarded right away.658*/659static int sctp_rcv_ootb(struct sk_buff *skb)660{661sctp_chunkhdr_t *ch;662__u8 *ch_end;663664ch = (sctp_chunkhdr_t *) skb->data;665666/* Scan through all the chunks in the packet. */667do {668/* Break out if chunk length is less then minimal. */669if (ntohs(ch->length) < sizeof(sctp_chunkhdr_t))670break;671672ch_end = ((__u8 *)ch) + WORD_ROUND(ntohs(ch->length));673if (ch_end > skb_tail_pointer(skb))674break;675676/* RFC 8.4, 2) If the OOTB packet contains an ABORT chunk, the677* receiver MUST silently discard the OOTB packet and take no678* further action.679*/680if (SCTP_CID_ABORT == ch->type)681goto discard;682683/* RFC 8.4, 6) If the packet contains a SHUTDOWN COMPLETE684* chunk, the receiver should silently discard the packet685* and take no further action.686*/687if (SCTP_CID_SHUTDOWN_COMPLETE == ch->type)688goto discard;689690/* RFC 4460, 2.11.2691* This will discard packets with INIT chunk bundled as692* subsequent chunks in the packet. When INIT is first,693* the normal INIT processing will discard the chunk.694*/695if (SCTP_CID_INIT == ch->type && (void *)ch != skb->data)696goto discard;697698ch = (sctp_chunkhdr_t *) ch_end;699} while (ch_end < skb_tail_pointer(skb));700701return 0;702703discard:704return 1;705}706707/* Insert endpoint into the hash table. */708static void __sctp_hash_endpoint(struct sctp_endpoint *ep)709{710struct sctp_ep_common *epb;711struct sctp_hashbucket *head;712713epb = &ep->base;714715epb->hashent = sctp_ep_hashfn(epb->bind_addr.port);716head = &sctp_ep_hashtable[epb->hashent];717718sctp_write_lock(&head->lock);719hlist_add_head(&epb->node, &head->chain);720sctp_write_unlock(&head->lock);721}722723/* Add an endpoint to the hash. Local BH-safe. */724void sctp_hash_endpoint(struct sctp_endpoint *ep)725{726sctp_local_bh_disable();727__sctp_hash_endpoint(ep);728sctp_local_bh_enable();729}730731/* Remove endpoint from the hash table. */732static void __sctp_unhash_endpoint(struct sctp_endpoint *ep)733{734struct sctp_hashbucket *head;735struct sctp_ep_common *epb;736737epb = &ep->base;738739if (hlist_unhashed(&epb->node))740return;741742epb->hashent = sctp_ep_hashfn(epb->bind_addr.port);743744head = &sctp_ep_hashtable[epb->hashent];745746sctp_write_lock(&head->lock);747__hlist_del(&epb->node);748sctp_write_unlock(&head->lock);749}750751/* Remove endpoint from the hash. Local BH-safe. */752void sctp_unhash_endpoint(struct sctp_endpoint *ep)753{754sctp_local_bh_disable();755__sctp_unhash_endpoint(ep);756sctp_local_bh_enable();757}758759/* Look up an endpoint. */760static struct sctp_endpoint *__sctp_rcv_lookup_endpoint(const union sctp_addr *laddr)761{762struct sctp_hashbucket *head;763struct sctp_ep_common *epb;764struct sctp_endpoint *ep;765struct hlist_node *node;766int hash;767768hash = sctp_ep_hashfn(ntohs(laddr->v4.sin_port));769head = &sctp_ep_hashtable[hash];770read_lock(&head->lock);771sctp_for_each_hentry(epb, node, &head->chain) {772ep = sctp_ep(epb);773if (sctp_endpoint_is_match(ep, laddr))774goto hit;775}776777ep = sctp_sk((sctp_get_ctl_sock()))->ep;778779hit:780sctp_endpoint_hold(ep);781read_unlock(&head->lock);782return ep;783}784785/* Insert association into the hash table. */786static void __sctp_hash_established(struct sctp_association *asoc)787{788struct sctp_ep_common *epb;789struct sctp_hashbucket *head;790791epb = &asoc->base;792793/* Calculate which chain this entry will belong to. */794epb->hashent = sctp_assoc_hashfn(epb->bind_addr.port, asoc->peer.port);795796head = &sctp_assoc_hashtable[epb->hashent];797798sctp_write_lock(&head->lock);799hlist_add_head(&epb->node, &head->chain);800sctp_write_unlock(&head->lock);801}802803/* Add an association to the hash. Local BH-safe. */804void sctp_hash_established(struct sctp_association *asoc)805{806if (asoc->temp)807return;808809sctp_local_bh_disable();810__sctp_hash_established(asoc);811sctp_local_bh_enable();812}813814/* Remove association from the hash table. */815static void __sctp_unhash_established(struct sctp_association *asoc)816{817struct sctp_hashbucket *head;818struct sctp_ep_common *epb;819820epb = &asoc->base;821822epb->hashent = sctp_assoc_hashfn(epb->bind_addr.port,823asoc->peer.port);824825head = &sctp_assoc_hashtable[epb->hashent];826827sctp_write_lock(&head->lock);828__hlist_del(&epb->node);829sctp_write_unlock(&head->lock);830}831832/* Remove association from the hash table. Local BH-safe. */833void sctp_unhash_established(struct sctp_association *asoc)834{835if (asoc->temp)836return;837838sctp_local_bh_disable();839__sctp_unhash_established(asoc);840sctp_local_bh_enable();841}842843/* Look up an association. */844static struct sctp_association *__sctp_lookup_association(845const union sctp_addr *local,846const union sctp_addr *peer,847struct sctp_transport **pt)848{849struct sctp_hashbucket *head;850struct sctp_ep_common *epb;851struct sctp_association *asoc;852struct sctp_transport *transport;853struct hlist_node *node;854int hash;855856/* Optimize here for direct hit, only listening connections can857* have wildcards anyways.858*/859hash = sctp_assoc_hashfn(ntohs(local->v4.sin_port), ntohs(peer->v4.sin_port));860head = &sctp_assoc_hashtable[hash];861read_lock(&head->lock);862sctp_for_each_hentry(epb, node, &head->chain) {863asoc = sctp_assoc(epb);864transport = sctp_assoc_is_match(asoc, local, peer);865if (transport)866goto hit;867}868869read_unlock(&head->lock);870871return NULL;872873hit:874*pt = transport;875sctp_association_hold(asoc);876read_unlock(&head->lock);877return asoc;878}879880/* Look up an association. BH-safe. */881SCTP_STATIC882struct sctp_association *sctp_lookup_association(const union sctp_addr *laddr,883const union sctp_addr *paddr,884struct sctp_transport **transportp)885{886struct sctp_association *asoc;887888sctp_local_bh_disable();889asoc = __sctp_lookup_association(laddr, paddr, transportp);890sctp_local_bh_enable();891892return asoc;893}894895/* Is there an association matching the given local and peer addresses? */896int sctp_has_association(const union sctp_addr *laddr,897const union sctp_addr *paddr)898{899struct sctp_association *asoc;900struct sctp_transport *transport;901902if ((asoc = sctp_lookup_association(laddr, paddr, &transport))) {903sctp_association_put(asoc);904return 1;905}906907return 0;908}909910/*911* SCTP Implementors Guide, 2.18 Handling of address912* parameters within the INIT or INIT-ACK.913*914* D) When searching for a matching TCB upon reception of an INIT915* or INIT-ACK chunk the receiver SHOULD use not only the916* source address of the packet (containing the INIT or917* INIT-ACK) but the receiver SHOULD also use all valid918* address parameters contained within the chunk.919*920* 2.18.3 Solution description921*922* This new text clearly specifies to an implementor the need923* to look within the INIT or INIT-ACK. Any implementation that924* does not do this, may not be able to establish associations925* in certain circumstances.926*927*/928static struct sctp_association *__sctp_rcv_init_lookup(struct sk_buff *skb,929const union sctp_addr *laddr, struct sctp_transport **transportp)930{931struct sctp_association *asoc;932union sctp_addr addr;933union sctp_addr *paddr = &addr;934struct sctphdr *sh = sctp_hdr(skb);935union sctp_params params;936sctp_init_chunk_t *init;937struct sctp_transport *transport;938struct sctp_af *af;939940/*941* This code will NOT touch anything inside the chunk--it is942* strictly READ-ONLY.943*944* RFC 2960 3 SCTP packet Format945*946* Multiple chunks can be bundled into one SCTP packet up to947* the MTU size, except for the INIT, INIT ACK, and SHUTDOWN948* COMPLETE chunks. These chunks MUST NOT be bundled with any949* other chunk in a packet. See Section 6.10 for more details950* on chunk bundling.951*/952953/* Find the start of the TLVs and the end of the chunk. This is954* the region we search for address parameters.955*/956init = (sctp_init_chunk_t *)skb->data;957958/* Walk the parameters looking for embedded addresses. */959sctp_walk_params(params, init, init_hdr.params) {960961/* Note: Ignoring hostname addresses. */962af = sctp_get_af_specific(param_type2af(params.p->type));963if (!af)964continue;965966af->from_addr_param(paddr, params.addr, sh->source, 0);967968asoc = __sctp_lookup_association(laddr, paddr, &transport);969if (asoc)970return asoc;971}972973return NULL;974}975976/* ADD-IP, Section 5.2977* When an endpoint receives an ASCONF Chunk from the remote peer978* special procedures may be needed to identify the association the979* ASCONF Chunk is associated with. To properly find the association980* the following procedures SHOULD be followed:981*982* D2) If the association is not found, use the address found in the983* Address Parameter TLV combined with the port number found in the984* SCTP common header. If found proceed to rule D4.985*986* D2-ext) If more than one ASCONF Chunks are packed together, use the987* address found in the ASCONF Address Parameter TLV of each of the988* subsequent ASCONF Chunks. If found, proceed to rule D4.989*/990static struct sctp_association *__sctp_rcv_asconf_lookup(991sctp_chunkhdr_t *ch,992const union sctp_addr *laddr,993__be16 peer_port,994struct sctp_transport **transportp)995{996sctp_addip_chunk_t *asconf = (struct sctp_addip_chunk *)ch;997struct sctp_af *af;998union sctp_addr_param *param;999union sctp_addr paddr;10001001/* Skip over the ADDIP header and find the Address parameter */1002param = (union sctp_addr_param *)(asconf + 1);10031004af = sctp_get_af_specific(param_type2af(param->p.type));1005if (unlikely(!af))1006return NULL;10071008af->from_addr_param(&paddr, param, peer_port, 0);10091010return __sctp_lookup_association(laddr, &paddr, transportp);1011}101210131014/* SCTP-AUTH, Section 6.3:1015* If the receiver does not find a STCB for a packet containing an AUTH1016* chunk as the first chunk and not a COOKIE-ECHO chunk as the second1017* chunk, it MUST use the chunks after the AUTH chunk to look up an existing1018* association.1019*1020* This means that any chunks that can help us identify the association need1021* to be looked at to find this association.1022*/1023static struct sctp_association *__sctp_rcv_walk_lookup(struct sk_buff *skb,1024const union sctp_addr *laddr,1025struct sctp_transport **transportp)1026{1027struct sctp_association *asoc = NULL;1028sctp_chunkhdr_t *ch;1029int have_auth = 0;1030unsigned int chunk_num = 1;1031__u8 *ch_end;10321033/* Walk through the chunks looking for AUTH or ASCONF chunks1034* to help us find the association.1035*/1036ch = (sctp_chunkhdr_t *) skb->data;1037do {1038/* Break out if chunk length is less then minimal. */1039if (ntohs(ch->length) < sizeof(sctp_chunkhdr_t))1040break;10411042ch_end = ((__u8 *)ch) + WORD_ROUND(ntohs(ch->length));1043if (ch_end > skb_tail_pointer(skb))1044break;10451046switch(ch->type) {1047case SCTP_CID_AUTH:1048have_auth = chunk_num;1049break;10501051case SCTP_CID_COOKIE_ECHO:1052/* If a packet arrives containing an AUTH chunk as1053* a first chunk, a COOKIE-ECHO chunk as the second1054* chunk, and possibly more chunks after them, and1055* the receiver does not have an STCB for that1056* packet, then authentication is based on1057* the contents of the COOKIE- ECHO chunk.1058*/1059if (have_auth == 1 && chunk_num == 2)1060return NULL;1061break;10621063case SCTP_CID_ASCONF:1064if (have_auth || sctp_addip_noauth)1065asoc = __sctp_rcv_asconf_lookup(ch, laddr,1066sctp_hdr(skb)->source,1067transportp);1068default:1069break;1070}10711072if (asoc)1073break;10741075ch = (sctp_chunkhdr_t *) ch_end;1076chunk_num++;1077} while (ch_end < skb_tail_pointer(skb));10781079return asoc;1080}10811082/*1083* There are circumstances when we need to look inside the SCTP packet1084* for information to help us find the association. Examples1085* include looking inside of INIT/INIT-ACK chunks or after the AUTH1086* chunks.1087*/1088static struct sctp_association *__sctp_rcv_lookup_harder(struct sk_buff *skb,1089const union sctp_addr *laddr,1090struct sctp_transport **transportp)1091{1092sctp_chunkhdr_t *ch;10931094ch = (sctp_chunkhdr_t *) skb->data;10951096/* The code below will attempt to walk the chunk and extract1097* parameter information. Before we do that, we need to verify1098* that the chunk length doesn't cause overflow. Otherwise, we'll1099* walk off the end.1100*/1101if (WORD_ROUND(ntohs(ch->length)) > skb->len)1102return NULL;11031104/* If this is INIT/INIT-ACK look inside the chunk too. */1105switch (ch->type) {1106case SCTP_CID_INIT:1107case SCTP_CID_INIT_ACK:1108return __sctp_rcv_init_lookup(skb, laddr, transportp);1109break;11101111default:1112return __sctp_rcv_walk_lookup(skb, laddr, transportp);1113break;1114}111511161117return NULL;1118}11191120/* Lookup an association for an inbound skb. */1121static struct sctp_association *__sctp_rcv_lookup(struct sk_buff *skb,1122const union sctp_addr *paddr,1123const union sctp_addr *laddr,1124struct sctp_transport **transportp)1125{1126struct sctp_association *asoc;11271128asoc = __sctp_lookup_association(laddr, paddr, transportp);11291130/* Further lookup for INIT/INIT-ACK packets.1131* SCTP Implementors Guide, 2.18 Handling of address1132* parameters within the INIT or INIT-ACK.1133*/1134if (!asoc)1135asoc = __sctp_rcv_lookup_harder(skb, laddr, transportp);11361137return asoc;1138}113911401141