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
9 * This file is part of the SCTP kernel implementation
11 * Initialization/cleanup for SCTP protocol support.
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)
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.
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/>.
29 * Please send any bug reports or fixes you make to the
31 * lksctp developers <linux-sctp@vger.kernel.org>
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>
42 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
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>
63 #define MAX_SCTP_PORT_HASH_ENTRIES (64 * 1024)
65 /* Global data structures. */
66 struct sctp_globals sctp_globals __read_mostly;
68 struct idr sctp_assocs_id;
69 DEFINE_SPINLOCK(sctp_assocs_id_lock);
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;
76 struct kmem_cache *sctp_chunk_cachep __read_mostly;
77 struct kmem_cache *sctp_bucket_cachep __read_mostly;
79 long sysctl_sctp_mem[3];
80 int sysctl_sctp_rmem[3];
81 int sysctl_sctp_wmem[3];
83 /* Private helper to extract ipv4 address and stash them in
84 * the protocol structure.
86 static void sctp_v4_copy_addrlist(struct list_head *addrlist,
87 struct net_device *dev)
89 struct in_device *in_dev;
90 struct in_ifaddr *ifa;
91 struct sctp_sockaddr_entry *addr;
94 if ((in_dev = __in_dev_get_rcu(dev)) == NULL) {
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);
103 addr->a.v4.sin_family = AF_INET;
104 addr->a.v4.sin_addr.s_addr = ifa->ifa_local;
106 INIT_LIST_HEAD(&addr->list);
107 list_add_tail(&addr->list, addrlist);
114 /* Extract our IP addresses from the system and stash them in the
115 * protocol structure.
117 static void sctp_get_local_addr_list(struct net *net)
119 struct net_device *dev;
120 struct list_head *pos;
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);
133 /* Free the existing local addresses. */
134 static void sctp_free_local_addr_list(struct net *net)
136 struct sctp_sockaddr_entry *addr;
137 struct list_head *pos, *temp;
139 list_for_each_safe(pos, temp, &net->sctp.local_addr_list) {
140 addr = list_entry(pos, struct sctp_sockaddr_entry, list);
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)
150 struct sctp_sockaddr_entry *addr;
151 union sctp_addr laddr;
155 list_for_each_entry_rcu(addr, &net->sctp.local_addr_list, list) {
158 if (!sctp_in_scope(net, &addr->a, scope))
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.
165 if (addr->a.sa.sa_family == AF_INET &&
166 (!(copy_flags & SCTP_ADDR4_ALLOWED) ||
167 !(copy_flags & SCTP_ADDR4_PEERSUPP)))
169 if (addr->a.sa.sa_family == AF_INET6 &&
170 (!(copy_flags & SCTP_ADDR6_ALLOWED) ||
171 !(copy_flags & SCTP_ADDR6_PEERSUPP)))
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)
180 error = sctp_add_bind_addr(bp, &addr->a, sizeof(addr->a),
181 SCTP_ADDR_SRC, GFP_ATOMIC);
190 /* Copy over any ip options */
191 static void sctp_v4_copy_ip_options(struct sock *sk, struct sock *newsk)
193 struct inet_sock *newinet, *inet = inet_sk(sk);
194 struct ip_options_rcu *inet_opt, *newopt = NULL;
196 newinet = inet_sk(newsk);
199 inet_opt = rcu_dereference(inet->inet_opt);
201 newopt = sock_kmalloc(newsk, sizeof(*inet_opt) +
202 inet_opt->opt.optlen, GFP_ATOMIC);
204 memcpy(newopt, inet_opt, sizeof(*inet_opt) +
205 inet_opt->opt.optlen);
207 pr_err("%s: Failed to copy ip options\n", __func__);
209 RCU_INIT_POINTER(newinet->inet_opt, newopt);
213 /* Account for the IP options */
214 static int sctp_v4_ip_options_len(struct sock *sk)
216 struct inet_sock *inet = inet_sk(sk);
217 struct ip_options_rcu *inet_opt;
221 inet_opt = rcu_dereference(inet->inet_opt);
223 len = inet_opt->opt.optlen;
229 /* Initialize a sctp_addr from in incoming skb. */
230 static void sctp_v4_from_skb(union sctp_addr *addr, struct sk_buff *skb,
233 /* Always called on head skb, so this is safe */
234 struct sctphdr *sh = sctp_hdr(skb);
235 struct sockaddr_in *sa = &addr->v4;
237 addr->v4.sin_family = AF_INET;
240 sa->sin_port = sh->source;
241 sa->sin_addr.s_addr = ip_hdr(skb)->saddr;
243 sa->sin_port = sh->dest;
244 sa->sin_addr.s_addr = ip_hdr(skb)->daddr;
246 memset(sa->sin_zero, 0, sizeof(sa->sin_zero));
249 /* Initialize an sctp_addr from a socket. */
250 static void sctp_v4_from_sk(union sctp_addr *addr, struct sock *sk)
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));
258 /* Initialize sk->sk_rcv_saddr from sctp_addr. */
259 static void sctp_v4_to_sk_saddr(union sctp_addr *addr, struct sock *sk)
261 inet_sk(sk)->inet_rcv_saddr = addr->v4.sin_addr.s_addr;
264 /* Initialize sk->sk_daddr from sctp_addr. */
265 static void sctp_v4_to_sk_daddr(union sctp_addr *addr, struct sock *sk)
267 inet_sk(sk)->inet_daddr = addr->v4.sin_addr.s_addr;
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)
275 if (ntohs(param->v4.param_hdr.length) < sizeof(struct sctp_ipv4addr_param))
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));
286 /* Initialize an address parameter from a sctp_addr and return the length
287 * of the address parameter.
289 static int sctp_v4_to_addr_param(const union sctp_addr *addr,
290 union sctp_addr_param *param)
292 int length = sizeof(struct sctp_ipv4addr_param);
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;
301 /* Initialize a sctp_addr from a dst_entry. */
302 static void sctp_v4_dst_saddr(union sctp_addr *saddr, struct flowi4 *fl4,
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));
311 /* Compare two addresses exactly. */
312 static int sctp_v4_cmp_addr(const union sctp_addr *addr1,
313 const union sctp_addr *addr2)
315 if (addr1->sa.sa_family != addr2->sa.sa_family)
317 if (addr1->v4.sin_port != addr2->v4.sin_port)
319 if (addr1->v4.sin_addr.s_addr != addr2->v4.sin_addr.s_addr)
325 /* Initialize addr struct to INADDR_ANY. */
326 static void sctp_v4_inaddr_any(union sctp_addr *addr, __be16 port)
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));
334 /* Is this a wildcard address? */
335 static int sctp_v4_is_any(const union sctp_addr *addr)
337 return htonl(INADDR_ANY) == addr->v4.sin_addr.s_addr;
340 /* This function checks if the address is a valid address to be used for
344 * Return 0 - If the address is a non-unicast or an illegal address.
345 * Return 1 - If the address is a unicast.
347 static int sctp_v4_addr_valid(union sctp_addr *addr,
348 struct sctp_sock *sp,
349 const struct sk_buff *skb)
351 /* IPv4 addresses not allowed */
352 if (sp && ipv6_only_sock(sctp_opt2sk(sp)))
355 /* Is this a non-unicast address or a unusable SCTP address? */
356 if (IS_IPV4_UNUSABLE_ADDRESS(addr->v4.sin_addr.s_addr))
359 /* Is this a broadcast address? */
360 if (skb && skb_rtable(skb)->rt_flags & RTCF_BROADCAST)
366 /* Should this be available for binding? */
367 static int sctp_v4_available(union sctp_addr *addr, struct sctp_sock *sp)
369 struct net *net = sock_net(&sp->inet.sk);
370 int ret = inet_addr_type(net, addr->v4.sin_addr.s_addr);
373 if (addr->v4.sin_addr.s_addr != htonl(INADDR_ANY) &&
375 !sp->inet.freebind &&
376 !net->ipv4.sysctl_ip_nonlocal_bind)
379 if (ipv6_only_sock(sctp_opt2sk(sp)))
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>.
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.
399 * IPv4 scoping can be controlled through sysctl option
400 * net.sctp.addr_scope_policy
402 static enum sctp_scope sctp_v4_scope(union sctp_addr *addr)
404 enum sctp_scope retval;
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;
419 retval = SCTP_SCOPE_GLOBAL;
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.
429 static void sctp_v4_get_dst(struct sctp_transport *t, union sctp_addr *saddr,
430 struct flowi *fl, struct sock *sk)
432 struct sctp_association *asoc = t->asoc;
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;
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;
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);
455 fl4->saddr = saddr->v4.sin_addr.s_addr;
457 fl4->fl4_sport = saddr->v4.sin_port;
460 pr_debug("%s: dst:%pI4, src:%pI4 - ", __func__, &fl4->daddr,
463 rt = ip_route_output_key(sock_net(sk), fl4);
467 memcpy(fl, &_fl, sizeof(_fl));
470 /* If there is no association or if a source address is passed, no
471 * more validation is required.
476 bp = &asoc->base.bind_addr;
479 /* Walk through the bind address list and look for a bind
480 * address that matches the source address of the returned dst.
482 sctp_v4_dst_saddr(&dst_saddr, fl4, htons(bp->port));
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))
489 if (sctp_v4_cmp_addr(&dst_saddr, &laddr->a))
494 /* None of the bound addresses match the source address of the
495 * dst. So release it.
501 /* Walk through the bind address list and try to get a dst that
502 * matches a bind address as the source address.
505 list_for_each_entry_rcu(laddr, &bp->address_list, list) {
506 struct net_device *odev;
510 if (laddr->state != SCTP_ADDR_SRC ||
511 AF_INET != laddr->a.sa.sa_family)
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);
521 rt = ip_route_output_key(sock_net(sk), fl4);
525 /* Ensure the src address belongs to the output
528 odev = __ip_dev_find(sock_net(sk), laddr->a.v4.sin_addr.s_addr,
530 if (!odev || odev->ifindex != fl4->flowi4_oif) {
534 memcpy(fl, &_fl, sizeof(_fl));
536 dst_release(&rt->dst);
544 memcpy(fl, &_fl, sizeof(_fl));
552 pr_debug("rt_dst:%pI4, rt_src:%pI4\n",
553 &fl->u.ip4.daddr, &fl->u.ip4.saddr);
556 pr_debug("no route\n");
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.
563 static void sctp_v4_get_saddr(struct sctp_sock *sk,
564 struct sctp_transport *t,
567 union sctp_addr *saddr = &t->saddr;
568 struct rtable *rt = (struct rtable *)t->dst;
571 saddr->v4.sin_family = AF_INET;
572 saddr->v4.sin_addr.s_addr = fl->u.ip4.saddr;
576 /* What interface did this skb arrive on? */
577 static int sctp_v4_skb_iif(const struct sk_buff *skb)
579 return inet_iif(skb);
582 /* Was this packet marked by Explicit Congestion Notification? */
583 static int sctp_v4_is_ce(const struct sk_buff *skb)
585 return INET_ECN_is_ce(ip_hdr(skb)->tos);
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,
593 struct sock *newsk = sk_alloc(sock_net(sk), PF_INET, GFP_KERNEL,
595 struct inet_sock *newinet;
600 sock_init_data(NULL, newsk);
602 sctp_copy_sock(newsk, sk, asoc);
603 sock_reset_flag(newsk, SOCK_ZAPPED);
605 sctp_v4_copy_ip_options(sk, newsk);
607 newinet = inet_sk(newsk);
609 newinet->inet_daddr = asoc->peer.primary_addr.v4.sin_addr.s_addr;
611 sk_refcnt_debug_inc(newsk);
613 if (newsk->sk_prot->init(newsk)) {
614 sk_common_release(newsk);
622 static int sctp_v4_addr_to_user(struct sctp_sock *sp, union sctp_addr *addr)
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);
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)
632 seq_printf(seq, "%pI4 ", &addr->v4.sin_addr);
635 static void sctp_v4_ecn_capable(struct sock *sk)
640 static void sctp_addr_wq_timeout_handler(struct timer_list *t)
642 struct net *net = from_timer(net, t, sctp.addr_wq_timer);
643 struct sctp_sockaddr_entry *addrw, *temp;
644 struct sctp_sock *sp;
646 spin_lock_bh(&net->sctp.addr_wq_lock);
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);
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;
659 if (ipv6_addr_type(&addrw->a.v6.sin6_addr) &
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;
668 pr_debug("%s: this is on DAD, trying %d sec "
670 SCTP_ADDRESS_TICK_DELAY);
673 timeo_val += msecs_to_jiffies(SCTP_ADDRESS_TICK_DELAY);
674 mod_timer(&net->sctp.addr_wq_timer, timeo_val);
679 list_for_each_entry(sp, &net->sctp.auto_asconf_splist, auto_asconf_list) {
682 sk = sctp_opt2sk(sp);
683 /* ignore bound-specific endpoints */
684 if (!sctp_is_ep_boundall(sk))
687 if (sctp_asconf_mgmt(sp, addrw) < 0)
688 pr_debug("%s: sctp_asconf_mgmt failed\n", __func__);
691 #if IS_ENABLED(CONFIG_IPV6)
694 list_del(&addrw->list);
697 spin_unlock_bh(&net->sctp.addr_wq_lock);
700 static void sctp_free_addr_wq(struct net *net)
702 struct sctp_sockaddr_entry *addrw;
703 struct sctp_sockaddr_entry *temp;
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);
711 spin_unlock_bh(&net->sctp.addr_wq_lock);
714 /* lookup the entry for the same address in the addr_waitq
715 * sctp_addr_wq MUST be locked
717 static struct sctp_sockaddr_entry *sctp_addr_wq_lookup(struct net *net,
718 struct sctp_sockaddr_entry *addr)
720 struct sctp_sockaddr_entry *addrw;
722 list_for_each_entry(addrw, &net->sctp.addr_waitq, list) {
723 if (addrw->a.sa.sa_family != addr->a.sa.sa_family)
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)
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))
738 void sctp_addr_wq_mgmt(struct net *net, struct sctp_sockaddr_entry *addr, int cmd)
740 struct sctp_sockaddr_entry *addrw;
741 unsigned long timeo_val;
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
749 spin_lock_bh(&net->sctp.addr_wq_lock);
750 /* Offsets existing events in addr_wq */
751 addrw = sctp_addr_wq_lookup(net, addr);
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);
758 list_del(&addrw->list);
761 spin_unlock_bh(&net->sctp.addr_wq_lock);
765 /* OK, we have to add the new address to the wait queue */
766 addrw = kmemdup(addr, sizeof(struct sctp_sockaddr_entry), GFP_ATOMIC);
768 spin_unlock_bh(&net->sctp.addr_wq_lock);
772 list_add_tail(&addrw->list, &net->sctp.addr_waitq);
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);
777 if (!timer_pending(&net->sctp.addr_wq_timer)) {
779 timeo_val += msecs_to_jiffies(SCTP_ADDRESS_TICK_DELAY);
780 mod_timer(&net->sctp.addr_wq_timer, timeo_val);
782 spin_unlock_bh(&net->sctp.addr_wq_lock);
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.
791 static int sctp_inetaddr_event(struct notifier_block *this, unsigned long ev,
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);
802 addr = kzalloc(sizeof(*addr), GFP_ATOMIC);
804 addr->a.v4.sin_family = AF_INET;
805 addr->a.v4.sin_addr.s_addr = ifa->ifa_local;
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);
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 ==
820 sctp_addr_wq_mgmt(net, addr, SCTP_ADDR_DEL);
823 list_del_rcu(&addr->list);
827 spin_unlock_bh(&net->sctp.local_addr_lock);
829 kfree_rcu(addr, rcu);
837 * Initialize the control inode/socket with a control endpoint data
838 * structure. This endpoint is reserved exclusively for the OOTB processing.
840 static int sctp_ctl_sock_init(struct net *net)
843 sa_family_t family = PF_INET;
845 if (sctp_get_pf_specific(PF_INET6))
848 err = inet_ctl_sock_create(&net->sctp.ctl_sock, family,
849 SOCK_SEQPACKET, IPPROTO_SCTP, net);
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,
858 pr_err("Failed to create the SCTP control socket\n");
864 /* Register address family specific functions. */
865 int sctp_register_af(struct sctp_af *af)
867 switch (af->sa_family) {
869 if (sctp_af_v4_specific)
871 sctp_af_v4_specific = af;
874 if (sctp_af_v6_specific)
876 sctp_af_v6_specific = af;
882 INIT_LIST_HEAD(&af->list);
883 list_add_tail(&af->list, &sctp_address_families);
887 /* Get the table of functions for manipulating a particular address
890 struct sctp_af *sctp_get_af_specific(sa_family_t family)
894 return sctp_af_v4_specific;
896 return sctp_af_v6_specific;
902 /* Common code to initialize a AF_INET msg_name. */
903 static void sctp_inet_msgname(char *msgname, int *addr_len)
905 struct sockaddr_in *sin;
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));
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,
917 struct sockaddr_in *sin, *sinfrom;
920 struct sctp_association *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;
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)
935 struct sctphdr *sh = sctp_hdr(skb);
936 struct sockaddr_in *sin = (struct sockaddr_in *)msgname;
938 sctp_inet_msgname(msgname, len);
939 sin->sin_port = sh->source;
940 sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
944 /* Do we support this AF? */
945 static int sctp_inet_af_supported(sa_family_t family, struct sctp_sock *sp)
947 /* PF_INET only supports AF_INET addresses. */
948 return AF_INET == family;
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)
956 /* PF_INET only supports AF_INET addresses. */
957 if (addr1->sa.sa_family != addr2->sa.sa_family)
959 if (htonl(INADDR_ANY) == addr1->v4.sin_addr.s_addr ||
960 htonl(INADDR_ANY) == addr2->v4.sin_addr.s_addr)
962 if (addr1->v4.sin_addr.s_addr == addr2->v4.sin_addr.s_addr)
968 /* Verify that provided sockaddr looks bindable. Common verification has
969 * already been taken care of.
971 static int sctp_inet_bind_verify(struct sctp_sock *opt, union sctp_addr *addr)
973 return sctp_v4_available(addr, opt);
976 /* Verify that sockaddr looks sendable. Common verification has already
977 * been taken care of.
979 static int sctp_inet_send_verify(struct sctp_sock *opt, union sctp_addr *addr)
984 /* Fill in Supported Address Type information for INIT and INIT-ACK
985 * chunks. Returns number of addresses supported.
987 static int sctp_inet_supported_addrs(const struct sctp_sock *opt,
990 types[0] = SCTP_PARAM_IPV4_ADDRESS;
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)
998 struct inet_sock *inet = inet_sk(skb->sk);
999 __u8 dscp = inet->tos;
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);
1005 if (transport->dscp & SCTP_DSCP_SET_MASK)
1006 dscp = transport->dscp & SCTP_DSCP_VAL_MASK;
1008 inet->pmtudisc = transport->param_flags & SPP_PMTUD_ENABLE ?
1009 IP_PMTUDISC_DO : IP_PMTUDISC_DONT;
1011 SCTP_INC_STATS(sock_net(&inet->sk), SCTP_MIB_OUTSCTPPACKS);
1013 return __ip_queue_xmit(&inet->sk, skb, &transport->fl, dscp);
1016 static struct sctp_af sctp_af_inet;
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,
1034 /* Notifier for inetaddr addition/deletion events. */
1035 static struct notifier_block sctp_inetaddr_notifier = {
1036 .notifier_call = sctp_inetaddr_event,
1039 /* Socket operations. */
1040 static const struct proto_ops inet_seqpacket_ops = {
1042 .owner = THIS_MODULE,
1043 .release = inet_release, /* Needs to be wrapped... */
1045 .connect = sctp_inet_connect,
1046 .socketpair = sock_no_socketpair,
1047 .accept = inet_accept,
1048 .getname = inet_getname, /* Semantics are different. */
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,
1065 /* Registration with AF_INET family. */
1066 static struct inet_protosw sctp_seqpacket_protosw = {
1067 .type = SOCK_SEQPACKET,
1068 .protocol = IPPROTO_SCTP,
1070 .ops = &inet_seqpacket_ops,
1071 .flags = SCTP_PROTOSW_FLAG
1073 static struct inet_protosw sctp_stream_protosw = {
1074 .type = SOCK_STREAM,
1075 .protocol = IPPROTO_SCTP,
1077 .ops = &inet_seqpacket_ops,
1078 .flags = SCTP_PROTOSW_FLAG
1081 /* Register with IP layer. */
1082 static const struct net_protocol sctp_protocol = {
1083 .handler = sctp_rcv,
1084 .err_handler = sctp_v4_err,
1087 .icmp_strict_tag_validation = 1,
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,
1122 struct sctp_pf *sctp_get_pf_specific(sa_family_t family)
1126 return sctp_pf_inet_specific;
1128 return sctp_pf_inet6_specific;
1134 /* Register the PF specific function table. */
1135 int sctp_register_pf(struct sctp_pf *pf, sa_family_t family)
1139 if (sctp_pf_inet_specific)
1141 sctp_pf_inet_specific = pf;
1144 if (sctp_pf_inet6_specific)
1146 sctp_pf_inet6_specific = pf;
1154 static inline int init_sctp_mibs(struct net *net)
1156 net->sctp.sctp_statistics = alloc_percpu(struct sctp_mib);
1157 if (!net->sctp.sctp_statistics)
1162 static inline void cleanup_sctp_mibs(struct net *net)
1164 free_percpu(net->sctp.sctp_statistics);
1167 static void sctp_v4_pf_init(void)
1169 /* Initialize the SCTP specific PF functions. */
1170 sctp_register_pf(&sctp_pf_inet, PF_INET);
1171 sctp_register_af(&sctp_af_inet);
1174 static void sctp_v4_pf_exit(void)
1176 list_del(&sctp_af_inet.list);
1179 static int sctp_v4_protosw_init(void)
1183 rc = proto_register(&sctp_prot, 1);
1187 /* Register SCTP(UDP and TCP style) with socket layer. */
1188 inet_register_protosw(&sctp_seqpacket_protosw);
1189 inet_register_protosw(&sctp_stream_protosw);
1194 static void sctp_v4_protosw_exit(void)
1196 inet_unregister_protosw(&sctp_stream_protosw);
1197 inet_unregister_protosw(&sctp_seqpacket_protosw);
1198 proto_unregister(&sctp_prot);
1201 static int sctp_v4_add_protocol(void)
1203 /* Register notifier for inet address additions/deletions. */
1204 register_inetaddr_notifier(&sctp_inetaddr_notifier);
1206 /* Register SCTP with inet layer. */
1207 if (inet_add_protocol(&sctp_protocol, IPPROTO_SCTP) < 0)
1213 static void sctp_v4_del_protocol(void)
1215 inet_del_protocol(&sctp_protocol, IPPROTO_SCTP);
1216 unregister_inetaddr_notifier(&sctp_inetaddr_notifier);
1219 static int __net_init sctp_defaults_init(struct net *net)
1224 * 14. Suggested SCTP Protocol Parameter Values
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;
1238 /* Valid.Cookie.Life - 60 seconds */
1239 net->sctp.valid_cookie_life = SCTP_DEFAULT_COOKIE_LIFE;
1241 /* Whether Cookie Preservative is enabled(1) or not(0) */
1242 net->sctp.cookie_preserve_enable = 1;
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";
1250 net->sctp.sctp_hmac_alg = NULL;
1254 net->sctp.max_burst = SCTP_DEFAULT_MAX_BURST;
1256 /* Enable pf state by default */
1257 net->sctp.pf_enable = 1;
1259 /* Association.Max.Retrans - 10 attempts
1260 * Path.Max.Retrans - 5 attempts (per destination address)
1261 * Max.Init.Retransmits - 8 attempts
1263 net->sctp.max_retrans_association = 10;
1264 net->sctp.max_retrans_path = 5;
1265 net->sctp.max_retrans_init = 8;
1267 /* Sendbuffer growth - do per-socket accounting */
1268 net->sctp.sndbuf_policy = 0;
1270 /* Rcvbuffer growth - do per-socket accounting */
1271 net->sctp.rcvbuf_policy = 0;
1273 /* HB.interval - 30 seconds */
1274 net->sctp.hb_interval = SCTP_DEFAULT_TIMEOUT_HEARTBEAT;
1276 /* delayed SACK timeout */
1277 net->sctp.sack_timeout = SCTP_DEFAULT_TIMEOUT_SACK;
1279 /* Disable ADDIP by default. */
1280 net->sctp.addip_enable = 0;
1281 net->sctp.addip_noauth = 0;
1282 net->sctp.default_auto_asconf = 0;
1284 /* Enable PR-SCTP by default. */
1285 net->sctp.prsctp_enable = 1;
1287 /* Disable RECONF by default. */
1288 net->sctp.reconf_enable = 0;
1290 /* Disable AUTH by default. */
1291 net->sctp.auth_enable = 0;
1293 /* Set SCOPE policy to enabled */
1294 net->sctp.scope_policy = SCTP_SCOPE_POLICY_ENABLE;
1296 /* Set the default rwnd update threshold */
1297 net->sctp.rwnd_upd_shift = SCTP_DEFAULT_RWND_SHIFT;
1299 /* Initialize maximum autoclose timeout. */
1300 net->sctp.max_autoclose = INT_MAX / HZ;
1302 status = sctp_sysctl_net_register(net);
1304 goto err_sysctl_register;
1306 /* Allocate and initialise sctp mibs. */
1307 status = init_sctp_mibs(net);
1311 #ifdef CONFIG_PROC_FS
1312 /* Initialize proc fs directory. */
1313 status = sctp_proc_init(net);
1318 sctp_dbg_objcnt_init(net);
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);
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);
1334 #ifdef CONFIG_PROC_FS
1336 cleanup_sctp_mibs(net);
1339 sctp_sysctl_net_unregister(net);
1340 err_sysctl_register:
1344 static void __net_exit sctp_defaults_exit(struct net *net)
1346 /* Free the local address list */
1347 sctp_free_addr_wq(net);
1348 sctp_free_local_addr_list(net);
1350 #ifdef CONFIG_PROC_FS
1351 remove_proc_subtree("sctp", net->proc_net);
1352 net->sctp.proc_net_sctp = NULL;
1354 cleanup_sctp_mibs(net);
1355 sctp_sysctl_net_unregister(net);
1358 static struct pernet_operations sctp_defaults_ops = {
1359 .init = sctp_defaults_init,
1360 .exit = sctp_defaults_exit,
1363 static int __net_init sctp_ctrlsock_init(struct net *net)
1367 /* Initialize the control inode/socket for handling OOTB packets. */
1368 status = sctp_ctl_sock_init(net);
1370 pr_err("Failed to initialize the SCTP control sock\n");
1375 static void __net_exit sctp_ctrlsock_exit(struct net *net)
1377 /* Free the control endpoint. */
1378 inet_ctl_sock_destroy(net->sctp.ctl_sock);
1381 static struct pernet_operations sctp_ctrlsock_ops = {
1382 .init = sctp_ctrlsock_init,
1383 .exit = sctp_ctrlsock_exit,
1386 /* Initialize the universe into something sensible. */
1387 static __init int sctp_init(void)
1390 int status = -EINVAL;
1392 unsigned long limit;
1396 int max_entry_order;
1398 sock_skb_cb_check_size(sizeof(struct sctp_ulpevent));
1400 /* Allocate bind_bucket and chunk caches. */
1402 sctp_bucket_cachep = kmem_cache_create("sctp_bind_bucket",
1403 sizeof(struct sctp_bind_bucket),
1404 0, SLAB_HWCACHE_ALIGN,
1406 if (!sctp_bucket_cachep)
1409 sctp_chunk_cachep = kmem_cache_create("sctp_chunk",
1410 sizeof(struct sctp_chunk),
1411 0, SLAB_HWCACHE_ALIGN,
1413 if (!sctp_chunk_cachep)
1414 goto err_chunk_cachep;
1416 status = percpu_counter_init(&sctp_sockets_allocated, 0, GFP_KERNEL);
1418 goto err_percpu_counter_init;
1420 /* Implementation specific variables. */
1422 /* Initialize default stream count setup information. */
1423 sctp_max_instreams = SCTP_DEFAULT_INSTREAMS;
1424 sctp_max_outstreams = SCTP_DEFAULT_OUTSTREAMS;
1426 /* Initialize handle used for association ids. */
1427 idr_init(&sctp_assocs_id);
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;
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);
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);
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);
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
1451 if (totalram_pages >= (128 * 1024))
1452 goal = totalram_pages >> (22 - PAGE_SHIFT);
1454 goal = totalram_pages >> (24 - PAGE_SHIFT);
1456 /* Then compute the page order for said goal */
1457 order = get_order(goal);
1459 /* Now compute the required page order for the maximum sized table we
1462 max_entry_order = get_order(MAX_SCTP_PORT_HASH_ENTRIES *
1463 sizeof(struct sctp_bind_hashbucket));
1465 /* Limit the page order by that maximum hash table size */
1466 order = min(order, max_entry_order);
1468 /* Allocate and initialize the endpoint hash table. */
1469 sctp_ep_hashsize = 64;
1471 kmalloc_array(64, sizeof(struct sctp_hashbucket), GFP_KERNEL);
1472 if (!sctp_ep_hashtable) {
1473 pr_err("Failed endpoint_hash alloc\n");
1475 goto err_ehash_alloc;
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);
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
1488 sctp_port_hashtable = (struct sctp_bind_hashbucket *)
1489 __get_free_pages(GFP_KERNEL | __GFP_NOWARN, order);
1490 } while (!sctp_port_hashtable && --order > 0);
1492 if (!sctp_port_hashtable) {
1493 pr_err("Failed bind hash alloc\n");
1495 goto err_bhash_alloc;
1498 /* Now compute the number of entries that will fit in the
1499 * port hash space we allocated
1501 num_entries = (1UL << order) * PAGE_SIZE /
1502 sizeof(struct sctp_bind_hashbucket);
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
1509 sctp_port_hashsize = rounddown_pow_of_two(num_entries);
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);
1516 status = sctp_transport_hashtable_init();
1518 goto err_thash_alloc;
1520 pr_info("Hash tables configured (bind %d/%d)\n", sctp_port_hashsize,
1523 sctp_sysctl_register();
1525 INIT_LIST_HEAD(&sctp_address_families);
1528 sctp_sched_ops_init();
1530 status = register_pernet_subsys(&sctp_defaults_ops);
1532 goto err_register_defaults;
1534 status = sctp_v4_protosw_init();
1536 goto err_protosw_init;
1538 status = sctp_v6_protosw_init();
1540 goto err_v6_protosw_init;
1542 status = register_pernet_subsys(&sctp_ctrlsock_ops);
1544 goto err_register_ctrlsock;
1546 status = sctp_v4_add_protocol();
1548 goto err_add_protocol;
1550 /* Register SCTP with inet6 layer. */
1551 status = sctp_v6_add_protocol();
1553 goto err_v6_add_protocol;
1555 if (sctp_offload_init() < 0)
1556 pr_crit("%s: Cannot add SCTP protocol offload\n", __func__);
1560 err_v6_add_protocol:
1561 sctp_v4_del_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();
1569 unregister_pernet_subsys(&sctp_defaults_ops);
1570 err_register_defaults:
1573 sctp_sysctl_unregister();
1574 free_pages((unsigned long)sctp_port_hashtable,
1575 get_order(sctp_port_hashsize *
1576 sizeof(struct sctp_bind_hashbucket)));
1578 sctp_transport_hashtable_destroy();
1580 kfree(sctp_ep_hashtable);
1582 percpu_counter_destroy(&sctp_sockets_allocated);
1583 err_percpu_counter_init:
1584 kmem_cache_destroy(sctp_chunk_cachep);
1586 kmem_cache_destroy(sctp_bucket_cachep);
1590 /* Exit handler for the SCTP protocol. */
1591 static __exit void sctp_exit(void)
1593 /* BUG. This should probably do something useful like clean
1594 * up all the remaining associations and all that memory.
1597 /* Unregister with inet6/inet layers. */
1598 sctp_v6_del_protocol();
1599 sctp_v4_del_protocol();
1601 unregister_pernet_subsys(&sctp_ctrlsock_ops);
1603 /* Free protosw registrations */
1604 sctp_v6_protosw_exit();
1605 sctp_v4_protosw_exit();
1607 unregister_pernet_subsys(&sctp_defaults_ops);
1609 /* Unregister with socket layer. */
1613 sctp_sysctl_unregister();
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();
1621 percpu_counter_destroy(&sctp_sockets_allocated);
1623 rcu_barrier(); /* Wait for completion of call_rcu()'s */
1625 kmem_cache_destroy(sctp_chunk_cachep);
1626 kmem_cache_destroy(sctp_bucket_cachep);
1629 module_init(sctp_init);
1630 module_exit(sctp_exit);
1633 * __stringify doesn't likes enums, so use IPPROTO_SCTP value (132) directly.
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");