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
43
44
45
46
47
48
49#include <linux/module.h>
50#include <linux/init.h>
51#include <linux/netdevice.h>
52#include <linux/inetdevice.h>
53#include <linux/seq_file.h>
54#include <net/protocol.h>
55#include <net/ip.h>
56#include <net/ipv6.h>
57#include <net/sctp/sctp.h>
58#include <net/addrconf.h>
59#include <net/inet_common.h>
60#include <net/inet_ecn.h>
61
62
63struct sctp_globals sctp_globals;
64struct proc_dir_entry *proc_net_sctp;
65DEFINE_SNMP_STAT(struct sctp_mib, sctp_statistics);
66
67struct idr sctp_assocs_id;
68DEFINE_SPINLOCK(sctp_assocs_id_lock);
69
70
71
72
73
74static struct socket *sctp_ctl_socket;
75
76static struct sctp_pf *sctp_pf_inet6_specific;
77static struct sctp_pf *sctp_pf_inet_specific;
78static struct sctp_af *sctp_af_v4_specific;
79static struct sctp_af *sctp_af_v6_specific;
80
81kmem_cache_t *sctp_chunk_cachep;
82kmem_cache_t *sctp_bucket_cachep;
83
84extern int sctp_snmp_proc_init(void);
85extern int sctp_snmp_proc_exit(void);
86extern int sctp_eps_proc_init(void);
87extern int sctp_eps_proc_exit(void);
88extern int sctp_assocs_proc_init(void);
89extern int sctp_assocs_proc_exit(void);
90
91
92struct sock *sctp_get_ctl_sock(void)
93{
94 return sctp_ctl_socket->sk;
95}
96
97
98static __init int sctp_proc_init(void)
99{
100 if (!proc_net_sctp) {
101 struct proc_dir_entry *ent;
102 ent = proc_mkdir("net/sctp", NULL);
103 if (ent) {
104 ent->owner = THIS_MODULE;
105 proc_net_sctp = ent;
106 } else
107 goto out_nomem;
108 }
109
110 if (sctp_snmp_proc_init())
111 goto out_nomem;
112 if (sctp_eps_proc_init())
113 goto out_nomem;
114 if (sctp_assocs_proc_init())
115 goto out_nomem;
116
117 return 0;
118
119out_nomem:
120 return -ENOMEM;
121}
122
123
124
125
126
127static void sctp_proc_exit(void)
128{
129 sctp_snmp_proc_exit();
130 sctp_eps_proc_exit();
131 sctp_assocs_proc_exit();
132
133 if (proc_net_sctp) {
134 proc_net_sctp = NULL;
135 remove_proc_entry("net/sctp", NULL);
136 }
137}
138
139
140
141
142static void sctp_v4_copy_addrlist(struct list_head *addrlist,
143 struct net_device *dev)
144{
145 struct in_device *in_dev;
146 struct in_ifaddr *ifa;
147 struct sctp_sockaddr_entry *addr;
148
149 rcu_read_lock();
150 if ((in_dev = __in_dev_get(dev)) == NULL) {
151 rcu_read_unlock();
152 return;
153 }
154
155 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
156
157 addr = t_new(struct sctp_sockaddr_entry, GFP_ATOMIC);
158 if (addr) {
159 addr->a.v4.sin_family = AF_INET;
160 addr->a.v4.sin_port = 0;
161 addr->a.v4.sin_addr.s_addr = ifa->ifa_local;
162 list_add_tail(&addr->list, addrlist);
163 }
164 }
165
166 rcu_read_unlock();
167}
168
169
170
171
172static void __sctp_get_local_addr_list(void)
173{
174 struct net_device *dev;
175 struct list_head *pos;
176 struct sctp_af *af;
177
178 read_lock(&dev_base_lock);
179 for (dev = dev_base; dev; dev = dev->next) {
180 __list_for_each(pos, &sctp_address_families) {
181 af = list_entry(pos, struct sctp_af, list);
182 af->copy_addrlist(&sctp_local_addr_list, dev);
183 }
184 }
185 read_unlock(&dev_base_lock);
186}
187
188static void sctp_get_local_addr_list(void)
189{
190 unsigned long flags;
191
192 sctp_spin_lock_irqsave(&sctp_local_addr_lock, flags);
193 __sctp_get_local_addr_list();
194 sctp_spin_unlock_irqrestore(&sctp_local_addr_lock, flags);
195}
196
197
198static void __sctp_free_local_addr_list(void)
199{
200 struct sctp_sockaddr_entry *addr;
201 struct list_head *pos, *temp;
202
203 list_for_each_safe(pos, temp, &sctp_local_addr_list) {
204 addr = list_entry(pos, struct sctp_sockaddr_entry, list);
205 list_del(pos);
206 kfree(addr);
207 }
208}
209
210
211static void sctp_free_local_addr_list(void)
212{
213 unsigned long flags;
214
215 sctp_spin_lock_irqsave(&sctp_local_addr_lock, flags);
216 __sctp_free_local_addr_list();
217 sctp_spin_unlock_irqrestore(&sctp_local_addr_lock, flags);
218}
219
220
221int sctp_copy_local_addr_list(struct sctp_bind_addr *bp, sctp_scope_t scope,
222 int gfp, int copy_flags)
223{
224 struct sctp_sockaddr_entry *addr;
225 int error = 0;
226 struct list_head *pos;
227 unsigned long flags;
228
229 sctp_spin_lock_irqsave(&sctp_local_addr_lock, flags);
230 list_for_each(pos, &sctp_local_addr_list) {
231 addr = list_entry(pos, struct sctp_sockaddr_entry, list);
232 if (sctp_in_scope(&addr->a, scope)) {
233
234
235
236
237 if ((((AF_INET == addr->a.sa.sa_family) &&
238 (copy_flags & SCTP_ADDR4_PEERSUPP))) ||
239 (((AF_INET6 == addr->a.sa.sa_family) &&
240 (copy_flags & SCTP_ADDR6_ALLOWED) &&
241 (copy_flags & SCTP_ADDR6_PEERSUPP)))) {
242 error = sctp_add_bind_addr(bp, &addr->a,
243 GFP_ATOMIC);
244 if (error)
245 goto end_copy;
246 }
247 }
248 }
249
250end_copy:
251 sctp_spin_unlock_irqrestore(&sctp_local_addr_lock, flags);
252 return error;
253}
254
255
256static void sctp_v4_from_skb(union sctp_addr *addr, struct sk_buff *skb,
257 int is_saddr)
258{
259 void *from;
260 __u16 *port;
261 struct sctphdr *sh;
262
263 port = &addr->v4.sin_port;
264 addr->v4.sin_family = AF_INET;
265
266 sh = (struct sctphdr *) skb->h.raw;
267 if (is_saddr) {
268 *port = ntohs(sh->source);
269 from = &skb->nh.iph->saddr;
270 } else {
271 *port = ntohs(sh->dest);
272 from = &skb->nh.iph->daddr;
273 }
274 memcpy(&addr->v4.sin_addr.s_addr, from, sizeof(struct in_addr));
275}
276
277
278static void sctp_v4_from_sk(union sctp_addr *addr, struct sock *sk)
279{
280 addr->v4.sin_family = AF_INET;
281 addr->v4.sin_port = inet_sk(sk)->num;
282 addr->v4.sin_addr.s_addr = inet_sk(sk)->rcv_saddr;
283}
284
285
286static void sctp_v4_to_sk_saddr(union sctp_addr *addr, struct sock *sk)
287{
288 inet_sk(sk)->rcv_saddr = addr->v4.sin_addr.s_addr;
289}
290
291
292static void sctp_v4_to_sk_daddr(union sctp_addr *addr, struct sock *sk)
293{
294 inet_sk(sk)->daddr = addr->v4.sin_addr.s_addr;
295}
296
297
298static void sctp_v4_from_addr_param(union sctp_addr *addr,
299 union sctp_addr_param *param,
300 __u16 port, int iif)
301{
302 addr->v4.sin_family = AF_INET;
303 addr->v4.sin_port = port;
304 addr->v4.sin_addr.s_addr = param->v4.addr.s_addr;
305}
306
307
308
309
310static int sctp_v4_to_addr_param(const union sctp_addr *addr,
311 union sctp_addr_param *param)
312{
313 int length = sizeof(sctp_ipv4addr_param_t);
314
315 param->v4.param_hdr.type = SCTP_PARAM_IPV4_ADDRESS;
316 param->v4.param_hdr.length = ntohs(length);
317 param->v4.addr.s_addr = addr->v4.sin_addr.s_addr;
318
319 return length;
320}
321
322
323static void sctp_v4_dst_saddr(union sctp_addr *saddr, struct dst_entry *dst,
324 unsigned short port)
325{
326 struct rtable *rt = (struct rtable *)dst;
327 saddr->v4.sin_family = AF_INET;
328 saddr->v4.sin_port = port;
329 saddr->v4.sin_addr.s_addr = rt->rt_src;
330}
331
332
333static int sctp_v4_cmp_addr(const union sctp_addr *addr1,
334 const union sctp_addr *addr2)
335{
336 if (addr1->sa.sa_family != addr2->sa.sa_family)
337 return 0;
338 if (addr1->v4.sin_port != addr2->v4.sin_port)
339 return 0;
340 if (addr1->v4.sin_addr.s_addr != addr2->v4.sin_addr.s_addr)
341 return 0;
342
343 return 1;
344}
345
346
347static void sctp_v4_inaddr_any(union sctp_addr *addr, unsigned short port)
348{
349 addr->v4.sin_family = AF_INET;
350 addr->v4.sin_addr.s_addr = INADDR_ANY;
351 addr->v4.sin_port = port;
352}
353
354
355static int sctp_v4_is_any(const union sctp_addr *addr)
356{
357 return INADDR_ANY == addr->v4.sin_addr.s_addr;
358}
359
360
361
362
363
364
365
366
367static int sctp_v4_addr_valid(union sctp_addr *addr, struct sctp_sock *sp)
368{
369
370 if (IS_IPV4_UNUSABLE_ADDRESS(&addr->v4.sin_addr.s_addr))
371 return 0;
372
373 return 1;
374}
375
376
377static int sctp_v4_available(union sctp_addr *addr, struct sctp_sock *sp)
378{
379 int ret = inet_addr_type(addr->v4.sin_addr.s_addr);
380
381
382
383 if (addr->v4.sin_addr.s_addr != INADDR_ANY && ret != RTN_LOCAL)
384 return 0;
385 return 1;
386}
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402static sctp_scope_t sctp_v4_scope(union sctp_addr *addr)
403{
404 sctp_scope_t retval;
405
406
407
408
409
410
411
412 if (IS_IPV4_UNUSABLE_ADDRESS(&addr->v4.sin_addr.s_addr)) {
413 retval = SCTP_SCOPE_UNUSABLE;
414 } else if (LOOPBACK(addr->v4.sin_addr.s_addr)) {
415 retval = SCTP_SCOPE_LOOPBACK;
416 } else if (IS_IPV4_LINK_ADDRESS(&addr->v4.sin_addr.s_addr)) {
417 retval = SCTP_SCOPE_LINK;
418 } else if (IS_IPV4_PRIVATE_ADDRESS(&addr->v4.sin_addr.s_addr)) {
419 retval = SCTP_SCOPE_PRIVATE;
420 } else {
421 retval = SCTP_SCOPE_GLOBAL;
422 }
423
424 return retval;
425}
426
427
428
429
430
431static struct dst_entry *sctp_v4_get_dst(struct sctp_association *asoc,
432 union sctp_addr *daddr,
433 union sctp_addr *saddr)
434{
435 struct rtable *rt;
436 struct flowi fl;
437 struct sctp_bind_addr *bp;
438 rwlock_t *addr_lock;
439 struct sctp_sockaddr_entry *laddr;
440 struct list_head *pos;
441 struct dst_entry *dst = NULL;
442 union sctp_addr dst_saddr;
443
444 memset(&fl, 0x0, sizeof(struct flowi));
445 fl.fl4_dst = daddr->v4.sin_addr.s_addr;
446 fl.proto = IPPROTO_SCTP;
447 if (asoc) {
448 fl.fl4_tos = RT_CONN_FLAGS(asoc->base.sk);
449 fl.oif = asoc->base.sk->sk_bound_dev_if;
450 }
451 if (saddr)
452 fl.fl4_src = saddr->v4.sin_addr.s_addr;
453
454 SCTP_DEBUG_PRINTK("%s: DST:%u.%u.%u.%u, SRC:%u.%u.%u.%u - ",
455 __FUNCTION__, NIPQUAD(fl.fl4_dst),
456 NIPQUAD(fl.fl4_src));
457
458 if (!ip_route_output_key(&rt, &fl)) {
459 dst = &rt->u.dst;
460 }
461
462
463
464
465 if (!asoc || saddr)
466 goto out;
467
468 bp = &asoc->base.bind_addr;
469 addr_lock = &asoc->base.addr_lock;
470
471 if (dst) {
472
473
474
475 sctp_read_lock(addr_lock);
476 list_for_each(pos, &bp->address_list) {
477 laddr = list_entry(pos, struct sctp_sockaddr_entry,
478 list);
479 sctp_v4_dst_saddr(&dst_saddr, dst, bp->port);
480 if (sctp_v4_cmp_addr(&dst_saddr, &laddr->a))
481 goto out_unlock;
482 }
483 sctp_read_unlock(addr_lock);
484
485
486
487
488 dst_release(dst);
489 dst = NULL;
490 }
491
492
493
494
495 sctp_read_lock(addr_lock);
496 list_for_each(pos, &bp->address_list) {
497 laddr = list_entry(pos, struct sctp_sockaddr_entry, list);
498
499 if (AF_INET == laddr->a.sa.sa_family) {
500 fl.fl4_src = laddr->a.v4.sin_addr.s_addr;
501 if (!ip_route_output_key(&rt, &fl)) {
502 dst = &rt->u.dst;
503 goto out_unlock;
504 }
505 }
506 }
507
508out_unlock:
509 sctp_read_unlock(addr_lock);
510out:
511 if (dst)
512 SCTP_DEBUG_PRINTK("rt_dst:%u.%u.%u.%u, rt_src:%u.%u.%u.%u\n",
513 NIPQUAD(rt->rt_dst), NIPQUAD(rt->rt_src));
514 else
515 SCTP_DEBUG_PRINTK("NO ROUTE\n");
516
517 return dst;
518}
519
520
521
522
523static void sctp_v4_get_saddr(struct sctp_association *asoc,
524 struct dst_entry *dst,
525 union sctp_addr *daddr,
526 union sctp_addr *saddr)
527{
528 struct rtable *rt = (struct rtable *)dst;
529
530 if (rt) {
531 saddr->v4.sin_family = AF_INET;
532 saddr->v4.sin_port = asoc->base.bind_addr.port;
533 saddr->v4.sin_addr.s_addr = rt->rt_src;
534 }
535}
536
537
538static int sctp_v4_skb_iif(const struct sk_buff *skb)
539{
540 return ((struct rtable *)skb->dst)->rt_iif;
541}
542
543
544static int sctp_v4_is_ce(const struct sk_buff *skb)
545{
546 return INET_ECN_is_ce(skb->nh.iph->tos);
547}
548
549
550static struct sock *sctp_v4_create_accept_sk(struct sock *sk,
551 struct sctp_association *asoc)
552{
553 struct sock *newsk;
554 struct inet_sock *inet = inet_sk(sk);
555 struct inet_sock *newinet;
556
557 newsk = sk_alloc(PF_INET, GFP_KERNEL, sk->sk_prot->slab_obj_size,
558 sk->sk_prot->slab);
559 if (!newsk)
560 goto out;
561
562 sock_init_data(NULL, newsk);
563 sk_set_owner(newsk, THIS_MODULE);
564
565 newsk->sk_type = SOCK_STREAM;
566
567 newsk->sk_prot = sk->sk_prot;
568 newsk->sk_no_check = sk->sk_no_check;
569 newsk->sk_reuse = sk->sk_reuse;
570 newsk->sk_shutdown = sk->sk_shutdown;
571
572 newsk->sk_destruct = inet_sock_destruct;
573 newsk->sk_zapped = 0;
574 newsk->sk_family = PF_INET;
575 newsk->sk_protocol = IPPROTO_SCTP;
576 newsk->sk_backlog_rcv = sk->sk_prot->backlog_rcv;
577
578 newinet = inet_sk(newsk);
579
580
581
582
583 newinet->sport = inet->sport;
584 newinet->saddr = inet->saddr;
585 newinet->rcv_saddr = inet->rcv_saddr;
586 newinet->dport = htons(asoc->peer.port);
587 newinet->daddr = asoc->peer.primary_addr.v4.sin_addr.s_addr;
588 newinet->pmtudisc = inet->pmtudisc;
589 newinet->id = 0;
590
591 newinet->uc_ttl = -1;
592 newinet->mc_loop = 1;
593 newinet->mc_ttl = 1;
594 newinet->mc_index = 0;
595 newinet->mc_list = NULL;
596
597#ifdef INET_REFCNT_DEBUG
598 atomic_inc(&inet_sock_nr);
599#endif
600
601 if (newsk->sk_prot->init(newsk)) {
602 sk_common_release(newsk);
603 newsk = NULL;
604 }
605
606out:
607 return newsk;
608}
609
610
611static void sctp_v4_addr_v4map(struct sctp_sock *sp, union sctp_addr *addr)
612{
613
614}
615
616
617static void sctp_v4_seq_dump_addr(struct seq_file *seq, union sctp_addr *addr)
618{
619 seq_printf(seq, "%d.%d.%d.%d ", NIPQUAD(addr->v4.sin_addr));
620}
621
622
623
624
625int sctp_inetaddr_event(struct notifier_block *this, unsigned long ev,
626 void *ptr)
627{
628 unsigned long flags;
629
630 sctp_spin_lock_irqsave(&sctp_local_addr_lock, flags);
631 __sctp_free_local_addr_list();
632 __sctp_get_local_addr_list();
633 sctp_spin_unlock_irqrestore(&sctp_local_addr_lock, flags);
634
635 return NOTIFY_DONE;
636}
637
638
639
640
641
642static int sctp_ctl_sock_init(void)
643{
644 int err;
645 sa_family_t family;
646
647 if (sctp_get_pf_specific(PF_INET6))
648 family = PF_INET6;
649 else
650 family = PF_INET;
651
652 err = sock_create_kern(family, SOCK_SEQPACKET, IPPROTO_SCTP,
653 &sctp_ctl_socket);
654 if (err < 0) {
655 printk(KERN_ERR
656 "SCTP: Failed to create the SCTP control socket.\n");
657 return err;
658 }
659 sctp_ctl_socket->sk->sk_allocation = GFP_ATOMIC;
660 inet_sk(sctp_ctl_socket->sk)->uc_ttl = -1;
661
662 return 0;
663}
664
665
666int sctp_register_af(struct sctp_af *af)
667{
668 switch (af->sa_family) {
669 case AF_INET:
670 if (sctp_af_v4_specific)
671 return 0;
672 sctp_af_v4_specific = af;
673 break;
674 case AF_INET6:
675 if (sctp_af_v6_specific)
676 return 0;
677 sctp_af_v6_specific = af;
678 break;
679 default:
680 return 0;
681 }
682
683 INIT_LIST_HEAD(&af->list);
684 list_add_tail(&af->list, &sctp_address_families);
685 return 1;
686}
687
688
689
690
691struct sctp_af *sctp_get_af_specific(sa_family_t family)
692{
693 switch (family) {
694 case AF_INET:
695 return sctp_af_v4_specific;
696 case AF_INET6:
697 return sctp_af_v6_specific;
698 default:
699 return NULL;
700 }
701}
702
703
704static void sctp_inet_msgname(char *msgname, int *addr_len)
705{
706 struct sockaddr_in *sin;
707
708 sin = (struct sockaddr_in *)msgname;
709 *addr_len = sizeof(struct sockaddr_in);
710 sin->sin_family = AF_INET;
711 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
712}
713
714
715static void sctp_inet_event_msgname(struct sctp_ulpevent *event, char *msgname,
716 int *addr_len)
717{
718 struct sockaddr_in *sin, *sinfrom;
719
720 if (msgname) {
721 struct sctp_association *asoc;
722
723 asoc = event->asoc;
724 sctp_inet_msgname(msgname, addr_len);
725 sin = (struct sockaddr_in *)msgname;
726 sinfrom = &asoc->peer.primary_addr.v4;
727 sin->sin_port = htons(asoc->peer.port);
728 sin->sin_addr.s_addr = sinfrom->sin_addr.s_addr;
729 }
730}
731
732
733static void sctp_inet_skb_msgname(struct sk_buff *skb, char *msgname, int *len)
734{
735 struct sctphdr *sh;
736 struct sockaddr_in *sin;
737
738 if (msgname) {
739 sctp_inet_msgname(msgname, len);
740 sin = (struct sockaddr_in *)msgname;
741 sh = (struct sctphdr *)skb->h.raw;
742 sin->sin_port = sh->source;
743 sin->sin_addr.s_addr = skb->nh.iph->saddr;
744 }
745}
746
747
748static int sctp_inet_af_supported(sa_family_t family, struct sctp_sock *sp)
749{
750
751 return (AF_INET == family);
752}
753
754
755static int sctp_inet_cmp_addr(const union sctp_addr *addr1,
756 const union sctp_addr *addr2,
757 struct sctp_sock *opt)
758{
759
760 if (addr1->sa.sa_family != addr2->sa.sa_family)
761 return 0;
762 if (INADDR_ANY == addr1->v4.sin_addr.s_addr ||
763 INADDR_ANY == addr2->v4.sin_addr.s_addr)
764 return 1;
765 if (addr1->v4.sin_addr.s_addr == addr2->v4.sin_addr.s_addr)
766 return 1;
767
768 return 0;
769}
770
771
772
773
774static int sctp_inet_bind_verify(struct sctp_sock *opt, union sctp_addr *addr)
775{
776 return sctp_v4_available(addr, opt);
777}
778
779
780
781
782static int sctp_inet_send_verify(struct sctp_sock *opt, union sctp_addr *addr)
783{
784 return 1;
785}
786
787
788
789
790static int sctp_inet_supported_addrs(const struct sctp_sock *opt,
791 __u16 *types)
792{
793 types[0] = SCTP_PARAM_IPV4_ADDRESS;
794 return 1;
795}
796
797
798static inline int sctp_v4_xmit(struct sk_buff *skb,
799 struct sctp_transport *transport, int ipfragok)
800{
801 SCTP_DEBUG_PRINTK("%s: skb:%p, len:%d, "
802 "src:%u.%u.%u.%u, dst:%u.%u.%u.%u\n",
803 __FUNCTION__, skb, skb->len,
804 NIPQUAD(((struct rtable *)skb->dst)->rt_src),
805 NIPQUAD(((struct rtable *)skb->dst)->rt_dst));
806
807 SCTP_INC_STATS(SCTP_MIB_OUTSCTPPACKS);
808 return ip_queue_xmit(skb, ipfragok);
809}
810
811static struct sctp_af sctp_ipv4_specific;
812
813static struct sctp_pf sctp_pf_inet = {
814 .event_msgname = sctp_inet_event_msgname,
815 .skb_msgname = sctp_inet_skb_msgname,
816 .af_supported = sctp_inet_af_supported,
817 .cmp_addr = sctp_inet_cmp_addr,
818 .bind_verify = sctp_inet_bind_verify,
819 .send_verify = sctp_inet_send_verify,
820 .supported_addrs = sctp_inet_supported_addrs,
821 .create_accept_sk = sctp_v4_create_accept_sk,
822 .addr_v4map = sctp_v4_addr_v4map,
823 .af = &sctp_ipv4_specific,
824};
825
826
827static struct notifier_block sctp_inetaddr_notifier = {
828 .notifier_call = sctp_inetaddr_event,
829};
830
831
832static struct proto_ops inet_seqpacket_ops = {
833 .family = PF_INET,
834 .owner = THIS_MODULE,
835 .release = inet_release,
836 .bind = inet_bind,
837 .connect = inet_dgram_connect,
838 .socketpair = sock_no_socketpair,
839 .accept = inet_accept,
840 .getname = inet_getname,
841 .poll = sctp_poll,
842 .ioctl = inet_ioctl,
843 .listen = sctp_inet_listen,
844 .shutdown = inet_shutdown,
845 .setsockopt = sock_common_setsockopt,
846 .getsockopt = sock_common_getsockopt,
847 .sendmsg = inet_sendmsg,
848 .recvmsg = sock_common_recvmsg,
849 .mmap = sock_no_mmap,
850 .sendpage = sock_no_sendpage,
851};
852
853
854static struct inet_protosw sctp_seqpacket_protosw = {
855 .type = SOCK_SEQPACKET,
856 .protocol = IPPROTO_SCTP,
857 .prot = &sctp_prot,
858 .ops = &inet_seqpacket_ops,
859 .capability = -1,
860 .no_check = 0,
861 .flags = SCTP_PROTOSW_FLAG
862};
863static struct inet_protosw sctp_stream_protosw = {
864 .type = SOCK_STREAM,
865 .protocol = IPPROTO_SCTP,
866 .prot = &sctp_prot,
867 .ops = &inet_seqpacket_ops,
868 .capability = -1,
869 .no_check = 0,
870 .flags = SCTP_PROTOSW_FLAG
871};
872
873
874static struct net_protocol sctp_protocol = {
875 .handler = sctp_rcv,
876 .err_handler = sctp_v4_err,
877 .no_policy = 1,
878};
879
880
881static struct sctp_af sctp_ipv4_specific = {
882 .sctp_xmit = sctp_v4_xmit,
883 .setsockopt = ip_setsockopt,
884 .getsockopt = ip_getsockopt,
885 .get_dst = sctp_v4_get_dst,
886 .get_saddr = sctp_v4_get_saddr,
887 .copy_addrlist = sctp_v4_copy_addrlist,
888 .from_skb = sctp_v4_from_skb,
889 .from_sk = sctp_v4_from_sk,
890 .to_sk_saddr = sctp_v4_to_sk_saddr,
891 .to_sk_daddr = sctp_v4_to_sk_daddr,
892 .from_addr_param= sctp_v4_from_addr_param,
893 .to_addr_param = sctp_v4_to_addr_param,
894 .dst_saddr = sctp_v4_dst_saddr,
895 .cmp_addr = sctp_v4_cmp_addr,
896 .addr_valid = sctp_v4_addr_valid,
897 .inaddr_any = sctp_v4_inaddr_any,
898 .is_any = sctp_v4_is_any,
899 .available = sctp_v4_available,
900 .scope = sctp_v4_scope,
901 .skb_iif = sctp_v4_skb_iif,
902 .is_ce = sctp_v4_is_ce,
903 .seq_dump_addr = sctp_v4_seq_dump_addr,
904 .net_header_len = sizeof(struct iphdr),
905 .sockaddr_len = sizeof(struct sockaddr_in),
906 .sa_family = AF_INET,
907};
908
909struct sctp_pf *sctp_get_pf_specific(sa_family_t family) {
910
911 switch (family) {
912 case PF_INET:
913 return sctp_pf_inet_specific;
914 case PF_INET6:
915 return sctp_pf_inet6_specific;
916 default:
917 return NULL;
918 }
919}
920
921
922int sctp_register_pf(struct sctp_pf *pf, sa_family_t family)
923{
924 switch (family) {
925 case PF_INET:
926 if (sctp_pf_inet_specific)
927 return 0;
928 sctp_pf_inet_specific = pf;
929 break;
930 case PF_INET6:
931 if (sctp_pf_inet6_specific)
932 return 0;
933 sctp_pf_inet6_specific = pf;
934 break;
935 default:
936 return 0;
937 }
938 return 1;
939}
940
941static int __init init_sctp_mibs(void)
942{
943 sctp_statistics[0] = alloc_percpu(struct sctp_mib);
944 if (!sctp_statistics[0])
945 return -ENOMEM;
946 sctp_statistics[1] = alloc_percpu(struct sctp_mib);
947 if (!sctp_statistics[1]) {
948 free_percpu(sctp_statistics[0]);
949 return -ENOMEM;
950 }
951 return 0;
952
953}
954
955static void cleanup_sctp_mibs(void)
956{
957 free_percpu(sctp_statistics[0]);
958 free_percpu(sctp_statistics[1]);
959}
960
961
962SCTP_STATIC __init int sctp_init(void)
963{
964 int i;
965 int status = -EINVAL;
966 unsigned long goal;
967 int order;
968
969
970 if (!sctp_sanity_check())
971 goto out;
972
973 status = sk_alloc_slab(&sctp_prot, "sctp_sock");
974 if (status)
975 goto out;
976
977
978 status = -EAGAIN;
979 if (inet_add_protocol(&sctp_protocol, IPPROTO_SCTP) < 0)
980 goto err_add_protocol;
981
982
983 inet_register_protosw(&sctp_seqpacket_protosw);
984 inet_register_protosw(&sctp_stream_protosw);
985
986
987 status = -ENOBUFS;
988 sctp_bucket_cachep = kmem_cache_create("sctp_bind_bucket",
989 sizeof(struct sctp_bind_bucket),
990 0, SLAB_HWCACHE_ALIGN,
991 NULL, NULL);
992
993 if (!sctp_bucket_cachep)
994 goto err_bucket_cachep;
995
996 sctp_chunk_cachep = kmem_cache_create("sctp_chunk",
997 sizeof(struct sctp_chunk),
998 0, SLAB_HWCACHE_ALIGN,
999 NULL, NULL);
1000 if (!sctp_chunk_cachep)
1001 goto err_chunk_cachep;
1002
1003
1004 status = init_sctp_mibs();
1005 if (status)
1006 goto err_init_mibs;
1007
1008
1009 status = sctp_proc_init();
1010 if (status)
1011 goto err_init_proc;
1012
1013
1014 sctp_dbg_objcnt_init();
1015
1016
1017 sctp_register_pf(&sctp_pf_inet, PF_INET);
1018
1019
1020
1021
1022
1023 sctp_rto_initial = SCTP_RTO_INITIAL;
1024
1025 sctp_rto_min = SCTP_RTO_MIN;
1026
1027 sctp_rto_max = SCTP_RTO_MAX;
1028
1029 sctp_rto_alpha = SCTP_RTO_ALPHA;
1030
1031 sctp_rto_beta = SCTP_RTO_BETA;
1032
1033
1034 sctp_valid_cookie_life = 60 * HZ;
1035
1036
1037 sctp_cookie_preserve_enable = 1;
1038
1039
1040 sctp_max_burst = SCTP_MAX_BURST;
1041
1042
1043
1044
1045
1046 sctp_max_retrans_association = 10;
1047 sctp_max_retrans_path = 5;
1048 sctp_max_retrans_init = 8;
1049
1050
1051 sctp_hb_interval = 30 * HZ;
1052
1053
1054
1055
1056 sctp_max_instreams = SCTP_DEFAULT_INSTREAMS;
1057 sctp_max_outstreams = SCTP_DEFAULT_OUTSTREAMS;
1058
1059
1060 idr_init(&sctp_assocs_id);
1061
1062
1063
1064
1065 if (num_physpages >= (128 * 1024))
1066 goal = num_physpages >> (22 - PAGE_SHIFT);
1067 else
1068 goal = num_physpages >> (24 - PAGE_SHIFT);
1069
1070 for (order = 0; (1UL << order) < goal; order++)
1071 ;
1072
1073 do {
1074 sctp_assoc_hashsize = (1UL << order) * PAGE_SIZE /
1075 sizeof(struct sctp_hashbucket);
1076 if ((sctp_assoc_hashsize > (64 * 1024)) && order > 0)
1077 continue;
1078 sctp_assoc_hashtable = (struct sctp_hashbucket *)
1079 __get_free_pages(GFP_ATOMIC, order);
1080 } while (!sctp_assoc_hashtable && --order > 0);
1081 if (!sctp_assoc_hashtable) {
1082 printk(KERN_ERR "SCTP: Failed association hash alloc.\n");
1083 status = -ENOMEM;
1084 goto err_ahash_alloc;
1085 }
1086 for (i = 0; i < sctp_assoc_hashsize; i++) {
1087 rwlock_init(&sctp_assoc_hashtable[i].lock);
1088 sctp_assoc_hashtable[i].chain = NULL;
1089 }
1090
1091
1092 sctp_ep_hashsize = 64;
1093 sctp_ep_hashtable = (struct sctp_hashbucket *)
1094 kmalloc(64 * sizeof(struct sctp_hashbucket), GFP_KERNEL);
1095 if (!sctp_ep_hashtable) {
1096 printk(KERN_ERR "SCTP: Failed endpoint_hash alloc.\n");
1097 status = -ENOMEM;
1098 goto err_ehash_alloc;
1099 }
1100 for (i = 0; i < sctp_ep_hashsize; i++) {
1101 rwlock_init(&sctp_ep_hashtable[i].lock);
1102 sctp_ep_hashtable[i].chain = NULL;
1103 }
1104
1105
1106 do {
1107 sctp_port_hashsize = (1UL << order) * PAGE_SIZE /
1108 sizeof(struct sctp_bind_hashbucket);
1109 if ((sctp_port_hashsize > (64 * 1024)) && order > 0)
1110 continue;
1111 sctp_port_hashtable = (struct sctp_bind_hashbucket *)
1112 __get_free_pages(GFP_ATOMIC, order);
1113 } while (!sctp_port_hashtable && --order > 0);
1114 if (!sctp_port_hashtable) {
1115 printk(KERN_ERR "SCTP: Failed bind hash alloc.");
1116 status = -ENOMEM;
1117 goto err_bhash_alloc;
1118 }
1119 for (i = 0; i < sctp_port_hashsize; i++) {
1120 spin_lock_init(&sctp_port_hashtable[i].lock);
1121 sctp_port_hashtable[i].chain = NULL;
1122 }
1123
1124 spin_lock_init(&sctp_port_alloc_lock);
1125 sctp_port_rover = sysctl_local_port_range[0] - 1;
1126
1127 printk(KERN_INFO "SCTP: Hash tables configured "
1128 "(established %d bind %d)\n",
1129 sctp_assoc_hashsize, sctp_port_hashsize);
1130
1131
1132 sctp_addip_enable = 0;
1133
1134
1135 sctp_prsctp_enable = 1;
1136
1137 sctp_sysctl_register();
1138
1139 INIT_LIST_HEAD(&sctp_address_families);
1140 sctp_register_af(&sctp_ipv4_specific);
1141
1142 status = sctp_v6_init();
1143 if (status)
1144 goto err_v6_init;
1145
1146
1147 if ((status = sctp_ctl_sock_init())) {
1148 printk (KERN_ERR
1149 "SCTP: Failed to initialize the SCTP control sock.\n");
1150 goto err_ctl_sock_init;
1151 }
1152
1153
1154 INIT_LIST_HEAD(&sctp_local_addr_list);
1155 spin_lock_init(&sctp_local_addr_lock);
1156
1157
1158 register_inetaddr_notifier(&sctp_inetaddr_notifier);
1159
1160 sctp_get_local_addr_list();
1161
1162 __unsafe(THIS_MODULE);
1163 status = 0;
1164out:
1165 return status;
1166err_add_protocol:
1167 sk_free_slab(&sctp_prot);
1168err_ctl_sock_init:
1169 sctp_v6_exit();
1170err_v6_init:
1171 sctp_sysctl_unregister();
1172 list_del(&sctp_ipv4_specific.list);
1173 free_pages((unsigned long)sctp_port_hashtable,
1174 get_order(sctp_port_hashsize *
1175 sizeof(struct sctp_bind_hashbucket)));
1176err_bhash_alloc:
1177 kfree(sctp_ep_hashtable);
1178err_ehash_alloc:
1179 free_pages((unsigned long)sctp_assoc_hashtable,
1180 get_order(sctp_assoc_hashsize *
1181 sizeof(struct sctp_hashbucket)));
1182err_ahash_alloc:
1183 sctp_dbg_objcnt_exit();
1184err_init_proc:
1185 sctp_proc_exit();
1186 cleanup_sctp_mibs();
1187err_init_mibs:
1188 kmem_cache_destroy(sctp_chunk_cachep);
1189err_chunk_cachep:
1190 kmem_cache_destroy(sctp_bucket_cachep);
1191err_bucket_cachep:
1192 inet_del_protocol(&sctp_protocol, IPPROTO_SCTP);
1193 inet_unregister_protosw(&sctp_seqpacket_protosw);
1194 inet_unregister_protosw(&sctp_stream_protosw);
1195 goto out;
1196}
1197
1198
1199SCTP_STATIC __exit void sctp_exit(void)
1200{
1201
1202
1203
1204
1205
1206 unregister_inetaddr_notifier(&sctp_inetaddr_notifier);
1207
1208
1209 sctp_free_local_addr_list();
1210
1211
1212 sock_release(sctp_ctl_socket);
1213
1214 sctp_v6_exit();
1215 sctp_sysctl_unregister();
1216 list_del(&sctp_ipv4_specific.list);
1217
1218 free_pages((unsigned long)sctp_assoc_hashtable,
1219 get_order(sctp_assoc_hashsize *
1220 sizeof(struct sctp_hashbucket)));
1221 kfree(sctp_ep_hashtable);
1222 free_pages((unsigned long)sctp_port_hashtable,
1223 get_order(sctp_port_hashsize *
1224 sizeof(struct sctp_bind_hashbucket)));
1225
1226 kmem_cache_destroy(sctp_chunk_cachep);
1227 kmem_cache_destroy(sctp_bucket_cachep);
1228
1229 sctp_dbg_objcnt_exit();
1230 sctp_proc_exit();
1231 cleanup_sctp_mibs();
1232
1233 inet_del_protocol(&sctp_protocol, IPPROTO_SCTP);
1234 inet_unregister_protosw(&sctp_seqpacket_protosw);
1235 inet_unregister_protosw(&sctp_stream_protosw);
1236 sk_free_slab(&sctp_prot);
1237}
1238
1239module_init(sctp_init);
1240module_exit(sctp_exit);
1241
1242MODULE_AUTHOR("Linux Kernel SCTP developers <lksctp-developers@lists.sourceforge.net>");
1243MODULE_DESCRIPTION("Support for the SCTP protocol (RFC2960)");
1244MODULE_LICENSE("GPL");
1245