1 /* SCTP kernel reference 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
9 * This file is part of the SCTP kernel reference Implementation
11 * These functions handle all input from the IP layer into SCTP.
13 * The SCTP reference 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)
19 * The SCTP reference 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.
25 * You should have received a copy of the GNU General Public License
26 * along with GNU CC; see the file COPYING. If not, write to
27 * the Free Software Foundation, 59 Temple Place - Suite 330,
28 * Boston, MA 02111-1307, USA.
30 * Please send any bug reports or fixes you make to the
32 * lksctp developers <lksctp-developers@lists.sourceforge.net>
34 * Or submit a bug report through the following website:
35 * http://www.sf.net/projects/lksctp
37 * Written or modified by:
38 * La Monte H.P. Yarroll <piggy@acm.org>
39 * Karl Knutson <karl@athena.chicago.il.us>
40 * Xingang Guo <xingang.guo@intel.com>
41 * Jon Grimm <jgrimm@us.ibm.com>
42 * Hui Huang <hui.huang@nokia.com>
43 * Daisy Chang <daisyc@us.ibm.com>
44 * Sridhar Samudrala <sri@us.ibm.com>
45 * Ardelle Fan <ardelle.fan@intel.com>
47 * Any bugs reported given to us we will try to fix... any fixes shared will
48 * be incorporated into the next SCTP release.
51 #include <linux/types.h>
52 #include <linux/list.h> /* For struct list_head */
53 #include <linux/socket.h>
55 #include <linux/time.h> /* For struct timeval */
61 #include <net/sctp/sctp.h>
62 #include <net/sctp/sm.h>
64 /* Forward declarations for internal helpers. */
65 static int sctp_rcv_ootb(struct sk_buff *);
66 struct sctp_association *__sctp_rcv_lookup(struct sk_buff *skb,
67 const union sctp_addr *laddr,
68 const union sctp_addr *paddr,
69 struct sctp_transport **transportp);
70 struct sctp_endpoint *__sctp_rcv_lookup_endpoint(const union sctp_addr *laddr);
73 /* Calculate the SCTP checksum of an SCTP packet. */
74 static inline int sctp_rcv_checksum(struct sk_buff *skb)
78 struct sk_buff *list = skb_shinfo(skb)->frag_list;
80 sh = (struct sctphdr *) skb->h.raw;
81 cmp = ntohl(sh->checksum);
83 val = sctp_start_cksum((__u8 *)sh, skb_headlen(skb));
85 for (; list; list = list->next)
86 val = sctp_update_cksum((__u8 *)list->data, skb_headlen(list),
89 val = sctp_end_cksum(val);
92 /* CRC failure, dump it. */
93 SCTP_INC_STATS_BH(SCTP_MIB_CHECKSUMERRORS);
100 * This is the routine which IP calls when receiving an SCTP packet.
102 int sctp_rcv(struct sk_buff *skb)
105 struct sctp_association *asoc;
106 struct sctp_endpoint *ep = NULL;
107 struct sctp_ep_common *rcvr;
108 struct sctp_transport *transport = NULL;
109 struct sctp_chunk *chunk;
112 union sctp_addr dest;
117 if (skb->pkt_type!=PACKET_HOST)
120 SCTP_INC_STATS_BH(SCTP_MIB_INSCTPPACKS);
122 sh = (struct sctphdr *) skb->h.raw;
124 /* Pull up the IP and SCTP headers. */
125 __skb_pull(skb, skb->h.raw - skb->data);
126 if (skb->len < sizeof(struct sctphdr))
128 if (sctp_rcv_checksum(skb) < 0)
131 skb_pull(skb, sizeof(struct sctphdr));
133 family = ipver2af(skb->nh.iph->version);
134 af = sctp_get_af_specific(family);
138 /* Initialize local addresses for lookups. */
139 af->from_skb(&src, skb, 1);
140 af->from_skb(&dest, skb, 0);
142 /* If the packet is to or from a non-unicast address,
143 * silently discard the packet.
145 * This is not clearly defined in the RFC except in section
146 * 8.4 - OOTB handling. However, based on the book "Stream Control
147 * Transmission Protocol" 2.1, "It is important to note that the
148 * IP address of an SCTP transport address must be a routable
149 * unicast address. In other words, IP multicast addresses and
150 * IP broadcast addresses cannot be used in an SCTP transport
153 if (!af->addr_valid(&src, NULL) || !af->addr_valid(&dest, NULL))
156 asoc = __sctp_rcv_lookup(skb, &src, &dest, &transport);
159 * RFC 2960, 8.4 - Handle "Out of the blue" Packets.
160 * An SCTP packet is called an "out of the blue" (OOTB)
161 * packet if it is correctly formed, i.e., passed the
162 * receiver's checksum check, but the receiver is not
163 * able to identify the association to which this
167 ep = __sctp_rcv_lookup_endpoint(&dest);
168 if (sctp_rcv_ootb(skb)) {
169 SCTP_INC_STATS_BH(SCTP_MIB_OUTOFBLUES);
170 goto discard_release;
174 /* Retrieve the common input handling substructure. */
175 rcvr = asoc ? &asoc->base : &ep->base;
178 /* SCTP seems to always need a timestamp right now (FIXME) */
179 if (skb->stamp.tv_sec == 0) {
180 do_gettimeofday(&skb->stamp);
181 sock_enable_timestamp(sk);
184 if (!xfrm_policy_check(sk, XFRM_POLICY_IN, skb, family))
185 goto discard_release;
187 ret = sk_filter(sk, skb, 1);
189 goto discard_release;
191 /* Create an SCTP packet structure. */
192 chunk = sctp_chunkify(skb, asoc, sk);
195 goto discard_release;
198 /* Remember what endpoint is to handle this packet. */
201 /* Remember the SCTP header. */
202 chunk->sctp_hdr = sh;
204 /* Set the source and destination addresses of the incoming chunk. */
205 sctp_init_addrs(chunk, &src, &dest);
207 /* Remember where we came from. */
208 chunk->transport = transport;
210 /* Acquire access to the sock lock. Note: We are safe from other
211 * bottom halves on this lock, but a user may be in the lock too,
212 * so check if it is busy.
214 sctp_bh_lock_sock(sk);
216 if (sock_owned_by_user(sk))
217 sk_add_backlog(sk, (struct sk_buff *) chunk);
219 sctp_backlog_rcv(sk, (struct sk_buff *) chunk);
221 /* Release the sock and any reference counts we took in the
224 sctp_bh_unlock_sock(sk);
226 sctp_association_put(asoc);
228 sctp_endpoint_put(ep);
237 /* Release any structures we may be holding. */
239 sock_put(asoc->base.sk);
240 sctp_association_put(asoc);
242 sock_put(ep->base.sk);
243 sctp_endpoint_put(ep);
249 /* Handle second half of inbound skb processing. If the sock was busy,
250 * we may have need to delay processing until later when the sock is
251 * released (on the backlog). If not busy, we call this routine
252 * directly from the bottom half.
254 int sctp_backlog_rcv(struct sock *sk, struct sk_buff *skb)
256 struct sctp_chunk *chunk;
257 struct sctp_inq *inqueue;
259 /* One day chunk will live inside the skb, but for
262 chunk = (struct sctp_chunk *) skb;
263 inqueue = &chunk->rcvr->inqueue;
265 sctp_inq_push(inqueue, chunk);
269 /* Handle icmp frag needed error. */
270 void sctp_icmp_frag_needed(struct sock *sk, struct sctp_association *asoc,
271 struct sctp_transport *t, __u32 pmtu)
273 if (unlikely(pmtu < SCTP_DEFAULT_MINSEGMENT)) {
274 printk(KERN_WARNING "%s: Reported pmtu %d too low, "
275 "using default minimum of %d\n", __FUNCTION__, pmtu,
276 SCTP_DEFAULT_MINSEGMENT);
277 pmtu = SCTP_DEFAULT_MINSEGMENT;
280 if (!sock_owned_by_user(sk) && t && (t->pmtu != pmtu)) {
282 sctp_assoc_sync_pmtu(asoc);
283 sctp_retransmit(&asoc->outqueue, t, SCTP_RTXR_PMTUD);
287 /* Common lookup code for icmp/icmpv6 error handler. */
288 struct sock *sctp_err_lookup(int family, struct sk_buff *skb,
289 struct sctphdr *sctphdr,
290 struct sctp_endpoint **epp,
291 struct sctp_association **app,
292 struct sctp_transport **tpp)
294 union sctp_addr saddr;
295 union sctp_addr daddr;
297 struct sock *sk = NULL;
298 struct sctp_endpoint *ep = NULL;
299 struct sctp_association *asoc = NULL;
300 struct sctp_transport *transport = NULL;
302 *app = NULL; *epp = NULL; *tpp = NULL;
304 af = sctp_get_af_specific(family);
309 /* Initialize local addresses for lookups. */
310 af->from_skb(&saddr, skb, 1);
311 af->from_skb(&daddr, skb, 0);
313 /* Look for an association that matches the incoming ICMP error
316 asoc = __sctp_lookup_association(&saddr, &daddr, &transport);
318 /* If there is no matching association, see if it matches any
319 * endpoint. This may happen for an ICMP error generated in
320 * response to an INIT_ACK.
322 ep = __sctp_rcv_lookup_endpoint(&daddr);
329 if (ntohl(sctphdr->vtag) != asoc->c.peer_vtag) {
330 ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
337 sctp_bh_lock_sock(sk);
339 /* If too many ICMPs get dropped on busy
340 * servers this needs to be solved differently.
342 if (sock_owned_by_user(sk))
343 NET_INC_STATS_BH(LINUX_MIB_LOCKDROPPEDICMPS);
353 sctp_association_put(asoc);
355 sctp_endpoint_put(ep);
359 /* Common cleanup code for icmp/icmpv6 error handler. */
360 void sctp_err_finish(struct sock *sk, struct sctp_endpoint *ep,
361 struct sctp_association *asoc)
363 sctp_bh_unlock_sock(sk);
366 sctp_association_put(asoc);
368 sctp_endpoint_put(ep);
372 * This routine is called by the ICMP module when it gets some
373 * sort of error condition. If err < 0 then the socket should
374 * be closed and the error returned to the user. If err > 0
375 * it's just the icmp type << 8 | icmp code. After adjustment
376 * header points to the first 8 bytes of the sctp header. We need
377 * to find the appropriate port.
379 * The locking strategy used here is very "optimistic". When
380 * someone else accesses the socket the ICMP is just dropped
381 * and for some paths there is no check at all.
382 * A more general error queue to queue errors for later handling
383 * is probably better.
386 void sctp_v4_err(struct sk_buff *skb, __u32 info)
388 struct iphdr *iph = (struct iphdr *)skb->data;
389 struct sctphdr *sh = (struct sctphdr *)(skb->data + (iph->ihl <<2));
390 int type = skb->h.icmph->type;
391 int code = skb->h.icmph->code;
393 struct sctp_endpoint *ep;
394 struct sctp_association *asoc;
395 struct sctp_transport *transport;
396 struct inet_opt *inet;
397 char *saveip, *savesctp;
400 if (skb->len < ((iph->ihl << 2) + 8)) {
401 ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
405 /* Fix up skb to look at the embedded net header. */
406 saveip = skb->nh.raw;
407 savesctp = skb->h.raw;
409 skb->h.raw = (char *)sh;
410 sk = sctp_err_lookup(AF_INET, skb, sh, &ep, &asoc, &transport);
411 /* Put back, the original pointers. */
412 skb->nh.raw = saveip;
413 skb->h.raw = savesctp;
415 ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
418 /* Warning: The sock lock is held. Remember to call
423 case ICMP_PARAMETERPROB:
426 case ICMP_DEST_UNREACH:
427 if (code > NR_ICMP_UNREACH)
430 /* PMTU discovery (RFC1191) */
431 if (ICMP_FRAG_NEEDED == code) {
432 sctp_icmp_frag_needed(sk, asoc, transport, info);
436 err = icmp_err_convert[code].errno;
438 case ICMP_TIME_EXCEEDED:
439 /* Ignore any time exceeded errors due to fragment reassembly
442 if (ICMP_EXC_FRAGTIME == code)
452 if (!sock_owned_by_user(sk) && inet->recverr) {
454 sk->sk_error_report(sk);
455 } else { /* Only an error on timeout */
456 sk->sk_err_soft = err;
460 sctp_err_finish(sk, ep, asoc);
464 * RFC 2960, 8.4 - Handle "Out of the blue" Packets.
466 * This function scans all the chunks in the OOTB packet to determine if
467 * the packet should be discarded right away. If a response might be needed
468 * for this packet, or, if further processing is possible, the packet will
469 * be queued to a proper inqueue for the next phase of handling.
472 * Return 0 - If further processing is needed.
473 * Return 1 - If the packet can be discarded right away.
475 int sctp_rcv_ootb(struct sk_buff *skb)
481 ch = (sctp_chunkhdr_t *) skb->data;
483 /* Scan through all the chunks in the packet. */
485 ch_end = ((__u8 *) ch) + WORD_ROUND(ntohs(ch->length));
487 /* RFC 8.4, 2) If the OOTB packet contains an ABORT chunk, the
488 * receiver MUST silently discard the OOTB packet and take no
491 if (SCTP_CID_ABORT == ch->type)
494 /* RFC 8.4, 6) If the packet contains a SHUTDOWN COMPLETE
495 * chunk, the receiver should silently discard the packet
496 * and take no further action.
498 if (SCTP_CID_SHUTDOWN_COMPLETE == ch->type)
501 /* RFC 8.4, 7) If the packet contains a "Stale cookie" ERROR
502 * or a COOKIE ACK the SCTP Packet should be silently
505 if (SCTP_CID_COOKIE_ACK == ch->type)
508 if (SCTP_CID_ERROR == ch->type) {
509 sctp_walk_errors(err, ch) {
510 if (SCTP_ERROR_STALE_COOKIE == err->cause)
515 ch = (sctp_chunkhdr_t *) ch_end;
516 } while (ch_end < skb->tail);
524 /* Insert endpoint into the hash table. */
525 void __sctp_hash_endpoint(struct sctp_endpoint *ep)
527 struct sctp_ep_common **epp;
528 struct sctp_ep_common *epb;
529 struct sctp_hashbucket *head;
533 epb->hashent = sctp_ep_hashfn(epb->bind_addr.port);
534 head = &sctp_ep_hashtable[epb->hashent];
536 sctp_write_lock(&head->lock);
540 (*epp)->pprev = &epb->next;
543 sctp_write_unlock(&head->lock);
546 /* Add an endpoint to the hash. Local BH-safe. */
547 void sctp_hash_endpoint(struct sctp_endpoint *ep)
549 sctp_local_bh_disable();
550 __sctp_hash_endpoint(ep);
551 sctp_local_bh_enable();
554 /* Remove endpoint from the hash table. */
555 void __sctp_unhash_endpoint(struct sctp_endpoint *ep)
557 struct sctp_hashbucket *head;
558 struct sctp_ep_common *epb;
562 epb->hashent = sctp_ep_hashfn(epb->bind_addr.port);
564 head = &sctp_ep_hashtable[epb->hashent];
566 sctp_write_lock(&head->lock);
570 epb->next->pprev = epb->pprev;
571 *epb->pprev = epb->next;
575 sctp_write_unlock(&head->lock);
578 /* Remove endpoint from the hash. Local BH-safe. */
579 void sctp_unhash_endpoint(struct sctp_endpoint *ep)
581 sctp_local_bh_disable();
582 __sctp_unhash_endpoint(ep);
583 sctp_local_bh_enable();
586 /* Look up an endpoint. */
587 struct sctp_endpoint *__sctp_rcv_lookup_endpoint(const union sctp_addr *laddr)
589 struct sctp_hashbucket *head;
590 struct sctp_ep_common *epb;
591 struct sctp_endpoint *ep;
594 hash = sctp_ep_hashfn(laddr->v4.sin_port);
595 head = &sctp_ep_hashtable[hash];
596 read_lock(&head->lock);
597 for (epb = head->chain; epb; epb = epb->next) {
599 if (sctp_endpoint_is_match(ep, laddr))
603 ep = sctp_sk((sctp_get_ctl_sock()))->ep;
607 sctp_endpoint_hold(ep);
609 read_unlock(&head->lock);
613 /* Add an association to the hash. Local BH-safe. */
614 void sctp_hash_established(struct sctp_association *asoc)
616 sctp_local_bh_disable();
617 __sctp_hash_established(asoc);
618 sctp_local_bh_enable();
621 /* Insert association into the hash table. */
622 void __sctp_hash_established(struct sctp_association *asoc)
624 struct sctp_ep_common **epp;
625 struct sctp_ep_common *epb;
626 struct sctp_hashbucket *head;
630 /* Calculate which chain this entry will belong to. */
631 epb->hashent = sctp_assoc_hashfn(epb->bind_addr.port, asoc->peer.port);
633 head = &sctp_assoc_hashtable[epb->hashent];
635 sctp_write_lock(&head->lock);
639 (*epp)->pprev = &epb->next;
642 sctp_write_unlock(&head->lock);
645 /* Remove association from the hash table. Local BH-safe. */
646 void sctp_unhash_established(struct sctp_association *asoc)
648 sctp_local_bh_disable();
649 __sctp_unhash_established(asoc);
650 sctp_local_bh_enable();
653 /* Remove association from the hash table. */
654 void __sctp_unhash_established(struct sctp_association *asoc)
656 struct sctp_hashbucket *head;
657 struct sctp_ep_common *epb;
661 epb->hashent = sctp_assoc_hashfn(epb->bind_addr.port,
664 head = &sctp_assoc_hashtable[epb->hashent];
666 sctp_write_lock(&head->lock);
670 epb->next->pprev = epb->pprev;
671 *epb->pprev = epb->next;
675 sctp_write_unlock(&head->lock);
678 /* Look up an association. */
679 struct sctp_association *__sctp_lookup_association(
680 const union sctp_addr *local,
681 const union sctp_addr *peer,
682 struct sctp_transport **pt)
684 struct sctp_hashbucket *head;
685 struct sctp_ep_common *epb;
686 struct sctp_association *asoc;
687 struct sctp_transport *transport;
690 /* Optimize here for direct hit, only listening connections can
691 * have wildcards anyways.
693 hash = sctp_assoc_hashfn(local->v4.sin_port, peer->v4.sin_port);
694 head = &sctp_assoc_hashtable[hash];
695 read_lock(&head->lock);
696 for (epb = head->chain; epb; epb = epb->next) {
697 asoc = sctp_assoc(epb);
698 transport = sctp_assoc_is_match(asoc, local, peer);
703 read_unlock(&head->lock);
709 sctp_association_hold(asoc);
711 read_unlock(&head->lock);
715 /* Look up an association. BH-safe. */
716 struct sctp_association *sctp_lookup_association(const union sctp_addr *laddr,
717 const union sctp_addr *paddr,
718 struct sctp_transport **transportp)
720 struct sctp_association *asoc;
722 sctp_local_bh_disable();
723 asoc = __sctp_lookup_association(laddr, paddr, transportp);
724 sctp_local_bh_enable();
729 /* Is there an association matching the given local and peer addresses? */
730 int sctp_has_association(const union sctp_addr *laddr,
731 const union sctp_addr *paddr)
733 struct sctp_association *asoc;
734 struct sctp_transport *transport;
736 if ((asoc = sctp_lookup_association(laddr, paddr, &transport))) {
737 sock_put(asoc->base.sk);
738 sctp_association_put(asoc);
746 * SCTP Implementors Guide, 2.18 Handling of address
747 * parameters within the INIT or INIT-ACK.
749 * D) When searching for a matching TCB upon reception of an INIT
750 * or INIT-ACK chunk the receiver SHOULD use not only the
751 * source address of the packet (containing the INIT or
752 * INIT-ACK) but the receiver SHOULD also use all valid
753 * address parameters contained within the chunk.
755 * 2.18.3 Solution description
757 * This new text clearly specifies to an implementor the need
758 * to look within the INIT or INIT-ACK. Any implementation that
759 * does not do this, may not be able to establish associations
760 * in certain circumstances.
763 static struct sctp_association *__sctp_rcv_init_lookup(struct sk_buff *skb,
764 const union sctp_addr *laddr, struct sctp_transport **transportp)
766 struct sctp_association *asoc;
767 union sctp_addr addr;
768 union sctp_addr *paddr = &addr;
769 struct sctphdr *sh = (struct sctphdr *) skb->h.raw;
771 union sctp_params params;
772 sctp_init_chunk_t *init;
773 struct sctp_transport *transport;
776 ch = (sctp_chunkhdr_t *) skb->data;
778 /* If this is INIT/INIT-ACK look inside the chunk too. */
781 case SCTP_CID_INIT_ACK:
788 * This code will NOT touch anything inside the chunk--it is
789 * strictly READ-ONLY.
791 * RFC 2960 3 SCTP packet Format
793 * Multiple chunks can be bundled into one SCTP packet up to
794 * the MTU size, except for the INIT, INIT ACK, and SHUTDOWN
795 * COMPLETE chunks. These chunks MUST NOT be bundled with any
796 * other chunk in a packet. See Section 6.10 for more details
800 /* Find the start of the TLVs and the end of the chunk. This is
801 * the region we search for address parameters.
803 init = (sctp_init_chunk_t *)skb->data;
805 /* Walk the parameters looking for embedded addresses. */
806 sctp_walk_params(params, init, init_hdr.params) {
808 /* Note: Ignoring hostname addresses. */
809 af = sctp_get_af_specific(param_type2af(params.p->type));
813 af->from_addr_param(paddr, params.addr, ntohs(sh->source), 0);
815 asoc = __sctp_lookup_association(laddr, paddr, &transport);
823 /* Lookup an association for an inbound skb. */
824 struct sctp_association *__sctp_rcv_lookup(struct sk_buff *skb,
825 const union sctp_addr *paddr,
826 const union sctp_addr *laddr,
827 struct sctp_transport **transportp)
829 struct sctp_association *asoc;
831 asoc = __sctp_lookup_association(laddr, paddr, transportp);
833 /* Further lookup for INIT/INIT-ACK packets.
834 * SCTP Implementors Guide, 2.18 Handling of address
835 * parameters within the INIT or INIT-ACK.
838 asoc = __sctp_rcv_init_lookup(skb, laddr, transportp);