blob: 238f1bffa6845e94b3e0bec1f6d5ad0da4c0e695 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* 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
8 *
9 * This file is part of the SCTP kernel reference Implementation
10 *
11 * These functions handle all input from the IP layer into SCTP.
12 *
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.
18 *
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.
24 *
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.
29 *
30 * Please send any bug reports or fixes you make to the
31 * email address(es):
32 * lksctp developers <lksctp-developers@lists.sourceforge.net>
33 *
34 * Or submit a bug report through the following website:
35 * http://www.sf.net/projects/lksctp
36 *
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>
46 *
47 * Any bugs reported given to us we will try to fix... any fixes shared will
48 * be incorporated into the next SCTP release.
49 */
50
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
64/* Forward declarations for internal helpers. */
65static int sctp_rcv_ootb(struct sk_buff *);
66static 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);
70static struct sctp_endpoint *__sctp_rcv_lookup_endpoint(const union sctp_addr *laddr);
71static struct sctp_association *__sctp_lookup_association(
72 const union sctp_addr *local,
73 const union sctp_addr *peer,
74 struct sctp_transport **pt);
75
76
77/* Calculate the SCTP checksum of an SCTP packet. */
78static inline int sctp_rcv_checksum(struct sk_buff *skb)
79{
80 struct sctphdr *sh;
81 __u32 cmp, val;
82 struct sk_buff *list = skb_shinfo(skb)->frag_list;
83
84 sh = (struct sctphdr *) skb->h.raw;
85 cmp = ntohl(sh->checksum);
86
87 val = sctp_start_cksum((__u8 *)sh, skb_headlen(skb));
88
89 for (; list; list = list->next)
90 val = sctp_update_cksum((__u8 *)list->data, skb_headlen(list),
91 val);
92
93 val = sctp_end_cksum(val);
94
95 if (val != cmp) {
96 /* CRC failure, dump it. */
97 SCTP_INC_STATS_BH(SCTP_MIB_CHECKSUMERRORS);
98 return -1;
99 }
100 return 0;
101}
102
David S. Miller79af02c2005-07-08 21:47:49 -0700103struct 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;
111};
112#define SCTP_INPUT_CB(__skb) ((struct sctp_input_cb *)&((__skb)->cb[0]))
113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114/*
115 * This is the routine which IP calls when receiving an SCTP packet.
116 */
117int sctp_rcv(struct sk_buff *skb)
118{
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;
130 int ret = 0;
131
132 if (skb->pkt_type!=PACKET_HOST)
133 goto discard_it;
134
135 SCTP_INC_STATS_BH(SCTP_MIB_INSCTPPACKS);
136
137 sh = (struct sctphdr *) skb->h.raw;
138
139 /* Pull up the IP and SCTP headers. */
140 __skb_pull(skb, skb->h.raw - skb->data);
141 if (skb->len < sizeof(struct sctphdr))
142 goto discard_it;
143 if (sctp_rcv_checksum(skb) < 0)
144 goto discard_it;
145
146 skb_pull(skb, sizeof(struct sctphdr));
147
148 /* Make sure we at least have chunk headers worth of data left. */
149 if (skb->len < sizeof(struct sctp_chunkhdr))
150 goto discard_it;
151
152 family = ipver2af(skb->nh.iph->version);
153 af = sctp_get_af_specific(family);
154 if (unlikely(!af))
155 goto discard_it;
156
157 /* Initialize local addresses for lookups. */
158 af->from_skb(&src, skb, 1);
159 af->from_skb(&dest, skb, 0);
160
161 /* If the packet is to or from a non-unicast address,
162 * silently discard the packet.
163 *
164 * This is not clearly defined in the RFC except in section
165 * 8.4 - OOTB handling. However, based on the book "Stream Control
166 * Transmission Protocol" 2.1, "It is important to note that the
167 * IP address of an SCTP transport address must be a routable
168 * unicast address. In other words, IP multicast addresses and
169 * IP broadcast addresses cannot be used in an SCTP transport
170 * address."
171 */
172 if (!af->addr_valid(&src, NULL) || !af->addr_valid(&dest, NULL))
173 goto discard_it;
174
175 asoc = __sctp_rcv_lookup(skb, &src, &dest, &transport);
176
Neil Horman0fd9a652005-06-13 15:11:24 -0700177 if (!asoc)
178 ep = __sctp_rcv_lookup_endpoint(&dest);
179
180 /* Retrieve the common input handling substructure. */
181 rcvr = asoc ? &asoc->base : &ep->base;
182 sk = rcvr->sk;
183
184 /*
185 * If a frame arrives on an interface and the receiving socket is
186 * bound to another interface, via SO_BINDTODEVICE, treat it as OOTB
187 */
188 if (sk->sk_bound_dev_if && (sk->sk_bound_dev_if != af->skb_iif(skb)))
189 {
190 sock_put(sk);
191 if (asoc) {
192 sctp_association_put(asoc);
193 asoc = NULL;
194 } else {
195 sctp_endpoint_put(ep);
196 ep = NULL;
197 }
198 sk = sctp_get_ctl_sock();
199 ep = sctp_sk(sk)->ep;
200 sctp_endpoint_hold(ep);
201 sock_hold(sk);
202 rcvr = &ep->base;
203 }
204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 /*
206 * RFC 2960, 8.4 - Handle "Out of the blue" Packets.
207 * An SCTP packet is called an "out of the blue" (OOTB)
208 * packet if it is correctly formed, i.e., passed the
209 * receiver's checksum check, but the receiver is not
210 * able to identify the association to which this
211 * packet belongs.
212 */
213 if (!asoc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 if (sctp_rcv_ootb(skb)) {
215 SCTP_INC_STATS_BH(SCTP_MIB_OUTOFBLUES);
216 goto discard_release;
217 }
218 }
219
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 /* SCTP seems to always need a timestamp right now (FIXME) */
Patrick McHardya61bbcf2005-08-14 17:24:31 -0700221 if (skb->tstamp.off_sec == 0) {
222 __net_timestamp(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 sock_enable_timestamp(sk);
224 }
225
226 if (!xfrm_policy_check(sk, XFRM_POLICY_IN, skb, family))
227 goto discard_release;
228
229 ret = sk_filter(sk, skb, 1);
230 if (ret)
231 goto discard_release;
232
233 /* Create an SCTP packet structure. */
234 chunk = sctp_chunkify(skb, asoc, sk);
235 if (!chunk) {
236 ret = -ENOMEM;
237 goto discard_release;
238 }
David S. Miller79af02c2005-07-08 21:47:49 -0700239 SCTP_INPUT_CB(skb)->chunk = chunk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 /* Remember what endpoint is to handle this packet. */
242 chunk->rcvr = rcvr;
243
244 /* Remember the SCTP header. */
245 chunk->sctp_hdr = sh;
246
247 /* Set the source and destination addresses of the incoming chunk. */
248 sctp_init_addrs(chunk, &src, &dest);
249
250 /* Remember where we came from. */
251 chunk->transport = transport;
252
253 /* Acquire access to the sock lock. Note: We are safe from other
254 * bottom halves on this lock, but a user may be in the lock too,
255 * so check if it is busy.
256 */
257 sctp_bh_lock_sock(sk);
258
259 if (sock_owned_by_user(sk))
David S. Miller79af02c2005-07-08 21:47:49 -0700260 sk_add_backlog(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 else
David S. Miller79af02c2005-07-08 21:47:49 -0700262 sctp_backlog_rcv(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
264 /* Release the sock and any reference counts we took in the
265 * lookup calls.
266 */
267 sctp_bh_unlock_sock(sk);
268 if (asoc)
269 sctp_association_put(asoc);
270 else
271 sctp_endpoint_put(ep);
272 sock_put(sk);
273 return ret;
274
275discard_it:
276 kfree_skb(skb);
277 return ret;
278
279discard_release:
280 /* Release any structures we may be holding. */
Neil Horman0fd9a652005-06-13 15:11:24 -0700281 sock_put(sk);
282 if (asoc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 sctp_association_put(asoc);
Neil Horman0fd9a652005-06-13 15:11:24 -0700284 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 sctp_endpoint_put(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
287 goto discard_it;
288}
289
290/* Handle second half of inbound skb processing. If the sock was busy,
291 * we may have need to delay processing until later when the sock is
292 * released (on the backlog). If not busy, we call this routine
293 * directly from the bottom half.
294 */
295int sctp_backlog_rcv(struct sock *sk, struct sk_buff *skb)
296{
David S. Miller79af02c2005-07-08 21:47:49 -0700297 struct sctp_chunk *chunk = SCTP_INPUT_CB(skb)->chunk;
298 struct sctp_inq *inqueue = &chunk->rcvr->inqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
300 sctp_inq_push(inqueue, chunk);
301 return 0;
302}
303
304/* Handle icmp frag needed error. */
305void sctp_icmp_frag_needed(struct sock *sk, struct sctp_association *asoc,
306 struct sctp_transport *t, __u32 pmtu)
307{
Frank Filz52ccb8e2005-12-22 11:36:46 -0800308 if (sock_owned_by_user(sk) || !t || (t->pathmtu == pmtu))
309 return;
310
311 if (t->param_flags & SPP_PMTUD_ENABLE) {
312 if (unlikely(pmtu < SCTP_DEFAULT_MINSEGMENT)) {
313 printk(KERN_WARNING "%s: Reported pmtu %d too low, "
314 "using default minimum of %d\n",
315 __FUNCTION__, pmtu,
316 SCTP_DEFAULT_MINSEGMENT);
317 /* Use default minimum segment size and disable
318 * pmtu discovery on this transport.
319 */
320 t->pathmtu = SCTP_DEFAULT_MINSEGMENT;
321 t->param_flags = (t->param_flags & ~SPP_HB) |
322 SPP_PMTUD_DISABLE;
323 } else {
324 t->pathmtu = pmtu;
325 }
326
327 /* Update association pmtu. */
328 sctp_assoc_sync_pmtu(asoc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 }
330
Frank Filz52ccb8e2005-12-22 11:36:46 -0800331 /* Retransmit with the new pmtu setting.
332 * Normally, if PMTU discovery is disabled, an ICMP Fragmentation
333 * Needed will never be sent, but if a message was sent before
334 * PMTU discovery was disabled that was larger than the PMTU, it
335 * would not be fragmented, so it must be re-transmitted fragmented.
336 */
337 sctp_retransmit(&asoc->outqueue, t, SCTP_RTXR_PMTUD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338}
339
340/*
341 * SCTP Implementer's Guide, 2.37 ICMP handling procedures
342 *
343 * ICMP8) If the ICMP code is a "Unrecognized next header type encountered"
344 * or a "Protocol Unreachable" treat this message as an abort
345 * with the T bit set.
346 *
347 * This function sends an event to the state machine, which will abort the
348 * association.
349 *
350 */
351void sctp_icmp_proto_unreachable(struct sock *sk,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 struct sctp_association *asoc,
353 struct sctp_transport *t)
354{
355 SCTP_DEBUG_PRINTK("%s\n", __FUNCTION__);
356
357 sctp_do_sm(SCTP_EVENT_T_OTHER,
358 SCTP_ST_OTHER(SCTP_EVENT_ICMP_PROTO_UNREACH),
Frank Filz3f7a87d2005-06-20 13:14:57 -0700359 asoc->state, asoc->ep, asoc, t,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 GFP_ATOMIC);
361
362}
363
364/* Common lookup code for icmp/icmpv6 error handler. */
365struct sock *sctp_err_lookup(int family, struct sk_buff *skb,
366 struct sctphdr *sctphdr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 struct sctp_association **app,
368 struct sctp_transport **tpp)
369{
370 union sctp_addr saddr;
371 union sctp_addr daddr;
372 struct sctp_af *af;
373 struct sock *sk = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 struct sctp_association *asoc = NULL;
375 struct sctp_transport *transport = NULL;
376
Sridhar Samudralad1ad1ff2005-07-18 13:44:10 -0700377 *app = NULL; *tpp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
379 af = sctp_get_af_specific(family);
380 if (unlikely(!af)) {
381 return NULL;
382 }
383
384 /* Initialize local addresses for lookups. */
385 af->from_skb(&saddr, skb, 1);
386 af->from_skb(&daddr, skb, 0);
387
388 /* Look for an association that matches the incoming ICMP error
389 * packet.
390 */
391 asoc = __sctp_lookup_association(&saddr, &daddr, &transport);
Sridhar Samudralad1ad1ff2005-07-18 13:44:10 -0700392 if (!asoc)
393 return NULL;
394
395 sk = asoc->base.sk;
396
397 if (ntohl(sctphdr->vtag) != asoc->c.peer_vtag) {
398 ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
399 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 }
401
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 sctp_bh_lock_sock(sk);
403
404 /* If too many ICMPs get dropped on busy
405 * servers this needs to be solved differently.
406 */
407 if (sock_owned_by_user(sk))
408 NET_INC_STATS_BH(LINUX_MIB_LOCKDROPPEDICMPS);
409
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 *app = asoc;
411 *tpp = transport;
412 return sk;
413
414out:
415 sock_put(sk);
416 if (asoc)
417 sctp_association_put(asoc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 return NULL;
419}
420
421/* Common cleanup code for icmp/icmpv6 error handler. */
Sridhar Samudralad1ad1ff2005-07-18 13:44:10 -0700422void sctp_err_finish(struct sock *sk, struct sctp_association *asoc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423{
424 sctp_bh_unlock_sock(sk);
425 sock_put(sk);
426 if (asoc)
427 sctp_association_put(asoc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428}
429
430/*
431 * This routine is called by the ICMP module when it gets some
432 * sort of error condition. If err < 0 then the socket should
433 * be closed and the error returned to the user. If err > 0
434 * it's just the icmp type << 8 | icmp code. After adjustment
435 * header points to the first 8 bytes of the sctp header. We need
436 * to find the appropriate port.
437 *
438 * The locking strategy used here is very "optimistic". When
439 * someone else accesses the socket the ICMP is just dropped
440 * and for some paths there is no check at all.
441 * A more general error queue to queue errors for later handling
442 * is probably better.
443 *
444 */
445void sctp_v4_err(struct sk_buff *skb, __u32 info)
446{
447 struct iphdr *iph = (struct iphdr *)skb->data;
448 struct sctphdr *sh = (struct sctphdr *)(skb->data + (iph->ihl <<2));
449 int type = skb->h.icmph->type;
450 int code = skb->h.icmph->code;
451 struct sock *sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 struct sctp_association *asoc;
453 struct sctp_transport *transport;
454 struct inet_sock *inet;
455 char *saveip, *savesctp;
456 int err;
457
458 if (skb->len < ((iph->ihl << 2) + 8)) {
459 ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
460 return;
461 }
462
463 /* Fix up skb to look at the embedded net header. */
464 saveip = skb->nh.raw;
465 savesctp = skb->h.raw;
466 skb->nh.iph = iph;
467 skb->h.raw = (char *)sh;
Sridhar Samudralad1ad1ff2005-07-18 13:44:10 -0700468 sk = sctp_err_lookup(AF_INET, skb, sh, &asoc, &transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 /* Put back, the original pointers. */
470 skb->nh.raw = saveip;
471 skb->h.raw = savesctp;
472 if (!sk) {
473 ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
474 return;
475 }
476 /* Warning: The sock lock is held. Remember to call
477 * sctp_err_finish!
478 */
479
480 switch (type) {
481 case ICMP_PARAMETERPROB:
482 err = EPROTO;
483 break;
484 case ICMP_DEST_UNREACH:
485 if (code > NR_ICMP_UNREACH)
486 goto out_unlock;
487
488 /* PMTU discovery (RFC1191) */
489 if (ICMP_FRAG_NEEDED == code) {
490 sctp_icmp_frag_needed(sk, asoc, transport, info);
491 goto out_unlock;
492 }
493 else {
494 if (ICMP_PROT_UNREACH == code) {
Sridhar Samudralad1ad1ff2005-07-18 13:44:10 -0700495 sctp_icmp_proto_unreachable(sk, asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 transport);
497 goto out_unlock;
498 }
499 }
500 err = icmp_err_convert[code].errno;
501 break;
502 case ICMP_TIME_EXCEEDED:
503 /* Ignore any time exceeded errors due to fragment reassembly
504 * timeouts.
505 */
506 if (ICMP_EXC_FRAGTIME == code)
507 goto out_unlock;
508
509 err = EHOSTUNREACH;
510 break;
511 default:
512 goto out_unlock;
513 }
514
515 inet = inet_sk(sk);
516 if (!sock_owned_by_user(sk) && inet->recverr) {
517 sk->sk_err = err;
518 sk->sk_error_report(sk);
519 } else { /* Only an error on timeout */
520 sk->sk_err_soft = err;
521 }
522
523out_unlock:
Sridhar Samudralad1ad1ff2005-07-18 13:44:10 -0700524 sctp_err_finish(sk, asoc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525}
526
527/*
528 * RFC 2960, 8.4 - Handle "Out of the blue" Packets.
529 *
530 * This function scans all the chunks in the OOTB packet to determine if
531 * the packet should be discarded right away. If a response might be needed
532 * for this packet, or, if further processing is possible, the packet will
533 * be queued to a proper inqueue for the next phase of handling.
534 *
535 * Output:
536 * Return 0 - If further processing is needed.
537 * Return 1 - If the packet can be discarded right away.
538 */
539int sctp_rcv_ootb(struct sk_buff *skb)
540{
541 sctp_chunkhdr_t *ch;
542 __u8 *ch_end;
543 sctp_errhdr_t *err;
544
545 ch = (sctp_chunkhdr_t *) skb->data;
546 ch_end = ((__u8 *) ch) + WORD_ROUND(ntohs(ch->length));
547
548 /* Scan through all the chunks in the packet. */
549 while (ch_end > (__u8 *)ch && ch_end < skb->tail) {
550
551 /* RFC 8.4, 2) If the OOTB packet contains an ABORT chunk, the
552 * receiver MUST silently discard the OOTB packet and take no
553 * further action.
554 */
555 if (SCTP_CID_ABORT == ch->type)
556 goto discard;
557
558 /* RFC 8.4, 6) If the packet contains a SHUTDOWN COMPLETE
559 * chunk, the receiver should silently discard the packet
560 * and take no further action.
561 */
562 if (SCTP_CID_SHUTDOWN_COMPLETE == ch->type)
563 goto discard;
564
565 /* RFC 8.4, 7) If the packet contains a "Stale cookie" ERROR
566 * or a COOKIE ACK the SCTP Packet should be silently
567 * discarded.
568 */
569 if (SCTP_CID_COOKIE_ACK == ch->type)
570 goto discard;
571
572 if (SCTP_CID_ERROR == ch->type) {
573 sctp_walk_errors(err, ch) {
574 if (SCTP_ERROR_STALE_COOKIE == err->cause)
575 goto discard;
576 }
577 }
578
579 ch = (sctp_chunkhdr_t *) ch_end;
580 ch_end = ((__u8 *) ch) + WORD_ROUND(ntohs(ch->length));
581 }
582
583 return 0;
584
585discard:
586 return 1;
587}
588
589/* Insert endpoint into the hash table. */
590static void __sctp_hash_endpoint(struct sctp_endpoint *ep)
591{
592 struct sctp_ep_common **epp;
593 struct sctp_ep_common *epb;
594 struct sctp_hashbucket *head;
595
596 epb = &ep->base;
597
598 epb->hashent = sctp_ep_hashfn(epb->bind_addr.port);
599 head = &sctp_ep_hashtable[epb->hashent];
600
601 sctp_write_lock(&head->lock);
602 epp = &head->chain;
603 epb->next = *epp;
604 if (epb->next)
605 (*epp)->pprev = &epb->next;
606 *epp = epb;
607 epb->pprev = epp;
608 sctp_write_unlock(&head->lock);
609}
610
611/* Add an endpoint to the hash. Local BH-safe. */
612void sctp_hash_endpoint(struct sctp_endpoint *ep)
613{
614 sctp_local_bh_disable();
615 __sctp_hash_endpoint(ep);
616 sctp_local_bh_enable();
617}
618
619/* Remove endpoint from the hash table. */
620static void __sctp_unhash_endpoint(struct sctp_endpoint *ep)
621{
622 struct sctp_hashbucket *head;
623 struct sctp_ep_common *epb;
624
625 epb = &ep->base;
626
627 epb->hashent = sctp_ep_hashfn(epb->bind_addr.port);
628
629 head = &sctp_ep_hashtable[epb->hashent];
630
631 sctp_write_lock(&head->lock);
632
633 if (epb->pprev) {
634 if (epb->next)
635 epb->next->pprev = epb->pprev;
636 *epb->pprev = epb->next;
637 epb->pprev = NULL;
638 }
639
640 sctp_write_unlock(&head->lock);
641}
642
643/* Remove endpoint from the hash. Local BH-safe. */
644void sctp_unhash_endpoint(struct sctp_endpoint *ep)
645{
646 sctp_local_bh_disable();
647 __sctp_unhash_endpoint(ep);
648 sctp_local_bh_enable();
649}
650
651/* Look up an endpoint. */
652static struct sctp_endpoint *__sctp_rcv_lookup_endpoint(const union sctp_addr *laddr)
653{
654 struct sctp_hashbucket *head;
655 struct sctp_ep_common *epb;
656 struct sctp_endpoint *ep;
657 int hash;
658
659 hash = sctp_ep_hashfn(laddr->v4.sin_port);
660 head = &sctp_ep_hashtable[hash];
661 read_lock(&head->lock);
662 for (epb = head->chain; epb; epb = epb->next) {
663 ep = sctp_ep(epb);
664 if (sctp_endpoint_is_match(ep, laddr))
665 goto hit;
666 }
667
668 ep = sctp_sk((sctp_get_ctl_sock()))->ep;
669 epb = &ep->base;
670
671hit:
672 sctp_endpoint_hold(ep);
673 sock_hold(epb->sk);
674 read_unlock(&head->lock);
675 return ep;
676}
677
678/* Insert association into the hash table. */
679static void __sctp_hash_established(struct sctp_association *asoc)
680{
681 struct sctp_ep_common **epp;
682 struct sctp_ep_common *epb;
683 struct sctp_hashbucket *head;
684
685 epb = &asoc->base;
686
687 /* Calculate which chain this entry will belong to. */
688 epb->hashent = sctp_assoc_hashfn(epb->bind_addr.port, asoc->peer.port);
689
690 head = &sctp_assoc_hashtable[epb->hashent];
691
692 sctp_write_lock(&head->lock);
693 epp = &head->chain;
694 epb->next = *epp;
695 if (epb->next)
696 (*epp)->pprev = &epb->next;
697 *epp = epb;
698 epb->pprev = epp;
699 sctp_write_unlock(&head->lock);
700}
701
702/* Add an association to the hash. Local BH-safe. */
703void sctp_hash_established(struct sctp_association *asoc)
704{
705 sctp_local_bh_disable();
706 __sctp_hash_established(asoc);
707 sctp_local_bh_enable();
708}
709
710/* Remove association from the hash table. */
711static void __sctp_unhash_established(struct sctp_association *asoc)
712{
713 struct sctp_hashbucket *head;
714 struct sctp_ep_common *epb;
715
716 epb = &asoc->base;
717
718 epb->hashent = sctp_assoc_hashfn(epb->bind_addr.port,
719 asoc->peer.port);
720
721 head = &sctp_assoc_hashtable[epb->hashent];
722
723 sctp_write_lock(&head->lock);
724
725 if (epb->pprev) {
726 if (epb->next)
727 epb->next->pprev = epb->pprev;
728 *epb->pprev = epb->next;
729 epb->pprev = NULL;
730 }
731
732 sctp_write_unlock(&head->lock);
733}
734
735/* Remove association from the hash table. Local BH-safe. */
736void sctp_unhash_established(struct sctp_association *asoc)
737{
738 sctp_local_bh_disable();
739 __sctp_unhash_established(asoc);
740 sctp_local_bh_enable();
741}
742
743/* Look up an association. */
744static struct sctp_association *__sctp_lookup_association(
745 const union sctp_addr *local,
746 const union sctp_addr *peer,
747 struct sctp_transport **pt)
748{
749 struct sctp_hashbucket *head;
750 struct sctp_ep_common *epb;
751 struct sctp_association *asoc;
752 struct sctp_transport *transport;
753 int hash;
754
755 /* Optimize here for direct hit, only listening connections can
756 * have wildcards anyways.
757 */
758 hash = sctp_assoc_hashfn(local->v4.sin_port, peer->v4.sin_port);
759 head = &sctp_assoc_hashtable[hash];
760 read_lock(&head->lock);
761 for (epb = head->chain; epb; epb = epb->next) {
762 asoc = sctp_assoc(epb);
763 transport = sctp_assoc_is_match(asoc, local, peer);
764 if (transport)
765 goto hit;
766 }
767
768 read_unlock(&head->lock);
769
770 return NULL;
771
772hit:
773 *pt = transport;
774 sctp_association_hold(asoc);
775 sock_hold(epb->sk);
776 read_unlock(&head->lock);
777 return asoc;
778}
779
780/* Look up an association. BH-safe. */
781SCTP_STATIC
782struct sctp_association *sctp_lookup_association(const union sctp_addr *laddr,
783 const union sctp_addr *paddr,
784 struct sctp_transport **transportp)
785{
786 struct sctp_association *asoc;
787
788 sctp_local_bh_disable();
789 asoc = __sctp_lookup_association(laddr, paddr, transportp);
790 sctp_local_bh_enable();
791
792 return asoc;
793}
794
795/* Is there an association matching the given local and peer addresses? */
796int sctp_has_association(const union sctp_addr *laddr,
797 const union sctp_addr *paddr)
798{
799 struct sctp_association *asoc;
800 struct sctp_transport *transport;
801
802 if ((asoc = sctp_lookup_association(laddr, paddr, &transport))) {
803 sock_put(asoc->base.sk);
804 sctp_association_put(asoc);
805 return 1;
806 }
807
808 return 0;
809}
810
811/*
812 * SCTP Implementors Guide, 2.18 Handling of address
813 * parameters within the INIT or INIT-ACK.
814 *
815 * D) When searching for a matching TCB upon reception of an INIT
816 * or INIT-ACK chunk the receiver SHOULD use not only the
817 * source address of the packet (containing the INIT or
818 * INIT-ACK) but the receiver SHOULD also use all valid
819 * address parameters contained within the chunk.
820 *
821 * 2.18.3 Solution description
822 *
823 * This new text clearly specifies to an implementor the need
824 * to look within the INIT or INIT-ACK. Any implementation that
825 * does not do this, may not be able to establish associations
826 * in certain circumstances.
827 *
828 */
829static struct sctp_association *__sctp_rcv_init_lookup(struct sk_buff *skb,
830 const union sctp_addr *laddr, struct sctp_transport **transportp)
831{
832 struct sctp_association *asoc;
833 union sctp_addr addr;
834 union sctp_addr *paddr = &addr;
835 struct sctphdr *sh = (struct sctphdr *) skb->h.raw;
836 sctp_chunkhdr_t *ch;
837 union sctp_params params;
838 sctp_init_chunk_t *init;
839 struct sctp_transport *transport;
840 struct sctp_af *af;
841
842 ch = (sctp_chunkhdr_t *) skb->data;
843
844 /* If this is INIT/INIT-ACK look inside the chunk too. */
845 switch (ch->type) {
846 case SCTP_CID_INIT:
847 case SCTP_CID_INIT_ACK:
848 break;
849 default:
850 return NULL;
851 }
852
853 /* The code below will attempt to walk the chunk and extract
854 * parameter information. Before we do that, we need to verify
855 * that the chunk length doesn't cause overflow. Otherwise, we'll
856 * walk off the end.
857 */
858 if (WORD_ROUND(ntohs(ch->length)) > skb->len)
859 return NULL;
860
861 /*
862 * This code will NOT touch anything inside the chunk--it is
863 * strictly READ-ONLY.
864 *
865 * RFC 2960 3 SCTP packet Format
866 *
867 * Multiple chunks can be bundled into one SCTP packet up to
868 * the MTU size, except for the INIT, INIT ACK, and SHUTDOWN
869 * COMPLETE chunks. These chunks MUST NOT be bundled with any
870 * other chunk in a packet. See Section 6.10 for more details
871 * on chunk bundling.
872 */
873
874 /* Find the start of the TLVs and the end of the chunk. This is
875 * the region we search for address parameters.
876 */
877 init = (sctp_init_chunk_t *)skb->data;
878
879 /* Walk the parameters looking for embedded addresses. */
880 sctp_walk_params(params, init, init_hdr.params) {
881
882 /* Note: Ignoring hostname addresses. */
883 af = sctp_get_af_specific(param_type2af(params.p->type));
884 if (!af)
885 continue;
886
887 af->from_addr_param(paddr, params.addr, ntohs(sh->source), 0);
888
889 asoc = __sctp_lookup_association(laddr, paddr, &transport);
890 if (asoc)
891 return asoc;
892 }
893
894 return NULL;
895}
896
897/* Lookup an association for an inbound skb. */
898static struct sctp_association *__sctp_rcv_lookup(struct sk_buff *skb,
899 const union sctp_addr *paddr,
900 const union sctp_addr *laddr,
901 struct sctp_transport **transportp)
902{
903 struct sctp_association *asoc;
904
905 asoc = __sctp_lookup_association(laddr, paddr, transportp);
906
907 /* Further lookup for INIT/INIT-ACK packets.
908 * SCTP Implementors Guide, 2.18 Handling of address
909 * parameters within the INIT or INIT-ACK.
910 */
911 if (!asoc)
912 asoc = __sctp_rcv_init_lookup(skb, laddr, transportp);
913
914 return asoc;
915}