60 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
62 #include <linux/types.h>
63 #include <linux/kernel.h>
64 #include <linux/wait.h>
65 #include <linux/time.h>
67 #include <linux/capability.h>
68 #include <linux/fcntl.h>
69 #include <linux/poll.h>
72 #include <linux/slab.h>
81 #include <linux/socket.h>
82 #include <linux/export.h>
93 static int sctp_writeable(
struct sock *
sk);
97 static int sctp_wait_for_packet(
struct sock *
sk,
int *
err,
long *timeo_p);
99 static int sctp_wait_for_accept(
struct sock *
sk,
long timeo);
100 static void sctp_wait_for_close(
struct sock *
sk,
long timeo);
103 static int sctp_bindx_add(
struct sock *,
struct sockaddr *,
int);
104 static int sctp_bindx_rem(
struct sock *,
struct sockaddr *,
int);
105 static int sctp_send_asconf_add_ip(
struct sock *,
struct sockaddr *,
int);
106 static int sctp_send_asconf_del_ip(
struct sock *,
struct sockaddr *,
int);
109 static int sctp_do_bind(
struct sock *,
union sctp_addr *,
int);
110 static int sctp_autobind(
struct sock *
sk);
111 static void sctp_sock_migrate(
struct sock *,
struct sock *,
120 static int sctp_memory_pressure;
124 static void sctp_enter_memory_pressure(
struct sock *
sk)
126 sctp_memory_pressure = 1;
135 if (asoc->
ep->sndbuf_policy)
138 amt = sk_wmem_alloc_get(asoc->
base.sk);
140 if (amt >= asoc->
base.sk->sk_sndbuf) {
144 amt = sk_stream_wspace(asoc->
base.sk);
149 amt = asoc->
base.sk->sk_sndbuf - amt;
171 skb_set_owner_w(chunk->
skb, sk);
173 chunk->
skb->destructor = sctp_wfree;
183 sk_mem_charge(sk, chunk->
skb->truesize);
193 af = sctp_sockaddr_af(sctp_sk(sk), addr, len);
201 if (!sctp_sk(sk)->pf->send_verify(sctp_sk(sk), (addr)))
224 if (!list_empty(&sctp_sk(sk)->
ep->asocs))
238 if (!asoc || (asoc->
base.sk != sk) || asoc->
base.dead)
264 if (id_asoc && (id_asoc != addr_asoc))
293 if (!sctp_sk(sk)->ep->base.bind_addr.port)
294 retval = sctp_do_bind(sk, (
union sctp_addr *)addr,
304 static long sctp_get_port_local(
struct sock *,
union sctp_addr *);
318 ipv6_addr_v4mapped(&addr->
v6.sin6_addr)) {
319 if (!opt->
pf->af_supported(
AF_INET, opt))
323 if (!opt->
pf->af_supported(addr->
sa.sa_family, opt))
347 af = sctp_sockaddr_af(sp, addr, len);
354 snum =
ntohs(addr->
v4.sin_port);
357 ", port: %d, new port: %d, len: %d)\n",
364 if (!sp->
pf->bind_verify(sp, addr))
374 else if (snum != bp->
port) {
376 " New port %d does not match existing port "
377 "%d.\n", snum, bp->
port);
395 addr->
v4.sin_port =
htons(snum);
396 if ((ret = sctp_get_port_local(sk, addr))) {
402 bp->
port = inet_sk(sk)->inet_num;
411 inet_sk(sk)->inet_sport =
htons(inet_sk(sk)->inet_num);
466 static int sctp_bindx_add(
struct sock *sk,
struct sockaddr *addrs,
int addrcnt)
478 for (cnt = 0; cnt < addrcnt; cnt++) {
489 retval = sctp_do_bind(sk, (
union sctp_addr *)sa_addr,
498 sctp_bindx_rem(sk, addrs, cnt);
516 static int sctp_send_asconf_add_ip(
struct sock *sk,
520 struct net *net = sock_net(sk);
535 if (!net->sctp.addip_enable)
542 __func__, sk, addrs, addrcnt);
546 if (!asoc->
peer.asconf_capable)
561 for (i = 0; i < addrcnt; i++) {
580 bp = &asoc->
base.bind_addr;
594 for (i = 0; i < addrcnt; i++) {
615 sctp_sk(asoc->
base.sk));
618 retval = sctp_send_asconf(asoc, chunk);
640 static int sctp_bindx_rem(
struct sock *sk,
struct sockaddr *addrs,
int addrcnt)
655 for (cnt = 0; cnt < addrcnt; cnt++) {
678 if (sa_addr->
v4.sin_port &&
684 if (!sa_addr->
v4.sin_port)
701 sctp_bindx_add(sk, addrs, cnt);
719 static int sctp_send_asconf_del_ip(
struct sock *sk,
723 struct net *net = sock_net(sk);
739 if (!net->sctp.addip_enable)
746 __func__, sk, addrs, addrcnt);
750 if (!asoc->
peer.asconf_capable)
765 for (i = 0; i < addrcnt; i++) {
786 bp = &asoc->
base.bind_addr;
789 if ((laddr ==
NULL) && (addrcnt == 1)) {
837 for (i = 0; i < addrcnt; i++) {
855 sctp_sk(asoc->
base.sk));
861 retval = sctp_send_asconf(asoc, chunk);
870 struct sock *sk = sctp_opt2sk(sp);
876 addr->
v4.sin_port =
htons(sp->
ep->base.bind_addr.port);
884 return sctp_send_asconf_add_ip(sk, (
struct sockaddr *)addr, 1);
886 return sctp_send_asconf_del_ip(sk, (
struct sockaddr *)addr, 1);
967 int addrs_size,
int op)
978 " addrs_size %d opt %d\n", sk, addrs, addrs_size, op);
999 while (walk_size < addrs_size) {
1000 if (walk_size +
sizeof(
sa_family_t) > addrs_size) {
1011 if (!af || (walk_size + af->
sockaddr_len) > addrs_size) {
1023 err = sctp_bindx_add(sk, kaddrs, addrcnt);
1026 err = sctp_send_asconf_add_ip(sk, kaddrs, addrcnt);
1030 err = sctp_bindx_rem(sk, kaddrs, addrcnt);
1033 err = sctp_send_asconf_del_ip(sk, kaddrs, addrcnt);
1052 static int __sctp_connect(
struct sock* sk,
1057 struct net *net = sock_net(sk);
1072 unsigned short port;
1073 unsigned int f_flags = 0;
1091 while (walk_size < addrs_size) {
1092 if (walk_size +
sizeof(
sa_family_t) > addrs_size) {
1103 if (!af || (walk_size + af->
sockaddr_len) > addrs_size) {
1108 port =
ntohs(sa_addr->
v4.sin_port);
1120 if (asoc && asoc->
peer.port && asoc->
peer.port != port)
1128 if (asoc2 && asoc2 != asoc) {
1151 if (!ep->
base.bind_addr.port) {
1152 if (sctp_autobind(sk)) {
1214 inet_sk(sk)->inet_dport =
htons(asoc->
peer.port);
1225 timeo = sock_sndtimeo(sk, f_flags &
O_NONBLOCK);
1227 err = sctp_wait_for_connect(asoc, &timeo);
1228 if ((err == 0 || err == -
EINPROGRESS) && assoc_id)
1237 " kaddrs: %p err: %d\n",
1321 __func__, sk, addrs, addrs_size);
1338 err = __sctp_connect(sk, kaddrs, addrs_size, assoc_id);
1387 char __user *optval,
1394 if (len <
sizeof(param))
1407 if (
put_user(
sizeof(assoc_id), optlen))
1466 struct net *net = sock_net(sk);
1470 unsigned int data_was_unread;
1478 ep = sctp_sk(sk)->ep;
1501 if (data_was_unread || !skb_queue_empty(&asoc->
ulpq.lobby) ||
1502 !skb_queue_empty(&asoc->
ulpq.reasm) ||
1515 sctp_wait_for_close(sk, timeout);
1541 static int sctp_error(
struct sock *sk,
int flags,
int err)
1544 err = sock_error(sk) ? : -
EPIPE;
1578 struct net *net = sock_net(sk);
1594 __u16 sinfo_flags = 0;
1634 if (msg_namelen >
sizeof(to))
1635 msg_namelen =
sizeof(to);
1650 msg_len, sinfo_flags);
1663 if (((sinfo_flags &
SCTP_EOF) && (msg_len > 0)) ||
1664 (!(sinfo_flags & (SCTP_EOF|
SCTP_ABORT)) && (msg_len == 0))) {
1721 if (sinfo_flags & SCTP_EOF) {
1759 sp->
initmsg.sinit_num_ostreams) {
1780 if (!ep->
base.bind_addr.port) {
1781 if (sctp_autobind(sk)) {
1818 asoc->c.sinit_num_ostreams =
1822 asoc->c.sinit_max_instreams =
1826 asoc->max_init_attempts
1830 asoc->max_init_timeo =
1850 memset(&default_sinfo, 0,
sizeof(default_sinfo));
1853 default_sinfo.
sinfo_ppid = asoc->default_ppid;
1857 sinfo = &default_sinfo;
1868 if (asoc->pmtu_pending)
1869 sctp_assoc_pending_pmtu(sk, asoc);
1876 if (sctp_sk(sk)->disable_fragments && (msg_len > asoc->frag_point)) {
1882 if (sinfo->
sinfo_stream >= asoc->c.sinit_num_ostreams) {
1888 if (!sctp_wspace(asoc)) {
1889 err = sctp_wait_for_sndbuf(asoc, &timeo, msg_len);
1899 (sinfo_flags & SCTP_ADDR_OVER)) {
1918 if (IS_ERR(datamsg)) {
1919 err = PTR_ERR(datamsg);
1928 sctp_set_owner_w(chunk);
1966 return sctp_error(sk, msg_flags, err);
1973 err = sock_error(sk);
1990 static int sctp_skb_pull(
struct sk_buff *
skb,
int len)
1993 int skb_len = skb_headlen(skb);
1996 if (len <= skb_len) {
1997 __skb_pull(skb, len);
2001 __skb_pull(skb, skb_len);
2003 skb_walk_frags(skb, list) {
2004 rlen = sctp_skb_pull(list, len);
2005 skb->
len -= (len-rlen);
2032 static struct sk_buff *sctp_skb_recv_datagram(
struct sock *,
int,
int,
int *);
2035 struct msghdr *
msg,
size_t len,
int noblock,
2036 int flags,
int *addr_len)
2046 "0x%x, %s: %p)\n",
"sk", sk,
"msghdr", msg,
2047 "len", len,
"knoblauch", noblock,
2048 "flags", flags,
"addr_len", addr_len);
2057 skb = sctp_skb_recv_datagram(sk, flags, noblock, &err);
2072 event = sctp_skb2event(skb);
2077 sock_recv_ts_and_drops(msg, sk, skb);
2082 sp->
pf->skb_msgname(skb, msg->
msg_name, addr_len);
2100 if (skb_len > copied) {
2104 sctp_skb_pull(skb, copied);
2145 static int sctp_setsockopt_disable_fragments(
struct sock *sk,
2146 char __user *optval,
2147 unsigned int optlen)
2151 if (optlen <
sizeof(
int))
2154 if (
get_user(val, (
int __user *)optval))
2157 sctp_sk(sk)->disable_fragments = (val == 0) ? 0 : 1;
2162 static int sctp_setsockopt_events(
struct sock *sk,
char __user *optval,
2163 unsigned int optlen)
2179 &sctp_sk(sk)->subscribe)) {
2206 static int sctp_setsockopt_autoclose(
struct sock *sk,
char __user *optval,
2207 unsigned int optlen)
2214 if (optlen !=
sizeof(
int))
2325 int sackdelay_change)
2330 struct net *net = sock_net(trans->
asoc->base.sk);
2429 if (sackdelay_change) {
2461 static int sctp_setsockopt_peer_addr_params(
struct sock *sk,
2462 char __user *optval,
2463 unsigned int optlen)
2470 int hb_change, pmtud_change, sackdelay_change;
2483 if (hb_change ==
SPP_HB ||
2495 trans = sctp_addr_id2transport(sk, ¶ms.
spp_address,
2516 error = sctp_apply_peer_addr_params(¶ms, trans, asoc, sp,
2517 hb_change, pmtud_change,
2526 if (!trans && asoc) {
2529 sctp_apply_peer_addr_params(¶ms, trans, asoc, sp,
2530 hb_change, pmtud_change,
2574 static int sctp_setsockopt_delayed_ack(
struct sock *sk,
2575 char __user *optval,
unsigned int optlen)
2586 if (params.sack_delay == 0 && params.sack_freq == 0)
2589 pr_warn(
"Use of struct sctp_assoc_value in delayed_ack socket option deprecated\n");
2590 pr_warn(
"Use struct sctp_sack_info instead\n");
2594 if (params.sack_delay == 0)
2595 params.sack_freq = 1;
2597 params.sack_freq = 0;
2602 if (params.sack_delay > 500)
2613 if (params.sack_delay) {
2628 if (params.sack_freq == 1) {
2638 }
else if (params.sack_freq > 1) {
2656 if (params.sack_delay) {
2663 if (params.sack_freq == 1) {
2667 }
else if (params.sack_freq > 1) {
2668 trans->
sackfreq = params.sack_freq;
2690 static int sctp_setsockopt_initmsg(
struct sock *sk,
char __user *optval,
unsigned int optlen)
2700 if (sinit.sinit_num_ostreams)
2701 sp->
initmsg.sinit_num_ostreams = sinit.sinit_num_ostreams;
2702 if (sinit.sinit_max_instreams)
2703 sp->
initmsg.sinit_max_instreams = sinit.sinit_max_instreams;
2704 if (sinit.sinit_max_attempts)
2705 sp->
initmsg.sinit_max_attempts = sinit.sinit_max_attempts;
2706 if (sinit.sinit_max_init_timeo)
2707 sp->
initmsg.sinit_max_init_timeo = sinit.sinit_max_init_timeo;
2726 static int sctp_setsockopt_default_send_param(
struct sock *sk,
2727 char __user *optval,
2728 unsigned int optlen)
2766 static int sctp_setsockopt_primary_addr(
struct sock *sk,
char __user *optval,
2767 unsigned int optlen)
2778 trans = sctp_addr_id2transport(sk, &
prim.ssp_addr,
prim.ssp_assoc_id);
2795 static int sctp_setsockopt_nodelay(
struct sock *sk,
char __user *optval,
2796 unsigned int optlen)
2800 if (optlen <
sizeof(
int))
2802 if (
get_user(val, (
int __user *)optval))
2805 sctp_sk(sk)->nodelay = (val == 0) ? 0 : 1;
2821 static int sctp_setsockopt_rtoinfo(
struct sock *sk,
char __user *optval,
unsigned int optlen)
2839 if (rtoinfo.srto_initial != 0)
2842 if (rtoinfo.srto_max != 0)
2844 if (rtoinfo.srto_min != 0)
2852 if (
rtoinfo.srto_initial != 0)
2874 static int sctp_setsockopt_associnfo(
struct sock *sk,
char __user *optval,
unsigned int optlen)
2887 if (!asoc && assocparams.sasoc_assoc_id &&
sctp_style(sk,
UDP))
2892 if (assocparams.sasoc_asocmaxrxt != 0) {
2909 assocparams.sasoc_asocmaxrxt > path_sum)
2915 if (assocparams.sasoc_cookie_life != 0) {
2917 assocparams.sasoc_cookie_life / 1000;
2919 (assocparams.sasoc_cookie_life % 1000)
2946 static int sctp_setsockopt_mappedv4(
struct sock *sk,
char __user *optval,
unsigned int optlen)
2951 if (optlen <
sizeof(
int))
2953 if (
get_user(val, (
int __user *)optval))
2990 static int sctp_setsockopt_maxseg(
struct sock *sk,
char __user *optval,
unsigned int optlen)
2997 if (optlen ==
sizeof(
int)) {
2998 pr_warn(
"Use of int in maxseg socket option deprecated\n");
2999 pr_warn(
"Use struct sctp_assoc_value instead\n");
3002 params.assoc_id = 0;
3006 val = params.assoc_value;
3020 val -= sp->
pf->af->net_header_len;
3021 val -=
sizeof(
struct sctphdr) +
3042 static int sctp_setsockopt_peer_primary_addr(
struct sock *sk,
char __user *optval,
3043 unsigned int optlen)
3045 struct net *net = sock_net(sk);
3055 if (!net->sctp.addip_enable)
3068 if (!asoc->
peer.asconf_capable)
3093 err = sctp_send_asconf(asoc, chunk);
3100 static int sctp_setsockopt_adaptation_layer(
struct sock *sk,
char __user *optval,
3101 unsigned int optlen)
3110 sctp_sk(sk)->adaptation_ind = adaptation.ssb_adaptation_ind;
3129 static int sctp_setsockopt_context(
struct sock *sk,
char __user *optval,
3130 unsigned int optlen)
3143 if (params.assoc_id != 0) {
3179 static int sctp_setsockopt_fragment_interleave(
struct sock *sk,
3180 char __user *optval,
3181 unsigned int optlen)
3185 if (optlen !=
sizeof(
int))
3187 if (
get_user(val, (
int __user *)optval))
3190 sctp_sk(sk)->frag_interleave = (val == 0) ? 0 : 1;
3212 static int sctp_setsockopt_partial_delivery_point(
struct sock *sk,
3213 char __user *optval,
3214 unsigned int optlen)
3218 if (optlen !=
sizeof(
u32))
3220 if (
get_user(val, (
int __user *)optval))
3229 sctp_sk(sk)->pd_point =
val;
3245 static int sctp_setsockopt_maxburst(
struct sock *sk,
3246 char __user *optval,
3247 unsigned int optlen)
3255 if (optlen ==
sizeof(
int)) {
3256 pr_warn(
"Use of int in max_burst socket option deprecated\n");
3257 pr_warn(
"Use struct sctp_assoc_value instead\n");
3263 val = params.assoc_value;
3264 assoc_id = params.assoc_id;
3270 if (assoc_id != 0) {
3288 static int sctp_setsockopt_auth_chunk(
struct sock *sk,
3289 char __user *optval,
3290 unsigned int optlen)
3292 struct net *net = sock_net(sk);
3295 if (!net->sctp.auth_enable)
3303 switch (val.sauth_chunk) {
3321 static int sctp_setsockopt_hmac_ident(
struct sock *sk,
3322 char __user *optval,
3323 unsigned int optlen)
3325 struct net *net = sock_net(sk);
3330 if (!net->sctp.auth_enable)
3338 return PTR_ERR(hmacs);
3359 static int sctp_setsockopt_auth_key(
struct sock *sk,
3360 char __user *optval,
3361 unsigned int optlen)
3363 struct net *net = sock_net(sk);
3368 if (!net->sctp.auth_enable)
3375 if (IS_ERR(authkey))
3376 return PTR_ERR(authkey);
3401 static int sctp_setsockopt_active_key(
struct sock *sk,
3402 char __user *optval,
3403 unsigned int optlen)
3405 struct net *net = sock_net(sk);
3409 if (!net->sctp.auth_enable)
3422 val.scact_keynumber);
3430 static int sctp_setsockopt_del_key(
struct sock *sk,
3431 char __user *optval,
3432 unsigned int optlen)
3434 struct net *net = sock_net(sk);
3438 if (!net->sctp.auth_enable)
3451 val.scact_keynumber);
3469 static int sctp_setsockopt_auto_asconf(
struct sock *sk,
char __user *optval,
3470 unsigned int optlen)
3475 if (optlen <
sizeof(
int))
3477 if (
get_user(val, (
int __user *)optval))
3489 &sock_net(sk)->sctp.auto_asconf_splist);
3503 static int sctp_setsockopt_paddr_thresholds(
struct sock *sk,
3504 char __user *optval,
3505 unsigned int optlen)
3524 if (val.spt_pathmaxrxt)
3529 if (val.spt_pathmaxrxt)
3533 trans = sctp_addr_id2transport(sk, &val.spt_address,
3538 if (val.spt_pathmaxrxt)
3566 char __user *optval,
unsigned int optlen)
3580 struct sctp_af *af = sctp_sk(sk)->pf->af;
3581 retval = af->
setsockopt(sk, level, optname, optval, optlen);
3615 retval = sctp_setsockopt_disable_fragments(sk, optval, optlen);
3619 retval = sctp_setsockopt_events(sk, optval, optlen);
3623 retval = sctp_setsockopt_autoclose(sk, optval, optlen);
3627 retval = sctp_setsockopt_peer_addr_params(sk, optval, optlen);
3631 retval = sctp_setsockopt_delayed_ack(sk, optval, optlen);
3634 retval = sctp_setsockopt_partial_delivery_point(sk, optval, optlen);
3638 retval = sctp_setsockopt_initmsg(sk, optval, optlen);
3641 retval = sctp_setsockopt_default_send_param(sk, optval,
3645 retval = sctp_setsockopt_primary_addr(sk, optval, optlen);
3648 retval = sctp_setsockopt_peer_primary_addr(sk, optval, optlen);
3651 retval = sctp_setsockopt_nodelay(sk, optval, optlen);
3654 retval = sctp_setsockopt_rtoinfo(sk, optval, optlen);
3657 retval = sctp_setsockopt_associnfo(sk, optval, optlen);
3660 retval = sctp_setsockopt_mappedv4(sk, optval, optlen);
3663 retval = sctp_setsockopt_maxseg(sk, optval, optlen);
3666 retval = sctp_setsockopt_adaptation_layer(sk, optval, optlen);
3669 retval = sctp_setsockopt_context(sk, optval, optlen);
3672 retval = sctp_setsockopt_fragment_interleave(sk, optval, optlen);
3675 retval = sctp_setsockopt_maxburst(sk, optval, optlen);
3678 retval = sctp_setsockopt_auth_chunk(sk, optval, optlen);
3681 retval = sctp_setsockopt_hmac_ident(sk, optval, optlen);
3684 retval = sctp_setsockopt_auth_key(sk, optval, optlen);
3687 retval = sctp_setsockopt_active_key(sk, optval, optlen);
3690 retval = sctp_setsockopt_del_key(sk, optval, optlen);
3693 retval = sctp_setsockopt_auto_asconf(sk, optval, optlen);
3696 retval = sctp_setsockopt_paddr_thresholds(sk, optval, optlen);
3734 __func__, sk, addr, addr_len);
3788 timeo = sock_rcvtimeo(sk, flags &
O_NONBLOCK);
3790 error = sctp_wait_for_accept(sk, timeo);
3799 newsk = sp->
pf->create_accept_sk(sk, asoc);
3833 unsigned int amount = 0;
3861 struct net *net = sock_net(sk);
3899 sp->
initmsg.sinit_max_attempts = net->sctp.max_retrans_init;
3900 sp->
initmsg.sinit_max_init_timeo = net->sctp.rto_max;
3905 sp->
rtoinfo.srto_initial = net->sctp.rto_initial;
3906 sp->
rtoinfo.srto_max = net->sctp.rto_max;
3907 sp->
rtoinfo.srto_min = net->sctp.rto_min;
3912 sp->
assocparams.sasoc_asocmaxrxt = net->sctp.max_retrans_association;
3913 sp->
assocparams.sasoc_number_peer_destinations = 0;
3916 sp->
assocparams.sasoc_cookie_life = net->sctp.valid_cookie_life;
3962 skb_queue_head_init(&sp->
pd_lobby);
3980 sock_prot_inuse_add(net, sk->sk_prot, 1);
3981 if (net->sctp.default_auto_asconf) {
3983 &net->sctp.auto_asconf_splist);
4008 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
4030 struct net *net = sock_net(sk);
4038 ep = sctp_sk(sk)->ep;
4039 if (!list_empty(&ep->
asocs)) {
4054 static int sctp_getsockopt_sctp_status(
struct sock *sk,
int len,
4055 char __user *optval,
4064 if (len <
sizeof(
status)) {
4075 associd =
status.sstat_assoc_id;
4082 transport = asoc->
peer.primary_path;
4084 status.sstat_assoc_id = sctp_assoc2id(asoc);
4090 status.sstat_instrms = asoc->
c.sinit_max_instreams;
4091 status.sstat_outstrms = asoc->
c.sinit_num_ostreams;
4093 status.sstat_primary.spinfo_assoc_id = sctp_assoc2id(transport->
asoc);
4099 status.sstat_primary.spinfo_state = transport->
state;
4100 status.sstat_primary.spinfo_cwnd = transport->
cwnd;
4101 status.sstat_primary.spinfo_srtt = transport->
srtt;
4134 static int sctp_getsockopt_peer_addr_info(
struct sock *sk,
int len,
4135 char __user *optval,
4142 if (len <
sizeof(
pinfo)) {
4147 len =
sizeof(
pinfo);
4153 transport = sctp_addr_id2transport(sk, &
pinfo.spinfo_address,
4154 pinfo.spinfo_assoc_id);
4158 pinfo.spinfo_assoc_id = sctp_assoc2id(transport->
asoc);
4189 static int sctp_getsockopt_disable_fragments(
struct sock *sk,
int len,
4190 char __user *optval,
int __user *optlen)
4194 if (len <
sizeof(
int))
4198 val = (sctp_sk(sk)->disable_fragments == 1);
4211 static int sctp_getsockopt_events(
struct sock *sk,
int len,
char __user *optval,
4220 if (
copy_to_user(optval, &sctp_sk(sk)->subscribe, len))
4236 static int sctp_getsockopt_autoclose(
struct sock *sk,
int len,
char __user *optval,
int __user *optlen)
4241 if (len <
sizeof(
int))
4246 if (
copy_to_user(optval, &sctp_sk(sk)->autoclose,
sizeof(
int)))
4292 static int sctp_getsockopt_peeloff(
struct sock *sk,
int len,
char __user *optval,
int __user *optlen)
4296 struct file *newfile;
4320 return PTR_ERR(newfile);
4324 __func__, sk, newsock->
sk, retval);
4436 static int sctp_getsockopt_peer_addr_params(
struct sock *sk,
int len,
4437 char __user *optval,
int __user *optlen)
4454 trans = sctp_addr_id2transport(sk, ¶ms.
spp_address,
4545 static int sctp_getsockopt_delayed_ack(
struct sock *sk,
int len,
4546 char __user *optval,
4559 pr_warn(
"Use of struct sctp_assoc_value in delayed_ack socket option deprecated\n");
4560 pr_warn(
"Use struct sctp_sack_info instead\n");
4582 params.sack_delay = 0;
4583 params.sack_freq = 1;
4591 params.sack_delay = 0;
4592 params.sack_freq = 1;
4616 static int sctp_getsockopt_initmsg(
struct sock *sk,
int len,
char __user *optval,
int __user *optlen)
4629 static int sctp_getsockopt_peer_addrs(
struct sock *sk,
int len,
4630 char __user *optval,
int __user *optlen)
4662 if (space_left < addrlen)
4668 space_left -= addrlen;
4673 bytes_copied = ((
char __user *)to) - optval;
4674 if (
put_user(bytes_copied, optlen))
4680 static int sctp_copy_laddrs(
struct sock *sk,
__u16 port,
void *to,
4681 size_t space_left,
int *bytes_copied)
4687 struct net *net = sock_net(sk);
4690 list_for_each_entry_rcu(addr, &net->sctp.local_addr_list, list) {
4694 if ((
PF_INET == sk->sk_family) &&
4702 if (!
temp.v4.sin_port)
4708 if (space_left < addrlen) {
4716 space_left -= addrlen;
4717 *bytes_copied += addrlen;
4725 static int sctp_getsockopt_local_addrs(
struct sock *sk,
int len,
4726 char __user *optval,
int __user *optlen)
4739 int bytes_copied = 0;
4755 if (0 == getaddrs.assoc_id) {
4756 bp = &sctp_sk(sk)->ep->base.bind_addr;
4761 bp = &asoc->
base.bind_addr;
4778 cnt = sctp_copy_laddrs(sk, bp->
port, addrs,
4779 space_left, &bytes_copied);
4797 if (space_left < addrlen) {
4803 bytes_copied += addrlen;
4805 space_left -= addrlen;
4817 if (
put_user(bytes_copied, optlen))
4830 static int sctp_getsockopt_primary_addr(
struct sock *sk,
int len,
4831 char __user *optval,
int __user *optlen)
4849 if (!asoc->
peer.primary_path)
4853 asoc->
peer.primary_path->af_specific->sockaddr_len);
4872 static int sctp_getsockopt_adaptation_layer(
struct sock *sk,
int len,
4873 char __user *optval,
int __user *optlen)
4882 adaptation.ssb_adaptation_ind = sctp_sk(sk)->adaptation_ind;
4911 static int sctp_getsockopt_default_send_param(
struct sock *sk,
4912 int len,
char __user *optval,
4963 static int sctp_getsockopt_nodelay(
struct sock *sk,
int len,
4964 char __user *optval,
int __user *optlen)
4968 if (len <
sizeof(
int))
4972 val = (sctp_sk(sk)->nodelay == 1);
4992 static int sctp_getsockopt_rtoinfo(
struct sock *sk,
int len,
4993 char __user *optval,
4994 int __user *optlen) {
5045 static int sctp_getsockopt_associnfo(
struct sock *sk,
int len,
5046 char __user *optval,
5065 if (!asoc && assocparams.sasoc_assoc_id &&
sctp_style(sk,
UDP))
5071 assocparams.sasoc_peer_rwnd = asoc->
peer.rwnd;
5072 assocparams.sasoc_local_rwnd = asoc->
a_rwnd;
5073 assocparams.sasoc_cookie_life = (asoc->
cookie_life.tv_sec
5082 assocparams.sasoc_number_peer_destinations =
cnt;
5094 sasoc_number_peer_destinations;
5116 static int sctp_getsockopt_mappedv4(
struct sock *sk,
int len,
5117 char __user *optval,
int __user *optlen)
5122 if (len <
sizeof(
int))
5139 static int sctp_getsockopt_context(
struct sock *sk,
int len,
5140 char __user *optval,
int __user *optlen)
5156 if (params.assoc_id != 0) {
5200 static int sctp_getsockopt_maxseg(
struct sock *sk,
int len,
5201 char __user *optval,
int __user *optlen)
5206 if (len ==
sizeof(
int)) {
5207 pr_warn(
"Use of int in maxseg socket option deprecated\n");
5208 pr_warn(
"Use struct sctp_assoc_value instead\n");
5209 params.assoc_id = 0;
5224 params.assoc_value = sctp_sk(sk)->user_frag;
5228 if (len ==
sizeof(
int)) {
5243 static int sctp_getsockopt_fragment_interleave(
struct sock *sk,
int len,
5244 char __user *optval,
int __user *optlen)
5248 if (len <
sizeof(
int))
5253 val = sctp_sk(sk)->frag_interleave;
5266 static int sctp_getsockopt_partial_delivery_point(
struct sock *sk,
int len,
5267 char __user *optval,
5272 if (len <
sizeof(
u32))
5277 val = sctp_sk(sk)->pd_point;
5290 static int sctp_getsockopt_maxburst(
struct sock *sk,
int len,
5291 char __user *optval,
5298 if (len ==
sizeof(
int)) {
5299 pr_warn(
"Use of int in max_burst socket option deprecated\n");
5300 pr_warn(
"Use struct sctp_assoc_value instead\n");
5301 params.assoc_id = 0;
5311 if (params.assoc_id != 0) {
5319 if (len ==
sizeof(
int)) {
5331 static int sctp_getsockopt_hmac_ident(
struct sock *sk,
int len,
5332 char __user *optval,
int __user *optlen)
5334 struct net *net = sock_net(sk);
5340 if (!net->sctp.auth_enable)
5343 hmacs = sctp_sk(sk)->ep->auth_hmacs_list;
5350 num_idents = data_len /
sizeof(
u16);
5361 static int sctp_getsockopt_active_key(
struct sock *sk,
int len,
5362 char __user *optval,
int __user *optlen)
5364 struct net *net = sock_net(sk);
5368 if (!net->sctp.auth_enable)
5383 val.scact_keynumber = sctp_sk(sk)->ep->active_key_id;
5394 static int sctp_getsockopt_peer_auth_chunks(
struct sock *sk,
int len,
5395 char __user *optval,
int __user *optlen)
5397 struct net *net = sock_net(sk);
5405 if (!net->sctp.auth_enable)
5419 ch = asoc->
peer.peer_chunks;
5425 if (len < num_chunks)
5438 static int sctp_getsockopt_local_auth_chunks(
struct sock *sk,
int len,
5439 char __user *optval,
int __user *optlen)
5441 struct net *net = sock_net(sk);
5449 if (!net->sctp.auth_enable)
5466 ch = sctp_sk(sk)->ep->auth_chunk_list;
5492 static int sctp_getsockopt_assoc_number(
struct sock *sk,
int len,
5493 char __user *optval,
int __user *optlen)
5502 if (len <
sizeof(
u32))
5523 static int sctp_getsockopt_auto_asconf(
struct sock *sk,
int len,
5524 char __user *optval,
int __user *optlen)
5528 if (len <
sizeof(
int))
5548 static int sctp_getsockopt_assoc_ids(
struct sock *sk,
int len,
5549 char __user *optval,
int __user *optlen)
5597 static int sctp_getsockopt_paddr_thresholds(
struct sock *sk,
5598 char __user *optval,
5620 trans = sctp_addr_id2transport(sk, &val.spt_address,
5636 char __user *optval,
int __user *optlen)
5651 struct sctp_af *af = sctp_sk(sk)->pf->af;
5653 retval = af->
getsockopt(sk, level, optname, optval, optlen);
5664 retval = sctp_getsockopt_sctp_status(sk, len, optval, optlen);
5667 retval = sctp_getsockopt_disable_fragments(sk, len, optval,
5671 retval = sctp_getsockopt_events(sk, len, optval, optlen);
5674 retval = sctp_getsockopt_autoclose(sk, len, optval, optlen);
5677 retval = sctp_getsockopt_peeloff(sk, len, optval, optlen);
5680 retval = sctp_getsockopt_peer_addr_params(sk, len, optval,
5684 retval = sctp_getsockopt_delayed_ack(sk, len, optval,
5688 retval = sctp_getsockopt_initmsg(sk, len, optval, optlen);
5691 retval = sctp_getsockopt_peer_addrs(sk, len, optval,
5695 retval = sctp_getsockopt_local_addrs(sk, len, optval,
5702 retval = sctp_getsockopt_default_send_param(sk, len,
5706 retval = sctp_getsockopt_primary_addr(sk, len, optval, optlen);
5709 retval = sctp_getsockopt_nodelay(sk, len, optval, optlen);
5712 retval = sctp_getsockopt_rtoinfo(sk, len, optval, optlen);
5715 retval = sctp_getsockopt_associnfo(sk, len, optval, optlen);
5718 retval = sctp_getsockopt_mappedv4(sk, len, optval, optlen);
5721 retval = sctp_getsockopt_maxseg(sk, len, optval, optlen);
5724 retval = sctp_getsockopt_peer_addr_info(sk, len, optval,
5728 retval = sctp_getsockopt_adaptation_layer(sk, len, optval,
5732 retval = sctp_getsockopt_context(sk, len, optval, optlen);
5735 retval = sctp_getsockopt_fragment_interleave(sk, len, optval,
5739 retval = sctp_getsockopt_partial_delivery_point(sk, len, optval,
5743 retval = sctp_getsockopt_maxburst(sk, len, optval, optlen);
5751 retval = sctp_getsockopt_hmac_ident(sk, len, optval, optlen);
5754 retval = sctp_getsockopt_active_key(sk, len, optval, optlen);
5757 retval = sctp_getsockopt_peer_auth_chunks(sk, len, optval,
5761 retval = sctp_getsockopt_local_auth_chunks(sk, len, optval,
5765 retval = sctp_getsockopt_assoc_number(sk, len, optval, optlen);
5768 retval = sctp_getsockopt_assoc_ids(sk, len, optval, optlen);
5771 retval = sctp_getsockopt_auto_asconf(sk, len, optval, optlen);
5774 retval = sctp_getsockopt_paddr_thresholds(sk, optval, len, optlen);
5785 static void sctp_hash(
struct sock *sk)
5790 static void sctp_unhash(
struct sock *sk)
5810 static long sctp_get_port_local(
struct sock *sk,
union sctp_addr *addr)
5815 unsigned short snum;
5818 snum =
ntohs(addr->
v4.sin_port);
5829 remaining = (high -
low) + 1;
5834 if ((rover < low) || (rover > high))
5836 if (inet_is_reserved_local_port(rover))
5838 index = sctp_phashfn(sock_net(sk), rover);
5842 if ((pp->port == rover) &&
5843 net_eq(sock_net(sk), pp->net))
5848 } while (--remaining > 0);
5870 if ((pp->
port == snum) && net_eq(pp->
net, sock_net(sk)))
5877 if (!hlist_empty(&pp->
owner)) {
5883 int reuse = sk->sk_reuse;
5903 ep2 = sctp_sk(sk2)->ep;
5906 (reuse && sk2->sk_reuse &&
5911 sctp_sk(sk2), sctp_sk(sk))) {
5921 if (!pp && !(pp = sctp_bucket_create(head, sock_net(sk), snum)))
5928 if (hlist_empty(&pp->
owner)) {
5942 if (!sctp_sk(sk)->bind_hash) {
5943 inet_sk(sk)->inet_num = snum;
5944 sk_add_bind_node(sk, &pp->
owner);
5945 sctp_sk(sk)->bind_hash =
pp;
5960 static int sctp_get_port(
struct sock *sk,
unsigned short snum)
5964 struct sctp_af *af = sctp_sk(sk)->pf->af;
5968 addr.
v4.sin_port =
htons(snum);
5971 ret = sctp_get_port_local(sk, &addr);
5986 if (!sctp_sk(sk)->hmac && sctp_hmac_alg) {
5990 sctp_hmac_alg, PTR_ERR(tfm));
5993 sctp_sk(sk)->hmac = tfm;
6008 if (!ep->
base.bind_addr.port) {
6009 if (sctp_autobind(sk))
6012 if (sctp_get_port(sk, inet_sk(sk)->inet_num)) {
6039 struct sock *sk = sock->
sk;
6064 sctp_sk(sk)->bind_hash->fastreuse = 1;
6098 struct sock *sk = sock->
sk;
6102 poll_wait(file, sk_sleep(sk), wait);
6108 return (!list_empty(&sp->
ep->asocs)) ?
6130 if (sctp_writeable(sk)) {
6142 if (sctp_writeable(sk))
6164 hlist_add_head(&pp->
node, &head->
chain);
6172 if (pp && hlist_empty(&pp->
owner)) {
6173 __hlist_del(&pp->
node);
6180 static inline void __sctp_put_port(
struct sock *sk)
6184 inet_sk(sk)->inet_num)];
6188 pp = sctp_sk(sk)->bind_hash;
6189 __sk_del_bind_node(sk);
6190 sctp_sk(sk)->bind_hash =
NULL;
6191 inet_sk(sk)->inet_num = 0;
6192 sctp_bucket_destroy(pp);
6199 __sctp_put_port(sk);
6209 static int sctp_autobind(
struct sock *sk)
6216 af = sctp_sk(sk)->pf->af;
6218 port =
htons(inet_sk(sk)->inet_num);
6321 if (cmsgs->
info->sinfo_flags &
6339 static int sctp_wait_for_packet(
struct sock * sk,
int *err,
long *timeo_p)
6347 error = sock_error(sk);
6385 error = sock_intr_errno(*timeo_p);
6397 static struct sk_buff *sctp_skb_recv_datagram(
struct sock *sk,
int flags,
6398 int noblock,
int *err)
6404 timeo = sock_rcvtimeo(sk, noblock);
6431 error = sock_error(sk);
6442 }
while (sctp_wait_for_packet(sk, err, &timeo) == 0);
6457 if ((sctp_wspace(asoc) > 0) && sock) {
6458 if (waitqueue_active(&asoc->
wait))
6461 if (sctp_writeable(sk)) {
6464 if (wq && waitqueue_active(wq))
6482 static void sctp_wfree(
struct sk_buff *skb)
6502 sk_mem_uncharge(sk, skb->
truesize);
6505 __sctp_write_space(asoc);
6517 struct sock *sk = skb->
sk;
6525 sk_mem_uncharge(sk, event->
rmem_len);
6530 static int sctp_wait_for_sndbuf(
struct sctp_association *asoc,
long *timeo_p,
6533 struct sock *sk = asoc->
base.sk;
6535 long current_timeo = *timeo_p;
6539 asoc, (
long)(*timeo_p), msg_len);
6554 goto do_interrupted;
6555 if (msg_len <= sctp_wspace(asoc))
6566 *timeo_p = current_timeo;
6582 err = sock_intr_errno(*timeo_p);
6596 if (wq_has_sleeper(wq))
6610 __sctp_write_space(asoc);
6625 static int sctp_writeable(
struct sock *sk)
6629 amt = sk->
sk_sndbuf - sk_wmem_alloc_get(sk);
6638 static int sctp_wait_for_connect(
struct sctp_association *asoc,
long *timeo_p)
6640 struct sock *sk = asoc->
base.sk;
6642 long current_timeo = *timeo_p;
6662 goto do_interrupted;
6674 *timeo_p = current_timeo;
6693 err = sock_intr_errno(*timeo_p);
6701 static int sctp_wait_for_accept(
struct sock *sk,
long timeo)
6707 ep = sctp_sk(sk)->ep;
6714 if (list_empty(&ep->
asocs)) {
6725 if (!list_empty(&ep->
asocs))
6728 err = sock_intr_errno(timeo);
6742 static void sctp_wait_for_close(
struct sock *sk,
long timeout)
6748 if (list_empty(&sctp_sk(sk)->ep->
asocs))
6753 }
while (!signal_pending(
current) && timeout);
6758 static void sctp_skb_set_owner_r_frag(
struct sk_buff *skb,
struct sock *sk)
6766 skb_walk_frags(skb, frag)
6767 sctp_skb_set_owner_r_frag(frag, sk);
6770 sctp_skb_set_owner_r(skb, sk);
6779 newsk->sk_type = sk->sk_type;
6780 newsk->sk_bound_dev_if = sk->sk_bound_dev_if;
6781 newsk->sk_flags = sk->sk_flags;
6782 newsk->sk_no_check = sk->sk_no_check;
6783 newsk->sk_reuse = sk->sk_reuse;
6785 newsk->sk_shutdown = sk->sk_shutdown;
6787 newsk->sk_family = sk->sk_family;
6789 newsk->sk_backlog_rcv = sk->sk_prot->backlog_rcv;
6790 newsk->sk_sndbuf = sk->sk_sndbuf;
6791 newsk->sk_rcvbuf = sk->sk_rcvbuf;
6792 newsk->sk_lingertime = sk->sk_lingertime;
6793 newsk->sk_rcvtimeo = sk->sk_rcvtimeo;
6794 newsk->sk_sndtimeo = sk->sk_sndtimeo;
6796 newinet = inet_sk(newsk);
6803 newinet->inet_rcv_saddr = inet->inet_rcv_saddr;
6818 static void sctp_sock_migrate(
struct sock *oldsk,
struct sock *newsk,
6822 struct sctp_sock *oldsp = sctp_sk(oldsk);
6823 struct sctp_sock *newsp = sctp_sk(newsk);
6839 inet_sk_copy_descendant(newsk, oldsk);
6842 inet_sk_copy_descendant(newsk, oldsk);
6852 inet_sk(oldsk)->inet_num)];
6855 pp = sctp_sk(oldsk)->bind_hash;
6856 sk_add_bind_node(newsk, &pp->
owner);
6857 sctp_sk(newsk)->bind_hash =
pp;
6858 inet_sk(newsk)->inet_num = inet_sk(oldsk)->inet_num;
6872 event = sctp_skb2event(skb);
6873 if (event->
asoc == assoc) {
6876 sctp_skb_set_owner_r_frag(skb, newsk);
6886 skb_queue_head_init(&newsp->
pd_lobby);
6893 if (assoc->
ulpq.pd_mode) {
6902 event = sctp_skb2event(skb);
6903 if (event->
asoc == assoc) {
6904 __skb_unlink(skb, &oldsp->
pd_lobby);
6905 __skb_queue_tail(queue, skb);
6906 sctp_skb_set_owner_r_frag(skb, newsk);
6913 if (assoc->
ulpq.pd_mode)
6919 sctp_skb_set_owner_r_frag(skb, newsk);
6922 sctp_skb_set_owner_r_frag(skb, newsk);
6972 .unhash = sctp_unhash,
6973 .get_port = sctp_get_port,
6978 .memory_pressure = &sctp_memory_pressure,
6979 .enter_memory_pressure = sctp_enter_memory_pressure,
6980 .memory_allocated = &sctp_memory_allocated,
6984 #if IS_ENABLED(CONFIG_IPV6)
7004 .unhash = sctp_unhash,
7005 .get_port = sctp_get_port,
7006 .obj_size =
sizeof(
struct sctp6_sock),
7010 .memory_pressure = &sctp_memory_pressure,
7011 .enter_memory_pressure = sctp_enter_memory_pressure,
7012 .memory_allocated = &sctp_memory_allocated,