Skip to content
Snippets Groups Projects
socket.c 146 KiB
Newer Older
  • Learn to ignore specific revisions
  • Linus Torvalds's avatar
    Linus Torvalds committed
    {
    	/* Applicable to UDP-style socket only */
    	if (sctp_style(sk, TCP))
    		return -EOPNOTSUPP;
    	if (len != sizeof(int))
    		return -EINVAL;
    	if (copy_to_user(optval, &sctp_sk(sk)->autoclose, len))
    		return -EFAULT;
    	return 0;
    }
    
    /* Helper routine to branch off an association to a new socket.  */
    SCTP_STATIC int sctp_do_peeloff(struct sctp_association *asoc,
    				struct socket **sockp)
    {
    	struct sock *sk = asoc->base.sk;
    	struct socket *sock;
    	int err = 0;
    
    	/* An association cannot be branched off from an already peeled-off
    	 * socket, nor is this supported for tcp style sockets.
    	 */
    	if (!sctp_style(sk, UDP))
    		return -EINVAL;
    
    	/* Create a new socket.  */
    	err = sock_create(sk->sk_family, SOCK_SEQPACKET, IPPROTO_SCTP, &sock);
    	if (err < 0)
    		return err;
    
    	/* Populate the fields of the newsk from the oldsk and migrate the
    	 * asoc to the newsk.
    	 */
    	sctp_sock_migrate(sk, sock->sk, asoc, SCTP_SOCKET_UDP_HIGH_BANDWIDTH);
    	*sockp = sock;
    
    	return err;
    }
    
    static int sctp_getsockopt_peeloff(struct sock *sk, int len, char __user *optval, int __user *optlen)
    {
    	sctp_peeloff_arg_t peeloff;
    	struct socket *newsock;
    	int retval = 0;
    	struct sctp_association *asoc;
    
    	if (len != sizeof(sctp_peeloff_arg_t))
    		return -EINVAL;
    	if (copy_from_user(&peeloff, optval, len))
    		return -EFAULT;
    
    	asoc = sctp_id2assoc(sk, peeloff.associd);
    	if (!asoc) {
    		retval = -EINVAL;
    		goto out;
    	}
    
    	SCTP_DEBUG_PRINTK("%s: sk: %p asoc: %p\n", __FUNCTION__, sk, asoc);
    
    	retval = sctp_do_peeloff(asoc, &newsock);
    	if (retval < 0)
    		goto out;
    
    	/* Map the socket to an unused fd that can be returned to the user.  */
    	retval = sock_map_fd(newsock);
    	if (retval < 0) {
    		sock_release(newsock);
    		goto out;
    	}
    
    	SCTP_DEBUG_PRINTK("%s: sk: %p asoc: %p newsk: %p sd: %d\n",
    			  __FUNCTION__, sk, asoc, newsock->sk, retval);
    
    	/* Return the fd mapped to the new socket.  */
    	peeloff.sd = retval;
    	if (copy_to_user(optval, &peeloff, len))
    		retval = -EFAULT;
    
    out:
    	return retval;
    }
    
    /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
     *
     * Applications can enable or disable heartbeats for any peer address of
     * an association, modify an address's heartbeat interval, force a
     * heartbeat to be sent immediately, and adjust the address's maximum
     * number of retransmissions sent before an address is considered
     * unreachable.  The following structure is used to access and modify an
     * address's parameters:
     *
     *  struct sctp_paddrparams {
     *      sctp_assoc_t            spp_assoc_id;
     *      struct sockaddr_storage spp_address;
     *      uint32_t                spp_hbinterval;
     *      uint16_t                spp_pathmaxrxt;
     *  };
     *
     *   spp_assoc_id    - (UDP style socket) This is filled in the application,
     *                     and identifies the association for this query.
     *   spp_address     - This specifies which address is of interest.
     *   spp_hbinterval  - This contains the value of the heartbeat interval,
     *                     in milliseconds.  A value of 0, when modifying the
     *                     parameter, specifies that the heartbeat on this
     *                     address should be disabled. A value of UINT32_MAX
     *                     (4294967295), when modifying the parameter,
     *                     specifies that a heartbeat should be sent
     *                     immediately to the peer address, and the current
     *                     interval should remain unchanged.
     *   spp_pathmaxrxt  - This contains the maximum number of
     *                     retransmissions before this address shall be
     *                     considered unreachable.
     */
    static int sctp_getsockopt_peer_addr_params(struct sock *sk, int len,
    						char __user *optval, int __user *optlen)
    {
    	struct sctp_paddrparams params;
    	struct sctp_transport *trans;
    
    	if (len != sizeof(struct sctp_paddrparams))
    		return -EINVAL;
    	if (copy_from_user(&params, optval, len))
    		return -EFAULT;
    
    	/* If no association id is specified retrieve the default value
    	 * for the endpoint that will be used for all future associations
    	 */
    	if (!params.spp_assoc_id &&
    	    sctp_is_any(( union sctp_addr *)&params.spp_address)) {
    		params.spp_hbinterval = sctp_sk(sk)->paddrparam.spp_hbinterval;
    		params.spp_pathmaxrxt = sctp_sk(sk)->paddrparam.spp_pathmaxrxt;
    
    		goto done;
    	}
    
    	trans = sctp_addr_id2transport(sk, &params.spp_address,
    				       params.spp_assoc_id);
    	if (!trans)
    		return -EINVAL;
    
    	/* The value of the heartbeat interval, in milliseconds. A value of 0,
    	 * when modifying the parameter, specifies that the heartbeat on this
    	 * address should be disabled.
    	 */
    	if (!trans->hb_allowed)
    		params.spp_hbinterval = 0;
    	else
    		params.spp_hbinterval = jiffies_to_msecs(trans->hb_interval);
    
    	/* spp_pathmaxrxt contains the maximum number of retransmissions
    	 * before this address shall be considered unreachable.
    	 */
    	params.spp_pathmaxrxt = trans->max_retrans;
    
    done:
    	if (copy_to_user(optval, &params, len))
    		return -EFAULT;
    
    	if (put_user(len, optlen))
    		return -EFAULT;
    
    	return 0;
    }
    
    /* 7.1.3 Initialization Parameters (SCTP_INITMSG)
     *
     * Applications can specify protocol parameters for the default association
     * initialization.  The option name argument to setsockopt() and getsockopt()
     * is SCTP_INITMSG.
     *
     * Setting initialization parameters is effective only on an unconnected
     * socket (for UDP-style sockets only future associations are effected
     * by the change).  With TCP-style sockets, this option is inherited by
     * sockets derived from a listener socket.
     */
    static int sctp_getsockopt_initmsg(struct sock *sk, int len, char __user *optval, int __user *optlen)
    {
    	if (len != sizeof(struct sctp_initmsg))
    		return -EINVAL;
    	if (copy_to_user(optval, &sctp_sk(sk)->initmsg, len))
    		return -EFAULT;
    	return 0;
    }
    
    
    static int sctp_getsockopt_peer_addrs_num_old(struct sock *sk, int len,
    					      char __user *optval,
    					      int __user *optlen)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    {
    	sctp_assoc_t id;
    	struct sctp_association *asoc;
    	struct list_head *pos;
    	int cnt = 0;
    
    	if (len != sizeof(sctp_assoc_t))
    		return -EINVAL;
    
    	if (copy_from_user(&id, optval, sizeof(sctp_assoc_t)))
    		return -EFAULT;
    
    	/* For UDP-style sockets, id specifies the association to query.  */
    	asoc = sctp_id2assoc(sk, id);
    	if (!asoc)
    		return -EINVAL;
    
    	list_for_each(pos, &asoc->peer.transport_addr_list) {
    		cnt ++;
    	}
    
    	return cnt;
    }
    
    
    /* 
     * Old API for getting list of peer addresses. Does not work for 32-bit
     * programs running on a 64-bit kernel
     */
    static int sctp_getsockopt_peer_addrs_old(struct sock *sk, int len,
    					  char __user *optval,
    					  int __user *optlen)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    {
    	struct sctp_association *asoc;
    	struct list_head *pos;
    	int cnt = 0;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	struct sctp_transport *from;
    	void __user *to;
    	union sctp_addr temp;
    	struct sctp_sock *sp = sctp_sk(sk);
    	int addrlen;
    
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		return -EINVAL;
    
    
    	if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs_old)))
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		return -EFAULT;
    
    	if (getaddrs.addr_num <= 0) return -EINVAL;
    
    	/* For UDP-style sockets, id specifies the association to query.  */
    	asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
    	if (!asoc)
    		return -EINVAL;
    
    	to = (void __user *)getaddrs.addrs;
    	list_for_each(pos, &asoc->peer.transport_addr_list) {
    		from = list_entry(pos, struct sctp_transport, transports);
    		memcpy(&temp, &from->ipaddr, sizeof(temp));
    		sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
    		addrlen = sctp_get_af_specific(sk->sk_family)->sockaddr_len;
    		temp.v4.sin_port = htons(temp.v4.sin_port);
    		if (copy_to_user(to, &temp, addrlen))
    			return -EFAULT;
    		to += addrlen ;
    		cnt ++;
    		if (cnt >= getaddrs.addr_num) break;
    	}
    	getaddrs.addr_num = cnt;
    
    	if (copy_to_user(optval, &getaddrs, sizeof(struct sctp_getaddrs_old)))
    		return -EFAULT;
    
    	return 0;
    }
    
    static int sctp_getsockopt_peer_addrs(struct sock *sk, int len,
    				      char __user *optval, int __user *optlen)
    {
    	struct sctp_association *asoc;
    	struct list_head *pos;
    	int cnt = 0;
    	struct sctp_getaddrs getaddrs;
    	struct sctp_transport *from;
    	void __user *to;
    	union sctp_addr temp;
    	struct sctp_sock *sp = sctp_sk(sk);
    	int addrlen;
    	size_t space_left;
    	int bytes_copied;
    
    	if (len < sizeof(struct sctp_getaddrs))
    		return -EINVAL;
    
    	if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
    		return -EFAULT;
    
    	/* For UDP-style sockets, id specifies the association to query.  */
    	asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
    	if (!asoc)
    		return -EINVAL;
    
    	to = optval + offsetof(struct sctp_getaddrs,addrs);
    	space_left = len - sizeof(struct sctp_getaddrs) - 
    			offsetof(struct sctp_getaddrs,addrs);
    
    	list_for_each(pos, &asoc->peer.transport_addr_list) {
    		from = list_entry(pos, struct sctp_transport, transports);
    		memcpy(&temp, &from->ipaddr, sizeof(temp));
    		sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
    		addrlen = sctp_get_af_specific(sk->sk_family)->sockaddr_len;
    		if(space_left < addrlen)
    			return -ENOMEM;
    		temp.v4.sin_port = htons(temp.v4.sin_port);
    		if (copy_to_user(to, &temp, addrlen))
    			return -EFAULT;
    		to += addrlen;
    		cnt++;
    		space_left -= addrlen;
    	}
    
    	if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num))
    		return -EFAULT;
    	bytes_copied = ((char __user *)to) - optval;
    	if (put_user(bytes_copied, optlen))
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		return -EFAULT;
    
    	return 0;
    }
    
    
    static int sctp_getsockopt_local_addrs_num_old(struct sock *sk, int len,
    					       char __user *optval,
    					       int __user *optlen)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    {
    	sctp_assoc_t id;
    	struct sctp_bind_addr *bp;
    	struct sctp_association *asoc;
    	struct list_head *pos;
    	struct sctp_sockaddr_entry *addr;
    	rwlock_t *addr_lock;
    	unsigned long flags;
    	int cnt = 0;
    
    	if (len != sizeof(sctp_assoc_t))
    		return -EINVAL;
    
    	if (copy_from_user(&id, optval, sizeof(sctp_assoc_t)))
    		return -EFAULT;
    
    	/*
    	 *  For UDP-style sockets, id specifies the association to query.
    	 *  If the id field is set to the value '0' then the locally bound
    	 *  addresses are returned without regard to any particular
    	 *  association.
    	 */
    	if (0 == id) {
    		bp = &sctp_sk(sk)->ep->base.bind_addr;
    		addr_lock = &sctp_sk(sk)->ep->base.addr_lock;
    	} else {
    		asoc = sctp_id2assoc(sk, id);
    		if (!asoc)
    			return -EINVAL;
    		bp = &asoc->base.bind_addr;
    		addr_lock = &asoc->base.addr_lock;
    	}
    
    	sctp_read_lock(addr_lock);
    
    	/* If the endpoint is bound to 0.0.0.0 or ::0, count the valid
    	 * addresses from the global local address list.
    	 */
    	if (sctp_list_single_entry(&bp->address_list)) {
    		addr = list_entry(bp->address_list.next,
    				  struct sctp_sockaddr_entry, list);
    		if (sctp_is_any(&addr->a)) {
    			sctp_spin_lock_irqsave(&sctp_local_addr_lock, flags);
    			list_for_each(pos, &sctp_local_addr_list) {
    				addr = list_entry(pos,
    						  struct sctp_sockaddr_entry,
    						  list);
    				if ((PF_INET == sk->sk_family) && 
    				    (AF_INET6 == addr->a.sa.sa_family))	
    					continue;
    				cnt++;
    			}
    			sctp_spin_unlock_irqrestore(&sctp_local_addr_lock,
    						    flags);
    		} else {
    			cnt = 1;
    		}
    		goto done;
    	}
    
    	list_for_each(pos, &bp->address_list) {
    		cnt ++;
    	}
    
    done:
    	sctp_read_unlock(addr_lock);
    	return cnt;
    }
    
    /* Helper function that copies local addresses to user and returns the number
     * of addresses copied.
     */
    
    static int sctp_copy_laddrs_to_user_old(struct sock *sk, __u16 port, int max_addrs,
    					void __user *to)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    {
    	struct list_head *pos;
    	struct sctp_sockaddr_entry *addr;
    	unsigned long flags;
    	union sctp_addr temp;
    	int cnt = 0;
    	int addrlen;
    
    	sctp_spin_lock_irqsave(&sctp_local_addr_lock, flags);
    	list_for_each(pos, &sctp_local_addr_list) {
    		addr = list_entry(pos, struct sctp_sockaddr_entry, list);
    		if ((PF_INET == sk->sk_family) && 
    		    (AF_INET6 == addr->a.sa.sa_family))
    			continue;
    		memcpy(&temp, &addr->a, sizeof(temp));
    		sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
    								&temp);
    		addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
    		temp.v4.sin_port = htons(port);
    		if (copy_to_user(to, &temp, addrlen)) {
    			sctp_spin_unlock_irqrestore(&sctp_local_addr_lock,
    						    flags);
    			return -EFAULT;
    		}
    		to += addrlen;
    		cnt ++;
    		if (cnt >= max_addrs) break;
    	}
    	sctp_spin_unlock_irqrestore(&sctp_local_addr_lock, flags);
    
    	return cnt;
    }
    
    
    static int sctp_copy_laddrs_to_user(struct sock *sk, __u16 port,
    				    void * __user *to, size_t space_left)
    {
    	struct list_head *pos;
    	struct sctp_sockaddr_entry *addr;
    	unsigned long flags;
    	union sctp_addr temp;
    	int cnt = 0;
    	int addrlen;
    
    	sctp_spin_lock_irqsave(&sctp_local_addr_lock, flags);
    	list_for_each(pos, &sctp_local_addr_list) {
    		addr = list_entry(pos, struct sctp_sockaddr_entry, list);
    		if ((PF_INET == sk->sk_family) && 
    		    (AF_INET6 == addr->a.sa.sa_family))
    			continue;
    		memcpy(&temp, &addr->a, sizeof(temp));
    		sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
    								&temp);
    		addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
    		if(space_left<addrlen)
    			return -ENOMEM;
    		temp.v4.sin_port = htons(port);
    		if (copy_to_user(*to, &temp, addrlen)) {
    			sctp_spin_unlock_irqrestore(&sctp_local_addr_lock,
    						    flags);
    			return -EFAULT;
    		}
    		*to += addrlen;
    		cnt ++;
    		space_left -= addrlen;
    	}
    	sctp_spin_unlock_irqrestore(&sctp_local_addr_lock, flags);
    
    	return cnt;
    }
    
    /* Old API for getting list of local addresses. Does not work for 32-bit
     * programs running on a 64-bit kernel
     */
    static int sctp_getsockopt_local_addrs_old(struct sock *sk, int len,
    					   char __user *optval, int __user *optlen)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    {
    	struct sctp_bind_addr *bp;
    	struct sctp_association *asoc;
    	struct list_head *pos;
    	int cnt = 0;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	struct sctp_sockaddr_entry *addr;
    	void __user *to;
    	union sctp_addr temp;
    	struct sctp_sock *sp = sctp_sk(sk);
    	int addrlen;
    	rwlock_t *addr_lock;
    	int err = 0;
    
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		return -EINVAL;
    
    
    	if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs_old)))
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		return -EFAULT;
    
    	if (getaddrs.addr_num <= 0) return -EINVAL;
    	/*
    	 *  For UDP-style sockets, id specifies the association to query.
    	 *  If the id field is set to the value '0' then the locally bound
    	 *  addresses are returned without regard to any particular
    	 *  association.
    	 */
    	if (0 == getaddrs.assoc_id) {
    		bp = &sctp_sk(sk)->ep->base.bind_addr;
    		addr_lock = &sctp_sk(sk)->ep->base.addr_lock;
    	} else {
    		asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
    		if (!asoc)
    			return -EINVAL;
    		bp = &asoc->base.bind_addr;
    		addr_lock = &asoc->base.addr_lock;
    	}
    
    	to = getaddrs.addrs;
    
    	sctp_read_lock(addr_lock);
    
    	/* If the endpoint is bound to 0.0.0.0 or ::0, get the valid
    	 * addresses from the global local address list.
    	 */
    	if (sctp_list_single_entry(&bp->address_list)) {
    		addr = list_entry(bp->address_list.next,
    				  struct sctp_sockaddr_entry, list);
    		if (sctp_is_any(&addr->a)) {
    
    			cnt = sctp_copy_laddrs_to_user_old(sk, bp->port,
    							   getaddrs.addr_num,
    							   to);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    			if (cnt < 0) {
    				err = cnt;
    				goto unlock;
    			}
    			goto copy_getaddrs;		
    		}
    	}
    
    	list_for_each(pos, &bp->address_list) {
    		addr = list_entry(pos, struct sctp_sockaddr_entry, list);
    		memcpy(&temp, &addr->a, sizeof(temp));
    		sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
    		addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
    		temp.v4.sin_port = htons(temp.v4.sin_port);
    		if (copy_to_user(to, &temp, addrlen)) {
    			err = -EFAULT;
    			goto unlock;
    		}
    		to += addrlen;
    		cnt ++;
    		if (cnt >= getaddrs.addr_num) break;
    	}
    
    copy_getaddrs:
    	getaddrs.addr_num = cnt;
    
    	if (copy_to_user(optval, &getaddrs, sizeof(struct sctp_getaddrs_old)))
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		err = -EFAULT;
    
    unlock:
    	sctp_read_unlock(addr_lock);
    	return err;
    }
    
    
    static int sctp_getsockopt_local_addrs(struct sock *sk, int len,
    				       char __user *optval, int __user *optlen)
    {
    	struct sctp_bind_addr *bp;
    	struct sctp_association *asoc;
    	struct list_head *pos;
    	int cnt = 0;
    	struct sctp_getaddrs getaddrs;
    	struct sctp_sockaddr_entry *addr;
    	void __user *to;
    	union sctp_addr temp;
    	struct sctp_sock *sp = sctp_sk(sk);
    	int addrlen;
    	rwlock_t *addr_lock;
    	int err = 0;
    	size_t space_left;
    	int bytes_copied;
    
    	if (len <= sizeof(struct sctp_getaddrs))
    		return -EINVAL;
    
    	if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
    		return -EFAULT;
    
    	/*
    	 *  For UDP-style sockets, id specifies the association to query.
    	 *  If the id field is set to the value '0' then the locally bound
    	 *  addresses are returned without regard to any particular
    	 *  association.
    	 */
    	if (0 == getaddrs.assoc_id) {
    		bp = &sctp_sk(sk)->ep->base.bind_addr;
    		addr_lock = &sctp_sk(sk)->ep->base.addr_lock;
    	} else {
    		asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
    		if (!asoc)
    			return -EINVAL;
    		bp = &asoc->base.bind_addr;
    		addr_lock = &asoc->base.addr_lock;
    	}
    
    	to = optval + offsetof(struct sctp_getaddrs,addrs);
    	space_left = len - sizeof(struct sctp_getaddrs) -
    			 offsetof(struct sctp_getaddrs,addrs);
    
    	sctp_read_lock(addr_lock);
    
    	/* If the endpoint is bound to 0.0.0.0 or ::0, get the valid
    	 * addresses from the global local address list.
    	 */
    	if (sctp_list_single_entry(&bp->address_list)) {
    		addr = list_entry(bp->address_list.next,
    				  struct sctp_sockaddr_entry, list);
    		if (sctp_is_any(&addr->a)) {
    			cnt = sctp_copy_laddrs_to_user(sk, bp->port,
    						       &to, space_left);
    			if (cnt < 0) {
    				err = cnt;
    				goto unlock;
    			}
    			goto copy_getaddrs;		
    		}
    	}
    
    	list_for_each(pos, &bp->address_list) {
    		addr = list_entry(pos, struct sctp_sockaddr_entry, list);
    		memcpy(&temp, &addr->a, sizeof(temp));
    		sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
    		addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
    		if(space_left < addrlen)
    			return -ENOMEM; /*fixme: right error?*/
    		temp.v4.sin_port = htons(temp.v4.sin_port);
    		if (copy_to_user(to, &temp, addrlen)) {
    			err = -EFAULT;
    			goto unlock;
    		}
    		to += addrlen;
    		cnt ++;
    		space_left -= addrlen;
    	}
    
    copy_getaddrs:
    	if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num))
    		return -EFAULT;
    	bytes_copied = ((char __user *)to) - optval;
    	if (put_user(bytes_copied, optlen))
    		return -EFAULT;
    
    unlock:
    	sctp_read_unlock(addr_lock);
    	return err;
    }
    
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
     *
     * Requests that the local SCTP stack use the enclosed peer address as
     * the association primary.  The enclosed address must be one of the
     * association peer's addresses.
     */
    static int sctp_getsockopt_primary_addr(struct sock *sk, int len,
    					char __user *optval, int __user *optlen)
    {
    	struct sctp_prim prim;
    	struct sctp_association *asoc;
    	struct sctp_sock *sp = sctp_sk(sk);
    
    	if (len != sizeof(struct sctp_prim))
    		return -EINVAL;
    
    	if (copy_from_user(&prim, optval, sizeof(struct sctp_prim)))
    		return -EFAULT;
    
    	asoc = sctp_id2assoc(sk, prim.ssp_assoc_id);
    	if (!asoc)
    		return -EINVAL;
    
    	if (!asoc->peer.primary_path)
    		return -ENOTCONN;
    	
    	asoc->peer.primary_path->ipaddr.v4.sin_port =
    		htons(asoc->peer.primary_path->ipaddr.v4.sin_port);
    	memcpy(&prim.ssp_addr, &asoc->peer.primary_path->ipaddr,
    	       sizeof(union sctp_addr));
    	asoc->peer.primary_path->ipaddr.v4.sin_port =
    		ntohs(asoc->peer.primary_path->ipaddr.v4.sin_port);
    
    	sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp,
    			(union sctp_addr *)&prim.ssp_addr);
    
    	if (copy_to_user(optval, &prim, sizeof(struct sctp_prim)))
    		return -EFAULT;
    
    	return 0;
    }
    
    /*
     * 7.1.11  Set Adaption Layer Indicator (SCTP_ADAPTION_LAYER)
     *
     * Requests that the local endpoint set the specified Adaption Layer
     * Indication parameter for all future INIT and INIT-ACK exchanges.
     */
    static int sctp_getsockopt_adaption_layer(struct sock *sk, int len,
    				  char __user *optval, int __user *optlen)
    {
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    	if (len != sizeof(struct sctp_setadaption))
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		return -EINVAL;
    
    
    	adaption.ssb_adaption_ind = sctp_sk(sk)->adaption_ind;
    	if (copy_to_user(optval, &adaption, len))
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		return -EFAULT;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	return 0;
    }
    
    /*
     *
     * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
     *
     *   Applications that wish to use the sendto() system call may wish to
     *   specify a default set of parameters that would normally be supplied
     *   through the inclusion of ancillary data.  This socket option allows
     *   such an application to set the default sctp_sndrcvinfo structure.
    
    
     *   The application that wishes to use this socket option simply passes
     *   in to this call the sctp_sndrcvinfo structure defined in Section
     *   5.2.2) The input parameters accepted by this call include
     *   sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
     *   sinfo_timetolive.  The user must provide the sinfo_assoc_id field in
     *   to this call if the caller is using the UDP model.
     *
     *   For getsockopt, it get the default sctp_sndrcvinfo structure.
     */
    static int sctp_getsockopt_default_send_param(struct sock *sk,
    					int len, char __user *optval,
    					int __user *optlen)
    {
    	struct sctp_sndrcvinfo info;
    	struct sctp_association *asoc;
    	struct sctp_sock *sp = sctp_sk(sk);
    
    	if (len != sizeof(struct sctp_sndrcvinfo))
    		return -EINVAL;
    	if (copy_from_user(&info, optval, sizeof(struct sctp_sndrcvinfo)))
    		return -EFAULT;
    
    	asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
    	if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
    		return -EINVAL;
    
    	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);