Merge with Linux 2.5.59.
[linux-2.6/linux-mips.git] / net / sctp / input.c
blobd6e64da75733ea4144d2d49a46f95bd4883246da
1 /* SCTP kernel reference Implementation
2 * Copyright (c) 1999-2000 Cisco, Inc.
3 * Copyright (c) 1999-2001 Motorola, Inc.
4 * Copyright (c) 2001 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)
17 * any later version.
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
31 * email address(es):
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>
54 #include <linux/ip.h>
55 #include <linux/time.h> /* For struct timeval */
56 #include <net/sock.h>
57 #include <net/xfrm.h>
58 #include <net/sctp/sctp.h>
59 #include <net/sctp/sm.h>
61 /* Forward declarations for internal helpers. */
62 static int sctp_rcv_ootb(struct sk_buff *);
63 sctp_association_t *__sctp_rcv_lookup(struct sk_buff *skb,
64 const union sctp_addr *laddr,
65 const union sctp_addr *paddr,
66 sctp_transport_t **transportp);
67 sctp_endpoint_t *__sctp_rcv_lookup_endpoint(const union sctp_addr *laddr);
70 /* Calculate the SCTP checksum of an SCTP packet. */
71 static inline int sctp_rcv_checksum(struct sk_buff *skb)
73 struct sctphdr *sh;
74 __u32 cmp, val;
76 sh = (struct sctphdr *) skb->h.raw;
77 cmp = ntohl(sh->checksum);
78 val = count_crc((__u8 *)sh, skb->len);
79 if (val != cmp) {
80 /* CRC failure, dump it. */
81 return -1;
83 return 0;
87 * This is the routine which IP calls when receiving an SCTP packet.
89 int sctp_rcv(struct sk_buff *skb)
91 struct sock *sk;
92 sctp_association_t *asoc;
93 sctp_endpoint_t *ep = NULL;
94 sctp_endpoint_common_t *rcvr;
95 sctp_transport_t *transport = NULL;
96 sctp_chunk_t *chunk;
97 struct sctphdr *sh;
98 union sctp_addr src;
99 union sctp_addr dest;
100 struct sctp_af *af;
101 int ret = 0;
103 if (skb->pkt_type!=PACKET_HOST)
104 goto discard_it;
106 sh = (struct sctphdr *) skb->h.raw;
108 /* Pull up the IP and SCTP headers. */
109 __skb_pull(skb, skb->h.raw - skb->data);
110 if (skb->len < sizeof(struct sctphdr))
111 goto bad_packet;
112 if (sctp_rcv_checksum(skb) < 0)
113 goto bad_packet;
115 skb_pull(skb, sizeof(struct sctphdr));
117 af = sctp_get_af_specific(ipver2af(skb->nh.iph->version));
118 if (unlikely(!af))
119 goto bad_packet;
121 /* Initialize local addresses for lookups. */
122 af->from_skb(&src, skb, 1);
123 af->from_skb(&dest, skb, 0);
125 /* If the packet is to or from a non-unicast address,
126 * silently discard the packet.
128 * This is not clearly defined in the RFC except in section
129 * 8.4 - OOTB handling. However, based on the book "Stream Control
130 * Transmission Protocol" 2.1, "It is important to note that the
131 * IP address of an SCTP transport address must be a routable
132 * unicast address. In other words, IP multicast addresses and
133 * IP broadcast addresses cannot be used in an SCTP transport
134 * address."
136 if (!af->addr_valid(&src) || !af->addr_valid(&dest))
137 goto discard_it;
139 asoc = __sctp_rcv_lookup(skb, &src, &dest, &transport);
142 * RFC 2960, 8.4 - Handle "Out of the blue" Packets.
143 * An SCTP packet is called an "out of the blue" (OOTB)
144 * packet if it is correctly formed, i.e., passed the
145 * receiver's checksum check, but the receiver is not
146 * able to identify the association to which this
147 * packet belongs.
149 if (!asoc) {
150 ep = __sctp_rcv_lookup_endpoint(&dest);
151 if (sctp_rcv_ootb(skb))
152 goto discard_release;
155 /* Retrieve the common input handling substructure. */
156 rcvr = asoc ? &asoc->base : &ep->base;
157 sk = rcvr->sk;
159 if (!xfrm_policy_check(sk, XFRM_POLICY_IN, skb))
160 goto discard_release;
162 /* Create an SCTP packet structure. */
163 chunk = sctp_chunkify(skb, asoc, sk);
164 if (!chunk) {
165 ret = -ENOMEM;
166 goto discard_release;
169 /* Remember what endpoint is to handle this packet. */
170 chunk->rcvr = rcvr;
172 /* Remember the SCTP header. */
173 chunk->sctp_hdr = sh;
175 /* Set the source and destination addresses of the incoming chunk. */
176 sctp_init_addrs(chunk, &src, &dest);
178 /* Remember where we came from. */
179 chunk->transport = transport;
181 /* Acquire access to the sock lock. Note: We are safe from other
182 * bottom halves on this lock, but a user may be in the lock too,
183 * so check if it is busy.
185 sctp_bh_lock_sock(sk);
187 if (sock_owned_by_user(sk)) {
188 sk_add_backlog(sk, (struct sk_buff *) chunk);
189 } else {
190 sctp_backlog_rcv(sk, (struct sk_buff *) chunk);
193 /* Release the sock and any reference counts we took in the
194 * lookup calls.
196 sctp_bh_unlock_sock(sk);
197 if (asoc) {
198 sctp_association_put(asoc);
199 } else {
200 sctp_endpoint_put(ep);
202 sock_put(sk);
203 return ret;
205 bad_packet:
206 #if 0 /* FIXME */
207 SCTP_INC_STATS(SctpInErrs);
208 #endif /* FIXME*/
210 discard_it:
211 kfree_skb(skb);
212 return ret;
214 discard_release:
215 /* Release any structures we may be holding. */
216 if (asoc) {
217 sock_put(asoc->base.sk);
218 sctp_association_put(asoc);
219 } else {
220 sock_put(ep->base.sk);
221 sctp_endpoint_put(ep);
224 goto discard_it;
227 /* Handle second half of inbound skb processing. If the sock was busy,
228 * we may have need to delay processing until later when the sock is
229 * released (on the backlog). If not busy, we call this routine
230 * directly from the bottom half.
232 int sctp_backlog_rcv(struct sock *sk, struct sk_buff *skb)
234 sctp_chunk_t *chunk;
235 sctp_inqueue_t *inqueue;
237 /* One day chunk will live inside the skb, but for
238 * now this works.
240 chunk = (sctp_chunk_t *) skb;
241 inqueue = &chunk->rcvr->inqueue;
243 sctp_push_inqueue(inqueue, chunk);
244 return 0;
248 * This routine is called by the ICMP module when it gets some
249 * sort of error condition. If err < 0 then the socket should
250 * be closed and the error returned to the user. If err > 0
251 * it's just the icmp type << 8 | icmp code. After adjustment
252 * header points to the first 8 bytes of the sctp header. We need
253 * to find the appropriate port.
255 * The locking strategy used here is very "optimistic". When
256 * someone else accesses the socket the ICMP is just dropped
257 * and for some paths there is no check at all.
258 * A more general error queue to queue errors for later handling
259 * is probably better.
262 void sctp_v4_err(struct sk_buff *skb, u32 info)
264 /* This should probably involve a call to SCTPhandleICMP(). */
268 * RFC 2960, 8.4 - Handle "Out of the blue" Packets.
270 * This function scans all the chunks in the OOTB packet to determine if
271 * the packet should be discarded right away. If a response might be needed
272 * for this packet, or, if further processing is possible, the packet will
273 * be queued to a proper inqueue for the next phase of handling.
275 * Output:
276 * Return 0 - If further processing is needed.
277 * Return 1 - If the packet can be discarded right away.
279 int sctp_rcv_ootb(struct sk_buff *skb)
281 sctp_chunkhdr_t *ch;
282 __u8 *ch_end;
283 sctp_errhdr_t *err;
285 ch = (sctp_chunkhdr_t *) skb->data;
287 /* Scan through all the chunks in the packet. */
288 do {
289 ch_end = ((__u8 *) ch) + WORD_ROUND(ntohs(ch->length));
291 /* RFC 8.4, 2) If the OOTB packet contains an ABORT chunk, the
292 * receiver MUST silently discard the OOTB packet and take no
293 * further action.
295 if (SCTP_CID_ABORT == ch->type)
296 goto discard;
298 /* RFC 8.4, 6) If the packet contains a SHUTDOWN COMPLETE
299 * chunk, the receiver should silently discard the packet
300 * and take no further action.
302 if (ch->type == SCTP_CID_SHUTDOWN_COMPLETE)
303 goto discard;
305 /* RFC 8.4, 7) If the packet contains a "Stale cookie" ERROR
306 * or a COOKIE ACK the SCTP Packet should be silently
307 * discarded.
309 if (ch->type == SCTP_CID_COOKIE_ACK)
310 goto discard;
312 if (ch->type == SCTP_CID_ERROR) {
313 err = (sctp_errhdr_t *)(ch + sizeof(sctp_chunkhdr_t));
314 if (SCTP_ERROR_STALE_COOKIE == err->cause)
315 goto discard;
318 ch = (sctp_chunkhdr_t *) ch_end;
319 } while (ch_end < skb->tail);
321 return 0;
323 discard:
324 return 1;
327 /* Insert endpoint into the hash table. */
328 void __sctp_hash_endpoint(sctp_endpoint_t *ep)
330 sctp_endpoint_common_t **epp;
331 sctp_endpoint_common_t *epb;
332 sctp_hashbucket_t *head;
334 epb = &ep->base;
336 epb->hashent = sctp_ep_hashfn(epb->bind_addr.port);
337 head = &sctp_proto.ep_hashbucket[epb->hashent];
339 sctp_write_lock(&head->lock);
340 epp = &head->chain;
341 epb->next = *epp;
342 if (epb->next)
343 (*epp)->pprev = &epb->next;
344 *epp = epb;
345 epb->pprev = epp;
346 sctp_write_unlock(&head->lock);
349 /* Add an endpoint to the hash. Local BH-safe. */
350 void sctp_hash_endpoint(sctp_endpoint_t *ep)
352 sctp_local_bh_disable();
353 __sctp_hash_endpoint(ep);
354 sctp_local_bh_enable();
357 /* Remove endpoint from the hash table. */
358 void __sctp_unhash_endpoint(sctp_endpoint_t *ep)
360 sctp_hashbucket_t *head;
361 sctp_endpoint_common_t *epb;
363 epb = &ep->base;
365 epb->hashent = sctp_ep_hashfn(epb->bind_addr.port);
367 head = &sctp_proto.ep_hashbucket[epb->hashent];
369 sctp_write_lock(&head->lock);
371 if (epb->pprev) {
372 if (epb->next)
373 epb->next->pprev = epb->pprev;
374 *epb->pprev = epb->next;
375 epb->pprev = NULL;
378 sctp_write_unlock(&head->lock);
381 /* Remove endpoint from the hash. Local BH-safe. */
382 void sctp_unhash_endpoint(sctp_endpoint_t *ep)
384 sctp_local_bh_disable();
385 __sctp_unhash_endpoint(ep);
386 sctp_local_bh_enable();
389 /* Look up an endpoint. */
390 sctp_endpoint_t *__sctp_rcv_lookup_endpoint(const union sctp_addr *laddr)
392 sctp_hashbucket_t *head;
393 sctp_endpoint_common_t *epb;
394 sctp_endpoint_t *ep;
395 int hash;
397 hash = sctp_ep_hashfn(laddr->v4.sin_port);
398 head = &sctp_proto.ep_hashbucket[hash];
399 read_lock(&head->lock);
400 for (epb = head->chain; epb; epb = epb->next) {
401 ep = sctp_ep(epb);
402 if (sctp_endpoint_is_match(ep, laddr))
403 goto hit;
406 ep = sctp_sk((sctp_get_ctl_sock()))->ep;
407 epb = &ep->base;
409 hit:
410 sctp_endpoint_hold(ep);
411 sock_hold(epb->sk);
412 read_unlock(&head->lock);
413 return ep;
416 /* Add an association to the hash. Local BH-safe. */
417 void sctp_hash_established(sctp_association_t *asoc)
419 sctp_local_bh_disable();
420 __sctp_hash_established(asoc);
421 sctp_local_bh_enable();
424 /* Insert association into the hash table. */
425 void __sctp_hash_established(sctp_association_t *asoc)
427 sctp_endpoint_common_t **epp;
428 sctp_endpoint_common_t *epb;
429 sctp_hashbucket_t *head;
431 epb = &asoc->base;
433 /* Calculate which chain this entry will belong to. */
434 epb->hashent = sctp_assoc_hashfn(epb->bind_addr.port, asoc->peer.port);
436 head = &sctp_proto.assoc_hashbucket[epb->hashent];
438 sctp_write_lock(&head->lock);
439 epp = &head->chain;
440 epb->next = *epp;
441 if (epb->next)
442 (*epp)->pprev = &epb->next;
443 *epp = epb;
444 epb->pprev = epp;
445 sctp_write_unlock(&head->lock);
448 /* Remove association from the hash table. Local BH-safe. */
449 void sctp_unhash_established(sctp_association_t *asoc)
451 sctp_local_bh_disable();
452 __sctp_unhash_established(asoc);
453 sctp_local_bh_enable();
456 /* Remove association from the hash table. */
457 void __sctp_unhash_established(sctp_association_t *asoc)
459 sctp_hashbucket_t *head;
460 sctp_endpoint_common_t *epb;
462 epb = &asoc->base;
464 epb->hashent = sctp_assoc_hashfn(epb->bind_addr.port,
465 asoc->peer.port);
467 head = &sctp_proto.assoc_hashbucket[epb->hashent];
469 sctp_write_lock(&head->lock);
471 if (epb->pprev) {
472 if (epb->next)
473 epb->next->pprev = epb->pprev;
474 *epb->pprev = epb->next;
475 epb->pprev = NULL;
478 sctp_write_unlock(&head->lock);
481 /* Look up an association. */
482 sctp_association_t *__sctp_lookup_association(const union sctp_addr *laddr,
483 const union sctp_addr *paddr,
484 sctp_transport_t **transportp)
486 sctp_hashbucket_t *head;
487 sctp_endpoint_common_t *epb;
488 sctp_association_t *asoc;
489 sctp_transport_t *transport;
490 int hash;
492 /* Optimize here for direct hit, only listening connections can
493 * have wildcards anyways.
495 hash = sctp_assoc_hashfn(laddr->v4.sin_port, paddr->v4.sin_port);
496 head = &sctp_proto.assoc_hashbucket[hash];
497 read_lock(&head->lock);
498 for (epb = head->chain; epb; epb = epb->next) {
499 asoc = sctp_assoc(epb);
500 transport = sctp_assoc_is_match(asoc, laddr, paddr);
501 if (transport)
502 goto hit;
505 read_unlock(&head->lock);
507 return NULL;
509 hit:
510 *transportp = transport;
511 sctp_association_hold(asoc);
512 sock_hold(epb->sk);
513 read_unlock(&head->lock);
514 return asoc;
517 /* Look up an association. BH-safe. */
518 sctp_association_t *sctp_lookup_association(const union sctp_addr *laddr,
519 const union sctp_addr *paddr,
520 sctp_transport_t **transportp)
522 sctp_association_t *asoc;
524 sctp_local_bh_disable();
525 asoc = __sctp_lookup_association(laddr, paddr, transportp);
526 sctp_local_bh_enable();
528 return asoc;
531 /* Is there an association matching the given local and peer addresses? */
532 int sctp_has_association(const union sctp_addr *laddr,
533 const union sctp_addr *paddr)
535 sctp_association_t *asoc;
536 sctp_transport_t *transport;
538 if ((asoc = sctp_lookup_association(laddr, paddr, &transport))) {
539 sock_put(asoc->base.sk);
540 sctp_association_put(asoc);
541 return 1;
544 return 0;
548 * SCTP Implementors Guide, 2.18 Handling of address
549 * parameters within the INIT or INIT-ACK.
551 * D) When searching for a matching TCB upon reception of an INIT
552 * or INIT-ACK chunk the receiver SHOULD use not only the
553 * source address of the packet (containing the INIT or
554 * INIT-ACK) but the receiver SHOULD also use all valid
555 * address parameters contained within the chunk.
557 * 2.18.3 Solution description
559 * This new text clearly specifies to an implementor the need
560 * to look within the INIT or INIT-ACK. Any implementation that
561 * does not do this, may not be able to establish associations
562 * in certain circumstances.
565 static sctp_association_t *__sctp_rcv_init_lookup(struct sk_buff *skb,
566 const union sctp_addr *laddr, sctp_transport_t **transportp)
568 sctp_association_t *asoc;
569 union sctp_addr addr;
570 union sctp_addr *paddr = &addr;
571 struct sctphdr *sh = (struct sctphdr *) skb->h.raw;
572 sctp_chunkhdr_t *ch;
573 union sctp_params params;
574 sctp_init_chunk_t *init;
576 ch = (sctp_chunkhdr_t *) skb->data;
578 /* If this is INIT/INIT-ACK look inside the chunk too. */
579 switch (ch->type) {
580 case SCTP_CID_INIT:
581 case SCTP_CID_INIT_ACK:
582 break;
583 default:
584 return NULL;
588 * This code will NOT touch anything inside the chunk--it is
589 * strictly READ-ONLY.
591 * RFC 2960 3 SCTP packet Format
593 * Multiple chunks can be bundled into one SCTP packet up to
594 * the MTU size, except for the INIT, INIT ACK, and SHUTDOWN
595 * COMPLETE chunks. These chunks MUST NOT be bundled with any
596 * other chunk in a packet. See Section 6.10 for more details
597 * on chunk bundling.
600 /* Find the start of the TLVs and the end of the chunk. This is
601 * the region we search for address parameters.
603 init = (sctp_init_chunk_t *)skb->data;
605 /* Walk the parameters looking for embedded addresses. */
606 sctp_walk_params(params, init, init_hdr.params) {
608 /* Note: Ignoring hostname addresses. */
609 if ((SCTP_PARAM_IPV4_ADDRESS != params.p->type) &&
610 (SCTP_PARAM_IPV6_ADDRESS != params.p->type))
611 continue;
613 sctp_param2sockaddr(paddr, params.addr, ntohs(sh->source));
614 asoc = __sctp_lookup_association(laddr, paddr, transportp);
615 if (asoc)
616 return asoc;
619 return NULL;
622 /* Lookup an association for an inbound skb. */
623 sctp_association_t *__sctp_rcv_lookup(struct sk_buff *skb,
624 const union sctp_addr *paddr,
625 const union sctp_addr *laddr,
626 sctp_transport_t **transportp)
628 sctp_association_t *asoc;
630 asoc = __sctp_lookup_association(laddr, paddr, transportp);
632 /* Further lookup for INIT/INIT-ACK packets.
633 * SCTP Implementors Guide, 2.18 Handling of address
634 * parameters within the INIT or INIT-ACK.
636 if (!asoc)
637 asoc = __sctp_rcv_init_lookup(skb, laddr, transportp);
639 return asoc;