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