This source file includes following definitions.
- sctp_enter_memory_pressure
- sctp_wspace
- sctp_set_owner_w
- sctp_clear_owner_w
- sctp_for_each_tx_datachunk
- sctp_for_each_rx_skb
- sctp_verify_addr
- sctp_id2assoc
- sctp_addr_id2transport
- sctp_bind
- sctp_sockaddr_af
- sctp_do_bind
- sctp_send_asconf
- sctp_bindx_add
- sctp_send_asconf_add_ip
- sctp_bindx_rem
- sctp_send_asconf_del_ip
- sctp_asconf_mgmt
- sctp_setsockopt_bindx
- sctp_connect_new_asoc
- sctp_connect_add_peer
- __sctp_connect
- __sctp_setsockopt_connectx
- sctp_setsockopt_connectx_old
- sctp_setsockopt_connectx
- sctp_getsockopt_connectx3
- sctp_close
- sctp_error
- sctp_sendmsg_parse
- sctp_sendmsg_new_asoc
- sctp_sendmsg_check_sflags
- sctp_sendmsg_to_asoc
- sctp_sendmsg_get_daddr
- sctp_sendmsg_update_sinfo
- sctp_sendmsg
- sctp_skb_pull
- sctp_recvmsg
- sctp_setsockopt_disable_fragments
- sctp_setsockopt_events
- sctp_setsockopt_autoclose
- sctp_apply_peer_addr_params
- sctp_setsockopt_peer_addr_params
- sctp_spp_sackdelay_enable
- sctp_spp_sackdelay_disable
- sctp_apply_asoc_delayed_ack
- sctp_setsockopt_delayed_ack
- sctp_setsockopt_initmsg
- sctp_setsockopt_default_send_param
- sctp_setsockopt_default_sndinfo
- sctp_setsockopt_primary_addr
- sctp_setsockopt_nodelay
- sctp_setsockopt_rtoinfo
- sctp_setsockopt_associnfo
- sctp_setsockopt_mappedv4
- sctp_setsockopt_maxseg
- sctp_setsockopt_peer_primary_addr
- sctp_setsockopt_adaptation_layer
- sctp_setsockopt_context
- sctp_setsockopt_fragment_interleave
- sctp_setsockopt_partial_delivery_point
- sctp_setsockopt_maxburst
- sctp_setsockopt_auth_chunk
- sctp_setsockopt_hmac_ident
- sctp_setsockopt_auth_key
- sctp_setsockopt_active_key
- sctp_setsockopt_del_key
- sctp_setsockopt_deactivate_key
- sctp_setsockopt_auto_asconf
- sctp_setsockopt_paddr_thresholds
- sctp_setsockopt_recvrcvinfo
- sctp_setsockopt_recvnxtinfo
- sctp_setsockopt_pr_supported
- sctp_setsockopt_default_prinfo
- sctp_setsockopt_reconfig_supported
- sctp_setsockopt_enable_strreset
- sctp_setsockopt_reset_streams
- sctp_setsockopt_reset_assoc
- sctp_setsockopt_add_streams
- sctp_setsockopt_scheduler
- sctp_setsockopt_scheduler_value
- sctp_setsockopt_interleaving_supported
- sctp_setsockopt_reuse_port
- sctp_assoc_ulpevent_type_set
- sctp_setsockopt_event
- sctp_setsockopt_asconf_supported
- sctp_setsockopt_auth_supported
- sctp_setsockopt_ecn_supported
- sctp_setsockopt
- sctp_connect
- sctp_inet_connect
- sctp_disconnect
- sctp_accept
- sctp_ioctl
- sctp_init_sock
- sctp_destroy_sock
- sctp_destruct_sock
- sctp_shutdown
- sctp_get_sctp_info
- sctp_transport_walk_start
- sctp_transport_walk_stop
- sctp_transport_get_next
- sctp_transport_get_idx
- sctp_for_each_endpoint
- sctp_transport_lookup_process
- sctp_for_each_transport
- sctp_getsockopt_sctp_status
- sctp_getsockopt_peer_addr_info
- sctp_getsockopt_disable_fragments
- sctp_getsockopt_events
- sctp_getsockopt_autoclose
- sctp_do_peeloff
- sctp_getsockopt_peeloff_common
- sctp_getsockopt_peeloff
- sctp_getsockopt_peeloff_flags
- sctp_getsockopt_peer_addr_params
- sctp_getsockopt_delayed_ack
- sctp_getsockopt_initmsg
- sctp_getsockopt_peer_addrs
- sctp_copy_laddrs
- sctp_getsockopt_local_addrs
- sctp_getsockopt_primary_addr
- sctp_getsockopt_adaptation_layer
- sctp_getsockopt_default_send_param
- sctp_getsockopt_default_sndinfo
- sctp_getsockopt_nodelay
- sctp_getsockopt_rtoinfo
- sctp_getsockopt_associnfo
- sctp_getsockopt_mappedv4
- sctp_getsockopt_context
- sctp_getsockopt_maxseg
- sctp_getsockopt_fragment_interleave
- sctp_getsockopt_partial_delivery_point
- sctp_getsockopt_maxburst
- sctp_getsockopt_hmac_ident
- sctp_getsockopt_active_key
- sctp_getsockopt_peer_auth_chunks
- sctp_getsockopt_local_auth_chunks
- sctp_getsockopt_assoc_number
- sctp_getsockopt_auto_asconf
- sctp_getsockopt_assoc_ids
- sctp_getsockopt_paddr_thresholds
- sctp_getsockopt_assoc_stats
- sctp_getsockopt_recvrcvinfo
- sctp_getsockopt_recvnxtinfo
- sctp_getsockopt_pr_supported
- sctp_getsockopt_default_prinfo
- sctp_getsockopt_pr_assocstatus
- sctp_getsockopt_pr_streamstatus
- sctp_getsockopt_reconfig_supported
- sctp_getsockopt_enable_strreset
- sctp_getsockopt_scheduler
- sctp_getsockopt_scheduler_value
- sctp_getsockopt_interleaving_supported
- sctp_getsockopt_reuse_port
- sctp_getsockopt_event
- sctp_getsockopt_asconf_supported
- sctp_getsockopt_auth_supported
- sctp_getsockopt_ecn_supported
- sctp_getsockopt
- sctp_hash
- sctp_unhash
- sctp_get_port_local
- sctp_get_port
- sctp_listen_start
- sctp_inet_listen
- sctp_poll
- sctp_bucket_create
- sctp_bucket_destroy
- __sctp_put_port
- sctp_put_port
- sctp_autobind
- sctp_msghdr_parse
- sctp_wait_for_packet
- sctp_skb_recv_datagram
- __sctp_write_space
- sctp_wake_up_waiters
- sctp_wfree
- sctp_sock_rfree
- sctp_wait_for_sndbuf
- sctp_data_ready
- sctp_write_space
- sctp_writeable
- sctp_wait_for_connect
- sctp_wait_for_accept
- sctp_wait_for_close
- sctp_skb_set_owner_r_frag
- sctp_copy_sock
- sctp_copy_descendant
- sctp_sock_migrate
- sctp_v6_destroy_sock
   1 
   2 
   3 
   4 
   5 
   6 
   7 
   8 
   9 
  10 
  11 
  12 
  13 
  14 
  15 
  16 
  17 
  18 
  19 
  20 
  21 
  22 
  23 
  24 
  25 
  26 
  27 
  28 
  29 
  30 
  31 
  32 
  33 
  34 
  35 
  36 
  37 
  38 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  39 
  40 #include <crypto/hash.h>
  41 #include <linux/types.h>
  42 #include <linux/kernel.h>
  43 #include <linux/wait.h>
  44 #include <linux/time.h>
  45 #include <linux/sched/signal.h>
  46 #include <linux/ip.h>
  47 #include <linux/capability.h>
  48 #include <linux/fcntl.h>
  49 #include <linux/poll.h>
  50 #include <linux/init.h>
  51 #include <linux/slab.h>
  52 #include <linux/file.h>
  53 #include <linux/compat.h>
  54 #include <linux/rhashtable.h>
  55 
  56 #include <net/ip.h>
  57 #include <net/icmp.h>
  58 #include <net/route.h>
  59 #include <net/ipv6.h>
  60 #include <net/inet_common.h>
  61 #include <net/busy_poll.h>
  62 
  63 #include <linux/socket.h> 
  64 #include <linux/export.h>
  65 #include <net/sock.h>
  66 #include <net/sctp/sctp.h>
  67 #include <net/sctp/sm.h>
  68 #include <net/sctp/stream_sched.h>
  69 
  70 
  71 static bool sctp_writeable(struct sock *sk);
  72 static void sctp_wfree(struct sk_buff *skb);
  73 static int sctp_wait_for_sndbuf(struct sctp_association *asoc, long *timeo_p,
  74                                 size_t msg_len);
  75 static int sctp_wait_for_packet(struct sock *sk, int *err, long *timeo_p);
  76 static int sctp_wait_for_connect(struct sctp_association *, long *timeo_p);
  77 static int sctp_wait_for_accept(struct sock *sk, long timeo);
  78 static void sctp_wait_for_close(struct sock *sk, long timeo);
  79 static void sctp_destruct_sock(struct sock *sk);
  80 static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt,
  81                                         union sctp_addr *addr, int len);
  82 static int sctp_bindx_add(struct sock *, struct sockaddr *, int);
  83 static int sctp_bindx_rem(struct sock *, struct sockaddr *, int);
  84 static int sctp_send_asconf_add_ip(struct sock *, struct sockaddr *, int);
  85 static int sctp_send_asconf_del_ip(struct sock *, struct sockaddr *, int);
  86 static int sctp_send_asconf(struct sctp_association *asoc,
  87                             struct sctp_chunk *chunk);
  88 static int sctp_do_bind(struct sock *, union sctp_addr *, int);
  89 static int sctp_autobind(struct sock *sk);
  90 static int sctp_sock_migrate(struct sock *oldsk, struct sock *newsk,
  91                              struct sctp_association *assoc,
  92                              enum sctp_socket_type type);
  93 
  94 static unsigned long sctp_memory_pressure;
  95 static atomic_long_t sctp_memory_allocated;
  96 struct percpu_counter sctp_sockets_allocated;
  97 
  98 static void sctp_enter_memory_pressure(struct sock *sk)
  99 {
 100         sctp_memory_pressure = 1;
 101 }
 102 
 103 
 104 
 105 static inline int sctp_wspace(struct sctp_association *asoc)
 106 {
 107         struct sock *sk = asoc->base.sk;
 108 
 109         return asoc->ep->sndbuf_policy ? sk->sk_sndbuf - asoc->sndbuf_used
 110                                        : sk_stream_wspace(sk);
 111 }
 112 
 113 
 114 
 115 
 116 
 117 
 118 
 119 
 120 
 121 
 122 static inline void sctp_set_owner_w(struct sctp_chunk *chunk)
 123 {
 124         struct sctp_association *asoc = chunk->asoc;
 125         struct sock *sk = asoc->base.sk;
 126 
 127         
 128         sctp_association_hold(asoc);
 129 
 130         if (chunk->shkey)
 131                 sctp_auth_shkey_hold(chunk->shkey);
 132 
 133         skb_set_owner_w(chunk->skb, sk);
 134 
 135         chunk->skb->destructor = sctp_wfree;
 136         
 137         skb_shinfo(chunk->skb)->destructor_arg = chunk;
 138 
 139         refcount_add(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc);
 140         asoc->sndbuf_used += chunk->skb->truesize + sizeof(struct sctp_chunk);
 141         sk->sk_wmem_queued += chunk->skb->truesize + sizeof(struct sctp_chunk);
 142         sk_mem_charge(sk, chunk->skb->truesize);
 143 }
 144 
 145 static void sctp_clear_owner_w(struct sctp_chunk *chunk)
 146 {
 147         skb_orphan(chunk->skb);
 148 }
 149 
 150 #define traverse_and_process()  \
 151 do {                            \
 152         msg = chunk->msg;       \
 153         if (msg == prev_msg)    \
 154                 continue;       \
 155         list_for_each_entry(c, &msg->chunks, frag_list) {       \
 156                 if ((clear && asoc->base.sk == c->skb->sk) ||   \
 157                     (!clear && asoc->base.sk != c->skb->sk))    \
 158                         cb(c);  \
 159         }                       \
 160         prev_msg = msg;         \
 161 } while (0)
 162 
 163 static void sctp_for_each_tx_datachunk(struct sctp_association *asoc,
 164                                        bool clear,
 165                                        void (*cb)(struct sctp_chunk *))
 166 
 167 {
 168         struct sctp_datamsg *msg, *prev_msg = NULL;
 169         struct sctp_outq *q = &asoc->outqueue;
 170         struct sctp_chunk *chunk, *c;
 171         struct sctp_transport *t;
 172 
 173         list_for_each_entry(t, &asoc->peer.transport_addr_list, transports)
 174                 list_for_each_entry(chunk, &t->transmitted, transmitted_list)
 175                         traverse_and_process();
 176 
 177         list_for_each_entry(chunk, &q->retransmit, transmitted_list)
 178                 traverse_and_process();
 179 
 180         list_for_each_entry(chunk, &q->sacked, transmitted_list)
 181                 traverse_and_process();
 182 
 183         list_for_each_entry(chunk, &q->abandoned, transmitted_list)
 184                 traverse_and_process();
 185 
 186         list_for_each_entry(chunk, &q->out_chunk_list, list)
 187                 traverse_and_process();
 188 }
 189 
 190 static void sctp_for_each_rx_skb(struct sctp_association *asoc, struct sock *sk,
 191                                  void (*cb)(struct sk_buff *, struct sock *))
 192 
 193 {
 194         struct sk_buff *skb, *tmp;
 195 
 196         sctp_skb_for_each(skb, &asoc->ulpq.lobby, tmp)
 197                 cb(skb, sk);
 198 
 199         sctp_skb_for_each(skb, &asoc->ulpq.reasm, tmp)
 200                 cb(skb, sk);
 201 
 202         sctp_skb_for_each(skb, &asoc->ulpq.reasm_uo, tmp)
 203                 cb(skb, sk);
 204 }
 205 
 206 
 207 static inline int sctp_verify_addr(struct sock *sk, union sctp_addr *addr,
 208                                    int len)
 209 {
 210         struct sctp_af *af;
 211 
 212         
 213         af = sctp_sockaddr_af(sctp_sk(sk), addr, len);
 214         if (!af)
 215                 return -EINVAL;
 216 
 217         
 218         if (!af->addr_valid(addr, sctp_sk(sk), NULL))
 219                 return -EINVAL;
 220 
 221         if (!sctp_sk(sk)->pf->send_verify(sctp_sk(sk), (addr)))
 222                 return -EINVAL;
 223 
 224         return 0;
 225 }
 226 
 227 
 228 
 229 
 230 struct sctp_association *sctp_id2assoc(struct sock *sk, sctp_assoc_t id)
 231 {
 232         struct sctp_association *asoc = NULL;
 233 
 234         
 235         if (!sctp_style(sk, UDP)) {
 236                 
 237 
 238 
 239 
 240                 if (!sctp_sstate(sk, ESTABLISHED) && !sctp_sstate(sk, CLOSING))
 241                         return NULL;
 242 
 243                 
 244                 if (!list_empty(&sctp_sk(sk)->ep->asocs))
 245                         asoc = list_entry(sctp_sk(sk)->ep->asocs.next,
 246                                           struct sctp_association, asocs);
 247                 return asoc;
 248         }
 249 
 250         
 251         if (id <= SCTP_ALL_ASSOC)
 252                 return NULL;
 253 
 254         spin_lock_bh(&sctp_assocs_id_lock);
 255         asoc = (struct sctp_association *)idr_find(&sctp_assocs_id, (int)id);
 256         if (asoc && (asoc->base.sk != sk || asoc->base.dead))
 257                 asoc = NULL;
 258         spin_unlock_bh(&sctp_assocs_id_lock);
 259 
 260         return asoc;
 261 }
 262 
 263 
 264 
 265 
 266 
 267 static struct sctp_transport *sctp_addr_id2transport(struct sock *sk,
 268                                               struct sockaddr_storage *addr,
 269                                               sctp_assoc_t id)
 270 {
 271         struct sctp_association *addr_asoc = NULL, *id_asoc = NULL;
 272         struct sctp_af *af = sctp_get_af_specific(addr->ss_family);
 273         union sctp_addr *laddr = (union sctp_addr *)addr;
 274         struct sctp_transport *transport;
 275 
 276         if (!af || sctp_verify_addr(sk, laddr, af->sockaddr_len))
 277                 return NULL;
 278 
 279         addr_asoc = sctp_endpoint_lookup_assoc(sctp_sk(sk)->ep,
 280                                                laddr,
 281                                                &transport);
 282 
 283         if (!addr_asoc)
 284                 return NULL;
 285 
 286         id_asoc = sctp_id2assoc(sk, id);
 287         if (id_asoc && (id_asoc != addr_asoc))
 288                 return NULL;
 289 
 290         sctp_get_pf_specific(sk->sk_family)->addr_to_user(sctp_sk(sk),
 291                                                 (union sctp_addr *)addr);
 292 
 293         return transport;
 294 }
 295 
 296 
 297 
 298 
 299 
 300 
 301 
 302 
 303 
 304 
 305 
 306 static int sctp_bind(struct sock *sk, struct sockaddr *addr, int addr_len)
 307 {
 308         int retval = 0;
 309 
 310         lock_sock(sk);
 311 
 312         pr_debug("%s: sk:%p, addr:%p, addr_len:%d\n", __func__, sk,
 313                  addr, addr_len);
 314 
 315         
 316         if (!sctp_sk(sk)->ep->base.bind_addr.port)
 317                 retval = sctp_do_bind(sk, (union sctp_addr *)addr,
 318                                       addr_len);
 319         else
 320                 retval = -EINVAL;
 321 
 322         release_sock(sk);
 323 
 324         return retval;
 325 }
 326 
 327 static int sctp_get_port_local(struct sock *, union sctp_addr *);
 328 
 329 
 330 static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt,
 331                                         union sctp_addr *addr, int len)
 332 {
 333         struct sctp_af *af;
 334 
 335         
 336         if (len < sizeof (struct sockaddr))
 337                 return NULL;
 338 
 339         if (!opt->pf->af_supported(addr->sa.sa_family, opt))
 340                 return NULL;
 341 
 342         if (addr->sa.sa_family == AF_INET6) {
 343                 if (len < SIN6_LEN_RFC2133)
 344                         return NULL;
 345                 
 346                 if (ipv6_addr_v4mapped(&addr->v6.sin6_addr) &&
 347                     !opt->pf->af_supported(AF_INET, opt))
 348                         return NULL;
 349         }
 350 
 351         
 352         af = sctp_get_af_specific(addr->sa.sa_family);
 353 
 354         if (len < af->sockaddr_len)
 355                 return NULL;
 356 
 357         return af;
 358 }
 359 
 360 
 361 static int sctp_do_bind(struct sock *sk, union sctp_addr *addr, int len)
 362 {
 363         struct net *net = sock_net(sk);
 364         struct sctp_sock *sp = sctp_sk(sk);
 365         struct sctp_endpoint *ep = sp->ep;
 366         struct sctp_bind_addr *bp = &ep->base.bind_addr;
 367         struct sctp_af *af;
 368         unsigned short snum;
 369         int ret = 0;
 370 
 371         
 372         af = sctp_sockaddr_af(sp, addr, len);
 373         if (!af) {
 374                 pr_debug("%s: sk:%p, newaddr:%p, len:%d EINVAL\n",
 375                          __func__, sk, addr, len);
 376                 return -EINVAL;
 377         }
 378 
 379         snum = ntohs(addr->v4.sin_port);
 380 
 381         pr_debug("%s: sk:%p, new addr:%pISc, port:%d, new port:%d, len:%d\n",
 382                  __func__, sk, &addr->sa, bp->port, snum, len);
 383 
 384         
 385         if (!sp->pf->bind_verify(sp, addr))
 386                 return -EADDRNOTAVAIL;
 387 
 388         
 389 
 390 
 391 
 392         if (bp->port) {
 393                 if (!snum)
 394                         snum = bp->port;
 395                 else if (snum != bp->port) {
 396                         pr_debug("%s: new port %d doesn't match existing port "
 397                                  "%d\n", __func__, snum, bp->port);
 398                         return -EINVAL;
 399                 }
 400         }
 401 
 402         if (snum && snum < inet_prot_sock(net) &&
 403             !ns_capable(net->user_ns, CAP_NET_BIND_SERVICE))
 404                 return -EACCES;
 405 
 406         
 407 
 408 
 409         if (sctp_bind_addr_match(bp, addr, sp))
 410                 return -EINVAL;
 411 
 412         
 413 
 414 
 415 
 416         addr->v4.sin_port = htons(snum);
 417         if (sctp_get_port_local(sk, addr))
 418                 return -EADDRINUSE;
 419 
 420         
 421         if (!bp->port)
 422                 bp->port = inet_sk(sk)->inet_num;
 423 
 424         
 425 
 426 
 427         ret = sctp_add_bind_addr(bp, addr, af->sockaddr_len,
 428                                  SCTP_ADDR_SRC, GFP_ATOMIC);
 429 
 430         if (ret) {
 431                 sctp_put_port(sk);
 432                 return ret;
 433         }
 434         
 435         inet_sk(sk)->inet_sport = htons(inet_sk(sk)->inet_num);
 436         sp->pf->to_sk_saddr(addr, sk);
 437 
 438         return ret;
 439 }
 440 
 441  
 442 
 443 
 444 
 445 
 446 
 447 
 448 
 449 
 450 
 451 static int sctp_send_asconf(struct sctp_association *asoc,
 452                             struct sctp_chunk *chunk)
 453 {
 454         struct net      *net = sock_net(asoc->base.sk);
 455         int             retval = 0;
 456 
 457         
 458 
 459 
 460         if (asoc->addip_last_asconf) {
 461                 list_add_tail(&chunk->list, &asoc->addip_chunk_list);
 462                 goto out;
 463         }
 464 
 465         
 466         sctp_chunk_hold(chunk);
 467         retval = sctp_primitive_ASCONF(net, asoc, chunk);
 468         if (retval)
 469                 sctp_chunk_free(chunk);
 470         else
 471                 asoc->addip_last_asconf = chunk;
 472 
 473 out:
 474         return retval;
 475 }
 476 
 477 
 478 
 479 
 480 
 481 
 482 
 483 
 484 
 485 
 486 
 487 
 488 
 489 static int sctp_bindx_add(struct sock *sk, struct sockaddr *addrs, int addrcnt)
 490 {
 491         int cnt;
 492         int retval = 0;
 493         void *addr_buf;
 494         struct sockaddr *sa_addr;
 495         struct sctp_af *af;
 496 
 497         pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n", __func__, sk,
 498                  addrs, addrcnt);
 499 
 500         addr_buf = addrs;
 501         for (cnt = 0; cnt < addrcnt; cnt++) {
 502                 
 503 
 504 
 505                 sa_addr = addr_buf;
 506                 af = sctp_get_af_specific(sa_addr->sa_family);
 507                 if (!af) {
 508                         retval = -EINVAL;
 509                         goto err_bindx_add;
 510                 }
 511 
 512                 retval = sctp_do_bind(sk, (union sctp_addr *)sa_addr,
 513                                       af->sockaddr_len);
 514 
 515                 addr_buf += af->sockaddr_len;
 516 
 517 err_bindx_add:
 518                 if (retval < 0) {
 519                         
 520                         if (cnt > 0)
 521                                 sctp_bindx_rem(sk, addrs, cnt);
 522                         return retval;
 523                 }
 524         }
 525 
 526         return retval;
 527 }
 528 
 529 
 530 
 531 
 532 
 533 
 534 
 535 
 536 
 537 
 538 
 539 static int sctp_send_asconf_add_ip(struct sock          *sk,
 540                                    struct sockaddr      *addrs,
 541                                    int                  addrcnt)
 542 {
 543         struct sctp_sock                *sp;
 544         struct sctp_endpoint            *ep;
 545         struct sctp_association         *asoc;
 546         struct sctp_bind_addr           *bp;
 547         struct sctp_chunk               *chunk;
 548         struct sctp_sockaddr_entry      *laddr;
 549         union sctp_addr                 *addr;
 550         union sctp_addr                 saveaddr;
 551         void                            *addr_buf;
 552         struct sctp_af                  *af;
 553         struct list_head                *p;
 554         int                             i;
 555         int                             retval = 0;
 556 
 557         sp = sctp_sk(sk);
 558         ep = sp->ep;
 559 
 560         if (!ep->asconf_enable)
 561                 return retval;
 562 
 563         pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n",
 564                  __func__, sk, addrs, addrcnt);
 565 
 566         list_for_each_entry(asoc, &ep->asocs, asocs) {
 567                 if (!asoc->peer.asconf_capable)
 568                         continue;
 569 
 570                 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_ADD_IP)
 571                         continue;
 572 
 573                 if (!sctp_state(asoc, ESTABLISHED))
 574                         continue;
 575 
 576                 
 577 
 578 
 579 
 580 
 581                 addr_buf = addrs;
 582                 for (i = 0; i < addrcnt; i++) {
 583                         addr = addr_buf;
 584                         af = sctp_get_af_specific(addr->v4.sin_family);
 585                         if (!af) {
 586                                 retval = -EINVAL;
 587                                 goto out;
 588                         }
 589 
 590                         if (sctp_assoc_lookup_laddr(asoc, addr))
 591                                 break;
 592 
 593                         addr_buf += af->sockaddr_len;
 594                 }
 595                 if (i < addrcnt)
 596                         continue;
 597 
 598                 
 599 
 600 
 601                 bp = &asoc->base.bind_addr;
 602                 p = bp->address_list.next;
 603                 laddr = list_entry(p, struct sctp_sockaddr_entry, list);
 604                 chunk = sctp_make_asconf_update_ip(asoc, &laddr->a, addrs,
 605                                                    addrcnt, SCTP_PARAM_ADD_IP);
 606                 if (!chunk) {
 607                         retval = -ENOMEM;
 608                         goto out;
 609                 }
 610 
 611                 
 612 
 613 
 614                 addr_buf = addrs;
 615                 for (i = 0; i < addrcnt; i++) {
 616                         addr = addr_buf;
 617                         af = sctp_get_af_specific(addr->v4.sin_family);
 618                         memcpy(&saveaddr, addr, af->sockaddr_len);
 619                         retval = sctp_add_bind_addr(bp, &saveaddr,
 620                                                     sizeof(saveaddr),
 621                                                     SCTP_ADDR_NEW, GFP_ATOMIC);
 622                         addr_buf += af->sockaddr_len;
 623                 }
 624                 if (asoc->src_out_of_asoc_ok) {
 625                         struct sctp_transport *trans;
 626 
 627                         list_for_each_entry(trans,
 628                             &asoc->peer.transport_addr_list, transports) {
 629                                 trans->cwnd = min(4*asoc->pathmtu, max_t(__u32,
 630                                     2*asoc->pathmtu, 4380));
 631                                 trans->ssthresh = asoc->peer.i.a_rwnd;
 632                                 trans->rto = asoc->rto_initial;
 633                                 sctp_max_rto(asoc, trans);
 634                                 trans->rtt = trans->srtt = trans->rttvar = 0;
 635                                 
 636                                 sctp_transport_route(trans, NULL,
 637                                                      sctp_sk(asoc->base.sk));
 638                         }
 639                 }
 640                 retval = sctp_send_asconf(asoc, chunk);
 641         }
 642 
 643 out:
 644         return retval;
 645 }
 646 
 647 
 648 
 649 
 650 
 651 
 652 
 653 
 654 
 655 
 656 
 657 
 658 
 659 
 660 
 661 
 662 static int sctp_bindx_rem(struct sock *sk, struct sockaddr *addrs, int addrcnt)
 663 {
 664         struct sctp_sock *sp = sctp_sk(sk);
 665         struct sctp_endpoint *ep = sp->ep;
 666         int cnt;
 667         struct sctp_bind_addr *bp = &ep->base.bind_addr;
 668         int retval = 0;
 669         void *addr_buf;
 670         union sctp_addr *sa_addr;
 671         struct sctp_af *af;
 672 
 673         pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n",
 674                  __func__, sk, addrs, addrcnt);
 675 
 676         addr_buf = addrs;
 677         for (cnt = 0; cnt < addrcnt; cnt++) {
 678                 
 679 
 680 
 681 
 682                 if (list_empty(&bp->address_list) ||
 683                     (sctp_list_single_entry(&bp->address_list))) {
 684                         retval = -EBUSY;
 685                         goto err_bindx_rem;
 686                 }
 687 
 688                 sa_addr = addr_buf;
 689                 af = sctp_get_af_specific(sa_addr->sa.sa_family);
 690                 if (!af) {
 691                         retval = -EINVAL;
 692                         goto err_bindx_rem;
 693                 }
 694 
 695                 if (!af->addr_valid(sa_addr, sp, NULL)) {
 696                         retval = -EADDRNOTAVAIL;
 697                         goto err_bindx_rem;
 698                 }
 699 
 700                 if (sa_addr->v4.sin_port &&
 701                     sa_addr->v4.sin_port != htons(bp->port)) {
 702                         retval = -EINVAL;
 703                         goto err_bindx_rem;
 704                 }
 705 
 706                 if (!sa_addr->v4.sin_port)
 707                         sa_addr->v4.sin_port = htons(bp->port);
 708 
 709                 
 710 
 711 
 712 
 713 
 714 
 715 
 716                 retval = sctp_del_bind_addr(bp, sa_addr);
 717 
 718                 addr_buf += af->sockaddr_len;
 719 err_bindx_rem:
 720                 if (retval < 0) {
 721                         
 722                         if (cnt > 0)
 723                                 sctp_bindx_add(sk, addrs, cnt);
 724                         return retval;
 725                 }
 726         }
 727 
 728         return retval;
 729 }
 730 
 731 
 732 
 733 
 734 
 735 
 736 
 737 
 738 
 739 
 740 
 741 static int sctp_send_asconf_del_ip(struct sock          *sk,
 742                                    struct sockaddr      *addrs,
 743                                    int                  addrcnt)
 744 {
 745         struct sctp_sock        *sp;
 746         struct sctp_endpoint    *ep;
 747         struct sctp_association *asoc;
 748         struct sctp_transport   *transport;
 749         struct sctp_bind_addr   *bp;
 750         struct sctp_chunk       *chunk;
 751         union sctp_addr         *laddr;
 752         void                    *addr_buf;
 753         struct sctp_af          *af;
 754         struct sctp_sockaddr_entry *saddr;
 755         int                     i;
 756         int                     retval = 0;
 757         int                     stored = 0;
 758 
 759         chunk = NULL;
 760         sp = sctp_sk(sk);
 761         ep = sp->ep;
 762 
 763         if (!ep->asconf_enable)
 764                 return retval;
 765 
 766         pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n",
 767                  __func__, sk, addrs, addrcnt);
 768 
 769         list_for_each_entry(asoc, &ep->asocs, asocs) {
 770 
 771                 if (!asoc->peer.asconf_capable)
 772                         continue;
 773 
 774                 if (asoc->peer.addip_disabled_mask & SCTP_PARAM_DEL_IP)
 775                         continue;
 776 
 777                 if (!sctp_state(asoc, ESTABLISHED))
 778                         continue;
 779 
 780                 
 781 
 782 
 783 
 784 
 785                 addr_buf = addrs;
 786                 for (i = 0; i < addrcnt; i++) {
 787                         laddr = addr_buf;
 788                         af = sctp_get_af_specific(laddr->v4.sin_family);
 789                         if (!af) {
 790                                 retval = -EINVAL;
 791                                 goto out;
 792                         }
 793 
 794                         if (!sctp_assoc_lookup_laddr(asoc, laddr))
 795                                 break;
 796 
 797                         addr_buf += af->sockaddr_len;
 798                 }
 799                 if (i < addrcnt)
 800                         continue;
 801 
 802                 
 803 
 804 
 805 
 806 
 807                 bp = &asoc->base.bind_addr;
 808                 laddr = sctp_find_unmatch_addr(bp, (union sctp_addr *)addrs,
 809                                                addrcnt, sp);
 810                 if ((laddr == NULL) && (addrcnt == 1)) {
 811                         if (asoc->asconf_addr_del_pending)
 812                                 continue;
 813                         asoc->asconf_addr_del_pending =
 814                             kzalloc(sizeof(union sctp_addr), GFP_ATOMIC);
 815                         if (asoc->asconf_addr_del_pending == NULL) {
 816                                 retval = -ENOMEM;
 817                                 goto out;
 818                         }
 819                         asoc->asconf_addr_del_pending->sa.sa_family =
 820                                     addrs->sa_family;
 821                         asoc->asconf_addr_del_pending->v4.sin_port =
 822                                     htons(bp->port);
 823                         if (addrs->sa_family == AF_INET) {
 824                                 struct sockaddr_in *sin;
 825 
 826                                 sin = (struct sockaddr_in *)addrs;
 827                                 asoc->asconf_addr_del_pending->v4.sin_addr.s_addr = sin->sin_addr.s_addr;
 828                         } else if (addrs->sa_family == AF_INET6) {
 829                                 struct sockaddr_in6 *sin6;
 830 
 831                                 sin6 = (struct sockaddr_in6 *)addrs;
 832                                 asoc->asconf_addr_del_pending->v6.sin6_addr = sin6->sin6_addr;
 833                         }
 834 
 835                         pr_debug("%s: keep the last address asoc:%p %pISc at %p\n",
 836                                  __func__, asoc, &asoc->asconf_addr_del_pending->sa,
 837                                  asoc->asconf_addr_del_pending);
 838 
 839                         asoc->src_out_of_asoc_ok = 1;
 840                         stored = 1;
 841                         goto skip_mkasconf;
 842                 }
 843 
 844                 if (laddr == NULL)
 845                         return -EINVAL;
 846 
 847                 
 848 
 849 
 850 
 851                 chunk = sctp_make_asconf_update_ip(asoc, laddr, addrs, addrcnt,
 852                                                    SCTP_PARAM_DEL_IP);
 853                 if (!chunk) {
 854                         retval = -ENOMEM;
 855                         goto out;
 856                 }
 857 
 858 skip_mkasconf:
 859                 
 860 
 861 
 862                 addr_buf = addrs;
 863                 for (i = 0; i < addrcnt; i++) {
 864                         laddr = addr_buf;
 865                         af = sctp_get_af_specific(laddr->v4.sin_family);
 866                         list_for_each_entry(saddr, &bp->address_list, list) {
 867                                 if (sctp_cmp_addr_exact(&saddr->a, laddr))
 868                                         saddr->state = SCTP_ADDR_DEL;
 869                         }
 870                         addr_buf += af->sockaddr_len;
 871                 }
 872 
 873                 
 874 
 875 
 876 
 877                 list_for_each_entry(transport, &asoc->peer.transport_addr_list,
 878                                         transports) {
 879                         sctp_transport_route(transport, NULL,
 880                                              sctp_sk(asoc->base.sk));
 881                 }
 882 
 883                 if (stored)
 884                         
 885                         continue;
 886                 retval = sctp_send_asconf(asoc, chunk);
 887         }
 888 out:
 889         return retval;
 890 }
 891 
 892 
 893 int sctp_asconf_mgmt(struct sctp_sock *sp, struct sctp_sockaddr_entry *addrw)
 894 {
 895         struct sock *sk = sctp_opt2sk(sp);
 896         union sctp_addr *addr;
 897         struct sctp_af *af;
 898 
 899         
 900         addr = &addrw->a;
 901         addr->v4.sin_port = htons(sp->ep->base.bind_addr.port);
 902         af = sctp_get_af_specific(addr->sa.sa_family);
 903         if (!af)
 904                 return -EINVAL;
 905         if (sctp_verify_addr(sk, addr, af->sockaddr_len))
 906                 return -EINVAL;
 907 
 908         if (addrw->state == SCTP_ADDR_NEW)
 909                 return sctp_send_asconf_add_ip(sk, (struct sockaddr *)addr, 1);
 910         else
 911                 return sctp_send_asconf_del_ip(sk, (struct sockaddr *)addr, 1);
 912 }
 913 
 914 
 915 
 916 
 917 
 918 
 919 
 920 
 921 
 922 
 923 
 924 
 925 
 926 
 927 
 928 
 929 
 930 
 931 
 932 
 933 
 934 
 935 
 936 
 937 
 938 
 939 
 940 
 941 
 942 
 943 
 944 
 945 
 946 
 947 
 948 
 949 
 950 
 951 
 952 
 953 
 954 
 955 
 956 
 957 
 958 
 959 
 960 
 961 
 962 
 963 
 964 
 965 
 966 
 967 
 968 
 969 
 970 
 971 
 972 
 973 
 974 
 975 
 976 
 977 
 978 
 979 
 980 
 981 
 982 
 983 static int sctp_setsockopt_bindx(struct sock *sk,
 984                                  struct sockaddr __user *addrs,
 985                                  int addrs_size, int op)
 986 {
 987         struct sockaddr *kaddrs;
 988         int err;
 989         int addrcnt = 0;
 990         int walk_size = 0;
 991         struct sockaddr *sa_addr;
 992         void *addr_buf;
 993         struct sctp_af *af;
 994 
 995         pr_debug("%s: sk:%p addrs:%p addrs_size:%d opt:%d\n",
 996                  __func__, sk, addrs, addrs_size, op);
 997 
 998         if (unlikely(addrs_size <= 0))
 999                 return -EINVAL;
1000 
1001         kaddrs = memdup_user(addrs, addrs_size);
1002         if (IS_ERR(kaddrs))
1003                 return PTR_ERR(kaddrs);
1004 
1005         
1006         addr_buf = kaddrs;
1007         while (walk_size < addrs_size) {
1008                 if (walk_size + sizeof(sa_family_t) > addrs_size) {
1009                         kfree(kaddrs);
1010                         return -EINVAL;
1011                 }
1012 
1013                 sa_addr = addr_buf;
1014                 af = sctp_get_af_specific(sa_addr->sa_family);
1015 
1016                 
1017 
1018 
1019                 if (!af || (walk_size + af->sockaddr_len) > addrs_size) {
1020                         kfree(kaddrs);
1021                         return -EINVAL;
1022                 }
1023                 addrcnt++;
1024                 addr_buf += af->sockaddr_len;
1025                 walk_size += af->sockaddr_len;
1026         }
1027 
1028         
1029         switch (op) {
1030         case SCTP_BINDX_ADD_ADDR:
1031                 
1032                 err = security_sctp_bind_connect(sk, SCTP_SOCKOPT_BINDX_ADD,
1033                                                  (struct sockaddr *)kaddrs,
1034                                                  addrs_size);
1035                 if (err)
1036                         goto out;
1037                 err = sctp_bindx_add(sk, kaddrs, addrcnt);
1038                 if (err)
1039                         goto out;
1040                 err = sctp_send_asconf_add_ip(sk, kaddrs, addrcnt);
1041                 break;
1042 
1043         case SCTP_BINDX_REM_ADDR:
1044                 err = sctp_bindx_rem(sk, kaddrs, addrcnt);
1045                 if (err)
1046                         goto out;
1047                 err = sctp_send_asconf_del_ip(sk, kaddrs, addrcnt);
1048                 break;
1049 
1050         default:
1051                 err = -EINVAL;
1052                 break;
1053         }
1054 
1055 out:
1056         kfree(kaddrs);
1057 
1058         return err;
1059 }
1060 
1061 static int sctp_connect_new_asoc(struct sctp_endpoint *ep,
1062                                  const union sctp_addr *daddr,
1063                                  const struct sctp_initmsg *init,
1064                                  struct sctp_transport **tp)
1065 {
1066         struct sctp_association *asoc;
1067         struct sock *sk = ep->base.sk;
1068         struct net *net = sock_net(sk);
1069         enum sctp_scope scope;
1070         int err;
1071 
1072         if (sctp_endpoint_is_peeled_off(ep, daddr))
1073                 return -EADDRNOTAVAIL;
1074 
1075         if (!ep->base.bind_addr.port) {
1076                 if (sctp_autobind(sk))
1077                         return -EAGAIN;
1078         } else {
1079                 if (ep->base.bind_addr.port < inet_prot_sock(net) &&
1080                     !ns_capable(net->user_ns, CAP_NET_BIND_SERVICE))
1081                         return -EACCES;
1082         }
1083 
1084         scope = sctp_scope(daddr);
1085         asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
1086         if (!asoc)
1087                 return -ENOMEM;
1088 
1089         err = sctp_assoc_set_bind_addr_from_ep(asoc, scope, GFP_KERNEL);
1090         if (err < 0)
1091                 goto free;
1092 
1093         *tp = sctp_assoc_add_peer(asoc, daddr, GFP_KERNEL, SCTP_UNKNOWN);
1094         if (!*tp) {
1095                 err = -ENOMEM;
1096                 goto free;
1097         }
1098 
1099         if (!init)
1100                 return 0;
1101 
1102         if (init->sinit_num_ostreams) {
1103                 __u16 outcnt = init->sinit_num_ostreams;
1104 
1105                 asoc->c.sinit_num_ostreams = outcnt;
1106                 
1107                 err = sctp_stream_init(&asoc->stream, outcnt, 0, GFP_KERNEL);
1108                 if (err)
1109                         goto free;
1110         }
1111 
1112         if (init->sinit_max_instreams)
1113                 asoc->c.sinit_max_instreams = init->sinit_max_instreams;
1114 
1115         if (init->sinit_max_attempts)
1116                 asoc->max_init_attempts = init->sinit_max_attempts;
1117 
1118         if (init->sinit_max_init_timeo)
1119                 asoc->max_init_timeo =
1120                         msecs_to_jiffies(init->sinit_max_init_timeo);
1121 
1122         return 0;
1123 free:
1124         sctp_association_free(asoc);
1125         return err;
1126 }
1127 
1128 static int sctp_connect_add_peer(struct sctp_association *asoc,
1129                                  union sctp_addr *daddr, int addr_len)
1130 {
1131         struct sctp_endpoint *ep = asoc->ep;
1132         struct sctp_association *old;
1133         struct sctp_transport *t;
1134         int err;
1135 
1136         err = sctp_verify_addr(ep->base.sk, daddr, addr_len);
1137         if (err)
1138                 return err;
1139 
1140         old = sctp_endpoint_lookup_assoc(ep, daddr, &t);
1141         if (old && old != asoc)
1142                 return old->state >= SCTP_STATE_ESTABLISHED ? -EISCONN
1143                                                             : -EALREADY;
1144 
1145         if (sctp_endpoint_is_peeled_off(ep, daddr))
1146                 return -EADDRNOTAVAIL;
1147 
1148         t = sctp_assoc_add_peer(asoc, daddr, GFP_KERNEL, SCTP_UNKNOWN);
1149         if (!t)
1150                 return -ENOMEM;
1151 
1152         return 0;
1153 }
1154 
1155 
1156 
1157 
1158 
1159 
1160 static int __sctp_connect(struct sock *sk, struct sockaddr *kaddrs,
1161                           int addrs_size, int flags, sctp_assoc_t *assoc_id)
1162 {
1163         struct sctp_sock *sp = sctp_sk(sk);
1164         struct sctp_endpoint *ep = sp->ep;
1165         struct sctp_transport *transport;
1166         struct sctp_association *asoc;
1167         void *addr_buf = kaddrs;
1168         union sctp_addr *daddr;
1169         struct sctp_af *af;
1170         int walk_size, err;
1171         long timeo;
1172 
1173         if (sctp_sstate(sk, ESTABLISHED) || sctp_sstate(sk, CLOSING) ||
1174             (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING)))
1175                 return -EISCONN;
1176 
1177         daddr = addr_buf;
1178         af = sctp_get_af_specific(daddr->sa.sa_family);
1179         if (!af || af->sockaddr_len > addrs_size)
1180                 return -EINVAL;
1181 
1182         err = sctp_verify_addr(sk, daddr, af->sockaddr_len);
1183         if (err)
1184                 return err;
1185 
1186         asoc = sctp_endpoint_lookup_assoc(ep, daddr, &transport);
1187         if (asoc)
1188                 return asoc->state >= SCTP_STATE_ESTABLISHED ? -EISCONN
1189                                                              : -EALREADY;
1190 
1191         err = sctp_connect_new_asoc(ep, daddr, NULL, &transport);
1192         if (err)
1193                 return err;
1194         asoc = transport->asoc;
1195 
1196         addr_buf += af->sockaddr_len;
1197         walk_size = af->sockaddr_len;
1198         while (walk_size < addrs_size) {
1199                 err = -EINVAL;
1200                 if (walk_size + sizeof(sa_family_t) > addrs_size)
1201                         goto out_free;
1202 
1203                 daddr = addr_buf;
1204                 af = sctp_get_af_specific(daddr->sa.sa_family);
1205                 if (!af || af->sockaddr_len + walk_size > addrs_size)
1206                         goto out_free;
1207 
1208                 if (asoc->peer.port != ntohs(daddr->v4.sin_port))
1209                         goto out_free;
1210 
1211                 err = sctp_connect_add_peer(asoc, daddr, af->sockaddr_len);
1212                 if (err)
1213                         goto out_free;
1214 
1215                 addr_buf  += af->sockaddr_len;
1216                 walk_size += af->sockaddr_len;
1217         }
1218 
1219         
1220 
1221 
1222         if (assoc_id) {
1223                 err = sctp_assoc_set_id(asoc, GFP_KERNEL);
1224                 if (err < 0)
1225                         goto out_free;
1226         }
1227 
1228         err = sctp_primitive_ASSOCIATE(sock_net(sk), asoc, NULL);
1229         if (err < 0)
1230                 goto out_free;
1231 
1232         
1233         inet_sk(sk)->inet_dport = htons(asoc->peer.port);
1234         sp->pf->to_sk_daddr(daddr, sk);
1235         sk->sk_err = 0;
1236 
1237         if (assoc_id)
1238                 *assoc_id = asoc->assoc_id;
1239 
1240         timeo = sock_sndtimeo(sk, flags & O_NONBLOCK);
1241         return sctp_wait_for_connect(asoc, &timeo);
1242 
1243 out_free:
1244         pr_debug("%s: took out_free path with asoc:%p kaddrs:%p err:%d\n",
1245                  __func__, asoc, kaddrs, err);
1246         sctp_association_free(asoc);
1247         return err;
1248 }
1249 
1250 
1251 
1252 
1253 
1254 
1255 
1256 
1257 
1258 
1259 
1260 
1261 
1262 
1263 
1264 
1265 
1266 
1267 
1268 
1269 
1270 
1271 
1272 
1273 
1274 
1275 
1276 
1277 
1278 
1279 
1280 
1281 
1282 
1283 
1284 
1285 
1286 
1287 
1288 
1289 
1290 
1291 
1292 
1293 
1294 
1295 
1296 
1297 
1298 
1299 
1300 
1301 
1302 
1303 
1304 
1305 static int __sctp_setsockopt_connectx(struct sock *sk,
1306                                       struct sockaddr __user *addrs,
1307                                       int addrs_size,
1308                                       sctp_assoc_t *assoc_id)
1309 {
1310         struct sockaddr *kaddrs;
1311         int err = 0, flags = 0;
1312 
1313         pr_debug("%s: sk:%p addrs:%p addrs_size:%d\n",
1314                  __func__, sk, addrs, addrs_size);
1315 
1316         
1317         if (unlikely(addrs_size < sizeof(sa_family_t)))
1318                 return -EINVAL;
1319 
1320         kaddrs = memdup_user(addrs, addrs_size);
1321         if (IS_ERR(kaddrs))
1322                 return PTR_ERR(kaddrs);
1323 
1324         
1325         err = security_sctp_bind_connect(sk, SCTP_SOCKOPT_CONNECTX,
1326                                          (struct sockaddr *)kaddrs,
1327                                           addrs_size);
1328         if (err)
1329                 goto out_free;
1330 
1331         
1332 
1333 
1334         if (sk->sk_socket->file)
1335                 flags = sk->sk_socket->file->f_flags;
1336 
1337         err = __sctp_connect(sk, kaddrs, addrs_size, flags, assoc_id);
1338 
1339 out_free:
1340         kfree(kaddrs);
1341 
1342         return err;
1343 }
1344 
1345 
1346 
1347 
1348 
1349 static int sctp_setsockopt_connectx_old(struct sock *sk,
1350                                         struct sockaddr __user *addrs,
1351                                         int addrs_size)
1352 {
1353         return __sctp_setsockopt_connectx(sk, addrs, addrs_size, NULL);
1354 }
1355 
1356 
1357 
1358 
1359 
1360 
1361 
1362 static int sctp_setsockopt_connectx(struct sock *sk,
1363                                     struct sockaddr __user *addrs,
1364                                     int addrs_size)
1365 {
1366         sctp_assoc_t assoc_id = 0;
1367         int err = 0;
1368 
1369         err = __sctp_setsockopt_connectx(sk, addrs, addrs_size, &assoc_id);
1370 
1371         if (err)
1372                 return err;
1373         else
1374                 return assoc_id;
1375 }
1376 
1377 
1378 
1379 
1380 
1381 
1382 
1383 
1384 
1385 #ifdef CONFIG_COMPAT
1386 struct compat_sctp_getaddrs_old {
1387         sctp_assoc_t    assoc_id;
1388         s32             addr_num;
1389         compat_uptr_t   addrs;          
1390 };
1391 #endif
1392 
1393 static int sctp_getsockopt_connectx3(struct sock *sk, int len,
1394                                      char __user *optval,
1395                                      int __user *optlen)
1396 {
1397         struct sctp_getaddrs_old param;
1398         sctp_assoc_t assoc_id = 0;
1399         int err = 0;
1400 
1401 #ifdef CONFIG_COMPAT
1402         if (in_compat_syscall()) {
1403                 struct compat_sctp_getaddrs_old param32;
1404 
1405                 if (len < sizeof(param32))
1406                         return -EINVAL;
1407                 if (copy_from_user(¶m32, optval, sizeof(param32)))
1408                         return -EFAULT;
1409 
1410                 param.assoc_id = param32.assoc_id;
1411                 param.addr_num = param32.addr_num;
1412                 param.addrs = compat_ptr(param32.addrs);
1413         } else
1414 #endif
1415         {
1416                 if (len < sizeof(param))
1417                         return -EINVAL;
1418                 if (copy_from_user(¶m, optval, sizeof(param)))
1419                         return -EFAULT;
1420         }
1421 
1422         err = __sctp_setsockopt_connectx(sk, (struct sockaddr __user *)
1423                                          param.addrs, param.addr_num,
1424                                          &assoc_id);
1425         if (err == 0 || err == -EINPROGRESS) {
1426                 if (copy_to_user(optval, &assoc_id, sizeof(assoc_id)))
1427                         return -EFAULT;
1428                 if (put_user(sizeof(assoc_id), optlen))
1429                         return -EFAULT;
1430         }
1431 
1432         return err;
1433 }
1434 
1435 
1436 
1437 
1438 
1439 
1440 
1441 
1442 
1443 
1444 
1445 
1446 
1447 
1448 
1449 
1450 
1451 
1452 
1453 
1454 
1455 
1456 
1457 
1458 
1459 
1460 
1461 
1462 
1463 
1464 
1465 
1466 
1467 
1468 
1469 
1470 
1471 
1472 
1473 
1474 
1475 
1476 
1477 
1478 
1479 
1480 
1481 
1482 
1483 
1484 
1485 static void sctp_close(struct sock *sk, long timeout)
1486 {
1487         struct net *net = sock_net(sk);
1488         struct sctp_endpoint *ep;
1489         struct sctp_association *asoc;
1490         struct list_head *pos, *temp;
1491         unsigned int data_was_unread;
1492 
1493         pr_debug("%s: sk:%p, timeout:%ld\n", __func__, sk, timeout);
1494 
1495         lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
1496         sk->sk_shutdown = SHUTDOWN_MASK;
1497         inet_sk_set_state(sk, SCTP_SS_CLOSING);
1498 
1499         ep = sctp_sk(sk)->ep;
1500 
1501         
1502         data_was_unread = sctp_queue_purge_ulpevents(&sk->sk_receive_queue);
1503         data_was_unread += sctp_queue_purge_ulpevents(&sctp_sk(sk)->pd_lobby);
1504 
1505         
1506         list_for_each_safe(pos, temp, &ep->asocs) {
1507                 asoc = list_entry(pos, struct sctp_association, asocs);
1508 
1509                 if (sctp_style(sk, TCP)) {
1510                         
1511 
1512 
1513 
1514 
1515                         if (sctp_state(asoc, CLOSED)) {
1516                                 sctp_association_free(asoc);
1517                                 continue;
1518                         }
1519                 }
1520 
1521                 if (data_was_unread || !skb_queue_empty(&asoc->ulpq.lobby) ||
1522                     !skb_queue_empty(&asoc->ulpq.reasm) ||
1523                     !skb_queue_empty(&asoc->ulpq.reasm_uo) ||
1524                     (sock_flag(sk, SOCK_LINGER) && !sk->sk_lingertime)) {
1525                         struct sctp_chunk *chunk;
1526 
1527                         chunk = sctp_make_abort_user(asoc, NULL, 0);
1528                         sctp_primitive_ABORT(net, asoc, chunk);
1529                 } else
1530                         sctp_primitive_SHUTDOWN(net, asoc, NULL);
1531         }
1532 
1533         
1534         if (sctp_style(sk, TCP) && timeout)
1535                 sctp_wait_for_close(sk, timeout);
1536 
1537         
1538         release_sock(sk);
1539 
1540         
1541 
1542 
1543 
1544 
1545         spin_lock_bh(&net->sctp.addr_wq_lock);
1546         bh_lock_sock_nested(sk);
1547 
1548         
1549 
1550 
1551         sock_hold(sk);
1552         sk_common_release(sk);
1553 
1554         bh_unlock_sock(sk);
1555         spin_unlock_bh(&net->sctp.addr_wq_lock);
1556 
1557         sock_put(sk);
1558 
1559         SCTP_DBG_OBJCNT_DEC(sock);
1560 }
1561 
1562 
1563 static int sctp_error(struct sock *sk, int flags, int err)
1564 {
1565         if (err == -EPIPE)
1566                 err = sock_error(sk) ? : -EPIPE;
1567         if (err == -EPIPE && !(flags & MSG_NOSIGNAL))
1568                 send_sig(SIGPIPE, current, 0);
1569         return err;
1570 }
1571 
1572 
1573 
1574 
1575 
1576 
1577 
1578 
1579 
1580 
1581 
1582 
1583 
1584 
1585 
1586 
1587 
1588 
1589 
1590 
1591 
1592 
1593 
1594 
1595 static int sctp_msghdr_parse(const struct msghdr *msg,
1596                              struct sctp_cmsgs *cmsgs);
1597 
1598 static int sctp_sendmsg_parse(struct sock *sk, struct sctp_cmsgs *cmsgs,
1599                               struct sctp_sndrcvinfo *srinfo,
1600                               const struct msghdr *msg, size_t msg_len)
1601 {
1602         __u16 sflags;
1603         int err;
1604 
1605         if (sctp_sstate(sk, LISTENING) && sctp_style(sk, TCP))
1606                 return -EPIPE;
1607 
1608         if (msg_len > sk->sk_sndbuf)
1609                 return -EMSGSIZE;
1610 
1611         memset(cmsgs, 0, sizeof(*cmsgs));
1612         err = sctp_msghdr_parse(msg, cmsgs);
1613         if (err) {
1614                 pr_debug("%s: msghdr parse err:%x\n", __func__, err);
1615                 return err;
1616         }
1617 
1618         memset(srinfo, 0, sizeof(*srinfo));
1619         if (cmsgs->srinfo) {
1620                 srinfo->sinfo_stream = cmsgs->srinfo->sinfo_stream;
1621                 srinfo->sinfo_flags = cmsgs->srinfo->sinfo_flags;
1622                 srinfo->sinfo_ppid = cmsgs->srinfo->sinfo_ppid;
1623                 srinfo->sinfo_context = cmsgs->srinfo->sinfo_context;
1624                 srinfo->sinfo_assoc_id = cmsgs->srinfo->sinfo_assoc_id;
1625                 srinfo->sinfo_timetolive = cmsgs->srinfo->sinfo_timetolive;
1626         }
1627 
1628         if (cmsgs->sinfo) {
1629                 srinfo->sinfo_stream = cmsgs->sinfo->snd_sid;
1630                 srinfo->sinfo_flags = cmsgs->sinfo->snd_flags;
1631                 srinfo->sinfo_ppid = cmsgs->sinfo->snd_ppid;
1632                 srinfo->sinfo_context = cmsgs->sinfo->snd_context;
1633                 srinfo->sinfo_assoc_id = cmsgs->sinfo->snd_assoc_id;
1634         }
1635 
1636         if (cmsgs->prinfo) {
1637                 srinfo->sinfo_timetolive = cmsgs->prinfo->pr_value;
1638                 SCTP_PR_SET_POLICY(srinfo->sinfo_flags,
1639                                    cmsgs->prinfo->pr_policy);
1640         }
1641 
1642         sflags = srinfo->sinfo_flags;
1643         if (!sflags && msg_len)
1644                 return 0;
1645 
1646         if (sctp_style(sk, TCP) && (sflags & (SCTP_EOF | SCTP_ABORT)))
1647                 return -EINVAL;
1648 
1649         if (((sflags & SCTP_EOF) && msg_len > 0) ||
1650             (!(sflags & (SCTP_EOF | SCTP_ABORT)) && msg_len == 0))
1651                 return -EINVAL;
1652 
1653         if ((sflags & SCTP_ADDR_OVER) && !msg->msg_name)
1654                 return -EINVAL;
1655 
1656         return 0;
1657 }
1658 
1659 static int sctp_sendmsg_new_asoc(struct sock *sk, __u16 sflags,
1660                                  struct sctp_cmsgs *cmsgs,
1661                                  union sctp_addr *daddr,
1662                                  struct sctp_transport **tp)
1663 {
1664         struct sctp_endpoint *ep = sctp_sk(sk)->ep;
1665         struct sctp_association *asoc;
1666         struct cmsghdr *cmsg;
1667         __be32 flowinfo = 0;
1668         struct sctp_af *af;
1669         int err;
1670 
1671         *tp = NULL;
1672 
1673         if (sflags & (SCTP_EOF | SCTP_ABORT))
1674                 return -EINVAL;
1675 
1676         if (sctp_style(sk, TCP) && (sctp_sstate(sk, ESTABLISHED) ||
1677                                     sctp_sstate(sk, CLOSING)))
1678                 return -EADDRNOTAVAIL;
1679 
1680         
1681 
1682 
1683 
1684 
1685 
1686         af = sctp_get_af_specific(daddr->sa.sa_family);
1687         if (!af)
1688                 return -EINVAL;
1689         err = security_sctp_bind_connect(sk, SCTP_SENDMSG_CONNECT,
1690                                          (struct sockaddr *)daddr,
1691                                          af->sockaddr_len);
1692         if (err < 0)
1693                 return err;
1694 
1695         err = sctp_connect_new_asoc(ep, daddr, cmsgs->init, tp);
1696         if (err)
1697                 return err;
1698         asoc = (*tp)->asoc;
1699 
1700         if (!cmsgs->addrs_msg)
1701                 return 0;
1702 
1703         if (daddr->sa.sa_family == AF_INET6)
1704                 flowinfo = daddr->v6.sin6_flowinfo;
1705 
1706         
1707         for_each_cmsghdr(cmsg, cmsgs->addrs_msg) {
1708                 union sctp_addr _daddr;
1709                 int dlen;
1710 
1711                 if (cmsg->cmsg_level != IPPROTO_SCTP ||
1712                     (cmsg->cmsg_type != SCTP_DSTADDRV4 &&
1713                      cmsg->cmsg_type != SCTP_DSTADDRV6))
1714                         continue;
1715 
1716                 daddr = &_daddr;
1717                 memset(daddr, 0, sizeof(*daddr));
1718                 dlen = cmsg->cmsg_len - sizeof(struct cmsghdr);
1719                 if (cmsg->cmsg_type == SCTP_DSTADDRV4) {
1720                         if (dlen < sizeof(struct in_addr)) {
1721                                 err = -EINVAL;
1722                                 goto free;
1723                         }
1724 
1725                         dlen = sizeof(struct in_addr);
1726                         daddr->v4.sin_family = AF_INET;
1727                         daddr->v4.sin_port = htons(asoc->peer.port);
1728                         memcpy(&daddr->v4.sin_addr, CMSG_DATA(cmsg), dlen);
1729                 } else {
1730                         if (dlen < sizeof(struct in6_addr)) {
1731                                 err = -EINVAL;
1732                                 goto free;
1733                         }
1734 
1735                         dlen = sizeof(struct in6_addr);
1736                         daddr->v6.sin6_flowinfo = flowinfo;
1737                         daddr->v6.sin6_family = AF_INET6;
1738                         daddr->v6.sin6_port = htons(asoc->peer.port);
1739                         memcpy(&daddr->v6.sin6_addr, CMSG_DATA(cmsg), dlen);
1740                 }
1741 
1742                 err = sctp_connect_add_peer(asoc, daddr, sizeof(*daddr));
1743                 if (err)
1744                         goto free;
1745         }
1746 
1747         return 0;
1748 
1749 free:
1750         sctp_association_free(asoc);
1751         return err;
1752 }
1753 
1754 static int sctp_sendmsg_check_sflags(struct sctp_association *asoc,
1755                                      __u16 sflags, struct msghdr *msg,
1756                                      size_t msg_len)
1757 {
1758         struct sock *sk = asoc->base.sk;
1759         struct net *net = sock_net(sk);
1760 
1761         if (sctp_state(asoc, CLOSED) && sctp_style(sk, TCP))
1762                 return -EPIPE;
1763 
1764         if ((sflags & SCTP_SENDALL) && sctp_style(sk, UDP) &&
1765             !sctp_state(asoc, ESTABLISHED))
1766                 return 0;
1767 
1768         if (sflags & SCTP_EOF) {
1769                 pr_debug("%s: shutting down association:%p\n", __func__, asoc);
1770                 sctp_primitive_SHUTDOWN(net, asoc, NULL);
1771 
1772                 return 0;
1773         }
1774 
1775         if (sflags & SCTP_ABORT) {
1776                 struct sctp_chunk *chunk;
1777 
1778                 chunk = sctp_make_abort_user(asoc, msg, msg_len);
1779                 if (!chunk)
1780                         return -ENOMEM;
1781 
1782                 pr_debug("%s: aborting association:%p\n", __func__, asoc);
1783                 sctp_primitive_ABORT(net, asoc, chunk);
1784                 iov_iter_revert(&msg->msg_iter, msg_len);
1785 
1786                 return 0;
1787         }
1788 
1789         return 1;
1790 }
1791 
1792 static int sctp_sendmsg_to_asoc(struct sctp_association *asoc,
1793                                 struct msghdr *msg, size_t msg_len,
1794                                 struct sctp_transport *transport,
1795                                 struct sctp_sndrcvinfo *sinfo)
1796 {
1797         struct sock *sk = asoc->base.sk;
1798         struct sctp_sock *sp = sctp_sk(sk);
1799         struct net *net = sock_net(sk);
1800         struct sctp_datamsg *datamsg;
1801         bool wait_connect = false;
1802         struct sctp_chunk *chunk;
1803         long timeo;
1804         int err;
1805 
1806         if (sinfo->sinfo_stream >= asoc->stream.outcnt) {
1807                 err = -EINVAL;
1808                 goto err;
1809         }
1810 
1811         if (unlikely(!SCTP_SO(&asoc->stream, sinfo->sinfo_stream)->ext)) {
1812                 err = sctp_stream_init_ext(&asoc->stream, sinfo->sinfo_stream);
1813                 if (err)
1814                         goto err;
1815         }
1816 
1817         if (sp->disable_fragments && msg_len > asoc->frag_point) {
1818                 err = -EMSGSIZE;
1819                 goto err;
1820         }
1821 
1822         if (asoc->pmtu_pending) {
1823                 if (sp->param_flags & SPP_PMTUD_ENABLE)
1824                         sctp_assoc_sync_pmtu(asoc);
1825                 asoc->pmtu_pending = 0;
1826         }
1827 
1828         if (sctp_wspace(asoc) < (int)msg_len)
1829                 sctp_prsctp_prune(asoc, sinfo, msg_len - sctp_wspace(asoc));
1830 
1831         if (sk_under_memory_pressure(sk))
1832                 sk_mem_reclaim(sk);
1833 
1834         if (sctp_wspace(asoc) <= 0 || !sk_wmem_schedule(sk, msg_len)) {
1835                 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1836                 err = sctp_wait_for_sndbuf(asoc, &timeo, msg_len);
1837                 if (err)
1838                         goto err;
1839         }
1840 
1841         if (sctp_state(asoc, CLOSED)) {
1842                 err = sctp_primitive_ASSOCIATE(net, asoc, NULL);
1843                 if (err)
1844                         goto err;
1845 
1846                 if (asoc->ep->intl_enable) {
1847                         timeo = sock_sndtimeo(sk, 0);
1848                         err = sctp_wait_for_connect(asoc, &timeo);
1849                         if (err) {
1850                                 err = -ESRCH;
1851                                 goto err;
1852                         }
1853                 } else {
1854                         wait_connect = true;
1855                 }
1856 
1857                 pr_debug("%s: we associated primitively\n", __func__);
1858         }
1859 
1860         datamsg = sctp_datamsg_from_user(asoc, sinfo, &msg->msg_iter);
1861         if (IS_ERR(datamsg)) {
1862                 err = PTR_ERR(datamsg);
1863                 goto err;
1864         }
1865 
1866         asoc->force_delay = !!(msg->msg_flags & MSG_MORE);
1867 
1868         list_for_each_entry(chunk, &datamsg->chunks, frag_list) {
1869                 sctp_chunk_hold(chunk);
1870                 sctp_set_owner_w(chunk);
1871                 chunk->transport = transport;
1872         }
1873 
1874         err = sctp_primitive_SEND(net, asoc, datamsg);
1875         if (err) {
1876                 sctp_datamsg_free(datamsg);
1877                 goto err;
1878         }
1879 
1880         pr_debug("%s: we sent primitively\n", __func__);
1881 
1882         sctp_datamsg_put(datamsg);
1883 
1884         if (unlikely(wait_connect)) {
1885                 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1886                 sctp_wait_for_connect(asoc, &timeo);
1887         }
1888 
1889         err = msg_len;
1890 
1891 err:
1892         return err;
1893 }
1894 
1895 static union sctp_addr *sctp_sendmsg_get_daddr(struct sock *sk,
1896                                                const struct msghdr *msg,
1897                                                struct sctp_cmsgs *cmsgs)
1898 {
1899         union sctp_addr *daddr = NULL;
1900         int err;
1901 
1902         if (!sctp_style(sk, UDP_HIGH_BANDWIDTH) && msg->msg_name) {
1903                 int len = msg->msg_namelen;
1904 
1905                 if (len > sizeof(*daddr))
1906                         len = sizeof(*daddr);
1907 
1908                 daddr = (union sctp_addr *)msg->msg_name;
1909 
1910                 err = sctp_verify_addr(sk, daddr, len);
1911                 if (err)
1912                         return ERR_PTR(err);
1913         }
1914 
1915         return daddr;
1916 }
1917 
1918 static void sctp_sendmsg_update_sinfo(struct sctp_association *asoc,
1919                                       struct sctp_sndrcvinfo *sinfo,
1920                                       struct sctp_cmsgs *cmsgs)
1921 {
1922         if (!cmsgs->srinfo && !cmsgs->sinfo) {
1923                 sinfo->sinfo_stream = asoc->default_stream;
1924                 sinfo->sinfo_ppid = asoc->default_ppid;
1925                 sinfo->sinfo_context = asoc->default_context;
1926                 sinfo->sinfo_assoc_id = sctp_assoc2id(asoc);
1927 
1928                 if (!cmsgs->prinfo)
1929                         sinfo->sinfo_flags = asoc->default_flags;
1930         }
1931 
1932         if (!cmsgs->srinfo && !cmsgs->prinfo)
1933                 sinfo->sinfo_timetolive = asoc->default_timetolive;
1934 
1935         if (cmsgs->authinfo) {
1936                 
1937 
1938 
1939                 sinfo->sinfo_tsn = 1;
1940                 sinfo->sinfo_ssn = cmsgs->authinfo->auth_keynumber;
1941         }
1942 }
1943 
1944 static int sctp_sendmsg(struct sock *sk, struct msghdr *msg, size_t msg_len)
1945 {
1946         struct sctp_endpoint *ep = sctp_sk(sk)->ep;
1947         struct sctp_transport *transport = NULL;
1948         struct sctp_sndrcvinfo _sinfo, *sinfo;
1949         struct sctp_association *asoc, *tmp;
1950         struct sctp_cmsgs cmsgs;
1951         union sctp_addr *daddr;
1952         bool new = false;
1953         __u16 sflags;
1954         int err;
1955 
1956         
1957         err = sctp_sendmsg_parse(sk, &cmsgs, &_sinfo, msg, msg_len);
1958         if (err)
1959                 goto out;
1960 
1961         sinfo  = &_sinfo;
1962         sflags = sinfo->sinfo_flags;
1963 
1964         
1965         daddr = sctp_sendmsg_get_daddr(sk, msg, &cmsgs);
1966         if (IS_ERR(daddr)) {
1967                 err = PTR_ERR(daddr);
1968                 goto out;
1969         }
1970 
1971         lock_sock(sk);
1972 
1973         
1974         if ((sflags & SCTP_SENDALL) && sctp_style(sk, UDP)) {
1975                 list_for_each_entry_safe(asoc, tmp, &ep->asocs, asocs) {
1976                         err = sctp_sendmsg_check_sflags(asoc, sflags, msg,
1977                                                         msg_len);
1978                         if (err == 0)
1979                                 continue;
1980                         if (err < 0)
1981                                 goto out_unlock;
1982 
1983                         sctp_sendmsg_update_sinfo(asoc, sinfo, &cmsgs);
1984 
1985                         err = sctp_sendmsg_to_asoc(asoc, msg, msg_len,
1986                                                    NULL, sinfo);
1987                         if (err < 0)
1988                                 goto out_unlock;
1989 
1990                         iov_iter_revert(&msg->msg_iter, err);
1991                 }
1992 
1993                 goto out_unlock;
1994         }
1995 
1996         
1997         if (daddr) {
1998                 asoc = sctp_endpoint_lookup_assoc(ep, daddr, &transport);
1999                 if (asoc) {
2000                         err = sctp_sendmsg_check_sflags(asoc, sflags, msg,
2001                                                         msg_len);
2002                         if (err <= 0)
2003                                 goto out_unlock;
2004                 } else {
2005                         err = sctp_sendmsg_new_asoc(sk, sflags, &cmsgs, daddr,
2006                                                     &transport);
2007                         if (err)
2008                                 goto out_unlock;
2009 
2010                         asoc = transport->asoc;
2011                         new = true;
2012                 }
2013 
2014                 if (!sctp_style(sk, TCP) && !(sflags & SCTP_ADDR_OVER))
2015                         transport = NULL;
2016         } else {
2017                 asoc = sctp_id2assoc(sk, sinfo->sinfo_assoc_id);
2018                 if (!asoc) {
2019                         err = -EPIPE;
2020                         goto out_unlock;
2021                 }
2022 
2023                 err = sctp_sendmsg_check_sflags(asoc, sflags, msg, msg_len);
2024                 if (err <= 0)
2025                         goto out_unlock;
2026         }
2027 
2028         
2029         sctp_sendmsg_update_sinfo(asoc, sinfo, &cmsgs);
2030 
2031         
2032         err = sctp_sendmsg_to_asoc(asoc, msg, msg_len, transport, sinfo);
2033         if (err < 0 && err != -ESRCH && new)
2034                 sctp_association_free(asoc);
2035 
2036 out_unlock:
2037         release_sock(sk);
2038 out:
2039         return sctp_error(sk, msg->msg_flags, err);
2040 }
2041 
2042 
2043 
2044 
2045 
2046 
2047 
2048 
2049 static int sctp_skb_pull(struct sk_buff *skb, int len)
2050 {
2051         struct sk_buff *list;
2052         int skb_len = skb_headlen(skb);
2053         int rlen;
2054 
2055         if (len <= skb_len) {
2056                 __skb_pull(skb, len);
2057                 return 0;
2058         }
2059         len -= skb_len;
2060         __skb_pull(skb, skb_len);
2061 
2062         skb_walk_frags(skb, list) {
2063                 rlen = sctp_skb_pull(list, len);
2064                 skb->len -= (len-rlen);
2065                 skb->data_len -= (len-rlen);
2066 
2067                 if (!rlen)
2068                         return 0;
2069 
2070                 len = rlen;
2071         }
2072 
2073         return len;
2074 }
2075 
2076 
2077 
2078 
2079 
2080 
2081 
2082 
2083 
2084 
2085 
2086 
2087 
2088 
2089 
2090 
2091 static int sctp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
2092                         int noblock, int flags, int *addr_len)
2093 {
2094         struct sctp_ulpevent *event = NULL;
2095         struct sctp_sock *sp = sctp_sk(sk);
2096         struct sk_buff *skb, *head_skb;
2097         int copied;
2098         int err = 0;
2099         int skb_len;
2100 
2101         pr_debug("%s: sk:%p, msghdr:%p, len:%zd, noblock:%d, flags:0x%x, "
2102                  "addr_len:%p)\n", __func__, sk, msg, len, noblock, flags,
2103                  addr_len);
2104 
2105         lock_sock(sk);
2106 
2107         if (sctp_style(sk, TCP) && !sctp_sstate(sk, ESTABLISHED) &&
2108             !sctp_sstate(sk, CLOSING) && !sctp_sstate(sk, CLOSED)) {
2109                 err = -ENOTCONN;
2110                 goto out;
2111         }
2112 
2113         skb = sctp_skb_recv_datagram(sk, flags, noblock, &err);
2114         if (!skb)
2115                 goto out;
2116 
2117         
2118 
2119 
2120         skb_len = skb->len;
2121 
2122         copied = skb_len;
2123         if (copied > len)
2124                 copied = len;
2125 
2126         err = skb_copy_datagram_msg(skb, 0, msg, copied);
2127 
2128         event = sctp_skb2event(skb);
2129 
2130         if (err)
2131                 goto out_free;
2132 
2133         if (event->chunk && event->chunk->head_skb)
2134                 head_skb = event->chunk->head_skb;
2135         else
2136                 head_skb = skb;
2137         sock_recv_ts_and_drops(msg, sk, head_skb);
2138         if (sctp_ulpevent_is_notification(event)) {
2139                 msg->msg_flags |= MSG_NOTIFICATION;
2140                 sp->pf->event_msgname(event, msg->msg_name, addr_len);
2141         } else {
2142                 sp->pf->skb_msgname(head_skb, msg->msg_name, addr_len);
2143         }
2144 
2145         
2146         if (sp->recvnxtinfo)
2147                 sctp_ulpevent_read_nxtinfo(event, msg, sk);
2148         
2149         if (sp->recvrcvinfo)
2150                 sctp_ulpevent_read_rcvinfo(event, msg);
2151         
2152         if (sctp_ulpevent_type_enabled(sp->subscribe, SCTP_DATA_IO_EVENT))
2153                 sctp_ulpevent_read_sndrcvinfo(event, msg);
2154 
2155         err = copied;
2156 
2157         
2158 
2159 
2160 
2161         if (skb_len > copied) {
2162                 msg->msg_flags &= ~MSG_EOR;
2163                 if (flags & MSG_PEEK)
2164                         goto out_free;
2165                 sctp_skb_pull(skb, copied);
2166                 skb_queue_head(&sk->sk_receive_queue, skb);
2167 
2168                 
2169 
2170 
2171 
2172                 if (!sctp_ulpevent_is_notification(event))
2173                         sctp_assoc_rwnd_increase(event->asoc, copied);
2174                 goto out;
2175         } else if ((event->msg_flags & MSG_NOTIFICATION) ||
2176                    (event->msg_flags & MSG_EOR))
2177                 msg->msg_flags |= MSG_EOR;
2178         else
2179                 msg->msg_flags &= ~MSG_EOR;
2180 
2181 out_free:
2182         if (flags & MSG_PEEK) {
2183                 
2184 
2185 
2186                 kfree_skb(skb);
2187         } else {
2188                 
2189 
2190 
2191 
2192                 sctp_ulpevent_free(event);
2193         }
2194 out:
2195         release_sock(sk);
2196         return err;
2197 }
2198 
2199 
2200 
2201 
2202 
2203 
2204 
2205 
2206 static int sctp_setsockopt_disable_fragments(struct sock *sk,
2207                                              char __user *optval,
2208                                              unsigned int optlen)
2209 {
2210         int val;
2211 
2212         if (optlen < sizeof(int))
2213                 return -EINVAL;
2214 
2215         if (get_user(val, (int __user *)optval))
2216                 return -EFAULT;
2217 
2218         sctp_sk(sk)->disable_fragments = (val == 0) ? 0 : 1;
2219 
2220         return 0;
2221 }
2222 
2223 static int sctp_setsockopt_events(struct sock *sk, char __user *optval,
2224                                   unsigned int optlen)
2225 {
2226         struct sctp_event_subscribe subscribe;
2227         __u8 *sn_type = (__u8 *)&subscribe;
2228         struct sctp_sock *sp = sctp_sk(sk);
2229         struct sctp_association *asoc;
2230         int i;
2231 
2232         if (optlen > sizeof(struct sctp_event_subscribe))
2233                 return -EINVAL;
2234 
2235         if (copy_from_user(&subscribe, optval, optlen))
2236                 return -EFAULT;
2237 
2238         for (i = 0; i < optlen; i++)
2239                 sctp_ulpevent_type_set(&sp->subscribe, SCTP_SN_TYPE_BASE + i,
2240                                        sn_type[i]);
2241 
2242         list_for_each_entry(asoc, &sp->ep->asocs, asocs)
2243                 asoc->subscribe = sctp_sk(sk)->subscribe;
2244 
2245         
2246 
2247 
2248 
2249         if (sctp_ulpevent_type_enabled(sp->subscribe, SCTP_SENDER_DRY_EVENT)) {
2250                 struct sctp_ulpevent *event;
2251 
2252                 asoc = sctp_id2assoc(sk, 0);
2253                 if (asoc && sctp_outq_is_empty(&asoc->outqueue)) {
2254                         event = sctp_ulpevent_make_sender_dry_event(asoc,
2255                                         GFP_USER | __GFP_NOWARN);
2256                         if (!event)
2257                                 return -ENOMEM;
2258 
2259                         asoc->stream.si->enqueue_event(&asoc->ulpq, event);
2260                 }
2261         }
2262 
2263         return 0;
2264 }
2265 
2266 
2267 
2268 
2269 
2270 
2271 
2272 
2273 
2274 
2275 
2276 
2277 static int sctp_setsockopt_autoclose(struct sock *sk, char __user *optval,
2278                                      unsigned int optlen)
2279 {
2280         struct sctp_sock *sp = sctp_sk(sk);
2281         struct net *net = sock_net(sk);
2282 
2283         
2284         if (sctp_style(sk, TCP))
2285                 return -EOPNOTSUPP;
2286         if (optlen != sizeof(int))
2287                 return -EINVAL;
2288         if (copy_from_user(&sp->autoclose, optval, optlen))
2289                 return -EFAULT;
2290 
2291         if (sp->autoclose > net->sctp.max_autoclose)
2292                 sp->autoclose = net->sctp.max_autoclose;
2293 
2294         return 0;
2295 }
2296 
2297 
2298 
2299 
2300 
2301 
2302 
2303 
2304 
2305 
2306 
2307 
2308 
2309 
2310 
2311 
2312 
2313 
2314 
2315 
2316 
2317 
2318 
2319 
2320 
2321 
2322 
2323 
2324 
2325 
2326 
2327 
2328 
2329 
2330 
2331 
2332 
2333 
2334 
2335 
2336 
2337 
2338 
2339 
2340 
2341 
2342 
2343 
2344 
2345 
2346 
2347 
2348 
2349 
2350 
2351 
2352 
2353 
2354 
2355 
2356 
2357 
2358 
2359 
2360 
2361 
2362 
2363 
2364 
2365 
2366 
2367 
2368 
2369 
2370 
2371 
2372 
2373 
2374 
2375 
2376 
2377 
2378 
2379 
2380 
2381 
2382 
2383 
2384 
2385 
2386 
2387 
2388 
2389 
2390 
2391 
2392 
2393 
2394 
2395 
2396 
2397 
2398 
2399 
2400 
2401 
2402 
2403 
2404 
2405 
2406 
2407 
2408 
2409 
2410 
2411 
2412 
2413 
2414 
2415 
2416 
2417 
2418 
2419 
2420 
2421 
2422 
2423 
2424 
2425 
2426 
2427 
2428 
2429 
2430 
2431 
2432 
2433 
2434 
2435 static int sctp_apply_peer_addr_params(struct sctp_paddrparams *params,
2436                                        struct sctp_transport   *trans,
2437                                        struct sctp_association *asoc,
2438                                        struct sctp_sock        *sp,
2439                                        int                      hb_change,
2440                                        int                      pmtud_change,
2441                                        int                      sackdelay_change)
2442 {
2443         int error;
2444 
2445         if (params->spp_flags & SPP_HB_DEMAND && trans) {
2446                 struct net *net = sock_net(trans->asoc->base.sk);
2447 
2448                 error = sctp_primitive_REQUESTHEARTBEAT(net, trans->asoc, trans);
2449                 if (error)
2450                         return error;
2451         }
2452 
2453         
2454 
2455 
2456 
2457         if (params->spp_flags & SPP_HB_ENABLE) {
2458 
2459                 
2460 
2461 
2462 
2463                 if (params->spp_flags & SPP_HB_TIME_IS_ZERO)
2464                         params->spp_hbinterval = 0;
2465 
2466                 if (params->spp_hbinterval ||
2467                     (params->spp_flags & SPP_HB_TIME_IS_ZERO)) {
2468                         if (trans) {
2469                                 trans->hbinterval =
2470                                     msecs_to_jiffies(params->spp_hbinterval);
2471                         } else if (asoc) {
2472                                 asoc->hbinterval =
2473                                     msecs_to_jiffies(params->spp_hbinterval);
2474                         } else {
2475                                 sp->hbinterval = params->spp_hbinterval;
2476                         }
2477                 }
2478         }
2479 
2480         if (hb_change) {
2481                 if (trans) {
2482                         trans->param_flags =
2483                                 (trans->param_flags & ~SPP_HB) | hb_change;
2484                 } else if (asoc) {
2485                         asoc->param_flags =
2486                                 (asoc->param_flags & ~SPP_HB) | hb_change;
2487                 } else {
2488                         sp->param_flags =
2489                                 (sp->param_flags & ~SPP_HB) | hb_change;
2490                 }
2491         }
2492 
2493         
2494 
2495 
2496 
2497 
2498         if ((params->spp_flags & SPP_PMTUD_DISABLE) && params->spp_pathmtu) {
2499                 if (trans) {
2500                         trans->pathmtu = params->spp_pathmtu;
2501                         sctp_assoc_sync_pmtu(asoc);
2502                 } else if (asoc) {
2503                         sctp_assoc_set_pmtu(asoc, params->spp_pathmtu);
2504                 } else {
2505                         sp->pathmtu = params->spp_pathmtu;
2506                 }
2507         }
2508 
2509         if (pmtud_change) {
2510                 if (trans) {
2511                         int update = (trans->param_flags & SPP_PMTUD_DISABLE) &&
2512                                 (params->spp_flags & SPP_PMTUD_ENABLE);
2513                         trans->param_flags =
2514                                 (trans->param_flags & ~SPP_PMTUD) | pmtud_change;
2515                         if (update) {
2516                                 sctp_transport_pmtu(trans, sctp_opt2sk(sp));
2517                                 sctp_assoc_sync_pmtu(asoc);
2518                         }
2519                 } else if (asoc) {
2520                         asoc->param_flags =
2521                                 (asoc->param_flags & ~SPP_PMTUD) | pmtud_change;
2522                 } else {
2523                         sp->param_flags =
2524                                 (sp->param_flags & ~SPP_PMTUD) | pmtud_change;
2525                 }
2526         }
2527 
2528         
2529 
2530 
2531 
2532         if ((params->spp_flags & SPP_SACKDELAY_ENABLE) && params->spp_sackdelay) {
2533                 if (trans) {
2534                         trans->sackdelay =
2535                                 msecs_to_jiffies(params->spp_sackdelay);
2536                 } else if (asoc) {
2537                         asoc->sackdelay =
2538                                 msecs_to_jiffies(params->spp_sackdelay);
2539                 } else {
2540                         sp->sackdelay = params->spp_sackdelay;
2541                 }
2542         }
2543 
2544         if (sackdelay_change) {
2545                 if (trans) {
2546                         trans->param_flags =
2547                                 (trans->param_flags & ~SPP_SACKDELAY) |
2548                                 sackdelay_change;
2549                 } else if (asoc) {
2550                         asoc->param_flags =
2551                                 (asoc->param_flags & ~SPP_SACKDELAY) |
2552                                 sackdelay_change;
2553                 } else {
2554                         sp->param_flags =
2555                                 (sp->param_flags & ~SPP_SACKDELAY) |
2556                                 sackdelay_change;
2557                 }
2558         }
2559 
2560         
2561 
2562 
2563         if (params->spp_pathmaxrxt) {
2564                 if (trans) {
2565                         trans->pathmaxrxt = params->spp_pathmaxrxt;
2566                 } else if (asoc) {
2567                         asoc->pathmaxrxt = params->spp_pathmaxrxt;
2568                 } else {
2569                         sp->pathmaxrxt = params->spp_pathmaxrxt;
2570                 }
2571         }
2572 
2573         if (params->spp_flags & SPP_IPV6_FLOWLABEL) {
2574                 if (trans) {
2575                         if (trans->ipaddr.sa.sa_family == AF_INET6) {
2576                                 trans->flowlabel = params->spp_ipv6_flowlabel &
2577                                                    SCTP_FLOWLABEL_VAL_MASK;
2578                                 trans->flowlabel |= SCTP_FLOWLABEL_SET_MASK;
2579                         }
2580                 } else if (asoc) {
2581                         struct sctp_transport *t;
2582 
2583                         list_for_each_entry(t, &asoc->peer.transport_addr_list,
2584                                             transports) {
2585                                 if (t->ipaddr.sa.sa_family != AF_INET6)
2586                                         continue;
2587                                 t->flowlabel = params->spp_ipv6_flowlabel &
2588                                                SCTP_FLOWLABEL_VAL_MASK;
2589                                 t->flowlabel |= SCTP_FLOWLABEL_SET_MASK;
2590                         }
2591                         asoc->flowlabel = params->spp_ipv6_flowlabel &
2592                                           SCTP_FLOWLABEL_VAL_MASK;
2593                         asoc->flowlabel |= SCTP_FLOWLABEL_SET_MASK;
2594                 } else if (sctp_opt2sk(sp)->sk_family == AF_INET6) {
2595                         sp->flowlabel = params->spp_ipv6_flowlabel &
2596                                         SCTP_FLOWLABEL_VAL_MASK;
2597                         sp->flowlabel |= SCTP_FLOWLABEL_SET_MASK;
2598                 }
2599         }
2600 
2601         if (params->spp_flags & SPP_DSCP) {
2602                 if (trans) {
2603                         trans->dscp = params->spp_dscp & SCTP_DSCP_VAL_MASK;
2604                         trans->dscp |= SCTP_DSCP_SET_MASK;
2605                 } else if (asoc) {
2606                         struct sctp_transport *t;
2607 
2608                         list_for_each_entry(t, &asoc->peer.transport_addr_list,
2609                                             transports) {
2610                                 t->dscp = params->spp_dscp &
2611                                           SCTP_DSCP_VAL_MASK;
2612                                 t->dscp |= SCTP_DSCP_SET_MASK;
2613                         }
2614                         asoc->dscp = params->spp_dscp & SCTP_DSCP_VAL_MASK;
2615                         asoc->dscp |= SCTP_DSCP_SET_MASK;
2616                 } else {
2617                         sp->dscp = params->spp_dscp & SCTP_DSCP_VAL_MASK;
2618                         sp->dscp |= SCTP_DSCP_SET_MASK;
2619                 }
2620         }
2621 
2622         return 0;
2623 }
2624 
2625 static int sctp_setsockopt_peer_addr_params(struct sock *sk,
2626                                             char __user *optval,
2627                                             unsigned int optlen)
2628 {
2629         struct sctp_paddrparams  params;
2630         struct sctp_transport   *trans = NULL;
2631         struct sctp_association *asoc = NULL;
2632         struct sctp_sock        *sp = sctp_sk(sk);
2633         int error;
2634         int hb_change, pmtud_change, sackdelay_change;
2635 
2636         if (optlen == sizeof(params)) {
2637                 if (copy_from_user(¶ms, optval, optlen))
2638                         return -EFAULT;
2639         } else if (optlen == ALIGN(offsetof(struct sctp_paddrparams,
2640                                             spp_ipv6_flowlabel), 4)) {
2641                 if (copy_from_user(¶ms, optval, optlen))
2642                         return -EFAULT;
2643                 if (params.spp_flags & (SPP_DSCP | SPP_IPV6_FLOWLABEL))
2644                         return -EINVAL;
2645         } else {
2646                 return -EINVAL;
2647         }
2648 
2649         
2650         hb_change        = params.spp_flags & SPP_HB;
2651         pmtud_change     = params.spp_flags & SPP_PMTUD;
2652         sackdelay_change = params.spp_flags & SPP_SACKDELAY;
2653 
2654         if (hb_change        == SPP_HB ||
2655             pmtud_change     == SPP_PMTUD ||
2656             sackdelay_change == SPP_SACKDELAY ||
2657             params.spp_sackdelay > 500 ||
2658             (params.spp_pathmtu &&
2659              params.spp_pathmtu < SCTP_DEFAULT_MINSEGMENT))
2660                 return -EINVAL;
2661 
2662         
2663 
2664 
2665         if (!sctp_is_any(sk, (union sctp_addr *)¶ms.spp_address)) {
2666                 trans = sctp_addr_id2transport(sk, ¶ms.spp_address,
2667                                                params.spp_assoc_id);
2668                 if (!trans)
2669                         return -EINVAL;
2670         }
2671 
2672         
2673 
2674 
2675 
2676         asoc = sctp_id2assoc(sk, params.spp_assoc_id);
2677         if (!asoc && params.spp_assoc_id != SCTP_FUTURE_ASSOC &&
2678             sctp_style(sk, UDP))
2679                 return -EINVAL;
2680 
2681         
2682 
2683 
2684         if (params.spp_flags & SPP_HB_DEMAND && !trans && !asoc)
2685                 return -EINVAL;
2686 
2687         
2688         error = sctp_apply_peer_addr_params(¶ms, trans, asoc, sp,
2689                                             hb_change, pmtud_change,
2690                                             sackdelay_change);
2691 
2692         if (error)
2693                 return error;
2694 
2695         
2696 
2697 
2698         if (!trans && asoc) {
2699                 list_for_each_entry(trans, &asoc->peer.transport_addr_list,
2700                                 transports) {
2701                         sctp_apply_peer_addr_params(¶ms, trans, asoc, sp,
2702                                                     hb_change, pmtud_change,
2703                                                     sackdelay_change);
2704                 }
2705         }
2706 
2707         return 0;
2708 }
2709 
2710 static inline __u32 sctp_spp_sackdelay_enable(__u32 param_flags)
2711 {
2712         return (param_flags & ~SPP_SACKDELAY) | SPP_SACKDELAY_ENABLE;
2713 }
2714 
2715 static inline __u32 sctp_spp_sackdelay_disable(__u32 param_flags)
2716 {
2717         return (param_flags & ~SPP_SACKDELAY) | SPP_SACKDELAY_DISABLE;
2718 }
2719 
2720 static void sctp_apply_asoc_delayed_ack(struct sctp_sack_info *params,
2721                                         struct sctp_association *asoc)
2722 {
2723         struct sctp_transport *trans;
2724 
2725         if (params->sack_delay) {
2726                 asoc->sackdelay = msecs_to_jiffies(params->sack_delay);
2727                 asoc->param_flags =
2728                         sctp_spp_sackdelay_enable(asoc->param_flags);
2729         }
2730         if (params->sack_freq == 1) {
2731                 asoc->param_flags =
2732                         sctp_spp_sackdelay_disable(asoc->param_flags);
2733         } else if (params->sack_freq > 1) {
2734                 asoc->sackfreq = params->sack_freq;
2735                 asoc->param_flags =
2736                         sctp_spp_sackdelay_enable(asoc->param_flags);
2737         }
2738 
2739         list_for_each_entry(trans, &asoc->peer.transport_addr_list,
2740                             transports) {
2741                 if (params->sack_delay) {
2742                         trans->sackdelay = msecs_to_jiffies(params->sack_delay);
2743                         trans->param_flags =
2744                                 sctp_spp_sackdelay_enable(trans->param_flags);
2745                 }
2746                 if (params->sack_freq == 1) {
2747                         trans->param_flags =
2748                                 sctp_spp_sackdelay_disable(trans->param_flags);
2749                 } else if (params->sack_freq > 1) {
2750                         trans->sackfreq = params->sack_freq;
2751                         trans->param_flags =
2752                                 sctp_spp_sackdelay_enable(trans->param_flags);
2753                 }
2754         }
2755 }
2756 
2757 
2758 
2759 
2760 
2761 
2762 
2763 
2764 
2765 
2766 
2767 
2768 
2769 
2770 
2771 
2772 
2773 
2774 
2775 
2776 
2777 
2778 
2779 
2780 
2781 
2782 
2783 
2784 
2785 
2786 
2787 
2788 
2789 
2790 
2791 
2792 
2793 static int sctp_setsockopt_delayed_ack(struct sock *sk,
2794                                        char __user *optval, unsigned int optlen)
2795 {
2796         struct sctp_sock *sp = sctp_sk(sk);
2797         struct sctp_association *asoc;
2798         struct sctp_sack_info params;
2799 
2800         if (optlen == sizeof(struct sctp_sack_info)) {
2801                 if (copy_from_user(¶ms, optval, optlen))
2802                         return -EFAULT;
2803 
2804                 if (params.sack_delay == 0 && params.sack_freq == 0)
2805                         return 0;
2806         } else if (optlen == sizeof(struct sctp_assoc_value)) {
2807                 pr_warn_ratelimited(DEPRECATED
2808                                     "%s (pid %d) "
2809                                     "Use of struct sctp_assoc_value in delayed_ack socket option.\n"
2810                                     "Use struct sctp_sack_info instead\n",
2811                                     current->comm, task_pid_nr(current));
2812                 if (copy_from_user(¶ms, optval, optlen))
2813                         return -EFAULT;
2814 
2815                 if (params.sack_delay == 0)
2816                         params.sack_freq = 1;
2817                 else
2818                         params.sack_freq = 0;
2819         } else
2820                 return -EINVAL;
2821 
2822         
2823         if (params.sack_delay > 500)
2824                 return -EINVAL;
2825 
2826         
2827 
2828 
2829 
2830         asoc = sctp_id2assoc(sk, params.sack_assoc_id);
2831         if (!asoc && params.sack_assoc_id > SCTP_ALL_ASSOC &&
2832             sctp_style(sk, UDP))
2833                 return -EINVAL;
2834 
2835         if (asoc) {
2836                 sctp_apply_asoc_delayed_ack(¶ms, asoc);
2837 
2838                 return 0;
2839         }
2840 
2841         if (sctp_style(sk, TCP))
2842                 params.sack_assoc_id = SCTP_FUTURE_ASSOC;
2843 
2844         if (params.sack_assoc_id == SCTP_FUTURE_ASSOC ||
2845             params.sack_assoc_id == SCTP_ALL_ASSOC) {
2846                 if (params.sack_delay) {
2847                         sp->sackdelay = params.sack_delay;
2848                         sp->param_flags =
2849                                 sctp_spp_sackdelay_enable(sp->param_flags);
2850                 }
2851                 if (params.sack_freq == 1) {
2852                         sp->param_flags =
2853                                 sctp_spp_sackdelay_disable(sp->param_flags);
2854                 } else if (params.sack_freq > 1) {
2855                         sp->sackfreq = params.sack_freq;
2856                         sp->param_flags =
2857                                 sctp_spp_sackdelay_enable(sp->param_flags);
2858                 }
2859         }
2860 
2861         if (params.sack_assoc_id == SCTP_CURRENT_ASSOC ||
2862             params.sack_assoc_id == SCTP_ALL_ASSOC)
2863                 list_for_each_entry(asoc, &sp->ep->asocs, asocs)
2864                         sctp_apply_asoc_delayed_ack(¶ms, asoc);
2865 
2866         return 0;
2867 }
2868 
2869 
2870 
2871 
2872 
2873 
2874 
2875 
2876 
2877 
2878 
2879 
2880 static int sctp_setsockopt_initmsg(struct sock *sk, char __user *optval, unsigned int optlen)
2881 {
2882         struct sctp_initmsg sinit;
2883         struct sctp_sock *sp = sctp_sk(sk);
2884 
2885         if (optlen != sizeof(struct sctp_initmsg))
2886                 return -EINVAL;
2887         if (copy_from_user(&sinit, optval, optlen))
2888                 return -EFAULT;
2889 
2890         if (sinit.sinit_num_ostreams)
2891                 sp->initmsg.sinit_num_ostreams = sinit.sinit_num_ostreams;
2892         if (sinit.sinit_max_instreams)
2893                 sp->initmsg.sinit_max_instreams = sinit.sinit_max_instreams;
2894         if (sinit.sinit_max_attempts)
2895                 sp->initmsg.sinit_max_attempts = sinit.sinit_max_attempts;
2896         if (sinit.sinit_max_init_timeo)
2897                 sp->initmsg.sinit_max_init_timeo = sinit.sinit_max_init_timeo;
2898 
2899         return 0;
2900 }
2901 
2902 
2903 
2904 
2905 
2906 
2907 
2908 
2909 
2910 
2911 
2912 
2913 
2914 
2915 
2916 static int sctp_setsockopt_default_send_param(struct sock *sk,
2917                                               char __user *optval,
2918                                               unsigned int optlen)
2919 {
2920         struct sctp_sock *sp = sctp_sk(sk);
2921         struct sctp_association *asoc;
2922         struct sctp_sndrcvinfo info;
2923 
2924         if (optlen != sizeof(info))
2925                 return -EINVAL;
2926         if (copy_from_user(&info, optval, optlen))
2927                 return -EFAULT;
2928         if (info.sinfo_flags &
2929             ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
2930               SCTP_ABORT | SCTP_EOF))
2931                 return -EINVAL;
2932 
2933         asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
2934         if (!asoc && info.sinfo_assoc_id > SCTP_ALL_ASSOC &&
2935             sctp_style(sk, UDP))
2936                 return -EINVAL;
2937 
2938         if (asoc) {
2939                 asoc->default_stream = info.sinfo_stream;
2940                 asoc->default_flags = info.sinfo_flags;
2941                 asoc->default_ppid = info.sinfo_ppid;
2942                 asoc->default_context = info.sinfo_context;
2943                 asoc->default_timetolive = info.sinfo_timetolive;
2944 
2945                 return 0;
2946         }
2947 
2948         if (sctp_style(sk, TCP))
2949                 info.sinfo_assoc_id = SCTP_FUTURE_ASSOC;
2950 
2951         if (info.sinfo_assoc_id == SCTP_FUTURE_ASSOC ||
2952             info.sinfo_assoc_id == SCTP_ALL_ASSOC) {
2953                 sp->default_stream = info.sinfo_stream;
2954                 sp->default_flags = info.sinfo_flags;
2955                 sp->default_ppid = info.sinfo_ppid;
2956                 sp->default_context = info.sinfo_context;
2957                 sp->default_timetolive = info.sinfo_timetolive;
2958         }
2959 
2960         if (info.sinfo_assoc_id == SCTP_CURRENT_ASSOC ||
2961             info.sinfo_assoc_id == SCTP_ALL_ASSOC) {
2962                 list_for_each_entry(asoc, &sp->ep->asocs, asocs) {
2963                         asoc->default_stream = info.sinfo_stream;
2964                         asoc->default_flags = info.sinfo_flags;
2965                         asoc->default_ppid = info.sinfo_ppid;
2966                         asoc->default_context = info.sinfo_context;
2967                         asoc->default_timetolive = info.sinfo_timetolive;
2968                 }
2969         }
2970 
2971         return 0;
2972 }
2973 
2974 
2975 
2976 
2977 static int sctp_setsockopt_default_sndinfo(struct sock *sk,
2978                                            char __user *optval,
2979                                            unsigned int optlen)
2980 {
2981         struct sctp_sock *sp = sctp_sk(sk);
2982         struct sctp_association *asoc;
2983         struct sctp_sndinfo info;
2984 
2985         if (optlen != sizeof(info))
2986                 return -EINVAL;
2987         if (copy_from_user(&info, optval, optlen))
2988                 return -EFAULT;
2989         if (info.snd_flags &
2990             ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
2991               SCTP_ABORT | SCTP_EOF))
2992                 return -EINVAL;
2993 
2994         asoc = sctp_id2assoc(sk, info.snd_assoc_id);
2995         if (!asoc && info.snd_assoc_id > SCTP_ALL_ASSOC &&
2996             sctp_style(sk, UDP))
2997                 return -EINVAL;
2998 
2999         if (asoc) {
3000                 asoc->default_stream = info.snd_sid;
3001                 asoc->default_flags = info.snd_flags;
3002                 asoc->default_ppid = info.snd_ppid;
3003                 asoc->default_context = info.snd_context;
3004 
3005                 return 0;
3006         }
3007 
3008         if (sctp_style(sk, TCP))
3009                 info.snd_assoc_id = SCTP_FUTURE_ASSOC;
3010 
3011         if (info.snd_assoc_id == SCTP_FUTURE_ASSOC ||
3012             info.snd_assoc_id == SCTP_ALL_ASSOC) {
3013                 sp->default_stream = info.snd_sid;
3014                 sp->default_flags = info.snd_flags;
3015                 sp->default_ppid = info.snd_ppid;
3016                 sp->default_context = info.snd_context;
3017         }
3018 
3019         if (info.snd_assoc_id == SCTP_CURRENT_ASSOC ||
3020             info.snd_assoc_id == SCTP_ALL_ASSOC) {
3021                 list_for_each_entry(asoc, &sp->ep->asocs, asocs) {
3022                         asoc->default_stream = info.snd_sid;
3023                         asoc->default_flags = info.snd_flags;
3024                         asoc->default_ppid = info.snd_ppid;
3025                         asoc->default_context = info.snd_context;
3026                 }
3027         }
3028 
3029         return 0;
3030 }
3031 
3032 
3033 
3034 
3035 
3036 
3037 
3038 static int sctp_setsockopt_primary_addr(struct sock *sk, char __user *optval,
3039                                         unsigned int optlen)
3040 {
3041         struct sctp_prim prim;
3042         struct sctp_transport *trans;
3043         struct sctp_af *af;
3044         int err;
3045 
3046         if (optlen != sizeof(struct sctp_prim))
3047                 return -EINVAL;
3048 
3049         if (copy_from_user(&prim, optval, sizeof(struct sctp_prim)))
3050                 return -EFAULT;
3051 
3052         
3053         af = sctp_get_af_specific(prim.ssp_addr.ss_family);
3054         if (!af)
3055                 return -EINVAL;
3056 
3057         err = security_sctp_bind_connect(sk, SCTP_PRIMARY_ADDR,
3058                                          (struct sockaddr *)&prim.ssp_addr,
3059                                          af->sockaddr_len);
3060         if (err)
3061                 return err;
3062 
3063         trans = sctp_addr_id2transport(sk, &prim.ssp_addr, prim.ssp_assoc_id);
3064         if (!trans)
3065                 return -EINVAL;
3066 
3067         sctp_assoc_set_primary(trans->asoc, trans);
3068 
3069         return 0;
3070 }
3071 
3072 
3073 
3074 
3075 
3076 
3077 
3078 
3079 
3080 static int sctp_setsockopt_nodelay(struct sock *sk, char __user *optval,
3081                                    unsigned int optlen)
3082 {
3083         int val;
3084 
3085         if (optlen < sizeof(int))
3086                 return -EINVAL;
3087         if (get_user(val, (int __user *)optval))
3088                 return -EFAULT;
3089 
3090         sctp_sk(sk)->nodelay = (val == 0) ? 0 : 1;
3091         return 0;
3092 }
3093 
3094 
3095 
3096 
3097 
3098 
3099 
3100 
3101 
3102 
3103 
3104 
3105 
3106 static int sctp_setsockopt_rtoinfo(struct sock *sk, char __user *optval, unsigned int optlen)
3107 {
3108         struct sctp_rtoinfo rtoinfo;
3109         struct sctp_association *asoc;
3110         unsigned long rto_min, rto_max;
3111         struct sctp_sock *sp = sctp_sk(sk);
3112 
3113         if (optlen != sizeof (struct sctp_rtoinfo))
3114                 return -EINVAL;
3115 
3116         if (copy_from_user(&rtoinfo, optval, optlen))
3117                 return -EFAULT;
3118 
3119         asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
3120 
3121         
3122         if (!asoc && rtoinfo.srto_assoc_id != SCTP_FUTURE_ASSOC &&
3123             sctp_style(sk, UDP))
3124                 return -EINVAL;
3125 
3126         rto_max = rtoinfo.srto_max;
3127         rto_min = rtoinfo.srto_min;
3128 
3129         if (rto_max)
3130                 rto_max = asoc ? msecs_to_jiffies(rto_max) : rto_max;
3131         else
3132                 rto_max = asoc ? asoc->rto_max : sp->rtoinfo.srto_max;
3133 
3134         if (rto_min)
3135                 rto_min = asoc ? msecs_to_jiffies(rto_min) : rto_min;
3136         else
3137                 rto_min = asoc ? asoc->rto_min : sp->rtoinfo.srto_min;
3138 
3139         if (rto_min > rto_max)
3140                 return -EINVAL;
3141 
3142         if (asoc) {
3143                 if (rtoinfo.srto_initial != 0)
3144                         asoc->rto_initial =
3145                                 msecs_to_jiffies(rtoinfo.srto_initial);
3146                 asoc->rto_max = rto_max;
3147                 asoc->rto_min = rto_min;
3148         } else {
3149                 
3150 
3151 
3152                 if (rtoinfo.srto_initial != 0)
3153                         sp->rtoinfo.srto_initial = rtoinfo.srto_initial;
3154                 sp->rtoinfo.srto_max = rto_max;
3155                 sp->rtoinfo.srto_min = rto_min;
3156         }
3157 
3158         return 0;
3159 }
3160 
3161 
3162 
3163 
3164 
3165 
3166 
3167 
3168 
3169 
3170 
3171 
3172 static int sctp_setsockopt_associnfo(struct sock *sk, char __user *optval, unsigned int optlen)
3173 {
3174 
3175         struct sctp_assocparams assocparams;
3176         struct sctp_association *asoc;
3177 
3178         if (optlen != sizeof(struct sctp_assocparams))
3179                 return -EINVAL;
3180         if (copy_from_user(&assocparams, optval, optlen))
3181                 return -EFAULT;
3182 
3183         asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
3184 
3185         if (!asoc && assocparams.sasoc_assoc_id != SCTP_FUTURE_ASSOC &&
3186             sctp_style(sk, UDP))
3187                 return -EINVAL;
3188 
3189         
3190         if (asoc) {
3191                 if (assocparams.sasoc_asocmaxrxt != 0) {
3192                         __u32 path_sum = 0;
3193                         int   paths = 0;
3194                         struct sctp_transport *peer_addr;
3195 
3196                         list_for_each_entry(peer_addr, &asoc->peer.transport_addr_list,
3197                                         transports) {
3198                                 path_sum += peer_addr->pathmaxrxt;
3199                                 paths++;
3200                         }
3201 
3202                         
3203 
3204 
3205 
3206 
3207                         if (paths > 1 &&
3208                             assocparams.sasoc_asocmaxrxt > path_sum)
3209                                 return -EINVAL;
3210 
3211                         asoc->max_retrans = assocparams.sasoc_asocmaxrxt;
3212                 }
3213 
3214                 if (assocparams.sasoc_cookie_life != 0)
3215                         asoc->cookie_life = ms_to_ktime(assocparams.sasoc_cookie_life);
3216         } else {
3217                 
3218                 struct sctp_sock *sp = sctp_sk(sk);
3219 
3220                 if (assocparams.sasoc_asocmaxrxt != 0)
3221                         sp->assocparams.sasoc_asocmaxrxt =
3222                                                 assocparams.sasoc_asocmaxrxt;
3223                 if (assocparams.sasoc_cookie_life != 0)
3224                         sp->assocparams.sasoc_cookie_life =
3225                                                 assocparams.sasoc_cookie_life;
3226         }
3227         return 0;
3228 }
3229 
3230 
3231 
3232 
3233 
3234 
3235 
3236 
3237 
3238 
3239 
3240 static int sctp_setsockopt_mappedv4(struct sock *sk, char __user *optval, unsigned int optlen)
3241 {
3242         int val;
3243         struct sctp_sock *sp = sctp_sk(sk);
3244 
3245         if (optlen < sizeof(int))
3246                 return -EINVAL;
3247         if (get_user(val, (int __user *)optval))
3248                 return -EFAULT;
3249         if (val)
3250                 sp->v4mapped = 1;
3251         else
3252                 sp->v4mapped = 0;
3253 
3254         return 0;
3255 }
3256 
3257 
3258 
3259 
3260 
3261 
3262 
3263 
3264 
3265 
3266 
3267 
3268 
3269 
3270 
3271 
3272 
3273 
3274 
3275 
3276 
3277 
3278 
3279 
3280 
3281 
3282 
3283 
3284 static int sctp_setsockopt_maxseg(struct sock *sk, char __user *optval, unsigned int optlen)
3285 {
3286         struct sctp_sock *sp = sctp_sk(sk);
3287         struct sctp_assoc_value params;
3288         struct sctp_association *asoc;
3289         int val;
3290 
3291         if (optlen == sizeof(int)) {
3292                 pr_warn_ratelimited(DEPRECATED
3293                                     "%s (pid %d) "
3294                                     "Use of int in maxseg socket option.\n"
3295                                     "Use struct sctp_assoc_value instead\n",
3296                                     current->comm, task_pid_nr(current));
3297                 if (copy_from_user(&val, optval, optlen))
3298                         return -EFAULT;
3299                 params.assoc_id = SCTP_FUTURE_ASSOC;
3300         } else if (optlen == sizeof(struct sctp_assoc_value)) {
3301                 if (copy_from_user(¶ms, optval, optlen))
3302                         return -EFAULT;
3303                 val = params.assoc_value;
3304         } else {
3305                 return -EINVAL;
3306         }
3307 
3308         asoc = sctp_id2assoc(sk, params.assoc_id);
3309         if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
3310             sctp_style(sk, UDP))
3311                 return -EINVAL;
3312 
3313         if (val) {
3314                 int min_len, max_len;
3315                 __u16 datasize = asoc ? sctp_datachk_len(&asoc->stream) :
3316                                  sizeof(struct sctp_data_chunk);
3317 
3318                 min_len = sctp_min_frag_point(sp, datasize);
3319                 max_len = SCTP_MAX_CHUNK_LEN - datasize;
3320 
3321                 if (val < min_len || val > max_len)
3322                         return -EINVAL;
3323         }
3324 
3325         if (asoc) {
3326                 asoc->user_frag = val;
3327                 sctp_assoc_update_frag_point(asoc);
3328         } else {
3329                 sp->user_frag = val;
3330         }
3331 
3332         return 0;
3333 }
3334 
3335 
3336 
3337 
3338 
3339 
3340 
3341 
3342 
3343 
3344 static int sctp_setsockopt_peer_primary_addr(struct sock *sk, char __user *optval,
3345                                              unsigned int optlen)
3346 {
3347         struct sctp_sock        *sp;
3348         struct sctp_association *asoc = NULL;
3349         struct sctp_setpeerprim prim;
3350         struct sctp_chunk       *chunk;
3351         struct sctp_af          *af;
3352         int                     err;
3353 
3354         sp = sctp_sk(sk);
3355 
3356         if (!sp->ep->asconf_enable)
3357                 return -EPERM;
3358 
3359         if (optlen != sizeof(struct sctp_setpeerprim))
3360                 return -EINVAL;
3361 
3362         if (copy_from_user(&prim, optval, optlen))
3363                 return -EFAULT;
3364 
3365         asoc = sctp_id2assoc(sk, prim.sspp_assoc_id);
3366         if (!asoc)
3367                 return -EINVAL;
3368 
3369         if (!asoc->peer.asconf_capable)
3370                 return -EPERM;
3371 
3372         if (asoc->peer.addip_disabled_mask & SCTP_PARAM_SET_PRIMARY)
3373                 return -EPERM;
3374 
3375         if (!sctp_state(asoc, ESTABLISHED))
3376                 return -ENOTCONN;
3377 
3378         af = sctp_get_af_specific(prim.sspp_addr.ss_family);
3379         if (!af)
3380                 return -EINVAL;
3381 
3382         if (!af->addr_valid((union sctp_addr *)&prim.sspp_addr, sp, NULL))
3383                 return -EADDRNOTAVAIL;
3384 
3385         if (!sctp_assoc_lookup_laddr(asoc, (union sctp_addr *)&prim.sspp_addr))
3386                 return -EADDRNOTAVAIL;
3387 
3388         
3389         err = security_sctp_bind_connect(sk, SCTP_SET_PEER_PRIMARY_ADDR,
3390                                          (struct sockaddr *)&prim.sspp_addr,
3391                                          af->sockaddr_len);
3392         if (err)
3393                 return err;
3394 
3395         
3396         chunk = sctp_make_asconf_set_prim(asoc,
3397                                           (union sctp_addr *)&prim.sspp_addr);
3398         if (!chunk)
3399                 return -ENOMEM;
3400 
3401         err = sctp_send_asconf(asoc, chunk);
3402 
3403         pr_debug("%s: we set peer primary addr primitively\n", __func__);
3404 
3405         return err;
3406 }
3407 
3408 static int sctp_setsockopt_adaptation_layer(struct sock *sk, char __user *optval,
3409                                             unsigned int optlen)
3410 {
3411         struct sctp_setadaptation adaptation;
3412 
3413         if (optlen != sizeof(struct sctp_setadaptation))
3414                 return -EINVAL;
3415         if (copy_from_user(&adaptation, optval, optlen))
3416                 return -EFAULT;
3417 
3418         sctp_sk(sk)->adaptation_ind = adaptation.ssb_adaptation_ind;
3419 
3420         return 0;
3421 }
3422 
3423 
3424 
3425 
3426 
3427 
3428 
3429 
3430 
3431 
3432 
3433 
3434 
3435 
3436 
3437 static int sctp_setsockopt_context(struct sock *sk, char __user *optval,
3438                                    unsigned int optlen)
3439 {
3440         struct sctp_sock *sp = sctp_sk(sk);
3441         struct sctp_assoc_value params;
3442         struct sctp_association *asoc;
3443 
3444         if (optlen != sizeof(struct sctp_assoc_value))
3445                 return -EINVAL;
3446         if (copy_from_user(¶ms, optval, optlen))
3447                 return -EFAULT;
3448 
3449         asoc = sctp_id2assoc(sk, params.assoc_id);
3450         if (!asoc && params.assoc_id > SCTP_ALL_ASSOC &&
3451             sctp_style(sk, UDP))
3452                 return -EINVAL;
3453 
3454         if (asoc) {
3455                 asoc->default_rcv_context = params.assoc_value;
3456 
3457                 return 0;
3458         }
3459 
3460         if (sctp_style(sk, TCP))
3461                 params.assoc_id = SCTP_FUTURE_ASSOC;
3462 
3463         if (params.assoc_id == SCTP_FUTURE_ASSOC ||
3464             params.assoc_id == SCTP_ALL_ASSOC)
3465                 sp->default_rcv_context = params.assoc_value;
3466 
3467         if (params.assoc_id == SCTP_CURRENT_ASSOC ||
3468             params.assoc_id == SCTP_ALL_ASSOC)
3469                 list_for_each_entry(asoc, &sp->ep->asocs, asocs)
3470                         asoc->default_rcv_context = params.assoc_value;
3471 
3472         return 0;
3473 }
3474 
3475 
3476 
3477 
3478 
3479 
3480 
3481 
3482 
3483 
3484 
3485 
3486 
3487 
3488 
3489 
3490 
3491 
3492 
3493 
3494 
3495 
3496 
3497 
3498 
3499 static int sctp_setsockopt_fragment_interleave(struct sock *sk,
3500                                                char __user *optval,
3501                                                unsigned int optlen)
3502 {
3503         int val;
3504 
3505         if (optlen != sizeof(int))
3506                 return -EINVAL;
3507         if (get_user(val, (int __user *)optval))
3508                 return -EFAULT;
3509 
3510         sctp_sk(sk)->frag_interleave = !!val;
3511 
3512         if (!sctp_sk(sk)->frag_interleave)
3513                 sctp_sk(sk)->ep->intl_enable = 0;
3514 
3515         return 0;
3516 }
3517 
3518 
3519 
3520 
3521 
3522 
3523 
3524 
3525 
3526 
3527 
3528 
3529 
3530 
3531 
3532 
3533 
3534 
3535 static int sctp_setsockopt_partial_delivery_point(struct sock *sk,
3536                                                   char __user *optval,
3537                                                   unsigned int optlen)
3538 {
3539         u32 val;
3540 
3541         if (optlen != sizeof(u32))
3542                 return -EINVAL;
3543         if (get_user(val, (int __user *)optval))
3544                 return -EFAULT;
3545 
3546         
3547 
3548 
3549         if (val > (sk->sk_rcvbuf >> 1))
3550                 return -EINVAL;
3551 
3552         sctp_sk(sk)->pd_point = val;
3553 
3554         return 0; 
3555 }
3556 
3557 
3558 
3559 
3560 
3561 
3562 
3563 
3564 
3565 
3566 
3567 
3568 static int sctp_setsockopt_maxburst(struct sock *sk,
3569                                     char __user *optval,
3570                                     unsigned int optlen)
3571 {
3572         struct sctp_sock *sp = sctp_sk(sk);
3573         struct sctp_assoc_value params;
3574         struct sctp_association *asoc;
3575 
3576         if (optlen == sizeof(int)) {
3577                 pr_warn_ratelimited(DEPRECATED
3578                                     "%s (pid %d) "
3579                                     "Use of int in max_burst socket option deprecated.\n"
3580                                     "Use struct sctp_assoc_value instead\n",
3581                                     current->comm, task_pid_nr(current));
3582                 if (copy_from_user(¶ms.assoc_value, optval, optlen))
3583                         return -EFAULT;
3584                 params.assoc_id = SCTP_FUTURE_ASSOC;
3585         } else if (optlen == sizeof(struct sctp_assoc_value)) {
3586                 if (copy_from_user(¶ms, optval, optlen))
3587                         return -EFAULT;
3588         } else
3589                 return -EINVAL;
3590 
3591         asoc = sctp_id2assoc(sk, params.assoc_id);
3592         if (!asoc && params.assoc_id > SCTP_ALL_ASSOC &&
3593             sctp_style(sk, UDP))
3594                 return -EINVAL;
3595 
3596         if (asoc) {
3597                 asoc->max_burst = params.assoc_value;
3598 
3599                 return 0;
3600         }
3601 
3602         if (sctp_style(sk, TCP))
3603                 params.assoc_id = SCTP_FUTURE_ASSOC;
3604 
3605         if (params.assoc_id == SCTP_FUTURE_ASSOC ||
3606             params.assoc_id == SCTP_ALL_ASSOC)
3607                 sp->max_burst = params.assoc_value;
3608 
3609         if (params.assoc_id == SCTP_CURRENT_ASSOC ||
3610             params.assoc_id == SCTP_ALL_ASSOC)
3611                 list_for_each_entry(asoc, &sp->ep->asocs, asocs)
3612                         asoc->max_burst = params.assoc_value;
3613 
3614         return 0;
3615 }
3616 
3617 
3618 
3619 
3620 
3621 
3622 
3623 
3624 static int sctp_setsockopt_auth_chunk(struct sock *sk,
3625                                       char __user *optval,
3626                                       unsigned int optlen)
3627 {
3628         struct sctp_endpoint *ep = sctp_sk(sk)->ep;
3629         struct sctp_authchunk val;
3630 
3631         if (!ep->auth_enable)
3632                 return -EACCES;
3633 
3634         if (optlen != sizeof(struct sctp_authchunk))
3635                 return -EINVAL;
3636         if (copy_from_user(&val, optval, optlen))
3637                 return -EFAULT;
3638 
3639         switch (val.sauth_chunk) {
3640         case SCTP_CID_INIT:
3641         case SCTP_CID_INIT_ACK:
3642         case SCTP_CID_SHUTDOWN_COMPLETE:
3643         case SCTP_CID_AUTH:
3644                 return -EINVAL;
3645         }
3646 
3647         
3648         return sctp_auth_ep_add_chunkid(ep, val.sauth_chunk);
3649 }
3650 
3651 
3652 
3653 
3654 
3655 
3656 
3657 static int sctp_setsockopt_hmac_ident(struct sock *sk,
3658                                       char __user *optval,
3659                                       unsigned int optlen)
3660 {
3661         struct sctp_endpoint *ep = sctp_sk(sk)->ep;
3662         struct sctp_hmacalgo *hmacs;
3663         u32 idents;
3664         int err;
3665 
3666         if (!ep->auth_enable)
3667                 return -EACCES;
3668 
3669         if (optlen < sizeof(struct sctp_hmacalgo))
3670                 return -EINVAL;
3671         optlen = min_t(unsigned int, optlen, sizeof(struct sctp_hmacalgo) +
3672                                              SCTP_AUTH_NUM_HMACS * sizeof(u16));
3673 
3674         hmacs = memdup_user(optval, optlen);
3675         if (IS_ERR(hmacs))
3676                 return PTR_ERR(hmacs);
3677 
3678         idents = hmacs->shmac_num_idents;
3679         if (idents == 0 || idents > SCTP_AUTH_NUM_HMACS ||
3680             (idents * sizeof(u16)) > (optlen - sizeof(struct sctp_hmacalgo))) {
3681                 err = -EINVAL;
3682                 goto out;
3683         }
3684 
3685         err = sctp_auth_ep_set_hmacs(ep, hmacs);
3686 out:
3687         kfree(hmacs);
3688         return err;
3689 }
3690 
3691 
3692 
3693 
3694 
3695 
3696 
3697 static int sctp_setsockopt_auth_key(struct sock *sk,
3698                                     char __user *optval,
3699                                     unsigned int optlen)
3700 {
3701         struct sctp_endpoint *ep = sctp_sk(sk)->ep;
3702         struct sctp_authkey *authkey;
3703         struct sctp_association *asoc;
3704         int ret = -EINVAL;
3705 
3706         if (optlen <= sizeof(struct sctp_authkey))
3707                 return -EINVAL;
3708         
3709 
3710 
3711         optlen = min_t(unsigned int, optlen, USHRT_MAX + sizeof(*authkey));
3712 
3713         authkey = memdup_user(optval, optlen);
3714         if (IS_ERR(authkey))
3715                 return PTR_ERR(authkey);
3716 
3717         if (authkey->sca_keylength > optlen - sizeof(*authkey))
3718                 goto out;
3719 
3720         asoc = sctp_id2assoc(sk, authkey->sca_assoc_id);
3721         if (!asoc && authkey->sca_assoc_id > SCTP_ALL_ASSOC &&
3722             sctp_style(sk, UDP))
3723                 goto out;
3724 
3725         if (asoc) {
3726                 ret = sctp_auth_set_key(ep, asoc, authkey);
3727                 goto out;
3728         }
3729 
3730         if (sctp_style(sk, TCP))
3731                 authkey->sca_assoc_id = SCTP_FUTURE_ASSOC;
3732 
3733         if (authkey->sca_assoc_id == SCTP_FUTURE_ASSOC ||
3734             authkey->sca_assoc_id == SCTP_ALL_ASSOC) {
3735                 ret = sctp_auth_set_key(ep, asoc, authkey);
3736                 if (ret)
3737                         goto out;
3738         }
3739 
3740         ret = 0;
3741 
3742         if (authkey->sca_assoc_id == SCTP_CURRENT_ASSOC ||
3743             authkey->sca_assoc_id == SCTP_ALL_ASSOC) {
3744                 list_for_each_entry(asoc, &ep->asocs, asocs) {
3745                         int res = sctp_auth_set_key(ep, asoc, authkey);
3746 
3747                         if (res && !ret)
3748                                 ret = res;
3749                 }
3750         }
3751 
3752 out:
3753         kzfree(authkey);
3754         return ret;
3755 }
3756 
3757 
3758 
3759 
3760 
3761 
3762 
3763 static int sctp_setsockopt_active_key(struct sock *sk,
3764                                       char __user *optval,
3765                                       unsigned int optlen)
3766 {
3767         struct sctp_endpoint *ep = sctp_sk(sk)->ep;
3768         struct sctp_association *asoc;
3769         struct sctp_authkeyid val;
3770         int ret = 0;
3771 
3772         if (optlen != sizeof(struct sctp_authkeyid))
3773                 return -EINVAL;
3774         if (copy_from_user(&val, optval, optlen))
3775                 return -EFAULT;
3776 
3777         asoc = sctp_id2assoc(sk, val.scact_assoc_id);
3778         if (!asoc && val.scact_assoc_id > SCTP_ALL_ASSOC &&
3779             sctp_style(sk, UDP))
3780                 return -EINVAL;
3781 
3782         if (asoc)
3783                 return sctp_auth_set_active_key(ep, asoc, val.scact_keynumber);
3784 
3785         if (sctp_style(sk, TCP))
3786                 val.scact_assoc_id = SCTP_FUTURE_ASSOC;
3787 
3788         if (val.scact_assoc_id == SCTP_FUTURE_ASSOC ||
3789             val.scact_assoc_id == SCTP_ALL_ASSOC) {
3790                 ret = sctp_auth_set_active_key(ep, asoc, val.scact_keynumber);
3791                 if (ret)
3792                         return ret;
3793         }
3794 
3795         if (val.scact_assoc_id == SCTP_CURRENT_ASSOC ||
3796             val.scact_assoc_id == SCTP_ALL_ASSOC) {
3797                 list_for_each_entry(asoc, &ep->asocs, asocs) {
3798                         int res = sctp_auth_set_active_key(ep, asoc,
3799                                                            val.scact_keynumber);
3800 
3801                         if (res && !ret)
3802                                 ret = res;
3803                 }
3804         }
3805 
3806         return ret;
3807 }
3808 
3809 
3810 
3811 
3812 
3813 
3814 static int sctp_setsockopt_del_key(struct sock *sk,
3815                                    char __user *optval,
3816                                    unsigned int optlen)
3817 {
3818         struct sctp_endpoint *ep = sctp_sk(sk)->ep;
3819         struct sctp_association *asoc;
3820         struct sctp_authkeyid val;
3821         int ret = 0;
3822 
3823         if (optlen != sizeof(struct sctp_authkeyid))
3824                 return -EINVAL;
3825         if (copy_from_user(&val, optval, optlen))
3826                 return -EFAULT;
3827 
3828         asoc = sctp_id2assoc(sk, val.scact_assoc_id);
3829         if (!asoc && val.scact_assoc_id > SCTP_ALL_ASSOC &&
3830             sctp_style(sk, UDP))
3831                 return -EINVAL;
3832 
3833         if (asoc)
3834                 return sctp_auth_del_key_id(ep, asoc, val.scact_keynumber);
3835 
3836         if (sctp_style(sk, TCP))
3837                 val.scact_assoc_id = SCTP_FUTURE_ASSOC;
3838 
3839         if (val.scact_assoc_id == SCTP_FUTURE_ASSOC ||
3840             val.scact_assoc_id == SCTP_ALL_ASSOC) {
3841                 ret = sctp_auth_del_key_id(ep, asoc, val.scact_keynumber);
3842                 if (ret)
3843                         return ret;
3844         }
3845 
3846         if (val.scact_assoc_id == SCTP_CURRENT_ASSOC ||
3847             val.scact_assoc_id == SCTP_ALL_ASSOC) {
3848                 list_for_each_entry(asoc, &ep->asocs, asocs) {
3849                         int res = sctp_auth_del_key_id(ep, asoc,
3850                                                        val.scact_keynumber);
3851 
3852                         if (res && !ret)
3853                                 ret = res;
3854                 }
3855         }
3856 
3857         return ret;
3858 }
3859 
3860 
3861 
3862 
3863 
3864 
3865 static int sctp_setsockopt_deactivate_key(struct sock *sk, char __user *optval,
3866                                           unsigned int optlen)
3867 {
3868         struct sctp_endpoint *ep = sctp_sk(sk)->ep;
3869         struct sctp_association *asoc;
3870         struct sctp_authkeyid val;
3871         int ret = 0;
3872 
3873         if (optlen != sizeof(struct sctp_authkeyid))
3874                 return -EINVAL;
3875         if (copy_from_user(&val, optval, optlen))
3876                 return -EFAULT;
3877 
3878         asoc = sctp_id2assoc(sk, val.scact_assoc_id);
3879         if (!asoc && val.scact_assoc_id > SCTP_ALL_ASSOC &&
3880             sctp_style(sk, UDP))
3881                 return -EINVAL;
3882 
3883         if (asoc)
3884                 return sctp_auth_deact_key_id(ep, asoc, val.scact_keynumber);
3885 
3886         if (sctp_style(sk, TCP))
3887                 val.scact_assoc_id = SCTP_FUTURE_ASSOC;
3888 
3889         if (val.scact_assoc_id == SCTP_FUTURE_ASSOC ||
3890             val.scact_assoc_id == SCTP_ALL_ASSOC) {
3891                 ret = sctp_auth_deact_key_id(ep, asoc, val.scact_keynumber);
3892                 if (ret)
3893                         return ret;
3894         }
3895 
3896         if (val.scact_assoc_id == SCTP_CURRENT_ASSOC ||
3897             val.scact_assoc_id == SCTP_ALL_ASSOC) {
3898                 list_for_each_entry(asoc, &ep->asocs, asocs) {
3899                         int res = sctp_auth_deact_key_id(ep, asoc,
3900                                                          val.scact_keynumber);
3901 
3902                         if (res && !ret)
3903                                 ret = res;
3904                 }
3905         }
3906 
3907         return ret;
3908 }
3909 
3910 
3911 
3912 
3913 
3914 
3915 
3916 
3917 
3918 
3919 
3920 
3921 
3922 
3923 
3924 static int sctp_setsockopt_auto_asconf(struct sock *sk, char __user *optval,
3925                                         unsigned int optlen)
3926 {
3927         int val;
3928         struct sctp_sock *sp = sctp_sk(sk);
3929 
3930         if (optlen < sizeof(int))
3931                 return -EINVAL;
3932         if (get_user(val, (int __user *)optval))
3933                 return -EFAULT;
3934         if (!sctp_is_ep_boundall(sk) && val)
3935                 return -EINVAL;
3936         if ((val && sp->do_auto_asconf) || (!val && !sp->do_auto_asconf))
3937                 return 0;
3938 
3939         spin_lock_bh(&sock_net(sk)->sctp.addr_wq_lock);
3940         if (val == 0 && sp->do_auto_asconf) {
3941                 list_del(&sp->auto_asconf_list);
3942                 sp->do_auto_asconf = 0;
3943         } else if (val && !sp->do_auto_asconf) {
3944                 list_add_tail(&sp->auto_asconf_list,
3945                     &sock_net(sk)->sctp.auto_asconf_splist);
3946                 sp->do_auto_asconf = 1;
3947         }
3948         spin_unlock_bh(&sock_net(sk)->sctp.addr_wq_lock);
3949         return 0;
3950 }
3951 
3952 
3953 
3954 
3955 
3956 
3957 
3958 
3959 static int sctp_setsockopt_paddr_thresholds(struct sock *sk,
3960                                             char __user *optval,
3961                                             unsigned int optlen)
3962 {
3963         struct sctp_paddrthlds val;
3964         struct sctp_transport *trans;
3965         struct sctp_association *asoc;
3966 
3967         if (optlen < sizeof(struct sctp_paddrthlds))
3968                 return -EINVAL;
3969         if (copy_from_user(&val, (struct sctp_paddrthlds __user *)optval,
3970                            sizeof(struct sctp_paddrthlds)))
3971                 return -EFAULT;
3972 
3973         if (!sctp_is_any(sk, (const union sctp_addr *)&val.spt_address)) {
3974                 trans = sctp_addr_id2transport(sk, &val.spt_address,
3975                                                val.spt_assoc_id);
3976                 if (!trans)
3977                         return -ENOENT;
3978 
3979                 if (val.spt_pathmaxrxt)
3980                         trans->pathmaxrxt = val.spt_pathmaxrxt;
3981                 trans->pf_retrans = val.spt_pathpfthld;
3982 
3983                 return 0;
3984         }
3985 
3986         asoc = sctp_id2assoc(sk, val.spt_assoc_id);
3987         if (!asoc && val.spt_assoc_id != SCTP_FUTURE_ASSOC &&
3988             sctp_style(sk, UDP))
3989                 return -EINVAL;
3990 
3991         if (asoc) {
3992                 list_for_each_entry(trans, &asoc->peer.transport_addr_list,
3993                                     transports) {
3994                         if (val.spt_pathmaxrxt)
3995                                 trans->pathmaxrxt = val.spt_pathmaxrxt;
3996                         trans->pf_retrans = val.spt_pathpfthld;
3997                 }
3998 
3999                 if (val.spt_pathmaxrxt)
4000                         asoc->pathmaxrxt = val.spt_pathmaxrxt;
4001                 asoc->pf_retrans = val.spt_pathpfthld;
4002         } else {
4003                 struct sctp_sock *sp = sctp_sk(sk);
4004 
4005                 if (val.spt_pathmaxrxt)
4006                         sp->pathmaxrxt = val.spt_pathmaxrxt;
4007                 sp->pf_retrans = val.spt_pathpfthld;
4008         }
4009 
4010         return 0;
4011 }
4012 
4013 static int sctp_setsockopt_recvrcvinfo(struct sock *sk,
4014                                        char __user *optval,
4015                                        unsigned int optlen)
4016 {
4017         int val;
4018 
4019         if (optlen < sizeof(int))
4020                 return -EINVAL;
4021         if (get_user(val, (int __user *) optval))
4022                 return -EFAULT;
4023 
4024         sctp_sk(sk)->recvrcvinfo = (val == 0) ? 0 : 1;
4025 
4026         return 0;
4027 }
4028 
4029 static int sctp_setsockopt_recvnxtinfo(struct sock *sk,
4030                                        char __user *optval,
4031                                        unsigned int optlen)
4032 {
4033         int val;
4034 
4035         if (optlen < sizeof(int))
4036                 return -EINVAL;
4037         if (get_user(val, (int __user *) optval))
4038                 return -EFAULT;
4039 
4040         sctp_sk(sk)->recvnxtinfo = (val == 0) ? 0 : 1;
4041 
4042         return 0;
4043 }
4044 
4045 static int sctp_setsockopt_pr_supported(struct sock *sk,
4046                                         char __user *optval,
4047                                         unsigned int optlen)
4048 {
4049         struct sctp_assoc_value params;
4050         struct sctp_association *asoc;
4051 
4052         if (optlen != sizeof(params))
4053                 return -EINVAL;
4054 
4055         if (copy_from_user(¶ms, optval, optlen))
4056                 return -EFAULT;
4057 
4058         asoc = sctp_id2assoc(sk, params.assoc_id);
4059         if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
4060             sctp_style(sk, UDP))
4061                 return -EINVAL;
4062 
4063         sctp_sk(sk)->ep->prsctp_enable = !!params.assoc_value;
4064 
4065         return 0;
4066 }
4067 
4068 static int sctp_setsockopt_default_prinfo(struct sock *sk,
4069                                           char __user *optval,
4070                                           unsigned int optlen)
4071 {
4072         struct sctp_sock *sp = sctp_sk(sk);
4073         struct sctp_default_prinfo info;
4074         struct sctp_association *asoc;
4075         int retval = -EINVAL;
4076 
4077         if (optlen != sizeof(info))
4078                 goto out;
4079 
4080         if (copy_from_user(&info, optval, sizeof(info))) {
4081                 retval = -EFAULT;
4082                 goto out;
4083         }
4084 
4085         if (info.pr_policy & ~SCTP_PR_SCTP_MASK)
4086                 goto out;
4087 
4088         if (info.pr_policy == SCTP_PR_SCTP_NONE)
4089                 info.pr_value = 0;
4090 
4091         asoc = sctp_id2assoc(sk, info.pr_assoc_id);
4092         if (!asoc && info.pr_assoc_id > SCTP_ALL_ASSOC &&
4093             sctp_style(sk, UDP))
4094                 goto out;
4095 
4096         retval = 0;
4097 
4098         if (asoc) {
4099                 SCTP_PR_SET_POLICY(asoc->default_flags, info.pr_policy);
4100                 asoc->default_timetolive = info.pr_value;
4101                 goto out;
4102         }
4103 
4104         if (sctp_style(sk, TCP))
4105                 info.pr_assoc_id = SCTP_FUTURE_ASSOC;
4106 
4107         if (info.pr_assoc_id == SCTP_FUTURE_ASSOC ||
4108             info.pr_assoc_id == SCTP_ALL_ASSOC) {
4109                 SCTP_PR_SET_POLICY(sp->default_flags, info.pr_policy);
4110                 sp->default_timetolive = info.pr_value;
4111         }
4112 
4113         if (info.pr_assoc_id == SCTP_CURRENT_ASSOC ||
4114             info.pr_assoc_id == SCTP_ALL_ASSOC) {
4115                 list_for_each_entry(asoc, &sp->ep->asocs, asocs) {
4116                         SCTP_PR_SET_POLICY(asoc->default_flags, info.pr_policy);
4117                         asoc->default_timetolive = info.pr_value;
4118                 }
4119         }
4120 
4121 out:
4122         return retval;
4123 }
4124 
4125 static int sctp_setsockopt_reconfig_supported(struct sock *sk,
4126                                               char __user *optval,
4127                                               unsigned int optlen)
4128 {
4129         struct sctp_assoc_value params;
4130         struct sctp_association *asoc;
4131         int retval = -EINVAL;
4132 
4133         if (optlen != sizeof(params))
4134                 goto out;
4135 
4136         if (copy_from_user(¶ms, optval, optlen)) {
4137                 retval = -EFAULT;
4138                 goto out;
4139         }
4140 
4141         asoc = sctp_id2assoc(sk, params.assoc_id);
4142         if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
4143             sctp_style(sk, UDP))
4144                 goto out;
4145 
4146         sctp_sk(sk)->ep->reconf_enable = !!params.assoc_value;
4147 
4148         retval = 0;
4149 
4150 out:
4151         return retval;
4152 }
4153 
4154 static int sctp_setsockopt_enable_strreset(struct sock *sk,
4155                                            char __user *optval,
4156                                            unsigned int optlen)
4157 {
4158         struct sctp_endpoint *ep = sctp_sk(sk)->ep;
4159         struct sctp_assoc_value params;
4160         struct sctp_association *asoc;
4161         int retval = -EINVAL;
4162 
4163         if (optlen != sizeof(params))
4164                 goto out;
4165 
4166         if (copy_from_user(¶ms, optval, optlen)) {
4167                 retval = -EFAULT;
4168                 goto out;
4169         }
4170 
4171         if (params.assoc_value & (~SCTP_ENABLE_STRRESET_MASK))
4172                 goto out;
4173 
4174         asoc = sctp_id2assoc(sk, params.assoc_id);
4175         if (!asoc && params.assoc_id > SCTP_ALL_ASSOC &&
4176             sctp_style(sk, UDP))
4177                 goto out;
4178 
4179         retval = 0;
4180 
4181         if (asoc) {
4182                 asoc->strreset_enable = params.assoc_value;
4183                 goto out;
4184         }
4185 
4186         if (sctp_style(sk, TCP))
4187                 params.assoc_id = SCTP_FUTURE_ASSOC;
4188 
4189         if (params.assoc_id == SCTP_FUTURE_ASSOC ||
4190             params.assoc_id == SCTP_ALL_ASSOC)
4191                 ep->strreset_enable = params.assoc_value;
4192 
4193         if (params.assoc_id == SCTP_CURRENT_ASSOC ||
4194             params.assoc_id == SCTP_ALL_ASSOC)
4195                 list_for_each_entry(asoc, &ep->asocs, asocs)
4196                         asoc->strreset_enable = params.assoc_value;
4197 
4198 out:
4199         return retval;
4200 }
4201 
4202 static int sctp_setsockopt_reset_streams(struct sock *sk,
4203                                          char __user *optval,
4204                                          unsigned int optlen)
4205 {
4206         struct sctp_reset_streams *params;
4207         struct sctp_association *asoc;
4208         int retval = -EINVAL;
4209 
4210         if (optlen < sizeof(*params))
4211                 return -EINVAL;
4212         
4213         optlen = min_t(unsigned int, optlen, USHRT_MAX +
4214                                              sizeof(__u16) * sizeof(*params));
4215 
4216         params = memdup_user(optval, optlen);
4217         if (IS_ERR(params))
4218                 return PTR_ERR(params);
4219 
4220         if (params->srs_number_streams * sizeof(__u16) >
4221             optlen - sizeof(*params))
4222                 goto out;
4223 
4224         asoc = sctp_id2assoc(sk, params->srs_assoc_id);
4225         if (!asoc)
4226                 goto out;
4227 
4228         retval = sctp_send_reset_streams(asoc, params);
4229 
4230 out:
4231         kfree(params);
4232         return retval;
4233 }
4234 
4235 static int sctp_setsockopt_reset_assoc(struct sock *sk,
4236                                        char __user *optval,
4237                                        unsigned int optlen)
4238 {
4239         struct sctp_association *asoc;
4240         sctp_assoc_t associd;
4241         int retval = -EINVAL;
4242 
4243         if (optlen != sizeof(associd))
4244                 goto out;
4245 
4246         if (copy_from_user(&associd, optval, optlen)) {
4247                 retval = -EFAULT;
4248                 goto out;
4249         }
4250 
4251         asoc = sctp_id2assoc(sk, associd);
4252         if (!asoc)
4253                 goto out;
4254 
4255         retval = sctp_send_reset_assoc(asoc);
4256 
4257 out:
4258         return retval;
4259 }
4260 
4261 static int sctp_setsockopt_add_streams(struct sock *sk,
4262                                        char __user *optval,
4263                                        unsigned int optlen)
4264 {
4265         struct sctp_association *asoc;
4266         struct sctp_add_streams params;
4267         int retval = -EINVAL;
4268 
4269         if (optlen != sizeof(params))
4270                 goto out;
4271 
4272         if (copy_from_user(¶ms, optval, optlen)) {
4273                 retval = -EFAULT;
4274                 goto out;
4275         }
4276 
4277         asoc = sctp_id2assoc(sk, params.sas_assoc_id);
4278         if (!asoc)
4279                 goto out;
4280 
4281         retval = sctp_send_add_streams(asoc, ¶ms);
4282 
4283 out:
4284         return retval;
4285 }
4286 
4287 static int sctp_setsockopt_scheduler(struct sock *sk,
4288                                      char __user *optval,
4289                                      unsigned int optlen)
4290 {
4291         struct sctp_sock *sp = sctp_sk(sk);
4292         struct sctp_association *asoc;
4293         struct sctp_assoc_value params;
4294         int retval = 0;
4295 
4296         if (optlen < sizeof(params))
4297                 return -EINVAL;
4298 
4299         optlen = sizeof(params);
4300         if (copy_from_user(¶ms, optval, optlen))
4301                 return -EFAULT;
4302 
4303         if (params.assoc_value > SCTP_SS_MAX)
4304                 return -EINVAL;
4305 
4306         asoc = sctp_id2assoc(sk, params.assoc_id);
4307         if (!asoc && params.assoc_id > SCTP_ALL_ASSOC &&
4308             sctp_style(sk, UDP))
4309                 return -EINVAL;
4310 
4311         if (asoc)
4312                 return sctp_sched_set_sched(asoc, params.assoc_value);
4313 
4314         if (sctp_style(sk, TCP))
4315                 params.assoc_id = SCTP_FUTURE_ASSOC;
4316 
4317         if (params.assoc_id == SCTP_FUTURE_ASSOC ||
4318             params.assoc_id == SCTP_ALL_ASSOC)
4319                 sp->default_ss = params.assoc_value;
4320 
4321         if (params.assoc_id == SCTP_CURRENT_ASSOC ||
4322             params.assoc_id == SCTP_ALL_ASSOC) {
4323                 list_for_each_entry(asoc, &sp->ep->asocs, asocs) {
4324                         int ret = sctp_sched_set_sched(asoc,
4325                                                        params.assoc_value);
4326 
4327                         if (ret && !retval)
4328                                 retval = ret;
4329                 }
4330         }
4331 
4332         return retval;
4333 }
4334 
4335 static int sctp_setsockopt_scheduler_value(struct sock *sk,
4336                                            char __user *optval,
4337                                            unsigned int optlen)
4338 {
4339         struct sctp_stream_value params;
4340         struct sctp_association *asoc;
4341         int retval = -EINVAL;
4342 
4343         if (optlen < sizeof(params))
4344                 goto out;
4345 
4346         optlen = sizeof(params);
4347         if (copy_from_user(¶ms, optval, optlen)) {
4348                 retval = -EFAULT;
4349                 goto out;
4350         }
4351 
4352         asoc = sctp_id2assoc(sk, params.assoc_id);
4353         if (!asoc && params.assoc_id != SCTP_CURRENT_ASSOC &&
4354             sctp_style(sk, UDP))
4355                 goto out;
4356 
4357         if (asoc) {
4358                 retval = sctp_sched_set_value(asoc, params.stream_id,
4359                                               params.stream_value, GFP_KERNEL);
4360                 goto out;
4361         }
4362 
4363         retval = 0;
4364 
4365         list_for_each_entry(asoc, &sctp_sk(sk)->ep->asocs, asocs) {
4366                 int ret = sctp_sched_set_value(asoc, params.stream_id,
4367                                                params.stream_value, GFP_KERNEL);
4368                 if (ret && !retval) 
4369                         retval = ret;
4370         }
4371 
4372 out:
4373         return retval;
4374 }
4375 
4376 static int sctp_setsockopt_interleaving_supported(struct sock *sk,
4377                                                   char __user *optval,
4378                                                   unsigned int optlen)
4379 {
4380         struct sctp_sock *sp = sctp_sk(sk);
4381         struct sctp_assoc_value params;
4382         struct sctp_association *asoc;
4383         int retval = -EINVAL;
4384 
4385         if (optlen < sizeof(params))
4386                 goto out;
4387 
4388         optlen = sizeof(params);
4389         if (copy_from_user(¶ms, optval, optlen)) {
4390                 retval = -EFAULT;
4391                 goto out;
4392         }
4393 
4394         asoc = sctp_id2assoc(sk, params.assoc_id);
4395         if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
4396             sctp_style(sk, UDP))
4397                 goto out;
4398 
4399         if (!sock_net(sk)->sctp.intl_enable || !sp->frag_interleave) {
4400                 retval = -EPERM;
4401                 goto out;
4402         }
4403 
4404         sp->ep->intl_enable = !!params.assoc_value;
4405 
4406         retval = 0;
4407 
4408 out:
4409         return retval;
4410 }
4411 
4412 static int sctp_setsockopt_reuse_port(struct sock *sk, char __user *optval,
4413                                       unsigned int optlen)
4414 {
4415         int val;
4416 
4417         if (!sctp_style(sk, TCP))
4418                 return -EOPNOTSUPP;
4419 
4420         if (sctp_sk(sk)->ep->base.bind_addr.port)
4421                 return -EFAULT;
4422 
4423         if (optlen < sizeof(int))
4424                 return -EINVAL;
4425 
4426         if (get_user(val, (int __user *)optval))
4427                 return -EFAULT;
4428 
4429         sctp_sk(sk)->reuse = !!val;
4430 
4431         return 0;
4432 }
4433 
4434 static int sctp_assoc_ulpevent_type_set(struct sctp_event *param,
4435                                         struct sctp_association *asoc)
4436 {
4437         struct sctp_ulpevent *event;
4438 
4439         sctp_ulpevent_type_set(&asoc->subscribe, param->se_type, param->se_on);
4440 
4441         if (param->se_type == SCTP_SENDER_DRY_EVENT && param->se_on) {
4442                 if (sctp_outq_is_empty(&asoc->outqueue)) {
4443                         event = sctp_ulpevent_make_sender_dry_event(asoc,
4444                                         GFP_USER | __GFP_NOWARN);
4445                         if (!event)
4446                                 return -ENOMEM;
4447 
4448                         asoc->stream.si->enqueue_event(&asoc->ulpq, event);
4449                 }
4450         }
4451 
4452         return 0;
4453 }
4454 
4455 static int sctp_setsockopt_event(struct sock *sk, char __user *optval,
4456                                  unsigned int optlen)
4457 {
4458         struct sctp_sock *sp = sctp_sk(sk);
4459         struct sctp_association *asoc;
4460         struct sctp_event param;
4461         int retval = 0;
4462 
4463         if (optlen < sizeof(param))
4464                 return -EINVAL;
4465 
4466         optlen = sizeof(param);
4467         if (copy_from_user(¶m, optval, optlen))
4468                 return -EFAULT;
4469 
4470         if (param.se_type < SCTP_SN_TYPE_BASE ||
4471             param.se_type > SCTP_SN_TYPE_MAX)
4472                 return -EINVAL;
4473 
4474         asoc = sctp_id2assoc(sk, param.se_assoc_id);
4475         if (!asoc && param.se_assoc_id > SCTP_ALL_ASSOC &&
4476             sctp_style(sk, UDP))
4477                 return -EINVAL;
4478 
4479         if (asoc)
4480                 return sctp_assoc_ulpevent_type_set(¶m, asoc);
4481 
4482         if (sctp_style(sk, TCP))
4483                 param.se_assoc_id = SCTP_FUTURE_ASSOC;
4484 
4485         if (param.se_assoc_id == SCTP_FUTURE_ASSOC ||
4486             param.se_assoc_id == SCTP_ALL_ASSOC)
4487                 sctp_ulpevent_type_set(&sp->subscribe,
4488                                        param.se_type, param.se_on);
4489 
4490         if (param.se_assoc_id == SCTP_CURRENT_ASSOC ||
4491             param.se_assoc_id == SCTP_ALL_ASSOC) {
4492                 list_for_each_entry(asoc, &sp->ep->asocs, asocs) {
4493                         int ret = sctp_assoc_ulpevent_type_set(¶m, asoc);
4494 
4495                         if (ret && !retval)
4496                                 retval = ret;
4497                 }
4498         }
4499 
4500         return retval;
4501 }
4502 
4503 static int sctp_setsockopt_asconf_supported(struct sock *sk,
4504                                             char __user *optval,
4505                                             unsigned int optlen)
4506 {
4507         struct sctp_assoc_value params;
4508         struct sctp_association *asoc;
4509         struct sctp_endpoint *ep;
4510         int retval = -EINVAL;
4511 
4512         if (optlen != sizeof(params))
4513                 goto out;
4514 
4515         if (copy_from_user(¶ms, optval, optlen)) {
4516                 retval = -EFAULT;
4517                 goto out;
4518         }
4519 
4520         asoc = sctp_id2assoc(sk, params.assoc_id);
4521         if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
4522             sctp_style(sk, UDP))
4523                 goto out;
4524 
4525         ep = sctp_sk(sk)->ep;
4526         ep->asconf_enable = !!params.assoc_value;
4527 
4528         if (ep->asconf_enable && ep->auth_enable) {
4529                 sctp_auth_ep_add_chunkid(ep, SCTP_CID_ASCONF);
4530                 sctp_auth_ep_add_chunkid(ep, SCTP_CID_ASCONF_ACK);
4531         }
4532 
4533         retval = 0;
4534 
4535 out:
4536         return retval;
4537 }
4538 
4539 static int sctp_setsockopt_auth_supported(struct sock *sk,
4540                                           char __user *optval,
4541                                           unsigned int optlen)
4542 {
4543         struct sctp_assoc_value params;
4544         struct sctp_association *asoc;
4545         struct sctp_endpoint *ep;
4546         int retval = -EINVAL;
4547 
4548         if (optlen != sizeof(params))
4549                 goto out;
4550 
4551         if (copy_from_user(¶ms, optval, optlen)) {
4552                 retval = -EFAULT;
4553                 goto out;
4554         }
4555 
4556         asoc = sctp_id2assoc(sk, params.assoc_id);
4557         if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
4558             sctp_style(sk, UDP))
4559                 goto out;
4560 
4561         ep = sctp_sk(sk)->ep;
4562         if (params.assoc_value) {
4563                 retval = sctp_auth_init(ep, GFP_KERNEL);
4564                 if (retval)
4565                         goto out;
4566                 if (ep->asconf_enable) {
4567                         sctp_auth_ep_add_chunkid(ep, SCTP_CID_ASCONF);
4568                         sctp_auth_ep_add_chunkid(ep, SCTP_CID_ASCONF_ACK);
4569                 }
4570         }
4571 
4572         ep->auth_enable = !!params.assoc_value;
4573         retval = 0;
4574 
4575 out:
4576         return retval;
4577 }
4578 
4579 static int sctp_setsockopt_ecn_supported(struct sock *sk,
4580                                          char __user *optval,
4581                                          unsigned int optlen)
4582 {
4583         struct sctp_assoc_value params;
4584         struct sctp_association *asoc;
4585         int retval = -EINVAL;
4586 
4587         if (optlen != sizeof(params))
4588                 goto out;
4589 
4590         if (copy_from_user(¶ms, optval, optlen)) {
4591                 retval = -EFAULT;
4592                 goto out;
4593         }
4594 
4595         asoc = sctp_id2assoc(sk, params.assoc_id);
4596         if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
4597             sctp_style(sk, UDP))
4598                 goto out;
4599 
4600         sctp_sk(sk)->ep->ecn_enable = !!params.assoc_value;
4601         retval = 0;
4602 
4603 out:
4604         return retval;
4605 }
4606 
4607 
4608 
4609 
4610 
4611 
4612 
4613 
4614 
4615 
4616 
4617 
4618 
4619 
4620 
4621 
4622 
4623 
4624 
4625 
4626 static int sctp_setsockopt(struct sock *sk, int level, int optname,
4627                            char __user *optval, unsigned int optlen)
4628 {
4629         int retval = 0;
4630 
4631         pr_debug("%s: sk:%p, optname:%d\n", __func__, sk, optname);
4632 
4633         
4634 
4635 
4636 
4637 
4638 
4639         if (level != SOL_SCTP) {
4640                 struct sctp_af *af = sctp_sk(sk)->pf->af;
4641                 retval = af->setsockopt(sk, level, optname, optval, optlen);
4642                 goto out_nounlock;
4643         }
4644 
4645         lock_sock(sk);
4646 
4647         switch (optname) {
4648         case SCTP_SOCKOPT_BINDX_ADD:
4649                 
4650                 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
4651                                                optlen, SCTP_BINDX_ADD_ADDR);
4652                 break;
4653 
4654         case SCTP_SOCKOPT_BINDX_REM:
4655                 
4656                 retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
4657                                                optlen, SCTP_BINDX_REM_ADDR);
4658                 break;
4659 
4660         case SCTP_SOCKOPT_CONNECTX_OLD:
4661                 
4662                 retval = sctp_setsockopt_connectx_old(sk,
4663                                             (struct sockaddr __user *)optval,
4664                                             optlen);
4665                 break;
4666 
4667         case SCTP_SOCKOPT_CONNECTX:
4668                 
4669                 retval = sctp_setsockopt_connectx(sk,
4670                                             (struct sockaddr __user *)optval,
4671                                             optlen);
4672                 break;
4673 
4674         case SCTP_DISABLE_FRAGMENTS:
4675                 retval = sctp_setsockopt_disable_fragments(sk, optval, optlen);
4676                 break;
4677 
4678         case SCTP_EVENTS:
4679                 retval = sctp_setsockopt_events(sk, optval, optlen);
4680                 break;
4681 
4682         case SCTP_AUTOCLOSE:
4683                 retval = sctp_setsockopt_autoclose(sk, optval, optlen);
4684                 break;
4685 
4686         case SCTP_PEER_ADDR_PARAMS:
4687                 retval = sctp_setsockopt_peer_addr_params(sk, optval, optlen);
4688                 break;
4689 
4690         case SCTP_DELAYED_SACK:
4691                 retval = sctp_setsockopt_delayed_ack(sk, optval, optlen);
4692                 break;
4693         case SCTP_PARTIAL_DELIVERY_POINT:
4694                 retval = sctp_setsockopt_partial_delivery_point(sk, optval, optlen);
4695                 break;
4696 
4697         case SCTP_INITMSG:
4698                 retval = sctp_setsockopt_initmsg(sk, optval, optlen);
4699                 break;
4700         case SCTP_DEFAULT_SEND_PARAM:
4701                 retval = sctp_setsockopt_default_send_param(sk, optval,
4702                                                             optlen);
4703                 break;
4704         case SCTP_DEFAULT_SNDINFO:
4705                 retval = sctp_setsockopt_default_sndinfo(sk, optval, optlen);
4706                 break;
4707         case SCTP_PRIMARY_ADDR:
4708                 retval = sctp_setsockopt_primary_addr(sk, optval, optlen);
4709                 break;
4710         case SCTP_SET_PEER_PRIMARY_ADDR:
4711                 retval = sctp_setsockopt_peer_primary_addr(sk, optval, optlen);
4712                 break;
4713         case SCTP_NODELAY:
4714                 retval = sctp_setsockopt_nodelay(sk, optval, optlen);
4715                 break;
4716         case SCTP_RTOINFO:
4717                 retval = sctp_setsockopt_rtoinfo(sk, optval, optlen);
4718                 break;
4719         case SCTP_ASSOCINFO:
4720                 retval = sctp_setsockopt_associnfo(sk, optval, optlen);
4721                 break;
4722         case SCTP_I_WANT_MAPPED_V4_ADDR:
4723                 retval = sctp_setsockopt_mappedv4(sk, optval, optlen);
4724                 break;
4725         case SCTP_MAXSEG:
4726                 retval = sctp_setsockopt_maxseg(sk, optval, optlen);
4727                 break;
4728         case SCTP_ADAPTATION_LAYER:
4729                 retval = sctp_setsockopt_adaptation_layer(sk, optval, optlen);
4730                 break;
4731         case SCTP_CONTEXT:
4732                 retval = sctp_setsockopt_context(sk, optval, optlen);
4733                 break;
4734         case SCTP_FRAGMENT_INTERLEAVE:
4735                 retval = sctp_setsockopt_fragment_interleave(sk, optval, optlen);
4736                 break;
4737         case SCTP_MAX_BURST:
4738                 retval = sctp_setsockopt_maxburst(sk, optval, optlen);
4739                 break;
4740         case SCTP_AUTH_CHUNK:
4741                 retval = sctp_setsockopt_auth_chunk(sk, optval, optlen);
4742                 break;
4743         case SCTP_HMAC_IDENT:
4744                 retval = sctp_setsockopt_hmac_ident(sk, optval, optlen);
4745                 break;
4746         case SCTP_AUTH_KEY:
4747                 retval = sctp_setsockopt_auth_key(sk, optval, optlen);
4748                 break;
4749         case SCTP_AUTH_ACTIVE_KEY:
4750                 retval = sctp_setsockopt_active_key(sk, optval, optlen);
4751                 break;
4752         case SCTP_AUTH_DELETE_KEY:
4753                 retval = sctp_setsockopt_del_key(sk, optval, optlen);
4754                 break;
4755         case SCTP_AUTH_DEACTIVATE_KEY:
4756                 retval = sctp_setsockopt_deactivate_key(sk, optval, optlen);
4757                 break;
4758         case SCTP_AUTO_ASCONF:
4759                 retval = sctp_setsockopt_auto_asconf(sk, optval, optlen);
4760                 break;
4761         case SCTP_PEER_ADDR_THLDS:
4762                 retval = sctp_setsockopt_paddr_thresholds(sk, optval, optlen);
4763                 break;
4764         case SCTP_RECVRCVINFO:
4765                 retval = sctp_setsockopt_recvrcvinfo(sk, optval, optlen);
4766                 break;
4767         case SCTP_RECVNXTINFO:
4768                 retval = sctp_setsockopt_recvnxtinfo(sk, optval, optlen);
4769                 break;
4770         case SCTP_PR_SUPPORTED:
4771                 retval = sctp_setsockopt_pr_supported(sk, optval, optlen);
4772                 break;
4773         case SCTP_DEFAULT_PRINFO:
4774                 retval = sctp_setsockopt_default_prinfo(sk, optval, optlen);
4775                 break;
4776         case SCTP_RECONFIG_SUPPORTED:
4777                 retval = sctp_setsockopt_reconfig_supported(sk, optval, optlen);
4778                 break;
4779         case SCTP_ENABLE_STREAM_RESET:
4780                 retval = sctp_setsockopt_enable_strreset(sk, optval, optlen);
4781                 break;
4782         case SCTP_RESET_STREAMS:
4783                 retval = sctp_setsockopt_reset_streams(sk, optval, optlen);
4784                 break;
4785         case SCTP_RESET_ASSOC:
4786                 retval = sctp_setsockopt_reset_assoc(sk, optval, optlen);
4787                 break;
4788         case SCTP_ADD_STREAMS:
4789                 retval = sctp_setsockopt_add_streams(sk, optval, optlen);
4790                 break;
4791         case SCTP_STREAM_SCHEDULER:
4792                 retval = sctp_setsockopt_scheduler(sk, optval, optlen);
4793                 break;
4794         case SCTP_STREAM_SCHEDULER_VALUE:
4795                 retval = sctp_setsockopt_scheduler_value(sk, optval, optlen);
4796                 break;
4797         case SCTP_INTERLEAVING_SUPPORTED:
4798                 retval = sctp_setsockopt_interleaving_supported(sk, optval,
4799                                                                 optlen);
4800                 break;
4801         case SCTP_REUSE_PORT:
4802                 retval = sctp_setsockopt_reuse_port(sk, optval, optlen);
4803                 break;
4804         case SCTP_EVENT:
4805                 retval = sctp_setsockopt_event(sk, optval, optlen);
4806                 break;
4807         case SCTP_ASCONF_SUPPORTED:
4808                 retval = sctp_setsockopt_asconf_supported(sk, optval, optlen);
4809                 break;
4810         case SCTP_AUTH_SUPPORTED:
4811                 retval = sctp_setsockopt_auth_supported(sk, optval, optlen);
4812                 break;
4813         case SCTP_ECN_SUPPORTED:
4814                 retval = sctp_setsockopt_ecn_supported(sk, optval, optlen);
4815                 break;
4816         default:
4817                 retval = -ENOPROTOOPT;
4818                 break;
4819         }
4820 
4821         release_sock(sk);
4822 
4823 out_nounlock:
4824         return retval;
4825 }
4826 
4827 
4828 
4829 
4830 
4831 
4832 
4833 
4834 
4835 
4836 
4837 
4838 
4839 
4840 
4841 
4842 
4843 static int sctp_connect(struct sock *sk, struct sockaddr *addr,
4844                         int addr_len, int flags)
4845 {
4846         struct sctp_af *af;
4847         int err = -EINVAL;
4848 
4849         lock_sock(sk);
4850         pr_debug("%s: sk:%p, sockaddr:%p, addr_len:%d\n", __func__, sk,
4851                  addr, addr_len);
4852 
4853         
4854         af = sctp_get_af_specific(addr->sa_family);
4855         if (af && addr_len >= af->sockaddr_len)
4856                 err = __sctp_connect(sk, addr, af->sockaddr_len, flags, NULL);
4857 
4858         release_sock(sk);
4859         return err;
4860 }
4861 
4862 int sctp_inet_connect(struct socket *sock, struct sockaddr *uaddr,
4863                       int addr_len, int flags)
4864 {
4865         if (addr_len < sizeof(uaddr->sa_family))
4866                 return -EINVAL;
4867 
4868         if (uaddr->sa_family == AF_UNSPEC)
4869                 return -EOPNOTSUPP;
4870 
4871         return sctp_connect(sock->sk, uaddr, addr_len, flags);
4872 }
4873 
4874 
4875 static int sctp_disconnect(struct sock *sk, int flags)
4876 {
4877         return -EOPNOTSUPP; 
4878 }
4879 
4880 
4881 
4882 
4883 
4884 
4885 
4886 
4887 static struct sock *sctp_accept(struct sock *sk, int flags, int *err, bool kern)
4888 {
4889         struct sctp_sock *sp;
4890         struct sctp_endpoint *ep;
4891         struct sock *newsk = NULL;
4892         struct sctp_association *asoc;
4893         long timeo;
4894         int error = 0;
4895 
4896         lock_sock(sk);
4897 
4898         sp = sctp_sk(sk);
4899         ep = sp->ep;
4900 
4901         if (!sctp_style(sk, TCP)) {
4902                 error = -EOPNOTSUPP;
4903                 goto out;
4904         }
4905 
4906         if (!sctp_sstate(sk, LISTENING)) {
4907                 error = -EINVAL;
4908                 goto out;
4909         }
4910 
4911         timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
4912 
4913         error = sctp_wait_for_accept(sk, timeo);
4914         if (error)
4915                 goto out;
4916 
4917         
4918 
4919 
4920         asoc = list_entry(ep->asocs.next, struct sctp_association, asocs);
4921 
4922         newsk = sp->pf->create_accept_sk(sk, asoc, kern);
4923         if (!newsk) {
4924                 error = -ENOMEM;
4925                 goto out;
4926         }
4927 
4928         
4929 
4930 
4931         error = sctp_sock_migrate(sk, newsk, asoc, SCTP_SOCKET_TCP);
4932         if (error) {
4933                 sk_common_release(newsk);
4934                 newsk = NULL;
4935         }
4936 
4937 out:
4938         release_sock(sk);
4939         *err = error;
4940         return newsk;
4941 }
4942 
4943 
4944 static int sctp_ioctl(struct sock *sk, int cmd, unsigned long arg)
4945 {
4946         int rc = -ENOTCONN;
4947 
4948         lock_sock(sk);
4949 
4950         
4951 
4952 
4953 
4954         if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
4955                 goto out;
4956 
4957         switch (cmd) {
4958         case SIOCINQ: {
4959                 struct sk_buff *skb;
4960                 unsigned int amount = 0;
4961 
4962                 skb = skb_peek(&sk->sk_receive_queue);
4963                 if (skb != NULL) {
4964                         
4965 
4966 
4967 
4968                         amount = skb->len;
4969                 }
4970                 rc = put_user(amount, (int __user *)arg);
4971                 break;
4972         }
4973         default:
4974                 rc = -ENOIOCTLCMD;
4975                 break;
4976         }
4977 out:
4978         release_sock(sk);
4979         return rc;
4980 }
4981 
4982 
4983 
4984 
4985 
4986 static int sctp_init_sock(struct sock *sk)
4987 {
4988         struct net *net = sock_net(sk);
4989         struct sctp_sock *sp;
4990 
4991         pr_debug("%s: sk:%p\n", __func__, sk);
4992 
4993         sp = sctp_sk(sk);
4994 
4995         
4996         switch (sk->sk_type) {
4997         case SOCK_SEQPACKET:
4998                 sp->type = SCTP_SOCKET_UDP;
4999                 break;
5000         case SOCK_STREAM:
5001                 sp->type = SCTP_SOCKET_TCP;
5002                 break;
5003         default:
5004                 return -ESOCKTNOSUPPORT;
5005         }
5006 
5007         sk->sk_gso_type = SKB_GSO_SCTP;
5008 
5009         
5010 
5011 
5012         sp->default_stream = 0;
5013         sp->default_ppid = 0;
5014         sp->default_flags = 0;
5015         sp->default_context = 0;
5016         sp->default_timetolive = 0;
5017 
5018         sp->default_rcv_context = 0;
5019         sp->max_burst = net->sctp.max_burst;
5020 
5021         sp->sctp_hmac_alg = net->sctp.sctp_hmac_alg;
5022 
5023         
5024 
5025 
5026 
5027         sp->initmsg.sinit_num_ostreams   = sctp_max_outstreams;
5028         sp->initmsg.sinit_max_instreams  = sctp_max_instreams;
5029         sp->initmsg.sinit_max_attempts   = net->sctp.max_retrans_init;
5030         sp->initmsg.sinit_max_init_timeo = net->sctp.rto_max;
5031 
5032         
5033 
5034 
5035         sp->rtoinfo.srto_initial = net->sctp.rto_initial;
5036         sp->rtoinfo.srto_max     = net->sctp.rto_max;
5037         sp->rtoinfo.srto_min     = net->sctp.rto_min;
5038 
5039         
5040 
5041 
5042         sp->assocparams.sasoc_asocmaxrxt = net->sctp.max_retrans_association;
5043         sp->assocparams.sasoc_number_peer_destinations = 0;
5044         sp->assocparams.sasoc_peer_rwnd = 0;
5045         sp->assocparams.sasoc_local_rwnd = 0;
5046         sp->assocparams.sasoc_cookie_life = net->sctp.valid_cookie_life;
5047 
5048         
5049 
5050 
5051         sp->subscribe = 0;
5052 
5053         
5054 
5055 
5056         sp->hbinterval  = net->sctp.hb_interval;
5057         sp->pathmaxrxt  = net->sctp.max_retrans_path;
5058         sp->pf_retrans  = net->sctp.pf_retrans;
5059         sp->pathmtu     = 0; 
5060         sp->sackdelay   = net->sctp.sack_timeout;
5061         sp->sackfreq    = 2;
5062         sp->param_flags = SPP_HB_ENABLE |
5063                           SPP_PMTUD_ENABLE |
5064                           SPP_SACKDELAY_ENABLE;
5065         sp->default_ss = SCTP_SS_DEFAULT;
5066 
5067         
5068 
5069 
5070         sp->disable_fragments = 0;
5071 
5072         
5073         sp->nodelay           = 0;
5074 
5075         sp->recvrcvinfo = 0;
5076         sp->recvnxtinfo = 0;
5077 
5078         
5079         sp->v4mapped          = 1;
5080 
5081         
5082 
5083 
5084 
5085 
5086         sp->autoclose         = 0;
5087 
5088         
5089         sp->user_frag         = 0;
5090 
5091         sp->adaptation_ind = 0;
5092 
5093         sp->pf = sctp_get_pf_specific(sk->sk_family);
5094 
5095         
5096         atomic_set(&sp->pd_mode, 0);
5097         skb_queue_head_init(&sp->pd_lobby);
5098         sp->frag_interleave = 0;
5099 
5100         
5101 
5102 
5103 
5104         sp->ep = sctp_endpoint_new(sk, GFP_KERNEL);
5105         if (!sp->ep)
5106                 return -ENOMEM;
5107 
5108         sp->hmac = NULL;
5109 
5110         sk->sk_destruct = sctp_destruct_sock;
5111 
5112         SCTP_DBG_OBJCNT_INC(sock);
5113 
5114         local_bh_disable();
5115         sk_sockets_allocated_inc(sk);
5116         sock_prot_inuse_add(net, sk->sk_prot, 1);
5117 
5118         
5119 
5120 
5121         if (net->sctp.default_auto_asconf) {
5122                 spin_lock(&sock_net(sk)->sctp.addr_wq_lock);
5123                 list_add_tail(&sp->auto_asconf_list,
5124                     &net->sctp.auto_asconf_splist);
5125                 sp->do_auto_asconf = 1;
5126                 spin_unlock(&sock_net(sk)->sctp.addr_wq_lock);
5127         } else {
5128                 sp->do_auto_asconf = 0;
5129         }
5130 
5131         local_bh_enable();
5132 
5133         return 0;
5134 }
5135 
5136 
5137 
5138 
5139 static void sctp_destroy_sock(struct sock *sk)
5140 {
5141         struct sctp_sock *sp;
5142 
5143         pr_debug("%s: sk:%p\n", __func__, sk);
5144 
5145         
5146         sp = sctp_sk(sk);
5147         
5148 
5149 
5150         if (sp->ep == NULL)
5151                 return;
5152 
5153         if (sp->do_auto_asconf) {
5154                 sp->do_auto_asconf = 0;
5155                 list_del(&sp->auto_asconf_list);
5156         }
5157         sctp_endpoint_free(sp->ep);
5158         local_bh_disable();
5159         sk_sockets_allocated_dec(sk);
5160         sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
5161         local_bh_enable();
5162 }
5163 
5164 
5165 static void sctp_destruct_sock(struct sock *sk)
5166 {
5167         struct sctp_sock *sp = sctp_sk(sk);
5168 
5169         
5170         crypto_free_shash(sp->hmac);
5171 
5172         inet_sock_destruct(sk);
5173 }
5174 
5175 
5176 
5177 
5178 
5179 
5180 
5181 
5182 
5183 
5184 
5185 
5186 
5187 
5188 
5189 
5190 
5191 static void sctp_shutdown(struct sock *sk, int how)
5192 {
5193         struct net *net = sock_net(sk);
5194         struct sctp_endpoint *ep;
5195 
5196         if (!sctp_style(sk, TCP))
5197                 return;
5198 
5199         ep = sctp_sk(sk)->ep;
5200         if (how & SEND_SHUTDOWN && !list_empty(&ep->asocs)) {
5201                 struct sctp_association *asoc;
5202 
5203                 inet_sk_set_state(sk, SCTP_SS_CLOSING);
5204                 asoc = list_entry(ep->asocs.next,
5205                                   struct sctp_association, asocs);
5206                 sctp_primitive_SHUTDOWN(net, asoc, NULL);
5207         }
5208 }
5209 
5210 int sctp_get_sctp_info(struct sock *sk, struct sctp_association *asoc,
5211                        struct sctp_info *info)
5212 {
5213         struct sctp_transport *prim;
5214         struct list_head *pos;
5215         int mask;
5216 
5217         memset(info, 0, sizeof(*info));
5218         if (!asoc) {
5219                 struct sctp_sock *sp = sctp_sk(sk);
5220 
5221                 info->sctpi_s_autoclose = sp->autoclose;
5222                 info->sctpi_s_adaptation_ind = sp->adaptation_ind;
5223                 info->sctpi_s_pd_point = sp->pd_point;
5224                 info->sctpi_s_nodelay = sp->nodelay;
5225                 info->sctpi_s_disable_fragments = sp->disable_fragments;
5226                 info->sctpi_s_v4mapped = sp->v4mapped;
5227                 info->sctpi_s_frag_interleave = sp->frag_interleave;
5228                 info->sctpi_s_type = sp->type;
5229 
5230                 return 0;
5231         }
5232 
5233         info->sctpi_tag = asoc->c.my_vtag;
5234         info->sctpi_state = asoc->state;
5235         info->sctpi_rwnd = asoc->a_rwnd;
5236         info->sctpi_unackdata = asoc->unack_data;
5237         info->sctpi_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map);
5238         info->sctpi_instrms = asoc->stream.incnt;
5239         info->sctpi_outstrms = asoc->stream.outcnt;
5240         list_for_each(pos, &asoc->base.inqueue.in_chunk_list)
5241                 info->sctpi_inqueue++;
5242         list_for_each(pos, &asoc->outqueue.out_chunk_list)
5243                 info->sctpi_outqueue++;
5244         info->sctpi_overall_error = asoc->overall_error_count;
5245         info->sctpi_max_burst = asoc->max_burst;
5246         info->sctpi_maxseg = asoc->frag_point;
5247         info->sctpi_peer_rwnd = asoc->peer.rwnd;
5248         info->sctpi_peer_tag = asoc->c.peer_vtag;
5249 
5250         mask = asoc->peer.ecn_capable << 1;
5251         mask = (mask | asoc->peer.ipv4_address) << 1;
5252         mask = (mask | asoc->peer.ipv6_address) << 1;
5253         mask = (mask | asoc->peer.hostname_address) << 1;
5254         mask = (mask | asoc->peer.asconf_capable) << 1;
5255         mask = (mask | asoc->peer.prsctp_capable) << 1;
5256         mask = (mask | asoc->peer.auth_capable);
5257         info->sctpi_peer_capable = mask;
5258         mask = asoc->peer.sack_needed << 1;
5259         mask = (mask | asoc->peer.sack_generation) << 1;
5260         mask = (mask | asoc->peer.zero_window_announced);
5261         info->sctpi_peer_sack = mask;
5262 
5263         info->sctpi_isacks = asoc->stats.isacks;
5264         info->sctpi_osacks = asoc->stats.osacks;
5265         info->sctpi_opackets = asoc->stats.opackets;
5266         info->sctpi_ipackets = asoc->stats.ipackets;
5267         info->sctpi_rtxchunks = asoc->stats.rtxchunks;
5268         info->sctpi_outofseqtsns = asoc->stats.outofseqtsns;
5269         info->sctpi_idupchunks = asoc->stats.idupchunks;
5270         info->sctpi_gapcnt = asoc->stats.gapcnt;
5271         info->sctpi_ouodchunks = asoc->stats.ouodchunks;
5272         info->sctpi_iuodchunks = asoc->stats.iuodchunks;
5273         info->sctpi_oodchunks = asoc->stats.oodchunks;
5274         info->sctpi_iodchunks = asoc->stats.iodchunks;
5275         info->sctpi_octrlchunks = asoc->stats.octrlchunks;
5276         info->sctpi_ictrlchunks = asoc->stats.ictrlchunks;
5277 
5278         prim = asoc->peer.primary_path;
5279         memcpy(&info->sctpi_p_address, &prim->ipaddr, sizeof(prim->ipaddr));
5280         info->sctpi_p_state = prim->state;
5281         info->sctpi_p_cwnd = prim->cwnd;
5282         info->sctpi_p_srtt = prim->srtt;
5283         info->sctpi_p_rto = jiffies_to_msecs(prim->rto);
5284         info->sctpi_p_hbinterval = prim->hbinterval;
5285         info->sctpi_p_pathmaxrxt = prim->pathmaxrxt;
5286         info->sctpi_p_sackdelay = jiffies_to_msecs(prim->sackdelay);
5287         info->sctpi_p_ssthresh = prim->ssthresh;
5288         info->sctpi_p_partial_bytes_acked = prim->partial_bytes_acked;
5289         info->sctpi_p_flight_size = prim->flight_size;
5290         info->sctpi_p_error = prim->error_count;
5291 
5292         return 0;
5293 }
5294 EXPORT_SYMBOL_GPL(sctp_get_sctp_info);
5295 
5296 
5297 void sctp_transport_walk_start(struct rhashtable_iter *iter)
5298 {
5299         rhltable_walk_enter(&sctp_transport_hashtable, iter);
5300 
5301         rhashtable_walk_start(iter);
5302 }
5303 
5304 void sctp_transport_walk_stop(struct rhashtable_iter *iter)
5305 {
5306         rhashtable_walk_stop(iter);
5307         rhashtable_walk_exit(iter);
5308 }
5309 
5310 struct sctp_transport *sctp_transport_get_next(struct net *net,
5311                                                struct rhashtable_iter *iter)
5312 {
5313         struct sctp_transport *t;
5314 
5315         t = rhashtable_walk_next(iter);
5316         for (; t; t = rhashtable_walk_next(iter)) {
5317                 if (IS_ERR(t)) {
5318                         if (PTR_ERR(t) == -EAGAIN)
5319                                 continue;
5320                         break;
5321                 }
5322 
5323                 if (!sctp_transport_hold(t))
5324                         continue;
5325 
5326                 if (net_eq(sock_net(t->asoc->base.sk), net) &&
5327                     t->asoc->peer.primary_path == t)
5328                         break;
5329 
5330                 sctp_transport_put(t);
5331         }
5332 
5333         return t;
5334 }
5335 
5336 struct sctp_transport *sctp_transport_get_idx(struct net *net,
5337                                               struct rhashtable_iter *iter,
5338                                               int pos)
5339 {
5340         struct sctp_transport *t;
5341 
5342         if (!pos)
5343                 return SEQ_START_TOKEN;
5344 
5345         while ((t = sctp_transport_get_next(net, iter)) && !IS_ERR(t)) {
5346                 if (!--pos)
5347                         break;
5348                 sctp_transport_put(t);
5349         }
5350 
5351         return t;
5352 }
5353 
5354 int sctp_for_each_endpoint(int (*cb)(struct sctp_endpoint *, void *),
5355                            void *p) {
5356         int err = 0;
5357         int hash = 0;
5358         struct sctp_ep_common *epb;
5359         struct sctp_hashbucket *head;
5360 
5361         for (head = sctp_ep_hashtable; hash < sctp_ep_hashsize;
5362              hash++, head++) {
5363                 read_lock_bh(&head->lock);
5364                 sctp_for_each_hentry(epb, &head->chain) {
5365                         err = cb(sctp_ep(epb), p);
5366                         if (err)
5367                                 break;
5368                 }
5369                 read_unlock_bh(&head->lock);
5370         }
5371 
5372         return err;
5373 }
5374 EXPORT_SYMBOL_GPL(sctp_for_each_endpoint);
5375 
5376 int sctp_transport_lookup_process(int (*cb)(struct sctp_transport *, void *),
5377                                   struct net *net,
5378                                   const union sctp_addr *laddr,
5379                                   const union sctp_addr *paddr, void *p)
5380 {
5381         struct sctp_transport *transport;
5382         int err;
5383 
5384         rcu_read_lock();
5385         transport = sctp_addrs_lookup_transport(net, laddr, paddr);
5386         rcu_read_unlock();
5387         if (!transport)
5388                 return -ENOENT;
5389 
5390         err = cb(transport, p);
5391         sctp_transport_put(transport);
5392 
5393         return err;
5394 }
5395 EXPORT_SYMBOL_GPL(sctp_transport_lookup_process);
5396 
5397 int sctp_for_each_transport(int (*cb)(struct sctp_transport *, void *),
5398                             int (*cb_done)(struct sctp_transport *, void *),
5399                             struct net *net, int *pos, void *p) {
5400         struct rhashtable_iter hti;
5401         struct sctp_transport *tsp;
5402         int ret;
5403 
5404 again:
5405         ret = 0;
5406         sctp_transport_walk_start(&hti);
5407 
5408         tsp = sctp_transport_get_idx(net, &hti, *pos + 1);
5409         for (; !IS_ERR_OR_NULL(tsp); tsp = sctp_transport_get_next(net, &hti)) {
5410                 ret = cb(tsp, p);
5411                 if (ret)
5412                         break;
5413                 (*pos)++;
5414                 sctp_transport_put(tsp);
5415         }
5416         sctp_transport_walk_stop(&hti);
5417 
5418         if (ret) {
5419                 if (cb_done && !cb_done(tsp, p)) {
5420                         (*pos)++;
5421                         sctp_transport_put(tsp);
5422                         goto again;
5423                 }
5424                 sctp_transport_put(tsp);
5425         }
5426 
5427         return ret;
5428 }
5429 EXPORT_SYMBOL_GPL(sctp_for_each_transport);
5430 
5431 
5432 
5433 
5434 
5435 
5436 
5437 
5438 static int sctp_getsockopt_sctp_status(struct sock *sk, int len,
5439                                        char __user *optval,
5440                                        int __user *optlen)
5441 {
5442         struct sctp_status status;
5443         struct sctp_association *asoc = NULL;
5444         struct sctp_transport *transport;
5445         sctp_assoc_t associd;
5446         int retval = 0;
5447 
5448         if (len < sizeof(status)) {
5449                 retval = -EINVAL;
5450                 goto out;
5451         }
5452 
5453         len = sizeof(status);
5454         if (copy_from_user(&status, optval, len)) {
5455                 retval = -EFAULT;
5456                 goto out;
5457         }
5458 
5459         associd = status.sstat_assoc_id;
5460         asoc = sctp_id2assoc(sk, associd);
5461         if (!asoc) {
5462                 retval = -EINVAL;
5463                 goto out;
5464         }
5465 
5466         transport = asoc->peer.primary_path;
5467 
5468         status.sstat_assoc_id = sctp_assoc2id(asoc);
5469         status.sstat_state = sctp_assoc_to_state(asoc);
5470         status.sstat_rwnd =  asoc->peer.rwnd;
5471         status.sstat_unackdata = asoc->unack_data;
5472 
5473         status.sstat_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map);
5474         status.sstat_instrms = asoc->stream.incnt;
5475         status.sstat_outstrms = asoc->stream.outcnt;
5476         status.sstat_fragmentation_point = asoc->frag_point;
5477         status.sstat_primary.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
5478         memcpy(&status.sstat_primary.spinfo_address, &transport->ipaddr,
5479                         transport->af_specific->sockaddr_len);
5480         
5481         sctp_get_pf_specific(sk->sk_family)->addr_to_user(sctp_sk(sk),
5482                 (union sctp_addr *)&status.sstat_primary.spinfo_address);
5483         status.sstat_primary.spinfo_state = transport->state;
5484         status.sstat_primary.spinfo_cwnd = transport->cwnd;
5485         status.sstat_primary.spinfo_srtt = transport->srtt;
5486         status.sstat_primary.spinfo_rto = jiffies_to_msecs(transport->rto);
5487         status.sstat_primary.spinfo_mtu = transport->pathmtu;
5488 
5489         if (status.sstat_primary.spinfo_state == SCTP_UNKNOWN)
5490                 status.sstat_primary.spinfo_state = SCTP_ACTIVE;
5491 
5492         if (put_user(len, optlen)) {
5493                 retval = -EFAULT;
5494                 goto out;
5495         }
5496 
5497         pr_debug("%s: len:%d, state:%d, rwnd:%d, assoc_id:%d\n",
5498                  __func__, len, status.sstat_state, status.sstat_rwnd,
5499                  status.sstat_assoc_id);
5500 
5501         if (copy_to_user(optval, &status, len)) {
5502                 retval = -EFAULT;
5503                 goto out;
5504         }
5505 
5506 out:
5507         return retval;
5508 }
5509 
5510 
5511 
5512 
5513 
5514 
5515 
5516 
5517 
5518 static int sctp_getsockopt_peer_addr_info(struct sock *sk, int len,
5519                                           char __user *optval,
5520                                           int __user *optlen)
5521 {
5522         struct sctp_paddrinfo pinfo;
5523         struct sctp_transport *transport;
5524         int retval = 0;
5525 
5526         if (len < sizeof(pinfo)) {
5527                 retval = -EINVAL;
5528                 goto out;
5529         }
5530 
5531         len = sizeof(pinfo);
5532         if (copy_from_user(&pinfo, optval, len)) {
5533                 retval = -EFAULT;
5534                 goto out;
5535         }
5536 
5537         transport = sctp_addr_id2transport(sk, &pinfo.spinfo_address,
5538                                            pinfo.spinfo_assoc_id);
5539         if (!transport)
5540                 return -EINVAL;
5541 
5542         pinfo.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
5543         pinfo.spinfo_state = transport->state;
5544         pinfo.spinfo_cwnd = transport->cwnd;
5545         pinfo.spinfo_srtt = transport->srtt;
5546         pinfo.spinfo_rto = jiffies_to_msecs(transport->rto);
5547         pinfo.spinfo_mtu = transport->pathmtu;
5548 
5549         if (pinfo.spinfo_state == SCTP_UNKNOWN)
5550                 pinfo.spinfo_state = SCTP_ACTIVE;
5551 
5552         if (put_user(len, optlen)) {
5553                 retval = -EFAULT;
5554                 goto out;
5555         }
5556 
5557         if (copy_to_user(optval, &pinfo, len)) {
5558                 retval = -EFAULT;
5559                 goto out;
5560         }
5561 
5562 out:
5563         return retval;
5564 }
5565 
5566 
5567 
5568 
5569 
5570 
5571 
5572 
5573 static int sctp_getsockopt_disable_fragments(struct sock *sk, int len,
5574                                         char __user *optval, int __user *optlen)
5575 {
5576         int val;
5577 
5578         if (len < sizeof(int))
5579                 return -EINVAL;
5580 
5581         len = sizeof(int);
5582         val = (sctp_sk(sk)->disable_fragments == 1);
5583         if (put_user(len, optlen))
5584                 return -EFAULT;
5585         if (copy_to_user(optval, &val, len))
5586                 return -EFAULT;
5587         return 0;
5588 }
5589 
5590 
5591 
5592 
5593 
5594 
5595 static int sctp_getsockopt_events(struct sock *sk, int len, char __user *optval,
5596                                   int __user *optlen)
5597 {
5598         struct sctp_event_subscribe subscribe;
5599         __u8 *sn_type = (__u8 *)&subscribe;
5600         int i;
5601 
5602         if (len == 0)
5603                 return -EINVAL;
5604         if (len > sizeof(struct sctp_event_subscribe))
5605                 len = sizeof(struct sctp_event_subscribe);
5606         if (put_user(len, optlen))
5607                 return -EFAULT;
5608 
5609         for (i = 0; i < len; i++)
5610                 sn_type[i] = sctp_ulpevent_type_enabled(sctp_sk(sk)->subscribe,
5611                                                         SCTP_SN_TYPE_BASE + i);
5612 
5613         if (copy_to_user(optval, &subscribe, len))
5614                 return -EFAULT;
5615 
5616         return 0;
5617 }
5618 
5619 
5620 
5621 
5622 
5623 
5624 
5625 
5626 
5627 
5628 
5629 
5630 static int sctp_getsockopt_autoclose(struct sock *sk, int len, char __user *optval, int __user *optlen)
5631 {
5632         
5633         if (sctp_style(sk, TCP))
5634                 return -EOPNOTSUPP;
5635         if (len < sizeof(int))
5636                 return -EINVAL;
5637         len = sizeof(int);
5638         if (put_user(len, optlen))
5639                 return -EFAULT;
5640         if (put_user(sctp_sk(sk)->autoclose, (int __user *)optval))
5641                 return -EFAULT;
5642         return 0;
5643 }
5644 
5645 
5646 int sctp_do_peeloff(struct sock *sk, sctp_assoc_t id, struct socket **sockp)
5647 {
5648         struct sctp_association *asoc = sctp_id2assoc(sk, id);
5649         struct sctp_sock *sp = sctp_sk(sk);
5650         struct socket *sock;
5651         int err = 0;
5652 
5653         
5654         if (!net_eq(current->nsproxy->net_ns, sock_net(sk)))
5655                 return -EINVAL;
5656 
5657         if (!asoc)
5658                 return -EINVAL;
5659 
5660         
5661 
5662 
5663         if (!sctp_style(sk, UDP))
5664                 return -EINVAL;
5665 
5666         
5667         err = sock_create(sk->sk_family, SOCK_SEQPACKET, IPPROTO_SCTP, &sock);
5668         if (err < 0)
5669                 return err;
5670 
5671         sctp_copy_sock(sock->sk, sk, asoc);
5672 
5673         
5674 
5675 
5676 
5677         sp->pf->to_sk_daddr(&asoc->peer.primary_addr, sk);
5678         sp->pf->copy_ip_options(sk, sock->sk);
5679 
5680         
5681 
5682 
5683         err = sctp_sock_migrate(sk, sock->sk, asoc,
5684                                 SCTP_SOCKET_UDP_HIGH_BANDWIDTH);
5685         if (err) {
5686                 sock_release(sock);
5687                 sock = NULL;
5688         }
5689 
5690         *sockp = sock;
5691 
5692         return err;
5693 }
5694 EXPORT_SYMBOL(sctp_do_peeloff);
5695 
5696 static int sctp_getsockopt_peeloff_common(struct sock *sk, sctp_peeloff_arg_t *peeloff,
5697                                           struct file **newfile, unsigned flags)
5698 {
5699         struct socket *newsock;
5700         int retval;
5701 
5702         retval = sctp_do_peeloff(sk, peeloff->associd, &newsock);
5703         if (retval < 0)
5704                 goto out;
5705 
5706         
5707         retval = get_unused_fd_flags(flags & SOCK_CLOEXEC);
5708         if (retval < 0) {
5709                 sock_release(newsock);
5710                 goto out;
5711         }
5712 
5713         *newfile = sock_alloc_file(newsock, 0, NULL);
5714         if (IS_ERR(*newfile)) {
5715                 put_unused_fd(retval);
5716                 retval = PTR_ERR(*newfile);
5717                 *newfile = NULL;
5718                 return retval;
5719         }
5720 
5721         pr_debug("%s: sk:%p, newsk:%p, sd:%d\n", __func__, sk, newsock->sk,
5722                  retval);
5723 
5724         peeloff->sd = retval;
5725 
5726         if (flags & SOCK_NONBLOCK)
5727                 (*newfile)->f_flags |= O_NONBLOCK;
5728 out:
5729         return retval;
5730 }
5731 
5732 static int sctp_getsockopt_peeloff(struct sock *sk, int len, char __user *optval, int __user *optlen)
5733 {
5734         sctp_peeloff_arg_t peeloff;
5735         struct file *newfile = NULL;
5736         int retval = 0;
5737 
5738         if (len < sizeof(sctp_peeloff_arg_t))
5739                 return -EINVAL;
5740         len = sizeof(sctp_peeloff_arg_t);
5741         if (copy_from_user(&peeloff, optval, len))
5742                 return -EFAULT;
5743 
5744         retval = sctp_getsockopt_peeloff_common(sk, &peeloff, &newfile, 0);
5745         if (retval < 0)
5746                 goto out;
5747 
5748         
5749         if (put_user(len, optlen)) {
5750                 fput(newfile);
5751                 put_unused_fd(retval);
5752                 return -EFAULT;
5753         }
5754 
5755         if (copy_to_user(optval, &peeloff, len)) {
5756                 fput(newfile);
5757                 put_unused_fd(retval);
5758                 return -EFAULT;
5759         }
5760         fd_install(retval, newfile);
5761 out:
5762         return retval;
5763 }
5764 
5765 static int sctp_getsockopt_peeloff_flags(struct sock *sk, int len,
5766                                          char __user *optval, int __user *optlen)
5767 {
5768         sctp_peeloff_flags_arg_t peeloff;
5769         struct file *newfile = NULL;
5770         int retval = 0;
5771 
5772         if (len < sizeof(sctp_peeloff_flags_arg_t))
5773                 return -EINVAL;
5774         len = sizeof(sctp_peeloff_flags_arg_t);
5775         if (copy_from_user(&peeloff, optval, len))
5776                 return -EFAULT;
5777 
5778         retval = sctp_getsockopt_peeloff_common(sk, &peeloff.p_arg,
5779                                                 &newfile, peeloff.flags);
5780         if (retval < 0)
5781                 goto out;
5782 
5783         
5784         if (put_user(len, optlen)) {
5785                 fput(newfile);
5786                 put_unused_fd(retval);
5787                 return -EFAULT;
5788         }
5789 
5790         if (copy_to_user(optval, &peeloff, len)) {
5791                 fput(newfile);
5792                 put_unused_fd(retval);
5793                 return -EFAULT;
5794         }
5795         fd_install(retval, newfile);
5796 out:
5797         return retval;
5798 }
5799 
5800 
5801 
5802 
5803 
5804 
5805 
5806 
5807 
5808 
5809 
5810 
5811 
5812 
5813 
5814 
5815 
5816 
5817 
5818 
5819 
5820 
5821 
5822 
5823 
5824 
5825 
5826 
5827 
5828 
5829 
5830 
5831 
5832 
5833 
5834 
5835 
5836 
5837 
5838 
5839 
5840 
5841 
5842 
5843 
5844 
5845 
5846 
5847 
5848 
5849 
5850 
5851 
5852 
5853 
5854 
5855 
5856 
5857 
5858 
5859 
5860 
5861 
5862 
5863 
5864 
5865 
5866 
5867 
5868 
5869 
5870 
5871 
5872 
5873 
5874 
5875 
5876 
5877 
5878 
5879 
5880 
5881 
5882 
5883 
5884 
5885 
5886 
5887 
5888 
5889 
5890 
5891 
5892 
5893 
5894 
5895 
5896 
5897 
5898 
5899 
5900 
5901 
5902 
5903 
5904 
5905 
5906 
5907 
5908 
5909 
5910 
5911 
5912 
5913 
5914 
5915 
5916 
5917 
5918 
5919 
5920 
5921 
5922 
5923 
5924 
5925 
5926 
5927 
5928 
5929 
5930 
5931 
5932 static int sctp_getsockopt_peer_addr_params(struct sock *sk, int len,
5933                                             char __user *optval, int __user *optlen)
5934 {
5935         struct sctp_paddrparams  params;
5936         struct sctp_transport   *trans = NULL;
5937         struct sctp_association *asoc = NULL;
5938         struct sctp_sock        *sp = sctp_sk(sk);
5939 
5940         if (len >= sizeof(params))
5941                 len = sizeof(params);
5942         else if (len >= ALIGN(offsetof(struct sctp_paddrparams,
5943                                        spp_ipv6_flowlabel), 4))
5944                 len = ALIGN(offsetof(struct sctp_paddrparams,
5945                                      spp_ipv6_flowlabel), 4);
5946         else
5947                 return -EINVAL;
5948 
5949         if (copy_from_user(¶ms, optval, len))
5950                 return -EFAULT;
5951 
5952         
5953 
5954 
5955         if (!sctp_is_any(sk, (union sctp_addr *)¶ms.spp_address)) {
5956                 trans = sctp_addr_id2transport(sk, ¶ms.spp_address,
5957                                                params.spp_assoc_id);
5958                 if (!trans) {
5959                         pr_debug("%s: failed no transport\n", __func__);
5960                         return -EINVAL;
5961                 }
5962         }
5963 
5964         
5965 
5966 
5967 
5968         asoc = sctp_id2assoc(sk, params.spp_assoc_id);
5969         if (!asoc && params.spp_assoc_id != SCTP_FUTURE_ASSOC &&
5970             sctp_style(sk, UDP)) {
5971                 pr_debug("%s: failed no association\n", __func__);
5972                 return -EINVAL;
5973         }
5974 
5975         if (trans) {
5976                 
5977                 params.spp_hbinterval = jiffies_to_msecs(trans->hbinterval);
5978                 params.spp_pathmtu    = trans->pathmtu;
5979                 params.spp_pathmaxrxt = trans->pathmaxrxt;
5980                 params.spp_sackdelay  = jiffies_to_msecs(trans->sackdelay);
5981 
5982                 
5983                 params.spp_flags      = trans->param_flags;
5984                 if (trans->flowlabel & SCTP_FLOWLABEL_SET_MASK) {
5985                         params.spp_ipv6_flowlabel = trans->flowlabel &
5986                                                     SCTP_FLOWLABEL_VAL_MASK;
5987                         params.spp_flags |= SPP_IPV6_FLOWLABEL;
5988                 }
5989                 if (trans->dscp & SCTP_DSCP_SET_MASK) {
5990                         params.spp_dscp = trans->dscp & SCTP_DSCP_VAL_MASK;
5991                         params.spp_flags |= SPP_DSCP;
5992                 }
5993         } else if (asoc) {
5994                 
5995                 params.spp_hbinterval = jiffies_to_msecs(asoc->hbinterval);
5996                 params.spp_pathmtu    = asoc->pathmtu;
5997                 params.spp_pathmaxrxt = asoc->pathmaxrxt;
5998                 params.spp_sackdelay  = jiffies_to_msecs(asoc->sackdelay);
5999 
6000                 
6001                 params.spp_flags      = asoc->param_flags;
6002                 if (asoc->flowlabel & SCTP_FLOWLABEL_SET_MASK) {
6003                         params.spp_ipv6_flowlabel = asoc->flowlabel &
6004                                                     SCTP_FLOWLABEL_VAL_MASK;
6005                         params.spp_flags |= SPP_IPV6_FLOWLABEL;
6006                 }
6007                 if (asoc->dscp & SCTP_DSCP_SET_MASK) {
6008                         params.spp_dscp = asoc->dscp & SCTP_DSCP_VAL_MASK;
6009                         params.spp_flags |= SPP_DSCP;
6010                 }
6011         } else {
6012                 
6013                 params.spp_hbinterval = sp->hbinterval;
6014                 params.spp_pathmtu    = sp->pathmtu;
6015                 params.spp_sackdelay  = sp->sackdelay;
6016                 params.spp_pathmaxrxt = sp->pathmaxrxt;
6017 
6018                 
6019                 params.spp_flags      = sp->param_flags;
6020                 if (sp->flowlabel & SCTP_FLOWLABEL_SET_MASK) {
6021                         params.spp_ipv6_flowlabel = sp->flowlabel &
6022                                                     SCTP_FLOWLABEL_VAL_MASK;
6023                         params.spp_flags |= SPP_IPV6_FLOWLABEL;
6024                 }
6025                 if (sp->dscp & SCTP_DSCP_SET_MASK) {
6026                         params.spp_dscp = sp->dscp & SCTP_DSCP_VAL_MASK;
6027                         params.spp_flags |= SPP_DSCP;
6028                 }
6029         }
6030 
6031         if (copy_to_user(optval, ¶ms, len))
6032                 return -EFAULT;
6033 
6034         if (put_user(len, optlen))
6035                 return -EFAULT;
6036 
6037         return 0;
6038 }
6039 
6040 
6041 
6042 
6043 
6044 
6045 
6046 
6047 
6048 
6049 
6050 
6051 
6052 
6053 
6054 
6055 
6056 
6057 
6058 
6059 
6060 
6061 
6062 
6063 
6064 
6065 
6066 
6067 
6068 
6069 
6070 
6071 
6072 
6073 
6074 
6075 static int sctp_getsockopt_delayed_ack(struct sock *sk, int len,
6076                                             char __user *optval,
6077                                             int __user *optlen)
6078 {
6079         struct sctp_sack_info    params;
6080         struct sctp_association *asoc = NULL;
6081         struct sctp_sock        *sp = sctp_sk(sk);
6082 
6083         if (len >= sizeof(struct sctp_sack_info)) {
6084                 len = sizeof(struct sctp_sack_info);
6085 
6086                 if (copy_from_user(¶ms, optval, len))
6087                         return -EFAULT;
6088         } else if (len == sizeof(struct sctp_assoc_value)) {
6089                 pr_warn_ratelimited(DEPRECATED
6090                                     "%s (pid %d) "
6091                                     "Use of struct sctp_assoc_value in delayed_ack socket option.\n"
6092                                     "Use struct sctp_sack_info instead\n",
6093                                     current->comm, task_pid_nr(current));
6094                 if (copy_from_user(¶ms, optval, len))
6095                         return -EFAULT;
6096         } else
6097                 return -EINVAL;
6098 
6099         
6100 
6101 
6102 
6103         asoc = sctp_id2assoc(sk, params.sack_assoc_id);
6104         if (!asoc && params.sack_assoc_id != SCTP_FUTURE_ASSOC &&
6105             sctp_style(sk, UDP))
6106                 return -EINVAL;
6107 
6108         if (asoc) {
6109                 
6110                 if (asoc->param_flags & SPP_SACKDELAY_ENABLE) {
6111                         params.sack_delay = jiffies_to_msecs(asoc->sackdelay);
6112                         params.sack_freq = asoc->sackfreq;
6113 
6114                 } else {
6115                         params.sack_delay = 0;
6116                         params.sack_freq = 1;
6117                 }
6118         } else {
6119                 
6120                 if (sp->param_flags & SPP_SACKDELAY_ENABLE) {
6121                         params.sack_delay  = sp->sackdelay;
6122                         params.sack_freq = sp->sackfreq;
6123                 } else {
6124                         params.sack_delay  = 0;
6125                         params.sack_freq = 1;
6126                 }
6127         }
6128 
6129         if (copy_to_user(optval, ¶ms, len))
6130                 return -EFAULT;
6131 
6132         if (put_user(len, optlen))
6133                 return -EFAULT;
6134 
6135         return 0;
6136 }
6137 
6138 
6139 
6140 
6141 
6142 
6143 
6144 
6145 
6146 
6147 
6148 
6149 static int sctp_getsockopt_initmsg(struct sock *sk, int len, char __user *optval, int __user *optlen)
6150 {
6151         if (len < sizeof(struct sctp_initmsg))
6152                 return -EINVAL;
6153         len = sizeof(struct sctp_initmsg);
6154         if (put_user(len, optlen))
6155                 return -EFAULT;
6156         if (copy_to_user(optval, &sctp_sk(sk)->initmsg, len))
6157                 return -EFAULT;
6158         return 0;
6159 }
6160 
6161 
6162 static int sctp_getsockopt_peer_addrs(struct sock *sk, int len,
6163                                       char __user *optval, int __user *optlen)
6164 {
6165         struct sctp_association *asoc;
6166         int cnt = 0;
6167         struct sctp_getaddrs getaddrs;
6168         struct sctp_transport *from;
6169         void __user *to;
6170         union sctp_addr temp;
6171         struct sctp_sock *sp = sctp_sk(sk);
6172         int addrlen;
6173         size_t space_left;
6174         int bytes_copied;
6175 
6176         if (len < sizeof(struct sctp_getaddrs))
6177                 return -EINVAL;
6178 
6179         if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
6180                 return -EFAULT;
6181 
6182         
6183         asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
6184         if (!asoc)
6185                 return -EINVAL;
6186 
6187         to = optval + offsetof(struct sctp_getaddrs, addrs);
6188         space_left = len - offsetof(struct sctp_getaddrs, addrs);
6189 
6190         list_for_each_entry(from, &asoc->peer.transport_addr_list,
6191                                 transports) {
6192                 memcpy(&temp, &from->ipaddr, sizeof(temp));
6193                 addrlen = sctp_get_pf_specific(sk->sk_family)
6194                               ->addr_to_user(sp, &temp);
6195                 if (space_left < addrlen)
6196                         return -ENOMEM;
6197                 if (copy_to_user(to, &temp, addrlen))
6198                         return -EFAULT;
6199                 to += addrlen;
6200                 cnt++;
6201                 space_left -= addrlen;
6202         }
6203 
6204         if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num))
6205                 return -EFAULT;
6206         bytes_copied = ((char __user *)to) - optval;
6207         if (put_user(bytes_copied, optlen))
6208                 return -EFAULT;
6209 
6210         return 0;
6211 }
6212 
6213 static int sctp_copy_laddrs(struct sock *sk, __u16 port, void *to,
6214                             size_t space_left, int *bytes_copied)
6215 {
6216         struct sctp_sockaddr_entry *addr;
6217         union sctp_addr temp;
6218         int cnt = 0;
6219         int addrlen;
6220         struct net *net = sock_net(sk);
6221 
6222         rcu_read_lock();
6223         list_for_each_entry_rcu(addr, &net->sctp.local_addr_list, list) {
6224                 if (!addr->valid)
6225                         continue;
6226 
6227                 if ((PF_INET == sk->sk_family) &&
6228                     (AF_INET6 == addr->a.sa.sa_family))
6229                         continue;
6230                 if ((PF_INET6 == sk->sk_family) &&
6231                     inet_v6_ipv6only(sk) &&
6232                     (AF_INET == addr->a.sa.sa_family))
6233                         continue;
6234                 memcpy(&temp, &addr->a, sizeof(temp));
6235                 if (!temp.v4.sin_port)
6236                         temp.v4.sin_port = htons(port);
6237 
6238                 addrlen = sctp_get_pf_specific(sk->sk_family)
6239                               ->addr_to_user(sctp_sk(sk), &temp);
6240 
6241                 if (space_left < addrlen) {
6242                         cnt =  -ENOMEM;
6243                         break;
6244                 }
6245                 memcpy(to, &temp, addrlen);
6246 
6247                 to += addrlen;
6248                 cnt++;
6249                 space_left -= addrlen;
6250                 *bytes_copied += addrlen;
6251         }
6252         rcu_read_unlock();
6253 
6254         return cnt;
6255 }
6256 
6257 
6258 static int sctp_getsockopt_local_addrs(struct sock *sk, int len,
6259                                        char __user *optval, int __user *optlen)
6260 {
6261         struct sctp_bind_addr *bp;
6262         struct sctp_association *asoc;
6263         int cnt = 0;
6264         struct sctp_getaddrs getaddrs;
6265         struct sctp_sockaddr_entry *addr;
6266         void __user *to;
6267         union sctp_addr temp;
6268         struct sctp_sock *sp = sctp_sk(sk);
6269         int addrlen;
6270         int err = 0;
6271         size_t space_left;
6272         int bytes_copied = 0;
6273         void *addrs;
6274         void *buf;
6275 
6276         if (len < sizeof(struct sctp_getaddrs))
6277                 return -EINVAL;
6278 
6279         if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
6280                 return -EFAULT;
6281 
6282         
6283 
6284 
6285 
6286 
6287 
6288         if (0 == getaddrs.assoc_id) {
6289                 bp = &sctp_sk(sk)->ep->base.bind_addr;
6290         } else {
6291                 asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
6292                 if (!asoc)
6293                         return -EINVAL;
6294                 bp = &asoc->base.bind_addr;
6295         }
6296 
6297         to = optval + offsetof(struct sctp_getaddrs, addrs);
6298         space_left = len - offsetof(struct sctp_getaddrs, addrs);
6299 
6300         addrs = kmalloc(space_left, GFP_USER | __GFP_NOWARN);
6301         if (!addrs)
6302                 return -ENOMEM;
6303 
6304         
6305 
6306 
6307         if (sctp_list_single_entry(&bp->address_list)) {
6308                 addr = list_entry(bp->address_list.next,
6309                                   struct sctp_sockaddr_entry, list);
6310                 if (sctp_is_any(sk, &addr->a)) {
6311                         cnt = sctp_copy_laddrs(sk, bp->port, addrs,
6312                                                 space_left, &bytes_copied);
6313                         if (cnt < 0) {
6314                                 err = cnt;
6315                                 goto out;
6316                         }
6317                         goto copy_getaddrs;
6318                 }
6319         }
6320 
6321         buf = addrs;
6322         
6323 
6324 
6325 
6326         list_for_each_entry(addr, &bp->address_list, list) {
6327                 memcpy(&temp, &addr->a, sizeof(temp));
6328                 addrlen = sctp_get_pf_specific(sk->sk_family)
6329                               ->addr_to_user(sp, &temp);
6330                 if (space_left < addrlen) {
6331                         err =  -ENOMEM; 
6332                         goto out;
6333                 }
6334                 memcpy(buf, &temp, addrlen);
6335                 buf += addrlen;
6336                 bytes_copied += addrlen;
6337                 cnt++;
6338                 space_left -= addrlen;
6339         }
6340 
6341 copy_getaddrs:
6342         if (copy_to_user(to, addrs, bytes_copied)) {
6343                 err = -EFAULT;
6344                 goto out;
6345         }
6346         if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num)) {
6347                 err = -EFAULT;
6348                 goto out;
6349         }
6350         
6351 
6352 
6353         if (put_user(bytes_copied, optlen))
6354                 err = -EFAULT;
6355 out:
6356         kfree(addrs);
6357         return err;
6358 }
6359 
6360 
6361 
6362 
6363 
6364 
6365 
6366 static int sctp_getsockopt_primary_addr(struct sock *sk, int len,
6367                                         char __user *optval, int __user *optlen)
6368 {
6369         struct sctp_prim prim;
6370         struct sctp_association *asoc;
6371         struct sctp_sock *sp = sctp_sk(sk);
6372 
6373         if (len < sizeof(struct sctp_prim))
6374                 return -EINVAL;
6375 
6376         len = sizeof(struct sctp_prim);
6377 
6378         if (copy_from_user(&prim, optval, len))
6379                 return -EFAULT;
6380 
6381         asoc = sctp_id2assoc(sk, prim.ssp_assoc_id);
6382         if (!asoc)
6383                 return -EINVAL;
6384 
6385         if (!asoc->peer.primary_path)
6386                 return -ENOTCONN;
6387 
6388         memcpy(&prim.ssp_addr, &asoc->peer.primary_path->ipaddr,
6389                 asoc->peer.primary_path->af_specific->sockaddr_len);
6390 
6391         sctp_get_pf_specific(sk->sk_family)->addr_to_user(sp,
6392                         (union sctp_addr *)&prim.ssp_addr);
6393 
6394         if (put_user(len, optlen))
6395                 return -EFAULT;
6396         if (copy_to_user(optval, &prim, len))
6397                 return -EFAULT;
6398 
6399         return 0;
6400 }
6401 
6402 
6403 
6404 
6405 
6406 
6407 
6408 static int sctp_getsockopt_adaptation_layer(struct sock *sk, int len,
6409                                   char __user *optval, int __user *optlen)
6410 {
6411         struct sctp_setadaptation adaptation;
6412 
6413         if (len < sizeof(struct sctp_setadaptation))
6414                 return -EINVAL;
6415 
6416         len = sizeof(struct sctp_setadaptation);
6417 
6418         adaptation.ssb_adaptation_ind = sctp_sk(sk)->adaptation_ind;
6419 
6420         if (put_user(len, optlen))
6421                 return -EFAULT;
6422         if (copy_to_user(optval, &adaptation, len))
6423                 return -EFAULT;
6424 
6425         return 0;
6426 }
6427 
6428 
6429 
6430 
6431 
6432 
6433 
6434 
6435 
6436 
6437 
6438 
6439 
6440 
6441 
6442 
6443 
6444 
6445 
6446 
6447 static int sctp_getsockopt_default_send_param(struct sock *sk,
6448                                         int len, char __user *optval,
6449                                         int __user *optlen)
6450 {
6451         struct sctp_sock *sp = sctp_sk(sk);
6452         struct sctp_association *asoc;
6453         struct sctp_sndrcvinfo info;
6454 
6455         if (len < sizeof(info))
6456                 return -EINVAL;
6457 
6458         len = sizeof(info);
6459 
6460         if (copy_from_user(&info, optval, len))
6461                 return -EFAULT;
6462 
6463         asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
6464         if (!asoc && info.sinfo_assoc_id != SCTP_FUTURE_ASSOC &&
6465             sctp_style(sk, UDP))
6466                 return -EINVAL;
6467 
6468         if (asoc) {
6469                 info.sinfo_stream = asoc->default_stream;
6470                 info.sinfo_flags = asoc->default_flags;
6471                 info.sinfo_ppid = asoc->default_ppid;
6472                 info.sinfo_context = asoc->default_context;
6473                 info.sinfo_timetolive = asoc->default_timetolive;
6474         } else {
6475                 info.sinfo_stream = sp->default_stream;
6476                 info.sinfo_flags = sp->default_flags;
6477                 info.sinfo_ppid = sp->default_ppid;
6478                 info.sinfo_context = sp->default_context;
6479                 info.sinfo_timetolive = sp->default_timetolive;
6480         }
6481 
6482         if (put_user(len, optlen))
6483                 return -EFAULT;
6484         if (copy_to_user(optval, &info, len))
6485                 return -EFAULT;
6486 
6487         return 0;
6488 }
6489 
6490 
6491 
6492 
6493 static int sctp_getsockopt_default_sndinfo(struct sock *sk, int len,
6494                                            char __user *optval,
6495                                            int __user *optlen)
6496 {
6497         struct sctp_sock *sp = sctp_sk(sk);
6498         struct sctp_association *asoc;
6499         struct sctp_sndinfo info;
6500 
6501         if (len < sizeof(info))
6502                 return -EINVAL;
6503 
6504         len = sizeof(info);
6505 
6506         if (copy_from_user(&info, optval, len))
6507                 return -EFAULT;
6508 
6509         asoc = sctp_id2assoc(sk, info.snd_assoc_id);
6510         if (!asoc && info.snd_assoc_id != SCTP_FUTURE_ASSOC &&
6511             sctp_style(sk, UDP))
6512                 return -EINVAL;
6513 
6514         if (asoc) {
6515                 info.snd_sid = asoc->default_stream;
6516                 info.snd_flags = asoc->default_flags;
6517                 info.snd_ppid = asoc->default_ppid;
6518                 info.snd_context = asoc->default_context;
6519         } else {
6520                 info.snd_sid = sp->default_stream;
6521                 info.snd_flags = sp->default_flags;
6522                 info.snd_ppid = sp->default_ppid;
6523                 info.snd_context = sp->default_context;
6524         }
6525 
6526         if (put_user(len, optlen))
6527                 return -EFAULT;
6528         if (copy_to_user(optval, &info, len))
6529                 return -EFAULT;
6530 
6531         return 0;
6532 }
6533 
6534 
6535 
6536 
6537 
6538 
6539 
6540 
6541 
6542 
6543 
6544 static int sctp_getsockopt_nodelay(struct sock *sk, int len,
6545                                    char __user *optval, int __user *optlen)
6546 {
6547         int val;
6548 
6549         if (len < sizeof(int))
6550                 return -EINVAL;
6551 
6552         len = sizeof(int);
6553         val = (sctp_sk(sk)->nodelay == 1);
6554         if (put_user(len, optlen))
6555                 return -EFAULT;
6556         if (copy_to_user(optval, &val, len))
6557                 return -EFAULT;
6558         return 0;
6559 }
6560 
6561 
6562 
6563 
6564 
6565 
6566 
6567 
6568 
6569 
6570 
6571 
6572 
6573 static int sctp_getsockopt_rtoinfo(struct sock *sk, int len,
6574                                 char __user *optval,
6575                                 int __user *optlen) {
6576         struct sctp_rtoinfo rtoinfo;
6577         struct sctp_association *asoc;
6578 
6579         if (len < sizeof (struct sctp_rtoinfo))
6580                 return -EINVAL;
6581 
6582         len = sizeof(struct sctp_rtoinfo);
6583 
6584         if (copy_from_user(&rtoinfo, optval, len))
6585                 return -EFAULT;
6586 
6587         asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
6588 
6589         if (!asoc && rtoinfo.srto_assoc_id != SCTP_FUTURE_ASSOC &&
6590             sctp_style(sk, UDP))
6591                 return -EINVAL;
6592 
6593         
6594         if (asoc) {
6595                 rtoinfo.srto_initial = jiffies_to_msecs(asoc->rto_initial);
6596                 rtoinfo.srto_max = jiffies_to_msecs(asoc->rto_max);
6597                 rtoinfo.srto_min = jiffies_to_msecs(asoc->rto_min);
6598         } else {
6599                 
6600                 struct sctp_sock *sp = sctp_sk(sk);
6601 
6602                 rtoinfo.srto_initial = sp->rtoinfo.srto_initial;
6603                 rtoinfo.srto_max = sp->rtoinfo.srto_max;
6604                 rtoinfo.srto_min = sp->rtoinfo.srto_min;
6605         }
6606 
6607         if (put_user(len, optlen))
6608                 return -EFAULT;
6609 
6610         if (copy_to_user(optval, &rtoinfo, len))
6611                 return -EFAULT;
6612 
6613         return 0;
6614 }
6615 
6616 
6617 
6618 
6619 
6620 
6621 
6622 
6623 
6624 
6625 
6626 
6627 static int sctp_getsockopt_associnfo(struct sock *sk, int len,
6628                                      char __user *optval,
6629                                      int __user *optlen)
6630 {
6631 
6632         struct sctp_assocparams assocparams;
6633         struct sctp_association *asoc;
6634         struct list_head *pos;
6635         int cnt = 0;
6636 
6637         if (len < sizeof (struct sctp_assocparams))
6638                 return -EINVAL;
6639 
6640         len = sizeof(struct sctp_assocparams);
6641 
6642         if (copy_from_user(&assocparams, optval, len))
6643                 return -EFAULT;
6644 
6645         asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
6646 
6647         if (!asoc && assocparams.sasoc_assoc_id != SCTP_FUTURE_ASSOC &&
6648             sctp_style(sk, UDP))
6649                 return -EINVAL;
6650 
6651         
6652         if (asoc) {
6653                 assocparams.sasoc_asocmaxrxt = asoc->max_retrans;
6654                 assocparams.sasoc_peer_rwnd = asoc->peer.rwnd;
6655                 assocparams.sasoc_local_rwnd = asoc->a_rwnd;
6656                 assocparams.sasoc_cookie_life = ktime_to_ms(asoc->cookie_life);
6657 
6658                 list_for_each(pos, &asoc->peer.transport_addr_list) {
6659                         cnt++;
6660                 }
6661 
6662                 assocparams.sasoc_number_peer_destinations = cnt;
6663         } else {
6664                 
6665                 struct sctp_sock *sp = sctp_sk(sk);
6666 
6667                 assocparams.sasoc_asocmaxrxt = sp->assocparams.sasoc_asocmaxrxt;
6668                 assocparams.sasoc_peer_rwnd = sp->assocparams.sasoc_peer_rwnd;
6669                 assocparams.sasoc_local_rwnd = sp->assocparams.sasoc_local_rwnd;
6670                 assocparams.sasoc_cookie_life =
6671                                         sp->assocparams.sasoc_cookie_life;
6672                 assocparams.sasoc_number_peer_destinations =
6673                                         sp->assocparams.
6674                                         sasoc_number_peer_destinations;
6675         }
6676 
6677         if (put_user(len, optlen))
6678                 return -EFAULT;
6679 
6680         if (copy_to_user(optval, &assocparams, len))
6681                 return -EFAULT;
6682 
6683         return 0;
6684 }
6685 
6686 
6687 
6688 
6689 
6690 
6691 
6692 
6693 
6694 
6695 
6696 static int sctp_getsockopt_mappedv4(struct sock *sk, int len,
6697                                     char __user *optval, int __user *optlen)
6698 {
6699         int val;
6700         struct sctp_sock *sp = sctp_sk(sk);
6701 
6702         if (len < sizeof(int))
6703                 return -EINVAL;
6704 
6705         len = sizeof(int);
6706         val = sp->v4mapped;
6707         if (put_user(len, optlen))
6708                 return -EFAULT;
6709         if (copy_to_user(optval, &val, len))
6710                 return -EFAULT;
6711 
6712         return 0;
6713 }
6714 
6715 
6716 
6717 
6718 
6719 static int sctp_getsockopt_context(struct sock *sk, int len,
6720                                    char __user *optval, int __user *optlen)
6721 {
6722         struct sctp_assoc_value params;
6723         struct sctp_association *asoc;
6724 
6725         if (len < sizeof(struct sctp_assoc_value))
6726                 return -EINVAL;
6727 
6728         len = sizeof(struct sctp_assoc_value);
6729 
6730         if (copy_from_user(¶ms, optval, len))
6731                 return -EFAULT;
6732 
6733         asoc = sctp_id2assoc(sk, params.assoc_id);
6734         if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
6735             sctp_style(sk, UDP))
6736                 return -EINVAL;
6737 
6738         params.assoc_value = asoc ? asoc->default_rcv_context
6739                                   : sctp_sk(sk)->default_rcv_context;
6740 
6741         if (put_user(len, optlen))
6742                 return -EFAULT;
6743         if (copy_to_user(optval, ¶ms, len))
6744                 return -EFAULT;
6745 
6746         return 0;
6747 }
6748 
6749 
6750 
6751 
6752 
6753 
6754 
6755 
6756 
6757 
6758 
6759 
6760 
6761 
6762 
6763 
6764 
6765 
6766 
6767 
6768 
6769 
6770 
6771 
6772 
6773 
6774 
6775 
6776 static int sctp_getsockopt_maxseg(struct sock *sk, int len,
6777                                   char __user *optval, int __user *optlen)
6778 {
6779         struct sctp_assoc_value params;
6780         struct sctp_association *asoc;
6781 
6782         if (len == sizeof(int)) {
6783                 pr_warn_ratelimited(DEPRECATED
6784                                     "%s (pid %d) "
6785                                     "Use of int in maxseg socket option.\n"
6786                                     "Use struct sctp_assoc_value instead\n",
6787                                     current->comm, task_pid_nr(current));
6788                 params.assoc_id = SCTP_FUTURE_ASSOC;
6789         } else if (len >= sizeof(struct sctp_assoc_value)) {
6790                 len = sizeof(struct sctp_assoc_value);
6791                 if (copy_from_user(¶ms, optval, len))
6792                         return -EFAULT;
6793         } else
6794                 return -EINVAL;
6795 
6796         asoc = sctp_id2assoc(sk, params.assoc_id);
6797         if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
6798             sctp_style(sk, UDP))
6799                 return -EINVAL;
6800 
6801         if (asoc)
6802                 params.assoc_value = asoc->frag_point;
6803         else
6804                 params.assoc_value = sctp_sk(sk)->user_frag;
6805 
6806         if (put_user(len, optlen))
6807                 return -EFAULT;
6808         if (len == sizeof(int)) {
6809                 if (copy_to_user(optval, ¶ms.assoc_value, len))
6810                         return -EFAULT;
6811         } else {
6812                 if (copy_to_user(optval, ¶ms, len))
6813                         return -EFAULT;
6814         }
6815 
6816         return 0;
6817 }
6818 
6819 
6820 
6821 
6822 
6823 static int sctp_getsockopt_fragment_interleave(struct sock *sk, int len,
6824                                                char __user *optval, int __user *optlen)
6825 {
6826         int val;
6827 
6828         if (len < sizeof(int))
6829                 return -EINVAL;
6830 
6831         len = sizeof(int);
6832 
6833         val = sctp_sk(sk)->frag_interleave;
6834         if (put_user(len, optlen))
6835                 return -EFAULT;
6836         if (copy_to_user(optval, &val, len))
6837                 return -EFAULT;
6838 
6839         return 0;
6840 }
6841 
6842 
6843 
6844 
6845 
6846 static int sctp_getsockopt_partial_delivery_point(struct sock *sk, int len,
6847                                                   char __user *optval,
6848                                                   int __user *optlen)
6849 {
6850         u32 val;
6851 
6852         if (len < sizeof(u32))
6853                 return -EINVAL;
6854 
6855         len = sizeof(u32);
6856 
6857         val = sctp_sk(sk)->pd_point;
6858         if (put_user(len, optlen))
6859                 return -EFAULT;
6860         if (copy_to_user(optval, &val, len))
6861                 return -EFAULT;
6862 
6863         return 0;
6864 }
6865 
6866 
6867 
6868 
6869 
6870 static int sctp_getsockopt_maxburst(struct sock *sk, int len,
6871                                     char __user *optval,
6872                                     int __user *optlen)
6873 {
6874         struct sctp_assoc_value params;
6875         struct sctp_association *asoc;
6876 
6877         if (len == sizeof(int)) {
6878                 pr_warn_ratelimited(DEPRECATED
6879                                     "%s (pid %d) "
6880                                     "Use of int in max_burst socket option.\n"
6881                                     "Use struct sctp_assoc_value instead\n",
6882                                     current->comm, task_pid_nr(current));
6883                 params.assoc_id = SCTP_FUTURE_ASSOC;
6884         } else if (len >= sizeof(struct sctp_assoc_value)) {
6885                 len = sizeof(struct sctp_assoc_value);
6886                 if (copy_from_user(¶ms, optval, len))
6887                         return -EFAULT;
6888         } else
6889                 return -EINVAL;
6890 
6891         asoc = sctp_id2assoc(sk, params.assoc_id);
6892         if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
6893             sctp_style(sk, UDP))
6894                 return -EINVAL;
6895 
6896         params.assoc_value = asoc ? asoc->max_burst : sctp_sk(sk)->max_burst;
6897 
6898         if (len == sizeof(int)) {
6899                 if (copy_to_user(optval, ¶ms.assoc_value, len))
6900                         return -EFAULT;
6901         } else {
6902                 if (copy_to_user(optval, ¶ms, len))
6903                         return -EFAULT;
6904         }
6905 
6906         return 0;
6907 
6908 }
6909 
6910 static int sctp_getsockopt_hmac_ident(struct sock *sk, int len,
6911                                     char __user *optval, int __user *optlen)
6912 {
6913         struct sctp_endpoint *ep = sctp_sk(sk)->ep;
6914         struct sctp_hmacalgo  __user *p = (void __user *)optval;
6915         struct sctp_hmac_algo_param *hmacs;
6916         __u16 data_len = 0;
6917         u32 num_idents;
6918         int i;
6919 
6920         if (!ep->auth_enable)
6921                 return -EACCES;
6922 
6923         hmacs = ep->auth_hmacs_list;
6924         data_len = ntohs(hmacs->param_hdr.length) -
6925                    sizeof(struct sctp_paramhdr);
6926 
6927         if (len < sizeof(struct sctp_hmacalgo) + data_len)
6928                 return -EINVAL;
6929 
6930         len = sizeof(struct sctp_hmacalgo) + data_len;
6931         num_idents = data_len / sizeof(u16);
6932 
6933         if (put_user(len, optlen))
6934                 return -EFAULT;
6935         if (put_user(num_idents, &p->shmac_num_idents))
6936                 return -EFAULT;
6937         for (i = 0; i < num_idents; i++) {
6938                 __u16 hmacid = ntohs(hmacs->hmac_ids[i]);
6939 
6940                 if (copy_to_user(&p->shmac_idents[i], &hmacid, sizeof(__u16)))
6941                         return -EFAULT;
6942         }
6943         return 0;
6944 }
6945 
6946 static int sctp_getsockopt_active_key(struct sock *sk, int len,
6947                                     char __user *optval, int __user *optlen)
6948 {
6949         struct sctp_endpoint *ep = sctp_sk(sk)->ep;
6950         struct sctp_authkeyid val;
6951         struct sctp_association *asoc;
6952 
6953         if (len < sizeof(struct sctp_authkeyid))
6954                 return -EINVAL;
6955 
6956         len = sizeof(struct sctp_authkeyid);
6957         if (copy_from_user(&val, optval, len))
6958                 return -EFAULT;
6959 
6960         asoc = sctp_id2assoc(sk, val.scact_assoc_id);
6961         if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP))
6962                 return -EINVAL;
6963 
6964         if (asoc) {
6965                 if (!asoc->peer.auth_capable)
6966                         return -EACCES;
6967                 val.scact_keynumber = asoc->active_key_id;
6968         } else {
6969                 if (!ep->auth_enable)
6970                         return -EACCES;
6971                 val.scact_keynumber = ep->active_key_id;
6972         }
6973 
6974         if (put_user(len, optlen))
6975                 return -EFAULT;
6976         if (copy_to_user(optval, &val, len))
6977                 return -EFAULT;
6978 
6979         return 0;
6980 }
6981 
6982 static int sctp_getsockopt_peer_auth_chunks(struct sock *sk, int len,
6983                                     char __user *optval, int __user *optlen)
6984 {
6985         struct sctp_authchunks __user *p = (void __user *)optval;
6986         struct sctp_authchunks val;
6987         struct sctp_association *asoc;
6988         struct sctp_chunks_param *ch;
6989         u32    num_chunks = 0;
6990         char __user *to;
6991 
6992         if (len < sizeof(struct sctp_authchunks))
6993                 return -EINVAL;
6994 
6995         if (copy_from_user(&val, optval, sizeof(val)))
6996                 return -EFAULT;
6997 
6998         to = p->gauth_chunks;
6999         asoc = sctp_id2assoc(sk, val.gauth_assoc_id);
7000         if (!asoc)
7001                 return -EINVAL;
7002 
7003         if (!asoc->peer.auth_capable)
7004                 return -EACCES;
7005 
7006         ch = asoc->peer.peer_chunks;
7007         if (!ch)
7008                 goto num;
7009 
7010         
7011         num_chunks = ntohs(ch->param_hdr.length) - sizeof(struct sctp_paramhdr);
7012         if (len < num_chunks)
7013                 return -EINVAL;
7014 
7015         if (copy_to_user(to, ch->chunks, num_chunks))
7016                 return -EFAULT;
7017 num:
7018         len = sizeof(struct sctp_authchunks) + num_chunks;
7019         if (put_user(len, optlen))
7020                 return -EFAULT;
7021         if (put_user(num_chunks, &p->gauth_number_of_chunks))
7022                 return -EFAULT;
7023         return 0;
7024 }
7025 
7026 static int sctp_getsockopt_local_auth_chunks(struct sock *sk, int len,
7027                                     char __user *optval, int __user *optlen)
7028 {
7029         struct sctp_endpoint *ep = sctp_sk(sk)->ep;
7030         struct sctp_authchunks __user *p = (void __user *)optval;
7031         struct sctp_authchunks val;
7032         struct sctp_association *asoc;
7033         struct sctp_chunks_param *ch;
7034         u32    num_chunks = 0;
7035         char __user *to;
7036 
7037         if (len < sizeof(struct sctp_authchunks))
7038                 return -EINVAL;
7039 
7040         if (copy_from_user(&val, optval, sizeof(val)))
7041                 return -EFAULT;
7042 
7043         to = p->gauth_chunks;
7044         asoc = sctp_id2assoc(sk, val.gauth_assoc_id);
7045         if (!asoc && val.gauth_assoc_id != SCTP_FUTURE_ASSOC &&
7046             sctp_style(sk, UDP))
7047                 return -EINVAL;
7048 
7049         if (asoc) {
7050                 if (!asoc->peer.auth_capable)
7051                         return -EACCES;
7052                 ch = (struct sctp_chunks_param *)asoc->c.auth_chunks;
7053         } else {
7054                 if (!ep->auth_enable)
7055                         return -EACCES;
7056                 ch = ep->auth_chunk_list;
7057         }
7058         if (!ch)
7059                 goto num;
7060 
7061         num_chunks = ntohs(ch->param_hdr.length) - sizeof(struct sctp_paramhdr);
7062         if (len < sizeof(struct sctp_authchunks) + num_chunks)
7063                 return -EINVAL;
7064 
7065         if (copy_to_user(to, ch->chunks, num_chunks))
7066                 return -EFAULT;
7067 num:
7068         len = sizeof(struct sctp_authchunks) + num_chunks;
7069         if (put_user(len, optlen))
7070                 return -EFAULT;
7071         if (put_user(num_chunks, &p->gauth_number_of_chunks))
7072                 return -EFAULT;
7073 
7074         return 0;
7075 }
7076 
7077 
7078 
7079 
7080 
7081 
7082 static int sctp_getsockopt_assoc_number(struct sock *sk, int len,
7083                                     char __user *optval, int __user *optlen)
7084 {
7085         struct sctp_sock *sp = sctp_sk(sk);
7086         struct sctp_association *asoc;
7087         u32 val = 0;
7088 
7089         if (sctp_style(sk, TCP))
7090                 return -EOPNOTSUPP;
7091 
7092         if (len < sizeof(u32))
7093                 return -EINVAL;
7094 
7095         len = sizeof(u32);
7096 
7097         list_for_each_entry(asoc, &(sp->ep->asocs), asocs) {
7098                 val++;
7099         }
7100 
7101         if (put_user(len, optlen))
7102                 return -EFAULT;
7103         if (copy_to_user(optval, &val, len))
7104                 return -EFAULT;
7105 
7106         return 0;
7107 }
7108 
7109 
7110 
7111 
7112 
7113 static int sctp_getsockopt_auto_asconf(struct sock *sk, int len,
7114                                    char __user *optval, int __user *optlen)
7115 {
7116         int val = 0;
7117 
7118         if (len < sizeof(int))
7119                 return -EINVAL;
7120 
7121         len = sizeof(int);
7122         if (sctp_sk(sk)->do_auto_asconf && sctp_is_ep_boundall(sk))
7123                 val = 1;
7124         if (put_user(len, optlen))
7125                 return -EFAULT;
7126         if (copy_to_user(optval, &val, len))
7127                 return -EFAULT;
7128         return 0;
7129 }
7130 
7131 
7132 
7133 
7134 
7135 
7136 
7137 
7138 static int sctp_getsockopt_assoc_ids(struct sock *sk, int len,
7139                                     char __user *optval, int __user *optlen)
7140 {
7141         struct sctp_sock *sp = sctp_sk(sk);
7142         struct sctp_association *asoc;
7143         struct sctp_assoc_ids *ids;
7144         u32 num = 0;
7145 
7146         if (sctp_style(sk, TCP))
7147                 return -EOPNOTSUPP;
7148 
7149         if (len < sizeof(struct sctp_assoc_ids))
7150                 return -EINVAL;
7151 
7152         list_for_each_entry(asoc, &(sp->ep->asocs), asocs) {
7153                 num++;
7154         }
7155 
7156         if (len < sizeof(struct sctp_assoc_ids) + sizeof(sctp_assoc_t) * num)
7157                 return -EINVAL;
7158 
7159         len = sizeof(struct sctp_assoc_ids) + sizeof(sctp_assoc_t) * num;
7160 
7161         ids = kmalloc(len, GFP_USER | __GFP_NOWARN);
7162         if (unlikely(!ids))
7163                 return -ENOMEM;
7164 
7165         ids->gaids_number_of_ids = num;
7166         num = 0;
7167         list_for_each_entry(asoc, &(sp->ep->asocs), asocs) {
7168                 ids->gaids_assoc_id[num++] = asoc->assoc_id;
7169         }
7170 
7171         if (put_user(len, optlen) || copy_to_user(optval, ids, len)) {
7172                 kfree(ids);
7173                 return -EFAULT;
7174         }
7175 
7176         kfree(ids);
7177         return 0;
7178 }
7179 
7180 
7181 
7182 
7183 
7184 
7185 
7186 
7187 static int sctp_getsockopt_paddr_thresholds(struct sock *sk,
7188                                             char __user *optval,
7189                                             int len,
7190                                             int __user *optlen)
7191 {
7192         struct sctp_paddrthlds val;
7193         struct sctp_transport *trans;
7194         struct sctp_association *asoc;
7195 
7196         if (len < sizeof(struct sctp_paddrthlds))
7197                 return -EINVAL;
7198         len = sizeof(struct sctp_paddrthlds);
7199         if (copy_from_user(&val, (struct sctp_paddrthlds __user *)optval, len))
7200                 return -EFAULT;
7201 
7202         if (!sctp_is_any(sk, (const union sctp_addr *)&val.spt_address)) {
7203                 trans = sctp_addr_id2transport(sk, &val.spt_address,
7204                                                val.spt_assoc_id);
7205                 if (!trans)
7206                         return -ENOENT;
7207 
7208                 val.spt_pathmaxrxt = trans->pathmaxrxt;
7209                 val.spt_pathpfthld = trans->pf_retrans;
7210 
7211                 goto out;
7212         }
7213 
7214         asoc = sctp_id2assoc(sk, val.spt_assoc_id);
7215         if (!asoc && val.spt_assoc_id != SCTP_FUTURE_ASSOC &&
7216             sctp_style(sk, UDP))
7217                 return -EINVAL;
7218 
7219         if (asoc) {
7220                 val.spt_pathpfthld = asoc->pf_retrans;
7221                 val.spt_pathmaxrxt = asoc->pathmaxrxt;
7222         } else {
7223                 struct sctp_sock *sp = sctp_sk(sk);
7224 
7225                 val.spt_pathpfthld = sp->pf_retrans;
7226                 val.spt_pathmaxrxt = sp->pathmaxrxt;
7227         }
7228 
7229 out:
7230         if (put_user(len, optlen) || copy_to_user(optval, &val, len))
7231                 return -EFAULT;
7232 
7233         return 0;
7234 }
7235 
7236 
7237 
7238 
7239 
7240 
7241 
7242 static int sctp_getsockopt_assoc_stats(struct sock *sk, int len,
7243                                        char __user *optval,
7244                                        int __user *optlen)
7245 {
7246         struct sctp_assoc_stats sas;
7247         struct sctp_association *asoc = NULL;
7248 
7249         
7250         if (len < sizeof(sctp_assoc_t))
7251                 return -EINVAL;
7252 
7253         
7254         len = min_t(size_t, len, sizeof(sas));
7255 
7256         if (copy_from_user(&sas, optval, len))
7257                 return -EFAULT;
7258 
7259         asoc = sctp_id2assoc(sk, sas.sas_assoc_id);
7260         if (!asoc)
7261                 return -EINVAL;
7262 
7263         sas.sas_rtxchunks = asoc->stats.rtxchunks;
7264         sas.sas_gapcnt = asoc->stats.gapcnt;
7265         sas.sas_outofseqtsns = asoc->stats.outofseqtsns;
7266         sas.sas_osacks = asoc->stats.osacks;
7267         sas.sas_isacks = asoc->stats.isacks;
7268         sas.sas_octrlchunks = asoc->stats.octrlchunks;
7269         sas.sas_ictrlchunks = asoc->stats.ictrlchunks;
7270         sas.sas_oodchunks = asoc->stats.oodchunks;
7271         sas.sas_iodchunks = asoc->stats.iodchunks;
7272         sas.sas_ouodchunks = asoc->stats.ouodchunks;
7273         sas.sas_iuodchunks = asoc->stats.iuodchunks;
7274         sas.sas_idupchunks = asoc->stats.idupchunks;
7275         sas.sas_opackets = asoc->stats.opackets;
7276         sas.sas_ipackets = asoc->stats.ipackets;
7277 
7278         
7279 
7280 
7281 
7282         sas.sas_maxrto = asoc->stats.max_obs_rto;
7283         memcpy(&sas.sas_obs_rto_ipaddr, &asoc->stats.obs_rto_ipaddr,
7284                 sizeof(struct sockaddr_storage));
7285 
7286         
7287         asoc->stats.max_obs_rto = asoc->rto_min;
7288 
7289         if (put_user(len, optlen))
7290                 return -EFAULT;
7291 
7292         pr_debug("%s: len:%d, assoc_id:%d\n", __func__, len, sas.sas_assoc_id);
7293 
7294         if (copy_to_user(optval, &sas, len))
7295                 return -EFAULT;
7296 
7297         return 0;
7298 }
7299 
7300 static int sctp_getsockopt_recvrcvinfo(struct sock *sk, int len,
7301                                        char __user *optval,
7302                                        int __user *optlen)
7303 {
7304         int val = 0;
7305 
7306         if (len < sizeof(int))
7307                 return -EINVAL;
7308 
7309         len = sizeof(int);
7310         if (sctp_sk(sk)->recvrcvinfo)
7311                 val = 1;
7312         if (put_user(len, optlen))
7313                 return -EFAULT;
7314         if (copy_to_user(optval, &val, len))
7315                 return -EFAULT;
7316 
7317         return 0;
7318 }
7319 
7320 static int sctp_getsockopt_recvnxtinfo(struct sock *sk, int len,
7321                                        char __user *optval,
7322                                        int __user *optlen)
7323 {
7324         int val = 0;
7325 
7326         if (len < sizeof(int))
7327                 return -EINVAL;
7328 
7329         len = sizeof(int);
7330         if (sctp_sk(sk)->recvnxtinfo)
7331                 val = 1;
7332         if (put_user(len, optlen))
7333                 return -EFAULT;
7334         if (copy_to_user(optval, &val, len))
7335                 return -EFAULT;
7336 
7337         return 0;
7338 }
7339 
7340 static int sctp_getsockopt_pr_supported(struct sock *sk, int len,
7341                                         char __user *optval,
7342                                         int __user *optlen)
7343 {
7344         struct sctp_assoc_value params;
7345         struct sctp_association *asoc;
7346         int retval = -EFAULT;
7347 
7348         if (len < sizeof(params)) {
7349                 retval = -EINVAL;
7350                 goto out;
7351         }
7352 
7353         len = sizeof(params);
7354         if (copy_from_user(¶ms, optval, len))
7355                 goto out;
7356 
7357         asoc = sctp_id2assoc(sk, params.assoc_id);
7358         if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
7359             sctp_style(sk, UDP)) {
7360                 retval = -EINVAL;
7361                 goto out;
7362         }
7363 
7364         params.assoc_value = asoc ? asoc->peer.prsctp_capable
7365                                   : sctp_sk(sk)->ep->prsctp_enable;
7366 
7367         if (put_user(len, optlen))
7368                 goto out;
7369 
7370         if (copy_to_user(optval, ¶ms, len))
7371                 goto out;
7372 
7373         retval = 0;
7374 
7375 out:
7376         return retval;
7377 }
7378 
7379 static int sctp_getsockopt_default_prinfo(struct sock *sk, int len,
7380                                           char __user *optval,
7381                                           int __user *optlen)
7382 {
7383         struct sctp_default_prinfo info;
7384         struct sctp_association *asoc;
7385         int retval = -EFAULT;
7386 
7387         if (len < sizeof(info)) {
7388                 retval = -EINVAL;
7389                 goto out;
7390         }
7391 
7392         len = sizeof(info);
7393         if (copy_from_user(&info, optval, len))
7394                 goto out;
7395 
7396         asoc = sctp_id2assoc(sk, info.pr_assoc_id);
7397         if (!asoc && info.pr_assoc_id != SCTP_FUTURE_ASSOC &&
7398             sctp_style(sk, UDP)) {
7399                 retval = -EINVAL;
7400                 goto out;
7401         }
7402 
7403         if (asoc) {
7404                 info.pr_policy = SCTP_PR_POLICY(asoc->default_flags);
7405                 info.pr_value = asoc->default_timetolive;
7406         } else {
7407                 struct sctp_sock *sp = sctp_sk(sk);
7408 
7409                 info.pr_policy = SCTP_PR_POLICY(sp->default_flags);
7410                 info.pr_value = sp->default_timetolive;
7411         }
7412 
7413         if (put_user(len, optlen))
7414                 goto out;
7415 
7416         if (copy_to_user(optval, &info, len))
7417                 goto out;
7418 
7419         retval = 0;
7420 
7421 out:
7422         return retval;
7423 }
7424 
7425 static int sctp_getsockopt_pr_assocstatus(struct sock *sk, int len,
7426                                           char __user *optval,
7427                                           int __user *optlen)
7428 {
7429         struct sctp_prstatus params;
7430         struct sctp_association *asoc;
7431         int policy;
7432         int retval = -EINVAL;
7433 
7434         if (len < sizeof(params))
7435                 goto out;
7436 
7437         len = sizeof(params);
7438         if (copy_from_user(¶ms, optval, len)) {
7439                 retval = -EFAULT;
7440                 goto out;
7441         }
7442 
7443         policy = params.sprstat_policy;
7444         if (!policy || (policy & ~(SCTP_PR_SCTP_MASK | SCTP_PR_SCTP_ALL)) ||
7445             ((policy & SCTP_PR_SCTP_ALL) && (policy & SCTP_PR_SCTP_MASK)))
7446                 goto out;
7447 
7448         asoc = sctp_id2assoc(sk, params.sprstat_assoc_id);
7449         if (!asoc)
7450                 goto out;
7451 
7452         if (policy == SCTP_PR_SCTP_ALL) {
7453                 params.sprstat_abandoned_unsent = 0;
7454                 params.sprstat_abandoned_sent = 0;
7455                 for (policy = 0; policy <= SCTP_PR_INDEX(MAX); policy++) {
7456                         params.sprstat_abandoned_unsent +=
7457                                 asoc->abandoned_unsent[policy];
7458                         params.sprstat_abandoned_sent +=
7459                                 asoc->abandoned_sent[policy];
7460                 }
7461         } else {
7462                 params.sprstat_abandoned_unsent =
7463                         asoc->abandoned_unsent[__SCTP_PR_INDEX(policy)];
7464                 params.sprstat_abandoned_sent =
7465                         asoc->abandoned_sent[__SCTP_PR_INDEX(policy)];
7466         }
7467 
7468         if (put_user(len, optlen)) {
7469                 retval = -EFAULT;
7470                 goto out;
7471         }
7472 
7473         if (copy_to_user(optval, ¶ms, len)) {
7474                 retval = -EFAULT;
7475                 goto out;
7476         }
7477 
7478         retval = 0;
7479 
7480 out:
7481         return retval;
7482 }
7483 
7484 static int sctp_getsockopt_pr_streamstatus(struct sock *sk, int len,
7485                                            char __user *optval,
7486                                            int __user *optlen)
7487 {
7488         struct sctp_stream_out_ext *streamoute;
7489         struct sctp_association *asoc;
7490         struct sctp_prstatus params;
7491         int retval = -EINVAL;
7492         int policy;
7493 
7494         if (len < sizeof(params))
7495                 goto out;
7496 
7497         len = sizeof(params);
7498         if (copy_from_user(¶ms, optval, len)) {
7499                 retval = -EFAULT;
7500                 goto out;
7501         }
7502 
7503         policy = params.sprstat_policy;
7504         if (!policy || (policy & ~(SCTP_PR_SCTP_MASK | SCTP_PR_SCTP_ALL)) ||
7505             ((policy & SCTP_PR_SCTP_ALL) && (policy & SCTP_PR_SCTP_MASK)))
7506                 goto out;
7507 
7508         asoc = sctp_id2assoc(sk, params.sprstat_assoc_id);
7509         if (!asoc || params.sprstat_sid >= asoc->stream.outcnt)
7510                 goto out;
7511 
7512         streamoute = SCTP_SO(&asoc->stream, params.sprstat_sid)->ext;
7513         if (!streamoute) {
7514                 
7515                 params.sprstat_abandoned_unsent = 0;
7516                 params.sprstat_abandoned_sent = 0;
7517                 retval = 0;
7518                 goto out;
7519         }
7520 
7521         if (policy == SCTP_PR_SCTP_ALL) {
7522                 params.sprstat_abandoned_unsent = 0;
7523                 params.sprstat_abandoned_sent = 0;
7524                 for (policy = 0; policy <= SCTP_PR_INDEX(MAX); policy++) {
7525                         params.sprstat_abandoned_unsent +=
7526                                 streamoute->abandoned_unsent[policy];
7527                         params.sprstat_abandoned_sent +=
7528                                 streamoute->abandoned_sent[policy];
7529                 }
7530         } else {
7531                 params.sprstat_abandoned_unsent =
7532                         streamoute->abandoned_unsent[__SCTP_PR_INDEX(policy)];
7533                 params.sprstat_abandoned_sent =
7534                         streamoute->abandoned_sent[__SCTP_PR_INDEX(policy)];
7535         }
7536 
7537         if (put_user(len, optlen) || copy_to_user(optval, ¶ms, len)) {
7538                 retval = -EFAULT;
7539                 goto out;
7540         }
7541 
7542         retval = 0;
7543 
7544 out:
7545         return retval;
7546 }
7547 
7548 static int sctp_getsockopt_reconfig_supported(struct sock *sk, int len,
7549                                               char __user *optval,
7550                                               int __user *optlen)
7551 {
7552         struct sctp_assoc_value params;
7553         struct sctp_association *asoc;
7554         int retval = -EFAULT;
7555 
7556         if (len < sizeof(params)) {
7557                 retval = -EINVAL;
7558                 goto out;
7559         }
7560 
7561         len = sizeof(params);
7562         if (copy_from_user(¶ms, optval, len))
7563                 goto out;
7564 
7565         asoc = sctp_id2assoc(sk, params.assoc_id);
7566         if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
7567             sctp_style(sk, UDP)) {
7568                 retval = -EINVAL;
7569                 goto out;
7570         }
7571 
7572         params.assoc_value = asoc ? asoc->peer.reconf_capable
7573                                   : sctp_sk(sk)->ep->reconf_enable;
7574 
7575         if (put_user(len, optlen))
7576                 goto out;
7577 
7578         if (copy_to_user(optval, ¶ms, len))
7579                 goto out;
7580 
7581         retval = 0;
7582 
7583 out:
7584         return retval;
7585 }
7586 
7587 static int sctp_getsockopt_enable_strreset(struct sock *sk, int len,
7588                                            char __user *optval,
7589                                            int __user *optlen)
7590 {
7591         struct sctp_assoc_value params;
7592         struct sctp_association *asoc;
7593         int retval = -EFAULT;
7594 
7595         if (len < sizeof(params)) {
7596                 retval = -EINVAL;
7597                 goto out;
7598         }
7599 
7600         len = sizeof(params);
7601         if (copy_from_user(¶ms, optval, len))
7602                 goto out;
7603 
7604         asoc = sctp_id2assoc(sk, params.assoc_id);
7605         if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
7606             sctp_style(sk, UDP)) {
7607                 retval = -EINVAL;
7608                 goto out;
7609         }
7610 
7611         params.assoc_value = asoc ? asoc->strreset_enable
7612                                   : sctp_sk(sk)->ep->strreset_enable;
7613 
7614         if (put_user(len, optlen))
7615                 goto out;
7616 
7617         if (copy_to_user(optval, ¶ms, len))
7618                 goto out;
7619 
7620         retval = 0;
7621 
7622 out:
7623         return retval;
7624 }
7625 
7626 static int sctp_getsockopt_scheduler(struct sock *sk, int len,
7627                                      char __user *optval,
7628                                      int __user *optlen)
7629 {
7630         struct sctp_assoc_value params;
7631         struct sctp_association *asoc;
7632         int retval = -EFAULT;
7633 
7634         if (len < sizeof(params)) {
7635                 retval = -EINVAL;
7636                 goto out;
7637         }
7638 
7639         len = sizeof(params);
7640         if (copy_from_user(¶ms, optval, len))
7641                 goto out;
7642 
7643         asoc = sctp_id2assoc(sk, params.assoc_id);
7644         if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
7645             sctp_style(sk, UDP)) {
7646                 retval = -EINVAL;
7647                 goto out;
7648         }
7649 
7650         params.assoc_value = asoc ? sctp_sched_get_sched(asoc)
7651                                   : sctp_sk(sk)->default_ss;
7652 
7653         if (put_user(len, optlen))
7654                 goto out;
7655 
7656         if (copy_to_user(optval, ¶ms, len))
7657                 goto out;
7658 
7659         retval = 0;
7660 
7661 out:
7662         return retval;
7663 }
7664 
7665 static int sctp_getsockopt_scheduler_value(struct sock *sk, int len,
7666                                            char __user *optval,
7667                                            int __user *optlen)
7668 {
7669         struct sctp_stream_value params;
7670         struct sctp_association *asoc;
7671         int retval = -EFAULT;
7672 
7673         if (len < sizeof(params)) {
7674                 retval = -EINVAL;
7675                 goto out;
7676         }
7677 
7678         len = sizeof(params);
7679         if (copy_from_user(¶ms, optval, len))
7680                 goto out;
7681 
7682         asoc = sctp_id2assoc(sk, params.assoc_id);
7683         if (!asoc) {
7684                 retval = -EINVAL;
7685                 goto out;
7686         }
7687 
7688         retval = sctp_sched_get_value(asoc, params.stream_id,
7689                                       ¶ms.stream_value);
7690         if (retval)
7691                 goto out;
7692 
7693         if (put_user(len, optlen)) {
7694                 retval = -EFAULT;
7695                 goto out;
7696         }
7697 
7698         if (copy_to_user(optval, ¶ms, len)) {
7699                 retval = -EFAULT;
7700                 goto out;
7701         }
7702 
7703 out:
7704         return retval;
7705 }
7706 
7707 static int sctp_getsockopt_interleaving_supported(struct sock *sk, int len,
7708                                                   char __user *optval,
7709                                                   int __user *optlen)
7710 {
7711         struct sctp_assoc_value params;
7712         struct sctp_association *asoc;
7713         int retval = -EFAULT;
7714 
7715         if (len < sizeof(params)) {
7716                 retval = -EINVAL;
7717                 goto out;
7718         }
7719 
7720         len = sizeof(params);
7721         if (copy_from_user(¶ms, optval, len))
7722                 goto out;
7723 
7724         asoc = sctp_id2assoc(sk, params.assoc_id);
7725         if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
7726             sctp_style(sk, UDP)) {
7727                 retval = -EINVAL;
7728                 goto out;
7729         }
7730 
7731         params.assoc_value = asoc ? asoc->peer.intl_capable
7732                                   : sctp_sk(sk)->ep->intl_enable;
7733 
7734         if (put_user(len, optlen))
7735                 goto out;
7736 
7737         if (copy_to_user(optval, ¶ms, len))
7738                 goto out;
7739 
7740         retval = 0;
7741 
7742 out:
7743         return retval;
7744 }
7745 
7746 static int sctp_getsockopt_reuse_port(struct sock *sk, int len,
7747                                       char __user *optval,
7748                                       int __user *optlen)
7749 {
7750         int val;
7751 
7752         if (len < sizeof(int))
7753                 return -EINVAL;
7754 
7755         len = sizeof(int);
7756         val = sctp_sk(sk)->reuse;
7757         if (put_user(len, optlen))
7758                 return -EFAULT;
7759 
7760         if (copy_to_user(optval, &val, len))
7761                 return -EFAULT;
7762 
7763         return 0;
7764 }
7765 
7766 static int sctp_getsockopt_event(struct sock *sk, int len, char __user *optval,
7767                                  int __user *optlen)
7768 {
7769         struct sctp_association *asoc;
7770         struct sctp_event param;
7771         __u16 subscribe;
7772 
7773         if (len < sizeof(param))
7774                 return -EINVAL;
7775 
7776         len = sizeof(param);
7777         if (copy_from_user(¶m, optval, len))
7778                 return -EFAULT;
7779 
7780         if (param.se_type < SCTP_SN_TYPE_BASE ||
7781             param.se_type > SCTP_SN_TYPE_MAX)
7782                 return -EINVAL;
7783 
7784         asoc = sctp_id2assoc(sk, param.se_assoc_id);
7785         if (!asoc && param.se_assoc_id != SCTP_FUTURE_ASSOC &&
7786             sctp_style(sk, UDP))
7787                 return -EINVAL;
7788 
7789         subscribe = asoc ? asoc->subscribe : sctp_sk(sk)->subscribe;
7790         param.se_on = sctp_ulpevent_type_enabled(subscribe, param.se_type);
7791 
7792         if (put_user(len, optlen))
7793                 return -EFAULT;
7794 
7795         if (copy_to_user(optval, ¶m, len))
7796                 return -EFAULT;
7797 
7798         return 0;
7799 }
7800 
7801 static int sctp_getsockopt_asconf_supported(struct sock *sk, int len,
7802                                             char __user *optval,
7803                                             int __user *optlen)
7804 {
7805         struct sctp_assoc_value params;
7806         struct sctp_association *asoc;
7807         int retval = -EFAULT;
7808 
7809         if (len < sizeof(params)) {
7810                 retval = -EINVAL;
7811                 goto out;
7812         }
7813 
7814         len = sizeof(params);
7815         if (copy_from_user(¶ms, optval, len))
7816                 goto out;
7817 
7818         asoc = sctp_id2assoc(sk, params.assoc_id);
7819         if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
7820             sctp_style(sk, UDP)) {
7821                 retval = -EINVAL;
7822                 goto out;
7823         }
7824 
7825         params.assoc_value = asoc ? asoc->peer.asconf_capable
7826                                   : sctp_sk(sk)->ep->asconf_enable;
7827 
7828         if (put_user(len, optlen))
7829                 goto out;
7830 
7831         if (copy_to_user(optval, ¶ms, len))
7832                 goto out;
7833 
7834         retval = 0;
7835 
7836 out:
7837         return retval;
7838 }
7839 
7840 static int sctp_getsockopt_auth_supported(struct sock *sk, int len,
7841                                           char __user *optval,
7842                                           int __user *optlen)
7843 {
7844         struct sctp_assoc_value params;
7845         struct sctp_association *asoc;
7846         int retval = -EFAULT;
7847 
7848         if (len < sizeof(params)) {
7849                 retval = -EINVAL;
7850                 goto out;
7851         }
7852 
7853         len = sizeof(params);
7854         if (copy_from_user(¶ms, optval, len))
7855                 goto out;
7856 
7857         asoc = sctp_id2assoc(sk, params.assoc_id);
7858         if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
7859             sctp_style(sk, UDP)) {
7860                 retval = -EINVAL;
7861                 goto out;
7862         }
7863 
7864         params.assoc_value = asoc ? asoc->peer.auth_capable
7865                                   : sctp_sk(sk)->ep->auth_enable;
7866 
7867         if (put_user(len, optlen))
7868                 goto out;
7869 
7870         if (copy_to_user(optval, ¶ms, len))
7871                 goto out;
7872 
7873         retval = 0;
7874 
7875 out:
7876         return retval;
7877 }
7878 
7879 static int sctp_getsockopt_ecn_supported(struct sock *sk, int len,
7880                                          char __user *optval,
7881                                          int __user *optlen)
7882 {
7883         struct sctp_assoc_value params;
7884         struct sctp_association *asoc;
7885         int retval = -EFAULT;
7886 
7887         if (len < sizeof(params)) {
7888                 retval = -EINVAL;
7889                 goto out;
7890         }
7891 
7892         len = sizeof(params);
7893         if (copy_from_user(¶ms, optval, len))
7894                 goto out;
7895 
7896         asoc = sctp_id2assoc(sk, params.assoc_id);
7897         if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
7898             sctp_style(sk, UDP)) {
7899                 retval = -EINVAL;
7900                 goto out;
7901         }
7902 
7903         params.assoc_value = asoc ? asoc->peer.ecn_capable
7904                                   : sctp_sk(sk)->ep->ecn_enable;
7905 
7906         if (put_user(len, optlen))
7907                 goto out;
7908 
7909         if (copy_to_user(optval, ¶ms, len))
7910                 goto out;
7911 
7912         retval = 0;
7913 
7914 out:
7915         return retval;
7916 }
7917 
7918 static int sctp_getsockopt(struct sock *sk, int level, int optname,
7919                            char __user *optval, int __user *optlen)
7920 {
7921         int retval = 0;
7922         int len;
7923 
7924         pr_debug("%s: sk:%p, optname:%d\n", __func__, sk, optname);
7925 
7926         
7927 
7928 
7929 
7930 
7931 
7932         if (level != SOL_SCTP) {
7933                 struct sctp_af *af = sctp_sk(sk)->pf->af;
7934 
7935                 retval = af->getsockopt(sk, level, optname, optval, optlen);
7936                 return retval;
7937         }
7938 
7939         if (get_user(len, optlen))
7940                 return -EFAULT;
7941 
7942         if (len < 0)
7943                 return -EINVAL;
7944 
7945         lock_sock(sk);
7946 
7947         switch (optname) {
7948         case SCTP_STATUS:
7949                 retval = sctp_getsockopt_sctp_status(sk, len, optval, optlen);
7950                 break;
7951         case SCTP_DISABLE_FRAGMENTS:
7952                 retval = sctp_getsockopt_disable_fragments(sk, len, optval,
7953                                                            optlen);
7954                 break;
7955         case SCTP_EVENTS:
7956                 retval = sctp_getsockopt_events(sk, len, optval, optlen);
7957                 break;
7958         case SCTP_AUTOCLOSE:
7959                 retval = sctp_getsockopt_autoclose(sk, len, optval, optlen);
7960                 break;
7961         case SCTP_SOCKOPT_PEELOFF:
7962                 retval = sctp_getsockopt_peeloff(sk, len, optval, optlen);
7963                 break;
7964         case SCTP_SOCKOPT_PEELOFF_FLAGS:
7965                 retval = sctp_getsockopt_peeloff_flags(sk, len, optval, optlen);
7966                 break;
7967         case SCTP_PEER_ADDR_PARAMS:
7968                 retval = sctp_getsockopt_peer_addr_params(sk, len, optval,
7969                                                           optlen);
7970                 break;
7971         case SCTP_DELAYED_SACK:
7972                 retval = sctp_getsockopt_delayed_ack(sk, len, optval,
7973                                                           optlen);
7974                 break;
7975         case SCTP_INITMSG:
7976                 retval = sctp_getsockopt_initmsg(sk, len, optval, optlen);
7977                 break;
7978         case SCTP_GET_PEER_ADDRS:
7979                 retval = sctp_getsockopt_peer_addrs(sk, len, optval,
7980                                                     optlen);
7981                 break;
7982         case SCTP_GET_LOCAL_ADDRS:
7983                 retval = sctp_getsockopt_local_addrs(sk, len, optval,
7984                                                      optlen);
7985                 break;
7986         case SCTP_SOCKOPT_CONNECTX3:
7987                 retval = sctp_getsockopt_connectx3(sk, len, optval, optlen);
7988                 break;
7989         case SCTP_DEFAULT_SEND_PARAM:
7990                 retval = sctp_getsockopt_default_send_param(sk, len,
7991                                                             optval, optlen);
7992                 break;
7993         case SCTP_DEFAULT_SNDINFO:
7994                 retval = sctp_getsockopt_default_sndinfo(sk, len,
7995                                                          optval, optlen);
7996                 break;
7997         case SCTP_PRIMARY_ADDR:
7998                 retval = sctp_getsockopt_primary_addr(sk, len, optval, optlen);
7999                 break;
8000         case SCTP_NODELAY:
8001                 retval = sctp_getsockopt_nodelay(sk, len, optval, optlen);
8002                 break;
8003         case SCTP_RTOINFO:
8004                 retval = sctp_getsockopt_rtoinfo(sk, len, optval, optlen);
8005                 break;
8006         case SCTP_ASSOCINFO:
8007                 retval = sctp_getsockopt_associnfo(sk, len, optval, optlen);
8008                 break;
8009         case SCTP_I_WANT_MAPPED_V4_ADDR:
8010                 retval = sctp_getsockopt_mappedv4(sk, len, optval, optlen);
8011                 break;
8012         case SCTP_MAXSEG:
8013                 retval = sctp_getsockopt_maxseg(sk, len, optval, optlen);
8014                 break;
8015         case SCTP_GET_PEER_ADDR_INFO:
8016                 retval = sctp_getsockopt_peer_addr_info(sk, len, optval,
8017                                                         optlen);
8018                 break;
8019         case SCTP_ADAPTATION_LAYER:
8020                 retval = sctp_getsockopt_adaptation_layer(sk, len, optval,
8021                                                         optlen);
8022                 break;
8023         case SCTP_CONTEXT:
8024                 retval = sctp_getsockopt_context(sk, len, optval, optlen);
8025                 break;
8026         case SCTP_FRAGMENT_INTERLEAVE:
8027                 retval = sctp_getsockopt_fragment_interleave(sk, len, optval,
8028                                                              optlen);
8029                 break;
8030         case SCTP_PARTIAL_DELIVERY_POINT:
8031                 retval = sctp_getsockopt_partial_delivery_point(sk, len, optval,
8032                                                                 optlen);
8033                 break;
8034         case SCTP_MAX_BURST:
8035                 retval = sctp_getsockopt_maxburst(sk, len, optval, optlen);
8036                 break;
8037         case SCTP_AUTH_KEY:
8038         case SCTP_AUTH_CHUNK:
8039         case SCTP_AUTH_DELETE_KEY:
8040         case SCTP_AUTH_DEACTIVATE_KEY:
8041                 retval = -EOPNOTSUPP;
8042                 break;
8043         case SCTP_HMAC_IDENT:
8044                 retval = sctp_getsockopt_hmac_ident(sk, len, optval, optlen);
8045                 break;
8046         case SCTP_AUTH_ACTIVE_KEY:
8047                 retval = sctp_getsockopt_active_key(sk, len, optval, optlen);
8048                 break;
8049         case SCTP_PEER_AUTH_CHUNKS:
8050                 retval = sctp_getsockopt_peer_auth_chunks(sk, len, optval,
8051                                                         optlen);
8052                 break;
8053         case SCTP_LOCAL_AUTH_CHUNKS:
8054                 retval = sctp_getsockopt_local_auth_chunks(sk, len, optval,
8055                                                         optlen);
8056                 break;
8057         case SCTP_GET_ASSOC_NUMBER:
8058                 retval = sctp_getsockopt_assoc_number(sk, len, optval, optlen);
8059                 break;
8060         case SCTP_GET_ASSOC_ID_LIST:
8061                 retval = sctp_getsockopt_assoc_ids(sk, len, optval, optlen);
8062                 break;
8063         case SCTP_AUTO_ASCONF:
8064                 retval = sctp_getsockopt_auto_asconf(sk, len, optval, optlen);
8065                 break;
8066         case SCTP_PEER_ADDR_THLDS:
8067                 retval = sctp_getsockopt_paddr_thresholds(sk, optval, len, optlen);
8068                 break;
8069         case SCTP_GET_ASSOC_STATS:
8070                 retval = sctp_getsockopt_assoc_stats(sk, len, optval, optlen);
8071                 break;
8072         case SCTP_RECVRCVINFO:
8073                 retval = sctp_getsockopt_recvrcvinfo(sk, len, optval, optlen);
8074                 break;
8075         case SCTP_RECVNXTINFO:
8076                 retval = sctp_getsockopt_recvnxtinfo(sk, len, optval, optlen);
8077                 break;
8078         case SCTP_PR_SUPPORTED:
8079                 retval = sctp_getsockopt_pr_supported(sk, len, optval, optlen);
8080                 break;
8081         case SCTP_DEFAULT_PRINFO:
8082                 retval = sctp_getsockopt_default_prinfo(sk, len, optval,
8083                                                         optlen);
8084                 break;
8085         case SCTP_PR_ASSOC_STATUS:
8086                 retval = sctp_getsockopt_pr_assocstatus(sk, len, optval,
8087                                                         optlen);
8088                 break;
8089         case SCTP_PR_STREAM_STATUS:
8090                 retval = sctp_getsockopt_pr_streamstatus(sk, len, optval,
8091                                                          optlen);
8092                 break;
8093         case SCTP_RECONFIG_SUPPORTED:
8094                 retval = sctp_getsockopt_reconfig_supported(sk, len, optval,
8095                                                             optlen);
8096                 break;
8097         case SCTP_ENABLE_STREAM_RESET:
8098                 retval = sctp_getsockopt_enable_strreset(sk, len, optval,
8099                                                          optlen);
8100                 break;
8101         case SCTP_STREAM_SCHEDULER:
8102                 retval = sctp_getsockopt_scheduler(sk, len, optval,
8103                                                    optlen);
8104                 break;
8105         case SCTP_STREAM_SCHEDULER_VALUE:
8106                 retval = sctp_getsockopt_scheduler_value(sk, len, optval,
8107                                                          optlen);
8108                 break;
8109         case SCTP_INTERLEAVING_SUPPORTED:
8110                 retval = sctp_getsockopt_interleaving_supported(sk, len, optval,
8111                                                                 optlen);
8112                 break;
8113         case SCTP_REUSE_PORT:
8114                 retval = sctp_getsockopt_reuse_port(sk, len, optval, optlen);
8115                 break;
8116         case SCTP_EVENT:
8117                 retval = sctp_getsockopt_event(sk, len, optval, optlen);
8118                 break;
8119         case SCTP_ASCONF_SUPPORTED:
8120                 retval = sctp_getsockopt_asconf_supported(sk, len, optval,
8121                                                           optlen);
8122                 break;
8123         case SCTP_AUTH_SUPPORTED:
8124                 retval = sctp_getsockopt_auth_supported(sk, len, optval,
8125                                                         optlen);
8126                 break;
8127         case SCTP_ECN_SUPPORTED:
8128                 retval = sctp_getsockopt_ecn_supported(sk, len, optval, optlen);
8129                 break;
8130         default:
8131                 retval = -ENOPROTOOPT;
8132                 break;
8133         }
8134 
8135         release_sock(sk);
8136         return retval;
8137 }
8138 
8139 static int sctp_hash(struct sock *sk)
8140 {
8141         
8142         return 0;
8143 }
8144 
8145 static void sctp_unhash(struct sock *sk)
8146 {
8147         
8148 }
8149 
8150 
8151 
8152 
8153 
8154 
8155 
8156 
8157 
8158 
8159 
8160 
8161 
8162 static struct sctp_bind_bucket *sctp_bucket_create(
8163         struct sctp_bind_hashbucket *head, struct net *, unsigned short snum);
8164 
8165 static int sctp_get_port_local(struct sock *sk, union sctp_addr *addr)
8166 {
8167         struct sctp_sock *sp = sctp_sk(sk);
8168         bool reuse = (sk->sk_reuse || sp->reuse);
8169         struct sctp_bind_hashbucket *head; 
8170         kuid_t uid = sock_i_uid(sk);
8171         struct sctp_bind_bucket *pp;
8172         unsigned short snum;
8173         int ret;
8174 
8175         snum = ntohs(addr->v4.sin_port);
8176 
8177         pr_debug("%s: begins, snum:%d\n", __func__, snum);
8178 
8179         local_bh_disable();
8180 
8181         if (snum == 0) {
8182                 
8183                 int low, high, remaining, index;
8184                 unsigned int rover;
8185                 struct net *net = sock_net(sk);
8186 
8187                 inet_get_local_port_range(net, &low, &high);
8188                 remaining = (high - low) + 1;
8189                 rover = prandom_u32() % remaining + low;
8190 
8191                 do {
8192                         rover++;
8193                         if ((rover < low) || (rover > high))
8194                                 rover = low;
8195                         if (inet_is_local_reserved_port(net, rover))
8196                                 continue;
8197                         index = sctp_phashfn(sock_net(sk), rover);
8198                         head = &sctp_port_hashtable[index];
8199                         spin_lock(&head->lock);
8200                         sctp_for_each_hentry(pp, &head->chain)
8201                                 if ((pp->port == rover) &&
8202                                     net_eq(sock_net(sk), pp->net))
8203                                         goto next;
8204                         break;
8205                 next:
8206                         spin_unlock(&head->lock);
8207                 } while (--remaining > 0);
8208 
8209                 
8210                 ret = 1;
8211                 if (remaining <= 0)
8212                         goto fail;
8213 
8214                 
8215 
8216 
8217 
8218                 snum = rover;
8219         } else {
8220                 
8221 
8222 
8223 
8224 
8225 
8226                 head = &sctp_port_hashtable[sctp_phashfn(sock_net(sk), snum)];
8227                 spin_lock(&head->lock);
8228                 sctp_for_each_hentry(pp, &head->chain) {
8229                         if ((pp->port == snum) && net_eq(pp->net, sock_net(sk)))
8230                                 goto pp_found;
8231                 }
8232         }
8233         pp = NULL;
8234         goto pp_not_found;
8235 pp_found:
8236         if (!hlist_empty(&pp->owner)) {
8237                 
8238 
8239 
8240 
8241 
8242                 struct sock *sk2;
8243 
8244                 pr_debug("%s: found a possible match\n", __func__);
8245 
8246                 if ((pp->fastreuse && reuse &&
8247                      sk->sk_state != SCTP_SS_LISTENING) ||
8248                     (pp->fastreuseport && sk->sk_reuseport &&
8249                      uid_eq(pp->fastuid, uid)))
8250                         goto success;
8251 
8252                 
8253 
8254 
8255 
8256 
8257 
8258 
8259 
8260 
8261 
8262                 sk_for_each_bound(sk2, &pp->owner) {
8263                         struct sctp_sock *sp2 = sctp_sk(sk2);
8264                         struct sctp_endpoint *ep2 = sp2->ep;
8265 
8266                         if (sk == sk2 ||
8267                             (reuse && (sk2->sk_reuse || sp2->reuse) &&
8268                              sk2->sk_state != SCTP_SS_LISTENING) ||
8269                             (sk->sk_reuseport && sk2->sk_reuseport &&
8270                              uid_eq(uid, sock_i_uid(sk2))))
8271                                 continue;
8272 
8273                         if (sctp_bind_addr_conflict(&ep2->base.bind_addr,
8274                                                     addr, sp2, sp)) {
8275                                 ret = 1;
8276                                 goto fail_unlock;
8277                         }
8278                 }
8279 
8280                 pr_debug("%s: found a match\n", __func__);
8281         }
8282 pp_not_found:
8283         
8284         ret = 1;
8285         if (!pp && !(pp = sctp_bucket_create(head, sock_net(sk), snum)))
8286                 goto fail_unlock;
8287 
8288         
8289 
8290 
8291 
8292         if (hlist_empty(&pp->owner)) {
8293                 if (reuse && sk->sk_state != SCTP_SS_LISTENING)
8294                         pp->fastreuse = 1;
8295                 else
8296                         pp->fastreuse = 0;
8297 
8298                 if (sk->sk_reuseport) {
8299                         pp->fastreuseport = 1;
8300                         pp->fastuid = uid;
8301                 } else {
8302                         pp->fastreuseport = 0;
8303                 }
8304         } else {
8305                 if (pp->fastreuse &&
8306                     (!reuse || sk->sk_state == SCTP_SS_LISTENING))
8307                         pp->fastreuse = 0;
8308 
8309                 if (pp->fastreuseport &&
8310                     (!sk->sk_reuseport || !uid_eq(pp->fastuid, uid)))
8311                         pp->fastreuseport = 0;
8312         }
8313 
8314         
8315 
8316 
8317 
8318 success:
8319         if (!sp->bind_hash) {
8320                 inet_sk(sk)->inet_num = snum;
8321                 sk_add_bind_node(sk, &pp->owner);
8322                 sp->bind_hash = pp;
8323         }
8324         ret = 0;
8325 
8326 fail_unlock:
8327         spin_unlock(&head->lock);
8328 
8329 fail:
8330         local_bh_enable();
8331         return ret;
8332 }
8333 
8334 
8335 
8336 
8337 static int sctp_get_port(struct sock *sk, unsigned short snum)
8338 {
8339         union sctp_addr addr;
8340         struct sctp_af *af = sctp_sk(sk)->pf->af;
8341 
8342         
8343         af->from_sk(&addr, sk);
8344         addr.v4.sin_port = htons(snum);
8345 
8346         
8347         return sctp_get_port_local(sk, &addr);
8348 }
8349 
8350 
8351 
8352 
8353 static int sctp_listen_start(struct sock *sk, int backlog)
8354 {
8355         struct sctp_sock *sp = sctp_sk(sk);
8356         struct sctp_endpoint *ep = sp->ep;
8357         struct crypto_shash *tfm = NULL;
8358         char alg[32];
8359 
8360         
8361         if (!sp->hmac && sp->sctp_hmac_alg) {
8362                 sprintf(alg, "hmac(%s)", sp->sctp_hmac_alg);
8363                 tfm = crypto_alloc_shash(alg, 0, 0);
8364                 if (IS_ERR(tfm)) {
8365                         net_info_ratelimited("failed to load transform for %s: %ld\n",
8366                                              sp->sctp_hmac_alg, PTR_ERR(tfm));
8367                         return -ENOSYS;
8368                 }
8369                 sctp_sk(sk)->hmac = tfm;
8370         }
8371 
8372         
8373 
8374 
8375 
8376 
8377 
8378 
8379 
8380 
8381 
8382 
8383         inet_sk_set_state(sk, SCTP_SS_LISTENING);
8384         if (!ep->base.bind_addr.port) {
8385                 if (sctp_autobind(sk))
8386                         return -EAGAIN;
8387         } else {
8388                 if (sctp_get_port(sk, inet_sk(sk)->inet_num)) {
8389                         inet_sk_set_state(sk, SCTP_SS_CLOSED);
8390                         return -EADDRINUSE;
8391                 }
8392         }
8393 
8394         sk->sk_max_ack_backlog = backlog;
8395         return sctp_hash_endpoint(ep);
8396 }
8397 
8398 
8399 
8400 
8401 
8402 
8403 
8404 
8405 
8406 
8407 
8408 
8409 
8410 
8411 
8412 int sctp_inet_listen(struct socket *sock, int backlog)
8413 {
8414         struct sock *sk = sock->sk;
8415         struct sctp_endpoint *ep = sctp_sk(sk)->ep;
8416         int err = -EINVAL;
8417 
8418         if (unlikely(backlog < 0))
8419                 return err;
8420 
8421         lock_sock(sk);
8422 
8423         
8424         if (sctp_style(sk, UDP_HIGH_BANDWIDTH))
8425                 goto out;
8426 
8427         if (sock->state != SS_UNCONNECTED)
8428                 goto out;
8429 
8430         if (!sctp_sstate(sk, LISTENING) && !sctp_sstate(sk, CLOSED))
8431                 goto out;
8432 
8433         
8434         if (!backlog) {
8435                 if (sctp_sstate(sk, CLOSED))
8436                         goto out;
8437 
8438                 err = 0;
8439                 sctp_unhash_endpoint(ep);
8440                 sk->sk_state = SCTP_SS_CLOSED;
8441                 if (sk->sk_reuse || sctp_sk(sk)->reuse)
8442                         sctp_sk(sk)->bind_hash->fastreuse = 1;
8443                 goto out;
8444         }
8445 
8446         
8447         if (sctp_sstate(sk, LISTENING))
8448                 sk->sk_max_ack_backlog = backlog;
8449         else {
8450                 err = sctp_listen_start(sk, backlog);
8451                 if (err)
8452                         goto out;
8453         }
8454 
8455         err = 0;
8456 out:
8457         release_sock(sk);
8458         return err;
8459 }
8460 
8461 
8462 
8463 
8464 
8465 
8466 
8467 
8468 
8469 
8470 
8471 
8472 
8473 
8474 __poll_t sctp_poll(struct file *file, struct socket *sock, poll_table *wait)
8475 {
8476         struct sock *sk = sock->sk;
8477         struct sctp_sock *sp = sctp_sk(sk);
8478         __poll_t mask;
8479 
8480         poll_wait(file, sk_sleep(sk), wait);
8481 
8482         sock_rps_record_flow(sk);
8483 
8484         
8485 
8486 
8487         if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
8488                 return (!list_empty(&sp->ep->asocs)) ?
8489                         (EPOLLIN | EPOLLRDNORM) : 0;
8490 
8491         mask = 0;
8492 
8493         
8494         if (sk->sk_err || !skb_queue_empty_lockless(&sk->sk_error_queue))
8495                 mask |= EPOLLERR |
8496                         (sock_flag(sk, SOCK_SELECT_ERR_QUEUE) ? EPOLLPRI : 0);
8497         if (sk->sk_shutdown & RCV_SHUTDOWN)
8498                 mask |= EPOLLRDHUP | EPOLLIN | EPOLLRDNORM;
8499         if (sk->sk_shutdown == SHUTDOWN_MASK)
8500                 mask |= EPOLLHUP;
8501 
8502         
8503         if (!skb_queue_empty_lockless(&sk->sk_receive_queue))
8504                 mask |= EPOLLIN | EPOLLRDNORM;
8505 
8506         
8507         if (!sctp_style(sk, UDP) && sctp_sstate(sk, CLOSED))
8508                 return mask;
8509 
8510         
8511         if (sctp_writeable(sk)) {
8512                 mask |= EPOLLOUT | EPOLLWRNORM;
8513         } else {
8514                 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
8515                 
8516 
8517 
8518 
8519 
8520 
8521 
8522 
8523                 if (sctp_writeable(sk))
8524                         mask |= EPOLLOUT | EPOLLWRNORM;
8525         }
8526         return mask;
8527 }
8528 
8529 
8530 
8531 
8532 
8533 static struct sctp_bind_bucket *sctp_bucket_create(
8534         struct sctp_bind_hashbucket *head, struct net *net, unsigned short snum)
8535 {
8536         struct sctp_bind_bucket *pp;
8537 
8538         pp = kmem_cache_alloc(sctp_bucket_cachep, GFP_ATOMIC);
8539         if (pp) {
8540                 SCTP_DBG_OBJCNT_INC(bind_bucket);
8541                 pp->port = snum;
8542                 pp->fastreuse = 0;
8543                 INIT_HLIST_HEAD(&pp->owner);
8544                 pp->net = net;
8545                 hlist_add_head(&pp->node, &head->chain);
8546         }
8547         return pp;
8548 }
8549 
8550 
8551 static void sctp_bucket_destroy(struct sctp_bind_bucket *pp)
8552 {
8553         if (pp && hlist_empty(&pp->owner)) {
8554                 __hlist_del(&pp->node);
8555                 kmem_cache_free(sctp_bucket_cachep, pp);
8556                 SCTP_DBG_OBJCNT_DEC(bind_bucket);
8557         }
8558 }
8559 
8560 
8561 static inline void __sctp_put_port(struct sock *sk)
8562 {
8563         struct sctp_bind_hashbucket *head =
8564                 &sctp_port_hashtable[sctp_phashfn(sock_net(sk),
8565                                                   inet_sk(sk)->inet_num)];
8566         struct sctp_bind_bucket *pp;
8567 
8568         spin_lock(&head->lock);
8569         pp = sctp_sk(sk)->bind_hash;
8570         __sk_del_bind_node(sk);
8571         sctp_sk(sk)->bind_hash = NULL;
8572         inet_sk(sk)->inet_num = 0;
8573         sctp_bucket_destroy(pp);
8574         spin_unlock(&head->lock);
8575 }
8576 
8577 void sctp_put_port(struct sock *sk)
8578 {
8579         local_bh_disable();
8580         __sctp_put_port(sk);
8581         local_bh_enable();
8582 }
8583 
8584 
8585 
8586 
8587 
8588 
8589 
8590 static int sctp_autobind(struct sock *sk)
8591 {
8592         union sctp_addr autoaddr;
8593         struct sctp_af *af;
8594         __be16 port;
8595 
8596         
8597         af = sctp_sk(sk)->pf->af;
8598 
8599         port = htons(inet_sk(sk)->inet_num);
8600         af->inaddr_any(&autoaddr, port);
8601 
8602         return sctp_do_bind(sk, &autoaddr, af->sockaddr_len);
8603 }
8604 
8605 
8606 
8607 
8608 
8609 
8610 
8611 
8612 
8613 
8614 
8615 
8616 
8617 
8618 
8619 
8620 
8621 
8622 
8623 
8624 
8625 
8626 
8627 
8628 
8629 
8630 
8631 
8632 
8633 
8634 
8635 
8636 
8637 
8638 
8639 
8640 
8641 
8642 
8643 
8644 static int sctp_msghdr_parse(const struct msghdr *msg, struct sctp_cmsgs *cmsgs)
8645 {
8646         struct msghdr *my_msg = (struct msghdr *)msg;
8647         struct cmsghdr *cmsg;
8648 
8649         for_each_cmsghdr(cmsg, my_msg) {
8650                 if (!CMSG_OK(my_msg, cmsg))
8651                         return -EINVAL;
8652 
8653                 
8654                 if (cmsg->cmsg_level != IPPROTO_SCTP)
8655                         continue;
8656 
8657                 
8658                 switch (cmsg->cmsg_type) {
8659                 case SCTP_INIT:
8660                         
8661 
8662 
8663 
8664 
8665 
8666 
8667 
8668 
8669 
8670 
8671 
8672 
8673                         if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_initmsg)))
8674                                 return -EINVAL;
8675 
8676                         cmsgs->init = CMSG_DATA(cmsg);
8677                         break;
8678 
8679                 case SCTP_SNDRCV:
8680                         
8681 
8682 
8683 
8684 
8685 
8686 
8687 
8688 
8689 
8690 
8691                         if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_sndrcvinfo)))
8692                                 return -EINVAL;
8693 
8694                         cmsgs->srinfo = CMSG_DATA(cmsg);
8695 
8696                         if (cmsgs->srinfo->sinfo_flags &
8697                             ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
8698                               SCTP_SACK_IMMEDIATELY | SCTP_SENDALL |
8699                               SCTP_PR_SCTP_MASK | SCTP_ABORT | SCTP_EOF))
8700                                 return -EINVAL;
8701                         break;
8702 
8703                 case SCTP_SNDINFO:
8704                         
8705 
8706 
8707 
8708 
8709 
8710 
8711 
8712 
8713 
8714 
8715                         if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_sndinfo)))
8716                                 return -EINVAL;
8717 
8718                         cmsgs->sinfo = CMSG_DATA(cmsg);
8719 
8720                         if (cmsgs->sinfo->snd_flags &
8721                             ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
8722                               SCTP_SACK_IMMEDIATELY | SCTP_SENDALL |
8723                               SCTP_PR_SCTP_MASK | SCTP_ABORT | SCTP_EOF))
8724                                 return -EINVAL;
8725                         break;
8726                 case SCTP_PRINFO:
8727                         
8728 
8729 
8730 
8731 
8732 
8733 
8734 
8735 
8736                         if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_prinfo)))
8737                                 return -EINVAL;
8738 
8739                         cmsgs->prinfo = CMSG_DATA(cmsg);
8740                         if (cmsgs->prinfo->pr_policy & ~SCTP_PR_SCTP_MASK)
8741                                 return -EINVAL;
8742 
8743                         if (cmsgs->prinfo->pr_policy == SCTP_PR_SCTP_NONE)
8744                                 cmsgs->prinfo->pr_value = 0;
8745                         break;
8746                 case SCTP_AUTHINFO:
8747                         
8748 
8749 
8750 
8751 
8752 
8753 
8754 
8755 
8756                         if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_authinfo)))
8757                                 return -EINVAL;
8758 
8759                         cmsgs->authinfo = CMSG_DATA(cmsg);
8760                         break;
8761                 case SCTP_DSTADDRV4:
8762                 case SCTP_DSTADDRV6:
8763                         
8764 
8765 
8766 
8767 
8768 
8769 
8770 
8771 
8772 
8773 
8774                         cmsgs->addrs_msg = my_msg;
8775                         break;
8776                 default:
8777                         return -EINVAL;
8778                 }
8779         }
8780 
8781         return 0;
8782 }
8783 
8784 
8785 
8786 
8787 
8788 
8789 static int sctp_wait_for_packet(struct sock *sk, int *err, long *timeo_p)
8790 {
8791         int error;
8792         DEFINE_WAIT(wait);
8793 
8794         prepare_to_wait_exclusive(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
8795 
8796         
8797         error = sock_error(sk);
8798         if (error)
8799                 goto out;
8800 
8801         if (!skb_queue_empty(&sk->sk_receive_queue))
8802                 goto ready;
8803 
8804         
8805         if (sk->sk_shutdown & RCV_SHUTDOWN)
8806                 goto out;
8807 
8808         
8809 
8810 
8811         error = -ENOTCONN;
8812 
8813         
8814         if (list_empty(&sctp_sk(sk)->ep->asocs) && !sctp_sstate(sk, LISTENING))
8815                 goto out;
8816 
8817         
8818         if (signal_pending(current))
8819                 goto interrupted;
8820 
8821         
8822 
8823 
8824 
8825 
8826         release_sock(sk);
8827         *timeo_p = schedule_timeout(*timeo_p);
8828         lock_sock(sk);
8829 
8830 ready:
8831         finish_wait(sk_sleep(sk), &wait);
8832         return 0;
8833 
8834 interrupted:
8835         error = sock_intr_errno(*timeo_p);
8836 
8837 out:
8838         finish_wait(sk_sleep(sk), &wait);
8839         *err = error;
8840         return error;
8841 }
8842 
8843 
8844 
8845 
8846 
8847 struct sk_buff *sctp_skb_recv_datagram(struct sock *sk, int flags,
8848                                        int noblock, int *err)
8849 {
8850         int error;
8851         struct sk_buff *skb;
8852         long timeo;
8853 
8854         timeo = sock_rcvtimeo(sk, noblock);
8855 
8856         pr_debug("%s: timeo:%ld, max:%ld\n", __func__, timeo,
8857                  MAX_SCHEDULE_TIMEOUT);
8858 
8859         do {
8860                 
8861 
8862 
8863 
8864 
8865 
8866 
8867                 if (flags & MSG_PEEK) {
8868                         skb = skb_peek(&sk->sk_receive_queue);
8869                         if (skb)
8870                                 refcount_inc(&skb->users);
8871                 } else {
8872                         skb = __skb_dequeue(&sk->sk_receive_queue);
8873                 }
8874 
8875                 if (skb)
8876                         return skb;
8877 
8878                 
8879                 error = sock_error(sk);
8880                 if (error)
8881                         goto no_packet;
8882 
8883                 if (sk->sk_shutdown & RCV_SHUTDOWN)
8884                         break;
8885 
8886                 if (sk_can_busy_loop(sk)) {
8887                         sk_busy_loop(sk, noblock);
8888 
8889                         if (!skb_queue_empty_lockless(&sk->sk_receive_queue))
8890                                 continue;
8891                 }
8892 
8893                 
8894                 error = -EAGAIN;
8895                 if (!timeo)
8896                         goto no_packet;
8897         } while (sctp_wait_for_packet(sk, err, &timeo) == 0);
8898 
8899         return NULL;
8900 
8901 no_packet:
8902         *err = error;
8903         return NULL;
8904 }
8905 
8906 
8907 static void __sctp_write_space(struct sctp_association *asoc)
8908 {
8909         struct sock *sk = asoc->base.sk;
8910 
8911         if (sctp_wspace(asoc) <= 0)
8912                 return;
8913 
8914         if (waitqueue_active(&asoc->wait))
8915                 wake_up_interruptible(&asoc->wait);
8916 
8917         if (sctp_writeable(sk)) {
8918                 struct socket_wq *wq;
8919 
8920                 rcu_read_lock();
8921                 wq = rcu_dereference(sk->sk_wq);
8922                 if (wq) {
8923                         if (waitqueue_active(&wq->wait))
8924                                 wake_up_interruptible(&wq->wait);
8925 
8926                         
8927 
8928 
8929 
8930                         if (!(sk->sk_shutdown & SEND_SHUTDOWN))
8931                                 sock_wake_async(wq, SOCK_WAKE_SPACE, POLL_OUT);
8932                 }
8933                 rcu_read_unlock();
8934         }
8935 }
8936 
8937 static void sctp_wake_up_waiters(struct sock *sk,
8938                                  struct sctp_association *asoc)
8939 {
8940         struct sctp_association *tmp = asoc;
8941 
8942         
8943 
8944 
8945         if (asoc->ep->sndbuf_policy)
8946                 return __sctp_write_space(asoc);
8947 
8948         
8949 
8950 
8951         if (asoc->base.dead)
8952                 return sctp_write_space(sk);
8953 
8954         
8955 
8956 
8957 
8958 
8959 
8960 
8961 
8962 
8963 
8964         for (tmp = list_next_entry(tmp, asocs); 1;
8965              tmp = list_next_entry(tmp, asocs)) {
8966                 
8967                 if (&tmp->asocs == &((sctp_sk(sk))->ep->asocs))
8968                         continue;
8969                 
8970                 __sctp_write_space(tmp);
8971                 
8972                 if (tmp == asoc)
8973                         break;
8974         }
8975 }
8976 
8977 
8978 
8979 
8980 
8981 static void sctp_wfree(struct sk_buff *skb)
8982 {
8983         struct sctp_chunk *chunk = skb_shinfo(skb)->destructor_arg;
8984         struct sctp_association *asoc = chunk->asoc;
8985         struct sock *sk = asoc->base.sk;
8986 
8987         sk_mem_uncharge(sk, skb->truesize);
8988         sk->sk_wmem_queued -= skb->truesize + sizeof(struct sctp_chunk);
8989         asoc->sndbuf_used -= skb->truesize + sizeof(struct sctp_chunk);
8990         WARN_ON(refcount_sub_and_test(sizeof(struct sctp_chunk),
8991                                       &sk->sk_wmem_alloc));
8992 
8993         if (chunk->shkey) {
8994                 struct sctp_shared_key *shkey = chunk->shkey;
8995 
8996                 
8997 
8998 
8999 
9000                 if (shkey->deactivated && !list_empty(&shkey->key_list) &&
9001                     refcount_read(&shkey->refcnt) == 2) {
9002                         struct sctp_ulpevent *ev;
9003 
9004                         ev = sctp_ulpevent_make_authkey(asoc, shkey->key_id,
9005                                                         SCTP_AUTH_FREE_KEY,
9006                                                         GFP_KERNEL);
9007                         if (ev)
9008                                 asoc->stream.si->enqueue_event(&asoc->ulpq, ev);
9009                 }
9010                 sctp_auth_shkey_release(chunk->shkey);
9011         }
9012 
9013         sock_wfree(skb);
9014         sctp_wake_up_waiters(sk, asoc);
9015 
9016         sctp_association_put(asoc);
9017 }
9018 
9019 
9020 
9021 
9022 
9023 
9024 void sctp_sock_rfree(struct sk_buff *skb)
9025 {
9026         struct sock *sk = skb->sk;
9027         struct sctp_ulpevent *event = sctp_skb2event(skb);
9028 
9029         atomic_sub(event->rmem_len, &sk->sk_rmem_alloc);
9030 
9031         
9032 
9033 
9034         sk_mem_uncharge(sk, event->rmem_len);
9035 }
9036 
9037 
9038 
9039 static int sctp_wait_for_sndbuf(struct sctp_association *asoc, long *timeo_p,
9040                                 size_t msg_len)
9041 {
9042         struct sock *sk = asoc->base.sk;
9043         long current_timeo = *timeo_p;
9044         DEFINE_WAIT(wait);
9045         int err = 0;
9046 
9047         pr_debug("%s: asoc:%p, timeo:%ld, msg_len:%zu\n", __func__, asoc,
9048                  *timeo_p, msg_len);
9049 
9050         
9051         sctp_association_hold(asoc);
9052 
9053         
9054         for (;;) {
9055                 prepare_to_wait_exclusive(&asoc->wait, &wait,
9056                                           TASK_INTERRUPTIBLE);
9057                 if (asoc->base.dead)
9058                         goto do_dead;
9059                 if (!*timeo_p)
9060                         goto do_nonblock;
9061                 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING)
9062                         goto do_error;
9063                 if (signal_pending(current))
9064                         goto do_interrupted;
9065                 if (sk_under_memory_pressure(sk))
9066                         sk_mem_reclaim(sk);
9067                 if ((int)msg_len <= sctp_wspace(asoc) &&
9068                     sk_wmem_schedule(sk, msg_len))
9069                         break;
9070 
9071                 
9072 
9073 
9074                 release_sock(sk);
9075                 current_timeo = schedule_timeout(current_timeo);
9076                 lock_sock(sk);
9077                 if (sk != asoc->base.sk)
9078                         goto do_error;
9079 
9080                 *timeo_p = current_timeo;
9081         }
9082 
9083 out:
9084         finish_wait(&asoc->wait, &wait);
9085 
9086         
9087         sctp_association_put(asoc);
9088 
9089         return err;
9090 
9091 do_dead:
9092         err = -ESRCH;
9093         goto out;
9094 
9095 do_error:
9096         err = -EPIPE;
9097         goto out;
9098 
9099 do_interrupted:
9100         err = sock_intr_errno(*timeo_p);
9101         goto out;
9102 
9103 do_nonblock:
9104         err = -EAGAIN;
9105         goto out;
9106 }
9107 
9108 void sctp_data_ready(struct sock *sk)
9109 {
9110         struct socket_wq *wq;
9111 
9112         rcu_read_lock();
9113         wq = rcu_dereference(sk->sk_wq);
9114         if (skwq_has_sleeper(wq))
9115                 wake_up_interruptible_sync_poll(&wq->wait, EPOLLIN |
9116                                                 EPOLLRDNORM | EPOLLRDBAND);
9117         sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
9118         rcu_read_unlock();
9119 }
9120 
9121 
9122 void sctp_write_space(struct sock *sk)
9123 {
9124         struct sctp_association *asoc;
9125 
9126         
9127         list_for_each_entry(asoc, &((sctp_sk(sk))->ep->asocs), asocs) {
9128                 __sctp_write_space(asoc);
9129         }
9130 }
9131 
9132 
9133 
9134 
9135 
9136 
9137 
9138 
9139 
9140 
9141 
9142 
9143 static bool sctp_writeable(struct sock *sk)
9144 {
9145         return sk->sk_sndbuf > sk->sk_wmem_queued;
9146 }
9147 
9148 
9149 
9150 
9151 static int sctp_wait_for_connect(struct sctp_association *asoc, long *timeo_p)
9152 {
9153         struct sock *sk = asoc->base.sk;
9154         int err = 0;
9155         long current_timeo = *timeo_p;
9156         DEFINE_WAIT(wait);
9157 
9158         pr_debug("%s: asoc:%p, timeo:%ld\n", __func__, asoc, *timeo_p);
9159 
9160         
9161         sctp_association_hold(asoc);
9162 
9163         for (;;) {
9164                 prepare_to_wait_exclusive(&asoc->wait, &wait,
9165                                           TASK_INTERRUPTIBLE);
9166                 if (!*timeo_p)
9167                         goto do_nonblock;
9168                 if (sk->sk_shutdown & RCV_SHUTDOWN)
9169                         break;
9170                 if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING ||
9171                     asoc->base.dead)
9172                         goto do_error;
9173                 if (signal_pending(current))
9174                         goto do_interrupted;
9175 
9176                 if (sctp_state(asoc, ESTABLISHED))
9177                         break;
9178 
9179                 
9180 
9181 
9182                 release_sock(sk);
9183                 current_timeo = schedule_timeout(current_timeo);
9184                 lock_sock(sk);
9185 
9186                 *timeo_p = current_timeo;
9187         }
9188 
9189 out:
9190         finish_wait(&asoc->wait, &wait);
9191 
9192         
9193         sctp_association_put(asoc);
9194 
9195         return err;
9196 
9197 do_error:
9198         if (asoc->init_err_counter + 1 > asoc->max_init_attempts)
9199                 err = -ETIMEDOUT;
9200         else
9201                 err = -ECONNREFUSED;
9202         goto out;
9203 
9204 do_interrupted:
9205         err = sock_intr_errno(*timeo_p);
9206         goto out;
9207 
9208 do_nonblock:
9209         err = -EINPROGRESS;
9210         goto out;
9211 }
9212 
9213 static int sctp_wait_for_accept(struct sock *sk, long timeo)
9214 {
9215         struct sctp_endpoint *ep;
9216         int err = 0;
9217         DEFINE_WAIT(wait);
9218 
9219         ep = sctp_sk(sk)->ep;
9220 
9221 
9222         for (;;) {
9223                 prepare_to_wait_exclusive(sk_sleep(sk), &wait,
9224                                           TASK_INTERRUPTIBLE);
9225 
9226                 if (list_empty(&ep->asocs)) {
9227                         release_sock(sk);
9228                         timeo = schedule_timeout(timeo);
9229                         lock_sock(sk);
9230                 }
9231 
9232                 err = -EINVAL;
9233                 if (!sctp_sstate(sk, LISTENING))
9234                         break;
9235 
9236                 err = 0;
9237                 if (!list_empty(&ep->asocs))
9238                         break;
9239 
9240                 err = sock_intr_errno(timeo);
9241                 if (signal_pending(current))
9242                         break;
9243 
9244                 err = -EAGAIN;
9245                 if (!timeo)
9246                         break;
9247         }
9248 
9249         finish_wait(sk_sleep(sk), &wait);
9250 
9251         return err;
9252 }
9253 
9254 static void sctp_wait_for_close(struct sock *sk, long timeout)
9255 {
9256         DEFINE_WAIT(wait);
9257 
9258         do {
9259                 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
9260                 if (list_empty(&sctp_sk(sk)->ep->asocs))
9261                         break;
9262                 release_sock(sk);
9263                 timeout = schedule_timeout(timeout);
9264                 lock_sock(sk);
9265         } while (!signal_pending(current) && timeout);
9266 
9267         finish_wait(sk_sleep(sk), &wait);
9268 }
9269 
9270 static void sctp_skb_set_owner_r_frag(struct sk_buff *skb, struct sock *sk)
9271 {
9272         struct sk_buff *frag;
9273 
9274         if (!skb->data_len)
9275                 goto done;
9276 
9277         
9278         skb_walk_frags(skb, frag)
9279                 sctp_skb_set_owner_r_frag(frag, sk);
9280 
9281 done:
9282         sctp_skb_set_owner_r(skb, sk);
9283 }
9284 
9285 void sctp_copy_sock(struct sock *newsk, struct sock *sk,
9286                     struct sctp_association *asoc)
9287 {
9288         struct inet_sock *inet = inet_sk(sk);
9289         struct inet_sock *newinet;
9290         struct sctp_sock *sp = sctp_sk(sk);
9291         struct sctp_endpoint *ep = sp->ep;
9292 
9293         newsk->sk_type = sk->sk_type;
9294         newsk->sk_bound_dev_if = sk->sk_bound_dev_if;
9295         newsk->sk_flags = sk->sk_flags;
9296         newsk->sk_tsflags = sk->sk_tsflags;
9297         newsk->sk_no_check_tx = sk->sk_no_check_tx;
9298         newsk->sk_no_check_rx = sk->sk_no_check_rx;
9299         newsk->sk_reuse = sk->sk_reuse;
9300         sctp_sk(newsk)->reuse = sp->reuse;
9301 
9302         newsk->sk_shutdown = sk->sk_shutdown;
9303         newsk->sk_destruct = sctp_destruct_sock;
9304         newsk->sk_family = sk->sk_family;
9305         newsk->sk_protocol = IPPROTO_SCTP;
9306         newsk->sk_backlog_rcv = sk->sk_prot->backlog_rcv;
9307         newsk->sk_sndbuf = sk->sk_sndbuf;
9308         newsk->sk_rcvbuf = sk->sk_rcvbuf;
9309         newsk->sk_lingertime = sk->sk_lingertime;
9310         newsk->sk_rcvtimeo = sk->sk_rcvtimeo;
9311         newsk->sk_sndtimeo = sk->sk_sndtimeo;
9312         newsk->sk_rxhash = sk->sk_rxhash;
9313 
9314         newinet = inet_sk(newsk);
9315 
9316         
9317 
9318 
9319         newinet->inet_sport = inet->inet_sport;
9320         newinet->inet_saddr = inet->inet_saddr;
9321         newinet->inet_rcv_saddr = inet->inet_rcv_saddr;
9322         newinet->inet_dport = htons(asoc->peer.port);
9323         newinet->pmtudisc = inet->pmtudisc;
9324         newinet->inet_id = prandom_u32();
9325 
9326         newinet->uc_ttl = inet->uc_ttl;
9327         newinet->mc_loop = 1;
9328         newinet->mc_ttl = 1;
9329         newinet->mc_index = 0;
9330         newinet->mc_list = NULL;
9331 
9332         if (newsk->sk_flags & SK_FLAGS_TIMESTAMP)
9333                 net_enable_timestamp();
9334 
9335         
9336 
9337 
9338         security_sctp_sk_clone(ep, sk, newsk);
9339 }
9340 
9341 static inline void sctp_copy_descendant(struct sock *sk_to,
9342                                         const struct sock *sk_from)
9343 {
9344         int ancestor_size = sizeof(struct inet_sock) +
9345                             sizeof(struct sctp_sock) -
9346                             offsetof(struct sctp_sock, pd_lobby);
9347 
9348         if (sk_from->sk_family == PF_INET6)
9349                 ancestor_size += sizeof(struct ipv6_pinfo);
9350 
9351         __inet_sk_copy_descendant(sk_to, sk_from, ancestor_size);
9352 }
9353 
9354 
9355 
9356 
9357 static int sctp_sock_migrate(struct sock *oldsk, struct sock *newsk,
9358                              struct sctp_association *assoc,
9359                              enum sctp_socket_type type)
9360 {
9361         struct sctp_sock *oldsp = sctp_sk(oldsk);
9362         struct sctp_sock *newsp = sctp_sk(newsk);
9363         struct sctp_bind_bucket *pp; 
9364         struct sctp_endpoint *newep = newsp->ep;
9365         struct sk_buff *skb, *tmp;
9366         struct sctp_ulpevent *event;
9367         struct sctp_bind_hashbucket *head;
9368         int err;
9369 
9370         
9371 
9372 
9373         newsk->sk_sndbuf = oldsk->sk_sndbuf;
9374         newsk->sk_rcvbuf = oldsk->sk_rcvbuf;
9375         
9376         sctp_copy_descendant(newsk, oldsk);
9377 
9378         
9379 
9380 
9381         newsp->ep = newep;
9382         newsp->hmac = NULL;
9383 
9384         
9385         head = &sctp_port_hashtable[sctp_phashfn(sock_net(oldsk),
9386                                                  inet_sk(oldsk)->inet_num)];
9387         spin_lock_bh(&head->lock);
9388         pp = sctp_sk(oldsk)->bind_hash;
9389         sk_add_bind_node(newsk, &pp->owner);
9390         sctp_sk(newsk)->bind_hash = pp;
9391         inet_sk(newsk)->inet_num = inet_sk(oldsk)->inet_num;
9392         spin_unlock_bh(&head->lock);
9393 
9394         
9395 
9396 
9397         err = sctp_bind_addr_dup(&newsp->ep->base.bind_addr,
9398                                  &oldsp->ep->base.bind_addr, GFP_KERNEL);
9399         if (err)
9400                 return err;
9401 
9402         
9403 
9404 
9405 
9406         if (oldsp->ep->auth_hmacs) {
9407                 err = sctp_auth_init_hmacs(newsp->ep, GFP_KERNEL);
9408                 if (err)
9409                         return err;
9410         }
9411 
9412         
9413 
9414 
9415         sctp_skb_for_each(skb, &oldsk->sk_receive_queue, tmp) {
9416                 event = sctp_skb2event(skb);
9417                 if (event->asoc == assoc) {
9418                         __skb_unlink(skb, &oldsk->sk_receive_queue);
9419                         __skb_queue_tail(&newsk->sk_receive_queue, skb);
9420                         sctp_skb_set_owner_r_frag(skb, newsk);
9421                 }
9422         }
9423 
9424         
9425 
9426 
9427 
9428 
9429 
9430         atomic_set(&sctp_sk(newsk)->pd_mode, assoc->ulpq.pd_mode);
9431 
9432         if (atomic_read(&sctp_sk(oldsk)->pd_mode)) {
9433                 struct sk_buff_head *queue;
9434 
9435                 
9436                 if (assoc->ulpq.pd_mode) {
9437                         queue = &newsp->pd_lobby;
9438                 } else
9439                         queue = &newsk->sk_receive_queue;
9440 
9441                 
9442 
9443 
9444                 sctp_skb_for_each(skb, &oldsp->pd_lobby, tmp) {
9445                         event = sctp_skb2event(skb);
9446                         if (event->asoc == assoc) {
9447                                 __skb_unlink(skb, &oldsp->pd_lobby);
9448                                 __skb_queue_tail(queue, skb);
9449                                 sctp_skb_set_owner_r_frag(skb, newsk);
9450                         }
9451                 }
9452 
9453                 
9454 
9455 
9456                 if (assoc->ulpq.pd_mode)
9457                         sctp_clear_pd(oldsk, NULL);
9458 
9459         }
9460 
9461         sctp_for_each_rx_skb(assoc, newsk, sctp_skb_set_owner_r_frag);
9462 
9463         
9464 
9465 
9466 
9467         newsp->type = type;
9468 
9469         
9470 
9471 
9472 
9473 
9474 
9475 
9476 
9477 
9478         lock_sock_nested(newsk, SINGLE_DEPTH_NESTING);
9479         sctp_for_each_tx_datachunk(assoc, true, sctp_clear_owner_w);
9480         sctp_assoc_migrate(assoc, newsk);
9481         sctp_for_each_tx_datachunk(assoc, false, sctp_set_owner_w);
9482 
9483         
9484 
9485 
9486         if (sctp_state(assoc, CLOSED) && sctp_style(newsk, TCP)) {
9487                 inet_sk_set_state(newsk, SCTP_SS_CLOSED);
9488                 newsk->sk_shutdown |= RCV_SHUTDOWN;
9489         } else {
9490                 inet_sk_set_state(newsk, SCTP_SS_ESTABLISHED);
9491         }
9492 
9493         release_sock(newsk);
9494 
9495         return 0;
9496 }
9497 
9498 
9499 
9500 struct proto sctp_prot = {
9501         .name        =  "SCTP",
9502         .owner       =  THIS_MODULE,
9503         .close       =  sctp_close,
9504         .disconnect  =  sctp_disconnect,
9505         .accept      =  sctp_accept,
9506         .ioctl       =  sctp_ioctl,
9507         .init        =  sctp_init_sock,
9508         .destroy     =  sctp_destroy_sock,
9509         .shutdown    =  sctp_shutdown,
9510         .setsockopt  =  sctp_setsockopt,
9511         .getsockopt  =  sctp_getsockopt,
9512         .sendmsg     =  sctp_sendmsg,
9513         .recvmsg     =  sctp_recvmsg,
9514         .bind        =  sctp_bind,
9515         .backlog_rcv =  sctp_backlog_rcv,
9516         .hash        =  sctp_hash,
9517         .unhash      =  sctp_unhash,
9518         .no_autobind =  true,
9519         .obj_size    =  sizeof(struct sctp_sock),
9520         .useroffset  =  offsetof(struct sctp_sock, subscribe),
9521         .usersize    =  offsetof(struct sctp_sock, initmsg) -
9522                                 offsetof(struct sctp_sock, subscribe) +
9523                                 sizeof_field(struct sctp_sock, initmsg),
9524         .sysctl_mem  =  sysctl_sctp_mem,
9525         .sysctl_rmem =  sysctl_sctp_rmem,
9526         .sysctl_wmem =  sysctl_sctp_wmem,
9527         .memory_pressure = &sctp_memory_pressure,
9528         .enter_memory_pressure = sctp_enter_memory_pressure,
9529         .memory_allocated = &sctp_memory_allocated,
9530         .sockets_allocated = &sctp_sockets_allocated,
9531 };
9532 
9533 #if IS_ENABLED(CONFIG_IPV6)
9534 
9535 #include <net/transp_v6.h>
9536 static void sctp_v6_destroy_sock(struct sock *sk)
9537 {
9538         sctp_destroy_sock(sk);
9539         inet6_destroy_sock(sk);
9540 }
9541 
9542 struct proto sctpv6_prot = {
9543         .name           = "SCTPv6",
9544         .owner          = THIS_MODULE,
9545         .close          = sctp_close,
9546         .disconnect     = sctp_disconnect,
9547         .accept         = sctp_accept,
9548         .ioctl          = sctp_ioctl,
9549         .init           = sctp_init_sock,
9550         .destroy        = sctp_v6_destroy_sock,
9551         .shutdown       = sctp_shutdown,
9552         .setsockopt     = sctp_setsockopt,
9553         .getsockopt     = sctp_getsockopt,
9554         .sendmsg        = sctp_sendmsg,
9555         .recvmsg        = sctp_recvmsg,
9556         .bind           = sctp_bind,
9557         .backlog_rcv    = sctp_backlog_rcv,
9558         .hash           = sctp_hash,
9559         .unhash         = sctp_unhash,
9560         .no_autobind    = true,
9561         .obj_size       = sizeof(struct sctp6_sock),
9562         .useroffset     = offsetof(struct sctp6_sock, sctp.subscribe),
9563         .usersize       = offsetof(struct sctp6_sock, sctp.initmsg) -
9564                                 offsetof(struct sctp6_sock, sctp.subscribe) +
9565                                 sizeof_field(struct sctp6_sock, sctp.initmsg),
9566         .sysctl_mem     = sysctl_sctp_mem,
9567         .sysctl_rmem    = sysctl_sctp_rmem,
9568         .sysctl_wmem    = sysctl_sctp_wmem,
9569         .memory_pressure = &sctp_memory_pressure,
9570         .enter_memory_pressure = sctp_enter_memory_pressure,
9571         .memory_allocated = &sctp_memory_allocated,
9572         .sockets_allocated = &sctp_sockets_allocated,
9573 };
9574 #endif