Skip to content
Snippets Groups Projects
socket.c 155 KiB
Newer Older
  • Learn to ignore specific revisions
  • Linus Torvalds's avatar
    Linus Torvalds committed
    	if (asoc) {
    		info.sinfo_stream = asoc->default_stream;
    		info.sinfo_flags = asoc->default_flags;
    		info.sinfo_ppid = asoc->default_ppid;
    		info.sinfo_context = asoc->default_context;
    		info.sinfo_timetolive = asoc->default_timetolive;
    	} else {
    		info.sinfo_stream = sp->default_stream;
    		info.sinfo_flags = sp->default_flags;
    		info.sinfo_ppid = sp->default_ppid;
    		info.sinfo_context = sp->default_context;
    		info.sinfo_timetolive = sp->default_timetolive;
    	}
    
    	if (copy_to_user(optval, &info, sizeof(struct sctp_sndrcvinfo)))
    		return -EFAULT;
    
    	return 0;
    }
    
    /*
     *
     * 7.1.5 SCTP_NODELAY
     *
     * Turn on/off any Nagle-like algorithm.  This means that packets are
     * generally sent as soon as possible and no unnecessary delays are
     * introduced, at the cost of more packets in the network.  Expects an
     * integer boolean flag.
     */
    
    static int sctp_getsockopt_nodelay(struct sock *sk, int len,
    				   char __user *optval, int __user *optlen)
    {
    	int val;
    
    	if (len < sizeof(int))
    		return -EINVAL;
    
    	len = sizeof(int);
    	val = (sctp_sk(sk)->nodelay == 1);
    	if (put_user(len, optlen))
    		return -EFAULT;
    	if (copy_to_user(optval, &val, len))
    		return -EFAULT;
    	return 0;
    }
    
    /*
     *
     * 7.1.1 SCTP_RTOINFO
     *
     * The protocol parameters used to initialize and bound retransmission
     * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
     * and modify these parameters.
     * All parameters are time values, in milliseconds.  A value of 0, when
     * modifying the parameters, indicates that the current value should not
     * be changed.
     *
     */
    static int sctp_getsockopt_rtoinfo(struct sock *sk, int len,
    				char __user *optval,
    				int __user *optlen) {
    	struct sctp_rtoinfo rtoinfo;
    	struct sctp_association *asoc;
    
    	if (len != sizeof (struct sctp_rtoinfo))
    		return -EINVAL;
    
    	if (copy_from_user(&rtoinfo, optval, sizeof (struct sctp_rtoinfo)))
    		return -EFAULT;
    
    	asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
    
    	if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
    		return -EINVAL;
    
    	/* Values corresponding to the specific association. */
    	if (asoc) {
    		rtoinfo.srto_initial = jiffies_to_msecs(asoc->rto_initial);
    		rtoinfo.srto_max = jiffies_to_msecs(asoc->rto_max);
    		rtoinfo.srto_min = jiffies_to_msecs(asoc->rto_min);
    	} else {
    		/* Values corresponding to the endpoint. */
    		struct sctp_sock *sp = sctp_sk(sk);
    
    		rtoinfo.srto_initial = sp->rtoinfo.srto_initial;
    		rtoinfo.srto_max = sp->rtoinfo.srto_max;
    		rtoinfo.srto_min = sp->rtoinfo.srto_min;
    	}
    
    	if (put_user(len, optlen))
    		return -EFAULT;
    
    	if (copy_to_user(optval, &rtoinfo, len))
    		return -EFAULT;
    
    	return 0;
    }
    
    /*
     *
     * 7.1.2 SCTP_ASSOCINFO
     *
     * This option is used to tune the the maximum retransmission attempts
     * of the association.
     * Returns an error if the new association retransmission value is
     * greater than the sum of the retransmission value  of the peer.
     * See [SCTP] for more information.
     *
     */
    static int sctp_getsockopt_associnfo(struct sock *sk, int len,
    				     char __user *optval,
    				     int __user *optlen)
    {
    
    	struct sctp_assocparams assocparams;
    	struct sctp_association *asoc;
    	struct list_head *pos;
    	int cnt = 0;
    
    	if (len != sizeof (struct sctp_assocparams))
    		return -EINVAL;
    
    	if (copy_from_user(&assocparams, optval,
    			sizeof (struct sctp_assocparams)))
    		return -EFAULT;
    
    	asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
    
    	if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
    		return -EINVAL;
    
    	/* Values correspoinding to the specific association */
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		assocparams.sasoc_asocmaxrxt = asoc->max_retrans;
    		assocparams.sasoc_peer_rwnd = asoc->peer.rwnd;
    		assocparams.sasoc_local_rwnd = asoc->a_rwnd;
    		assocparams.sasoc_cookie_life = (asoc->cookie_life.tv_sec
    						* 1000) +
    						(asoc->cookie_life.tv_usec
    						/ 1000);
    
    		list_for_each(pos, &asoc->peer.transport_addr_list) {
    			cnt ++;
    		}
    
    		assocparams.sasoc_number_peer_destinations = cnt;
    	} else {
    		/* Values corresponding to the endpoint */
    		struct sctp_sock *sp = sctp_sk(sk);
    
    		assocparams.sasoc_asocmaxrxt = sp->assocparams.sasoc_asocmaxrxt;
    		assocparams.sasoc_peer_rwnd = sp->assocparams.sasoc_peer_rwnd;
    		assocparams.sasoc_local_rwnd = sp->assocparams.sasoc_local_rwnd;
    		assocparams.sasoc_cookie_life =
    					sp->assocparams.sasoc_cookie_life;
    		assocparams.sasoc_number_peer_destinations =
    					sp->assocparams.
    					sasoc_number_peer_destinations;
    	}
    
    	if (put_user(len, optlen))
    		return -EFAULT;
    
    	if (copy_to_user(optval, &assocparams, len))
    		return -EFAULT;
    
    	return 0;
    }
    
    /*
     * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
     *
     * This socket option is a boolean flag which turns on or off mapped V4
     * addresses.  If this option is turned on and the socket is type
     * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
     * If this option is turned off, then no mapping will be done of V4
     * addresses and a user will receive both PF_INET6 and PF_INET type
     * addresses on the socket.
     */
    static int sctp_getsockopt_mappedv4(struct sock *sk, int len,
    				    char __user *optval, int __user *optlen)
    {
    	int val;
    	struct sctp_sock *sp = sctp_sk(sk);
    
    	if (len < sizeof(int))
    		return -EINVAL;
    
    	len = sizeof(int);
    	val = sp->v4mapped;
    	if (put_user(len, optlen))
    		return -EFAULT;
    	if (copy_to_user(optval, &val, len))
    		return -EFAULT;
    
    	return 0;
    }
    
    /*
     * 7.1.17 Set the maximum fragrmentation size (SCTP_MAXSEG)
     *
     * This socket option specifies the maximum size to put in any outgoing
     * SCTP chunk.  If a message is larger than this size it will be
     * fragmented by SCTP into the specified size.  Note that the underlying
     * SCTP implementation may fragment into smaller sized chunks when the
     * PMTU of the underlying association is smaller than the value set by
     * the user.
     */
    static int sctp_getsockopt_maxseg(struct sock *sk, int len,
    				  char __user *optval, int __user *optlen)
    {
    	int val;
    
    	if (len < sizeof(int))
    		return -EINVAL;
    
    	len = sizeof(int);
    
    	val = sctp_sk(sk)->user_frag;
    	if (put_user(len, optlen))
    		return -EFAULT;
    	if (copy_to_user(optval, &val, len))
    		return -EFAULT;
    
    	return 0;
    }
    
    SCTP_STATIC int sctp_getsockopt(struct sock *sk, int level, int optname,
    				char __user *optval, int __user *optlen)
    {
    	int retval = 0;
    	int len;
    
    
    	SCTP_DEBUG_PRINTK("sctp_getsockopt(sk: %p... optname: %d)\n",
    			  sk, optname);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    	/* I can hardly begin to describe how wrong this is.  This is
    	 * so broken as to be worse than useless.  The API draft
    	 * REALLY is NOT helpful here...  I am not convinced that the
    	 * semantics of getsockopt() with a level OTHER THAN SOL_SCTP
    	 * are at all well-founded.
    	 */
    	if (level != SOL_SCTP) {
    		struct sctp_af *af = sctp_sk(sk)->pf->af;
    
    		retval = af->getsockopt(sk, level, optname, optval, optlen);
    		return retval;
    	}
    
    	if (get_user(len, optlen))
    		return -EFAULT;
    
    	sctp_lock_sock(sk);
    
    	switch (optname) {
    	case SCTP_STATUS:
    		retval = sctp_getsockopt_sctp_status(sk, len, optval, optlen);
    		break;
    	case SCTP_DISABLE_FRAGMENTS:
    		retval = sctp_getsockopt_disable_fragments(sk, len, optval,
    							   optlen);
    		break;
    	case SCTP_EVENTS:
    		retval = sctp_getsockopt_events(sk, len, optval, optlen);
    		break;
    	case SCTP_AUTOCLOSE:
    		retval = sctp_getsockopt_autoclose(sk, len, optval, optlen);
    		break;
    	case SCTP_SOCKOPT_PEELOFF:
    		retval = sctp_getsockopt_peeloff(sk, len, optval, optlen);
    		break;
    	case SCTP_PEER_ADDR_PARAMS:
    		retval = sctp_getsockopt_peer_addr_params(sk, len, optval,
    							  optlen);
    		break;
    	case SCTP_INITMSG:
    		retval = sctp_getsockopt_initmsg(sk, len, optval, optlen);
    		break;
    
    	case SCTP_GET_PEER_ADDRS_NUM_OLD:
    		retval = sctp_getsockopt_peer_addrs_num_old(sk, len, optval,
    							    optlen);
    		break;
    	case SCTP_GET_LOCAL_ADDRS_NUM_OLD:
    		retval = sctp_getsockopt_local_addrs_num_old(sk, len, optval,
    							     optlen);
    		break;
    	case SCTP_GET_PEER_ADDRS_OLD:
    		retval = sctp_getsockopt_peer_addrs_old(sk, len, optval,
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    							optlen);
    		break;
    
    	case SCTP_GET_LOCAL_ADDRS_OLD:
    		retval = sctp_getsockopt_local_addrs_old(sk, len, optval,
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674
    							 optlen);
    		break;
    	case SCTP_GET_PEER_ADDRS:
    		retval = sctp_getsockopt_peer_addrs(sk, len, optval,
    						    optlen);
    		break;
    	case SCTP_GET_LOCAL_ADDRS:
    		retval = sctp_getsockopt_local_addrs(sk, len, optval,
    						     optlen);
    		break;
    	case SCTP_DEFAULT_SEND_PARAM:
    		retval = sctp_getsockopt_default_send_param(sk, len,
    							    optval, optlen);
    		break;
    	case SCTP_PRIMARY_ADDR:
    		retval = sctp_getsockopt_primary_addr(sk, len, optval, optlen);
    		break;
    	case SCTP_NODELAY:
    		retval = sctp_getsockopt_nodelay(sk, len, optval, optlen);
    		break;
    	case SCTP_RTOINFO:
    		retval = sctp_getsockopt_rtoinfo(sk, len, optval, optlen);
    		break;
    	case SCTP_ASSOCINFO:
    		retval = sctp_getsockopt_associnfo(sk, len, optval, optlen);
    		break;
    	case SCTP_I_WANT_MAPPED_V4_ADDR:
    		retval = sctp_getsockopt_mappedv4(sk, len, optval, optlen);
    		break;
    	case SCTP_MAXSEG:
    		retval = sctp_getsockopt_maxseg(sk, len, optval, optlen);
    		break;
    	case SCTP_GET_PEER_ADDR_INFO:
    		retval = sctp_getsockopt_peer_addr_info(sk, len, optval,
    							optlen);
    		break;
    	case SCTP_ADAPTION_LAYER:
    		retval = sctp_getsockopt_adaption_layer(sk, len, optval,
    							optlen);
    		break;
    	default:
    		retval = -ENOPROTOOPT;
    		break;
    	};
    
    	sctp_release_sock(sk);
    	return retval;
    }
    
    static void sctp_hash(struct sock *sk)
    {
    	/* STUB */
    }
    
    static void sctp_unhash(struct sock *sk)
    {
    	/* STUB */
    }
    
    /* Check if port is acceptable.  Possibly find first available port.
     *
     * The port hash table (contained in the 'global' SCTP protocol storage
     * returned by struct sctp_protocol *sctp_get_protocol()). The hash
     * table is an array of 4096 lists (sctp_bind_hashbucket). Each
     * list (the list number is the port number hashed out, so as you
     * would expect from a hash function, all the ports in a given list have
     * such a number that hashes out to the same list number; you were
     * expecting that, right?); so each list has a set of ports, with a
     * link to the socket (struct sock) that uses it, the port number and
     * a fastreuse flag (FIXME: NPI ipg).
     */
    static struct sctp_bind_bucket *sctp_bucket_create(
    	struct sctp_bind_hashbucket *head, unsigned short snum);
    
    static long sctp_get_port_local(struct sock *sk, union sctp_addr *addr)
    {
    	struct sctp_bind_hashbucket *head; /* hash list */
    	struct sctp_bind_bucket *pp; /* hash list port iterator */
    	unsigned short snum;
    	int ret;
    
    	/* NOTE:  Remember to put this back to net order. */
    	addr->v4.sin_port = ntohs(addr->v4.sin_port);
    	snum = addr->v4.sin_port;
    
    	SCTP_DEBUG_PRINTK("sctp_get_port() begins, snum=%d\n", snum);
    	sctp_local_bh_disable();
    
    	if (snum == 0) {
    		/* Search for an available port.
    		 *
    		 * 'sctp_port_rover' was the last port assigned, so
    		 * we start to search from 'sctp_port_rover +
    		 * 1'. What we do is first check if port 'rover' is
    		 * already in the hash table; if not, we use that; if
    		 * it is, we try next.
    		 */
    		int low = sysctl_local_port_range[0];
    		int high = sysctl_local_port_range[1];
    		int remaining = (high - low) + 1;
    		int rover;
    		int index;
    
    		sctp_spin_lock(&sctp_port_alloc_lock);
    		rover = sctp_port_rover;
    		do {
    			rover++;
    			if ((rover < low) || (rover > high))
    				rover = low;
    			index = sctp_phashfn(rover);
    			head = &sctp_port_hashtable[index];
    			sctp_spin_lock(&head->lock);
    			for (pp = head->chain; pp; pp = pp->next)
    				if (pp->port == rover)
    					goto next;
    			break;
    		next:
    			sctp_spin_unlock(&head->lock);
    		} while (--remaining > 0);
    		sctp_port_rover = rover;
    		sctp_spin_unlock(&sctp_port_alloc_lock);
    
    		/* Exhausted local port range during search? */
    		ret = 1;
    		if (remaining <= 0)
    			goto fail;
    
    		/* OK, here is the one we will use.  HEAD (the port
    		 * hash table list entry) is non-NULL and we hold it's
    		 * mutex.
    		 */
    		snum = rover;
    	} else {
    		/* We are given an specific port number; we verify
    		 * that it is not being used. If it is used, we will
    		 * exahust the search in the hash list corresponding
    		 * to the port number (snum) - we detect that with the
    		 * port iterator, pp being NULL.
    		 */
    		head = &sctp_port_hashtable[sctp_phashfn(snum)];
    		sctp_spin_lock(&head->lock);
    		for (pp = head->chain; pp; pp = pp->next) {
    			if (pp->port == snum)
    				goto pp_found;
    		}
    	}
    	pp = NULL;
    	goto pp_not_found;
    pp_found:
    	if (!hlist_empty(&pp->owner)) {
    		/* We had a port hash table hit - there is an
    		 * available port (pp != NULL) and it is being
    		 * used by other socket (pp->owner not empty); that other
    		 * socket is going to be sk2.
    		 */
    		int reuse = sk->sk_reuse;
    		struct sock *sk2;
    		struct hlist_node *node;
    
    		SCTP_DEBUG_PRINTK("sctp_get_port() found a possible match\n");
    		if (pp->fastreuse && sk->sk_reuse)
    			goto success;
    
    		/* Run through the list of sockets bound to the port
    		 * (pp->port) [via the pointers bind_next and
    		 * bind_pprev in the struct sock *sk2 (pp->sk)]. On each one,
    		 * we get the endpoint they describe and run through
    		 * the endpoint's list of IP (v4 or v6) addresses,
    		 * comparing each of the addresses with the address of
    		 * the socket sk. If we find a match, then that means
    		 * that this port/socket (sk) combination are already
    		 * in an endpoint.
    		 */
    		sk_for_each_bound(sk2, node, &pp->owner) {
    			struct sctp_endpoint *ep2;
    			ep2 = sctp_sk(sk2)->ep;
    
    			if (reuse && sk2->sk_reuse)
    				continue;
    
    			if (sctp_bind_addr_match(&ep2->base.bind_addr, addr,
    						 sctp_sk(sk))) {
    				ret = (long)sk2;
    				goto fail_unlock;
    			}
    		}
    		SCTP_DEBUG_PRINTK("sctp_get_port(): Found a match\n");
    	}
    pp_not_found:
    	/* If there was a hash table miss, create a new port.  */
    	ret = 1;
    	if (!pp && !(pp = sctp_bucket_create(head, snum)))
    		goto fail_unlock;
    
    	/* In either case (hit or miss), make sure fastreuse is 1 only
    	 * if sk->sk_reuse is too (that is, if the caller requested
    	 * SO_REUSEADDR on this socket -sk-).
    	 */
    	if (hlist_empty(&pp->owner))
    		pp->fastreuse = sk->sk_reuse ? 1 : 0;
    	else if (pp->fastreuse && !sk->sk_reuse)
    		pp->fastreuse = 0;
    
    	/* We are set, so fill up all the data in the hash table
    	 * entry, tie the socket list information with the rest of the
    	 * sockets FIXME: Blurry, NPI (ipg).
    	 */
    success:
    	inet_sk(sk)->num = snum;
    	if (!sctp_sk(sk)->bind_hash) {
    		sk_add_bind_node(sk, &pp->owner);
    		sctp_sk(sk)->bind_hash = pp;
    	}
    	ret = 0;
    
    fail_unlock:
    	sctp_spin_unlock(&head->lock);
    
    fail:
    	sctp_local_bh_enable();
    	addr->v4.sin_port = htons(addr->v4.sin_port);
    	return ret;
    }
    
    /* Assign a 'snum' port to the socket.  If snum == 0, an ephemeral
     * port is requested.
     */
    static int sctp_get_port(struct sock *sk, unsigned short snum)
    {
    	long ret;
    	union sctp_addr addr;
    	struct sctp_af *af = sctp_sk(sk)->pf->af;
    
    	/* Set up a dummy address struct from the sk. */
    	af->from_sk(&addr, sk);
    	addr.v4.sin_port = htons(snum);
    
    	/* Note: sk->sk_num gets filled in if ephemeral port request. */
    	ret = sctp_get_port_local(sk, &addr);
    
    	return (ret ? 1 : 0);
    }
    
    /*
     * 3.1.3 listen() - UDP Style Syntax
     *
     *   By default, new associations are not accepted for UDP style sockets.
     *   An application uses listen() to mark a socket as being able to
     *   accept new associations.
     */
    SCTP_STATIC int sctp_seqpacket_listen(struct sock *sk, int backlog)
    {
    	struct sctp_sock *sp = sctp_sk(sk);
    	struct sctp_endpoint *ep = sp->ep;
    
    	/* Only UDP style sockets that are not peeled off are allowed to
    	 * listen().
    	 */
    	if (!sctp_style(sk, UDP))
    		return -EINVAL;
    
    	/* If backlog is zero, disable listening. */
    	if (!backlog) {
    		if (sctp_sstate(sk, CLOSED))
    			return 0;
    		
    		sctp_unhash_endpoint(ep);
    		sk->sk_state = SCTP_SS_CLOSED;
    	}
    
    	/* Return if we are already listening. */
    	if (sctp_sstate(sk, LISTENING))
    		return 0;
    		
    	/*
    	 * If a bind() or sctp_bindx() is not called prior to a listen()
    	 * call that allows new associations to be accepted, the system
    	 * picks an ephemeral port and will choose an address set equivalent
    	 * to binding with a wildcard address.
    	 *
    	 * This is not currently spelled out in the SCTP sockets
    	 * extensions draft, but follows the practice as seen in TCP
    	 * sockets.
    	 */
    	if (!ep->base.bind_addr.port) {
    		if (sctp_autobind(sk))
    			return -EAGAIN;
    	}
    	sk->sk_state = SCTP_SS_LISTENING;
    	sctp_hash_endpoint(ep);
    	return 0;
    }
    
    /*
     * 4.1.3 listen() - TCP Style Syntax
     *
     *   Applications uses listen() to ready the SCTP endpoint for accepting
     *   inbound associations.
     */
    SCTP_STATIC int sctp_stream_listen(struct sock *sk, int backlog)
    {
    	struct sctp_sock *sp = sctp_sk(sk);
    	struct sctp_endpoint *ep = sp->ep;
    
    	/* If backlog is zero, disable listening. */
    	if (!backlog) {
    		if (sctp_sstate(sk, CLOSED))
    			return 0;
    		
    		sctp_unhash_endpoint(ep);
    		sk->sk_state = SCTP_SS_CLOSED;
    	}
    
    	if (sctp_sstate(sk, LISTENING))
    		return 0;
    
    	/*
    	 * If a bind() or sctp_bindx() is not called prior to a listen()
    	 * call that allows new associations to be accepted, the system
    	 * picks an ephemeral port and will choose an address set equivalent
    	 * to binding with a wildcard address.
    	 *
    	 * This is not currently spelled out in the SCTP sockets
    	 * extensions draft, but follows the practice as seen in TCP
    	 * sockets.
    	 */
    	if (!ep->base.bind_addr.port) {
    		if (sctp_autobind(sk))
    			return -EAGAIN;
    	}
    	sk->sk_state = SCTP_SS_LISTENING;
    	sk->sk_max_ack_backlog = backlog;
    	sctp_hash_endpoint(ep);
    	return 0;
    }
    
    /*
     *  Move a socket to LISTENING state.
     */
    int sctp_inet_listen(struct socket *sock, int backlog)
    {
    	struct sock *sk = sock->sk;
    	struct crypto_tfm *tfm=NULL;
    	int err = -EINVAL;
    
    	if (unlikely(backlog < 0))
    		goto out;
    
    	sctp_lock_sock(sk);
    
    	if (sock->state != SS_UNCONNECTED)
    		goto out;
    
    	/* Allocate HMAC for generating cookie. */
    	if (sctp_hmac_alg) {
    		tfm = sctp_crypto_alloc_tfm(sctp_hmac_alg, 0);
    		if (!tfm) {
    			err = -ENOSYS;
    			goto out;
    		}
    	}
    
    	switch (sock->type) {
    	case SOCK_SEQPACKET:
    		err = sctp_seqpacket_listen(sk, backlog);
    		break;
    	case SOCK_STREAM:
    		err = sctp_stream_listen(sk, backlog);
    		break;
    	default:
    		break;
    	};
    	if (err)
    		goto cleanup;
    
    	/* Store away the transform reference. */
    	sctp_sk(sk)->hmac = tfm;
    out:
    	sctp_release_sock(sk);
    	return err;
    cleanup:
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	goto out;
    }
    
    /*
     * This function is done by modeling the current datagram_poll() and the
     * tcp_poll().  Note that, based on these implementations, we don't
     * lock the socket in this function, even though it seems that,
     * ideally, locking or some other mechanisms can be used to ensure
    
     * the integrity of the counters (sndbuf and wmem_alloc) used
    
    Linus Torvalds's avatar
    Linus Torvalds committed
     * in this place.  We assume that we don't need locks either until proven
     * otherwise.
     *
     * Another thing to note is that we include the Async I/O support
     * here, again, by modeling the current TCP/UDP code.  We don't have
     * a good way to test with it yet.
     */
    unsigned int sctp_poll(struct file *file, struct socket *sock, poll_table *wait)
    {
    	struct sock *sk = sock->sk;
    	struct sctp_sock *sp = sctp_sk(sk);
    	unsigned int mask;
    
    	poll_wait(file, sk->sk_sleep, wait);
    
    	/* A TCP-style listening socket becomes readable when the accept queue
    	 * is not empty.
    	 */
    	if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
    		return (!list_empty(&sp->ep->asocs)) ?
    		       	(POLLIN | POLLRDNORM) : 0;
    
    	mask = 0;
    
    	/* Is there any exceptional events?  */
    	if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
    		mask |= POLLERR;
    	if (sk->sk_shutdown == SHUTDOWN_MASK)
    		mask |= POLLHUP;
    
    	/* Is it readable?  Reconsider this code with TCP-style support.  */
    	if (!skb_queue_empty(&sk->sk_receive_queue) ||
    	    (sk->sk_shutdown & RCV_SHUTDOWN))
    		mask |= POLLIN | POLLRDNORM;
    
    	/* The association is either gone or not ready.  */
    	if (!sctp_style(sk, UDP) && sctp_sstate(sk, CLOSED))
    		return mask;
    
    	/* Is it writable?  */
    	if (sctp_writeable(sk)) {
    		mask |= POLLOUT | POLLWRNORM;
    	} else {
    		set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
    		/*
    		 * Since the socket is not locked, the buffer
    		 * might be made available after the writeable check and
    		 * before the bit is set.  This could cause a lost I/O
    		 * signal.  tcp_poll() has a race breaker for this race
    		 * condition.  Based on their implementation, we put
    		 * in the following code to cover it as well.
    		 */
    		if (sctp_writeable(sk))
    			mask |= POLLOUT | POLLWRNORM;
    	}
    	return mask;
    }
    
    /********************************************************************
     * 2nd Level Abstractions
     ********************************************************************/
    
    static struct sctp_bind_bucket *sctp_bucket_create(
    	struct sctp_bind_hashbucket *head, unsigned short snum)
    {
    	struct sctp_bind_bucket *pp;
    
    	pp = kmem_cache_alloc(sctp_bucket_cachep, SLAB_ATOMIC);
    	SCTP_DBG_OBJCNT_INC(bind_bucket);
    	if (pp) {
    		pp->port = snum;
    		pp->fastreuse = 0;
    		INIT_HLIST_HEAD(&pp->owner);
    		if ((pp->next = head->chain) != NULL)
    			pp->next->pprev = &pp->next;
    		head->chain = pp;
    		pp->pprev = &head->chain;
    	}
    	return pp;
    }
    
    /* Caller must hold hashbucket lock for this tb with local BH disabled */
    static void sctp_bucket_destroy(struct sctp_bind_bucket *pp)
    {
    	if (hlist_empty(&pp->owner)) {
    		if (pp->next)
    			pp->next->pprev = pp->pprev;
    		*(pp->pprev) = pp->next;
    		kmem_cache_free(sctp_bucket_cachep, pp);
    		SCTP_DBG_OBJCNT_DEC(bind_bucket);
    	}
    }
    
    /* Release this socket's reference to a local port.  */
    static inline void __sctp_put_port(struct sock *sk)
    {
    	struct sctp_bind_hashbucket *head =
    		&sctp_port_hashtable[sctp_phashfn(inet_sk(sk)->num)];
    	struct sctp_bind_bucket *pp;
    
    	sctp_spin_lock(&head->lock);
    	pp = sctp_sk(sk)->bind_hash;
    	__sk_del_bind_node(sk);
    	sctp_sk(sk)->bind_hash = NULL;
    	inet_sk(sk)->num = 0;
    	sctp_bucket_destroy(pp);
    	sctp_spin_unlock(&head->lock);
    }
    
    void sctp_put_port(struct sock *sk)
    {
    	sctp_local_bh_disable();
    	__sctp_put_port(sk);
    	sctp_local_bh_enable();
    }
    
    /*
     * The system picks an ephemeral port and choose an address set equivalent
     * to binding with a wildcard address.
     * One of those addresses will be the primary address for the association.
     * This automatically enables the multihoming capability of SCTP.
     */
    static int sctp_autobind(struct sock *sk)
    {
    	union sctp_addr autoaddr;
    	struct sctp_af *af;
    	unsigned short port;
    
    	/* Initialize a local sockaddr structure to INADDR_ANY. */
    	af = sctp_sk(sk)->pf->af;
    
    	port = htons(inet_sk(sk)->num);
    	af->inaddr_any(&autoaddr, port);
    
    	return sctp_do_bind(sk, &autoaddr, af->sockaddr_len);
    }
    
    /* Parse out IPPROTO_SCTP CMSG headers.  Perform only minimal validation.
     *
     * From RFC 2292
     * 4.2 The cmsghdr Structure *
     *
     * When ancillary data is sent or received, any number of ancillary data
     * objects can be specified by the msg_control and msg_controllen members of
     * the msghdr structure, because each object is preceded by
     * a cmsghdr structure defining the object's length (the cmsg_len member).
     * Historically Berkeley-derived implementations have passed only one object
     * at a time, but this API allows multiple objects to be
     * passed in a single call to sendmsg() or recvmsg(). The following example
     * shows two ancillary data objects in a control buffer.
     *
     *   |<--------------------------- msg_controllen -------------------------->|
     *   |                                                                       |
     *
     *   |<----- ancillary data object ----->|<----- ancillary data object ----->|
     *
     *   |<---------- CMSG_SPACE() --------->|<---------- CMSG_SPACE() --------->|
     *   |                                   |                                   |
     *
     *   |<---------- cmsg_len ---------->|  |<--------- cmsg_len ----------->|  |
     *
     *   |<--------- CMSG_LEN() --------->|  |<-------- CMSG_LEN() ---------->|  |
     *   |                                |  |                                |  |
     *
     *   +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
     *   |cmsg_|cmsg_|cmsg_|XX|           |XX|cmsg_|cmsg_|cmsg_|XX|           |XX|
     *
     *   |len  |level|type |XX|cmsg_data[]|XX|len  |level|type |XX|cmsg_data[]|XX|
     *
     *   +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
     *    ^
     *    |
     *
     * msg_control
     * points here
     */
    SCTP_STATIC int sctp_msghdr_parse(const struct msghdr *msg,
    				  sctp_cmsgs_t *cmsgs)
    {
    	struct cmsghdr *cmsg;
    
    	for (cmsg = CMSG_FIRSTHDR(msg);
    	     cmsg != NULL;
    	     cmsg = CMSG_NXTHDR((struct msghdr*)msg, cmsg)) {
    		if (!CMSG_OK(msg, cmsg))
    			return -EINVAL;
    
    		/* Should we parse this header or ignore?  */
    		if (cmsg->cmsg_level != IPPROTO_SCTP)
    			continue;
    
    		/* Strictly check lengths following example in SCM code.  */
    		switch (cmsg->cmsg_type) {
    		case SCTP_INIT:
    			/* SCTP Socket API Extension
    			 * 5.2.1 SCTP Initiation Structure (SCTP_INIT)
    			 *
    			 * This cmsghdr structure provides information for
    			 * initializing new SCTP associations with sendmsg().
    			 * The SCTP_INITMSG socket option uses this same data
    			 * structure.  This structure is not used for
    			 * recvmsg().
    			 *
    			 * cmsg_level    cmsg_type      cmsg_data[]
    			 * ------------  ------------   ----------------------
    			 * IPPROTO_SCTP  SCTP_INIT      struct sctp_initmsg
    			 */
    			if (cmsg->cmsg_len !=
    			    CMSG_LEN(sizeof(struct sctp_initmsg)))
    				return -EINVAL;
    			cmsgs->init = (struct sctp_initmsg *)CMSG_DATA(cmsg);
    			break;
    
    		case SCTP_SNDRCV:
    			/* SCTP Socket API Extension
    			 * 5.2.2 SCTP Header Information Structure(SCTP_SNDRCV)
    			 *
    			 * This cmsghdr structure specifies SCTP options for
    			 * sendmsg() and describes SCTP header information
    			 * about a received message through recvmsg().
    			 *
    			 * cmsg_level    cmsg_type      cmsg_data[]
    			 * ------------  ------------   ----------------------
    			 * IPPROTO_SCTP  SCTP_SNDRCV    struct sctp_sndrcvinfo
    			 */
    			if (cmsg->cmsg_len !=
    			    CMSG_LEN(sizeof(struct sctp_sndrcvinfo)))
    				return -EINVAL;
    
    			cmsgs->info =
    				(struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
    
    			/* Minimally, validate the sinfo_flags. */
    			if (cmsgs->info->sinfo_flags &
    
    			    ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
    			      SCTP_ABORT | SCTP_EOF))
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    				return -EINVAL;
    			break;
    
    		default:
    			return -EINVAL;
    		};
    	}
    	return 0;
    }
    
    /*
     * Wait for a packet..
     * Note: This function is the same function as in core/datagram.c
     * with a few modifications to make lksctp work.
     */
    static int sctp_wait_for_packet(struct sock * sk, int *err, long *timeo_p)
    {
    	int error;
    	DEFINE_WAIT(wait);
    
    	prepare_to_wait_exclusive(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
    
    	/* Socket errors? */
    	error = sock_error(sk);
    	if (error)
    		goto out;
    
    	if (!skb_queue_empty(&sk->sk_receive_queue))
    		goto ready;
    
    	/* Socket shut down?  */
    	if (sk->sk_shutdown & RCV_SHUTDOWN)
    		goto out;
    
    	/* Sequenced packets can come disconnected.  If so we report the
    	 * problem.
    	 */
    	error = -ENOTCONN;
    
    	/* Is there a good reason to think that we may receive some data?  */
    	if (list_empty(&sctp_sk(sk)->ep->asocs) && !sctp_sstate(sk, LISTENING))
    		goto out;
    
    	/* Handle signals.  */
    	if (signal_pending(current))
    		goto interrupted;
    
    	/* Let another process have a go.  Since we are going to sleep
    	 * anyway.  Note: This may cause odd behaviors if the message
    	 * does not fit in the user's buffer, but this seems to be the
    	 * only way to honor MSG_DONTWAIT realistically.
    	 */
    	sctp_release_sock(sk);
    	*timeo_p = schedule_timeout(*timeo_p);
    	sctp_lock_sock(sk);
    
    ready:
    	finish_wait(sk->sk_sleep, &wait);
    	return 0;
    
    interrupted:
    	error = sock_intr_errno(*timeo_p);
    
    out:
    	finish_wait(sk->sk_sleep, &wait);
    	*err = error;
    	return error;
    }
    
    /* Receive a datagram.
     * Note: This is pretty much the same routine as in core/datagram.c
     * with a few changes to make lksctp work.
     */
    static struct sk_buff *sctp_skb_recv_datagram(struct sock *sk, int flags,
    					      int noblock, int *err)
    {
    	int error;
    	struct sk_buff *skb;
    	long timeo;