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
39
40
41
42#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
43
44#include <linux/module.h>
45#include <linux/init.h>
46#include <linux/netdevice.h>
47#include <linux/inetdevice.h>
48#include <linux/seq_file.h>
49#include <linux/memblock.h>
50#include <linux/highmem.h>
51#include <linux/swap.h>
52#include <linux/slab.h>
53#include <net/net_namespace.h>
54#include <net/protocol.h>
55#include <net/ip.h>
56#include <net/ipv6.h>
57#include <net/route.h>
58#include <net/sctp/sctp.h>
59#include <net/addrconf.h>
60#include <net/inet_common.h>
61#include <net/inet_ecn.h>
62#include <net/udp_tunnel.h>
63
64#define MAX_SCTP_PORT_HASH_ENTRIES (64 * 1024)
65
66
67struct sctp_globals sctp_globals __read_mostly;
68
69struct idr sctp_assocs_id;
70DEFINE_SPINLOCK(sctp_assocs_id_lock);
71
72static struct sctp_pf *sctp_pf_inet6_specific;
73static struct sctp_pf *sctp_pf_inet_specific;
74static struct sctp_af *sctp_af_v4_specific;
75static struct sctp_af *sctp_af_v6_specific;
76
77struct kmem_cache *sctp_chunk_cachep __read_mostly;
78struct kmem_cache *sctp_bucket_cachep __read_mostly;
79
80long sysctl_sctp_mem[3];
81int sysctl_sctp_rmem[3];
82int sysctl_sctp_wmem[3];
83
84
85
86
87static void sctp_v4_copy_addrlist(struct list_head *addrlist,
88 struct net_device *dev)
89{
90 struct in_device *in_dev;
91 struct in_ifaddr *ifa;
92 struct sctp_sockaddr_entry *addr;
93
94 rcu_read_lock();
95 if ((in_dev = __in_dev_get_rcu(dev)) == NULL) {
96 rcu_read_unlock();
97 return;
98 }
99
100 in_dev_for_each_ifa_rcu(ifa, in_dev) {
101
102 addr = kzalloc(sizeof(*addr), GFP_ATOMIC);
103 if (addr) {
104 addr->a.v4.sin_family = AF_INET;
105 addr->a.v4.sin_addr.s_addr = ifa->ifa_local;
106 addr->valid = 1;
107 INIT_LIST_HEAD(&addr->list);
108 list_add_tail(&addr->list, addrlist);
109 }
110 }
111
112 rcu_read_unlock();
113}
114
115
116
117
118static void sctp_get_local_addr_list(struct net *net)
119{
120 struct net_device *dev;
121 struct list_head *pos;
122 struct sctp_af *af;
123
124 rcu_read_lock();
125 for_each_netdev_rcu(net, dev) {
126 list_for_each(pos, &sctp_address_families) {
127 af = list_entry(pos, struct sctp_af, list);
128 af->copy_addrlist(&net->sctp.local_addr_list, dev);
129 }
130 }
131 rcu_read_unlock();
132}
133
134
135static void sctp_free_local_addr_list(struct net *net)
136{
137 struct sctp_sockaddr_entry *addr;
138 struct list_head *pos, *temp;
139
140 list_for_each_safe(pos, temp, &net->sctp.local_addr_list) {
141 addr = list_entry(pos, struct sctp_sockaddr_entry, list);
142 list_del(pos);
143 kfree(addr);
144 }
145}
146
147
148int sctp_copy_local_addr_list(struct net *net, struct sctp_bind_addr *bp,
149 enum sctp_scope scope, gfp_t gfp, int copy_flags)
150{
151 struct sctp_sockaddr_entry *addr;
152 union sctp_addr laddr;
153 int error = 0;
154
155 rcu_read_lock();
156 list_for_each_entry_rcu(addr, &net->sctp.local_addr_list, list) {
157 if (!addr->valid)
158 continue;
159 if (!sctp_in_scope(net, &addr->a, scope))
160 continue;
161
162
163
164
165
166 if (addr->a.sa.sa_family == AF_INET &&
167 (!(copy_flags & SCTP_ADDR4_ALLOWED) ||
168 !(copy_flags & SCTP_ADDR4_PEERSUPP)))
169 continue;
170 if (addr->a.sa.sa_family == AF_INET6 &&
171 (!(copy_flags & SCTP_ADDR6_ALLOWED) ||
172 !(copy_flags & SCTP_ADDR6_PEERSUPP)))
173 continue;
174
175 laddr = addr->a;
176
177 laddr.v4.sin_port = htons(bp->port);
178 if (sctp_bind_addr_state(bp, &laddr) != -1)
179 continue;
180
181 error = sctp_add_bind_addr(bp, &addr->a, sizeof(addr->a),
182 SCTP_ADDR_SRC, GFP_ATOMIC);
183 if (error)
184 break;
185 }
186
187 rcu_read_unlock();
188 return error;
189}
190
191
192static void sctp_v4_copy_ip_options(struct sock *sk, struct sock *newsk)
193{
194 struct inet_sock *newinet, *inet = inet_sk(sk);
195 struct ip_options_rcu *inet_opt, *newopt = NULL;
196
197 newinet = inet_sk(newsk);
198
199 rcu_read_lock();
200 inet_opt = rcu_dereference(inet->inet_opt);
201 if (inet_opt) {
202 newopt = sock_kmalloc(newsk, sizeof(*inet_opt) +
203 inet_opt->opt.optlen, GFP_ATOMIC);
204 if (newopt)
205 memcpy(newopt, inet_opt, sizeof(*inet_opt) +
206 inet_opt->opt.optlen);
207 else
208 pr_err("%s: Failed to copy ip options\n", __func__);
209 }
210 RCU_INIT_POINTER(newinet->inet_opt, newopt);
211 rcu_read_unlock();
212}
213
214
215static int sctp_v4_ip_options_len(struct sock *sk)
216{
217 struct inet_sock *inet = inet_sk(sk);
218 struct ip_options_rcu *inet_opt;
219 int len = 0;
220
221 rcu_read_lock();
222 inet_opt = rcu_dereference(inet->inet_opt);
223 if (inet_opt)
224 len = inet_opt->opt.optlen;
225
226 rcu_read_unlock();
227 return len;
228}
229
230
231static void sctp_v4_from_skb(union sctp_addr *addr, struct sk_buff *skb,
232 int is_saddr)
233{
234
235 struct sctphdr *sh = sctp_hdr(skb);
236 struct sockaddr_in *sa = &addr->v4;
237
238 addr->v4.sin_family = AF_INET;
239
240 if (is_saddr) {
241 sa->sin_port = sh->source;
242 sa->sin_addr.s_addr = ip_hdr(skb)->saddr;
243 } else {
244 sa->sin_port = sh->dest;
245 sa->sin_addr.s_addr = ip_hdr(skb)->daddr;
246 }
247 memset(sa->sin_zero, 0, sizeof(sa->sin_zero));
248}
249
250
251static void sctp_v4_from_sk(union sctp_addr *addr, struct sock *sk)
252{
253 addr->v4.sin_family = AF_INET;
254 addr->v4.sin_port = 0;
255 addr->v4.sin_addr.s_addr = inet_sk(sk)->inet_rcv_saddr;
256 memset(addr->v4.sin_zero, 0, sizeof(addr->v4.sin_zero));
257}
258
259
260static void sctp_v4_to_sk_saddr(union sctp_addr *addr, struct sock *sk)
261{
262 inet_sk(sk)->inet_rcv_saddr = addr->v4.sin_addr.s_addr;
263}
264
265
266static void sctp_v4_to_sk_daddr(union sctp_addr *addr, struct sock *sk)
267{
268 inet_sk(sk)->inet_daddr = addr->v4.sin_addr.s_addr;
269}
270
271
272static bool sctp_v4_from_addr_param(union sctp_addr *addr,
273 union sctp_addr_param *param,
274 __be16 port, int iif)
275{
276 if (ntohs(param->v4.param_hdr.length) < sizeof(struct sctp_ipv4addr_param))
277 return false;
278
279 addr->v4.sin_family = AF_INET;
280 addr->v4.sin_port = port;
281 addr->v4.sin_addr.s_addr = param->v4.addr.s_addr;
282 memset(addr->v4.sin_zero, 0, sizeof(addr->v4.sin_zero));
283
284 return true;
285}
286
287
288
289
290static int sctp_v4_to_addr_param(const union sctp_addr *addr,
291 union sctp_addr_param *param)
292{
293 int length = sizeof(struct sctp_ipv4addr_param);
294
295 param->v4.param_hdr.type = SCTP_PARAM_IPV4_ADDRESS;
296 param->v4.param_hdr.length = htons(length);
297 param->v4.addr.s_addr = addr->v4.sin_addr.s_addr;
298
299 return length;
300}
301
302
303static void sctp_v4_dst_saddr(union sctp_addr *saddr, struct flowi4 *fl4,
304 __be16 port)
305{
306 saddr->v4.sin_family = AF_INET;
307 saddr->v4.sin_port = port;
308 saddr->v4.sin_addr.s_addr = fl4->saddr;
309 memset(saddr->v4.sin_zero, 0, sizeof(saddr->v4.sin_zero));
310}
311
312
313static int sctp_v4_cmp_addr(const union sctp_addr *addr1,
314 const union sctp_addr *addr2)
315{
316 if (addr1->sa.sa_family != addr2->sa.sa_family)
317 return 0;
318 if (addr1->v4.sin_port != addr2->v4.sin_port)
319 return 0;
320 if (addr1->v4.sin_addr.s_addr != addr2->v4.sin_addr.s_addr)
321 return 0;
322
323 return 1;
324}
325
326
327static void sctp_v4_inaddr_any(union sctp_addr *addr, __be16 port)
328{
329 addr->v4.sin_family = AF_INET;
330 addr->v4.sin_addr.s_addr = htonl(INADDR_ANY);
331 addr->v4.sin_port = port;
332 memset(addr->v4.sin_zero, 0, sizeof(addr->v4.sin_zero));
333}
334
335
336static int sctp_v4_is_any(const union sctp_addr *addr)
337{
338 return htonl(INADDR_ANY) == addr->v4.sin_addr.s_addr;
339}
340
341
342
343
344
345
346
347
348static int sctp_v4_addr_valid(union sctp_addr *addr,
349 struct sctp_sock *sp,
350 const struct sk_buff *skb)
351{
352
353 if (sp && ipv6_only_sock(sctp_opt2sk(sp)))
354 return 0;
355
356
357 if (IS_IPV4_UNUSABLE_ADDRESS(addr->v4.sin_addr.s_addr))
358 return 0;
359
360
361 if (skb && skb_rtable(skb)->rt_flags & RTCF_BROADCAST)
362 return 0;
363
364 return 1;
365}
366
367
368static int sctp_v4_available(union sctp_addr *addr, struct sctp_sock *sp)
369{
370 struct net *net = sock_net(&sp->inet.sk);
371 int ret = inet_addr_type(net, addr->v4.sin_addr.s_addr);
372
373
374 if (addr->v4.sin_addr.s_addr != htonl(INADDR_ANY) &&
375 ret != RTN_LOCAL &&
376 !sp->inet.freebind &&
377 !net->ipv4.sysctl_ip_nonlocal_bind)
378 return 0;
379
380 if (ipv6_only_sock(sctp_opt2sk(sp)))
381 return 0;
382
383 return 1;
384}
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403static enum sctp_scope sctp_v4_scope(union sctp_addr *addr)
404{
405 enum sctp_scope retval;
406
407
408 if (IS_IPV4_UNUSABLE_ADDRESS(addr->v4.sin_addr.s_addr)) {
409 retval = SCTP_SCOPE_UNUSABLE;
410 } else if (ipv4_is_loopback(addr->v4.sin_addr.s_addr)) {
411 retval = SCTP_SCOPE_LOOPBACK;
412 } else if (ipv4_is_linklocal_169(addr->v4.sin_addr.s_addr)) {
413 retval = SCTP_SCOPE_LINK;
414 } else if (ipv4_is_private_10(addr->v4.sin_addr.s_addr) ||
415 ipv4_is_private_172(addr->v4.sin_addr.s_addr) ||
416 ipv4_is_private_192(addr->v4.sin_addr.s_addr) ||
417 ipv4_is_test_198(addr->v4.sin_addr.s_addr)) {
418 retval = SCTP_SCOPE_PRIVATE;
419 } else {
420 retval = SCTP_SCOPE_GLOBAL;
421 }
422
423 return retval;
424}
425
426
427
428
429
430static void sctp_v4_get_dst(struct sctp_transport *t, union sctp_addr *saddr,
431 struct flowi *fl, struct sock *sk)
432{
433 struct sctp_association *asoc = t->asoc;
434 struct rtable *rt;
435 struct flowi _fl;
436 struct flowi4 *fl4 = &_fl.u.ip4;
437 struct sctp_bind_addr *bp;
438 struct sctp_sockaddr_entry *laddr;
439 struct dst_entry *dst = NULL;
440 union sctp_addr *daddr = &t->ipaddr;
441 union sctp_addr dst_saddr;
442 __u8 tos = inet_sk(sk)->tos;
443
444 if (t->dscp & SCTP_DSCP_SET_MASK)
445 tos = t->dscp & SCTP_DSCP_VAL_MASK;
446 memset(&_fl, 0x0, sizeof(_fl));
447 fl4->daddr = daddr->v4.sin_addr.s_addr;
448 fl4->fl4_dport = daddr->v4.sin_port;
449 fl4->flowi4_proto = IPPROTO_SCTP;
450 if (asoc) {
451 fl4->flowi4_tos = RT_CONN_FLAGS_TOS(asoc->base.sk, tos);
452 fl4->flowi4_oif = asoc->base.sk->sk_bound_dev_if;
453 fl4->fl4_sport = htons(asoc->base.bind_addr.port);
454 }
455 if (saddr) {
456 fl4->saddr = saddr->v4.sin_addr.s_addr;
457 if (!fl4->fl4_sport)
458 fl4->fl4_sport = saddr->v4.sin_port;
459 }
460
461 pr_debug("%s: dst:%pI4, src:%pI4 - ", __func__, &fl4->daddr,
462 &fl4->saddr);
463
464 rt = ip_route_output_key(sock_net(sk), fl4);
465 if (!IS_ERR(rt)) {
466 dst = &rt->dst;
467 t->dst = dst;
468 memcpy(fl, &_fl, sizeof(_fl));
469 }
470
471
472
473
474 if (!asoc || saddr)
475 goto out;
476
477 bp = &asoc->base.bind_addr;
478
479 if (dst) {
480
481
482
483 sctp_v4_dst_saddr(&dst_saddr, fl4, htons(bp->port));
484 rcu_read_lock();
485 list_for_each_entry_rcu(laddr, &bp->address_list, list) {
486 if (!laddr->valid || (laddr->state == SCTP_ADDR_DEL) ||
487 (laddr->state != SCTP_ADDR_SRC &&
488 !asoc->src_out_of_asoc_ok))
489 continue;
490 if (sctp_v4_cmp_addr(&dst_saddr, &laddr->a))
491 goto out_unlock;
492 }
493 rcu_read_unlock();
494
495
496
497
498 dst_release(dst);
499 dst = NULL;
500 }
501
502
503
504
505 rcu_read_lock();
506 list_for_each_entry_rcu(laddr, &bp->address_list, list) {
507 struct net_device *odev;
508
509 if (!laddr->valid)
510 continue;
511 if (laddr->state != SCTP_ADDR_SRC ||
512 AF_INET != laddr->a.sa.sa_family)
513 continue;
514
515 fl4->fl4_sport = laddr->a.v4.sin_port;
516 flowi4_update_output(fl4,
517 asoc->base.sk->sk_bound_dev_if,
518 RT_CONN_FLAGS_TOS(asoc->base.sk, tos),
519 daddr->v4.sin_addr.s_addr,
520 laddr->a.v4.sin_addr.s_addr);
521
522 rt = ip_route_output_key(sock_net(sk), fl4);
523 if (IS_ERR(rt))
524 continue;
525
526
527
528
529 odev = __ip_dev_find(sock_net(sk), laddr->a.v4.sin_addr.s_addr,
530 false);
531 if (!odev || odev->ifindex != fl4->flowi4_oif) {
532 if (!dst) {
533 dst = &rt->dst;
534 t->dst = dst;
535 memcpy(fl, &_fl, sizeof(_fl));
536 } else {
537 dst_release(&rt->dst);
538 }
539 continue;
540 }
541
542 dst_release(dst);
543 dst = &rt->dst;
544 t->dst = dst;
545 memcpy(fl, &_fl, sizeof(_fl));
546 break;
547 }
548
549out_unlock:
550 rcu_read_unlock();
551out:
552 if (dst) {
553 pr_debug("rt_dst:%pI4, rt_src:%pI4\n",
554 &fl->u.ip4.daddr, &fl->u.ip4.saddr);
555 } else {
556 t->dst = NULL;
557 pr_debug("no route\n");
558 }
559}
560
561
562
563
564static void sctp_v4_get_saddr(struct sctp_sock *sk,
565 struct sctp_transport *t,
566 struct flowi *fl)
567{
568 union sctp_addr *saddr = &t->saddr;
569 struct rtable *rt = (struct rtable *)t->dst;
570
571 if (rt) {
572 saddr->v4.sin_family = AF_INET;
573 saddr->v4.sin_addr.s_addr = fl->u.ip4.saddr;
574 }
575}
576
577
578static int sctp_v4_skb_iif(const struct sk_buff *skb)
579{
580 return inet_iif(skb);
581}
582
583
584static int sctp_v4_is_ce(const struct sk_buff *skb)
585{
586 return INET_ECN_is_ce(ip_hdr(skb)->tos);
587}
588
589
590static struct sock *sctp_v4_create_accept_sk(struct sock *sk,
591 struct sctp_association *asoc,
592 bool kern)
593{
594 struct sock *newsk = sk_alloc(sock_net(sk), PF_INET, GFP_KERNEL,
595 sk->sk_prot, kern);
596 struct inet_sock *newinet;
597
598 if (!newsk)
599 goto out;
600
601 sock_init_data(NULL, newsk);
602
603 sctp_copy_sock(newsk, sk, asoc);
604 sock_reset_flag(newsk, SOCK_ZAPPED);
605
606 sctp_v4_copy_ip_options(sk, newsk);
607
608 newinet = inet_sk(newsk);
609
610 newinet->inet_daddr = asoc->peer.primary_addr.v4.sin_addr.s_addr;
611
612 sk_refcnt_debug_inc(newsk);
613
614 if (newsk->sk_prot->init(newsk)) {
615 sk_common_release(newsk);
616 newsk = NULL;
617 }
618
619out:
620 return newsk;
621}
622
623static int sctp_v4_addr_to_user(struct sctp_sock *sp, union sctp_addr *addr)
624{
625
626 memset(addr->v4.sin_zero, 0, sizeof(addr->v4.sin_zero));
627 return sizeof(struct sockaddr_in);
628}
629
630
631static void sctp_v4_seq_dump_addr(struct seq_file *seq, union sctp_addr *addr)
632{
633 seq_printf(seq, "%pI4 ", &addr->v4.sin_addr);
634}
635
636static void sctp_v4_ecn_capable(struct sock *sk)
637{
638 INET_ECN_xmit(sk);
639}
640
641static void sctp_addr_wq_timeout_handler(struct timer_list *t)
642{
643 struct net *net = from_timer(net, t, sctp.addr_wq_timer);
644 struct sctp_sockaddr_entry *addrw, *temp;
645 struct sctp_sock *sp;
646
647 spin_lock_bh(&net->sctp.addr_wq_lock);
648
649 list_for_each_entry_safe(addrw, temp, &net->sctp.addr_waitq, list) {
650 pr_debug("%s: the first ent in wq:%p is addr:%pISc for cmd:%d at "
651 "entry:%p\n", __func__, &net->sctp.addr_waitq, &addrw->a.sa,
652 addrw->state, addrw);
653
654#if IS_ENABLED(CONFIG_IPV6)
655
656
657 if (addrw->a.sa.sa_family == AF_INET6) {
658 struct in6_addr *in6;
659
660 if (ipv6_addr_type(&addrw->a.v6.sin6_addr) &
661 IPV6_ADDR_LINKLOCAL)
662 goto free_next;
663
664 in6 = (struct in6_addr *)&addrw->a.v6.sin6_addr;
665 if (ipv6_chk_addr(net, in6, NULL, 0) == 0 &&
666 addrw->state == SCTP_ADDR_NEW) {
667 unsigned long timeo_val;
668
669 pr_debug("%s: this is on DAD, trying %d sec "
670 "later\n", __func__,
671 SCTP_ADDRESS_TICK_DELAY);
672
673 timeo_val = jiffies;
674 timeo_val += msecs_to_jiffies(SCTP_ADDRESS_TICK_DELAY);
675 mod_timer(&net->sctp.addr_wq_timer, timeo_val);
676 break;
677 }
678 }
679#endif
680 list_for_each_entry(sp, &net->sctp.auto_asconf_splist, auto_asconf_list) {
681 struct sock *sk;
682
683 sk = sctp_opt2sk(sp);
684
685 if (!sctp_is_ep_boundall(sk))
686 continue;
687 bh_lock_sock(sk);
688 if (sctp_asconf_mgmt(sp, addrw) < 0)
689 pr_debug("%s: sctp_asconf_mgmt failed\n", __func__);
690 bh_unlock_sock(sk);
691 }
692#if IS_ENABLED(CONFIG_IPV6)
693free_next:
694#endif
695 list_del(&addrw->list);
696 kfree(addrw);
697 }
698 spin_unlock_bh(&net->sctp.addr_wq_lock);
699}
700
701static void sctp_free_addr_wq(struct net *net)
702{
703 struct sctp_sockaddr_entry *addrw;
704 struct sctp_sockaddr_entry *temp;
705
706 spin_lock_bh(&net->sctp.addr_wq_lock);
707 del_timer(&net->sctp.addr_wq_timer);
708 list_for_each_entry_safe(addrw, temp, &net->sctp.addr_waitq, list) {
709 list_del(&addrw->list);
710 kfree(addrw);
711 }
712 spin_unlock_bh(&net->sctp.addr_wq_lock);
713}
714
715
716
717
718static struct sctp_sockaddr_entry *sctp_addr_wq_lookup(struct net *net,
719 struct sctp_sockaddr_entry *addr)
720{
721 struct sctp_sockaddr_entry *addrw;
722
723 list_for_each_entry(addrw, &net->sctp.addr_waitq, list) {
724 if (addrw->a.sa.sa_family != addr->a.sa.sa_family)
725 continue;
726 if (addrw->a.sa.sa_family == AF_INET) {
727 if (addrw->a.v4.sin_addr.s_addr ==
728 addr->a.v4.sin_addr.s_addr)
729 return addrw;
730 } else if (addrw->a.sa.sa_family == AF_INET6) {
731 if (ipv6_addr_equal(&addrw->a.v6.sin6_addr,
732 &addr->a.v6.sin6_addr))
733 return addrw;
734 }
735 }
736 return NULL;
737}
738
739void sctp_addr_wq_mgmt(struct net *net, struct sctp_sockaddr_entry *addr, int cmd)
740{
741 struct sctp_sockaddr_entry *addrw;
742 unsigned long timeo_val;
743
744
745
746
747
748
749
750 spin_lock_bh(&net->sctp.addr_wq_lock);
751
752 addrw = sctp_addr_wq_lookup(net, addr);
753 if (addrw) {
754 if (addrw->state != cmd) {
755 pr_debug("%s: offsets existing entry for %d, addr:%pISc "
756 "in wq:%p\n", __func__, addrw->state, &addrw->a.sa,
757 &net->sctp.addr_waitq);
758
759 list_del(&addrw->list);
760 kfree(addrw);
761 }
762 spin_unlock_bh(&net->sctp.addr_wq_lock);
763 return;
764 }
765
766
767 addrw = kmemdup(addr, sizeof(struct sctp_sockaddr_entry), GFP_ATOMIC);
768 if (addrw == NULL) {
769 spin_unlock_bh(&net->sctp.addr_wq_lock);
770 return;
771 }
772 addrw->state = cmd;
773 list_add_tail(&addrw->list, &net->sctp.addr_waitq);
774
775 pr_debug("%s: add new entry for cmd:%d, addr:%pISc in wq:%p\n",
776 __func__, addrw->state, &addrw->a.sa, &net->sctp.addr_waitq);
777
778 if (!timer_pending(&net->sctp.addr_wq_timer)) {
779 timeo_val = jiffies;
780 timeo_val += msecs_to_jiffies(SCTP_ADDRESS_TICK_DELAY);
781 mod_timer(&net->sctp.addr_wq_timer, timeo_val);
782 }
783 spin_unlock_bh(&net->sctp.addr_wq_lock);
784}
785
786
787
788
789
790
791
792static int sctp_inetaddr_event(struct notifier_block *this, unsigned long ev,
793 void *ptr)
794{
795 struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
796 struct sctp_sockaddr_entry *addr = NULL;
797 struct sctp_sockaddr_entry *temp;
798 struct net *net = dev_net(ifa->ifa_dev->dev);
799 int found = 0;
800
801 switch (ev) {
802 case NETDEV_UP:
803 addr = kzalloc(sizeof(*addr), GFP_ATOMIC);
804 if (addr) {
805 addr->a.v4.sin_family = AF_INET;
806 addr->a.v4.sin_addr.s_addr = ifa->ifa_local;
807 addr->valid = 1;
808 spin_lock_bh(&net->sctp.local_addr_lock);
809 list_add_tail_rcu(&addr->list, &net->sctp.local_addr_list);
810 sctp_addr_wq_mgmt(net, addr, SCTP_ADDR_NEW);
811 spin_unlock_bh(&net->sctp.local_addr_lock);
812 }
813 break;
814 case NETDEV_DOWN:
815 spin_lock_bh(&net->sctp.local_addr_lock);
816 list_for_each_entry_safe(addr, temp,
817 &net->sctp.local_addr_list, list) {
818 if (addr->a.sa.sa_family == AF_INET &&
819 addr->a.v4.sin_addr.s_addr ==
820 ifa->ifa_local) {
821 sctp_addr_wq_mgmt(net, addr, SCTP_ADDR_DEL);
822 found = 1;
823 addr->valid = 0;
824 list_del_rcu(&addr->list);
825 break;
826 }
827 }
828 spin_unlock_bh(&net->sctp.local_addr_lock);
829 if (found)
830 kfree_rcu(addr, rcu);
831 break;
832 }
833
834 return NOTIFY_DONE;
835}
836
837
838
839
840
841static int sctp_ctl_sock_init(struct net *net)
842{
843 int err;
844 sa_family_t family = PF_INET;
845
846 if (sctp_get_pf_specific(PF_INET6))
847 family = PF_INET6;
848
849 err = inet_ctl_sock_create(&net->sctp.ctl_sock, family,
850 SOCK_SEQPACKET, IPPROTO_SCTP, net);
851
852
853 if (err < 0 && family == PF_INET6)
854 err = inet_ctl_sock_create(&net->sctp.ctl_sock, AF_INET,
855 SOCK_SEQPACKET, IPPROTO_SCTP,
856 net);
857
858 if (err < 0) {
859 pr_err("Failed to create the SCTP control socket\n");
860 return err;
861 }
862 return 0;
863}
864
865static int sctp_udp_rcv(struct sock *sk, struct sk_buff *skb)
866{
867 SCTP_INPUT_CB(skb)->encap_port = udp_hdr(skb)->source;
868
869 skb_set_transport_header(skb, sizeof(struct udphdr));
870 sctp_rcv(skb);
871 return 0;
872}
873
874int sctp_udp_sock_start(struct net *net)
875{
876 struct udp_tunnel_sock_cfg tuncfg = {NULL};
877 struct udp_port_cfg udp_conf = {0};
878 struct socket *sock;
879 int err;
880
881 udp_conf.family = AF_INET;
882 udp_conf.local_ip.s_addr = htonl(INADDR_ANY);
883 udp_conf.local_udp_port = htons(net->sctp_udp_port);
884 err = udp_sock_create(net, &udp_conf, &sock);
885 if (err) {
886 pr_err("Failed to create the SCTP UDP tunneling v4 sock\n");
887 return err;
888 }
889
890 tuncfg.encap_type = 1;
891 tuncfg.encap_rcv = sctp_udp_rcv;
892 tuncfg.encap_err_lookup = sctp_udp_v4_err;
893 setup_udp_tunnel_sock(net, sock, &tuncfg);
894 net->sctp_udp4_sock = sock->sk;
895
896#if IS_ENABLED(CONFIG_IPV6)
897 memset(&udp_conf, 0, sizeof(udp_conf));
898
899 udp_conf.family = AF_INET6;
900 udp_conf.local_ip6 = in6addr_any;
901 udp_conf.local_udp_port = htons(net->sctp_udp_port);
902 udp_conf.use_udp6_rx_checksums = true;
903 udp_conf.ipv6_v6only = true;
904 err = udp_sock_create(net, &udp_conf, &sock);
905 if (err) {
906 pr_err("Failed to create the SCTP UDP tunneling v6 sock\n");
907 udp_tunnel_sock_release(net->sctp_udp4_sock->sk_socket);
908 net->sctp_udp4_sock = NULL;
909 return err;
910 }
911
912 tuncfg.encap_type = 1;
913 tuncfg.encap_rcv = sctp_udp_rcv;
914 tuncfg.encap_err_lookup = sctp_udp_v6_err;
915 setup_udp_tunnel_sock(net, sock, &tuncfg);
916 net->sctp_udp6_sock = sock->sk;
917#endif
918
919 return 0;
920}
921
922void sctp_udp_sock_stop(struct net *net)
923{
924 if (net->sctp_udp4_sock) {
925 udp_tunnel_sock_release(net->sctp_udp4_sock->sk_socket);
926 net->sctp_udp4_sock = NULL;
927 }
928 if (net->sctp_udp6_sock) {
929 udp_tunnel_sock_release(net->sctp_udp6_sock->sk_socket);
930 net->sctp_udp6_sock = NULL;
931 }
932}
933
934
935int sctp_register_af(struct sctp_af *af)
936{
937 switch (af->sa_family) {
938 case AF_INET:
939 if (sctp_af_v4_specific)
940 return 0;
941 sctp_af_v4_specific = af;
942 break;
943 case AF_INET6:
944 if (sctp_af_v6_specific)
945 return 0;
946 sctp_af_v6_specific = af;
947 break;
948 default:
949 return 0;
950 }
951
952 INIT_LIST_HEAD(&af->list);
953 list_add_tail(&af->list, &sctp_address_families);
954 return 1;
955}
956
957
958
959
960struct sctp_af *sctp_get_af_specific(sa_family_t family)
961{
962 switch (family) {
963 case AF_INET:
964 return sctp_af_v4_specific;
965 case AF_INET6:
966 return sctp_af_v6_specific;
967 default:
968 return NULL;
969 }
970}
971
972
973static void sctp_inet_msgname(char *msgname, int *addr_len)
974{
975 struct sockaddr_in *sin;
976
977 sin = (struct sockaddr_in *)msgname;
978 *addr_len = sizeof(struct sockaddr_in);
979 sin->sin_family = AF_INET;
980 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
981}
982
983
984static void sctp_inet_event_msgname(struct sctp_ulpevent *event, char *msgname,
985 int *addr_len)
986{
987 struct sockaddr_in *sin, *sinfrom;
988
989 if (msgname) {
990 struct sctp_association *asoc;
991
992 asoc = event->asoc;
993 sctp_inet_msgname(msgname, addr_len);
994 sin = (struct sockaddr_in *)msgname;
995 sinfrom = &asoc->peer.primary_addr.v4;
996 sin->sin_port = htons(asoc->peer.port);
997 sin->sin_addr.s_addr = sinfrom->sin_addr.s_addr;
998 }
999}
1000
1001
1002static void sctp_inet_skb_msgname(struct sk_buff *skb, char *msgname, int *len)
1003{
1004 if (msgname) {
1005 struct sctphdr *sh = sctp_hdr(skb);
1006 struct sockaddr_in *sin = (struct sockaddr_in *)msgname;
1007
1008 sctp_inet_msgname(msgname, len);
1009 sin->sin_port = sh->source;
1010 sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
1011 }
1012}
1013
1014
1015static int sctp_inet_af_supported(sa_family_t family, struct sctp_sock *sp)
1016{
1017
1018 return AF_INET == family;
1019}
1020
1021
1022static int sctp_inet_cmp_addr(const union sctp_addr *addr1,
1023 const union sctp_addr *addr2,
1024 struct sctp_sock *opt)
1025{
1026
1027 if (addr1->sa.sa_family != addr2->sa.sa_family)
1028 return 0;
1029 if (htonl(INADDR_ANY) == addr1->v4.sin_addr.s_addr ||
1030 htonl(INADDR_ANY) == addr2->v4.sin_addr.s_addr)
1031 return 1;
1032 if (addr1->v4.sin_addr.s_addr == addr2->v4.sin_addr.s_addr)
1033 return 1;
1034
1035 return 0;
1036}
1037
1038
1039
1040
1041static int sctp_inet_bind_verify(struct sctp_sock *opt, union sctp_addr *addr)
1042{
1043 return sctp_v4_available(addr, opt);
1044}
1045
1046
1047
1048
1049static int sctp_inet_send_verify(struct sctp_sock *opt, union sctp_addr *addr)
1050{
1051 return 1;
1052}
1053
1054
1055
1056
1057static int sctp_inet_supported_addrs(const struct sctp_sock *opt,
1058 __be16 *types)
1059{
1060 types[0] = SCTP_PARAM_IPV4_ADDRESS;
1061 return 1;
1062}
1063
1064
1065static inline int sctp_v4_xmit(struct sk_buff *skb, struct sctp_transport *t)
1066{
1067 struct dst_entry *dst = dst_clone(t->dst);
1068 struct flowi4 *fl4 = &t->fl.u.ip4;
1069 struct sock *sk = skb->sk;
1070 struct inet_sock *inet = inet_sk(sk);
1071 __u8 dscp = inet->tos;
1072 __be16 df = 0;
1073
1074 pr_debug("%s: skb:%p, len:%d, src:%pI4, dst:%pI4\n", __func__, skb,
1075 skb->len, &fl4->saddr, &fl4->daddr);
1076
1077 if (t->dscp & SCTP_DSCP_SET_MASK)
1078 dscp = t->dscp & SCTP_DSCP_VAL_MASK;
1079
1080 inet->pmtudisc = t->param_flags & SPP_PMTUD_ENABLE ? IP_PMTUDISC_DO
1081 : IP_PMTUDISC_DONT;
1082 SCTP_INC_STATS(sock_net(sk), SCTP_MIB_OUTSCTPPACKS);
1083
1084 if (!t->encap_port || !sctp_sk(sk)->udp_port) {
1085 skb_dst_set(skb, dst);
1086 return __ip_queue_xmit(sk, skb, &t->fl, dscp);
1087 }
1088
1089 if (skb_is_gso(skb))
1090 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL_CSUM;
1091
1092 if (ip_dont_fragment(sk, dst) && !skb->ignore_df)
1093 df = htons(IP_DF);
1094
1095 skb->encapsulation = 1;
1096 skb_reset_inner_mac_header(skb);
1097 skb_reset_inner_transport_header(skb);
1098 skb_set_inner_ipproto(skb, IPPROTO_SCTP);
1099 udp_tunnel_xmit_skb((struct rtable *)dst, sk, skb, fl4->saddr,
1100 fl4->daddr, dscp, ip4_dst_hoplimit(dst), df,
1101 sctp_sk(sk)->udp_port, t->encap_port, false, false);
1102 return 0;
1103}
1104
1105static struct sctp_af sctp_af_inet;
1106
1107static struct sctp_pf sctp_pf_inet = {
1108 .event_msgname = sctp_inet_event_msgname,
1109 .skb_msgname = sctp_inet_skb_msgname,
1110 .af_supported = sctp_inet_af_supported,
1111 .cmp_addr = sctp_inet_cmp_addr,
1112 .bind_verify = sctp_inet_bind_verify,
1113 .send_verify = sctp_inet_send_verify,
1114 .supported_addrs = sctp_inet_supported_addrs,
1115 .create_accept_sk = sctp_v4_create_accept_sk,
1116 .addr_to_user = sctp_v4_addr_to_user,
1117 .to_sk_saddr = sctp_v4_to_sk_saddr,
1118 .to_sk_daddr = sctp_v4_to_sk_daddr,
1119 .copy_ip_options = sctp_v4_copy_ip_options,
1120 .af = &sctp_af_inet
1121};
1122
1123
1124static struct notifier_block sctp_inetaddr_notifier = {
1125 .notifier_call = sctp_inetaddr_event,
1126};
1127
1128
1129static const struct proto_ops inet_seqpacket_ops = {
1130 .family = PF_INET,
1131 .owner = THIS_MODULE,
1132 .release = inet_release,
1133 .bind = inet_bind,
1134 .connect = sctp_inet_connect,
1135 .socketpair = sock_no_socketpair,
1136 .accept = inet_accept,
1137 .getname = inet_getname,
1138 .poll = sctp_poll,
1139 .ioctl = inet_ioctl,
1140 .listen = sctp_inet_listen,
1141 .shutdown = inet_shutdown,
1142 .setsockopt = sock_common_setsockopt,
1143 .getsockopt = sock_common_getsockopt,
1144 .sendmsg = inet_sendmsg,
1145 .recvmsg = inet_recvmsg,
1146 .mmap = sock_no_mmap,
1147 .sendpage = sock_no_sendpage,
1148#ifdef CONFIG_COMPAT
1149 .compat_setsockopt = compat_sock_common_setsockopt,
1150 .compat_getsockopt = compat_sock_common_getsockopt,
1151#endif
1152};
1153
1154
1155static struct inet_protosw sctp_seqpacket_protosw = {
1156 .type = SOCK_SEQPACKET,
1157 .protocol = IPPROTO_SCTP,
1158 .prot = &sctp_prot,
1159 .ops = &inet_seqpacket_ops,
1160 .flags = SCTP_PROTOSW_FLAG
1161};
1162static struct inet_protosw sctp_stream_protosw = {
1163 .type = SOCK_STREAM,
1164 .protocol = IPPROTO_SCTP,
1165 .prot = &sctp_prot,
1166 .ops = &inet_seqpacket_ops,
1167 .flags = SCTP_PROTOSW_FLAG
1168};
1169
1170static int sctp4_rcv(struct sk_buff *skb)
1171{
1172 SCTP_INPUT_CB(skb)->encap_port = 0;
1173 return sctp_rcv(skb);
1174}
1175
1176
1177static const struct net_protocol sctp_protocol = {
1178 .handler = sctp4_rcv,
1179 .err_handler = sctp_v4_err,
1180 .no_policy = 1,
1181 .netns_ok = 1,
1182 .icmp_strict_tag_validation = 1,
1183};
1184
1185
1186static struct sctp_af sctp_af_inet = {
1187 .sa_family = AF_INET,
1188 .sctp_xmit = sctp_v4_xmit,
1189 .setsockopt = ip_setsockopt,
1190 .getsockopt = ip_getsockopt,
1191 .get_dst = sctp_v4_get_dst,
1192 .get_saddr = sctp_v4_get_saddr,
1193 .copy_addrlist = sctp_v4_copy_addrlist,
1194 .from_skb = sctp_v4_from_skb,
1195 .from_sk = sctp_v4_from_sk,
1196 .from_addr_param = sctp_v4_from_addr_param,
1197 .to_addr_param = sctp_v4_to_addr_param,
1198 .cmp_addr = sctp_v4_cmp_addr,
1199 .addr_valid = sctp_v4_addr_valid,
1200 .inaddr_any = sctp_v4_inaddr_any,
1201 .is_any = sctp_v4_is_any,
1202 .available = sctp_v4_available,
1203 .scope = sctp_v4_scope,
1204 .skb_iif = sctp_v4_skb_iif,
1205 .is_ce = sctp_v4_is_ce,
1206 .seq_dump_addr = sctp_v4_seq_dump_addr,
1207 .ecn_capable = sctp_v4_ecn_capable,
1208 .net_header_len = sizeof(struct iphdr),
1209 .sockaddr_len = sizeof(struct sockaddr_in),
1210 .ip_options_len = sctp_v4_ip_options_len,
1211#ifdef CONFIG_COMPAT
1212 .compat_setsockopt = compat_ip_setsockopt,
1213 .compat_getsockopt = compat_ip_getsockopt,
1214#endif
1215};
1216
1217struct sctp_pf *sctp_get_pf_specific(sa_family_t family)
1218{
1219 switch (family) {
1220 case PF_INET:
1221 return sctp_pf_inet_specific;
1222 case PF_INET6:
1223 return sctp_pf_inet6_specific;
1224 default:
1225 return NULL;
1226 }
1227}
1228
1229
1230int sctp_register_pf(struct sctp_pf *pf, sa_family_t family)
1231{
1232 switch (family) {
1233 case PF_INET:
1234 if (sctp_pf_inet_specific)
1235 return 0;
1236 sctp_pf_inet_specific = pf;
1237 break;
1238 case PF_INET6:
1239 if (sctp_pf_inet6_specific)
1240 return 0;
1241 sctp_pf_inet6_specific = pf;
1242 break;
1243 default:
1244 return 0;
1245 }
1246 return 1;
1247}
1248
1249static inline int init_sctp_mibs(struct net *net)
1250{
1251 net->sctp.sctp_statistics = alloc_percpu(struct sctp_mib);
1252 if (!net->sctp.sctp_statistics)
1253 return -ENOMEM;
1254 return 0;
1255}
1256
1257static inline void cleanup_sctp_mibs(struct net *net)
1258{
1259 free_percpu(net->sctp.sctp_statistics);
1260}
1261
1262static void sctp_v4_pf_init(void)
1263{
1264
1265 sctp_register_pf(&sctp_pf_inet, PF_INET);
1266 sctp_register_af(&sctp_af_inet);
1267}
1268
1269static void sctp_v4_pf_exit(void)
1270{
1271 list_del(&sctp_af_inet.list);
1272}
1273
1274static int sctp_v4_protosw_init(void)
1275{
1276 int rc;
1277
1278 rc = proto_register(&sctp_prot, 1);
1279 if (rc)
1280 return rc;
1281
1282
1283 inet_register_protosw(&sctp_seqpacket_protosw);
1284 inet_register_protosw(&sctp_stream_protosw);
1285
1286 return 0;
1287}
1288
1289static void sctp_v4_protosw_exit(void)
1290{
1291 inet_unregister_protosw(&sctp_stream_protosw);
1292 inet_unregister_protosw(&sctp_seqpacket_protosw);
1293 proto_unregister(&sctp_prot);
1294}
1295
1296static int sctp_v4_add_protocol(void)
1297{
1298
1299 register_inetaddr_notifier(&sctp_inetaddr_notifier);
1300
1301
1302 if (inet_add_protocol(&sctp_protocol, IPPROTO_SCTP) < 0)
1303 return -EAGAIN;
1304
1305 return 0;
1306}
1307
1308static void sctp_v4_del_protocol(void)
1309{
1310 inet_del_protocol(&sctp_protocol, IPPROTO_SCTP);
1311 unregister_inetaddr_notifier(&sctp_inetaddr_notifier);
1312}
1313
1314static int __net_init sctp_defaults_init(struct net *net)
1315{
1316 int status;
1317
1318
1319
1320
1321
1322
1323 net->sctp.rto_initial = SCTP_RTO_INITIAL;
1324
1325 net->sctp.rto_min = SCTP_RTO_MIN;
1326
1327 net->sctp.rto_max = SCTP_RTO_MAX;
1328
1329 net->sctp.rto_alpha = SCTP_RTO_ALPHA;
1330
1331 net->sctp.rto_beta = SCTP_RTO_BETA;
1332
1333
1334 net->sctp.valid_cookie_life = SCTP_DEFAULT_COOKIE_LIFE;
1335
1336
1337 net->sctp.cookie_preserve_enable = 1;
1338
1339
1340#if defined (CONFIG_SCTP_DEFAULT_COOKIE_HMAC_MD5)
1341 net->sctp.sctp_hmac_alg = "md5";
1342#elif defined (CONFIG_SCTP_DEFAULT_COOKIE_HMAC_SHA1)
1343 net->sctp.sctp_hmac_alg = "sha1";
1344#else
1345 net->sctp.sctp_hmac_alg = NULL;
1346#endif
1347
1348
1349 net->sctp.max_burst = SCTP_DEFAULT_MAX_BURST;
1350
1351
1352 net->sctp_ps_retrans = SCTP_PS_RETRANS_MAX;
1353
1354
1355 net->sctp.pf_enable = 1;
1356
1357
1358 net->sctp_pf_expose = SCTP_PF_EXPOSE_UNSET;
1359
1360
1361
1362
1363
1364 net->sctp.max_retrans_association = 10;
1365 net->sctp.max_retrans_path = 5;
1366 net->sctp.max_retrans_init = 8;
1367
1368
1369 net->sctp.sndbuf_policy = 0;
1370
1371
1372 net->sctp.rcvbuf_policy = 0;
1373
1374
1375 net->sctp.hb_interval = SCTP_DEFAULT_TIMEOUT_HEARTBEAT;
1376
1377
1378 net->sctp.sack_timeout = SCTP_DEFAULT_TIMEOUT_SACK;
1379
1380
1381 net->sctp.addip_enable = 0;
1382 net->sctp.addip_noauth = 0;
1383 net->sctp.default_auto_asconf = 0;
1384
1385
1386 net->sctp.prsctp_enable = 1;
1387
1388
1389 net->sctp.reconf_enable = 0;
1390
1391
1392 net->sctp.auth_enable = 0;
1393
1394
1395 net->sctp_ecn_enable = 1;
1396
1397
1398 net->sctp_udp_port = 0;
1399
1400
1401 net->sctp_encap_port = 0;
1402
1403
1404 net->sctp.scope_policy = SCTP_SCOPE_POLICY_ENABLE;
1405
1406
1407 net->sctp.rwnd_upd_shift = SCTP_DEFAULT_RWND_SHIFT;
1408
1409
1410 net->sctp.max_autoclose = INT_MAX / HZ;
1411
1412 status = sctp_sysctl_net_register(net);
1413 if (status)
1414 goto err_sysctl_register;
1415
1416
1417 status = init_sctp_mibs(net);
1418 if (status)
1419 goto err_init_mibs;
1420
1421#ifdef CONFIG_PROC_FS
1422
1423 status = sctp_proc_init(net);
1424 if (status)
1425 goto err_init_proc;
1426#endif
1427
1428 sctp_dbg_objcnt_init(net);
1429
1430
1431 INIT_LIST_HEAD(&net->sctp.local_addr_list);
1432 spin_lock_init(&net->sctp.local_addr_lock);
1433 sctp_get_local_addr_list(net);
1434
1435
1436 INIT_LIST_HEAD(&net->sctp.addr_waitq);
1437 INIT_LIST_HEAD(&net->sctp.auto_asconf_splist);
1438 spin_lock_init(&net->sctp.addr_wq_lock);
1439 net->sctp.addr_wq_timer.expires = 0;
1440 timer_setup(&net->sctp.addr_wq_timer, sctp_addr_wq_timeout_handler, 0);
1441
1442 return 0;
1443
1444#ifdef CONFIG_PROC_FS
1445err_init_proc:
1446 cleanup_sctp_mibs(net);
1447#endif
1448err_init_mibs:
1449 sctp_sysctl_net_unregister(net);
1450err_sysctl_register:
1451 return status;
1452}
1453
1454static void __net_exit sctp_defaults_exit(struct net *net)
1455{
1456
1457 sctp_free_addr_wq(net);
1458 sctp_free_local_addr_list(net);
1459
1460#ifdef CONFIG_PROC_FS
1461 remove_proc_subtree("sctp", net->proc_net);
1462 net->sctp.proc_net_sctp = NULL;
1463#endif
1464 cleanup_sctp_mibs(net);
1465 sctp_sysctl_net_unregister(net);
1466}
1467
1468static struct pernet_operations sctp_defaults_ops = {
1469 .init = sctp_defaults_init,
1470 .exit = sctp_defaults_exit,
1471};
1472
1473static int __net_init sctp_ctrlsock_init(struct net *net)
1474{
1475 int status;
1476
1477
1478 status = sctp_ctl_sock_init(net);
1479 if (status)
1480 pr_err("Failed to initialize the SCTP control sock\n");
1481
1482 return status;
1483}
1484
1485static void __net_exit sctp_ctrlsock_exit(struct net *net)
1486{
1487
1488 inet_ctl_sock_destroy(net->sctp.ctl_sock);
1489}
1490
1491static struct pernet_operations sctp_ctrlsock_ops = {
1492 .init = sctp_ctrlsock_init,
1493 .exit = sctp_ctrlsock_exit,
1494};
1495
1496
1497static __init int sctp_init(void)
1498{
1499 int i;
1500 int status = -EINVAL;
1501 unsigned long goal;
1502 unsigned long limit;
1503 unsigned long nr_pages = totalram_pages();
1504 int max_share;
1505 int order;
1506 int num_entries;
1507 int max_entry_order;
1508
1509 sock_skb_cb_check_size(sizeof(struct sctp_ulpevent));
1510
1511
1512 status = -ENOBUFS;
1513 sctp_bucket_cachep = kmem_cache_create("sctp_bind_bucket",
1514 sizeof(struct sctp_bind_bucket),
1515 0, SLAB_HWCACHE_ALIGN,
1516 NULL);
1517 if (!sctp_bucket_cachep)
1518 goto out;
1519
1520 sctp_chunk_cachep = kmem_cache_create("sctp_chunk",
1521 sizeof(struct sctp_chunk),
1522 0, SLAB_HWCACHE_ALIGN,
1523 NULL);
1524 if (!sctp_chunk_cachep)
1525 goto err_chunk_cachep;
1526
1527 status = percpu_counter_init(&sctp_sockets_allocated, 0, GFP_KERNEL);
1528 if (status)
1529 goto err_percpu_counter_init;
1530
1531
1532
1533
1534 sctp_max_instreams = SCTP_DEFAULT_INSTREAMS;
1535 sctp_max_outstreams = SCTP_DEFAULT_OUTSTREAMS;
1536
1537
1538 idr_init(&sctp_assocs_id);
1539
1540 limit = nr_free_buffer_pages() / 8;
1541 limit = max(limit, 128UL);
1542 sysctl_sctp_mem[0] = limit / 4 * 3;
1543 sysctl_sctp_mem[1] = limit;
1544 sysctl_sctp_mem[2] = sysctl_sctp_mem[0] * 2;
1545
1546
1547 limit = (sysctl_sctp_mem[1]) << (PAGE_SHIFT - 7);
1548 max_share = min(4UL*1024*1024, limit);
1549
1550 sysctl_sctp_rmem[0] = SK_MEM_QUANTUM;
1551 sysctl_sctp_rmem[1] = 1500 * SKB_TRUESIZE(1);
1552 sysctl_sctp_rmem[2] = max(sysctl_sctp_rmem[1], max_share);
1553
1554 sysctl_sctp_wmem[0] = SK_MEM_QUANTUM;
1555 sysctl_sctp_wmem[1] = 16*1024;
1556 sysctl_sctp_wmem[2] = max(64*1024, max_share);
1557
1558
1559
1560
1561
1562 if (nr_pages >= (128 * 1024))
1563 goal = nr_pages >> (22 - PAGE_SHIFT);
1564 else
1565 goal = nr_pages >> (24 - PAGE_SHIFT);
1566
1567
1568 order = get_order(goal);
1569
1570
1571
1572
1573 max_entry_order = get_order(MAX_SCTP_PORT_HASH_ENTRIES *
1574 sizeof(struct sctp_bind_hashbucket));
1575
1576
1577 order = min(order, max_entry_order);
1578
1579
1580 sctp_ep_hashsize = 64;
1581 sctp_ep_hashtable =
1582 kmalloc_array(64, sizeof(struct sctp_hashbucket), GFP_KERNEL);
1583 if (!sctp_ep_hashtable) {
1584 pr_err("Failed endpoint_hash alloc\n");
1585 status = -ENOMEM;
1586 goto err_ehash_alloc;
1587 }
1588 for (i = 0; i < sctp_ep_hashsize; i++) {
1589 rwlock_init(&sctp_ep_hashtable[i].lock);
1590 INIT_HLIST_HEAD(&sctp_ep_hashtable[i].chain);
1591 }
1592
1593
1594
1595
1596
1597
1598 do {
1599 sctp_port_hashtable = (struct sctp_bind_hashbucket *)
1600 __get_free_pages(GFP_KERNEL | __GFP_NOWARN, order);
1601 } while (!sctp_port_hashtable && --order > 0);
1602
1603 if (!sctp_port_hashtable) {
1604 pr_err("Failed bind hash alloc\n");
1605 status = -ENOMEM;
1606 goto err_bhash_alloc;
1607 }
1608
1609
1610
1611
1612 num_entries = (1UL << order) * PAGE_SIZE /
1613 sizeof(struct sctp_bind_hashbucket);
1614
1615
1616
1617
1618
1619
1620 sctp_port_hashsize = rounddown_pow_of_two(num_entries);
1621
1622 for (i = 0; i < sctp_port_hashsize; i++) {
1623 spin_lock_init(&sctp_port_hashtable[i].lock);
1624 INIT_HLIST_HEAD(&sctp_port_hashtable[i].chain);
1625 }
1626
1627 status = sctp_transport_hashtable_init();
1628 if (status)
1629 goto err_thash_alloc;
1630
1631 pr_info("Hash tables configured (bind %d/%d)\n", sctp_port_hashsize,
1632 num_entries);
1633
1634 sctp_sysctl_register();
1635
1636 INIT_LIST_HEAD(&sctp_address_families);
1637 sctp_v4_pf_init();
1638 sctp_v6_pf_init();
1639 sctp_sched_ops_init();
1640
1641 status = register_pernet_subsys(&sctp_defaults_ops);
1642 if (status)
1643 goto err_register_defaults;
1644
1645 status = sctp_v4_protosw_init();
1646 if (status)
1647 goto err_protosw_init;
1648
1649 status = sctp_v6_protosw_init();
1650 if (status)
1651 goto err_v6_protosw_init;
1652
1653 status = register_pernet_subsys(&sctp_ctrlsock_ops);
1654 if (status)
1655 goto err_register_ctrlsock;
1656
1657 status = sctp_v4_add_protocol();
1658 if (status)
1659 goto err_add_protocol;
1660
1661
1662 status = sctp_v6_add_protocol();
1663 if (status)
1664 goto err_v6_add_protocol;
1665
1666 if (sctp_offload_init() < 0)
1667 pr_crit("%s: Cannot add SCTP protocol offload\n", __func__);
1668
1669out:
1670 return status;
1671err_v6_add_protocol:
1672 sctp_v4_del_protocol();
1673err_add_protocol:
1674 unregister_pernet_subsys(&sctp_ctrlsock_ops);
1675err_register_ctrlsock:
1676 sctp_v6_protosw_exit();
1677err_v6_protosw_init:
1678 sctp_v4_protosw_exit();
1679err_protosw_init:
1680 unregister_pernet_subsys(&sctp_defaults_ops);
1681err_register_defaults:
1682 sctp_v4_pf_exit();
1683 sctp_v6_pf_exit();
1684 sctp_sysctl_unregister();
1685 free_pages((unsigned long)sctp_port_hashtable,
1686 get_order(sctp_port_hashsize *
1687 sizeof(struct sctp_bind_hashbucket)));
1688err_bhash_alloc:
1689 sctp_transport_hashtable_destroy();
1690err_thash_alloc:
1691 kfree(sctp_ep_hashtable);
1692err_ehash_alloc:
1693 percpu_counter_destroy(&sctp_sockets_allocated);
1694err_percpu_counter_init:
1695 kmem_cache_destroy(sctp_chunk_cachep);
1696err_chunk_cachep:
1697 kmem_cache_destroy(sctp_bucket_cachep);
1698 goto out;
1699}
1700
1701
1702static __exit void sctp_exit(void)
1703{
1704
1705
1706
1707
1708
1709 sctp_v6_del_protocol();
1710 sctp_v4_del_protocol();
1711
1712 unregister_pernet_subsys(&sctp_ctrlsock_ops);
1713
1714
1715 sctp_v6_protosw_exit();
1716 sctp_v4_protosw_exit();
1717
1718 unregister_pernet_subsys(&sctp_defaults_ops);
1719
1720
1721 sctp_v6_pf_exit();
1722 sctp_v4_pf_exit();
1723
1724 sctp_sysctl_unregister();
1725
1726 free_pages((unsigned long)sctp_port_hashtable,
1727 get_order(sctp_port_hashsize *
1728 sizeof(struct sctp_bind_hashbucket)));
1729 kfree(sctp_ep_hashtable);
1730 sctp_transport_hashtable_destroy();
1731
1732 percpu_counter_destroy(&sctp_sockets_allocated);
1733
1734 rcu_barrier();
1735
1736 kmem_cache_destroy(sctp_chunk_cachep);
1737 kmem_cache_destroy(sctp_bucket_cachep);
1738}
1739
1740module_init(sctp_init);
1741module_exit(sctp_exit);
1742
1743
1744
1745
1746MODULE_ALIAS("net-pf-" __stringify(PF_INET) "-proto-132");
1747MODULE_ALIAS("net-pf-" __stringify(PF_INET6) "-proto-132");
1748MODULE_AUTHOR("Linux Kernel SCTP developers <linux-sctp@vger.kernel.org>");
1749MODULE_DESCRIPTION("Support for the SCTP protocol (RFC2960)");
1750module_param_named(no_checksums, sctp_checksum_disable, bool, 0644);
1751MODULE_PARM_DESC(no_checksums, "Disable checksums computing and verification");
1752MODULE_LICENSE("GPL");
1753