123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230 |
- #include <linux/types.h>
- #include <linux/list.h> /* For struct list_head */
- #include <linux/socket.h>
- #include <linux/ip.h>
- #include <linux/time.h> /* For struct timeval */
- #include <linux/slab.h>
- #include <net/ip.h>
- #include <net/icmp.h>
- #include <net/snmp.h>
- #include <net/sock.h>
- #include <net/xfrm.h>
- #include <net/sctp/sctp.h>
- #include <net/sctp/sm.h>
- #include <net/sctp/checksum.h>
- #include <net/net_namespace.h>
- static int sctp_rcv_ootb(struct sk_buff *);
- static struct sctp_association *__sctp_rcv_lookup(struct net *net,
- struct sk_buff *skb,
- const union sctp_addr *paddr,
- const union sctp_addr *laddr,
- struct sctp_transport **transportp);
- static struct sctp_endpoint *__sctp_rcv_lookup_endpoint(struct net *net,
- const union sctp_addr *laddr);
- static struct sctp_association *__sctp_lookup_association(
- struct net *net,
- const union sctp_addr *local,
- const union sctp_addr *peer,
- struct sctp_transport **pt);
- static int sctp_add_backlog(struct sock *sk, struct sk_buff *skb);
- static inline int sctp_rcv_checksum(struct net *net, struct sk_buff *skb)
- {
- struct sctphdr *sh = sctp_hdr(skb);
- __le32 cmp = sh->checksum;
- __le32 val = sctp_compute_cksum(skb, 0);
- if (val != cmp) {
-
- __SCTP_INC_STATS(net, SCTP_MIB_CHECKSUMERRORS);
- return -1;
- }
- return 0;
- }
- int sctp_rcv(struct sk_buff *skb)
- {
- struct sock *sk;
- struct sctp_association *asoc;
- struct sctp_endpoint *ep = NULL;
- struct sctp_ep_common *rcvr;
- struct sctp_transport *transport = NULL;
- struct sctp_chunk *chunk;
- union sctp_addr src;
- union sctp_addr dest;
- int family;
- struct sctp_af *af;
- struct net *net = dev_net(skb->dev);
- if (skb->pkt_type != PACKET_HOST)
- goto discard_it;
- __SCTP_INC_STATS(net, SCTP_MIB_INSCTPPACKS);
-
- if (skb->len < sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr) +
- skb_transport_offset(skb))
- goto discard_it;
-
- if ((!(skb_shinfo(skb)->gso_type & SKB_GSO_SCTP) &&
- skb_linearize(skb)) ||
- !pskb_may_pull(skb, sizeof(struct sctphdr)))
- goto discard_it;
-
- __skb_pull(skb, skb_transport_offset(skb));
- skb->csum_valid = 0;
- if (skb_csum_unnecessary(skb))
- __skb_decr_checksum_unnecessary(skb);
- else if (!sctp_checksum_disable &&
- !(skb_shinfo(skb)->gso_type & SKB_GSO_SCTP) &&
- sctp_rcv_checksum(net, skb) < 0)
- goto discard_it;
- skb->csum_valid = 1;
- __skb_pull(skb, sizeof(struct sctphdr));
- family = ipver2af(ip_hdr(skb)->version);
- af = sctp_get_af_specific(family);
- if (unlikely(!af))
- goto discard_it;
- SCTP_INPUT_CB(skb)->af = af;
-
- af->from_skb(&src, skb, 1);
- af->from_skb(&dest, skb, 0);
-
- if (!af->addr_valid(&src, NULL, skb) ||
- !af->addr_valid(&dest, NULL, skb))
- goto discard_it;
- asoc = __sctp_rcv_lookup(net, skb, &src, &dest, &transport);
- if (!asoc)
- ep = __sctp_rcv_lookup_endpoint(net, &dest);
-
- rcvr = asoc ? &asoc->base : &ep->base;
- sk = rcvr->sk;
-
- if (sk->sk_bound_dev_if && (sk->sk_bound_dev_if != af->skb_iif(skb))) {
- if (transport) {
- sctp_transport_put(transport);
- asoc = NULL;
- transport = NULL;
- } else {
- sctp_endpoint_put(ep);
- ep = NULL;
- }
- sk = net->sctp.ctl_sock;
- ep = sctp_sk(sk)->ep;
- sctp_endpoint_hold(ep);
- rcvr = &ep->base;
- }
-
- if (!asoc) {
- if (sctp_rcv_ootb(skb)) {
- __SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
- goto discard_release;
- }
- }
- if (!xfrm_policy_check(sk, XFRM_POLICY_IN, skb, family))
- goto discard_release;
- nf_reset(skb);
- if (sk_filter(sk, skb))
- goto discard_release;
-
- chunk = sctp_chunkify(skb, asoc, sk, GFP_ATOMIC);
- if (!chunk)
- goto discard_release;
- SCTP_INPUT_CB(skb)->chunk = chunk;
-
- chunk->rcvr = rcvr;
-
- chunk->sctp_hdr = sctp_hdr(skb);
-
- sctp_init_addrs(chunk, &src, &dest);
-
- chunk->transport = transport;
-
- bh_lock_sock(sk);
- if (sk != rcvr->sk) {
-
- bh_unlock_sock(sk);
- sk = rcvr->sk;
- bh_lock_sock(sk);
- }
- if (sock_owned_by_user(sk)) {
- if (sctp_add_backlog(sk, skb)) {
- bh_unlock_sock(sk);
- sctp_chunk_free(chunk);
- skb = NULL;
- goto discard_release;
- }
- __SCTP_INC_STATS(net, SCTP_MIB_IN_PKT_BACKLOG);
- } else {
- __SCTP_INC_STATS(net, SCTP_MIB_IN_PKT_SOFTIRQ);
- sctp_inq_push(&chunk->rcvr->inqueue, chunk);
- }
- bh_unlock_sock(sk);
-
- if (transport)
- sctp_transport_put(transport);
- else
- sctp_endpoint_put(ep);
- return 0;
- discard_it:
- __SCTP_INC_STATS(net, SCTP_MIB_IN_PKT_DISCARDS);
- kfree_skb(skb);
- return 0;
- discard_release:
-
- if (transport)
- sctp_transport_put(transport);
- else
- sctp_endpoint_put(ep);
- goto discard_it;
- }
- int sctp_backlog_rcv(struct sock *sk, struct sk_buff *skb)
- {
- struct sctp_chunk *chunk = SCTP_INPUT_CB(skb)->chunk;
- struct sctp_inq *inqueue = &chunk->rcvr->inqueue;
- struct sctp_transport *t = chunk->transport;
- struct sctp_ep_common *rcvr = NULL;
- int backloged = 0;
- rcvr = chunk->rcvr;
-
- if (rcvr->dead) {
- sctp_chunk_free(chunk);
- goto done;
- }
- if (unlikely(rcvr->sk != sk)) {
-
- sk = rcvr->sk;
- local_bh_disable();
- bh_lock_sock(sk);
- if (sock_owned_by_user(sk)) {
- if (sk_add_backlog(sk, skb, sk->sk_rcvbuf))
- sctp_chunk_free(chunk);
- else
- backloged = 1;
- } else
- sctp_inq_push(inqueue, chunk);
- bh_unlock_sock(sk);
- local_bh_enable();
-
- if (backloged)
- return 0;
- } else {
- sctp_inq_push(inqueue, chunk);
- }
- done:
-
- if (SCTP_EP_TYPE_ASSOCIATION == rcvr->type)
- sctp_transport_put(t);
- else if (SCTP_EP_TYPE_SOCKET == rcvr->type)
- sctp_endpoint_put(sctp_ep(rcvr));
- else
- BUG();
- return 0;
- }
- static int sctp_add_backlog(struct sock *sk, struct sk_buff *skb)
- {
- struct sctp_chunk *chunk = SCTP_INPUT_CB(skb)->chunk;
- struct sctp_transport *t = chunk->transport;
- struct sctp_ep_common *rcvr = chunk->rcvr;
- int ret;
- ret = sk_add_backlog(sk, skb, sk->sk_rcvbuf);
- if (!ret) {
-
- if (SCTP_EP_TYPE_ASSOCIATION == rcvr->type)
- sctp_transport_hold(t);
- else if (SCTP_EP_TYPE_SOCKET == rcvr->type)
- sctp_endpoint_hold(sctp_ep(rcvr));
- else
- BUG();
- }
- return ret;
- }
- void sctp_icmp_frag_needed(struct sock *sk, struct sctp_association *asoc,
- struct sctp_transport *t, __u32 pmtu)
- {
- if (!t || (t->pathmtu <= pmtu))
- return;
- if (sock_owned_by_user(sk)) {
- asoc->pmtu_pending = 1;
- t->pmtu_pending = 1;
- return;
- }
- if (t->param_flags & SPP_PMTUD_ENABLE) {
-
- sctp_transport_update_pmtu(sk, t, pmtu);
-
- sctp_assoc_sync_pmtu(sk, asoc);
- }
-
- sctp_retransmit(&asoc->outqueue, t, SCTP_RTXR_PMTUD);
- }
- void sctp_icmp_redirect(struct sock *sk, struct sctp_transport *t,
- struct sk_buff *skb)
- {
- struct dst_entry *dst;
- if (!t)
- return;
- dst = sctp_transport_dst_check(t);
- if (dst)
- dst->ops->redirect(dst, sk, skb);
- }
- void sctp_icmp_proto_unreachable(struct sock *sk,
- struct sctp_association *asoc,
- struct sctp_transport *t)
- {
- if (sock_owned_by_user(sk)) {
- if (timer_pending(&t->proto_unreach_timer))
- return;
- else {
- if (!mod_timer(&t->proto_unreach_timer,
- jiffies + (HZ/20)))
- sctp_association_hold(asoc);
- }
- } else {
- struct net *net = sock_net(sk);
- pr_debug("%s: unrecognized next header type "
- "encountered!\n", __func__);
- if (del_timer(&t->proto_unreach_timer))
- sctp_association_put(asoc);
- sctp_do_sm(net, SCTP_EVENT_T_OTHER,
- SCTP_ST_OTHER(SCTP_EVENT_ICMP_PROTO_UNREACH),
- asoc->state, asoc->ep, asoc, t,
- GFP_ATOMIC);
- }
- }
- struct sock *sctp_err_lookup(struct net *net, int family, struct sk_buff *skb,
- struct sctphdr *sctphdr,
- struct sctp_association **app,
- struct sctp_transport **tpp)
- {
- struct sctp_init_chunk *chunkhdr, _chunkhdr;
- union sctp_addr saddr;
- union sctp_addr daddr;
- struct sctp_af *af;
- struct sock *sk = NULL;
- struct sctp_association *asoc;
- struct sctp_transport *transport = NULL;
- __u32 vtag = ntohl(sctphdr->vtag);
- *app = NULL; *tpp = NULL;
- af = sctp_get_af_specific(family);
- if (unlikely(!af)) {
- return NULL;
- }
-
- af->from_skb(&saddr, skb, 1);
- af->from_skb(&daddr, skb, 0);
-
- asoc = __sctp_lookup_association(net, &saddr, &daddr, &transport);
- if (!asoc)
- return NULL;
- sk = asoc->base.sk;
-
- if (vtag == 0) {
-
- chunkhdr = skb_header_pointer(skb, skb_transport_offset(skb) +
- sizeof(struct sctphdr),
- sizeof(struct sctp_chunkhdr) +
- sizeof(__be32), &_chunkhdr);
- if (!chunkhdr ||
- chunkhdr->chunk_hdr.type != SCTP_CID_INIT ||
- ntohl(chunkhdr->init_hdr.init_tag) != asoc->c.my_vtag)
- goto out;
- } else if (vtag != asoc->c.peer_vtag) {
- goto out;
- }
- bh_lock_sock(sk);
-
- if (sock_owned_by_user(sk))
- __NET_INC_STATS(net, LINUX_MIB_LOCKDROPPEDICMPS);
- *app = asoc;
- *tpp = transport;
- return sk;
- out:
- sctp_transport_put(transport);
- return NULL;
- }
- void sctp_err_finish(struct sock *sk, struct sctp_transport *t)
- {
- bh_unlock_sock(sk);
- sctp_transport_put(t);
- }
- void sctp_v4_err(struct sk_buff *skb, __u32 info)
- {
- const struct iphdr *iph = (const struct iphdr *)skb->data;
- const int ihlen = iph->ihl * 4;
- const int type = icmp_hdr(skb)->type;
- const int code = icmp_hdr(skb)->code;
- struct sock *sk;
- struct sctp_association *asoc = NULL;
- struct sctp_transport *transport;
- struct inet_sock *inet;
- __u16 saveip, savesctp;
- int err;
- struct net *net = dev_net(skb->dev);
-
- saveip = skb->network_header;
- savesctp = skb->transport_header;
- skb_reset_network_header(skb);
- skb_set_transport_header(skb, ihlen);
- sk = sctp_err_lookup(net, AF_INET, skb, sctp_hdr(skb), &asoc, &transport);
-
- skb->network_header = saveip;
- skb->transport_header = savesctp;
- if (!sk) {
- __ICMP_INC_STATS(net, ICMP_MIB_INERRORS);
- return;
- }
-
- switch (type) {
- case ICMP_PARAMETERPROB:
- err = EPROTO;
- break;
- case ICMP_DEST_UNREACH:
- if (code > NR_ICMP_UNREACH)
- goto out_unlock;
-
- if (ICMP_FRAG_NEEDED == code) {
- sctp_icmp_frag_needed(sk, asoc, transport,
- SCTP_TRUNC4(info));
- goto out_unlock;
- } else {
- if (ICMP_PROT_UNREACH == code) {
- sctp_icmp_proto_unreachable(sk, asoc,
- transport);
- goto out_unlock;
- }
- }
- err = icmp_err_convert[code].errno;
- break;
- case ICMP_TIME_EXCEEDED:
-
- if (ICMP_EXC_FRAGTIME == code)
- goto out_unlock;
- err = EHOSTUNREACH;
- break;
- case ICMP_REDIRECT:
- sctp_icmp_redirect(sk, transport, skb);
-
- default:
- goto out_unlock;
- }
- inet = inet_sk(sk);
- if (!sock_owned_by_user(sk) && inet->recverr) {
- sk->sk_err = err;
- sk->sk_error_report(sk);
- } else {
- sk->sk_err_soft = err;
- }
- out_unlock:
- sctp_err_finish(sk, transport);
- }
- static int sctp_rcv_ootb(struct sk_buff *skb)
- {
- sctp_chunkhdr_t *ch, _ch;
- int ch_end, offset = 0;
-
- do {
-
- if (offset + sizeof(sctp_chunkhdr_t) > skb->len)
- break;
- ch = skb_header_pointer(skb, offset, sizeof(*ch), &_ch);
-
- if (ntohs(ch->length) < sizeof(sctp_chunkhdr_t))
- break;
- ch_end = offset + SCTP_PAD4(ntohs(ch->length));
- if (ch_end > skb->len)
- break;
-
- if (SCTP_CID_ABORT == ch->type)
- goto discard;
-
- if (SCTP_CID_SHUTDOWN_COMPLETE == ch->type)
- goto discard;
-
- if (SCTP_CID_INIT == ch->type && (void *)ch != skb->data)
- goto discard;
- offset = ch_end;
- } while (ch_end < skb->len);
- return 0;
- discard:
- return 1;
- }
- static void __sctp_hash_endpoint(struct sctp_endpoint *ep)
- {
- struct net *net = sock_net(ep->base.sk);
- struct sctp_ep_common *epb;
- struct sctp_hashbucket *head;
- epb = &ep->base;
- epb->hashent = sctp_ep_hashfn(net, epb->bind_addr.port);
- head = &sctp_ep_hashtable[epb->hashent];
- write_lock(&head->lock);
- hlist_add_head(&epb->node, &head->chain);
- write_unlock(&head->lock);
- }
- void sctp_hash_endpoint(struct sctp_endpoint *ep)
- {
- local_bh_disable();
- __sctp_hash_endpoint(ep);
- local_bh_enable();
- }
- static void __sctp_unhash_endpoint(struct sctp_endpoint *ep)
- {
- struct net *net = sock_net(ep->base.sk);
- struct sctp_hashbucket *head;
- struct sctp_ep_common *epb;
- epb = &ep->base;
- epb->hashent = sctp_ep_hashfn(net, epb->bind_addr.port);
- head = &sctp_ep_hashtable[epb->hashent];
- write_lock(&head->lock);
- hlist_del_init(&epb->node);
- write_unlock(&head->lock);
- }
- void sctp_unhash_endpoint(struct sctp_endpoint *ep)
- {
- local_bh_disable();
- __sctp_unhash_endpoint(ep);
- local_bh_enable();
- }
- static struct sctp_endpoint *__sctp_rcv_lookup_endpoint(struct net *net,
- const union sctp_addr *laddr)
- {
- struct sctp_hashbucket *head;
- struct sctp_ep_common *epb;
- struct sctp_endpoint *ep;
- int hash;
- hash = sctp_ep_hashfn(net, ntohs(laddr->v4.sin_port));
- head = &sctp_ep_hashtable[hash];
- read_lock(&head->lock);
- sctp_for_each_hentry(epb, &head->chain) {
- ep = sctp_ep(epb);
- if (sctp_endpoint_is_match(ep, net, laddr))
- goto hit;
- }
- ep = sctp_sk(net->sctp.ctl_sock)->ep;
- hit:
- sctp_endpoint_hold(ep);
- read_unlock(&head->lock);
- return ep;
- }
- struct sctp_hash_cmp_arg {
- const struct sctp_endpoint *ep;
- const union sctp_addr *laddr;
- const union sctp_addr *paddr;
- const struct net *net;
- };
- static inline int sctp_hash_cmp(struct rhashtable_compare_arg *arg,
- const void *ptr)
- {
- struct sctp_transport *t = (struct sctp_transport *)ptr;
- const struct sctp_hash_cmp_arg *x = arg->key;
- struct sctp_association *asoc;
- int err = 1;
- if (!sctp_cmp_addr_exact(&t->ipaddr, x->paddr))
- return err;
- if (!sctp_transport_hold(t))
- return err;
- asoc = t->asoc;
- if (!net_eq(sock_net(asoc->base.sk), x->net))
- goto out;
- if (x->ep) {
- if (x->ep != asoc->ep)
- goto out;
- } else {
- if (x->laddr->v4.sin_port != htons(asoc->base.bind_addr.port))
- goto out;
- if (!sctp_bind_addr_match(&asoc->base.bind_addr,
- x->laddr, sctp_sk(asoc->base.sk)))
- goto out;
- }
- err = 0;
- out:
- sctp_transport_put(t);
- return err;
- }
- static inline u32 sctp_hash_obj(const void *data, u32 len, u32 seed)
- {
- const struct sctp_transport *t = data;
- const union sctp_addr *paddr = &t->ipaddr;
- const struct net *net = sock_net(t->asoc->base.sk);
- u16 lport = htons(t->asoc->base.bind_addr.port);
- u32 addr;
- if (paddr->sa.sa_family == AF_INET6)
- addr = jhash(&paddr->v6.sin6_addr, 16, seed);
- else
- addr = paddr->v4.sin_addr.s_addr;
- return jhash_3words(addr, ((__u32)paddr->v4.sin_port) << 16 |
- (__force __u32)lport, net_hash_mix(net), seed);
- }
- static inline u32 sctp_hash_key(const void *data, u32 len, u32 seed)
- {
- const struct sctp_hash_cmp_arg *x = data;
- const union sctp_addr *paddr = x->paddr;
- const struct net *net = x->net;
- u16 lport;
- u32 addr;
- lport = x->ep ? htons(x->ep->base.bind_addr.port) :
- x->laddr->v4.sin_port;
- if (paddr->sa.sa_family == AF_INET6)
- addr = jhash(&paddr->v6.sin6_addr, 16, seed);
- else
- addr = paddr->v4.sin_addr.s_addr;
- return jhash_3words(addr, ((__u32)paddr->v4.sin_port) << 16 |
- (__force __u32)lport, net_hash_mix(net), seed);
- }
- static const struct rhashtable_params sctp_hash_params = {
- .head_offset = offsetof(struct sctp_transport, node),
- .hashfn = sctp_hash_key,
- .obj_hashfn = sctp_hash_obj,
- .obj_cmpfn = sctp_hash_cmp,
- .automatic_shrinking = true,
- };
- int sctp_transport_hashtable_init(void)
- {
- return rhashtable_init(&sctp_transport_hashtable, &sctp_hash_params);
- }
- void sctp_transport_hashtable_destroy(void)
- {
- rhashtable_destroy(&sctp_transport_hashtable);
- }
- void sctp_hash_transport(struct sctp_transport *t)
- {
- struct sctp_hash_cmp_arg arg;
- if (t->asoc->temp)
- return;
- arg.ep = t->asoc->ep;
- arg.paddr = &t->ipaddr;
- arg.net = sock_net(t->asoc->base.sk);
- reinsert:
- if (rhashtable_lookup_insert_key(&sctp_transport_hashtable, &arg,
- &t->node, sctp_hash_params) == -EBUSY)
- goto reinsert;
- }
- void sctp_unhash_transport(struct sctp_transport *t)
- {
- if (t->asoc->temp)
- return;
- rhashtable_remove_fast(&sctp_transport_hashtable, &t->node,
- sctp_hash_params);
- }
- struct sctp_transport *sctp_addrs_lookup_transport(
- struct net *net,
- const union sctp_addr *laddr,
- const union sctp_addr *paddr)
- {
- struct sctp_hash_cmp_arg arg = {
- .ep = NULL,
- .laddr = laddr,
- .paddr = paddr,
- .net = net,
- };
- return rhashtable_lookup_fast(&sctp_transport_hashtable, &arg,
- sctp_hash_params);
- }
- struct sctp_transport *sctp_epaddr_lookup_transport(
- const struct sctp_endpoint *ep,
- const union sctp_addr *paddr)
- {
- struct net *net = sock_net(ep->base.sk);
- struct sctp_hash_cmp_arg arg = {
- .ep = ep,
- .paddr = paddr,
- .net = net,
- };
- return rhashtable_lookup_fast(&sctp_transport_hashtable, &arg,
- sctp_hash_params);
- }
- static struct sctp_association *__sctp_lookup_association(
- struct net *net,
- const union sctp_addr *local,
- const union sctp_addr *peer,
- struct sctp_transport **pt)
- {
- struct sctp_transport *t;
- struct sctp_association *asoc = NULL;
- t = sctp_addrs_lookup_transport(net, local, peer);
- if (!t || !sctp_transport_hold(t))
- goto out;
- asoc = t->asoc;
- *pt = t;
- out:
- return asoc;
- }
- static
- struct sctp_association *sctp_lookup_association(struct net *net,
- const union sctp_addr *laddr,
- const union sctp_addr *paddr,
- struct sctp_transport **transportp)
- {
- struct sctp_association *asoc;
- rcu_read_lock();
- asoc = __sctp_lookup_association(net, laddr, paddr, transportp);
- rcu_read_unlock();
- return asoc;
- }
- int sctp_has_association(struct net *net,
- const union sctp_addr *laddr,
- const union sctp_addr *paddr)
- {
- struct sctp_association *asoc;
- struct sctp_transport *transport;
- if ((asoc = sctp_lookup_association(net, laddr, paddr, &transport))) {
- sctp_transport_put(transport);
- return 1;
- }
- return 0;
- }
- static struct sctp_association *__sctp_rcv_init_lookup(struct net *net,
- struct sk_buff *skb,
- const union sctp_addr *laddr, struct sctp_transport **transportp)
- {
- struct sctp_association *asoc;
- union sctp_addr addr;
- union sctp_addr *paddr = &addr;
- struct sctphdr *sh = sctp_hdr(skb);
- union sctp_params params;
- sctp_init_chunk_t *init;
- struct sctp_af *af;
-
-
- init = (sctp_init_chunk_t *)skb->data;
-
- sctp_walk_params(params, init, init_hdr.params) {
-
- af = sctp_get_af_specific(param_type2af(params.p->type));
- if (!af)
- continue;
- af->from_addr_param(paddr, params.addr, sh->source, 0);
- asoc = __sctp_lookup_association(net, laddr, paddr, transportp);
- if (asoc)
- return asoc;
- }
- return NULL;
- }
- static struct sctp_association *__sctp_rcv_asconf_lookup(
- struct net *net,
- sctp_chunkhdr_t *ch,
- const union sctp_addr *laddr,
- __be16 peer_port,
- struct sctp_transport **transportp)
- {
- sctp_addip_chunk_t *asconf = (struct sctp_addip_chunk *)ch;
- struct sctp_af *af;
- union sctp_addr_param *param;
- union sctp_addr paddr;
-
- param = (union sctp_addr_param *)(asconf + 1);
- af = sctp_get_af_specific(param_type2af(param->p.type));
- if (unlikely(!af))
- return NULL;
- af->from_addr_param(&paddr, param, peer_port, 0);
- return __sctp_lookup_association(net, laddr, &paddr, transportp);
- }
- static struct sctp_association *__sctp_rcv_walk_lookup(struct net *net,
- struct sk_buff *skb,
- const union sctp_addr *laddr,
- struct sctp_transport **transportp)
- {
- struct sctp_association *asoc = NULL;
- sctp_chunkhdr_t *ch;
- int have_auth = 0;
- unsigned int chunk_num = 1;
- __u8 *ch_end;
-
- ch = (sctp_chunkhdr_t *) skb->data;
- do {
-
- if (ntohs(ch->length) < sizeof(sctp_chunkhdr_t))
- break;
- ch_end = ((__u8 *)ch) + SCTP_PAD4(ntohs(ch->length));
- if (ch_end > skb_tail_pointer(skb))
- break;
- switch (ch->type) {
- case SCTP_CID_AUTH:
- have_auth = chunk_num;
- break;
- case SCTP_CID_COOKIE_ECHO:
-
- if (have_auth == 1 && chunk_num == 2)
- return NULL;
- break;
- case SCTP_CID_ASCONF:
- if (have_auth || net->sctp.addip_noauth)
- asoc = __sctp_rcv_asconf_lookup(
- net, ch, laddr,
- sctp_hdr(skb)->source,
- transportp);
- default:
- break;
- }
- if (asoc)
- break;
- ch = (sctp_chunkhdr_t *) ch_end;
- chunk_num++;
- } while (ch_end < skb_tail_pointer(skb));
- return asoc;
- }
- static struct sctp_association *__sctp_rcv_lookup_harder(struct net *net,
- struct sk_buff *skb,
- const union sctp_addr *laddr,
- struct sctp_transport **transportp)
- {
- sctp_chunkhdr_t *ch;
-
- if ((skb_shinfo(skb)->gso_type & SKB_GSO_SCTP) == SKB_GSO_SCTP)
- return NULL;
- ch = (sctp_chunkhdr_t *) skb->data;
-
- if (SCTP_PAD4(ntohs(ch->length)) > skb->len)
- return NULL;
-
- if (ch->type == SCTP_CID_INIT || ch->type == SCTP_CID_INIT_ACK)
- return __sctp_rcv_init_lookup(net, skb, laddr, transportp);
- return __sctp_rcv_walk_lookup(net, skb, laddr, transportp);
- }
- static struct sctp_association *__sctp_rcv_lookup(struct net *net,
- struct sk_buff *skb,
- const union sctp_addr *paddr,
- const union sctp_addr *laddr,
- struct sctp_transport **transportp)
- {
- struct sctp_association *asoc;
- asoc = __sctp_lookup_association(net, laddr, paddr, transportp);
-
- if (!asoc)
- asoc = __sctp_rcv_lookup_harder(net, skb, laddr, transportp);
- return asoc;
- }
|