sctp: Allow to disable SCTP checksums via module parameter
[linux-2.6/mini2440.git] / net / sctp / input.c
blob693fd0804810b37462d76af1b764615a7580852c
1 /* SCTP kernel 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 implementation
11 * These functions handle all input from the IP layer into SCTP.
13 * This SCTP 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 * This SCTP 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/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>
66 /* Forward declarations for internal helpers. */
67 static int sctp_rcv_ootb(struct sk_buff *);
68 static struct sctp_association *__sctp_rcv_lookup(struct sk_buff *skb,
69 const union sctp_addr *laddr,
70 const union sctp_addr *paddr,
71 struct sctp_transport **transportp);
72 static struct sctp_endpoint *__sctp_rcv_lookup_endpoint(const union sctp_addr *laddr);
73 static struct sctp_association *__sctp_lookup_association(
74 const union sctp_addr *local,
75 const union sctp_addr *peer,
76 struct sctp_transport **pt);
78 static void sctp_add_backlog(struct sock *sk, struct sk_buff *skb);
81 /* Calculate the SCTP checksum of an SCTP packet. */
82 static inline int sctp_rcv_checksum(struct sk_buff *skb)
84 struct sk_buff *list = skb_shinfo(skb)->frag_list;
85 struct sctphdr *sh = sctp_hdr(skb);
86 __be32 cmp = sh->checksum;
87 __be32 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),
91 val);
93 val = sctp_end_cksum(val);
95 if (val != cmp) {
96 /* CRC failure, dump it. */
97 SCTP_INC_STATS_BH(SCTP_MIB_CHECKSUMERRORS);
98 return -1;
100 return 0;
103 struct sctp_input_cb {
104 union {
105 struct inet_skb_parm h4;
106 #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
107 struct inet6_skb_parm h6;
108 #endif
109 } header;
110 struct sctp_chunk *chunk;
112 #define SCTP_INPUT_CB(__skb) ((struct sctp_input_cb *)&((__skb)->cb[0]))
115 * This is the routine which IP calls when receiving an SCTP packet.
117 int sctp_rcv(struct sk_buff *skb)
119 struct sock *sk;
120 struct sctp_association *asoc;
121 struct sctp_endpoint *ep = NULL;
122 struct sctp_ep_common *rcvr;
123 struct sctp_transport *transport = NULL;
124 struct sctp_chunk *chunk;
125 struct sctphdr *sh;
126 union sctp_addr src;
127 union sctp_addr dest;
128 int family;
129 struct sctp_af *af;
131 if (skb->pkt_type!=PACKET_HOST)
132 goto discard_it;
134 SCTP_INC_STATS_BH(SCTP_MIB_INSCTPPACKS);
136 if (skb_linearize(skb))
137 goto discard_it;
139 sh = sctp_hdr(skb);
141 /* Pull up the IP and SCTP headers. */
142 __skb_pull(skb, skb_transport_offset(skb));
143 if (skb->len < sizeof(struct sctphdr))
144 goto discard_it;
145 if (!sctp_checksum_disable && !skb_csum_unnecessary(skb) &&
146 sctp_rcv_checksum(skb) < 0)
147 goto discard_it;
149 skb_pull(skb, sizeof(struct sctphdr));
151 /* Make sure we at least have chunk headers worth of data left. */
152 if (skb->len < sizeof(struct sctp_chunkhdr))
153 goto discard_it;
155 family = ipver2af(ip_hdr(skb)->version);
156 af = sctp_get_af_specific(family);
157 if (unlikely(!af))
158 goto discard_it;
160 /* Initialize local addresses for lookups. */
161 af->from_skb(&src, skb, 1);
162 af->from_skb(&dest, skb, 0);
164 /* If the packet is to or from a non-unicast address,
165 * silently discard the packet.
167 * This is not clearly defined in the RFC except in section
168 * 8.4 - OOTB handling. However, based on the book "Stream Control
169 * Transmission Protocol" 2.1, "It is important to note that the
170 * IP address of an SCTP transport address must be a routable
171 * unicast address. In other words, IP multicast addresses and
172 * IP broadcast addresses cannot be used in an SCTP transport
173 * address."
175 if (!af->addr_valid(&src, NULL, skb) ||
176 !af->addr_valid(&dest, NULL, skb))
177 goto discard_it;
179 asoc = __sctp_rcv_lookup(skb, &src, &dest, &transport);
181 if (!asoc)
182 ep = __sctp_rcv_lookup_endpoint(&dest);
184 /* Retrieve the common input handling substructure. */
185 rcvr = asoc ? &asoc->base : &ep->base;
186 sk = rcvr->sk;
189 * If a frame arrives on an interface and the receiving socket is
190 * bound to another interface, via SO_BINDTODEVICE, treat it as OOTB
192 if (sk->sk_bound_dev_if && (sk->sk_bound_dev_if != af->skb_iif(skb)))
194 if (asoc) {
195 sctp_association_put(asoc);
196 asoc = NULL;
197 } else {
198 sctp_endpoint_put(ep);
199 ep = NULL;
201 sk = sctp_get_ctl_sock();
202 ep = sctp_sk(sk)->ep;
203 sctp_endpoint_hold(ep);
204 rcvr = &ep->base;
208 * RFC 2960, 8.4 - Handle "Out of the blue" Packets.
209 * An SCTP packet is called an "out of the blue" (OOTB)
210 * packet if it is correctly formed, i.e., passed the
211 * receiver's checksum check, but the receiver is not
212 * able to identify the association to which this
213 * packet belongs.
215 if (!asoc) {
216 if (sctp_rcv_ootb(skb)) {
217 SCTP_INC_STATS_BH(SCTP_MIB_OUTOFBLUES);
218 goto discard_release;
222 if (!xfrm_policy_check(sk, XFRM_POLICY_IN, skb, family))
223 goto discard_release;
224 nf_reset(skb);
226 if (sk_filter(sk, skb))
227 goto discard_release;
229 /* Create an SCTP packet structure. */
230 chunk = sctp_chunkify(skb, asoc, sk);
231 if (!chunk)
232 goto discard_release;
233 SCTP_INPUT_CB(skb)->chunk = chunk;
235 /* Remember what endpoint is to handle this packet. */
236 chunk->rcvr = rcvr;
238 /* Remember the SCTP header. */
239 chunk->sctp_hdr = sh;
241 /* Set the source and destination addresses of the incoming chunk. */
242 sctp_init_addrs(chunk, &src, &dest);
244 /* Remember where we came from. */
245 chunk->transport = transport;
247 /* Acquire access to the sock lock. Note: We are safe from other
248 * bottom halves on this lock, but a user may be in the lock too,
249 * so check if it is busy.
251 sctp_bh_lock_sock(sk);
253 if (sk != rcvr->sk) {
254 /* Our cached sk is different from the rcvr->sk. This is
255 * because migrate()/accept() may have moved the association
256 * to a new socket and released all the sockets. So now we
257 * are holding a lock on the old socket while the user may
258 * be doing something with the new socket. Switch our veiw
259 * of the current sk.
261 sctp_bh_unlock_sock(sk);
262 sk = rcvr->sk;
263 sctp_bh_lock_sock(sk);
266 if (sock_owned_by_user(sk)) {
267 SCTP_INC_STATS_BH(SCTP_MIB_IN_PKT_BACKLOG);
268 sctp_add_backlog(sk, skb);
269 } else {
270 SCTP_INC_STATS_BH(SCTP_MIB_IN_PKT_SOFTIRQ);
271 sctp_inq_push(&chunk->rcvr->inqueue, chunk);
274 sctp_bh_unlock_sock(sk);
276 /* Release the asoc/ep ref we took in the lookup calls. */
277 if (asoc)
278 sctp_association_put(asoc);
279 else
280 sctp_endpoint_put(ep);
282 return 0;
284 discard_it:
285 SCTP_INC_STATS_BH(SCTP_MIB_IN_PKT_DISCARDS);
286 kfree_skb(skb);
287 return 0;
289 discard_release:
290 /* Release the asoc/ep ref we took in the lookup calls. */
291 if (asoc)
292 sctp_association_put(asoc);
293 else
294 sctp_endpoint_put(ep);
296 goto discard_it;
299 /* Process the backlog queue of the socket. Every skb on
300 * the backlog holds a ref on an association or endpoint.
301 * We hold this ref throughout the state machine to make
302 * sure that the structure we need is still around.
304 int sctp_backlog_rcv(struct sock *sk, struct sk_buff *skb)
306 struct sctp_chunk *chunk = SCTP_INPUT_CB(skb)->chunk;
307 struct sctp_inq *inqueue = &chunk->rcvr->inqueue;
308 struct sctp_ep_common *rcvr = NULL;
309 int backloged = 0;
311 rcvr = chunk->rcvr;
313 /* If the rcvr is dead then the association or endpoint
314 * has been deleted and we can safely drop the chunk
315 * and refs that we are holding.
317 if (rcvr->dead) {
318 sctp_chunk_free(chunk);
319 goto done;
322 if (unlikely(rcvr->sk != sk)) {
323 /* In this case, the association moved from one socket to
324 * another. We are currently sitting on the backlog of the
325 * old socket, so we need to move.
326 * However, since we are here in the process context we
327 * need to take make sure that the user doesn't own
328 * the new socket when we process the packet.
329 * If the new socket is user-owned, queue the chunk to the
330 * backlog of the new socket without dropping any refs.
331 * Otherwise, we can safely push the chunk on the inqueue.
334 sk = rcvr->sk;
335 sctp_bh_lock_sock(sk);
337 if (sock_owned_by_user(sk)) {
338 sk_add_backlog(sk, skb);
339 backloged = 1;
340 } else
341 sctp_inq_push(inqueue, chunk);
343 sctp_bh_unlock_sock(sk);
345 /* If the chunk was backloged again, don't drop refs */
346 if (backloged)
347 return 0;
348 } else {
349 sctp_inq_push(inqueue, chunk);
352 done:
353 /* Release the refs we took in sctp_add_backlog */
354 if (SCTP_EP_TYPE_ASSOCIATION == rcvr->type)
355 sctp_association_put(sctp_assoc(rcvr));
356 else if (SCTP_EP_TYPE_SOCKET == rcvr->type)
357 sctp_endpoint_put(sctp_ep(rcvr));
358 else
359 BUG();
361 return 0;
364 static void sctp_add_backlog(struct sock *sk, struct sk_buff *skb)
366 struct sctp_chunk *chunk = SCTP_INPUT_CB(skb)->chunk;
367 struct sctp_ep_common *rcvr = chunk->rcvr;
369 /* Hold the assoc/ep while hanging on the backlog queue.
370 * This way, we know structures we need will not disappear from us
372 if (SCTP_EP_TYPE_ASSOCIATION == rcvr->type)
373 sctp_association_hold(sctp_assoc(rcvr));
374 else if (SCTP_EP_TYPE_SOCKET == rcvr->type)
375 sctp_endpoint_hold(sctp_ep(rcvr));
376 else
377 BUG();
379 sk_add_backlog(sk, skb);
382 /* Handle icmp frag needed error. */
383 void sctp_icmp_frag_needed(struct sock *sk, struct sctp_association *asoc,
384 struct sctp_transport *t, __u32 pmtu)
386 if (!t || (t->pathmtu <= pmtu))
387 return;
389 if (sock_owned_by_user(sk)) {
390 asoc->pmtu_pending = 1;
391 t->pmtu_pending = 1;
392 return;
395 if (t->param_flags & SPP_PMTUD_ENABLE) {
396 /* Update transports view of the MTU */
397 sctp_transport_update_pmtu(t, pmtu);
399 /* Update association pmtu. */
400 sctp_assoc_sync_pmtu(asoc);
403 /* Retransmit with the new pmtu setting.
404 * Normally, if PMTU discovery is disabled, an ICMP Fragmentation
405 * Needed will never be sent, but if a message was sent before
406 * PMTU discovery was disabled that was larger than the PMTU, it
407 * would not be fragmented, so it must be re-transmitted fragmented.
409 sctp_retransmit(&asoc->outqueue, t, SCTP_RTXR_PMTUD);
413 * SCTP Implementer's Guide, 2.37 ICMP handling procedures
415 * ICMP8) If the ICMP code is a "Unrecognized next header type encountered"
416 * or a "Protocol Unreachable" treat this message as an abort
417 * with the T bit set.
419 * This function sends an event to the state machine, which will abort the
420 * association.
423 void sctp_icmp_proto_unreachable(struct sock *sk,
424 struct sctp_association *asoc,
425 struct sctp_transport *t)
427 SCTP_DEBUG_PRINTK("%s\n", __func__);
429 sctp_do_sm(SCTP_EVENT_T_OTHER,
430 SCTP_ST_OTHER(SCTP_EVENT_ICMP_PROTO_UNREACH),
431 asoc->state, asoc->ep, asoc, t,
432 GFP_ATOMIC);
436 /* Common lookup code for icmp/icmpv6 error handler. */
437 struct sock *sctp_err_lookup(int family, struct sk_buff *skb,
438 struct sctphdr *sctphdr,
439 struct sctp_association **app,
440 struct sctp_transport **tpp)
442 union sctp_addr saddr;
443 union sctp_addr daddr;
444 struct sctp_af *af;
445 struct sock *sk = NULL;
446 struct sctp_association *asoc;
447 struct sctp_transport *transport = NULL;
448 struct sctp_init_chunk *chunkhdr;
449 __u32 vtag = ntohl(sctphdr->vtag);
450 int len = skb->len - ((void *)sctphdr - (void *)skb->data);
452 *app = NULL; *tpp = NULL;
454 af = sctp_get_af_specific(family);
455 if (unlikely(!af)) {
456 return NULL;
459 /* Initialize local addresses for lookups. */
460 af->from_skb(&saddr, skb, 1);
461 af->from_skb(&daddr, skb, 0);
463 /* Look for an association that matches the incoming ICMP error
464 * packet.
466 asoc = __sctp_lookup_association(&saddr, &daddr, &transport);
467 if (!asoc)
468 return NULL;
470 sk = asoc->base.sk;
472 /* RFC 4960, Appendix C. ICMP Handling
474 * ICMP6) An implementation MUST validate that the Verification Tag
475 * contained in the ICMP message matches the Verification Tag of
476 * the peer. If the Verification Tag is not 0 and does NOT
477 * match, discard the ICMP message. If it is 0 and the ICMP
478 * message contains enough bytes to verify that the chunk type is
479 * an INIT chunk and that the Initiate Tag matches the tag of the
480 * peer, continue with ICMP7. If the ICMP message is too short
481 * or the chunk type or the Initiate Tag does not match, silently
482 * discard the packet.
484 if (vtag == 0) {
485 chunkhdr = (struct sctp_init_chunk *)((void *)sctphdr
486 + sizeof(struct sctphdr));
487 if (len < sizeof(struct sctphdr) + sizeof(sctp_chunkhdr_t)
488 + sizeof(__be32) ||
489 chunkhdr->chunk_hdr.type != SCTP_CID_INIT ||
490 ntohl(chunkhdr->init_hdr.init_tag) != asoc->c.my_vtag) {
491 goto out;
493 } else if (vtag != asoc->c.peer_vtag) {
494 goto out;
497 sctp_bh_lock_sock(sk);
499 /* If too many ICMPs get dropped on busy
500 * servers this needs to be solved differently.
502 if (sock_owned_by_user(sk))
503 NET_INC_STATS_BH(&init_net, LINUX_MIB_LOCKDROPPEDICMPS);
505 *app = asoc;
506 *tpp = transport;
507 return sk;
509 out:
510 if (asoc)
511 sctp_association_put(asoc);
512 return NULL;
515 /* Common cleanup code for icmp/icmpv6 error handler. */
516 void sctp_err_finish(struct sock *sk, struct sctp_association *asoc)
518 sctp_bh_unlock_sock(sk);
519 if (asoc)
520 sctp_association_put(asoc);
524 * This routine is called by the ICMP module when it gets some
525 * sort of error condition. If err < 0 then the socket should
526 * be closed and the error returned to the user. If err > 0
527 * it's just the icmp type << 8 | icmp code. After adjustment
528 * header points to the first 8 bytes of the sctp header. We need
529 * to find the appropriate port.
531 * The locking strategy used here is very "optimistic". When
532 * someone else accesses the socket the ICMP is just dropped
533 * and for some paths there is no check at all.
534 * A more general error queue to queue errors for later handling
535 * is probably better.
538 void sctp_v4_err(struct sk_buff *skb, __u32 info)
540 struct iphdr *iph = (struct iphdr *)skb->data;
541 const int ihlen = iph->ihl * 4;
542 const int type = icmp_hdr(skb)->type;
543 const int code = icmp_hdr(skb)->code;
544 struct sock *sk;
545 struct sctp_association *asoc = NULL;
546 struct sctp_transport *transport;
547 struct inet_sock *inet;
548 sk_buff_data_t saveip, savesctp;
549 int err;
551 if (skb->len < ihlen + 8) {
552 ICMP_INC_STATS_BH(&init_net, ICMP_MIB_INERRORS);
553 return;
556 /* Fix up skb to look at the embedded net header. */
557 saveip = skb->network_header;
558 savesctp = skb->transport_header;
559 skb_reset_network_header(skb);
560 skb_set_transport_header(skb, ihlen);
561 sk = sctp_err_lookup(AF_INET, skb, sctp_hdr(skb), &asoc, &transport);
562 /* Put back, the original values. */
563 skb->network_header = saveip;
564 skb->transport_header = savesctp;
565 if (!sk) {
566 ICMP_INC_STATS_BH(&init_net, ICMP_MIB_INERRORS);
567 return;
569 /* Warning: The sock lock is held. Remember to call
570 * sctp_err_finish!
573 switch (type) {
574 case ICMP_PARAMETERPROB:
575 err = EPROTO;
576 break;
577 case ICMP_DEST_UNREACH:
578 if (code > NR_ICMP_UNREACH)
579 goto out_unlock;
581 /* PMTU discovery (RFC1191) */
582 if (ICMP_FRAG_NEEDED == code) {
583 sctp_icmp_frag_needed(sk, asoc, transport, info);
584 goto out_unlock;
586 else {
587 if (ICMP_PROT_UNREACH == code) {
588 sctp_icmp_proto_unreachable(sk, asoc,
589 transport);
590 goto out_unlock;
593 err = icmp_err_convert[code].errno;
594 break;
595 case ICMP_TIME_EXCEEDED:
596 /* Ignore any time exceeded errors due to fragment reassembly
597 * timeouts.
599 if (ICMP_EXC_FRAGTIME == code)
600 goto out_unlock;
602 err = EHOSTUNREACH;
603 break;
604 default:
605 goto out_unlock;
608 inet = inet_sk(sk);
609 if (!sock_owned_by_user(sk) && inet->recverr) {
610 sk->sk_err = err;
611 sk->sk_error_report(sk);
612 } else { /* Only an error on timeout */
613 sk->sk_err_soft = err;
616 out_unlock:
617 sctp_err_finish(sk, asoc);
621 * RFC 2960, 8.4 - Handle "Out of the blue" Packets.
623 * This function scans all the chunks in the OOTB packet to determine if
624 * the packet should be discarded right away. If a response might be needed
625 * for this packet, or, if further processing is possible, the packet will
626 * be queued to a proper inqueue for the next phase of handling.
628 * Output:
629 * Return 0 - If further processing is needed.
630 * Return 1 - If the packet can be discarded right away.
632 static int sctp_rcv_ootb(struct sk_buff *skb)
634 sctp_chunkhdr_t *ch;
635 __u8 *ch_end;
636 sctp_errhdr_t *err;
638 ch = (sctp_chunkhdr_t *) skb->data;
640 /* Scan through all the chunks in the packet. */
641 do {
642 /* Break out if chunk length is less then minimal. */
643 if (ntohs(ch->length) < sizeof(sctp_chunkhdr_t))
644 break;
646 ch_end = ((__u8 *)ch) + WORD_ROUND(ntohs(ch->length));
647 if (ch_end > skb_tail_pointer(skb))
648 break;
650 /* RFC 8.4, 2) If the OOTB packet contains an ABORT chunk, the
651 * receiver MUST silently discard the OOTB packet and take no
652 * further action.
654 if (SCTP_CID_ABORT == ch->type)
655 goto discard;
657 /* RFC 8.4, 6) If the packet contains a SHUTDOWN COMPLETE
658 * chunk, the receiver should silently discard the packet
659 * and take no further action.
661 if (SCTP_CID_SHUTDOWN_COMPLETE == ch->type)
662 goto discard;
664 /* RFC 4460, 2.11.2
665 * This will discard packets with INIT chunk bundled as
666 * subsequent chunks in the packet. When INIT is first,
667 * the normal INIT processing will discard the chunk.
669 if (SCTP_CID_INIT == ch->type && (void *)ch != skb->data)
670 goto discard;
672 /* RFC 8.4, 7) If the packet contains a "Stale cookie" ERROR
673 * or a COOKIE ACK the SCTP Packet should be silently
674 * discarded.
676 if (SCTP_CID_COOKIE_ACK == ch->type)
677 goto discard;
679 if (SCTP_CID_ERROR == ch->type) {
680 sctp_walk_errors(err, ch) {
681 if (SCTP_ERROR_STALE_COOKIE == err->cause)
682 goto discard;
686 ch = (sctp_chunkhdr_t *) ch_end;
687 } while (ch_end < skb_tail_pointer(skb));
689 return 0;
691 discard:
692 return 1;
695 /* Insert endpoint into the hash table. */
696 static void __sctp_hash_endpoint(struct sctp_endpoint *ep)
698 struct sctp_ep_common *epb;
699 struct sctp_hashbucket *head;
701 epb = &ep->base;
703 epb->hashent = sctp_ep_hashfn(epb->bind_addr.port);
704 head = &sctp_ep_hashtable[epb->hashent];
706 sctp_write_lock(&head->lock);
707 hlist_add_head(&epb->node, &head->chain);
708 sctp_write_unlock(&head->lock);
711 /* Add an endpoint to the hash. Local BH-safe. */
712 void sctp_hash_endpoint(struct sctp_endpoint *ep)
714 sctp_local_bh_disable();
715 __sctp_hash_endpoint(ep);
716 sctp_local_bh_enable();
719 /* Remove endpoint from the hash table. */
720 static void __sctp_unhash_endpoint(struct sctp_endpoint *ep)
722 struct sctp_hashbucket *head;
723 struct sctp_ep_common *epb;
725 epb = &ep->base;
727 if (hlist_unhashed(&epb->node))
728 return;
730 epb->hashent = sctp_ep_hashfn(epb->bind_addr.port);
732 head = &sctp_ep_hashtable[epb->hashent];
734 sctp_write_lock(&head->lock);
735 __hlist_del(&epb->node);
736 sctp_write_unlock(&head->lock);
739 /* Remove endpoint from the hash. Local BH-safe. */
740 void sctp_unhash_endpoint(struct sctp_endpoint *ep)
742 sctp_local_bh_disable();
743 __sctp_unhash_endpoint(ep);
744 sctp_local_bh_enable();
747 /* Look up an endpoint. */
748 static struct sctp_endpoint *__sctp_rcv_lookup_endpoint(const union sctp_addr *laddr)
750 struct sctp_hashbucket *head;
751 struct sctp_ep_common *epb;
752 struct sctp_endpoint *ep;
753 struct hlist_node *node;
754 int hash;
756 hash = sctp_ep_hashfn(ntohs(laddr->v4.sin_port));
757 head = &sctp_ep_hashtable[hash];
758 read_lock(&head->lock);
759 sctp_for_each_hentry(epb, node, &head->chain) {
760 ep = sctp_ep(epb);
761 if (sctp_endpoint_is_match(ep, laddr))
762 goto hit;
765 ep = sctp_sk((sctp_get_ctl_sock()))->ep;
767 hit:
768 sctp_endpoint_hold(ep);
769 read_unlock(&head->lock);
770 return ep;
773 /* Insert association into the hash table. */
774 static void __sctp_hash_established(struct sctp_association *asoc)
776 struct sctp_ep_common *epb;
777 struct sctp_hashbucket *head;
779 epb = &asoc->base;
781 /* Calculate which chain this entry will belong to. */
782 epb->hashent = sctp_assoc_hashfn(epb->bind_addr.port, asoc->peer.port);
784 head = &sctp_assoc_hashtable[epb->hashent];
786 sctp_write_lock(&head->lock);
787 hlist_add_head(&epb->node, &head->chain);
788 sctp_write_unlock(&head->lock);
791 /* Add an association to the hash. Local BH-safe. */
792 void sctp_hash_established(struct sctp_association *asoc)
794 if (asoc->temp)
795 return;
797 sctp_local_bh_disable();
798 __sctp_hash_established(asoc);
799 sctp_local_bh_enable();
802 /* Remove association from the hash table. */
803 static void __sctp_unhash_established(struct sctp_association *asoc)
805 struct sctp_hashbucket *head;
806 struct sctp_ep_common *epb;
808 epb = &asoc->base;
810 epb->hashent = sctp_assoc_hashfn(epb->bind_addr.port,
811 asoc->peer.port);
813 head = &sctp_assoc_hashtable[epb->hashent];
815 sctp_write_lock(&head->lock);
816 __hlist_del(&epb->node);
817 sctp_write_unlock(&head->lock);
820 /* Remove association from the hash table. Local BH-safe. */
821 void sctp_unhash_established(struct sctp_association *asoc)
823 if (asoc->temp)
824 return;
826 sctp_local_bh_disable();
827 __sctp_unhash_established(asoc);
828 sctp_local_bh_enable();
831 /* Look up an association. */
832 static struct sctp_association *__sctp_lookup_association(
833 const union sctp_addr *local,
834 const union sctp_addr *peer,
835 struct sctp_transport **pt)
837 struct sctp_hashbucket *head;
838 struct sctp_ep_common *epb;
839 struct sctp_association *asoc;
840 struct sctp_transport *transport;
841 struct hlist_node *node;
842 int hash;
844 /* Optimize here for direct hit, only listening connections can
845 * have wildcards anyways.
847 hash = sctp_assoc_hashfn(ntohs(local->v4.sin_port), ntohs(peer->v4.sin_port));
848 head = &sctp_assoc_hashtable[hash];
849 read_lock(&head->lock);
850 sctp_for_each_hentry(epb, node, &head->chain) {
851 asoc = sctp_assoc(epb);
852 transport = sctp_assoc_is_match(asoc, local, peer);
853 if (transport)
854 goto hit;
857 read_unlock(&head->lock);
859 return NULL;
861 hit:
862 *pt = transport;
863 sctp_association_hold(asoc);
864 read_unlock(&head->lock);
865 return asoc;
868 /* Look up an association. BH-safe. */
869 SCTP_STATIC
870 struct sctp_association *sctp_lookup_association(const union sctp_addr *laddr,
871 const union sctp_addr *paddr,
872 struct sctp_transport **transportp)
874 struct sctp_association *asoc;
876 sctp_local_bh_disable();
877 asoc = __sctp_lookup_association(laddr, paddr, transportp);
878 sctp_local_bh_enable();
880 return asoc;
883 /* Is there an association matching the given local and peer addresses? */
884 int sctp_has_association(const union sctp_addr *laddr,
885 const union sctp_addr *paddr)
887 struct sctp_association *asoc;
888 struct sctp_transport *transport;
890 if ((asoc = sctp_lookup_association(laddr, paddr, &transport))) {
891 sctp_association_put(asoc);
892 return 1;
895 return 0;
899 * SCTP Implementors Guide, 2.18 Handling of address
900 * parameters within the INIT or INIT-ACK.
902 * D) When searching for a matching TCB upon reception of an INIT
903 * or INIT-ACK chunk the receiver SHOULD use not only the
904 * source address of the packet (containing the INIT or
905 * INIT-ACK) but the receiver SHOULD also use all valid
906 * address parameters contained within the chunk.
908 * 2.18.3 Solution description
910 * This new text clearly specifies to an implementor the need
911 * to look within the INIT or INIT-ACK. Any implementation that
912 * does not do this, may not be able to establish associations
913 * in certain circumstances.
916 static struct sctp_association *__sctp_rcv_init_lookup(struct sk_buff *skb,
917 const union sctp_addr *laddr, struct sctp_transport **transportp)
919 struct sctp_association *asoc;
920 union sctp_addr addr;
921 union sctp_addr *paddr = &addr;
922 struct sctphdr *sh = sctp_hdr(skb);
923 sctp_chunkhdr_t *ch;
924 union sctp_params params;
925 sctp_init_chunk_t *init;
926 struct sctp_transport *transport;
927 struct sctp_af *af;
929 ch = (sctp_chunkhdr_t *) skb->data;
932 * This code will NOT touch anything inside the chunk--it is
933 * strictly READ-ONLY.
935 * RFC 2960 3 SCTP packet Format
937 * Multiple chunks can be bundled into one SCTP packet up to
938 * the MTU size, except for the INIT, INIT ACK, and SHUTDOWN
939 * COMPLETE chunks. These chunks MUST NOT be bundled with any
940 * other chunk in a packet. See Section 6.10 for more details
941 * on chunk bundling.
944 /* Find the start of the TLVs and the end of the chunk. This is
945 * the region we search for address parameters.
947 init = (sctp_init_chunk_t *)skb->data;
949 /* Walk the parameters looking for embedded addresses. */
950 sctp_walk_params(params, init, init_hdr.params) {
952 /* Note: Ignoring hostname addresses. */
953 af = sctp_get_af_specific(param_type2af(params.p->type));
954 if (!af)
955 continue;
957 af->from_addr_param(paddr, params.addr, sh->source, 0);
959 asoc = __sctp_lookup_association(laddr, paddr, &transport);
960 if (asoc)
961 return asoc;
964 return NULL;
967 /* ADD-IP, Section 5.2
968 * When an endpoint receives an ASCONF Chunk from the remote peer
969 * special procedures may be needed to identify the association the
970 * ASCONF Chunk is associated with. To properly find the association
971 * the following procedures SHOULD be followed:
973 * D2) If the association is not found, use the address found in the
974 * Address Parameter TLV combined with the port number found in the
975 * SCTP common header. If found proceed to rule D4.
977 * D2-ext) If more than one ASCONF Chunks are packed together, use the
978 * address found in the ASCONF Address Parameter TLV of each of the
979 * subsequent ASCONF Chunks. If found, proceed to rule D4.
981 static struct sctp_association *__sctp_rcv_asconf_lookup(
982 sctp_chunkhdr_t *ch,
983 const union sctp_addr *laddr,
984 __be16 peer_port,
985 struct sctp_transport **transportp)
987 sctp_addip_chunk_t *asconf = (struct sctp_addip_chunk *)ch;
988 struct sctp_af *af;
989 union sctp_addr_param *param;
990 union sctp_addr paddr;
992 /* Skip over the ADDIP header and find the Address parameter */
993 param = (union sctp_addr_param *)(asconf + 1);
995 af = sctp_get_af_specific(param_type2af(param->v4.param_hdr.type));
996 if (unlikely(!af))
997 return NULL;
999 af->from_addr_param(&paddr, param, peer_port, 0);
1001 return __sctp_lookup_association(laddr, &paddr, transportp);
1005 /* SCTP-AUTH, Section 6.3:
1006 * If the receiver does not find a STCB for a packet containing an AUTH
1007 * chunk as the first chunk and not a COOKIE-ECHO chunk as the second
1008 * chunk, it MUST use the chunks after the AUTH chunk to look up an existing
1009 * association.
1011 * This means that any chunks that can help us identify the association need
1012 * to be looked at to find this assocation.
1014 static struct sctp_association *__sctp_rcv_walk_lookup(struct sk_buff *skb,
1015 const union sctp_addr *laddr,
1016 struct sctp_transport **transportp)
1018 struct sctp_association *asoc = NULL;
1019 sctp_chunkhdr_t *ch;
1020 int have_auth = 0;
1021 unsigned int chunk_num = 1;
1022 __u8 *ch_end;
1024 /* Walk through the chunks looking for AUTH or ASCONF chunks
1025 * to help us find the association.
1027 ch = (sctp_chunkhdr_t *) skb->data;
1028 do {
1029 /* Break out if chunk length is less then minimal. */
1030 if (ntohs(ch->length) < sizeof(sctp_chunkhdr_t))
1031 break;
1033 ch_end = ((__u8 *)ch) + WORD_ROUND(ntohs(ch->length));
1034 if (ch_end > skb_tail_pointer(skb))
1035 break;
1037 switch(ch->type) {
1038 case SCTP_CID_AUTH:
1039 have_auth = chunk_num;
1040 break;
1042 case SCTP_CID_COOKIE_ECHO:
1043 /* If a packet arrives containing an AUTH chunk as
1044 * a first chunk, a COOKIE-ECHO chunk as the second
1045 * chunk, and possibly more chunks after them, and
1046 * the receiver does not have an STCB for that
1047 * packet, then authentication is based on
1048 * the contents of the COOKIE- ECHO chunk.
1050 if (have_auth == 1 && chunk_num == 2)
1051 return NULL;
1052 break;
1054 case SCTP_CID_ASCONF:
1055 if (have_auth || sctp_addip_noauth)
1056 asoc = __sctp_rcv_asconf_lookup(ch, laddr,
1057 sctp_hdr(skb)->source,
1058 transportp);
1059 default:
1060 break;
1063 if (asoc)
1064 break;
1066 ch = (sctp_chunkhdr_t *) ch_end;
1067 chunk_num++;
1068 } while (ch_end < skb_tail_pointer(skb));
1070 return asoc;
1074 * There are circumstances when we need to look inside the SCTP packet
1075 * for information to help us find the association. Examples
1076 * include looking inside of INIT/INIT-ACK chunks or after the AUTH
1077 * chunks.
1079 static struct sctp_association *__sctp_rcv_lookup_harder(struct sk_buff *skb,
1080 const union sctp_addr *laddr,
1081 struct sctp_transport **transportp)
1083 sctp_chunkhdr_t *ch;
1085 ch = (sctp_chunkhdr_t *) skb->data;
1087 /* The code below will attempt to walk the chunk and extract
1088 * parameter information. Before we do that, we need to verify
1089 * that the chunk length doesn't cause overflow. Otherwise, we'll
1090 * walk off the end.
1092 if (WORD_ROUND(ntohs(ch->length)) > skb->len)
1093 return NULL;
1095 /* If this is INIT/INIT-ACK look inside the chunk too. */
1096 switch (ch->type) {
1097 case SCTP_CID_INIT:
1098 case SCTP_CID_INIT_ACK:
1099 return __sctp_rcv_init_lookup(skb, laddr, transportp);
1100 break;
1102 default:
1103 return __sctp_rcv_walk_lookup(skb, laddr, transportp);
1104 break;
1108 return NULL;
1111 /* Lookup an association for an inbound skb. */
1112 static struct sctp_association *__sctp_rcv_lookup(struct sk_buff *skb,
1113 const union sctp_addr *paddr,
1114 const union sctp_addr *laddr,
1115 struct sctp_transport **transportp)
1117 struct sctp_association *asoc;
1119 asoc = __sctp_lookup_association(laddr, paddr, transportp);
1121 /* Further lookup for INIT/INIT-ACK packets.
1122 * SCTP Implementors Guide, 2.18 Handling of address
1123 * parameters within the INIT or INIT-ACK.
1125 if (!asoc)
1126 asoc = __sctp_rcv_lookup_harder(skb, laddr, transportp);
1128 return asoc;