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 static 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 static struct sctp_endpoint *__sctp_rcv_lookup_endpoint(const union sctp_addr *laddr);
71 static struct sctp_association *__sctp_lookup_association(
72 const union sctp_addr *local,
73 const union sctp_addr *peer,
74 struct sctp_transport **pt);
77 /* Calculate the SCTP checksum of an SCTP packet. */
78 static inline int sctp_rcv_checksum(struct sk_buff *skb)
82 struct sk_buff *list = skb_shinfo(skb)->frag_list;
84 sh = (struct sctphdr *) skb->h.raw;
85 cmp = ntohl(sh->checksum);
87 val = sctp_start_cksum((__u8 *)sh, skb_headlen(skb));
89 for (; list; list = list->next)
90 val = sctp_update_cksum((__u8 *)list->data, skb_headlen(list),
93 val = sctp_end_cksum(val);
96 /* CRC failure, dump it. */
97 SCTP_INC_STATS_BH(SCTP_MIB_CHECKSUMERRORS);
104 * This is the routine which IP calls when receiving an SCTP packet.
106 int sctp_rcv(struct sk_buff *skb)
109 struct sctp_association *asoc;
110 struct sctp_endpoint *ep = NULL;
111 struct sctp_ep_common *rcvr;
112 struct sctp_transport *transport = NULL;
113 struct sctp_chunk *chunk;
116 union sctp_addr dest;
121 if (skb->pkt_type!=PACKET_HOST)
124 SCTP_INC_STATS_BH(SCTP_MIB_INSCTPPACKS);
126 sh = (struct sctphdr *) skb->h.raw;
128 /* Pull up the IP and SCTP headers. */
129 __skb_pull(skb, skb->h.raw - skb->data);
130 if (skb->len < sizeof(struct sctphdr))
132 if (sctp_rcv_checksum(skb) < 0)
135 skb_pull(skb, sizeof(struct sctphdr));
137 /* Make sure we at least have chunk headers worth of data left. */
138 if (skb->len < sizeof(struct sctp_chunkhdr))
141 family = ipver2af(skb->nh.iph->version);
142 af = sctp_get_af_specific(family);
146 /* Initialize local addresses for lookups. */
147 af->from_skb(&src, skb, 1);
148 af->from_skb(&dest, skb, 0);
150 /* If the packet is to or from a non-unicast address,
151 * silently discard the packet.
153 * This is not clearly defined in the RFC except in section
154 * 8.4 - OOTB handling. However, based on the book "Stream Control
155 * Transmission Protocol" 2.1, "It is important to note that the
156 * IP address of an SCTP transport address must be a routable
157 * unicast address. In other words, IP multicast addresses and
158 * IP broadcast addresses cannot be used in an SCTP transport
161 if (!af->addr_valid(&src, NULL) || !af->addr_valid(&dest, NULL))
164 asoc = __sctp_rcv_lookup(skb, &src, &dest, &transport);
167 * RFC 2960, 8.4 - Handle "Out of the blue" Packets.
168 * An SCTP packet is called an "out of the blue" (OOTB)
169 * packet if it is correctly formed, i.e., passed the
170 * receiver's checksum check, but the receiver is not
171 * able to identify the association to which this
175 ep = __sctp_rcv_lookup_endpoint(&dest);
176 if (sctp_rcv_ootb(skb)) {
177 SCTP_INC_STATS_BH(SCTP_MIB_OUTOFBLUES);
178 goto discard_release;
182 /* Retrieve the common input handling substructure. */
183 rcvr = asoc ? &asoc->base : &ep->base;
186 /* SCTP seems to always need a timestamp right now (FIXME) */
187 if (skb->stamp.tv_sec == 0) {
188 do_gettimeofday(&skb->stamp);
189 sock_enable_timestamp(sk);
192 if (!xfrm_policy_check(sk, XFRM_POLICY_IN, skb, family))
193 goto discard_release;
195 ret = sk_filter(sk, skb, 1);
197 goto discard_release;
199 /* Create an SCTP packet structure. */
200 chunk = sctp_chunkify(skb, asoc, sk);
203 goto discard_release;
206 /* Remember what endpoint is to handle this packet. */
209 /* Remember the SCTP header. */
210 chunk->sctp_hdr = sh;
212 /* Set the source and destination addresses of the incoming chunk. */
213 sctp_init_addrs(chunk, &src, &dest);
215 /* Remember where we came from. */
216 chunk->transport = transport;
218 /* Acquire access to the sock lock. Note: We are safe from other
219 * bottom halves on this lock, but a user may be in the lock too,
220 * so check if it is busy.
222 sctp_bh_lock_sock(sk);
224 if (sock_owned_by_user(sk))
225 sk_add_backlog(sk, (struct sk_buff *) chunk);
227 sctp_backlog_rcv(sk, (struct sk_buff *) chunk);
229 /* Release the sock and any reference counts we took in the
232 sctp_bh_unlock_sock(sk);
234 sctp_association_put(asoc);
236 sctp_endpoint_put(ep);
245 /* Release any structures we may be holding. */
247 sock_put(asoc->base.sk);
248 sctp_association_put(asoc);
250 sock_put(ep->base.sk);
251 sctp_endpoint_put(ep);
257 /* Handle second half of inbound skb processing. If the sock was busy,
258 * we may have need to delay processing until later when the sock is
259 * released (on the backlog). If not busy, we call this routine
260 * directly from the bottom half.
262 int sctp_backlog_rcv(struct sock *sk, struct sk_buff *skb)
264 struct sctp_chunk *chunk;
265 struct sctp_inq *inqueue;
267 /* One day chunk will live inside the skb, but for
270 chunk = (struct sctp_chunk *) skb;
271 inqueue = &chunk->rcvr->inqueue;
273 sctp_inq_push(inqueue, chunk);
277 /* Handle icmp frag needed error. */
278 void sctp_icmp_frag_needed(struct sock *sk, struct sctp_association *asoc,
279 struct sctp_transport *t, __u32 pmtu)
281 if (unlikely(pmtu < SCTP_DEFAULT_MINSEGMENT)) {
282 printk(KERN_WARNING "%s: Reported pmtu %d too low, "
283 "using default minimum of %d\n", __FUNCTION__, pmtu,
284 SCTP_DEFAULT_MINSEGMENT);
285 pmtu = SCTP_DEFAULT_MINSEGMENT;
288 if (!sock_owned_by_user(sk) && t && (t->pmtu != pmtu)) {
290 sctp_assoc_sync_pmtu(asoc);
291 sctp_retransmit(&asoc->outqueue, t, SCTP_RTXR_PMTUD);
296 * SCTP Implementer's Guide, 2.37 ICMP handling procedures
298 * ICMP8) If the ICMP code is a "Unrecognized next header type encountered"
299 * or a "Protocol Unreachable" treat this message as an abort
300 * with the T bit set.
302 * This function sends an event to the state machine, which will abort the
306 void sctp_icmp_proto_unreachable(struct sock *sk,
307 struct sctp_endpoint *ep,
308 struct sctp_association *asoc,
309 struct sctp_transport *t)
311 SCTP_DEBUG_PRINTK("%s\n", __FUNCTION__);
313 sctp_do_sm(SCTP_EVENT_T_OTHER,
314 SCTP_ST_OTHER(SCTP_EVENT_ICMP_PROTO_UNREACH),
315 asoc->state, asoc->ep, asoc, NULL,
320 /* Common lookup code for icmp/icmpv6 error handler. */
321 struct sock *sctp_err_lookup(int family, struct sk_buff *skb,
322 struct sctphdr *sctphdr,
323 struct sctp_endpoint **epp,
324 struct sctp_association **app,
325 struct sctp_transport **tpp)
327 union sctp_addr saddr;
328 union sctp_addr daddr;
330 struct sock *sk = NULL;
331 struct sctp_endpoint *ep = NULL;
332 struct sctp_association *asoc = NULL;
333 struct sctp_transport *transport = NULL;
335 *app = NULL; *epp = NULL; *tpp = NULL;
337 af = sctp_get_af_specific(family);
342 /* Initialize local addresses for lookups. */
343 af->from_skb(&saddr, skb, 1);
344 af->from_skb(&daddr, skb, 0);
346 /* Look for an association that matches the incoming ICMP error
349 asoc = __sctp_lookup_association(&saddr, &daddr, &transport);
351 /* If there is no matching association, see if it matches any
352 * endpoint. This may happen for an ICMP error generated in
353 * response to an INIT_ACK.
355 ep = __sctp_rcv_lookup_endpoint(&daddr);
364 if (ntohl(sctphdr->vtag) != asoc->c.peer_vtag) {
365 ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
371 sctp_bh_lock_sock(sk);
373 /* If too many ICMPs get dropped on busy
374 * servers this needs to be solved differently.
376 if (sock_owned_by_user(sk))
377 NET_INC_STATS_BH(LINUX_MIB_LOCKDROPPEDICMPS);
387 sctp_association_put(asoc);
389 sctp_endpoint_put(ep);
393 /* Common cleanup code for icmp/icmpv6 error handler. */
394 void sctp_err_finish(struct sock *sk, struct sctp_endpoint *ep,
395 struct sctp_association *asoc)
397 sctp_bh_unlock_sock(sk);
400 sctp_association_put(asoc);
402 sctp_endpoint_put(ep);
406 * This routine is called by the ICMP module when it gets some
407 * sort of error condition. If err < 0 then the socket should
408 * be closed and the error returned to the user. If err > 0
409 * it's just the icmp type << 8 | icmp code. After adjustment
410 * header points to the first 8 bytes of the sctp header. We need
411 * to find the appropriate port.
413 * The locking strategy used here is very "optimistic". When
414 * someone else accesses the socket the ICMP is just dropped
415 * and for some paths there is no check at all.
416 * A more general error queue to queue errors for later handling
417 * is probably better.
420 void sctp_v4_err(struct sk_buff *skb, __u32 info)
422 struct iphdr *iph = (struct iphdr *)skb->data;
423 struct sctphdr *sh = (struct sctphdr *)(skb->data + (iph->ihl <<2));
424 int type = skb->h.icmph->type;
425 int code = skb->h.icmph->code;
427 struct sctp_endpoint *ep;
428 struct sctp_association *asoc;
429 struct sctp_transport *transport;
430 struct inet_sock *inet;
431 char *saveip, *savesctp;
434 if (skb->len < ((iph->ihl << 2) + 8)) {
435 ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
439 /* Fix up skb to look at the embedded net header. */
440 saveip = skb->nh.raw;
441 savesctp = skb->h.raw;
443 skb->h.raw = (char *)sh;
444 sk = sctp_err_lookup(AF_INET, skb, sh, &ep, &asoc, &transport);
445 /* Put back, the original pointers. */
446 skb->nh.raw = saveip;
447 skb->h.raw = savesctp;
449 ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
452 /* Warning: The sock lock is held. Remember to call
457 case ICMP_PARAMETERPROB:
460 case ICMP_DEST_UNREACH:
461 if (code > NR_ICMP_UNREACH)
464 /* PMTU discovery (RFC1191) */
465 if (ICMP_FRAG_NEEDED == code) {
466 sctp_icmp_frag_needed(sk, asoc, transport, info);
470 if (ICMP_PROT_UNREACH == code) {
471 sctp_icmp_proto_unreachable(sk, ep, asoc,
476 err = icmp_err_convert[code].errno;
478 case ICMP_TIME_EXCEEDED:
479 /* Ignore any time exceeded errors due to fragment reassembly
482 if (ICMP_EXC_FRAGTIME == code)
492 if (!sock_owned_by_user(sk) && inet->recverr) {
494 sk->sk_error_report(sk);
495 } else { /* Only an error on timeout */
496 sk->sk_err_soft = err;
500 sctp_err_finish(sk, ep, asoc);
504 * RFC 2960, 8.4 - Handle "Out of the blue" Packets.
506 * This function scans all the chunks in the OOTB packet to determine if
507 * the packet should be discarded right away. If a response might be needed
508 * for this packet, or, if further processing is possible, the packet will
509 * be queued to a proper inqueue for the next phase of handling.
512 * Return 0 - If further processing is needed.
513 * Return 1 - If the packet can be discarded right away.
515 int sctp_rcv_ootb(struct sk_buff *skb)
521 ch = (sctp_chunkhdr_t *) skb->data;
522 ch_end = ((__u8 *) ch) + WORD_ROUND(ntohs(ch->length));
524 /* Scan through all the chunks in the packet. */
525 while (ch_end > (__u8 *)ch && ch_end < skb->tail) {
527 /* RFC 8.4, 2) If the OOTB packet contains an ABORT chunk, the
528 * receiver MUST silently discard the OOTB packet and take no
531 if (SCTP_CID_ABORT == ch->type)
534 /* RFC 8.4, 6) If the packet contains a SHUTDOWN COMPLETE
535 * chunk, the receiver should silently discard the packet
536 * and take no further action.
538 if (SCTP_CID_SHUTDOWN_COMPLETE == ch->type)
541 /* RFC 8.4, 7) If the packet contains a "Stale cookie" ERROR
542 * or a COOKIE ACK the SCTP Packet should be silently
545 if (SCTP_CID_COOKIE_ACK == ch->type)
548 if (SCTP_CID_ERROR == ch->type) {
549 sctp_walk_errors(err, ch) {
550 if (SCTP_ERROR_STALE_COOKIE == err->cause)
555 ch = (sctp_chunkhdr_t *) ch_end;
556 ch_end = ((__u8 *) ch) + WORD_ROUND(ntohs(ch->length));
565 /* Insert endpoint into the hash table. */
566 static void __sctp_hash_endpoint(struct sctp_endpoint *ep)
568 struct sctp_ep_common **epp;
569 struct sctp_ep_common *epb;
570 struct sctp_hashbucket *head;
574 epb->hashent = sctp_ep_hashfn(epb->bind_addr.port);
575 head = &sctp_ep_hashtable[epb->hashent];
577 sctp_write_lock(&head->lock);
581 (*epp)->pprev = &epb->next;
584 sctp_write_unlock(&head->lock);
587 /* Add an endpoint to the hash. Local BH-safe. */
588 void sctp_hash_endpoint(struct sctp_endpoint *ep)
590 sctp_local_bh_disable();
591 __sctp_hash_endpoint(ep);
592 sctp_local_bh_enable();
595 /* Remove endpoint from the hash table. */
596 static void __sctp_unhash_endpoint(struct sctp_endpoint *ep)
598 struct sctp_hashbucket *head;
599 struct sctp_ep_common *epb;
603 epb->hashent = sctp_ep_hashfn(epb->bind_addr.port);
605 head = &sctp_ep_hashtable[epb->hashent];
607 sctp_write_lock(&head->lock);
611 epb->next->pprev = epb->pprev;
612 *epb->pprev = epb->next;
616 sctp_write_unlock(&head->lock);
619 /* Remove endpoint from the hash. Local BH-safe. */
620 void sctp_unhash_endpoint(struct sctp_endpoint *ep)
622 sctp_local_bh_disable();
623 __sctp_unhash_endpoint(ep);
624 sctp_local_bh_enable();
627 /* Look up an endpoint. */
628 static struct sctp_endpoint *__sctp_rcv_lookup_endpoint(const union sctp_addr *laddr)
630 struct sctp_hashbucket *head;
631 struct sctp_ep_common *epb;
632 struct sctp_endpoint *ep;
635 hash = sctp_ep_hashfn(laddr->v4.sin_port);
636 head = &sctp_ep_hashtable[hash];
637 read_lock(&head->lock);
638 for (epb = head->chain; epb; epb = epb->next) {
640 if (sctp_endpoint_is_match(ep, laddr))
644 ep = sctp_sk((sctp_get_ctl_sock()))->ep;
648 sctp_endpoint_hold(ep);
650 read_unlock(&head->lock);
654 /* Insert association into the hash table. */
655 static void __sctp_hash_established(struct sctp_association *asoc)
657 struct sctp_ep_common **epp;
658 struct sctp_ep_common *epb;
659 struct sctp_hashbucket *head;
663 /* Calculate which chain this entry will belong to. */
664 epb->hashent = sctp_assoc_hashfn(epb->bind_addr.port, asoc->peer.port);
666 head = &sctp_assoc_hashtable[epb->hashent];
668 sctp_write_lock(&head->lock);
672 (*epp)->pprev = &epb->next;
675 sctp_write_unlock(&head->lock);
678 /* Add an association to the hash. Local BH-safe. */
679 void sctp_hash_established(struct sctp_association *asoc)
681 sctp_local_bh_disable();
682 __sctp_hash_established(asoc);
683 sctp_local_bh_enable();
686 /* Remove association from the hash table. */
687 static void __sctp_unhash_established(struct sctp_association *asoc)
689 struct sctp_hashbucket *head;
690 struct sctp_ep_common *epb;
694 epb->hashent = sctp_assoc_hashfn(epb->bind_addr.port,
697 head = &sctp_assoc_hashtable[epb->hashent];
699 sctp_write_lock(&head->lock);
703 epb->next->pprev = epb->pprev;
704 *epb->pprev = epb->next;
708 sctp_write_unlock(&head->lock);
711 /* Remove association from the hash table. Local BH-safe. */
712 void sctp_unhash_established(struct sctp_association *asoc)
714 sctp_local_bh_disable();
715 __sctp_unhash_established(asoc);
716 sctp_local_bh_enable();
719 /* Look up an association. */
720 static struct sctp_association *__sctp_lookup_association(
721 const union sctp_addr *local,
722 const union sctp_addr *peer,
723 struct sctp_transport **pt)
725 struct sctp_hashbucket *head;
726 struct sctp_ep_common *epb;
727 struct sctp_association *asoc;
728 struct sctp_transport *transport;
731 /* Optimize here for direct hit, only listening connections can
732 * have wildcards anyways.
734 hash = sctp_assoc_hashfn(local->v4.sin_port, peer->v4.sin_port);
735 head = &sctp_assoc_hashtable[hash];
736 read_lock(&head->lock);
737 for (epb = head->chain; epb; epb = epb->next) {
738 asoc = sctp_assoc(epb);
739 transport = sctp_assoc_is_match(asoc, local, peer);
744 read_unlock(&head->lock);
750 sctp_association_hold(asoc);
752 read_unlock(&head->lock);
756 /* Look up an association. BH-safe. */
758 struct sctp_association *sctp_lookup_association(const union sctp_addr *laddr,
759 const union sctp_addr *paddr,
760 struct sctp_transport **transportp)
762 struct sctp_association *asoc;
764 sctp_local_bh_disable();
765 asoc = __sctp_lookup_association(laddr, paddr, transportp);
766 sctp_local_bh_enable();
771 /* Is there an association matching the given local and peer addresses? */
772 int sctp_has_association(const union sctp_addr *laddr,
773 const union sctp_addr *paddr)
775 struct sctp_association *asoc;
776 struct sctp_transport *transport;
778 if ((asoc = sctp_lookup_association(laddr, paddr, &transport))) {
779 sock_put(asoc->base.sk);
780 sctp_association_put(asoc);
788 * SCTP Implementors Guide, 2.18 Handling of address
789 * parameters within the INIT or INIT-ACK.
791 * D) When searching for a matching TCB upon reception of an INIT
792 * or INIT-ACK chunk the receiver SHOULD use not only the
793 * source address of the packet (containing the INIT or
794 * INIT-ACK) but the receiver SHOULD also use all valid
795 * address parameters contained within the chunk.
797 * 2.18.3 Solution description
799 * This new text clearly specifies to an implementor the need
800 * to look within the INIT or INIT-ACK. Any implementation that
801 * does not do this, may not be able to establish associations
802 * in certain circumstances.
805 static struct sctp_association *__sctp_rcv_init_lookup(struct sk_buff *skb,
806 const union sctp_addr *laddr, struct sctp_transport **transportp)
808 struct sctp_association *asoc;
809 union sctp_addr addr;
810 union sctp_addr *paddr = &addr;
811 struct sctphdr *sh = (struct sctphdr *) skb->h.raw;
813 union sctp_params params;
814 sctp_init_chunk_t *init;
815 struct sctp_transport *transport;
818 ch = (sctp_chunkhdr_t *) skb->data;
820 /* If this is INIT/INIT-ACK look inside the chunk too. */
823 case SCTP_CID_INIT_ACK:
829 /* The code below will attempt to walk the chunk and extract
830 * parameter information. Before we do that, we need to verify
831 * that the chunk length doesn't cause overflow. Otherwise, we'll
834 if (WORD_ROUND(ntohs(ch->length)) > skb->len)
838 * This code will NOT touch anything inside the chunk--it is
839 * strictly READ-ONLY.
841 * RFC 2960 3 SCTP packet Format
843 * Multiple chunks can be bundled into one SCTP packet up to
844 * the MTU size, except for the INIT, INIT ACK, and SHUTDOWN
845 * COMPLETE chunks. These chunks MUST NOT be bundled with any
846 * other chunk in a packet. See Section 6.10 for more details
850 /* Find the start of the TLVs and the end of the chunk. This is
851 * the region we search for address parameters.
853 init = (sctp_init_chunk_t *)skb->data;
855 /* Walk the parameters looking for embedded addresses. */
856 sctp_walk_params(params, init, init_hdr.params) {
858 /* Note: Ignoring hostname addresses. */
859 af = sctp_get_af_specific(param_type2af(params.p->type));
863 af->from_addr_param(paddr, params.addr, ntohs(sh->source), 0);
865 asoc = __sctp_lookup_association(laddr, paddr, &transport);
873 /* Lookup an association for an inbound skb. */
874 static struct sctp_association *__sctp_rcv_lookup(struct sk_buff *skb,
875 const union sctp_addr *paddr,
876 const union sctp_addr *laddr,
877 struct sctp_transport **transportp)
879 struct sctp_association *asoc;
881 asoc = __sctp_lookup_association(laddr, paddr, transportp);
883 /* Further lookup for INIT/INIT-ACK packets.
884 * SCTP Implementors Guide, 2.18 Handling of address
885 * parameters within the INIT or INIT-ACK.
888 asoc = __sctp_rcv_init_lookup(skb, laddr, transportp);