input.c 21.3 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 *);
Jon Grimm's avatar
Jon Grimm committed
66
struct sctp_association *__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
struct sctp_endpoint *__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
	if (val != cmp) {
		/* CRC failure, dump it. */
93
		SCTP_INC_STATS_BH(SctpChecksumErrors);
94 95 96 97
		return -1;
	}
	return 0;
}
Jon Grimm's avatar
Jon Grimm committed
98 99 100 101

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

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

120 121
	SCTP_INC_STATS_BH(SctpInSCTPPacks);

122
	sh = (struct sctphdr *) skb->h.raw;
Jon Grimm's avatar
Jon Grimm committed
123 124 125

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

Jon Grimm's avatar
Jon Grimm committed
131
	skb_pull(skb, sizeof(struct sctphdr));
Jon Grimm's avatar
Jon Grimm committed
132

133 134
	family = ipver2af(skb->nh.iph->version);
	af = sctp_get_af_specific(family);
Jon Grimm's avatar
Jon Grimm committed
135
	if (unlikely(!af))
Jon Grimm's avatar
Jon Grimm committed
136
		goto bad_packet;
Jon Grimm's avatar
Jon Grimm committed
137

Jon Grimm's avatar
Jon Grimm committed
138 139 140
	/* Initialize local addresses for lookups. */
	af->from_skb(&src, skb, 1);
	af->from_skb(&dest, skb, 0);
141

Jon Grimm's avatar
Jon Grimm committed
142 143 144 145
	/* 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
146 147
	 * 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
148 149 150 151 152
	 * 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."
	 */
153
	if (!af->addr_valid(&src) || !af->addr_valid(&dest))
Jon Grimm's avatar
Jon Grimm committed
154 155 156 157
		goto discard_it;

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

158
	/*
Jon Grimm's avatar
Jon Grimm committed
159
	 * RFC 2960, 8.4 - Handle "Out of the blue" Packets.
160 161 162 163 164
	 * 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
165
	 */
166
	if (!asoc) {
Jon Grimm's avatar
Jon Grimm committed
167
		ep = __sctp_rcv_lookup_endpoint(&dest);
168 169
		if (sctp_rcv_ootb(skb)) {
			SCTP_INC_STATS_BH(SctpOutOfBlues);
Jon Grimm's avatar
Jon Grimm committed
170
			goto discard_release;
171
		}
Jon Grimm's avatar
Jon Grimm committed
172 173 174 175 176 177
	}

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

178
	if (!xfrm_policy_check(sk, XFRM_POLICY_IN, skb, family))
Jon Grimm's avatar
Jon Grimm committed
179 180
		goto discard_release;

181 182 183 184
	ret = sk_filter(sk, skb, 1);
	if (ret)
                goto discard_release;

185
	/* Create an SCTP packet structure. */
Jon Grimm's avatar
Jon Grimm committed
186 187 188 189 190 191 192 193 194
	chunk = sctp_chunkify(skb, asoc, sk);
	if (!chunk) {
		ret = -ENOMEM;
		goto discard_release;
	}

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

195 196 197
	/* Remember the SCTP header. */
	chunk->sctp_hdr = sh;

198
	/* Set the source and destination addresses of the incoming chunk.  */
199
	sctp_init_addrs(chunk, &src, &dest);
Jon Grimm's avatar
Jon Grimm committed
200 201 202 203 204 205 206 207 208 209

	/* 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);

210
	if (sock_owned_by_user(sk))
211
		sk_add_backlog(sk, (struct sk_buff *) chunk);
212
	else
213
		sctp_backlog_rcv(sk, (struct sk_buff *) chunk);
Jon Grimm's avatar
Jon Grimm committed
214

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

bad_packet:
227
	SCTP_INC_STATS(SctpChecksumErrors);
228

Jon Grimm's avatar
Jon Grimm committed
229 230
discard_it:
	kfree_skb(skb);
231
	return ret;
Jon Grimm's avatar
Jon Grimm committed
232 233 234 235 236 237 238 239

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);
240
		sctp_endpoint_put(ep);
Jon Grimm's avatar
Jon Grimm committed
241 242
	}

243 244
	goto discard_it;
}
Jon Grimm's avatar
Jon Grimm committed
245 246 247

/* 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
248 249
 * released (on the backlog).   If not busy, we call this routine
 * directly from the bottom half.
Jon Grimm's avatar
Jon Grimm committed
250
 */
251
int sctp_backlog_rcv(struct sock *sk, struct sk_buff *skb)
Jon Grimm's avatar
Jon Grimm committed
252
{
Jon Grimm's avatar
Jon Grimm committed
253
	struct sctp_chunk *chunk;
254
	struct sctp_inq *inqueue;
Jon Grimm's avatar
Jon Grimm committed
255 256 257 258

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

262
	sctp_inq_push(inqueue, chunk);
Jon Grimm's avatar
Jon Grimm committed
263
        return 0;
264
}
Jon Grimm's avatar
Jon Grimm committed
265

266
/* Handle icmp frag needed error. */
267 268
void sctp_icmp_frag_needed(struct sock *sk, struct sctp_association *asoc,
			   struct sctp_transport *t, __u32 pmtu)
269
{
270 271 272 273 274 275 276
	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;
	}

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

284 285 286
/* Common lookup code for icmp/icmpv6 error handler. */
struct sock *sctp_err_lookup(int family, struct sk_buff *skb,
			     struct sctphdr *sctphdr,
Jon Grimm's avatar
Jon Grimm committed
287 288
			     struct sctp_endpoint **epp,
			     struct sctp_association **app,
289
			     struct sctp_transport **tpp)
Jon Grimm's avatar
Jon Grimm committed
290
{
291 292 293
	union sctp_addr saddr;
	union sctp_addr daddr;
	struct sctp_af *af;
294
	struct sock *sk = NULL;
295 296 297
	struct sctp_endpoint *ep = NULL;
	struct sctp_association *asoc = NULL;
	struct sctp_transport *transport = NULL;
298

299 300 301 302 303
	*app = NULL; *epp = NULL; *tpp = NULL;

	af = sctp_get_af_specific(family);
	if (unlikely(!af)) {
		return NULL;
304 305
	}

306 307 308
	/* Initialize local addresses for lookups. */
	af->from_skb(&saddr, skb, 1);
	af->from_skb(&daddr, skb, 0);
309

Jon Grimm's avatar
Jon Grimm committed
310
	/* Look for an association that matches the incoming ICMP error
311 312 313 314 315
	 * packet.
	 */
	asoc = __sctp_lookup_association(&saddr, &daddr, &transport);
	if (!asoc) {
		/* If there is no matching association, see if it matches any
Jon Grimm's avatar
Jon Grimm committed
316 317 318
		 * endpoint. This may happen for an ICMP error generated in
		 * response to an INIT_ACK.
		 */
319 320
		ep = __sctp_rcv_lookup_endpoint(&daddr);
		if (!ep) {
321
			return NULL;
322 323 324 325
		}
	}

	if (asoc) {
326
		if (ntohl(sctphdr->vtag) != asoc->c.peer_vtag) {
327 328 329 330 331 332 333 334
			ICMP_INC_STATS_BH(IcmpInErrors);
			goto out;
		}
		sk = asoc->base.sk;
	} else
		sk = ep->base.sk;

	sctp_bh_lock_sock(sk);
335

336 337 338 339 340 341
	/* 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);

342 343 344 345
	*epp = ep;
	*app = asoc;
	*tpp = transport;
	return sk;
Jon Grimm's avatar
Jon Grimm committed
346

347 348 349 350
out:
	sock_put(sk);
	if (asoc)
		sctp_association_put(asoc);
Jon Grimm's avatar
Jon Grimm committed
351
	if (ep)
352 353 354 355 356
		sctp_endpoint_put(ep);
	return NULL;
}

/* Common cleanup code for icmp/icmpv6 error handler. */
Jon Grimm's avatar
Jon Grimm committed
357
void sctp_err_finish(struct sock *sk, struct sctp_endpoint *ep,
358 359 360 361 362 363
		     struct sctp_association *asoc)
{
	sctp_bh_unlock_sock(sk);
	sock_put(sk);
	if (asoc)
		sctp_association_put(asoc);
Jon Grimm's avatar
Jon Grimm committed
364
	if (ep)
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
		sctp_endpoint_put(ep);
}

/*
 * 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.
 *
 */
void sctp_v4_err(struct sk_buff *skb, __u32 info)
{
	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;
	struct sock *sk;
Jon Grimm's avatar
Jon Grimm committed
390 391
	struct sctp_endpoint *ep;
	struct sctp_association *asoc;
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
	struct sctp_transport *transport;
	struct inet_opt *inet;
	char *saveip, *savesctp;
	int err;

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

	/* Fix up skb to look at the embedded net header. */
	saveip = skb->nh.raw;
	savesctp  = skb->h.raw;
	skb->nh.iph = iph;
	skb->h.raw = (char *)sh;
	sk = sctp_err_lookup(AF_INET, skb, sh, &ep, &asoc, &transport);
	/* Put back, the original pointers. */
	skb->nh.raw = saveip;
	skb->h.raw = savesctp;
	if (!sk) {
		ICMP_INC_STATS_BH(IcmpInErrors);
		return;
	}
Jon Grimm's avatar
Jon Grimm committed
415
	/* Warning:  The sock lock is held.  Remember to call
416 417 418
	 * sctp_err_finish!
	 */

419 420 421 422 423 424 425 426 427
	switch (type) {
	case ICMP_PARAMETERPROB:
		err = EPROTO;
		break;
	case ICMP_DEST_UNREACH:
		if (code > NR_ICMP_UNREACH)
			goto out_unlock;

		/* PMTU discovery (RFC1191) */
428
		if (ICMP_FRAG_NEEDED == code) {
429 430 431 432 433 434 435
			sctp_icmp_frag_needed(sk, asoc, transport, info);
			goto out_unlock;
		}

		err = icmp_err_convert[code].errno;
		break;
	case ICMP_TIME_EXCEEDED:
436 437 438 439 440 441
		/* Ignore any time exceeded errors due to fragment reassembly
		 * timeouts.
		 */
		if (ICMP_EXC_FRAGTIME == code)
			goto out_unlock;

442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
		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:
457
	sctp_err_finish(sk, ep, asoc);
458
}
Jon Grimm's avatar
Jon Grimm committed
459 460 461 462 463 464 465 466 467 468 469 470 471

/*
 * 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.
 */
472
int sctp_rcv_ootb(struct sk_buff *skb)
Jon Grimm's avatar
Jon Grimm committed
473 474
{
	sctp_chunkhdr_t *ch;
475
	__u8 *ch_end;
476
	sctp_errhdr_t *err;
Jon Grimm's avatar
Jon Grimm committed
477

478
	ch = (sctp_chunkhdr_t *) skb->data;
Jon Grimm's avatar
Jon Grimm committed
479 480 481

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

484 485
		/* 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
486 487
		 * further action.
		 */
488
		if (SCTP_CID_ABORT == ch->type)
Jon Grimm's avatar
Jon Grimm committed
489
			goto discard;
490 491 492

		/* 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
493 494
		 * and take no further action.
		 */
495
		if (SCTP_CID_SHUTDOWN_COMPLETE == ch->type)
Jon Grimm's avatar
Jon Grimm committed
496 497
			goto discard;

498 499 500 501
		/* RFC 8.4, 7) If the packet contains a "Stale cookie" ERROR
		 * or a COOKIE ACK the SCTP Packet should be silently
		 * discarded.
		 */
502
		if (SCTP_CID_COOKIE_ACK == ch->type)
Jon Grimm's avatar
Jon Grimm committed
503 504
			goto discard;

505
		if (SCTP_CID_ERROR == ch->type) {
506 507 508
			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
509 510
		}

511
		ch = (sctp_chunkhdr_t *) ch_end;
Jon Grimm's avatar
Jon Grimm committed
512 513 514
	} while (ch_end < skb->tail);

	return 0;
515

Jon Grimm's avatar
Jon Grimm committed
516 517
discard:
	return 1;
518
}
Jon Grimm's avatar
Jon Grimm committed
519 520

/* Insert endpoint into the hash table.  */
Jon Grimm's avatar
Jon Grimm committed
521
void __sctp_hash_endpoint(struct sctp_endpoint *ep)
Jon Grimm's avatar
Jon Grimm committed
522
{
Jon Grimm's avatar
Jon Grimm committed
523 524
	struct sctp_ep_common **epp;
	struct sctp_ep_common *epb;
525 526
	sctp_hashbucket_t *head;

Jon Grimm's avatar
Jon Grimm committed
527
	epb = &ep->base;
528

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

Jon Grimm's avatar
Jon Grimm committed
532 533 534
	sctp_write_lock(&head->lock);
	epp = &head->chain;
	epb->next = *epp;
535
	if (epb->next)
Jon Grimm's avatar
Jon Grimm committed
536 537 538 539
		(*epp)->pprev = &epb->next;
	*epp = epb;
	epb->pprev = epp;
	sctp_write_unlock(&head->lock);
540
}
Jon Grimm's avatar
Jon Grimm committed
541 542

/* Add an endpoint to the hash. Local BH-safe. */
Jon Grimm's avatar
Jon Grimm committed
543
void sctp_hash_endpoint(struct sctp_endpoint *ep)
Jon Grimm's avatar
Jon Grimm committed
544 545 546 547
{
	sctp_local_bh_disable();
	__sctp_hash_endpoint(ep);
	sctp_local_bh_enable();
548
}
Jon Grimm's avatar
Jon Grimm committed
549 550

/* Remove endpoint from the hash table.  */
Jon Grimm's avatar
Jon Grimm committed
551
void __sctp_unhash_endpoint(struct sctp_endpoint *ep)
Jon Grimm's avatar
Jon Grimm committed
552 553
{
	sctp_hashbucket_t *head;
Jon Grimm's avatar
Jon Grimm committed
554
	struct sctp_ep_common *epb;
Jon Grimm's avatar
Jon Grimm committed
555 556 557 558

	epb = &ep->base;

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

Jon Grimm's avatar
Jon Grimm committed
560 561 562 563 564
	head = &sctp_proto.ep_hashbucket[epb->hashent];

	sctp_write_lock(&head->lock);

	if (epb->pprev) {
565
		if (epb->next)
Jon Grimm's avatar
Jon Grimm committed
566 567 568 569 570 571
			epb->next->pprev = epb->pprev;
		*epb->pprev = epb->next;
		epb->pprev = NULL;
	}

	sctp_write_unlock(&head->lock);
572
}
Jon Grimm's avatar
Jon Grimm committed
573 574

/* Remove endpoint from the hash.  Local BH-safe. */
Jon Grimm's avatar
Jon Grimm committed
575
void sctp_unhash_endpoint(struct sctp_endpoint *ep)
Jon Grimm's avatar
Jon Grimm committed
576 577 578 579
{
	sctp_local_bh_disable();
	__sctp_unhash_endpoint(ep);
	sctp_local_bh_enable();
580
}
Jon Grimm's avatar
Jon Grimm committed
581 582

/* Look up an endpoint. */
Jon Grimm's avatar
Jon Grimm committed
583
struct sctp_endpoint *__sctp_rcv_lookup_endpoint(const union sctp_addr *laddr)
Jon Grimm's avatar
Jon Grimm committed
584 585
{
	sctp_hashbucket_t *head;
Jon Grimm's avatar
Jon Grimm committed
586 587
	struct sctp_ep_common *epb;
	struct sctp_endpoint *ep;
Jon Grimm's avatar
Jon Grimm committed
588 589 590 591 592 593 594
	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);
595 596 597 598
		if (sctp_endpoint_is_match(ep, laddr))
			goto hit;
	}

Jon Grimm's avatar
Jon Grimm committed
599 600 601
	ep = sctp_sk((sctp_get_ctl_sock()))->ep;
	epb = &ep->base;

602
hit:
Jon Grimm's avatar
Jon Grimm committed
603 604 605 606
	sctp_endpoint_hold(ep);
	sock_hold(epb->sk);
	read_unlock(&head->lock);
	return ep;
607
}
Jon Grimm's avatar
Jon Grimm committed
608 609

/* Add an association to the hash. Local BH-safe. */
Jon Grimm's avatar
Jon Grimm committed
610
void sctp_hash_established(struct sctp_association *asoc)
Jon Grimm's avatar
Jon Grimm committed
611 612 613 614
{
	sctp_local_bh_disable();
	__sctp_hash_established(asoc);
	sctp_local_bh_enable();
615
}
Jon Grimm's avatar
Jon Grimm committed
616 617

/* Insert association into the hash table.  */
Jon Grimm's avatar
Jon Grimm committed
618
void __sctp_hash_established(struct sctp_association *asoc)
Jon Grimm's avatar
Jon Grimm committed
619
{
Jon Grimm's avatar
Jon Grimm committed
620 621
	struct sctp_ep_common **epp;
	struct sctp_ep_common *epb;
Jon Grimm's avatar
Jon Grimm committed
622
	sctp_hashbucket_t *head;
623

Jon Grimm's avatar
Jon Grimm committed
624 625 626 627
	epb = &asoc->base;

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

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

Jon Grimm's avatar
Jon Grimm committed
631 632 633
	sctp_write_lock(&head->lock);
	epp = &head->chain;
	epb->next = *epp;
634
	if (epb->next)
Jon Grimm's avatar
Jon Grimm committed
635 636 637 638
		(*epp)->pprev = &epb->next;
	*epp = epb;
	epb->pprev = epp;
	sctp_write_unlock(&head->lock);
639
}
Jon Grimm's avatar
Jon Grimm committed
640 641

/* Remove association from the hash table.  Local BH-safe. */
Jon Grimm's avatar
Jon Grimm committed
642
void sctp_unhash_established(struct sctp_association *asoc)
Jon Grimm's avatar
Jon Grimm committed
643 644 645 646
{
	sctp_local_bh_disable();
	__sctp_unhash_established(asoc);
	sctp_local_bh_enable();
647
}
Jon Grimm's avatar
Jon Grimm committed
648 649

/* Remove association from the hash table.  */
Jon Grimm's avatar
Jon Grimm committed
650
void __sctp_unhash_established(struct sctp_association *asoc)
Jon Grimm's avatar
Jon Grimm committed
651 652
{
	sctp_hashbucket_t *head;
Jon Grimm's avatar
Jon Grimm committed
653
	struct sctp_ep_common *epb;
Jon Grimm's avatar
Jon Grimm committed
654 655 656 657 658

	epb = &asoc->base;

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

Jon Grimm's avatar
Jon Grimm committed
660 661 662 663 664
	head = &sctp_proto.assoc_hashbucket[epb->hashent];

	sctp_write_lock(&head->lock);

	if (epb->pprev) {
665
		if (epb->next)
Jon Grimm's avatar
Jon Grimm committed
666 667 668 669 670 671
			epb->next->pprev = epb->pprev;
		*epb->pprev = epb->next;
		epb->pprev = NULL;
	}

	sctp_write_unlock(&head->lock);
672
}
Jon Grimm's avatar
Jon Grimm committed
673 674

/* Look up an association. */
Jon Grimm's avatar
Jon Grimm committed
675 676 677 678
struct sctp_association *__sctp_lookup_association(
					const union sctp_addr *local,
					const union sctp_addr *peer,
					struct sctp_transport **pt)
Jon Grimm's avatar
Jon Grimm committed
679 680
{
	sctp_hashbucket_t *head;
Jon Grimm's avatar
Jon Grimm committed
681 682
	struct sctp_ep_common *epb;
	struct sctp_association *asoc;
683
	struct sctp_transport *transport;
Jon Grimm's avatar
Jon Grimm committed
684 685 686
	int hash;

	/* Optimize here for direct hit, only listening connections can
687
	 * have wildcards anyways.
Jon Grimm's avatar
Jon Grimm committed
688
	 */
689
	hash = sctp_assoc_hashfn(local->v4.sin_port, peer->v4.sin_port);
Jon Grimm's avatar
Jon Grimm committed
690 691 692 693
	head = &sctp_proto.assoc_hashbucket[hash];
	read_lock(&head->lock);
	for (epb = head->chain; epb; epb = epb->next) {
		asoc = sctp_assoc(epb);
694
		transport = sctp_assoc_is_match(asoc, local, peer);
695 696 697 698
		if (transport)
			goto hit;
	}

Jon Grimm's avatar
Jon Grimm committed
699 700 701 702 703
	read_unlock(&head->lock);

	return NULL;

hit:
704
	*pt = transport;
Jon Grimm's avatar
Jon Grimm committed
705 706 707 708
	sctp_association_hold(asoc);
	sock_hold(epb->sk);
	read_unlock(&head->lock);
	return asoc;
709
}
Jon Grimm's avatar
Jon Grimm committed
710

711
/* Look up an association. BH-safe. */
Jon Grimm's avatar
Jon Grimm committed
712
struct sctp_association *sctp_lookup_association(const union sctp_addr *laddr,
Jon Grimm's avatar
Jon Grimm committed
713
					    const union sctp_addr *paddr,
714
					    struct sctp_transport **transportp)
715
{
Jon Grimm's avatar
Jon Grimm committed
716
	struct sctp_association *asoc;
717 718 719 720

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

722 723 724 725
	return asoc;
}

/* Is there an association matching the given local and peer addresses? */
Jon Grimm's avatar
Jon Grimm committed
726 727
int sctp_has_association(const union sctp_addr *laddr,
			 const union sctp_addr *paddr)
728
{
Jon Grimm's avatar
Jon Grimm committed
729
	struct sctp_association *asoc;
730
	struct sctp_transport *transport;
731

732
	if ((asoc = sctp_lookup_association(laddr, paddr, &transport))) {
733 734 735 736 737 738 739 740
		sock_put(asoc->base.sk);
		sctp_association_put(asoc);
		return 1;
	}

	return 0;
}

Jon Grimm's avatar
Jon Grimm committed
741
/*
742
 * SCTP Implementors Guide, 2.18 Handling of address
Jon Grimm's avatar
Jon Grimm committed
743 744 745 746 747 748 749
 * 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.
750
 *
Jon Grimm's avatar
Jon Grimm committed
751 752 753 754 755 756 757 758
 * 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
759
static struct sctp_association *__sctp_rcv_init_lookup(struct sk_buff *skb,
760
	const union sctp_addr *laddr, struct sctp_transport **transportp)
Jon Grimm's avatar
Jon Grimm committed
761
{
Jon Grimm's avatar
Jon Grimm committed
762
	struct sctp_association *asoc;
Jon Grimm's avatar
Jon Grimm committed
763 764
	union sctp_addr addr;
	union sctp_addr *paddr = &addr;
765
	struct sctphdr *sh = (struct sctphdr *) skb->h.raw;
Jon Grimm's avatar
Jon Grimm committed
766
	sctp_chunkhdr_t *ch;
767 768
	union sctp_params params;
	sctp_init_chunk_t *init;
Jon Grimm's avatar
Jon Grimm committed
769

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

772 773 774 775 776 777
	/* 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
778
		return NULL;
779
	}
Jon Grimm's avatar
Jon Grimm committed
780

781 782 783 784 785 786 787 788 789 790 791 792 793 794
	/*
	 * 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
795 796
	 * the region we search for address parameters.
	 */
797
	init = (sctp_init_chunk_t *)skb->data;
798

799 800
	/* Walk the parameters looking for embedded addresses. */
	sctp_walk_params(params, init, init_hdr.params) {
801 802

		/* Note: Ignoring hostname addresses. */
803 804
		if ((SCTP_PARAM_IPV4_ADDRESS != params.p->type) &&
		    (SCTP_PARAM_IPV6_ADDRESS != params.p->type))
Jon Grimm's avatar
Jon Grimm committed
805
			continue;
806

807
		sctp_param2sockaddr(paddr, params.addr, ntohs(sh->source), 0);
808
		asoc = __sctp_lookup_association(laddr, paddr, transportp);
809 810 811
		if (asoc)
			return asoc;
	}
Jon Grimm's avatar
Jon Grimm committed
812 813

	return NULL;
814
}
Jon Grimm's avatar
Jon Grimm committed
815 816

/* Lookup an association for an inbound skb. */
Jon Grimm's avatar
Jon Grimm committed
817
struct sctp_association *__sctp_rcv_lookup(struct sk_buff *skb,
Jon Grimm's avatar
Jon Grimm committed
818 819
				      const union sctp_addr *paddr,
				      const union sctp_addr *laddr,
820
				      struct sctp_transport **transportp)
Jon Grimm's avatar
Jon Grimm committed
821
{
Jon Grimm's avatar
Jon Grimm committed
822
	struct sctp_association *asoc;
Jon Grimm's avatar
Jon Grimm committed
823

824
	asoc = __sctp_lookup_association(laddr, paddr, transportp);
825

Jon Grimm's avatar
Jon Grimm committed
826
	/* Further lookup for INIT/INIT-ACK packets.
827
	 * SCTP Implementors Guide, 2.18 Handling of address
Jon Grimm's avatar
Jon Grimm committed
828 829
	 * parameters within the INIT or INIT-ACK.
	 */
830
	if (!asoc)
Jon Grimm's avatar
Jon Grimm committed
831
		asoc = __sctp_rcv_init_lookup(skb, laddr, transportp);
Jon Grimm's avatar
Jon Grimm committed
832

833 834
	return asoc;
}