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