input.c 20 KB
Newer Older
Jon Grimm's avatar
Jon Grimm committed
1 2 3
/* SCTP kernel reference Implementation
 * Copyright (c) 1999-2000 Cisco, Inc.
 * Copyright (c) 1999-2001 Motorola, Inc.
4
 * Copyright (c) 2001-2003 International Business Machines, Corp.
Jon Grimm's avatar
Jon Grimm committed
5 6 7
 * Copyright (c) 2001 Intel Corp.
 * Copyright (c) 2001 Nokia, Inc.
 * Copyright (c) 2001 La Monte H.P. Yarroll
Jon Grimm's avatar
Jon Grimm committed
8
 *
Jon Grimm's avatar
Jon Grimm committed
9
 * This file is part of the SCTP kernel reference Implementation
Jon Grimm's avatar
Jon Grimm committed
10 11 12 13 14
 *
 * These functions handle all input from the IP layer into SCTP.
 *
 * The SCTP reference implementation is free software;
 * you can redistribute it and/or modify it under the terms of
Jon Grimm's avatar
Jon Grimm committed
15 16 17
 * the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
Jon Grimm's avatar
Jon Grimm committed
18 19
 *
 * The SCTP reference implementation is distributed in the hope that it
Jon Grimm's avatar
Jon Grimm committed
20 21 22 23
 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
 *                 ************************
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
Jon Grimm's avatar
Jon Grimm committed
24
 *
Jon Grimm's avatar
Jon Grimm committed
25 26 27
 * You should have received a copy of the GNU General Public License
 * along with GNU CC; see the file COPYING.  If not, write to
 * the Free Software Foundation, 59 Temple Place - Suite 330,
Jon Grimm's avatar
Jon Grimm committed
28 29
 * Boston, MA 02111-1307, USA.
 *
Jon Grimm's avatar
Jon Grimm committed
30 31 32
 * Please send any bug reports or fixes you make to the
 * email address(es):
 *    lksctp developers <lksctp-developers@lists.sourceforge.net>
Jon Grimm's avatar
Jon Grimm committed
33
 *
Jon Grimm's avatar
Jon Grimm committed
34 35 36
 * Or submit a bug report through the following website:
 *    http://www.sf.net/projects/lksctp
 *
Jon Grimm's avatar
Jon Grimm committed
37
 * Written or modified by:
Jon Grimm's avatar
Jon Grimm committed
38 39 40 41 42 43
 *    La Monte H.P. Yarroll <piggy@acm.org>
 *    Karl Knutson <karl@athena.chicago.il.us>
 *    Xingang Guo <xingang.guo@intel.com>
 *    Jon Grimm <jgrimm@us.ibm.com>
 *    Hui Huang <hui.huang@nokia.com>
 *    Daisy Chang <daisyc@us.ibm.com>
44
 *    Sridhar Samudrala <sri@us.ibm.com>
45
 *    Ardelle Fan <ardelle.fan@intel.com>
Jon Grimm's avatar
Jon Grimm committed
46
 *
Jon Grimm's avatar
Jon Grimm committed
47 48 49 50 51 52 53 54 55
 * Any bugs reported given to us we will try to fix... any fixes shared will
 * be incorporated into the next SCTP release.
 */

#include <linux/types.h>
#include <linux/list.h> /* For struct list_head */
#include <linux/socket.h>
#include <linux/ip.h>
#include <linux/time.h> /* For struct timeval */
56 57 58
#include <net/ip.h>
#include <net/icmp.h>
#include <net/snmp.h>
Jon Grimm's avatar
Jon Grimm committed
59
#include <net/sock.h>
60
#include <net/xfrm.h>
Jon Grimm's avatar
Jon Grimm committed
61
#include <net/sctp/sctp.h>
62
#include <net/sctp/sm.h>
Jon Grimm's avatar
Jon Grimm committed
63 64

/* Forward declarations for internal helpers. */
Jon Grimm's avatar
Jon Grimm committed
65
static int sctp_rcv_ootb(struct sk_buff *);
66
sctp_association_t *__sctp_rcv_lookup(struct sk_buff *skb,
Jon Grimm's avatar
Jon Grimm committed
67 68
				      const union sctp_addr *laddr,
				      const union sctp_addr *paddr,
69
				      struct sctp_transport **transportp);
Jon Grimm's avatar
Jon Grimm committed
70
sctp_endpoint_t *__sctp_rcv_lookup_endpoint(const union sctp_addr *laddr);
Jon Grimm's avatar
Jon Grimm committed
71

72 73 74 75 76 77

/* Calculate the SCTP checksum of an SCTP packet.  */
static inline int sctp_rcv_checksum(struct sk_buff *skb)
{
	struct sctphdr *sh;
	__u32 cmp, val;
78
	struct sk_buff *list = skb_shinfo(skb)->frag_list;
79 80 81

	sh = (struct sctphdr *) skb->h.raw;
	cmp = ntohl(sh->checksum);
82 83 84 85 86 87 88 89 90

	val = sctp_start_cksum((__u8 *)sh, skb_headlen(skb));

	for (; list; list = list->next)
		val = sctp_update_cksum((__u8 *)list->data, skb_headlen(list),
					val);

	val = sctp_end_cksum(val);

91 92 93 94 95 96
	if (val != cmp) {
		/* CRC failure, dump it. */
		return -1;
	}
	return 0;
}
Jon Grimm's avatar
Jon Grimm committed
97 98 99 100

/*
 * This is the routine which IP calls when receiving an SCTP packet.
 */
101
int sctp_rcv(struct sk_buff *skb)
Jon Grimm's avatar
Jon Grimm committed
102 103
{
	struct sock *sk;
104
	sctp_association_t *asoc;
Jon Grimm's avatar
Jon Grimm committed
105 106
	sctp_endpoint_t *ep = NULL;
	sctp_endpoint_common_t *rcvr;
107
	struct sctp_transport *transport = NULL;
108
	sctp_chunk_t *chunk;
Jon Grimm's avatar
Jon Grimm committed
109
	struct sctphdr *sh;
Jon Grimm's avatar
Jon Grimm committed
110 111
	union sctp_addr src;
	union sctp_addr dest;
112
	struct sctp_af *af;
113
	int ret = 0;
Jon Grimm's avatar
Jon Grimm committed
114

115
	if (skb->pkt_type!=PACKET_HOST)
Jon Grimm's avatar
Jon Grimm committed
116 117
		goto discard_it;

118
	sh = (struct sctphdr *) skb->h.raw;
Jon Grimm's avatar
Jon Grimm committed
119 120 121

	/* Pull up the IP and SCTP headers. */
	__skb_pull(skb, skb->h.raw - skb->data);
122
	if (skb->len < sizeof(struct sctphdr))
Jon Grimm's avatar
Jon Grimm committed
123
		goto bad_packet;
124
	if (sctp_rcv_checksum(skb) < 0)
Jon Grimm's avatar
Jon Grimm committed
125
		goto bad_packet;
126

Jon Grimm's avatar
Jon Grimm committed
127 128 129 130 131
	skb_pull(skb, sizeof(struct sctphdr));	

	af = sctp_get_af_specific(ipver2af(skb->nh.iph->version));
	if (unlikely(!af)) 
		goto bad_packet;
Jon Grimm's avatar
Jon Grimm committed
132

Jon Grimm's avatar
Jon Grimm committed
133 134 135
	/* Initialize local addresses for lookups. */
	af->from_skb(&src, skb, 1);
	af->from_skb(&dest, skb, 0);
136

Jon Grimm's avatar
Jon Grimm committed
137 138 139 140
	/* If the packet is to or from a non-unicast address,
	 * silently discard the packet.
	 *
	 * This is not clearly defined in the RFC except in section
141 142
	 * 8.4 - OOTB handling.  However, based on the book "Stream Control
	 * Transmission Protocol" 2.1, "It is important to note that the
Jon Grimm's avatar
Jon Grimm committed
143 144 145 146 147
	 * IP address of an SCTP transport address must be a routable
	 * unicast address.  In other words, IP multicast addresses and
	 * IP broadcast addresses cannot be used in an SCTP transport
	 * address."
	 */
148
	if (!af->addr_valid(&src) || !af->addr_valid(&dest))
Jon Grimm's avatar
Jon Grimm committed
149 150 151 152
		goto discard_it;

	asoc = __sctp_rcv_lookup(skb, &src, &dest, &transport);

153
	/*
Jon Grimm's avatar
Jon Grimm committed
154
	 * RFC 2960, 8.4 - Handle "Out of the blue" Packets.
155 156 157 158 159
	 * An SCTP packet is called an "out of the blue" (OOTB)
	 * packet if it is correctly formed, i.e., passed the
	 * receiver's checksum check, but the receiver is not
	 * able to identify the association to which this
	 * packet belongs.
Jon Grimm's avatar
Jon Grimm committed
160
	 */
161
	if (!asoc) {
Jon Grimm's avatar
Jon Grimm committed
162
		ep = __sctp_rcv_lookup_endpoint(&dest);
163
		if (sctp_rcv_ootb(skb))
Jon Grimm's avatar
Jon Grimm committed
164 165 166 167 168 169 170
			goto discard_release;
	}

	/* Retrieve the common input handling substructure. */
	rcvr = asoc ? &asoc->base : &ep->base;
	sk = rcvr->sk;

171
	if (!xfrm_policy_check(sk, XFRM_POLICY_IN, skb))
Jon Grimm's avatar
Jon Grimm committed
172 173
		goto discard_release;

174 175 176 177
	ret = sk_filter(sk, skb, 1);
	if (ret)
                goto discard_release;

178
	/* Create an SCTP packet structure. */
Jon Grimm's avatar
Jon Grimm committed
179 180 181 182 183 184 185 186 187
	chunk = sctp_chunkify(skb, asoc, sk);
	if (!chunk) {
		ret = -ENOMEM;
		goto discard_release;
	}

	/* Remember what endpoint is to handle this packet. */
	chunk->rcvr = rcvr;

188 189 190
	/* Remember the SCTP header. */
	chunk->sctp_hdr = sh;

191
	/* Set the source and destination addresses of the incoming chunk.  */
192
	sctp_init_addrs(chunk, &src, &dest);
Jon Grimm's avatar
Jon Grimm committed
193 194 195 196 197 198 199 200 201 202

	/* Remember where we came from.  */
	chunk->transport = transport;

	/* Acquire access to the sock lock. Note: We are safe from other
	 * bottom halves on this lock, but a user may be in the lock too,
	 * so check if it is busy.
	 */
	sctp_bh_lock_sock(sk);

203
	if (sock_owned_by_user(sk)) {
204
		sk_add_backlog(sk, (struct sk_buff *) chunk);
Jon Grimm's avatar
Jon Grimm committed
205
	} else {
206
		sctp_backlog_rcv(sk, (struct sk_buff *) chunk);
Jon Grimm's avatar
Jon Grimm committed
207 208
	}

209
	/* Release the sock and any reference counts we took in the
Jon Grimm's avatar
Jon Grimm committed
210 211
	 * lookup calls.
	 */
212
	sctp_bh_unlock_sock(sk);
Jon Grimm's avatar
Jon Grimm committed
213 214 215
	if (asoc) {
		sctp_association_put(asoc);
	} else {
216
		sctp_endpoint_put(ep);
Jon Grimm's avatar
Jon Grimm committed
217 218
	}
	sock_put(sk);
219
	return ret;
Jon Grimm's avatar
Jon Grimm committed
220 221 222 223 224

bad_packet:
#if 0 /* FIXME */
	SCTP_INC_STATS(SctpInErrs);
#endif /* FIXME*/
225

Jon Grimm's avatar
Jon Grimm committed
226 227
discard_it:
	kfree_skb(skb);
228
	return ret;
Jon Grimm's avatar
Jon Grimm committed
229 230 231 232 233 234 235 236

discard_release:
	/* Release any structures we may be holding. */
	if (asoc) {
		sock_put(asoc->base.sk);
		sctp_association_put(asoc);
	} else {
		sock_put(ep->base.sk);
237
		sctp_endpoint_put(ep);
Jon Grimm's avatar
Jon Grimm committed
238 239
	}

240 241
	goto discard_it;
}
Jon Grimm's avatar
Jon Grimm committed
242 243 244

/* Handle second half of inbound skb processing.  If the sock was busy,
 * we may have need to delay processing until later when the sock is
245 246
 * released (on the backlog).   If not busy, we call this routine
 * directly from the bottom half.
Jon Grimm's avatar
Jon Grimm committed
247
 */
248
int sctp_backlog_rcv(struct sock *sk, struct sk_buff *skb)
Jon Grimm's avatar
Jon Grimm committed
249 250 251 252 253 254 255
{
	sctp_chunk_t *chunk;
	sctp_inqueue_t *inqueue;

	/* One day chunk will live inside the skb, but for
	 * now this works.
	 */
256
	chunk = (sctp_chunk_t *) skb;
Jon Grimm's avatar
Jon Grimm committed
257 258 259 260
	inqueue = &chunk->rcvr->inqueue;

	sctp_push_inqueue(inqueue, chunk);
        return 0;
261
}
Jon Grimm's avatar
Jon Grimm committed
262

263 264 265
/* Handle icmp frag needed error. */
static inline void sctp_icmp_frag_needed(struct sock *sk,
					 sctp_association_t *asoc,
Jon Grimm's avatar
Jon Grimm committed
266
					 struct sctp_transport *transport,
267 268
					 __u32 pmtu)
{
269 270 271 272 273 274 275
	if (unlikely(pmtu < SCTP_DEFAULT_MINSEGMENT)) {
		printk(KERN_WARNING "%s: Reported pmtu %d too low, "
		       "using default minimum of %d\n", __FUNCTION__, pmtu,
		       SCTP_DEFAULT_MINSEGMENT);
		pmtu = SCTP_DEFAULT_MINSEGMENT;
	}

276 277 278 279 280 281 282 283
	if (!sock_owned_by_user(sk) && transport && (transport->pmtu != pmtu)) {
		transport->pmtu = pmtu;
		sctp_assoc_sync_pmtu(asoc);
		sctp_retransmit(&asoc->outqueue, transport,
				SCTP_RETRANSMIT_PMTU_DISCOVERY );
	}
}

Jon Grimm's avatar
Jon Grimm committed
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
/*
 * This routine is called by the ICMP module when it gets some
 * sort of error condition.  If err < 0 then the socket should
 * be closed and the error returned to the user.  If err > 0
 * it's just the icmp type << 8 | icmp code.  After adjustment
 * header points to the first 8 bytes of the sctp header.  We need
 * to find the appropriate port.
 *
 * The locking strategy used here is very "optimistic". When
 * someone else accesses the socket the ICMP is just dropped
 * and for some paths there is no check at all.
 * A more general error queue to queue errors for later handling
 * is probably better.
 *
 */
299
void sctp_v4_err(struct sk_buff *skb, __u32 info)
Jon Grimm's avatar
Jon Grimm committed
300
{
301 302 303 304 305 306 307 308 309
	struct iphdr *iph = (struct iphdr *)skb->data;
	struct sctphdr *sh = (struct sctphdr *)(skb->data + (iph->ihl <<2));
	int type = skb->h.icmph->type;
	int code = skb->h.icmph->code;
	union sctp_addr saddr, daddr;
	struct inet_opt *inet;
	struct sock *sk = NULL;
	sctp_endpoint_t *ep = NULL;
	sctp_association_t *asoc = NULL;
Jon Grimm's avatar
Jon Grimm committed
310
	struct sctp_transport *transport;
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
	int err;

	if (skb->len < ((iph->ihl << 2) + 8)) {
		ICMP_INC_STATS_BH(IcmpInErrors);
		return;
	}

	saddr.v4.sin_family = AF_INET;
	saddr.v4.sin_port = ntohs(sh->source);
	memcpy(&saddr.v4.sin_addr.s_addr, &iph->saddr, sizeof(struct in_addr));	
	daddr.v4.sin_family = AF_INET;
	daddr.v4.sin_port = ntohs(sh->dest);
	memcpy(&daddr.v4.sin_addr.s_addr, &iph->daddr, sizeof(struct in_addr));	

	/* Look for an association that matches the incoming ICMP error 
	 * packet.
	 */
	asoc = __sctp_lookup_association(&saddr, &daddr, &transport);
	if (!asoc) {
		/* If there is no matching association, see if it matches any
		 * endpoint. This may happen for an ICMP error generated in 
		 * response to an INIT_ACK. 
		 */ 
		ep = __sctp_rcv_lookup_endpoint(&daddr);
		if (!ep) {
			ICMP_INC_STATS_BH(IcmpInErrors);
			return;
		}
	}

	if (asoc) {
		if (ntohl(sh->vtag) != asoc->c.peer_vtag) {
			ICMP_INC_STATS_BH(IcmpInErrors);
			goto out;
		}
		sk = asoc->base.sk;
	} else
		sk = ep->base.sk;

	sctp_bh_lock_sock(sk);
	/* If too many ICMPs get dropped on busy
	 * servers this needs to be solved differently.
	 */
	if (sock_owned_by_user(sk))
		NET_INC_STATS_BH(LockDroppedIcmps);

	switch (type) {
	case ICMP_PARAMETERPROB:
		err = EPROTO;
		break;
	case ICMP_DEST_UNREACH:
		if (code > NR_ICMP_UNREACH)
			goto out_unlock;

		/* PMTU discovery (RFC1191) */
366
		if (ICMP_FRAG_NEEDED == code) {
367 368 369 370 371 372 373
			sctp_icmp_frag_needed(sk, asoc, transport, info);
			goto out_unlock;
		}

		err = icmp_err_convert[code].errno;
		break;
	case ICMP_TIME_EXCEEDED:
374 375 376 377 378 379
		/* Ignore any time exceeded errors due to fragment reassembly
		 * timeouts.
		 */
		if (ICMP_EXC_FRAGTIME == code)
			goto out_unlock;

380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
		err = EHOSTUNREACH;
		break;
	default:
		goto out_unlock;
	}

	inet = inet_sk(sk);
	if (!sock_owned_by_user(sk) && inet->recverr) {
		sk->err = err;
		sk->error_report(sk);
	} else {  /* Only an error on timeout */
		sk->err_soft = err;
	}

out_unlock:
	sctp_bh_unlock_sock(sk);
out:
	sock_put(sk);
	if (asoc)
		sctp_association_put(asoc);
	if (ep)	
		sctp_endpoint_put(ep);
402
}
Jon Grimm's avatar
Jon Grimm committed
403 404 405 406 407 408 409 410 411 412 413 414 415

/*
 * RFC 2960, 8.4 - Handle "Out of the blue" Packets.
 *
 * This function scans all the chunks in the OOTB packet to determine if
 * the packet should be discarded right away.  If a response might be needed
 * for this packet, or, if further processing is possible, the packet will
 * be queued to a proper inqueue for the next phase of handling.
 *
 * Output:
 * Return 0 - If further processing is needed.
 * Return 1 - If the packet can be discarded right away.
 */
416
int sctp_rcv_ootb(struct sk_buff *skb)
Jon Grimm's avatar
Jon Grimm committed
417 418
{
	sctp_chunkhdr_t *ch;
419
	__u8 *ch_end;
420
	sctp_errhdr_t *err;
Jon Grimm's avatar
Jon Grimm committed
421

422
	ch = (sctp_chunkhdr_t *) skb->data;
Jon Grimm's avatar
Jon Grimm committed
423 424 425

	/* Scan through all the chunks in the packet.  */
	do {
426
		ch_end = ((__u8 *) ch) + WORD_ROUND(ntohs(ch->length));
Jon Grimm's avatar
Jon Grimm committed
427

428 429
		/* RFC 8.4, 2) If the OOTB packet contains an ABORT chunk, the
		 * receiver MUST silently discard the OOTB packet and take no
Jon Grimm's avatar
Jon Grimm committed
430 431
		 * further action.
		 */
432
		if (SCTP_CID_ABORT == ch->type)
Jon Grimm's avatar
Jon Grimm committed
433
			goto discard;
434 435 436

		/* RFC 8.4, 6) If the packet contains a SHUTDOWN COMPLETE
		 * chunk, the receiver should silently discard the packet
Jon Grimm's avatar
Jon Grimm committed
437 438
		 * and take no further action.
		 */
439
		if (SCTP_CID_SHUTDOWN_COMPLETE == ch->type)
Jon Grimm's avatar
Jon Grimm committed
440 441
			goto discard;

442 443 444 445
		/* RFC 8.4, 7) If the packet contains a "Stale cookie" ERROR
		 * or a COOKIE ACK the SCTP Packet should be silently
		 * discarded.
		 */
446
		if (SCTP_CID_COOKIE_ACK == ch->type)
Jon Grimm's avatar
Jon Grimm committed
447 448
			goto discard;

449
		if (SCTP_CID_ERROR == ch->type) {
450 451 452
			err = (sctp_errhdr_t *)(ch + sizeof(sctp_chunkhdr_t));
			if (SCTP_ERROR_STALE_COOKIE == err->cause)
				goto discard;
Jon Grimm's avatar
Jon Grimm committed
453 454
		}

455
		ch = (sctp_chunkhdr_t *) ch_end;
Jon Grimm's avatar
Jon Grimm committed
456 457 458
	} while (ch_end < skb->tail);

	return 0;
459

Jon Grimm's avatar
Jon Grimm committed
460 461
discard:
	return 1;
462
}
Jon Grimm's avatar
Jon Grimm committed
463 464 465 466 467 468

/* Insert endpoint into the hash table.  */
void __sctp_hash_endpoint(sctp_endpoint_t *ep)
{
	sctp_endpoint_common_t **epp;
	sctp_endpoint_common_t *epb;
469 470
	sctp_hashbucket_t *head;

Jon Grimm's avatar
Jon Grimm committed
471
	epb = &ep->base;
472

Jon Grimm's avatar
Jon Grimm committed
473 474
	epb->hashent = sctp_ep_hashfn(epb->bind_addr.port);
	head = &sctp_proto.ep_hashbucket[epb->hashent];
475

Jon Grimm's avatar
Jon Grimm committed
476 477 478
	sctp_write_lock(&head->lock);
	epp = &head->chain;
	epb->next = *epp;
479
	if (epb->next)
Jon Grimm's avatar
Jon Grimm committed
480 481 482 483
		(*epp)->pprev = &epb->next;
	*epp = epb;
	epb->pprev = epp;
	sctp_write_unlock(&head->lock);
484
}
Jon Grimm's avatar
Jon Grimm committed
485 486 487 488 489 490 491

/* Add an endpoint to the hash. Local BH-safe. */
void sctp_hash_endpoint(sctp_endpoint_t *ep)
{
	sctp_local_bh_disable();
	__sctp_hash_endpoint(ep);
	sctp_local_bh_enable();
492
}
Jon Grimm's avatar
Jon Grimm committed
493 494 495 496 497 498 499 500 501 502

/* Remove endpoint from the hash table.  */
void __sctp_unhash_endpoint(sctp_endpoint_t *ep)
{
	sctp_hashbucket_t *head;
	sctp_endpoint_common_t *epb;

	epb = &ep->base;

	epb->hashent = sctp_ep_hashfn(epb->bind_addr.port);
503

Jon Grimm's avatar
Jon Grimm committed
504 505 506 507 508
	head = &sctp_proto.ep_hashbucket[epb->hashent];

	sctp_write_lock(&head->lock);

	if (epb->pprev) {
509
		if (epb->next)
Jon Grimm's avatar
Jon Grimm committed
510 511 512 513 514 515
			epb->next->pprev = epb->pprev;
		*epb->pprev = epb->next;
		epb->pprev = NULL;
	}

	sctp_write_unlock(&head->lock);
516
}
Jon Grimm's avatar
Jon Grimm committed
517 518 519 520 521 522 523

/* Remove endpoint from the hash.  Local BH-safe. */
void sctp_unhash_endpoint(sctp_endpoint_t *ep)
{
	sctp_local_bh_disable();
	__sctp_unhash_endpoint(ep);
	sctp_local_bh_enable();
524
}
Jon Grimm's avatar
Jon Grimm committed
525 526

/* Look up an endpoint. */
Jon Grimm's avatar
Jon Grimm committed
527
sctp_endpoint_t *__sctp_rcv_lookup_endpoint(const union sctp_addr *laddr)
Jon Grimm's avatar
Jon Grimm committed
528 529 530 531 532 533 534 535 536 537 538
{
	sctp_hashbucket_t *head;
	sctp_endpoint_common_t *epb;
	sctp_endpoint_t *ep;
	int hash;

	hash = sctp_ep_hashfn(laddr->v4.sin_port);
	head = &sctp_proto.ep_hashbucket[hash];
	read_lock(&head->lock);
	for (epb = head->chain; epb; epb = epb->next) {
		ep = sctp_ep(epb);
539 540 541 542
		if (sctp_endpoint_is_match(ep, laddr))
			goto hit;
	}

Jon Grimm's avatar
Jon Grimm committed
543 544 545
	ep = sctp_sk((sctp_get_ctl_sock()))->ep;
	epb = &ep->base;

546
hit:
Jon Grimm's avatar
Jon Grimm committed
547 548 549 550
	sctp_endpoint_hold(ep);
	sock_hold(epb->sk);
	read_unlock(&head->lock);
	return ep;
551
}
Jon Grimm's avatar
Jon Grimm committed
552 553 554 555 556 557 558

/* Add an association to the hash. Local BH-safe. */
void sctp_hash_established(sctp_association_t *asoc)
{
	sctp_local_bh_disable();
	__sctp_hash_established(asoc);
	sctp_local_bh_enable();
559
}
Jon Grimm's avatar
Jon Grimm committed
560 561 562 563 564 565 566

/* Insert association into the hash table.  */
void __sctp_hash_established(sctp_association_t *asoc)
{
	sctp_endpoint_common_t **epp;
	sctp_endpoint_common_t *epb;
	sctp_hashbucket_t *head;
567

Jon Grimm's avatar
Jon Grimm committed
568 569 570 571
	epb = &asoc->base;

	/* Calculate which chain this entry will belong to. */
	epb->hashent = sctp_assoc_hashfn(epb->bind_addr.port, asoc->peer.port);
572

Jon Grimm's avatar
Jon Grimm committed
573
	head = &sctp_proto.assoc_hashbucket[epb->hashent];
574

Jon Grimm's avatar
Jon Grimm committed
575 576 577
	sctp_write_lock(&head->lock);
	epp = &head->chain;
	epb->next = *epp;
578
	if (epb->next)
Jon Grimm's avatar
Jon Grimm committed
579 580 581 582
		(*epp)->pprev = &epb->next;
	*epp = epb;
	epb->pprev = epp;
	sctp_write_unlock(&head->lock);
583
}
Jon Grimm's avatar
Jon Grimm committed
584 585 586 587 588 589 590

/* Remove association from the hash table.  Local BH-safe. */
void sctp_unhash_established(sctp_association_t *asoc)
{
	sctp_local_bh_disable();
	__sctp_unhash_established(asoc);
	sctp_local_bh_enable();
591
}
Jon Grimm's avatar
Jon Grimm committed
592 593 594 595 596 597 598 599 600 601 602

/* Remove association from the hash table.  */
void __sctp_unhash_established(sctp_association_t *asoc)
{
	sctp_hashbucket_t *head;
	sctp_endpoint_common_t *epb;

	epb = &asoc->base;

	epb->hashent = sctp_assoc_hashfn(epb->bind_addr.port,
					 asoc->peer.port);
603

Jon Grimm's avatar
Jon Grimm committed
604 605 606 607 608
	head = &sctp_proto.assoc_hashbucket[epb->hashent];

	sctp_write_lock(&head->lock);

	if (epb->pprev) {
609
		if (epb->next)
Jon Grimm's avatar
Jon Grimm committed
610 611 612 613 614 615
			epb->next->pprev = epb->pprev;
		*epb->pprev = epb->next;
		epb->pprev = NULL;
	}

	sctp_write_unlock(&head->lock);
616
}
Jon Grimm's avatar
Jon Grimm committed
617 618

/* Look up an association. */
Jon Grimm's avatar
Jon Grimm committed
619 620
sctp_association_t *__sctp_lookup_association(const union sctp_addr *laddr,
					      const union sctp_addr *paddr,
621
					      struct sctp_transport **transportp)
Jon Grimm's avatar
Jon Grimm committed
622 623 624 625
{
	sctp_hashbucket_t *head;
	sctp_endpoint_common_t *epb;
	sctp_association_t *asoc;
626
	struct sctp_transport *transport;
Jon Grimm's avatar
Jon Grimm committed
627 628 629
	int hash;

	/* Optimize here for direct hit, only listening connections can
630
	 * have wildcards anyways.
Jon Grimm's avatar
Jon Grimm committed
631 632 633 634 635 636 637
	 */
	hash = sctp_assoc_hashfn(laddr->v4.sin_port, paddr->v4.sin_port);
	head = &sctp_proto.assoc_hashbucket[hash];
	read_lock(&head->lock);
	for (epb = head->chain; epb; epb = epb->next) {
		asoc = sctp_assoc(epb);
		transport = sctp_assoc_is_match(asoc, laddr, paddr);
638 639 640 641
		if (transport)
			goto hit;
	}

Jon Grimm's avatar
Jon Grimm committed
642 643 644 645 646 647 648 649 650 651
	read_unlock(&head->lock);

	return NULL;

hit:
	*transportp = transport;
	sctp_association_hold(asoc);
	sock_hold(epb->sk);
	read_unlock(&head->lock);
	return asoc;
652
}
Jon Grimm's avatar
Jon Grimm committed
653

654
/* Look up an association. BH-safe. */
Jon Grimm's avatar
Jon Grimm committed
655 656
sctp_association_t *sctp_lookup_association(const union sctp_addr *laddr,
					    const union sctp_addr *paddr,
657
					    struct sctp_transport **transportp)
658 659 660 661 662 663
{
	sctp_association_t *asoc;

	sctp_local_bh_disable();
	asoc = __sctp_lookup_association(laddr, paddr, transportp);
	sctp_local_bh_enable();
664

665 666 667 668
	return asoc;
}

/* Is there an association matching the given local and peer addresses? */
Jon Grimm's avatar
Jon Grimm committed
669 670
int sctp_has_association(const union sctp_addr *laddr,
			 const union sctp_addr *paddr)
671 672
{
	sctp_association_t *asoc;
673
	struct sctp_transport *transport;
674

675
	if ((asoc = sctp_lookup_association(laddr, paddr, &transport))) {
676 677 678 679 680 681 682 683
		sock_put(asoc->base.sk);
		sctp_association_put(asoc);
		return 1;
	}

	return 0;
}

Jon Grimm's avatar
Jon Grimm committed
684
/*
685
 * SCTP Implementors Guide, 2.18 Handling of address
Jon Grimm's avatar
Jon Grimm committed
686 687 688 689 690 691 692
 * parameters within the INIT or INIT-ACK.
 *
 * D) When searching for a matching TCB upon reception of an INIT
 *    or INIT-ACK chunk the receiver SHOULD use not only the
 *    source address of the packet (containing the INIT or
 *    INIT-ACK) but the receiver SHOULD also use all valid
 *    address parameters contained within the chunk.
693
 *
Jon Grimm's avatar
Jon Grimm committed
694 695 696 697 698 699 700 701
 * 2.18.3 Solution description
 *
 * This new text clearly specifies to an implementor the need
 * to look within the INIT or INIT-ACK. Any implementation that
 * does not do this, may not be able to establish associations
 * in certain circumstances.
 *
 */
Jon Grimm's avatar
Jon Grimm committed
702
static sctp_association_t *__sctp_rcv_init_lookup(struct sk_buff *skb,
703
	const union sctp_addr *laddr, struct sctp_transport **transportp)
Jon Grimm's avatar
Jon Grimm committed
704 705
{
	sctp_association_t *asoc;
Jon Grimm's avatar
Jon Grimm committed
706 707
	union sctp_addr addr;
	union sctp_addr *paddr = &addr;
708
	struct sctphdr *sh = (struct sctphdr *) skb->h.raw;
Jon Grimm's avatar
Jon Grimm committed
709
	sctp_chunkhdr_t *ch;
710 711
	union sctp_params params;
	sctp_init_chunk_t *init;
Jon Grimm's avatar
Jon Grimm committed
712

713
	ch = (sctp_chunkhdr_t *) skb->data;
Jon Grimm's avatar
Jon Grimm committed
714

715 716 717 718 719 720
	/* If this is INIT/INIT-ACK look inside the chunk too. */
	switch (ch->type) {
	case SCTP_CID_INIT:
	case SCTP_CID_INIT_ACK:
		break;
	default:
Jon Grimm's avatar
Jon Grimm committed
721
		return NULL;
722
	}
Jon Grimm's avatar
Jon Grimm committed
723

724 725 726 727 728 729 730 731 732 733 734 735 736 737
	/*
	 * This code will NOT touch anything inside the chunk--it is
	 * strictly READ-ONLY.
	 *
	 * RFC 2960 3  SCTP packet Format
	 *
	 * Multiple chunks can be bundled into one SCTP packet up to
	 * the MTU size, except for the INIT, INIT ACK, and SHUTDOWN
	 * COMPLETE chunks.  These chunks MUST NOT be bundled with any
	 * other chunk in a packet.  See Section 6.10 for more details
	 * on chunk bundling.
	 */

	/* Find the start of the TLVs and the end of the chunk.  This is
Jon Grimm's avatar
Jon Grimm committed
738 739
	 * the region we search for address parameters.
	 */
740
	init = (sctp_init_chunk_t *)skb->data;
741

742 743
	/* Walk the parameters looking for embedded addresses. */
	sctp_walk_params(params, init, init_hdr.params) {
744 745

		/* Note: Ignoring hostname addresses. */
746 747
		if ((SCTP_PARAM_IPV4_ADDRESS != params.p->type) &&
		    (SCTP_PARAM_IPV6_ADDRESS != params.p->type))
Jon Grimm's avatar
Jon Grimm committed
748
			continue;
749

750
		sctp_param2sockaddr(paddr, params.addr, ntohs(sh->source));
751
		asoc = __sctp_lookup_association(laddr, paddr, transportp);
752 753 754
		if (asoc)
			return asoc;
	}
Jon Grimm's avatar
Jon Grimm committed
755 756

	return NULL;
757
}
Jon Grimm's avatar
Jon Grimm committed
758 759

/* Lookup an association for an inbound skb. */
760
sctp_association_t *__sctp_rcv_lookup(struct sk_buff *skb,
Jon Grimm's avatar
Jon Grimm committed
761 762
				      const union sctp_addr *paddr,
				      const union sctp_addr *laddr,
763
				      struct sctp_transport **transportp)
Jon Grimm's avatar
Jon Grimm committed
764 765 766
{
	sctp_association_t *asoc;

767
	asoc = __sctp_lookup_association(laddr, paddr, transportp);
768

Jon Grimm's avatar
Jon Grimm committed
769
	/* Further lookup for INIT/INIT-ACK packets.
770
	 * SCTP Implementors Guide, 2.18 Handling of address
Jon Grimm's avatar
Jon Grimm committed
771 772
	 * parameters within the INIT or INIT-ACK.
	 */
773
	if (!asoc)
Jon Grimm's avatar
Jon Grimm committed
774
		asoc = __sctp_rcv_init_lookup(skb, laddr, transportp);
Jon Grimm's avatar
Jon Grimm committed
775

776 777
	return asoc;
}
Jon Grimm's avatar
Jon Grimm committed
778 779 780 781 782