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