1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /* SCTP kernel implementation
3*4882a593Smuzhiyun * (C) Copyright IBM Corp. 2001, 2004
4*4882a593Smuzhiyun * Copyright (c) 1999-2000 Cisco, Inc.
5*4882a593Smuzhiyun * Copyright (c) 1999-2001 Motorola, Inc.
6*4882a593Smuzhiyun * Copyright (c) 2001-2003 Intel Corp.
7*4882a593Smuzhiyun * Copyright (c) 2001-2002 Nokia, Inc.
8*4882a593Smuzhiyun * Copyright (c) 2001 La Monte H.P. Yarroll
9*4882a593Smuzhiyun *
10*4882a593Smuzhiyun * This file is part of the SCTP kernel implementation
11*4882a593Smuzhiyun *
12*4882a593Smuzhiyun * These functions interface with the sockets layer to implement the
13*4882a593Smuzhiyun * SCTP Extensions for the Sockets API.
14*4882a593Smuzhiyun *
15*4882a593Smuzhiyun * Note that the descriptions from the specification are USER level
16*4882a593Smuzhiyun * functions--this file is the functions which populate the struct proto
17*4882a593Smuzhiyun * for SCTP which is the BOTTOM of the sockets interface.
18*4882a593Smuzhiyun *
19*4882a593Smuzhiyun * Please send any bug reports or fixes you make to the
20*4882a593Smuzhiyun * email address(es):
21*4882a593Smuzhiyun * lksctp developers <linux-sctp@vger.kernel.org>
22*4882a593Smuzhiyun *
23*4882a593Smuzhiyun * Written or modified by:
24*4882a593Smuzhiyun * La Monte H.P. Yarroll <piggy@acm.org>
25*4882a593Smuzhiyun * Narasimha Budihal <narsi@refcode.org>
26*4882a593Smuzhiyun * Karl Knutson <karl@athena.chicago.il.us>
27*4882a593Smuzhiyun * Jon Grimm <jgrimm@us.ibm.com>
28*4882a593Smuzhiyun * Xingang Guo <xingang.guo@intel.com>
29*4882a593Smuzhiyun * Daisy Chang <daisyc@us.ibm.com>
30*4882a593Smuzhiyun * Sridhar Samudrala <samudrala@us.ibm.com>
31*4882a593Smuzhiyun * Inaky Perez-Gonzalez <inaky.gonzalez@intel.com>
32*4882a593Smuzhiyun * Ardelle Fan <ardelle.fan@intel.com>
33*4882a593Smuzhiyun * Ryan Layer <rmlayer@us.ibm.com>
34*4882a593Smuzhiyun * Anup Pemmaiah <pemmaiah@cc.usu.edu>
35*4882a593Smuzhiyun * Kevin Gao <kevin.gao@intel.com>
36*4882a593Smuzhiyun */
37*4882a593Smuzhiyun
38*4882a593Smuzhiyun #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
39*4882a593Smuzhiyun
40*4882a593Smuzhiyun #include <crypto/hash.h>
41*4882a593Smuzhiyun #include <linux/types.h>
42*4882a593Smuzhiyun #include <linux/kernel.h>
43*4882a593Smuzhiyun #include <linux/wait.h>
44*4882a593Smuzhiyun #include <linux/time.h>
45*4882a593Smuzhiyun #include <linux/sched/signal.h>
46*4882a593Smuzhiyun #include <linux/ip.h>
47*4882a593Smuzhiyun #include <linux/capability.h>
48*4882a593Smuzhiyun #include <linux/fcntl.h>
49*4882a593Smuzhiyun #include <linux/poll.h>
50*4882a593Smuzhiyun #include <linux/init.h>
51*4882a593Smuzhiyun #include <linux/slab.h>
52*4882a593Smuzhiyun #include <linux/file.h>
53*4882a593Smuzhiyun #include <linux/compat.h>
54*4882a593Smuzhiyun #include <linux/rhashtable.h>
55*4882a593Smuzhiyun
56*4882a593Smuzhiyun #include <net/ip.h>
57*4882a593Smuzhiyun #include <net/icmp.h>
58*4882a593Smuzhiyun #include <net/route.h>
59*4882a593Smuzhiyun #include <net/ipv6.h>
60*4882a593Smuzhiyun #include <net/inet_common.h>
61*4882a593Smuzhiyun #include <net/busy_poll.h>
62*4882a593Smuzhiyun
63*4882a593Smuzhiyun #include <linux/socket.h> /* for sa_family_t */
64*4882a593Smuzhiyun #include <linux/export.h>
65*4882a593Smuzhiyun #include <net/sock.h>
66*4882a593Smuzhiyun #include <net/sctp/sctp.h>
67*4882a593Smuzhiyun #include <net/sctp/sm.h>
68*4882a593Smuzhiyun #include <net/sctp/stream_sched.h>
69*4882a593Smuzhiyun
70*4882a593Smuzhiyun /* Forward declarations for internal helper functions. */
71*4882a593Smuzhiyun static bool sctp_writeable(struct sock *sk);
72*4882a593Smuzhiyun static void sctp_wfree(struct sk_buff *skb);
73*4882a593Smuzhiyun static int sctp_wait_for_sndbuf(struct sctp_association *asoc, long *timeo_p,
74*4882a593Smuzhiyun size_t msg_len);
75*4882a593Smuzhiyun static int sctp_wait_for_packet(struct sock *sk, int *err, long *timeo_p);
76*4882a593Smuzhiyun static int sctp_wait_for_connect(struct sctp_association *, long *timeo_p);
77*4882a593Smuzhiyun static int sctp_wait_for_accept(struct sock *sk, long timeo);
78*4882a593Smuzhiyun static void sctp_wait_for_close(struct sock *sk, long timeo);
79*4882a593Smuzhiyun static void sctp_destruct_sock(struct sock *sk);
80*4882a593Smuzhiyun static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt,
81*4882a593Smuzhiyun union sctp_addr *addr, int len);
82*4882a593Smuzhiyun static int sctp_bindx_add(struct sock *, struct sockaddr *, int);
83*4882a593Smuzhiyun static int sctp_bindx_rem(struct sock *, struct sockaddr *, int);
84*4882a593Smuzhiyun static int sctp_send_asconf_add_ip(struct sock *, struct sockaddr *, int);
85*4882a593Smuzhiyun static int sctp_send_asconf_del_ip(struct sock *, struct sockaddr *, int);
86*4882a593Smuzhiyun static int sctp_send_asconf(struct sctp_association *asoc,
87*4882a593Smuzhiyun struct sctp_chunk *chunk);
88*4882a593Smuzhiyun static int sctp_do_bind(struct sock *, union sctp_addr *, int);
89*4882a593Smuzhiyun static int sctp_autobind(struct sock *sk);
90*4882a593Smuzhiyun static int sctp_sock_migrate(struct sock *oldsk, struct sock *newsk,
91*4882a593Smuzhiyun struct sctp_association *assoc,
92*4882a593Smuzhiyun enum sctp_socket_type type);
93*4882a593Smuzhiyun
94*4882a593Smuzhiyun static unsigned long sctp_memory_pressure;
95*4882a593Smuzhiyun static atomic_long_t sctp_memory_allocated;
96*4882a593Smuzhiyun struct percpu_counter sctp_sockets_allocated;
97*4882a593Smuzhiyun
sctp_enter_memory_pressure(struct sock * sk)98*4882a593Smuzhiyun static void sctp_enter_memory_pressure(struct sock *sk)
99*4882a593Smuzhiyun {
100*4882a593Smuzhiyun sctp_memory_pressure = 1;
101*4882a593Smuzhiyun }
102*4882a593Smuzhiyun
103*4882a593Smuzhiyun
104*4882a593Smuzhiyun /* Get the sndbuf space available at the time on the association. */
sctp_wspace(struct sctp_association * asoc)105*4882a593Smuzhiyun static inline int sctp_wspace(struct sctp_association *asoc)
106*4882a593Smuzhiyun {
107*4882a593Smuzhiyun struct sock *sk = asoc->base.sk;
108*4882a593Smuzhiyun
109*4882a593Smuzhiyun return asoc->ep->sndbuf_policy ? sk->sk_sndbuf - asoc->sndbuf_used
110*4882a593Smuzhiyun : sk_stream_wspace(sk);
111*4882a593Smuzhiyun }
112*4882a593Smuzhiyun
113*4882a593Smuzhiyun /* Increment the used sndbuf space count of the corresponding association by
114*4882a593Smuzhiyun * the size of the outgoing data chunk.
115*4882a593Smuzhiyun * Also, set the skb destructor for sndbuf accounting later.
116*4882a593Smuzhiyun *
117*4882a593Smuzhiyun * Since it is always 1-1 between chunk and skb, and also a new skb is always
118*4882a593Smuzhiyun * allocated for chunk bundling in sctp_packet_transmit(), we can use the
119*4882a593Smuzhiyun * destructor in the data chunk skb for the purpose of the sndbuf space
120*4882a593Smuzhiyun * tracking.
121*4882a593Smuzhiyun */
sctp_set_owner_w(struct sctp_chunk * chunk)122*4882a593Smuzhiyun static inline void sctp_set_owner_w(struct sctp_chunk *chunk)
123*4882a593Smuzhiyun {
124*4882a593Smuzhiyun struct sctp_association *asoc = chunk->asoc;
125*4882a593Smuzhiyun struct sock *sk = asoc->base.sk;
126*4882a593Smuzhiyun
127*4882a593Smuzhiyun /* The sndbuf space is tracked per association. */
128*4882a593Smuzhiyun sctp_association_hold(asoc);
129*4882a593Smuzhiyun
130*4882a593Smuzhiyun if (chunk->shkey)
131*4882a593Smuzhiyun sctp_auth_shkey_hold(chunk->shkey);
132*4882a593Smuzhiyun
133*4882a593Smuzhiyun skb_set_owner_w(chunk->skb, sk);
134*4882a593Smuzhiyun
135*4882a593Smuzhiyun chunk->skb->destructor = sctp_wfree;
136*4882a593Smuzhiyun /* Save the chunk pointer in skb for sctp_wfree to use later. */
137*4882a593Smuzhiyun skb_shinfo(chunk->skb)->destructor_arg = chunk;
138*4882a593Smuzhiyun
139*4882a593Smuzhiyun refcount_add(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc);
140*4882a593Smuzhiyun asoc->sndbuf_used += chunk->skb->truesize + sizeof(struct sctp_chunk);
141*4882a593Smuzhiyun sk->sk_wmem_queued += chunk->skb->truesize + sizeof(struct sctp_chunk);
142*4882a593Smuzhiyun sk_mem_charge(sk, chunk->skb->truesize);
143*4882a593Smuzhiyun }
144*4882a593Smuzhiyun
sctp_clear_owner_w(struct sctp_chunk * chunk)145*4882a593Smuzhiyun static void sctp_clear_owner_w(struct sctp_chunk *chunk)
146*4882a593Smuzhiyun {
147*4882a593Smuzhiyun skb_orphan(chunk->skb);
148*4882a593Smuzhiyun }
149*4882a593Smuzhiyun
150*4882a593Smuzhiyun #define traverse_and_process() \
151*4882a593Smuzhiyun do { \
152*4882a593Smuzhiyun msg = chunk->msg; \
153*4882a593Smuzhiyun if (msg == prev_msg) \
154*4882a593Smuzhiyun continue; \
155*4882a593Smuzhiyun list_for_each_entry(c, &msg->chunks, frag_list) { \
156*4882a593Smuzhiyun if ((clear && asoc->base.sk == c->skb->sk) || \
157*4882a593Smuzhiyun (!clear && asoc->base.sk != c->skb->sk)) \
158*4882a593Smuzhiyun cb(c); \
159*4882a593Smuzhiyun } \
160*4882a593Smuzhiyun prev_msg = msg; \
161*4882a593Smuzhiyun } while (0)
162*4882a593Smuzhiyun
sctp_for_each_tx_datachunk(struct sctp_association * asoc,bool clear,void (* cb)(struct sctp_chunk *))163*4882a593Smuzhiyun static void sctp_for_each_tx_datachunk(struct sctp_association *asoc,
164*4882a593Smuzhiyun bool clear,
165*4882a593Smuzhiyun void (*cb)(struct sctp_chunk *))
166*4882a593Smuzhiyun
167*4882a593Smuzhiyun {
168*4882a593Smuzhiyun struct sctp_datamsg *msg, *prev_msg = NULL;
169*4882a593Smuzhiyun struct sctp_outq *q = &asoc->outqueue;
170*4882a593Smuzhiyun struct sctp_chunk *chunk, *c;
171*4882a593Smuzhiyun struct sctp_transport *t;
172*4882a593Smuzhiyun
173*4882a593Smuzhiyun list_for_each_entry(t, &asoc->peer.transport_addr_list, transports)
174*4882a593Smuzhiyun list_for_each_entry(chunk, &t->transmitted, transmitted_list)
175*4882a593Smuzhiyun traverse_and_process();
176*4882a593Smuzhiyun
177*4882a593Smuzhiyun list_for_each_entry(chunk, &q->retransmit, transmitted_list)
178*4882a593Smuzhiyun traverse_and_process();
179*4882a593Smuzhiyun
180*4882a593Smuzhiyun list_for_each_entry(chunk, &q->sacked, transmitted_list)
181*4882a593Smuzhiyun traverse_and_process();
182*4882a593Smuzhiyun
183*4882a593Smuzhiyun list_for_each_entry(chunk, &q->abandoned, transmitted_list)
184*4882a593Smuzhiyun traverse_and_process();
185*4882a593Smuzhiyun
186*4882a593Smuzhiyun list_for_each_entry(chunk, &q->out_chunk_list, list)
187*4882a593Smuzhiyun traverse_and_process();
188*4882a593Smuzhiyun }
189*4882a593Smuzhiyun
sctp_for_each_rx_skb(struct sctp_association * asoc,struct sock * sk,void (* cb)(struct sk_buff *,struct sock *))190*4882a593Smuzhiyun static void sctp_for_each_rx_skb(struct sctp_association *asoc, struct sock *sk,
191*4882a593Smuzhiyun void (*cb)(struct sk_buff *, struct sock *))
192*4882a593Smuzhiyun
193*4882a593Smuzhiyun {
194*4882a593Smuzhiyun struct sk_buff *skb, *tmp;
195*4882a593Smuzhiyun
196*4882a593Smuzhiyun sctp_skb_for_each(skb, &asoc->ulpq.lobby, tmp)
197*4882a593Smuzhiyun cb(skb, sk);
198*4882a593Smuzhiyun
199*4882a593Smuzhiyun sctp_skb_for_each(skb, &asoc->ulpq.reasm, tmp)
200*4882a593Smuzhiyun cb(skb, sk);
201*4882a593Smuzhiyun
202*4882a593Smuzhiyun sctp_skb_for_each(skb, &asoc->ulpq.reasm_uo, tmp)
203*4882a593Smuzhiyun cb(skb, sk);
204*4882a593Smuzhiyun }
205*4882a593Smuzhiyun
206*4882a593Smuzhiyun /* Verify that this is a valid address. */
sctp_verify_addr(struct sock * sk,union sctp_addr * addr,int len)207*4882a593Smuzhiyun static inline int sctp_verify_addr(struct sock *sk, union sctp_addr *addr,
208*4882a593Smuzhiyun int len)
209*4882a593Smuzhiyun {
210*4882a593Smuzhiyun struct sctp_af *af;
211*4882a593Smuzhiyun
212*4882a593Smuzhiyun /* Verify basic sockaddr. */
213*4882a593Smuzhiyun af = sctp_sockaddr_af(sctp_sk(sk), addr, len);
214*4882a593Smuzhiyun if (!af)
215*4882a593Smuzhiyun return -EINVAL;
216*4882a593Smuzhiyun
217*4882a593Smuzhiyun /* Is this a valid SCTP address? */
218*4882a593Smuzhiyun if (!af->addr_valid(addr, sctp_sk(sk), NULL))
219*4882a593Smuzhiyun return -EINVAL;
220*4882a593Smuzhiyun
221*4882a593Smuzhiyun if (!sctp_sk(sk)->pf->send_verify(sctp_sk(sk), (addr)))
222*4882a593Smuzhiyun return -EINVAL;
223*4882a593Smuzhiyun
224*4882a593Smuzhiyun return 0;
225*4882a593Smuzhiyun }
226*4882a593Smuzhiyun
227*4882a593Smuzhiyun /* Look up the association by its id. If this is not a UDP-style
228*4882a593Smuzhiyun * socket, the ID field is always ignored.
229*4882a593Smuzhiyun */
sctp_id2assoc(struct sock * sk,sctp_assoc_t id)230*4882a593Smuzhiyun struct sctp_association *sctp_id2assoc(struct sock *sk, sctp_assoc_t id)
231*4882a593Smuzhiyun {
232*4882a593Smuzhiyun struct sctp_association *asoc = NULL;
233*4882a593Smuzhiyun
234*4882a593Smuzhiyun /* If this is not a UDP-style socket, assoc id should be ignored. */
235*4882a593Smuzhiyun if (!sctp_style(sk, UDP)) {
236*4882a593Smuzhiyun /* Return NULL if the socket state is not ESTABLISHED. It
237*4882a593Smuzhiyun * could be a TCP-style listening socket or a socket which
238*4882a593Smuzhiyun * hasn't yet called connect() to establish an association.
239*4882a593Smuzhiyun */
240*4882a593Smuzhiyun if (!sctp_sstate(sk, ESTABLISHED) && !sctp_sstate(sk, CLOSING))
241*4882a593Smuzhiyun return NULL;
242*4882a593Smuzhiyun
243*4882a593Smuzhiyun /* Get the first and the only association from the list. */
244*4882a593Smuzhiyun if (!list_empty(&sctp_sk(sk)->ep->asocs))
245*4882a593Smuzhiyun asoc = list_entry(sctp_sk(sk)->ep->asocs.next,
246*4882a593Smuzhiyun struct sctp_association, asocs);
247*4882a593Smuzhiyun return asoc;
248*4882a593Smuzhiyun }
249*4882a593Smuzhiyun
250*4882a593Smuzhiyun /* Otherwise this is a UDP-style socket. */
251*4882a593Smuzhiyun if (id <= SCTP_ALL_ASSOC)
252*4882a593Smuzhiyun return NULL;
253*4882a593Smuzhiyun
254*4882a593Smuzhiyun spin_lock_bh(&sctp_assocs_id_lock);
255*4882a593Smuzhiyun asoc = (struct sctp_association *)idr_find(&sctp_assocs_id, (int)id);
256*4882a593Smuzhiyun if (asoc && (asoc->base.sk != sk || asoc->base.dead))
257*4882a593Smuzhiyun asoc = NULL;
258*4882a593Smuzhiyun spin_unlock_bh(&sctp_assocs_id_lock);
259*4882a593Smuzhiyun
260*4882a593Smuzhiyun return asoc;
261*4882a593Smuzhiyun }
262*4882a593Smuzhiyun
263*4882a593Smuzhiyun /* Look up the transport from an address and an assoc id. If both address and
264*4882a593Smuzhiyun * id are specified, the associations matching the address and the id should be
265*4882a593Smuzhiyun * the same.
266*4882a593Smuzhiyun */
sctp_addr_id2transport(struct sock * sk,struct sockaddr_storage * addr,sctp_assoc_t id)267*4882a593Smuzhiyun static struct sctp_transport *sctp_addr_id2transport(struct sock *sk,
268*4882a593Smuzhiyun struct sockaddr_storage *addr,
269*4882a593Smuzhiyun sctp_assoc_t id)
270*4882a593Smuzhiyun {
271*4882a593Smuzhiyun struct sctp_association *addr_asoc = NULL, *id_asoc = NULL;
272*4882a593Smuzhiyun struct sctp_af *af = sctp_get_af_specific(addr->ss_family);
273*4882a593Smuzhiyun union sctp_addr *laddr = (union sctp_addr *)addr;
274*4882a593Smuzhiyun struct sctp_transport *transport;
275*4882a593Smuzhiyun
276*4882a593Smuzhiyun if (!af || sctp_verify_addr(sk, laddr, af->sockaddr_len))
277*4882a593Smuzhiyun return NULL;
278*4882a593Smuzhiyun
279*4882a593Smuzhiyun addr_asoc = sctp_endpoint_lookup_assoc(sctp_sk(sk)->ep,
280*4882a593Smuzhiyun laddr,
281*4882a593Smuzhiyun &transport);
282*4882a593Smuzhiyun
283*4882a593Smuzhiyun if (!addr_asoc)
284*4882a593Smuzhiyun return NULL;
285*4882a593Smuzhiyun
286*4882a593Smuzhiyun id_asoc = sctp_id2assoc(sk, id);
287*4882a593Smuzhiyun if (id_asoc && (id_asoc != addr_asoc))
288*4882a593Smuzhiyun return NULL;
289*4882a593Smuzhiyun
290*4882a593Smuzhiyun sctp_get_pf_specific(sk->sk_family)->addr_to_user(sctp_sk(sk),
291*4882a593Smuzhiyun (union sctp_addr *)addr);
292*4882a593Smuzhiyun
293*4882a593Smuzhiyun return transport;
294*4882a593Smuzhiyun }
295*4882a593Smuzhiyun
296*4882a593Smuzhiyun /* API 3.1.2 bind() - UDP Style Syntax
297*4882a593Smuzhiyun * The syntax of bind() is,
298*4882a593Smuzhiyun *
299*4882a593Smuzhiyun * ret = bind(int sd, struct sockaddr *addr, int addrlen);
300*4882a593Smuzhiyun *
301*4882a593Smuzhiyun * sd - the socket descriptor returned by socket().
302*4882a593Smuzhiyun * addr - the address structure (struct sockaddr_in or struct
303*4882a593Smuzhiyun * sockaddr_in6 [RFC 2553]),
304*4882a593Smuzhiyun * addr_len - the size of the address structure.
305*4882a593Smuzhiyun */
sctp_bind(struct sock * sk,struct sockaddr * addr,int addr_len)306*4882a593Smuzhiyun static int sctp_bind(struct sock *sk, struct sockaddr *addr, int addr_len)
307*4882a593Smuzhiyun {
308*4882a593Smuzhiyun int retval = 0;
309*4882a593Smuzhiyun
310*4882a593Smuzhiyun lock_sock(sk);
311*4882a593Smuzhiyun
312*4882a593Smuzhiyun pr_debug("%s: sk:%p, addr:%p, addr_len:%d\n", __func__, sk,
313*4882a593Smuzhiyun addr, addr_len);
314*4882a593Smuzhiyun
315*4882a593Smuzhiyun /* Disallow binding twice. */
316*4882a593Smuzhiyun if (!sctp_sk(sk)->ep->base.bind_addr.port)
317*4882a593Smuzhiyun retval = sctp_do_bind(sk, (union sctp_addr *)addr,
318*4882a593Smuzhiyun addr_len);
319*4882a593Smuzhiyun else
320*4882a593Smuzhiyun retval = -EINVAL;
321*4882a593Smuzhiyun
322*4882a593Smuzhiyun release_sock(sk);
323*4882a593Smuzhiyun
324*4882a593Smuzhiyun return retval;
325*4882a593Smuzhiyun }
326*4882a593Smuzhiyun
327*4882a593Smuzhiyun static int sctp_get_port_local(struct sock *, union sctp_addr *);
328*4882a593Smuzhiyun
329*4882a593Smuzhiyun /* Verify this is a valid sockaddr. */
sctp_sockaddr_af(struct sctp_sock * opt,union sctp_addr * addr,int len)330*4882a593Smuzhiyun static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt,
331*4882a593Smuzhiyun union sctp_addr *addr, int len)
332*4882a593Smuzhiyun {
333*4882a593Smuzhiyun struct sctp_af *af;
334*4882a593Smuzhiyun
335*4882a593Smuzhiyun /* Check minimum size. */
336*4882a593Smuzhiyun if (len < sizeof (struct sockaddr))
337*4882a593Smuzhiyun return NULL;
338*4882a593Smuzhiyun
339*4882a593Smuzhiyun if (!opt->pf->af_supported(addr->sa.sa_family, opt))
340*4882a593Smuzhiyun return NULL;
341*4882a593Smuzhiyun
342*4882a593Smuzhiyun if (addr->sa.sa_family == AF_INET6) {
343*4882a593Smuzhiyun if (len < SIN6_LEN_RFC2133)
344*4882a593Smuzhiyun return NULL;
345*4882a593Smuzhiyun /* V4 mapped address are really of AF_INET family */
346*4882a593Smuzhiyun if (ipv6_addr_v4mapped(&addr->v6.sin6_addr) &&
347*4882a593Smuzhiyun !opt->pf->af_supported(AF_INET, opt))
348*4882a593Smuzhiyun return NULL;
349*4882a593Smuzhiyun }
350*4882a593Smuzhiyun
351*4882a593Smuzhiyun /* If we get this far, af is valid. */
352*4882a593Smuzhiyun af = sctp_get_af_specific(addr->sa.sa_family);
353*4882a593Smuzhiyun
354*4882a593Smuzhiyun if (len < af->sockaddr_len)
355*4882a593Smuzhiyun return NULL;
356*4882a593Smuzhiyun
357*4882a593Smuzhiyun return af;
358*4882a593Smuzhiyun }
359*4882a593Smuzhiyun
sctp_auto_asconf_init(struct sctp_sock * sp)360*4882a593Smuzhiyun static void sctp_auto_asconf_init(struct sctp_sock *sp)
361*4882a593Smuzhiyun {
362*4882a593Smuzhiyun struct net *net = sock_net(&sp->inet.sk);
363*4882a593Smuzhiyun
364*4882a593Smuzhiyun if (net->sctp.default_auto_asconf) {
365*4882a593Smuzhiyun spin_lock(&net->sctp.addr_wq_lock);
366*4882a593Smuzhiyun list_add_tail(&sp->auto_asconf_list, &net->sctp.auto_asconf_splist);
367*4882a593Smuzhiyun spin_unlock(&net->sctp.addr_wq_lock);
368*4882a593Smuzhiyun sp->do_auto_asconf = 1;
369*4882a593Smuzhiyun }
370*4882a593Smuzhiyun }
371*4882a593Smuzhiyun
372*4882a593Smuzhiyun /* Bind a local address either to an endpoint or to an association. */
sctp_do_bind(struct sock * sk,union sctp_addr * addr,int len)373*4882a593Smuzhiyun static int sctp_do_bind(struct sock *sk, union sctp_addr *addr, int len)
374*4882a593Smuzhiyun {
375*4882a593Smuzhiyun struct net *net = sock_net(sk);
376*4882a593Smuzhiyun struct sctp_sock *sp = sctp_sk(sk);
377*4882a593Smuzhiyun struct sctp_endpoint *ep = sp->ep;
378*4882a593Smuzhiyun struct sctp_bind_addr *bp = &ep->base.bind_addr;
379*4882a593Smuzhiyun struct sctp_af *af;
380*4882a593Smuzhiyun unsigned short snum;
381*4882a593Smuzhiyun int ret = 0;
382*4882a593Smuzhiyun
383*4882a593Smuzhiyun /* Common sockaddr verification. */
384*4882a593Smuzhiyun af = sctp_sockaddr_af(sp, addr, len);
385*4882a593Smuzhiyun if (!af) {
386*4882a593Smuzhiyun pr_debug("%s: sk:%p, newaddr:%p, len:%d EINVAL\n",
387*4882a593Smuzhiyun __func__, sk, addr, len);
388*4882a593Smuzhiyun return -EINVAL;
389*4882a593Smuzhiyun }
390*4882a593Smuzhiyun
391*4882a593Smuzhiyun snum = ntohs(addr->v4.sin_port);
392*4882a593Smuzhiyun
393*4882a593Smuzhiyun pr_debug("%s: sk:%p, new addr:%pISc, port:%d, new port:%d, len:%d\n",
394*4882a593Smuzhiyun __func__, sk, &addr->sa, bp->port, snum, len);
395*4882a593Smuzhiyun
396*4882a593Smuzhiyun /* PF specific bind() address verification. */
397*4882a593Smuzhiyun if (!sp->pf->bind_verify(sp, addr))
398*4882a593Smuzhiyun return -EADDRNOTAVAIL;
399*4882a593Smuzhiyun
400*4882a593Smuzhiyun /* We must either be unbound, or bind to the same port.
401*4882a593Smuzhiyun * It's OK to allow 0 ports if we are already bound.
402*4882a593Smuzhiyun * We'll just inhert an already bound port in this case
403*4882a593Smuzhiyun */
404*4882a593Smuzhiyun if (bp->port) {
405*4882a593Smuzhiyun if (!snum)
406*4882a593Smuzhiyun snum = bp->port;
407*4882a593Smuzhiyun else if (snum != bp->port) {
408*4882a593Smuzhiyun pr_debug("%s: new port %d doesn't match existing port "
409*4882a593Smuzhiyun "%d\n", __func__, snum, bp->port);
410*4882a593Smuzhiyun return -EINVAL;
411*4882a593Smuzhiyun }
412*4882a593Smuzhiyun }
413*4882a593Smuzhiyun
414*4882a593Smuzhiyun if (snum && inet_is_local_unbindable_port(net, snum))
415*4882a593Smuzhiyun return -EPERM;
416*4882a593Smuzhiyun
417*4882a593Smuzhiyun if (snum && inet_port_requires_bind_service(net, snum) &&
418*4882a593Smuzhiyun !ns_capable(net->user_ns, CAP_NET_BIND_SERVICE))
419*4882a593Smuzhiyun return -EACCES;
420*4882a593Smuzhiyun
421*4882a593Smuzhiyun /* See if the address matches any of the addresses we may have
422*4882a593Smuzhiyun * already bound before checking against other endpoints.
423*4882a593Smuzhiyun */
424*4882a593Smuzhiyun if (sctp_bind_addr_match(bp, addr, sp))
425*4882a593Smuzhiyun return -EINVAL;
426*4882a593Smuzhiyun
427*4882a593Smuzhiyun /* Make sure we are allowed to bind here.
428*4882a593Smuzhiyun * The function sctp_get_port_local() does duplicate address
429*4882a593Smuzhiyun * detection.
430*4882a593Smuzhiyun */
431*4882a593Smuzhiyun addr->v4.sin_port = htons(snum);
432*4882a593Smuzhiyun if (sctp_get_port_local(sk, addr))
433*4882a593Smuzhiyun return -EADDRINUSE;
434*4882a593Smuzhiyun
435*4882a593Smuzhiyun /* Refresh ephemeral port. */
436*4882a593Smuzhiyun if (!bp->port) {
437*4882a593Smuzhiyun bp->port = inet_sk(sk)->inet_num;
438*4882a593Smuzhiyun sctp_auto_asconf_init(sp);
439*4882a593Smuzhiyun }
440*4882a593Smuzhiyun
441*4882a593Smuzhiyun /* Add the address to the bind address list.
442*4882a593Smuzhiyun * Use GFP_ATOMIC since BHs will be disabled.
443*4882a593Smuzhiyun */
444*4882a593Smuzhiyun ret = sctp_add_bind_addr(bp, addr, af->sockaddr_len,
445*4882a593Smuzhiyun SCTP_ADDR_SRC, GFP_ATOMIC);
446*4882a593Smuzhiyun
447*4882a593Smuzhiyun if (ret) {
448*4882a593Smuzhiyun sctp_put_port(sk);
449*4882a593Smuzhiyun return ret;
450*4882a593Smuzhiyun }
451*4882a593Smuzhiyun /* Copy back into socket for getsockname() use. */
452*4882a593Smuzhiyun inet_sk(sk)->inet_sport = htons(inet_sk(sk)->inet_num);
453*4882a593Smuzhiyun sp->pf->to_sk_saddr(addr, sk);
454*4882a593Smuzhiyun
455*4882a593Smuzhiyun return ret;
456*4882a593Smuzhiyun }
457*4882a593Smuzhiyun
458*4882a593Smuzhiyun /* ADDIP Section 4.1.1 Congestion Control of ASCONF Chunks
459*4882a593Smuzhiyun *
460*4882a593Smuzhiyun * R1) One and only one ASCONF Chunk MAY be in transit and unacknowledged
461*4882a593Smuzhiyun * at any one time. If a sender, after sending an ASCONF chunk, decides
462*4882a593Smuzhiyun * it needs to transfer another ASCONF Chunk, it MUST wait until the
463*4882a593Smuzhiyun * ASCONF-ACK Chunk returns from the previous ASCONF Chunk before sending a
464*4882a593Smuzhiyun * subsequent ASCONF. Note this restriction binds each side, so at any
465*4882a593Smuzhiyun * time two ASCONF may be in-transit on any given association (one sent
466*4882a593Smuzhiyun * from each endpoint).
467*4882a593Smuzhiyun */
sctp_send_asconf(struct sctp_association * asoc,struct sctp_chunk * chunk)468*4882a593Smuzhiyun static int sctp_send_asconf(struct sctp_association *asoc,
469*4882a593Smuzhiyun struct sctp_chunk *chunk)
470*4882a593Smuzhiyun {
471*4882a593Smuzhiyun int retval = 0;
472*4882a593Smuzhiyun
473*4882a593Smuzhiyun /* If there is an outstanding ASCONF chunk, queue it for later
474*4882a593Smuzhiyun * transmission.
475*4882a593Smuzhiyun */
476*4882a593Smuzhiyun if (asoc->addip_last_asconf) {
477*4882a593Smuzhiyun list_add_tail(&chunk->list, &asoc->addip_chunk_list);
478*4882a593Smuzhiyun goto out;
479*4882a593Smuzhiyun }
480*4882a593Smuzhiyun
481*4882a593Smuzhiyun /* Hold the chunk until an ASCONF_ACK is received. */
482*4882a593Smuzhiyun sctp_chunk_hold(chunk);
483*4882a593Smuzhiyun retval = sctp_primitive_ASCONF(asoc->base.net, asoc, chunk);
484*4882a593Smuzhiyun if (retval)
485*4882a593Smuzhiyun sctp_chunk_free(chunk);
486*4882a593Smuzhiyun else
487*4882a593Smuzhiyun asoc->addip_last_asconf = chunk;
488*4882a593Smuzhiyun
489*4882a593Smuzhiyun out:
490*4882a593Smuzhiyun return retval;
491*4882a593Smuzhiyun }
492*4882a593Smuzhiyun
493*4882a593Smuzhiyun /* Add a list of addresses as bind addresses to local endpoint or
494*4882a593Smuzhiyun * association.
495*4882a593Smuzhiyun *
496*4882a593Smuzhiyun * Basically run through each address specified in the addrs/addrcnt
497*4882a593Smuzhiyun * array/length pair, determine if it is IPv6 or IPv4 and call
498*4882a593Smuzhiyun * sctp_do_bind() on it.
499*4882a593Smuzhiyun *
500*4882a593Smuzhiyun * If any of them fails, then the operation will be reversed and the
501*4882a593Smuzhiyun * ones that were added will be removed.
502*4882a593Smuzhiyun *
503*4882a593Smuzhiyun * Only sctp_setsockopt_bindx() is supposed to call this function.
504*4882a593Smuzhiyun */
sctp_bindx_add(struct sock * sk,struct sockaddr * addrs,int addrcnt)505*4882a593Smuzhiyun static int sctp_bindx_add(struct sock *sk, struct sockaddr *addrs, int addrcnt)
506*4882a593Smuzhiyun {
507*4882a593Smuzhiyun int cnt;
508*4882a593Smuzhiyun int retval = 0;
509*4882a593Smuzhiyun void *addr_buf;
510*4882a593Smuzhiyun struct sockaddr *sa_addr;
511*4882a593Smuzhiyun struct sctp_af *af;
512*4882a593Smuzhiyun
513*4882a593Smuzhiyun pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n", __func__, sk,
514*4882a593Smuzhiyun addrs, addrcnt);
515*4882a593Smuzhiyun
516*4882a593Smuzhiyun addr_buf = addrs;
517*4882a593Smuzhiyun for (cnt = 0; cnt < addrcnt; cnt++) {
518*4882a593Smuzhiyun /* The list may contain either IPv4 or IPv6 address;
519*4882a593Smuzhiyun * determine the address length for walking thru the list.
520*4882a593Smuzhiyun */
521*4882a593Smuzhiyun sa_addr = addr_buf;
522*4882a593Smuzhiyun af = sctp_get_af_specific(sa_addr->sa_family);
523*4882a593Smuzhiyun if (!af) {
524*4882a593Smuzhiyun retval = -EINVAL;
525*4882a593Smuzhiyun goto err_bindx_add;
526*4882a593Smuzhiyun }
527*4882a593Smuzhiyun
528*4882a593Smuzhiyun retval = sctp_do_bind(sk, (union sctp_addr *)sa_addr,
529*4882a593Smuzhiyun af->sockaddr_len);
530*4882a593Smuzhiyun
531*4882a593Smuzhiyun addr_buf += af->sockaddr_len;
532*4882a593Smuzhiyun
533*4882a593Smuzhiyun err_bindx_add:
534*4882a593Smuzhiyun if (retval < 0) {
535*4882a593Smuzhiyun /* Failed. Cleanup the ones that have been added */
536*4882a593Smuzhiyun if (cnt > 0)
537*4882a593Smuzhiyun sctp_bindx_rem(sk, addrs, cnt);
538*4882a593Smuzhiyun return retval;
539*4882a593Smuzhiyun }
540*4882a593Smuzhiyun }
541*4882a593Smuzhiyun
542*4882a593Smuzhiyun return retval;
543*4882a593Smuzhiyun }
544*4882a593Smuzhiyun
545*4882a593Smuzhiyun /* Send an ASCONF chunk with Add IP address parameters to all the peers of the
546*4882a593Smuzhiyun * associations that are part of the endpoint indicating that a list of local
547*4882a593Smuzhiyun * addresses are added to the endpoint.
548*4882a593Smuzhiyun *
549*4882a593Smuzhiyun * If any of the addresses is already in the bind address list of the
550*4882a593Smuzhiyun * association, we do not send the chunk for that association. But it will not
551*4882a593Smuzhiyun * affect other associations.
552*4882a593Smuzhiyun *
553*4882a593Smuzhiyun * Only sctp_setsockopt_bindx() is supposed to call this function.
554*4882a593Smuzhiyun */
sctp_send_asconf_add_ip(struct sock * sk,struct sockaddr * addrs,int addrcnt)555*4882a593Smuzhiyun static int sctp_send_asconf_add_ip(struct sock *sk,
556*4882a593Smuzhiyun struct sockaddr *addrs,
557*4882a593Smuzhiyun int addrcnt)
558*4882a593Smuzhiyun {
559*4882a593Smuzhiyun struct sctp_sock *sp;
560*4882a593Smuzhiyun struct sctp_endpoint *ep;
561*4882a593Smuzhiyun struct sctp_association *asoc;
562*4882a593Smuzhiyun struct sctp_bind_addr *bp;
563*4882a593Smuzhiyun struct sctp_chunk *chunk;
564*4882a593Smuzhiyun struct sctp_sockaddr_entry *laddr;
565*4882a593Smuzhiyun union sctp_addr *addr;
566*4882a593Smuzhiyun union sctp_addr saveaddr;
567*4882a593Smuzhiyun void *addr_buf;
568*4882a593Smuzhiyun struct sctp_af *af;
569*4882a593Smuzhiyun struct list_head *p;
570*4882a593Smuzhiyun int i;
571*4882a593Smuzhiyun int retval = 0;
572*4882a593Smuzhiyun
573*4882a593Smuzhiyun sp = sctp_sk(sk);
574*4882a593Smuzhiyun ep = sp->ep;
575*4882a593Smuzhiyun
576*4882a593Smuzhiyun if (!ep->asconf_enable)
577*4882a593Smuzhiyun return retval;
578*4882a593Smuzhiyun
579*4882a593Smuzhiyun pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n",
580*4882a593Smuzhiyun __func__, sk, addrs, addrcnt);
581*4882a593Smuzhiyun
582*4882a593Smuzhiyun list_for_each_entry(asoc, &ep->asocs, asocs) {
583*4882a593Smuzhiyun if (!asoc->peer.asconf_capable)
584*4882a593Smuzhiyun continue;
585*4882a593Smuzhiyun
586*4882a593Smuzhiyun if (asoc->peer.addip_disabled_mask & SCTP_PARAM_ADD_IP)
587*4882a593Smuzhiyun continue;
588*4882a593Smuzhiyun
589*4882a593Smuzhiyun if (!sctp_state(asoc, ESTABLISHED))
590*4882a593Smuzhiyun continue;
591*4882a593Smuzhiyun
592*4882a593Smuzhiyun /* Check if any address in the packed array of addresses is
593*4882a593Smuzhiyun * in the bind address list of the association. If so,
594*4882a593Smuzhiyun * do not send the asconf chunk to its peer, but continue with
595*4882a593Smuzhiyun * other associations.
596*4882a593Smuzhiyun */
597*4882a593Smuzhiyun addr_buf = addrs;
598*4882a593Smuzhiyun for (i = 0; i < addrcnt; i++) {
599*4882a593Smuzhiyun addr = addr_buf;
600*4882a593Smuzhiyun af = sctp_get_af_specific(addr->v4.sin_family);
601*4882a593Smuzhiyun if (!af) {
602*4882a593Smuzhiyun retval = -EINVAL;
603*4882a593Smuzhiyun goto out;
604*4882a593Smuzhiyun }
605*4882a593Smuzhiyun
606*4882a593Smuzhiyun if (sctp_assoc_lookup_laddr(asoc, addr))
607*4882a593Smuzhiyun break;
608*4882a593Smuzhiyun
609*4882a593Smuzhiyun addr_buf += af->sockaddr_len;
610*4882a593Smuzhiyun }
611*4882a593Smuzhiyun if (i < addrcnt)
612*4882a593Smuzhiyun continue;
613*4882a593Smuzhiyun
614*4882a593Smuzhiyun /* Use the first valid address in bind addr list of
615*4882a593Smuzhiyun * association as Address Parameter of ASCONF CHUNK.
616*4882a593Smuzhiyun */
617*4882a593Smuzhiyun bp = &asoc->base.bind_addr;
618*4882a593Smuzhiyun p = bp->address_list.next;
619*4882a593Smuzhiyun laddr = list_entry(p, struct sctp_sockaddr_entry, list);
620*4882a593Smuzhiyun chunk = sctp_make_asconf_update_ip(asoc, &laddr->a, addrs,
621*4882a593Smuzhiyun addrcnt, SCTP_PARAM_ADD_IP);
622*4882a593Smuzhiyun if (!chunk) {
623*4882a593Smuzhiyun retval = -ENOMEM;
624*4882a593Smuzhiyun goto out;
625*4882a593Smuzhiyun }
626*4882a593Smuzhiyun
627*4882a593Smuzhiyun /* Add the new addresses to the bind address list with
628*4882a593Smuzhiyun * use_as_src set to 0.
629*4882a593Smuzhiyun */
630*4882a593Smuzhiyun addr_buf = addrs;
631*4882a593Smuzhiyun for (i = 0; i < addrcnt; i++) {
632*4882a593Smuzhiyun addr = addr_buf;
633*4882a593Smuzhiyun af = sctp_get_af_specific(addr->v4.sin_family);
634*4882a593Smuzhiyun memcpy(&saveaddr, addr, af->sockaddr_len);
635*4882a593Smuzhiyun retval = sctp_add_bind_addr(bp, &saveaddr,
636*4882a593Smuzhiyun sizeof(saveaddr),
637*4882a593Smuzhiyun SCTP_ADDR_NEW, GFP_ATOMIC);
638*4882a593Smuzhiyun addr_buf += af->sockaddr_len;
639*4882a593Smuzhiyun }
640*4882a593Smuzhiyun if (asoc->src_out_of_asoc_ok) {
641*4882a593Smuzhiyun struct sctp_transport *trans;
642*4882a593Smuzhiyun
643*4882a593Smuzhiyun list_for_each_entry(trans,
644*4882a593Smuzhiyun &asoc->peer.transport_addr_list, transports) {
645*4882a593Smuzhiyun trans->cwnd = min(4*asoc->pathmtu, max_t(__u32,
646*4882a593Smuzhiyun 2*asoc->pathmtu, 4380));
647*4882a593Smuzhiyun trans->ssthresh = asoc->peer.i.a_rwnd;
648*4882a593Smuzhiyun trans->rto = asoc->rto_initial;
649*4882a593Smuzhiyun sctp_max_rto(asoc, trans);
650*4882a593Smuzhiyun trans->rtt = trans->srtt = trans->rttvar = 0;
651*4882a593Smuzhiyun /* Clear the source and route cache */
652*4882a593Smuzhiyun sctp_transport_route(trans, NULL,
653*4882a593Smuzhiyun sctp_sk(asoc->base.sk));
654*4882a593Smuzhiyun }
655*4882a593Smuzhiyun }
656*4882a593Smuzhiyun retval = sctp_send_asconf(asoc, chunk);
657*4882a593Smuzhiyun }
658*4882a593Smuzhiyun
659*4882a593Smuzhiyun out:
660*4882a593Smuzhiyun return retval;
661*4882a593Smuzhiyun }
662*4882a593Smuzhiyun
663*4882a593Smuzhiyun /* Remove a list of addresses from bind addresses list. Do not remove the
664*4882a593Smuzhiyun * last address.
665*4882a593Smuzhiyun *
666*4882a593Smuzhiyun * Basically run through each address specified in the addrs/addrcnt
667*4882a593Smuzhiyun * array/length pair, determine if it is IPv6 or IPv4 and call
668*4882a593Smuzhiyun * sctp_del_bind() on it.
669*4882a593Smuzhiyun *
670*4882a593Smuzhiyun * If any of them fails, then the operation will be reversed and the
671*4882a593Smuzhiyun * ones that were removed will be added back.
672*4882a593Smuzhiyun *
673*4882a593Smuzhiyun * At least one address has to be left; if only one address is
674*4882a593Smuzhiyun * available, the operation will return -EBUSY.
675*4882a593Smuzhiyun *
676*4882a593Smuzhiyun * Only sctp_setsockopt_bindx() is supposed to call this function.
677*4882a593Smuzhiyun */
sctp_bindx_rem(struct sock * sk,struct sockaddr * addrs,int addrcnt)678*4882a593Smuzhiyun static int sctp_bindx_rem(struct sock *sk, struct sockaddr *addrs, int addrcnt)
679*4882a593Smuzhiyun {
680*4882a593Smuzhiyun struct sctp_sock *sp = sctp_sk(sk);
681*4882a593Smuzhiyun struct sctp_endpoint *ep = sp->ep;
682*4882a593Smuzhiyun int cnt;
683*4882a593Smuzhiyun struct sctp_bind_addr *bp = &ep->base.bind_addr;
684*4882a593Smuzhiyun int retval = 0;
685*4882a593Smuzhiyun void *addr_buf;
686*4882a593Smuzhiyun union sctp_addr *sa_addr;
687*4882a593Smuzhiyun struct sctp_af *af;
688*4882a593Smuzhiyun
689*4882a593Smuzhiyun pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n",
690*4882a593Smuzhiyun __func__, sk, addrs, addrcnt);
691*4882a593Smuzhiyun
692*4882a593Smuzhiyun addr_buf = addrs;
693*4882a593Smuzhiyun for (cnt = 0; cnt < addrcnt; cnt++) {
694*4882a593Smuzhiyun /* If the bind address list is empty or if there is only one
695*4882a593Smuzhiyun * bind address, there is nothing more to be removed (we need
696*4882a593Smuzhiyun * at least one address here).
697*4882a593Smuzhiyun */
698*4882a593Smuzhiyun if (list_empty(&bp->address_list) ||
699*4882a593Smuzhiyun (sctp_list_single_entry(&bp->address_list))) {
700*4882a593Smuzhiyun retval = -EBUSY;
701*4882a593Smuzhiyun goto err_bindx_rem;
702*4882a593Smuzhiyun }
703*4882a593Smuzhiyun
704*4882a593Smuzhiyun sa_addr = addr_buf;
705*4882a593Smuzhiyun af = sctp_get_af_specific(sa_addr->sa.sa_family);
706*4882a593Smuzhiyun if (!af) {
707*4882a593Smuzhiyun retval = -EINVAL;
708*4882a593Smuzhiyun goto err_bindx_rem;
709*4882a593Smuzhiyun }
710*4882a593Smuzhiyun
711*4882a593Smuzhiyun if (!af->addr_valid(sa_addr, sp, NULL)) {
712*4882a593Smuzhiyun retval = -EADDRNOTAVAIL;
713*4882a593Smuzhiyun goto err_bindx_rem;
714*4882a593Smuzhiyun }
715*4882a593Smuzhiyun
716*4882a593Smuzhiyun if (sa_addr->v4.sin_port &&
717*4882a593Smuzhiyun sa_addr->v4.sin_port != htons(bp->port)) {
718*4882a593Smuzhiyun retval = -EINVAL;
719*4882a593Smuzhiyun goto err_bindx_rem;
720*4882a593Smuzhiyun }
721*4882a593Smuzhiyun
722*4882a593Smuzhiyun if (!sa_addr->v4.sin_port)
723*4882a593Smuzhiyun sa_addr->v4.sin_port = htons(bp->port);
724*4882a593Smuzhiyun
725*4882a593Smuzhiyun /* FIXME - There is probably a need to check if sk->sk_saddr and
726*4882a593Smuzhiyun * sk->sk_rcv_addr are currently set to one of the addresses to
727*4882a593Smuzhiyun * be removed. This is something which needs to be looked into
728*4882a593Smuzhiyun * when we are fixing the outstanding issues with multi-homing
729*4882a593Smuzhiyun * socket routing and failover schemes. Refer to comments in
730*4882a593Smuzhiyun * sctp_do_bind(). -daisy
731*4882a593Smuzhiyun */
732*4882a593Smuzhiyun retval = sctp_del_bind_addr(bp, sa_addr);
733*4882a593Smuzhiyun
734*4882a593Smuzhiyun addr_buf += af->sockaddr_len;
735*4882a593Smuzhiyun err_bindx_rem:
736*4882a593Smuzhiyun if (retval < 0) {
737*4882a593Smuzhiyun /* Failed. Add the ones that has been removed back */
738*4882a593Smuzhiyun if (cnt > 0)
739*4882a593Smuzhiyun sctp_bindx_add(sk, addrs, cnt);
740*4882a593Smuzhiyun return retval;
741*4882a593Smuzhiyun }
742*4882a593Smuzhiyun }
743*4882a593Smuzhiyun
744*4882a593Smuzhiyun return retval;
745*4882a593Smuzhiyun }
746*4882a593Smuzhiyun
747*4882a593Smuzhiyun /* Send an ASCONF chunk with Delete IP address parameters to all the peers of
748*4882a593Smuzhiyun * the associations that are part of the endpoint indicating that a list of
749*4882a593Smuzhiyun * local addresses are removed from the endpoint.
750*4882a593Smuzhiyun *
751*4882a593Smuzhiyun * If any of the addresses is already in the bind address list of the
752*4882a593Smuzhiyun * association, we do not send the chunk for that association. But it will not
753*4882a593Smuzhiyun * affect other associations.
754*4882a593Smuzhiyun *
755*4882a593Smuzhiyun * Only sctp_setsockopt_bindx() is supposed to call this function.
756*4882a593Smuzhiyun */
sctp_send_asconf_del_ip(struct sock * sk,struct sockaddr * addrs,int addrcnt)757*4882a593Smuzhiyun static int sctp_send_asconf_del_ip(struct sock *sk,
758*4882a593Smuzhiyun struct sockaddr *addrs,
759*4882a593Smuzhiyun int addrcnt)
760*4882a593Smuzhiyun {
761*4882a593Smuzhiyun struct sctp_sock *sp;
762*4882a593Smuzhiyun struct sctp_endpoint *ep;
763*4882a593Smuzhiyun struct sctp_association *asoc;
764*4882a593Smuzhiyun struct sctp_transport *transport;
765*4882a593Smuzhiyun struct sctp_bind_addr *bp;
766*4882a593Smuzhiyun struct sctp_chunk *chunk;
767*4882a593Smuzhiyun union sctp_addr *laddr;
768*4882a593Smuzhiyun void *addr_buf;
769*4882a593Smuzhiyun struct sctp_af *af;
770*4882a593Smuzhiyun struct sctp_sockaddr_entry *saddr;
771*4882a593Smuzhiyun int i;
772*4882a593Smuzhiyun int retval = 0;
773*4882a593Smuzhiyun int stored = 0;
774*4882a593Smuzhiyun
775*4882a593Smuzhiyun chunk = NULL;
776*4882a593Smuzhiyun sp = sctp_sk(sk);
777*4882a593Smuzhiyun ep = sp->ep;
778*4882a593Smuzhiyun
779*4882a593Smuzhiyun if (!ep->asconf_enable)
780*4882a593Smuzhiyun return retval;
781*4882a593Smuzhiyun
782*4882a593Smuzhiyun pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n",
783*4882a593Smuzhiyun __func__, sk, addrs, addrcnt);
784*4882a593Smuzhiyun
785*4882a593Smuzhiyun list_for_each_entry(asoc, &ep->asocs, asocs) {
786*4882a593Smuzhiyun
787*4882a593Smuzhiyun if (!asoc->peer.asconf_capable)
788*4882a593Smuzhiyun continue;
789*4882a593Smuzhiyun
790*4882a593Smuzhiyun if (asoc->peer.addip_disabled_mask & SCTP_PARAM_DEL_IP)
791*4882a593Smuzhiyun continue;
792*4882a593Smuzhiyun
793*4882a593Smuzhiyun if (!sctp_state(asoc, ESTABLISHED))
794*4882a593Smuzhiyun continue;
795*4882a593Smuzhiyun
796*4882a593Smuzhiyun /* Check if any address in the packed array of addresses is
797*4882a593Smuzhiyun * not present in the bind address list of the association.
798*4882a593Smuzhiyun * If so, do not send the asconf chunk to its peer, but
799*4882a593Smuzhiyun * continue with other associations.
800*4882a593Smuzhiyun */
801*4882a593Smuzhiyun addr_buf = addrs;
802*4882a593Smuzhiyun for (i = 0; i < addrcnt; i++) {
803*4882a593Smuzhiyun laddr = addr_buf;
804*4882a593Smuzhiyun af = sctp_get_af_specific(laddr->v4.sin_family);
805*4882a593Smuzhiyun if (!af) {
806*4882a593Smuzhiyun retval = -EINVAL;
807*4882a593Smuzhiyun goto out;
808*4882a593Smuzhiyun }
809*4882a593Smuzhiyun
810*4882a593Smuzhiyun if (!sctp_assoc_lookup_laddr(asoc, laddr))
811*4882a593Smuzhiyun break;
812*4882a593Smuzhiyun
813*4882a593Smuzhiyun addr_buf += af->sockaddr_len;
814*4882a593Smuzhiyun }
815*4882a593Smuzhiyun if (i < addrcnt)
816*4882a593Smuzhiyun continue;
817*4882a593Smuzhiyun
818*4882a593Smuzhiyun /* Find one address in the association's bind address list
819*4882a593Smuzhiyun * that is not in the packed array of addresses. This is to
820*4882a593Smuzhiyun * make sure that we do not delete all the addresses in the
821*4882a593Smuzhiyun * association.
822*4882a593Smuzhiyun */
823*4882a593Smuzhiyun bp = &asoc->base.bind_addr;
824*4882a593Smuzhiyun laddr = sctp_find_unmatch_addr(bp, (union sctp_addr *)addrs,
825*4882a593Smuzhiyun addrcnt, sp);
826*4882a593Smuzhiyun if ((laddr == NULL) && (addrcnt == 1)) {
827*4882a593Smuzhiyun if (asoc->asconf_addr_del_pending)
828*4882a593Smuzhiyun continue;
829*4882a593Smuzhiyun asoc->asconf_addr_del_pending =
830*4882a593Smuzhiyun kzalloc(sizeof(union sctp_addr), GFP_ATOMIC);
831*4882a593Smuzhiyun if (asoc->asconf_addr_del_pending == NULL) {
832*4882a593Smuzhiyun retval = -ENOMEM;
833*4882a593Smuzhiyun goto out;
834*4882a593Smuzhiyun }
835*4882a593Smuzhiyun asoc->asconf_addr_del_pending->sa.sa_family =
836*4882a593Smuzhiyun addrs->sa_family;
837*4882a593Smuzhiyun asoc->asconf_addr_del_pending->v4.sin_port =
838*4882a593Smuzhiyun htons(bp->port);
839*4882a593Smuzhiyun if (addrs->sa_family == AF_INET) {
840*4882a593Smuzhiyun struct sockaddr_in *sin;
841*4882a593Smuzhiyun
842*4882a593Smuzhiyun sin = (struct sockaddr_in *)addrs;
843*4882a593Smuzhiyun asoc->asconf_addr_del_pending->v4.sin_addr.s_addr = sin->sin_addr.s_addr;
844*4882a593Smuzhiyun } else if (addrs->sa_family == AF_INET6) {
845*4882a593Smuzhiyun struct sockaddr_in6 *sin6;
846*4882a593Smuzhiyun
847*4882a593Smuzhiyun sin6 = (struct sockaddr_in6 *)addrs;
848*4882a593Smuzhiyun asoc->asconf_addr_del_pending->v6.sin6_addr = sin6->sin6_addr;
849*4882a593Smuzhiyun }
850*4882a593Smuzhiyun
851*4882a593Smuzhiyun pr_debug("%s: keep the last address asoc:%p %pISc at %p\n",
852*4882a593Smuzhiyun __func__, asoc, &asoc->asconf_addr_del_pending->sa,
853*4882a593Smuzhiyun asoc->asconf_addr_del_pending);
854*4882a593Smuzhiyun
855*4882a593Smuzhiyun asoc->src_out_of_asoc_ok = 1;
856*4882a593Smuzhiyun stored = 1;
857*4882a593Smuzhiyun goto skip_mkasconf;
858*4882a593Smuzhiyun }
859*4882a593Smuzhiyun
860*4882a593Smuzhiyun if (laddr == NULL)
861*4882a593Smuzhiyun return -EINVAL;
862*4882a593Smuzhiyun
863*4882a593Smuzhiyun /* We do not need RCU protection throughout this loop
864*4882a593Smuzhiyun * because this is done under a socket lock from the
865*4882a593Smuzhiyun * setsockopt call.
866*4882a593Smuzhiyun */
867*4882a593Smuzhiyun chunk = sctp_make_asconf_update_ip(asoc, laddr, addrs, addrcnt,
868*4882a593Smuzhiyun SCTP_PARAM_DEL_IP);
869*4882a593Smuzhiyun if (!chunk) {
870*4882a593Smuzhiyun retval = -ENOMEM;
871*4882a593Smuzhiyun goto out;
872*4882a593Smuzhiyun }
873*4882a593Smuzhiyun
874*4882a593Smuzhiyun skip_mkasconf:
875*4882a593Smuzhiyun /* Reset use_as_src flag for the addresses in the bind address
876*4882a593Smuzhiyun * list that are to be deleted.
877*4882a593Smuzhiyun */
878*4882a593Smuzhiyun addr_buf = addrs;
879*4882a593Smuzhiyun for (i = 0; i < addrcnt; i++) {
880*4882a593Smuzhiyun laddr = addr_buf;
881*4882a593Smuzhiyun af = sctp_get_af_specific(laddr->v4.sin_family);
882*4882a593Smuzhiyun list_for_each_entry(saddr, &bp->address_list, list) {
883*4882a593Smuzhiyun if (sctp_cmp_addr_exact(&saddr->a, laddr))
884*4882a593Smuzhiyun saddr->state = SCTP_ADDR_DEL;
885*4882a593Smuzhiyun }
886*4882a593Smuzhiyun addr_buf += af->sockaddr_len;
887*4882a593Smuzhiyun }
888*4882a593Smuzhiyun
889*4882a593Smuzhiyun /* Update the route and saddr entries for all the transports
890*4882a593Smuzhiyun * as some of the addresses in the bind address list are
891*4882a593Smuzhiyun * about to be deleted and cannot be used as source addresses.
892*4882a593Smuzhiyun */
893*4882a593Smuzhiyun list_for_each_entry(transport, &asoc->peer.transport_addr_list,
894*4882a593Smuzhiyun transports) {
895*4882a593Smuzhiyun sctp_transport_route(transport, NULL,
896*4882a593Smuzhiyun sctp_sk(asoc->base.sk));
897*4882a593Smuzhiyun }
898*4882a593Smuzhiyun
899*4882a593Smuzhiyun if (stored)
900*4882a593Smuzhiyun /* We don't need to transmit ASCONF */
901*4882a593Smuzhiyun continue;
902*4882a593Smuzhiyun retval = sctp_send_asconf(asoc, chunk);
903*4882a593Smuzhiyun }
904*4882a593Smuzhiyun out:
905*4882a593Smuzhiyun return retval;
906*4882a593Smuzhiyun }
907*4882a593Smuzhiyun
908*4882a593Smuzhiyun /* set addr events to assocs in the endpoint. ep and addr_wq must be locked */
sctp_asconf_mgmt(struct sctp_sock * sp,struct sctp_sockaddr_entry * addrw)909*4882a593Smuzhiyun int sctp_asconf_mgmt(struct sctp_sock *sp, struct sctp_sockaddr_entry *addrw)
910*4882a593Smuzhiyun {
911*4882a593Smuzhiyun struct sock *sk = sctp_opt2sk(sp);
912*4882a593Smuzhiyun union sctp_addr *addr;
913*4882a593Smuzhiyun struct sctp_af *af;
914*4882a593Smuzhiyun
915*4882a593Smuzhiyun /* It is safe to write port space in caller. */
916*4882a593Smuzhiyun addr = &addrw->a;
917*4882a593Smuzhiyun addr->v4.sin_port = htons(sp->ep->base.bind_addr.port);
918*4882a593Smuzhiyun af = sctp_get_af_specific(addr->sa.sa_family);
919*4882a593Smuzhiyun if (!af)
920*4882a593Smuzhiyun return -EINVAL;
921*4882a593Smuzhiyun if (sctp_verify_addr(sk, addr, af->sockaddr_len))
922*4882a593Smuzhiyun return -EINVAL;
923*4882a593Smuzhiyun
924*4882a593Smuzhiyun if (addrw->state == SCTP_ADDR_NEW)
925*4882a593Smuzhiyun return sctp_send_asconf_add_ip(sk, (struct sockaddr *)addr, 1);
926*4882a593Smuzhiyun else
927*4882a593Smuzhiyun return sctp_send_asconf_del_ip(sk, (struct sockaddr *)addr, 1);
928*4882a593Smuzhiyun }
929*4882a593Smuzhiyun
930*4882a593Smuzhiyun /* Helper for tunneling sctp_bindx() requests through sctp_setsockopt()
931*4882a593Smuzhiyun *
932*4882a593Smuzhiyun * API 8.1
933*4882a593Smuzhiyun * int sctp_bindx(int sd, struct sockaddr *addrs, int addrcnt,
934*4882a593Smuzhiyun * int flags);
935*4882a593Smuzhiyun *
936*4882a593Smuzhiyun * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
937*4882a593Smuzhiyun * If the sd is an IPv6 socket, the addresses passed can either be IPv4
938*4882a593Smuzhiyun * or IPv6 addresses.
939*4882a593Smuzhiyun *
940*4882a593Smuzhiyun * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
941*4882a593Smuzhiyun * Section 3.1.2 for this usage.
942*4882a593Smuzhiyun *
943*4882a593Smuzhiyun * addrs is a pointer to an array of one or more socket addresses. Each
944*4882a593Smuzhiyun * address is contained in its appropriate structure (i.e. struct
945*4882a593Smuzhiyun * sockaddr_in or struct sockaddr_in6) the family of the address type
946*4882a593Smuzhiyun * must be used to distinguish the address length (note that this
947*4882a593Smuzhiyun * representation is termed a "packed array" of addresses). The caller
948*4882a593Smuzhiyun * specifies the number of addresses in the array with addrcnt.
949*4882a593Smuzhiyun *
950*4882a593Smuzhiyun * On success, sctp_bindx() returns 0. On failure, sctp_bindx() returns
951*4882a593Smuzhiyun * -1, and sets errno to the appropriate error code.
952*4882a593Smuzhiyun *
953*4882a593Smuzhiyun * For SCTP, the port given in each socket address must be the same, or
954*4882a593Smuzhiyun * sctp_bindx() will fail, setting errno to EINVAL.
955*4882a593Smuzhiyun *
956*4882a593Smuzhiyun * The flags parameter is formed from the bitwise OR of zero or more of
957*4882a593Smuzhiyun * the following currently defined flags:
958*4882a593Smuzhiyun *
959*4882a593Smuzhiyun * SCTP_BINDX_ADD_ADDR
960*4882a593Smuzhiyun *
961*4882a593Smuzhiyun * SCTP_BINDX_REM_ADDR
962*4882a593Smuzhiyun *
963*4882a593Smuzhiyun * SCTP_BINDX_ADD_ADDR directs SCTP to add the given addresses to the
964*4882a593Smuzhiyun * association, and SCTP_BINDX_REM_ADDR directs SCTP to remove the given
965*4882a593Smuzhiyun * addresses from the association. The two flags are mutually exclusive;
966*4882a593Smuzhiyun * if both are given, sctp_bindx() will fail with EINVAL. A caller may
967*4882a593Smuzhiyun * not remove all addresses from an association; sctp_bindx() will
968*4882a593Smuzhiyun * reject such an attempt with EINVAL.
969*4882a593Smuzhiyun *
970*4882a593Smuzhiyun * An application can use sctp_bindx(SCTP_BINDX_ADD_ADDR) to associate
971*4882a593Smuzhiyun * additional addresses with an endpoint after calling bind(). Or use
972*4882a593Smuzhiyun * sctp_bindx(SCTP_BINDX_REM_ADDR) to remove some addresses a listening
973*4882a593Smuzhiyun * socket is associated with so that no new association accepted will be
974*4882a593Smuzhiyun * associated with those addresses. If the endpoint supports dynamic
975*4882a593Smuzhiyun * address a SCTP_BINDX_REM_ADDR or SCTP_BINDX_ADD_ADDR may cause a
976*4882a593Smuzhiyun * endpoint to send the appropriate message to the peer to change the
977*4882a593Smuzhiyun * peers address lists.
978*4882a593Smuzhiyun *
979*4882a593Smuzhiyun * Adding and removing addresses from a connected association is
980*4882a593Smuzhiyun * optional functionality. Implementations that do not support this
981*4882a593Smuzhiyun * functionality should return EOPNOTSUPP.
982*4882a593Smuzhiyun *
983*4882a593Smuzhiyun * Basically do nothing but copying the addresses from user to kernel
984*4882a593Smuzhiyun * land and invoking either sctp_bindx_add() or sctp_bindx_rem() on the sk.
985*4882a593Smuzhiyun * This is used for tunneling the sctp_bindx() request through sctp_setsockopt()
986*4882a593Smuzhiyun * from userspace.
987*4882a593Smuzhiyun *
988*4882a593Smuzhiyun * On exit there is no need to do sockfd_put(), sys_setsockopt() does
989*4882a593Smuzhiyun * it.
990*4882a593Smuzhiyun *
991*4882a593Smuzhiyun * sk The sk of the socket
992*4882a593Smuzhiyun * addrs The pointer to the addresses
993*4882a593Smuzhiyun * addrssize Size of the addrs buffer
994*4882a593Smuzhiyun * op Operation to perform (add or remove, see the flags of
995*4882a593Smuzhiyun * sctp_bindx)
996*4882a593Smuzhiyun *
997*4882a593Smuzhiyun * Returns 0 if ok, <0 errno code on error.
998*4882a593Smuzhiyun */
sctp_setsockopt_bindx(struct sock * sk,struct sockaddr * addrs,int addrs_size,int op)999*4882a593Smuzhiyun static int sctp_setsockopt_bindx(struct sock *sk, struct sockaddr *addrs,
1000*4882a593Smuzhiyun int addrs_size, int op)
1001*4882a593Smuzhiyun {
1002*4882a593Smuzhiyun int err;
1003*4882a593Smuzhiyun int addrcnt = 0;
1004*4882a593Smuzhiyun int walk_size = 0;
1005*4882a593Smuzhiyun struct sockaddr *sa_addr;
1006*4882a593Smuzhiyun void *addr_buf = addrs;
1007*4882a593Smuzhiyun struct sctp_af *af;
1008*4882a593Smuzhiyun
1009*4882a593Smuzhiyun pr_debug("%s: sk:%p addrs:%p addrs_size:%d opt:%d\n",
1010*4882a593Smuzhiyun __func__, sk, addr_buf, addrs_size, op);
1011*4882a593Smuzhiyun
1012*4882a593Smuzhiyun if (unlikely(addrs_size <= 0))
1013*4882a593Smuzhiyun return -EINVAL;
1014*4882a593Smuzhiyun
1015*4882a593Smuzhiyun /* Walk through the addrs buffer and count the number of addresses. */
1016*4882a593Smuzhiyun while (walk_size < addrs_size) {
1017*4882a593Smuzhiyun if (walk_size + sizeof(sa_family_t) > addrs_size)
1018*4882a593Smuzhiyun return -EINVAL;
1019*4882a593Smuzhiyun
1020*4882a593Smuzhiyun sa_addr = addr_buf;
1021*4882a593Smuzhiyun af = sctp_get_af_specific(sa_addr->sa_family);
1022*4882a593Smuzhiyun
1023*4882a593Smuzhiyun /* If the address family is not supported or if this address
1024*4882a593Smuzhiyun * causes the address buffer to overflow return EINVAL.
1025*4882a593Smuzhiyun */
1026*4882a593Smuzhiyun if (!af || (walk_size + af->sockaddr_len) > addrs_size)
1027*4882a593Smuzhiyun return -EINVAL;
1028*4882a593Smuzhiyun addrcnt++;
1029*4882a593Smuzhiyun addr_buf += af->sockaddr_len;
1030*4882a593Smuzhiyun walk_size += af->sockaddr_len;
1031*4882a593Smuzhiyun }
1032*4882a593Smuzhiyun
1033*4882a593Smuzhiyun /* Do the work. */
1034*4882a593Smuzhiyun switch (op) {
1035*4882a593Smuzhiyun case SCTP_BINDX_ADD_ADDR:
1036*4882a593Smuzhiyun /* Allow security module to validate bindx addresses. */
1037*4882a593Smuzhiyun err = security_sctp_bind_connect(sk, SCTP_SOCKOPT_BINDX_ADD,
1038*4882a593Smuzhiyun addrs, addrs_size);
1039*4882a593Smuzhiyun if (err)
1040*4882a593Smuzhiyun return err;
1041*4882a593Smuzhiyun err = sctp_bindx_add(sk, addrs, addrcnt);
1042*4882a593Smuzhiyun if (err)
1043*4882a593Smuzhiyun return err;
1044*4882a593Smuzhiyun return sctp_send_asconf_add_ip(sk, addrs, addrcnt);
1045*4882a593Smuzhiyun case SCTP_BINDX_REM_ADDR:
1046*4882a593Smuzhiyun err = sctp_bindx_rem(sk, addrs, addrcnt);
1047*4882a593Smuzhiyun if (err)
1048*4882a593Smuzhiyun return err;
1049*4882a593Smuzhiyun return sctp_send_asconf_del_ip(sk, addrs, addrcnt);
1050*4882a593Smuzhiyun
1051*4882a593Smuzhiyun default:
1052*4882a593Smuzhiyun return -EINVAL;
1053*4882a593Smuzhiyun }
1054*4882a593Smuzhiyun }
1055*4882a593Smuzhiyun
sctp_bind_add(struct sock * sk,struct sockaddr * addrs,int addrlen)1056*4882a593Smuzhiyun static int sctp_bind_add(struct sock *sk, struct sockaddr *addrs,
1057*4882a593Smuzhiyun int addrlen)
1058*4882a593Smuzhiyun {
1059*4882a593Smuzhiyun int err;
1060*4882a593Smuzhiyun
1061*4882a593Smuzhiyun lock_sock(sk);
1062*4882a593Smuzhiyun err = sctp_setsockopt_bindx(sk, addrs, addrlen, SCTP_BINDX_ADD_ADDR);
1063*4882a593Smuzhiyun release_sock(sk);
1064*4882a593Smuzhiyun return err;
1065*4882a593Smuzhiyun }
1066*4882a593Smuzhiyun
sctp_connect_new_asoc(struct sctp_endpoint * ep,const union sctp_addr * daddr,const struct sctp_initmsg * init,struct sctp_transport ** tp)1067*4882a593Smuzhiyun static int sctp_connect_new_asoc(struct sctp_endpoint *ep,
1068*4882a593Smuzhiyun const union sctp_addr *daddr,
1069*4882a593Smuzhiyun const struct sctp_initmsg *init,
1070*4882a593Smuzhiyun struct sctp_transport **tp)
1071*4882a593Smuzhiyun {
1072*4882a593Smuzhiyun struct sctp_association *asoc;
1073*4882a593Smuzhiyun struct sock *sk = ep->base.sk;
1074*4882a593Smuzhiyun struct net *net = sock_net(sk);
1075*4882a593Smuzhiyun enum sctp_scope scope;
1076*4882a593Smuzhiyun int err;
1077*4882a593Smuzhiyun
1078*4882a593Smuzhiyun if (sctp_endpoint_is_peeled_off(ep, daddr))
1079*4882a593Smuzhiyun return -EADDRNOTAVAIL;
1080*4882a593Smuzhiyun
1081*4882a593Smuzhiyun if (!ep->base.bind_addr.port) {
1082*4882a593Smuzhiyun if (sctp_autobind(sk))
1083*4882a593Smuzhiyun return -EAGAIN;
1084*4882a593Smuzhiyun } else {
1085*4882a593Smuzhiyun if (inet_is_local_unbindable_port(net, ep->base.bind_addr.port))
1086*4882a593Smuzhiyun return -EPERM;
1087*4882a593Smuzhiyun if (inet_port_requires_bind_service(net, ep->base.bind_addr.port) &&
1088*4882a593Smuzhiyun !ns_capable(net->user_ns, CAP_NET_BIND_SERVICE))
1089*4882a593Smuzhiyun return -EACCES;
1090*4882a593Smuzhiyun }
1091*4882a593Smuzhiyun
1092*4882a593Smuzhiyun scope = sctp_scope(daddr);
1093*4882a593Smuzhiyun asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
1094*4882a593Smuzhiyun if (!asoc)
1095*4882a593Smuzhiyun return -ENOMEM;
1096*4882a593Smuzhiyun
1097*4882a593Smuzhiyun err = sctp_assoc_set_bind_addr_from_ep(asoc, scope, GFP_KERNEL);
1098*4882a593Smuzhiyun if (err < 0)
1099*4882a593Smuzhiyun goto free;
1100*4882a593Smuzhiyun
1101*4882a593Smuzhiyun *tp = sctp_assoc_add_peer(asoc, daddr, GFP_KERNEL, SCTP_UNKNOWN);
1102*4882a593Smuzhiyun if (!*tp) {
1103*4882a593Smuzhiyun err = -ENOMEM;
1104*4882a593Smuzhiyun goto free;
1105*4882a593Smuzhiyun }
1106*4882a593Smuzhiyun
1107*4882a593Smuzhiyun if (!init)
1108*4882a593Smuzhiyun return 0;
1109*4882a593Smuzhiyun
1110*4882a593Smuzhiyun if (init->sinit_num_ostreams) {
1111*4882a593Smuzhiyun __u16 outcnt = init->sinit_num_ostreams;
1112*4882a593Smuzhiyun
1113*4882a593Smuzhiyun asoc->c.sinit_num_ostreams = outcnt;
1114*4882a593Smuzhiyun /* outcnt has been changed, need to re-init stream */
1115*4882a593Smuzhiyun err = sctp_stream_init(&asoc->stream, outcnt, 0, GFP_KERNEL);
1116*4882a593Smuzhiyun if (err)
1117*4882a593Smuzhiyun goto free;
1118*4882a593Smuzhiyun }
1119*4882a593Smuzhiyun
1120*4882a593Smuzhiyun if (init->sinit_max_instreams)
1121*4882a593Smuzhiyun asoc->c.sinit_max_instreams = init->sinit_max_instreams;
1122*4882a593Smuzhiyun
1123*4882a593Smuzhiyun if (init->sinit_max_attempts)
1124*4882a593Smuzhiyun asoc->max_init_attempts = init->sinit_max_attempts;
1125*4882a593Smuzhiyun
1126*4882a593Smuzhiyun if (init->sinit_max_init_timeo)
1127*4882a593Smuzhiyun asoc->max_init_timeo =
1128*4882a593Smuzhiyun msecs_to_jiffies(init->sinit_max_init_timeo);
1129*4882a593Smuzhiyun
1130*4882a593Smuzhiyun return 0;
1131*4882a593Smuzhiyun free:
1132*4882a593Smuzhiyun sctp_association_free(asoc);
1133*4882a593Smuzhiyun return err;
1134*4882a593Smuzhiyun }
1135*4882a593Smuzhiyun
sctp_connect_add_peer(struct sctp_association * asoc,union sctp_addr * daddr,int addr_len)1136*4882a593Smuzhiyun static int sctp_connect_add_peer(struct sctp_association *asoc,
1137*4882a593Smuzhiyun union sctp_addr *daddr, int addr_len)
1138*4882a593Smuzhiyun {
1139*4882a593Smuzhiyun struct sctp_endpoint *ep = asoc->ep;
1140*4882a593Smuzhiyun struct sctp_association *old;
1141*4882a593Smuzhiyun struct sctp_transport *t;
1142*4882a593Smuzhiyun int err;
1143*4882a593Smuzhiyun
1144*4882a593Smuzhiyun err = sctp_verify_addr(ep->base.sk, daddr, addr_len);
1145*4882a593Smuzhiyun if (err)
1146*4882a593Smuzhiyun return err;
1147*4882a593Smuzhiyun
1148*4882a593Smuzhiyun old = sctp_endpoint_lookup_assoc(ep, daddr, &t);
1149*4882a593Smuzhiyun if (old && old != asoc)
1150*4882a593Smuzhiyun return old->state >= SCTP_STATE_ESTABLISHED ? -EISCONN
1151*4882a593Smuzhiyun : -EALREADY;
1152*4882a593Smuzhiyun
1153*4882a593Smuzhiyun if (sctp_endpoint_is_peeled_off(ep, daddr))
1154*4882a593Smuzhiyun return -EADDRNOTAVAIL;
1155*4882a593Smuzhiyun
1156*4882a593Smuzhiyun t = sctp_assoc_add_peer(asoc, daddr, GFP_KERNEL, SCTP_UNKNOWN);
1157*4882a593Smuzhiyun if (!t)
1158*4882a593Smuzhiyun return -ENOMEM;
1159*4882a593Smuzhiyun
1160*4882a593Smuzhiyun return 0;
1161*4882a593Smuzhiyun }
1162*4882a593Smuzhiyun
1163*4882a593Smuzhiyun /* __sctp_connect(struct sock* sk, struct sockaddr *kaddrs, int addrs_size)
1164*4882a593Smuzhiyun *
1165*4882a593Smuzhiyun * Common routine for handling connect() and sctp_connectx().
1166*4882a593Smuzhiyun * Connect will come in with just a single address.
1167*4882a593Smuzhiyun */
__sctp_connect(struct sock * sk,struct sockaddr * kaddrs,int addrs_size,int flags,sctp_assoc_t * assoc_id)1168*4882a593Smuzhiyun static int __sctp_connect(struct sock *sk, struct sockaddr *kaddrs,
1169*4882a593Smuzhiyun int addrs_size, int flags, sctp_assoc_t *assoc_id)
1170*4882a593Smuzhiyun {
1171*4882a593Smuzhiyun struct sctp_sock *sp = sctp_sk(sk);
1172*4882a593Smuzhiyun struct sctp_endpoint *ep = sp->ep;
1173*4882a593Smuzhiyun struct sctp_transport *transport;
1174*4882a593Smuzhiyun struct sctp_association *asoc;
1175*4882a593Smuzhiyun void *addr_buf = kaddrs;
1176*4882a593Smuzhiyun union sctp_addr *daddr;
1177*4882a593Smuzhiyun struct sctp_af *af;
1178*4882a593Smuzhiyun int walk_size, err;
1179*4882a593Smuzhiyun long timeo;
1180*4882a593Smuzhiyun
1181*4882a593Smuzhiyun if (sctp_sstate(sk, ESTABLISHED) || sctp_sstate(sk, CLOSING) ||
1182*4882a593Smuzhiyun (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING)))
1183*4882a593Smuzhiyun return -EISCONN;
1184*4882a593Smuzhiyun
1185*4882a593Smuzhiyun daddr = addr_buf;
1186*4882a593Smuzhiyun af = sctp_get_af_specific(daddr->sa.sa_family);
1187*4882a593Smuzhiyun if (!af || af->sockaddr_len > addrs_size)
1188*4882a593Smuzhiyun return -EINVAL;
1189*4882a593Smuzhiyun
1190*4882a593Smuzhiyun err = sctp_verify_addr(sk, daddr, af->sockaddr_len);
1191*4882a593Smuzhiyun if (err)
1192*4882a593Smuzhiyun return err;
1193*4882a593Smuzhiyun
1194*4882a593Smuzhiyun asoc = sctp_endpoint_lookup_assoc(ep, daddr, &transport);
1195*4882a593Smuzhiyun if (asoc)
1196*4882a593Smuzhiyun return asoc->state >= SCTP_STATE_ESTABLISHED ? -EISCONN
1197*4882a593Smuzhiyun : -EALREADY;
1198*4882a593Smuzhiyun
1199*4882a593Smuzhiyun err = sctp_connect_new_asoc(ep, daddr, NULL, &transport);
1200*4882a593Smuzhiyun if (err)
1201*4882a593Smuzhiyun return err;
1202*4882a593Smuzhiyun asoc = transport->asoc;
1203*4882a593Smuzhiyun
1204*4882a593Smuzhiyun addr_buf += af->sockaddr_len;
1205*4882a593Smuzhiyun walk_size = af->sockaddr_len;
1206*4882a593Smuzhiyun while (walk_size < addrs_size) {
1207*4882a593Smuzhiyun err = -EINVAL;
1208*4882a593Smuzhiyun if (walk_size + sizeof(sa_family_t) > addrs_size)
1209*4882a593Smuzhiyun goto out_free;
1210*4882a593Smuzhiyun
1211*4882a593Smuzhiyun daddr = addr_buf;
1212*4882a593Smuzhiyun af = sctp_get_af_specific(daddr->sa.sa_family);
1213*4882a593Smuzhiyun if (!af || af->sockaddr_len + walk_size > addrs_size)
1214*4882a593Smuzhiyun goto out_free;
1215*4882a593Smuzhiyun
1216*4882a593Smuzhiyun if (asoc->peer.port != ntohs(daddr->v4.sin_port))
1217*4882a593Smuzhiyun goto out_free;
1218*4882a593Smuzhiyun
1219*4882a593Smuzhiyun err = sctp_connect_add_peer(asoc, daddr, af->sockaddr_len);
1220*4882a593Smuzhiyun if (err)
1221*4882a593Smuzhiyun goto out_free;
1222*4882a593Smuzhiyun
1223*4882a593Smuzhiyun addr_buf += af->sockaddr_len;
1224*4882a593Smuzhiyun walk_size += af->sockaddr_len;
1225*4882a593Smuzhiyun }
1226*4882a593Smuzhiyun
1227*4882a593Smuzhiyun /* In case the user of sctp_connectx() wants an association
1228*4882a593Smuzhiyun * id back, assign one now.
1229*4882a593Smuzhiyun */
1230*4882a593Smuzhiyun if (assoc_id) {
1231*4882a593Smuzhiyun err = sctp_assoc_set_id(asoc, GFP_KERNEL);
1232*4882a593Smuzhiyun if (err < 0)
1233*4882a593Smuzhiyun goto out_free;
1234*4882a593Smuzhiyun }
1235*4882a593Smuzhiyun
1236*4882a593Smuzhiyun err = sctp_primitive_ASSOCIATE(sock_net(sk), asoc, NULL);
1237*4882a593Smuzhiyun if (err < 0)
1238*4882a593Smuzhiyun goto out_free;
1239*4882a593Smuzhiyun
1240*4882a593Smuzhiyun /* Initialize sk's dport and daddr for getpeername() */
1241*4882a593Smuzhiyun inet_sk(sk)->inet_dport = htons(asoc->peer.port);
1242*4882a593Smuzhiyun sp->pf->to_sk_daddr(daddr, sk);
1243*4882a593Smuzhiyun sk->sk_err = 0;
1244*4882a593Smuzhiyun
1245*4882a593Smuzhiyun if (assoc_id)
1246*4882a593Smuzhiyun *assoc_id = asoc->assoc_id;
1247*4882a593Smuzhiyun
1248*4882a593Smuzhiyun timeo = sock_sndtimeo(sk, flags & O_NONBLOCK);
1249*4882a593Smuzhiyun return sctp_wait_for_connect(asoc, &timeo);
1250*4882a593Smuzhiyun
1251*4882a593Smuzhiyun out_free:
1252*4882a593Smuzhiyun pr_debug("%s: took out_free path with asoc:%p kaddrs:%p err:%d\n",
1253*4882a593Smuzhiyun __func__, asoc, kaddrs, err);
1254*4882a593Smuzhiyun sctp_association_free(asoc);
1255*4882a593Smuzhiyun return err;
1256*4882a593Smuzhiyun }
1257*4882a593Smuzhiyun
1258*4882a593Smuzhiyun /* Helper for tunneling sctp_connectx() requests through sctp_setsockopt()
1259*4882a593Smuzhiyun *
1260*4882a593Smuzhiyun * API 8.9
1261*4882a593Smuzhiyun * int sctp_connectx(int sd, struct sockaddr *addrs, int addrcnt,
1262*4882a593Smuzhiyun * sctp_assoc_t *asoc);
1263*4882a593Smuzhiyun *
1264*4882a593Smuzhiyun * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
1265*4882a593Smuzhiyun * If the sd is an IPv6 socket, the addresses passed can either be IPv4
1266*4882a593Smuzhiyun * or IPv6 addresses.
1267*4882a593Smuzhiyun *
1268*4882a593Smuzhiyun * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
1269*4882a593Smuzhiyun * Section 3.1.2 for this usage.
1270*4882a593Smuzhiyun *
1271*4882a593Smuzhiyun * addrs is a pointer to an array of one or more socket addresses. Each
1272*4882a593Smuzhiyun * address is contained in its appropriate structure (i.e. struct
1273*4882a593Smuzhiyun * sockaddr_in or struct sockaddr_in6) the family of the address type
1274*4882a593Smuzhiyun * must be used to distengish the address length (note that this
1275*4882a593Smuzhiyun * representation is termed a "packed array" of addresses). The caller
1276*4882a593Smuzhiyun * specifies the number of addresses in the array with addrcnt.
1277*4882a593Smuzhiyun *
1278*4882a593Smuzhiyun * On success, sctp_connectx() returns 0. It also sets the assoc_id to
1279*4882a593Smuzhiyun * the association id of the new association. On failure, sctp_connectx()
1280*4882a593Smuzhiyun * returns -1, and sets errno to the appropriate error code. The assoc_id
1281*4882a593Smuzhiyun * is not touched by the kernel.
1282*4882a593Smuzhiyun *
1283*4882a593Smuzhiyun * For SCTP, the port given in each socket address must be the same, or
1284*4882a593Smuzhiyun * sctp_connectx() will fail, setting errno to EINVAL.
1285*4882a593Smuzhiyun *
1286*4882a593Smuzhiyun * An application can use sctp_connectx to initiate an association with
1287*4882a593Smuzhiyun * an endpoint that is multi-homed. Much like sctp_bindx() this call
1288*4882a593Smuzhiyun * allows a caller to specify multiple addresses at which a peer can be
1289*4882a593Smuzhiyun * reached. The way the SCTP stack uses the list of addresses to set up
1290*4882a593Smuzhiyun * the association is implementation dependent. This function only
1291*4882a593Smuzhiyun * specifies that the stack will try to make use of all the addresses in
1292*4882a593Smuzhiyun * the list when needed.
1293*4882a593Smuzhiyun *
1294*4882a593Smuzhiyun * Note that the list of addresses passed in is only used for setting up
1295*4882a593Smuzhiyun * the association. It does not necessarily equal the set of addresses
1296*4882a593Smuzhiyun * the peer uses for the resulting association. If the caller wants to
1297*4882a593Smuzhiyun * find out the set of peer addresses, it must use sctp_getpaddrs() to
1298*4882a593Smuzhiyun * retrieve them after the association has been set up.
1299*4882a593Smuzhiyun *
1300*4882a593Smuzhiyun * Basically do nothing but copying the addresses from user to kernel
1301*4882a593Smuzhiyun * land and invoking either sctp_connectx(). This is used for tunneling
1302*4882a593Smuzhiyun * the sctp_connectx() request through sctp_setsockopt() from userspace.
1303*4882a593Smuzhiyun *
1304*4882a593Smuzhiyun * On exit there is no need to do sockfd_put(), sys_setsockopt() does
1305*4882a593Smuzhiyun * it.
1306*4882a593Smuzhiyun *
1307*4882a593Smuzhiyun * sk The sk of the socket
1308*4882a593Smuzhiyun * addrs The pointer to the addresses
1309*4882a593Smuzhiyun * addrssize Size of the addrs buffer
1310*4882a593Smuzhiyun *
1311*4882a593Smuzhiyun * Returns >=0 if ok, <0 errno code on error.
1312*4882a593Smuzhiyun */
__sctp_setsockopt_connectx(struct sock * sk,struct sockaddr * kaddrs,int addrs_size,sctp_assoc_t * assoc_id)1313*4882a593Smuzhiyun static int __sctp_setsockopt_connectx(struct sock *sk, struct sockaddr *kaddrs,
1314*4882a593Smuzhiyun int addrs_size, sctp_assoc_t *assoc_id)
1315*4882a593Smuzhiyun {
1316*4882a593Smuzhiyun int err = 0, flags = 0;
1317*4882a593Smuzhiyun
1318*4882a593Smuzhiyun pr_debug("%s: sk:%p addrs:%p addrs_size:%d\n",
1319*4882a593Smuzhiyun __func__, sk, kaddrs, addrs_size);
1320*4882a593Smuzhiyun
1321*4882a593Smuzhiyun /* make sure the 1st addr's sa_family is accessible later */
1322*4882a593Smuzhiyun if (unlikely(addrs_size < sizeof(sa_family_t)))
1323*4882a593Smuzhiyun return -EINVAL;
1324*4882a593Smuzhiyun
1325*4882a593Smuzhiyun /* Allow security module to validate connectx addresses. */
1326*4882a593Smuzhiyun err = security_sctp_bind_connect(sk, SCTP_SOCKOPT_CONNECTX,
1327*4882a593Smuzhiyun (struct sockaddr *)kaddrs,
1328*4882a593Smuzhiyun addrs_size);
1329*4882a593Smuzhiyun if (err)
1330*4882a593Smuzhiyun return err;
1331*4882a593Smuzhiyun
1332*4882a593Smuzhiyun /* in-kernel sockets don't generally have a file allocated to them
1333*4882a593Smuzhiyun * if all they do is call sock_create_kern().
1334*4882a593Smuzhiyun */
1335*4882a593Smuzhiyun if (sk->sk_socket->file)
1336*4882a593Smuzhiyun flags = sk->sk_socket->file->f_flags;
1337*4882a593Smuzhiyun
1338*4882a593Smuzhiyun return __sctp_connect(sk, kaddrs, addrs_size, flags, assoc_id);
1339*4882a593Smuzhiyun }
1340*4882a593Smuzhiyun
1341*4882a593Smuzhiyun /*
1342*4882a593Smuzhiyun * This is an older interface. It's kept for backward compatibility
1343*4882a593Smuzhiyun * to the option that doesn't provide association id.
1344*4882a593Smuzhiyun */
sctp_setsockopt_connectx_old(struct sock * sk,struct sockaddr * kaddrs,int addrs_size)1345*4882a593Smuzhiyun static int sctp_setsockopt_connectx_old(struct sock *sk,
1346*4882a593Smuzhiyun struct sockaddr *kaddrs,
1347*4882a593Smuzhiyun int addrs_size)
1348*4882a593Smuzhiyun {
1349*4882a593Smuzhiyun return __sctp_setsockopt_connectx(sk, kaddrs, addrs_size, NULL);
1350*4882a593Smuzhiyun }
1351*4882a593Smuzhiyun
1352*4882a593Smuzhiyun /*
1353*4882a593Smuzhiyun * New interface for the API. The since the API is done with a socket
1354*4882a593Smuzhiyun * option, to make it simple we feed back the association id is as a return
1355*4882a593Smuzhiyun * indication to the call. Error is always negative and association id is
1356*4882a593Smuzhiyun * always positive.
1357*4882a593Smuzhiyun */
sctp_setsockopt_connectx(struct sock * sk,struct sockaddr * kaddrs,int addrs_size)1358*4882a593Smuzhiyun static int sctp_setsockopt_connectx(struct sock *sk,
1359*4882a593Smuzhiyun struct sockaddr *kaddrs,
1360*4882a593Smuzhiyun int addrs_size)
1361*4882a593Smuzhiyun {
1362*4882a593Smuzhiyun sctp_assoc_t assoc_id = 0;
1363*4882a593Smuzhiyun int err = 0;
1364*4882a593Smuzhiyun
1365*4882a593Smuzhiyun err = __sctp_setsockopt_connectx(sk, kaddrs, addrs_size, &assoc_id);
1366*4882a593Smuzhiyun
1367*4882a593Smuzhiyun if (err)
1368*4882a593Smuzhiyun return err;
1369*4882a593Smuzhiyun else
1370*4882a593Smuzhiyun return assoc_id;
1371*4882a593Smuzhiyun }
1372*4882a593Smuzhiyun
1373*4882a593Smuzhiyun /*
1374*4882a593Smuzhiyun * New (hopefully final) interface for the API.
1375*4882a593Smuzhiyun * We use the sctp_getaddrs_old structure so that use-space library
1376*4882a593Smuzhiyun * can avoid any unnecessary allocations. The only different part
1377*4882a593Smuzhiyun * is that we store the actual length of the address buffer into the
1378*4882a593Smuzhiyun * addrs_num structure member. That way we can re-use the existing
1379*4882a593Smuzhiyun * code.
1380*4882a593Smuzhiyun */
1381*4882a593Smuzhiyun #ifdef CONFIG_COMPAT
1382*4882a593Smuzhiyun struct compat_sctp_getaddrs_old {
1383*4882a593Smuzhiyun sctp_assoc_t assoc_id;
1384*4882a593Smuzhiyun s32 addr_num;
1385*4882a593Smuzhiyun compat_uptr_t addrs; /* struct sockaddr * */
1386*4882a593Smuzhiyun };
1387*4882a593Smuzhiyun #endif
1388*4882a593Smuzhiyun
sctp_getsockopt_connectx3(struct sock * sk,int len,char __user * optval,int __user * optlen)1389*4882a593Smuzhiyun static int sctp_getsockopt_connectx3(struct sock *sk, int len,
1390*4882a593Smuzhiyun char __user *optval,
1391*4882a593Smuzhiyun int __user *optlen)
1392*4882a593Smuzhiyun {
1393*4882a593Smuzhiyun struct sctp_getaddrs_old param;
1394*4882a593Smuzhiyun sctp_assoc_t assoc_id = 0;
1395*4882a593Smuzhiyun struct sockaddr *kaddrs;
1396*4882a593Smuzhiyun int err = 0;
1397*4882a593Smuzhiyun
1398*4882a593Smuzhiyun #ifdef CONFIG_COMPAT
1399*4882a593Smuzhiyun if (in_compat_syscall()) {
1400*4882a593Smuzhiyun struct compat_sctp_getaddrs_old param32;
1401*4882a593Smuzhiyun
1402*4882a593Smuzhiyun if (len < sizeof(param32))
1403*4882a593Smuzhiyun return -EINVAL;
1404*4882a593Smuzhiyun if (copy_from_user(¶m32, optval, sizeof(param32)))
1405*4882a593Smuzhiyun return -EFAULT;
1406*4882a593Smuzhiyun
1407*4882a593Smuzhiyun param.assoc_id = param32.assoc_id;
1408*4882a593Smuzhiyun param.addr_num = param32.addr_num;
1409*4882a593Smuzhiyun param.addrs = compat_ptr(param32.addrs);
1410*4882a593Smuzhiyun } else
1411*4882a593Smuzhiyun #endif
1412*4882a593Smuzhiyun {
1413*4882a593Smuzhiyun if (len < sizeof(param))
1414*4882a593Smuzhiyun return -EINVAL;
1415*4882a593Smuzhiyun if (copy_from_user(¶m, optval, sizeof(param)))
1416*4882a593Smuzhiyun return -EFAULT;
1417*4882a593Smuzhiyun }
1418*4882a593Smuzhiyun
1419*4882a593Smuzhiyun kaddrs = memdup_user(param.addrs, param.addr_num);
1420*4882a593Smuzhiyun if (IS_ERR(kaddrs))
1421*4882a593Smuzhiyun return PTR_ERR(kaddrs);
1422*4882a593Smuzhiyun
1423*4882a593Smuzhiyun err = __sctp_setsockopt_connectx(sk, kaddrs, param.addr_num, &assoc_id);
1424*4882a593Smuzhiyun kfree(kaddrs);
1425*4882a593Smuzhiyun if (err == 0 || err == -EINPROGRESS) {
1426*4882a593Smuzhiyun if (copy_to_user(optval, &assoc_id, sizeof(assoc_id)))
1427*4882a593Smuzhiyun return -EFAULT;
1428*4882a593Smuzhiyun if (put_user(sizeof(assoc_id), optlen))
1429*4882a593Smuzhiyun return -EFAULT;
1430*4882a593Smuzhiyun }
1431*4882a593Smuzhiyun
1432*4882a593Smuzhiyun return err;
1433*4882a593Smuzhiyun }
1434*4882a593Smuzhiyun
1435*4882a593Smuzhiyun /* API 3.1.4 close() - UDP Style Syntax
1436*4882a593Smuzhiyun * Applications use close() to perform graceful shutdown (as described in
1437*4882a593Smuzhiyun * Section 10.1 of [SCTP]) on ALL the associations currently represented
1438*4882a593Smuzhiyun * by a UDP-style socket.
1439*4882a593Smuzhiyun *
1440*4882a593Smuzhiyun * The syntax is
1441*4882a593Smuzhiyun *
1442*4882a593Smuzhiyun * ret = close(int sd);
1443*4882a593Smuzhiyun *
1444*4882a593Smuzhiyun * sd - the socket descriptor of the associations to be closed.
1445*4882a593Smuzhiyun *
1446*4882a593Smuzhiyun * To gracefully shutdown a specific association represented by the
1447*4882a593Smuzhiyun * UDP-style socket, an application should use the sendmsg() call,
1448*4882a593Smuzhiyun * passing no user data, but including the appropriate flag in the
1449*4882a593Smuzhiyun * ancillary data (see Section xxxx).
1450*4882a593Smuzhiyun *
1451*4882a593Smuzhiyun * If sd in the close() call is a branched-off socket representing only
1452*4882a593Smuzhiyun * one association, the shutdown is performed on that association only.
1453*4882a593Smuzhiyun *
1454*4882a593Smuzhiyun * 4.1.6 close() - TCP Style Syntax
1455*4882a593Smuzhiyun *
1456*4882a593Smuzhiyun * Applications use close() to gracefully close down an association.
1457*4882a593Smuzhiyun *
1458*4882a593Smuzhiyun * The syntax is:
1459*4882a593Smuzhiyun *
1460*4882a593Smuzhiyun * int close(int sd);
1461*4882a593Smuzhiyun *
1462*4882a593Smuzhiyun * sd - the socket descriptor of the association to be closed.
1463*4882a593Smuzhiyun *
1464*4882a593Smuzhiyun * After an application calls close() on a socket descriptor, no further
1465*4882a593Smuzhiyun * socket operations will succeed on that descriptor.
1466*4882a593Smuzhiyun *
1467*4882a593Smuzhiyun * API 7.1.4 SO_LINGER
1468*4882a593Smuzhiyun *
1469*4882a593Smuzhiyun * An application using the TCP-style socket can use this option to
1470*4882a593Smuzhiyun * perform the SCTP ABORT primitive. The linger option structure is:
1471*4882a593Smuzhiyun *
1472*4882a593Smuzhiyun * struct linger {
1473*4882a593Smuzhiyun * int l_onoff; // option on/off
1474*4882a593Smuzhiyun * int l_linger; // linger time
1475*4882a593Smuzhiyun * };
1476*4882a593Smuzhiyun *
1477*4882a593Smuzhiyun * To enable the option, set l_onoff to 1. If the l_linger value is set
1478*4882a593Smuzhiyun * to 0, calling close() is the same as the ABORT primitive. If the
1479*4882a593Smuzhiyun * value is set to a negative value, the setsockopt() call will return
1480*4882a593Smuzhiyun * an error. If the value is set to a positive value linger_time, the
1481*4882a593Smuzhiyun * close() can be blocked for at most linger_time ms. If the graceful
1482*4882a593Smuzhiyun * shutdown phase does not finish during this period, close() will
1483*4882a593Smuzhiyun * return but the graceful shutdown phase continues in the system.
1484*4882a593Smuzhiyun */
sctp_close(struct sock * sk,long timeout)1485*4882a593Smuzhiyun static void sctp_close(struct sock *sk, long timeout)
1486*4882a593Smuzhiyun {
1487*4882a593Smuzhiyun struct net *net = sock_net(sk);
1488*4882a593Smuzhiyun struct sctp_endpoint *ep;
1489*4882a593Smuzhiyun struct sctp_association *asoc;
1490*4882a593Smuzhiyun struct list_head *pos, *temp;
1491*4882a593Smuzhiyun unsigned int data_was_unread;
1492*4882a593Smuzhiyun
1493*4882a593Smuzhiyun pr_debug("%s: sk:%p, timeout:%ld\n", __func__, sk, timeout);
1494*4882a593Smuzhiyun
1495*4882a593Smuzhiyun lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
1496*4882a593Smuzhiyun sk->sk_shutdown = SHUTDOWN_MASK;
1497*4882a593Smuzhiyun inet_sk_set_state(sk, SCTP_SS_CLOSING);
1498*4882a593Smuzhiyun
1499*4882a593Smuzhiyun ep = sctp_sk(sk)->ep;
1500*4882a593Smuzhiyun
1501*4882a593Smuzhiyun /* Clean up any skbs sitting on the receive queue. */
1502*4882a593Smuzhiyun data_was_unread = sctp_queue_purge_ulpevents(&sk->sk_receive_queue);
1503*4882a593Smuzhiyun data_was_unread += sctp_queue_purge_ulpevents(&sctp_sk(sk)->pd_lobby);
1504*4882a593Smuzhiyun
1505*4882a593Smuzhiyun /* Walk all associations on an endpoint. */
1506*4882a593Smuzhiyun list_for_each_safe(pos, temp, &ep->asocs) {
1507*4882a593Smuzhiyun asoc = list_entry(pos, struct sctp_association, asocs);
1508*4882a593Smuzhiyun
1509*4882a593Smuzhiyun if (sctp_style(sk, TCP)) {
1510*4882a593Smuzhiyun /* A closed association can still be in the list if
1511*4882a593Smuzhiyun * it belongs to a TCP-style listening socket that is
1512*4882a593Smuzhiyun * not yet accepted. If so, free it. If not, send an
1513*4882a593Smuzhiyun * ABORT or SHUTDOWN based on the linger options.
1514*4882a593Smuzhiyun */
1515*4882a593Smuzhiyun if (sctp_state(asoc, CLOSED)) {
1516*4882a593Smuzhiyun sctp_association_free(asoc);
1517*4882a593Smuzhiyun continue;
1518*4882a593Smuzhiyun }
1519*4882a593Smuzhiyun }
1520*4882a593Smuzhiyun
1521*4882a593Smuzhiyun if (data_was_unread || !skb_queue_empty(&asoc->ulpq.lobby) ||
1522*4882a593Smuzhiyun !skb_queue_empty(&asoc->ulpq.reasm) ||
1523*4882a593Smuzhiyun !skb_queue_empty(&asoc->ulpq.reasm_uo) ||
1524*4882a593Smuzhiyun (sock_flag(sk, SOCK_LINGER) && !sk->sk_lingertime)) {
1525*4882a593Smuzhiyun struct sctp_chunk *chunk;
1526*4882a593Smuzhiyun
1527*4882a593Smuzhiyun chunk = sctp_make_abort_user(asoc, NULL, 0);
1528*4882a593Smuzhiyun sctp_primitive_ABORT(net, asoc, chunk);
1529*4882a593Smuzhiyun } else
1530*4882a593Smuzhiyun sctp_primitive_SHUTDOWN(net, asoc, NULL);
1531*4882a593Smuzhiyun }
1532*4882a593Smuzhiyun
1533*4882a593Smuzhiyun /* On a TCP-style socket, block for at most linger_time if set. */
1534*4882a593Smuzhiyun if (sctp_style(sk, TCP) && timeout)
1535*4882a593Smuzhiyun sctp_wait_for_close(sk, timeout);
1536*4882a593Smuzhiyun
1537*4882a593Smuzhiyun /* This will run the backlog queue. */
1538*4882a593Smuzhiyun release_sock(sk);
1539*4882a593Smuzhiyun
1540*4882a593Smuzhiyun /* Supposedly, no process has access to the socket, but
1541*4882a593Smuzhiyun * the net layers still may.
1542*4882a593Smuzhiyun * Also, sctp_destroy_sock() needs to be called with addr_wq_lock
1543*4882a593Smuzhiyun * held and that should be grabbed before socket lock.
1544*4882a593Smuzhiyun */
1545*4882a593Smuzhiyun spin_lock_bh(&net->sctp.addr_wq_lock);
1546*4882a593Smuzhiyun bh_lock_sock_nested(sk);
1547*4882a593Smuzhiyun
1548*4882a593Smuzhiyun /* Hold the sock, since sk_common_release() will put sock_put()
1549*4882a593Smuzhiyun * and we have just a little more cleanup.
1550*4882a593Smuzhiyun */
1551*4882a593Smuzhiyun sock_hold(sk);
1552*4882a593Smuzhiyun sk_common_release(sk);
1553*4882a593Smuzhiyun
1554*4882a593Smuzhiyun bh_unlock_sock(sk);
1555*4882a593Smuzhiyun spin_unlock_bh(&net->sctp.addr_wq_lock);
1556*4882a593Smuzhiyun
1557*4882a593Smuzhiyun sock_put(sk);
1558*4882a593Smuzhiyun
1559*4882a593Smuzhiyun SCTP_DBG_OBJCNT_DEC(sock);
1560*4882a593Smuzhiyun }
1561*4882a593Smuzhiyun
1562*4882a593Smuzhiyun /* Handle EPIPE error. */
sctp_error(struct sock * sk,int flags,int err)1563*4882a593Smuzhiyun static int sctp_error(struct sock *sk, int flags, int err)
1564*4882a593Smuzhiyun {
1565*4882a593Smuzhiyun if (err == -EPIPE)
1566*4882a593Smuzhiyun err = sock_error(sk) ? : -EPIPE;
1567*4882a593Smuzhiyun if (err == -EPIPE && !(flags & MSG_NOSIGNAL))
1568*4882a593Smuzhiyun send_sig(SIGPIPE, current, 0);
1569*4882a593Smuzhiyun return err;
1570*4882a593Smuzhiyun }
1571*4882a593Smuzhiyun
1572*4882a593Smuzhiyun /* API 3.1.3 sendmsg() - UDP Style Syntax
1573*4882a593Smuzhiyun *
1574*4882a593Smuzhiyun * An application uses sendmsg() and recvmsg() calls to transmit data to
1575*4882a593Smuzhiyun * and receive data from its peer.
1576*4882a593Smuzhiyun *
1577*4882a593Smuzhiyun * ssize_t sendmsg(int socket, const struct msghdr *message,
1578*4882a593Smuzhiyun * int flags);
1579*4882a593Smuzhiyun *
1580*4882a593Smuzhiyun * socket - the socket descriptor of the endpoint.
1581*4882a593Smuzhiyun * message - pointer to the msghdr structure which contains a single
1582*4882a593Smuzhiyun * user message and possibly some ancillary data.
1583*4882a593Smuzhiyun *
1584*4882a593Smuzhiyun * See Section 5 for complete description of the data
1585*4882a593Smuzhiyun * structures.
1586*4882a593Smuzhiyun *
1587*4882a593Smuzhiyun * flags - flags sent or received with the user message, see Section
1588*4882a593Smuzhiyun * 5 for complete description of the flags.
1589*4882a593Smuzhiyun *
1590*4882a593Smuzhiyun * Note: This function could use a rewrite especially when explicit
1591*4882a593Smuzhiyun * connect support comes in.
1592*4882a593Smuzhiyun */
1593*4882a593Smuzhiyun /* BUG: We do not implement the equivalent of sk_stream_wait_memory(). */
1594*4882a593Smuzhiyun
1595*4882a593Smuzhiyun static int sctp_msghdr_parse(const struct msghdr *msg,
1596*4882a593Smuzhiyun struct sctp_cmsgs *cmsgs);
1597*4882a593Smuzhiyun
sctp_sendmsg_parse(struct sock * sk,struct sctp_cmsgs * cmsgs,struct sctp_sndrcvinfo * srinfo,const struct msghdr * msg,size_t msg_len)1598*4882a593Smuzhiyun static int sctp_sendmsg_parse(struct sock *sk, struct sctp_cmsgs *cmsgs,
1599*4882a593Smuzhiyun struct sctp_sndrcvinfo *srinfo,
1600*4882a593Smuzhiyun const struct msghdr *msg, size_t msg_len)
1601*4882a593Smuzhiyun {
1602*4882a593Smuzhiyun __u16 sflags;
1603*4882a593Smuzhiyun int err;
1604*4882a593Smuzhiyun
1605*4882a593Smuzhiyun if (sctp_sstate(sk, LISTENING) && sctp_style(sk, TCP))
1606*4882a593Smuzhiyun return -EPIPE;
1607*4882a593Smuzhiyun
1608*4882a593Smuzhiyun if (msg_len > sk->sk_sndbuf)
1609*4882a593Smuzhiyun return -EMSGSIZE;
1610*4882a593Smuzhiyun
1611*4882a593Smuzhiyun memset(cmsgs, 0, sizeof(*cmsgs));
1612*4882a593Smuzhiyun err = sctp_msghdr_parse(msg, cmsgs);
1613*4882a593Smuzhiyun if (err) {
1614*4882a593Smuzhiyun pr_debug("%s: msghdr parse err:%x\n", __func__, err);
1615*4882a593Smuzhiyun return err;
1616*4882a593Smuzhiyun }
1617*4882a593Smuzhiyun
1618*4882a593Smuzhiyun memset(srinfo, 0, sizeof(*srinfo));
1619*4882a593Smuzhiyun if (cmsgs->srinfo) {
1620*4882a593Smuzhiyun srinfo->sinfo_stream = cmsgs->srinfo->sinfo_stream;
1621*4882a593Smuzhiyun srinfo->sinfo_flags = cmsgs->srinfo->sinfo_flags;
1622*4882a593Smuzhiyun srinfo->sinfo_ppid = cmsgs->srinfo->sinfo_ppid;
1623*4882a593Smuzhiyun srinfo->sinfo_context = cmsgs->srinfo->sinfo_context;
1624*4882a593Smuzhiyun srinfo->sinfo_assoc_id = cmsgs->srinfo->sinfo_assoc_id;
1625*4882a593Smuzhiyun srinfo->sinfo_timetolive = cmsgs->srinfo->sinfo_timetolive;
1626*4882a593Smuzhiyun }
1627*4882a593Smuzhiyun
1628*4882a593Smuzhiyun if (cmsgs->sinfo) {
1629*4882a593Smuzhiyun srinfo->sinfo_stream = cmsgs->sinfo->snd_sid;
1630*4882a593Smuzhiyun srinfo->sinfo_flags = cmsgs->sinfo->snd_flags;
1631*4882a593Smuzhiyun srinfo->sinfo_ppid = cmsgs->sinfo->snd_ppid;
1632*4882a593Smuzhiyun srinfo->sinfo_context = cmsgs->sinfo->snd_context;
1633*4882a593Smuzhiyun srinfo->sinfo_assoc_id = cmsgs->sinfo->snd_assoc_id;
1634*4882a593Smuzhiyun }
1635*4882a593Smuzhiyun
1636*4882a593Smuzhiyun if (cmsgs->prinfo) {
1637*4882a593Smuzhiyun srinfo->sinfo_timetolive = cmsgs->prinfo->pr_value;
1638*4882a593Smuzhiyun SCTP_PR_SET_POLICY(srinfo->sinfo_flags,
1639*4882a593Smuzhiyun cmsgs->prinfo->pr_policy);
1640*4882a593Smuzhiyun }
1641*4882a593Smuzhiyun
1642*4882a593Smuzhiyun sflags = srinfo->sinfo_flags;
1643*4882a593Smuzhiyun if (!sflags && msg_len)
1644*4882a593Smuzhiyun return 0;
1645*4882a593Smuzhiyun
1646*4882a593Smuzhiyun if (sctp_style(sk, TCP) && (sflags & (SCTP_EOF | SCTP_ABORT)))
1647*4882a593Smuzhiyun return -EINVAL;
1648*4882a593Smuzhiyun
1649*4882a593Smuzhiyun if (((sflags & SCTP_EOF) && msg_len > 0) ||
1650*4882a593Smuzhiyun (!(sflags & (SCTP_EOF | SCTP_ABORT)) && msg_len == 0))
1651*4882a593Smuzhiyun return -EINVAL;
1652*4882a593Smuzhiyun
1653*4882a593Smuzhiyun if ((sflags & SCTP_ADDR_OVER) && !msg->msg_name)
1654*4882a593Smuzhiyun return -EINVAL;
1655*4882a593Smuzhiyun
1656*4882a593Smuzhiyun return 0;
1657*4882a593Smuzhiyun }
1658*4882a593Smuzhiyun
sctp_sendmsg_new_asoc(struct sock * sk,__u16 sflags,struct sctp_cmsgs * cmsgs,union sctp_addr * daddr,struct sctp_transport ** tp)1659*4882a593Smuzhiyun static int sctp_sendmsg_new_asoc(struct sock *sk, __u16 sflags,
1660*4882a593Smuzhiyun struct sctp_cmsgs *cmsgs,
1661*4882a593Smuzhiyun union sctp_addr *daddr,
1662*4882a593Smuzhiyun struct sctp_transport **tp)
1663*4882a593Smuzhiyun {
1664*4882a593Smuzhiyun struct sctp_endpoint *ep = sctp_sk(sk)->ep;
1665*4882a593Smuzhiyun struct sctp_association *asoc;
1666*4882a593Smuzhiyun struct cmsghdr *cmsg;
1667*4882a593Smuzhiyun __be32 flowinfo = 0;
1668*4882a593Smuzhiyun struct sctp_af *af;
1669*4882a593Smuzhiyun int err;
1670*4882a593Smuzhiyun
1671*4882a593Smuzhiyun *tp = NULL;
1672*4882a593Smuzhiyun
1673*4882a593Smuzhiyun if (sflags & (SCTP_EOF | SCTP_ABORT))
1674*4882a593Smuzhiyun return -EINVAL;
1675*4882a593Smuzhiyun
1676*4882a593Smuzhiyun if (sctp_style(sk, TCP) && (sctp_sstate(sk, ESTABLISHED) ||
1677*4882a593Smuzhiyun sctp_sstate(sk, CLOSING)))
1678*4882a593Smuzhiyun return -EADDRNOTAVAIL;
1679*4882a593Smuzhiyun
1680*4882a593Smuzhiyun /* Label connection socket for first association 1-to-many
1681*4882a593Smuzhiyun * style for client sequence socket()->sendmsg(). This
1682*4882a593Smuzhiyun * needs to be done before sctp_assoc_add_peer() as that will
1683*4882a593Smuzhiyun * set up the initial packet that needs to account for any
1684*4882a593Smuzhiyun * security ip options (CIPSO/CALIPSO) added to the packet.
1685*4882a593Smuzhiyun */
1686*4882a593Smuzhiyun af = sctp_get_af_specific(daddr->sa.sa_family);
1687*4882a593Smuzhiyun if (!af)
1688*4882a593Smuzhiyun return -EINVAL;
1689*4882a593Smuzhiyun err = security_sctp_bind_connect(sk, SCTP_SENDMSG_CONNECT,
1690*4882a593Smuzhiyun (struct sockaddr *)daddr,
1691*4882a593Smuzhiyun af->sockaddr_len);
1692*4882a593Smuzhiyun if (err < 0)
1693*4882a593Smuzhiyun return err;
1694*4882a593Smuzhiyun
1695*4882a593Smuzhiyun err = sctp_connect_new_asoc(ep, daddr, cmsgs->init, tp);
1696*4882a593Smuzhiyun if (err)
1697*4882a593Smuzhiyun return err;
1698*4882a593Smuzhiyun asoc = (*tp)->asoc;
1699*4882a593Smuzhiyun
1700*4882a593Smuzhiyun if (!cmsgs->addrs_msg)
1701*4882a593Smuzhiyun return 0;
1702*4882a593Smuzhiyun
1703*4882a593Smuzhiyun if (daddr->sa.sa_family == AF_INET6)
1704*4882a593Smuzhiyun flowinfo = daddr->v6.sin6_flowinfo;
1705*4882a593Smuzhiyun
1706*4882a593Smuzhiyun /* sendv addr list parse */
1707*4882a593Smuzhiyun for_each_cmsghdr(cmsg, cmsgs->addrs_msg) {
1708*4882a593Smuzhiyun union sctp_addr _daddr;
1709*4882a593Smuzhiyun int dlen;
1710*4882a593Smuzhiyun
1711*4882a593Smuzhiyun if (cmsg->cmsg_level != IPPROTO_SCTP ||
1712*4882a593Smuzhiyun (cmsg->cmsg_type != SCTP_DSTADDRV4 &&
1713*4882a593Smuzhiyun cmsg->cmsg_type != SCTP_DSTADDRV6))
1714*4882a593Smuzhiyun continue;
1715*4882a593Smuzhiyun
1716*4882a593Smuzhiyun daddr = &_daddr;
1717*4882a593Smuzhiyun memset(daddr, 0, sizeof(*daddr));
1718*4882a593Smuzhiyun dlen = cmsg->cmsg_len - sizeof(struct cmsghdr);
1719*4882a593Smuzhiyun if (cmsg->cmsg_type == SCTP_DSTADDRV4) {
1720*4882a593Smuzhiyun if (dlen < sizeof(struct in_addr)) {
1721*4882a593Smuzhiyun err = -EINVAL;
1722*4882a593Smuzhiyun goto free;
1723*4882a593Smuzhiyun }
1724*4882a593Smuzhiyun
1725*4882a593Smuzhiyun dlen = sizeof(struct in_addr);
1726*4882a593Smuzhiyun daddr->v4.sin_family = AF_INET;
1727*4882a593Smuzhiyun daddr->v4.sin_port = htons(asoc->peer.port);
1728*4882a593Smuzhiyun memcpy(&daddr->v4.sin_addr, CMSG_DATA(cmsg), dlen);
1729*4882a593Smuzhiyun } else {
1730*4882a593Smuzhiyun if (dlen < sizeof(struct in6_addr)) {
1731*4882a593Smuzhiyun err = -EINVAL;
1732*4882a593Smuzhiyun goto free;
1733*4882a593Smuzhiyun }
1734*4882a593Smuzhiyun
1735*4882a593Smuzhiyun dlen = sizeof(struct in6_addr);
1736*4882a593Smuzhiyun daddr->v6.sin6_flowinfo = flowinfo;
1737*4882a593Smuzhiyun daddr->v6.sin6_family = AF_INET6;
1738*4882a593Smuzhiyun daddr->v6.sin6_port = htons(asoc->peer.port);
1739*4882a593Smuzhiyun memcpy(&daddr->v6.sin6_addr, CMSG_DATA(cmsg), dlen);
1740*4882a593Smuzhiyun }
1741*4882a593Smuzhiyun
1742*4882a593Smuzhiyun err = sctp_connect_add_peer(asoc, daddr, sizeof(*daddr));
1743*4882a593Smuzhiyun if (err)
1744*4882a593Smuzhiyun goto free;
1745*4882a593Smuzhiyun }
1746*4882a593Smuzhiyun
1747*4882a593Smuzhiyun return 0;
1748*4882a593Smuzhiyun
1749*4882a593Smuzhiyun free:
1750*4882a593Smuzhiyun sctp_association_free(asoc);
1751*4882a593Smuzhiyun return err;
1752*4882a593Smuzhiyun }
1753*4882a593Smuzhiyun
sctp_sendmsg_check_sflags(struct sctp_association * asoc,__u16 sflags,struct msghdr * msg,size_t msg_len)1754*4882a593Smuzhiyun static int sctp_sendmsg_check_sflags(struct sctp_association *asoc,
1755*4882a593Smuzhiyun __u16 sflags, struct msghdr *msg,
1756*4882a593Smuzhiyun size_t msg_len)
1757*4882a593Smuzhiyun {
1758*4882a593Smuzhiyun struct sock *sk = asoc->base.sk;
1759*4882a593Smuzhiyun struct net *net = sock_net(sk);
1760*4882a593Smuzhiyun
1761*4882a593Smuzhiyun if (sctp_state(asoc, CLOSED) && sctp_style(sk, TCP))
1762*4882a593Smuzhiyun return -EPIPE;
1763*4882a593Smuzhiyun
1764*4882a593Smuzhiyun if ((sflags & SCTP_SENDALL) && sctp_style(sk, UDP) &&
1765*4882a593Smuzhiyun !sctp_state(asoc, ESTABLISHED))
1766*4882a593Smuzhiyun return 0;
1767*4882a593Smuzhiyun
1768*4882a593Smuzhiyun if (sflags & SCTP_EOF) {
1769*4882a593Smuzhiyun pr_debug("%s: shutting down association:%p\n", __func__, asoc);
1770*4882a593Smuzhiyun sctp_primitive_SHUTDOWN(net, asoc, NULL);
1771*4882a593Smuzhiyun
1772*4882a593Smuzhiyun return 0;
1773*4882a593Smuzhiyun }
1774*4882a593Smuzhiyun
1775*4882a593Smuzhiyun if (sflags & SCTP_ABORT) {
1776*4882a593Smuzhiyun struct sctp_chunk *chunk;
1777*4882a593Smuzhiyun
1778*4882a593Smuzhiyun chunk = sctp_make_abort_user(asoc, msg, msg_len);
1779*4882a593Smuzhiyun if (!chunk)
1780*4882a593Smuzhiyun return -ENOMEM;
1781*4882a593Smuzhiyun
1782*4882a593Smuzhiyun pr_debug("%s: aborting association:%p\n", __func__, asoc);
1783*4882a593Smuzhiyun sctp_primitive_ABORT(net, asoc, chunk);
1784*4882a593Smuzhiyun iov_iter_revert(&msg->msg_iter, msg_len);
1785*4882a593Smuzhiyun
1786*4882a593Smuzhiyun return 0;
1787*4882a593Smuzhiyun }
1788*4882a593Smuzhiyun
1789*4882a593Smuzhiyun return 1;
1790*4882a593Smuzhiyun }
1791*4882a593Smuzhiyun
sctp_sendmsg_to_asoc(struct sctp_association * asoc,struct msghdr * msg,size_t msg_len,struct sctp_transport * transport,struct sctp_sndrcvinfo * sinfo)1792*4882a593Smuzhiyun static int sctp_sendmsg_to_asoc(struct sctp_association *asoc,
1793*4882a593Smuzhiyun struct msghdr *msg, size_t msg_len,
1794*4882a593Smuzhiyun struct sctp_transport *transport,
1795*4882a593Smuzhiyun struct sctp_sndrcvinfo *sinfo)
1796*4882a593Smuzhiyun {
1797*4882a593Smuzhiyun struct sock *sk = asoc->base.sk;
1798*4882a593Smuzhiyun struct sctp_sock *sp = sctp_sk(sk);
1799*4882a593Smuzhiyun struct net *net = sock_net(sk);
1800*4882a593Smuzhiyun struct sctp_datamsg *datamsg;
1801*4882a593Smuzhiyun bool wait_connect = false;
1802*4882a593Smuzhiyun struct sctp_chunk *chunk;
1803*4882a593Smuzhiyun long timeo;
1804*4882a593Smuzhiyun int err;
1805*4882a593Smuzhiyun
1806*4882a593Smuzhiyun if (sinfo->sinfo_stream >= asoc->stream.outcnt) {
1807*4882a593Smuzhiyun err = -EINVAL;
1808*4882a593Smuzhiyun goto err;
1809*4882a593Smuzhiyun }
1810*4882a593Smuzhiyun
1811*4882a593Smuzhiyun if (unlikely(!SCTP_SO(&asoc->stream, sinfo->sinfo_stream)->ext)) {
1812*4882a593Smuzhiyun err = sctp_stream_init_ext(&asoc->stream, sinfo->sinfo_stream);
1813*4882a593Smuzhiyun if (err)
1814*4882a593Smuzhiyun goto err;
1815*4882a593Smuzhiyun }
1816*4882a593Smuzhiyun
1817*4882a593Smuzhiyun if (sp->disable_fragments && msg_len > asoc->frag_point) {
1818*4882a593Smuzhiyun err = -EMSGSIZE;
1819*4882a593Smuzhiyun goto err;
1820*4882a593Smuzhiyun }
1821*4882a593Smuzhiyun
1822*4882a593Smuzhiyun if (asoc->pmtu_pending) {
1823*4882a593Smuzhiyun if (sp->param_flags & SPP_PMTUD_ENABLE)
1824*4882a593Smuzhiyun sctp_assoc_sync_pmtu(asoc);
1825*4882a593Smuzhiyun asoc->pmtu_pending = 0;
1826*4882a593Smuzhiyun }
1827*4882a593Smuzhiyun
1828*4882a593Smuzhiyun if (sctp_wspace(asoc) < (int)msg_len)
1829*4882a593Smuzhiyun sctp_prsctp_prune(asoc, sinfo, msg_len - sctp_wspace(asoc));
1830*4882a593Smuzhiyun
1831*4882a593Smuzhiyun if (sk_under_memory_pressure(sk))
1832*4882a593Smuzhiyun sk_mem_reclaim(sk);
1833*4882a593Smuzhiyun
1834*4882a593Smuzhiyun if (sctp_wspace(asoc) <= 0 || !sk_wmem_schedule(sk, msg_len)) {
1835*4882a593Smuzhiyun timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1836*4882a593Smuzhiyun err = sctp_wait_for_sndbuf(asoc, &timeo, msg_len);
1837*4882a593Smuzhiyun if (err)
1838*4882a593Smuzhiyun goto err;
1839*4882a593Smuzhiyun }
1840*4882a593Smuzhiyun
1841*4882a593Smuzhiyun if (sctp_state(asoc, CLOSED)) {
1842*4882a593Smuzhiyun err = sctp_primitive_ASSOCIATE(net, asoc, NULL);
1843*4882a593Smuzhiyun if (err)
1844*4882a593Smuzhiyun goto err;
1845*4882a593Smuzhiyun
1846*4882a593Smuzhiyun if (asoc->ep->intl_enable) {
1847*4882a593Smuzhiyun timeo = sock_sndtimeo(sk, 0);
1848*4882a593Smuzhiyun err = sctp_wait_for_connect(asoc, &timeo);
1849*4882a593Smuzhiyun if (err) {
1850*4882a593Smuzhiyun err = -ESRCH;
1851*4882a593Smuzhiyun goto err;
1852*4882a593Smuzhiyun }
1853*4882a593Smuzhiyun } else {
1854*4882a593Smuzhiyun wait_connect = true;
1855*4882a593Smuzhiyun }
1856*4882a593Smuzhiyun
1857*4882a593Smuzhiyun pr_debug("%s: we associated primitively\n", __func__);
1858*4882a593Smuzhiyun }
1859*4882a593Smuzhiyun
1860*4882a593Smuzhiyun datamsg = sctp_datamsg_from_user(asoc, sinfo, &msg->msg_iter);
1861*4882a593Smuzhiyun if (IS_ERR(datamsg)) {
1862*4882a593Smuzhiyun err = PTR_ERR(datamsg);
1863*4882a593Smuzhiyun goto err;
1864*4882a593Smuzhiyun }
1865*4882a593Smuzhiyun
1866*4882a593Smuzhiyun asoc->force_delay = !!(msg->msg_flags & MSG_MORE);
1867*4882a593Smuzhiyun
1868*4882a593Smuzhiyun list_for_each_entry(chunk, &datamsg->chunks, frag_list) {
1869*4882a593Smuzhiyun sctp_chunk_hold(chunk);
1870*4882a593Smuzhiyun sctp_set_owner_w(chunk);
1871*4882a593Smuzhiyun chunk->transport = transport;
1872*4882a593Smuzhiyun }
1873*4882a593Smuzhiyun
1874*4882a593Smuzhiyun err = sctp_primitive_SEND(net, asoc, datamsg);
1875*4882a593Smuzhiyun if (err) {
1876*4882a593Smuzhiyun sctp_datamsg_free(datamsg);
1877*4882a593Smuzhiyun goto err;
1878*4882a593Smuzhiyun }
1879*4882a593Smuzhiyun
1880*4882a593Smuzhiyun pr_debug("%s: we sent primitively\n", __func__);
1881*4882a593Smuzhiyun
1882*4882a593Smuzhiyun sctp_datamsg_put(datamsg);
1883*4882a593Smuzhiyun
1884*4882a593Smuzhiyun if (unlikely(wait_connect)) {
1885*4882a593Smuzhiyun timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1886*4882a593Smuzhiyun sctp_wait_for_connect(asoc, &timeo);
1887*4882a593Smuzhiyun }
1888*4882a593Smuzhiyun
1889*4882a593Smuzhiyun err = msg_len;
1890*4882a593Smuzhiyun
1891*4882a593Smuzhiyun err:
1892*4882a593Smuzhiyun return err;
1893*4882a593Smuzhiyun }
1894*4882a593Smuzhiyun
sctp_sendmsg_get_daddr(struct sock * sk,const struct msghdr * msg,struct sctp_cmsgs * cmsgs)1895*4882a593Smuzhiyun static union sctp_addr *sctp_sendmsg_get_daddr(struct sock *sk,
1896*4882a593Smuzhiyun const struct msghdr *msg,
1897*4882a593Smuzhiyun struct sctp_cmsgs *cmsgs)
1898*4882a593Smuzhiyun {
1899*4882a593Smuzhiyun union sctp_addr *daddr = NULL;
1900*4882a593Smuzhiyun int err;
1901*4882a593Smuzhiyun
1902*4882a593Smuzhiyun if (!sctp_style(sk, UDP_HIGH_BANDWIDTH) && msg->msg_name) {
1903*4882a593Smuzhiyun int len = msg->msg_namelen;
1904*4882a593Smuzhiyun
1905*4882a593Smuzhiyun if (len > sizeof(*daddr))
1906*4882a593Smuzhiyun len = sizeof(*daddr);
1907*4882a593Smuzhiyun
1908*4882a593Smuzhiyun daddr = (union sctp_addr *)msg->msg_name;
1909*4882a593Smuzhiyun
1910*4882a593Smuzhiyun err = sctp_verify_addr(sk, daddr, len);
1911*4882a593Smuzhiyun if (err)
1912*4882a593Smuzhiyun return ERR_PTR(err);
1913*4882a593Smuzhiyun }
1914*4882a593Smuzhiyun
1915*4882a593Smuzhiyun return daddr;
1916*4882a593Smuzhiyun }
1917*4882a593Smuzhiyun
sctp_sendmsg_update_sinfo(struct sctp_association * asoc,struct sctp_sndrcvinfo * sinfo,struct sctp_cmsgs * cmsgs)1918*4882a593Smuzhiyun static void sctp_sendmsg_update_sinfo(struct sctp_association *asoc,
1919*4882a593Smuzhiyun struct sctp_sndrcvinfo *sinfo,
1920*4882a593Smuzhiyun struct sctp_cmsgs *cmsgs)
1921*4882a593Smuzhiyun {
1922*4882a593Smuzhiyun if (!cmsgs->srinfo && !cmsgs->sinfo) {
1923*4882a593Smuzhiyun sinfo->sinfo_stream = asoc->default_stream;
1924*4882a593Smuzhiyun sinfo->sinfo_ppid = asoc->default_ppid;
1925*4882a593Smuzhiyun sinfo->sinfo_context = asoc->default_context;
1926*4882a593Smuzhiyun sinfo->sinfo_assoc_id = sctp_assoc2id(asoc);
1927*4882a593Smuzhiyun
1928*4882a593Smuzhiyun if (!cmsgs->prinfo)
1929*4882a593Smuzhiyun sinfo->sinfo_flags = asoc->default_flags;
1930*4882a593Smuzhiyun }
1931*4882a593Smuzhiyun
1932*4882a593Smuzhiyun if (!cmsgs->srinfo && !cmsgs->prinfo)
1933*4882a593Smuzhiyun sinfo->sinfo_timetolive = asoc->default_timetolive;
1934*4882a593Smuzhiyun
1935*4882a593Smuzhiyun if (cmsgs->authinfo) {
1936*4882a593Smuzhiyun /* Reuse sinfo_tsn to indicate that authinfo was set and
1937*4882a593Smuzhiyun * sinfo_ssn to save the keyid on tx path.
1938*4882a593Smuzhiyun */
1939*4882a593Smuzhiyun sinfo->sinfo_tsn = 1;
1940*4882a593Smuzhiyun sinfo->sinfo_ssn = cmsgs->authinfo->auth_keynumber;
1941*4882a593Smuzhiyun }
1942*4882a593Smuzhiyun }
1943*4882a593Smuzhiyun
sctp_sendmsg(struct sock * sk,struct msghdr * msg,size_t msg_len)1944*4882a593Smuzhiyun static int sctp_sendmsg(struct sock *sk, struct msghdr *msg, size_t msg_len)
1945*4882a593Smuzhiyun {
1946*4882a593Smuzhiyun struct sctp_endpoint *ep = sctp_sk(sk)->ep;
1947*4882a593Smuzhiyun struct sctp_transport *transport = NULL;
1948*4882a593Smuzhiyun struct sctp_sndrcvinfo _sinfo, *sinfo;
1949*4882a593Smuzhiyun struct sctp_association *asoc, *tmp;
1950*4882a593Smuzhiyun struct sctp_cmsgs cmsgs;
1951*4882a593Smuzhiyun union sctp_addr *daddr;
1952*4882a593Smuzhiyun bool new = false;
1953*4882a593Smuzhiyun __u16 sflags;
1954*4882a593Smuzhiyun int err;
1955*4882a593Smuzhiyun
1956*4882a593Smuzhiyun /* Parse and get snd_info */
1957*4882a593Smuzhiyun err = sctp_sendmsg_parse(sk, &cmsgs, &_sinfo, msg, msg_len);
1958*4882a593Smuzhiyun if (err)
1959*4882a593Smuzhiyun goto out;
1960*4882a593Smuzhiyun
1961*4882a593Smuzhiyun sinfo = &_sinfo;
1962*4882a593Smuzhiyun sflags = sinfo->sinfo_flags;
1963*4882a593Smuzhiyun
1964*4882a593Smuzhiyun /* Get daddr from msg */
1965*4882a593Smuzhiyun daddr = sctp_sendmsg_get_daddr(sk, msg, &cmsgs);
1966*4882a593Smuzhiyun if (IS_ERR(daddr)) {
1967*4882a593Smuzhiyun err = PTR_ERR(daddr);
1968*4882a593Smuzhiyun goto out;
1969*4882a593Smuzhiyun }
1970*4882a593Smuzhiyun
1971*4882a593Smuzhiyun lock_sock(sk);
1972*4882a593Smuzhiyun
1973*4882a593Smuzhiyun /* SCTP_SENDALL process */
1974*4882a593Smuzhiyun if ((sflags & SCTP_SENDALL) && sctp_style(sk, UDP)) {
1975*4882a593Smuzhiyun list_for_each_entry_safe(asoc, tmp, &ep->asocs, asocs) {
1976*4882a593Smuzhiyun err = sctp_sendmsg_check_sflags(asoc, sflags, msg,
1977*4882a593Smuzhiyun msg_len);
1978*4882a593Smuzhiyun if (err == 0)
1979*4882a593Smuzhiyun continue;
1980*4882a593Smuzhiyun if (err < 0)
1981*4882a593Smuzhiyun goto out_unlock;
1982*4882a593Smuzhiyun
1983*4882a593Smuzhiyun sctp_sendmsg_update_sinfo(asoc, sinfo, &cmsgs);
1984*4882a593Smuzhiyun
1985*4882a593Smuzhiyun err = sctp_sendmsg_to_asoc(asoc, msg, msg_len,
1986*4882a593Smuzhiyun NULL, sinfo);
1987*4882a593Smuzhiyun if (err < 0)
1988*4882a593Smuzhiyun goto out_unlock;
1989*4882a593Smuzhiyun
1990*4882a593Smuzhiyun iov_iter_revert(&msg->msg_iter, err);
1991*4882a593Smuzhiyun }
1992*4882a593Smuzhiyun
1993*4882a593Smuzhiyun goto out_unlock;
1994*4882a593Smuzhiyun }
1995*4882a593Smuzhiyun
1996*4882a593Smuzhiyun /* Get and check or create asoc */
1997*4882a593Smuzhiyun if (daddr) {
1998*4882a593Smuzhiyun asoc = sctp_endpoint_lookup_assoc(ep, daddr, &transport);
1999*4882a593Smuzhiyun if (asoc) {
2000*4882a593Smuzhiyun err = sctp_sendmsg_check_sflags(asoc, sflags, msg,
2001*4882a593Smuzhiyun msg_len);
2002*4882a593Smuzhiyun if (err <= 0)
2003*4882a593Smuzhiyun goto out_unlock;
2004*4882a593Smuzhiyun } else {
2005*4882a593Smuzhiyun err = sctp_sendmsg_new_asoc(sk, sflags, &cmsgs, daddr,
2006*4882a593Smuzhiyun &transport);
2007*4882a593Smuzhiyun if (err)
2008*4882a593Smuzhiyun goto out_unlock;
2009*4882a593Smuzhiyun
2010*4882a593Smuzhiyun asoc = transport->asoc;
2011*4882a593Smuzhiyun new = true;
2012*4882a593Smuzhiyun }
2013*4882a593Smuzhiyun
2014*4882a593Smuzhiyun if (!sctp_style(sk, TCP) && !(sflags & SCTP_ADDR_OVER))
2015*4882a593Smuzhiyun transport = NULL;
2016*4882a593Smuzhiyun } else {
2017*4882a593Smuzhiyun asoc = sctp_id2assoc(sk, sinfo->sinfo_assoc_id);
2018*4882a593Smuzhiyun if (!asoc) {
2019*4882a593Smuzhiyun err = -EPIPE;
2020*4882a593Smuzhiyun goto out_unlock;
2021*4882a593Smuzhiyun }
2022*4882a593Smuzhiyun
2023*4882a593Smuzhiyun err = sctp_sendmsg_check_sflags(asoc, sflags, msg, msg_len);
2024*4882a593Smuzhiyun if (err <= 0)
2025*4882a593Smuzhiyun goto out_unlock;
2026*4882a593Smuzhiyun }
2027*4882a593Smuzhiyun
2028*4882a593Smuzhiyun /* Update snd_info with the asoc */
2029*4882a593Smuzhiyun sctp_sendmsg_update_sinfo(asoc, sinfo, &cmsgs);
2030*4882a593Smuzhiyun
2031*4882a593Smuzhiyun /* Send msg to the asoc */
2032*4882a593Smuzhiyun err = sctp_sendmsg_to_asoc(asoc, msg, msg_len, transport, sinfo);
2033*4882a593Smuzhiyun if (err < 0 && err != -ESRCH && new)
2034*4882a593Smuzhiyun sctp_association_free(asoc);
2035*4882a593Smuzhiyun
2036*4882a593Smuzhiyun out_unlock:
2037*4882a593Smuzhiyun release_sock(sk);
2038*4882a593Smuzhiyun out:
2039*4882a593Smuzhiyun return sctp_error(sk, msg->msg_flags, err);
2040*4882a593Smuzhiyun }
2041*4882a593Smuzhiyun
2042*4882a593Smuzhiyun /* This is an extended version of skb_pull() that removes the data from the
2043*4882a593Smuzhiyun * start of a skb even when data is spread across the list of skb's in the
2044*4882a593Smuzhiyun * frag_list. len specifies the total amount of data that needs to be removed.
2045*4882a593Smuzhiyun * when 'len' bytes could be removed from the skb, it returns 0.
2046*4882a593Smuzhiyun * If 'len' exceeds the total skb length, it returns the no. of bytes that
2047*4882a593Smuzhiyun * could not be removed.
2048*4882a593Smuzhiyun */
sctp_skb_pull(struct sk_buff * skb,int len)2049*4882a593Smuzhiyun static int sctp_skb_pull(struct sk_buff *skb, int len)
2050*4882a593Smuzhiyun {
2051*4882a593Smuzhiyun struct sk_buff *list;
2052*4882a593Smuzhiyun int skb_len = skb_headlen(skb);
2053*4882a593Smuzhiyun int rlen;
2054*4882a593Smuzhiyun
2055*4882a593Smuzhiyun if (len <= skb_len) {
2056*4882a593Smuzhiyun __skb_pull(skb, len);
2057*4882a593Smuzhiyun return 0;
2058*4882a593Smuzhiyun }
2059*4882a593Smuzhiyun len -= skb_len;
2060*4882a593Smuzhiyun __skb_pull(skb, skb_len);
2061*4882a593Smuzhiyun
2062*4882a593Smuzhiyun skb_walk_frags(skb, list) {
2063*4882a593Smuzhiyun rlen = sctp_skb_pull(list, len);
2064*4882a593Smuzhiyun skb->len -= (len-rlen);
2065*4882a593Smuzhiyun skb->data_len -= (len-rlen);
2066*4882a593Smuzhiyun
2067*4882a593Smuzhiyun if (!rlen)
2068*4882a593Smuzhiyun return 0;
2069*4882a593Smuzhiyun
2070*4882a593Smuzhiyun len = rlen;
2071*4882a593Smuzhiyun }
2072*4882a593Smuzhiyun
2073*4882a593Smuzhiyun return len;
2074*4882a593Smuzhiyun }
2075*4882a593Smuzhiyun
2076*4882a593Smuzhiyun /* API 3.1.3 recvmsg() - UDP Style Syntax
2077*4882a593Smuzhiyun *
2078*4882a593Smuzhiyun * ssize_t recvmsg(int socket, struct msghdr *message,
2079*4882a593Smuzhiyun * int flags);
2080*4882a593Smuzhiyun *
2081*4882a593Smuzhiyun * socket - the socket descriptor of the endpoint.
2082*4882a593Smuzhiyun * message - pointer to the msghdr structure which contains a single
2083*4882a593Smuzhiyun * user message and possibly some ancillary data.
2084*4882a593Smuzhiyun *
2085*4882a593Smuzhiyun * See Section 5 for complete description of the data
2086*4882a593Smuzhiyun * structures.
2087*4882a593Smuzhiyun *
2088*4882a593Smuzhiyun * flags - flags sent or received with the user message, see Section
2089*4882a593Smuzhiyun * 5 for complete description of the flags.
2090*4882a593Smuzhiyun */
sctp_recvmsg(struct sock * sk,struct msghdr * msg,size_t len,int noblock,int flags,int * addr_len)2091*4882a593Smuzhiyun static int sctp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
2092*4882a593Smuzhiyun int noblock, int flags, int *addr_len)
2093*4882a593Smuzhiyun {
2094*4882a593Smuzhiyun struct sctp_ulpevent *event = NULL;
2095*4882a593Smuzhiyun struct sctp_sock *sp = sctp_sk(sk);
2096*4882a593Smuzhiyun struct sk_buff *skb, *head_skb;
2097*4882a593Smuzhiyun int copied;
2098*4882a593Smuzhiyun int err = 0;
2099*4882a593Smuzhiyun int skb_len;
2100*4882a593Smuzhiyun
2101*4882a593Smuzhiyun pr_debug("%s: sk:%p, msghdr:%p, len:%zd, noblock:%d, flags:0x%x, "
2102*4882a593Smuzhiyun "addr_len:%p)\n", __func__, sk, msg, len, noblock, flags,
2103*4882a593Smuzhiyun addr_len);
2104*4882a593Smuzhiyun
2105*4882a593Smuzhiyun lock_sock(sk);
2106*4882a593Smuzhiyun
2107*4882a593Smuzhiyun if (sctp_style(sk, TCP) && !sctp_sstate(sk, ESTABLISHED) &&
2108*4882a593Smuzhiyun !sctp_sstate(sk, CLOSING) && !sctp_sstate(sk, CLOSED)) {
2109*4882a593Smuzhiyun err = -ENOTCONN;
2110*4882a593Smuzhiyun goto out;
2111*4882a593Smuzhiyun }
2112*4882a593Smuzhiyun
2113*4882a593Smuzhiyun skb = sctp_skb_recv_datagram(sk, flags, noblock, &err);
2114*4882a593Smuzhiyun if (!skb)
2115*4882a593Smuzhiyun goto out;
2116*4882a593Smuzhiyun
2117*4882a593Smuzhiyun /* Get the total length of the skb including any skb's in the
2118*4882a593Smuzhiyun * frag_list.
2119*4882a593Smuzhiyun */
2120*4882a593Smuzhiyun skb_len = skb->len;
2121*4882a593Smuzhiyun
2122*4882a593Smuzhiyun copied = skb_len;
2123*4882a593Smuzhiyun if (copied > len)
2124*4882a593Smuzhiyun copied = len;
2125*4882a593Smuzhiyun
2126*4882a593Smuzhiyun err = skb_copy_datagram_msg(skb, 0, msg, copied);
2127*4882a593Smuzhiyun
2128*4882a593Smuzhiyun event = sctp_skb2event(skb);
2129*4882a593Smuzhiyun
2130*4882a593Smuzhiyun if (err)
2131*4882a593Smuzhiyun goto out_free;
2132*4882a593Smuzhiyun
2133*4882a593Smuzhiyun if (event->chunk && event->chunk->head_skb)
2134*4882a593Smuzhiyun head_skb = event->chunk->head_skb;
2135*4882a593Smuzhiyun else
2136*4882a593Smuzhiyun head_skb = skb;
2137*4882a593Smuzhiyun sock_recv_ts_and_drops(msg, sk, head_skb);
2138*4882a593Smuzhiyun if (sctp_ulpevent_is_notification(event)) {
2139*4882a593Smuzhiyun msg->msg_flags |= MSG_NOTIFICATION;
2140*4882a593Smuzhiyun sp->pf->event_msgname(event, msg->msg_name, addr_len);
2141*4882a593Smuzhiyun } else {
2142*4882a593Smuzhiyun sp->pf->skb_msgname(head_skb, msg->msg_name, addr_len);
2143*4882a593Smuzhiyun }
2144*4882a593Smuzhiyun
2145*4882a593Smuzhiyun /* Check if we allow SCTP_NXTINFO. */
2146*4882a593Smuzhiyun if (sp->recvnxtinfo)
2147*4882a593Smuzhiyun sctp_ulpevent_read_nxtinfo(event, msg, sk);
2148*4882a593Smuzhiyun /* Check if we allow SCTP_RCVINFO. */
2149*4882a593Smuzhiyun if (sp->recvrcvinfo)
2150*4882a593Smuzhiyun sctp_ulpevent_read_rcvinfo(event, msg);
2151*4882a593Smuzhiyun /* Check if we allow SCTP_SNDRCVINFO. */
2152*4882a593Smuzhiyun if (sctp_ulpevent_type_enabled(sp->subscribe, SCTP_DATA_IO_EVENT))
2153*4882a593Smuzhiyun sctp_ulpevent_read_sndrcvinfo(event, msg);
2154*4882a593Smuzhiyun
2155*4882a593Smuzhiyun err = copied;
2156*4882a593Smuzhiyun
2157*4882a593Smuzhiyun /* If skb's length exceeds the user's buffer, update the skb and
2158*4882a593Smuzhiyun * push it back to the receive_queue so that the next call to
2159*4882a593Smuzhiyun * recvmsg() will return the remaining data. Don't set MSG_EOR.
2160*4882a593Smuzhiyun */
2161*4882a593Smuzhiyun if (skb_len > copied) {
2162*4882a593Smuzhiyun msg->msg_flags &= ~MSG_EOR;
2163*4882a593Smuzhiyun if (flags & MSG_PEEK)
2164*4882a593Smuzhiyun goto out_free;
2165*4882a593Smuzhiyun sctp_skb_pull(skb, copied);
2166*4882a593Smuzhiyun skb_queue_head(&sk->sk_receive_queue, skb);
2167*4882a593Smuzhiyun
2168*4882a593Smuzhiyun /* When only partial message is copied to the user, increase
2169*4882a593Smuzhiyun * rwnd by that amount. If all the data in the skb is read,
2170*4882a593Smuzhiyun * rwnd is updated when the event is freed.
2171*4882a593Smuzhiyun */
2172*4882a593Smuzhiyun if (!sctp_ulpevent_is_notification(event))
2173*4882a593Smuzhiyun sctp_assoc_rwnd_increase(event->asoc, copied);
2174*4882a593Smuzhiyun goto out;
2175*4882a593Smuzhiyun } else if ((event->msg_flags & MSG_NOTIFICATION) ||
2176*4882a593Smuzhiyun (event->msg_flags & MSG_EOR))
2177*4882a593Smuzhiyun msg->msg_flags |= MSG_EOR;
2178*4882a593Smuzhiyun else
2179*4882a593Smuzhiyun msg->msg_flags &= ~MSG_EOR;
2180*4882a593Smuzhiyun
2181*4882a593Smuzhiyun out_free:
2182*4882a593Smuzhiyun if (flags & MSG_PEEK) {
2183*4882a593Smuzhiyun /* Release the skb reference acquired after peeking the skb in
2184*4882a593Smuzhiyun * sctp_skb_recv_datagram().
2185*4882a593Smuzhiyun */
2186*4882a593Smuzhiyun kfree_skb(skb);
2187*4882a593Smuzhiyun } else {
2188*4882a593Smuzhiyun /* Free the event which includes releasing the reference to
2189*4882a593Smuzhiyun * the owner of the skb, freeing the skb and updating the
2190*4882a593Smuzhiyun * rwnd.
2191*4882a593Smuzhiyun */
2192*4882a593Smuzhiyun sctp_ulpevent_free(event);
2193*4882a593Smuzhiyun }
2194*4882a593Smuzhiyun out:
2195*4882a593Smuzhiyun release_sock(sk);
2196*4882a593Smuzhiyun return err;
2197*4882a593Smuzhiyun }
2198*4882a593Smuzhiyun
2199*4882a593Smuzhiyun /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
2200*4882a593Smuzhiyun *
2201*4882a593Smuzhiyun * This option is a on/off flag. If enabled no SCTP message
2202*4882a593Smuzhiyun * fragmentation will be performed. Instead if a message being sent
2203*4882a593Smuzhiyun * exceeds the current PMTU size, the message will NOT be sent and
2204*4882a593Smuzhiyun * instead a error will be indicated to the user.
2205*4882a593Smuzhiyun */
sctp_setsockopt_disable_fragments(struct sock * sk,int * val,unsigned int optlen)2206*4882a593Smuzhiyun static int sctp_setsockopt_disable_fragments(struct sock *sk, int *val,
2207*4882a593Smuzhiyun unsigned int optlen)
2208*4882a593Smuzhiyun {
2209*4882a593Smuzhiyun if (optlen < sizeof(int))
2210*4882a593Smuzhiyun return -EINVAL;
2211*4882a593Smuzhiyun sctp_sk(sk)->disable_fragments = (*val == 0) ? 0 : 1;
2212*4882a593Smuzhiyun return 0;
2213*4882a593Smuzhiyun }
2214*4882a593Smuzhiyun
sctp_setsockopt_events(struct sock * sk,__u8 * sn_type,unsigned int optlen)2215*4882a593Smuzhiyun static int sctp_setsockopt_events(struct sock *sk, __u8 *sn_type,
2216*4882a593Smuzhiyun unsigned int optlen)
2217*4882a593Smuzhiyun {
2218*4882a593Smuzhiyun struct sctp_sock *sp = sctp_sk(sk);
2219*4882a593Smuzhiyun struct sctp_association *asoc;
2220*4882a593Smuzhiyun int i;
2221*4882a593Smuzhiyun
2222*4882a593Smuzhiyun if (optlen > sizeof(struct sctp_event_subscribe))
2223*4882a593Smuzhiyun return -EINVAL;
2224*4882a593Smuzhiyun
2225*4882a593Smuzhiyun for (i = 0; i < optlen; i++)
2226*4882a593Smuzhiyun sctp_ulpevent_type_set(&sp->subscribe, SCTP_SN_TYPE_BASE + i,
2227*4882a593Smuzhiyun sn_type[i]);
2228*4882a593Smuzhiyun
2229*4882a593Smuzhiyun list_for_each_entry(asoc, &sp->ep->asocs, asocs)
2230*4882a593Smuzhiyun asoc->subscribe = sctp_sk(sk)->subscribe;
2231*4882a593Smuzhiyun
2232*4882a593Smuzhiyun /* At the time when a user app subscribes to SCTP_SENDER_DRY_EVENT,
2233*4882a593Smuzhiyun * if there is no data to be sent or retransmit, the stack will
2234*4882a593Smuzhiyun * immediately send up this notification.
2235*4882a593Smuzhiyun */
2236*4882a593Smuzhiyun if (sctp_ulpevent_type_enabled(sp->subscribe, SCTP_SENDER_DRY_EVENT)) {
2237*4882a593Smuzhiyun struct sctp_ulpevent *event;
2238*4882a593Smuzhiyun
2239*4882a593Smuzhiyun asoc = sctp_id2assoc(sk, 0);
2240*4882a593Smuzhiyun if (asoc && sctp_outq_is_empty(&asoc->outqueue)) {
2241*4882a593Smuzhiyun event = sctp_ulpevent_make_sender_dry_event(asoc,
2242*4882a593Smuzhiyun GFP_USER | __GFP_NOWARN);
2243*4882a593Smuzhiyun if (!event)
2244*4882a593Smuzhiyun return -ENOMEM;
2245*4882a593Smuzhiyun
2246*4882a593Smuzhiyun asoc->stream.si->enqueue_event(&asoc->ulpq, event);
2247*4882a593Smuzhiyun }
2248*4882a593Smuzhiyun }
2249*4882a593Smuzhiyun
2250*4882a593Smuzhiyun return 0;
2251*4882a593Smuzhiyun }
2252*4882a593Smuzhiyun
2253*4882a593Smuzhiyun /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
2254*4882a593Smuzhiyun *
2255*4882a593Smuzhiyun * This socket option is applicable to the UDP-style socket only. When
2256*4882a593Smuzhiyun * set it will cause associations that are idle for more than the
2257*4882a593Smuzhiyun * specified number of seconds to automatically close. An association
2258*4882a593Smuzhiyun * being idle is defined an association that has NOT sent or received
2259*4882a593Smuzhiyun * user data. The special value of '0' indicates that no automatic
2260*4882a593Smuzhiyun * close of any associations should be performed. The option expects an
2261*4882a593Smuzhiyun * integer defining the number of seconds of idle time before an
2262*4882a593Smuzhiyun * association is closed.
2263*4882a593Smuzhiyun */
sctp_setsockopt_autoclose(struct sock * sk,u32 * optval,unsigned int optlen)2264*4882a593Smuzhiyun static int sctp_setsockopt_autoclose(struct sock *sk, u32 *optval,
2265*4882a593Smuzhiyun unsigned int optlen)
2266*4882a593Smuzhiyun {
2267*4882a593Smuzhiyun struct sctp_sock *sp = sctp_sk(sk);
2268*4882a593Smuzhiyun struct net *net = sock_net(sk);
2269*4882a593Smuzhiyun
2270*4882a593Smuzhiyun /* Applicable to UDP-style socket only */
2271*4882a593Smuzhiyun if (sctp_style(sk, TCP))
2272*4882a593Smuzhiyun return -EOPNOTSUPP;
2273*4882a593Smuzhiyun if (optlen != sizeof(int))
2274*4882a593Smuzhiyun return -EINVAL;
2275*4882a593Smuzhiyun
2276*4882a593Smuzhiyun sp->autoclose = *optval;
2277*4882a593Smuzhiyun if (sp->autoclose > net->sctp.max_autoclose)
2278*4882a593Smuzhiyun sp->autoclose = net->sctp.max_autoclose;
2279*4882a593Smuzhiyun
2280*4882a593Smuzhiyun return 0;
2281*4882a593Smuzhiyun }
2282*4882a593Smuzhiyun
2283*4882a593Smuzhiyun /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
2284*4882a593Smuzhiyun *
2285*4882a593Smuzhiyun * Applications can enable or disable heartbeats for any peer address of
2286*4882a593Smuzhiyun * an association, modify an address's heartbeat interval, force a
2287*4882a593Smuzhiyun * heartbeat to be sent immediately, and adjust the address's maximum
2288*4882a593Smuzhiyun * number of retransmissions sent before an address is considered
2289*4882a593Smuzhiyun * unreachable. The following structure is used to access and modify an
2290*4882a593Smuzhiyun * address's parameters:
2291*4882a593Smuzhiyun *
2292*4882a593Smuzhiyun * struct sctp_paddrparams {
2293*4882a593Smuzhiyun * sctp_assoc_t spp_assoc_id;
2294*4882a593Smuzhiyun * struct sockaddr_storage spp_address;
2295*4882a593Smuzhiyun * uint32_t spp_hbinterval;
2296*4882a593Smuzhiyun * uint16_t spp_pathmaxrxt;
2297*4882a593Smuzhiyun * uint32_t spp_pathmtu;
2298*4882a593Smuzhiyun * uint32_t spp_sackdelay;
2299*4882a593Smuzhiyun * uint32_t spp_flags;
2300*4882a593Smuzhiyun * uint32_t spp_ipv6_flowlabel;
2301*4882a593Smuzhiyun * uint8_t spp_dscp;
2302*4882a593Smuzhiyun * };
2303*4882a593Smuzhiyun *
2304*4882a593Smuzhiyun * spp_assoc_id - (one-to-many style socket) This is filled in the
2305*4882a593Smuzhiyun * application, and identifies the association for
2306*4882a593Smuzhiyun * this query.
2307*4882a593Smuzhiyun * spp_address - This specifies which address is of interest.
2308*4882a593Smuzhiyun * spp_hbinterval - This contains the value of the heartbeat interval,
2309*4882a593Smuzhiyun * in milliseconds. If a value of zero
2310*4882a593Smuzhiyun * is present in this field then no changes are to
2311*4882a593Smuzhiyun * be made to this parameter.
2312*4882a593Smuzhiyun * spp_pathmaxrxt - This contains the maximum number of
2313*4882a593Smuzhiyun * retransmissions before this address shall be
2314*4882a593Smuzhiyun * considered unreachable. If a value of zero
2315*4882a593Smuzhiyun * is present in this field then no changes are to
2316*4882a593Smuzhiyun * be made to this parameter.
2317*4882a593Smuzhiyun * spp_pathmtu - When Path MTU discovery is disabled the value
2318*4882a593Smuzhiyun * specified here will be the "fixed" path mtu.
2319*4882a593Smuzhiyun * Note that if the spp_address field is empty
2320*4882a593Smuzhiyun * then all associations on this address will
2321*4882a593Smuzhiyun * have this fixed path mtu set upon them.
2322*4882a593Smuzhiyun *
2323*4882a593Smuzhiyun * spp_sackdelay - When delayed sack is enabled, this value specifies
2324*4882a593Smuzhiyun * the number of milliseconds that sacks will be delayed
2325*4882a593Smuzhiyun * for. This value will apply to all addresses of an
2326*4882a593Smuzhiyun * association if the spp_address field is empty. Note
2327*4882a593Smuzhiyun * also, that if delayed sack is enabled and this
2328*4882a593Smuzhiyun * value is set to 0, no change is made to the last
2329*4882a593Smuzhiyun * recorded delayed sack timer value.
2330*4882a593Smuzhiyun *
2331*4882a593Smuzhiyun * spp_flags - These flags are used to control various features
2332*4882a593Smuzhiyun * on an association. The flag field may contain
2333*4882a593Smuzhiyun * zero or more of the following options.
2334*4882a593Smuzhiyun *
2335*4882a593Smuzhiyun * SPP_HB_ENABLE - Enable heartbeats on the
2336*4882a593Smuzhiyun * specified address. Note that if the address
2337*4882a593Smuzhiyun * field is empty all addresses for the association
2338*4882a593Smuzhiyun * have heartbeats enabled upon them.
2339*4882a593Smuzhiyun *
2340*4882a593Smuzhiyun * SPP_HB_DISABLE - Disable heartbeats on the
2341*4882a593Smuzhiyun * speicifed address. Note that if the address
2342*4882a593Smuzhiyun * field is empty all addresses for the association
2343*4882a593Smuzhiyun * will have their heartbeats disabled. Note also
2344*4882a593Smuzhiyun * that SPP_HB_ENABLE and SPP_HB_DISABLE are
2345*4882a593Smuzhiyun * mutually exclusive, only one of these two should
2346*4882a593Smuzhiyun * be specified. Enabling both fields will have
2347*4882a593Smuzhiyun * undetermined results.
2348*4882a593Smuzhiyun *
2349*4882a593Smuzhiyun * SPP_HB_DEMAND - Request a user initiated heartbeat
2350*4882a593Smuzhiyun * to be made immediately.
2351*4882a593Smuzhiyun *
2352*4882a593Smuzhiyun * SPP_HB_TIME_IS_ZERO - Specify's that the time for
2353*4882a593Smuzhiyun * heartbeat delayis to be set to the value of 0
2354*4882a593Smuzhiyun * milliseconds.
2355*4882a593Smuzhiyun *
2356*4882a593Smuzhiyun * SPP_PMTUD_ENABLE - This field will enable PMTU
2357*4882a593Smuzhiyun * discovery upon the specified address. Note that
2358*4882a593Smuzhiyun * if the address feild is empty then all addresses
2359*4882a593Smuzhiyun * on the association are effected.
2360*4882a593Smuzhiyun *
2361*4882a593Smuzhiyun * SPP_PMTUD_DISABLE - This field will disable PMTU
2362*4882a593Smuzhiyun * discovery upon the specified address. Note that
2363*4882a593Smuzhiyun * if the address feild is empty then all addresses
2364*4882a593Smuzhiyun * on the association are effected. Not also that
2365*4882a593Smuzhiyun * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
2366*4882a593Smuzhiyun * exclusive. Enabling both will have undetermined
2367*4882a593Smuzhiyun * results.
2368*4882a593Smuzhiyun *
2369*4882a593Smuzhiyun * SPP_SACKDELAY_ENABLE - Setting this flag turns
2370*4882a593Smuzhiyun * on delayed sack. The time specified in spp_sackdelay
2371*4882a593Smuzhiyun * is used to specify the sack delay for this address. Note
2372*4882a593Smuzhiyun * that if spp_address is empty then all addresses will
2373*4882a593Smuzhiyun * enable delayed sack and take on the sack delay
2374*4882a593Smuzhiyun * value specified in spp_sackdelay.
2375*4882a593Smuzhiyun * SPP_SACKDELAY_DISABLE - Setting this flag turns
2376*4882a593Smuzhiyun * off delayed sack. If the spp_address field is blank then
2377*4882a593Smuzhiyun * delayed sack is disabled for the entire association. Note
2378*4882a593Smuzhiyun * also that this field is mutually exclusive to
2379*4882a593Smuzhiyun * SPP_SACKDELAY_ENABLE, setting both will have undefined
2380*4882a593Smuzhiyun * results.
2381*4882a593Smuzhiyun *
2382*4882a593Smuzhiyun * SPP_IPV6_FLOWLABEL: Setting this flag enables the
2383*4882a593Smuzhiyun * setting of the IPV6 flow label value. The value is
2384*4882a593Smuzhiyun * contained in the spp_ipv6_flowlabel field.
2385*4882a593Smuzhiyun * Upon retrieval, this flag will be set to indicate that
2386*4882a593Smuzhiyun * the spp_ipv6_flowlabel field has a valid value returned.
2387*4882a593Smuzhiyun * If a specific destination address is set (in the
2388*4882a593Smuzhiyun * spp_address field), then the value returned is that of
2389*4882a593Smuzhiyun * the address. If just an association is specified (and
2390*4882a593Smuzhiyun * no address), then the association's default flow label
2391*4882a593Smuzhiyun * is returned. If neither an association nor a destination
2392*4882a593Smuzhiyun * is specified, then the socket's default flow label is
2393*4882a593Smuzhiyun * returned. For non-IPv6 sockets, this flag will be left
2394*4882a593Smuzhiyun * cleared.
2395*4882a593Smuzhiyun *
2396*4882a593Smuzhiyun * SPP_DSCP: Setting this flag enables the setting of the
2397*4882a593Smuzhiyun * Differentiated Services Code Point (DSCP) value
2398*4882a593Smuzhiyun * associated with either the association or a specific
2399*4882a593Smuzhiyun * address. The value is obtained in the spp_dscp field.
2400*4882a593Smuzhiyun * Upon retrieval, this flag will be set to indicate that
2401*4882a593Smuzhiyun * the spp_dscp field has a valid value returned. If a
2402*4882a593Smuzhiyun * specific destination address is set when called (in the
2403*4882a593Smuzhiyun * spp_address field), then that specific destination
2404*4882a593Smuzhiyun * address's DSCP value is returned. If just an association
2405*4882a593Smuzhiyun * is specified, then the association's default DSCP is
2406*4882a593Smuzhiyun * returned. If neither an association nor a destination is
2407*4882a593Smuzhiyun * specified, then the socket's default DSCP is returned.
2408*4882a593Smuzhiyun *
2409*4882a593Smuzhiyun * spp_ipv6_flowlabel
2410*4882a593Smuzhiyun * - This field is used in conjunction with the
2411*4882a593Smuzhiyun * SPP_IPV6_FLOWLABEL flag and contains the IPv6 flow label.
2412*4882a593Smuzhiyun * The 20 least significant bits are used for the flow
2413*4882a593Smuzhiyun * label. This setting has precedence over any IPv6-layer
2414*4882a593Smuzhiyun * setting.
2415*4882a593Smuzhiyun *
2416*4882a593Smuzhiyun * spp_dscp - This field is used in conjunction with the SPP_DSCP flag
2417*4882a593Smuzhiyun * and contains the DSCP. The 6 most significant bits are
2418*4882a593Smuzhiyun * used for the DSCP. This setting has precedence over any
2419*4882a593Smuzhiyun * IPv4- or IPv6- layer setting.
2420*4882a593Smuzhiyun */
sctp_apply_peer_addr_params(struct sctp_paddrparams * params,struct sctp_transport * trans,struct sctp_association * asoc,struct sctp_sock * sp,int hb_change,int pmtud_change,int sackdelay_change)2421*4882a593Smuzhiyun static int sctp_apply_peer_addr_params(struct sctp_paddrparams *params,
2422*4882a593Smuzhiyun struct sctp_transport *trans,
2423*4882a593Smuzhiyun struct sctp_association *asoc,
2424*4882a593Smuzhiyun struct sctp_sock *sp,
2425*4882a593Smuzhiyun int hb_change,
2426*4882a593Smuzhiyun int pmtud_change,
2427*4882a593Smuzhiyun int sackdelay_change)
2428*4882a593Smuzhiyun {
2429*4882a593Smuzhiyun int error;
2430*4882a593Smuzhiyun
2431*4882a593Smuzhiyun if (params->spp_flags & SPP_HB_DEMAND && trans) {
2432*4882a593Smuzhiyun error = sctp_primitive_REQUESTHEARTBEAT(trans->asoc->base.net,
2433*4882a593Smuzhiyun trans->asoc, trans);
2434*4882a593Smuzhiyun if (error)
2435*4882a593Smuzhiyun return error;
2436*4882a593Smuzhiyun }
2437*4882a593Smuzhiyun
2438*4882a593Smuzhiyun /* Note that unless the spp_flag is set to SPP_HB_ENABLE the value of
2439*4882a593Smuzhiyun * this field is ignored. Note also that a value of zero indicates
2440*4882a593Smuzhiyun * the current setting should be left unchanged.
2441*4882a593Smuzhiyun */
2442*4882a593Smuzhiyun if (params->spp_flags & SPP_HB_ENABLE) {
2443*4882a593Smuzhiyun
2444*4882a593Smuzhiyun /* Re-zero the interval if the SPP_HB_TIME_IS_ZERO is
2445*4882a593Smuzhiyun * set. This lets us use 0 value when this flag
2446*4882a593Smuzhiyun * is set.
2447*4882a593Smuzhiyun */
2448*4882a593Smuzhiyun if (params->spp_flags & SPP_HB_TIME_IS_ZERO)
2449*4882a593Smuzhiyun params->spp_hbinterval = 0;
2450*4882a593Smuzhiyun
2451*4882a593Smuzhiyun if (params->spp_hbinterval ||
2452*4882a593Smuzhiyun (params->spp_flags & SPP_HB_TIME_IS_ZERO)) {
2453*4882a593Smuzhiyun if (trans) {
2454*4882a593Smuzhiyun trans->hbinterval =
2455*4882a593Smuzhiyun msecs_to_jiffies(params->spp_hbinterval);
2456*4882a593Smuzhiyun } else if (asoc) {
2457*4882a593Smuzhiyun asoc->hbinterval =
2458*4882a593Smuzhiyun msecs_to_jiffies(params->spp_hbinterval);
2459*4882a593Smuzhiyun } else {
2460*4882a593Smuzhiyun sp->hbinterval = params->spp_hbinterval;
2461*4882a593Smuzhiyun }
2462*4882a593Smuzhiyun }
2463*4882a593Smuzhiyun }
2464*4882a593Smuzhiyun
2465*4882a593Smuzhiyun if (hb_change) {
2466*4882a593Smuzhiyun if (trans) {
2467*4882a593Smuzhiyun trans->param_flags =
2468*4882a593Smuzhiyun (trans->param_flags & ~SPP_HB) | hb_change;
2469*4882a593Smuzhiyun } else if (asoc) {
2470*4882a593Smuzhiyun asoc->param_flags =
2471*4882a593Smuzhiyun (asoc->param_flags & ~SPP_HB) | hb_change;
2472*4882a593Smuzhiyun } else {
2473*4882a593Smuzhiyun sp->param_flags =
2474*4882a593Smuzhiyun (sp->param_flags & ~SPP_HB) | hb_change;
2475*4882a593Smuzhiyun }
2476*4882a593Smuzhiyun }
2477*4882a593Smuzhiyun
2478*4882a593Smuzhiyun /* When Path MTU discovery is disabled the value specified here will
2479*4882a593Smuzhiyun * be the "fixed" path mtu (i.e. the value of the spp_flags field must
2480*4882a593Smuzhiyun * include the flag SPP_PMTUD_DISABLE for this field to have any
2481*4882a593Smuzhiyun * effect).
2482*4882a593Smuzhiyun */
2483*4882a593Smuzhiyun if ((params->spp_flags & SPP_PMTUD_DISABLE) && params->spp_pathmtu) {
2484*4882a593Smuzhiyun if (trans) {
2485*4882a593Smuzhiyun trans->pathmtu = params->spp_pathmtu;
2486*4882a593Smuzhiyun sctp_assoc_sync_pmtu(asoc);
2487*4882a593Smuzhiyun } else if (asoc) {
2488*4882a593Smuzhiyun sctp_assoc_set_pmtu(asoc, params->spp_pathmtu);
2489*4882a593Smuzhiyun } else {
2490*4882a593Smuzhiyun sp->pathmtu = params->spp_pathmtu;
2491*4882a593Smuzhiyun }
2492*4882a593Smuzhiyun }
2493*4882a593Smuzhiyun
2494*4882a593Smuzhiyun if (pmtud_change) {
2495*4882a593Smuzhiyun if (trans) {
2496*4882a593Smuzhiyun int update = (trans->param_flags & SPP_PMTUD_DISABLE) &&
2497*4882a593Smuzhiyun (params->spp_flags & SPP_PMTUD_ENABLE);
2498*4882a593Smuzhiyun trans->param_flags =
2499*4882a593Smuzhiyun (trans->param_flags & ~SPP_PMTUD) | pmtud_change;
2500*4882a593Smuzhiyun if (update) {
2501*4882a593Smuzhiyun sctp_transport_pmtu(trans, sctp_opt2sk(sp));
2502*4882a593Smuzhiyun sctp_assoc_sync_pmtu(asoc);
2503*4882a593Smuzhiyun }
2504*4882a593Smuzhiyun } else if (asoc) {
2505*4882a593Smuzhiyun asoc->param_flags =
2506*4882a593Smuzhiyun (asoc->param_flags & ~SPP_PMTUD) | pmtud_change;
2507*4882a593Smuzhiyun } else {
2508*4882a593Smuzhiyun sp->param_flags =
2509*4882a593Smuzhiyun (sp->param_flags & ~SPP_PMTUD) | pmtud_change;
2510*4882a593Smuzhiyun }
2511*4882a593Smuzhiyun }
2512*4882a593Smuzhiyun
2513*4882a593Smuzhiyun /* Note that unless the spp_flag is set to SPP_SACKDELAY_ENABLE the
2514*4882a593Smuzhiyun * value of this field is ignored. Note also that a value of zero
2515*4882a593Smuzhiyun * indicates the current setting should be left unchanged.
2516*4882a593Smuzhiyun */
2517*4882a593Smuzhiyun if ((params->spp_flags & SPP_SACKDELAY_ENABLE) && params->spp_sackdelay) {
2518*4882a593Smuzhiyun if (trans) {
2519*4882a593Smuzhiyun trans->sackdelay =
2520*4882a593Smuzhiyun msecs_to_jiffies(params->spp_sackdelay);
2521*4882a593Smuzhiyun } else if (asoc) {
2522*4882a593Smuzhiyun asoc->sackdelay =
2523*4882a593Smuzhiyun msecs_to_jiffies(params->spp_sackdelay);
2524*4882a593Smuzhiyun } else {
2525*4882a593Smuzhiyun sp->sackdelay = params->spp_sackdelay;
2526*4882a593Smuzhiyun }
2527*4882a593Smuzhiyun }
2528*4882a593Smuzhiyun
2529*4882a593Smuzhiyun if (sackdelay_change) {
2530*4882a593Smuzhiyun if (trans) {
2531*4882a593Smuzhiyun trans->param_flags =
2532*4882a593Smuzhiyun (trans->param_flags & ~SPP_SACKDELAY) |
2533*4882a593Smuzhiyun sackdelay_change;
2534*4882a593Smuzhiyun } else if (asoc) {
2535*4882a593Smuzhiyun asoc->param_flags =
2536*4882a593Smuzhiyun (asoc->param_flags & ~SPP_SACKDELAY) |
2537*4882a593Smuzhiyun sackdelay_change;
2538*4882a593Smuzhiyun } else {
2539*4882a593Smuzhiyun sp->param_flags =
2540*4882a593Smuzhiyun (sp->param_flags & ~SPP_SACKDELAY) |
2541*4882a593Smuzhiyun sackdelay_change;
2542*4882a593Smuzhiyun }
2543*4882a593Smuzhiyun }
2544*4882a593Smuzhiyun
2545*4882a593Smuzhiyun /* Note that a value of zero indicates the current setting should be
2546*4882a593Smuzhiyun left unchanged.
2547*4882a593Smuzhiyun */
2548*4882a593Smuzhiyun if (params->spp_pathmaxrxt) {
2549*4882a593Smuzhiyun if (trans) {
2550*4882a593Smuzhiyun trans->pathmaxrxt = params->spp_pathmaxrxt;
2551*4882a593Smuzhiyun } else if (asoc) {
2552*4882a593Smuzhiyun asoc->pathmaxrxt = params->spp_pathmaxrxt;
2553*4882a593Smuzhiyun } else {
2554*4882a593Smuzhiyun sp->pathmaxrxt = params->spp_pathmaxrxt;
2555*4882a593Smuzhiyun }
2556*4882a593Smuzhiyun }
2557*4882a593Smuzhiyun
2558*4882a593Smuzhiyun if (params->spp_flags & SPP_IPV6_FLOWLABEL) {
2559*4882a593Smuzhiyun if (trans) {
2560*4882a593Smuzhiyun if (trans->ipaddr.sa.sa_family == AF_INET6) {
2561*4882a593Smuzhiyun trans->flowlabel = params->spp_ipv6_flowlabel &
2562*4882a593Smuzhiyun SCTP_FLOWLABEL_VAL_MASK;
2563*4882a593Smuzhiyun trans->flowlabel |= SCTP_FLOWLABEL_SET_MASK;
2564*4882a593Smuzhiyun }
2565*4882a593Smuzhiyun } else if (asoc) {
2566*4882a593Smuzhiyun struct sctp_transport *t;
2567*4882a593Smuzhiyun
2568*4882a593Smuzhiyun list_for_each_entry(t, &asoc->peer.transport_addr_list,
2569*4882a593Smuzhiyun transports) {
2570*4882a593Smuzhiyun if (t->ipaddr.sa.sa_family != AF_INET6)
2571*4882a593Smuzhiyun continue;
2572*4882a593Smuzhiyun t->flowlabel = params->spp_ipv6_flowlabel &
2573*4882a593Smuzhiyun SCTP_FLOWLABEL_VAL_MASK;
2574*4882a593Smuzhiyun t->flowlabel |= SCTP_FLOWLABEL_SET_MASK;
2575*4882a593Smuzhiyun }
2576*4882a593Smuzhiyun asoc->flowlabel = params->spp_ipv6_flowlabel &
2577*4882a593Smuzhiyun SCTP_FLOWLABEL_VAL_MASK;
2578*4882a593Smuzhiyun asoc->flowlabel |= SCTP_FLOWLABEL_SET_MASK;
2579*4882a593Smuzhiyun } else if (sctp_opt2sk(sp)->sk_family == AF_INET6) {
2580*4882a593Smuzhiyun sp->flowlabel = params->spp_ipv6_flowlabel &
2581*4882a593Smuzhiyun SCTP_FLOWLABEL_VAL_MASK;
2582*4882a593Smuzhiyun sp->flowlabel |= SCTP_FLOWLABEL_SET_MASK;
2583*4882a593Smuzhiyun }
2584*4882a593Smuzhiyun }
2585*4882a593Smuzhiyun
2586*4882a593Smuzhiyun if (params->spp_flags & SPP_DSCP) {
2587*4882a593Smuzhiyun if (trans) {
2588*4882a593Smuzhiyun trans->dscp = params->spp_dscp & SCTP_DSCP_VAL_MASK;
2589*4882a593Smuzhiyun trans->dscp |= SCTP_DSCP_SET_MASK;
2590*4882a593Smuzhiyun } else if (asoc) {
2591*4882a593Smuzhiyun struct sctp_transport *t;
2592*4882a593Smuzhiyun
2593*4882a593Smuzhiyun list_for_each_entry(t, &asoc->peer.transport_addr_list,
2594*4882a593Smuzhiyun transports) {
2595*4882a593Smuzhiyun t->dscp = params->spp_dscp &
2596*4882a593Smuzhiyun SCTP_DSCP_VAL_MASK;
2597*4882a593Smuzhiyun t->dscp |= SCTP_DSCP_SET_MASK;
2598*4882a593Smuzhiyun }
2599*4882a593Smuzhiyun asoc->dscp = params->spp_dscp & SCTP_DSCP_VAL_MASK;
2600*4882a593Smuzhiyun asoc->dscp |= SCTP_DSCP_SET_MASK;
2601*4882a593Smuzhiyun } else {
2602*4882a593Smuzhiyun sp->dscp = params->spp_dscp & SCTP_DSCP_VAL_MASK;
2603*4882a593Smuzhiyun sp->dscp |= SCTP_DSCP_SET_MASK;
2604*4882a593Smuzhiyun }
2605*4882a593Smuzhiyun }
2606*4882a593Smuzhiyun
2607*4882a593Smuzhiyun return 0;
2608*4882a593Smuzhiyun }
2609*4882a593Smuzhiyun
sctp_setsockopt_peer_addr_params(struct sock * sk,struct sctp_paddrparams * params,unsigned int optlen)2610*4882a593Smuzhiyun static int sctp_setsockopt_peer_addr_params(struct sock *sk,
2611*4882a593Smuzhiyun struct sctp_paddrparams *params,
2612*4882a593Smuzhiyun unsigned int optlen)
2613*4882a593Smuzhiyun {
2614*4882a593Smuzhiyun struct sctp_transport *trans = NULL;
2615*4882a593Smuzhiyun struct sctp_association *asoc = NULL;
2616*4882a593Smuzhiyun struct sctp_sock *sp = sctp_sk(sk);
2617*4882a593Smuzhiyun int error;
2618*4882a593Smuzhiyun int hb_change, pmtud_change, sackdelay_change;
2619*4882a593Smuzhiyun
2620*4882a593Smuzhiyun if (optlen == ALIGN(offsetof(struct sctp_paddrparams,
2621*4882a593Smuzhiyun spp_ipv6_flowlabel), 4)) {
2622*4882a593Smuzhiyun if (params->spp_flags & (SPP_DSCP | SPP_IPV6_FLOWLABEL))
2623*4882a593Smuzhiyun return -EINVAL;
2624*4882a593Smuzhiyun } else if (optlen != sizeof(*params)) {
2625*4882a593Smuzhiyun return -EINVAL;
2626*4882a593Smuzhiyun }
2627*4882a593Smuzhiyun
2628*4882a593Smuzhiyun /* Validate flags and value parameters. */
2629*4882a593Smuzhiyun hb_change = params->spp_flags & SPP_HB;
2630*4882a593Smuzhiyun pmtud_change = params->spp_flags & SPP_PMTUD;
2631*4882a593Smuzhiyun sackdelay_change = params->spp_flags & SPP_SACKDELAY;
2632*4882a593Smuzhiyun
2633*4882a593Smuzhiyun if (hb_change == SPP_HB ||
2634*4882a593Smuzhiyun pmtud_change == SPP_PMTUD ||
2635*4882a593Smuzhiyun sackdelay_change == SPP_SACKDELAY ||
2636*4882a593Smuzhiyun params->spp_sackdelay > 500 ||
2637*4882a593Smuzhiyun (params->spp_pathmtu &&
2638*4882a593Smuzhiyun params->spp_pathmtu < SCTP_DEFAULT_MINSEGMENT))
2639*4882a593Smuzhiyun return -EINVAL;
2640*4882a593Smuzhiyun
2641*4882a593Smuzhiyun /* If an address other than INADDR_ANY is specified, and
2642*4882a593Smuzhiyun * no transport is found, then the request is invalid.
2643*4882a593Smuzhiyun */
2644*4882a593Smuzhiyun if (!sctp_is_any(sk, (union sctp_addr *)¶ms->spp_address)) {
2645*4882a593Smuzhiyun trans = sctp_addr_id2transport(sk, ¶ms->spp_address,
2646*4882a593Smuzhiyun params->spp_assoc_id);
2647*4882a593Smuzhiyun if (!trans)
2648*4882a593Smuzhiyun return -EINVAL;
2649*4882a593Smuzhiyun }
2650*4882a593Smuzhiyun
2651*4882a593Smuzhiyun /* Get association, if assoc_id != SCTP_FUTURE_ASSOC and the
2652*4882a593Smuzhiyun * socket is a one to many style socket, and an association
2653*4882a593Smuzhiyun * was not found, then the id was invalid.
2654*4882a593Smuzhiyun */
2655*4882a593Smuzhiyun asoc = sctp_id2assoc(sk, params->spp_assoc_id);
2656*4882a593Smuzhiyun if (!asoc && params->spp_assoc_id != SCTP_FUTURE_ASSOC &&
2657*4882a593Smuzhiyun sctp_style(sk, UDP))
2658*4882a593Smuzhiyun return -EINVAL;
2659*4882a593Smuzhiyun
2660*4882a593Smuzhiyun /* Heartbeat demand can only be sent on a transport or
2661*4882a593Smuzhiyun * association, but not a socket.
2662*4882a593Smuzhiyun */
2663*4882a593Smuzhiyun if (params->spp_flags & SPP_HB_DEMAND && !trans && !asoc)
2664*4882a593Smuzhiyun return -EINVAL;
2665*4882a593Smuzhiyun
2666*4882a593Smuzhiyun /* Process parameters. */
2667*4882a593Smuzhiyun error = sctp_apply_peer_addr_params(params, trans, asoc, sp,
2668*4882a593Smuzhiyun hb_change, pmtud_change,
2669*4882a593Smuzhiyun sackdelay_change);
2670*4882a593Smuzhiyun
2671*4882a593Smuzhiyun if (error)
2672*4882a593Smuzhiyun return error;
2673*4882a593Smuzhiyun
2674*4882a593Smuzhiyun /* If changes are for association, also apply parameters to each
2675*4882a593Smuzhiyun * transport.
2676*4882a593Smuzhiyun */
2677*4882a593Smuzhiyun if (!trans && asoc) {
2678*4882a593Smuzhiyun list_for_each_entry(trans, &asoc->peer.transport_addr_list,
2679*4882a593Smuzhiyun transports) {
2680*4882a593Smuzhiyun sctp_apply_peer_addr_params(params, trans, asoc, sp,
2681*4882a593Smuzhiyun hb_change, pmtud_change,
2682*4882a593Smuzhiyun sackdelay_change);
2683*4882a593Smuzhiyun }
2684*4882a593Smuzhiyun }
2685*4882a593Smuzhiyun
2686*4882a593Smuzhiyun return 0;
2687*4882a593Smuzhiyun }
2688*4882a593Smuzhiyun
sctp_spp_sackdelay_enable(__u32 param_flags)2689*4882a593Smuzhiyun static inline __u32 sctp_spp_sackdelay_enable(__u32 param_flags)
2690*4882a593Smuzhiyun {
2691*4882a593Smuzhiyun return (param_flags & ~SPP_SACKDELAY) | SPP_SACKDELAY_ENABLE;
2692*4882a593Smuzhiyun }
2693*4882a593Smuzhiyun
sctp_spp_sackdelay_disable(__u32 param_flags)2694*4882a593Smuzhiyun static inline __u32 sctp_spp_sackdelay_disable(__u32 param_flags)
2695*4882a593Smuzhiyun {
2696*4882a593Smuzhiyun return (param_flags & ~SPP_SACKDELAY) | SPP_SACKDELAY_DISABLE;
2697*4882a593Smuzhiyun }
2698*4882a593Smuzhiyun
sctp_apply_asoc_delayed_ack(struct sctp_sack_info * params,struct sctp_association * asoc)2699*4882a593Smuzhiyun static void sctp_apply_asoc_delayed_ack(struct sctp_sack_info *params,
2700*4882a593Smuzhiyun struct sctp_association *asoc)
2701*4882a593Smuzhiyun {
2702*4882a593Smuzhiyun struct sctp_transport *trans;
2703*4882a593Smuzhiyun
2704*4882a593Smuzhiyun if (params->sack_delay) {
2705*4882a593Smuzhiyun asoc->sackdelay = msecs_to_jiffies(params->sack_delay);
2706*4882a593Smuzhiyun asoc->param_flags =
2707*4882a593Smuzhiyun sctp_spp_sackdelay_enable(asoc->param_flags);
2708*4882a593Smuzhiyun }
2709*4882a593Smuzhiyun if (params->sack_freq == 1) {
2710*4882a593Smuzhiyun asoc->param_flags =
2711*4882a593Smuzhiyun sctp_spp_sackdelay_disable(asoc->param_flags);
2712*4882a593Smuzhiyun } else if (params->sack_freq > 1) {
2713*4882a593Smuzhiyun asoc->sackfreq = params->sack_freq;
2714*4882a593Smuzhiyun asoc->param_flags =
2715*4882a593Smuzhiyun sctp_spp_sackdelay_enable(asoc->param_flags);
2716*4882a593Smuzhiyun }
2717*4882a593Smuzhiyun
2718*4882a593Smuzhiyun list_for_each_entry(trans, &asoc->peer.transport_addr_list,
2719*4882a593Smuzhiyun transports) {
2720*4882a593Smuzhiyun if (params->sack_delay) {
2721*4882a593Smuzhiyun trans->sackdelay = msecs_to_jiffies(params->sack_delay);
2722*4882a593Smuzhiyun trans->param_flags =
2723*4882a593Smuzhiyun sctp_spp_sackdelay_enable(trans->param_flags);
2724*4882a593Smuzhiyun }
2725*4882a593Smuzhiyun if (params->sack_freq == 1) {
2726*4882a593Smuzhiyun trans->param_flags =
2727*4882a593Smuzhiyun sctp_spp_sackdelay_disable(trans->param_flags);
2728*4882a593Smuzhiyun } else if (params->sack_freq > 1) {
2729*4882a593Smuzhiyun trans->sackfreq = params->sack_freq;
2730*4882a593Smuzhiyun trans->param_flags =
2731*4882a593Smuzhiyun sctp_spp_sackdelay_enable(trans->param_flags);
2732*4882a593Smuzhiyun }
2733*4882a593Smuzhiyun }
2734*4882a593Smuzhiyun }
2735*4882a593Smuzhiyun
2736*4882a593Smuzhiyun /*
2737*4882a593Smuzhiyun * 7.1.23. Get or set delayed ack timer (SCTP_DELAYED_SACK)
2738*4882a593Smuzhiyun *
2739*4882a593Smuzhiyun * This option will effect the way delayed acks are performed. This
2740*4882a593Smuzhiyun * option allows you to get or set the delayed ack time, in
2741*4882a593Smuzhiyun * milliseconds. It also allows changing the delayed ack frequency.
2742*4882a593Smuzhiyun * Changing the frequency to 1 disables the delayed sack algorithm. If
2743*4882a593Smuzhiyun * the assoc_id is 0, then this sets or gets the endpoints default
2744*4882a593Smuzhiyun * values. If the assoc_id field is non-zero, then the set or get
2745*4882a593Smuzhiyun * effects the specified association for the one to many model (the
2746*4882a593Smuzhiyun * assoc_id field is ignored by the one to one model). Note that if
2747*4882a593Smuzhiyun * sack_delay or sack_freq are 0 when setting this option, then the
2748*4882a593Smuzhiyun * current values will remain unchanged.
2749*4882a593Smuzhiyun *
2750*4882a593Smuzhiyun * struct sctp_sack_info {
2751*4882a593Smuzhiyun * sctp_assoc_t sack_assoc_id;
2752*4882a593Smuzhiyun * uint32_t sack_delay;
2753*4882a593Smuzhiyun * uint32_t sack_freq;
2754*4882a593Smuzhiyun * };
2755*4882a593Smuzhiyun *
2756*4882a593Smuzhiyun * sack_assoc_id - This parameter, indicates which association the user
2757*4882a593Smuzhiyun * is performing an action upon. Note that if this field's value is
2758*4882a593Smuzhiyun * zero then the endpoints default value is changed (effecting future
2759*4882a593Smuzhiyun * associations only).
2760*4882a593Smuzhiyun *
2761*4882a593Smuzhiyun * sack_delay - This parameter contains the number of milliseconds that
2762*4882a593Smuzhiyun * the user is requesting the delayed ACK timer be set to. Note that
2763*4882a593Smuzhiyun * this value is defined in the standard to be between 200 and 500
2764*4882a593Smuzhiyun * milliseconds.
2765*4882a593Smuzhiyun *
2766*4882a593Smuzhiyun * sack_freq - This parameter contains the number of packets that must
2767*4882a593Smuzhiyun * be received before a sack is sent without waiting for the delay
2768*4882a593Smuzhiyun * timer to expire. The default value for this is 2, setting this
2769*4882a593Smuzhiyun * value to 1 will disable the delayed sack algorithm.
2770*4882a593Smuzhiyun */
__sctp_setsockopt_delayed_ack(struct sock * sk,struct sctp_sack_info * params)2771*4882a593Smuzhiyun static int __sctp_setsockopt_delayed_ack(struct sock *sk,
2772*4882a593Smuzhiyun struct sctp_sack_info *params)
2773*4882a593Smuzhiyun {
2774*4882a593Smuzhiyun struct sctp_sock *sp = sctp_sk(sk);
2775*4882a593Smuzhiyun struct sctp_association *asoc;
2776*4882a593Smuzhiyun
2777*4882a593Smuzhiyun /* Validate value parameter. */
2778*4882a593Smuzhiyun if (params->sack_delay > 500)
2779*4882a593Smuzhiyun return -EINVAL;
2780*4882a593Smuzhiyun
2781*4882a593Smuzhiyun /* Get association, if sack_assoc_id != SCTP_FUTURE_ASSOC and the
2782*4882a593Smuzhiyun * socket is a one to many style socket, and an association
2783*4882a593Smuzhiyun * was not found, then the id was invalid.
2784*4882a593Smuzhiyun */
2785*4882a593Smuzhiyun asoc = sctp_id2assoc(sk, params->sack_assoc_id);
2786*4882a593Smuzhiyun if (!asoc && params->sack_assoc_id > SCTP_ALL_ASSOC &&
2787*4882a593Smuzhiyun sctp_style(sk, UDP))
2788*4882a593Smuzhiyun return -EINVAL;
2789*4882a593Smuzhiyun
2790*4882a593Smuzhiyun if (asoc) {
2791*4882a593Smuzhiyun sctp_apply_asoc_delayed_ack(params, asoc);
2792*4882a593Smuzhiyun
2793*4882a593Smuzhiyun return 0;
2794*4882a593Smuzhiyun }
2795*4882a593Smuzhiyun
2796*4882a593Smuzhiyun if (sctp_style(sk, TCP))
2797*4882a593Smuzhiyun params->sack_assoc_id = SCTP_FUTURE_ASSOC;
2798*4882a593Smuzhiyun
2799*4882a593Smuzhiyun if (params->sack_assoc_id == SCTP_FUTURE_ASSOC ||
2800*4882a593Smuzhiyun params->sack_assoc_id == SCTP_ALL_ASSOC) {
2801*4882a593Smuzhiyun if (params->sack_delay) {
2802*4882a593Smuzhiyun sp->sackdelay = params->sack_delay;
2803*4882a593Smuzhiyun sp->param_flags =
2804*4882a593Smuzhiyun sctp_spp_sackdelay_enable(sp->param_flags);
2805*4882a593Smuzhiyun }
2806*4882a593Smuzhiyun if (params->sack_freq == 1) {
2807*4882a593Smuzhiyun sp->param_flags =
2808*4882a593Smuzhiyun sctp_spp_sackdelay_disable(sp->param_flags);
2809*4882a593Smuzhiyun } else if (params->sack_freq > 1) {
2810*4882a593Smuzhiyun sp->sackfreq = params->sack_freq;
2811*4882a593Smuzhiyun sp->param_flags =
2812*4882a593Smuzhiyun sctp_spp_sackdelay_enable(sp->param_flags);
2813*4882a593Smuzhiyun }
2814*4882a593Smuzhiyun }
2815*4882a593Smuzhiyun
2816*4882a593Smuzhiyun if (params->sack_assoc_id == SCTP_CURRENT_ASSOC ||
2817*4882a593Smuzhiyun params->sack_assoc_id == SCTP_ALL_ASSOC)
2818*4882a593Smuzhiyun list_for_each_entry(asoc, &sp->ep->asocs, asocs)
2819*4882a593Smuzhiyun sctp_apply_asoc_delayed_ack(params, asoc);
2820*4882a593Smuzhiyun
2821*4882a593Smuzhiyun return 0;
2822*4882a593Smuzhiyun }
2823*4882a593Smuzhiyun
sctp_setsockopt_delayed_ack(struct sock * sk,struct sctp_sack_info * params,unsigned int optlen)2824*4882a593Smuzhiyun static int sctp_setsockopt_delayed_ack(struct sock *sk,
2825*4882a593Smuzhiyun struct sctp_sack_info *params,
2826*4882a593Smuzhiyun unsigned int optlen)
2827*4882a593Smuzhiyun {
2828*4882a593Smuzhiyun if (optlen == sizeof(struct sctp_assoc_value)) {
2829*4882a593Smuzhiyun struct sctp_assoc_value *v = (struct sctp_assoc_value *)params;
2830*4882a593Smuzhiyun struct sctp_sack_info p;
2831*4882a593Smuzhiyun
2832*4882a593Smuzhiyun pr_warn_ratelimited(DEPRECATED
2833*4882a593Smuzhiyun "%s (pid %d) "
2834*4882a593Smuzhiyun "Use of struct sctp_assoc_value in delayed_ack socket option.\n"
2835*4882a593Smuzhiyun "Use struct sctp_sack_info instead\n",
2836*4882a593Smuzhiyun current->comm, task_pid_nr(current));
2837*4882a593Smuzhiyun
2838*4882a593Smuzhiyun p.sack_assoc_id = v->assoc_id;
2839*4882a593Smuzhiyun p.sack_delay = v->assoc_value;
2840*4882a593Smuzhiyun p.sack_freq = v->assoc_value ? 0 : 1;
2841*4882a593Smuzhiyun return __sctp_setsockopt_delayed_ack(sk, &p);
2842*4882a593Smuzhiyun }
2843*4882a593Smuzhiyun
2844*4882a593Smuzhiyun if (optlen != sizeof(struct sctp_sack_info))
2845*4882a593Smuzhiyun return -EINVAL;
2846*4882a593Smuzhiyun if (params->sack_delay == 0 && params->sack_freq == 0)
2847*4882a593Smuzhiyun return 0;
2848*4882a593Smuzhiyun return __sctp_setsockopt_delayed_ack(sk, params);
2849*4882a593Smuzhiyun }
2850*4882a593Smuzhiyun
2851*4882a593Smuzhiyun /* 7.1.3 Initialization Parameters (SCTP_INITMSG)
2852*4882a593Smuzhiyun *
2853*4882a593Smuzhiyun * Applications can specify protocol parameters for the default association
2854*4882a593Smuzhiyun * initialization. The option name argument to setsockopt() and getsockopt()
2855*4882a593Smuzhiyun * is SCTP_INITMSG.
2856*4882a593Smuzhiyun *
2857*4882a593Smuzhiyun * Setting initialization parameters is effective only on an unconnected
2858*4882a593Smuzhiyun * socket (for UDP-style sockets only future associations are effected
2859*4882a593Smuzhiyun * by the change). With TCP-style sockets, this option is inherited by
2860*4882a593Smuzhiyun * sockets derived from a listener socket.
2861*4882a593Smuzhiyun */
sctp_setsockopt_initmsg(struct sock * sk,struct sctp_initmsg * sinit,unsigned int optlen)2862*4882a593Smuzhiyun static int sctp_setsockopt_initmsg(struct sock *sk, struct sctp_initmsg *sinit,
2863*4882a593Smuzhiyun unsigned int optlen)
2864*4882a593Smuzhiyun {
2865*4882a593Smuzhiyun struct sctp_sock *sp = sctp_sk(sk);
2866*4882a593Smuzhiyun
2867*4882a593Smuzhiyun if (optlen != sizeof(struct sctp_initmsg))
2868*4882a593Smuzhiyun return -EINVAL;
2869*4882a593Smuzhiyun
2870*4882a593Smuzhiyun if (sinit->sinit_num_ostreams)
2871*4882a593Smuzhiyun sp->initmsg.sinit_num_ostreams = sinit->sinit_num_ostreams;
2872*4882a593Smuzhiyun if (sinit->sinit_max_instreams)
2873*4882a593Smuzhiyun sp->initmsg.sinit_max_instreams = sinit->sinit_max_instreams;
2874*4882a593Smuzhiyun if (sinit->sinit_max_attempts)
2875*4882a593Smuzhiyun sp->initmsg.sinit_max_attempts = sinit->sinit_max_attempts;
2876*4882a593Smuzhiyun if (sinit->sinit_max_init_timeo)
2877*4882a593Smuzhiyun sp->initmsg.sinit_max_init_timeo = sinit->sinit_max_init_timeo;
2878*4882a593Smuzhiyun
2879*4882a593Smuzhiyun return 0;
2880*4882a593Smuzhiyun }
2881*4882a593Smuzhiyun
2882*4882a593Smuzhiyun /*
2883*4882a593Smuzhiyun * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
2884*4882a593Smuzhiyun *
2885*4882a593Smuzhiyun * Applications that wish to use the sendto() system call may wish to
2886*4882a593Smuzhiyun * specify a default set of parameters that would normally be supplied
2887*4882a593Smuzhiyun * through the inclusion of ancillary data. This socket option allows
2888*4882a593Smuzhiyun * such an application to set the default sctp_sndrcvinfo structure.
2889*4882a593Smuzhiyun * The application that wishes to use this socket option simply passes
2890*4882a593Smuzhiyun * in to this call the sctp_sndrcvinfo structure defined in Section
2891*4882a593Smuzhiyun * 5.2.2) The input parameters accepted by this call include
2892*4882a593Smuzhiyun * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
2893*4882a593Smuzhiyun * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
2894*4882a593Smuzhiyun * to this call if the caller is using the UDP model.
2895*4882a593Smuzhiyun */
sctp_setsockopt_default_send_param(struct sock * sk,struct sctp_sndrcvinfo * info,unsigned int optlen)2896*4882a593Smuzhiyun static int sctp_setsockopt_default_send_param(struct sock *sk,
2897*4882a593Smuzhiyun struct sctp_sndrcvinfo *info,
2898*4882a593Smuzhiyun unsigned int optlen)
2899*4882a593Smuzhiyun {
2900*4882a593Smuzhiyun struct sctp_sock *sp = sctp_sk(sk);
2901*4882a593Smuzhiyun struct sctp_association *asoc;
2902*4882a593Smuzhiyun
2903*4882a593Smuzhiyun if (optlen != sizeof(*info))
2904*4882a593Smuzhiyun return -EINVAL;
2905*4882a593Smuzhiyun if (info->sinfo_flags &
2906*4882a593Smuzhiyun ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
2907*4882a593Smuzhiyun SCTP_ABORT | SCTP_EOF))
2908*4882a593Smuzhiyun return -EINVAL;
2909*4882a593Smuzhiyun
2910*4882a593Smuzhiyun asoc = sctp_id2assoc(sk, info->sinfo_assoc_id);
2911*4882a593Smuzhiyun if (!asoc && info->sinfo_assoc_id > SCTP_ALL_ASSOC &&
2912*4882a593Smuzhiyun sctp_style(sk, UDP))
2913*4882a593Smuzhiyun return -EINVAL;
2914*4882a593Smuzhiyun
2915*4882a593Smuzhiyun if (asoc) {
2916*4882a593Smuzhiyun asoc->default_stream = info->sinfo_stream;
2917*4882a593Smuzhiyun asoc->default_flags = info->sinfo_flags;
2918*4882a593Smuzhiyun asoc->default_ppid = info->sinfo_ppid;
2919*4882a593Smuzhiyun asoc->default_context = info->sinfo_context;
2920*4882a593Smuzhiyun asoc->default_timetolive = info->sinfo_timetolive;
2921*4882a593Smuzhiyun
2922*4882a593Smuzhiyun return 0;
2923*4882a593Smuzhiyun }
2924*4882a593Smuzhiyun
2925*4882a593Smuzhiyun if (sctp_style(sk, TCP))
2926*4882a593Smuzhiyun info->sinfo_assoc_id = SCTP_FUTURE_ASSOC;
2927*4882a593Smuzhiyun
2928*4882a593Smuzhiyun if (info->sinfo_assoc_id == SCTP_FUTURE_ASSOC ||
2929*4882a593Smuzhiyun info->sinfo_assoc_id == SCTP_ALL_ASSOC) {
2930*4882a593Smuzhiyun sp->default_stream = info->sinfo_stream;
2931*4882a593Smuzhiyun sp->default_flags = info->sinfo_flags;
2932*4882a593Smuzhiyun sp->default_ppid = info->sinfo_ppid;
2933*4882a593Smuzhiyun sp->default_context = info->sinfo_context;
2934*4882a593Smuzhiyun sp->default_timetolive = info->sinfo_timetolive;
2935*4882a593Smuzhiyun }
2936*4882a593Smuzhiyun
2937*4882a593Smuzhiyun if (info->sinfo_assoc_id == SCTP_CURRENT_ASSOC ||
2938*4882a593Smuzhiyun info->sinfo_assoc_id == SCTP_ALL_ASSOC) {
2939*4882a593Smuzhiyun list_for_each_entry(asoc, &sp->ep->asocs, asocs) {
2940*4882a593Smuzhiyun asoc->default_stream = info->sinfo_stream;
2941*4882a593Smuzhiyun asoc->default_flags = info->sinfo_flags;
2942*4882a593Smuzhiyun asoc->default_ppid = info->sinfo_ppid;
2943*4882a593Smuzhiyun asoc->default_context = info->sinfo_context;
2944*4882a593Smuzhiyun asoc->default_timetolive = info->sinfo_timetolive;
2945*4882a593Smuzhiyun }
2946*4882a593Smuzhiyun }
2947*4882a593Smuzhiyun
2948*4882a593Smuzhiyun return 0;
2949*4882a593Smuzhiyun }
2950*4882a593Smuzhiyun
2951*4882a593Smuzhiyun /* RFC6458, Section 8.1.31. Set/get Default Send Parameters
2952*4882a593Smuzhiyun * (SCTP_DEFAULT_SNDINFO)
2953*4882a593Smuzhiyun */
sctp_setsockopt_default_sndinfo(struct sock * sk,struct sctp_sndinfo * info,unsigned int optlen)2954*4882a593Smuzhiyun static int sctp_setsockopt_default_sndinfo(struct sock *sk,
2955*4882a593Smuzhiyun struct sctp_sndinfo *info,
2956*4882a593Smuzhiyun unsigned int optlen)
2957*4882a593Smuzhiyun {
2958*4882a593Smuzhiyun struct sctp_sock *sp = sctp_sk(sk);
2959*4882a593Smuzhiyun struct sctp_association *asoc;
2960*4882a593Smuzhiyun
2961*4882a593Smuzhiyun if (optlen != sizeof(*info))
2962*4882a593Smuzhiyun return -EINVAL;
2963*4882a593Smuzhiyun if (info->snd_flags &
2964*4882a593Smuzhiyun ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
2965*4882a593Smuzhiyun SCTP_ABORT | SCTP_EOF))
2966*4882a593Smuzhiyun return -EINVAL;
2967*4882a593Smuzhiyun
2968*4882a593Smuzhiyun asoc = sctp_id2assoc(sk, info->snd_assoc_id);
2969*4882a593Smuzhiyun if (!asoc && info->snd_assoc_id > SCTP_ALL_ASSOC &&
2970*4882a593Smuzhiyun sctp_style(sk, UDP))
2971*4882a593Smuzhiyun return -EINVAL;
2972*4882a593Smuzhiyun
2973*4882a593Smuzhiyun if (asoc) {
2974*4882a593Smuzhiyun asoc->default_stream = info->snd_sid;
2975*4882a593Smuzhiyun asoc->default_flags = info->snd_flags;
2976*4882a593Smuzhiyun asoc->default_ppid = info->snd_ppid;
2977*4882a593Smuzhiyun asoc->default_context = info->snd_context;
2978*4882a593Smuzhiyun
2979*4882a593Smuzhiyun return 0;
2980*4882a593Smuzhiyun }
2981*4882a593Smuzhiyun
2982*4882a593Smuzhiyun if (sctp_style(sk, TCP))
2983*4882a593Smuzhiyun info->snd_assoc_id = SCTP_FUTURE_ASSOC;
2984*4882a593Smuzhiyun
2985*4882a593Smuzhiyun if (info->snd_assoc_id == SCTP_FUTURE_ASSOC ||
2986*4882a593Smuzhiyun info->snd_assoc_id == SCTP_ALL_ASSOC) {
2987*4882a593Smuzhiyun sp->default_stream = info->snd_sid;
2988*4882a593Smuzhiyun sp->default_flags = info->snd_flags;
2989*4882a593Smuzhiyun sp->default_ppid = info->snd_ppid;
2990*4882a593Smuzhiyun sp->default_context = info->snd_context;
2991*4882a593Smuzhiyun }
2992*4882a593Smuzhiyun
2993*4882a593Smuzhiyun if (info->snd_assoc_id == SCTP_CURRENT_ASSOC ||
2994*4882a593Smuzhiyun info->snd_assoc_id == SCTP_ALL_ASSOC) {
2995*4882a593Smuzhiyun list_for_each_entry(asoc, &sp->ep->asocs, asocs) {
2996*4882a593Smuzhiyun asoc->default_stream = info->snd_sid;
2997*4882a593Smuzhiyun asoc->default_flags = info->snd_flags;
2998*4882a593Smuzhiyun asoc->default_ppid = info->snd_ppid;
2999*4882a593Smuzhiyun asoc->default_context = info->snd_context;
3000*4882a593Smuzhiyun }
3001*4882a593Smuzhiyun }
3002*4882a593Smuzhiyun
3003*4882a593Smuzhiyun return 0;
3004*4882a593Smuzhiyun }
3005*4882a593Smuzhiyun
3006*4882a593Smuzhiyun /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
3007*4882a593Smuzhiyun *
3008*4882a593Smuzhiyun * Requests that the local SCTP stack use the enclosed peer address as
3009*4882a593Smuzhiyun * the association primary. The enclosed address must be one of the
3010*4882a593Smuzhiyun * association peer's addresses.
3011*4882a593Smuzhiyun */
sctp_setsockopt_primary_addr(struct sock * sk,struct sctp_prim * prim,unsigned int optlen)3012*4882a593Smuzhiyun static int sctp_setsockopt_primary_addr(struct sock *sk, struct sctp_prim *prim,
3013*4882a593Smuzhiyun unsigned int optlen)
3014*4882a593Smuzhiyun {
3015*4882a593Smuzhiyun struct sctp_transport *trans;
3016*4882a593Smuzhiyun struct sctp_af *af;
3017*4882a593Smuzhiyun int err;
3018*4882a593Smuzhiyun
3019*4882a593Smuzhiyun if (optlen != sizeof(struct sctp_prim))
3020*4882a593Smuzhiyun return -EINVAL;
3021*4882a593Smuzhiyun
3022*4882a593Smuzhiyun /* Allow security module to validate address but need address len. */
3023*4882a593Smuzhiyun af = sctp_get_af_specific(prim->ssp_addr.ss_family);
3024*4882a593Smuzhiyun if (!af)
3025*4882a593Smuzhiyun return -EINVAL;
3026*4882a593Smuzhiyun
3027*4882a593Smuzhiyun err = security_sctp_bind_connect(sk, SCTP_PRIMARY_ADDR,
3028*4882a593Smuzhiyun (struct sockaddr *)&prim->ssp_addr,
3029*4882a593Smuzhiyun af->sockaddr_len);
3030*4882a593Smuzhiyun if (err)
3031*4882a593Smuzhiyun return err;
3032*4882a593Smuzhiyun
3033*4882a593Smuzhiyun trans = sctp_addr_id2transport(sk, &prim->ssp_addr, prim->ssp_assoc_id);
3034*4882a593Smuzhiyun if (!trans)
3035*4882a593Smuzhiyun return -EINVAL;
3036*4882a593Smuzhiyun
3037*4882a593Smuzhiyun sctp_assoc_set_primary(trans->asoc, trans);
3038*4882a593Smuzhiyun
3039*4882a593Smuzhiyun return 0;
3040*4882a593Smuzhiyun }
3041*4882a593Smuzhiyun
3042*4882a593Smuzhiyun /*
3043*4882a593Smuzhiyun * 7.1.5 SCTP_NODELAY
3044*4882a593Smuzhiyun *
3045*4882a593Smuzhiyun * Turn on/off any Nagle-like algorithm. This means that packets are
3046*4882a593Smuzhiyun * generally sent as soon as possible and no unnecessary delays are
3047*4882a593Smuzhiyun * introduced, at the cost of more packets in the network. Expects an
3048*4882a593Smuzhiyun * integer boolean flag.
3049*4882a593Smuzhiyun */
sctp_setsockopt_nodelay(struct sock * sk,int * val,unsigned int optlen)3050*4882a593Smuzhiyun static int sctp_setsockopt_nodelay(struct sock *sk, int *val,
3051*4882a593Smuzhiyun unsigned int optlen)
3052*4882a593Smuzhiyun {
3053*4882a593Smuzhiyun if (optlen < sizeof(int))
3054*4882a593Smuzhiyun return -EINVAL;
3055*4882a593Smuzhiyun sctp_sk(sk)->nodelay = (*val == 0) ? 0 : 1;
3056*4882a593Smuzhiyun return 0;
3057*4882a593Smuzhiyun }
3058*4882a593Smuzhiyun
3059*4882a593Smuzhiyun /*
3060*4882a593Smuzhiyun *
3061*4882a593Smuzhiyun * 7.1.1 SCTP_RTOINFO
3062*4882a593Smuzhiyun *
3063*4882a593Smuzhiyun * The protocol parameters used to initialize and bound retransmission
3064*4882a593Smuzhiyun * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
3065*4882a593Smuzhiyun * and modify these parameters.
3066*4882a593Smuzhiyun * All parameters are time values, in milliseconds. A value of 0, when
3067*4882a593Smuzhiyun * modifying the parameters, indicates that the current value should not
3068*4882a593Smuzhiyun * be changed.
3069*4882a593Smuzhiyun *
3070*4882a593Smuzhiyun */
sctp_setsockopt_rtoinfo(struct sock * sk,struct sctp_rtoinfo * rtoinfo,unsigned int optlen)3071*4882a593Smuzhiyun static int sctp_setsockopt_rtoinfo(struct sock *sk,
3072*4882a593Smuzhiyun struct sctp_rtoinfo *rtoinfo,
3073*4882a593Smuzhiyun unsigned int optlen)
3074*4882a593Smuzhiyun {
3075*4882a593Smuzhiyun struct sctp_association *asoc;
3076*4882a593Smuzhiyun unsigned long rto_min, rto_max;
3077*4882a593Smuzhiyun struct sctp_sock *sp = sctp_sk(sk);
3078*4882a593Smuzhiyun
3079*4882a593Smuzhiyun if (optlen != sizeof (struct sctp_rtoinfo))
3080*4882a593Smuzhiyun return -EINVAL;
3081*4882a593Smuzhiyun
3082*4882a593Smuzhiyun asoc = sctp_id2assoc(sk, rtoinfo->srto_assoc_id);
3083*4882a593Smuzhiyun
3084*4882a593Smuzhiyun /* Set the values to the specific association */
3085*4882a593Smuzhiyun if (!asoc && rtoinfo->srto_assoc_id != SCTP_FUTURE_ASSOC &&
3086*4882a593Smuzhiyun sctp_style(sk, UDP))
3087*4882a593Smuzhiyun return -EINVAL;
3088*4882a593Smuzhiyun
3089*4882a593Smuzhiyun rto_max = rtoinfo->srto_max;
3090*4882a593Smuzhiyun rto_min = rtoinfo->srto_min;
3091*4882a593Smuzhiyun
3092*4882a593Smuzhiyun if (rto_max)
3093*4882a593Smuzhiyun rto_max = asoc ? msecs_to_jiffies(rto_max) : rto_max;
3094*4882a593Smuzhiyun else
3095*4882a593Smuzhiyun rto_max = asoc ? asoc->rto_max : sp->rtoinfo.srto_max;
3096*4882a593Smuzhiyun
3097*4882a593Smuzhiyun if (rto_min)
3098*4882a593Smuzhiyun rto_min = asoc ? msecs_to_jiffies(rto_min) : rto_min;
3099*4882a593Smuzhiyun else
3100*4882a593Smuzhiyun rto_min = asoc ? asoc->rto_min : sp->rtoinfo.srto_min;
3101*4882a593Smuzhiyun
3102*4882a593Smuzhiyun if (rto_min > rto_max)
3103*4882a593Smuzhiyun return -EINVAL;
3104*4882a593Smuzhiyun
3105*4882a593Smuzhiyun if (asoc) {
3106*4882a593Smuzhiyun if (rtoinfo->srto_initial != 0)
3107*4882a593Smuzhiyun asoc->rto_initial =
3108*4882a593Smuzhiyun msecs_to_jiffies(rtoinfo->srto_initial);
3109*4882a593Smuzhiyun asoc->rto_max = rto_max;
3110*4882a593Smuzhiyun asoc->rto_min = rto_min;
3111*4882a593Smuzhiyun } else {
3112*4882a593Smuzhiyun /* If there is no association or the association-id = 0
3113*4882a593Smuzhiyun * set the values to the endpoint.
3114*4882a593Smuzhiyun */
3115*4882a593Smuzhiyun if (rtoinfo->srto_initial != 0)
3116*4882a593Smuzhiyun sp->rtoinfo.srto_initial = rtoinfo->srto_initial;
3117*4882a593Smuzhiyun sp->rtoinfo.srto_max = rto_max;
3118*4882a593Smuzhiyun sp->rtoinfo.srto_min = rto_min;
3119*4882a593Smuzhiyun }
3120*4882a593Smuzhiyun
3121*4882a593Smuzhiyun return 0;
3122*4882a593Smuzhiyun }
3123*4882a593Smuzhiyun
3124*4882a593Smuzhiyun /*
3125*4882a593Smuzhiyun *
3126*4882a593Smuzhiyun * 7.1.2 SCTP_ASSOCINFO
3127*4882a593Smuzhiyun *
3128*4882a593Smuzhiyun * This option is used to tune the maximum retransmission attempts
3129*4882a593Smuzhiyun * of the association.
3130*4882a593Smuzhiyun * Returns an error if the new association retransmission value is
3131*4882a593Smuzhiyun * greater than the sum of the retransmission value of the peer.
3132*4882a593Smuzhiyun * See [SCTP] for more information.
3133*4882a593Smuzhiyun *
3134*4882a593Smuzhiyun */
sctp_setsockopt_associnfo(struct sock * sk,struct sctp_assocparams * assocparams,unsigned int optlen)3135*4882a593Smuzhiyun static int sctp_setsockopt_associnfo(struct sock *sk,
3136*4882a593Smuzhiyun struct sctp_assocparams *assocparams,
3137*4882a593Smuzhiyun unsigned int optlen)
3138*4882a593Smuzhiyun {
3139*4882a593Smuzhiyun
3140*4882a593Smuzhiyun struct sctp_association *asoc;
3141*4882a593Smuzhiyun
3142*4882a593Smuzhiyun if (optlen != sizeof(struct sctp_assocparams))
3143*4882a593Smuzhiyun return -EINVAL;
3144*4882a593Smuzhiyun
3145*4882a593Smuzhiyun asoc = sctp_id2assoc(sk, assocparams->sasoc_assoc_id);
3146*4882a593Smuzhiyun
3147*4882a593Smuzhiyun if (!asoc && assocparams->sasoc_assoc_id != SCTP_FUTURE_ASSOC &&
3148*4882a593Smuzhiyun sctp_style(sk, UDP))
3149*4882a593Smuzhiyun return -EINVAL;
3150*4882a593Smuzhiyun
3151*4882a593Smuzhiyun /* Set the values to the specific association */
3152*4882a593Smuzhiyun if (asoc) {
3153*4882a593Smuzhiyun if (assocparams->sasoc_asocmaxrxt != 0) {
3154*4882a593Smuzhiyun __u32 path_sum = 0;
3155*4882a593Smuzhiyun int paths = 0;
3156*4882a593Smuzhiyun struct sctp_transport *peer_addr;
3157*4882a593Smuzhiyun
3158*4882a593Smuzhiyun list_for_each_entry(peer_addr, &asoc->peer.transport_addr_list,
3159*4882a593Smuzhiyun transports) {
3160*4882a593Smuzhiyun path_sum += peer_addr->pathmaxrxt;
3161*4882a593Smuzhiyun paths++;
3162*4882a593Smuzhiyun }
3163*4882a593Smuzhiyun
3164*4882a593Smuzhiyun /* Only validate asocmaxrxt if we have more than
3165*4882a593Smuzhiyun * one path/transport. We do this because path
3166*4882a593Smuzhiyun * retransmissions are only counted when we have more
3167*4882a593Smuzhiyun * then one path.
3168*4882a593Smuzhiyun */
3169*4882a593Smuzhiyun if (paths > 1 &&
3170*4882a593Smuzhiyun assocparams->sasoc_asocmaxrxt > path_sum)
3171*4882a593Smuzhiyun return -EINVAL;
3172*4882a593Smuzhiyun
3173*4882a593Smuzhiyun asoc->max_retrans = assocparams->sasoc_asocmaxrxt;
3174*4882a593Smuzhiyun }
3175*4882a593Smuzhiyun
3176*4882a593Smuzhiyun if (assocparams->sasoc_cookie_life != 0)
3177*4882a593Smuzhiyun asoc->cookie_life =
3178*4882a593Smuzhiyun ms_to_ktime(assocparams->sasoc_cookie_life);
3179*4882a593Smuzhiyun } else {
3180*4882a593Smuzhiyun /* Set the values to the endpoint */
3181*4882a593Smuzhiyun struct sctp_sock *sp = sctp_sk(sk);
3182*4882a593Smuzhiyun
3183*4882a593Smuzhiyun if (assocparams->sasoc_asocmaxrxt != 0)
3184*4882a593Smuzhiyun sp->assocparams.sasoc_asocmaxrxt =
3185*4882a593Smuzhiyun assocparams->sasoc_asocmaxrxt;
3186*4882a593Smuzhiyun if (assocparams->sasoc_cookie_life != 0)
3187*4882a593Smuzhiyun sp->assocparams.sasoc_cookie_life =
3188*4882a593Smuzhiyun assocparams->sasoc_cookie_life;
3189*4882a593Smuzhiyun }
3190*4882a593Smuzhiyun return 0;
3191*4882a593Smuzhiyun }
3192*4882a593Smuzhiyun
3193*4882a593Smuzhiyun /*
3194*4882a593Smuzhiyun * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
3195*4882a593Smuzhiyun *
3196*4882a593Smuzhiyun * This socket option is a boolean flag which turns on or off mapped V4
3197*4882a593Smuzhiyun * addresses. If this option is turned on and the socket is type
3198*4882a593Smuzhiyun * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
3199*4882a593Smuzhiyun * If this option is turned off, then no mapping will be done of V4
3200*4882a593Smuzhiyun * addresses and a user will receive both PF_INET6 and PF_INET type
3201*4882a593Smuzhiyun * addresses on the socket.
3202*4882a593Smuzhiyun */
sctp_setsockopt_mappedv4(struct sock * sk,int * val,unsigned int optlen)3203*4882a593Smuzhiyun static int sctp_setsockopt_mappedv4(struct sock *sk, int *val,
3204*4882a593Smuzhiyun unsigned int optlen)
3205*4882a593Smuzhiyun {
3206*4882a593Smuzhiyun struct sctp_sock *sp = sctp_sk(sk);
3207*4882a593Smuzhiyun
3208*4882a593Smuzhiyun if (optlen < sizeof(int))
3209*4882a593Smuzhiyun return -EINVAL;
3210*4882a593Smuzhiyun if (*val)
3211*4882a593Smuzhiyun sp->v4mapped = 1;
3212*4882a593Smuzhiyun else
3213*4882a593Smuzhiyun sp->v4mapped = 0;
3214*4882a593Smuzhiyun
3215*4882a593Smuzhiyun return 0;
3216*4882a593Smuzhiyun }
3217*4882a593Smuzhiyun
3218*4882a593Smuzhiyun /*
3219*4882a593Smuzhiyun * 8.1.16. Get or Set the Maximum Fragmentation Size (SCTP_MAXSEG)
3220*4882a593Smuzhiyun * This option will get or set the maximum size to put in any outgoing
3221*4882a593Smuzhiyun * SCTP DATA chunk. If a message is larger than this size it will be
3222*4882a593Smuzhiyun * fragmented by SCTP into the specified size. Note that the underlying
3223*4882a593Smuzhiyun * SCTP implementation may fragment into smaller sized chunks when the
3224*4882a593Smuzhiyun * PMTU of the underlying association is smaller than the value set by
3225*4882a593Smuzhiyun * the user. The default value for this option is '0' which indicates
3226*4882a593Smuzhiyun * the user is NOT limiting fragmentation and only the PMTU will effect
3227*4882a593Smuzhiyun * SCTP's choice of DATA chunk size. Note also that values set larger
3228*4882a593Smuzhiyun * than the maximum size of an IP datagram will effectively let SCTP
3229*4882a593Smuzhiyun * control fragmentation (i.e. the same as setting this option to 0).
3230*4882a593Smuzhiyun *
3231*4882a593Smuzhiyun * The following structure is used to access and modify this parameter:
3232*4882a593Smuzhiyun *
3233*4882a593Smuzhiyun * struct sctp_assoc_value {
3234*4882a593Smuzhiyun * sctp_assoc_t assoc_id;
3235*4882a593Smuzhiyun * uint32_t assoc_value;
3236*4882a593Smuzhiyun * };
3237*4882a593Smuzhiyun *
3238*4882a593Smuzhiyun * assoc_id: This parameter is ignored for one-to-one style sockets.
3239*4882a593Smuzhiyun * For one-to-many style sockets this parameter indicates which
3240*4882a593Smuzhiyun * association the user is performing an action upon. Note that if
3241*4882a593Smuzhiyun * this field's value is zero then the endpoints default value is
3242*4882a593Smuzhiyun * changed (effecting future associations only).
3243*4882a593Smuzhiyun * assoc_value: This parameter specifies the maximum size in bytes.
3244*4882a593Smuzhiyun */
sctp_setsockopt_maxseg(struct sock * sk,struct sctp_assoc_value * params,unsigned int optlen)3245*4882a593Smuzhiyun static int sctp_setsockopt_maxseg(struct sock *sk,
3246*4882a593Smuzhiyun struct sctp_assoc_value *params,
3247*4882a593Smuzhiyun unsigned int optlen)
3248*4882a593Smuzhiyun {
3249*4882a593Smuzhiyun struct sctp_sock *sp = sctp_sk(sk);
3250*4882a593Smuzhiyun struct sctp_association *asoc;
3251*4882a593Smuzhiyun sctp_assoc_t assoc_id;
3252*4882a593Smuzhiyun int val;
3253*4882a593Smuzhiyun
3254*4882a593Smuzhiyun if (optlen == sizeof(int)) {
3255*4882a593Smuzhiyun pr_warn_ratelimited(DEPRECATED
3256*4882a593Smuzhiyun "%s (pid %d) "
3257*4882a593Smuzhiyun "Use of int in maxseg socket option.\n"
3258*4882a593Smuzhiyun "Use struct sctp_assoc_value instead\n",
3259*4882a593Smuzhiyun current->comm, task_pid_nr(current));
3260*4882a593Smuzhiyun assoc_id = SCTP_FUTURE_ASSOC;
3261*4882a593Smuzhiyun val = *(int *)params;
3262*4882a593Smuzhiyun } else if (optlen == sizeof(struct sctp_assoc_value)) {
3263*4882a593Smuzhiyun assoc_id = params->assoc_id;
3264*4882a593Smuzhiyun val = params->assoc_value;
3265*4882a593Smuzhiyun } else {
3266*4882a593Smuzhiyun return -EINVAL;
3267*4882a593Smuzhiyun }
3268*4882a593Smuzhiyun
3269*4882a593Smuzhiyun asoc = sctp_id2assoc(sk, assoc_id);
3270*4882a593Smuzhiyun if (!asoc && assoc_id != SCTP_FUTURE_ASSOC &&
3271*4882a593Smuzhiyun sctp_style(sk, UDP))
3272*4882a593Smuzhiyun return -EINVAL;
3273*4882a593Smuzhiyun
3274*4882a593Smuzhiyun if (val) {
3275*4882a593Smuzhiyun int min_len, max_len;
3276*4882a593Smuzhiyun __u16 datasize = asoc ? sctp_datachk_len(&asoc->stream) :
3277*4882a593Smuzhiyun sizeof(struct sctp_data_chunk);
3278*4882a593Smuzhiyun
3279*4882a593Smuzhiyun min_len = sctp_min_frag_point(sp, datasize);
3280*4882a593Smuzhiyun max_len = SCTP_MAX_CHUNK_LEN - datasize;
3281*4882a593Smuzhiyun
3282*4882a593Smuzhiyun if (val < min_len || val > max_len)
3283*4882a593Smuzhiyun return -EINVAL;
3284*4882a593Smuzhiyun }
3285*4882a593Smuzhiyun
3286*4882a593Smuzhiyun if (asoc) {
3287*4882a593Smuzhiyun asoc->user_frag = val;
3288*4882a593Smuzhiyun sctp_assoc_update_frag_point(asoc);
3289*4882a593Smuzhiyun } else {
3290*4882a593Smuzhiyun sp->user_frag = val;
3291*4882a593Smuzhiyun }
3292*4882a593Smuzhiyun
3293*4882a593Smuzhiyun return 0;
3294*4882a593Smuzhiyun }
3295*4882a593Smuzhiyun
3296*4882a593Smuzhiyun
3297*4882a593Smuzhiyun /*
3298*4882a593Smuzhiyun * 7.1.9 Set Peer Primary Address (SCTP_SET_PEER_PRIMARY_ADDR)
3299*4882a593Smuzhiyun *
3300*4882a593Smuzhiyun * Requests that the peer mark the enclosed address as the association
3301*4882a593Smuzhiyun * primary. The enclosed address must be one of the association's
3302*4882a593Smuzhiyun * locally bound addresses. The following structure is used to make a
3303*4882a593Smuzhiyun * set primary request:
3304*4882a593Smuzhiyun */
sctp_setsockopt_peer_primary_addr(struct sock * sk,struct sctp_setpeerprim * prim,unsigned int optlen)3305*4882a593Smuzhiyun static int sctp_setsockopt_peer_primary_addr(struct sock *sk,
3306*4882a593Smuzhiyun struct sctp_setpeerprim *prim,
3307*4882a593Smuzhiyun unsigned int optlen)
3308*4882a593Smuzhiyun {
3309*4882a593Smuzhiyun struct sctp_sock *sp;
3310*4882a593Smuzhiyun struct sctp_association *asoc = NULL;
3311*4882a593Smuzhiyun struct sctp_chunk *chunk;
3312*4882a593Smuzhiyun struct sctp_af *af;
3313*4882a593Smuzhiyun int err;
3314*4882a593Smuzhiyun
3315*4882a593Smuzhiyun sp = sctp_sk(sk);
3316*4882a593Smuzhiyun
3317*4882a593Smuzhiyun if (!sp->ep->asconf_enable)
3318*4882a593Smuzhiyun return -EPERM;
3319*4882a593Smuzhiyun
3320*4882a593Smuzhiyun if (optlen != sizeof(struct sctp_setpeerprim))
3321*4882a593Smuzhiyun return -EINVAL;
3322*4882a593Smuzhiyun
3323*4882a593Smuzhiyun asoc = sctp_id2assoc(sk, prim->sspp_assoc_id);
3324*4882a593Smuzhiyun if (!asoc)
3325*4882a593Smuzhiyun return -EINVAL;
3326*4882a593Smuzhiyun
3327*4882a593Smuzhiyun if (!asoc->peer.asconf_capable)
3328*4882a593Smuzhiyun return -EPERM;
3329*4882a593Smuzhiyun
3330*4882a593Smuzhiyun if (asoc->peer.addip_disabled_mask & SCTP_PARAM_SET_PRIMARY)
3331*4882a593Smuzhiyun return -EPERM;
3332*4882a593Smuzhiyun
3333*4882a593Smuzhiyun if (!sctp_state(asoc, ESTABLISHED))
3334*4882a593Smuzhiyun return -ENOTCONN;
3335*4882a593Smuzhiyun
3336*4882a593Smuzhiyun af = sctp_get_af_specific(prim->sspp_addr.ss_family);
3337*4882a593Smuzhiyun if (!af)
3338*4882a593Smuzhiyun return -EINVAL;
3339*4882a593Smuzhiyun
3340*4882a593Smuzhiyun if (!af->addr_valid((union sctp_addr *)&prim->sspp_addr, sp, NULL))
3341*4882a593Smuzhiyun return -EADDRNOTAVAIL;
3342*4882a593Smuzhiyun
3343*4882a593Smuzhiyun if (!sctp_assoc_lookup_laddr(asoc, (union sctp_addr *)&prim->sspp_addr))
3344*4882a593Smuzhiyun return -EADDRNOTAVAIL;
3345*4882a593Smuzhiyun
3346*4882a593Smuzhiyun /* Allow security module to validate address. */
3347*4882a593Smuzhiyun err = security_sctp_bind_connect(sk, SCTP_SET_PEER_PRIMARY_ADDR,
3348*4882a593Smuzhiyun (struct sockaddr *)&prim->sspp_addr,
3349*4882a593Smuzhiyun af->sockaddr_len);
3350*4882a593Smuzhiyun if (err)
3351*4882a593Smuzhiyun return err;
3352*4882a593Smuzhiyun
3353*4882a593Smuzhiyun /* Create an ASCONF chunk with SET_PRIMARY parameter */
3354*4882a593Smuzhiyun chunk = sctp_make_asconf_set_prim(asoc,
3355*4882a593Smuzhiyun (union sctp_addr *)&prim->sspp_addr);
3356*4882a593Smuzhiyun if (!chunk)
3357*4882a593Smuzhiyun return -ENOMEM;
3358*4882a593Smuzhiyun
3359*4882a593Smuzhiyun err = sctp_send_asconf(asoc, chunk);
3360*4882a593Smuzhiyun
3361*4882a593Smuzhiyun pr_debug("%s: we set peer primary addr primitively\n", __func__);
3362*4882a593Smuzhiyun
3363*4882a593Smuzhiyun return err;
3364*4882a593Smuzhiyun }
3365*4882a593Smuzhiyun
sctp_setsockopt_adaptation_layer(struct sock * sk,struct sctp_setadaptation * adapt,unsigned int optlen)3366*4882a593Smuzhiyun static int sctp_setsockopt_adaptation_layer(struct sock *sk,
3367*4882a593Smuzhiyun struct sctp_setadaptation *adapt,
3368*4882a593Smuzhiyun unsigned int optlen)
3369*4882a593Smuzhiyun {
3370*4882a593Smuzhiyun if (optlen != sizeof(struct sctp_setadaptation))
3371*4882a593Smuzhiyun return -EINVAL;
3372*4882a593Smuzhiyun
3373*4882a593Smuzhiyun sctp_sk(sk)->adaptation_ind = adapt->ssb_adaptation_ind;
3374*4882a593Smuzhiyun
3375*4882a593Smuzhiyun return 0;
3376*4882a593Smuzhiyun }
3377*4882a593Smuzhiyun
3378*4882a593Smuzhiyun /*
3379*4882a593Smuzhiyun * 7.1.29. Set or Get the default context (SCTP_CONTEXT)
3380*4882a593Smuzhiyun *
3381*4882a593Smuzhiyun * The context field in the sctp_sndrcvinfo structure is normally only
3382*4882a593Smuzhiyun * used when a failed message is retrieved holding the value that was
3383*4882a593Smuzhiyun * sent down on the actual send call. This option allows the setting of
3384*4882a593Smuzhiyun * a default context on an association basis that will be received on
3385*4882a593Smuzhiyun * reading messages from the peer. This is especially helpful in the
3386*4882a593Smuzhiyun * one-2-many model for an application to keep some reference to an
3387*4882a593Smuzhiyun * internal state machine that is processing messages on the
3388*4882a593Smuzhiyun * association. Note that the setting of this value only effects
3389*4882a593Smuzhiyun * received messages from the peer and does not effect the value that is
3390*4882a593Smuzhiyun * saved with outbound messages.
3391*4882a593Smuzhiyun */
sctp_setsockopt_context(struct sock * sk,struct sctp_assoc_value * params,unsigned int optlen)3392*4882a593Smuzhiyun static int sctp_setsockopt_context(struct sock *sk,
3393*4882a593Smuzhiyun struct sctp_assoc_value *params,
3394*4882a593Smuzhiyun unsigned int optlen)
3395*4882a593Smuzhiyun {
3396*4882a593Smuzhiyun struct sctp_sock *sp = sctp_sk(sk);
3397*4882a593Smuzhiyun struct sctp_association *asoc;
3398*4882a593Smuzhiyun
3399*4882a593Smuzhiyun if (optlen != sizeof(struct sctp_assoc_value))
3400*4882a593Smuzhiyun return -EINVAL;
3401*4882a593Smuzhiyun
3402*4882a593Smuzhiyun asoc = sctp_id2assoc(sk, params->assoc_id);
3403*4882a593Smuzhiyun if (!asoc && params->assoc_id > SCTP_ALL_ASSOC &&
3404*4882a593Smuzhiyun sctp_style(sk, UDP))
3405*4882a593Smuzhiyun return -EINVAL;
3406*4882a593Smuzhiyun
3407*4882a593Smuzhiyun if (asoc) {
3408*4882a593Smuzhiyun asoc->default_rcv_context = params->assoc_value;
3409*4882a593Smuzhiyun
3410*4882a593Smuzhiyun return 0;
3411*4882a593Smuzhiyun }
3412*4882a593Smuzhiyun
3413*4882a593Smuzhiyun if (sctp_style(sk, TCP))
3414*4882a593Smuzhiyun params->assoc_id = SCTP_FUTURE_ASSOC;
3415*4882a593Smuzhiyun
3416*4882a593Smuzhiyun if (params->assoc_id == SCTP_FUTURE_ASSOC ||
3417*4882a593Smuzhiyun params->assoc_id == SCTP_ALL_ASSOC)
3418*4882a593Smuzhiyun sp->default_rcv_context = params->assoc_value;
3419*4882a593Smuzhiyun
3420*4882a593Smuzhiyun if (params->assoc_id == SCTP_CURRENT_ASSOC ||
3421*4882a593Smuzhiyun params->assoc_id == SCTP_ALL_ASSOC)
3422*4882a593Smuzhiyun list_for_each_entry(asoc, &sp->ep->asocs, asocs)
3423*4882a593Smuzhiyun asoc->default_rcv_context = params->assoc_value;
3424*4882a593Smuzhiyun
3425*4882a593Smuzhiyun return 0;
3426*4882a593Smuzhiyun }
3427*4882a593Smuzhiyun
3428*4882a593Smuzhiyun /*
3429*4882a593Smuzhiyun * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE)
3430*4882a593Smuzhiyun *
3431*4882a593Smuzhiyun * This options will at a minimum specify if the implementation is doing
3432*4882a593Smuzhiyun * fragmented interleave. Fragmented interleave, for a one to many
3433*4882a593Smuzhiyun * socket, is when subsequent calls to receive a message may return
3434*4882a593Smuzhiyun * parts of messages from different associations. Some implementations
3435*4882a593Smuzhiyun * may allow you to turn this value on or off. If so, when turned off,
3436*4882a593Smuzhiyun * no fragment interleave will occur (which will cause a head of line
3437*4882a593Smuzhiyun * blocking amongst multiple associations sharing the same one to many
3438*4882a593Smuzhiyun * socket). When this option is turned on, then each receive call may
3439*4882a593Smuzhiyun * come from a different association (thus the user must receive data
3440*4882a593Smuzhiyun * with the extended calls (e.g. sctp_recvmsg) to keep track of which
3441*4882a593Smuzhiyun * association each receive belongs to.
3442*4882a593Smuzhiyun *
3443*4882a593Smuzhiyun * This option takes a boolean value. A non-zero value indicates that
3444*4882a593Smuzhiyun * fragmented interleave is on. A value of zero indicates that
3445*4882a593Smuzhiyun * fragmented interleave is off.
3446*4882a593Smuzhiyun *
3447*4882a593Smuzhiyun * Note that it is important that an implementation that allows this
3448*4882a593Smuzhiyun * option to be turned on, have it off by default. Otherwise an unaware
3449*4882a593Smuzhiyun * application using the one to many model may become confused and act
3450*4882a593Smuzhiyun * incorrectly.
3451*4882a593Smuzhiyun */
sctp_setsockopt_fragment_interleave(struct sock * sk,int * val,unsigned int optlen)3452*4882a593Smuzhiyun static int sctp_setsockopt_fragment_interleave(struct sock *sk, int *val,
3453*4882a593Smuzhiyun unsigned int optlen)
3454*4882a593Smuzhiyun {
3455*4882a593Smuzhiyun if (optlen != sizeof(int))
3456*4882a593Smuzhiyun return -EINVAL;
3457*4882a593Smuzhiyun
3458*4882a593Smuzhiyun sctp_sk(sk)->frag_interleave = !!*val;
3459*4882a593Smuzhiyun
3460*4882a593Smuzhiyun if (!sctp_sk(sk)->frag_interleave)
3461*4882a593Smuzhiyun sctp_sk(sk)->ep->intl_enable = 0;
3462*4882a593Smuzhiyun
3463*4882a593Smuzhiyun return 0;
3464*4882a593Smuzhiyun }
3465*4882a593Smuzhiyun
3466*4882a593Smuzhiyun /*
3467*4882a593Smuzhiyun * 8.1.21. Set or Get the SCTP Partial Delivery Point
3468*4882a593Smuzhiyun * (SCTP_PARTIAL_DELIVERY_POINT)
3469*4882a593Smuzhiyun *
3470*4882a593Smuzhiyun * This option will set or get the SCTP partial delivery point. This
3471*4882a593Smuzhiyun * point is the size of a message where the partial delivery API will be
3472*4882a593Smuzhiyun * invoked to help free up rwnd space for the peer. Setting this to a
3473*4882a593Smuzhiyun * lower value will cause partial deliveries to happen more often. The
3474*4882a593Smuzhiyun * calls argument is an integer that sets or gets the partial delivery
3475*4882a593Smuzhiyun * point. Note also that the call will fail if the user attempts to set
3476*4882a593Smuzhiyun * this value larger than the socket receive buffer size.
3477*4882a593Smuzhiyun *
3478*4882a593Smuzhiyun * Note that any single message having a length smaller than or equal to
3479*4882a593Smuzhiyun * the SCTP partial delivery point will be delivered in one single read
3480*4882a593Smuzhiyun * call as long as the user provided buffer is large enough to hold the
3481*4882a593Smuzhiyun * message.
3482*4882a593Smuzhiyun */
sctp_setsockopt_partial_delivery_point(struct sock * sk,u32 * val,unsigned int optlen)3483*4882a593Smuzhiyun static int sctp_setsockopt_partial_delivery_point(struct sock *sk, u32 *val,
3484*4882a593Smuzhiyun unsigned int optlen)
3485*4882a593Smuzhiyun {
3486*4882a593Smuzhiyun if (optlen != sizeof(u32))
3487*4882a593Smuzhiyun return -EINVAL;
3488*4882a593Smuzhiyun
3489*4882a593Smuzhiyun /* Note: We double the receive buffer from what the user sets
3490*4882a593Smuzhiyun * it to be, also initial rwnd is based on rcvbuf/2.
3491*4882a593Smuzhiyun */
3492*4882a593Smuzhiyun if (*val > (sk->sk_rcvbuf >> 1))
3493*4882a593Smuzhiyun return -EINVAL;
3494*4882a593Smuzhiyun
3495*4882a593Smuzhiyun sctp_sk(sk)->pd_point = *val;
3496*4882a593Smuzhiyun
3497*4882a593Smuzhiyun return 0; /* is this the right error code? */
3498*4882a593Smuzhiyun }
3499*4882a593Smuzhiyun
3500*4882a593Smuzhiyun /*
3501*4882a593Smuzhiyun * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST)
3502*4882a593Smuzhiyun *
3503*4882a593Smuzhiyun * This option will allow a user to change the maximum burst of packets
3504*4882a593Smuzhiyun * that can be emitted by this association. Note that the default value
3505*4882a593Smuzhiyun * is 4, and some implementations may restrict this setting so that it
3506*4882a593Smuzhiyun * can only be lowered.
3507*4882a593Smuzhiyun *
3508*4882a593Smuzhiyun * NOTE: This text doesn't seem right. Do this on a socket basis with
3509*4882a593Smuzhiyun * future associations inheriting the socket value.
3510*4882a593Smuzhiyun */
sctp_setsockopt_maxburst(struct sock * sk,struct sctp_assoc_value * params,unsigned int optlen)3511*4882a593Smuzhiyun static int sctp_setsockopt_maxburst(struct sock *sk,
3512*4882a593Smuzhiyun struct sctp_assoc_value *params,
3513*4882a593Smuzhiyun unsigned int optlen)
3514*4882a593Smuzhiyun {
3515*4882a593Smuzhiyun struct sctp_sock *sp = sctp_sk(sk);
3516*4882a593Smuzhiyun struct sctp_association *asoc;
3517*4882a593Smuzhiyun sctp_assoc_t assoc_id;
3518*4882a593Smuzhiyun u32 assoc_value;
3519*4882a593Smuzhiyun
3520*4882a593Smuzhiyun if (optlen == sizeof(int)) {
3521*4882a593Smuzhiyun pr_warn_ratelimited(DEPRECATED
3522*4882a593Smuzhiyun "%s (pid %d) "
3523*4882a593Smuzhiyun "Use of int in max_burst socket option deprecated.\n"
3524*4882a593Smuzhiyun "Use struct sctp_assoc_value instead\n",
3525*4882a593Smuzhiyun current->comm, task_pid_nr(current));
3526*4882a593Smuzhiyun assoc_id = SCTP_FUTURE_ASSOC;
3527*4882a593Smuzhiyun assoc_value = *((int *)params);
3528*4882a593Smuzhiyun } else if (optlen == sizeof(struct sctp_assoc_value)) {
3529*4882a593Smuzhiyun assoc_id = params->assoc_id;
3530*4882a593Smuzhiyun assoc_value = params->assoc_value;
3531*4882a593Smuzhiyun } else
3532*4882a593Smuzhiyun return -EINVAL;
3533*4882a593Smuzhiyun
3534*4882a593Smuzhiyun asoc = sctp_id2assoc(sk, assoc_id);
3535*4882a593Smuzhiyun if (!asoc && assoc_id > SCTP_ALL_ASSOC && sctp_style(sk, UDP))
3536*4882a593Smuzhiyun return -EINVAL;
3537*4882a593Smuzhiyun
3538*4882a593Smuzhiyun if (asoc) {
3539*4882a593Smuzhiyun asoc->max_burst = assoc_value;
3540*4882a593Smuzhiyun
3541*4882a593Smuzhiyun return 0;
3542*4882a593Smuzhiyun }
3543*4882a593Smuzhiyun
3544*4882a593Smuzhiyun if (sctp_style(sk, TCP))
3545*4882a593Smuzhiyun assoc_id = SCTP_FUTURE_ASSOC;
3546*4882a593Smuzhiyun
3547*4882a593Smuzhiyun if (assoc_id == SCTP_FUTURE_ASSOC || assoc_id == SCTP_ALL_ASSOC)
3548*4882a593Smuzhiyun sp->max_burst = assoc_value;
3549*4882a593Smuzhiyun
3550*4882a593Smuzhiyun if (assoc_id == SCTP_CURRENT_ASSOC || assoc_id == SCTP_ALL_ASSOC)
3551*4882a593Smuzhiyun list_for_each_entry(asoc, &sp->ep->asocs, asocs)
3552*4882a593Smuzhiyun asoc->max_burst = assoc_value;
3553*4882a593Smuzhiyun
3554*4882a593Smuzhiyun return 0;
3555*4882a593Smuzhiyun }
3556*4882a593Smuzhiyun
3557*4882a593Smuzhiyun /*
3558*4882a593Smuzhiyun * 7.1.18. Add a chunk that must be authenticated (SCTP_AUTH_CHUNK)
3559*4882a593Smuzhiyun *
3560*4882a593Smuzhiyun * This set option adds a chunk type that the user is requesting to be
3561*4882a593Smuzhiyun * received only in an authenticated way. Changes to the list of chunks
3562*4882a593Smuzhiyun * will only effect future associations on the socket.
3563*4882a593Smuzhiyun */
sctp_setsockopt_auth_chunk(struct sock * sk,struct sctp_authchunk * val,unsigned int optlen)3564*4882a593Smuzhiyun static int sctp_setsockopt_auth_chunk(struct sock *sk,
3565*4882a593Smuzhiyun struct sctp_authchunk *val,
3566*4882a593Smuzhiyun unsigned int optlen)
3567*4882a593Smuzhiyun {
3568*4882a593Smuzhiyun struct sctp_endpoint *ep = sctp_sk(sk)->ep;
3569*4882a593Smuzhiyun
3570*4882a593Smuzhiyun if (!ep->auth_enable)
3571*4882a593Smuzhiyun return -EACCES;
3572*4882a593Smuzhiyun
3573*4882a593Smuzhiyun if (optlen != sizeof(struct sctp_authchunk))
3574*4882a593Smuzhiyun return -EINVAL;
3575*4882a593Smuzhiyun
3576*4882a593Smuzhiyun switch (val->sauth_chunk) {
3577*4882a593Smuzhiyun case SCTP_CID_INIT:
3578*4882a593Smuzhiyun case SCTP_CID_INIT_ACK:
3579*4882a593Smuzhiyun case SCTP_CID_SHUTDOWN_COMPLETE:
3580*4882a593Smuzhiyun case SCTP_CID_AUTH:
3581*4882a593Smuzhiyun return -EINVAL;
3582*4882a593Smuzhiyun }
3583*4882a593Smuzhiyun
3584*4882a593Smuzhiyun /* add this chunk id to the endpoint */
3585*4882a593Smuzhiyun return sctp_auth_ep_add_chunkid(ep, val->sauth_chunk);
3586*4882a593Smuzhiyun }
3587*4882a593Smuzhiyun
3588*4882a593Smuzhiyun /*
3589*4882a593Smuzhiyun * 7.1.19. Get or set the list of supported HMAC Identifiers (SCTP_HMAC_IDENT)
3590*4882a593Smuzhiyun *
3591*4882a593Smuzhiyun * This option gets or sets the list of HMAC algorithms that the local
3592*4882a593Smuzhiyun * endpoint requires the peer to use.
3593*4882a593Smuzhiyun */
sctp_setsockopt_hmac_ident(struct sock * sk,struct sctp_hmacalgo * hmacs,unsigned int optlen)3594*4882a593Smuzhiyun static int sctp_setsockopt_hmac_ident(struct sock *sk,
3595*4882a593Smuzhiyun struct sctp_hmacalgo *hmacs,
3596*4882a593Smuzhiyun unsigned int optlen)
3597*4882a593Smuzhiyun {
3598*4882a593Smuzhiyun struct sctp_endpoint *ep = sctp_sk(sk)->ep;
3599*4882a593Smuzhiyun u32 idents;
3600*4882a593Smuzhiyun
3601*4882a593Smuzhiyun if (!ep->auth_enable)
3602*4882a593Smuzhiyun return -EACCES;
3603*4882a593Smuzhiyun
3604*4882a593Smuzhiyun if (optlen < sizeof(struct sctp_hmacalgo))
3605*4882a593Smuzhiyun return -EINVAL;
3606*4882a593Smuzhiyun optlen = min_t(unsigned int, optlen, sizeof(struct sctp_hmacalgo) +
3607*4882a593Smuzhiyun SCTP_AUTH_NUM_HMACS * sizeof(u16));
3608*4882a593Smuzhiyun
3609*4882a593Smuzhiyun idents = hmacs->shmac_num_idents;
3610*4882a593Smuzhiyun if (idents == 0 || idents > SCTP_AUTH_NUM_HMACS ||
3611*4882a593Smuzhiyun (idents * sizeof(u16)) > (optlen - sizeof(struct sctp_hmacalgo)))
3612*4882a593Smuzhiyun return -EINVAL;
3613*4882a593Smuzhiyun
3614*4882a593Smuzhiyun return sctp_auth_ep_set_hmacs(ep, hmacs);
3615*4882a593Smuzhiyun }
3616*4882a593Smuzhiyun
3617*4882a593Smuzhiyun /*
3618*4882a593Smuzhiyun * 7.1.20. Set a shared key (SCTP_AUTH_KEY)
3619*4882a593Smuzhiyun *
3620*4882a593Smuzhiyun * This option will set a shared secret key which is used to build an
3621*4882a593Smuzhiyun * association shared key.
3622*4882a593Smuzhiyun */
sctp_setsockopt_auth_key(struct sock * sk,struct sctp_authkey * authkey,unsigned int optlen)3623*4882a593Smuzhiyun static int sctp_setsockopt_auth_key(struct sock *sk,
3624*4882a593Smuzhiyun struct sctp_authkey *authkey,
3625*4882a593Smuzhiyun unsigned int optlen)
3626*4882a593Smuzhiyun {
3627*4882a593Smuzhiyun struct sctp_endpoint *ep = sctp_sk(sk)->ep;
3628*4882a593Smuzhiyun struct sctp_association *asoc;
3629*4882a593Smuzhiyun int ret = -EINVAL;
3630*4882a593Smuzhiyun
3631*4882a593Smuzhiyun if (optlen <= sizeof(struct sctp_authkey))
3632*4882a593Smuzhiyun return -EINVAL;
3633*4882a593Smuzhiyun /* authkey->sca_keylength is u16, so optlen can't be bigger than
3634*4882a593Smuzhiyun * this.
3635*4882a593Smuzhiyun */
3636*4882a593Smuzhiyun optlen = min_t(unsigned int, optlen, USHRT_MAX + sizeof(*authkey));
3637*4882a593Smuzhiyun
3638*4882a593Smuzhiyun if (authkey->sca_keylength > optlen - sizeof(*authkey))
3639*4882a593Smuzhiyun goto out;
3640*4882a593Smuzhiyun
3641*4882a593Smuzhiyun asoc = sctp_id2assoc(sk, authkey->sca_assoc_id);
3642*4882a593Smuzhiyun if (!asoc && authkey->sca_assoc_id > SCTP_ALL_ASSOC &&
3643*4882a593Smuzhiyun sctp_style(sk, UDP))
3644*4882a593Smuzhiyun goto out;
3645*4882a593Smuzhiyun
3646*4882a593Smuzhiyun if (asoc) {
3647*4882a593Smuzhiyun ret = sctp_auth_set_key(ep, asoc, authkey);
3648*4882a593Smuzhiyun goto out;
3649*4882a593Smuzhiyun }
3650*4882a593Smuzhiyun
3651*4882a593Smuzhiyun if (sctp_style(sk, TCP))
3652*4882a593Smuzhiyun authkey->sca_assoc_id = SCTP_FUTURE_ASSOC;
3653*4882a593Smuzhiyun
3654*4882a593Smuzhiyun if (authkey->sca_assoc_id == SCTP_FUTURE_ASSOC ||
3655*4882a593Smuzhiyun authkey->sca_assoc_id == SCTP_ALL_ASSOC) {
3656*4882a593Smuzhiyun ret = sctp_auth_set_key(ep, asoc, authkey);
3657*4882a593Smuzhiyun if (ret)
3658*4882a593Smuzhiyun goto out;
3659*4882a593Smuzhiyun }
3660*4882a593Smuzhiyun
3661*4882a593Smuzhiyun ret = 0;
3662*4882a593Smuzhiyun
3663*4882a593Smuzhiyun if (authkey->sca_assoc_id == SCTP_CURRENT_ASSOC ||
3664*4882a593Smuzhiyun authkey->sca_assoc_id == SCTP_ALL_ASSOC) {
3665*4882a593Smuzhiyun list_for_each_entry(asoc, &ep->asocs, asocs) {
3666*4882a593Smuzhiyun int res = sctp_auth_set_key(ep, asoc, authkey);
3667*4882a593Smuzhiyun
3668*4882a593Smuzhiyun if (res && !ret)
3669*4882a593Smuzhiyun ret = res;
3670*4882a593Smuzhiyun }
3671*4882a593Smuzhiyun }
3672*4882a593Smuzhiyun
3673*4882a593Smuzhiyun out:
3674*4882a593Smuzhiyun memzero_explicit(authkey, optlen);
3675*4882a593Smuzhiyun return ret;
3676*4882a593Smuzhiyun }
3677*4882a593Smuzhiyun
3678*4882a593Smuzhiyun /*
3679*4882a593Smuzhiyun * 7.1.21. Get or set the active shared key (SCTP_AUTH_ACTIVE_KEY)
3680*4882a593Smuzhiyun *
3681*4882a593Smuzhiyun * This option will get or set the active shared key to be used to build
3682*4882a593Smuzhiyun * the association shared key.
3683*4882a593Smuzhiyun */
sctp_setsockopt_active_key(struct sock * sk,struct sctp_authkeyid * val,unsigned int optlen)3684*4882a593Smuzhiyun static int sctp_setsockopt_active_key(struct sock *sk,
3685*4882a593Smuzhiyun struct sctp_authkeyid *val,
3686*4882a593Smuzhiyun unsigned int optlen)
3687*4882a593Smuzhiyun {
3688*4882a593Smuzhiyun struct sctp_endpoint *ep = sctp_sk(sk)->ep;
3689*4882a593Smuzhiyun struct sctp_association *asoc;
3690*4882a593Smuzhiyun int ret = 0;
3691*4882a593Smuzhiyun
3692*4882a593Smuzhiyun if (optlen != sizeof(struct sctp_authkeyid))
3693*4882a593Smuzhiyun return -EINVAL;
3694*4882a593Smuzhiyun
3695*4882a593Smuzhiyun asoc = sctp_id2assoc(sk, val->scact_assoc_id);
3696*4882a593Smuzhiyun if (!asoc && val->scact_assoc_id > SCTP_ALL_ASSOC &&
3697*4882a593Smuzhiyun sctp_style(sk, UDP))
3698*4882a593Smuzhiyun return -EINVAL;
3699*4882a593Smuzhiyun
3700*4882a593Smuzhiyun if (asoc)
3701*4882a593Smuzhiyun return sctp_auth_set_active_key(ep, asoc, val->scact_keynumber);
3702*4882a593Smuzhiyun
3703*4882a593Smuzhiyun if (sctp_style(sk, TCP))
3704*4882a593Smuzhiyun val->scact_assoc_id = SCTP_FUTURE_ASSOC;
3705*4882a593Smuzhiyun
3706*4882a593Smuzhiyun if (val->scact_assoc_id == SCTP_FUTURE_ASSOC ||
3707*4882a593Smuzhiyun val->scact_assoc_id == SCTP_ALL_ASSOC) {
3708*4882a593Smuzhiyun ret = sctp_auth_set_active_key(ep, asoc, val->scact_keynumber);
3709*4882a593Smuzhiyun if (ret)
3710*4882a593Smuzhiyun return ret;
3711*4882a593Smuzhiyun }
3712*4882a593Smuzhiyun
3713*4882a593Smuzhiyun if (val->scact_assoc_id == SCTP_CURRENT_ASSOC ||
3714*4882a593Smuzhiyun val->scact_assoc_id == SCTP_ALL_ASSOC) {
3715*4882a593Smuzhiyun list_for_each_entry(asoc, &ep->asocs, asocs) {
3716*4882a593Smuzhiyun int res = sctp_auth_set_active_key(ep, asoc,
3717*4882a593Smuzhiyun val->scact_keynumber);
3718*4882a593Smuzhiyun
3719*4882a593Smuzhiyun if (res && !ret)
3720*4882a593Smuzhiyun ret = res;
3721*4882a593Smuzhiyun }
3722*4882a593Smuzhiyun }
3723*4882a593Smuzhiyun
3724*4882a593Smuzhiyun return ret;
3725*4882a593Smuzhiyun }
3726*4882a593Smuzhiyun
3727*4882a593Smuzhiyun /*
3728*4882a593Smuzhiyun * 7.1.22. Delete a shared key (SCTP_AUTH_DELETE_KEY)
3729*4882a593Smuzhiyun *
3730*4882a593Smuzhiyun * This set option will delete a shared secret key from use.
3731*4882a593Smuzhiyun */
sctp_setsockopt_del_key(struct sock * sk,struct sctp_authkeyid * val,unsigned int optlen)3732*4882a593Smuzhiyun static int sctp_setsockopt_del_key(struct sock *sk,
3733*4882a593Smuzhiyun struct sctp_authkeyid *val,
3734*4882a593Smuzhiyun unsigned int optlen)
3735*4882a593Smuzhiyun {
3736*4882a593Smuzhiyun struct sctp_endpoint *ep = sctp_sk(sk)->ep;
3737*4882a593Smuzhiyun struct sctp_association *asoc;
3738*4882a593Smuzhiyun int ret = 0;
3739*4882a593Smuzhiyun
3740*4882a593Smuzhiyun if (optlen != sizeof(struct sctp_authkeyid))
3741*4882a593Smuzhiyun return -EINVAL;
3742*4882a593Smuzhiyun
3743*4882a593Smuzhiyun asoc = sctp_id2assoc(sk, val->scact_assoc_id);
3744*4882a593Smuzhiyun if (!asoc && val->scact_assoc_id > SCTP_ALL_ASSOC &&
3745*4882a593Smuzhiyun sctp_style(sk, UDP))
3746*4882a593Smuzhiyun return -EINVAL;
3747*4882a593Smuzhiyun
3748*4882a593Smuzhiyun if (asoc)
3749*4882a593Smuzhiyun return sctp_auth_del_key_id(ep, asoc, val->scact_keynumber);
3750*4882a593Smuzhiyun
3751*4882a593Smuzhiyun if (sctp_style(sk, TCP))
3752*4882a593Smuzhiyun val->scact_assoc_id = SCTP_FUTURE_ASSOC;
3753*4882a593Smuzhiyun
3754*4882a593Smuzhiyun if (val->scact_assoc_id == SCTP_FUTURE_ASSOC ||
3755*4882a593Smuzhiyun val->scact_assoc_id == SCTP_ALL_ASSOC) {
3756*4882a593Smuzhiyun ret = sctp_auth_del_key_id(ep, asoc, val->scact_keynumber);
3757*4882a593Smuzhiyun if (ret)
3758*4882a593Smuzhiyun return ret;
3759*4882a593Smuzhiyun }
3760*4882a593Smuzhiyun
3761*4882a593Smuzhiyun if (val->scact_assoc_id == SCTP_CURRENT_ASSOC ||
3762*4882a593Smuzhiyun val->scact_assoc_id == SCTP_ALL_ASSOC) {
3763*4882a593Smuzhiyun list_for_each_entry(asoc, &ep->asocs, asocs) {
3764*4882a593Smuzhiyun int res = sctp_auth_del_key_id(ep, asoc,
3765*4882a593Smuzhiyun val->scact_keynumber);
3766*4882a593Smuzhiyun
3767*4882a593Smuzhiyun if (res && !ret)
3768*4882a593Smuzhiyun ret = res;
3769*4882a593Smuzhiyun }
3770*4882a593Smuzhiyun }
3771*4882a593Smuzhiyun
3772*4882a593Smuzhiyun return ret;
3773*4882a593Smuzhiyun }
3774*4882a593Smuzhiyun
3775*4882a593Smuzhiyun /*
3776*4882a593Smuzhiyun * 8.3.4 Deactivate a Shared Key (SCTP_AUTH_DEACTIVATE_KEY)
3777*4882a593Smuzhiyun *
3778*4882a593Smuzhiyun * This set option will deactivate a shared secret key.
3779*4882a593Smuzhiyun */
sctp_setsockopt_deactivate_key(struct sock * sk,struct sctp_authkeyid * val,unsigned int optlen)3780*4882a593Smuzhiyun static int sctp_setsockopt_deactivate_key(struct sock *sk,
3781*4882a593Smuzhiyun struct sctp_authkeyid *val,
3782*4882a593Smuzhiyun unsigned int optlen)
3783*4882a593Smuzhiyun {
3784*4882a593Smuzhiyun struct sctp_endpoint *ep = sctp_sk(sk)->ep;
3785*4882a593Smuzhiyun struct sctp_association *asoc;
3786*4882a593Smuzhiyun int ret = 0;
3787*4882a593Smuzhiyun
3788*4882a593Smuzhiyun if (optlen != sizeof(struct sctp_authkeyid))
3789*4882a593Smuzhiyun return -EINVAL;
3790*4882a593Smuzhiyun
3791*4882a593Smuzhiyun asoc = sctp_id2assoc(sk, val->scact_assoc_id);
3792*4882a593Smuzhiyun if (!asoc && val->scact_assoc_id > SCTP_ALL_ASSOC &&
3793*4882a593Smuzhiyun sctp_style(sk, UDP))
3794*4882a593Smuzhiyun return -EINVAL;
3795*4882a593Smuzhiyun
3796*4882a593Smuzhiyun if (asoc)
3797*4882a593Smuzhiyun return sctp_auth_deact_key_id(ep, asoc, val->scact_keynumber);
3798*4882a593Smuzhiyun
3799*4882a593Smuzhiyun if (sctp_style(sk, TCP))
3800*4882a593Smuzhiyun val->scact_assoc_id = SCTP_FUTURE_ASSOC;
3801*4882a593Smuzhiyun
3802*4882a593Smuzhiyun if (val->scact_assoc_id == SCTP_FUTURE_ASSOC ||
3803*4882a593Smuzhiyun val->scact_assoc_id == SCTP_ALL_ASSOC) {
3804*4882a593Smuzhiyun ret = sctp_auth_deact_key_id(ep, asoc, val->scact_keynumber);
3805*4882a593Smuzhiyun if (ret)
3806*4882a593Smuzhiyun return ret;
3807*4882a593Smuzhiyun }
3808*4882a593Smuzhiyun
3809*4882a593Smuzhiyun if (val->scact_assoc_id == SCTP_CURRENT_ASSOC ||
3810*4882a593Smuzhiyun val->scact_assoc_id == SCTP_ALL_ASSOC) {
3811*4882a593Smuzhiyun list_for_each_entry(asoc, &ep->asocs, asocs) {
3812*4882a593Smuzhiyun int res = sctp_auth_deact_key_id(ep, asoc,
3813*4882a593Smuzhiyun val->scact_keynumber);
3814*4882a593Smuzhiyun
3815*4882a593Smuzhiyun if (res && !ret)
3816*4882a593Smuzhiyun ret = res;
3817*4882a593Smuzhiyun }
3818*4882a593Smuzhiyun }
3819*4882a593Smuzhiyun
3820*4882a593Smuzhiyun return ret;
3821*4882a593Smuzhiyun }
3822*4882a593Smuzhiyun
3823*4882a593Smuzhiyun /*
3824*4882a593Smuzhiyun * 8.1.23 SCTP_AUTO_ASCONF
3825*4882a593Smuzhiyun *
3826*4882a593Smuzhiyun * This option will enable or disable the use of the automatic generation of
3827*4882a593Smuzhiyun * ASCONF chunks to add and delete addresses to an existing association. Note
3828*4882a593Smuzhiyun * that this option has two caveats namely: a) it only affects sockets that
3829*4882a593Smuzhiyun * are bound to all addresses available to the SCTP stack, and b) the system
3830*4882a593Smuzhiyun * administrator may have an overriding control that turns the ASCONF feature
3831*4882a593Smuzhiyun * off no matter what setting the socket option may have.
3832*4882a593Smuzhiyun * This option expects an integer boolean flag, where a non-zero value turns on
3833*4882a593Smuzhiyun * the option, and a zero value turns off the option.
3834*4882a593Smuzhiyun * Note. In this implementation, socket operation overrides default parameter
3835*4882a593Smuzhiyun * being set by sysctl as well as FreeBSD implementation
3836*4882a593Smuzhiyun */
sctp_setsockopt_auto_asconf(struct sock * sk,int * val,unsigned int optlen)3837*4882a593Smuzhiyun static int sctp_setsockopt_auto_asconf(struct sock *sk, int *val,
3838*4882a593Smuzhiyun unsigned int optlen)
3839*4882a593Smuzhiyun {
3840*4882a593Smuzhiyun struct sctp_sock *sp = sctp_sk(sk);
3841*4882a593Smuzhiyun
3842*4882a593Smuzhiyun if (optlen < sizeof(int))
3843*4882a593Smuzhiyun return -EINVAL;
3844*4882a593Smuzhiyun if (!sctp_is_ep_boundall(sk) && *val)
3845*4882a593Smuzhiyun return -EINVAL;
3846*4882a593Smuzhiyun if ((*val && sp->do_auto_asconf) || (!*val && !sp->do_auto_asconf))
3847*4882a593Smuzhiyun return 0;
3848*4882a593Smuzhiyun
3849*4882a593Smuzhiyun spin_lock_bh(&sock_net(sk)->sctp.addr_wq_lock);
3850*4882a593Smuzhiyun if (*val == 0 && sp->do_auto_asconf) {
3851*4882a593Smuzhiyun list_del(&sp->auto_asconf_list);
3852*4882a593Smuzhiyun sp->do_auto_asconf = 0;
3853*4882a593Smuzhiyun } else if (*val && !sp->do_auto_asconf) {
3854*4882a593Smuzhiyun list_add_tail(&sp->auto_asconf_list,
3855*4882a593Smuzhiyun &sock_net(sk)->sctp.auto_asconf_splist);
3856*4882a593Smuzhiyun sp->do_auto_asconf = 1;
3857*4882a593Smuzhiyun }
3858*4882a593Smuzhiyun spin_unlock_bh(&sock_net(sk)->sctp.addr_wq_lock);
3859*4882a593Smuzhiyun return 0;
3860*4882a593Smuzhiyun }
3861*4882a593Smuzhiyun
3862*4882a593Smuzhiyun /*
3863*4882a593Smuzhiyun * SCTP_PEER_ADDR_THLDS
3864*4882a593Smuzhiyun *
3865*4882a593Smuzhiyun * This option allows us to alter the partially failed threshold for one or all
3866*4882a593Smuzhiyun * transports in an association. See Section 6.1 of:
3867*4882a593Smuzhiyun * http://www.ietf.org/id/draft-nishida-tsvwg-sctp-failover-05.txt
3868*4882a593Smuzhiyun */
sctp_setsockopt_paddr_thresholds(struct sock * sk,struct sctp_paddrthlds_v2 * val,unsigned int optlen,bool v2)3869*4882a593Smuzhiyun static int sctp_setsockopt_paddr_thresholds(struct sock *sk,
3870*4882a593Smuzhiyun struct sctp_paddrthlds_v2 *val,
3871*4882a593Smuzhiyun unsigned int optlen, bool v2)
3872*4882a593Smuzhiyun {
3873*4882a593Smuzhiyun struct sctp_transport *trans;
3874*4882a593Smuzhiyun struct sctp_association *asoc;
3875*4882a593Smuzhiyun int len;
3876*4882a593Smuzhiyun
3877*4882a593Smuzhiyun len = v2 ? sizeof(*val) : sizeof(struct sctp_paddrthlds);
3878*4882a593Smuzhiyun if (optlen < len)
3879*4882a593Smuzhiyun return -EINVAL;
3880*4882a593Smuzhiyun
3881*4882a593Smuzhiyun if (v2 && val->spt_pathpfthld > val->spt_pathcpthld)
3882*4882a593Smuzhiyun return -EINVAL;
3883*4882a593Smuzhiyun
3884*4882a593Smuzhiyun if (!sctp_is_any(sk, (const union sctp_addr *)&val->spt_address)) {
3885*4882a593Smuzhiyun trans = sctp_addr_id2transport(sk, &val->spt_address,
3886*4882a593Smuzhiyun val->spt_assoc_id);
3887*4882a593Smuzhiyun if (!trans)
3888*4882a593Smuzhiyun return -ENOENT;
3889*4882a593Smuzhiyun
3890*4882a593Smuzhiyun if (val->spt_pathmaxrxt)
3891*4882a593Smuzhiyun trans->pathmaxrxt = val->spt_pathmaxrxt;
3892*4882a593Smuzhiyun if (v2)
3893*4882a593Smuzhiyun trans->ps_retrans = val->spt_pathcpthld;
3894*4882a593Smuzhiyun trans->pf_retrans = val->spt_pathpfthld;
3895*4882a593Smuzhiyun
3896*4882a593Smuzhiyun return 0;
3897*4882a593Smuzhiyun }
3898*4882a593Smuzhiyun
3899*4882a593Smuzhiyun asoc = sctp_id2assoc(sk, val->spt_assoc_id);
3900*4882a593Smuzhiyun if (!asoc && val->spt_assoc_id != SCTP_FUTURE_ASSOC &&
3901*4882a593Smuzhiyun sctp_style(sk, UDP))
3902*4882a593Smuzhiyun return -EINVAL;
3903*4882a593Smuzhiyun
3904*4882a593Smuzhiyun if (asoc) {
3905*4882a593Smuzhiyun list_for_each_entry(trans, &asoc->peer.transport_addr_list,
3906*4882a593Smuzhiyun transports) {
3907*4882a593Smuzhiyun if (val->spt_pathmaxrxt)
3908*4882a593Smuzhiyun trans->pathmaxrxt = val->spt_pathmaxrxt;
3909*4882a593Smuzhiyun if (v2)
3910*4882a593Smuzhiyun trans->ps_retrans = val->spt_pathcpthld;
3911*4882a593Smuzhiyun trans->pf_retrans = val->spt_pathpfthld;
3912*4882a593Smuzhiyun }
3913*4882a593Smuzhiyun
3914*4882a593Smuzhiyun if (val->spt_pathmaxrxt)
3915*4882a593Smuzhiyun asoc->pathmaxrxt = val->spt_pathmaxrxt;
3916*4882a593Smuzhiyun if (v2)
3917*4882a593Smuzhiyun asoc->ps_retrans = val->spt_pathcpthld;
3918*4882a593Smuzhiyun asoc->pf_retrans = val->spt_pathpfthld;
3919*4882a593Smuzhiyun } else {
3920*4882a593Smuzhiyun struct sctp_sock *sp = sctp_sk(sk);
3921*4882a593Smuzhiyun
3922*4882a593Smuzhiyun if (val->spt_pathmaxrxt)
3923*4882a593Smuzhiyun sp->pathmaxrxt = val->spt_pathmaxrxt;
3924*4882a593Smuzhiyun if (v2)
3925*4882a593Smuzhiyun sp->ps_retrans = val->spt_pathcpthld;
3926*4882a593Smuzhiyun sp->pf_retrans = val->spt_pathpfthld;
3927*4882a593Smuzhiyun }
3928*4882a593Smuzhiyun
3929*4882a593Smuzhiyun return 0;
3930*4882a593Smuzhiyun }
3931*4882a593Smuzhiyun
sctp_setsockopt_recvrcvinfo(struct sock * sk,int * val,unsigned int optlen)3932*4882a593Smuzhiyun static int sctp_setsockopt_recvrcvinfo(struct sock *sk, int *val,
3933*4882a593Smuzhiyun unsigned int optlen)
3934*4882a593Smuzhiyun {
3935*4882a593Smuzhiyun if (optlen < sizeof(int))
3936*4882a593Smuzhiyun return -EINVAL;
3937*4882a593Smuzhiyun
3938*4882a593Smuzhiyun sctp_sk(sk)->recvrcvinfo = (*val == 0) ? 0 : 1;
3939*4882a593Smuzhiyun
3940*4882a593Smuzhiyun return 0;
3941*4882a593Smuzhiyun }
3942*4882a593Smuzhiyun
sctp_setsockopt_recvnxtinfo(struct sock * sk,int * val,unsigned int optlen)3943*4882a593Smuzhiyun static int sctp_setsockopt_recvnxtinfo(struct sock *sk, int *val,
3944*4882a593Smuzhiyun unsigned int optlen)
3945*4882a593Smuzhiyun {
3946*4882a593Smuzhiyun if (optlen < sizeof(int))
3947*4882a593Smuzhiyun return -EINVAL;
3948*4882a593Smuzhiyun
3949*4882a593Smuzhiyun sctp_sk(sk)->recvnxtinfo = (*val == 0) ? 0 : 1;
3950*4882a593Smuzhiyun
3951*4882a593Smuzhiyun return 0;
3952*4882a593Smuzhiyun }
3953*4882a593Smuzhiyun
sctp_setsockopt_pr_supported(struct sock * sk,struct sctp_assoc_value * params,unsigned int optlen)3954*4882a593Smuzhiyun static int sctp_setsockopt_pr_supported(struct sock *sk,
3955*4882a593Smuzhiyun struct sctp_assoc_value *params,
3956*4882a593Smuzhiyun unsigned int optlen)
3957*4882a593Smuzhiyun {
3958*4882a593Smuzhiyun struct sctp_association *asoc;
3959*4882a593Smuzhiyun
3960*4882a593Smuzhiyun if (optlen != sizeof(*params))
3961*4882a593Smuzhiyun return -EINVAL;
3962*4882a593Smuzhiyun
3963*4882a593Smuzhiyun asoc = sctp_id2assoc(sk, params->assoc_id);
3964*4882a593Smuzhiyun if (!asoc && params->assoc_id != SCTP_FUTURE_ASSOC &&
3965*4882a593Smuzhiyun sctp_style(sk, UDP))
3966*4882a593Smuzhiyun return -EINVAL;
3967*4882a593Smuzhiyun
3968*4882a593Smuzhiyun sctp_sk(sk)->ep->prsctp_enable = !!params->assoc_value;
3969*4882a593Smuzhiyun
3970*4882a593Smuzhiyun return 0;
3971*4882a593Smuzhiyun }
3972*4882a593Smuzhiyun
sctp_setsockopt_default_prinfo(struct sock * sk,struct sctp_default_prinfo * info,unsigned int optlen)3973*4882a593Smuzhiyun static int sctp_setsockopt_default_prinfo(struct sock *sk,
3974*4882a593Smuzhiyun struct sctp_default_prinfo *info,
3975*4882a593Smuzhiyun unsigned int optlen)
3976*4882a593Smuzhiyun {
3977*4882a593Smuzhiyun struct sctp_sock *sp = sctp_sk(sk);
3978*4882a593Smuzhiyun struct sctp_association *asoc;
3979*4882a593Smuzhiyun int retval = -EINVAL;
3980*4882a593Smuzhiyun
3981*4882a593Smuzhiyun if (optlen != sizeof(*info))
3982*4882a593Smuzhiyun goto out;
3983*4882a593Smuzhiyun
3984*4882a593Smuzhiyun if (info->pr_policy & ~SCTP_PR_SCTP_MASK)
3985*4882a593Smuzhiyun goto out;
3986*4882a593Smuzhiyun
3987*4882a593Smuzhiyun if (info->pr_policy == SCTP_PR_SCTP_NONE)
3988*4882a593Smuzhiyun info->pr_value = 0;
3989*4882a593Smuzhiyun
3990*4882a593Smuzhiyun asoc = sctp_id2assoc(sk, info->pr_assoc_id);
3991*4882a593Smuzhiyun if (!asoc && info->pr_assoc_id > SCTP_ALL_ASSOC &&
3992*4882a593Smuzhiyun sctp_style(sk, UDP))
3993*4882a593Smuzhiyun goto out;
3994*4882a593Smuzhiyun
3995*4882a593Smuzhiyun retval = 0;
3996*4882a593Smuzhiyun
3997*4882a593Smuzhiyun if (asoc) {
3998*4882a593Smuzhiyun SCTP_PR_SET_POLICY(asoc->default_flags, info->pr_policy);
3999*4882a593Smuzhiyun asoc->default_timetolive = info->pr_value;
4000*4882a593Smuzhiyun goto out;
4001*4882a593Smuzhiyun }
4002*4882a593Smuzhiyun
4003*4882a593Smuzhiyun if (sctp_style(sk, TCP))
4004*4882a593Smuzhiyun info->pr_assoc_id = SCTP_FUTURE_ASSOC;
4005*4882a593Smuzhiyun
4006*4882a593Smuzhiyun if (info->pr_assoc_id == SCTP_FUTURE_ASSOC ||
4007*4882a593Smuzhiyun info->pr_assoc_id == SCTP_ALL_ASSOC) {
4008*4882a593Smuzhiyun SCTP_PR_SET_POLICY(sp->default_flags, info->pr_policy);
4009*4882a593Smuzhiyun sp->default_timetolive = info->pr_value;
4010*4882a593Smuzhiyun }
4011*4882a593Smuzhiyun
4012*4882a593Smuzhiyun if (info->pr_assoc_id == SCTP_CURRENT_ASSOC ||
4013*4882a593Smuzhiyun info->pr_assoc_id == SCTP_ALL_ASSOC) {
4014*4882a593Smuzhiyun list_for_each_entry(asoc, &sp->ep->asocs, asocs) {
4015*4882a593Smuzhiyun SCTP_PR_SET_POLICY(asoc->default_flags,
4016*4882a593Smuzhiyun info->pr_policy);
4017*4882a593Smuzhiyun asoc->default_timetolive = info->pr_value;
4018*4882a593Smuzhiyun }
4019*4882a593Smuzhiyun }
4020*4882a593Smuzhiyun
4021*4882a593Smuzhiyun out:
4022*4882a593Smuzhiyun return retval;
4023*4882a593Smuzhiyun }
4024*4882a593Smuzhiyun
sctp_setsockopt_reconfig_supported(struct sock * sk,struct sctp_assoc_value * params,unsigned int optlen)4025*4882a593Smuzhiyun static int sctp_setsockopt_reconfig_supported(struct sock *sk,
4026*4882a593Smuzhiyun struct sctp_assoc_value *params,
4027*4882a593Smuzhiyun unsigned int optlen)
4028*4882a593Smuzhiyun {
4029*4882a593Smuzhiyun struct sctp_association *asoc;
4030*4882a593Smuzhiyun int retval = -EINVAL;
4031*4882a593Smuzhiyun
4032*4882a593Smuzhiyun if (optlen != sizeof(*params))
4033*4882a593Smuzhiyun goto out;
4034*4882a593Smuzhiyun
4035*4882a593Smuzhiyun asoc = sctp_id2assoc(sk, params->assoc_id);
4036*4882a593Smuzhiyun if (!asoc && params->assoc_id != SCTP_FUTURE_ASSOC &&
4037*4882a593Smuzhiyun sctp_style(sk, UDP))
4038*4882a593Smuzhiyun goto out;
4039*4882a593Smuzhiyun
4040*4882a593Smuzhiyun sctp_sk(sk)->ep->reconf_enable = !!params->assoc_value;
4041*4882a593Smuzhiyun
4042*4882a593Smuzhiyun retval = 0;
4043*4882a593Smuzhiyun
4044*4882a593Smuzhiyun out:
4045*4882a593Smuzhiyun return retval;
4046*4882a593Smuzhiyun }
4047*4882a593Smuzhiyun
sctp_setsockopt_enable_strreset(struct sock * sk,struct sctp_assoc_value * params,unsigned int optlen)4048*4882a593Smuzhiyun static int sctp_setsockopt_enable_strreset(struct sock *sk,
4049*4882a593Smuzhiyun struct sctp_assoc_value *params,
4050*4882a593Smuzhiyun unsigned int optlen)
4051*4882a593Smuzhiyun {
4052*4882a593Smuzhiyun struct sctp_endpoint *ep = sctp_sk(sk)->ep;
4053*4882a593Smuzhiyun struct sctp_association *asoc;
4054*4882a593Smuzhiyun int retval = -EINVAL;
4055*4882a593Smuzhiyun
4056*4882a593Smuzhiyun if (optlen != sizeof(*params))
4057*4882a593Smuzhiyun goto out;
4058*4882a593Smuzhiyun
4059*4882a593Smuzhiyun if (params->assoc_value & (~SCTP_ENABLE_STRRESET_MASK))
4060*4882a593Smuzhiyun goto out;
4061*4882a593Smuzhiyun
4062*4882a593Smuzhiyun asoc = sctp_id2assoc(sk, params->assoc_id);
4063*4882a593Smuzhiyun if (!asoc && params->assoc_id > SCTP_ALL_ASSOC &&
4064*4882a593Smuzhiyun sctp_style(sk, UDP))
4065*4882a593Smuzhiyun goto out;
4066*4882a593Smuzhiyun
4067*4882a593Smuzhiyun retval = 0;
4068*4882a593Smuzhiyun
4069*4882a593Smuzhiyun if (asoc) {
4070*4882a593Smuzhiyun asoc->strreset_enable = params->assoc_value;
4071*4882a593Smuzhiyun goto out;
4072*4882a593Smuzhiyun }
4073*4882a593Smuzhiyun
4074*4882a593Smuzhiyun if (sctp_style(sk, TCP))
4075*4882a593Smuzhiyun params->assoc_id = SCTP_FUTURE_ASSOC;
4076*4882a593Smuzhiyun
4077*4882a593Smuzhiyun if (params->assoc_id == SCTP_FUTURE_ASSOC ||
4078*4882a593Smuzhiyun params->assoc_id == SCTP_ALL_ASSOC)
4079*4882a593Smuzhiyun ep->strreset_enable = params->assoc_value;
4080*4882a593Smuzhiyun
4081*4882a593Smuzhiyun if (params->assoc_id == SCTP_CURRENT_ASSOC ||
4082*4882a593Smuzhiyun params->assoc_id == SCTP_ALL_ASSOC)
4083*4882a593Smuzhiyun list_for_each_entry(asoc, &ep->asocs, asocs)
4084*4882a593Smuzhiyun asoc->strreset_enable = params->assoc_value;
4085*4882a593Smuzhiyun
4086*4882a593Smuzhiyun out:
4087*4882a593Smuzhiyun return retval;
4088*4882a593Smuzhiyun }
4089*4882a593Smuzhiyun
sctp_setsockopt_reset_streams(struct sock * sk,struct sctp_reset_streams * params,unsigned int optlen)4090*4882a593Smuzhiyun static int sctp_setsockopt_reset_streams(struct sock *sk,
4091*4882a593Smuzhiyun struct sctp_reset_streams *params,
4092*4882a593Smuzhiyun unsigned int optlen)
4093*4882a593Smuzhiyun {
4094*4882a593Smuzhiyun struct sctp_association *asoc;
4095*4882a593Smuzhiyun
4096*4882a593Smuzhiyun if (optlen < sizeof(*params))
4097*4882a593Smuzhiyun return -EINVAL;
4098*4882a593Smuzhiyun /* srs_number_streams is u16, so optlen can't be bigger than this. */
4099*4882a593Smuzhiyun optlen = min_t(unsigned int, optlen, USHRT_MAX +
4100*4882a593Smuzhiyun sizeof(__u16) * sizeof(*params));
4101*4882a593Smuzhiyun
4102*4882a593Smuzhiyun if (params->srs_number_streams * sizeof(__u16) >
4103*4882a593Smuzhiyun optlen - sizeof(*params))
4104*4882a593Smuzhiyun return -EINVAL;
4105*4882a593Smuzhiyun
4106*4882a593Smuzhiyun asoc = sctp_id2assoc(sk, params->srs_assoc_id);
4107*4882a593Smuzhiyun if (!asoc)
4108*4882a593Smuzhiyun return -EINVAL;
4109*4882a593Smuzhiyun
4110*4882a593Smuzhiyun return sctp_send_reset_streams(asoc, params);
4111*4882a593Smuzhiyun }
4112*4882a593Smuzhiyun
sctp_setsockopt_reset_assoc(struct sock * sk,sctp_assoc_t * associd,unsigned int optlen)4113*4882a593Smuzhiyun static int sctp_setsockopt_reset_assoc(struct sock *sk, sctp_assoc_t *associd,
4114*4882a593Smuzhiyun unsigned int optlen)
4115*4882a593Smuzhiyun {
4116*4882a593Smuzhiyun struct sctp_association *asoc;
4117*4882a593Smuzhiyun
4118*4882a593Smuzhiyun if (optlen != sizeof(*associd))
4119*4882a593Smuzhiyun return -EINVAL;
4120*4882a593Smuzhiyun
4121*4882a593Smuzhiyun asoc = sctp_id2assoc(sk, *associd);
4122*4882a593Smuzhiyun if (!asoc)
4123*4882a593Smuzhiyun return -EINVAL;
4124*4882a593Smuzhiyun
4125*4882a593Smuzhiyun return sctp_send_reset_assoc(asoc);
4126*4882a593Smuzhiyun }
4127*4882a593Smuzhiyun
sctp_setsockopt_add_streams(struct sock * sk,struct sctp_add_streams * params,unsigned int optlen)4128*4882a593Smuzhiyun static int sctp_setsockopt_add_streams(struct sock *sk,
4129*4882a593Smuzhiyun struct sctp_add_streams *params,
4130*4882a593Smuzhiyun unsigned int optlen)
4131*4882a593Smuzhiyun {
4132*4882a593Smuzhiyun struct sctp_association *asoc;
4133*4882a593Smuzhiyun
4134*4882a593Smuzhiyun if (optlen != sizeof(*params))
4135*4882a593Smuzhiyun return -EINVAL;
4136*4882a593Smuzhiyun
4137*4882a593Smuzhiyun asoc = sctp_id2assoc(sk, params->sas_assoc_id);
4138*4882a593Smuzhiyun if (!asoc)
4139*4882a593Smuzhiyun return -EINVAL;
4140*4882a593Smuzhiyun
4141*4882a593Smuzhiyun return sctp_send_add_streams(asoc, params);
4142*4882a593Smuzhiyun }
4143*4882a593Smuzhiyun
sctp_setsockopt_scheduler(struct sock * sk,struct sctp_assoc_value * params,unsigned int optlen)4144*4882a593Smuzhiyun static int sctp_setsockopt_scheduler(struct sock *sk,
4145*4882a593Smuzhiyun struct sctp_assoc_value *params,
4146*4882a593Smuzhiyun unsigned int optlen)
4147*4882a593Smuzhiyun {
4148*4882a593Smuzhiyun struct sctp_sock *sp = sctp_sk(sk);
4149*4882a593Smuzhiyun struct sctp_association *asoc;
4150*4882a593Smuzhiyun int retval = 0;
4151*4882a593Smuzhiyun
4152*4882a593Smuzhiyun if (optlen < sizeof(*params))
4153*4882a593Smuzhiyun return -EINVAL;
4154*4882a593Smuzhiyun
4155*4882a593Smuzhiyun if (params->assoc_value > SCTP_SS_MAX)
4156*4882a593Smuzhiyun return -EINVAL;
4157*4882a593Smuzhiyun
4158*4882a593Smuzhiyun asoc = sctp_id2assoc(sk, params->assoc_id);
4159*4882a593Smuzhiyun if (!asoc && params->assoc_id > SCTP_ALL_ASSOC &&
4160*4882a593Smuzhiyun sctp_style(sk, UDP))
4161*4882a593Smuzhiyun return -EINVAL;
4162*4882a593Smuzhiyun
4163*4882a593Smuzhiyun if (asoc)
4164*4882a593Smuzhiyun return sctp_sched_set_sched(asoc, params->assoc_value);
4165*4882a593Smuzhiyun
4166*4882a593Smuzhiyun if (sctp_style(sk, TCP))
4167*4882a593Smuzhiyun params->assoc_id = SCTP_FUTURE_ASSOC;
4168*4882a593Smuzhiyun
4169*4882a593Smuzhiyun if (params->assoc_id == SCTP_FUTURE_ASSOC ||
4170*4882a593Smuzhiyun params->assoc_id == SCTP_ALL_ASSOC)
4171*4882a593Smuzhiyun sp->default_ss = params->assoc_value;
4172*4882a593Smuzhiyun
4173*4882a593Smuzhiyun if (params->assoc_id == SCTP_CURRENT_ASSOC ||
4174*4882a593Smuzhiyun params->assoc_id == SCTP_ALL_ASSOC) {
4175*4882a593Smuzhiyun list_for_each_entry(asoc, &sp->ep->asocs, asocs) {
4176*4882a593Smuzhiyun int ret = sctp_sched_set_sched(asoc,
4177*4882a593Smuzhiyun params->assoc_value);
4178*4882a593Smuzhiyun
4179*4882a593Smuzhiyun if (ret && !retval)
4180*4882a593Smuzhiyun retval = ret;
4181*4882a593Smuzhiyun }
4182*4882a593Smuzhiyun }
4183*4882a593Smuzhiyun
4184*4882a593Smuzhiyun return retval;
4185*4882a593Smuzhiyun }
4186*4882a593Smuzhiyun
sctp_setsockopt_scheduler_value(struct sock * sk,struct sctp_stream_value * params,unsigned int optlen)4187*4882a593Smuzhiyun static int sctp_setsockopt_scheduler_value(struct sock *sk,
4188*4882a593Smuzhiyun struct sctp_stream_value *params,
4189*4882a593Smuzhiyun unsigned int optlen)
4190*4882a593Smuzhiyun {
4191*4882a593Smuzhiyun struct sctp_association *asoc;
4192*4882a593Smuzhiyun int retval = -EINVAL;
4193*4882a593Smuzhiyun
4194*4882a593Smuzhiyun if (optlen < sizeof(*params))
4195*4882a593Smuzhiyun goto out;
4196*4882a593Smuzhiyun
4197*4882a593Smuzhiyun asoc = sctp_id2assoc(sk, params->assoc_id);
4198*4882a593Smuzhiyun if (!asoc && params->assoc_id != SCTP_CURRENT_ASSOC &&
4199*4882a593Smuzhiyun sctp_style(sk, UDP))
4200*4882a593Smuzhiyun goto out;
4201*4882a593Smuzhiyun
4202*4882a593Smuzhiyun if (asoc) {
4203*4882a593Smuzhiyun retval = sctp_sched_set_value(asoc, params->stream_id,
4204*4882a593Smuzhiyun params->stream_value, GFP_KERNEL);
4205*4882a593Smuzhiyun goto out;
4206*4882a593Smuzhiyun }
4207*4882a593Smuzhiyun
4208*4882a593Smuzhiyun retval = 0;
4209*4882a593Smuzhiyun
4210*4882a593Smuzhiyun list_for_each_entry(asoc, &sctp_sk(sk)->ep->asocs, asocs) {
4211*4882a593Smuzhiyun int ret = sctp_sched_set_value(asoc, params->stream_id,
4212*4882a593Smuzhiyun params->stream_value,
4213*4882a593Smuzhiyun GFP_KERNEL);
4214*4882a593Smuzhiyun if (ret && !retval) /* try to return the 1st error. */
4215*4882a593Smuzhiyun retval = ret;
4216*4882a593Smuzhiyun }
4217*4882a593Smuzhiyun
4218*4882a593Smuzhiyun out:
4219*4882a593Smuzhiyun return retval;
4220*4882a593Smuzhiyun }
4221*4882a593Smuzhiyun
sctp_setsockopt_interleaving_supported(struct sock * sk,struct sctp_assoc_value * p,unsigned int optlen)4222*4882a593Smuzhiyun static int sctp_setsockopt_interleaving_supported(struct sock *sk,
4223*4882a593Smuzhiyun struct sctp_assoc_value *p,
4224*4882a593Smuzhiyun unsigned int optlen)
4225*4882a593Smuzhiyun {
4226*4882a593Smuzhiyun struct sctp_sock *sp = sctp_sk(sk);
4227*4882a593Smuzhiyun struct sctp_association *asoc;
4228*4882a593Smuzhiyun
4229*4882a593Smuzhiyun if (optlen < sizeof(*p))
4230*4882a593Smuzhiyun return -EINVAL;
4231*4882a593Smuzhiyun
4232*4882a593Smuzhiyun asoc = sctp_id2assoc(sk, p->assoc_id);
4233*4882a593Smuzhiyun if (!asoc && p->assoc_id != SCTP_FUTURE_ASSOC && sctp_style(sk, UDP))
4234*4882a593Smuzhiyun return -EINVAL;
4235*4882a593Smuzhiyun
4236*4882a593Smuzhiyun if (!sock_net(sk)->sctp.intl_enable || !sp->frag_interleave) {
4237*4882a593Smuzhiyun return -EPERM;
4238*4882a593Smuzhiyun }
4239*4882a593Smuzhiyun
4240*4882a593Smuzhiyun sp->ep->intl_enable = !!p->assoc_value;
4241*4882a593Smuzhiyun return 0;
4242*4882a593Smuzhiyun }
4243*4882a593Smuzhiyun
sctp_setsockopt_reuse_port(struct sock * sk,int * val,unsigned int optlen)4244*4882a593Smuzhiyun static int sctp_setsockopt_reuse_port(struct sock *sk, int *val,
4245*4882a593Smuzhiyun unsigned int optlen)
4246*4882a593Smuzhiyun {
4247*4882a593Smuzhiyun if (!sctp_style(sk, TCP))
4248*4882a593Smuzhiyun return -EOPNOTSUPP;
4249*4882a593Smuzhiyun
4250*4882a593Smuzhiyun if (sctp_sk(sk)->ep->base.bind_addr.port)
4251*4882a593Smuzhiyun return -EFAULT;
4252*4882a593Smuzhiyun
4253*4882a593Smuzhiyun if (optlen < sizeof(int))
4254*4882a593Smuzhiyun return -EINVAL;
4255*4882a593Smuzhiyun
4256*4882a593Smuzhiyun sctp_sk(sk)->reuse = !!*val;
4257*4882a593Smuzhiyun
4258*4882a593Smuzhiyun return 0;
4259*4882a593Smuzhiyun }
4260*4882a593Smuzhiyun
sctp_assoc_ulpevent_type_set(struct sctp_event * param,struct sctp_association * asoc)4261*4882a593Smuzhiyun static int sctp_assoc_ulpevent_type_set(struct sctp_event *param,
4262*4882a593Smuzhiyun struct sctp_association *asoc)
4263*4882a593Smuzhiyun {
4264*4882a593Smuzhiyun struct sctp_ulpevent *event;
4265*4882a593Smuzhiyun
4266*4882a593Smuzhiyun sctp_ulpevent_type_set(&asoc->subscribe, param->se_type, param->se_on);
4267*4882a593Smuzhiyun
4268*4882a593Smuzhiyun if (param->se_type == SCTP_SENDER_DRY_EVENT && param->se_on) {
4269*4882a593Smuzhiyun if (sctp_outq_is_empty(&asoc->outqueue)) {
4270*4882a593Smuzhiyun event = sctp_ulpevent_make_sender_dry_event(asoc,
4271*4882a593Smuzhiyun GFP_USER | __GFP_NOWARN);
4272*4882a593Smuzhiyun if (!event)
4273*4882a593Smuzhiyun return -ENOMEM;
4274*4882a593Smuzhiyun
4275*4882a593Smuzhiyun asoc->stream.si->enqueue_event(&asoc->ulpq, event);
4276*4882a593Smuzhiyun }
4277*4882a593Smuzhiyun }
4278*4882a593Smuzhiyun
4279*4882a593Smuzhiyun return 0;
4280*4882a593Smuzhiyun }
4281*4882a593Smuzhiyun
sctp_setsockopt_event(struct sock * sk,struct sctp_event * param,unsigned int optlen)4282*4882a593Smuzhiyun static int sctp_setsockopt_event(struct sock *sk, struct sctp_event *param,
4283*4882a593Smuzhiyun unsigned int optlen)
4284*4882a593Smuzhiyun {
4285*4882a593Smuzhiyun struct sctp_sock *sp = sctp_sk(sk);
4286*4882a593Smuzhiyun struct sctp_association *asoc;
4287*4882a593Smuzhiyun int retval = 0;
4288*4882a593Smuzhiyun
4289*4882a593Smuzhiyun if (optlen < sizeof(*param))
4290*4882a593Smuzhiyun return -EINVAL;
4291*4882a593Smuzhiyun
4292*4882a593Smuzhiyun if (param->se_type < SCTP_SN_TYPE_BASE ||
4293*4882a593Smuzhiyun param->se_type > SCTP_SN_TYPE_MAX)
4294*4882a593Smuzhiyun return -EINVAL;
4295*4882a593Smuzhiyun
4296*4882a593Smuzhiyun asoc = sctp_id2assoc(sk, param->se_assoc_id);
4297*4882a593Smuzhiyun if (!asoc && param->se_assoc_id > SCTP_ALL_ASSOC &&
4298*4882a593Smuzhiyun sctp_style(sk, UDP))
4299*4882a593Smuzhiyun return -EINVAL;
4300*4882a593Smuzhiyun
4301*4882a593Smuzhiyun if (asoc)
4302*4882a593Smuzhiyun return sctp_assoc_ulpevent_type_set(param, asoc);
4303*4882a593Smuzhiyun
4304*4882a593Smuzhiyun if (sctp_style(sk, TCP))
4305*4882a593Smuzhiyun param->se_assoc_id = SCTP_FUTURE_ASSOC;
4306*4882a593Smuzhiyun
4307*4882a593Smuzhiyun if (param->se_assoc_id == SCTP_FUTURE_ASSOC ||
4308*4882a593Smuzhiyun param->se_assoc_id == SCTP_ALL_ASSOC)
4309*4882a593Smuzhiyun sctp_ulpevent_type_set(&sp->subscribe,
4310*4882a593Smuzhiyun param->se_type, param->se_on);
4311*4882a593Smuzhiyun
4312*4882a593Smuzhiyun if (param->se_assoc_id == SCTP_CURRENT_ASSOC ||
4313*4882a593Smuzhiyun param->se_assoc_id == SCTP_ALL_ASSOC) {
4314*4882a593Smuzhiyun list_for_each_entry(asoc, &sp->ep->asocs, asocs) {
4315*4882a593Smuzhiyun int ret = sctp_assoc_ulpevent_type_set(param, asoc);
4316*4882a593Smuzhiyun
4317*4882a593Smuzhiyun if (ret && !retval)
4318*4882a593Smuzhiyun retval = ret;
4319*4882a593Smuzhiyun }
4320*4882a593Smuzhiyun }
4321*4882a593Smuzhiyun
4322*4882a593Smuzhiyun return retval;
4323*4882a593Smuzhiyun }
4324*4882a593Smuzhiyun
sctp_setsockopt_asconf_supported(struct sock * sk,struct sctp_assoc_value * params,unsigned int optlen)4325*4882a593Smuzhiyun static int sctp_setsockopt_asconf_supported(struct sock *sk,
4326*4882a593Smuzhiyun struct sctp_assoc_value *params,
4327*4882a593Smuzhiyun unsigned int optlen)
4328*4882a593Smuzhiyun {
4329*4882a593Smuzhiyun struct sctp_association *asoc;
4330*4882a593Smuzhiyun struct sctp_endpoint *ep;
4331*4882a593Smuzhiyun int retval = -EINVAL;
4332*4882a593Smuzhiyun
4333*4882a593Smuzhiyun if (optlen != sizeof(*params))
4334*4882a593Smuzhiyun goto out;
4335*4882a593Smuzhiyun
4336*4882a593Smuzhiyun asoc = sctp_id2assoc(sk, params->assoc_id);
4337*4882a593Smuzhiyun if (!asoc && params->assoc_id != SCTP_FUTURE_ASSOC &&
4338*4882a593Smuzhiyun sctp_style(sk, UDP))
4339*4882a593Smuzhiyun goto out;
4340*4882a593Smuzhiyun
4341*4882a593Smuzhiyun ep = sctp_sk(sk)->ep;
4342*4882a593Smuzhiyun ep->asconf_enable = !!params->assoc_value;
4343*4882a593Smuzhiyun
4344*4882a593Smuzhiyun if (ep->asconf_enable && ep->auth_enable) {
4345*4882a593Smuzhiyun sctp_auth_ep_add_chunkid(ep, SCTP_CID_ASCONF);
4346*4882a593Smuzhiyun sctp_auth_ep_add_chunkid(ep, SCTP_CID_ASCONF_ACK);
4347*4882a593Smuzhiyun }
4348*4882a593Smuzhiyun
4349*4882a593Smuzhiyun retval = 0;
4350*4882a593Smuzhiyun
4351*4882a593Smuzhiyun out:
4352*4882a593Smuzhiyun return retval;
4353*4882a593Smuzhiyun }
4354*4882a593Smuzhiyun
sctp_setsockopt_auth_supported(struct sock * sk,struct sctp_assoc_value * params,unsigned int optlen)4355*4882a593Smuzhiyun static int sctp_setsockopt_auth_supported(struct sock *sk,
4356*4882a593Smuzhiyun struct sctp_assoc_value *params,
4357*4882a593Smuzhiyun unsigned int optlen)
4358*4882a593Smuzhiyun {
4359*4882a593Smuzhiyun struct sctp_association *asoc;
4360*4882a593Smuzhiyun struct sctp_endpoint *ep;
4361*4882a593Smuzhiyun int retval = -EINVAL;
4362*4882a593Smuzhiyun
4363*4882a593Smuzhiyun if (optlen != sizeof(*params))
4364*4882a593Smuzhiyun goto out;
4365*4882a593Smuzhiyun
4366*4882a593Smuzhiyun asoc = sctp_id2assoc(sk, params->assoc_id);
4367*4882a593Smuzhiyun if (!asoc && params->assoc_id != SCTP_FUTURE_ASSOC &&
4368*4882a593Smuzhiyun sctp_style(sk, UDP))
4369*4882a593Smuzhiyun goto out;
4370*4882a593Smuzhiyun
4371*4882a593Smuzhiyun ep = sctp_sk(sk)->ep;
4372*4882a593Smuzhiyun if (params->assoc_value) {
4373*4882a593Smuzhiyun retval = sctp_auth_init(ep, GFP_KERNEL);
4374*4882a593Smuzhiyun if (retval)
4375*4882a593Smuzhiyun goto out;
4376*4882a593Smuzhiyun if (ep->asconf_enable) {
4377*4882a593Smuzhiyun sctp_auth_ep_add_chunkid(ep, SCTP_CID_ASCONF);
4378*4882a593Smuzhiyun sctp_auth_ep_add_chunkid(ep, SCTP_CID_ASCONF_ACK);
4379*4882a593Smuzhiyun }
4380*4882a593Smuzhiyun }
4381*4882a593Smuzhiyun
4382*4882a593Smuzhiyun ep->auth_enable = !!params->assoc_value;
4383*4882a593Smuzhiyun retval = 0;
4384*4882a593Smuzhiyun
4385*4882a593Smuzhiyun out:
4386*4882a593Smuzhiyun return retval;
4387*4882a593Smuzhiyun }
4388*4882a593Smuzhiyun
sctp_setsockopt_ecn_supported(struct sock * sk,struct sctp_assoc_value * params,unsigned int optlen)4389*4882a593Smuzhiyun static int sctp_setsockopt_ecn_supported(struct sock *sk,
4390*4882a593Smuzhiyun struct sctp_assoc_value *params,
4391*4882a593Smuzhiyun unsigned int optlen)
4392*4882a593Smuzhiyun {
4393*4882a593Smuzhiyun struct sctp_association *asoc;
4394*4882a593Smuzhiyun int retval = -EINVAL;
4395*4882a593Smuzhiyun
4396*4882a593Smuzhiyun if (optlen != sizeof(*params))
4397*4882a593Smuzhiyun goto out;
4398*4882a593Smuzhiyun
4399*4882a593Smuzhiyun asoc = sctp_id2assoc(sk, params->assoc_id);
4400*4882a593Smuzhiyun if (!asoc && params->assoc_id != SCTP_FUTURE_ASSOC &&
4401*4882a593Smuzhiyun sctp_style(sk, UDP))
4402*4882a593Smuzhiyun goto out;
4403*4882a593Smuzhiyun
4404*4882a593Smuzhiyun sctp_sk(sk)->ep->ecn_enable = !!params->assoc_value;
4405*4882a593Smuzhiyun retval = 0;
4406*4882a593Smuzhiyun
4407*4882a593Smuzhiyun out:
4408*4882a593Smuzhiyun return retval;
4409*4882a593Smuzhiyun }
4410*4882a593Smuzhiyun
sctp_setsockopt_pf_expose(struct sock * sk,struct sctp_assoc_value * params,unsigned int optlen)4411*4882a593Smuzhiyun static int sctp_setsockopt_pf_expose(struct sock *sk,
4412*4882a593Smuzhiyun struct sctp_assoc_value *params,
4413*4882a593Smuzhiyun unsigned int optlen)
4414*4882a593Smuzhiyun {
4415*4882a593Smuzhiyun struct sctp_association *asoc;
4416*4882a593Smuzhiyun int retval = -EINVAL;
4417*4882a593Smuzhiyun
4418*4882a593Smuzhiyun if (optlen != sizeof(*params))
4419*4882a593Smuzhiyun goto out;
4420*4882a593Smuzhiyun
4421*4882a593Smuzhiyun if (params->assoc_value > SCTP_PF_EXPOSE_MAX)
4422*4882a593Smuzhiyun goto out;
4423*4882a593Smuzhiyun
4424*4882a593Smuzhiyun asoc = sctp_id2assoc(sk, params->assoc_id);
4425*4882a593Smuzhiyun if (!asoc && params->assoc_id != SCTP_FUTURE_ASSOC &&
4426*4882a593Smuzhiyun sctp_style(sk, UDP))
4427*4882a593Smuzhiyun goto out;
4428*4882a593Smuzhiyun
4429*4882a593Smuzhiyun if (asoc)
4430*4882a593Smuzhiyun asoc->pf_expose = params->assoc_value;
4431*4882a593Smuzhiyun else
4432*4882a593Smuzhiyun sctp_sk(sk)->pf_expose = params->assoc_value;
4433*4882a593Smuzhiyun retval = 0;
4434*4882a593Smuzhiyun
4435*4882a593Smuzhiyun out:
4436*4882a593Smuzhiyun return retval;
4437*4882a593Smuzhiyun }
4438*4882a593Smuzhiyun
4439*4882a593Smuzhiyun /* API 6.2 setsockopt(), getsockopt()
4440*4882a593Smuzhiyun *
4441*4882a593Smuzhiyun * Applications use setsockopt() and getsockopt() to set or retrieve
4442*4882a593Smuzhiyun * socket options. Socket options are used to change the default
4443*4882a593Smuzhiyun * behavior of sockets calls. They are described in Section 7.
4444*4882a593Smuzhiyun *
4445*4882a593Smuzhiyun * The syntax is:
4446*4882a593Smuzhiyun *
4447*4882a593Smuzhiyun * ret = getsockopt(int sd, int level, int optname, void __user *optval,
4448*4882a593Smuzhiyun * int __user *optlen);
4449*4882a593Smuzhiyun * ret = setsockopt(int sd, int level, int optname, const void __user *optval,
4450*4882a593Smuzhiyun * int optlen);
4451*4882a593Smuzhiyun *
4452*4882a593Smuzhiyun * sd - the socket descript.
4453*4882a593Smuzhiyun * level - set to IPPROTO_SCTP for all SCTP options.
4454*4882a593Smuzhiyun * optname - the option name.
4455*4882a593Smuzhiyun * optval - the buffer to store the value of the option.
4456*4882a593Smuzhiyun * optlen - the size of the buffer.
4457*4882a593Smuzhiyun */
sctp_setsockopt(struct sock * sk,int level,int optname,sockptr_t optval,unsigned int optlen)4458*4882a593Smuzhiyun static int sctp_setsockopt(struct sock *sk, int level, int optname,
4459*4882a593Smuzhiyun sockptr_t optval, unsigned int optlen)
4460*4882a593Smuzhiyun {
4461*4882a593Smuzhiyun void *kopt = NULL;
4462*4882a593Smuzhiyun int retval = 0;
4463*4882a593Smuzhiyun
4464*4882a593Smuzhiyun pr_debug("%s: sk:%p, optname:%d\n", __func__, sk, optname);
4465*4882a593Smuzhiyun
4466*4882a593Smuzhiyun /* I can hardly begin to describe how wrong this is. This is
4467*4882a593Smuzhiyun * so broken as to be worse than useless. The API draft
4468*4882a593Smuzhiyun * REALLY is NOT helpful here... I am not convinced that the
4469*4882a593Smuzhiyun * semantics of setsockopt() with a level OTHER THAN SOL_SCTP
4470*4882a593Smuzhiyun * are at all well-founded.
4471*4882a593Smuzhiyun */
4472*4882a593Smuzhiyun if (level != SOL_SCTP) {
4473*4882a593Smuzhiyun struct sctp_af *af = sctp_sk(sk)->pf->af;
4474*4882a593Smuzhiyun
4475*4882a593Smuzhiyun return af->setsockopt(sk, level, optname, optval, optlen);
4476*4882a593Smuzhiyun }
4477*4882a593Smuzhiyun
4478*4882a593Smuzhiyun if (optlen > 0) {
4479*4882a593Smuzhiyun /* Trim it to the biggest size sctp sockopt may need if necessary */
4480*4882a593Smuzhiyun optlen = min_t(unsigned int, optlen,
4481*4882a593Smuzhiyun PAGE_ALIGN(USHRT_MAX +
4482*4882a593Smuzhiyun sizeof(__u16) * sizeof(struct sctp_reset_streams)));
4483*4882a593Smuzhiyun kopt = memdup_sockptr(optval, optlen);
4484*4882a593Smuzhiyun if (IS_ERR(kopt))
4485*4882a593Smuzhiyun return PTR_ERR(kopt);
4486*4882a593Smuzhiyun }
4487*4882a593Smuzhiyun
4488*4882a593Smuzhiyun lock_sock(sk);
4489*4882a593Smuzhiyun
4490*4882a593Smuzhiyun switch (optname) {
4491*4882a593Smuzhiyun case SCTP_SOCKOPT_BINDX_ADD:
4492*4882a593Smuzhiyun /* 'optlen' is the size of the addresses buffer. */
4493*4882a593Smuzhiyun retval = sctp_setsockopt_bindx(sk, kopt, optlen,
4494*4882a593Smuzhiyun SCTP_BINDX_ADD_ADDR);
4495*4882a593Smuzhiyun break;
4496*4882a593Smuzhiyun
4497*4882a593Smuzhiyun case SCTP_SOCKOPT_BINDX_REM:
4498*4882a593Smuzhiyun /* 'optlen' is the size of the addresses buffer. */
4499*4882a593Smuzhiyun retval = sctp_setsockopt_bindx(sk, kopt, optlen,
4500*4882a593Smuzhiyun SCTP_BINDX_REM_ADDR);
4501*4882a593Smuzhiyun break;
4502*4882a593Smuzhiyun
4503*4882a593Smuzhiyun case SCTP_SOCKOPT_CONNECTX_OLD:
4504*4882a593Smuzhiyun /* 'optlen' is the size of the addresses buffer. */
4505*4882a593Smuzhiyun retval = sctp_setsockopt_connectx_old(sk, kopt, optlen);
4506*4882a593Smuzhiyun break;
4507*4882a593Smuzhiyun
4508*4882a593Smuzhiyun case SCTP_SOCKOPT_CONNECTX:
4509*4882a593Smuzhiyun /* 'optlen' is the size of the addresses buffer. */
4510*4882a593Smuzhiyun retval = sctp_setsockopt_connectx(sk, kopt, optlen);
4511*4882a593Smuzhiyun break;
4512*4882a593Smuzhiyun
4513*4882a593Smuzhiyun case SCTP_DISABLE_FRAGMENTS:
4514*4882a593Smuzhiyun retval = sctp_setsockopt_disable_fragments(sk, kopt, optlen);
4515*4882a593Smuzhiyun break;
4516*4882a593Smuzhiyun
4517*4882a593Smuzhiyun case SCTP_EVENTS:
4518*4882a593Smuzhiyun retval = sctp_setsockopt_events(sk, kopt, optlen);
4519*4882a593Smuzhiyun break;
4520*4882a593Smuzhiyun
4521*4882a593Smuzhiyun case SCTP_AUTOCLOSE:
4522*4882a593Smuzhiyun retval = sctp_setsockopt_autoclose(sk, kopt, optlen);
4523*4882a593Smuzhiyun break;
4524*4882a593Smuzhiyun
4525*4882a593Smuzhiyun case SCTP_PEER_ADDR_PARAMS:
4526*4882a593Smuzhiyun retval = sctp_setsockopt_peer_addr_params(sk, kopt, optlen);
4527*4882a593Smuzhiyun break;
4528*4882a593Smuzhiyun
4529*4882a593Smuzhiyun case SCTP_DELAYED_SACK:
4530*4882a593Smuzhiyun retval = sctp_setsockopt_delayed_ack(sk, kopt, optlen);
4531*4882a593Smuzhiyun break;
4532*4882a593Smuzhiyun case SCTP_PARTIAL_DELIVERY_POINT:
4533*4882a593Smuzhiyun retval = sctp_setsockopt_partial_delivery_point(sk, kopt, optlen);
4534*4882a593Smuzhiyun break;
4535*4882a593Smuzhiyun
4536*4882a593Smuzhiyun case SCTP_INITMSG:
4537*4882a593Smuzhiyun retval = sctp_setsockopt_initmsg(sk, kopt, optlen);
4538*4882a593Smuzhiyun break;
4539*4882a593Smuzhiyun case SCTP_DEFAULT_SEND_PARAM:
4540*4882a593Smuzhiyun retval = sctp_setsockopt_default_send_param(sk, kopt, optlen);
4541*4882a593Smuzhiyun break;
4542*4882a593Smuzhiyun case SCTP_DEFAULT_SNDINFO:
4543*4882a593Smuzhiyun retval = sctp_setsockopt_default_sndinfo(sk, kopt, optlen);
4544*4882a593Smuzhiyun break;
4545*4882a593Smuzhiyun case SCTP_PRIMARY_ADDR:
4546*4882a593Smuzhiyun retval = sctp_setsockopt_primary_addr(sk, kopt, optlen);
4547*4882a593Smuzhiyun break;
4548*4882a593Smuzhiyun case SCTP_SET_PEER_PRIMARY_ADDR:
4549*4882a593Smuzhiyun retval = sctp_setsockopt_peer_primary_addr(sk, kopt, optlen);
4550*4882a593Smuzhiyun break;
4551*4882a593Smuzhiyun case SCTP_NODELAY:
4552*4882a593Smuzhiyun retval = sctp_setsockopt_nodelay(sk, kopt, optlen);
4553*4882a593Smuzhiyun break;
4554*4882a593Smuzhiyun case SCTP_RTOINFO:
4555*4882a593Smuzhiyun retval = sctp_setsockopt_rtoinfo(sk, kopt, optlen);
4556*4882a593Smuzhiyun break;
4557*4882a593Smuzhiyun case SCTP_ASSOCINFO:
4558*4882a593Smuzhiyun retval = sctp_setsockopt_associnfo(sk, kopt, optlen);
4559*4882a593Smuzhiyun break;
4560*4882a593Smuzhiyun case SCTP_I_WANT_MAPPED_V4_ADDR:
4561*4882a593Smuzhiyun retval = sctp_setsockopt_mappedv4(sk, kopt, optlen);
4562*4882a593Smuzhiyun break;
4563*4882a593Smuzhiyun case SCTP_MAXSEG:
4564*4882a593Smuzhiyun retval = sctp_setsockopt_maxseg(sk, kopt, optlen);
4565*4882a593Smuzhiyun break;
4566*4882a593Smuzhiyun case SCTP_ADAPTATION_LAYER:
4567*4882a593Smuzhiyun retval = sctp_setsockopt_adaptation_layer(sk, kopt, optlen);
4568*4882a593Smuzhiyun break;
4569*4882a593Smuzhiyun case SCTP_CONTEXT:
4570*4882a593Smuzhiyun retval = sctp_setsockopt_context(sk, kopt, optlen);
4571*4882a593Smuzhiyun break;
4572*4882a593Smuzhiyun case SCTP_FRAGMENT_INTERLEAVE:
4573*4882a593Smuzhiyun retval = sctp_setsockopt_fragment_interleave(sk, kopt, optlen);
4574*4882a593Smuzhiyun break;
4575*4882a593Smuzhiyun case SCTP_MAX_BURST:
4576*4882a593Smuzhiyun retval = sctp_setsockopt_maxburst(sk, kopt, optlen);
4577*4882a593Smuzhiyun break;
4578*4882a593Smuzhiyun case SCTP_AUTH_CHUNK:
4579*4882a593Smuzhiyun retval = sctp_setsockopt_auth_chunk(sk, kopt, optlen);
4580*4882a593Smuzhiyun break;
4581*4882a593Smuzhiyun case SCTP_HMAC_IDENT:
4582*4882a593Smuzhiyun retval = sctp_setsockopt_hmac_ident(sk, kopt, optlen);
4583*4882a593Smuzhiyun break;
4584*4882a593Smuzhiyun case SCTP_AUTH_KEY:
4585*4882a593Smuzhiyun retval = sctp_setsockopt_auth_key(sk, kopt, optlen);
4586*4882a593Smuzhiyun break;
4587*4882a593Smuzhiyun case SCTP_AUTH_ACTIVE_KEY:
4588*4882a593Smuzhiyun retval = sctp_setsockopt_active_key(sk, kopt, optlen);
4589*4882a593Smuzhiyun break;
4590*4882a593Smuzhiyun case SCTP_AUTH_DELETE_KEY:
4591*4882a593Smuzhiyun retval = sctp_setsockopt_del_key(sk, kopt, optlen);
4592*4882a593Smuzhiyun break;
4593*4882a593Smuzhiyun case SCTP_AUTH_DEACTIVATE_KEY:
4594*4882a593Smuzhiyun retval = sctp_setsockopt_deactivate_key(sk, kopt, optlen);
4595*4882a593Smuzhiyun break;
4596*4882a593Smuzhiyun case SCTP_AUTO_ASCONF:
4597*4882a593Smuzhiyun retval = sctp_setsockopt_auto_asconf(sk, kopt, optlen);
4598*4882a593Smuzhiyun break;
4599*4882a593Smuzhiyun case SCTP_PEER_ADDR_THLDS:
4600*4882a593Smuzhiyun retval = sctp_setsockopt_paddr_thresholds(sk, kopt, optlen,
4601*4882a593Smuzhiyun false);
4602*4882a593Smuzhiyun break;
4603*4882a593Smuzhiyun case SCTP_PEER_ADDR_THLDS_V2:
4604*4882a593Smuzhiyun retval = sctp_setsockopt_paddr_thresholds(sk, kopt, optlen,
4605*4882a593Smuzhiyun true);
4606*4882a593Smuzhiyun break;
4607*4882a593Smuzhiyun case SCTP_RECVRCVINFO:
4608*4882a593Smuzhiyun retval = sctp_setsockopt_recvrcvinfo(sk, kopt, optlen);
4609*4882a593Smuzhiyun break;
4610*4882a593Smuzhiyun case SCTP_RECVNXTINFO:
4611*4882a593Smuzhiyun retval = sctp_setsockopt_recvnxtinfo(sk, kopt, optlen);
4612*4882a593Smuzhiyun break;
4613*4882a593Smuzhiyun case SCTP_PR_SUPPORTED:
4614*4882a593Smuzhiyun retval = sctp_setsockopt_pr_supported(sk, kopt, optlen);
4615*4882a593Smuzhiyun break;
4616*4882a593Smuzhiyun case SCTP_DEFAULT_PRINFO:
4617*4882a593Smuzhiyun retval = sctp_setsockopt_default_prinfo(sk, kopt, optlen);
4618*4882a593Smuzhiyun break;
4619*4882a593Smuzhiyun case SCTP_RECONFIG_SUPPORTED:
4620*4882a593Smuzhiyun retval = sctp_setsockopt_reconfig_supported(sk, kopt, optlen);
4621*4882a593Smuzhiyun break;
4622*4882a593Smuzhiyun case SCTP_ENABLE_STREAM_RESET:
4623*4882a593Smuzhiyun retval = sctp_setsockopt_enable_strreset(sk, kopt, optlen);
4624*4882a593Smuzhiyun break;
4625*4882a593Smuzhiyun case SCTP_RESET_STREAMS:
4626*4882a593Smuzhiyun retval = sctp_setsockopt_reset_streams(sk, kopt, optlen);
4627*4882a593Smuzhiyun break;
4628*4882a593Smuzhiyun case SCTP_RESET_ASSOC:
4629*4882a593Smuzhiyun retval = sctp_setsockopt_reset_assoc(sk, kopt, optlen);
4630*4882a593Smuzhiyun break;
4631*4882a593Smuzhiyun case SCTP_ADD_STREAMS:
4632*4882a593Smuzhiyun retval = sctp_setsockopt_add_streams(sk, kopt, optlen);
4633*4882a593Smuzhiyun break;
4634*4882a593Smuzhiyun case SCTP_STREAM_SCHEDULER:
4635*4882a593Smuzhiyun retval = sctp_setsockopt_scheduler(sk, kopt, optlen);
4636*4882a593Smuzhiyun break;
4637*4882a593Smuzhiyun case SCTP_STREAM_SCHEDULER_VALUE:
4638*4882a593Smuzhiyun retval = sctp_setsockopt_scheduler_value(sk, kopt, optlen);
4639*4882a593Smuzhiyun break;
4640*4882a593Smuzhiyun case SCTP_INTERLEAVING_SUPPORTED:
4641*4882a593Smuzhiyun retval = sctp_setsockopt_interleaving_supported(sk, kopt,
4642*4882a593Smuzhiyun optlen);
4643*4882a593Smuzhiyun break;
4644*4882a593Smuzhiyun case SCTP_REUSE_PORT:
4645*4882a593Smuzhiyun retval = sctp_setsockopt_reuse_port(sk, kopt, optlen);
4646*4882a593Smuzhiyun break;
4647*4882a593Smuzhiyun case SCTP_EVENT:
4648*4882a593Smuzhiyun retval = sctp_setsockopt_event(sk, kopt, optlen);
4649*4882a593Smuzhiyun break;
4650*4882a593Smuzhiyun case SCTP_ASCONF_SUPPORTED:
4651*4882a593Smuzhiyun retval = sctp_setsockopt_asconf_supported(sk, kopt, optlen);
4652*4882a593Smuzhiyun break;
4653*4882a593Smuzhiyun case SCTP_AUTH_SUPPORTED:
4654*4882a593Smuzhiyun retval = sctp_setsockopt_auth_supported(sk, kopt, optlen);
4655*4882a593Smuzhiyun break;
4656*4882a593Smuzhiyun case SCTP_ECN_SUPPORTED:
4657*4882a593Smuzhiyun retval = sctp_setsockopt_ecn_supported(sk, kopt, optlen);
4658*4882a593Smuzhiyun break;
4659*4882a593Smuzhiyun case SCTP_EXPOSE_POTENTIALLY_FAILED_STATE:
4660*4882a593Smuzhiyun retval = sctp_setsockopt_pf_expose(sk, kopt, optlen);
4661*4882a593Smuzhiyun break;
4662*4882a593Smuzhiyun default:
4663*4882a593Smuzhiyun retval = -ENOPROTOOPT;
4664*4882a593Smuzhiyun break;
4665*4882a593Smuzhiyun }
4666*4882a593Smuzhiyun
4667*4882a593Smuzhiyun release_sock(sk);
4668*4882a593Smuzhiyun kfree(kopt);
4669*4882a593Smuzhiyun return retval;
4670*4882a593Smuzhiyun }
4671*4882a593Smuzhiyun
4672*4882a593Smuzhiyun /* API 3.1.6 connect() - UDP Style Syntax
4673*4882a593Smuzhiyun *
4674*4882a593Smuzhiyun * An application may use the connect() call in the UDP model to initiate an
4675*4882a593Smuzhiyun * association without sending data.
4676*4882a593Smuzhiyun *
4677*4882a593Smuzhiyun * The syntax is:
4678*4882a593Smuzhiyun *
4679*4882a593Smuzhiyun * ret = connect(int sd, const struct sockaddr *nam, socklen_t len);
4680*4882a593Smuzhiyun *
4681*4882a593Smuzhiyun * sd: the socket descriptor to have a new association added to.
4682*4882a593Smuzhiyun *
4683*4882a593Smuzhiyun * nam: the address structure (either struct sockaddr_in or struct
4684*4882a593Smuzhiyun * sockaddr_in6 defined in RFC2553 [7]).
4685*4882a593Smuzhiyun *
4686*4882a593Smuzhiyun * len: the size of the address.
4687*4882a593Smuzhiyun */
sctp_connect(struct sock * sk,struct sockaddr * addr,int addr_len,int flags)4688*4882a593Smuzhiyun static int sctp_connect(struct sock *sk, struct sockaddr *addr,
4689*4882a593Smuzhiyun int addr_len, int flags)
4690*4882a593Smuzhiyun {
4691*4882a593Smuzhiyun struct sctp_af *af;
4692*4882a593Smuzhiyun int err = -EINVAL;
4693*4882a593Smuzhiyun
4694*4882a593Smuzhiyun lock_sock(sk);
4695*4882a593Smuzhiyun pr_debug("%s: sk:%p, sockaddr:%p, addr_len:%d\n", __func__, sk,
4696*4882a593Smuzhiyun addr, addr_len);
4697*4882a593Smuzhiyun
4698*4882a593Smuzhiyun /* Validate addr_len before calling common connect/connectx routine. */
4699*4882a593Smuzhiyun af = sctp_get_af_specific(addr->sa_family);
4700*4882a593Smuzhiyun if (af && addr_len >= af->sockaddr_len)
4701*4882a593Smuzhiyun err = __sctp_connect(sk, addr, af->sockaddr_len, flags, NULL);
4702*4882a593Smuzhiyun
4703*4882a593Smuzhiyun release_sock(sk);
4704*4882a593Smuzhiyun return err;
4705*4882a593Smuzhiyun }
4706*4882a593Smuzhiyun
sctp_inet_connect(struct socket * sock,struct sockaddr * uaddr,int addr_len,int flags)4707*4882a593Smuzhiyun int sctp_inet_connect(struct socket *sock, struct sockaddr *uaddr,
4708*4882a593Smuzhiyun int addr_len, int flags)
4709*4882a593Smuzhiyun {
4710*4882a593Smuzhiyun if (addr_len < sizeof(uaddr->sa_family))
4711*4882a593Smuzhiyun return -EINVAL;
4712*4882a593Smuzhiyun
4713*4882a593Smuzhiyun if (uaddr->sa_family == AF_UNSPEC)
4714*4882a593Smuzhiyun return -EOPNOTSUPP;
4715*4882a593Smuzhiyun
4716*4882a593Smuzhiyun return sctp_connect(sock->sk, uaddr, addr_len, flags);
4717*4882a593Smuzhiyun }
4718*4882a593Smuzhiyun
4719*4882a593Smuzhiyun /* FIXME: Write comments. */
sctp_disconnect(struct sock * sk,int flags)4720*4882a593Smuzhiyun static int sctp_disconnect(struct sock *sk, int flags)
4721*4882a593Smuzhiyun {
4722*4882a593Smuzhiyun return -EOPNOTSUPP; /* STUB */
4723*4882a593Smuzhiyun }
4724*4882a593Smuzhiyun
4725*4882a593Smuzhiyun /* 4.1.4 accept() - TCP Style Syntax
4726*4882a593Smuzhiyun *
4727*4882a593Smuzhiyun * Applications use accept() call to remove an established SCTP
4728*4882a593Smuzhiyun * association from the accept queue of the endpoint. A new socket
4729*4882a593Smuzhiyun * descriptor will be returned from accept() to represent the newly
4730*4882a593Smuzhiyun * formed association.
4731*4882a593Smuzhiyun */
sctp_accept(struct sock * sk,int flags,int * err,bool kern)4732*4882a593Smuzhiyun static struct sock *sctp_accept(struct sock *sk, int flags, int *err, bool kern)
4733*4882a593Smuzhiyun {
4734*4882a593Smuzhiyun struct sctp_sock *sp;
4735*4882a593Smuzhiyun struct sctp_endpoint *ep;
4736*4882a593Smuzhiyun struct sock *newsk = NULL;
4737*4882a593Smuzhiyun struct sctp_association *asoc;
4738*4882a593Smuzhiyun long timeo;
4739*4882a593Smuzhiyun int error = 0;
4740*4882a593Smuzhiyun
4741*4882a593Smuzhiyun lock_sock(sk);
4742*4882a593Smuzhiyun
4743*4882a593Smuzhiyun sp = sctp_sk(sk);
4744*4882a593Smuzhiyun ep = sp->ep;
4745*4882a593Smuzhiyun
4746*4882a593Smuzhiyun if (!sctp_style(sk, TCP)) {
4747*4882a593Smuzhiyun error = -EOPNOTSUPP;
4748*4882a593Smuzhiyun goto out;
4749*4882a593Smuzhiyun }
4750*4882a593Smuzhiyun
4751*4882a593Smuzhiyun if (!sctp_sstate(sk, LISTENING)) {
4752*4882a593Smuzhiyun error = -EINVAL;
4753*4882a593Smuzhiyun goto out;
4754*4882a593Smuzhiyun }
4755*4882a593Smuzhiyun
4756*4882a593Smuzhiyun timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
4757*4882a593Smuzhiyun
4758*4882a593Smuzhiyun error = sctp_wait_for_accept(sk, timeo);
4759*4882a593Smuzhiyun if (error)
4760*4882a593Smuzhiyun goto out;
4761*4882a593Smuzhiyun
4762*4882a593Smuzhiyun /* We treat the list of associations on the endpoint as the accept
4763*4882a593Smuzhiyun * queue and pick the first association on the list.
4764*4882a593Smuzhiyun */
4765*4882a593Smuzhiyun asoc = list_entry(ep->asocs.next, struct sctp_association, asocs);
4766*4882a593Smuzhiyun
4767*4882a593Smuzhiyun newsk = sp->pf->create_accept_sk(sk, asoc, kern);
4768*4882a593Smuzhiyun if (!newsk) {
4769*4882a593Smuzhiyun error = -ENOMEM;
4770*4882a593Smuzhiyun goto out;
4771*4882a593Smuzhiyun }
4772*4882a593Smuzhiyun
4773*4882a593Smuzhiyun /* Populate the fields of the newsk from the oldsk and migrate the
4774*4882a593Smuzhiyun * asoc to the newsk.
4775*4882a593Smuzhiyun */
4776*4882a593Smuzhiyun error = sctp_sock_migrate(sk, newsk, asoc, SCTP_SOCKET_TCP);
4777*4882a593Smuzhiyun if (error) {
4778*4882a593Smuzhiyun sk_common_release(newsk);
4779*4882a593Smuzhiyun newsk = NULL;
4780*4882a593Smuzhiyun }
4781*4882a593Smuzhiyun
4782*4882a593Smuzhiyun out:
4783*4882a593Smuzhiyun release_sock(sk);
4784*4882a593Smuzhiyun *err = error;
4785*4882a593Smuzhiyun return newsk;
4786*4882a593Smuzhiyun }
4787*4882a593Smuzhiyun
4788*4882a593Smuzhiyun /* The SCTP ioctl handler. */
sctp_ioctl(struct sock * sk,int cmd,unsigned long arg)4789*4882a593Smuzhiyun static int sctp_ioctl(struct sock *sk, int cmd, unsigned long arg)
4790*4882a593Smuzhiyun {
4791*4882a593Smuzhiyun int rc = -ENOTCONN;
4792*4882a593Smuzhiyun
4793*4882a593Smuzhiyun lock_sock(sk);
4794*4882a593Smuzhiyun
4795*4882a593Smuzhiyun /*
4796*4882a593Smuzhiyun * SEQPACKET-style sockets in LISTENING state are valid, for
4797*4882a593Smuzhiyun * SCTP, so only discard TCP-style sockets in LISTENING state.
4798*4882a593Smuzhiyun */
4799*4882a593Smuzhiyun if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
4800*4882a593Smuzhiyun goto out;
4801*4882a593Smuzhiyun
4802*4882a593Smuzhiyun switch (cmd) {
4803*4882a593Smuzhiyun case SIOCINQ: {
4804*4882a593Smuzhiyun struct sk_buff *skb;
4805*4882a593Smuzhiyun unsigned int amount = 0;
4806*4882a593Smuzhiyun
4807*4882a593Smuzhiyun skb = skb_peek(&sk->sk_receive_queue);
4808*4882a593Smuzhiyun if (skb != NULL) {
4809*4882a593Smuzhiyun /*
4810*4882a593Smuzhiyun * We will only return the amount of this packet since
4811*4882a593Smuzhiyun * that is all that will be read.
4812*4882a593Smuzhiyun */
4813*4882a593Smuzhiyun amount = skb->len;
4814*4882a593Smuzhiyun }
4815*4882a593Smuzhiyun rc = put_user(amount, (int __user *)arg);
4816*4882a593Smuzhiyun break;
4817*4882a593Smuzhiyun }
4818*4882a593Smuzhiyun default:
4819*4882a593Smuzhiyun rc = -ENOIOCTLCMD;
4820*4882a593Smuzhiyun break;
4821*4882a593Smuzhiyun }
4822*4882a593Smuzhiyun out:
4823*4882a593Smuzhiyun release_sock(sk);
4824*4882a593Smuzhiyun return rc;
4825*4882a593Smuzhiyun }
4826*4882a593Smuzhiyun
4827*4882a593Smuzhiyun /* This is the function which gets called during socket creation to
4828*4882a593Smuzhiyun * initialized the SCTP-specific portion of the sock.
4829*4882a593Smuzhiyun * The sock structure should already be zero-filled memory.
4830*4882a593Smuzhiyun */
sctp_init_sock(struct sock * sk)4831*4882a593Smuzhiyun static int sctp_init_sock(struct sock *sk)
4832*4882a593Smuzhiyun {
4833*4882a593Smuzhiyun struct net *net = sock_net(sk);
4834*4882a593Smuzhiyun struct sctp_sock *sp;
4835*4882a593Smuzhiyun
4836*4882a593Smuzhiyun pr_debug("%s: sk:%p\n", __func__, sk);
4837*4882a593Smuzhiyun
4838*4882a593Smuzhiyun sp = sctp_sk(sk);
4839*4882a593Smuzhiyun
4840*4882a593Smuzhiyun /* Initialize the SCTP per socket area. */
4841*4882a593Smuzhiyun switch (sk->sk_type) {
4842*4882a593Smuzhiyun case SOCK_SEQPACKET:
4843*4882a593Smuzhiyun sp->type = SCTP_SOCKET_UDP;
4844*4882a593Smuzhiyun break;
4845*4882a593Smuzhiyun case SOCK_STREAM:
4846*4882a593Smuzhiyun sp->type = SCTP_SOCKET_TCP;
4847*4882a593Smuzhiyun break;
4848*4882a593Smuzhiyun default:
4849*4882a593Smuzhiyun return -ESOCKTNOSUPPORT;
4850*4882a593Smuzhiyun }
4851*4882a593Smuzhiyun
4852*4882a593Smuzhiyun sk->sk_gso_type = SKB_GSO_SCTP;
4853*4882a593Smuzhiyun
4854*4882a593Smuzhiyun /* Initialize default send parameters. These parameters can be
4855*4882a593Smuzhiyun * modified with the SCTP_DEFAULT_SEND_PARAM socket option.
4856*4882a593Smuzhiyun */
4857*4882a593Smuzhiyun sp->default_stream = 0;
4858*4882a593Smuzhiyun sp->default_ppid = 0;
4859*4882a593Smuzhiyun sp->default_flags = 0;
4860*4882a593Smuzhiyun sp->default_context = 0;
4861*4882a593Smuzhiyun sp->default_timetolive = 0;
4862*4882a593Smuzhiyun
4863*4882a593Smuzhiyun sp->default_rcv_context = 0;
4864*4882a593Smuzhiyun sp->max_burst = net->sctp.max_burst;
4865*4882a593Smuzhiyun
4866*4882a593Smuzhiyun sp->sctp_hmac_alg = net->sctp.sctp_hmac_alg;
4867*4882a593Smuzhiyun
4868*4882a593Smuzhiyun /* Initialize default setup parameters. These parameters
4869*4882a593Smuzhiyun * can be modified with the SCTP_INITMSG socket option or
4870*4882a593Smuzhiyun * overridden by the SCTP_INIT CMSG.
4871*4882a593Smuzhiyun */
4872*4882a593Smuzhiyun sp->initmsg.sinit_num_ostreams = sctp_max_outstreams;
4873*4882a593Smuzhiyun sp->initmsg.sinit_max_instreams = sctp_max_instreams;
4874*4882a593Smuzhiyun sp->initmsg.sinit_max_attempts = net->sctp.max_retrans_init;
4875*4882a593Smuzhiyun sp->initmsg.sinit_max_init_timeo = net->sctp.rto_max;
4876*4882a593Smuzhiyun
4877*4882a593Smuzhiyun /* Initialize default RTO related parameters. These parameters can
4878*4882a593Smuzhiyun * be modified for with the SCTP_RTOINFO socket option.
4879*4882a593Smuzhiyun */
4880*4882a593Smuzhiyun sp->rtoinfo.srto_initial = net->sctp.rto_initial;
4881*4882a593Smuzhiyun sp->rtoinfo.srto_max = net->sctp.rto_max;
4882*4882a593Smuzhiyun sp->rtoinfo.srto_min = net->sctp.rto_min;
4883*4882a593Smuzhiyun
4884*4882a593Smuzhiyun /* Initialize default association related parameters. These parameters
4885*4882a593Smuzhiyun * can be modified with the SCTP_ASSOCINFO socket option.
4886*4882a593Smuzhiyun */
4887*4882a593Smuzhiyun sp->assocparams.sasoc_asocmaxrxt = net->sctp.max_retrans_association;
4888*4882a593Smuzhiyun sp->assocparams.sasoc_number_peer_destinations = 0;
4889*4882a593Smuzhiyun sp->assocparams.sasoc_peer_rwnd = 0;
4890*4882a593Smuzhiyun sp->assocparams.sasoc_local_rwnd = 0;
4891*4882a593Smuzhiyun sp->assocparams.sasoc_cookie_life = net->sctp.valid_cookie_life;
4892*4882a593Smuzhiyun
4893*4882a593Smuzhiyun /* Initialize default event subscriptions. By default, all the
4894*4882a593Smuzhiyun * options are off.
4895*4882a593Smuzhiyun */
4896*4882a593Smuzhiyun sp->subscribe = 0;
4897*4882a593Smuzhiyun
4898*4882a593Smuzhiyun /* Default Peer Address Parameters. These defaults can
4899*4882a593Smuzhiyun * be modified via SCTP_PEER_ADDR_PARAMS
4900*4882a593Smuzhiyun */
4901*4882a593Smuzhiyun sp->hbinterval = net->sctp.hb_interval;
4902*4882a593Smuzhiyun sp->pathmaxrxt = net->sctp.max_retrans_path;
4903*4882a593Smuzhiyun sp->pf_retrans = net->sctp.pf_retrans;
4904*4882a593Smuzhiyun sp->ps_retrans = net->sctp.ps_retrans;
4905*4882a593Smuzhiyun sp->pf_expose = net->sctp.pf_expose;
4906*4882a593Smuzhiyun sp->pathmtu = 0; /* allow default discovery */
4907*4882a593Smuzhiyun sp->sackdelay = net->sctp.sack_timeout;
4908*4882a593Smuzhiyun sp->sackfreq = 2;
4909*4882a593Smuzhiyun sp->param_flags = SPP_HB_ENABLE |
4910*4882a593Smuzhiyun SPP_PMTUD_ENABLE |
4911*4882a593Smuzhiyun SPP_SACKDELAY_ENABLE;
4912*4882a593Smuzhiyun sp->default_ss = SCTP_SS_DEFAULT;
4913*4882a593Smuzhiyun
4914*4882a593Smuzhiyun /* If enabled no SCTP message fragmentation will be performed.
4915*4882a593Smuzhiyun * Configure through SCTP_DISABLE_FRAGMENTS socket option.
4916*4882a593Smuzhiyun */
4917*4882a593Smuzhiyun sp->disable_fragments = 0;
4918*4882a593Smuzhiyun
4919*4882a593Smuzhiyun /* Enable Nagle algorithm by default. */
4920*4882a593Smuzhiyun sp->nodelay = 0;
4921*4882a593Smuzhiyun
4922*4882a593Smuzhiyun sp->recvrcvinfo = 0;
4923*4882a593Smuzhiyun sp->recvnxtinfo = 0;
4924*4882a593Smuzhiyun
4925*4882a593Smuzhiyun /* Enable by default. */
4926*4882a593Smuzhiyun sp->v4mapped = 1;
4927*4882a593Smuzhiyun
4928*4882a593Smuzhiyun /* Auto-close idle associations after the configured
4929*4882a593Smuzhiyun * number of seconds. A value of 0 disables this
4930*4882a593Smuzhiyun * feature. Configure through the SCTP_AUTOCLOSE socket option,
4931*4882a593Smuzhiyun * for UDP-style sockets only.
4932*4882a593Smuzhiyun */
4933*4882a593Smuzhiyun sp->autoclose = 0;
4934*4882a593Smuzhiyun
4935*4882a593Smuzhiyun /* User specified fragmentation limit. */
4936*4882a593Smuzhiyun sp->user_frag = 0;
4937*4882a593Smuzhiyun
4938*4882a593Smuzhiyun sp->adaptation_ind = 0;
4939*4882a593Smuzhiyun
4940*4882a593Smuzhiyun sp->pf = sctp_get_pf_specific(sk->sk_family);
4941*4882a593Smuzhiyun
4942*4882a593Smuzhiyun /* Control variables for partial data delivery. */
4943*4882a593Smuzhiyun atomic_set(&sp->pd_mode, 0);
4944*4882a593Smuzhiyun skb_queue_head_init(&sp->pd_lobby);
4945*4882a593Smuzhiyun sp->frag_interleave = 0;
4946*4882a593Smuzhiyun
4947*4882a593Smuzhiyun /* Create a per socket endpoint structure. Even if we
4948*4882a593Smuzhiyun * change the data structure relationships, this may still
4949*4882a593Smuzhiyun * be useful for storing pre-connect address information.
4950*4882a593Smuzhiyun */
4951*4882a593Smuzhiyun sp->ep = sctp_endpoint_new(sk, GFP_KERNEL);
4952*4882a593Smuzhiyun if (!sp->ep)
4953*4882a593Smuzhiyun return -ENOMEM;
4954*4882a593Smuzhiyun
4955*4882a593Smuzhiyun sp->hmac = NULL;
4956*4882a593Smuzhiyun
4957*4882a593Smuzhiyun sk->sk_destruct = sctp_destruct_sock;
4958*4882a593Smuzhiyun
4959*4882a593Smuzhiyun SCTP_DBG_OBJCNT_INC(sock);
4960*4882a593Smuzhiyun
4961*4882a593Smuzhiyun local_bh_disable();
4962*4882a593Smuzhiyun sk_sockets_allocated_inc(sk);
4963*4882a593Smuzhiyun sock_prot_inuse_add(net, sk->sk_prot, 1);
4964*4882a593Smuzhiyun
4965*4882a593Smuzhiyun local_bh_enable();
4966*4882a593Smuzhiyun
4967*4882a593Smuzhiyun return 0;
4968*4882a593Smuzhiyun }
4969*4882a593Smuzhiyun
4970*4882a593Smuzhiyun /* Cleanup any SCTP per socket resources. Must be called with
4971*4882a593Smuzhiyun * sock_net(sk)->sctp.addr_wq_lock held if sp->do_auto_asconf is true
4972*4882a593Smuzhiyun */
sctp_destroy_sock(struct sock * sk)4973*4882a593Smuzhiyun static void sctp_destroy_sock(struct sock *sk)
4974*4882a593Smuzhiyun {
4975*4882a593Smuzhiyun struct sctp_sock *sp;
4976*4882a593Smuzhiyun
4977*4882a593Smuzhiyun pr_debug("%s: sk:%p\n", __func__, sk);
4978*4882a593Smuzhiyun
4979*4882a593Smuzhiyun /* Release our hold on the endpoint. */
4980*4882a593Smuzhiyun sp = sctp_sk(sk);
4981*4882a593Smuzhiyun /* This could happen during socket init, thus we bail out
4982*4882a593Smuzhiyun * early, since the rest of the below is not setup either.
4983*4882a593Smuzhiyun */
4984*4882a593Smuzhiyun if (sp->ep == NULL)
4985*4882a593Smuzhiyun return;
4986*4882a593Smuzhiyun
4987*4882a593Smuzhiyun if (sp->do_auto_asconf) {
4988*4882a593Smuzhiyun sp->do_auto_asconf = 0;
4989*4882a593Smuzhiyun list_del(&sp->auto_asconf_list);
4990*4882a593Smuzhiyun }
4991*4882a593Smuzhiyun sctp_endpoint_free(sp->ep);
4992*4882a593Smuzhiyun local_bh_disable();
4993*4882a593Smuzhiyun sk_sockets_allocated_dec(sk);
4994*4882a593Smuzhiyun sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
4995*4882a593Smuzhiyun local_bh_enable();
4996*4882a593Smuzhiyun }
4997*4882a593Smuzhiyun
4998*4882a593Smuzhiyun /* Triggered when there are no references on the socket anymore */
sctp_destruct_sock(struct sock * sk)4999*4882a593Smuzhiyun static void sctp_destruct_sock(struct sock *sk)
5000*4882a593Smuzhiyun {
5001*4882a593Smuzhiyun struct sctp_sock *sp = sctp_sk(sk);
5002*4882a593Smuzhiyun
5003*4882a593Smuzhiyun /* Free up the HMAC transform. */
5004*4882a593Smuzhiyun crypto_free_shash(sp->hmac);
5005*4882a593Smuzhiyun
5006*4882a593Smuzhiyun inet_sock_destruct(sk);
5007*4882a593Smuzhiyun }
5008*4882a593Smuzhiyun
5009*4882a593Smuzhiyun /* API 4.1.7 shutdown() - TCP Style Syntax
5010*4882a593Smuzhiyun * int shutdown(int socket, int how);
5011*4882a593Smuzhiyun *
5012*4882a593Smuzhiyun * sd - the socket descriptor of the association to be closed.
5013*4882a593Smuzhiyun * how - Specifies the type of shutdown. The values are
5014*4882a593Smuzhiyun * as follows:
5015*4882a593Smuzhiyun * SHUT_RD
5016*4882a593Smuzhiyun * Disables further receive operations. No SCTP
5017*4882a593Smuzhiyun * protocol action is taken.
5018*4882a593Smuzhiyun * SHUT_WR
5019*4882a593Smuzhiyun * Disables further send operations, and initiates
5020*4882a593Smuzhiyun * the SCTP shutdown sequence.
5021*4882a593Smuzhiyun * SHUT_RDWR
5022*4882a593Smuzhiyun * Disables further send and receive operations
5023*4882a593Smuzhiyun * and initiates the SCTP shutdown sequence.
5024*4882a593Smuzhiyun */
sctp_shutdown(struct sock * sk,int how)5025*4882a593Smuzhiyun static void sctp_shutdown(struct sock *sk, int how)
5026*4882a593Smuzhiyun {
5027*4882a593Smuzhiyun struct net *net = sock_net(sk);
5028*4882a593Smuzhiyun struct sctp_endpoint *ep;
5029*4882a593Smuzhiyun
5030*4882a593Smuzhiyun if (!sctp_style(sk, TCP))
5031*4882a593Smuzhiyun return;
5032*4882a593Smuzhiyun
5033*4882a593Smuzhiyun ep = sctp_sk(sk)->ep;
5034*4882a593Smuzhiyun if (how & SEND_SHUTDOWN && !list_empty(&ep->asocs)) {
5035*4882a593Smuzhiyun struct sctp_association *asoc;
5036*4882a593Smuzhiyun
5037*4882a593Smuzhiyun inet_sk_set_state(sk, SCTP_SS_CLOSING);
5038*4882a593Smuzhiyun asoc = list_entry(ep->asocs.next,
5039*4882a593Smuzhiyun struct sctp_association, asocs);
5040*4882a593Smuzhiyun sctp_primitive_SHUTDOWN(net, asoc, NULL);
5041*4882a593Smuzhiyun }
5042*4882a593Smuzhiyun }
5043*4882a593Smuzhiyun
sctp_get_sctp_info(struct sock * sk,struct sctp_association * asoc,struct sctp_info * info)5044*4882a593Smuzhiyun int sctp_get_sctp_info(struct sock *sk, struct sctp_association *asoc,
5045*4882a593Smuzhiyun struct sctp_info *info)
5046*4882a593Smuzhiyun {
5047*4882a593Smuzhiyun struct sctp_transport *prim;
5048*4882a593Smuzhiyun struct list_head *pos;
5049*4882a593Smuzhiyun int mask;
5050*4882a593Smuzhiyun
5051*4882a593Smuzhiyun memset(info, 0, sizeof(*info));
5052*4882a593Smuzhiyun if (!asoc) {
5053*4882a593Smuzhiyun struct sctp_sock *sp = sctp_sk(sk);
5054*4882a593Smuzhiyun
5055*4882a593Smuzhiyun info->sctpi_s_autoclose = sp->autoclose;
5056*4882a593Smuzhiyun info->sctpi_s_adaptation_ind = sp->adaptation_ind;
5057*4882a593Smuzhiyun info->sctpi_s_pd_point = sp->pd_point;
5058*4882a593Smuzhiyun info->sctpi_s_nodelay = sp->nodelay;
5059*4882a593Smuzhiyun info->sctpi_s_disable_fragments = sp->disable_fragments;
5060*4882a593Smuzhiyun info->sctpi_s_v4mapped = sp->v4mapped;
5061*4882a593Smuzhiyun info->sctpi_s_frag_interleave = sp->frag_interleave;
5062*4882a593Smuzhiyun info->sctpi_s_type = sp->type;
5063*4882a593Smuzhiyun
5064*4882a593Smuzhiyun return 0;
5065*4882a593Smuzhiyun }
5066*4882a593Smuzhiyun
5067*4882a593Smuzhiyun info->sctpi_tag = asoc->c.my_vtag;
5068*4882a593Smuzhiyun info->sctpi_state = asoc->state;
5069*4882a593Smuzhiyun info->sctpi_rwnd = asoc->a_rwnd;
5070*4882a593Smuzhiyun info->sctpi_unackdata = asoc->unack_data;
5071*4882a593Smuzhiyun info->sctpi_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map);
5072*4882a593Smuzhiyun info->sctpi_instrms = asoc->stream.incnt;
5073*4882a593Smuzhiyun info->sctpi_outstrms = asoc->stream.outcnt;
5074*4882a593Smuzhiyun list_for_each(pos, &asoc->base.inqueue.in_chunk_list)
5075*4882a593Smuzhiyun info->sctpi_inqueue++;
5076*4882a593Smuzhiyun list_for_each(pos, &asoc->outqueue.out_chunk_list)
5077*4882a593Smuzhiyun info->sctpi_outqueue++;
5078*4882a593Smuzhiyun info->sctpi_overall_error = asoc->overall_error_count;
5079*4882a593Smuzhiyun info->sctpi_max_burst = asoc->max_burst;
5080*4882a593Smuzhiyun info->sctpi_maxseg = asoc->frag_point;
5081*4882a593Smuzhiyun info->sctpi_peer_rwnd = asoc->peer.rwnd;
5082*4882a593Smuzhiyun info->sctpi_peer_tag = asoc->c.peer_vtag;
5083*4882a593Smuzhiyun
5084*4882a593Smuzhiyun mask = asoc->peer.ecn_capable << 1;
5085*4882a593Smuzhiyun mask = (mask | asoc->peer.ipv4_address) << 1;
5086*4882a593Smuzhiyun mask = (mask | asoc->peer.ipv6_address) << 1;
5087*4882a593Smuzhiyun mask = (mask | asoc->peer.hostname_address) << 1;
5088*4882a593Smuzhiyun mask = (mask | asoc->peer.asconf_capable) << 1;
5089*4882a593Smuzhiyun mask = (mask | asoc->peer.prsctp_capable) << 1;
5090*4882a593Smuzhiyun mask = (mask | asoc->peer.auth_capable);
5091*4882a593Smuzhiyun info->sctpi_peer_capable = mask;
5092*4882a593Smuzhiyun mask = asoc->peer.sack_needed << 1;
5093*4882a593Smuzhiyun mask = (mask | asoc->peer.sack_generation) << 1;
5094*4882a593Smuzhiyun mask = (mask | asoc->peer.zero_window_announced);
5095*4882a593Smuzhiyun info->sctpi_peer_sack = mask;
5096*4882a593Smuzhiyun
5097*4882a593Smuzhiyun info->sctpi_isacks = asoc->stats.isacks;
5098*4882a593Smuzhiyun info->sctpi_osacks = asoc->stats.osacks;
5099*4882a593Smuzhiyun info->sctpi_opackets = asoc->stats.opackets;
5100*4882a593Smuzhiyun info->sctpi_ipackets = asoc->stats.ipackets;
5101*4882a593Smuzhiyun info->sctpi_rtxchunks = asoc->stats.rtxchunks;
5102*4882a593Smuzhiyun info->sctpi_outofseqtsns = asoc->stats.outofseqtsns;
5103*4882a593Smuzhiyun info->sctpi_idupchunks = asoc->stats.idupchunks;
5104*4882a593Smuzhiyun info->sctpi_gapcnt = asoc->stats.gapcnt;
5105*4882a593Smuzhiyun info->sctpi_ouodchunks = asoc->stats.ouodchunks;
5106*4882a593Smuzhiyun info->sctpi_iuodchunks = asoc->stats.iuodchunks;
5107*4882a593Smuzhiyun info->sctpi_oodchunks = asoc->stats.oodchunks;
5108*4882a593Smuzhiyun info->sctpi_iodchunks = asoc->stats.iodchunks;
5109*4882a593Smuzhiyun info->sctpi_octrlchunks = asoc->stats.octrlchunks;
5110*4882a593Smuzhiyun info->sctpi_ictrlchunks = asoc->stats.ictrlchunks;
5111*4882a593Smuzhiyun
5112*4882a593Smuzhiyun prim = asoc->peer.primary_path;
5113*4882a593Smuzhiyun memcpy(&info->sctpi_p_address, &prim->ipaddr, sizeof(prim->ipaddr));
5114*4882a593Smuzhiyun info->sctpi_p_state = prim->state;
5115*4882a593Smuzhiyun info->sctpi_p_cwnd = prim->cwnd;
5116*4882a593Smuzhiyun info->sctpi_p_srtt = prim->srtt;
5117*4882a593Smuzhiyun info->sctpi_p_rto = jiffies_to_msecs(prim->rto);
5118*4882a593Smuzhiyun info->sctpi_p_hbinterval = prim->hbinterval;
5119*4882a593Smuzhiyun info->sctpi_p_pathmaxrxt = prim->pathmaxrxt;
5120*4882a593Smuzhiyun info->sctpi_p_sackdelay = jiffies_to_msecs(prim->sackdelay);
5121*4882a593Smuzhiyun info->sctpi_p_ssthresh = prim->ssthresh;
5122*4882a593Smuzhiyun info->sctpi_p_partial_bytes_acked = prim->partial_bytes_acked;
5123*4882a593Smuzhiyun info->sctpi_p_flight_size = prim->flight_size;
5124*4882a593Smuzhiyun info->sctpi_p_error = prim->error_count;
5125*4882a593Smuzhiyun
5126*4882a593Smuzhiyun return 0;
5127*4882a593Smuzhiyun }
5128*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(sctp_get_sctp_info);
5129*4882a593Smuzhiyun
5130*4882a593Smuzhiyun /* use callback to avoid exporting the core structure */
sctp_transport_walk_start(struct rhashtable_iter * iter)5131*4882a593Smuzhiyun void sctp_transport_walk_start(struct rhashtable_iter *iter) __acquires(RCU)
5132*4882a593Smuzhiyun {
5133*4882a593Smuzhiyun rhltable_walk_enter(&sctp_transport_hashtable, iter);
5134*4882a593Smuzhiyun
5135*4882a593Smuzhiyun rhashtable_walk_start(iter);
5136*4882a593Smuzhiyun }
5137*4882a593Smuzhiyun
sctp_transport_walk_stop(struct rhashtable_iter * iter)5138*4882a593Smuzhiyun void sctp_transport_walk_stop(struct rhashtable_iter *iter) __releases(RCU)
5139*4882a593Smuzhiyun {
5140*4882a593Smuzhiyun rhashtable_walk_stop(iter);
5141*4882a593Smuzhiyun rhashtable_walk_exit(iter);
5142*4882a593Smuzhiyun }
5143*4882a593Smuzhiyun
sctp_transport_get_next(struct net * net,struct rhashtable_iter * iter)5144*4882a593Smuzhiyun struct sctp_transport *sctp_transport_get_next(struct net *net,
5145*4882a593Smuzhiyun struct rhashtable_iter *iter)
5146*4882a593Smuzhiyun {
5147*4882a593Smuzhiyun struct sctp_transport *t;
5148*4882a593Smuzhiyun
5149*4882a593Smuzhiyun t = rhashtable_walk_next(iter);
5150*4882a593Smuzhiyun for (; t; t = rhashtable_walk_next(iter)) {
5151*4882a593Smuzhiyun if (IS_ERR(t)) {
5152*4882a593Smuzhiyun if (PTR_ERR(t) == -EAGAIN)
5153*4882a593Smuzhiyun continue;
5154*4882a593Smuzhiyun break;
5155*4882a593Smuzhiyun }
5156*4882a593Smuzhiyun
5157*4882a593Smuzhiyun if (!sctp_transport_hold(t))
5158*4882a593Smuzhiyun continue;
5159*4882a593Smuzhiyun
5160*4882a593Smuzhiyun if (net_eq(t->asoc->base.net, net) &&
5161*4882a593Smuzhiyun t->asoc->peer.primary_path == t)
5162*4882a593Smuzhiyun break;
5163*4882a593Smuzhiyun
5164*4882a593Smuzhiyun sctp_transport_put(t);
5165*4882a593Smuzhiyun }
5166*4882a593Smuzhiyun
5167*4882a593Smuzhiyun return t;
5168*4882a593Smuzhiyun }
5169*4882a593Smuzhiyun
sctp_transport_get_idx(struct net * net,struct rhashtable_iter * iter,int pos)5170*4882a593Smuzhiyun struct sctp_transport *sctp_transport_get_idx(struct net *net,
5171*4882a593Smuzhiyun struct rhashtable_iter *iter,
5172*4882a593Smuzhiyun int pos)
5173*4882a593Smuzhiyun {
5174*4882a593Smuzhiyun struct sctp_transport *t;
5175*4882a593Smuzhiyun
5176*4882a593Smuzhiyun if (!pos)
5177*4882a593Smuzhiyun return SEQ_START_TOKEN;
5178*4882a593Smuzhiyun
5179*4882a593Smuzhiyun while ((t = sctp_transport_get_next(net, iter)) && !IS_ERR(t)) {
5180*4882a593Smuzhiyun if (!--pos)
5181*4882a593Smuzhiyun break;
5182*4882a593Smuzhiyun sctp_transport_put(t);
5183*4882a593Smuzhiyun }
5184*4882a593Smuzhiyun
5185*4882a593Smuzhiyun return t;
5186*4882a593Smuzhiyun }
5187*4882a593Smuzhiyun
sctp_for_each_endpoint(int (* cb)(struct sctp_endpoint *,void *),void * p)5188*4882a593Smuzhiyun int sctp_for_each_endpoint(int (*cb)(struct sctp_endpoint *, void *),
5189*4882a593Smuzhiyun void *p) {
5190*4882a593Smuzhiyun int err = 0;
5191*4882a593Smuzhiyun int hash = 0;
5192*4882a593Smuzhiyun struct sctp_ep_common *epb;
5193*4882a593Smuzhiyun struct sctp_hashbucket *head;
5194*4882a593Smuzhiyun
5195*4882a593Smuzhiyun for (head = sctp_ep_hashtable; hash < sctp_ep_hashsize;
5196*4882a593Smuzhiyun hash++, head++) {
5197*4882a593Smuzhiyun read_lock_bh(&head->lock);
5198*4882a593Smuzhiyun sctp_for_each_hentry(epb, &head->chain) {
5199*4882a593Smuzhiyun err = cb(sctp_ep(epb), p);
5200*4882a593Smuzhiyun if (err)
5201*4882a593Smuzhiyun break;
5202*4882a593Smuzhiyun }
5203*4882a593Smuzhiyun read_unlock_bh(&head->lock);
5204*4882a593Smuzhiyun }
5205*4882a593Smuzhiyun
5206*4882a593Smuzhiyun return err;
5207*4882a593Smuzhiyun }
5208*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(sctp_for_each_endpoint);
5209*4882a593Smuzhiyun
sctp_transport_lookup_process(int (* cb)(struct sctp_transport *,void *),struct net * net,const union sctp_addr * laddr,const union sctp_addr * paddr,void * p)5210*4882a593Smuzhiyun int sctp_transport_lookup_process(int (*cb)(struct sctp_transport *, void *),
5211*4882a593Smuzhiyun struct net *net,
5212*4882a593Smuzhiyun const union sctp_addr *laddr,
5213*4882a593Smuzhiyun const union sctp_addr *paddr, void *p)
5214*4882a593Smuzhiyun {
5215*4882a593Smuzhiyun struct sctp_transport *transport;
5216*4882a593Smuzhiyun int err;
5217*4882a593Smuzhiyun
5218*4882a593Smuzhiyun rcu_read_lock();
5219*4882a593Smuzhiyun transport = sctp_addrs_lookup_transport(net, laddr, paddr);
5220*4882a593Smuzhiyun rcu_read_unlock();
5221*4882a593Smuzhiyun if (!transport)
5222*4882a593Smuzhiyun return -ENOENT;
5223*4882a593Smuzhiyun
5224*4882a593Smuzhiyun err = cb(transport, p);
5225*4882a593Smuzhiyun sctp_transport_put(transport);
5226*4882a593Smuzhiyun
5227*4882a593Smuzhiyun return err;
5228*4882a593Smuzhiyun }
5229*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(sctp_transport_lookup_process);
5230*4882a593Smuzhiyun
sctp_transport_traverse_process(sctp_callback_t cb,sctp_callback_t cb_done,struct net * net,int * pos,void * p)5231*4882a593Smuzhiyun int sctp_transport_traverse_process(sctp_callback_t cb, sctp_callback_t cb_done,
5232*4882a593Smuzhiyun struct net *net, int *pos, void *p)
5233*4882a593Smuzhiyun {
5234*4882a593Smuzhiyun struct rhashtable_iter hti;
5235*4882a593Smuzhiyun struct sctp_transport *tsp;
5236*4882a593Smuzhiyun struct sctp_endpoint *ep;
5237*4882a593Smuzhiyun int ret;
5238*4882a593Smuzhiyun
5239*4882a593Smuzhiyun again:
5240*4882a593Smuzhiyun ret = 0;
5241*4882a593Smuzhiyun sctp_transport_walk_start(&hti);
5242*4882a593Smuzhiyun
5243*4882a593Smuzhiyun tsp = sctp_transport_get_idx(net, &hti, *pos + 1);
5244*4882a593Smuzhiyun for (; !IS_ERR_OR_NULL(tsp); tsp = sctp_transport_get_next(net, &hti)) {
5245*4882a593Smuzhiyun ep = tsp->asoc->ep;
5246*4882a593Smuzhiyun if (sctp_endpoint_hold(ep)) { /* asoc can be peeled off */
5247*4882a593Smuzhiyun ret = cb(ep, tsp, p);
5248*4882a593Smuzhiyun if (ret)
5249*4882a593Smuzhiyun break;
5250*4882a593Smuzhiyun sctp_endpoint_put(ep);
5251*4882a593Smuzhiyun }
5252*4882a593Smuzhiyun (*pos)++;
5253*4882a593Smuzhiyun sctp_transport_put(tsp);
5254*4882a593Smuzhiyun }
5255*4882a593Smuzhiyun sctp_transport_walk_stop(&hti);
5256*4882a593Smuzhiyun
5257*4882a593Smuzhiyun if (ret) {
5258*4882a593Smuzhiyun if (cb_done && !cb_done(ep, tsp, p)) {
5259*4882a593Smuzhiyun (*pos)++;
5260*4882a593Smuzhiyun sctp_endpoint_put(ep);
5261*4882a593Smuzhiyun sctp_transport_put(tsp);
5262*4882a593Smuzhiyun goto again;
5263*4882a593Smuzhiyun }
5264*4882a593Smuzhiyun sctp_endpoint_put(ep);
5265*4882a593Smuzhiyun sctp_transport_put(tsp);
5266*4882a593Smuzhiyun }
5267*4882a593Smuzhiyun
5268*4882a593Smuzhiyun return ret;
5269*4882a593Smuzhiyun }
5270*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(sctp_transport_traverse_process);
5271*4882a593Smuzhiyun
5272*4882a593Smuzhiyun /* 7.2.1 Association Status (SCTP_STATUS)
5273*4882a593Smuzhiyun
5274*4882a593Smuzhiyun * Applications can retrieve current status information about an
5275*4882a593Smuzhiyun * association, including association state, peer receiver window size,
5276*4882a593Smuzhiyun * number of unacked data chunks, and number of data chunks pending
5277*4882a593Smuzhiyun * receipt. This information is read-only.
5278*4882a593Smuzhiyun */
sctp_getsockopt_sctp_status(struct sock * sk,int len,char __user * optval,int __user * optlen)5279*4882a593Smuzhiyun static int sctp_getsockopt_sctp_status(struct sock *sk, int len,
5280*4882a593Smuzhiyun char __user *optval,
5281*4882a593Smuzhiyun int __user *optlen)
5282*4882a593Smuzhiyun {
5283*4882a593Smuzhiyun struct sctp_status status;
5284*4882a593Smuzhiyun struct sctp_association *asoc = NULL;
5285*4882a593Smuzhiyun struct sctp_transport *transport;
5286*4882a593Smuzhiyun sctp_assoc_t associd;
5287*4882a593Smuzhiyun int retval = 0;
5288*4882a593Smuzhiyun
5289*4882a593Smuzhiyun if (len < sizeof(status)) {
5290*4882a593Smuzhiyun retval = -EINVAL;
5291*4882a593Smuzhiyun goto out;
5292*4882a593Smuzhiyun }
5293*4882a593Smuzhiyun
5294*4882a593Smuzhiyun len = sizeof(status);
5295*4882a593Smuzhiyun if (copy_from_user(&status, optval, len)) {
5296*4882a593Smuzhiyun retval = -EFAULT;
5297*4882a593Smuzhiyun goto out;
5298*4882a593Smuzhiyun }
5299*4882a593Smuzhiyun
5300*4882a593Smuzhiyun associd = status.sstat_assoc_id;
5301*4882a593Smuzhiyun asoc = sctp_id2assoc(sk, associd);
5302*4882a593Smuzhiyun if (!asoc) {
5303*4882a593Smuzhiyun retval = -EINVAL;
5304*4882a593Smuzhiyun goto out;
5305*4882a593Smuzhiyun }
5306*4882a593Smuzhiyun
5307*4882a593Smuzhiyun transport = asoc->peer.primary_path;
5308*4882a593Smuzhiyun
5309*4882a593Smuzhiyun status.sstat_assoc_id = sctp_assoc2id(asoc);
5310*4882a593Smuzhiyun status.sstat_state = sctp_assoc_to_state(asoc);
5311*4882a593Smuzhiyun status.sstat_rwnd = asoc->peer.rwnd;
5312*4882a593Smuzhiyun status.sstat_unackdata = asoc->unack_data;
5313*4882a593Smuzhiyun
5314*4882a593Smuzhiyun status.sstat_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map);
5315*4882a593Smuzhiyun status.sstat_instrms = asoc->stream.incnt;
5316*4882a593Smuzhiyun status.sstat_outstrms = asoc->stream.outcnt;
5317*4882a593Smuzhiyun status.sstat_fragmentation_point = asoc->frag_point;
5318*4882a593Smuzhiyun status.sstat_primary.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
5319*4882a593Smuzhiyun memcpy(&status.sstat_primary.spinfo_address, &transport->ipaddr,
5320*4882a593Smuzhiyun transport->af_specific->sockaddr_len);
5321*4882a593Smuzhiyun /* Map ipv4 address into v4-mapped-on-v6 address. */
5322*4882a593Smuzhiyun sctp_get_pf_specific(sk->sk_family)->addr_to_user(sctp_sk(sk),
5323*4882a593Smuzhiyun (union sctp_addr *)&status.sstat_primary.spinfo_address);
5324*4882a593Smuzhiyun status.sstat_primary.spinfo_state = transport->state;
5325*4882a593Smuzhiyun status.sstat_primary.spinfo_cwnd = transport->cwnd;
5326*4882a593Smuzhiyun status.sstat_primary.spinfo_srtt = transport->srtt;
5327*4882a593Smuzhiyun status.sstat_primary.spinfo_rto = jiffies_to_msecs(transport->rto);
5328*4882a593Smuzhiyun status.sstat_primary.spinfo_mtu = transport->pathmtu;
5329*4882a593Smuzhiyun
5330*4882a593Smuzhiyun if (status.sstat_primary.spinfo_state == SCTP_UNKNOWN)
5331*4882a593Smuzhiyun status.sstat_primary.spinfo_state = SCTP_ACTIVE;
5332*4882a593Smuzhiyun
5333*4882a593Smuzhiyun if (put_user(len, optlen)) {
5334*4882a593Smuzhiyun retval = -EFAULT;
5335*4882a593Smuzhiyun goto out;
5336*4882a593Smuzhiyun }
5337*4882a593Smuzhiyun
5338*4882a593Smuzhiyun pr_debug("%s: len:%d, state:%d, rwnd:%d, assoc_id:%d\n",
5339*4882a593Smuzhiyun __func__, len, status.sstat_state, status.sstat_rwnd,
5340*4882a593Smuzhiyun status.sstat_assoc_id);
5341*4882a593Smuzhiyun
5342*4882a593Smuzhiyun if (copy_to_user(optval, &status, len)) {
5343*4882a593Smuzhiyun retval = -EFAULT;
5344*4882a593Smuzhiyun goto out;
5345*4882a593Smuzhiyun }
5346*4882a593Smuzhiyun
5347*4882a593Smuzhiyun out:
5348*4882a593Smuzhiyun return retval;
5349*4882a593Smuzhiyun }
5350*4882a593Smuzhiyun
5351*4882a593Smuzhiyun
5352*4882a593Smuzhiyun /* 7.2.2 Peer Address Information (SCTP_GET_PEER_ADDR_INFO)
5353*4882a593Smuzhiyun *
5354*4882a593Smuzhiyun * Applications can retrieve information about a specific peer address
5355*4882a593Smuzhiyun * of an association, including its reachability state, congestion
5356*4882a593Smuzhiyun * window, and retransmission timer values. This information is
5357*4882a593Smuzhiyun * read-only.
5358*4882a593Smuzhiyun */
sctp_getsockopt_peer_addr_info(struct sock * sk,int len,char __user * optval,int __user * optlen)5359*4882a593Smuzhiyun static int sctp_getsockopt_peer_addr_info(struct sock *sk, int len,
5360*4882a593Smuzhiyun char __user *optval,
5361*4882a593Smuzhiyun int __user *optlen)
5362*4882a593Smuzhiyun {
5363*4882a593Smuzhiyun struct sctp_paddrinfo pinfo;
5364*4882a593Smuzhiyun struct sctp_transport *transport;
5365*4882a593Smuzhiyun int retval = 0;
5366*4882a593Smuzhiyun
5367*4882a593Smuzhiyun if (len < sizeof(pinfo)) {
5368*4882a593Smuzhiyun retval = -EINVAL;
5369*4882a593Smuzhiyun goto out;
5370*4882a593Smuzhiyun }
5371*4882a593Smuzhiyun
5372*4882a593Smuzhiyun len = sizeof(pinfo);
5373*4882a593Smuzhiyun if (copy_from_user(&pinfo, optval, len)) {
5374*4882a593Smuzhiyun retval = -EFAULT;
5375*4882a593Smuzhiyun goto out;
5376*4882a593Smuzhiyun }
5377*4882a593Smuzhiyun
5378*4882a593Smuzhiyun transport = sctp_addr_id2transport(sk, &pinfo.spinfo_address,
5379*4882a593Smuzhiyun pinfo.spinfo_assoc_id);
5380*4882a593Smuzhiyun if (!transport) {
5381*4882a593Smuzhiyun retval = -EINVAL;
5382*4882a593Smuzhiyun goto out;
5383*4882a593Smuzhiyun }
5384*4882a593Smuzhiyun
5385*4882a593Smuzhiyun if (transport->state == SCTP_PF &&
5386*4882a593Smuzhiyun transport->asoc->pf_expose == SCTP_PF_EXPOSE_DISABLE) {
5387*4882a593Smuzhiyun retval = -EACCES;
5388*4882a593Smuzhiyun goto out;
5389*4882a593Smuzhiyun }
5390*4882a593Smuzhiyun
5391*4882a593Smuzhiyun pinfo.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
5392*4882a593Smuzhiyun pinfo.spinfo_state = transport->state;
5393*4882a593Smuzhiyun pinfo.spinfo_cwnd = transport->cwnd;
5394*4882a593Smuzhiyun pinfo.spinfo_srtt = transport->srtt;
5395*4882a593Smuzhiyun pinfo.spinfo_rto = jiffies_to_msecs(transport->rto);
5396*4882a593Smuzhiyun pinfo.spinfo_mtu = transport->pathmtu;
5397*4882a593Smuzhiyun
5398*4882a593Smuzhiyun if (pinfo.spinfo_state == SCTP_UNKNOWN)
5399*4882a593Smuzhiyun pinfo.spinfo_state = SCTP_ACTIVE;
5400*4882a593Smuzhiyun
5401*4882a593Smuzhiyun if (put_user(len, optlen)) {
5402*4882a593Smuzhiyun retval = -EFAULT;
5403*4882a593Smuzhiyun goto out;
5404*4882a593Smuzhiyun }
5405*4882a593Smuzhiyun
5406*4882a593Smuzhiyun if (copy_to_user(optval, &pinfo, len)) {
5407*4882a593Smuzhiyun retval = -EFAULT;
5408*4882a593Smuzhiyun goto out;
5409*4882a593Smuzhiyun }
5410*4882a593Smuzhiyun
5411*4882a593Smuzhiyun out:
5412*4882a593Smuzhiyun return retval;
5413*4882a593Smuzhiyun }
5414*4882a593Smuzhiyun
5415*4882a593Smuzhiyun /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
5416*4882a593Smuzhiyun *
5417*4882a593Smuzhiyun * This option is a on/off flag. If enabled no SCTP message
5418*4882a593Smuzhiyun * fragmentation will be performed. Instead if a message being sent
5419*4882a593Smuzhiyun * exceeds the current PMTU size, the message will NOT be sent and
5420*4882a593Smuzhiyun * instead a error will be indicated to the user.
5421*4882a593Smuzhiyun */
sctp_getsockopt_disable_fragments(struct sock * sk,int len,char __user * optval,int __user * optlen)5422*4882a593Smuzhiyun static int sctp_getsockopt_disable_fragments(struct sock *sk, int len,
5423*4882a593Smuzhiyun char __user *optval, int __user *optlen)
5424*4882a593Smuzhiyun {
5425*4882a593Smuzhiyun int val;
5426*4882a593Smuzhiyun
5427*4882a593Smuzhiyun if (len < sizeof(int))
5428*4882a593Smuzhiyun return -EINVAL;
5429*4882a593Smuzhiyun
5430*4882a593Smuzhiyun len = sizeof(int);
5431*4882a593Smuzhiyun val = (sctp_sk(sk)->disable_fragments == 1);
5432*4882a593Smuzhiyun if (put_user(len, optlen))
5433*4882a593Smuzhiyun return -EFAULT;
5434*4882a593Smuzhiyun if (copy_to_user(optval, &val, len))
5435*4882a593Smuzhiyun return -EFAULT;
5436*4882a593Smuzhiyun return 0;
5437*4882a593Smuzhiyun }
5438*4882a593Smuzhiyun
5439*4882a593Smuzhiyun /* 7.1.15 Set notification and ancillary events (SCTP_EVENTS)
5440*4882a593Smuzhiyun *
5441*4882a593Smuzhiyun * This socket option is used to specify various notifications and
5442*4882a593Smuzhiyun * ancillary data the user wishes to receive.
5443*4882a593Smuzhiyun */
sctp_getsockopt_events(struct sock * sk,int len,char __user * optval,int __user * optlen)5444*4882a593Smuzhiyun static int sctp_getsockopt_events(struct sock *sk, int len, char __user *optval,
5445*4882a593Smuzhiyun int __user *optlen)
5446*4882a593Smuzhiyun {
5447*4882a593Smuzhiyun struct sctp_event_subscribe subscribe;
5448*4882a593Smuzhiyun __u8 *sn_type = (__u8 *)&subscribe;
5449*4882a593Smuzhiyun int i;
5450*4882a593Smuzhiyun
5451*4882a593Smuzhiyun if (len == 0)
5452*4882a593Smuzhiyun return -EINVAL;
5453*4882a593Smuzhiyun if (len > sizeof(struct sctp_event_subscribe))
5454*4882a593Smuzhiyun len = sizeof(struct sctp_event_subscribe);
5455*4882a593Smuzhiyun if (put_user(len, optlen))
5456*4882a593Smuzhiyun return -EFAULT;
5457*4882a593Smuzhiyun
5458*4882a593Smuzhiyun for (i = 0; i < len; i++)
5459*4882a593Smuzhiyun sn_type[i] = sctp_ulpevent_type_enabled(sctp_sk(sk)->subscribe,
5460*4882a593Smuzhiyun SCTP_SN_TYPE_BASE + i);
5461*4882a593Smuzhiyun
5462*4882a593Smuzhiyun if (copy_to_user(optval, &subscribe, len))
5463*4882a593Smuzhiyun return -EFAULT;
5464*4882a593Smuzhiyun
5465*4882a593Smuzhiyun return 0;
5466*4882a593Smuzhiyun }
5467*4882a593Smuzhiyun
5468*4882a593Smuzhiyun /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
5469*4882a593Smuzhiyun *
5470*4882a593Smuzhiyun * This socket option is applicable to the UDP-style socket only. When
5471*4882a593Smuzhiyun * set it will cause associations that are idle for more than the
5472*4882a593Smuzhiyun * specified number of seconds to automatically close. An association
5473*4882a593Smuzhiyun * being idle is defined an association that has NOT sent or received
5474*4882a593Smuzhiyun * user data. The special value of '0' indicates that no automatic
5475*4882a593Smuzhiyun * close of any associations should be performed. The option expects an
5476*4882a593Smuzhiyun * integer defining the number of seconds of idle time before an
5477*4882a593Smuzhiyun * association is closed.
5478*4882a593Smuzhiyun */
sctp_getsockopt_autoclose(struct sock * sk,int len,char __user * optval,int __user * optlen)5479*4882a593Smuzhiyun static int sctp_getsockopt_autoclose(struct sock *sk, int len, char __user *optval, int __user *optlen)
5480*4882a593Smuzhiyun {
5481*4882a593Smuzhiyun /* Applicable to UDP-style socket only */
5482*4882a593Smuzhiyun if (sctp_style(sk, TCP))
5483*4882a593Smuzhiyun return -EOPNOTSUPP;
5484*4882a593Smuzhiyun if (len < sizeof(int))
5485*4882a593Smuzhiyun return -EINVAL;
5486*4882a593Smuzhiyun len = sizeof(int);
5487*4882a593Smuzhiyun if (put_user(len, optlen))
5488*4882a593Smuzhiyun return -EFAULT;
5489*4882a593Smuzhiyun if (put_user(sctp_sk(sk)->autoclose, (int __user *)optval))
5490*4882a593Smuzhiyun return -EFAULT;
5491*4882a593Smuzhiyun return 0;
5492*4882a593Smuzhiyun }
5493*4882a593Smuzhiyun
5494*4882a593Smuzhiyun /* Helper routine to branch off an association to a new socket. */
sctp_do_peeloff(struct sock * sk,sctp_assoc_t id,struct socket ** sockp)5495*4882a593Smuzhiyun int sctp_do_peeloff(struct sock *sk, sctp_assoc_t id, struct socket **sockp)
5496*4882a593Smuzhiyun {
5497*4882a593Smuzhiyun struct sctp_association *asoc = sctp_id2assoc(sk, id);
5498*4882a593Smuzhiyun struct sctp_sock *sp = sctp_sk(sk);
5499*4882a593Smuzhiyun struct socket *sock;
5500*4882a593Smuzhiyun int err = 0;
5501*4882a593Smuzhiyun
5502*4882a593Smuzhiyun /* Do not peel off from one netns to another one. */
5503*4882a593Smuzhiyun if (!net_eq(current->nsproxy->net_ns, sock_net(sk)))
5504*4882a593Smuzhiyun return -EINVAL;
5505*4882a593Smuzhiyun
5506*4882a593Smuzhiyun if (!asoc)
5507*4882a593Smuzhiyun return -EINVAL;
5508*4882a593Smuzhiyun
5509*4882a593Smuzhiyun /* An association cannot be branched off from an already peeled-off
5510*4882a593Smuzhiyun * socket, nor is this supported for tcp style sockets.
5511*4882a593Smuzhiyun */
5512*4882a593Smuzhiyun if (!sctp_style(sk, UDP))
5513*4882a593Smuzhiyun return -EINVAL;
5514*4882a593Smuzhiyun
5515*4882a593Smuzhiyun /* Create a new socket. */
5516*4882a593Smuzhiyun err = sock_create(sk->sk_family, SOCK_SEQPACKET, IPPROTO_SCTP, &sock);
5517*4882a593Smuzhiyun if (err < 0)
5518*4882a593Smuzhiyun return err;
5519*4882a593Smuzhiyun
5520*4882a593Smuzhiyun sctp_copy_sock(sock->sk, sk, asoc);
5521*4882a593Smuzhiyun
5522*4882a593Smuzhiyun /* Make peeled-off sockets more like 1-1 accepted sockets.
5523*4882a593Smuzhiyun * Set the daddr and initialize id to something more random and also
5524*4882a593Smuzhiyun * copy over any ip options.
5525*4882a593Smuzhiyun */
5526*4882a593Smuzhiyun sp->pf->to_sk_daddr(&asoc->peer.primary_addr, sock->sk);
5527*4882a593Smuzhiyun sp->pf->copy_ip_options(sk, sock->sk);
5528*4882a593Smuzhiyun
5529*4882a593Smuzhiyun /* Populate the fields of the newsk from the oldsk and migrate the
5530*4882a593Smuzhiyun * asoc to the newsk.
5531*4882a593Smuzhiyun */
5532*4882a593Smuzhiyun err = sctp_sock_migrate(sk, sock->sk, asoc,
5533*4882a593Smuzhiyun SCTP_SOCKET_UDP_HIGH_BANDWIDTH);
5534*4882a593Smuzhiyun if (err) {
5535*4882a593Smuzhiyun sock_release(sock);
5536*4882a593Smuzhiyun sock = NULL;
5537*4882a593Smuzhiyun }
5538*4882a593Smuzhiyun
5539*4882a593Smuzhiyun *sockp = sock;
5540*4882a593Smuzhiyun
5541*4882a593Smuzhiyun return err;
5542*4882a593Smuzhiyun }
5543*4882a593Smuzhiyun EXPORT_SYMBOL(sctp_do_peeloff);
5544*4882a593Smuzhiyun
sctp_getsockopt_peeloff_common(struct sock * sk,sctp_peeloff_arg_t * peeloff,struct file ** newfile,unsigned flags)5545*4882a593Smuzhiyun static int sctp_getsockopt_peeloff_common(struct sock *sk, sctp_peeloff_arg_t *peeloff,
5546*4882a593Smuzhiyun struct file **newfile, unsigned flags)
5547*4882a593Smuzhiyun {
5548*4882a593Smuzhiyun struct socket *newsock;
5549*4882a593Smuzhiyun int retval;
5550*4882a593Smuzhiyun
5551*4882a593Smuzhiyun retval = sctp_do_peeloff(sk, peeloff->associd, &newsock);
5552*4882a593Smuzhiyun if (retval < 0)
5553*4882a593Smuzhiyun goto out;
5554*4882a593Smuzhiyun
5555*4882a593Smuzhiyun /* Map the socket to an unused fd that can be returned to the user. */
5556*4882a593Smuzhiyun retval = get_unused_fd_flags(flags & SOCK_CLOEXEC);
5557*4882a593Smuzhiyun if (retval < 0) {
5558*4882a593Smuzhiyun sock_release(newsock);
5559*4882a593Smuzhiyun goto out;
5560*4882a593Smuzhiyun }
5561*4882a593Smuzhiyun
5562*4882a593Smuzhiyun *newfile = sock_alloc_file(newsock, 0, NULL);
5563*4882a593Smuzhiyun if (IS_ERR(*newfile)) {
5564*4882a593Smuzhiyun put_unused_fd(retval);
5565*4882a593Smuzhiyun retval = PTR_ERR(*newfile);
5566*4882a593Smuzhiyun *newfile = NULL;
5567*4882a593Smuzhiyun return retval;
5568*4882a593Smuzhiyun }
5569*4882a593Smuzhiyun
5570*4882a593Smuzhiyun pr_debug("%s: sk:%p, newsk:%p, sd:%d\n", __func__, sk, newsock->sk,
5571*4882a593Smuzhiyun retval);
5572*4882a593Smuzhiyun
5573*4882a593Smuzhiyun peeloff->sd = retval;
5574*4882a593Smuzhiyun
5575*4882a593Smuzhiyun if (flags & SOCK_NONBLOCK)
5576*4882a593Smuzhiyun (*newfile)->f_flags |= O_NONBLOCK;
5577*4882a593Smuzhiyun out:
5578*4882a593Smuzhiyun return retval;
5579*4882a593Smuzhiyun }
5580*4882a593Smuzhiyun
sctp_getsockopt_peeloff(struct sock * sk,int len,char __user * optval,int __user * optlen)5581*4882a593Smuzhiyun static int sctp_getsockopt_peeloff(struct sock *sk, int len, char __user *optval, int __user *optlen)
5582*4882a593Smuzhiyun {
5583*4882a593Smuzhiyun sctp_peeloff_arg_t peeloff;
5584*4882a593Smuzhiyun struct file *newfile = NULL;
5585*4882a593Smuzhiyun int retval = 0;
5586*4882a593Smuzhiyun
5587*4882a593Smuzhiyun if (len < sizeof(sctp_peeloff_arg_t))
5588*4882a593Smuzhiyun return -EINVAL;
5589*4882a593Smuzhiyun len = sizeof(sctp_peeloff_arg_t);
5590*4882a593Smuzhiyun if (copy_from_user(&peeloff, optval, len))
5591*4882a593Smuzhiyun return -EFAULT;
5592*4882a593Smuzhiyun
5593*4882a593Smuzhiyun retval = sctp_getsockopt_peeloff_common(sk, &peeloff, &newfile, 0);
5594*4882a593Smuzhiyun if (retval < 0)
5595*4882a593Smuzhiyun goto out;
5596*4882a593Smuzhiyun
5597*4882a593Smuzhiyun /* Return the fd mapped to the new socket. */
5598*4882a593Smuzhiyun if (put_user(len, optlen)) {
5599*4882a593Smuzhiyun fput(newfile);
5600*4882a593Smuzhiyun put_unused_fd(retval);
5601*4882a593Smuzhiyun return -EFAULT;
5602*4882a593Smuzhiyun }
5603*4882a593Smuzhiyun
5604*4882a593Smuzhiyun if (copy_to_user(optval, &peeloff, len)) {
5605*4882a593Smuzhiyun fput(newfile);
5606*4882a593Smuzhiyun put_unused_fd(retval);
5607*4882a593Smuzhiyun return -EFAULT;
5608*4882a593Smuzhiyun }
5609*4882a593Smuzhiyun fd_install(retval, newfile);
5610*4882a593Smuzhiyun out:
5611*4882a593Smuzhiyun return retval;
5612*4882a593Smuzhiyun }
5613*4882a593Smuzhiyun
sctp_getsockopt_peeloff_flags(struct sock * sk,int len,char __user * optval,int __user * optlen)5614*4882a593Smuzhiyun static int sctp_getsockopt_peeloff_flags(struct sock *sk, int len,
5615*4882a593Smuzhiyun char __user *optval, int __user *optlen)
5616*4882a593Smuzhiyun {
5617*4882a593Smuzhiyun sctp_peeloff_flags_arg_t peeloff;
5618*4882a593Smuzhiyun struct file *newfile = NULL;
5619*4882a593Smuzhiyun int retval = 0;
5620*4882a593Smuzhiyun
5621*4882a593Smuzhiyun if (len < sizeof(sctp_peeloff_flags_arg_t))
5622*4882a593Smuzhiyun return -EINVAL;
5623*4882a593Smuzhiyun len = sizeof(sctp_peeloff_flags_arg_t);
5624*4882a593Smuzhiyun if (copy_from_user(&peeloff, optval, len))
5625*4882a593Smuzhiyun return -EFAULT;
5626*4882a593Smuzhiyun
5627*4882a593Smuzhiyun retval = sctp_getsockopt_peeloff_common(sk, &peeloff.p_arg,
5628*4882a593Smuzhiyun &newfile, peeloff.flags);
5629*4882a593Smuzhiyun if (retval < 0)
5630*4882a593Smuzhiyun goto out;
5631*4882a593Smuzhiyun
5632*4882a593Smuzhiyun /* Return the fd mapped to the new socket. */
5633*4882a593Smuzhiyun if (put_user(len, optlen)) {
5634*4882a593Smuzhiyun fput(newfile);
5635*4882a593Smuzhiyun put_unused_fd(retval);
5636*4882a593Smuzhiyun return -EFAULT;
5637*4882a593Smuzhiyun }
5638*4882a593Smuzhiyun
5639*4882a593Smuzhiyun if (copy_to_user(optval, &peeloff, len)) {
5640*4882a593Smuzhiyun fput(newfile);
5641*4882a593Smuzhiyun put_unused_fd(retval);
5642*4882a593Smuzhiyun return -EFAULT;
5643*4882a593Smuzhiyun }
5644*4882a593Smuzhiyun fd_install(retval, newfile);
5645*4882a593Smuzhiyun out:
5646*4882a593Smuzhiyun return retval;
5647*4882a593Smuzhiyun }
5648*4882a593Smuzhiyun
5649*4882a593Smuzhiyun /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
5650*4882a593Smuzhiyun *
5651*4882a593Smuzhiyun * Applications can enable or disable heartbeats for any peer address of
5652*4882a593Smuzhiyun * an association, modify an address's heartbeat interval, force a
5653*4882a593Smuzhiyun * heartbeat to be sent immediately, and adjust the address's maximum
5654*4882a593Smuzhiyun * number of retransmissions sent before an address is considered
5655*4882a593Smuzhiyun * unreachable. The following structure is used to access and modify an
5656*4882a593Smuzhiyun * address's parameters:
5657*4882a593Smuzhiyun *
5658*4882a593Smuzhiyun * struct sctp_paddrparams {
5659*4882a593Smuzhiyun * sctp_assoc_t spp_assoc_id;
5660*4882a593Smuzhiyun * struct sockaddr_storage spp_address;
5661*4882a593Smuzhiyun * uint32_t spp_hbinterval;
5662*4882a593Smuzhiyun * uint16_t spp_pathmaxrxt;
5663*4882a593Smuzhiyun * uint32_t spp_pathmtu;
5664*4882a593Smuzhiyun * uint32_t spp_sackdelay;
5665*4882a593Smuzhiyun * uint32_t spp_flags;
5666*4882a593Smuzhiyun * };
5667*4882a593Smuzhiyun *
5668*4882a593Smuzhiyun * spp_assoc_id - (one-to-many style socket) This is filled in the
5669*4882a593Smuzhiyun * application, and identifies the association for
5670*4882a593Smuzhiyun * this query.
5671*4882a593Smuzhiyun * spp_address - This specifies which address is of interest.
5672*4882a593Smuzhiyun * spp_hbinterval - This contains the value of the heartbeat interval,
5673*4882a593Smuzhiyun * in milliseconds. If a value of zero
5674*4882a593Smuzhiyun * is present in this field then no changes are to
5675*4882a593Smuzhiyun * be made to this parameter.
5676*4882a593Smuzhiyun * spp_pathmaxrxt - This contains the maximum number of
5677*4882a593Smuzhiyun * retransmissions before this address shall be
5678*4882a593Smuzhiyun * considered unreachable. If a value of zero
5679*4882a593Smuzhiyun * is present in this field then no changes are to
5680*4882a593Smuzhiyun * be made to this parameter.
5681*4882a593Smuzhiyun * spp_pathmtu - When Path MTU discovery is disabled the value
5682*4882a593Smuzhiyun * specified here will be the "fixed" path mtu.
5683*4882a593Smuzhiyun * Note that if the spp_address field is empty
5684*4882a593Smuzhiyun * then all associations on this address will
5685*4882a593Smuzhiyun * have this fixed path mtu set upon them.
5686*4882a593Smuzhiyun *
5687*4882a593Smuzhiyun * spp_sackdelay - When delayed sack is enabled, this value specifies
5688*4882a593Smuzhiyun * the number of milliseconds that sacks will be delayed
5689*4882a593Smuzhiyun * for. This value will apply to all addresses of an
5690*4882a593Smuzhiyun * association if the spp_address field is empty. Note
5691*4882a593Smuzhiyun * also, that if delayed sack is enabled and this
5692*4882a593Smuzhiyun * value is set to 0, no change is made to the last
5693*4882a593Smuzhiyun * recorded delayed sack timer value.
5694*4882a593Smuzhiyun *
5695*4882a593Smuzhiyun * spp_flags - These flags are used to control various features
5696*4882a593Smuzhiyun * on an association. The flag field may contain
5697*4882a593Smuzhiyun * zero or more of the following options.
5698*4882a593Smuzhiyun *
5699*4882a593Smuzhiyun * SPP_HB_ENABLE - Enable heartbeats on the
5700*4882a593Smuzhiyun * specified address. Note that if the address
5701*4882a593Smuzhiyun * field is empty all addresses for the association
5702*4882a593Smuzhiyun * have heartbeats enabled upon them.
5703*4882a593Smuzhiyun *
5704*4882a593Smuzhiyun * SPP_HB_DISABLE - Disable heartbeats on the
5705*4882a593Smuzhiyun * speicifed address. Note that if the address
5706*4882a593Smuzhiyun * field is empty all addresses for the association
5707*4882a593Smuzhiyun * will have their heartbeats disabled. Note also
5708*4882a593Smuzhiyun * that SPP_HB_ENABLE and SPP_HB_DISABLE are
5709*4882a593Smuzhiyun * mutually exclusive, only one of these two should
5710*4882a593Smuzhiyun * be specified. Enabling both fields will have
5711*4882a593Smuzhiyun * undetermined results.
5712*4882a593Smuzhiyun *
5713*4882a593Smuzhiyun * SPP_HB_DEMAND - Request a user initiated heartbeat
5714*4882a593Smuzhiyun * to be made immediately.
5715*4882a593Smuzhiyun *
5716*4882a593Smuzhiyun * SPP_PMTUD_ENABLE - This field will enable PMTU
5717*4882a593Smuzhiyun * discovery upon the specified address. Note that
5718*4882a593Smuzhiyun * if the address feild is empty then all addresses
5719*4882a593Smuzhiyun * on the association are effected.
5720*4882a593Smuzhiyun *
5721*4882a593Smuzhiyun * SPP_PMTUD_DISABLE - This field will disable PMTU
5722*4882a593Smuzhiyun * discovery upon the specified address. Note that
5723*4882a593Smuzhiyun * if the address feild is empty then all addresses
5724*4882a593Smuzhiyun * on the association are effected. Not also that
5725*4882a593Smuzhiyun * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
5726*4882a593Smuzhiyun * exclusive. Enabling both will have undetermined
5727*4882a593Smuzhiyun * results.
5728*4882a593Smuzhiyun *
5729*4882a593Smuzhiyun * SPP_SACKDELAY_ENABLE - Setting this flag turns
5730*4882a593Smuzhiyun * on delayed sack. The time specified in spp_sackdelay
5731*4882a593Smuzhiyun * is used to specify the sack delay for this address. Note
5732*4882a593Smuzhiyun * that if spp_address is empty then all addresses will
5733*4882a593Smuzhiyun * enable delayed sack and take on the sack delay
5734*4882a593Smuzhiyun * value specified in spp_sackdelay.
5735*4882a593Smuzhiyun * SPP_SACKDELAY_DISABLE - Setting this flag turns
5736*4882a593Smuzhiyun * off delayed sack. If the spp_address field is blank then
5737*4882a593Smuzhiyun * delayed sack is disabled for the entire association. Note
5738*4882a593Smuzhiyun * also that this field is mutually exclusive to
5739*4882a593Smuzhiyun * SPP_SACKDELAY_ENABLE, setting both will have undefined
5740*4882a593Smuzhiyun * results.
5741*4882a593Smuzhiyun *
5742*4882a593Smuzhiyun * SPP_IPV6_FLOWLABEL: Setting this flag enables the
5743*4882a593Smuzhiyun * setting of the IPV6 flow label value. The value is
5744*4882a593Smuzhiyun * contained in the spp_ipv6_flowlabel field.
5745*4882a593Smuzhiyun * Upon retrieval, this flag will be set to indicate that
5746*4882a593Smuzhiyun * the spp_ipv6_flowlabel field has a valid value returned.
5747*4882a593Smuzhiyun * If a specific destination address is set (in the
5748*4882a593Smuzhiyun * spp_address field), then the value returned is that of
5749*4882a593Smuzhiyun * the address. If just an association is specified (and
5750*4882a593Smuzhiyun * no address), then the association's default flow label
5751*4882a593Smuzhiyun * is returned. If neither an association nor a destination
5752*4882a593Smuzhiyun * is specified, then the socket's default flow label is
5753*4882a593Smuzhiyun * returned. For non-IPv6 sockets, this flag will be left
5754*4882a593Smuzhiyun * cleared.
5755*4882a593Smuzhiyun *
5756*4882a593Smuzhiyun * SPP_DSCP: Setting this flag enables the setting of the
5757*4882a593Smuzhiyun * Differentiated Services Code Point (DSCP) value
5758*4882a593Smuzhiyun * associated with either the association or a specific
5759*4882a593Smuzhiyun * address. The value is obtained in the spp_dscp field.
5760*4882a593Smuzhiyun * Upon retrieval, this flag will be set to indicate that
5761*4882a593Smuzhiyun * the spp_dscp field has a valid value returned. If a
5762*4882a593Smuzhiyun * specific destination address is set when called (in the
5763*4882a593Smuzhiyun * spp_address field), then that specific destination
5764*4882a593Smuzhiyun * address's DSCP value is returned. If just an association
5765*4882a593Smuzhiyun * is specified, then the association's default DSCP is
5766*4882a593Smuzhiyun * returned. If neither an association nor a destination is
5767*4882a593Smuzhiyun * specified, then the socket's default DSCP is returned.
5768*4882a593Smuzhiyun *
5769*4882a593Smuzhiyun * spp_ipv6_flowlabel
5770*4882a593Smuzhiyun * - This field is used in conjunction with the
5771*4882a593Smuzhiyun * SPP_IPV6_FLOWLABEL flag and contains the IPv6 flow label.
5772*4882a593Smuzhiyun * The 20 least significant bits are used for the flow
5773*4882a593Smuzhiyun * label. This setting has precedence over any IPv6-layer
5774*4882a593Smuzhiyun * setting.
5775*4882a593Smuzhiyun *
5776*4882a593Smuzhiyun * spp_dscp - This field is used in conjunction with the SPP_DSCP flag
5777*4882a593Smuzhiyun * and contains the DSCP. The 6 most significant bits are
5778*4882a593Smuzhiyun * used for the DSCP. This setting has precedence over any
5779*4882a593Smuzhiyun * IPv4- or IPv6- layer setting.
5780*4882a593Smuzhiyun */
sctp_getsockopt_peer_addr_params(struct sock * sk,int len,char __user * optval,int __user * optlen)5781*4882a593Smuzhiyun static int sctp_getsockopt_peer_addr_params(struct sock *sk, int len,
5782*4882a593Smuzhiyun char __user *optval, int __user *optlen)
5783*4882a593Smuzhiyun {
5784*4882a593Smuzhiyun struct sctp_paddrparams params;
5785*4882a593Smuzhiyun struct sctp_transport *trans = NULL;
5786*4882a593Smuzhiyun struct sctp_association *asoc = NULL;
5787*4882a593Smuzhiyun struct sctp_sock *sp = sctp_sk(sk);
5788*4882a593Smuzhiyun
5789*4882a593Smuzhiyun if (len >= sizeof(params))
5790*4882a593Smuzhiyun len = sizeof(params);
5791*4882a593Smuzhiyun else if (len >= ALIGN(offsetof(struct sctp_paddrparams,
5792*4882a593Smuzhiyun spp_ipv6_flowlabel), 4))
5793*4882a593Smuzhiyun len = ALIGN(offsetof(struct sctp_paddrparams,
5794*4882a593Smuzhiyun spp_ipv6_flowlabel), 4);
5795*4882a593Smuzhiyun else
5796*4882a593Smuzhiyun return -EINVAL;
5797*4882a593Smuzhiyun
5798*4882a593Smuzhiyun if (copy_from_user(¶ms, optval, len))
5799*4882a593Smuzhiyun return -EFAULT;
5800*4882a593Smuzhiyun
5801*4882a593Smuzhiyun /* If an address other than INADDR_ANY is specified, and
5802*4882a593Smuzhiyun * no transport is found, then the request is invalid.
5803*4882a593Smuzhiyun */
5804*4882a593Smuzhiyun if (!sctp_is_any(sk, (union sctp_addr *)¶ms.spp_address)) {
5805*4882a593Smuzhiyun trans = sctp_addr_id2transport(sk, ¶ms.spp_address,
5806*4882a593Smuzhiyun params.spp_assoc_id);
5807*4882a593Smuzhiyun if (!trans) {
5808*4882a593Smuzhiyun pr_debug("%s: failed no transport\n", __func__);
5809*4882a593Smuzhiyun return -EINVAL;
5810*4882a593Smuzhiyun }
5811*4882a593Smuzhiyun }
5812*4882a593Smuzhiyun
5813*4882a593Smuzhiyun /* Get association, if assoc_id != SCTP_FUTURE_ASSOC and the
5814*4882a593Smuzhiyun * socket is a one to many style socket, and an association
5815*4882a593Smuzhiyun * was not found, then the id was invalid.
5816*4882a593Smuzhiyun */
5817*4882a593Smuzhiyun asoc = sctp_id2assoc(sk, params.spp_assoc_id);
5818*4882a593Smuzhiyun if (!asoc && params.spp_assoc_id != SCTP_FUTURE_ASSOC &&
5819*4882a593Smuzhiyun sctp_style(sk, UDP)) {
5820*4882a593Smuzhiyun pr_debug("%s: failed no association\n", __func__);
5821*4882a593Smuzhiyun return -EINVAL;
5822*4882a593Smuzhiyun }
5823*4882a593Smuzhiyun
5824*4882a593Smuzhiyun if (trans) {
5825*4882a593Smuzhiyun /* Fetch transport values. */
5826*4882a593Smuzhiyun params.spp_hbinterval = jiffies_to_msecs(trans->hbinterval);
5827*4882a593Smuzhiyun params.spp_pathmtu = trans->pathmtu;
5828*4882a593Smuzhiyun params.spp_pathmaxrxt = trans->pathmaxrxt;
5829*4882a593Smuzhiyun params.spp_sackdelay = jiffies_to_msecs(trans->sackdelay);
5830*4882a593Smuzhiyun
5831*4882a593Smuzhiyun /*draft-11 doesn't say what to return in spp_flags*/
5832*4882a593Smuzhiyun params.spp_flags = trans->param_flags;
5833*4882a593Smuzhiyun if (trans->flowlabel & SCTP_FLOWLABEL_SET_MASK) {
5834*4882a593Smuzhiyun params.spp_ipv6_flowlabel = trans->flowlabel &
5835*4882a593Smuzhiyun SCTP_FLOWLABEL_VAL_MASK;
5836*4882a593Smuzhiyun params.spp_flags |= SPP_IPV6_FLOWLABEL;
5837*4882a593Smuzhiyun }
5838*4882a593Smuzhiyun if (trans->dscp & SCTP_DSCP_SET_MASK) {
5839*4882a593Smuzhiyun params.spp_dscp = trans->dscp & SCTP_DSCP_VAL_MASK;
5840*4882a593Smuzhiyun params.spp_flags |= SPP_DSCP;
5841*4882a593Smuzhiyun }
5842*4882a593Smuzhiyun } else if (asoc) {
5843*4882a593Smuzhiyun /* Fetch association values. */
5844*4882a593Smuzhiyun params.spp_hbinterval = jiffies_to_msecs(asoc->hbinterval);
5845*4882a593Smuzhiyun params.spp_pathmtu = asoc->pathmtu;
5846*4882a593Smuzhiyun params.spp_pathmaxrxt = asoc->pathmaxrxt;
5847*4882a593Smuzhiyun params.spp_sackdelay = jiffies_to_msecs(asoc->sackdelay);
5848*4882a593Smuzhiyun
5849*4882a593Smuzhiyun /*draft-11 doesn't say what to return in spp_flags*/
5850*4882a593Smuzhiyun params.spp_flags = asoc->param_flags;
5851*4882a593Smuzhiyun if (asoc->flowlabel & SCTP_FLOWLABEL_SET_MASK) {
5852*4882a593Smuzhiyun params.spp_ipv6_flowlabel = asoc->flowlabel &
5853*4882a593Smuzhiyun SCTP_FLOWLABEL_VAL_MASK;
5854*4882a593Smuzhiyun params.spp_flags |= SPP_IPV6_FLOWLABEL;
5855*4882a593Smuzhiyun }
5856*4882a593Smuzhiyun if (asoc->dscp & SCTP_DSCP_SET_MASK) {
5857*4882a593Smuzhiyun params.spp_dscp = asoc->dscp & SCTP_DSCP_VAL_MASK;
5858*4882a593Smuzhiyun params.spp_flags |= SPP_DSCP;
5859*4882a593Smuzhiyun }
5860*4882a593Smuzhiyun } else {
5861*4882a593Smuzhiyun /* Fetch socket values. */
5862*4882a593Smuzhiyun params.spp_hbinterval = sp->hbinterval;
5863*4882a593Smuzhiyun params.spp_pathmtu = sp->pathmtu;
5864*4882a593Smuzhiyun params.spp_sackdelay = sp->sackdelay;
5865*4882a593Smuzhiyun params.spp_pathmaxrxt = sp->pathmaxrxt;
5866*4882a593Smuzhiyun
5867*4882a593Smuzhiyun /*draft-11 doesn't say what to return in spp_flags*/
5868*4882a593Smuzhiyun params.spp_flags = sp->param_flags;
5869*4882a593Smuzhiyun if (sp->flowlabel & SCTP_FLOWLABEL_SET_MASK) {
5870*4882a593Smuzhiyun params.spp_ipv6_flowlabel = sp->flowlabel &
5871*4882a593Smuzhiyun SCTP_FLOWLABEL_VAL_MASK;
5872*4882a593Smuzhiyun params.spp_flags |= SPP_IPV6_FLOWLABEL;
5873*4882a593Smuzhiyun }
5874*4882a593Smuzhiyun if (sp->dscp & SCTP_DSCP_SET_MASK) {
5875*4882a593Smuzhiyun params.spp_dscp = sp->dscp & SCTP_DSCP_VAL_MASK;
5876*4882a593Smuzhiyun params.spp_flags |= SPP_DSCP;
5877*4882a593Smuzhiyun }
5878*4882a593Smuzhiyun }
5879*4882a593Smuzhiyun
5880*4882a593Smuzhiyun if (copy_to_user(optval, ¶ms, len))
5881*4882a593Smuzhiyun return -EFAULT;
5882*4882a593Smuzhiyun
5883*4882a593Smuzhiyun if (put_user(len, optlen))
5884*4882a593Smuzhiyun return -EFAULT;
5885*4882a593Smuzhiyun
5886*4882a593Smuzhiyun return 0;
5887*4882a593Smuzhiyun }
5888*4882a593Smuzhiyun
5889*4882a593Smuzhiyun /*
5890*4882a593Smuzhiyun * 7.1.23. Get or set delayed ack timer (SCTP_DELAYED_SACK)
5891*4882a593Smuzhiyun *
5892*4882a593Smuzhiyun * This option will effect the way delayed acks are performed. This
5893*4882a593Smuzhiyun * option allows you to get or set the delayed ack time, in
5894*4882a593Smuzhiyun * milliseconds. It also allows changing the delayed ack frequency.
5895*4882a593Smuzhiyun * Changing the frequency to 1 disables the delayed sack algorithm. If
5896*4882a593Smuzhiyun * the assoc_id is 0, then this sets or gets the endpoints default
5897*4882a593Smuzhiyun * values. If the assoc_id field is non-zero, then the set or get
5898*4882a593Smuzhiyun * effects the specified association for the one to many model (the
5899*4882a593Smuzhiyun * assoc_id field is ignored by the one to one model). Note that if
5900*4882a593Smuzhiyun * sack_delay or sack_freq are 0 when setting this option, then the
5901*4882a593Smuzhiyun * current values will remain unchanged.
5902*4882a593Smuzhiyun *
5903*4882a593Smuzhiyun * struct sctp_sack_info {
5904*4882a593Smuzhiyun * sctp_assoc_t sack_assoc_id;
5905*4882a593Smuzhiyun * uint32_t sack_delay;
5906*4882a593Smuzhiyun * uint32_t sack_freq;
5907*4882a593Smuzhiyun * };
5908*4882a593Smuzhiyun *
5909*4882a593Smuzhiyun * sack_assoc_id - This parameter, indicates which association the user
5910*4882a593Smuzhiyun * is performing an action upon. Note that if this field's value is
5911*4882a593Smuzhiyun * zero then the endpoints default value is changed (effecting future
5912*4882a593Smuzhiyun * associations only).
5913*4882a593Smuzhiyun *
5914*4882a593Smuzhiyun * sack_delay - This parameter contains the number of milliseconds that
5915*4882a593Smuzhiyun * the user is requesting the delayed ACK timer be set to. Note that
5916*4882a593Smuzhiyun * this value is defined in the standard to be between 200 and 500
5917*4882a593Smuzhiyun * milliseconds.
5918*4882a593Smuzhiyun *
5919*4882a593Smuzhiyun * sack_freq - This parameter contains the number of packets that must
5920*4882a593Smuzhiyun * be received before a sack is sent without waiting for the delay
5921*4882a593Smuzhiyun * timer to expire. The default value for this is 2, setting this
5922*4882a593Smuzhiyun * value to 1 will disable the delayed sack algorithm.
5923*4882a593Smuzhiyun */
sctp_getsockopt_delayed_ack(struct sock * sk,int len,char __user * optval,int __user * optlen)5924*4882a593Smuzhiyun static int sctp_getsockopt_delayed_ack(struct sock *sk, int len,
5925*4882a593Smuzhiyun char __user *optval,
5926*4882a593Smuzhiyun int __user *optlen)
5927*4882a593Smuzhiyun {
5928*4882a593Smuzhiyun struct sctp_sack_info params;
5929*4882a593Smuzhiyun struct sctp_association *asoc = NULL;
5930*4882a593Smuzhiyun struct sctp_sock *sp = sctp_sk(sk);
5931*4882a593Smuzhiyun
5932*4882a593Smuzhiyun if (len >= sizeof(struct sctp_sack_info)) {
5933*4882a593Smuzhiyun len = sizeof(struct sctp_sack_info);
5934*4882a593Smuzhiyun
5935*4882a593Smuzhiyun if (copy_from_user(¶ms, optval, len))
5936*4882a593Smuzhiyun return -EFAULT;
5937*4882a593Smuzhiyun } else if (len == sizeof(struct sctp_assoc_value)) {
5938*4882a593Smuzhiyun pr_warn_ratelimited(DEPRECATED
5939*4882a593Smuzhiyun "%s (pid %d) "
5940*4882a593Smuzhiyun "Use of struct sctp_assoc_value in delayed_ack socket option.\n"
5941*4882a593Smuzhiyun "Use struct sctp_sack_info instead\n",
5942*4882a593Smuzhiyun current->comm, task_pid_nr(current));
5943*4882a593Smuzhiyun if (copy_from_user(¶ms, optval, len))
5944*4882a593Smuzhiyun return -EFAULT;
5945*4882a593Smuzhiyun } else
5946*4882a593Smuzhiyun return -EINVAL;
5947*4882a593Smuzhiyun
5948*4882a593Smuzhiyun /* Get association, if sack_assoc_id != SCTP_FUTURE_ASSOC and the
5949*4882a593Smuzhiyun * socket is a one to many style socket, and an association
5950*4882a593Smuzhiyun * was not found, then the id was invalid.
5951*4882a593Smuzhiyun */
5952*4882a593Smuzhiyun asoc = sctp_id2assoc(sk, params.sack_assoc_id);
5953*4882a593Smuzhiyun if (!asoc && params.sack_assoc_id != SCTP_FUTURE_ASSOC &&
5954*4882a593Smuzhiyun sctp_style(sk, UDP))
5955*4882a593Smuzhiyun return -EINVAL;
5956*4882a593Smuzhiyun
5957*4882a593Smuzhiyun if (asoc) {
5958*4882a593Smuzhiyun /* Fetch association values. */
5959*4882a593Smuzhiyun if (asoc->param_flags & SPP_SACKDELAY_ENABLE) {
5960*4882a593Smuzhiyun params.sack_delay = jiffies_to_msecs(asoc->sackdelay);
5961*4882a593Smuzhiyun params.sack_freq = asoc->sackfreq;
5962*4882a593Smuzhiyun
5963*4882a593Smuzhiyun } else {
5964*4882a593Smuzhiyun params.sack_delay = 0;
5965*4882a593Smuzhiyun params.sack_freq = 1;
5966*4882a593Smuzhiyun }
5967*4882a593Smuzhiyun } else {
5968*4882a593Smuzhiyun /* Fetch socket values. */
5969*4882a593Smuzhiyun if (sp->param_flags & SPP_SACKDELAY_ENABLE) {
5970*4882a593Smuzhiyun params.sack_delay = sp->sackdelay;
5971*4882a593Smuzhiyun params.sack_freq = sp->sackfreq;
5972*4882a593Smuzhiyun } else {
5973*4882a593Smuzhiyun params.sack_delay = 0;
5974*4882a593Smuzhiyun params.sack_freq = 1;
5975*4882a593Smuzhiyun }
5976*4882a593Smuzhiyun }
5977*4882a593Smuzhiyun
5978*4882a593Smuzhiyun if (copy_to_user(optval, ¶ms, len))
5979*4882a593Smuzhiyun return -EFAULT;
5980*4882a593Smuzhiyun
5981*4882a593Smuzhiyun if (put_user(len, optlen))
5982*4882a593Smuzhiyun return -EFAULT;
5983*4882a593Smuzhiyun
5984*4882a593Smuzhiyun return 0;
5985*4882a593Smuzhiyun }
5986*4882a593Smuzhiyun
5987*4882a593Smuzhiyun /* 7.1.3 Initialization Parameters (SCTP_INITMSG)
5988*4882a593Smuzhiyun *
5989*4882a593Smuzhiyun * Applications can specify protocol parameters for the default association
5990*4882a593Smuzhiyun * initialization. The option name argument to setsockopt() and getsockopt()
5991*4882a593Smuzhiyun * is SCTP_INITMSG.
5992*4882a593Smuzhiyun *
5993*4882a593Smuzhiyun * Setting initialization parameters is effective only on an unconnected
5994*4882a593Smuzhiyun * socket (for UDP-style sockets only future associations are effected
5995*4882a593Smuzhiyun * by the change). With TCP-style sockets, this option is inherited by
5996*4882a593Smuzhiyun * sockets derived from a listener socket.
5997*4882a593Smuzhiyun */
sctp_getsockopt_initmsg(struct sock * sk,int len,char __user * optval,int __user * optlen)5998*4882a593Smuzhiyun static int sctp_getsockopt_initmsg(struct sock *sk, int len, char __user *optval, int __user *optlen)
5999*4882a593Smuzhiyun {
6000*4882a593Smuzhiyun if (len < sizeof(struct sctp_initmsg))
6001*4882a593Smuzhiyun return -EINVAL;
6002*4882a593Smuzhiyun len = sizeof(struct sctp_initmsg);
6003*4882a593Smuzhiyun if (put_user(len, optlen))
6004*4882a593Smuzhiyun return -EFAULT;
6005*4882a593Smuzhiyun if (copy_to_user(optval, &sctp_sk(sk)->initmsg, len))
6006*4882a593Smuzhiyun return -EFAULT;
6007*4882a593Smuzhiyun return 0;
6008*4882a593Smuzhiyun }
6009*4882a593Smuzhiyun
6010*4882a593Smuzhiyun
sctp_getsockopt_peer_addrs(struct sock * sk,int len,char __user * optval,int __user * optlen)6011*4882a593Smuzhiyun static int sctp_getsockopt_peer_addrs(struct sock *sk, int len,
6012*4882a593Smuzhiyun char __user *optval, int __user *optlen)
6013*4882a593Smuzhiyun {
6014*4882a593Smuzhiyun struct sctp_association *asoc;
6015*4882a593Smuzhiyun int cnt = 0;
6016*4882a593Smuzhiyun struct sctp_getaddrs getaddrs;
6017*4882a593Smuzhiyun struct sctp_transport *from;
6018*4882a593Smuzhiyun void __user *to;
6019*4882a593Smuzhiyun union sctp_addr temp;
6020*4882a593Smuzhiyun struct sctp_sock *sp = sctp_sk(sk);
6021*4882a593Smuzhiyun int addrlen;
6022*4882a593Smuzhiyun size_t space_left;
6023*4882a593Smuzhiyun int bytes_copied;
6024*4882a593Smuzhiyun
6025*4882a593Smuzhiyun if (len < sizeof(struct sctp_getaddrs))
6026*4882a593Smuzhiyun return -EINVAL;
6027*4882a593Smuzhiyun
6028*4882a593Smuzhiyun if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
6029*4882a593Smuzhiyun return -EFAULT;
6030*4882a593Smuzhiyun
6031*4882a593Smuzhiyun /* For UDP-style sockets, id specifies the association to query. */
6032*4882a593Smuzhiyun asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
6033*4882a593Smuzhiyun if (!asoc)
6034*4882a593Smuzhiyun return -EINVAL;
6035*4882a593Smuzhiyun
6036*4882a593Smuzhiyun to = optval + offsetof(struct sctp_getaddrs, addrs);
6037*4882a593Smuzhiyun space_left = len - offsetof(struct sctp_getaddrs, addrs);
6038*4882a593Smuzhiyun
6039*4882a593Smuzhiyun list_for_each_entry(from, &asoc->peer.transport_addr_list,
6040*4882a593Smuzhiyun transports) {
6041*4882a593Smuzhiyun memcpy(&temp, &from->ipaddr, sizeof(temp));
6042*4882a593Smuzhiyun addrlen = sctp_get_pf_specific(sk->sk_family)
6043*4882a593Smuzhiyun ->addr_to_user(sp, &temp);
6044*4882a593Smuzhiyun if (space_left < addrlen)
6045*4882a593Smuzhiyun return -ENOMEM;
6046*4882a593Smuzhiyun if (copy_to_user(to, &temp, addrlen))
6047*4882a593Smuzhiyun return -EFAULT;
6048*4882a593Smuzhiyun to += addrlen;
6049*4882a593Smuzhiyun cnt++;
6050*4882a593Smuzhiyun space_left -= addrlen;
6051*4882a593Smuzhiyun }
6052*4882a593Smuzhiyun
6053*4882a593Smuzhiyun if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num))
6054*4882a593Smuzhiyun return -EFAULT;
6055*4882a593Smuzhiyun bytes_copied = ((char __user *)to) - optval;
6056*4882a593Smuzhiyun if (put_user(bytes_copied, optlen))
6057*4882a593Smuzhiyun return -EFAULT;
6058*4882a593Smuzhiyun
6059*4882a593Smuzhiyun return 0;
6060*4882a593Smuzhiyun }
6061*4882a593Smuzhiyun
sctp_copy_laddrs(struct sock * sk,__u16 port,void * to,size_t space_left,int * bytes_copied)6062*4882a593Smuzhiyun static int sctp_copy_laddrs(struct sock *sk, __u16 port, void *to,
6063*4882a593Smuzhiyun size_t space_left, int *bytes_copied)
6064*4882a593Smuzhiyun {
6065*4882a593Smuzhiyun struct sctp_sockaddr_entry *addr;
6066*4882a593Smuzhiyun union sctp_addr temp;
6067*4882a593Smuzhiyun int cnt = 0;
6068*4882a593Smuzhiyun int addrlen;
6069*4882a593Smuzhiyun struct net *net = sock_net(sk);
6070*4882a593Smuzhiyun
6071*4882a593Smuzhiyun rcu_read_lock();
6072*4882a593Smuzhiyun list_for_each_entry_rcu(addr, &net->sctp.local_addr_list, list) {
6073*4882a593Smuzhiyun if (!addr->valid)
6074*4882a593Smuzhiyun continue;
6075*4882a593Smuzhiyun
6076*4882a593Smuzhiyun if ((PF_INET == sk->sk_family) &&
6077*4882a593Smuzhiyun (AF_INET6 == addr->a.sa.sa_family))
6078*4882a593Smuzhiyun continue;
6079*4882a593Smuzhiyun if ((PF_INET6 == sk->sk_family) &&
6080*4882a593Smuzhiyun inet_v6_ipv6only(sk) &&
6081*4882a593Smuzhiyun (AF_INET == addr->a.sa.sa_family))
6082*4882a593Smuzhiyun continue;
6083*4882a593Smuzhiyun memcpy(&temp, &addr->a, sizeof(temp));
6084*4882a593Smuzhiyun if (!temp.v4.sin_port)
6085*4882a593Smuzhiyun temp.v4.sin_port = htons(port);
6086*4882a593Smuzhiyun
6087*4882a593Smuzhiyun addrlen = sctp_get_pf_specific(sk->sk_family)
6088*4882a593Smuzhiyun ->addr_to_user(sctp_sk(sk), &temp);
6089*4882a593Smuzhiyun
6090*4882a593Smuzhiyun if (space_left < addrlen) {
6091*4882a593Smuzhiyun cnt = -ENOMEM;
6092*4882a593Smuzhiyun break;
6093*4882a593Smuzhiyun }
6094*4882a593Smuzhiyun memcpy(to, &temp, addrlen);
6095*4882a593Smuzhiyun
6096*4882a593Smuzhiyun to += addrlen;
6097*4882a593Smuzhiyun cnt++;
6098*4882a593Smuzhiyun space_left -= addrlen;
6099*4882a593Smuzhiyun *bytes_copied += addrlen;
6100*4882a593Smuzhiyun }
6101*4882a593Smuzhiyun rcu_read_unlock();
6102*4882a593Smuzhiyun
6103*4882a593Smuzhiyun return cnt;
6104*4882a593Smuzhiyun }
6105*4882a593Smuzhiyun
6106*4882a593Smuzhiyun
sctp_getsockopt_local_addrs(struct sock * sk,int len,char __user * optval,int __user * optlen)6107*4882a593Smuzhiyun static int sctp_getsockopt_local_addrs(struct sock *sk, int len,
6108*4882a593Smuzhiyun char __user *optval, int __user *optlen)
6109*4882a593Smuzhiyun {
6110*4882a593Smuzhiyun struct sctp_bind_addr *bp;
6111*4882a593Smuzhiyun struct sctp_association *asoc;
6112*4882a593Smuzhiyun int cnt = 0;
6113*4882a593Smuzhiyun struct sctp_getaddrs getaddrs;
6114*4882a593Smuzhiyun struct sctp_sockaddr_entry *addr;
6115*4882a593Smuzhiyun void __user *to;
6116*4882a593Smuzhiyun union sctp_addr temp;
6117*4882a593Smuzhiyun struct sctp_sock *sp = sctp_sk(sk);
6118*4882a593Smuzhiyun int addrlen;
6119*4882a593Smuzhiyun int err = 0;
6120*4882a593Smuzhiyun size_t space_left;
6121*4882a593Smuzhiyun int bytes_copied = 0;
6122*4882a593Smuzhiyun void *addrs;
6123*4882a593Smuzhiyun void *buf;
6124*4882a593Smuzhiyun
6125*4882a593Smuzhiyun if (len < sizeof(struct sctp_getaddrs))
6126*4882a593Smuzhiyun return -EINVAL;
6127*4882a593Smuzhiyun
6128*4882a593Smuzhiyun if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
6129*4882a593Smuzhiyun return -EFAULT;
6130*4882a593Smuzhiyun
6131*4882a593Smuzhiyun /*
6132*4882a593Smuzhiyun * For UDP-style sockets, id specifies the association to query.
6133*4882a593Smuzhiyun * If the id field is set to the value '0' then the locally bound
6134*4882a593Smuzhiyun * addresses are returned without regard to any particular
6135*4882a593Smuzhiyun * association.
6136*4882a593Smuzhiyun */
6137*4882a593Smuzhiyun if (0 == getaddrs.assoc_id) {
6138*4882a593Smuzhiyun bp = &sctp_sk(sk)->ep->base.bind_addr;
6139*4882a593Smuzhiyun } else {
6140*4882a593Smuzhiyun asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
6141*4882a593Smuzhiyun if (!asoc)
6142*4882a593Smuzhiyun return -EINVAL;
6143*4882a593Smuzhiyun bp = &asoc->base.bind_addr;
6144*4882a593Smuzhiyun }
6145*4882a593Smuzhiyun
6146*4882a593Smuzhiyun to = optval + offsetof(struct sctp_getaddrs, addrs);
6147*4882a593Smuzhiyun space_left = len - offsetof(struct sctp_getaddrs, addrs);
6148*4882a593Smuzhiyun
6149*4882a593Smuzhiyun addrs = kmalloc(space_left, GFP_USER | __GFP_NOWARN);
6150*4882a593Smuzhiyun if (!addrs)
6151*4882a593Smuzhiyun return -ENOMEM;
6152*4882a593Smuzhiyun
6153*4882a593Smuzhiyun /* If the endpoint is bound to 0.0.0.0 or ::0, get the valid
6154*4882a593Smuzhiyun * addresses from the global local address list.
6155*4882a593Smuzhiyun */
6156*4882a593Smuzhiyun if (sctp_list_single_entry(&bp->address_list)) {
6157*4882a593Smuzhiyun addr = list_entry(bp->address_list.next,
6158*4882a593Smuzhiyun struct sctp_sockaddr_entry, list);
6159*4882a593Smuzhiyun if (sctp_is_any(sk, &addr->a)) {
6160*4882a593Smuzhiyun cnt = sctp_copy_laddrs(sk, bp->port, addrs,
6161*4882a593Smuzhiyun space_left, &bytes_copied);
6162*4882a593Smuzhiyun if (cnt < 0) {
6163*4882a593Smuzhiyun err = cnt;
6164*4882a593Smuzhiyun goto out;
6165*4882a593Smuzhiyun }
6166*4882a593Smuzhiyun goto copy_getaddrs;
6167*4882a593Smuzhiyun }
6168*4882a593Smuzhiyun }
6169*4882a593Smuzhiyun
6170*4882a593Smuzhiyun buf = addrs;
6171*4882a593Smuzhiyun /* Protection on the bound address list is not needed since
6172*4882a593Smuzhiyun * in the socket option context we hold a socket lock and
6173*4882a593Smuzhiyun * thus the bound address list can't change.
6174*4882a593Smuzhiyun */
6175*4882a593Smuzhiyun list_for_each_entry(addr, &bp->address_list, list) {
6176*4882a593Smuzhiyun memcpy(&temp, &addr->a, sizeof(temp));
6177*4882a593Smuzhiyun addrlen = sctp_get_pf_specific(sk->sk_family)
6178*4882a593Smuzhiyun ->addr_to_user(sp, &temp);
6179*4882a593Smuzhiyun if (space_left < addrlen) {
6180*4882a593Smuzhiyun err = -ENOMEM; /*fixme: right error?*/
6181*4882a593Smuzhiyun goto out;
6182*4882a593Smuzhiyun }
6183*4882a593Smuzhiyun memcpy(buf, &temp, addrlen);
6184*4882a593Smuzhiyun buf += addrlen;
6185*4882a593Smuzhiyun bytes_copied += addrlen;
6186*4882a593Smuzhiyun cnt++;
6187*4882a593Smuzhiyun space_left -= addrlen;
6188*4882a593Smuzhiyun }
6189*4882a593Smuzhiyun
6190*4882a593Smuzhiyun copy_getaddrs:
6191*4882a593Smuzhiyun if (copy_to_user(to, addrs, bytes_copied)) {
6192*4882a593Smuzhiyun err = -EFAULT;
6193*4882a593Smuzhiyun goto out;
6194*4882a593Smuzhiyun }
6195*4882a593Smuzhiyun if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num)) {
6196*4882a593Smuzhiyun err = -EFAULT;
6197*4882a593Smuzhiyun goto out;
6198*4882a593Smuzhiyun }
6199*4882a593Smuzhiyun /* XXX: We should have accounted for sizeof(struct sctp_getaddrs) too,
6200*4882a593Smuzhiyun * but we can't change it anymore.
6201*4882a593Smuzhiyun */
6202*4882a593Smuzhiyun if (put_user(bytes_copied, optlen))
6203*4882a593Smuzhiyun err = -EFAULT;
6204*4882a593Smuzhiyun out:
6205*4882a593Smuzhiyun kfree(addrs);
6206*4882a593Smuzhiyun return err;
6207*4882a593Smuzhiyun }
6208*4882a593Smuzhiyun
6209*4882a593Smuzhiyun /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
6210*4882a593Smuzhiyun *
6211*4882a593Smuzhiyun * Requests that the local SCTP stack use the enclosed peer address as
6212*4882a593Smuzhiyun * the association primary. The enclosed address must be one of the
6213*4882a593Smuzhiyun * association peer's addresses.
6214*4882a593Smuzhiyun */
sctp_getsockopt_primary_addr(struct sock * sk,int len,char __user * optval,int __user * optlen)6215*4882a593Smuzhiyun static int sctp_getsockopt_primary_addr(struct sock *sk, int len,
6216*4882a593Smuzhiyun char __user *optval, int __user *optlen)
6217*4882a593Smuzhiyun {
6218*4882a593Smuzhiyun struct sctp_prim prim;
6219*4882a593Smuzhiyun struct sctp_association *asoc;
6220*4882a593Smuzhiyun struct sctp_sock *sp = sctp_sk(sk);
6221*4882a593Smuzhiyun
6222*4882a593Smuzhiyun if (len < sizeof(struct sctp_prim))
6223*4882a593Smuzhiyun return -EINVAL;
6224*4882a593Smuzhiyun
6225*4882a593Smuzhiyun len = sizeof(struct sctp_prim);
6226*4882a593Smuzhiyun
6227*4882a593Smuzhiyun if (copy_from_user(&prim, optval, len))
6228*4882a593Smuzhiyun return -EFAULT;
6229*4882a593Smuzhiyun
6230*4882a593Smuzhiyun asoc = sctp_id2assoc(sk, prim.ssp_assoc_id);
6231*4882a593Smuzhiyun if (!asoc)
6232*4882a593Smuzhiyun return -EINVAL;
6233*4882a593Smuzhiyun
6234*4882a593Smuzhiyun if (!asoc->peer.primary_path)
6235*4882a593Smuzhiyun return -ENOTCONN;
6236*4882a593Smuzhiyun
6237*4882a593Smuzhiyun memcpy(&prim.ssp_addr, &asoc->peer.primary_path->ipaddr,
6238*4882a593Smuzhiyun asoc->peer.primary_path->af_specific->sockaddr_len);
6239*4882a593Smuzhiyun
6240*4882a593Smuzhiyun sctp_get_pf_specific(sk->sk_family)->addr_to_user(sp,
6241*4882a593Smuzhiyun (union sctp_addr *)&prim.ssp_addr);
6242*4882a593Smuzhiyun
6243*4882a593Smuzhiyun if (put_user(len, optlen))
6244*4882a593Smuzhiyun return -EFAULT;
6245*4882a593Smuzhiyun if (copy_to_user(optval, &prim, len))
6246*4882a593Smuzhiyun return -EFAULT;
6247*4882a593Smuzhiyun
6248*4882a593Smuzhiyun return 0;
6249*4882a593Smuzhiyun }
6250*4882a593Smuzhiyun
6251*4882a593Smuzhiyun /*
6252*4882a593Smuzhiyun * 7.1.11 Set Adaptation Layer Indicator (SCTP_ADAPTATION_LAYER)
6253*4882a593Smuzhiyun *
6254*4882a593Smuzhiyun * Requests that the local endpoint set the specified Adaptation Layer
6255*4882a593Smuzhiyun * Indication parameter for all future INIT and INIT-ACK exchanges.
6256*4882a593Smuzhiyun */
sctp_getsockopt_adaptation_layer(struct sock * sk,int len,char __user * optval,int __user * optlen)6257*4882a593Smuzhiyun static int sctp_getsockopt_adaptation_layer(struct sock *sk, int len,
6258*4882a593Smuzhiyun char __user *optval, int __user *optlen)
6259*4882a593Smuzhiyun {
6260*4882a593Smuzhiyun struct sctp_setadaptation adaptation;
6261*4882a593Smuzhiyun
6262*4882a593Smuzhiyun if (len < sizeof(struct sctp_setadaptation))
6263*4882a593Smuzhiyun return -EINVAL;
6264*4882a593Smuzhiyun
6265*4882a593Smuzhiyun len = sizeof(struct sctp_setadaptation);
6266*4882a593Smuzhiyun
6267*4882a593Smuzhiyun adaptation.ssb_adaptation_ind = sctp_sk(sk)->adaptation_ind;
6268*4882a593Smuzhiyun
6269*4882a593Smuzhiyun if (put_user(len, optlen))
6270*4882a593Smuzhiyun return -EFAULT;
6271*4882a593Smuzhiyun if (copy_to_user(optval, &adaptation, len))
6272*4882a593Smuzhiyun return -EFAULT;
6273*4882a593Smuzhiyun
6274*4882a593Smuzhiyun return 0;
6275*4882a593Smuzhiyun }
6276*4882a593Smuzhiyun
6277*4882a593Smuzhiyun /*
6278*4882a593Smuzhiyun *
6279*4882a593Smuzhiyun * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
6280*4882a593Smuzhiyun *
6281*4882a593Smuzhiyun * Applications that wish to use the sendto() system call may wish to
6282*4882a593Smuzhiyun * specify a default set of parameters that would normally be supplied
6283*4882a593Smuzhiyun * through the inclusion of ancillary data. This socket option allows
6284*4882a593Smuzhiyun * such an application to set the default sctp_sndrcvinfo structure.
6285*4882a593Smuzhiyun
6286*4882a593Smuzhiyun
6287*4882a593Smuzhiyun * The application that wishes to use this socket option simply passes
6288*4882a593Smuzhiyun * in to this call the sctp_sndrcvinfo structure defined in Section
6289*4882a593Smuzhiyun * 5.2.2) The input parameters accepted by this call include
6290*4882a593Smuzhiyun * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
6291*4882a593Smuzhiyun * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
6292*4882a593Smuzhiyun * to this call if the caller is using the UDP model.
6293*4882a593Smuzhiyun *
6294*4882a593Smuzhiyun * For getsockopt, it get the default sctp_sndrcvinfo structure.
6295*4882a593Smuzhiyun */
sctp_getsockopt_default_send_param(struct sock * sk,int len,char __user * optval,int __user * optlen)6296*4882a593Smuzhiyun static int sctp_getsockopt_default_send_param(struct sock *sk,
6297*4882a593Smuzhiyun int len, char __user *optval,
6298*4882a593Smuzhiyun int __user *optlen)
6299*4882a593Smuzhiyun {
6300*4882a593Smuzhiyun struct sctp_sock *sp = sctp_sk(sk);
6301*4882a593Smuzhiyun struct sctp_association *asoc;
6302*4882a593Smuzhiyun struct sctp_sndrcvinfo info;
6303*4882a593Smuzhiyun
6304*4882a593Smuzhiyun if (len < sizeof(info))
6305*4882a593Smuzhiyun return -EINVAL;
6306*4882a593Smuzhiyun
6307*4882a593Smuzhiyun len = sizeof(info);
6308*4882a593Smuzhiyun
6309*4882a593Smuzhiyun if (copy_from_user(&info, optval, len))
6310*4882a593Smuzhiyun return -EFAULT;
6311*4882a593Smuzhiyun
6312*4882a593Smuzhiyun asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
6313*4882a593Smuzhiyun if (!asoc && info.sinfo_assoc_id != SCTP_FUTURE_ASSOC &&
6314*4882a593Smuzhiyun sctp_style(sk, UDP))
6315*4882a593Smuzhiyun return -EINVAL;
6316*4882a593Smuzhiyun
6317*4882a593Smuzhiyun if (asoc) {
6318*4882a593Smuzhiyun info.sinfo_stream = asoc->default_stream;
6319*4882a593Smuzhiyun info.sinfo_flags = asoc->default_flags;
6320*4882a593Smuzhiyun info.sinfo_ppid = asoc->default_ppid;
6321*4882a593Smuzhiyun info.sinfo_context = asoc->default_context;
6322*4882a593Smuzhiyun info.sinfo_timetolive = asoc->default_timetolive;
6323*4882a593Smuzhiyun } else {
6324*4882a593Smuzhiyun info.sinfo_stream = sp->default_stream;
6325*4882a593Smuzhiyun info.sinfo_flags = sp->default_flags;
6326*4882a593Smuzhiyun info.sinfo_ppid = sp->default_ppid;
6327*4882a593Smuzhiyun info.sinfo_context = sp->default_context;
6328*4882a593Smuzhiyun info.sinfo_timetolive = sp->default_timetolive;
6329*4882a593Smuzhiyun }
6330*4882a593Smuzhiyun
6331*4882a593Smuzhiyun if (put_user(len, optlen))
6332*4882a593Smuzhiyun return -EFAULT;
6333*4882a593Smuzhiyun if (copy_to_user(optval, &info, len))
6334*4882a593Smuzhiyun return -EFAULT;
6335*4882a593Smuzhiyun
6336*4882a593Smuzhiyun return 0;
6337*4882a593Smuzhiyun }
6338*4882a593Smuzhiyun
6339*4882a593Smuzhiyun /* RFC6458, Section 8.1.31. Set/get Default Send Parameters
6340*4882a593Smuzhiyun * (SCTP_DEFAULT_SNDINFO)
6341*4882a593Smuzhiyun */
sctp_getsockopt_default_sndinfo(struct sock * sk,int len,char __user * optval,int __user * optlen)6342*4882a593Smuzhiyun static int sctp_getsockopt_default_sndinfo(struct sock *sk, int len,
6343*4882a593Smuzhiyun char __user *optval,
6344*4882a593Smuzhiyun int __user *optlen)
6345*4882a593Smuzhiyun {
6346*4882a593Smuzhiyun struct sctp_sock *sp = sctp_sk(sk);
6347*4882a593Smuzhiyun struct sctp_association *asoc;
6348*4882a593Smuzhiyun struct sctp_sndinfo info;
6349*4882a593Smuzhiyun
6350*4882a593Smuzhiyun if (len < sizeof(info))
6351*4882a593Smuzhiyun return -EINVAL;
6352*4882a593Smuzhiyun
6353*4882a593Smuzhiyun len = sizeof(info);
6354*4882a593Smuzhiyun
6355*4882a593Smuzhiyun if (copy_from_user(&info, optval, len))
6356*4882a593Smuzhiyun return -EFAULT;
6357*4882a593Smuzhiyun
6358*4882a593Smuzhiyun asoc = sctp_id2assoc(sk, info.snd_assoc_id);
6359*4882a593Smuzhiyun if (!asoc && info.snd_assoc_id != SCTP_FUTURE_ASSOC &&
6360*4882a593Smuzhiyun sctp_style(sk, UDP))
6361*4882a593Smuzhiyun return -EINVAL;
6362*4882a593Smuzhiyun
6363*4882a593Smuzhiyun if (asoc) {
6364*4882a593Smuzhiyun info.snd_sid = asoc->default_stream;
6365*4882a593Smuzhiyun info.snd_flags = asoc->default_flags;
6366*4882a593Smuzhiyun info.snd_ppid = asoc->default_ppid;
6367*4882a593Smuzhiyun info.snd_context = asoc->default_context;
6368*4882a593Smuzhiyun } else {
6369*4882a593Smuzhiyun info.snd_sid = sp->default_stream;
6370*4882a593Smuzhiyun info.snd_flags = sp->default_flags;
6371*4882a593Smuzhiyun info.snd_ppid = sp->default_ppid;
6372*4882a593Smuzhiyun info.snd_context = sp->default_context;
6373*4882a593Smuzhiyun }
6374*4882a593Smuzhiyun
6375*4882a593Smuzhiyun if (put_user(len, optlen))
6376*4882a593Smuzhiyun return -EFAULT;
6377*4882a593Smuzhiyun if (copy_to_user(optval, &info, len))
6378*4882a593Smuzhiyun return -EFAULT;
6379*4882a593Smuzhiyun
6380*4882a593Smuzhiyun return 0;
6381*4882a593Smuzhiyun }
6382*4882a593Smuzhiyun
6383*4882a593Smuzhiyun /*
6384*4882a593Smuzhiyun *
6385*4882a593Smuzhiyun * 7.1.5 SCTP_NODELAY
6386*4882a593Smuzhiyun *
6387*4882a593Smuzhiyun * Turn on/off any Nagle-like algorithm. This means that packets are
6388*4882a593Smuzhiyun * generally sent as soon as possible and no unnecessary delays are
6389*4882a593Smuzhiyun * introduced, at the cost of more packets in the network. Expects an
6390*4882a593Smuzhiyun * integer boolean flag.
6391*4882a593Smuzhiyun */
6392*4882a593Smuzhiyun
sctp_getsockopt_nodelay(struct sock * sk,int len,char __user * optval,int __user * optlen)6393*4882a593Smuzhiyun static int sctp_getsockopt_nodelay(struct sock *sk, int len,
6394*4882a593Smuzhiyun char __user *optval, int __user *optlen)
6395*4882a593Smuzhiyun {
6396*4882a593Smuzhiyun int val;
6397*4882a593Smuzhiyun
6398*4882a593Smuzhiyun if (len < sizeof(int))
6399*4882a593Smuzhiyun return -EINVAL;
6400*4882a593Smuzhiyun
6401*4882a593Smuzhiyun len = sizeof(int);
6402*4882a593Smuzhiyun val = (sctp_sk(sk)->nodelay == 1);
6403*4882a593Smuzhiyun if (put_user(len, optlen))
6404*4882a593Smuzhiyun return -EFAULT;
6405*4882a593Smuzhiyun if (copy_to_user(optval, &val, len))
6406*4882a593Smuzhiyun return -EFAULT;
6407*4882a593Smuzhiyun return 0;
6408*4882a593Smuzhiyun }
6409*4882a593Smuzhiyun
6410*4882a593Smuzhiyun /*
6411*4882a593Smuzhiyun *
6412*4882a593Smuzhiyun * 7.1.1 SCTP_RTOINFO
6413*4882a593Smuzhiyun *
6414*4882a593Smuzhiyun * The protocol parameters used to initialize and bound retransmission
6415*4882a593Smuzhiyun * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
6416*4882a593Smuzhiyun * and modify these parameters.
6417*4882a593Smuzhiyun * All parameters are time values, in milliseconds. A value of 0, when
6418*4882a593Smuzhiyun * modifying the parameters, indicates that the current value should not
6419*4882a593Smuzhiyun * be changed.
6420*4882a593Smuzhiyun *
6421*4882a593Smuzhiyun */
sctp_getsockopt_rtoinfo(struct sock * sk,int len,char __user * optval,int __user * optlen)6422*4882a593Smuzhiyun static int sctp_getsockopt_rtoinfo(struct sock *sk, int len,
6423*4882a593Smuzhiyun char __user *optval,
6424*4882a593Smuzhiyun int __user *optlen) {
6425*4882a593Smuzhiyun struct sctp_rtoinfo rtoinfo;
6426*4882a593Smuzhiyun struct sctp_association *asoc;
6427*4882a593Smuzhiyun
6428*4882a593Smuzhiyun if (len < sizeof (struct sctp_rtoinfo))
6429*4882a593Smuzhiyun return -EINVAL;
6430*4882a593Smuzhiyun
6431*4882a593Smuzhiyun len = sizeof(struct sctp_rtoinfo);
6432*4882a593Smuzhiyun
6433*4882a593Smuzhiyun if (copy_from_user(&rtoinfo, optval, len))
6434*4882a593Smuzhiyun return -EFAULT;
6435*4882a593Smuzhiyun
6436*4882a593Smuzhiyun asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
6437*4882a593Smuzhiyun
6438*4882a593Smuzhiyun if (!asoc && rtoinfo.srto_assoc_id != SCTP_FUTURE_ASSOC &&
6439*4882a593Smuzhiyun sctp_style(sk, UDP))
6440*4882a593Smuzhiyun return -EINVAL;
6441*4882a593Smuzhiyun
6442*4882a593Smuzhiyun /* Values corresponding to the specific association. */
6443*4882a593Smuzhiyun if (asoc) {
6444*4882a593Smuzhiyun rtoinfo.srto_initial = jiffies_to_msecs(asoc->rto_initial);
6445*4882a593Smuzhiyun rtoinfo.srto_max = jiffies_to_msecs(asoc->rto_max);
6446*4882a593Smuzhiyun rtoinfo.srto_min = jiffies_to_msecs(asoc->rto_min);
6447*4882a593Smuzhiyun } else {
6448*4882a593Smuzhiyun /* Values corresponding to the endpoint. */
6449*4882a593Smuzhiyun struct sctp_sock *sp = sctp_sk(sk);
6450*4882a593Smuzhiyun
6451*4882a593Smuzhiyun rtoinfo.srto_initial = sp->rtoinfo.srto_initial;
6452*4882a593Smuzhiyun rtoinfo.srto_max = sp->rtoinfo.srto_max;
6453*4882a593Smuzhiyun rtoinfo.srto_min = sp->rtoinfo.srto_min;
6454*4882a593Smuzhiyun }
6455*4882a593Smuzhiyun
6456*4882a593Smuzhiyun if (put_user(len, optlen))
6457*4882a593Smuzhiyun return -EFAULT;
6458*4882a593Smuzhiyun
6459*4882a593Smuzhiyun if (copy_to_user(optval, &rtoinfo, len))
6460*4882a593Smuzhiyun return -EFAULT;
6461*4882a593Smuzhiyun
6462*4882a593Smuzhiyun return 0;
6463*4882a593Smuzhiyun }
6464*4882a593Smuzhiyun
6465*4882a593Smuzhiyun /*
6466*4882a593Smuzhiyun *
6467*4882a593Smuzhiyun * 7.1.2 SCTP_ASSOCINFO
6468*4882a593Smuzhiyun *
6469*4882a593Smuzhiyun * This option is used to tune the maximum retransmission attempts
6470*4882a593Smuzhiyun * of the association.
6471*4882a593Smuzhiyun * Returns an error if the new association retransmission value is
6472*4882a593Smuzhiyun * greater than the sum of the retransmission value of the peer.
6473*4882a593Smuzhiyun * See [SCTP] for more information.
6474*4882a593Smuzhiyun *
6475*4882a593Smuzhiyun */
sctp_getsockopt_associnfo(struct sock * sk,int len,char __user * optval,int __user * optlen)6476*4882a593Smuzhiyun static int sctp_getsockopt_associnfo(struct sock *sk, int len,
6477*4882a593Smuzhiyun char __user *optval,
6478*4882a593Smuzhiyun int __user *optlen)
6479*4882a593Smuzhiyun {
6480*4882a593Smuzhiyun
6481*4882a593Smuzhiyun struct sctp_assocparams assocparams;
6482*4882a593Smuzhiyun struct sctp_association *asoc;
6483*4882a593Smuzhiyun struct list_head *pos;
6484*4882a593Smuzhiyun int cnt = 0;
6485*4882a593Smuzhiyun
6486*4882a593Smuzhiyun if (len < sizeof (struct sctp_assocparams))
6487*4882a593Smuzhiyun return -EINVAL;
6488*4882a593Smuzhiyun
6489*4882a593Smuzhiyun len = sizeof(struct sctp_assocparams);
6490*4882a593Smuzhiyun
6491*4882a593Smuzhiyun if (copy_from_user(&assocparams, optval, len))
6492*4882a593Smuzhiyun return -EFAULT;
6493*4882a593Smuzhiyun
6494*4882a593Smuzhiyun asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
6495*4882a593Smuzhiyun
6496*4882a593Smuzhiyun if (!asoc && assocparams.sasoc_assoc_id != SCTP_FUTURE_ASSOC &&
6497*4882a593Smuzhiyun sctp_style(sk, UDP))
6498*4882a593Smuzhiyun return -EINVAL;
6499*4882a593Smuzhiyun
6500*4882a593Smuzhiyun /* Values correspoinding to the specific association */
6501*4882a593Smuzhiyun if (asoc) {
6502*4882a593Smuzhiyun assocparams.sasoc_asocmaxrxt = asoc->max_retrans;
6503*4882a593Smuzhiyun assocparams.sasoc_peer_rwnd = asoc->peer.rwnd;
6504*4882a593Smuzhiyun assocparams.sasoc_local_rwnd = asoc->a_rwnd;
6505*4882a593Smuzhiyun assocparams.sasoc_cookie_life = ktime_to_ms(asoc->cookie_life);
6506*4882a593Smuzhiyun
6507*4882a593Smuzhiyun list_for_each(pos, &asoc->peer.transport_addr_list) {
6508*4882a593Smuzhiyun cnt++;
6509*4882a593Smuzhiyun }
6510*4882a593Smuzhiyun
6511*4882a593Smuzhiyun assocparams.sasoc_number_peer_destinations = cnt;
6512*4882a593Smuzhiyun } else {
6513*4882a593Smuzhiyun /* Values corresponding to the endpoint */
6514*4882a593Smuzhiyun struct sctp_sock *sp = sctp_sk(sk);
6515*4882a593Smuzhiyun
6516*4882a593Smuzhiyun assocparams.sasoc_asocmaxrxt = sp->assocparams.sasoc_asocmaxrxt;
6517*4882a593Smuzhiyun assocparams.sasoc_peer_rwnd = sp->assocparams.sasoc_peer_rwnd;
6518*4882a593Smuzhiyun assocparams.sasoc_local_rwnd = sp->assocparams.sasoc_local_rwnd;
6519*4882a593Smuzhiyun assocparams.sasoc_cookie_life =
6520*4882a593Smuzhiyun sp->assocparams.sasoc_cookie_life;
6521*4882a593Smuzhiyun assocparams.sasoc_number_peer_destinations =
6522*4882a593Smuzhiyun sp->assocparams.
6523*4882a593Smuzhiyun sasoc_number_peer_destinations;
6524*4882a593Smuzhiyun }
6525*4882a593Smuzhiyun
6526*4882a593Smuzhiyun if (put_user(len, optlen))
6527*4882a593Smuzhiyun return -EFAULT;
6528*4882a593Smuzhiyun
6529*4882a593Smuzhiyun if (copy_to_user(optval, &assocparams, len))
6530*4882a593Smuzhiyun return -EFAULT;
6531*4882a593Smuzhiyun
6532*4882a593Smuzhiyun return 0;
6533*4882a593Smuzhiyun }
6534*4882a593Smuzhiyun
6535*4882a593Smuzhiyun /*
6536*4882a593Smuzhiyun * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
6537*4882a593Smuzhiyun *
6538*4882a593Smuzhiyun * This socket option is a boolean flag which turns on or off mapped V4
6539*4882a593Smuzhiyun * addresses. If this option is turned on and the socket is type
6540*4882a593Smuzhiyun * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
6541*4882a593Smuzhiyun * If this option is turned off, then no mapping will be done of V4
6542*4882a593Smuzhiyun * addresses and a user will receive both PF_INET6 and PF_INET type
6543*4882a593Smuzhiyun * addresses on the socket.
6544*4882a593Smuzhiyun */
sctp_getsockopt_mappedv4(struct sock * sk,int len,char __user * optval,int __user * optlen)6545*4882a593Smuzhiyun static int sctp_getsockopt_mappedv4(struct sock *sk, int len,
6546*4882a593Smuzhiyun char __user *optval, int __user *optlen)
6547*4882a593Smuzhiyun {
6548*4882a593Smuzhiyun int val;
6549*4882a593Smuzhiyun struct sctp_sock *sp = sctp_sk(sk);
6550*4882a593Smuzhiyun
6551*4882a593Smuzhiyun if (len < sizeof(int))
6552*4882a593Smuzhiyun return -EINVAL;
6553*4882a593Smuzhiyun
6554*4882a593Smuzhiyun len = sizeof(int);
6555*4882a593Smuzhiyun val = sp->v4mapped;
6556*4882a593Smuzhiyun if (put_user(len, optlen))
6557*4882a593Smuzhiyun return -EFAULT;
6558*4882a593Smuzhiyun if (copy_to_user(optval, &val, len))
6559*4882a593Smuzhiyun return -EFAULT;
6560*4882a593Smuzhiyun
6561*4882a593Smuzhiyun return 0;
6562*4882a593Smuzhiyun }
6563*4882a593Smuzhiyun
6564*4882a593Smuzhiyun /*
6565*4882a593Smuzhiyun * 7.1.29. Set or Get the default context (SCTP_CONTEXT)
6566*4882a593Smuzhiyun * (chapter and verse is quoted at sctp_setsockopt_context())
6567*4882a593Smuzhiyun */
sctp_getsockopt_context(struct sock * sk,int len,char __user * optval,int __user * optlen)6568*4882a593Smuzhiyun static int sctp_getsockopt_context(struct sock *sk, int len,
6569*4882a593Smuzhiyun char __user *optval, int __user *optlen)
6570*4882a593Smuzhiyun {
6571*4882a593Smuzhiyun struct sctp_assoc_value params;
6572*4882a593Smuzhiyun struct sctp_association *asoc;
6573*4882a593Smuzhiyun
6574*4882a593Smuzhiyun if (len < sizeof(struct sctp_assoc_value))
6575*4882a593Smuzhiyun return -EINVAL;
6576*4882a593Smuzhiyun
6577*4882a593Smuzhiyun len = sizeof(struct sctp_assoc_value);
6578*4882a593Smuzhiyun
6579*4882a593Smuzhiyun if (copy_from_user(¶ms, optval, len))
6580*4882a593Smuzhiyun return -EFAULT;
6581*4882a593Smuzhiyun
6582*4882a593Smuzhiyun asoc = sctp_id2assoc(sk, params.assoc_id);
6583*4882a593Smuzhiyun if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
6584*4882a593Smuzhiyun sctp_style(sk, UDP))
6585*4882a593Smuzhiyun return -EINVAL;
6586*4882a593Smuzhiyun
6587*4882a593Smuzhiyun params.assoc_value = asoc ? asoc->default_rcv_context
6588*4882a593Smuzhiyun : sctp_sk(sk)->default_rcv_context;
6589*4882a593Smuzhiyun
6590*4882a593Smuzhiyun if (put_user(len, optlen))
6591*4882a593Smuzhiyun return -EFAULT;
6592*4882a593Smuzhiyun if (copy_to_user(optval, ¶ms, len))
6593*4882a593Smuzhiyun return -EFAULT;
6594*4882a593Smuzhiyun
6595*4882a593Smuzhiyun return 0;
6596*4882a593Smuzhiyun }
6597*4882a593Smuzhiyun
6598*4882a593Smuzhiyun /*
6599*4882a593Smuzhiyun * 8.1.16. Get or Set the Maximum Fragmentation Size (SCTP_MAXSEG)
6600*4882a593Smuzhiyun * This option will get or set the maximum size to put in any outgoing
6601*4882a593Smuzhiyun * SCTP DATA chunk. If a message is larger than this size it will be
6602*4882a593Smuzhiyun * fragmented by SCTP into the specified size. Note that the underlying
6603*4882a593Smuzhiyun * SCTP implementation may fragment into smaller sized chunks when the
6604*4882a593Smuzhiyun * PMTU of the underlying association is smaller than the value set by
6605*4882a593Smuzhiyun * the user. The default value for this option is '0' which indicates
6606*4882a593Smuzhiyun * the user is NOT limiting fragmentation and only the PMTU will effect
6607*4882a593Smuzhiyun * SCTP's choice of DATA chunk size. Note also that values set larger
6608*4882a593Smuzhiyun * than the maximum size of an IP datagram will effectively let SCTP
6609*4882a593Smuzhiyun * control fragmentation (i.e. the same as setting this option to 0).
6610*4882a593Smuzhiyun *
6611*4882a593Smuzhiyun * The following structure is used to access and modify this parameter:
6612*4882a593Smuzhiyun *
6613*4882a593Smuzhiyun * struct sctp_assoc_value {
6614*4882a593Smuzhiyun * sctp_assoc_t assoc_id;
6615*4882a593Smuzhiyun * uint32_t assoc_value;
6616*4882a593Smuzhiyun * };
6617*4882a593Smuzhiyun *
6618*4882a593Smuzhiyun * assoc_id: This parameter is ignored for one-to-one style sockets.
6619*4882a593Smuzhiyun * For one-to-many style sockets this parameter indicates which
6620*4882a593Smuzhiyun * association the user is performing an action upon. Note that if
6621*4882a593Smuzhiyun * this field's value is zero then the endpoints default value is
6622*4882a593Smuzhiyun * changed (effecting future associations only).
6623*4882a593Smuzhiyun * assoc_value: This parameter specifies the maximum size in bytes.
6624*4882a593Smuzhiyun */
sctp_getsockopt_maxseg(struct sock * sk,int len,char __user * optval,int __user * optlen)6625*4882a593Smuzhiyun static int sctp_getsockopt_maxseg(struct sock *sk, int len,
6626*4882a593Smuzhiyun char __user *optval, int __user *optlen)
6627*4882a593Smuzhiyun {
6628*4882a593Smuzhiyun struct sctp_assoc_value params;
6629*4882a593Smuzhiyun struct sctp_association *asoc;
6630*4882a593Smuzhiyun
6631*4882a593Smuzhiyun if (len == sizeof(int)) {
6632*4882a593Smuzhiyun pr_warn_ratelimited(DEPRECATED
6633*4882a593Smuzhiyun "%s (pid %d) "
6634*4882a593Smuzhiyun "Use of int in maxseg socket option.\n"
6635*4882a593Smuzhiyun "Use struct sctp_assoc_value instead\n",
6636*4882a593Smuzhiyun current->comm, task_pid_nr(current));
6637*4882a593Smuzhiyun params.assoc_id = SCTP_FUTURE_ASSOC;
6638*4882a593Smuzhiyun } else if (len >= sizeof(struct sctp_assoc_value)) {
6639*4882a593Smuzhiyun len = sizeof(struct sctp_assoc_value);
6640*4882a593Smuzhiyun if (copy_from_user(¶ms, optval, len))
6641*4882a593Smuzhiyun return -EFAULT;
6642*4882a593Smuzhiyun } else
6643*4882a593Smuzhiyun return -EINVAL;
6644*4882a593Smuzhiyun
6645*4882a593Smuzhiyun asoc = sctp_id2assoc(sk, params.assoc_id);
6646*4882a593Smuzhiyun if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
6647*4882a593Smuzhiyun sctp_style(sk, UDP))
6648*4882a593Smuzhiyun return -EINVAL;
6649*4882a593Smuzhiyun
6650*4882a593Smuzhiyun if (asoc)
6651*4882a593Smuzhiyun params.assoc_value = asoc->frag_point;
6652*4882a593Smuzhiyun else
6653*4882a593Smuzhiyun params.assoc_value = sctp_sk(sk)->user_frag;
6654*4882a593Smuzhiyun
6655*4882a593Smuzhiyun if (put_user(len, optlen))
6656*4882a593Smuzhiyun return -EFAULT;
6657*4882a593Smuzhiyun if (len == sizeof(int)) {
6658*4882a593Smuzhiyun if (copy_to_user(optval, ¶ms.assoc_value, len))
6659*4882a593Smuzhiyun return -EFAULT;
6660*4882a593Smuzhiyun } else {
6661*4882a593Smuzhiyun if (copy_to_user(optval, ¶ms, len))
6662*4882a593Smuzhiyun return -EFAULT;
6663*4882a593Smuzhiyun }
6664*4882a593Smuzhiyun
6665*4882a593Smuzhiyun return 0;
6666*4882a593Smuzhiyun }
6667*4882a593Smuzhiyun
6668*4882a593Smuzhiyun /*
6669*4882a593Smuzhiyun * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE)
6670*4882a593Smuzhiyun * (chapter and verse is quoted at sctp_setsockopt_fragment_interleave())
6671*4882a593Smuzhiyun */
sctp_getsockopt_fragment_interleave(struct sock * sk,int len,char __user * optval,int __user * optlen)6672*4882a593Smuzhiyun static int sctp_getsockopt_fragment_interleave(struct sock *sk, int len,
6673*4882a593Smuzhiyun char __user *optval, int __user *optlen)
6674*4882a593Smuzhiyun {
6675*4882a593Smuzhiyun int val;
6676*4882a593Smuzhiyun
6677*4882a593Smuzhiyun if (len < sizeof(int))
6678*4882a593Smuzhiyun return -EINVAL;
6679*4882a593Smuzhiyun
6680*4882a593Smuzhiyun len = sizeof(int);
6681*4882a593Smuzhiyun
6682*4882a593Smuzhiyun val = sctp_sk(sk)->frag_interleave;
6683*4882a593Smuzhiyun if (put_user(len, optlen))
6684*4882a593Smuzhiyun return -EFAULT;
6685*4882a593Smuzhiyun if (copy_to_user(optval, &val, len))
6686*4882a593Smuzhiyun return -EFAULT;
6687*4882a593Smuzhiyun
6688*4882a593Smuzhiyun return 0;
6689*4882a593Smuzhiyun }
6690*4882a593Smuzhiyun
6691*4882a593Smuzhiyun /*
6692*4882a593Smuzhiyun * 7.1.25. Set or Get the sctp partial delivery point
6693*4882a593Smuzhiyun * (chapter and verse is quoted at sctp_setsockopt_partial_delivery_point())
6694*4882a593Smuzhiyun */
sctp_getsockopt_partial_delivery_point(struct sock * sk,int len,char __user * optval,int __user * optlen)6695*4882a593Smuzhiyun static int sctp_getsockopt_partial_delivery_point(struct sock *sk, int len,
6696*4882a593Smuzhiyun char __user *optval,
6697*4882a593Smuzhiyun int __user *optlen)
6698*4882a593Smuzhiyun {
6699*4882a593Smuzhiyun u32 val;
6700*4882a593Smuzhiyun
6701*4882a593Smuzhiyun if (len < sizeof(u32))
6702*4882a593Smuzhiyun return -EINVAL;
6703*4882a593Smuzhiyun
6704*4882a593Smuzhiyun len = sizeof(u32);
6705*4882a593Smuzhiyun
6706*4882a593Smuzhiyun val = sctp_sk(sk)->pd_point;
6707*4882a593Smuzhiyun if (put_user(len, optlen))
6708*4882a593Smuzhiyun return -EFAULT;
6709*4882a593Smuzhiyun if (copy_to_user(optval, &val, len))
6710*4882a593Smuzhiyun return -EFAULT;
6711*4882a593Smuzhiyun
6712*4882a593Smuzhiyun return 0;
6713*4882a593Smuzhiyun }
6714*4882a593Smuzhiyun
6715*4882a593Smuzhiyun /*
6716*4882a593Smuzhiyun * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST)
6717*4882a593Smuzhiyun * (chapter and verse is quoted at sctp_setsockopt_maxburst())
6718*4882a593Smuzhiyun */
sctp_getsockopt_maxburst(struct sock * sk,int len,char __user * optval,int __user * optlen)6719*4882a593Smuzhiyun static int sctp_getsockopt_maxburst(struct sock *sk, int len,
6720*4882a593Smuzhiyun char __user *optval,
6721*4882a593Smuzhiyun int __user *optlen)
6722*4882a593Smuzhiyun {
6723*4882a593Smuzhiyun struct sctp_assoc_value params;
6724*4882a593Smuzhiyun struct sctp_association *asoc;
6725*4882a593Smuzhiyun
6726*4882a593Smuzhiyun if (len == sizeof(int)) {
6727*4882a593Smuzhiyun pr_warn_ratelimited(DEPRECATED
6728*4882a593Smuzhiyun "%s (pid %d) "
6729*4882a593Smuzhiyun "Use of int in max_burst socket option.\n"
6730*4882a593Smuzhiyun "Use struct sctp_assoc_value instead\n",
6731*4882a593Smuzhiyun current->comm, task_pid_nr(current));
6732*4882a593Smuzhiyun params.assoc_id = SCTP_FUTURE_ASSOC;
6733*4882a593Smuzhiyun } else if (len >= sizeof(struct sctp_assoc_value)) {
6734*4882a593Smuzhiyun len = sizeof(struct sctp_assoc_value);
6735*4882a593Smuzhiyun if (copy_from_user(¶ms, optval, len))
6736*4882a593Smuzhiyun return -EFAULT;
6737*4882a593Smuzhiyun } else
6738*4882a593Smuzhiyun return -EINVAL;
6739*4882a593Smuzhiyun
6740*4882a593Smuzhiyun asoc = sctp_id2assoc(sk, params.assoc_id);
6741*4882a593Smuzhiyun if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
6742*4882a593Smuzhiyun sctp_style(sk, UDP))
6743*4882a593Smuzhiyun return -EINVAL;
6744*4882a593Smuzhiyun
6745*4882a593Smuzhiyun params.assoc_value = asoc ? asoc->max_burst : sctp_sk(sk)->max_burst;
6746*4882a593Smuzhiyun
6747*4882a593Smuzhiyun if (len == sizeof(int)) {
6748*4882a593Smuzhiyun if (copy_to_user(optval, ¶ms.assoc_value, len))
6749*4882a593Smuzhiyun return -EFAULT;
6750*4882a593Smuzhiyun } else {
6751*4882a593Smuzhiyun if (copy_to_user(optval, ¶ms, len))
6752*4882a593Smuzhiyun return -EFAULT;
6753*4882a593Smuzhiyun }
6754*4882a593Smuzhiyun
6755*4882a593Smuzhiyun return 0;
6756*4882a593Smuzhiyun
6757*4882a593Smuzhiyun }
6758*4882a593Smuzhiyun
sctp_getsockopt_hmac_ident(struct sock * sk,int len,char __user * optval,int __user * optlen)6759*4882a593Smuzhiyun static int sctp_getsockopt_hmac_ident(struct sock *sk, int len,
6760*4882a593Smuzhiyun char __user *optval, int __user *optlen)
6761*4882a593Smuzhiyun {
6762*4882a593Smuzhiyun struct sctp_endpoint *ep = sctp_sk(sk)->ep;
6763*4882a593Smuzhiyun struct sctp_hmacalgo __user *p = (void __user *)optval;
6764*4882a593Smuzhiyun struct sctp_hmac_algo_param *hmacs;
6765*4882a593Smuzhiyun __u16 data_len = 0;
6766*4882a593Smuzhiyun u32 num_idents;
6767*4882a593Smuzhiyun int i;
6768*4882a593Smuzhiyun
6769*4882a593Smuzhiyun if (!ep->auth_enable)
6770*4882a593Smuzhiyun return -EACCES;
6771*4882a593Smuzhiyun
6772*4882a593Smuzhiyun hmacs = ep->auth_hmacs_list;
6773*4882a593Smuzhiyun data_len = ntohs(hmacs->param_hdr.length) -
6774*4882a593Smuzhiyun sizeof(struct sctp_paramhdr);
6775*4882a593Smuzhiyun
6776*4882a593Smuzhiyun if (len < sizeof(struct sctp_hmacalgo) + data_len)
6777*4882a593Smuzhiyun return -EINVAL;
6778*4882a593Smuzhiyun
6779*4882a593Smuzhiyun len = sizeof(struct sctp_hmacalgo) + data_len;
6780*4882a593Smuzhiyun num_idents = data_len / sizeof(u16);
6781*4882a593Smuzhiyun
6782*4882a593Smuzhiyun if (put_user(len, optlen))
6783*4882a593Smuzhiyun return -EFAULT;
6784*4882a593Smuzhiyun if (put_user(num_idents, &p->shmac_num_idents))
6785*4882a593Smuzhiyun return -EFAULT;
6786*4882a593Smuzhiyun for (i = 0; i < num_idents; i++) {
6787*4882a593Smuzhiyun __u16 hmacid = ntohs(hmacs->hmac_ids[i]);
6788*4882a593Smuzhiyun
6789*4882a593Smuzhiyun if (copy_to_user(&p->shmac_idents[i], &hmacid, sizeof(__u16)))
6790*4882a593Smuzhiyun return -EFAULT;
6791*4882a593Smuzhiyun }
6792*4882a593Smuzhiyun return 0;
6793*4882a593Smuzhiyun }
6794*4882a593Smuzhiyun
sctp_getsockopt_active_key(struct sock * sk,int len,char __user * optval,int __user * optlen)6795*4882a593Smuzhiyun static int sctp_getsockopt_active_key(struct sock *sk, int len,
6796*4882a593Smuzhiyun char __user *optval, int __user *optlen)
6797*4882a593Smuzhiyun {
6798*4882a593Smuzhiyun struct sctp_endpoint *ep = sctp_sk(sk)->ep;
6799*4882a593Smuzhiyun struct sctp_authkeyid val;
6800*4882a593Smuzhiyun struct sctp_association *asoc;
6801*4882a593Smuzhiyun
6802*4882a593Smuzhiyun if (len < sizeof(struct sctp_authkeyid))
6803*4882a593Smuzhiyun return -EINVAL;
6804*4882a593Smuzhiyun
6805*4882a593Smuzhiyun len = sizeof(struct sctp_authkeyid);
6806*4882a593Smuzhiyun if (copy_from_user(&val, optval, len))
6807*4882a593Smuzhiyun return -EFAULT;
6808*4882a593Smuzhiyun
6809*4882a593Smuzhiyun asoc = sctp_id2assoc(sk, val.scact_assoc_id);
6810*4882a593Smuzhiyun if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP))
6811*4882a593Smuzhiyun return -EINVAL;
6812*4882a593Smuzhiyun
6813*4882a593Smuzhiyun if (asoc) {
6814*4882a593Smuzhiyun if (!asoc->peer.auth_capable)
6815*4882a593Smuzhiyun return -EACCES;
6816*4882a593Smuzhiyun val.scact_keynumber = asoc->active_key_id;
6817*4882a593Smuzhiyun } else {
6818*4882a593Smuzhiyun if (!ep->auth_enable)
6819*4882a593Smuzhiyun return -EACCES;
6820*4882a593Smuzhiyun val.scact_keynumber = ep->active_key_id;
6821*4882a593Smuzhiyun }
6822*4882a593Smuzhiyun
6823*4882a593Smuzhiyun if (put_user(len, optlen))
6824*4882a593Smuzhiyun return -EFAULT;
6825*4882a593Smuzhiyun if (copy_to_user(optval, &val, len))
6826*4882a593Smuzhiyun return -EFAULT;
6827*4882a593Smuzhiyun
6828*4882a593Smuzhiyun return 0;
6829*4882a593Smuzhiyun }
6830*4882a593Smuzhiyun
sctp_getsockopt_peer_auth_chunks(struct sock * sk,int len,char __user * optval,int __user * optlen)6831*4882a593Smuzhiyun static int sctp_getsockopt_peer_auth_chunks(struct sock *sk, int len,
6832*4882a593Smuzhiyun char __user *optval, int __user *optlen)
6833*4882a593Smuzhiyun {
6834*4882a593Smuzhiyun struct sctp_authchunks __user *p = (void __user *)optval;
6835*4882a593Smuzhiyun struct sctp_authchunks val;
6836*4882a593Smuzhiyun struct sctp_association *asoc;
6837*4882a593Smuzhiyun struct sctp_chunks_param *ch;
6838*4882a593Smuzhiyun u32 num_chunks = 0;
6839*4882a593Smuzhiyun char __user *to;
6840*4882a593Smuzhiyun
6841*4882a593Smuzhiyun if (len < sizeof(struct sctp_authchunks))
6842*4882a593Smuzhiyun return -EINVAL;
6843*4882a593Smuzhiyun
6844*4882a593Smuzhiyun if (copy_from_user(&val, optval, sizeof(val)))
6845*4882a593Smuzhiyun return -EFAULT;
6846*4882a593Smuzhiyun
6847*4882a593Smuzhiyun to = p->gauth_chunks;
6848*4882a593Smuzhiyun asoc = sctp_id2assoc(sk, val.gauth_assoc_id);
6849*4882a593Smuzhiyun if (!asoc)
6850*4882a593Smuzhiyun return -EINVAL;
6851*4882a593Smuzhiyun
6852*4882a593Smuzhiyun if (!asoc->peer.auth_capable)
6853*4882a593Smuzhiyun return -EACCES;
6854*4882a593Smuzhiyun
6855*4882a593Smuzhiyun ch = asoc->peer.peer_chunks;
6856*4882a593Smuzhiyun if (!ch)
6857*4882a593Smuzhiyun goto num;
6858*4882a593Smuzhiyun
6859*4882a593Smuzhiyun /* See if the user provided enough room for all the data */
6860*4882a593Smuzhiyun num_chunks = ntohs(ch->param_hdr.length) - sizeof(struct sctp_paramhdr);
6861*4882a593Smuzhiyun if (len < num_chunks)
6862*4882a593Smuzhiyun return -EINVAL;
6863*4882a593Smuzhiyun
6864*4882a593Smuzhiyun if (copy_to_user(to, ch->chunks, num_chunks))
6865*4882a593Smuzhiyun return -EFAULT;
6866*4882a593Smuzhiyun num:
6867*4882a593Smuzhiyun len = sizeof(struct sctp_authchunks) + num_chunks;
6868*4882a593Smuzhiyun if (put_user(len, optlen))
6869*4882a593Smuzhiyun return -EFAULT;
6870*4882a593Smuzhiyun if (put_user(num_chunks, &p->gauth_number_of_chunks))
6871*4882a593Smuzhiyun return -EFAULT;
6872*4882a593Smuzhiyun return 0;
6873*4882a593Smuzhiyun }
6874*4882a593Smuzhiyun
sctp_getsockopt_local_auth_chunks(struct sock * sk,int len,char __user * optval,int __user * optlen)6875*4882a593Smuzhiyun static int sctp_getsockopt_local_auth_chunks(struct sock *sk, int len,
6876*4882a593Smuzhiyun char __user *optval, int __user *optlen)
6877*4882a593Smuzhiyun {
6878*4882a593Smuzhiyun struct sctp_endpoint *ep = sctp_sk(sk)->ep;
6879*4882a593Smuzhiyun struct sctp_authchunks __user *p = (void __user *)optval;
6880*4882a593Smuzhiyun struct sctp_authchunks val;
6881*4882a593Smuzhiyun struct sctp_association *asoc;
6882*4882a593Smuzhiyun struct sctp_chunks_param *ch;
6883*4882a593Smuzhiyun u32 num_chunks = 0;
6884*4882a593Smuzhiyun char __user *to;
6885*4882a593Smuzhiyun
6886*4882a593Smuzhiyun if (len < sizeof(struct sctp_authchunks))
6887*4882a593Smuzhiyun return -EINVAL;
6888*4882a593Smuzhiyun
6889*4882a593Smuzhiyun if (copy_from_user(&val, optval, sizeof(val)))
6890*4882a593Smuzhiyun return -EFAULT;
6891*4882a593Smuzhiyun
6892*4882a593Smuzhiyun to = p->gauth_chunks;
6893*4882a593Smuzhiyun asoc = sctp_id2assoc(sk, val.gauth_assoc_id);
6894*4882a593Smuzhiyun if (!asoc && val.gauth_assoc_id != SCTP_FUTURE_ASSOC &&
6895*4882a593Smuzhiyun sctp_style(sk, UDP))
6896*4882a593Smuzhiyun return -EINVAL;
6897*4882a593Smuzhiyun
6898*4882a593Smuzhiyun if (asoc) {
6899*4882a593Smuzhiyun if (!asoc->peer.auth_capable)
6900*4882a593Smuzhiyun return -EACCES;
6901*4882a593Smuzhiyun ch = (struct sctp_chunks_param *)asoc->c.auth_chunks;
6902*4882a593Smuzhiyun } else {
6903*4882a593Smuzhiyun if (!ep->auth_enable)
6904*4882a593Smuzhiyun return -EACCES;
6905*4882a593Smuzhiyun ch = ep->auth_chunk_list;
6906*4882a593Smuzhiyun }
6907*4882a593Smuzhiyun if (!ch)
6908*4882a593Smuzhiyun goto num;
6909*4882a593Smuzhiyun
6910*4882a593Smuzhiyun num_chunks = ntohs(ch->param_hdr.length) - sizeof(struct sctp_paramhdr);
6911*4882a593Smuzhiyun if (len < sizeof(struct sctp_authchunks) + num_chunks)
6912*4882a593Smuzhiyun return -EINVAL;
6913*4882a593Smuzhiyun
6914*4882a593Smuzhiyun if (copy_to_user(to, ch->chunks, num_chunks))
6915*4882a593Smuzhiyun return -EFAULT;
6916*4882a593Smuzhiyun num:
6917*4882a593Smuzhiyun len = sizeof(struct sctp_authchunks) + num_chunks;
6918*4882a593Smuzhiyun if (put_user(len, optlen))
6919*4882a593Smuzhiyun return -EFAULT;
6920*4882a593Smuzhiyun if (put_user(num_chunks, &p->gauth_number_of_chunks))
6921*4882a593Smuzhiyun return -EFAULT;
6922*4882a593Smuzhiyun
6923*4882a593Smuzhiyun return 0;
6924*4882a593Smuzhiyun }
6925*4882a593Smuzhiyun
6926*4882a593Smuzhiyun /*
6927*4882a593Smuzhiyun * 8.2.5. Get the Current Number of Associations (SCTP_GET_ASSOC_NUMBER)
6928*4882a593Smuzhiyun * This option gets the current number of associations that are attached
6929*4882a593Smuzhiyun * to a one-to-many style socket. The option value is an uint32_t.
6930*4882a593Smuzhiyun */
sctp_getsockopt_assoc_number(struct sock * sk,int len,char __user * optval,int __user * optlen)6931*4882a593Smuzhiyun static int sctp_getsockopt_assoc_number(struct sock *sk, int len,
6932*4882a593Smuzhiyun char __user *optval, int __user *optlen)
6933*4882a593Smuzhiyun {
6934*4882a593Smuzhiyun struct sctp_sock *sp = sctp_sk(sk);
6935*4882a593Smuzhiyun struct sctp_association *asoc;
6936*4882a593Smuzhiyun u32 val = 0;
6937*4882a593Smuzhiyun
6938*4882a593Smuzhiyun if (sctp_style(sk, TCP))
6939*4882a593Smuzhiyun return -EOPNOTSUPP;
6940*4882a593Smuzhiyun
6941*4882a593Smuzhiyun if (len < sizeof(u32))
6942*4882a593Smuzhiyun return -EINVAL;
6943*4882a593Smuzhiyun
6944*4882a593Smuzhiyun len = sizeof(u32);
6945*4882a593Smuzhiyun
6946*4882a593Smuzhiyun list_for_each_entry(asoc, &(sp->ep->asocs), asocs) {
6947*4882a593Smuzhiyun val++;
6948*4882a593Smuzhiyun }
6949*4882a593Smuzhiyun
6950*4882a593Smuzhiyun if (put_user(len, optlen))
6951*4882a593Smuzhiyun return -EFAULT;
6952*4882a593Smuzhiyun if (copy_to_user(optval, &val, len))
6953*4882a593Smuzhiyun return -EFAULT;
6954*4882a593Smuzhiyun
6955*4882a593Smuzhiyun return 0;
6956*4882a593Smuzhiyun }
6957*4882a593Smuzhiyun
6958*4882a593Smuzhiyun /*
6959*4882a593Smuzhiyun * 8.1.23 SCTP_AUTO_ASCONF
6960*4882a593Smuzhiyun * See the corresponding setsockopt entry as description
6961*4882a593Smuzhiyun */
sctp_getsockopt_auto_asconf(struct sock * sk,int len,char __user * optval,int __user * optlen)6962*4882a593Smuzhiyun static int sctp_getsockopt_auto_asconf(struct sock *sk, int len,
6963*4882a593Smuzhiyun char __user *optval, int __user *optlen)
6964*4882a593Smuzhiyun {
6965*4882a593Smuzhiyun int val = 0;
6966*4882a593Smuzhiyun
6967*4882a593Smuzhiyun if (len < sizeof(int))
6968*4882a593Smuzhiyun return -EINVAL;
6969*4882a593Smuzhiyun
6970*4882a593Smuzhiyun len = sizeof(int);
6971*4882a593Smuzhiyun if (sctp_sk(sk)->do_auto_asconf && sctp_is_ep_boundall(sk))
6972*4882a593Smuzhiyun val = 1;
6973*4882a593Smuzhiyun if (put_user(len, optlen))
6974*4882a593Smuzhiyun return -EFAULT;
6975*4882a593Smuzhiyun if (copy_to_user(optval, &val, len))
6976*4882a593Smuzhiyun return -EFAULT;
6977*4882a593Smuzhiyun return 0;
6978*4882a593Smuzhiyun }
6979*4882a593Smuzhiyun
6980*4882a593Smuzhiyun /*
6981*4882a593Smuzhiyun * 8.2.6. Get the Current Identifiers of Associations
6982*4882a593Smuzhiyun * (SCTP_GET_ASSOC_ID_LIST)
6983*4882a593Smuzhiyun *
6984*4882a593Smuzhiyun * This option gets the current list of SCTP association identifiers of
6985*4882a593Smuzhiyun * the SCTP associations handled by a one-to-many style socket.
6986*4882a593Smuzhiyun */
sctp_getsockopt_assoc_ids(struct sock * sk,int len,char __user * optval,int __user * optlen)6987*4882a593Smuzhiyun static int sctp_getsockopt_assoc_ids(struct sock *sk, int len,
6988*4882a593Smuzhiyun char __user *optval, int __user *optlen)
6989*4882a593Smuzhiyun {
6990*4882a593Smuzhiyun struct sctp_sock *sp = sctp_sk(sk);
6991*4882a593Smuzhiyun struct sctp_association *asoc;
6992*4882a593Smuzhiyun struct sctp_assoc_ids *ids;
6993*4882a593Smuzhiyun u32 num = 0;
6994*4882a593Smuzhiyun
6995*4882a593Smuzhiyun if (sctp_style(sk, TCP))
6996*4882a593Smuzhiyun return -EOPNOTSUPP;
6997*4882a593Smuzhiyun
6998*4882a593Smuzhiyun if (len < sizeof(struct sctp_assoc_ids))
6999*4882a593Smuzhiyun return -EINVAL;
7000*4882a593Smuzhiyun
7001*4882a593Smuzhiyun list_for_each_entry(asoc, &(sp->ep->asocs), asocs) {
7002*4882a593Smuzhiyun num++;
7003*4882a593Smuzhiyun }
7004*4882a593Smuzhiyun
7005*4882a593Smuzhiyun if (len < sizeof(struct sctp_assoc_ids) + sizeof(sctp_assoc_t) * num)
7006*4882a593Smuzhiyun return -EINVAL;
7007*4882a593Smuzhiyun
7008*4882a593Smuzhiyun len = sizeof(struct sctp_assoc_ids) + sizeof(sctp_assoc_t) * num;
7009*4882a593Smuzhiyun
7010*4882a593Smuzhiyun ids = kmalloc(len, GFP_USER | __GFP_NOWARN);
7011*4882a593Smuzhiyun if (unlikely(!ids))
7012*4882a593Smuzhiyun return -ENOMEM;
7013*4882a593Smuzhiyun
7014*4882a593Smuzhiyun ids->gaids_number_of_ids = num;
7015*4882a593Smuzhiyun num = 0;
7016*4882a593Smuzhiyun list_for_each_entry(asoc, &(sp->ep->asocs), asocs) {
7017*4882a593Smuzhiyun ids->gaids_assoc_id[num++] = asoc->assoc_id;
7018*4882a593Smuzhiyun }
7019*4882a593Smuzhiyun
7020*4882a593Smuzhiyun if (put_user(len, optlen) || copy_to_user(optval, ids, len)) {
7021*4882a593Smuzhiyun kfree(ids);
7022*4882a593Smuzhiyun return -EFAULT;
7023*4882a593Smuzhiyun }
7024*4882a593Smuzhiyun
7025*4882a593Smuzhiyun kfree(ids);
7026*4882a593Smuzhiyun return 0;
7027*4882a593Smuzhiyun }
7028*4882a593Smuzhiyun
7029*4882a593Smuzhiyun /*
7030*4882a593Smuzhiyun * SCTP_PEER_ADDR_THLDS
7031*4882a593Smuzhiyun *
7032*4882a593Smuzhiyun * This option allows us to fetch the partially failed threshold for one or all
7033*4882a593Smuzhiyun * transports in an association. See Section 6.1 of:
7034*4882a593Smuzhiyun * http://www.ietf.org/id/draft-nishida-tsvwg-sctp-failover-05.txt
7035*4882a593Smuzhiyun */
sctp_getsockopt_paddr_thresholds(struct sock * sk,char __user * optval,int len,int __user * optlen,bool v2)7036*4882a593Smuzhiyun static int sctp_getsockopt_paddr_thresholds(struct sock *sk,
7037*4882a593Smuzhiyun char __user *optval, int len,
7038*4882a593Smuzhiyun int __user *optlen, bool v2)
7039*4882a593Smuzhiyun {
7040*4882a593Smuzhiyun struct sctp_paddrthlds_v2 val;
7041*4882a593Smuzhiyun struct sctp_transport *trans;
7042*4882a593Smuzhiyun struct sctp_association *asoc;
7043*4882a593Smuzhiyun int min;
7044*4882a593Smuzhiyun
7045*4882a593Smuzhiyun min = v2 ? sizeof(val) : sizeof(struct sctp_paddrthlds);
7046*4882a593Smuzhiyun if (len < min)
7047*4882a593Smuzhiyun return -EINVAL;
7048*4882a593Smuzhiyun len = min;
7049*4882a593Smuzhiyun if (copy_from_user(&val, optval, len))
7050*4882a593Smuzhiyun return -EFAULT;
7051*4882a593Smuzhiyun
7052*4882a593Smuzhiyun if (!sctp_is_any(sk, (const union sctp_addr *)&val.spt_address)) {
7053*4882a593Smuzhiyun trans = sctp_addr_id2transport(sk, &val.spt_address,
7054*4882a593Smuzhiyun val.spt_assoc_id);
7055*4882a593Smuzhiyun if (!trans)
7056*4882a593Smuzhiyun return -ENOENT;
7057*4882a593Smuzhiyun
7058*4882a593Smuzhiyun val.spt_pathmaxrxt = trans->pathmaxrxt;
7059*4882a593Smuzhiyun val.spt_pathpfthld = trans->pf_retrans;
7060*4882a593Smuzhiyun val.spt_pathcpthld = trans->ps_retrans;
7061*4882a593Smuzhiyun
7062*4882a593Smuzhiyun goto out;
7063*4882a593Smuzhiyun }
7064*4882a593Smuzhiyun
7065*4882a593Smuzhiyun asoc = sctp_id2assoc(sk, val.spt_assoc_id);
7066*4882a593Smuzhiyun if (!asoc && val.spt_assoc_id != SCTP_FUTURE_ASSOC &&
7067*4882a593Smuzhiyun sctp_style(sk, UDP))
7068*4882a593Smuzhiyun return -EINVAL;
7069*4882a593Smuzhiyun
7070*4882a593Smuzhiyun if (asoc) {
7071*4882a593Smuzhiyun val.spt_pathpfthld = asoc->pf_retrans;
7072*4882a593Smuzhiyun val.spt_pathmaxrxt = asoc->pathmaxrxt;
7073*4882a593Smuzhiyun val.spt_pathcpthld = asoc->ps_retrans;
7074*4882a593Smuzhiyun } else {
7075*4882a593Smuzhiyun struct sctp_sock *sp = sctp_sk(sk);
7076*4882a593Smuzhiyun
7077*4882a593Smuzhiyun val.spt_pathpfthld = sp->pf_retrans;
7078*4882a593Smuzhiyun val.spt_pathmaxrxt = sp->pathmaxrxt;
7079*4882a593Smuzhiyun val.spt_pathcpthld = sp->ps_retrans;
7080*4882a593Smuzhiyun }
7081*4882a593Smuzhiyun
7082*4882a593Smuzhiyun out:
7083*4882a593Smuzhiyun if (put_user(len, optlen) || copy_to_user(optval, &val, len))
7084*4882a593Smuzhiyun return -EFAULT;
7085*4882a593Smuzhiyun
7086*4882a593Smuzhiyun return 0;
7087*4882a593Smuzhiyun }
7088*4882a593Smuzhiyun
7089*4882a593Smuzhiyun /*
7090*4882a593Smuzhiyun * SCTP_GET_ASSOC_STATS
7091*4882a593Smuzhiyun *
7092*4882a593Smuzhiyun * This option retrieves local per endpoint statistics. It is modeled
7093*4882a593Smuzhiyun * after OpenSolaris' implementation
7094*4882a593Smuzhiyun */
sctp_getsockopt_assoc_stats(struct sock * sk,int len,char __user * optval,int __user * optlen)7095*4882a593Smuzhiyun static int sctp_getsockopt_assoc_stats(struct sock *sk, int len,
7096*4882a593Smuzhiyun char __user *optval,
7097*4882a593Smuzhiyun int __user *optlen)
7098*4882a593Smuzhiyun {
7099*4882a593Smuzhiyun struct sctp_assoc_stats sas;
7100*4882a593Smuzhiyun struct sctp_association *asoc = NULL;
7101*4882a593Smuzhiyun
7102*4882a593Smuzhiyun /* User must provide at least the assoc id */
7103*4882a593Smuzhiyun if (len < sizeof(sctp_assoc_t))
7104*4882a593Smuzhiyun return -EINVAL;
7105*4882a593Smuzhiyun
7106*4882a593Smuzhiyun /* Allow the struct to grow and fill in as much as possible */
7107*4882a593Smuzhiyun len = min_t(size_t, len, sizeof(sas));
7108*4882a593Smuzhiyun
7109*4882a593Smuzhiyun if (copy_from_user(&sas, optval, len))
7110*4882a593Smuzhiyun return -EFAULT;
7111*4882a593Smuzhiyun
7112*4882a593Smuzhiyun asoc = sctp_id2assoc(sk, sas.sas_assoc_id);
7113*4882a593Smuzhiyun if (!asoc)
7114*4882a593Smuzhiyun return -EINVAL;
7115*4882a593Smuzhiyun
7116*4882a593Smuzhiyun sas.sas_rtxchunks = asoc->stats.rtxchunks;
7117*4882a593Smuzhiyun sas.sas_gapcnt = asoc->stats.gapcnt;
7118*4882a593Smuzhiyun sas.sas_outofseqtsns = asoc->stats.outofseqtsns;
7119*4882a593Smuzhiyun sas.sas_osacks = asoc->stats.osacks;
7120*4882a593Smuzhiyun sas.sas_isacks = asoc->stats.isacks;
7121*4882a593Smuzhiyun sas.sas_octrlchunks = asoc->stats.octrlchunks;
7122*4882a593Smuzhiyun sas.sas_ictrlchunks = asoc->stats.ictrlchunks;
7123*4882a593Smuzhiyun sas.sas_oodchunks = asoc->stats.oodchunks;
7124*4882a593Smuzhiyun sas.sas_iodchunks = asoc->stats.iodchunks;
7125*4882a593Smuzhiyun sas.sas_ouodchunks = asoc->stats.ouodchunks;
7126*4882a593Smuzhiyun sas.sas_iuodchunks = asoc->stats.iuodchunks;
7127*4882a593Smuzhiyun sas.sas_idupchunks = asoc->stats.idupchunks;
7128*4882a593Smuzhiyun sas.sas_opackets = asoc->stats.opackets;
7129*4882a593Smuzhiyun sas.sas_ipackets = asoc->stats.ipackets;
7130*4882a593Smuzhiyun
7131*4882a593Smuzhiyun /* New high max rto observed, will return 0 if not a single
7132*4882a593Smuzhiyun * RTO update took place. obs_rto_ipaddr will be bogus
7133*4882a593Smuzhiyun * in such a case
7134*4882a593Smuzhiyun */
7135*4882a593Smuzhiyun sas.sas_maxrto = asoc->stats.max_obs_rto;
7136*4882a593Smuzhiyun memcpy(&sas.sas_obs_rto_ipaddr, &asoc->stats.obs_rto_ipaddr,
7137*4882a593Smuzhiyun sizeof(struct sockaddr_storage));
7138*4882a593Smuzhiyun
7139*4882a593Smuzhiyun /* Mark beginning of a new observation period */
7140*4882a593Smuzhiyun asoc->stats.max_obs_rto = asoc->rto_min;
7141*4882a593Smuzhiyun
7142*4882a593Smuzhiyun if (put_user(len, optlen))
7143*4882a593Smuzhiyun return -EFAULT;
7144*4882a593Smuzhiyun
7145*4882a593Smuzhiyun pr_debug("%s: len:%d, assoc_id:%d\n", __func__, len, sas.sas_assoc_id);
7146*4882a593Smuzhiyun
7147*4882a593Smuzhiyun if (copy_to_user(optval, &sas, len))
7148*4882a593Smuzhiyun return -EFAULT;
7149*4882a593Smuzhiyun
7150*4882a593Smuzhiyun return 0;
7151*4882a593Smuzhiyun }
7152*4882a593Smuzhiyun
sctp_getsockopt_recvrcvinfo(struct sock * sk,int len,char __user * optval,int __user * optlen)7153*4882a593Smuzhiyun static int sctp_getsockopt_recvrcvinfo(struct sock *sk, int len,
7154*4882a593Smuzhiyun char __user *optval,
7155*4882a593Smuzhiyun int __user *optlen)
7156*4882a593Smuzhiyun {
7157*4882a593Smuzhiyun int val = 0;
7158*4882a593Smuzhiyun
7159*4882a593Smuzhiyun if (len < sizeof(int))
7160*4882a593Smuzhiyun return -EINVAL;
7161*4882a593Smuzhiyun
7162*4882a593Smuzhiyun len = sizeof(int);
7163*4882a593Smuzhiyun if (sctp_sk(sk)->recvrcvinfo)
7164*4882a593Smuzhiyun val = 1;
7165*4882a593Smuzhiyun if (put_user(len, optlen))
7166*4882a593Smuzhiyun return -EFAULT;
7167*4882a593Smuzhiyun if (copy_to_user(optval, &val, len))
7168*4882a593Smuzhiyun return -EFAULT;
7169*4882a593Smuzhiyun
7170*4882a593Smuzhiyun return 0;
7171*4882a593Smuzhiyun }
7172*4882a593Smuzhiyun
sctp_getsockopt_recvnxtinfo(struct sock * sk,int len,char __user * optval,int __user * optlen)7173*4882a593Smuzhiyun static int sctp_getsockopt_recvnxtinfo(struct sock *sk, int len,
7174*4882a593Smuzhiyun char __user *optval,
7175*4882a593Smuzhiyun int __user *optlen)
7176*4882a593Smuzhiyun {
7177*4882a593Smuzhiyun int val = 0;
7178*4882a593Smuzhiyun
7179*4882a593Smuzhiyun if (len < sizeof(int))
7180*4882a593Smuzhiyun return -EINVAL;
7181*4882a593Smuzhiyun
7182*4882a593Smuzhiyun len = sizeof(int);
7183*4882a593Smuzhiyun if (sctp_sk(sk)->recvnxtinfo)
7184*4882a593Smuzhiyun val = 1;
7185*4882a593Smuzhiyun if (put_user(len, optlen))
7186*4882a593Smuzhiyun return -EFAULT;
7187*4882a593Smuzhiyun if (copy_to_user(optval, &val, len))
7188*4882a593Smuzhiyun return -EFAULT;
7189*4882a593Smuzhiyun
7190*4882a593Smuzhiyun return 0;
7191*4882a593Smuzhiyun }
7192*4882a593Smuzhiyun
sctp_getsockopt_pr_supported(struct sock * sk,int len,char __user * optval,int __user * optlen)7193*4882a593Smuzhiyun static int sctp_getsockopt_pr_supported(struct sock *sk, int len,
7194*4882a593Smuzhiyun char __user *optval,
7195*4882a593Smuzhiyun int __user *optlen)
7196*4882a593Smuzhiyun {
7197*4882a593Smuzhiyun struct sctp_assoc_value params;
7198*4882a593Smuzhiyun struct sctp_association *asoc;
7199*4882a593Smuzhiyun int retval = -EFAULT;
7200*4882a593Smuzhiyun
7201*4882a593Smuzhiyun if (len < sizeof(params)) {
7202*4882a593Smuzhiyun retval = -EINVAL;
7203*4882a593Smuzhiyun goto out;
7204*4882a593Smuzhiyun }
7205*4882a593Smuzhiyun
7206*4882a593Smuzhiyun len = sizeof(params);
7207*4882a593Smuzhiyun if (copy_from_user(¶ms, optval, len))
7208*4882a593Smuzhiyun goto out;
7209*4882a593Smuzhiyun
7210*4882a593Smuzhiyun asoc = sctp_id2assoc(sk, params.assoc_id);
7211*4882a593Smuzhiyun if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
7212*4882a593Smuzhiyun sctp_style(sk, UDP)) {
7213*4882a593Smuzhiyun retval = -EINVAL;
7214*4882a593Smuzhiyun goto out;
7215*4882a593Smuzhiyun }
7216*4882a593Smuzhiyun
7217*4882a593Smuzhiyun params.assoc_value = asoc ? asoc->peer.prsctp_capable
7218*4882a593Smuzhiyun : sctp_sk(sk)->ep->prsctp_enable;
7219*4882a593Smuzhiyun
7220*4882a593Smuzhiyun if (put_user(len, optlen))
7221*4882a593Smuzhiyun goto out;
7222*4882a593Smuzhiyun
7223*4882a593Smuzhiyun if (copy_to_user(optval, ¶ms, len))
7224*4882a593Smuzhiyun goto out;
7225*4882a593Smuzhiyun
7226*4882a593Smuzhiyun retval = 0;
7227*4882a593Smuzhiyun
7228*4882a593Smuzhiyun out:
7229*4882a593Smuzhiyun return retval;
7230*4882a593Smuzhiyun }
7231*4882a593Smuzhiyun
sctp_getsockopt_default_prinfo(struct sock * sk,int len,char __user * optval,int __user * optlen)7232*4882a593Smuzhiyun static int sctp_getsockopt_default_prinfo(struct sock *sk, int len,
7233*4882a593Smuzhiyun char __user *optval,
7234*4882a593Smuzhiyun int __user *optlen)
7235*4882a593Smuzhiyun {
7236*4882a593Smuzhiyun struct sctp_default_prinfo info;
7237*4882a593Smuzhiyun struct sctp_association *asoc;
7238*4882a593Smuzhiyun int retval = -EFAULT;
7239*4882a593Smuzhiyun
7240*4882a593Smuzhiyun if (len < sizeof(info)) {
7241*4882a593Smuzhiyun retval = -EINVAL;
7242*4882a593Smuzhiyun goto out;
7243*4882a593Smuzhiyun }
7244*4882a593Smuzhiyun
7245*4882a593Smuzhiyun len = sizeof(info);
7246*4882a593Smuzhiyun if (copy_from_user(&info, optval, len))
7247*4882a593Smuzhiyun goto out;
7248*4882a593Smuzhiyun
7249*4882a593Smuzhiyun asoc = sctp_id2assoc(sk, info.pr_assoc_id);
7250*4882a593Smuzhiyun if (!asoc && info.pr_assoc_id != SCTP_FUTURE_ASSOC &&
7251*4882a593Smuzhiyun sctp_style(sk, UDP)) {
7252*4882a593Smuzhiyun retval = -EINVAL;
7253*4882a593Smuzhiyun goto out;
7254*4882a593Smuzhiyun }
7255*4882a593Smuzhiyun
7256*4882a593Smuzhiyun if (asoc) {
7257*4882a593Smuzhiyun info.pr_policy = SCTP_PR_POLICY(asoc->default_flags);
7258*4882a593Smuzhiyun info.pr_value = asoc->default_timetolive;
7259*4882a593Smuzhiyun } else {
7260*4882a593Smuzhiyun struct sctp_sock *sp = sctp_sk(sk);
7261*4882a593Smuzhiyun
7262*4882a593Smuzhiyun info.pr_policy = SCTP_PR_POLICY(sp->default_flags);
7263*4882a593Smuzhiyun info.pr_value = sp->default_timetolive;
7264*4882a593Smuzhiyun }
7265*4882a593Smuzhiyun
7266*4882a593Smuzhiyun if (put_user(len, optlen))
7267*4882a593Smuzhiyun goto out;
7268*4882a593Smuzhiyun
7269*4882a593Smuzhiyun if (copy_to_user(optval, &info, len))
7270*4882a593Smuzhiyun goto out;
7271*4882a593Smuzhiyun
7272*4882a593Smuzhiyun retval = 0;
7273*4882a593Smuzhiyun
7274*4882a593Smuzhiyun out:
7275*4882a593Smuzhiyun return retval;
7276*4882a593Smuzhiyun }
7277*4882a593Smuzhiyun
sctp_getsockopt_pr_assocstatus(struct sock * sk,int len,char __user * optval,int __user * optlen)7278*4882a593Smuzhiyun static int sctp_getsockopt_pr_assocstatus(struct sock *sk, int len,
7279*4882a593Smuzhiyun char __user *optval,
7280*4882a593Smuzhiyun int __user *optlen)
7281*4882a593Smuzhiyun {
7282*4882a593Smuzhiyun struct sctp_prstatus params;
7283*4882a593Smuzhiyun struct sctp_association *asoc;
7284*4882a593Smuzhiyun int policy;
7285*4882a593Smuzhiyun int retval = -EINVAL;
7286*4882a593Smuzhiyun
7287*4882a593Smuzhiyun if (len < sizeof(params))
7288*4882a593Smuzhiyun goto out;
7289*4882a593Smuzhiyun
7290*4882a593Smuzhiyun len = sizeof(params);
7291*4882a593Smuzhiyun if (copy_from_user(¶ms, optval, len)) {
7292*4882a593Smuzhiyun retval = -EFAULT;
7293*4882a593Smuzhiyun goto out;
7294*4882a593Smuzhiyun }
7295*4882a593Smuzhiyun
7296*4882a593Smuzhiyun policy = params.sprstat_policy;
7297*4882a593Smuzhiyun if (!policy || (policy & ~(SCTP_PR_SCTP_MASK | SCTP_PR_SCTP_ALL)) ||
7298*4882a593Smuzhiyun ((policy & SCTP_PR_SCTP_ALL) && (policy & SCTP_PR_SCTP_MASK)))
7299*4882a593Smuzhiyun goto out;
7300*4882a593Smuzhiyun
7301*4882a593Smuzhiyun asoc = sctp_id2assoc(sk, params.sprstat_assoc_id);
7302*4882a593Smuzhiyun if (!asoc)
7303*4882a593Smuzhiyun goto out;
7304*4882a593Smuzhiyun
7305*4882a593Smuzhiyun if (policy == SCTP_PR_SCTP_ALL) {
7306*4882a593Smuzhiyun params.sprstat_abandoned_unsent = 0;
7307*4882a593Smuzhiyun params.sprstat_abandoned_sent = 0;
7308*4882a593Smuzhiyun for (policy = 0; policy <= SCTP_PR_INDEX(MAX); policy++) {
7309*4882a593Smuzhiyun params.sprstat_abandoned_unsent +=
7310*4882a593Smuzhiyun asoc->abandoned_unsent[policy];
7311*4882a593Smuzhiyun params.sprstat_abandoned_sent +=
7312*4882a593Smuzhiyun asoc->abandoned_sent[policy];
7313*4882a593Smuzhiyun }
7314*4882a593Smuzhiyun } else {
7315*4882a593Smuzhiyun params.sprstat_abandoned_unsent =
7316*4882a593Smuzhiyun asoc->abandoned_unsent[__SCTP_PR_INDEX(policy)];
7317*4882a593Smuzhiyun params.sprstat_abandoned_sent =
7318*4882a593Smuzhiyun asoc->abandoned_sent[__SCTP_PR_INDEX(policy)];
7319*4882a593Smuzhiyun }
7320*4882a593Smuzhiyun
7321*4882a593Smuzhiyun if (put_user(len, optlen)) {
7322*4882a593Smuzhiyun retval = -EFAULT;
7323*4882a593Smuzhiyun goto out;
7324*4882a593Smuzhiyun }
7325*4882a593Smuzhiyun
7326*4882a593Smuzhiyun if (copy_to_user(optval, ¶ms, len)) {
7327*4882a593Smuzhiyun retval = -EFAULT;
7328*4882a593Smuzhiyun goto out;
7329*4882a593Smuzhiyun }
7330*4882a593Smuzhiyun
7331*4882a593Smuzhiyun retval = 0;
7332*4882a593Smuzhiyun
7333*4882a593Smuzhiyun out:
7334*4882a593Smuzhiyun return retval;
7335*4882a593Smuzhiyun }
7336*4882a593Smuzhiyun
sctp_getsockopt_pr_streamstatus(struct sock * sk,int len,char __user * optval,int __user * optlen)7337*4882a593Smuzhiyun static int sctp_getsockopt_pr_streamstatus(struct sock *sk, int len,
7338*4882a593Smuzhiyun char __user *optval,
7339*4882a593Smuzhiyun int __user *optlen)
7340*4882a593Smuzhiyun {
7341*4882a593Smuzhiyun struct sctp_stream_out_ext *streamoute;
7342*4882a593Smuzhiyun struct sctp_association *asoc;
7343*4882a593Smuzhiyun struct sctp_prstatus params;
7344*4882a593Smuzhiyun int retval = -EINVAL;
7345*4882a593Smuzhiyun int policy;
7346*4882a593Smuzhiyun
7347*4882a593Smuzhiyun if (len < sizeof(params))
7348*4882a593Smuzhiyun goto out;
7349*4882a593Smuzhiyun
7350*4882a593Smuzhiyun len = sizeof(params);
7351*4882a593Smuzhiyun if (copy_from_user(¶ms, optval, len)) {
7352*4882a593Smuzhiyun retval = -EFAULT;
7353*4882a593Smuzhiyun goto out;
7354*4882a593Smuzhiyun }
7355*4882a593Smuzhiyun
7356*4882a593Smuzhiyun policy = params.sprstat_policy;
7357*4882a593Smuzhiyun if (!policy || (policy & ~(SCTP_PR_SCTP_MASK | SCTP_PR_SCTP_ALL)) ||
7358*4882a593Smuzhiyun ((policy & SCTP_PR_SCTP_ALL) && (policy & SCTP_PR_SCTP_MASK)))
7359*4882a593Smuzhiyun goto out;
7360*4882a593Smuzhiyun
7361*4882a593Smuzhiyun asoc = sctp_id2assoc(sk, params.sprstat_assoc_id);
7362*4882a593Smuzhiyun if (!asoc || params.sprstat_sid >= asoc->stream.outcnt)
7363*4882a593Smuzhiyun goto out;
7364*4882a593Smuzhiyun
7365*4882a593Smuzhiyun streamoute = SCTP_SO(&asoc->stream, params.sprstat_sid)->ext;
7366*4882a593Smuzhiyun if (!streamoute) {
7367*4882a593Smuzhiyun /* Not allocated yet, means all stats are 0 */
7368*4882a593Smuzhiyun params.sprstat_abandoned_unsent = 0;
7369*4882a593Smuzhiyun params.sprstat_abandoned_sent = 0;
7370*4882a593Smuzhiyun retval = 0;
7371*4882a593Smuzhiyun goto out;
7372*4882a593Smuzhiyun }
7373*4882a593Smuzhiyun
7374*4882a593Smuzhiyun if (policy == SCTP_PR_SCTP_ALL) {
7375*4882a593Smuzhiyun params.sprstat_abandoned_unsent = 0;
7376*4882a593Smuzhiyun params.sprstat_abandoned_sent = 0;
7377*4882a593Smuzhiyun for (policy = 0; policy <= SCTP_PR_INDEX(MAX); policy++) {
7378*4882a593Smuzhiyun params.sprstat_abandoned_unsent +=
7379*4882a593Smuzhiyun streamoute->abandoned_unsent[policy];
7380*4882a593Smuzhiyun params.sprstat_abandoned_sent +=
7381*4882a593Smuzhiyun streamoute->abandoned_sent[policy];
7382*4882a593Smuzhiyun }
7383*4882a593Smuzhiyun } else {
7384*4882a593Smuzhiyun params.sprstat_abandoned_unsent =
7385*4882a593Smuzhiyun streamoute->abandoned_unsent[__SCTP_PR_INDEX(policy)];
7386*4882a593Smuzhiyun params.sprstat_abandoned_sent =
7387*4882a593Smuzhiyun streamoute->abandoned_sent[__SCTP_PR_INDEX(policy)];
7388*4882a593Smuzhiyun }
7389*4882a593Smuzhiyun
7390*4882a593Smuzhiyun if (put_user(len, optlen) || copy_to_user(optval, ¶ms, len)) {
7391*4882a593Smuzhiyun retval = -EFAULT;
7392*4882a593Smuzhiyun goto out;
7393*4882a593Smuzhiyun }
7394*4882a593Smuzhiyun
7395*4882a593Smuzhiyun retval = 0;
7396*4882a593Smuzhiyun
7397*4882a593Smuzhiyun out:
7398*4882a593Smuzhiyun return retval;
7399*4882a593Smuzhiyun }
7400*4882a593Smuzhiyun
sctp_getsockopt_reconfig_supported(struct sock * sk,int len,char __user * optval,int __user * optlen)7401*4882a593Smuzhiyun static int sctp_getsockopt_reconfig_supported(struct sock *sk, int len,
7402*4882a593Smuzhiyun char __user *optval,
7403*4882a593Smuzhiyun int __user *optlen)
7404*4882a593Smuzhiyun {
7405*4882a593Smuzhiyun struct sctp_assoc_value params;
7406*4882a593Smuzhiyun struct sctp_association *asoc;
7407*4882a593Smuzhiyun int retval = -EFAULT;
7408*4882a593Smuzhiyun
7409*4882a593Smuzhiyun if (len < sizeof(params)) {
7410*4882a593Smuzhiyun retval = -EINVAL;
7411*4882a593Smuzhiyun goto out;
7412*4882a593Smuzhiyun }
7413*4882a593Smuzhiyun
7414*4882a593Smuzhiyun len = sizeof(params);
7415*4882a593Smuzhiyun if (copy_from_user(¶ms, optval, len))
7416*4882a593Smuzhiyun goto out;
7417*4882a593Smuzhiyun
7418*4882a593Smuzhiyun asoc = sctp_id2assoc(sk, params.assoc_id);
7419*4882a593Smuzhiyun if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
7420*4882a593Smuzhiyun sctp_style(sk, UDP)) {
7421*4882a593Smuzhiyun retval = -EINVAL;
7422*4882a593Smuzhiyun goto out;
7423*4882a593Smuzhiyun }
7424*4882a593Smuzhiyun
7425*4882a593Smuzhiyun params.assoc_value = asoc ? asoc->peer.reconf_capable
7426*4882a593Smuzhiyun : sctp_sk(sk)->ep->reconf_enable;
7427*4882a593Smuzhiyun
7428*4882a593Smuzhiyun if (put_user(len, optlen))
7429*4882a593Smuzhiyun goto out;
7430*4882a593Smuzhiyun
7431*4882a593Smuzhiyun if (copy_to_user(optval, ¶ms, len))
7432*4882a593Smuzhiyun goto out;
7433*4882a593Smuzhiyun
7434*4882a593Smuzhiyun retval = 0;
7435*4882a593Smuzhiyun
7436*4882a593Smuzhiyun out:
7437*4882a593Smuzhiyun return retval;
7438*4882a593Smuzhiyun }
7439*4882a593Smuzhiyun
sctp_getsockopt_enable_strreset(struct sock * sk,int len,char __user * optval,int __user * optlen)7440*4882a593Smuzhiyun static int sctp_getsockopt_enable_strreset(struct sock *sk, int len,
7441*4882a593Smuzhiyun char __user *optval,
7442*4882a593Smuzhiyun int __user *optlen)
7443*4882a593Smuzhiyun {
7444*4882a593Smuzhiyun struct sctp_assoc_value params;
7445*4882a593Smuzhiyun struct sctp_association *asoc;
7446*4882a593Smuzhiyun int retval = -EFAULT;
7447*4882a593Smuzhiyun
7448*4882a593Smuzhiyun if (len < sizeof(params)) {
7449*4882a593Smuzhiyun retval = -EINVAL;
7450*4882a593Smuzhiyun goto out;
7451*4882a593Smuzhiyun }
7452*4882a593Smuzhiyun
7453*4882a593Smuzhiyun len = sizeof(params);
7454*4882a593Smuzhiyun if (copy_from_user(¶ms, optval, len))
7455*4882a593Smuzhiyun goto out;
7456*4882a593Smuzhiyun
7457*4882a593Smuzhiyun asoc = sctp_id2assoc(sk, params.assoc_id);
7458*4882a593Smuzhiyun if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
7459*4882a593Smuzhiyun sctp_style(sk, UDP)) {
7460*4882a593Smuzhiyun retval = -EINVAL;
7461*4882a593Smuzhiyun goto out;
7462*4882a593Smuzhiyun }
7463*4882a593Smuzhiyun
7464*4882a593Smuzhiyun params.assoc_value = asoc ? asoc->strreset_enable
7465*4882a593Smuzhiyun : sctp_sk(sk)->ep->strreset_enable;
7466*4882a593Smuzhiyun
7467*4882a593Smuzhiyun if (put_user(len, optlen))
7468*4882a593Smuzhiyun goto out;
7469*4882a593Smuzhiyun
7470*4882a593Smuzhiyun if (copy_to_user(optval, ¶ms, len))
7471*4882a593Smuzhiyun goto out;
7472*4882a593Smuzhiyun
7473*4882a593Smuzhiyun retval = 0;
7474*4882a593Smuzhiyun
7475*4882a593Smuzhiyun out:
7476*4882a593Smuzhiyun return retval;
7477*4882a593Smuzhiyun }
7478*4882a593Smuzhiyun
sctp_getsockopt_scheduler(struct sock * sk,int len,char __user * optval,int __user * optlen)7479*4882a593Smuzhiyun static int sctp_getsockopt_scheduler(struct sock *sk, int len,
7480*4882a593Smuzhiyun char __user *optval,
7481*4882a593Smuzhiyun int __user *optlen)
7482*4882a593Smuzhiyun {
7483*4882a593Smuzhiyun struct sctp_assoc_value params;
7484*4882a593Smuzhiyun struct sctp_association *asoc;
7485*4882a593Smuzhiyun int retval = -EFAULT;
7486*4882a593Smuzhiyun
7487*4882a593Smuzhiyun if (len < sizeof(params)) {
7488*4882a593Smuzhiyun retval = -EINVAL;
7489*4882a593Smuzhiyun goto out;
7490*4882a593Smuzhiyun }
7491*4882a593Smuzhiyun
7492*4882a593Smuzhiyun len = sizeof(params);
7493*4882a593Smuzhiyun if (copy_from_user(¶ms, optval, len))
7494*4882a593Smuzhiyun goto out;
7495*4882a593Smuzhiyun
7496*4882a593Smuzhiyun asoc = sctp_id2assoc(sk, params.assoc_id);
7497*4882a593Smuzhiyun if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
7498*4882a593Smuzhiyun sctp_style(sk, UDP)) {
7499*4882a593Smuzhiyun retval = -EINVAL;
7500*4882a593Smuzhiyun goto out;
7501*4882a593Smuzhiyun }
7502*4882a593Smuzhiyun
7503*4882a593Smuzhiyun params.assoc_value = asoc ? sctp_sched_get_sched(asoc)
7504*4882a593Smuzhiyun : sctp_sk(sk)->default_ss;
7505*4882a593Smuzhiyun
7506*4882a593Smuzhiyun if (put_user(len, optlen))
7507*4882a593Smuzhiyun goto out;
7508*4882a593Smuzhiyun
7509*4882a593Smuzhiyun if (copy_to_user(optval, ¶ms, len))
7510*4882a593Smuzhiyun goto out;
7511*4882a593Smuzhiyun
7512*4882a593Smuzhiyun retval = 0;
7513*4882a593Smuzhiyun
7514*4882a593Smuzhiyun out:
7515*4882a593Smuzhiyun return retval;
7516*4882a593Smuzhiyun }
7517*4882a593Smuzhiyun
sctp_getsockopt_scheduler_value(struct sock * sk,int len,char __user * optval,int __user * optlen)7518*4882a593Smuzhiyun static int sctp_getsockopt_scheduler_value(struct sock *sk, int len,
7519*4882a593Smuzhiyun char __user *optval,
7520*4882a593Smuzhiyun int __user *optlen)
7521*4882a593Smuzhiyun {
7522*4882a593Smuzhiyun struct sctp_stream_value params;
7523*4882a593Smuzhiyun struct sctp_association *asoc;
7524*4882a593Smuzhiyun int retval = -EFAULT;
7525*4882a593Smuzhiyun
7526*4882a593Smuzhiyun if (len < sizeof(params)) {
7527*4882a593Smuzhiyun retval = -EINVAL;
7528*4882a593Smuzhiyun goto out;
7529*4882a593Smuzhiyun }
7530*4882a593Smuzhiyun
7531*4882a593Smuzhiyun len = sizeof(params);
7532*4882a593Smuzhiyun if (copy_from_user(¶ms, optval, len))
7533*4882a593Smuzhiyun goto out;
7534*4882a593Smuzhiyun
7535*4882a593Smuzhiyun asoc = sctp_id2assoc(sk, params.assoc_id);
7536*4882a593Smuzhiyun if (!asoc) {
7537*4882a593Smuzhiyun retval = -EINVAL;
7538*4882a593Smuzhiyun goto out;
7539*4882a593Smuzhiyun }
7540*4882a593Smuzhiyun
7541*4882a593Smuzhiyun retval = sctp_sched_get_value(asoc, params.stream_id,
7542*4882a593Smuzhiyun ¶ms.stream_value);
7543*4882a593Smuzhiyun if (retval)
7544*4882a593Smuzhiyun goto out;
7545*4882a593Smuzhiyun
7546*4882a593Smuzhiyun if (put_user(len, optlen)) {
7547*4882a593Smuzhiyun retval = -EFAULT;
7548*4882a593Smuzhiyun goto out;
7549*4882a593Smuzhiyun }
7550*4882a593Smuzhiyun
7551*4882a593Smuzhiyun if (copy_to_user(optval, ¶ms, len)) {
7552*4882a593Smuzhiyun retval = -EFAULT;
7553*4882a593Smuzhiyun goto out;
7554*4882a593Smuzhiyun }
7555*4882a593Smuzhiyun
7556*4882a593Smuzhiyun out:
7557*4882a593Smuzhiyun return retval;
7558*4882a593Smuzhiyun }
7559*4882a593Smuzhiyun
sctp_getsockopt_interleaving_supported(struct sock * sk,int len,char __user * optval,int __user * optlen)7560*4882a593Smuzhiyun static int sctp_getsockopt_interleaving_supported(struct sock *sk, int len,
7561*4882a593Smuzhiyun char __user *optval,
7562*4882a593Smuzhiyun int __user *optlen)
7563*4882a593Smuzhiyun {
7564*4882a593Smuzhiyun struct sctp_assoc_value params;
7565*4882a593Smuzhiyun struct sctp_association *asoc;
7566*4882a593Smuzhiyun int retval = -EFAULT;
7567*4882a593Smuzhiyun
7568*4882a593Smuzhiyun if (len < sizeof(params)) {
7569*4882a593Smuzhiyun retval = -EINVAL;
7570*4882a593Smuzhiyun goto out;
7571*4882a593Smuzhiyun }
7572*4882a593Smuzhiyun
7573*4882a593Smuzhiyun len = sizeof(params);
7574*4882a593Smuzhiyun if (copy_from_user(¶ms, optval, len))
7575*4882a593Smuzhiyun goto out;
7576*4882a593Smuzhiyun
7577*4882a593Smuzhiyun asoc = sctp_id2assoc(sk, params.assoc_id);
7578*4882a593Smuzhiyun if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
7579*4882a593Smuzhiyun sctp_style(sk, UDP)) {
7580*4882a593Smuzhiyun retval = -EINVAL;
7581*4882a593Smuzhiyun goto out;
7582*4882a593Smuzhiyun }
7583*4882a593Smuzhiyun
7584*4882a593Smuzhiyun params.assoc_value = asoc ? asoc->peer.intl_capable
7585*4882a593Smuzhiyun : sctp_sk(sk)->ep->intl_enable;
7586*4882a593Smuzhiyun
7587*4882a593Smuzhiyun if (put_user(len, optlen))
7588*4882a593Smuzhiyun goto out;
7589*4882a593Smuzhiyun
7590*4882a593Smuzhiyun if (copy_to_user(optval, ¶ms, len))
7591*4882a593Smuzhiyun goto out;
7592*4882a593Smuzhiyun
7593*4882a593Smuzhiyun retval = 0;
7594*4882a593Smuzhiyun
7595*4882a593Smuzhiyun out:
7596*4882a593Smuzhiyun return retval;
7597*4882a593Smuzhiyun }
7598*4882a593Smuzhiyun
sctp_getsockopt_reuse_port(struct sock * sk,int len,char __user * optval,int __user * optlen)7599*4882a593Smuzhiyun static int sctp_getsockopt_reuse_port(struct sock *sk, int len,
7600*4882a593Smuzhiyun char __user *optval,
7601*4882a593Smuzhiyun int __user *optlen)
7602*4882a593Smuzhiyun {
7603*4882a593Smuzhiyun int val;
7604*4882a593Smuzhiyun
7605*4882a593Smuzhiyun if (len < sizeof(int))
7606*4882a593Smuzhiyun return -EINVAL;
7607*4882a593Smuzhiyun
7608*4882a593Smuzhiyun len = sizeof(int);
7609*4882a593Smuzhiyun val = sctp_sk(sk)->reuse;
7610*4882a593Smuzhiyun if (put_user(len, optlen))
7611*4882a593Smuzhiyun return -EFAULT;
7612*4882a593Smuzhiyun
7613*4882a593Smuzhiyun if (copy_to_user(optval, &val, len))
7614*4882a593Smuzhiyun return -EFAULT;
7615*4882a593Smuzhiyun
7616*4882a593Smuzhiyun return 0;
7617*4882a593Smuzhiyun }
7618*4882a593Smuzhiyun
sctp_getsockopt_event(struct sock * sk,int len,char __user * optval,int __user * optlen)7619*4882a593Smuzhiyun static int sctp_getsockopt_event(struct sock *sk, int len, char __user *optval,
7620*4882a593Smuzhiyun int __user *optlen)
7621*4882a593Smuzhiyun {
7622*4882a593Smuzhiyun struct sctp_association *asoc;
7623*4882a593Smuzhiyun struct sctp_event param;
7624*4882a593Smuzhiyun __u16 subscribe;
7625*4882a593Smuzhiyun
7626*4882a593Smuzhiyun if (len < sizeof(param))
7627*4882a593Smuzhiyun return -EINVAL;
7628*4882a593Smuzhiyun
7629*4882a593Smuzhiyun len = sizeof(param);
7630*4882a593Smuzhiyun if (copy_from_user(¶m, optval, len))
7631*4882a593Smuzhiyun return -EFAULT;
7632*4882a593Smuzhiyun
7633*4882a593Smuzhiyun if (param.se_type < SCTP_SN_TYPE_BASE ||
7634*4882a593Smuzhiyun param.se_type > SCTP_SN_TYPE_MAX)
7635*4882a593Smuzhiyun return -EINVAL;
7636*4882a593Smuzhiyun
7637*4882a593Smuzhiyun asoc = sctp_id2assoc(sk, param.se_assoc_id);
7638*4882a593Smuzhiyun if (!asoc && param.se_assoc_id != SCTP_FUTURE_ASSOC &&
7639*4882a593Smuzhiyun sctp_style(sk, UDP))
7640*4882a593Smuzhiyun return -EINVAL;
7641*4882a593Smuzhiyun
7642*4882a593Smuzhiyun subscribe = asoc ? asoc->subscribe : sctp_sk(sk)->subscribe;
7643*4882a593Smuzhiyun param.se_on = sctp_ulpevent_type_enabled(subscribe, param.se_type);
7644*4882a593Smuzhiyun
7645*4882a593Smuzhiyun if (put_user(len, optlen))
7646*4882a593Smuzhiyun return -EFAULT;
7647*4882a593Smuzhiyun
7648*4882a593Smuzhiyun if (copy_to_user(optval, ¶m, len))
7649*4882a593Smuzhiyun return -EFAULT;
7650*4882a593Smuzhiyun
7651*4882a593Smuzhiyun return 0;
7652*4882a593Smuzhiyun }
7653*4882a593Smuzhiyun
sctp_getsockopt_asconf_supported(struct sock * sk,int len,char __user * optval,int __user * optlen)7654*4882a593Smuzhiyun static int sctp_getsockopt_asconf_supported(struct sock *sk, int len,
7655*4882a593Smuzhiyun char __user *optval,
7656*4882a593Smuzhiyun int __user *optlen)
7657*4882a593Smuzhiyun {
7658*4882a593Smuzhiyun struct sctp_assoc_value params;
7659*4882a593Smuzhiyun struct sctp_association *asoc;
7660*4882a593Smuzhiyun int retval = -EFAULT;
7661*4882a593Smuzhiyun
7662*4882a593Smuzhiyun if (len < sizeof(params)) {
7663*4882a593Smuzhiyun retval = -EINVAL;
7664*4882a593Smuzhiyun goto out;
7665*4882a593Smuzhiyun }
7666*4882a593Smuzhiyun
7667*4882a593Smuzhiyun len = sizeof(params);
7668*4882a593Smuzhiyun if (copy_from_user(¶ms, optval, len))
7669*4882a593Smuzhiyun goto out;
7670*4882a593Smuzhiyun
7671*4882a593Smuzhiyun asoc = sctp_id2assoc(sk, params.assoc_id);
7672*4882a593Smuzhiyun if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
7673*4882a593Smuzhiyun sctp_style(sk, UDP)) {
7674*4882a593Smuzhiyun retval = -EINVAL;
7675*4882a593Smuzhiyun goto out;
7676*4882a593Smuzhiyun }
7677*4882a593Smuzhiyun
7678*4882a593Smuzhiyun params.assoc_value = asoc ? asoc->peer.asconf_capable
7679*4882a593Smuzhiyun : sctp_sk(sk)->ep->asconf_enable;
7680*4882a593Smuzhiyun
7681*4882a593Smuzhiyun if (put_user(len, optlen))
7682*4882a593Smuzhiyun goto out;
7683*4882a593Smuzhiyun
7684*4882a593Smuzhiyun if (copy_to_user(optval, ¶ms, len))
7685*4882a593Smuzhiyun goto out;
7686*4882a593Smuzhiyun
7687*4882a593Smuzhiyun retval = 0;
7688*4882a593Smuzhiyun
7689*4882a593Smuzhiyun out:
7690*4882a593Smuzhiyun return retval;
7691*4882a593Smuzhiyun }
7692*4882a593Smuzhiyun
sctp_getsockopt_auth_supported(struct sock * sk,int len,char __user * optval,int __user * optlen)7693*4882a593Smuzhiyun static int sctp_getsockopt_auth_supported(struct sock *sk, int len,
7694*4882a593Smuzhiyun char __user *optval,
7695*4882a593Smuzhiyun int __user *optlen)
7696*4882a593Smuzhiyun {
7697*4882a593Smuzhiyun struct sctp_assoc_value params;
7698*4882a593Smuzhiyun struct sctp_association *asoc;
7699*4882a593Smuzhiyun int retval = -EFAULT;
7700*4882a593Smuzhiyun
7701*4882a593Smuzhiyun if (len < sizeof(params)) {
7702*4882a593Smuzhiyun retval = -EINVAL;
7703*4882a593Smuzhiyun goto out;
7704*4882a593Smuzhiyun }
7705*4882a593Smuzhiyun
7706*4882a593Smuzhiyun len = sizeof(params);
7707*4882a593Smuzhiyun if (copy_from_user(¶ms, optval, len))
7708*4882a593Smuzhiyun goto out;
7709*4882a593Smuzhiyun
7710*4882a593Smuzhiyun asoc = sctp_id2assoc(sk, params.assoc_id);
7711*4882a593Smuzhiyun if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
7712*4882a593Smuzhiyun sctp_style(sk, UDP)) {
7713*4882a593Smuzhiyun retval = -EINVAL;
7714*4882a593Smuzhiyun goto out;
7715*4882a593Smuzhiyun }
7716*4882a593Smuzhiyun
7717*4882a593Smuzhiyun params.assoc_value = asoc ? asoc->peer.auth_capable
7718*4882a593Smuzhiyun : sctp_sk(sk)->ep->auth_enable;
7719*4882a593Smuzhiyun
7720*4882a593Smuzhiyun if (put_user(len, optlen))
7721*4882a593Smuzhiyun goto out;
7722*4882a593Smuzhiyun
7723*4882a593Smuzhiyun if (copy_to_user(optval, ¶ms, len))
7724*4882a593Smuzhiyun goto out;
7725*4882a593Smuzhiyun
7726*4882a593Smuzhiyun retval = 0;
7727*4882a593Smuzhiyun
7728*4882a593Smuzhiyun out:
7729*4882a593Smuzhiyun return retval;
7730*4882a593Smuzhiyun }
7731*4882a593Smuzhiyun
sctp_getsockopt_ecn_supported(struct sock * sk,int len,char __user * optval,int __user * optlen)7732*4882a593Smuzhiyun static int sctp_getsockopt_ecn_supported(struct sock *sk, int len,
7733*4882a593Smuzhiyun char __user *optval,
7734*4882a593Smuzhiyun int __user *optlen)
7735*4882a593Smuzhiyun {
7736*4882a593Smuzhiyun struct sctp_assoc_value params;
7737*4882a593Smuzhiyun struct sctp_association *asoc;
7738*4882a593Smuzhiyun int retval = -EFAULT;
7739*4882a593Smuzhiyun
7740*4882a593Smuzhiyun if (len < sizeof(params)) {
7741*4882a593Smuzhiyun retval = -EINVAL;
7742*4882a593Smuzhiyun goto out;
7743*4882a593Smuzhiyun }
7744*4882a593Smuzhiyun
7745*4882a593Smuzhiyun len = sizeof(params);
7746*4882a593Smuzhiyun if (copy_from_user(¶ms, optval, len))
7747*4882a593Smuzhiyun goto out;
7748*4882a593Smuzhiyun
7749*4882a593Smuzhiyun asoc = sctp_id2assoc(sk, params.assoc_id);
7750*4882a593Smuzhiyun if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
7751*4882a593Smuzhiyun sctp_style(sk, UDP)) {
7752*4882a593Smuzhiyun retval = -EINVAL;
7753*4882a593Smuzhiyun goto out;
7754*4882a593Smuzhiyun }
7755*4882a593Smuzhiyun
7756*4882a593Smuzhiyun params.assoc_value = asoc ? asoc->peer.ecn_capable
7757*4882a593Smuzhiyun : sctp_sk(sk)->ep->ecn_enable;
7758*4882a593Smuzhiyun
7759*4882a593Smuzhiyun if (put_user(len, optlen))
7760*4882a593Smuzhiyun goto out;
7761*4882a593Smuzhiyun
7762*4882a593Smuzhiyun if (copy_to_user(optval, ¶ms, len))
7763*4882a593Smuzhiyun goto out;
7764*4882a593Smuzhiyun
7765*4882a593Smuzhiyun retval = 0;
7766*4882a593Smuzhiyun
7767*4882a593Smuzhiyun out:
7768*4882a593Smuzhiyun return retval;
7769*4882a593Smuzhiyun }
7770*4882a593Smuzhiyun
sctp_getsockopt_pf_expose(struct sock * sk,int len,char __user * optval,int __user * optlen)7771*4882a593Smuzhiyun static int sctp_getsockopt_pf_expose(struct sock *sk, int len,
7772*4882a593Smuzhiyun char __user *optval,
7773*4882a593Smuzhiyun int __user *optlen)
7774*4882a593Smuzhiyun {
7775*4882a593Smuzhiyun struct sctp_assoc_value params;
7776*4882a593Smuzhiyun struct sctp_association *asoc;
7777*4882a593Smuzhiyun int retval = -EFAULT;
7778*4882a593Smuzhiyun
7779*4882a593Smuzhiyun if (len < sizeof(params)) {
7780*4882a593Smuzhiyun retval = -EINVAL;
7781*4882a593Smuzhiyun goto out;
7782*4882a593Smuzhiyun }
7783*4882a593Smuzhiyun
7784*4882a593Smuzhiyun len = sizeof(params);
7785*4882a593Smuzhiyun if (copy_from_user(¶ms, optval, len))
7786*4882a593Smuzhiyun goto out;
7787*4882a593Smuzhiyun
7788*4882a593Smuzhiyun asoc = sctp_id2assoc(sk, params.assoc_id);
7789*4882a593Smuzhiyun if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
7790*4882a593Smuzhiyun sctp_style(sk, UDP)) {
7791*4882a593Smuzhiyun retval = -EINVAL;
7792*4882a593Smuzhiyun goto out;
7793*4882a593Smuzhiyun }
7794*4882a593Smuzhiyun
7795*4882a593Smuzhiyun params.assoc_value = asoc ? asoc->pf_expose
7796*4882a593Smuzhiyun : sctp_sk(sk)->pf_expose;
7797*4882a593Smuzhiyun
7798*4882a593Smuzhiyun if (put_user(len, optlen))
7799*4882a593Smuzhiyun goto out;
7800*4882a593Smuzhiyun
7801*4882a593Smuzhiyun if (copy_to_user(optval, ¶ms, len))
7802*4882a593Smuzhiyun goto out;
7803*4882a593Smuzhiyun
7804*4882a593Smuzhiyun retval = 0;
7805*4882a593Smuzhiyun
7806*4882a593Smuzhiyun out:
7807*4882a593Smuzhiyun return retval;
7808*4882a593Smuzhiyun }
7809*4882a593Smuzhiyun
sctp_getsockopt(struct sock * sk,int level,int optname,char __user * optval,int __user * optlen)7810*4882a593Smuzhiyun static int sctp_getsockopt(struct sock *sk, int level, int optname,
7811*4882a593Smuzhiyun char __user *optval, int __user *optlen)
7812*4882a593Smuzhiyun {
7813*4882a593Smuzhiyun int retval = 0;
7814*4882a593Smuzhiyun int len;
7815*4882a593Smuzhiyun
7816*4882a593Smuzhiyun pr_debug("%s: sk:%p, optname:%d\n", __func__, sk, optname);
7817*4882a593Smuzhiyun
7818*4882a593Smuzhiyun /* I can hardly begin to describe how wrong this is. This is
7819*4882a593Smuzhiyun * so broken as to be worse than useless. The API draft
7820*4882a593Smuzhiyun * REALLY is NOT helpful here... I am not convinced that the
7821*4882a593Smuzhiyun * semantics of getsockopt() with a level OTHER THAN SOL_SCTP
7822*4882a593Smuzhiyun * are at all well-founded.
7823*4882a593Smuzhiyun */
7824*4882a593Smuzhiyun if (level != SOL_SCTP) {
7825*4882a593Smuzhiyun struct sctp_af *af = sctp_sk(sk)->pf->af;
7826*4882a593Smuzhiyun
7827*4882a593Smuzhiyun retval = af->getsockopt(sk, level, optname, optval, optlen);
7828*4882a593Smuzhiyun return retval;
7829*4882a593Smuzhiyun }
7830*4882a593Smuzhiyun
7831*4882a593Smuzhiyun if (get_user(len, optlen))
7832*4882a593Smuzhiyun return -EFAULT;
7833*4882a593Smuzhiyun
7834*4882a593Smuzhiyun if (len < 0)
7835*4882a593Smuzhiyun return -EINVAL;
7836*4882a593Smuzhiyun
7837*4882a593Smuzhiyun lock_sock(sk);
7838*4882a593Smuzhiyun
7839*4882a593Smuzhiyun switch (optname) {
7840*4882a593Smuzhiyun case SCTP_STATUS:
7841*4882a593Smuzhiyun retval = sctp_getsockopt_sctp_status(sk, len, optval, optlen);
7842*4882a593Smuzhiyun break;
7843*4882a593Smuzhiyun case SCTP_DISABLE_FRAGMENTS:
7844*4882a593Smuzhiyun retval = sctp_getsockopt_disable_fragments(sk, len, optval,
7845*4882a593Smuzhiyun optlen);
7846*4882a593Smuzhiyun break;
7847*4882a593Smuzhiyun case SCTP_EVENTS:
7848*4882a593Smuzhiyun retval = sctp_getsockopt_events(sk, len, optval, optlen);
7849*4882a593Smuzhiyun break;
7850*4882a593Smuzhiyun case SCTP_AUTOCLOSE:
7851*4882a593Smuzhiyun retval = sctp_getsockopt_autoclose(sk, len, optval, optlen);
7852*4882a593Smuzhiyun break;
7853*4882a593Smuzhiyun case SCTP_SOCKOPT_PEELOFF:
7854*4882a593Smuzhiyun retval = sctp_getsockopt_peeloff(sk, len, optval, optlen);
7855*4882a593Smuzhiyun break;
7856*4882a593Smuzhiyun case SCTP_SOCKOPT_PEELOFF_FLAGS:
7857*4882a593Smuzhiyun retval = sctp_getsockopt_peeloff_flags(sk, len, optval, optlen);
7858*4882a593Smuzhiyun break;
7859*4882a593Smuzhiyun case SCTP_PEER_ADDR_PARAMS:
7860*4882a593Smuzhiyun retval = sctp_getsockopt_peer_addr_params(sk, len, optval,
7861*4882a593Smuzhiyun optlen);
7862*4882a593Smuzhiyun break;
7863*4882a593Smuzhiyun case SCTP_DELAYED_SACK:
7864*4882a593Smuzhiyun retval = sctp_getsockopt_delayed_ack(sk, len, optval,
7865*4882a593Smuzhiyun optlen);
7866*4882a593Smuzhiyun break;
7867*4882a593Smuzhiyun case SCTP_INITMSG:
7868*4882a593Smuzhiyun retval = sctp_getsockopt_initmsg(sk, len, optval, optlen);
7869*4882a593Smuzhiyun break;
7870*4882a593Smuzhiyun case SCTP_GET_PEER_ADDRS:
7871*4882a593Smuzhiyun retval = sctp_getsockopt_peer_addrs(sk, len, optval,
7872*4882a593Smuzhiyun optlen);
7873*4882a593Smuzhiyun break;
7874*4882a593Smuzhiyun case SCTP_GET_LOCAL_ADDRS:
7875*4882a593Smuzhiyun retval = sctp_getsockopt_local_addrs(sk, len, optval,
7876*4882a593Smuzhiyun optlen);
7877*4882a593Smuzhiyun break;
7878*4882a593Smuzhiyun case SCTP_SOCKOPT_CONNECTX3:
7879*4882a593Smuzhiyun retval = sctp_getsockopt_connectx3(sk, len, optval, optlen);
7880*4882a593Smuzhiyun break;
7881*4882a593Smuzhiyun case SCTP_DEFAULT_SEND_PARAM:
7882*4882a593Smuzhiyun retval = sctp_getsockopt_default_send_param(sk, len,
7883*4882a593Smuzhiyun optval, optlen);
7884*4882a593Smuzhiyun break;
7885*4882a593Smuzhiyun case SCTP_DEFAULT_SNDINFO:
7886*4882a593Smuzhiyun retval = sctp_getsockopt_default_sndinfo(sk, len,
7887*4882a593Smuzhiyun optval, optlen);
7888*4882a593Smuzhiyun break;
7889*4882a593Smuzhiyun case SCTP_PRIMARY_ADDR:
7890*4882a593Smuzhiyun retval = sctp_getsockopt_primary_addr(sk, len, optval, optlen);
7891*4882a593Smuzhiyun break;
7892*4882a593Smuzhiyun case SCTP_NODELAY:
7893*4882a593Smuzhiyun retval = sctp_getsockopt_nodelay(sk, len, optval, optlen);
7894*4882a593Smuzhiyun break;
7895*4882a593Smuzhiyun case SCTP_RTOINFO:
7896*4882a593Smuzhiyun retval = sctp_getsockopt_rtoinfo(sk, len, optval, optlen);
7897*4882a593Smuzhiyun break;
7898*4882a593Smuzhiyun case SCTP_ASSOCINFO:
7899*4882a593Smuzhiyun retval = sctp_getsockopt_associnfo(sk, len, optval, optlen);
7900*4882a593Smuzhiyun break;
7901*4882a593Smuzhiyun case SCTP_I_WANT_MAPPED_V4_ADDR:
7902*4882a593Smuzhiyun retval = sctp_getsockopt_mappedv4(sk, len, optval, optlen);
7903*4882a593Smuzhiyun break;
7904*4882a593Smuzhiyun case SCTP_MAXSEG:
7905*4882a593Smuzhiyun retval = sctp_getsockopt_maxseg(sk, len, optval, optlen);
7906*4882a593Smuzhiyun break;
7907*4882a593Smuzhiyun case SCTP_GET_PEER_ADDR_INFO:
7908*4882a593Smuzhiyun retval = sctp_getsockopt_peer_addr_info(sk, len, optval,
7909*4882a593Smuzhiyun optlen);
7910*4882a593Smuzhiyun break;
7911*4882a593Smuzhiyun case SCTP_ADAPTATION_LAYER:
7912*4882a593Smuzhiyun retval = sctp_getsockopt_adaptation_layer(sk, len, optval,
7913*4882a593Smuzhiyun optlen);
7914*4882a593Smuzhiyun break;
7915*4882a593Smuzhiyun case SCTP_CONTEXT:
7916*4882a593Smuzhiyun retval = sctp_getsockopt_context(sk, len, optval, optlen);
7917*4882a593Smuzhiyun break;
7918*4882a593Smuzhiyun case SCTP_FRAGMENT_INTERLEAVE:
7919*4882a593Smuzhiyun retval = sctp_getsockopt_fragment_interleave(sk, len, optval,
7920*4882a593Smuzhiyun optlen);
7921*4882a593Smuzhiyun break;
7922*4882a593Smuzhiyun case SCTP_PARTIAL_DELIVERY_POINT:
7923*4882a593Smuzhiyun retval = sctp_getsockopt_partial_delivery_point(sk, len, optval,
7924*4882a593Smuzhiyun optlen);
7925*4882a593Smuzhiyun break;
7926*4882a593Smuzhiyun case SCTP_MAX_BURST:
7927*4882a593Smuzhiyun retval = sctp_getsockopt_maxburst(sk, len, optval, optlen);
7928*4882a593Smuzhiyun break;
7929*4882a593Smuzhiyun case SCTP_AUTH_KEY:
7930*4882a593Smuzhiyun case SCTP_AUTH_CHUNK:
7931*4882a593Smuzhiyun case SCTP_AUTH_DELETE_KEY:
7932*4882a593Smuzhiyun case SCTP_AUTH_DEACTIVATE_KEY:
7933*4882a593Smuzhiyun retval = -EOPNOTSUPP;
7934*4882a593Smuzhiyun break;
7935*4882a593Smuzhiyun case SCTP_HMAC_IDENT:
7936*4882a593Smuzhiyun retval = sctp_getsockopt_hmac_ident(sk, len, optval, optlen);
7937*4882a593Smuzhiyun break;
7938*4882a593Smuzhiyun case SCTP_AUTH_ACTIVE_KEY:
7939*4882a593Smuzhiyun retval = sctp_getsockopt_active_key(sk, len, optval, optlen);
7940*4882a593Smuzhiyun break;
7941*4882a593Smuzhiyun case SCTP_PEER_AUTH_CHUNKS:
7942*4882a593Smuzhiyun retval = sctp_getsockopt_peer_auth_chunks(sk, len, optval,
7943*4882a593Smuzhiyun optlen);
7944*4882a593Smuzhiyun break;
7945*4882a593Smuzhiyun case SCTP_LOCAL_AUTH_CHUNKS:
7946*4882a593Smuzhiyun retval = sctp_getsockopt_local_auth_chunks(sk, len, optval,
7947*4882a593Smuzhiyun optlen);
7948*4882a593Smuzhiyun break;
7949*4882a593Smuzhiyun case SCTP_GET_ASSOC_NUMBER:
7950*4882a593Smuzhiyun retval = sctp_getsockopt_assoc_number(sk, len, optval, optlen);
7951*4882a593Smuzhiyun break;
7952*4882a593Smuzhiyun case SCTP_GET_ASSOC_ID_LIST:
7953*4882a593Smuzhiyun retval = sctp_getsockopt_assoc_ids(sk, len, optval, optlen);
7954*4882a593Smuzhiyun break;
7955*4882a593Smuzhiyun case SCTP_AUTO_ASCONF:
7956*4882a593Smuzhiyun retval = sctp_getsockopt_auto_asconf(sk, len, optval, optlen);
7957*4882a593Smuzhiyun break;
7958*4882a593Smuzhiyun case SCTP_PEER_ADDR_THLDS:
7959*4882a593Smuzhiyun retval = sctp_getsockopt_paddr_thresholds(sk, optval, len,
7960*4882a593Smuzhiyun optlen, false);
7961*4882a593Smuzhiyun break;
7962*4882a593Smuzhiyun case SCTP_PEER_ADDR_THLDS_V2:
7963*4882a593Smuzhiyun retval = sctp_getsockopt_paddr_thresholds(sk, optval, len,
7964*4882a593Smuzhiyun optlen, true);
7965*4882a593Smuzhiyun break;
7966*4882a593Smuzhiyun case SCTP_GET_ASSOC_STATS:
7967*4882a593Smuzhiyun retval = sctp_getsockopt_assoc_stats(sk, len, optval, optlen);
7968*4882a593Smuzhiyun break;
7969*4882a593Smuzhiyun case SCTP_RECVRCVINFO:
7970*4882a593Smuzhiyun retval = sctp_getsockopt_recvrcvinfo(sk, len, optval, optlen);
7971*4882a593Smuzhiyun break;
7972*4882a593Smuzhiyun case SCTP_RECVNXTINFO:
7973*4882a593Smuzhiyun retval = sctp_getsockopt_recvnxtinfo(sk, len, optval, optlen);
7974*4882a593Smuzhiyun break;
7975*4882a593Smuzhiyun case SCTP_PR_SUPPORTED:
7976*4882a593Smuzhiyun retval = sctp_getsockopt_pr_supported(sk, len, optval, optlen);
7977*4882a593Smuzhiyun break;
7978*4882a593Smuzhiyun case SCTP_DEFAULT_PRINFO:
7979*4882a593Smuzhiyun retval = sctp_getsockopt_default_prinfo(sk, len, optval,
7980*4882a593Smuzhiyun optlen);
7981*4882a593Smuzhiyun break;
7982*4882a593Smuzhiyun case SCTP_PR_ASSOC_STATUS:
7983*4882a593Smuzhiyun retval = sctp_getsockopt_pr_assocstatus(sk, len, optval,
7984*4882a593Smuzhiyun optlen);
7985*4882a593Smuzhiyun break;
7986*4882a593Smuzhiyun case SCTP_PR_STREAM_STATUS:
7987*4882a593Smuzhiyun retval = sctp_getsockopt_pr_streamstatus(sk, len, optval,
7988*4882a593Smuzhiyun optlen);
7989*4882a593Smuzhiyun break;
7990*4882a593Smuzhiyun case SCTP_RECONFIG_SUPPORTED:
7991*4882a593Smuzhiyun retval = sctp_getsockopt_reconfig_supported(sk, len, optval,
7992*4882a593Smuzhiyun optlen);
7993*4882a593Smuzhiyun break;
7994*4882a593Smuzhiyun case SCTP_ENABLE_STREAM_RESET:
7995*4882a593Smuzhiyun retval = sctp_getsockopt_enable_strreset(sk, len, optval,
7996*4882a593Smuzhiyun optlen);
7997*4882a593Smuzhiyun break;
7998*4882a593Smuzhiyun case SCTP_STREAM_SCHEDULER:
7999*4882a593Smuzhiyun retval = sctp_getsockopt_scheduler(sk, len, optval,
8000*4882a593Smuzhiyun optlen);
8001*4882a593Smuzhiyun break;
8002*4882a593Smuzhiyun case SCTP_STREAM_SCHEDULER_VALUE:
8003*4882a593Smuzhiyun retval = sctp_getsockopt_scheduler_value(sk, len, optval,
8004*4882a593Smuzhiyun optlen);
8005*4882a593Smuzhiyun break;
8006*4882a593Smuzhiyun case SCTP_INTERLEAVING_SUPPORTED:
8007*4882a593Smuzhiyun retval = sctp_getsockopt_interleaving_supported(sk, len, optval,
8008*4882a593Smuzhiyun optlen);
8009*4882a593Smuzhiyun break;
8010*4882a593Smuzhiyun case SCTP_REUSE_PORT:
8011*4882a593Smuzhiyun retval = sctp_getsockopt_reuse_port(sk, len, optval, optlen);
8012*4882a593Smuzhiyun break;
8013*4882a593Smuzhiyun case SCTP_EVENT:
8014*4882a593Smuzhiyun retval = sctp_getsockopt_event(sk, len, optval, optlen);
8015*4882a593Smuzhiyun break;
8016*4882a593Smuzhiyun case SCTP_ASCONF_SUPPORTED:
8017*4882a593Smuzhiyun retval = sctp_getsockopt_asconf_supported(sk, len, optval,
8018*4882a593Smuzhiyun optlen);
8019*4882a593Smuzhiyun break;
8020*4882a593Smuzhiyun case SCTP_AUTH_SUPPORTED:
8021*4882a593Smuzhiyun retval = sctp_getsockopt_auth_supported(sk, len, optval,
8022*4882a593Smuzhiyun optlen);
8023*4882a593Smuzhiyun break;
8024*4882a593Smuzhiyun case SCTP_ECN_SUPPORTED:
8025*4882a593Smuzhiyun retval = sctp_getsockopt_ecn_supported(sk, len, optval, optlen);
8026*4882a593Smuzhiyun break;
8027*4882a593Smuzhiyun case SCTP_EXPOSE_POTENTIALLY_FAILED_STATE:
8028*4882a593Smuzhiyun retval = sctp_getsockopt_pf_expose(sk, len, optval, optlen);
8029*4882a593Smuzhiyun break;
8030*4882a593Smuzhiyun default:
8031*4882a593Smuzhiyun retval = -ENOPROTOOPT;
8032*4882a593Smuzhiyun break;
8033*4882a593Smuzhiyun }
8034*4882a593Smuzhiyun
8035*4882a593Smuzhiyun release_sock(sk);
8036*4882a593Smuzhiyun return retval;
8037*4882a593Smuzhiyun }
8038*4882a593Smuzhiyun
sctp_hash(struct sock * sk)8039*4882a593Smuzhiyun static int sctp_hash(struct sock *sk)
8040*4882a593Smuzhiyun {
8041*4882a593Smuzhiyun /* STUB */
8042*4882a593Smuzhiyun return 0;
8043*4882a593Smuzhiyun }
8044*4882a593Smuzhiyun
sctp_unhash(struct sock * sk)8045*4882a593Smuzhiyun static void sctp_unhash(struct sock *sk)
8046*4882a593Smuzhiyun {
8047*4882a593Smuzhiyun /* STUB */
8048*4882a593Smuzhiyun }
8049*4882a593Smuzhiyun
8050*4882a593Smuzhiyun /* Check if port is acceptable. Possibly find first available port.
8051*4882a593Smuzhiyun *
8052*4882a593Smuzhiyun * The port hash table (contained in the 'global' SCTP protocol storage
8053*4882a593Smuzhiyun * returned by struct sctp_protocol *sctp_get_protocol()). The hash
8054*4882a593Smuzhiyun * table is an array of 4096 lists (sctp_bind_hashbucket). Each
8055*4882a593Smuzhiyun * list (the list number is the port number hashed out, so as you
8056*4882a593Smuzhiyun * would expect from a hash function, all the ports in a given list have
8057*4882a593Smuzhiyun * such a number that hashes out to the same list number; you were
8058*4882a593Smuzhiyun * expecting that, right?); so each list has a set of ports, with a
8059*4882a593Smuzhiyun * link to the socket (struct sock) that uses it, the port number and
8060*4882a593Smuzhiyun * a fastreuse flag (FIXME: NPI ipg).
8061*4882a593Smuzhiyun */
8062*4882a593Smuzhiyun static struct sctp_bind_bucket *sctp_bucket_create(
8063*4882a593Smuzhiyun struct sctp_bind_hashbucket *head, struct net *, unsigned short snum);
8064*4882a593Smuzhiyun
sctp_get_port_local(struct sock * sk,union sctp_addr * addr)8065*4882a593Smuzhiyun static int sctp_get_port_local(struct sock *sk, union sctp_addr *addr)
8066*4882a593Smuzhiyun {
8067*4882a593Smuzhiyun struct sctp_sock *sp = sctp_sk(sk);
8068*4882a593Smuzhiyun bool reuse = (sk->sk_reuse || sp->reuse);
8069*4882a593Smuzhiyun struct sctp_bind_hashbucket *head; /* hash list */
8070*4882a593Smuzhiyun struct net *net = sock_net(sk);
8071*4882a593Smuzhiyun kuid_t uid = sock_i_uid(sk);
8072*4882a593Smuzhiyun struct sctp_bind_bucket *pp;
8073*4882a593Smuzhiyun unsigned short snum;
8074*4882a593Smuzhiyun int ret;
8075*4882a593Smuzhiyun
8076*4882a593Smuzhiyun snum = ntohs(addr->v4.sin_port);
8077*4882a593Smuzhiyun
8078*4882a593Smuzhiyun pr_debug("%s: begins, snum:%d\n", __func__, snum);
8079*4882a593Smuzhiyun
8080*4882a593Smuzhiyun if (snum == 0) {
8081*4882a593Smuzhiyun /* Search for an available port. */
8082*4882a593Smuzhiyun int low, high, remaining, index;
8083*4882a593Smuzhiyun unsigned int rover;
8084*4882a593Smuzhiyun
8085*4882a593Smuzhiyun inet_get_local_port_range(net, &low, &high);
8086*4882a593Smuzhiyun remaining = (high - low) + 1;
8087*4882a593Smuzhiyun rover = prandom_u32() % remaining + low;
8088*4882a593Smuzhiyun
8089*4882a593Smuzhiyun do {
8090*4882a593Smuzhiyun rover++;
8091*4882a593Smuzhiyun if ((rover < low) || (rover > high))
8092*4882a593Smuzhiyun rover = low;
8093*4882a593Smuzhiyun if (inet_is_local_reserved_port(net, rover))
8094*4882a593Smuzhiyun continue;
8095*4882a593Smuzhiyun index = sctp_phashfn(net, rover);
8096*4882a593Smuzhiyun head = &sctp_port_hashtable[index];
8097*4882a593Smuzhiyun spin_lock_bh(&head->lock);
8098*4882a593Smuzhiyun sctp_for_each_hentry(pp, &head->chain)
8099*4882a593Smuzhiyun if ((pp->port == rover) &&
8100*4882a593Smuzhiyun net_eq(net, pp->net))
8101*4882a593Smuzhiyun goto next;
8102*4882a593Smuzhiyun break;
8103*4882a593Smuzhiyun next:
8104*4882a593Smuzhiyun spin_unlock_bh(&head->lock);
8105*4882a593Smuzhiyun cond_resched();
8106*4882a593Smuzhiyun } while (--remaining > 0);
8107*4882a593Smuzhiyun
8108*4882a593Smuzhiyun /* Exhausted local port range during search? */
8109*4882a593Smuzhiyun ret = 1;
8110*4882a593Smuzhiyun if (remaining <= 0)
8111*4882a593Smuzhiyun return ret;
8112*4882a593Smuzhiyun
8113*4882a593Smuzhiyun /* OK, here is the one we will use. HEAD (the port
8114*4882a593Smuzhiyun * hash table list entry) is non-NULL and we hold it's
8115*4882a593Smuzhiyun * mutex.
8116*4882a593Smuzhiyun */
8117*4882a593Smuzhiyun snum = rover;
8118*4882a593Smuzhiyun } else {
8119*4882a593Smuzhiyun /* We are given an specific port number; we verify
8120*4882a593Smuzhiyun * that it is not being used. If it is used, we will
8121*4882a593Smuzhiyun * exahust the search in the hash list corresponding
8122*4882a593Smuzhiyun * to the port number (snum) - we detect that with the
8123*4882a593Smuzhiyun * port iterator, pp being NULL.
8124*4882a593Smuzhiyun */
8125*4882a593Smuzhiyun head = &sctp_port_hashtable[sctp_phashfn(net, snum)];
8126*4882a593Smuzhiyun spin_lock_bh(&head->lock);
8127*4882a593Smuzhiyun sctp_for_each_hentry(pp, &head->chain) {
8128*4882a593Smuzhiyun if ((pp->port == snum) && net_eq(pp->net, net))
8129*4882a593Smuzhiyun goto pp_found;
8130*4882a593Smuzhiyun }
8131*4882a593Smuzhiyun }
8132*4882a593Smuzhiyun pp = NULL;
8133*4882a593Smuzhiyun goto pp_not_found;
8134*4882a593Smuzhiyun pp_found:
8135*4882a593Smuzhiyun if (!hlist_empty(&pp->owner)) {
8136*4882a593Smuzhiyun /* We had a port hash table hit - there is an
8137*4882a593Smuzhiyun * available port (pp != NULL) and it is being
8138*4882a593Smuzhiyun * used by other socket (pp->owner not empty); that other
8139*4882a593Smuzhiyun * socket is going to be sk2.
8140*4882a593Smuzhiyun */
8141*4882a593Smuzhiyun struct sock *sk2;
8142*4882a593Smuzhiyun
8143*4882a593Smuzhiyun pr_debug("%s: found a possible match\n", __func__);
8144*4882a593Smuzhiyun
8145*4882a593Smuzhiyun if ((pp->fastreuse && reuse &&
8146*4882a593Smuzhiyun sk->sk_state != SCTP_SS_LISTENING) ||
8147*4882a593Smuzhiyun (pp->fastreuseport && sk->sk_reuseport &&
8148*4882a593Smuzhiyun uid_eq(pp->fastuid, uid)))
8149*4882a593Smuzhiyun goto success;
8150*4882a593Smuzhiyun
8151*4882a593Smuzhiyun /* Run through the list of sockets bound to the port
8152*4882a593Smuzhiyun * (pp->port) [via the pointers bind_next and
8153*4882a593Smuzhiyun * bind_pprev in the struct sock *sk2 (pp->sk)]. On each one,
8154*4882a593Smuzhiyun * we get the endpoint they describe and run through
8155*4882a593Smuzhiyun * the endpoint's list of IP (v4 or v6) addresses,
8156*4882a593Smuzhiyun * comparing each of the addresses with the address of
8157*4882a593Smuzhiyun * the socket sk. If we find a match, then that means
8158*4882a593Smuzhiyun * that this port/socket (sk) combination are already
8159*4882a593Smuzhiyun * in an endpoint.
8160*4882a593Smuzhiyun */
8161*4882a593Smuzhiyun sk_for_each_bound(sk2, &pp->owner) {
8162*4882a593Smuzhiyun struct sctp_sock *sp2 = sctp_sk(sk2);
8163*4882a593Smuzhiyun struct sctp_endpoint *ep2 = sp2->ep;
8164*4882a593Smuzhiyun
8165*4882a593Smuzhiyun if (sk == sk2 ||
8166*4882a593Smuzhiyun (reuse && (sk2->sk_reuse || sp2->reuse) &&
8167*4882a593Smuzhiyun sk2->sk_state != SCTP_SS_LISTENING) ||
8168*4882a593Smuzhiyun (sk->sk_reuseport && sk2->sk_reuseport &&
8169*4882a593Smuzhiyun uid_eq(uid, sock_i_uid(sk2))))
8170*4882a593Smuzhiyun continue;
8171*4882a593Smuzhiyun
8172*4882a593Smuzhiyun if (sctp_bind_addr_conflict(&ep2->base.bind_addr,
8173*4882a593Smuzhiyun addr, sp2, sp)) {
8174*4882a593Smuzhiyun ret = 1;
8175*4882a593Smuzhiyun goto fail_unlock;
8176*4882a593Smuzhiyun }
8177*4882a593Smuzhiyun }
8178*4882a593Smuzhiyun
8179*4882a593Smuzhiyun pr_debug("%s: found a match\n", __func__);
8180*4882a593Smuzhiyun }
8181*4882a593Smuzhiyun pp_not_found:
8182*4882a593Smuzhiyun /* If there was a hash table miss, create a new port. */
8183*4882a593Smuzhiyun ret = 1;
8184*4882a593Smuzhiyun if (!pp && !(pp = sctp_bucket_create(head, net, snum)))
8185*4882a593Smuzhiyun goto fail_unlock;
8186*4882a593Smuzhiyun
8187*4882a593Smuzhiyun /* In either case (hit or miss), make sure fastreuse is 1 only
8188*4882a593Smuzhiyun * if sk->sk_reuse is too (that is, if the caller requested
8189*4882a593Smuzhiyun * SO_REUSEADDR on this socket -sk-).
8190*4882a593Smuzhiyun */
8191*4882a593Smuzhiyun if (hlist_empty(&pp->owner)) {
8192*4882a593Smuzhiyun if (reuse && sk->sk_state != SCTP_SS_LISTENING)
8193*4882a593Smuzhiyun pp->fastreuse = 1;
8194*4882a593Smuzhiyun else
8195*4882a593Smuzhiyun pp->fastreuse = 0;
8196*4882a593Smuzhiyun
8197*4882a593Smuzhiyun if (sk->sk_reuseport) {
8198*4882a593Smuzhiyun pp->fastreuseport = 1;
8199*4882a593Smuzhiyun pp->fastuid = uid;
8200*4882a593Smuzhiyun } else {
8201*4882a593Smuzhiyun pp->fastreuseport = 0;
8202*4882a593Smuzhiyun }
8203*4882a593Smuzhiyun } else {
8204*4882a593Smuzhiyun if (pp->fastreuse &&
8205*4882a593Smuzhiyun (!reuse || sk->sk_state == SCTP_SS_LISTENING))
8206*4882a593Smuzhiyun pp->fastreuse = 0;
8207*4882a593Smuzhiyun
8208*4882a593Smuzhiyun if (pp->fastreuseport &&
8209*4882a593Smuzhiyun (!sk->sk_reuseport || !uid_eq(pp->fastuid, uid)))
8210*4882a593Smuzhiyun pp->fastreuseport = 0;
8211*4882a593Smuzhiyun }
8212*4882a593Smuzhiyun
8213*4882a593Smuzhiyun /* We are set, so fill up all the data in the hash table
8214*4882a593Smuzhiyun * entry, tie the socket list information with the rest of the
8215*4882a593Smuzhiyun * sockets FIXME: Blurry, NPI (ipg).
8216*4882a593Smuzhiyun */
8217*4882a593Smuzhiyun success:
8218*4882a593Smuzhiyun if (!sp->bind_hash) {
8219*4882a593Smuzhiyun inet_sk(sk)->inet_num = snum;
8220*4882a593Smuzhiyun sk_add_bind_node(sk, &pp->owner);
8221*4882a593Smuzhiyun sp->bind_hash = pp;
8222*4882a593Smuzhiyun }
8223*4882a593Smuzhiyun ret = 0;
8224*4882a593Smuzhiyun
8225*4882a593Smuzhiyun fail_unlock:
8226*4882a593Smuzhiyun spin_unlock_bh(&head->lock);
8227*4882a593Smuzhiyun return ret;
8228*4882a593Smuzhiyun }
8229*4882a593Smuzhiyun
8230*4882a593Smuzhiyun /* Assign a 'snum' port to the socket. If snum == 0, an ephemeral
8231*4882a593Smuzhiyun * port is requested.
8232*4882a593Smuzhiyun */
sctp_get_port(struct sock * sk,unsigned short snum)8233*4882a593Smuzhiyun static int sctp_get_port(struct sock *sk, unsigned short snum)
8234*4882a593Smuzhiyun {
8235*4882a593Smuzhiyun union sctp_addr addr;
8236*4882a593Smuzhiyun struct sctp_af *af = sctp_sk(sk)->pf->af;
8237*4882a593Smuzhiyun
8238*4882a593Smuzhiyun /* Set up a dummy address struct from the sk. */
8239*4882a593Smuzhiyun af->from_sk(&addr, sk);
8240*4882a593Smuzhiyun addr.v4.sin_port = htons(snum);
8241*4882a593Smuzhiyun
8242*4882a593Smuzhiyun /* Note: sk->sk_num gets filled in if ephemeral port request. */
8243*4882a593Smuzhiyun return sctp_get_port_local(sk, &addr);
8244*4882a593Smuzhiyun }
8245*4882a593Smuzhiyun
8246*4882a593Smuzhiyun /*
8247*4882a593Smuzhiyun * Move a socket to LISTENING state.
8248*4882a593Smuzhiyun */
sctp_listen_start(struct sock * sk,int backlog)8249*4882a593Smuzhiyun static int sctp_listen_start(struct sock *sk, int backlog)
8250*4882a593Smuzhiyun {
8251*4882a593Smuzhiyun struct sctp_sock *sp = sctp_sk(sk);
8252*4882a593Smuzhiyun struct sctp_endpoint *ep = sp->ep;
8253*4882a593Smuzhiyun struct crypto_shash *tfm = NULL;
8254*4882a593Smuzhiyun char alg[32];
8255*4882a593Smuzhiyun
8256*4882a593Smuzhiyun /* Allocate HMAC for generating cookie. */
8257*4882a593Smuzhiyun if (!sp->hmac && sp->sctp_hmac_alg) {
8258*4882a593Smuzhiyun sprintf(alg, "hmac(%s)", sp->sctp_hmac_alg);
8259*4882a593Smuzhiyun tfm = crypto_alloc_shash(alg, 0, 0);
8260*4882a593Smuzhiyun if (IS_ERR(tfm)) {
8261*4882a593Smuzhiyun net_info_ratelimited("failed to load transform for %s: %ld\n",
8262*4882a593Smuzhiyun sp->sctp_hmac_alg, PTR_ERR(tfm));
8263*4882a593Smuzhiyun return -ENOSYS;
8264*4882a593Smuzhiyun }
8265*4882a593Smuzhiyun sctp_sk(sk)->hmac = tfm;
8266*4882a593Smuzhiyun }
8267*4882a593Smuzhiyun
8268*4882a593Smuzhiyun /*
8269*4882a593Smuzhiyun * If a bind() or sctp_bindx() is not called prior to a listen()
8270*4882a593Smuzhiyun * call that allows new associations to be accepted, the system
8271*4882a593Smuzhiyun * picks an ephemeral port and will choose an address set equivalent
8272*4882a593Smuzhiyun * to binding with a wildcard address.
8273*4882a593Smuzhiyun *
8274*4882a593Smuzhiyun * This is not currently spelled out in the SCTP sockets
8275*4882a593Smuzhiyun * extensions draft, but follows the practice as seen in TCP
8276*4882a593Smuzhiyun * sockets.
8277*4882a593Smuzhiyun *
8278*4882a593Smuzhiyun */
8279*4882a593Smuzhiyun inet_sk_set_state(sk, SCTP_SS_LISTENING);
8280*4882a593Smuzhiyun if (!ep->base.bind_addr.port) {
8281*4882a593Smuzhiyun if (sctp_autobind(sk))
8282*4882a593Smuzhiyun return -EAGAIN;
8283*4882a593Smuzhiyun } else {
8284*4882a593Smuzhiyun if (sctp_get_port(sk, inet_sk(sk)->inet_num)) {
8285*4882a593Smuzhiyun inet_sk_set_state(sk, SCTP_SS_CLOSED);
8286*4882a593Smuzhiyun return -EADDRINUSE;
8287*4882a593Smuzhiyun }
8288*4882a593Smuzhiyun }
8289*4882a593Smuzhiyun
8290*4882a593Smuzhiyun WRITE_ONCE(sk->sk_max_ack_backlog, backlog);
8291*4882a593Smuzhiyun return sctp_hash_endpoint(ep);
8292*4882a593Smuzhiyun }
8293*4882a593Smuzhiyun
8294*4882a593Smuzhiyun /*
8295*4882a593Smuzhiyun * 4.1.3 / 5.1.3 listen()
8296*4882a593Smuzhiyun *
8297*4882a593Smuzhiyun * By default, new associations are not accepted for UDP style sockets.
8298*4882a593Smuzhiyun * An application uses listen() to mark a socket as being able to
8299*4882a593Smuzhiyun * accept new associations.
8300*4882a593Smuzhiyun *
8301*4882a593Smuzhiyun * On TCP style sockets, applications use listen() to ready the SCTP
8302*4882a593Smuzhiyun * endpoint for accepting inbound associations.
8303*4882a593Smuzhiyun *
8304*4882a593Smuzhiyun * On both types of endpoints a backlog of '0' disables listening.
8305*4882a593Smuzhiyun *
8306*4882a593Smuzhiyun * Move a socket to LISTENING state.
8307*4882a593Smuzhiyun */
sctp_inet_listen(struct socket * sock,int backlog)8308*4882a593Smuzhiyun int sctp_inet_listen(struct socket *sock, int backlog)
8309*4882a593Smuzhiyun {
8310*4882a593Smuzhiyun struct sock *sk = sock->sk;
8311*4882a593Smuzhiyun struct sctp_endpoint *ep = sctp_sk(sk)->ep;
8312*4882a593Smuzhiyun int err = -EINVAL;
8313*4882a593Smuzhiyun
8314*4882a593Smuzhiyun if (unlikely(backlog < 0))
8315*4882a593Smuzhiyun return err;
8316*4882a593Smuzhiyun
8317*4882a593Smuzhiyun lock_sock(sk);
8318*4882a593Smuzhiyun
8319*4882a593Smuzhiyun /* Peeled-off sockets are not allowed to listen(). */
8320*4882a593Smuzhiyun if (sctp_style(sk, UDP_HIGH_BANDWIDTH))
8321*4882a593Smuzhiyun goto out;
8322*4882a593Smuzhiyun
8323*4882a593Smuzhiyun if (sock->state != SS_UNCONNECTED)
8324*4882a593Smuzhiyun goto out;
8325*4882a593Smuzhiyun
8326*4882a593Smuzhiyun if (!sctp_sstate(sk, LISTENING) && !sctp_sstate(sk, CLOSED))
8327*4882a593Smuzhiyun goto out;
8328*4882a593Smuzhiyun
8329*4882a593Smuzhiyun /* If backlog is zero, disable listening. */
8330*4882a593Smuzhiyun if (!backlog) {
8331*4882a593Smuzhiyun if (sctp_sstate(sk, CLOSED))
8332*4882a593Smuzhiyun goto out;
8333*4882a593Smuzhiyun
8334*4882a593Smuzhiyun err = 0;
8335*4882a593Smuzhiyun sctp_unhash_endpoint(ep);
8336*4882a593Smuzhiyun sk->sk_state = SCTP_SS_CLOSED;
8337*4882a593Smuzhiyun if (sk->sk_reuse || sctp_sk(sk)->reuse)
8338*4882a593Smuzhiyun sctp_sk(sk)->bind_hash->fastreuse = 1;
8339*4882a593Smuzhiyun goto out;
8340*4882a593Smuzhiyun }
8341*4882a593Smuzhiyun
8342*4882a593Smuzhiyun /* If we are already listening, just update the backlog */
8343*4882a593Smuzhiyun if (sctp_sstate(sk, LISTENING))
8344*4882a593Smuzhiyun WRITE_ONCE(sk->sk_max_ack_backlog, backlog);
8345*4882a593Smuzhiyun else {
8346*4882a593Smuzhiyun err = sctp_listen_start(sk, backlog);
8347*4882a593Smuzhiyun if (err)
8348*4882a593Smuzhiyun goto out;
8349*4882a593Smuzhiyun }
8350*4882a593Smuzhiyun
8351*4882a593Smuzhiyun err = 0;
8352*4882a593Smuzhiyun out:
8353*4882a593Smuzhiyun release_sock(sk);
8354*4882a593Smuzhiyun return err;
8355*4882a593Smuzhiyun }
8356*4882a593Smuzhiyun
8357*4882a593Smuzhiyun /*
8358*4882a593Smuzhiyun * This function is done by modeling the current datagram_poll() and the
8359*4882a593Smuzhiyun * tcp_poll(). Note that, based on these implementations, we don't
8360*4882a593Smuzhiyun * lock the socket in this function, even though it seems that,
8361*4882a593Smuzhiyun * ideally, locking or some other mechanisms can be used to ensure
8362*4882a593Smuzhiyun * the integrity of the counters (sndbuf and wmem_alloc) used
8363*4882a593Smuzhiyun * in this place. We assume that we don't need locks either until proven
8364*4882a593Smuzhiyun * otherwise.
8365*4882a593Smuzhiyun *
8366*4882a593Smuzhiyun * Another thing to note is that we include the Async I/O support
8367*4882a593Smuzhiyun * here, again, by modeling the current TCP/UDP code. We don't have
8368*4882a593Smuzhiyun * a good way to test with it yet.
8369*4882a593Smuzhiyun */
sctp_poll(struct file * file,struct socket * sock,poll_table * wait)8370*4882a593Smuzhiyun __poll_t sctp_poll(struct file *file, struct socket *sock, poll_table *wait)
8371*4882a593Smuzhiyun {
8372*4882a593Smuzhiyun struct sock *sk = sock->sk;
8373*4882a593Smuzhiyun struct sctp_sock *sp = sctp_sk(sk);
8374*4882a593Smuzhiyun __poll_t mask;
8375*4882a593Smuzhiyun
8376*4882a593Smuzhiyun poll_wait(file, sk_sleep(sk), wait);
8377*4882a593Smuzhiyun
8378*4882a593Smuzhiyun sock_rps_record_flow(sk);
8379*4882a593Smuzhiyun
8380*4882a593Smuzhiyun /* A TCP-style listening socket becomes readable when the accept queue
8381*4882a593Smuzhiyun * is not empty.
8382*4882a593Smuzhiyun */
8383*4882a593Smuzhiyun if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
8384*4882a593Smuzhiyun return (!list_empty(&sp->ep->asocs)) ?
8385*4882a593Smuzhiyun (EPOLLIN | EPOLLRDNORM) : 0;
8386*4882a593Smuzhiyun
8387*4882a593Smuzhiyun mask = 0;
8388*4882a593Smuzhiyun
8389*4882a593Smuzhiyun /* Is there any exceptional events? */
8390*4882a593Smuzhiyun if (sk->sk_err || !skb_queue_empty_lockless(&sk->sk_error_queue))
8391*4882a593Smuzhiyun mask |= EPOLLERR |
8392*4882a593Smuzhiyun (sock_flag(sk, SOCK_SELECT_ERR_QUEUE) ? EPOLLPRI : 0);
8393*4882a593Smuzhiyun if (sk->sk_shutdown & RCV_SHUTDOWN)
8394*4882a593Smuzhiyun mask |= EPOLLRDHUP | EPOLLIN | EPOLLRDNORM;
8395*4882a593Smuzhiyun if (sk->sk_shutdown == SHUTDOWN_MASK)
8396*4882a593Smuzhiyun mask |= EPOLLHUP;
8397*4882a593Smuzhiyun
8398*4882a593Smuzhiyun /* Is it readable? Reconsider this code with TCP-style support. */
8399*4882a593Smuzhiyun if (!skb_queue_empty_lockless(&sk->sk_receive_queue))
8400*4882a593Smuzhiyun mask |= EPOLLIN | EPOLLRDNORM;
8401*4882a593Smuzhiyun
8402*4882a593Smuzhiyun /* The association is either gone or not ready. */
8403*4882a593Smuzhiyun if (!sctp_style(sk, UDP) && sctp_sstate(sk, CLOSED))
8404*4882a593Smuzhiyun return mask;
8405*4882a593Smuzhiyun
8406*4882a593Smuzhiyun /* Is it writable? */
8407*4882a593Smuzhiyun if (sctp_writeable(sk)) {
8408*4882a593Smuzhiyun mask |= EPOLLOUT | EPOLLWRNORM;
8409*4882a593Smuzhiyun } else {
8410*4882a593Smuzhiyun sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
8411*4882a593Smuzhiyun /*
8412*4882a593Smuzhiyun * Since the socket is not locked, the buffer
8413*4882a593Smuzhiyun * might be made available after the writeable check and
8414*4882a593Smuzhiyun * before the bit is set. This could cause a lost I/O
8415*4882a593Smuzhiyun * signal. tcp_poll() has a race breaker for this race
8416*4882a593Smuzhiyun * condition. Based on their implementation, we put
8417*4882a593Smuzhiyun * in the following code to cover it as well.
8418*4882a593Smuzhiyun */
8419*4882a593Smuzhiyun if (sctp_writeable(sk))
8420*4882a593Smuzhiyun mask |= EPOLLOUT | EPOLLWRNORM;
8421*4882a593Smuzhiyun }
8422*4882a593Smuzhiyun return mask;
8423*4882a593Smuzhiyun }
8424*4882a593Smuzhiyun
8425*4882a593Smuzhiyun /********************************************************************
8426*4882a593Smuzhiyun * 2nd Level Abstractions
8427*4882a593Smuzhiyun ********************************************************************/
8428*4882a593Smuzhiyun
sctp_bucket_create(struct sctp_bind_hashbucket * head,struct net * net,unsigned short snum)8429*4882a593Smuzhiyun static struct sctp_bind_bucket *sctp_bucket_create(
8430*4882a593Smuzhiyun struct sctp_bind_hashbucket *head, struct net *net, unsigned short snum)
8431*4882a593Smuzhiyun {
8432*4882a593Smuzhiyun struct sctp_bind_bucket *pp;
8433*4882a593Smuzhiyun
8434*4882a593Smuzhiyun pp = kmem_cache_alloc(sctp_bucket_cachep, GFP_ATOMIC);
8435*4882a593Smuzhiyun if (pp) {
8436*4882a593Smuzhiyun SCTP_DBG_OBJCNT_INC(bind_bucket);
8437*4882a593Smuzhiyun pp->port = snum;
8438*4882a593Smuzhiyun pp->fastreuse = 0;
8439*4882a593Smuzhiyun INIT_HLIST_HEAD(&pp->owner);
8440*4882a593Smuzhiyun pp->net = net;
8441*4882a593Smuzhiyun hlist_add_head(&pp->node, &head->chain);
8442*4882a593Smuzhiyun }
8443*4882a593Smuzhiyun return pp;
8444*4882a593Smuzhiyun }
8445*4882a593Smuzhiyun
8446*4882a593Smuzhiyun /* Caller must hold hashbucket lock for this tb with local BH disabled */
sctp_bucket_destroy(struct sctp_bind_bucket * pp)8447*4882a593Smuzhiyun static void sctp_bucket_destroy(struct sctp_bind_bucket *pp)
8448*4882a593Smuzhiyun {
8449*4882a593Smuzhiyun if (pp && hlist_empty(&pp->owner)) {
8450*4882a593Smuzhiyun __hlist_del(&pp->node);
8451*4882a593Smuzhiyun kmem_cache_free(sctp_bucket_cachep, pp);
8452*4882a593Smuzhiyun SCTP_DBG_OBJCNT_DEC(bind_bucket);
8453*4882a593Smuzhiyun }
8454*4882a593Smuzhiyun }
8455*4882a593Smuzhiyun
8456*4882a593Smuzhiyun /* Release this socket's reference to a local port. */
__sctp_put_port(struct sock * sk)8457*4882a593Smuzhiyun static inline void __sctp_put_port(struct sock *sk)
8458*4882a593Smuzhiyun {
8459*4882a593Smuzhiyun struct sctp_bind_hashbucket *head =
8460*4882a593Smuzhiyun &sctp_port_hashtable[sctp_phashfn(sock_net(sk),
8461*4882a593Smuzhiyun inet_sk(sk)->inet_num)];
8462*4882a593Smuzhiyun struct sctp_bind_bucket *pp;
8463*4882a593Smuzhiyun
8464*4882a593Smuzhiyun spin_lock(&head->lock);
8465*4882a593Smuzhiyun pp = sctp_sk(sk)->bind_hash;
8466*4882a593Smuzhiyun __sk_del_bind_node(sk);
8467*4882a593Smuzhiyun sctp_sk(sk)->bind_hash = NULL;
8468*4882a593Smuzhiyun inet_sk(sk)->inet_num = 0;
8469*4882a593Smuzhiyun sctp_bucket_destroy(pp);
8470*4882a593Smuzhiyun spin_unlock(&head->lock);
8471*4882a593Smuzhiyun }
8472*4882a593Smuzhiyun
sctp_put_port(struct sock * sk)8473*4882a593Smuzhiyun void sctp_put_port(struct sock *sk)
8474*4882a593Smuzhiyun {
8475*4882a593Smuzhiyun local_bh_disable();
8476*4882a593Smuzhiyun __sctp_put_port(sk);
8477*4882a593Smuzhiyun local_bh_enable();
8478*4882a593Smuzhiyun }
8479*4882a593Smuzhiyun
8480*4882a593Smuzhiyun /*
8481*4882a593Smuzhiyun * The system picks an ephemeral port and choose an address set equivalent
8482*4882a593Smuzhiyun * to binding with a wildcard address.
8483*4882a593Smuzhiyun * One of those addresses will be the primary address for the association.
8484*4882a593Smuzhiyun * This automatically enables the multihoming capability of SCTP.
8485*4882a593Smuzhiyun */
sctp_autobind(struct sock * sk)8486*4882a593Smuzhiyun static int sctp_autobind(struct sock *sk)
8487*4882a593Smuzhiyun {
8488*4882a593Smuzhiyun union sctp_addr autoaddr;
8489*4882a593Smuzhiyun struct sctp_af *af;
8490*4882a593Smuzhiyun __be16 port;
8491*4882a593Smuzhiyun
8492*4882a593Smuzhiyun /* Initialize a local sockaddr structure to INADDR_ANY. */
8493*4882a593Smuzhiyun af = sctp_sk(sk)->pf->af;
8494*4882a593Smuzhiyun
8495*4882a593Smuzhiyun port = htons(inet_sk(sk)->inet_num);
8496*4882a593Smuzhiyun af->inaddr_any(&autoaddr, port);
8497*4882a593Smuzhiyun
8498*4882a593Smuzhiyun return sctp_do_bind(sk, &autoaddr, af->sockaddr_len);
8499*4882a593Smuzhiyun }
8500*4882a593Smuzhiyun
8501*4882a593Smuzhiyun /* Parse out IPPROTO_SCTP CMSG headers. Perform only minimal validation.
8502*4882a593Smuzhiyun *
8503*4882a593Smuzhiyun * From RFC 2292
8504*4882a593Smuzhiyun * 4.2 The cmsghdr Structure *
8505*4882a593Smuzhiyun *
8506*4882a593Smuzhiyun * When ancillary data is sent or received, any number of ancillary data
8507*4882a593Smuzhiyun * objects can be specified by the msg_control and msg_controllen members of
8508*4882a593Smuzhiyun * the msghdr structure, because each object is preceded by
8509*4882a593Smuzhiyun * a cmsghdr structure defining the object's length (the cmsg_len member).
8510*4882a593Smuzhiyun * Historically Berkeley-derived implementations have passed only one object
8511*4882a593Smuzhiyun * at a time, but this API allows multiple objects to be
8512*4882a593Smuzhiyun * passed in a single call to sendmsg() or recvmsg(). The following example
8513*4882a593Smuzhiyun * shows two ancillary data objects in a control buffer.
8514*4882a593Smuzhiyun *
8515*4882a593Smuzhiyun * |<--------------------------- msg_controllen -------------------------->|
8516*4882a593Smuzhiyun * | |
8517*4882a593Smuzhiyun *
8518*4882a593Smuzhiyun * |<----- ancillary data object ----->|<----- ancillary data object ----->|
8519*4882a593Smuzhiyun *
8520*4882a593Smuzhiyun * |<---------- CMSG_SPACE() --------->|<---------- CMSG_SPACE() --------->|
8521*4882a593Smuzhiyun * | | |
8522*4882a593Smuzhiyun *
8523*4882a593Smuzhiyun * |<---------- cmsg_len ---------->| |<--------- cmsg_len ----------->| |
8524*4882a593Smuzhiyun *
8525*4882a593Smuzhiyun * |<--------- CMSG_LEN() --------->| |<-------- CMSG_LEN() ---------->| |
8526*4882a593Smuzhiyun * | | | | |
8527*4882a593Smuzhiyun *
8528*4882a593Smuzhiyun * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
8529*4882a593Smuzhiyun * |cmsg_|cmsg_|cmsg_|XX| |XX|cmsg_|cmsg_|cmsg_|XX| |XX|
8530*4882a593Smuzhiyun *
8531*4882a593Smuzhiyun * |len |level|type |XX|cmsg_data[]|XX|len |level|type |XX|cmsg_data[]|XX|
8532*4882a593Smuzhiyun *
8533*4882a593Smuzhiyun * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
8534*4882a593Smuzhiyun * ^
8535*4882a593Smuzhiyun * |
8536*4882a593Smuzhiyun *
8537*4882a593Smuzhiyun * msg_control
8538*4882a593Smuzhiyun * points here
8539*4882a593Smuzhiyun */
sctp_msghdr_parse(const struct msghdr * msg,struct sctp_cmsgs * cmsgs)8540*4882a593Smuzhiyun static int sctp_msghdr_parse(const struct msghdr *msg, struct sctp_cmsgs *cmsgs)
8541*4882a593Smuzhiyun {
8542*4882a593Smuzhiyun struct msghdr *my_msg = (struct msghdr *)msg;
8543*4882a593Smuzhiyun struct cmsghdr *cmsg;
8544*4882a593Smuzhiyun
8545*4882a593Smuzhiyun for_each_cmsghdr(cmsg, my_msg) {
8546*4882a593Smuzhiyun if (!CMSG_OK(my_msg, cmsg))
8547*4882a593Smuzhiyun return -EINVAL;
8548*4882a593Smuzhiyun
8549*4882a593Smuzhiyun /* Should we parse this header or ignore? */
8550*4882a593Smuzhiyun if (cmsg->cmsg_level != IPPROTO_SCTP)
8551*4882a593Smuzhiyun continue;
8552*4882a593Smuzhiyun
8553*4882a593Smuzhiyun /* Strictly check lengths following example in SCM code. */
8554*4882a593Smuzhiyun switch (cmsg->cmsg_type) {
8555*4882a593Smuzhiyun case SCTP_INIT:
8556*4882a593Smuzhiyun /* SCTP Socket API Extension
8557*4882a593Smuzhiyun * 5.3.1 SCTP Initiation Structure (SCTP_INIT)
8558*4882a593Smuzhiyun *
8559*4882a593Smuzhiyun * This cmsghdr structure provides information for
8560*4882a593Smuzhiyun * initializing new SCTP associations with sendmsg().
8561*4882a593Smuzhiyun * The SCTP_INITMSG socket option uses this same data
8562*4882a593Smuzhiyun * structure. This structure is not used for
8563*4882a593Smuzhiyun * recvmsg().
8564*4882a593Smuzhiyun *
8565*4882a593Smuzhiyun * cmsg_level cmsg_type cmsg_data[]
8566*4882a593Smuzhiyun * ------------ ------------ ----------------------
8567*4882a593Smuzhiyun * IPPROTO_SCTP SCTP_INIT struct sctp_initmsg
8568*4882a593Smuzhiyun */
8569*4882a593Smuzhiyun if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_initmsg)))
8570*4882a593Smuzhiyun return -EINVAL;
8571*4882a593Smuzhiyun
8572*4882a593Smuzhiyun cmsgs->init = CMSG_DATA(cmsg);
8573*4882a593Smuzhiyun break;
8574*4882a593Smuzhiyun
8575*4882a593Smuzhiyun case SCTP_SNDRCV:
8576*4882a593Smuzhiyun /* SCTP Socket API Extension
8577*4882a593Smuzhiyun * 5.3.2 SCTP Header Information Structure(SCTP_SNDRCV)
8578*4882a593Smuzhiyun *
8579*4882a593Smuzhiyun * This cmsghdr structure specifies SCTP options for
8580*4882a593Smuzhiyun * sendmsg() and describes SCTP header information
8581*4882a593Smuzhiyun * about a received message through recvmsg().
8582*4882a593Smuzhiyun *
8583*4882a593Smuzhiyun * cmsg_level cmsg_type cmsg_data[]
8584*4882a593Smuzhiyun * ------------ ------------ ----------------------
8585*4882a593Smuzhiyun * IPPROTO_SCTP SCTP_SNDRCV struct sctp_sndrcvinfo
8586*4882a593Smuzhiyun */
8587*4882a593Smuzhiyun if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_sndrcvinfo)))
8588*4882a593Smuzhiyun return -EINVAL;
8589*4882a593Smuzhiyun
8590*4882a593Smuzhiyun cmsgs->srinfo = CMSG_DATA(cmsg);
8591*4882a593Smuzhiyun
8592*4882a593Smuzhiyun if (cmsgs->srinfo->sinfo_flags &
8593*4882a593Smuzhiyun ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
8594*4882a593Smuzhiyun SCTP_SACK_IMMEDIATELY | SCTP_SENDALL |
8595*4882a593Smuzhiyun SCTP_PR_SCTP_MASK | SCTP_ABORT | SCTP_EOF))
8596*4882a593Smuzhiyun return -EINVAL;
8597*4882a593Smuzhiyun break;
8598*4882a593Smuzhiyun
8599*4882a593Smuzhiyun case SCTP_SNDINFO:
8600*4882a593Smuzhiyun /* SCTP Socket API Extension
8601*4882a593Smuzhiyun * 5.3.4 SCTP Send Information Structure (SCTP_SNDINFO)
8602*4882a593Smuzhiyun *
8603*4882a593Smuzhiyun * This cmsghdr structure specifies SCTP options for
8604*4882a593Smuzhiyun * sendmsg(). This structure and SCTP_RCVINFO replaces
8605*4882a593Smuzhiyun * SCTP_SNDRCV which has been deprecated.
8606*4882a593Smuzhiyun *
8607*4882a593Smuzhiyun * cmsg_level cmsg_type cmsg_data[]
8608*4882a593Smuzhiyun * ------------ ------------ ---------------------
8609*4882a593Smuzhiyun * IPPROTO_SCTP SCTP_SNDINFO struct sctp_sndinfo
8610*4882a593Smuzhiyun */
8611*4882a593Smuzhiyun if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_sndinfo)))
8612*4882a593Smuzhiyun return -EINVAL;
8613*4882a593Smuzhiyun
8614*4882a593Smuzhiyun cmsgs->sinfo = CMSG_DATA(cmsg);
8615*4882a593Smuzhiyun
8616*4882a593Smuzhiyun if (cmsgs->sinfo->snd_flags &
8617*4882a593Smuzhiyun ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
8618*4882a593Smuzhiyun SCTP_SACK_IMMEDIATELY | SCTP_SENDALL |
8619*4882a593Smuzhiyun SCTP_PR_SCTP_MASK | SCTP_ABORT | SCTP_EOF))
8620*4882a593Smuzhiyun return -EINVAL;
8621*4882a593Smuzhiyun break;
8622*4882a593Smuzhiyun case SCTP_PRINFO:
8623*4882a593Smuzhiyun /* SCTP Socket API Extension
8624*4882a593Smuzhiyun * 5.3.7 SCTP PR-SCTP Information Structure (SCTP_PRINFO)
8625*4882a593Smuzhiyun *
8626*4882a593Smuzhiyun * This cmsghdr structure specifies SCTP options for sendmsg().
8627*4882a593Smuzhiyun *
8628*4882a593Smuzhiyun * cmsg_level cmsg_type cmsg_data[]
8629*4882a593Smuzhiyun * ------------ ------------ ---------------------
8630*4882a593Smuzhiyun * IPPROTO_SCTP SCTP_PRINFO struct sctp_prinfo
8631*4882a593Smuzhiyun */
8632*4882a593Smuzhiyun if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_prinfo)))
8633*4882a593Smuzhiyun return -EINVAL;
8634*4882a593Smuzhiyun
8635*4882a593Smuzhiyun cmsgs->prinfo = CMSG_DATA(cmsg);
8636*4882a593Smuzhiyun if (cmsgs->prinfo->pr_policy & ~SCTP_PR_SCTP_MASK)
8637*4882a593Smuzhiyun return -EINVAL;
8638*4882a593Smuzhiyun
8639*4882a593Smuzhiyun if (cmsgs->prinfo->pr_policy == SCTP_PR_SCTP_NONE)
8640*4882a593Smuzhiyun cmsgs->prinfo->pr_value = 0;
8641*4882a593Smuzhiyun break;
8642*4882a593Smuzhiyun case SCTP_AUTHINFO:
8643*4882a593Smuzhiyun /* SCTP Socket API Extension
8644*4882a593Smuzhiyun * 5.3.8 SCTP AUTH Information Structure (SCTP_AUTHINFO)
8645*4882a593Smuzhiyun *
8646*4882a593Smuzhiyun * This cmsghdr structure specifies SCTP options for sendmsg().
8647*4882a593Smuzhiyun *
8648*4882a593Smuzhiyun * cmsg_level cmsg_type cmsg_data[]
8649*4882a593Smuzhiyun * ------------ ------------ ---------------------
8650*4882a593Smuzhiyun * IPPROTO_SCTP SCTP_AUTHINFO struct sctp_authinfo
8651*4882a593Smuzhiyun */
8652*4882a593Smuzhiyun if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_authinfo)))
8653*4882a593Smuzhiyun return -EINVAL;
8654*4882a593Smuzhiyun
8655*4882a593Smuzhiyun cmsgs->authinfo = CMSG_DATA(cmsg);
8656*4882a593Smuzhiyun break;
8657*4882a593Smuzhiyun case SCTP_DSTADDRV4:
8658*4882a593Smuzhiyun case SCTP_DSTADDRV6:
8659*4882a593Smuzhiyun /* SCTP Socket API Extension
8660*4882a593Smuzhiyun * 5.3.9/10 SCTP Destination IPv4/6 Address Structure (SCTP_DSTADDRV4/6)
8661*4882a593Smuzhiyun *
8662*4882a593Smuzhiyun * This cmsghdr structure specifies SCTP options for sendmsg().
8663*4882a593Smuzhiyun *
8664*4882a593Smuzhiyun * cmsg_level cmsg_type cmsg_data[]
8665*4882a593Smuzhiyun * ------------ ------------ ---------------------
8666*4882a593Smuzhiyun * IPPROTO_SCTP SCTP_DSTADDRV4 struct in_addr
8667*4882a593Smuzhiyun * ------------ ------------ ---------------------
8668*4882a593Smuzhiyun * IPPROTO_SCTP SCTP_DSTADDRV6 struct in6_addr
8669*4882a593Smuzhiyun */
8670*4882a593Smuzhiyun cmsgs->addrs_msg = my_msg;
8671*4882a593Smuzhiyun break;
8672*4882a593Smuzhiyun default:
8673*4882a593Smuzhiyun return -EINVAL;
8674*4882a593Smuzhiyun }
8675*4882a593Smuzhiyun }
8676*4882a593Smuzhiyun
8677*4882a593Smuzhiyun return 0;
8678*4882a593Smuzhiyun }
8679*4882a593Smuzhiyun
8680*4882a593Smuzhiyun /*
8681*4882a593Smuzhiyun * Wait for a packet..
8682*4882a593Smuzhiyun * Note: This function is the same function as in core/datagram.c
8683*4882a593Smuzhiyun * with a few modifications to make lksctp work.
8684*4882a593Smuzhiyun */
sctp_wait_for_packet(struct sock * sk,int * err,long * timeo_p)8685*4882a593Smuzhiyun static int sctp_wait_for_packet(struct sock *sk, int *err, long *timeo_p)
8686*4882a593Smuzhiyun {
8687*4882a593Smuzhiyun int error;
8688*4882a593Smuzhiyun DEFINE_WAIT(wait);
8689*4882a593Smuzhiyun
8690*4882a593Smuzhiyun prepare_to_wait_exclusive(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
8691*4882a593Smuzhiyun
8692*4882a593Smuzhiyun /* Socket errors? */
8693*4882a593Smuzhiyun error = sock_error(sk);
8694*4882a593Smuzhiyun if (error)
8695*4882a593Smuzhiyun goto out;
8696*4882a593Smuzhiyun
8697*4882a593Smuzhiyun if (!skb_queue_empty(&sk->sk_receive_queue))
8698*4882a593Smuzhiyun goto ready;
8699*4882a593Smuzhiyun
8700*4882a593Smuzhiyun /* Socket shut down? */
8701*4882a593Smuzhiyun if (sk->sk_shutdown & RCV_SHUTDOWN)
8702*4882a593Smuzhiyun goto out;
8703*4882a593Smuzhiyun
8704*4882a593Smuzhiyun /* Sequenced packets can come disconnected. If so we report the
8705*4882a593Smuzhiyun * problem.
8706*4882a593Smuzhiyun */
8707*4882a593Smuzhiyun error = -ENOTCONN;
8708*4882a593Smuzhiyun
8709*4882a593Smuzhiyun /* Is there a good reason to think that we may receive some data? */
8710*4882a593Smuzhiyun if (list_empty(&sctp_sk(sk)->ep->asocs) && !sctp_sstate(sk, LISTENING))
8711*4882a593Smuzhiyun goto out;
8712*4882a593Smuzhiyun
8713*4882a593Smuzhiyun /* Handle signals. */
8714*4882a593Smuzhiyun if (signal_pending(current))
8715*4882a593Smuzhiyun goto interrupted;
8716*4882a593Smuzhiyun
8717*4882a593Smuzhiyun /* Let another process have a go. Since we are going to sleep
8718*4882a593Smuzhiyun * anyway. Note: This may cause odd behaviors if the message
8719*4882a593Smuzhiyun * does not fit in the user's buffer, but this seems to be the
8720*4882a593Smuzhiyun * only way to honor MSG_DONTWAIT realistically.
8721*4882a593Smuzhiyun */
8722*4882a593Smuzhiyun release_sock(sk);
8723*4882a593Smuzhiyun *timeo_p = schedule_timeout(*timeo_p);
8724*4882a593Smuzhiyun lock_sock(sk);
8725*4882a593Smuzhiyun
8726*4882a593Smuzhiyun ready:
8727*4882a593Smuzhiyun finish_wait(sk_sleep(sk), &wait);
8728*4882a593Smuzhiyun return 0;
8729*4882a593Smuzhiyun
8730*4882a593Smuzhiyun interrupted:
8731*4882a593Smuzhiyun error = sock_intr_errno(*timeo_p);
8732*4882a593Smuzhiyun
8733*4882a593Smuzhiyun out:
8734*4882a593Smuzhiyun finish_wait(sk_sleep(sk), &wait);
8735*4882a593Smuzhiyun *err = error;
8736*4882a593Smuzhiyun return error;
8737*4882a593Smuzhiyun }
8738*4882a593Smuzhiyun
8739*4882a593Smuzhiyun /* Receive a datagram.
8740*4882a593Smuzhiyun * Note: This is pretty much the same routine as in core/datagram.c
8741*4882a593Smuzhiyun * with a few changes to make lksctp work.
8742*4882a593Smuzhiyun */
sctp_skb_recv_datagram(struct sock * sk,int flags,int noblock,int * err)8743*4882a593Smuzhiyun struct sk_buff *sctp_skb_recv_datagram(struct sock *sk, int flags,
8744*4882a593Smuzhiyun int noblock, int *err)
8745*4882a593Smuzhiyun {
8746*4882a593Smuzhiyun int error;
8747*4882a593Smuzhiyun struct sk_buff *skb;
8748*4882a593Smuzhiyun long timeo;
8749*4882a593Smuzhiyun
8750*4882a593Smuzhiyun timeo = sock_rcvtimeo(sk, noblock);
8751*4882a593Smuzhiyun
8752*4882a593Smuzhiyun pr_debug("%s: timeo:%ld, max:%ld\n", __func__, timeo,
8753*4882a593Smuzhiyun MAX_SCHEDULE_TIMEOUT);
8754*4882a593Smuzhiyun
8755*4882a593Smuzhiyun do {
8756*4882a593Smuzhiyun /* Again only user level code calls this function,
8757*4882a593Smuzhiyun * so nothing interrupt level
8758*4882a593Smuzhiyun * will suddenly eat the receive_queue.
8759*4882a593Smuzhiyun *
8760*4882a593Smuzhiyun * Look at current nfs client by the way...
8761*4882a593Smuzhiyun * However, this function was correct in any case. 8)
8762*4882a593Smuzhiyun */
8763*4882a593Smuzhiyun if (flags & MSG_PEEK) {
8764*4882a593Smuzhiyun skb = skb_peek(&sk->sk_receive_queue);
8765*4882a593Smuzhiyun if (skb)
8766*4882a593Smuzhiyun refcount_inc(&skb->users);
8767*4882a593Smuzhiyun } else {
8768*4882a593Smuzhiyun skb = __skb_dequeue(&sk->sk_receive_queue);
8769*4882a593Smuzhiyun }
8770*4882a593Smuzhiyun
8771*4882a593Smuzhiyun if (skb)
8772*4882a593Smuzhiyun return skb;
8773*4882a593Smuzhiyun
8774*4882a593Smuzhiyun /* Caller is allowed not to check sk->sk_err before calling. */
8775*4882a593Smuzhiyun error = sock_error(sk);
8776*4882a593Smuzhiyun if (error)
8777*4882a593Smuzhiyun goto no_packet;
8778*4882a593Smuzhiyun
8779*4882a593Smuzhiyun if (sk->sk_shutdown & RCV_SHUTDOWN)
8780*4882a593Smuzhiyun break;
8781*4882a593Smuzhiyun
8782*4882a593Smuzhiyun if (sk_can_busy_loop(sk)) {
8783*4882a593Smuzhiyun sk_busy_loop(sk, noblock);
8784*4882a593Smuzhiyun
8785*4882a593Smuzhiyun if (!skb_queue_empty_lockless(&sk->sk_receive_queue))
8786*4882a593Smuzhiyun continue;
8787*4882a593Smuzhiyun }
8788*4882a593Smuzhiyun
8789*4882a593Smuzhiyun /* User doesn't want to wait. */
8790*4882a593Smuzhiyun error = -EAGAIN;
8791*4882a593Smuzhiyun if (!timeo)
8792*4882a593Smuzhiyun goto no_packet;
8793*4882a593Smuzhiyun } while (sctp_wait_for_packet(sk, err, &timeo) == 0);
8794*4882a593Smuzhiyun
8795*4882a593Smuzhiyun return NULL;
8796*4882a593Smuzhiyun
8797*4882a593Smuzhiyun no_packet:
8798*4882a593Smuzhiyun *err = error;
8799*4882a593Smuzhiyun return NULL;
8800*4882a593Smuzhiyun }
8801*4882a593Smuzhiyun
8802*4882a593Smuzhiyun /* If sndbuf has changed, wake up per association sndbuf waiters. */
__sctp_write_space(struct sctp_association * asoc)8803*4882a593Smuzhiyun static void __sctp_write_space(struct sctp_association *asoc)
8804*4882a593Smuzhiyun {
8805*4882a593Smuzhiyun struct sock *sk = asoc->base.sk;
8806*4882a593Smuzhiyun
8807*4882a593Smuzhiyun if (sctp_wspace(asoc) <= 0)
8808*4882a593Smuzhiyun return;
8809*4882a593Smuzhiyun
8810*4882a593Smuzhiyun if (waitqueue_active(&asoc->wait))
8811*4882a593Smuzhiyun wake_up_interruptible(&asoc->wait);
8812*4882a593Smuzhiyun
8813*4882a593Smuzhiyun if (sctp_writeable(sk)) {
8814*4882a593Smuzhiyun struct socket_wq *wq;
8815*4882a593Smuzhiyun
8816*4882a593Smuzhiyun rcu_read_lock();
8817*4882a593Smuzhiyun wq = rcu_dereference(sk->sk_wq);
8818*4882a593Smuzhiyun if (wq) {
8819*4882a593Smuzhiyun if (waitqueue_active(&wq->wait))
8820*4882a593Smuzhiyun wake_up_interruptible(&wq->wait);
8821*4882a593Smuzhiyun
8822*4882a593Smuzhiyun /* Note that we try to include the Async I/O support
8823*4882a593Smuzhiyun * here by modeling from the current TCP/UDP code.
8824*4882a593Smuzhiyun * We have not tested with it yet.
8825*4882a593Smuzhiyun */
8826*4882a593Smuzhiyun if (!(sk->sk_shutdown & SEND_SHUTDOWN))
8827*4882a593Smuzhiyun sock_wake_async(wq, SOCK_WAKE_SPACE, POLL_OUT);
8828*4882a593Smuzhiyun }
8829*4882a593Smuzhiyun rcu_read_unlock();
8830*4882a593Smuzhiyun }
8831*4882a593Smuzhiyun }
8832*4882a593Smuzhiyun
sctp_wake_up_waiters(struct sock * sk,struct sctp_association * asoc)8833*4882a593Smuzhiyun static void sctp_wake_up_waiters(struct sock *sk,
8834*4882a593Smuzhiyun struct sctp_association *asoc)
8835*4882a593Smuzhiyun {
8836*4882a593Smuzhiyun struct sctp_association *tmp = asoc;
8837*4882a593Smuzhiyun
8838*4882a593Smuzhiyun /* We do accounting for the sndbuf space per association,
8839*4882a593Smuzhiyun * so we only need to wake our own association.
8840*4882a593Smuzhiyun */
8841*4882a593Smuzhiyun if (asoc->ep->sndbuf_policy)
8842*4882a593Smuzhiyun return __sctp_write_space(asoc);
8843*4882a593Smuzhiyun
8844*4882a593Smuzhiyun /* If association goes down and is just flushing its
8845*4882a593Smuzhiyun * outq, then just normally notify others.
8846*4882a593Smuzhiyun */
8847*4882a593Smuzhiyun if (asoc->base.dead)
8848*4882a593Smuzhiyun return sctp_write_space(sk);
8849*4882a593Smuzhiyun
8850*4882a593Smuzhiyun /* Accounting for the sndbuf space is per socket, so we
8851*4882a593Smuzhiyun * need to wake up others, try to be fair and in case of
8852*4882a593Smuzhiyun * other associations, let them have a go first instead
8853*4882a593Smuzhiyun * of just doing a sctp_write_space() call.
8854*4882a593Smuzhiyun *
8855*4882a593Smuzhiyun * Note that we reach sctp_wake_up_waiters() only when
8856*4882a593Smuzhiyun * associations free up queued chunks, thus we are under
8857*4882a593Smuzhiyun * lock and the list of associations on a socket is
8858*4882a593Smuzhiyun * guaranteed not to change.
8859*4882a593Smuzhiyun */
8860*4882a593Smuzhiyun for (tmp = list_next_entry(tmp, asocs); 1;
8861*4882a593Smuzhiyun tmp = list_next_entry(tmp, asocs)) {
8862*4882a593Smuzhiyun /* Manually skip the head element. */
8863*4882a593Smuzhiyun if (&tmp->asocs == &((sctp_sk(sk))->ep->asocs))
8864*4882a593Smuzhiyun continue;
8865*4882a593Smuzhiyun /* Wake up association. */
8866*4882a593Smuzhiyun __sctp_write_space(tmp);
8867*4882a593Smuzhiyun /* We've reached the end. */
8868*4882a593Smuzhiyun if (tmp == asoc)
8869*4882a593Smuzhiyun break;
8870*4882a593Smuzhiyun }
8871*4882a593Smuzhiyun }
8872*4882a593Smuzhiyun
8873*4882a593Smuzhiyun /* Do accounting for the sndbuf space.
8874*4882a593Smuzhiyun * Decrement the used sndbuf space of the corresponding association by the
8875*4882a593Smuzhiyun * data size which was just transmitted(freed).
8876*4882a593Smuzhiyun */
sctp_wfree(struct sk_buff * skb)8877*4882a593Smuzhiyun static void sctp_wfree(struct sk_buff *skb)
8878*4882a593Smuzhiyun {
8879*4882a593Smuzhiyun struct sctp_chunk *chunk = skb_shinfo(skb)->destructor_arg;
8880*4882a593Smuzhiyun struct sctp_association *asoc = chunk->asoc;
8881*4882a593Smuzhiyun struct sock *sk = asoc->base.sk;
8882*4882a593Smuzhiyun
8883*4882a593Smuzhiyun sk_mem_uncharge(sk, skb->truesize);
8884*4882a593Smuzhiyun sk->sk_wmem_queued -= skb->truesize + sizeof(struct sctp_chunk);
8885*4882a593Smuzhiyun asoc->sndbuf_used -= skb->truesize + sizeof(struct sctp_chunk);
8886*4882a593Smuzhiyun WARN_ON(refcount_sub_and_test(sizeof(struct sctp_chunk),
8887*4882a593Smuzhiyun &sk->sk_wmem_alloc));
8888*4882a593Smuzhiyun
8889*4882a593Smuzhiyun if (chunk->shkey) {
8890*4882a593Smuzhiyun struct sctp_shared_key *shkey = chunk->shkey;
8891*4882a593Smuzhiyun
8892*4882a593Smuzhiyun /* refcnt == 2 and !list_empty mean after this release, it's
8893*4882a593Smuzhiyun * not being used anywhere, and it's time to notify userland
8894*4882a593Smuzhiyun * that this shkey can be freed if it's been deactivated.
8895*4882a593Smuzhiyun */
8896*4882a593Smuzhiyun if (shkey->deactivated && !list_empty(&shkey->key_list) &&
8897*4882a593Smuzhiyun refcount_read(&shkey->refcnt) == 2) {
8898*4882a593Smuzhiyun struct sctp_ulpevent *ev;
8899*4882a593Smuzhiyun
8900*4882a593Smuzhiyun ev = sctp_ulpevent_make_authkey(asoc, shkey->key_id,
8901*4882a593Smuzhiyun SCTP_AUTH_FREE_KEY,
8902*4882a593Smuzhiyun GFP_KERNEL);
8903*4882a593Smuzhiyun if (ev)
8904*4882a593Smuzhiyun asoc->stream.si->enqueue_event(&asoc->ulpq, ev);
8905*4882a593Smuzhiyun }
8906*4882a593Smuzhiyun sctp_auth_shkey_release(chunk->shkey);
8907*4882a593Smuzhiyun }
8908*4882a593Smuzhiyun
8909*4882a593Smuzhiyun sock_wfree(skb);
8910*4882a593Smuzhiyun sctp_wake_up_waiters(sk, asoc);
8911*4882a593Smuzhiyun
8912*4882a593Smuzhiyun sctp_association_put(asoc);
8913*4882a593Smuzhiyun }
8914*4882a593Smuzhiyun
8915*4882a593Smuzhiyun /* Do accounting for the receive space on the socket.
8916*4882a593Smuzhiyun * Accounting for the association is done in ulpevent.c
8917*4882a593Smuzhiyun * We set this as a destructor for the cloned data skbs so that
8918*4882a593Smuzhiyun * accounting is done at the correct time.
8919*4882a593Smuzhiyun */
sctp_sock_rfree(struct sk_buff * skb)8920*4882a593Smuzhiyun void sctp_sock_rfree(struct sk_buff *skb)
8921*4882a593Smuzhiyun {
8922*4882a593Smuzhiyun struct sock *sk = skb->sk;
8923*4882a593Smuzhiyun struct sctp_ulpevent *event = sctp_skb2event(skb);
8924*4882a593Smuzhiyun
8925*4882a593Smuzhiyun atomic_sub(event->rmem_len, &sk->sk_rmem_alloc);
8926*4882a593Smuzhiyun
8927*4882a593Smuzhiyun /*
8928*4882a593Smuzhiyun * Mimic the behavior of sock_rfree
8929*4882a593Smuzhiyun */
8930*4882a593Smuzhiyun sk_mem_uncharge(sk, event->rmem_len);
8931*4882a593Smuzhiyun }
8932*4882a593Smuzhiyun
8933*4882a593Smuzhiyun
8934*4882a593Smuzhiyun /* Helper function to wait for space in the sndbuf. */
sctp_wait_for_sndbuf(struct sctp_association * asoc,long * timeo_p,size_t msg_len)8935*4882a593Smuzhiyun static int sctp_wait_for_sndbuf(struct sctp_association *asoc, long *timeo_p,
8936*4882a593Smuzhiyun size_t msg_len)
8937*4882a593Smuzhiyun {
8938*4882a593Smuzhiyun struct sock *sk = asoc->base.sk;
8939*4882a593Smuzhiyun long current_timeo = *timeo_p;
8940*4882a593Smuzhiyun DEFINE_WAIT(wait);
8941*4882a593Smuzhiyun int err = 0;
8942*4882a593Smuzhiyun
8943*4882a593Smuzhiyun pr_debug("%s: asoc:%p, timeo:%ld, msg_len:%zu\n", __func__, asoc,
8944*4882a593Smuzhiyun *timeo_p, msg_len);
8945*4882a593Smuzhiyun
8946*4882a593Smuzhiyun /* Increment the association's refcnt. */
8947*4882a593Smuzhiyun sctp_association_hold(asoc);
8948*4882a593Smuzhiyun
8949*4882a593Smuzhiyun /* Wait on the association specific sndbuf space. */
8950*4882a593Smuzhiyun for (;;) {
8951*4882a593Smuzhiyun prepare_to_wait_exclusive(&asoc->wait, &wait,
8952*4882a593Smuzhiyun TASK_INTERRUPTIBLE);
8953*4882a593Smuzhiyun if (asoc->base.dead)
8954*4882a593Smuzhiyun goto do_dead;
8955*4882a593Smuzhiyun if (!*timeo_p)
8956*4882a593Smuzhiyun goto do_nonblock;
8957*4882a593Smuzhiyun if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING)
8958*4882a593Smuzhiyun goto do_error;
8959*4882a593Smuzhiyun if (signal_pending(current))
8960*4882a593Smuzhiyun goto do_interrupted;
8961*4882a593Smuzhiyun if (sk_under_memory_pressure(sk))
8962*4882a593Smuzhiyun sk_mem_reclaim(sk);
8963*4882a593Smuzhiyun if ((int)msg_len <= sctp_wspace(asoc) &&
8964*4882a593Smuzhiyun sk_wmem_schedule(sk, msg_len))
8965*4882a593Smuzhiyun break;
8966*4882a593Smuzhiyun
8967*4882a593Smuzhiyun /* Let another process have a go. Since we are going
8968*4882a593Smuzhiyun * to sleep anyway.
8969*4882a593Smuzhiyun */
8970*4882a593Smuzhiyun release_sock(sk);
8971*4882a593Smuzhiyun current_timeo = schedule_timeout(current_timeo);
8972*4882a593Smuzhiyun lock_sock(sk);
8973*4882a593Smuzhiyun if (sk != asoc->base.sk)
8974*4882a593Smuzhiyun goto do_error;
8975*4882a593Smuzhiyun
8976*4882a593Smuzhiyun *timeo_p = current_timeo;
8977*4882a593Smuzhiyun }
8978*4882a593Smuzhiyun
8979*4882a593Smuzhiyun out:
8980*4882a593Smuzhiyun finish_wait(&asoc->wait, &wait);
8981*4882a593Smuzhiyun
8982*4882a593Smuzhiyun /* Release the association's refcnt. */
8983*4882a593Smuzhiyun sctp_association_put(asoc);
8984*4882a593Smuzhiyun
8985*4882a593Smuzhiyun return err;
8986*4882a593Smuzhiyun
8987*4882a593Smuzhiyun do_dead:
8988*4882a593Smuzhiyun err = -ESRCH;
8989*4882a593Smuzhiyun goto out;
8990*4882a593Smuzhiyun
8991*4882a593Smuzhiyun do_error:
8992*4882a593Smuzhiyun err = -EPIPE;
8993*4882a593Smuzhiyun goto out;
8994*4882a593Smuzhiyun
8995*4882a593Smuzhiyun do_interrupted:
8996*4882a593Smuzhiyun err = sock_intr_errno(*timeo_p);
8997*4882a593Smuzhiyun goto out;
8998*4882a593Smuzhiyun
8999*4882a593Smuzhiyun do_nonblock:
9000*4882a593Smuzhiyun err = -EAGAIN;
9001*4882a593Smuzhiyun goto out;
9002*4882a593Smuzhiyun }
9003*4882a593Smuzhiyun
sctp_data_ready(struct sock * sk)9004*4882a593Smuzhiyun void sctp_data_ready(struct sock *sk)
9005*4882a593Smuzhiyun {
9006*4882a593Smuzhiyun struct socket_wq *wq;
9007*4882a593Smuzhiyun
9008*4882a593Smuzhiyun rcu_read_lock();
9009*4882a593Smuzhiyun wq = rcu_dereference(sk->sk_wq);
9010*4882a593Smuzhiyun if (skwq_has_sleeper(wq))
9011*4882a593Smuzhiyun wake_up_interruptible_sync_poll(&wq->wait, EPOLLIN |
9012*4882a593Smuzhiyun EPOLLRDNORM | EPOLLRDBAND);
9013*4882a593Smuzhiyun sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
9014*4882a593Smuzhiyun rcu_read_unlock();
9015*4882a593Smuzhiyun }
9016*4882a593Smuzhiyun
9017*4882a593Smuzhiyun /* If socket sndbuf has changed, wake up all per association waiters. */
sctp_write_space(struct sock * sk)9018*4882a593Smuzhiyun void sctp_write_space(struct sock *sk)
9019*4882a593Smuzhiyun {
9020*4882a593Smuzhiyun struct sctp_association *asoc;
9021*4882a593Smuzhiyun
9022*4882a593Smuzhiyun /* Wake up the tasks in each wait queue. */
9023*4882a593Smuzhiyun list_for_each_entry(asoc, &((sctp_sk(sk))->ep->asocs), asocs) {
9024*4882a593Smuzhiyun __sctp_write_space(asoc);
9025*4882a593Smuzhiyun }
9026*4882a593Smuzhiyun }
9027*4882a593Smuzhiyun
9028*4882a593Smuzhiyun /* Is there any sndbuf space available on the socket?
9029*4882a593Smuzhiyun *
9030*4882a593Smuzhiyun * Note that sk_wmem_alloc is the sum of the send buffers on all of the
9031*4882a593Smuzhiyun * associations on the same socket. For a UDP-style socket with
9032*4882a593Smuzhiyun * multiple associations, it is possible for it to be "unwriteable"
9033*4882a593Smuzhiyun * prematurely. I assume that this is acceptable because
9034*4882a593Smuzhiyun * a premature "unwriteable" is better than an accidental "writeable" which
9035*4882a593Smuzhiyun * would cause an unwanted block under certain circumstances. For the 1-1
9036*4882a593Smuzhiyun * UDP-style sockets or TCP-style sockets, this code should work.
9037*4882a593Smuzhiyun * - Daisy
9038*4882a593Smuzhiyun */
sctp_writeable(struct sock * sk)9039*4882a593Smuzhiyun static bool sctp_writeable(struct sock *sk)
9040*4882a593Smuzhiyun {
9041*4882a593Smuzhiyun return sk->sk_sndbuf > sk->sk_wmem_queued;
9042*4882a593Smuzhiyun }
9043*4882a593Smuzhiyun
9044*4882a593Smuzhiyun /* Wait for an association to go into ESTABLISHED state. If timeout is 0,
9045*4882a593Smuzhiyun * returns immediately with EINPROGRESS.
9046*4882a593Smuzhiyun */
sctp_wait_for_connect(struct sctp_association * asoc,long * timeo_p)9047*4882a593Smuzhiyun static int sctp_wait_for_connect(struct sctp_association *asoc, long *timeo_p)
9048*4882a593Smuzhiyun {
9049*4882a593Smuzhiyun struct sock *sk = asoc->base.sk;
9050*4882a593Smuzhiyun int err = 0;
9051*4882a593Smuzhiyun long current_timeo = *timeo_p;
9052*4882a593Smuzhiyun DEFINE_WAIT(wait);
9053*4882a593Smuzhiyun
9054*4882a593Smuzhiyun pr_debug("%s: asoc:%p, timeo:%ld\n", __func__, asoc, *timeo_p);
9055*4882a593Smuzhiyun
9056*4882a593Smuzhiyun /* Increment the association's refcnt. */
9057*4882a593Smuzhiyun sctp_association_hold(asoc);
9058*4882a593Smuzhiyun
9059*4882a593Smuzhiyun for (;;) {
9060*4882a593Smuzhiyun prepare_to_wait_exclusive(&asoc->wait, &wait,
9061*4882a593Smuzhiyun TASK_INTERRUPTIBLE);
9062*4882a593Smuzhiyun if (!*timeo_p)
9063*4882a593Smuzhiyun goto do_nonblock;
9064*4882a593Smuzhiyun if (sk->sk_shutdown & RCV_SHUTDOWN)
9065*4882a593Smuzhiyun break;
9066*4882a593Smuzhiyun if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING ||
9067*4882a593Smuzhiyun asoc->base.dead)
9068*4882a593Smuzhiyun goto do_error;
9069*4882a593Smuzhiyun if (signal_pending(current))
9070*4882a593Smuzhiyun goto do_interrupted;
9071*4882a593Smuzhiyun
9072*4882a593Smuzhiyun if (sctp_state(asoc, ESTABLISHED))
9073*4882a593Smuzhiyun break;
9074*4882a593Smuzhiyun
9075*4882a593Smuzhiyun /* Let another process have a go. Since we are going
9076*4882a593Smuzhiyun * to sleep anyway.
9077*4882a593Smuzhiyun */
9078*4882a593Smuzhiyun release_sock(sk);
9079*4882a593Smuzhiyun current_timeo = schedule_timeout(current_timeo);
9080*4882a593Smuzhiyun lock_sock(sk);
9081*4882a593Smuzhiyun
9082*4882a593Smuzhiyun *timeo_p = current_timeo;
9083*4882a593Smuzhiyun }
9084*4882a593Smuzhiyun
9085*4882a593Smuzhiyun out:
9086*4882a593Smuzhiyun finish_wait(&asoc->wait, &wait);
9087*4882a593Smuzhiyun
9088*4882a593Smuzhiyun /* Release the association's refcnt. */
9089*4882a593Smuzhiyun sctp_association_put(asoc);
9090*4882a593Smuzhiyun
9091*4882a593Smuzhiyun return err;
9092*4882a593Smuzhiyun
9093*4882a593Smuzhiyun do_error:
9094*4882a593Smuzhiyun if (asoc->init_err_counter + 1 > asoc->max_init_attempts)
9095*4882a593Smuzhiyun err = -ETIMEDOUT;
9096*4882a593Smuzhiyun else
9097*4882a593Smuzhiyun err = -ECONNREFUSED;
9098*4882a593Smuzhiyun goto out;
9099*4882a593Smuzhiyun
9100*4882a593Smuzhiyun do_interrupted:
9101*4882a593Smuzhiyun err = sock_intr_errno(*timeo_p);
9102*4882a593Smuzhiyun goto out;
9103*4882a593Smuzhiyun
9104*4882a593Smuzhiyun do_nonblock:
9105*4882a593Smuzhiyun err = -EINPROGRESS;
9106*4882a593Smuzhiyun goto out;
9107*4882a593Smuzhiyun }
9108*4882a593Smuzhiyun
sctp_wait_for_accept(struct sock * sk,long timeo)9109*4882a593Smuzhiyun static int sctp_wait_for_accept(struct sock *sk, long timeo)
9110*4882a593Smuzhiyun {
9111*4882a593Smuzhiyun struct sctp_endpoint *ep;
9112*4882a593Smuzhiyun int err = 0;
9113*4882a593Smuzhiyun DEFINE_WAIT(wait);
9114*4882a593Smuzhiyun
9115*4882a593Smuzhiyun ep = sctp_sk(sk)->ep;
9116*4882a593Smuzhiyun
9117*4882a593Smuzhiyun
9118*4882a593Smuzhiyun for (;;) {
9119*4882a593Smuzhiyun prepare_to_wait_exclusive(sk_sleep(sk), &wait,
9120*4882a593Smuzhiyun TASK_INTERRUPTIBLE);
9121*4882a593Smuzhiyun
9122*4882a593Smuzhiyun if (list_empty(&ep->asocs)) {
9123*4882a593Smuzhiyun release_sock(sk);
9124*4882a593Smuzhiyun timeo = schedule_timeout(timeo);
9125*4882a593Smuzhiyun lock_sock(sk);
9126*4882a593Smuzhiyun }
9127*4882a593Smuzhiyun
9128*4882a593Smuzhiyun err = -EINVAL;
9129*4882a593Smuzhiyun if (!sctp_sstate(sk, LISTENING))
9130*4882a593Smuzhiyun break;
9131*4882a593Smuzhiyun
9132*4882a593Smuzhiyun err = 0;
9133*4882a593Smuzhiyun if (!list_empty(&ep->asocs))
9134*4882a593Smuzhiyun break;
9135*4882a593Smuzhiyun
9136*4882a593Smuzhiyun err = sock_intr_errno(timeo);
9137*4882a593Smuzhiyun if (signal_pending(current))
9138*4882a593Smuzhiyun break;
9139*4882a593Smuzhiyun
9140*4882a593Smuzhiyun err = -EAGAIN;
9141*4882a593Smuzhiyun if (!timeo)
9142*4882a593Smuzhiyun break;
9143*4882a593Smuzhiyun }
9144*4882a593Smuzhiyun
9145*4882a593Smuzhiyun finish_wait(sk_sleep(sk), &wait);
9146*4882a593Smuzhiyun
9147*4882a593Smuzhiyun return err;
9148*4882a593Smuzhiyun }
9149*4882a593Smuzhiyun
sctp_wait_for_close(struct sock * sk,long timeout)9150*4882a593Smuzhiyun static void sctp_wait_for_close(struct sock *sk, long timeout)
9151*4882a593Smuzhiyun {
9152*4882a593Smuzhiyun DEFINE_WAIT(wait);
9153*4882a593Smuzhiyun
9154*4882a593Smuzhiyun do {
9155*4882a593Smuzhiyun prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
9156*4882a593Smuzhiyun if (list_empty(&sctp_sk(sk)->ep->asocs))
9157*4882a593Smuzhiyun break;
9158*4882a593Smuzhiyun release_sock(sk);
9159*4882a593Smuzhiyun timeout = schedule_timeout(timeout);
9160*4882a593Smuzhiyun lock_sock(sk);
9161*4882a593Smuzhiyun } while (!signal_pending(current) && timeout);
9162*4882a593Smuzhiyun
9163*4882a593Smuzhiyun finish_wait(sk_sleep(sk), &wait);
9164*4882a593Smuzhiyun }
9165*4882a593Smuzhiyun
sctp_skb_set_owner_r_frag(struct sk_buff * skb,struct sock * sk)9166*4882a593Smuzhiyun static void sctp_skb_set_owner_r_frag(struct sk_buff *skb, struct sock *sk)
9167*4882a593Smuzhiyun {
9168*4882a593Smuzhiyun struct sk_buff *frag;
9169*4882a593Smuzhiyun
9170*4882a593Smuzhiyun if (!skb->data_len)
9171*4882a593Smuzhiyun goto done;
9172*4882a593Smuzhiyun
9173*4882a593Smuzhiyun /* Don't forget the fragments. */
9174*4882a593Smuzhiyun skb_walk_frags(skb, frag)
9175*4882a593Smuzhiyun sctp_skb_set_owner_r_frag(frag, sk);
9176*4882a593Smuzhiyun
9177*4882a593Smuzhiyun done:
9178*4882a593Smuzhiyun sctp_skb_set_owner_r(skb, sk);
9179*4882a593Smuzhiyun }
9180*4882a593Smuzhiyun
sctp_copy_sock(struct sock * newsk,struct sock * sk,struct sctp_association * asoc)9181*4882a593Smuzhiyun void sctp_copy_sock(struct sock *newsk, struct sock *sk,
9182*4882a593Smuzhiyun struct sctp_association *asoc)
9183*4882a593Smuzhiyun {
9184*4882a593Smuzhiyun struct inet_sock *inet = inet_sk(sk);
9185*4882a593Smuzhiyun struct inet_sock *newinet;
9186*4882a593Smuzhiyun struct sctp_sock *sp = sctp_sk(sk);
9187*4882a593Smuzhiyun struct sctp_endpoint *ep = sp->ep;
9188*4882a593Smuzhiyun
9189*4882a593Smuzhiyun newsk->sk_type = sk->sk_type;
9190*4882a593Smuzhiyun newsk->sk_bound_dev_if = sk->sk_bound_dev_if;
9191*4882a593Smuzhiyun newsk->sk_flags = sk->sk_flags;
9192*4882a593Smuzhiyun newsk->sk_tsflags = sk->sk_tsflags;
9193*4882a593Smuzhiyun newsk->sk_no_check_tx = sk->sk_no_check_tx;
9194*4882a593Smuzhiyun newsk->sk_no_check_rx = sk->sk_no_check_rx;
9195*4882a593Smuzhiyun newsk->sk_reuse = sk->sk_reuse;
9196*4882a593Smuzhiyun sctp_sk(newsk)->reuse = sp->reuse;
9197*4882a593Smuzhiyun
9198*4882a593Smuzhiyun newsk->sk_shutdown = sk->sk_shutdown;
9199*4882a593Smuzhiyun newsk->sk_destruct = sctp_destruct_sock;
9200*4882a593Smuzhiyun newsk->sk_family = sk->sk_family;
9201*4882a593Smuzhiyun newsk->sk_protocol = IPPROTO_SCTP;
9202*4882a593Smuzhiyun newsk->sk_backlog_rcv = sk->sk_prot->backlog_rcv;
9203*4882a593Smuzhiyun newsk->sk_sndbuf = sk->sk_sndbuf;
9204*4882a593Smuzhiyun newsk->sk_rcvbuf = sk->sk_rcvbuf;
9205*4882a593Smuzhiyun newsk->sk_lingertime = sk->sk_lingertime;
9206*4882a593Smuzhiyun newsk->sk_rcvtimeo = sk->sk_rcvtimeo;
9207*4882a593Smuzhiyun newsk->sk_sndtimeo = sk->sk_sndtimeo;
9208*4882a593Smuzhiyun newsk->sk_rxhash = sk->sk_rxhash;
9209*4882a593Smuzhiyun
9210*4882a593Smuzhiyun newinet = inet_sk(newsk);
9211*4882a593Smuzhiyun
9212*4882a593Smuzhiyun /* Initialize sk's sport, dport, rcv_saddr and daddr for
9213*4882a593Smuzhiyun * getsockname() and getpeername()
9214*4882a593Smuzhiyun */
9215*4882a593Smuzhiyun newinet->inet_sport = inet->inet_sport;
9216*4882a593Smuzhiyun newinet->inet_saddr = inet->inet_saddr;
9217*4882a593Smuzhiyun newinet->inet_rcv_saddr = inet->inet_rcv_saddr;
9218*4882a593Smuzhiyun newinet->inet_dport = htons(asoc->peer.port);
9219*4882a593Smuzhiyun newinet->pmtudisc = inet->pmtudisc;
9220*4882a593Smuzhiyun newinet->inet_id = prandom_u32();
9221*4882a593Smuzhiyun
9222*4882a593Smuzhiyun newinet->uc_ttl = inet->uc_ttl;
9223*4882a593Smuzhiyun newinet->mc_loop = 1;
9224*4882a593Smuzhiyun newinet->mc_ttl = 1;
9225*4882a593Smuzhiyun newinet->mc_index = 0;
9226*4882a593Smuzhiyun newinet->mc_list = NULL;
9227*4882a593Smuzhiyun
9228*4882a593Smuzhiyun if (newsk->sk_flags & SK_FLAGS_TIMESTAMP)
9229*4882a593Smuzhiyun net_enable_timestamp();
9230*4882a593Smuzhiyun
9231*4882a593Smuzhiyun /* Set newsk security attributes from orginal sk and connection
9232*4882a593Smuzhiyun * security attribute from ep.
9233*4882a593Smuzhiyun */
9234*4882a593Smuzhiyun security_sctp_sk_clone(ep, sk, newsk);
9235*4882a593Smuzhiyun }
9236*4882a593Smuzhiyun
sctp_copy_descendant(struct sock * sk_to,const struct sock * sk_from)9237*4882a593Smuzhiyun static inline void sctp_copy_descendant(struct sock *sk_to,
9238*4882a593Smuzhiyun const struct sock *sk_from)
9239*4882a593Smuzhiyun {
9240*4882a593Smuzhiyun size_t ancestor_size = sizeof(struct inet_sock);
9241*4882a593Smuzhiyun
9242*4882a593Smuzhiyun ancestor_size += sk_from->sk_prot->obj_size;
9243*4882a593Smuzhiyun ancestor_size -= offsetof(struct sctp_sock, pd_lobby);
9244*4882a593Smuzhiyun __inet_sk_copy_descendant(sk_to, sk_from, ancestor_size);
9245*4882a593Smuzhiyun }
9246*4882a593Smuzhiyun
9247*4882a593Smuzhiyun /* Populate the fields of the newsk from the oldsk and migrate the assoc
9248*4882a593Smuzhiyun * and its messages to the newsk.
9249*4882a593Smuzhiyun */
sctp_sock_migrate(struct sock * oldsk,struct sock * newsk,struct sctp_association * assoc,enum sctp_socket_type type)9250*4882a593Smuzhiyun static int sctp_sock_migrate(struct sock *oldsk, struct sock *newsk,
9251*4882a593Smuzhiyun struct sctp_association *assoc,
9252*4882a593Smuzhiyun enum sctp_socket_type type)
9253*4882a593Smuzhiyun {
9254*4882a593Smuzhiyun struct sctp_sock *oldsp = sctp_sk(oldsk);
9255*4882a593Smuzhiyun struct sctp_sock *newsp = sctp_sk(newsk);
9256*4882a593Smuzhiyun struct sctp_bind_bucket *pp; /* hash list port iterator */
9257*4882a593Smuzhiyun struct sctp_endpoint *newep = newsp->ep;
9258*4882a593Smuzhiyun struct sk_buff *skb, *tmp;
9259*4882a593Smuzhiyun struct sctp_ulpevent *event;
9260*4882a593Smuzhiyun struct sctp_bind_hashbucket *head;
9261*4882a593Smuzhiyun int err;
9262*4882a593Smuzhiyun
9263*4882a593Smuzhiyun /* Migrate socket buffer sizes and all the socket level options to the
9264*4882a593Smuzhiyun * new socket.
9265*4882a593Smuzhiyun */
9266*4882a593Smuzhiyun newsk->sk_sndbuf = oldsk->sk_sndbuf;
9267*4882a593Smuzhiyun newsk->sk_rcvbuf = oldsk->sk_rcvbuf;
9268*4882a593Smuzhiyun /* Brute force copy old sctp opt. */
9269*4882a593Smuzhiyun sctp_copy_descendant(newsk, oldsk);
9270*4882a593Smuzhiyun
9271*4882a593Smuzhiyun /* Restore the ep value that was overwritten with the above structure
9272*4882a593Smuzhiyun * copy.
9273*4882a593Smuzhiyun */
9274*4882a593Smuzhiyun newsp->ep = newep;
9275*4882a593Smuzhiyun newsp->hmac = NULL;
9276*4882a593Smuzhiyun
9277*4882a593Smuzhiyun /* Hook this new socket in to the bind_hash list. */
9278*4882a593Smuzhiyun head = &sctp_port_hashtable[sctp_phashfn(sock_net(oldsk),
9279*4882a593Smuzhiyun inet_sk(oldsk)->inet_num)];
9280*4882a593Smuzhiyun spin_lock_bh(&head->lock);
9281*4882a593Smuzhiyun pp = sctp_sk(oldsk)->bind_hash;
9282*4882a593Smuzhiyun sk_add_bind_node(newsk, &pp->owner);
9283*4882a593Smuzhiyun sctp_sk(newsk)->bind_hash = pp;
9284*4882a593Smuzhiyun inet_sk(newsk)->inet_num = inet_sk(oldsk)->inet_num;
9285*4882a593Smuzhiyun spin_unlock_bh(&head->lock);
9286*4882a593Smuzhiyun
9287*4882a593Smuzhiyun /* Copy the bind_addr list from the original endpoint to the new
9288*4882a593Smuzhiyun * endpoint so that we can handle restarts properly
9289*4882a593Smuzhiyun */
9290*4882a593Smuzhiyun err = sctp_bind_addr_dup(&newsp->ep->base.bind_addr,
9291*4882a593Smuzhiyun &oldsp->ep->base.bind_addr, GFP_KERNEL);
9292*4882a593Smuzhiyun if (err)
9293*4882a593Smuzhiyun return err;
9294*4882a593Smuzhiyun
9295*4882a593Smuzhiyun /* New ep's auth_hmacs should be set if old ep's is set, in case
9296*4882a593Smuzhiyun * that net->sctp.auth_enable has been changed to 0 by users and
9297*4882a593Smuzhiyun * new ep's auth_hmacs couldn't be set in sctp_endpoint_init().
9298*4882a593Smuzhiyun */
9299*4882a593Smuzhiyun if (oldsp->ep->auth_hmacs) {
9300*4882a593Smuzhiyun err = sctp_auth_init_hmacs(newsp->ep, GFP_KERNEL);
9301*4882a593Smuzhiyun if (err)
9302*4882a593Smuzhiyun return err;
9303*4882a593Smuzhiyun }
9304*4882a593Smuzhiyun
9305*4882a593Smuzhiyun sctp_auto_asconf_init(newsp);
9306*4882a593Smuzhiyun
9307*4882a593Smuzhiyun /* Move any messages in the old socket's receive queue that are for the
9308*4882a593Smuzhiyun * peeled off association to the new socket's receive queue.
9309*4882a593Smuzhiyun */
9310*4882a593Smuzhiyun sctp_skb_for_each(skb, &oldsk->sk_receive_queue, tmp) {
9311*4882a593Smuzhiyun event = sctp_skb2event(skb);
9312*4882a593Smuzhiyun if (event->asoc == assoc) {
9313*4882a593Smuzhiyun __skb_unlink(skb, &oldsk->sk_receive_queue);
9314*4882a593Smuzhiyun __skb_queue_tail(&newsk->sk_receive_queue, skb);
9315*4882a593Smuzhiyun sctp_skb_set_owner_r_frag(skb, newsk);
9316*4882a593Smuzhiyun }
9317*4882a593Smuzhiyun }
9318*4882a593Smuzhiyun
9319*4882a593Smuzhiyun /* Clean up any messages pending delivery due to partial
9320*4882a593Smuzhiyun * delivery. Three cases:
9321*4882a593Smuzhiyun * 1) No partial deliver; no work.
9322*4882a593Smuzhiyun * 2) Peeling off partial delivery; keep pd_lobby in new pd_lobby.
9323*4882a593Smuzhiyun * 3) Peeling off non-partial delivery; move pd_lobby to receive_queue.
9324*4882a593Smuzhiyun */
9325*4882a593Smuzhiyun atomic_set(&sctp_sk(newsk)->pd_mode, assoc->ulpq.pd_mode);
9326*4882a593Smuzhiyun
9327*4882a593Smuzhiyun if (atomic_read(&sctp_sk(oldsk)->pd_mode)) {
9328*4882a593Smuzhiyun struct sk_buff_head *queue;
9329*4882a593Smuzhiyun
9330*4882a593Smuzhiyun /* Decide which queue to move pd_lobby skbs to. */
9331*4882a593Smuzhiyun if (assoc->ulpq.pd_mode) {
9332*4882a593Smuzhiyun queue = &newsp->pd_lobby;
9333*4882a593Smuzhiyun } else
9334*4882a593Smuzhiyun queue = &newsk->sk_receive_queue;
9335*4882a593Smuzhiyun
9336*4882a593Smuzhiyun /* Walk through the pd_lobby, looking for skbs that
9337*4882a593Smuzhiyun * need moved to the new socket.
9338*4882a593Smuzhiyun */
9339*4882a593Smuzhiyun sctp_skb_for_each(skb, &oldsp->pd_lobby, tmp) {
9340*4882a593Smuzhiyun event = sctp_skb2event(skb);
9341*4882a593Smuzhiyun if (event->asoc == assoc) {
9342*4882a593Smuzhiyun __skb_unlink(skb, &oldsp->pd_lobby);
9343*4882a593Smuzhiyun __skb_queue_tail(queue, skb);
9344*4882a593Smuzhiyun sctp_skb_set_owner_r_frag(skb, newsk);
9345*4882a593Smuzhiyun }
9346*4882a593Smuzhiyun }
9347*4882a593Smuzhiyun
9348*4882a593Smuzhiyun /* Clear up any skbs waiting for the partial
9349*4882a593Smuzhiyun * delivery to finish.
9350*4882a593Smuzhiyun */
9351*4882a593Smuzhiyun if (assoc->ulpq.pd_mode)
9352*4882a593Smuzhiyun sctp_clear_pd(oldsk, NULL);
9353*4882a593Smuzhiyun
9354*4882a593Smuzhiyun }
9355*4882a593Smuzhiyun
9356*4882a593Smuzhiyun sctp_for_each_rx_skb(assoc, newsk, sctp_skb_set_owner_r_frag);
9357*4882a593Smuzhiyun
9358*4882a593Smuzhiyun /* Set the type of socket to indicate that it is peeled off from the
9359*4882a593Smuzhiyun * original UDP-style socket or created with the accept() call on a
9360*4882a593Smuzhiyun * TCP-style socket..
9361*4882a593Smuzhiyun */
9362*4882a593Smuzhiyun newsp->type = type;
9363*4882a593Smuzhiyun
9364*4882a593Smuzhiyun /* Mark the new socket "in-use" by the user so that any packets
9365*4882a593Smuzhiyun * that may arrive on the association after we've moved it are
9366*4882a593Smuzhiyun * queued to the backlog. This prevents a potential race between
9367*4882a593Smuzhiyun * backlog processing on the old socket and new-packet processing
9368*4882a593Smuzhiyun * on the new socket.
9369*4882a593Smuzhiyun *
9370*4882a593Smuzhiyun * The caller has just allocated newsk so we can guarantee that other
9371*4882a593Smuzhiyun * paths won't try to lock it and then oldsk.
9372*4882a593Smuzhiyun */
9373*4882a593Smuzhiyun lock_sock_nested(newsk, SINGLE_DEPTH_NESTING);
9374*4882a593Smuzhiyun sctp_for_each_tx_datachunk(assoc, true, sctp_clear_owner_w);
9375*4882a593Smuzhiyun sctp_assoc_migrate(assoc, newsk);
9376*4882a593Smuzhiyun sctp_for_each_tx_datachunk(assoc, false, sctp_set_owner_w);
9377*4882a593Smuzhiyun
9378*4882a593Smuzhiyun /* If the association on the newsk is already closed before accept()
9379*4882a593Smuzhiyun * is called, set RCV_SHUTDOWN flag.
9380*4882a593Smuzhiyun */
9381*4882a593Smuzhiyun if (sctp_state(assoc, CLOSED) && sctp_style(newsk, TCP)) {
9382*4882a593Smuzhiyun inet_sk_set_state(newsk, SCTP_SS_CLOSED);
9383*4882a593Smuzhiyun newsk->sk_shutdown |= RCV_SHUTDOWN;
9384*4882a593Smuzhiyun } else {
9385*4882a593Smuzhiyun inet_sk_set_state(newsk, SCTP_SS_ESTABLISHED);
9386*4882a593Smuzhiyun }
9387*4882a593Smuzhiyun
9388*4882a593Smuzhiyun release_sock(newsk);
9389*4882a593Smuzhiyun
9390*4882a593Smuzhiyun return 0;
9391*4882a593Smuzhiyun }
9392*4882a593Smuzhiyun
9393*4882a593Smuzhiyun
9394*4882a593Smuzhiyun /* This proto struct describes the ULP interface for SCTP. */
9395*4882a593Smuzhiyun struct proto sctp_prot = {
9396*4882a593Smuzhiyun .name = "SCTP",
9397*4882a593Smuzhiyun .owner = THIS_MODULE,
9398*4882a593Smuzhiyun .close = sctp_close,
9399*4882a593Smuzhiyun .disconnect = sctp_disconnect,
9400*4882a593Smuzhiyun .accept = sctp_accept,
9401*4882a593Smuzhiyun .ioctl = sctp_ioctl,
9402*4882a593Smuzhiyun .init = sctp_init_sock,
9403*4882a593Smuzhiyun .destroy = sctp_destroy_sock,
9404*4882a593Smuzhiyun .shutdown = sctp_shutdown,
9405*4882a593Smuzhiyun .setsockopt = sctp_setsockopt,
9406*4882a593Smuzhiyun .getsockopt = sctp_getsockopt,
9407*4882a593Smuzhiyun .sendmsg = sctp_sendmsg,
9408*4882a593Smuzhiyun .recvmsg = sctp_recvmsg,
9409*4882a593Smuzhiyun .bind = sctp_bind,
9410*4882a593Smuzhiyun .bind_add = sctp_bind_add,
9411*4882a593Smuzhiyun .backlog_rcv = sctp_backlog_rcv,
9412*4882a593Smuzhiyun .hash = sctp_hash,
9413*4882a593Smuzhiyun .unhash = sctp_unhash,
9414*4882a593Smuzhiyun .no_autobind = true,
9415*4882a593Smuzhiyun .obj_size = sizeof(struct sctp_sock),
9416*4882a593Smuzhiyun .useroffset = offsetof(struct sctp_sock, subscribe),
9417*4882a593Smuzhiyun .usersize = offsetof(struct sctp_sock, initmsg) -
9418*4882a593Smuzhiyun offsetof(struct sctp_sock, subscribe) +
9419*4882a593Smuzhiyun sizeof_field(struct sctp_sock, initmsg),
9420*4882a593Smuzhiyun .sysctl_mem = sysctl_sctp_mem,
9421*4882a593Smuzhiyun .sysctl_rmem = sysctl_sctp_rmem,
9422*4882a593Smuzhiyun .sysctl_wmem = sysctl_sctp_wmem,
9423*4882a593Smuzhiyun .memory_pressure = &sctp_memory_pressure,
9424*4882a593Smuzhiyun .enter_memory_pressure = sctp_enter_memory_pressure,
9425*4882a593Smuzhiyun .memory_allocated = &sctp_memory_allocated,
9426*4882a593Smuzhiyun .sockets_allocated = &sctp_sockets_allocated,
9427*4882a593Smuzhiyun };
9428*4882a593Smuzhiyun
9429*4882a593Smuzhiyun #if IS_ENABLED(CONFIG_IPV6)
9430*4882a593Smuzhiyun
9431*4882a593Smuzhiyun #include <net/transp_v6.h>
sctp_v6_destroy_sock(struct sock * sk)9432*4882a593Smuzhiyun static void sctp_v6_destroy_sock(struct sock *sk)
9433*4882a593Smuzhiyun {
9434*4882a593Smuzhiyun sctp_destroy_sock(sk);
9435*4882a593Smuzhiyun inet6_destroy_sock(sk);
9436*4882a593Smuzhiyun }
9437*4882a593Smuzhiyun
9438*4882a593Smuzhiyun struct proto sctpv6_prot = {
9439*4882a593Smuzhiyun .name = "SCTPv6",
9440*4882a593Smuzhiyun .owner = THIS_MODULE,
9441*4882a593Smuzhiyun .close = sctp_close,
9442*4882a593Smuzhiyun .disconnect = sctp_disconnect,
9443*4882a593Smuzhiyun .accept = sctp_accept,
9444*4882a593Smuzhiyun .ioctl = sctp_ioctl,
9445*4882a593Smuzhiyun .init = sctp_init_sock,
9446*4882a593Smuzhiyun .destroy = sctp_v6_destroy_sock,
9447*4882a593Smuzhiyun .shutdown = sctp_shutdown,
9448*4882a593Smuzhiyun .setsockopt = sctp_setsockopt,
9449*4882a593Smuzhiyun .getsockopt = sctp_getsockopt,
9450*4882a593Smuzhiyun .sendmsg = sctp_sendmsg,
9451*4882a593Smuzhiyun .recvmsg = sctp_recvmsg,
9452*4882a593Smuzhiyun .bind = sctp_bind,
9453*4882a593Smuzhiyun .bind_add = sctp_bind_add,
9454*4882a593Smuzhiyun .backlog_rcv = sctp_backlog_rcv,
9455*4882a593Smuzhiyun .hash = sctp_hash,
9456*4882a593Smuzhiyun .unhash = sctp_unhash,
9457*4882a593Smuzhiyun .no_autobind = true,
9458*4882a593Smuzhiyun .obj_size = sizeof(struct sctp6_sock),
9459*4882a593Smuzhiyun .useroffset = offsetof(struct sctp6_sock, sctp.subscribe),
9460*4882a593Smuzhiyun .usersize = offsetof(struct sctp6_sock, sctp.initmsg) -
9461*4882a593Smuzhiyun offsetof(struct sctp6_sock, sctp.subscribe) +
9462*4882a593Smuzhiyun sizeof_field(struct sctp6_sock, sctp.initmsg),
9463*4882a593Smuzhiyun .sysctl_mem = sysctl_sctp_mem,
9464*4882a593Smuzhiyun .sysctl_rmem = sysctl_sctp_rmem,
9465*4882a593Smuzhiyun .sysctl_wmem = sysctl_sctp_wmem,
9466*4882a593Smuzhiyun .memory_pressure = &sctp_memory_pressure,
9467*4882a593Smuzhiyun .enter_memory_pressure = sctp_enter_memory_pressure,
9468*4882a593Smuzhiyun .memory_allocated = &sctp_memory_allocated,
9469*4882a593Smuzhiyun .sockets_allocated = &sctp_sockets_allocated,
9470*4882a593Smuzhiyun };
9471*4882a593Smuzhiyun #endif /* IS_ENABLED(CONFIG_IPV6) */
9472