sctp: Fix port hash table size computation
[pandora-kernel.git] / net / sctp / protocol.c
1 /* SCTP kernel implementation
2  * (C) Copyright IBM Corp. 2001, 2004
3  * Copyright (c) 1999-2000 Cisco, Inc.
4  * Copyright (c) 1999-2001 Motorola, Inc.
5  * Copyright (c) 2001 Intel Corp.
6  * Copyright (c) 2001 Nokia, Inc.
7  * Copyright (c) 2001 La Monte H.P. Yarroll
8  *
9  * This file is part of the SCTP kernel implementation
10  *
11  * Initialization/cleanup for SCTP protocol support.
12  *
13  * This SCTP implementation is free software;
14  * you can redistribute it and/or modify it under the terms of
15  * the GNU General Public License as published by
16  * the Free Software Foundation; either version 2, or (at your option)
17  * any later version.
18  *
19  * This SCTP implementation is distributed in the hope that it
20  * will be useful, but WITHOUT ANY WARRANTY; without even the implied
21  *                 ************************
22  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
23  * See the GNU General Public License for more details.
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with GNU CC; see the file COPYING.  If not, write to
27  * the Free Software Foundation, 59 Temple Place - Suite 330,
28  * Boston, MA 02111-1307, USA.
29  *
30  * Please send any bug reports or fixes you make to the
31  * email address(es):
32  *    lksctp developers <lksctp-developers@lists.sourceforge.net>
33  *
34  * Or submit a bug report through the following website:
35  *    http://www.sf.net/projects/lksctp
36  *
37  * Written or modified by:
38  *    La Monte H.P. Yarroll <piggy@acm.org>
39  *    Karl Knutson <karl@athena.chicago.il.us>
40  *    Jon Grimm <jgrimm@us.ibm.com>
41  *    Sridhar Samudrala <sri@us.ibm.com>
42  *    Daisy Chang <daisyc@us.ibm.com>
43  *    Ardelle Fan <ardelle.fan@intel.com>
44  *
45  * Any bugs reported given to us we will try to fix... any fixes shared will
46  * be incorporated into the next SCTP release.
47  */
48
49 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
50
51 #include <linux/module.h>
52 #include <linux/init.h>
53 #include <linux/netdevice.h>
54 #include <linux/inetdevice.h>
55 #include <linux/seq_file.h>
56 #include <linux/bootmem.h>
57 #include <linux/highmem.h>
58 #include <linux/swap.h>
59 #include <linux/slab.h>
60 #include <net/net_namespace.h>
61 #include <net/protocol.h>
62 #include <net/ip.h>
63 #include <net/ipv6.h>
64 #include <net/route.h>
65 #include <net/sctp/sctp.h>
66 #include <net/addrconf.h>
67 #include <net/inet_common.h>
68 #include <net/inet_ecn.h>
69
70 #define MAX_SCTP_PORT_HASH_ENTRIES (64 * 1024)
71
72 /* Global data structures. */
73 struct sctp_globals sctp_globals __read_mostly;
74 DEFINE_SNMP_STAT(struct sctp_mib, sctp_statistics) __read_mostly;
75
76 #ifdef CONFIG_PROC_FS
77 struct proc_dir_entry   *proc_net_sctp;
78 #endif
79
80 struct idr sctp_assocs_id;
81 DEFINE_SPINLOCK(sctp_assocs_id_lock);
82
83 /* This is the global socket data structure used for responding to
84  * the Out-of-the-blue (OOTB) packets.  A control sock will be created
85  * for this socket at the initialization time.
86  */
87 static struct sock *sctp_ctl_sock;
88
89 static struct sctp_pf *sctp_pf_inet6_specific;
90 static struct sctp_pf *sctp_pf_inet_specific;
91 static struct sctp_af *sctp_af_v4_specific;
92 static struct sctp_af *sctp_af_v6_specific;
93
94 struct kmem_cache *sctp_chunk_cachep __read_mostly;
95 struct kmem_cache *sctp_bucket_cachep __read_mostly;
96
97 long sysctl_sctp_mem[3];
98 int sysctl_sctp_rmem[3];
99 int sysctl_sctp_wmem[3];
100
101 /* Return the address of the control sock. */
102 struct sock *sctp_get_ctl_sock(void)
103 {
104         return sctp_ctl_sock;
105 }
106
107 /* Set up the proc fs entry for the SCTP protocol. */
108 static __init int sctp_proc_init(void)
109 {
110         if (percpu_counter_init(&sctp_sockets_allocated, 0))
111                 goto out_nomem;
112 #ifdef CONFIG_PROC_FS
113         if (!proc_net_sctp) {
114                 proc_net_sctp = proc_mkdir("sctp", init_net.proc_net);
115                 if (!proc_net_sctp)
116                         goto out_free_percpu;
117         }
118
119         if (sctp_snmp_proc_init())
120                 goto out_snmp_proc_init;
121         if (sctp_eps_proc_init())
122                 goto out_eps_proc_init;
123         if (sctp_assocs_proc_init())
124                 goto out_assocs_proc_init;
125         if (sctp_remaddr_proc_init())
126                 goto out_remaddr_proc_init;
127
128         return 0;
129
130 out_remaddr_proc_init:
131         sctp_assocs_proc_exit();
132 out_assocs_proc_init:
133         sctp_eps_proc_exit();
134 out_eps_proc_init:
135         sctp_snmp_proc_exit();
136 out_snmp_proc_init:
137         if (proc_net_sctp) {
138                 proc_net_sctp = NULL;
139                 remove_proc_entry("sctp", init_net.proc_net);
140         }
141 out_free_percpu:
142         percpu_counter_destroy(&sctp_sockets_allocated);
143 #else
144         return 0;
145 #endif /* CONFIG_PROC_FS */
146
147 out_nomem:
148         return -ENOMEM;
149 }
150
151 /* Clean up the proc fs entry for the SCTP protocol.
152  * Note: Do not make this __exit as it is used in the init error
153  * path.
154  */
155 static void sctp_proc_exit(void)
156 {
157 #ifdef CONFIG_PROC_FS
158         sctp_snmp_proc_exit();
159         sctp_eps_proc_exit();
160         sctp_assocs_proc_exit();
161         sctp_remaddr_proc_exit();
162
163         if (proc_net_sctp) {
164                 proc_net_sctp = NULL;
165                 remove_proc_entry("sctp", init_net.proc_net);
166         }
167 #endif
168         percpu_counter_destroy(&sctp_sockets_allocated);
169 }
170
171 /* Private helper to extract ipv4 address and stash them in
172  * the protocol structure.
173  */
174 static void sctp_v4_copy_addrlist(struct list_head *addrlist,
175                                   struct net_device *dev)
176 {
177         struct in_device *in_dev;
178         struct in_ifaddr *ifa;
179         struct sctp_sockaddr_entry *addr;
180
181         rcu_read_lock();
182         if ((in_dev = __in_dev_get_rcu(dev)) == NULL) {
183                 rcu_read_unlock();
184                 return;
185         }
186
187         for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
188                 /* Add the address to the local list.  */
189                 addr = t_new(struct sctp_sockaddr_entry, GFP_ATOMIC);
190                 if (addr) {
191                         addr->a.v4.sin_family = AF_INET;
192                         addr->a.v4.sin_port = 0;
193                         addr->a.v4.sin_addr.s_addr = ifa->ifa_local;
194                         addr->valid = 1;
195                         INIT_LIST_HEAD(&addr->list);
196                         list_add_tail(&addr->list, addrlist);
197                 }
198         }
199
200         rcu_read_unlock();
201 }
202
203 /* Extract our IP addresses from the system and stash them in the
204  * protocol structure.
205  */
206 static void sctp_get_local_addr_list(void)
207 {
208         struct net_device *dev;
209         struct list_head *pos;
210         struct sctp_af *af;
211
212         rcu_read_lock();
213         for_each_netdev_rcu(&init_net, dev) {
214                 __list_for_each(pos, &sctp_address_families) {
215                         af = list_entry(pos, struct sctp_af, list);
216                         af->copy_addrlist(&sctp_local_addr_list, dev);
217                 }
218         }
219         rcu_read_unlock();
220 }
221
222 /* Free the existing local addresses.  */
223 static void sctp_free_local_addr_list(void)
224 {
225         struct sctp_sockaddr_entry *addr;
226         struct list_head *pos, *temp;
227
228         list_for_each_safe(pos, temp, &sctp_local_addr_list) {
229                 addr = list_entry(pos, struct sctp_sockaddr_entry, list);
230                 list_del(pos);
231                 kfree(addr);
232         }
233 }
234
235 /* Copy the local addresses which are valid for 'scope' into 'bp'.  */
236 int sctp_copy_local_addr_list(struct sctp_bind_addr *bp, sctp_scope_t scope,
237                               gfp_t gfp, int copy_flags)
238 {
239         struct sctp_sockaddr_entry *addr;
240         int error = 0;
241
242         rcu_read_lock();
243         list_for_each_entry_rcu(addr, &sctp_local_addr_list, list) {
244                 if (!addr->valid)
245                         continue;
246                 if (sctp_in_scope(&addr->a, scope)) {
247                         /* Now that the address is in scope, check to see if
248                          * the address type is really supported by the local
249                          * sock as well as the remote peer.
250                          */
251                         if ((((AF_INET == addr->a.sa.sa_family) &&
252                               (copy_flags & SCTP_ADDR4_PEERSUPP))) ||
253                             (((AF_INET6 == addr->a.sa.sa_family) &&
254                               (copy_flags & SCTP_ADDR6_ALLOWED) &&
255                               (copy_flags & SCTP_ADDR6_PEERSUPP)))) {
256                                 error = sctp_add_bind_addr(bp, &addr->a,
257                                                     SCTP_ADDR_SRC, GFP_ATOMIC);
258                                 if (error)
259                                         goto end_copy;
260                         }
261                 }
262         }
263
264 end_copy:
265         rcu_read_unlock();
266         return error;
267 }
268
269 /* Initialize a sctp_addr from in incoming skb.  */
270 static void sctp_v4_from_skb(union sctp_addr *addr, struct sk_buff *skb,
271                              int is_saddr)
272 {
273         void *from;
274         __be16 *port;
275         struct sctphdr *sh;
276
277         port = &addr->v4.sin_port;
278         addr->v4.sin_family = AF_INET;
279
280         sh = sctp_hdr(skb);
281         if (is_saddr) {
282                 *port  = sh->source;
283                 from = &ip_hdr(skb)->saddr;
284         } else {
285                 *port = sh->dest;
286                 from = &ip_hdr(skb)->daddr;
287         }
288         memcpy(&addr->v4.sin_addr.s_addr, from, sizeof(struct in_addr));
289 }
290
291 /* Initialize an sctp_addr from a socket. */
292 static void sctp_v4_from_sk(union sctp_addr *addr, struct sock *sk)
293 {
294         addr->v4.sin_family = AF_INET;
295         addr->v4.sin_port = 0;
296         addr->v4.sin_addr.s_addr = inet_sk(sk)->inet_rcv_saddr;
297 }
298
299 /* Initialize sk->sk_rcv_saddr from sctp_addr. */
300 static void sctp_v4_to_sk_saddr(union sctp_addr *addr, struct sock *sk)
301 {
302         inet_sk(sk)->inet_rcv_saddr = addr->v4.sin_addr.s_addr;
303 }
304
305 /* Initialize sk->sk_daddr from sctp_addr. */
306 static void sctp_v4_to_sk_daddr(union sctp_addr *addr, struct sock *sk)
307 {
308         inet_sk(sk)->inet_daddr = addr->v4.sin_addr.s_addr;
309 }
310
311 /* Initialize a sctp_addr from an address parameter. */
312 static void sctp_v4_from_addr_param(union sctp_addr *addr,
313                                     union sctp_addr_param *param,
314                                     __be16 port, int iif)
315 {
316         addr->v4.sin_family = AF_INET;
317         addr->v4.sin_port = port;
318         addr->v4.sin_addr.s_addr = param->v4.addr.s_addr;
319 }
320
321 /* Initialize an address parameter from a sctp_addr and return the length
322  * of the address parameter.
323  */
324 static int sctp_v4_to_addr_param(const union sctp_addr *addr,
325                                  union sctp_addr_param *param)
326 {
327         int length = sizeof(sctp_ipv4addr_param_t);
328
329         param->v4.param_hdr.type = SCTP_PARAM_IPV4_ADDRESS;
330         param->v4.param_hdr.length = htons(length);
331         param->v4.addr.s_addr = addr->v4.sin_addr.s_addr;
332
333         return length;
334 }
335
336 /* Initialize a sctp_addr from a dst_entry. */
337 static void sctp_v4_dst_saddr(union sctp_addr *saddr, struct flowi4 *fl4,
338                               __be16 port)
339 {
340         saddr->v4.sin_family = AF_INET;
341         saddr->v4.sin_port = port;
342         saddr->v4.sin_addr.s_addr = fl4->saddr;
343 }
344
345 /* Compare two addresses exactly. */
346 static int sctp_v4_cmp_addr(const union sctp_addr *addr1,
347                             const union sctp_addr *addr2)
348 {
349         if (addr1->sa.sa_family != addr2->sa.sa_family)
350                 return 0;
351         if (addr1->v4.sin_port != addr2->v4.sin_port)
352                 return 0;
353         if (addr1->v4.sin_addr.s_addr != addr2->v4.sin_addr.s_addr)
354                 return 0;
355
356         return 1;
357 }
358
359 /* Initialize addr struct to INADDR_ANY. */
360 static void sctp_v4_inaddr_any(union sctp_addr *addr, __be16 port)
361 {
362         addr->v4.sin_family = AF_INET;
363         addr->v4.sin_addr.s_addr = htonl(INADDR_ANY);
364         addr->v4.sin_port = port;
365 }
366
367 /* Is this a wildcard address? */
368 static int sctp_v4_is_any(const union sctp_addr *addr)
369 {
370         return htonl(INADDR_ANY) == addr->v4.sin_addr.s_addr;
371 }
372
373 /* This function checks if the address is a valid address to be used for
374  * SCTP binding.
375  *
376  * Output:
377  * Return 0 - If the address is a non-unicast or an illegal address.
378  * Return 1 - If the address is a unicast.
379  */
380 static int sctp_v4_addr_valid(union sctp_addr *addr,
381                               struct sctp_sock *sp,
382                               const struct sk_buff *skb)
383 {
384         /* IPv4 addresses not allowed */
385         if (sp && ipv6_only_sock(sctp_opt2sk(sp)))
386                 return 0;
387
388         /* Is this a non-unicast address or a unusable SCTP address? */
389         if (IS_IPV4_UNUSABLE_ADDRESS(addr->v4.sin_addr.s_addr))
390                 return 0;
391
392         /* Is this a broadcast address? */
393         if (skb && skb_rtable(skb)->rt_flags & RTCF_BROADCAST)
394                 return 0;
395
396         return 1;
397 }
398
399 /* Should this be available for binding?   */
400 static int sctp_v4_available(union sctp_addr *addr, struct sctp_sock *sp)
401 {
402         int ret = inet_addr_type(&init_net, addr->v4.sin_addr.s_addr);
403
404
405         if (addr->v4.sin_addr.s_addr != htonl(INADDR_ANY) &&
406            ret != RTN_LOCAL &&
407            !sp->inet.freebind &&
408            !sysctl_ip_nonlocal_bind)
409                 return 0;
410
411         if (ipv6_only_sock(sctp_opt2sk(sp)))
412                 return 0;
413
414         return 1;
415 }
416
417 /* Checking the loopback, private and other address scopes as defined in
418  * RFC 1918.   The IPv4 scoping is based on the draft for SCTP IPv4
419  * scoping <draft-stewart-tsvwg-sctp-ipv4-00.txt>.
420  *
421  * Level 0 - unusable SCTP addresses
422  * Level 1 - loopback address
423  * Level 2 - link-local addresses
424  * Level 3 - private addresses.
425  * Level 4 - global addresses
426  * For INIT and INIT-ACK address list, let L be the level of
427  * of requested destination address, sender and receiver
428  * SHOULD include all of its addresses with level greater
429  * than or equal to L.
430  *
431  * IPv4 scoping can be controlled through sysctl option
432  * net.sctp.addr_scope_policy
433  */
434 static sctp_scope_t sctp_v4_scope(union sctp_addr *addr)
435 {
436         sctp_scope_t retval;
437
438         /* Check for unusable SCTP addresses. */
439         if (IS_IPV4_UNUSABLE_ADDRESS(addr->v4.sin_addr.s_addr)) {
440                 retval =  SCTP_SCOPE_UNUSABLE;
441         } else if (ipv4_is_loopback(addr->v4.sin_addr.s_addr)) {
442                 retval = SCTP_SCOPE_LOOPBACK;
443         } else if (ipv4_is_linklocal_169(addr->v4.sin_addr.s_addr)) {
444                 retval = SCTP_SCOPE_LINK;
445         } else if (ipv4_is_private_10(addr->v4.sin_addr.s_addr) ||
446                    ipv4_is_private_172(addr->v4.sin_addr.s_addr) ||
447                    ipv4_is_private_192(addr->v4.sin_addr.s_addr)) {
448                 retval = SCTP_SCOPE_PRIVATE;
449         } else {
450                 retval = SCTP_SCOPE_GLOBAL;
451         }
452
453         return retval;
454 }
455
456 /* Returns a valid dst cache entry for the given source and destination ip
457  * addresses. If an association is passed, trys to get a dst entry with a
458  * source address that matches an address in the bind address list.
459  */
460 static void sctp_v4_get_dst(struct sctp_transport *t, union sctp_addr *saddr,
461                                 struct flowi *fl, struct sock *sk)
462 {
463         struct sctp_association *asoc = t->asoc;
464         struct rtable *rt;
465         struct flowi4 *fl4 = &fl->u.ip4;
466         struct sctp_bind_addr *bp;
467         struct sctp_sockaddr_entry *laddr;
468         struct dst_entry *dst = NULL;
469         union sctp_addr *daddr = &t->ipaddr;
470         union sctp_addr dst_saddr;
471
472         memset(fl4, 0x0, sizeof(struct flowi4));
473         fl4->daddr  = daddr->v4.sin_addr.s_addr;
474         fl4->fl4_dport = daddr->v4.sin_port;
475         fl4->flowi4_proto = IPPROTO_SCTP;
476         if (asoc) {
477                 fl4->flowi4_tos = RT_CONN_FLAGS(asoc->base.sk);
478                 fl4->flowi4_oif = asoc->base.sk->sk_bound_dev_if;
479                 fl4->fl4_sport = htons(asoc->base.bind_addr.port);
480         }
481         if (saddr) {
482                 fl4->saddr = saddr->v4.sin_addr.s_addr;
483                 fl4->fl4_sport = saddr->v4.sin_port;
484         }
485
486         SCTP_DEBUG_PRINTK("%s: DST:%pI4, SRC:%pI4 - ",
487                           __func__, &fl4->daddr, &fl4->saddr);
488
489         rt = ip_route_output_key(&init_net, fl4);
490         if (!IS_ERR(rt))
491                 dst = &rt->dst;
492
493         /* If there is no association or if a source address is passed, no
494          * more validation is required.
495          */
496         if (!asoc || saddr)
497                 goto out;
498
499         bp = &asoc->base.bind_addr;
500
501         if (dst) {
502                 /* Walk through the bind address list and look for a bind
503                  * address that matches the source address of the returned dst.
504                  */
505                 sctp_v4_dst_saddr(&dst_saddr, fl4, htons(bp->port));
506                 rcu_read_lock();
507                 list_for_each_entry_rcu(laddr, &bp->address_list, list) {
508                         if (!laddr->valid || (laddr->state == SCTP_ADDR_DEL) ||
509                             (laddr->state != SCTP_ADDR_SRC &&
510                             !asoc->src_out_of_asoc_ok))
511                                 continue;
512                         if (sctp_v4_cmp_addr(&dst_saddr, &laddr->a))
513                                 goto out_unlock;
514                 }
515                 rcu_read_unlock();
516
517                 /* None of the bound addresses match the source address of the
518                  * dst. So release it.
519                  */
520                 dst_release(dst);
521                 dst = NULL;
522         }
523
524         /* Walk through the bind address list and try to get a dst that
525          * matches a bind address as the source address.
526          */
527         rcu_read_lock();
528         list_for_each_entry_rcu(laddr, &bp->address_list, list) {
529                 if (!laddr->valid)
530                         continue;
531                 if ((laddr->state == SCTP_ADDR_SRC) &&
532                     (AF_INET == laddr->a.sa.sa_family)) {
533                         fl4->fl4_sport = laddr->a.v4.sin_port;
534                         flowi4_update_output(fl4,
535                                              asoc->base.sk->sk_bound_dev_if,
536                                              RT_CONN_FLAGS(asoc->base.sk),
537                                              daddr->v4.sin_addr.s_addr,
538                                              laddr->a.v4.sin_addr.s_addr);
539
540                         rt = ip_route_output_key(&init_net, fl4);
541                         if (!IS_ERR(rt)) {
542                                 dst = &rt->dst;
543                                 goto out_unlock;
544                         }
545                 }
546         }
547
548 out_unlock:
549         rcu_read_unlock();
550 out:
551         t->dst = dst;
552         if (dst)
553                 SCTP_DEBUG_PRINTK("rt_dst:%pI4, rt_src:%pI4\n",
554                                   &fl4->daddr, &fl4->saddr);
555         else
556                 SCTP_DEBUG_PRINTK("NO ROUTE\n");
557 }
558
559 /* For v4, the source address is cached in the route entry(dst). So no need
560  * to cache it separately and hence this is an empty routine.
561  */
562 static void sctp_v4_get_saddr(struct sctp_sock *sk,
563                               struct sctp_transport *t,
564                               struct flowi *fl)
565 {
566         union sctp_addr *saddr = &t->saddr;
567         struct rtable *rt = (struct rtable *)t->dst;
568
569         if (rt) {
570                 saddr->v4.sin_family = AF_INET;
571                 saddr->v4.sin_addr.s_addr = fl->u.ip4.saddr;
572         }
573 }
574
575 /* What interface did this skb arrive on? */
576 static int sctp_v4_skb_iif(const struct sk_buff *skb)
577 {
578         return skb_rtable(skb)->rt_iif;
579 }
580
581 /* Was this packet marked by Explicit Congestion Notification? */
582 static int sctp_v4_is_ce(const struct sk_buff *skb)
583 {
584         return INET_ECN_is_ce(ip_hdr(skb)->tos);
585 }
586
587 /* Create and initialize a new sk for the socket returned by accept(). */
588 static struct sock *sctp_v4_create_accept_sk(struct sock *sk,
589                                              struct sctp_association *asoc)
590 {
591         struct sock *newsk = sk_alloc(sock_net(sk), PF_INET, GFP_KERNEL,
592                         sk->sk_prot);
593         struct inet_sock *newinet;
594
595         if (!newsk)
596                 goto out;
597
598         sock_init_data(NULL, newsk);
599
600         sctp_copy_sock(newsk, sk, asoc);
601         sock_reset_flag(newsk, SOCK_ZAPPED);
602
603         newinet = inet_sk(newsk);
604
605         newinet->inet_daddr = asoc->peer.primary_addr.v4.sin_addr.s_addr;
606
607         sk_refcnt_debug_inc(newsk);
608
609         if (newsk->sk_prot->init(newsk)) {
610                 sk_common_release(newsk);
611                 newsk = NULL;
612         }
613
614 out:
615         return newsk;
616 }
617
618 /* Map address, empty for v4 family */
619 static void sctp_v4_addr_v4map(struct sctp_sock *sp, union sctp_addr *addr)
620 {
621         /* Empty */
622 }
623
624 /* Dump the v4 addr to the seq file. */
625 static void sctp_v4_seq_dump_addr(struct seq_file *seq, union sctp_addr *addr)
626 {
627         seq_printf(seq, "%pI4 ", &addr->v4.sin_addr);
628 }
629
630 static void sctp_v4_ecn_capable(struct sock *sk)
631 {
632         INET_ECN_xmit(sk);
633 }
634
635 void sctp_addr_wq_timeout_handler(unsigned long arg)
636 {
637         struct sctp_sockaddr_entry *addrw, *temp;
638         struct sctp_sock *sp;
639
640         spin_lock_bh(&sctp_addr_wq_lock);
641
642         list_for_each_entry_safe(addrw, temp, &sctp_addr_waitq, list) {
643                 SCTP_DEBUG_PRINTK_IPADDR("sctp_addrwq_timo_handler: the first ent in wq %p is ",
644                     " for cmd %d at entry %p\n", &sctp_addr_waitq, &addrw->a, addrw->state,
645                     addrw);
646
647 #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
648                 /* Now we send an ASCONF for each association */
649                 /* Note. we currently don't handle link local IPv6 addressees */
650                 if (addrw->a.sa.sa_family == AF_INET6) {
651                         struct in6_addr *in6;
652
653                         if (ipv6_addr_type(&addrw->a.v6.sin6_addr) &
654                             IPV6_ADDR_LINKLOCAL)
655                                 goto free_next;
656
657                         in6 = (struct in6_addr *)&addrw->a.v6.sin6_addr;
658                         if (ipv6_chk_addr(&init_net, in6, NULL, 0) == 0 &&
659                             addrw->state == SCTP_ADDR_NEW) {
660                                 unsigned long timeo_val;
661
662                                 SCTP_DEBUG_PRINTK("sctp_timo_handler: this is on DAD, trying %d sec later\n",
663                                     SCTP_ADDRESS_TICK_DELAY);
664                                 timeo_val = jiffies;
665                                 timeo_val += msecs_to_jiffies(SCTP_ADDRESS_TICK_DELAY);
666                                 mod_timer(&sctp_addr_wq_timer, timeo_val);
667                                 break;
668                         }
669                 }
670 #endif
671                 list_for_each_entry(sp, &sctp_auto_asconf_splist, auto_asconf_list) {
672                         struct sock *sk;
673
674                         sk = sctp_opt2sk(sp);
675                         /* ignore bound-specific endpoints */
676                         if (!sctp_is_ep_boundall(sk))
677                                 continue;
678                         sctp_bh_lock_sock(sk);
679                         if (sctp_asconf_mgmt(sp, addrw) < 0)
680                                 SCTP_DEBUG_PRINTK("sctp_addrwq_timo_handler: sctp_asconf_mgmt failed\n");
681                         sctp_bh_unlock_sock(sk);
682                 }
683 free_next:
684                 list_del(&addrw->list);
685                 kfree(addrw);
686         }
687         spin_unlock_bh(&sctp_addr_wq_lock);
688 }
689
690 static void sctp_free_addr_wq(void)
691 {
692         struct sctp_sockaddr_entry *addrw;
693         struct sctp_sockaddr_entry *temp;
694
695         spin_lock_bh(&sctp_addr_wq_lock);
696         del_timer(&sctp_addr_wq_timer);
697         list_for_each_entry_safe(addrw, temp, &sctp_addr_waitq, list) {
698                 list_del(&addrw->list);
699                 kfree(addrw);
700         }
701         spin_unlock_bh(&sctp_addr_wq_lock);
702 }
703
704 /* lookup the entry for the same address in the addr_waitq
705  * sctp_addr_wq MUST be locked
706  */
707 static struct sctp_sockaddr_entry *sctp_addr_wq_lookup(struct sctp_sockaddr_entry *addr)
708 {
709         struct sctp_sockaddr_entry *addrw;
710
711         list_for_each_entry(addrw, &sctp_addr_waitq, list) {
712                 if (addrw->a.sa.sa_family != addr->a.sa.sa_family)
713                         continue;
714                 if (addrw->a.sa.sa_family == AF_INET) {
715                         if (addrw->a.v4.sin_addr.s_addr ==
716                             addr->a.v4.sin_addr.s_addr)
717                                 return addrw;
718                 } else if (addrw->a.sa.sa_family == AF_INET6) {
719                         if (ipv6_addr_equal(&addrw->a.v6.sin6_addr,
720                             &addr->a.v6.sin6_addr))
721                                 return addrw;
722                 }
723         }
724         return NULL;
725 }
726
727 void sctp_addr_wq_mgmt(struct sctp_sockaddr_entry *addr, int cmd)
728 {
729         struct sctp_sockaddr_entry *addrw;
730         unsigned long timeo_val;
731
732         /* first, we check if an opposite message already exist in the queue.
733          * If we found such message, it is removed.
734          * This operation is a bit stupid, but the DHCP client attaches the
735          * new address after a couple of addition and deletion of that address
736          */
737
738         spin_lock_bh(&sctp_addr_wq_lock);
739         /* Offsets existing events in addr_wq */
740         addrw = sctp_addr_wq_lookup(addr);
741         if (addrw) {
742                 if (addrw->state != cmd) {
743                         SCTP_DEBUG_PRINTK_IPADDR("sctp_addr_wq_mgmt offsets existing entry for %d ",
744                             " in wq %p\n", addrw->state, &addrw->a,
745                             &sctp_addr_waitq);
746                         list_del(&addrw->list);
747                         kfree(addrw);
748                 }
749                 spin_unlock_bh(&sctp_addr_wq_lock);
750                 return;
751         }
752
753         /* OK, we have to add the new address to the wait queue */
754         addrw = kmemdup(addr, sizeof(struct sctp_sockaddr_entry), GFP_ATOMIC);
755         if (addrw == NULL) {
756                 spin_unlock_bh(&sctp_addr_wq_lock);
757                 return;
758         }
759         addrw->state = cmd;
760         list_add_tail(&addrw->list, &sctp_addr_waitq);
761         SCTP_DEBUG_PRINTK_IPADDR("sctp_addr_wq_mgmt add new entry for cmd:%d ",
762             " in wq %p\n", addrw->state, &addrw->a, &sctp_addr_waitq);
763
764         if (!timer_pending(&sctp_addr_wq_timer)) {
765                 timeo_val = jiffies;
766                 timeo_val += msecs_to_jiffies(SCTP_ADDRESS_TICK_DELAY);
767                 mod_timer(&sctp_addr_wq_timer, timeo_val);
768         }
769         spin_unlock_bh(&sctp_addr_wq_lock);
770 }
771
772 /* Event handler for inet address addition/deletion events.
773  * The sctp_local_addr_list needs to be protocted by a spin lock since
774  * multiple notifiers (say IPv4 and IPv6) may be running at the same
775  * time and thus corrupt the list.
776  * The reader side is protected with RCU.
777  */
778 static int sctp_inetaddr_event(struct notifier_block *this, unsigned long ev,
779                                void *ptr)
780 {
781         struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
782         struct sctp_sockaddr_entry *addr = NULL;
783         struct sctp_sockaddr_entry *temp;
784         int found = 0;
785
786         if (!net_eq(dev_net(ifa->ifa_dev->dev), &init_net))
787                 return NOTIFY_DONE;
788
789         switch (ev) {
790         case NETDEV_UP:
791                 addr = kmalloc(sizeof(struct sctp_sockaddr_entry), GFP_ATOMIC);
792                 if (addr) {
793                         addr->a.v4.sin_family = AF_INET;
794                         addr->a.v4.sin_port = 0;
795                         addr->a.v4.sin_addr.s_addr = ifa->ifa_local;
796                         addr->valid = 1;
797                         spin_lock_bh(&sctp_local_addr_lock);
798                         list_add_tail_rcu(&addr->list, &sctp_local_addr_list);
799                         sctp_addr_wq_mgmt(addr, SCTP_ADDR_NEW);
800                         spin_unlock_bh(&sctp_local_addr_lock);
801                 }
802                 break;
803         case NETDEV_DOWN:
804                 spin_lock_bh(&sctp_local_addr_lock);
805                 list_for_each_entry_safe(addr, temp,
806                                         &sctp_local_addr_list, list) {
807                         if (addr->a.sa.sa_family == AF_INET &&
808                                         addr->a.v4.sin_addr.s_addr ==
809                                         ifa->ifa_local) {
810                                 sctp_addr_wq_mgmt(addr, SCTP_ADDR_DEL);
811                                 found = 1;
812                                 addr->valid = 0;
813                                 list_del_rcu(&addr->list);
814                                 break;
815                         }
816                 }
817                 spin_unlock_bh(&sctp_local_addr_lock);
818                 if (found)
819                         kfree_rcu(addr, rcu);
820                 break;
821         }
822
823         return NOTIFY_DONE;
824 }
825
826 /*
827  * Initialize the control inode/socket with a control endpoint data
828  * structure.  This endpoint is reserved exclusively for the OOTB processing.
829  */
830 static int sctp_ctl_sock_init(void)
831 {
832         int err;
833         sa_family_t family = PF_INET;
834
835         if (sctp_get_pf_specific(PF_INET6))
836                 family = PF_INET6;
837
838         err = inet_ctl_sock_create(&sctp_ctl_sock, family,
839                                    SOCK_SEQPACKET, IPPROTO_SCTP, &init_net);
840
841         /* If IPv6 socket could not be created, try the IPv4 socket */
842         if (err < 0 && family == PF_INET6)
843                 err = inet_ctl_sock_create(&sctp_ctl_sock, AF_INET,
844                                            SOCK_SEQPACKET, IPPROTO_SCTP,
845                                            &init_net);
846
847         if (err < 0) {
848                 pr_err("Failed to create the SCTP control socket\n");
849                 return err;
850         }
851         return 0;
852 }
853
854 /* Register address family specific functions. */
855 int sctp_register_af(struct sctp_af *af)
856 {
857         switch (af->sa_family) {
858         case AF_INET:
859                 if (sctp_af_v4_specific)
860                         return 0;
861                 sctp_af_v4_specific = af;
862                 break;
863         case AF_INET6:
864                 if (sctp_af_v6_specific)
865                         return 0;
866                 sctp_af_v6_specific = af;
867                 break;
868         default:
869                 return 0;
870         }
871
872         INIT_LIST_HEAD(&af->list);
873         list_add_tail(&af->list, &sctp_address_families);
874         return 1;
875 }
876
877 /* Get the table of functions for manipulating a particular address
878  * family.
879  */
880 struct sctp_af *sctp_get_af_specific(sa_family_t family)
881 {
882         switch (family) {
883         case AF_INET:
884                 return sctp_af_v4_specific;
885         case AF_INET6:
886                 return sctp_af_v6_specific;
887         default:
888                 return NULL;
889         }
890 }
891
892 /* Common code to initialize a AF_INET msg_name. */
893 static void sctp_inet_msgname(char *msgname, int *addr_len)
894 {
895         struct sockaddr_in *sin;
896
897         sin = (struct sockaddr_in *)msgname;
898         *addr_len = sizeof(struct sockaddr_in);
899         sin->sin_family = AF_INET;
900         memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
901 }
902
903 /* Copy the primary address of the peer primary address as the msg_name. */
904 static void sctp_inet_event_msgname(struct sctp_ulpevent *event, char *msgname,
905                                     int *addr_len)
906 {
907         struct sockaddr_in *sin, *sinfrom;
908
909         if (msgname) {
910                 struct sctp_association *asoc;
911
912                 asoc = event->asoc;
913                 sctp_inet_msgname(msgname, addr_len);
914                 sin = (struct sockaddr_in *)msgname;
915                 sinfrom = &asoc->peer.primary_addr.v4;
916                 sin->sin_port = htons(asoc->peer.port);
917                 sin->sin_addr.s_addr = sinfrom->sin_addr.s_addr;
918         }
919 }
920
921 /* Initialize and copy out a msgname from an inbound skb. */
922 static void sctp_inet_skb_msgname(struct sk_buff *skb, char *msgname, int *len)
923 {
924         if (msgname) {
925                 struct sctphdr *sh = sctp_hdr(skb);
926                 struct sockaddr_in *sin = (struct sockaddr_in *)msgname;
927
928                 sctp_inet_msgname(msgname, len);
929                 sin->sin_port = sh->source;
930                 sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
931         }
932 }
933
934 /* Do we support this AF? */
935 static int sctp_inet_af_supported(sa_family_t family, struct sctp_sock *sp)
936 {
937         /* PF_INET only supports AF_INET addresses. */
938         return AF_INET == family;
939 }
940
941 /* Address matching with wildcards allowed. */
942 static int sctp_inet_cmp_addr(const union sctp_addr *addr1,
943                               const union sctp_addr *addr2,
944                               struct sctp_sock *opt)
945 {
946         /* PF_INET only supports AF_INET addresses. */
947         if (addr1->sa.sa_family != addr2->sa.sa_family)
948                 return 0;
949         if (htonl(INADDR_ANY) == addr1->v4.sin_addr.s_addr ||
950             htonl(INADDR_ANY) == addr2->v4.sin_addr.s_addr)
951                 return 1;
952         if (addr1->v4.sin_addr.s_addr == addr2->v4.sin_addr.s_addr)
953                 return 1;
954
955         return 0;
956 }
957
958 /* Verify that provided sockaddr looks bindable.  Common verification has
959  * already been taken care of.
960  */
961 static int sctp_inet_bind_verify(struct sctp_sock *opt, union sctp_addr *addr)
962 {
963         return sctp_v4_available(addr, opt);
964 }
965
966 /* Verify that sockaddr looks sendable.  Common verification has already
967  * been taken care of.
968  */
969 static int sctp_inet_send_verify(struct sctp_sock *opt, union sctp_addr *addr)
970 {
971         return 1;
972 }
973
974 /* Fill in Supported Address Type information for INIT and INIT-ACK
975  * chunks.  Returns number of addresses supported.
976  */
977 static int sctp_inet_supported_addrs(const struct sctp_sock *opt,
978                                      __be16 *types)
979 {
980         types[0] = SCTP_PARAM_IPV4_ADDRESS;
981         return 1;
982 }
983
984 /* Wrapper routine that calls the ip transmit routine. */
985 static inline int sctp_v4_xmit(struct sk_buff *skb,
986                                struct sctp_transport *transport)
987 {
988         struct inet_sock *inet = inet_sk(skb->sk);
989
990         SCTP_DEBUG_PRINTK("%s: skb:%p, len:%d, src:%pI4, dst:%pI4\n",
991                           __func__, skb, skb->len,
992                           &transport->fl.u.ip4.saddr,
993                           &transport->fl.u.ip4.daddr);
994
995         inet->pmtudisc = transport->param_flags & SPP_PMTUD_ENABLE ?
996                          IP_PMTUDISC_DO : IP_PMTUDISC_DONT;
997
998         SCTP_INC_STATS(SCTP_MIB_OUTSCTPPACKS);
999         return ip_queue_xmit(skb, &transport->fl);
1000 }
1001
1002 static struct sctp_af sctp_af_inet;
1003
1004 static struct sctp_pf sctp_pf_inet = {
1005         .event_msgname = sctp_inet_event_msgname,
1006         .skb_msgname   = sctp_inet_skb_msgname,
1007         .af_supported  = sctp_inet_af_supported,
1008         .cmp_addr      = sctp_inet_cmp_addr,
1009         .bind_verify   = sctp_inet_bind_verify,
1010         .send_verify   = sctp_inet_send_verify,
1011         .supported_addrs = sctp_inet_supported_addrs,
1012         .create_accept_sk = sctp_v4_create_accept_sk,
1013         .addr_v4map     = sctp_v4_addr_v4map,
1014         .af            = &sctp_af_inet
1015 };
1016
1017 /* Notifier for inetaddr addition/deletion events.  */
1018 static struct notifier_block sctp_inetaddr_notifier = {
1019         .notifier_call = sctp_inetaddr_event,
1020 };
1021
1022 /* Socket operations.  */
1023 static const struct proto_ops inet_seqpacket_ops = {
1024         .family            = PF_INET,
1025         .owner             = THIS_MODULE,
1026         .release           = inet_release,      /* Needs to be wrapped... */
1027         .bind              = inet_bind,
1028         .connect           = inet_dgram_connect,
1029         .socketpair        = sock_no_socketpair,
1030         .accept            = inet_accept,
1031         .getname           = inet_getname,      /* Semantics are different.  */
1032         .poll              = sctp_poll,
1033         .ioctl             = inet_ioctl,
1034         .listen            = sctp_inet_listen,
1035         .shutdown          = inet_shutdown,     /* Looks harmless.  */
1036         .setsockopt        = sock_common_setsockopt, /* IP_SOL IP_OPTION is a problem */
1037         .getsockopt        = sock_common_getsockopt,
1038         .sendmsg           = inet_sendmsg,
1039         .recvmsg           = sock_common_recvmsg,
1040         .mmap              = sock_no_mmap,
1041         .sendpage          = sock_no_sendpage,
1042 #ifdef CONFIG_COMPAT
1043         .compat_setsockopt = compat_sock_common_setsockopt,
1044         .compat_getsockopt = compat_sock_common_getsockopt,
1045 #endif
1046 };
1047
1048 /* Registration with AF_INET family.  */
1049 static struct inet_protosw sctp_seqpacket_protosw = {
1050         .type       = SOCK_SEQPACKET,
1051         .protocol   = IPPROTO_SCTP,
1052         .prot       = &sctp_prot,
1053         .ops        = &inet_seqpacket_ops,
1054         .no_check   = 0,
1055         .flags      = SCTP_PROTOSW_FLAG
1056 };
1057 static struct inet_protosw sctp_stream_protosw = {
1058         .type       = SOCK_STREAM,
1059         .protocol   = IPPROTO_SCTP,
1060         .prot       = &sctp_prot,
1061         .ops        = &inet_seqpacket_ops,
1062         .no_check   = 0,
1063         .flags      = SCTP_PROTOSW_FLAG
1064 };
1065
1066 /* Register with IP layer.  */
1067 static const struct net_protocol sctp_protocol = {
1068         .handler     = sctp_rcv,
1069         .err_handler = sctp_v4_err,
1070         .no_policy   = 1,
1071 };
1072
1073 /* IPv4 address related functions.  */
1074 static struct sctp_af sctp_af_inet = {
1075         .sa_family         = AF_INET,
1076         .sctp_xmit         = sctp_v4_xmit,
1077         .setsockopt        = ip_setsockopt,
1078         .getsockopt        = ip_getsockopt,
1079         .get_dst           = sctp_v4_get_dst,
1080         .get_saddr         = sctp_v4_get_saddr,
1081         .copy_addrlist     = sctp_v4_copy_addrlist,
1082         .from_skb          = sctp_v4_from_skb,
1083         .from_sk           = sctp_v4_from_sk,
1084         .to_sk_saddr       = sctp_v4_to_sk_saddr,
1085         .to_sk_daddr       = sctp_v4_to_sk_daddr,
1086         .from_addr_param   = sctp_v4_from_addr_param,
1087         .to_addr_param     = sctp_v4_to_addr_param,
1088         .cmp_addr          = sctp_v4_cmp_addr,
1089         .addr_valid        = sctp_v4_addr_valid,
1090         .inaddr_any        = sctp_v4_inaddr_any,
1091         .is_any            = sctp_v4_is_any,
1092         .available         = sctp_v4_available,
1093         .scope             = sctp_v4_scope,
1094         .skb_iif           = sctp_v4_skb_iif,
1095         .is_ce             = sctp_v4_is_ce,
1096         .seq_dump_addr     = sctp_v4_seq_dump_addr,
1097         .ecn_capable       = sctp_v4_ecn_capable,
1098         .net_header_len    = sizeof(struct iphdr),
1099         .sockaddr_len      = sizeof(struct sockaddr_in),
1100 #ifdef CONFIG_COMPAT
1101         .compat_setsockopt = compat_ip_setsockopt,
1102         .compat_getsockopt = compat_ip_getsockopt,
1103 #endif
1104 };
1105
1106 struct sctp_pf *sctp_get_pf_specific(sa_family_t family) {
1107
1108         switch (family) {
1109         case PF_INET:
1110                 return sctp_pf_inet_specific;
1111         case PF_INET6:
1112                 return sctp_pf_inet6_specific;
1113         default:
1114                 return NULL;
1115         }
1116 }
1117
1118 /* Register the PF specific function table.  */
1119 int sctp_register_pf(struct sctp_pf *pf, sa_family_t family)
1120 {
1121         switch (family) {
1122         case PF_INET:
1123                 if (sctp_pf_inet_specific)
1124                         return 0;
1125                 sctp_pf_inet_specific = pf;
1126                 break;
1127         case PF_INET6:
1128                 if (sctp_pf_inet6_specific)
1129                         return 0;
1130                 sctp_pf_inet6_specific = pf;
1131                 break;
1132         default:
1133                 return 0;
1134         }
1135         return 1;
1136 }
1137
1138 static inline int init_sctp_mibs(void)
1139 {
1140         return snmp_mib_init((void __percpu **)sctp_statistics,
1141                              sizeof(struct sctp_mib),
1142                              __alignof__(struct sctp_mib));
1143 }
1144
1145 static inline void cleanup_sctp_mibs(void)
1146 {
1147         snmp_mib_free((void __percpu **)sctp_statistics);
1148 }
1149
1150 static void sctp_v4_pf_init(void)
1151 {
1152         /* Initialize the SCTP specific PF functions. */
1153         sctp_register_pf(&sctp_pf_inet, PF_INET);
1154         sctp_register_af(&sctp_af_inet);
1155 }
1156
1157 static void sctp_v4_pf_exit(void)
1158 {
1159         list_del(&sctp_af_inet.list);
1160 }
1161
1162 static int sctp_v4_protosw_init(void)
1163 {
1164         int rc;
1165
1166         rc = proto_register(&sctp_prot, 1);
1167         if (rc)
1168                 return rc;
1169
1170         /* Register SCTP(UDP and TCP style) with socket layer.  */
1171         inet_register_protosw(&sctp_seqpacket_protosw);
1172         inet_register_protosw(&sctp_stream_protosw);
1173
1174         return 0;
1175 }
1176
1177 static void sctp_v4_protosw_exit(void)
1178 {
1179         inet_unregister_protosw(&sctp_stream_protosw);
1180         inet_unregister_protosw(&sctp_seqpacket_protosw);
1181         proto_unregister(&sctp_prot);
1182 }
1183
1184 static int sctp_v4_add_protocol(void)
1185 {
1186         /* Register notifier for inet address additions/deletions. */
1187         register_inetaddr_notifier(&sctp_inetaddr_notifier);
1188
1189         /* Register SCTP with inet layer.  */
1190         if (inet_add_protocol(&sctp_protocol, IPPROTO_SCTP) < 0)
1191                 return -EAGAIN;
1192
1193         return 0;
1194 }
1195
1196 static void sctp_v4_del_protocol(void)
1197 {
1198         inet_del_protocol(&sctp_protocol, IPPROTO_SCTP);
1199         unregister_inetaddr_notifier(&sctp_inetaddr_notifier);
1200 }
1201
1202 /* Initialize the universe into something sensible.  */
1203 SCTP_STATIC __init int sctp_init(void)
1204 {
1205         int i;
1206         int status = -EINVAL;
1207         unsigned long goal;
1208         unsigned long limit;
1209         int max_share;
1210         int order;
1211         int num_entries;
1212         int max_entry_order;
1213
1214         /* SCTP_DEBUG sanity check. */
1215         if (!sctp_sanity_check())
1216                 goto out;
1217
1218         /* Allocate bind_bucket and chunk caches. */
1219         status = -ENOBUFS;
1220         sctp_bucket_cachep = kmem_cache_create("sctp_bind_bucket",
1221                                                sizeof(struct sctp_bind_bucket),
1222                                                0, SLAB_HWCACHE_ALIGN,
1223                                                NULL);
1224         if (!sctp_bucket_cachep)
1225                 goto out;
1226
1227         sctp_chunk_cachep = kmem_cache_create("sctp_chunk",
1228                                                sizeof(struct sctp_chunk),
1229                                                0, SLAB_HWCACHE_ALIGN,
1230                                                NULL);
1231         if (!sctp_chunk_cachep)
1232                 goto err_chunk_cachep;
1233
1234         /* Allocate and initialise sctp mibs.  */
1235         status = init_sctp_mibs();
1236         if (status)
1237                 goto err_init_mibs;
1238
1239         /* Initialize proc fs directory.  */
1240         status = sctp_proc_init();
1241         if (status)
1242                 goto err_init_proc;
1243
1244         /* Initialize object count debugging.  */
1245         sctp_dbg_objcnt_init();
1246
1247         /*
1248          * 14. Suggested SCTP Protocol Parameter Values
1249          */
1250         /* The following protocol parameters are RECOMMENDED:  */
1251         /* RTO.Initial              - 3  seconds */
1252         sctp_rto_initial                = SCTP_RTO_INITIAL;
1253         /* RTO.Min                  - 1  second */
1254         sctp_rto_min                    = SCTP_RTO_MIN;
1255         /* RTO.Max                 -  60 seconds */
1256         sctp_rto_max                    = SCTP_RTO_MAX;
1257         /* RTO.Alpha                - 1/8 */
1258         sctp_rto_alpha                  = SCTP_RTO_ALPHA;
1259         /* RTO.Beta                 - 1/4 */
1260         sctp_rto_beta                   = SCTP_RTO_BETA;
1261
1262         /* Valid.Cookie.Life        - 60  seconds */
1263         sctp_valid_cookie_life          = SCTP_DEFAULT_COOKIE_LIFE;
1264
1265         /* Whether Cookie Preservative is enabled(1) or not(0) */
1266         sctp_cookie_preserve_enable     = 1;
1267
1268         /* Max.Burst                - 4 */
1269         sctp_max_burst                  = SCTP_DEFAULT_MAX_BURST;
1270
1271         /* Association.Max.Retrans  - 10 attempts
1272          * Path.Max.Retrans         - 5  attempts (per destination address)
1273          * Max.Init.Retransmits     - 8  attempts
1274          */
1275         sctp_max_retrans_association    = 10;
1276         sctp_max_retrans_path           = 5;
1277         sctp_max_retrans_init           = 8;
1278
1279         /* Sendbuffer growth        - do per-socket accounting */
1280         sctp_sndbuf_policy              = 0;
1281
1282         /* Rcvbuffer growth         - do per-socket accounting */
1283         sctp_rcvbuf_policy              = 0;
1284
1285         /* HB.interval              - 30 seconds */
1286         sctp_hb_interval                = SCTP_DEFAULT_TIMEOUT_HEARTBEAT;
1287
1288         /* delayed SACK timeout */
1289         sctp_sack_timeout               = SCTP_DEFAULT_TIMEOUT_SACK;
1290
1291         /* Implementation specific variables. */
1292
1293         /* Initialize default stream count setup information. */
1294         sctp_max_instreams              = SCTP_DEFAULT_INSTREAMS;
1295         sctp_max_outstreams             = SCTP_DEFAULT_OUTSTREAMS;
1296
1297         /* Initialize maximum autoclose timeout. */
1298         sctp_max_autoclose              = INT_MAX / HZ;
1299
1300         /* Initialize handle used for association ids. */
1301         idr_init(&sctp_assocs_id);
1302
1303         limit = nr_free_buffer_pages() / 8;
1304         limit = max(limit, 128UL);
1305         sysctl_sctp_mem[0] = limit / 4 * 3;
1306         sysctl_sctp_mem[1] = limit;
1307         sysctl_sctp_mem[2] = sysctl_sctp_mem[0] * 2;
1308
1309         /* Set per-socket limits to no more than 1/128 the pressure threshold*/
1310         limit = (sysctl_sctp_mem[1]) << (PAGE_SHIFT - 7);
1311         max_share = min(4UL*1024*1024, limit);
1312
1313         sysctl_sctp_rmem[0] = SK_MEM_QUANTUM; /* give each asoc 1 page min */
1314         sysctl_sctp_rmem[1] = 1500 * SKB_TRUESIZE(1);
1315         sysctl_sctp_rmem[2] = max(sysctl_sctp_rmem[1], max_share);
1316
1317         sysctl_sctp_wmem[0] = SK_MEM_QUANTUM;
1318         sysctl_sctp_wmem[1] = 16*1024;
1319         sysctl_sctp_wmem[2] = max(64*1024, max_share);
1320
1321         /* Size and allocate the association hash table.
1322          * The methodology is similar to that of the tcp hash tables.
1323          * Though not identical.  Start by getting a goal size
1324          */
1325         if (totalram_pages >= (128 * 1024))
1326                 goal = totalram_pages >> (22 - PAGE_SHIFT);
1327         else
1328                 goal = totalram_pages >> (24 - PAGE_SHIFT);
1329
1330         /* Then compute the page order for said goal */
1331         order = get_order(goal);
1332
1333         /* Now compute the required page order for the maximum sized table we
1334          * want to create
1335          */
1336         max_entry_order = get_order(MAX_SCTP_PORT_HASH_ENTRIES *
1337                                     sizeof(struct sctp_bind_hashbucket));
1338
1339         /* Limit the page order by that maximum hash table size */
1340         order = min(order, max_entry_order);
1341
1342         do {
1343                 sctp_assoc_hashsize = (1UL << order) * PAGE_SIZE /
1344                                         sizeof(struct sctp_hashbucket);
1345                 if ((sctp_assoc_hashsize > (64 * 1024)) && order > 0)
1346                         continue;
1347                 sctp_assoc_hashtable = (struct sctp_hashbucket *)
1348                         __get_free_pages(GFP_ATOMIC|__GFP_NOWARN, order);
1349         } while (!sctp_assoc_hashtable && --order > 0);
1350         if (!sctp_assoc_hashtable) {
1351                 pr_err("Failed association hash alloc\n");
1352                 status = -ENOMEM;
1353                 goto err_ahash_alloc;
1354         }
1355         for (i = 0; i < sctp_assoc_hashsize; i++) {
1356                 rwlock_init(&sctp_assoc_hashtable[i].lock);
1357                 INIT_HLIST_HEAD(&sctp_assoc_hashtable[i].chain);
1358         }
1359
1360         /* Allocate and initialize the endpoint hash table.  */
1361         sctp_ep_hashsize = 64;
1362         sctp_ep_hashtable = (struct sctp_hashbucket *)
1363                 kmalloc(64 * sizeof(struct sctp_hashbucket), GFP_KERNEL);
1364         if (!sctp_ep_hashtable) {
1365                 pr_err("Failed endpoint_hash alloc\n");
1366                 status = -ENOMEM;
1367                 goto err_ehash_alloc;
1368         }
1369         for (i = 0; i < sctp_ep_hashsize; i++) {
1370                 rwlock_init(&sctp_ep_hashtable[i].lock);
1371                 INIT_HLIST_HEAD(&sctp_ep_hashtable[i].chain);
1372         }
1373
1374         /* Allocate and initialize the SCTP port hash table.
1375          * Note that order is initalized to start at the max sized
1376          * table we want to support.  If we can't get that many pages
1377          * reduce the order and try again
1378          */
1379         do {
1380                 sctp_port_hashtable = (struct sctp_bind_hashbucket *)
1381                         __get_free_pages(GFP_ATOMIC|__GFP_NOWARN, order);
1382         } while (!sctp_port_hashtable && --order > 0);
1383
1384         if (!sctp_port_hashtable) {
1385                 pr_err("Failed bind hash alloc\n");
1386                 status = -ENOMEM;
1387                 goto err_bhash_alloc;
1388         }
1389
1390         /* Now compute the number of entries that will fit in the
1391          * port hash space we allocated
1392          */
1393         num_entries = (1UL << order) * PAGE_SIZE /
1394                       sizeof(struct sctp_bind_hashbucket);
1395
1396         /* And finish by rounding it down to the nearest power of two
1397          * this wastes some memory of course, but its needed because
1398          * the hash function operates based on the assumption that
1399          * that the number of entries is a power of two
1400          */
1401         sctp_port_hashsize = rounddown_pow_of_two(num_entries);
1402
1403         for (i = 0; i < sctp_port_hashsize; i++) {
1404                 spin_lock_init(&sctp_port_hashtable[i].lock);
1405                 INIT_HLIST_HEAD(&sctp_port_hashtable[i].chain);
1406         }
1407
1408         pr_info("Hash tables configured (established %d bind %d/%d)\n",
1409                 sctp_assoc_hashsize, sctp_port_hashsize, num_entries);
1410
1411         /* Disable ADDIP by default. */
1412         sctp_addip_enable = 0;
1413         sctp_addip_noauth = 0;
1414         sctp_default_auto_asconf = 0;
1415
1416         /* Enable PR-SCTP by default. */
1417         sctp_prsctp_enable = 1;
1418
1419         /* Disable AUTH by default. */
1420         sctp_auth_enable = 0;
1421
1422         /* Set SCOPE policy to enabled */
1423         sctp_scope_policy = SCTP_SCOPE_POLICY_ENABLE;
1424
1425         /* Set the default rwnd update threshold */
1426         sctp_rwnd_upd_shift             = SCTP_DEFAULT_RWND_SHIFT;
1427
1428         sctp_sysctl_register();
1429
1430         INIT_LIST_HEAD(&sctp_address_families);
1431         sctp_v4_pf_init();
1432         sctp_v6_pf_init();
1433
1434         /* Initialize the local address list. */
1435         INIT_LIST_HEAD(&sctp_local_addr_list);
1436         spin_lock_init(&sctp_local_addr_lock);
1437         sctp_get_local_addr_list();
1438
1439         /* Initialize the address event list */
1440         INIT_LIST_HEAD(&sctp_addr_waitq);
1441         INIT_LIST_HEAD(&sctp_auto_asconf_splist);
1442         spin_lock_init(&sctp_addr_wq_lock);
1443         sctp_addr_wq_timer.expires = 0;
1444         setup_timer(&sctp_addr_wq_timer, sctp_addr_wq_timeout_handler, 0);
1445
1446         status = sctp_v4_protosw_init();
1447
1448         if (status)
1449                 goto err_protosw_init;
1450
1451         status = sctp_v6_protosw_init();
1452         if (status)
1453                 goto err_v6_protosw_init;
1454
1455         /* Initialize the control inode/socket for handling OOTB packets.  */
1456         if ((status = sctp_ctl_sock_init())) {
1457                 pr_err("Failed to initialize the SCTP control sock\n");
1458                 goto err_ctl_sock_init;
1459         }
1460
1461         status = sctp_v4_add_protocol();
1462         if (status)
1463                 goto err_add_protocol;
1464
1465         /* Register SCTP with inet6 layer.  */
1466         status = sctp_v6_add_protocol();
1467         if (status)
1468                 goto err_v6_add_protocol;
1469
1470         status = 0;
1471 out:
1472         return status;
1473 err_v6_add_protocol:
1474         sctp_v4_del_protocol();
1475 err_add_protocol:
1476         inet_ctl_sock_destroy(sctp_ctl_sock);
1477 err_ctl_sock_init:
1478         sctp_v6_protosw_exit();
1479 err_v6_protosw_init:
1480         sctp_v4_protosw_exit();
1481 err_protosw_init:
1482         sctp_free_local_addr_list();
1483         sctp_v4_pf_exit();
1484         sctp_v6_pf_exit();
1485         sctp_sysctl_unregister();
1486         free_pages((unsigned long)sctp_port_hashtable,
1487                    get_order(sctp_port_hashsize *
1488                              sizeof(struct sctp_bind_hashbucket)));
1489 err_bhash_alloc:
1490         kfree(sctp_ep_hashtable);
1491 err_ehash_alloc:
1492         free_pages((unsigned long)sctp_assoc_hashtable,
1493                    get_order(sctp_assoc_hashsize *
1494                              sizeof(struct sctp_hashbucket)));
1495 err_ahash_alloc:
1496         sctp_dbg_objcnt_exit();
1497         sctp_proc_exit();
1498 err_init_proc:
1499         cleanup_sctp_mibs();
1500 err_init_mibs:
1501         kmem_cache_destroy(sctp_chunk_cachep);
1502 err_chunk_cachep:
1503         kmem_cache_destroy(sctp_bucket_cachep);
1504         goto out;
1505 }
1506
1507 /* Exit handler for the SCTP protocol.  */
1508 SCTP_STATIC __exit void sctp_exit(void)
1509 {
1510         /* BUG.  This should probably do something useful like clean
1511          * up all the remaining associations and all that memory.
1512          */
1513
1514         /* Unregister with inet6/inet layers. */
1515         sctp_v6_del_protocol();
1516         sctp_v4_del_protocol();
1517         sctp_free_addr_wq();
1518
1519         /* Free the control endpoint.  */
1520         inet_ctl_sock_destroy(sctp_ctl_sock);
1521
1522         /* Free protosw registrations */
1523         sctp_v6_protosw_exit();
1524         sctp_v4_protosw_exit();
1525
1526         /* Free the local address list.  */
1527         sctp_free_local_addr_list();
1528
1529         /* Unregister with socket layer. */
1530         sctp_v6_pf_exit();
1531         sctp_v4_pf_exit();
1532
1533         sctp_sysctl_unregister();
1534
1535         free_pages((unsigned long)sctp_assoc_hashtable,
1536                    get_order(sctp_assoc_hashsize *
1537                              sizeof(struct sctp_hashbucket)));
1538         kfree(sctp_ep_hashtable);
1539         free_pages((unsigned long)sctp_port_hashtable,
1540                    get_order(sctp_port_hashsize *
1541                              sizeof(struct sctp_bind_hashbucket)));
1542
1543         sctp_dbg_objcnt_exit();
1544         sctp_proc_exit();
1545         cleanup_sctp_mibs();
1546
1547         rcu_barrier(); /* Wait for completion of call_rcu()'s */
1548
1549         kmem_cache_destroy(sctp_chunk_cachep);
1550         kmem_cache_destroy(sctp_bucket_cachep);
1551 }
1552
1553 module_init(sctp_init);
1554 module_exit(sctp_exit);
1555
1556 /*
1557  * __stringify doesn't likes enums, so use IPPROTO_SCTP value (132) directly.
1558  */
1559 MODULE_ALIAS("net-pf-" __stringify(PF_INET) "-proto-132");
1560 MODULE_ALIAS("net-pf-" __stringify(PF_INET6) "-proto-132");
1561 MODULE_AUTHOR("Linux Kernel SCTP developers <lksctp-developers@lists.sourceforge.net>");
1562 MODULE_DESCRIPTION("Support for the SCTP protocol (RFC2960)");
1563 module_param_named(no_checksums, sctp_checksum_disable, bool, 0644);
1564 MODULE_PARM_DESC(no_checksums, "Disable checksums computing and verification");
1565 MODULE_LICENSE("GPL");