Linux 4.19.133
[linux/fpc-iii.git] / net / sctp / protocol.c
blobaf054f38341b91f824b80996835598054bb5c488
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)
17 * any later version.
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
30 * email address(es):
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>
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>
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;
93 rcu_read_lock();
94 if ((in_dev = __in_dev_get_rcu(dev)) == NULL) {
95 rcu_read_unlock();
96 return;
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);
111 rcu_read_unlock();
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;
121 struct sctp_af *af;
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);
130 rcu_read_unlock();
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);
141 list_del(pos);
142 kfree(addr);
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;
152 int error = 0;
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;
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)))
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;
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;
180 error = sctp_add_bind_addr(bp, &addr->a, sizeof(addr->a),
181 SCTP_ADDR_SRC, GFP_ATOMIC);
182 if (error)
183 break;
186 rcu_read_unlock();
187 return error;
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);
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__);
209 RCU_INIT_POINTER(newinet->inet_opt, newopt);
210 rcu_read_unlock();
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;
218 int len = 0;
220 rcu_read_lock();
221 inet_opt = rcu_dereference(inet->inet_opt);
222 if (inet_opt)
223 len = inet_opt->opt.optlen;
225 rcu_read_unlock();
226 return len;
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)
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;
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;
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 void sctp_v4_from_addr_param(union sctp_addr *addr,
272 union sctp_addr_param *param,
273 __be16 port, int iif)
275 addr->v4.sin_family = AF_INET;
276 addr->v4.sin_port = port;
277 addr->v4.sin_addr.s_addr = param->v4.addr.s_addr;
278 memset(addr->v4.sin_zero, 0, sizeof(addr->v4.sin_zero));
281 /* Initialize an address parameter from a sctp_addr and return the length
282 * of the address parameter.
284 static int sctp_v4_to_addr_param(const union sctp_addr *addr,
285 union sctp_addr_param *param)
287 int length = sizeof(struct sctp_ipv4addr_param);
289 param->v4.param_hdr.type = SCTP_PARAM_IPV4_ADDRESS;
290 param->v4.param_hdr.length = htons(length);
291 param->v4.addr.s_addr = addr->v4.sin_addr.s_addr;
293 return length;
296 /* Initialize a sctp_addr from a dst_entry. */
297 static void sctp_v4_dst_saddr(union sctp_addr *saddr, struct flowi4 *fl4,
298 __be16 port)
300 saddr->v4.sin_family = AF_INET;
301 saddr->v4.sin_port = port;
302 saddr->v4.sin_addr.s_addr = fl4->saddr;
303 memset(saddr->v4.sin_zero, 0, sizeof(saddr->v4.sin_zero));
306 /* Compare two addresses exactly. */
307 static int sctp_v4_cmp_addr(const union sctp_addr *addr1,
308 const union sctp_addr *addr2)
310 if (addr1->sa.sa_family != addr2->sa.sa_family)
311 return 0;
312 if (addr1->v4.sin_port != addr2->v4.sin_port)
313 return 0;
314 if (addr1->v4.sin_addr.s_addr != addr2->v4.sin_addr.s_addr)
315 return 0;
317 return 1;
320 /* Initialize addr struct to INADDR_ANY. */
321 static void sctp_v4_inaddr_any(union sctp_addr *addr, __be16 port)
323 addr->v4.sin_family = AF_INET;
324 addr->v4.sin_addr.s_addr = htonl(INADDR_ANY);
325 addr->v4.sin_port = port;
326 memset(addr->v4.sin_zero, 0, sizeof(addr->v4.sin_zero));
329 /* Is this a wildcard address? */
330 static int sctp_v4_is_any(const union sctp_addr *addr)
332 return htonl(INADDR_ANY) == addr->v4.sin_addr.s_addr;
335 /* This function checks if the address is a valid address to be used for
336 * SCTP binding.
338 * Output:
339 * Return 0 - If the address is a non-unicast or an illegal address.
340 * Return 1 - If the address is a unicast.
342 static int sctp_v4_addr_valid(union sctp_addr *addr,
343 struct sctp_sock *sp,
344 const struct sk_buff *skb)
346 /* IPv4 addresses not allowed */
347 if (sp && ipv6_only_sock(sctp_opt2sk(sp)))
348 return 0;
350 /* Is this a non-unicast address or a unusable SCTP address? */
351 if (IS_IPV4_UNUSABLE_ADDRESS(addr->v4.sin_addr.s_addr))
352 return 0;
354 /* Is this a broadcast address? */
355 if (skb && skb_rtable(skb)->rt_flags & RTCF_BROADCAST)
356 return 0;
358 return 1;
361 /* Should this be available for binding? */
362 static int sctp_v4_available(union sctp_addr *addr, struct sctp_sock *sp)
364 struct net *net = sock_net(&sp->inet.sk);
365 int ret = inet_addr_type(net, addr->v4.sin_addr.s_addr);
368 if (addr->v4.sin_addr.s_addr != htonl(INADDR_ANY) &&
369 ret != RTN_LOCAL &&
370 !sp->inet.freebind &&
371 !net->ipv4.sysctl_ip_nonlocal_bind)
372 return 0;
374 if (ipv6_only_sock(sctp_opt2sk(sp)))
375 return 0;
377 return 1;
380 /* Checking the loopback, private and other address scopes as defined in
381 * RFC 1918. The IPv4 scoping is based on the draft for SCTP IPv4
382 * scoping <draft-stewart-tsvwg-sctp-ipv4-00.txt>.
384 * Level 0 - unusable SCTP addresses
385 * Level 1 - loopback address
386 * Level 2 - link-local addresses
387 * Level 3 - private addresses.
388 * Level 4 - global addresses
389 * For INIT and INIT-ACK address list, let L be the level of
390 * of requested destination address, sender and receiver
391 * SHOULD include all of its addresses with level greater
392 * than or equal to L.
394 * IPv4 scoping can be controlled through sysctl option
395 * net.sctp.addr_scope_policy
397 static enum sctp_scope sctp_v4_scope(union sctp_addr *addr)
399 enum sctp_scope retval;
401 /* Check for unusable SCTP addresses. */
402 if (IS_IPV4_UNUSABLE_ADDRESS(addr->v4.sin_addr.s_addr)) {
403 retval = SCTP_SCOPE_UNUSABLE;
404 } else if (ipv4_is_loopback(addr->v4.sin_addr.s_addr)) {
405 retval = SCTP_SCOPE_LOOPBACK;
406 } else if (ipv4_is_linklocal_169(addr->v4.sin_addr.s_addr)) {
407 retval = SCTP_SCOPE_LINK;
408 } else if (ipv4_is_private_10(addr->v4.sin_addr.s_addr) ||
409 ipv4_is_private_172(addr->v4.sin_addr.s_addr) ||
410 ipv4_is_private_192(addr->v4.sin_addr.s_addr)) {
411 retval = SCTP_SCOPE_PRIVATE;
412 } else {
413 retval = SCTP_SCOPE_GLOBAL;
416 return retval;
419 /* Returns a valid dst cache entry for the given source and destination ip
420 * addresses. If an association is passed, trys to get a dst entry with a
421 * source address that matches an address in the bind address list.
423 static void sctp_v4_get_dst(struct sctp_transport *t, union sctp_addr *saddr,
424 struct flowi *fl, struct sock *sk)
426 struct sctp_association *asoc = t->asoc;
427 struct rtable *rt;
428 struct flowi _fl;
429 struct flowi4 *fl4 = &_fl.u.ip4;
430 struct sctp_bind_addr *bp;
431 struct sctp_sockaddr_entry *laddr;
432 struct dst_entry *dst = NULL;
433 union sctp_addr *daddr = &t->ipaddr;
434 union sctp_addr dst_saddr;
435 __u8 tos = inet_sk(sk)->tos;
437 if (t->dscp & SCTP_DSCP_SET_MASK)
438 tos = t->dscp & SCTP_DSCP_VAL_MASK;
439 memset(&_fl, 0x0, sizeof(_fl));
440 fl4->daddr = daddr->v4.sin_addr.s_addr;
441 fl4->fl4_dport = daddr->v4.sin_port;
442 fl4->flowi4_proto = IPPROTO_SCTP;
443 if (asoc) {
444 fl4->flowi4_tos = RT_CONN_FLAGS_TOS(asoc->base.sk, tos);
445 fl4->flowi4_oif = asoc->base.sk->sk_bound_dev_if;
446 fl4->fl4_sport = htons(asoc->base.bind_addr.port);
448 if (saddr) {
449 fl4->saddr = saddr->v4.sin_addr.s_addr;
450 if (!fl4->fl4_sport)
451 fl4->fl4_sport = saddr->v4.sin_port;
454 pr_debug("%s: dst:%pI4, src:%pI4 - ", __func__, &fl4->daddr,
455 &fl4->saddr);
457 rt = ip_route_output_key(sock_net(sk), fl4);
458 if (!IS_ERR(rt)) {
459 dst = &rt->dst;
460 t->dst = dst;
461 memcpy(fl, &_fl, sizeof(_fl));
464 /* If there is no association or if a source address is passed, no
465 * more validation is required.
467 if (!asoc || saddr)
468 goto out;
470 bp = &asoc->base.bind_addr;
472 if (dst) {
473 /* Walk through the bind address list and look for a bind
474 * address that matches the source address of the returned dst.
476 sctp_v4_dst_saddr(&dst_saddr, fl4, htons(bp->port));
477 rcu_read_lock();
478 list_for_each_entry_rcu(laddr, &bp->address_list, list) {
479 if (!laddr->valid || (laddr->state == SCTP_ADDR_DEL) ||
480 (laddr->state != SCTP_ADDR_SRC &&
481 !asoc->src_out_of_asoc_ok))
482 continue;
483 if (sctp_v4_cmp_addr(&dst_saddr, &laddr->a))
484 goto out_unlock;
486 rcu_read_unlock();
488 /* None of the bound addresses match the source address of the
489 * dst. So release it.
491 dst_release(dst);
492 dst = NULL;
495 /* Walk through the bind address list and try to get a dst that
496 * matches a bind address as the source address.
498 rcu_read_lock();
499 list_for_each_entry_rcu(laddr, &bp->address_list, list) {
500 struct net_device *odev;
502 if (!laddr->valid)
503 continue;
504 if (laddr->state != SCTP_ADDR_SRC ||
505 AF_INET != laddr->a.sa.sa_family)
506 continue;
508 fl4->fl4_sport = laddr->a.v4.sin_port;
509 flowi4_update_output(fl4,
510 asoc->base.sk->sk_bound_dev_if,
511 RT_CONN_FLAGS_TOS(asoc->base.sk, tos),
512 daddr->v4.sin_addr.s_addr,
513 laddr->a.v4.sin_addr.s_addr);
515 rt = ip_route_output_key(sock_net(sk), fl4);
516 if (IS_ERR(rt))
517 continue;
519 /* Ensure the src address belongs to the output
520 * interface.
522 odev = __ip_dev_find(sock_net(sk), laddr->a.v4.sin_addr.s_addr,
523 false);
524 if (!odev || odev->ifindex != fl4->flowi4_oif) {
525 if (!dst) {
526 dst = &rt->dst;
527 t->dst = dst;
528 memcpy(fl, &_fl, sizeof(_fl));
529 } else {
530 dst_release(&rt->dst);
532 continue;
535 dst_release(dst);
536 dst = &rt->dst;
537 t->dst = dst;
538 memcpy(fl, &_fl, sizeof(_fl));
539 break;
542 out_unlock:
543 rcu_read_unlock();
544 out:
545 if (dst) {
546 pr_debug("rt_dst:%pI4, rt_src:%pI4\n",
547 &fl->u.ip4.daddr, &fl->u.ip4.saddr);
548 } else {
549 t->dst = NULL;
550 pr_debug("no route\n");
554 /* For v4, the source address is cached in the route entry(dst). So no need
555 * to cache it separately and hence this is an empty routine.
557 static void sctp_v4_get_saddr(struct sctp_sock *sk,
558 struct sctp_transport *t,
559 struct flowi *fl)
561 union sctp_addr *saddr = &t->saddr;
562 struct rtable *rt = (struct rtable *)t->dst;
564 if (rt) {
565 saddr->v4.sin_family = AF_INET;
566 saddr->v4.sin_addr.s_addr = fl->u.ip4.saddr;
570 /* What interface did this skb arrive on? */
571 static int sctp_v4_skb_iif(const struct sk_buff *skb)
573 return inet_iif(skb);
576 /* Was this packet marked by Explicit Congestion Notification? */
577 static int sctp_v4_is_ce(const struct sk_buff *skb)
579 return INET_ECN_is_ce(ip_hdr(skb)->tos);
582 /* Create and initialize a new sk for the socket returned by accept(). */
583 static struct sock *sctp_v4_create_accept_sk(struct sock *sk,
584 struct sctp_association *asoc,
585 bool kern)
587 struct sock *newsk = sk_alloc(sock_net(sk), PF_INET, GFP_KERNEL,
588 sk->sk_prot, kern);
589 struct inet_sock *newinet;
591 if (!newsk)
592 goto out;
594 sock_init_data(NULL, newsk);
596 sctp_copy_sock(newsk, sk, asoc);
597 sock_reset_flag(newsk, SOCK_ZAPPED);
599 sctp_v4_copy_ip_options(sk, newsk);
601 newinet = inet_sk(newsk);
603 newinet->inet_daddr = asoc->peer.primary_addr.v4.sin_addr.s_addr;
605 sk_refcnt_debug_inc(newsk);
607 if (newsk->sk_prot->init(newsk)) {
608 sk_common_release(newsk);
609 newsk = NULL;
612 out:
613 return newsk;
616 static int sctp_v4_addr_to_user(struct sctp_sock *sp, union sctp_addr *addr)
618 /* No address mapping for V4 sockets */
619 memset(addr->v4.sin_zero, 0, sizeof(addr->v4.sin_zero));
620 return sizeof(struct sockaddr_in);
623 /* Dump the v4 addr to the seq file. */
624 static void sctp_v4_seq_dump_addr(struct seq_file *seq, union sctp_addr *addr)
626 seq_printf(seq, "%pI4 ", &addr->v4.sin_addr);
629 static void sctp_v4_ecn_capable(struct sock *sk)
631 INET_ECN_xmit(sk);
634 static void sctp_addr_wq_timeout_handler(struct timer_list *t)
636 struct net *net = from_timer(net, t, sctp.addr_wq_timer);
637 struct sctp_sockaddr_entry *addrw, *temp;
638 struct sctp_sock *sp;
640 spin_lock_bh(&net->sctp.addr_wq_lock);
642 list_for_each_entry_safe(addrw, temp, &net->sctp.addr_waitq, list) {
643 pr_debug("%s: the first ent in wq:%p is addr:%pISc for cmd:%d at "
644 "entry:%p\n", __func__, &net->sctp.addr_waitq, &addrw->a.sa,
645 addrw->state, addrw);
647 #if IS_ENABLED(CONFIG_IPV6)
648 /* Now we send an ASCONF for each association */
649 /* Note. we currently don't handle link local IPv6 addressees */
650 if (addrw->a.sa.sa_family == AF_INET6) {
651 struct in6_addr *in6;
653 if (ipv6_addr_type(&addrw->a.v6.sin6_addr) &
654 IPV6_ADDR_LINKLOCAL)
655 goto free_next;
657 in6 = (struct in6_addr *)&addrw->a.v6.sin6_addr;
658 if (ipv6_chk_addr(net, in6, NULL, 0) == 0 &&
659 addrw->state == SCTP_ADDR_NEW) {
660 unsigned long timeo_val;
662 pr_debug("%s: this is on DAD, trying %d sec "
663 "later\n", __func__,
664 SCTP_ADDRESS_TICK_DELAY);
666 timeo_val = jiffies;
667 timeo_val += msecs_to_jiffies(SCTP_ADDRESS_TICK_DELAY);
668 mod_timer(&net->sctp.addr_wq_timer, timeo_val);
669 break;
672 #endif
673 list_for_each_entry(sp, &net->sctp.auto_asconf_splist, auto_asconf_list) {
674 struct sock *sk;
676 sk = sctp_opt2sk(sp);
677 /* ignore bound-specific endpoints */
678 if (!sctp_is_ep_boundall(sk))
679 continue;
680 bh_lock_sock(sk);
681 if (sctp_asconf_mgmt(sp, addrw) < 0)
682 pr_debug("%s: sctp_asconf_mgmt failed\n", __func__);
683 bh_unlock_sock(sk);
685 #if IS_ENABLED(CONFIG_IPV6)
686 free_next:
687 #endif
688 list_del(&addrw->list);
689 kfree(addrw);
691 spin_unlock_bh(&net->sctp.addr_wq_lock);
694 static void sctp_free_addr_wq(struct net *net)
696 struct sctp_sockaddr_entry *addrw;
697 struct sctp_sockaddr_entry *temp;
699 spin_lock_bh(&net->sctp.addr_wq_lock);
700 del_timer(&net->sctp.addr_wq_timer);
701 list_for_each_entry_safe(addrw, temp, &net->sctp.addr_waitq, list) {
702 list_del(&addrw->list);
703 kfree(addrw);
705 spin_unlock_bh(&net->sctp.addr_wq_lock);
708 /* lookup the entry for the same address in the addr_waitq
709 * sctp_addr_wq MUST be locked
711 static struct sctp_sockaddr_entry *sctp_addr_wq_lookup(struct net *net,
712 struct sctp_sockaddr_entry *addr)
714 struct sctp_sockaddr_entry *addrw;
716 list_for_each_entry(addrw, &net->sctp.addr_waitq, list) {
717 if (addrw->a.sa.sa_family != addr->a.sa.sa_family)
718 continue;
719 if (addrw->a.sa.sa_family == AF_INET) {
720 if (addrw->a.v4.sin_addr.s_addr ==
721 addr->a.v4.sin_addr.s_addr)
722 return addrw;
723 } else if (addrw->a.sa.sa_family == AF_INET6) {
724 if (ipv6_addr_equal(&addrw->a.v6.sin6_addr,
725 &addr->a.v6.sin6_addr))
726 return addrw;
729 return NULL;
732 void sctp_addr_wq_mgmt(struct net *net, struct sctp_sockaddr_entry *addr, int cmd)
734 struct sctp_sockaddr_entry *addrw;
735 unsigned long timeo_val;
737 /* first, we check if an opposite message already exist in the queue.
738 * If we found such message, it is removed.
739 * This operation is a bit stupid, but the DHCP client attaches the
740 * new address after a couple of addition and deletion of that address
743 spin_lock_bh(&net->sctp.addr_wq_lock);
744 /* Offsets existing events in addr_wq */
745 addrw = sctp_addr_wq_lookup(net, addr);
746 if (addrw) {
747 if (addrw->state != cmd) {
748 pr_debug("%s: offsets existing entry for %d, addr:%pISc "
749 "in wq:%p\n", __func__, addrw->state, &addrw->a.sa,
750 &net->sctp.addr_waitq);
752 list_del(&addrw->list);
753 kfree(addrw);
755 spin_unlock_bh(&net->sctp.addr_wq_lock);
756 return;
759 /* OK, we have to add the new address to the wait queue */
760 addrw = kmemdup(addr, sizeof(struct sctp_sockaddr_entry), GFP_ATOMIC);
761 if (addrw == NULL) {
762 spin_unlock_bh(&net->sctp.addr_wq_lock);
763 return;
765 addrw->state = cmd;
766 list_add_tail(&addrw->list, &net->sctp.addr_waitq);
768 pr_debug("%s: add new entry for cmd:%d, addr:%pISc in wq:%p\n",
769 __func__, addrw->state, &addrw->a.sa, &net->sctp.addr_waitq);
771 if (!timer_pending(&net->sctp.addr_wq_timer)) {
772 timeo_val = jiffies;
773 timeo_val += msecs_to_jiffies(SCTP_ADDRESS_TICK_DELAY);
774 mod_timer(&net->sctp.addr_wq_timer, timeo_val);
776 spin_unlock_bh(&net->sctp.addr_wq_lock);
779 /* Event handler for inet address addition/deletion events.
780 * The sctp_local_addr_list needs to be protocted by a spin lock since
781 * multiple notifiers (say IPv4 and IPv6) may be running at the same
782 * time and thus corrupt the list.
783 * The reader side is protected with RCU.
785 static int sctp_inetaddr_event(struct notifier_block *this, unsigned long ev,
786 void *ptr)
788 struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
789 struct sctp_sockaddr_entry *addr = NULL;
790 struct sctp_sockaddr_entry *temp;
791 struct net *net = dev_net(ifa->ifa_dev->dev);
792 int found = 0;
794 switch (ev) {
795 case NETDEV_UP:
796 addr = kzalloc(sizeof(*addr), GFP_ATOMIC);
797 if (addr) {
798 addr->a.v4.sin_family = AF_INET;
799 addr->a.v4.sin_addr.s_addr = ifa->ifa_local;
800 addr->valid = 1;
801 spin_lock_bh(&net->sctp.local_addr_lock);
802 list_add_tail_rcu(&addr->list, &net->sctp.local_addr_list);
803 sctp_addr_wq_mgmt(net, addr, SCTP_ADDR_NEW);
804 spin_unlock_bh(&net->sctp.local_addr_lock);
806 break;
807 case NETDEV_DOWN:
808 spin_lock_bh(&net->sctp.local_addr_lock);
809 list_for_each_entry_safe(addr, temp,
810 &net->sctp.local_addr_list, list) {
811 if (addr->a.sa.sa_family == AF_INET &&
812 addr->a.v4.sin_addr.s_addr ==
813 ifa->ifa_local) {
814 sctp_addr_wq_mgmt(net, addr, SCTP_ADDR_DEL);
815 found = 1;
816 addr->valid = 0;
817 list_del_rcu(&addr->list);
818 break;
821 spin_unlock_bh(&net->sctp.local_addr_lock);
822 if (found)
823 kfree_rcu(addr, rcu);
824 break;
827 return NOTIFY_DONE;
831 * Initialize the control inode/socket with a control endpoint data
832 * structure. This endpoint is reserved exclusively for the OOTB processing.
834 static int sctp_ctl_sock_init(struct net *net)
836 int err;
837 sa_family_t family = PF_INET;
839 if (sctp_get_pf_specific(PF_INET6))
840 family = PF_INET6;
842 err = inet_ctl_sock_create(&net->sctp.ctl_sock, family,
843 SOCK_SEQPACKET, IPPROTO_SCTP, net);
845 /* If IPv6 socket could not be created, try the IPv4 socket */
846 if (err < 0 && family == PF_INET6)
847 err = inet_ctl_sock_create(&net->sctp.ctl_sock, AF_INET,
848 SOCK_SEQPACKET, IPPROTO_SCTP,
849 net);
851 if (err < 0) {
852 pr_err("Failed to create the SCTP control socket\n");
853 return err;
855 return 0;
858 /* Register address family specific functions. */
859 int sctp_register_af(struct sctp_af *af)
861 switch (af->sa_family) {
862 case AF_INET:
863 if (sctp_af_v4_specific)
864 return 0;
865 sctp_af_v4_specific = af;
866 break;
867 case AF_INET6:
868 if (sctp_af_v6_specific)
869 return 0;
870 sctp_af_v6_specific = af;
871 break;
872 default:
873 return 0;
876 INIT_LIST_HEAD(&af->list);
877 list_add_tail(&af->list, &sctp_address_families);
878 return 1;
881 /* Get the table of functions for manipulating a particular address
882 * family.
884 struct sctp_af *sctp_get_af_specific(sa_family_t family)
886 switch (family) {
887 case AF_INET:
888 return sctp_af_v4_specific;
889 case AF_INET6:
890 return sctp_af_v6_specific;
891 default:
892 return NULL;
896 /* Common code to initialize a AF_INET msg_name. */
897 static void sctp_inet_msgname(char *msgname, int *addr_len)
899 struct sockaddr_in *sin;
901 sin = (struct sockaddr_in *)msgname;
902 *addr_len = sizeof(struct sockaddr_in);
903 sin->sin_family = AF_INET;
904 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
907 /* Copy the primary address of the peer primary address as the msg_name. */
908 static void sctp_inet_event_msgname(struct sctp_ulpevent *event, char *msgname,
909 int *addr_len)
911 struct sockaddr_in *sin, *sinfrom;
913 if (msgname) {
914 struct sctp_association *asoc;
916 asoc = event->asoc;
917 sctp_inet_msgname(msgname, addr_len);
918 sin = (struct sockaddr_in *)msgname;
919 sinfrom = &asoc->peer.primary_addr.v4;
920 sin->sin_port = htons(asoc->peer.port);
921 sin->sin_addr.s_addr = sinfrom->sin_addr.s_addr;
925 /* Initialize and copy out a msgname from an inbound skb. */
926 static void sctp_inet_skb_msgname(struct sk_buff *skb, char *msgname, int *len)
928 if (msgname) {
929 struct sctphdr *sh = sctp_hdr(skb);
930 struct sockaddr_in *sin = (struct sockaddr_in *)msgname;
932 sctp_inet_msgname(msgname, len);
933 sin->sin_port = sh->source;
934 sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
938 /* Do we support this AF? */
939 static int sctp_inet_af_supported(sa_family_t family, struct sctp_sock *sp)
941 /* PF_INET only supports AF_INET addresses. */
942 return AF_INET == family;
945 /* Address matching with wildcards allowed. */
946 static int sctp_inet_cmp_addr(const union sctp_addr *addr1,
947 const union sctp_addr *addr2,
948 struct sctp_sock *opt)
950 /* PF_INET only supports AF_INET addresses. */
951 if (addr1->sa.sa_family != addr2->sa.sa_family)
952 return 0;
953 if (htonl(INADDR_ANY) == addr1->v4.sin_addr.s_addr ||
954 htonl(INADDR_ANY) == addr2->v4.sin_addr.s_addr)
955 return 1;
956 if (addr1->v4.sin_addr.s_addr == addr2->v4.sin_addr.s_addr)
957 return 1;
959 return 0;
962 /* Verify that provided sockaddr looks bindable. Common verification has
963 * already been taken care of.
965 static int sctp_inet_bind_verify(struct sctp_sock *opt, union sctp_addr *addr)
967 return sctp_v4_available(addr, opt);
970 /* Verify that sockaddr looks sendable. Common verification has already
971 * been taken care of.
973 static int sctp_inet_send_verify(struct sctp_sock *opt, union sctp_addr *addr)
975 return 1;
978 /* Fill in Supported Address Type information for INIT and INIT-ACK
979 * chunks. Returns number of addresses supported.
981 static int sctp_inet_supported_addrs(const struct sctp_sock *opt,
982 __be16 *types)
984 types[0] = SCTP_PARAM_IPV4_ADDRESS;
985 return 1;
988 /* Wrapper routine that calls the ip transmit routine. */
989 static inline int sctp_v4_xmit(struct sk_buff *skb,
990 struct sctp_transport *transport)
992 struct inet_sock *inet = inet_sk(skb->sk);
993 __u8 dscp = inet->tos;
995 pr_debug("%s: skb:%p, len:%d, src:%pI4, dst:%pI4\n", __func__, skb,
996 skb->len, &transport->fl.u.ip4.saddr,
997 &transport->fl.u.ip4.daddr);
999 if (transport->dscp & SCTP_DSCP_SET_MASK)
1000 dscp = transport->dscp & SCTP_DSCP_VAL_MASK;
1002 inet->pmtudisc = transport->param_flags & SPP_PMTUD_ENABLE ?
1003 IP_PMTUDISC_DO : IP_PMTUDISC_DONT;
1005 SCTP_INC_STATS(sock_net(&inet->sk), SCTP_MIB_OUTSCTPPACKS);
1007 return __ip_queue_xmit(&inet->sk, skb, &transport->fl, dscp);
1010 static struct sctp_af sctp_af_inet;
1012 static struct sctp_pf sctp_pf_inet = {
1013 .event_msgname = sctp_inet_event_msgname,
1014 .skb_msgname = sctp_inet_skb_msgname,
1015 .af_supported = sctp_inet_af_supported,
1016 .cmp_addr = sctp_inet_cmp_addr,
1017 .bind_verify = sctp_inet_bind_verify,
1018 .send_verify = sctp_inet_send_verify,
1019 .supported_addrs = sctp_inet_supported_addrs,
1020 .create_accept_sk = sctp_v4_create_accept_sk,
1021 .addr_to_user = sctp_v4_addr_to_user,
1022 .to_sk_saddr = sctp_v4_to_sk_saddr,
1023 .to_sk_daddr = sctp_v4_to_sk_daddr,
1024 .copy_ip_options = sctp_v4_copy_ip_options,
1025 .af = &sctp_af_inet
1028 /* Notifier for inetaddr addition/deletion events. */
1029 static struct notifier_block sctp_inetaddr_notifier = {
1030 .notifier_call = sctp_inetaddr_event,
1033 /* Socket operations. */
1034 static const struct proto_ops inet_seqpacket_ops = {
1035 .family = PF_INET,
1036 .owner = THIS_MODULE,
1037 .release = inet_release, /* Needs to be wrapped... */
1038 .bind = inet_bind,
1039 .connect = sctp_inet_connect,
1040 .socketpair = sock_no_socketpair,
1041 .accept = inet_accept,
1042 .getname = inet_getname, /* Semantics are different. */
1043 .poll = sctp_poll,
1044 .ioctl = inet_ioctl,
1045 .listen = sctp_inet_listen,
1046 .shutdown = inet_shutdown, /* Looks harmless. */
1047 .setsockopt = sock_common_setsockopt, /* IP_SOL IP_OPTION is a problem */
1048 .getsockopt = sock_common_getsockopt,
1049 .sendmsg = inet_sendmsg,
1050 .recvmsg = inet_recvmsg,
1051 .mmap = sock_no_mmap,
1052 .sendpage = sock_no_sendpage,
1053 #ifdef CONFIG_COMPAT
1054 .compat_setsockopt = compat_sock_common_setsockopt,
1055 .compat_getsockopt = compat_sock_common_getsockopt,
1056 #endif
1059 /* Registration with AF_INET family. */
1060 static struct inet_protosw sctp_seqpacket_protosw = {
1061 .type = SOCK_SEQPACKET,
1062 .protocol = IPPROTO_SCTP,
1063 .prot = &sctp_prot,
1064 .ops = &inet_seqpacket_ops,
1065 .flags = SCTP_PROTOSW_FLAG
1067 static struct inet_protosw sctp_stream_protosw = {
1068 .type = SOCK_STREAM,
1069 .protocol = IPPROTO_SCTP,
1070 .prot = &sctp_prot,
1071 .ops = &inet_seqpacket_ops,
1072 .flags = SCTP_PROTOSW_FLAG
1075 /* Register with IP layer. */
1076 static const struct net_protocol sctp_protocol = {
1077 .handler = sctp_rcv,
1078 .err_handler = sctp_v4_err,
1079 .no_policy = 1,
1080 .netns_ok = 1,
1081 .icmp_strict_tag_validation = 1,
1084 /* IPv4 address related functions. */
1085 static struct sctp_af sctp_af_inet = {
1086 .sa_family = AF_INET,
1087 .sctp_xmit = sctp_v4_xmit,
1088 .setsockopt = ip_setsockopt,
1089 .getsockopt = ip_getsockopt,
1090 .get_dst = sctp_v4_get_dst,
1091 .get_saddr = sctp_v4_get_saddr,
1092 .copy_addrlist = sctp_v4_copy_addrlist,
1093 .from_skb = sctp_v4_from_skb,
1094 .from_sk = sctp_v4_from_sk,
1095 .from_addr_param = sctp_v4_from_addr_param,
1096 .to_addr_param = sctp_v4_to_addr_param,
1097 .cmp_addr = sctp_v4_cmp_addr,
1098 .addr_valid = sctp_v4_addr_valid,
1099 .inaddr_any = sctp_v4_inaddr_any,
1100 .is_any = sctp_v4_is_any,
1101 .available = sctp_v4_available,
1102 .scope = sctp_v4_scope,
1103 .skb_iif = sctp_v4_skb_iif,
1104 .is_ce = sctp_v4_is_ce,
1105 .seq_dump_addr = sctp_v4_seq_dump_addr,
1106 .ecn_capable = sctp_v4_ecn_capable,
1107 .net_header_len = sizeof(struct iphdr),
1108 .sockaddr_len = sizeof(struct sockaddr_in),
1109 .ip_options_len = sctp_v4_ip_options_len,
1110 #ifdef CONFIG_COMPAT
1111 .compat_setsockopt = compat_ip_setsockopt,
1112 .compat_getsockopt = compat_ip_getsockopt,
1113 #endif
1116 struct sctp_pf *sctp_get_pf_specific(sa_family_t family)
1118 switch (family) {
1119 case PF_INET:
1120 return sctp_pf_inet_specific;
1121 case PF_INET6:
1122 return sctp_pf_inet6_specific;
1123 default:
1124 return NULL;
1128 /* Register the PF specific function table. */
1129 int sctp_register_pf(struct sctp_pf *pf, sa_family_t family)
1131 switch (family) {
1132 case PF_INET:
1133 if (sctp_pf_inet_specific)
1134 return 0;
1135 sctp_pf_inet_specific = pf;
1136 break;
1137 case PF_INET6:
1138 if (sctp_pf_inet6_specific)
1139 return 0;
1140 sctp_pf_inet6_specific = pf;
1141 break;
1142 default:
1143 return 0;
1145 return 1;
1148 static inline int init_sctp_mibs(struct net *net)
1150 net->sctp.sctp_statistics = alloc_percpu(struct sctp_mib);
1151 if (!net->sctp.sctp_statistics)
1152 return -ENOMEM;
1153 return 0;
1156 static inline void cleanup_sctp_mibs(struct net *net)
1158 free_percpu(net->sctp.sctp_statistics);
1161 static void sctp_v4_pf_init(void)
1163 /* Initialize the SCTP specific PF functions. */
1164 sctp_register_pf(&sctp_pf_inet, PF_INET);
1165 sctp_register_af(&sctp_af_inet);
1168 static void sctp_v4_pf_exit(void)
1170 list_del(&sctp_af_inet.list);
1173 static int sctp_v4_protosw_init(void)
1175 int rc;
1177 rc = proto_register(&sctp_prot, 1);
1178 if (rc)
1179 return rc;
1181 /* Register SCTP(UDP and TCP style) with socket layer. */
1182 inet_register_protosw(&sctp_seqpacket_protosw);
1183 inet_register_protosw(&sctp_stream_protosw);
1185 return 0;
1188 static void sctp_v4_protosw_exit(void)
1190 inet_unregister_protosw(&sctp_stream_protosw);
1191 inet_unregister_protosw(&sctp_seqpacket_protosw);
1192 proto_unregister(&sctp_prot);
1195 static int sctp_v4_add_protocol(void)
1197 /* Register notifier for inet address additions/deletions. */
1198 register_inetaddr_notifier(&sctp_inetaddr_notifier);
1200 /* Register SCTP with inet layer. */
1201 if (inet_add_protocol(&sctp_protocol, IPPROTO_SCTP) < 0)
1202 return -EAGAIN;
1204 return 0;
1207 static void sctp_v4_del_protocol(void)
1209 inet_del_protocol(&sctp_protocol, IPPROTO_SCTP);
1210 unregister_inetaddr_notifier(&sctp_inetaddr_notifier);
1213 static int __net_init sctp_defaults_init(struct net *net)
1215 int status;
1218 * 14. Suggested SCTP Protocol Parameter Values
1220 /* The following protocol parameters are RECOMMENDED: */
1221 /* RTO.Initial - 3 seconds */
1222 net->sctp.rto_initial = SCTP_RTO_INITIAL;
1223 /* RTO.Min - 1 second */
1224 net->sctp.rto_min = SCTP_RTO_MIN;
1225 /* RTO.Max - 60 seconds */
1226 net->sctp.rto_max = SCTP_RTO_MAX;
1227 /* RTO.Alpha - 1/8 */
1228 net->sctp.rto_alpha = SCTP_RTO_ALPHA;
1229 /* RTO.Beta - 1/4 */
1230 net->sctp.rto_beta = SCTP_RTO_BETA;
1232 /* Valid.Cookie.Life - 60 seconds */
1233 net->sctp.valid_cookie_life = SCTP_DEFAULT_COOKIE_LIFE;
1235 /* Whether Cookie Preservative is enabled(1) or not(0) */
1236 net->sctp.cookie_preserve_enable = 1;
1238 /* Default sctp sockets to use md5 as their hmac alg */
1239 #if defined (CONFIG_SCTP_DEFAULT_COOKIE_HMAC_MD5)
1240 net->sctp.sctp_hmac_alg = "md5";
1241 #elif defined (CONFIG_SCTP_DEFAULT_COOKIE_HMAC_SHA1)
1242 net->sctp.sctp_hmac_alg = "sha1";
1243 #else
1244 net->sctp.sctp_hmac_alg = NULL;
1245 #endif
1247 /* Max.Burst - 4 */
1248 net->sctp.max_burst = SCTP_DEFAULT_MAX_BURST;
1250 /* Enable pf state by default */
1251 net->sctp.pf_enable = 1;
1253 /* Association.Max.Retrans - 10 attempts
1254 * Path.Max.Retrans - 5 attempts (per destination address)
1255 * Max.Init.Retransmits - 8 attempts
1257 net->sctp.max_retrans_association = 10;
1258 net->sctp.max_retrans_path = 5;
1259 net->sctp.max_retrans_init = 8;
1261 /* Sendbuffer growth - do per-socket accounting */
1262 net->sctp.sndbuf_policy = 0;
1264 /* Rcvbuffer growth - do per-socket accounting */
1265 net->sctp.rcvbuf_policy = 0;
1267 /* HB.interval - 30 seconds */
1268 net->sctp.hb_interval = SCTP_DEFAULT_TIMEOUT_HEARTBEAT;
1270 /* delayed SACK timeout */
1271 net->sctp.sack_timeout = SCTP_DEFAULT_TIMEOUT_SACK;
1273 /* Disable ADDIP by default. */
1274 net->sctp.addip_enable = 0;
1275 net->sctp.addip_noauth = 0;
1276 net->sctp.default_auto_asconf = 0;
1278 /* Enable PR-SCTP by default. */
1279 net->sctp.prsctp_enable = 1;
1281 /* Disable RECONF by default. */
1282 net->sctp.reconf_enable = 0;
1284 /* Disable AUTH by default. */
1285 net->sctp.auth_enable = 0;
1287 /* Set SCOPE policy to enabled */
1288 net->sctp.scope_policy = SCTP_SCOPE_POLICY_ENABLE;
1290 /* Set the default rwnd update threshold */
1291 net->sctp.rwnd_upd_shift = SCTP_DEFAULT_RWND_SHIFT;
1293 /* Initialize maximum autoclose timeout. */
1294 net->sctp.max_autoclose = INT_MAX / HZ;
1296 status = sctp_sysctl_net_register(net);
1297 if (status)
1298 goto err_sysctl_register;
1300 /* Allocate and initialise sctp mibs. */
1301 status = init_sctp_mibs(net);
1302 if (status)
1303 goto err_init_mibs;
1305 #ifdef CONFIG_PROC_FS
1306 /* Initialize proc fs directory. */
1307 status = sctp_proc_init(net);
1308 if (status)
1309 goto err_init_proc;
1310 #endif
1312 sctp_dbg_objcnt_init(net);
1314 /* Initialize the local address list. */
1315 INIT_LIST_HEAD(&net->sctp.local_addr_list);
1316 spin_lock_init(&net->sctp.local_addr_lock);
1317 sctp_get_local_addr_list(net);
1319 /* Initialize the address event list */
1320 INIT_LIST_HEAD(&net->sctp.addr_waitq);
1321 INIT_LIST_HEAD(&net->sctp.auto_asconf_splist);
1322 spin_lock_init(&net->sctp.addr_wq_lock);
1323 net->sctp.addr_wq_timer.expires = 0;
1324 timer_setup(&net->sctp.addr_wq_timer, sctp_addr_wq_timeout_handler, 0);
1326 return 0;
1328 #ifdef CONFIG_PROC_FS
1329 err_init_proc:
1330 cleanup_sctp_mibs(net);
1331 #endif
1332 err_init_mibs:
1333 sctp_sysctl_net_unregister(net);
1334 err_sysctl_register:
1335 return status;
1338 static void __net_exit sctp_defaults_exit(struct net *net)
1340 /* Free the local address list */
1341 sctp_free_addr_wq(net);
1342 sctp_free_local_addr_list(net);
1344 #ifdef CONFIG_PROC_FS
1345 remove_proc_subtree("sctp", net->proc_net);
1346 net->sctp.proc_net_sctp = NULL;
1347 #endif
1348 cleanup_sctp_mibs(net);
1349 sctp_sysctl_net_unregister(net);
1352 static struct pernet_operations sctp_defaults_ops = {
1353 .init = sctp_defaults_init,
1354 .exit = sctp_defaults_exit,
1357 static int __net_init sctp_ctrlsock_init(struct net *net)
1359 int status;
1361 /* Initialize the control inode/socket for handling OOTB packets. */
1362 status = sctp_ctl_sock_init(net);
1363 if (status)
1364 pr_err("Failed to initialize the SCTP control sock\n");
1366 return status;
1369 static void __net_exit sctp_ctrlsock_exit(struct net *net)
1371 /* Free the control endpoint. */
1372 inet_ctl_sock_destroy(net->sctp.ctl_sock);
1375 static struct pernet_operations sctp_ctrlsock_ops = {
1376 .init = sctp_ctrlsock_init,
1377 .exit = sctp_ctrlsock_exit,
1380 /* Initialize the universe into something sensible. */
1381 static __init int sctp_init(void)
1383 int i;
1384 int status = -EINVAL;
1385 unsigned long goal;
1386 unsigned long limit;
1387 int max_share;
1388 int order;
1389 int num_entries;
1390 int max_entry_order;
1392 sock_skb_cb_check_size(sizeof(struct sctp_ulpevent));
1394 /* Allocate bind_bucket and chunk caches. */
1395 status = -ENOBUFS;
1396 sctp_bucket_cachep = kmem_cache_create("sctp_bind_bucket",
1397 sizeof(struct sctp_bind_bucket),
1398 0, SLAB_HWCACHE_ALIGN,
1399 NULL);
1400 if (!sctp_bucket_cachep)
1401 goto out;
1403 sctp_chunk_cachep = kmem_cache_create("sctp_chunk",
1404 sizeof(struct sctp_chunk),
1405 0, SLAB_HWCACHE_ALIGN,
1406 NULL);
1407 if (!sctp_chunk_cachep)
1408 goto err_chunk_cachep;
1410 status = percpu_counter_init(&sctp_sockets_allocated, 0, GFP_KERNEL);
1411 if (status)
1412 goto err_percpu_counter_init;
1414 /* Implementation specific variables. */
1416 /* Initialize default stream count setup information. */
1417 sctp_max_instreams = SCTP_DEFAULT_INSTREAMS;
1418 sctp_max_outstreams = SCTP_DEFAULT_OUTSTREAMS;
1420 /* Initialize handle used for association ids. */
1421 idr_init(&sctp_assocs_id);
1423 limit = nr_free_buffer_pages() / 8;
1424 limit = max(limit, 128UL);
1425 sysctl_sctp_mem[0] = limit / 4 * 3;
1426 sysctl_sctp_mem[1] = limit;
1427 sysctl_sctp_mem[2] = sysctl_sctp_mem[0] * 2;
1429 /* Set per-socket limits to no more than 1/128 the pressure threshold*/
1430 limit = (sysctl_sctp_mem[1]) << (PAGE_SHIFT - 7);
1431 max_share = min(4UL*1024*1024, limit);
1433 sysctl_sctp_rmem[0] = SK_MEM_QUANTUM; /* give each asoc 1 page min */
1434 sysctl_sctp_rmem[1] = 1500 * SKB_TRUESIZE(1);
1435 sysctl_sctp_rmem[2] = max(sysctl_sctp_rmem[1], max_share);
1437 sysctl_sctp_wmem[0] = SK_MEM_QUANTUM;
1438 sysctl_sctp_wmem[1] = 16*1024;
1439 sysctl_sctp_wmem[2] = max(64*1024, max_share);
1441 /* Size and allocate the association hash table.
1442 * The methodology is similar to that of the tcp hash tables.
1443 * Though not identical. Start by getting a goal size
1445 if (totalram_pages >= (128 * 1024))
1446 goal = totalram_pages >> (22 - PAGE_SHIFT);
1447 else
1448 goal = totalram_pages >> (24 - PAGE_SHIFT);
1450 /* Then compute the page order for said goal */
1451 order = get_order(goal);
1453 /* Now compute the required page order for the maximum sized table we
1454 * want to create
1456 max_entry_order = get_order(MAX_SCTP_PORT_HASH_ENTRIES *
1457 sizeof(struct sctp_bind_hashbucket));
1459 /* Limit the page order by that maximum hash table size */
1460 order = min(order, max_entry_order);
1462 /* Allocate and initialize the endpoint hash table. */
1463 sctp_ep_hashsize = 64;
1464 sctp_ep_hashtable =
1465 kmalloc_array(64, sizeof(struct sctp_hashbucket), GFP_KERNEL);
1466 if (!sctp_ep_hashtable) {
1467 pr_err("Failed endpoint_hash alloc\n");
1468 status = -ENOMEM;
1469 goto err_ehash_alloc;
1471 for (i = 0; i < sctp_ep_hashsize; i++) {
1472 rwlock_init(&sctp_ep_hashtable[i].lock);
1473 INIT_HLIST_HEAD(&sctp_ep_hashtable[i].chain);
1476 /* Allocate and initialize the SCTP port hash table.
1477 * Note that order is initalized to start at the max sized
1478 * table we want to support. If we can't get that many pages
1479 * reduce the order and try again
1481 do {
1482 sctp_port_hashtable = (struct sctp_bind_hashbucket *)
1483 __get_free_pages(GFP_KERNEL | __GFP_NOWARN, order);
1484 } while (!sctp_port_hashtable && --order > 0);
1486 if (!sctp_port_hashtable) {
1487 pr_err("Failed bind hash alloc\n");
1488 status = -ENOMEM;
1489 goto err_bhash_alloc;
1492 /* Now compute the number of entries that will fit in the
1493 * port hash space we allocated
1495 num_entries = (1UL << order) * PAGE_SIZE /
1496 sizeof(struct sctp_bind_hashbucket);
1498 /* And finish by rounding it down to the nearest power of two
1499 * this wastes some memory of course, but its needed because
1500 * the hash function operates based on the assumption that
1501 * that the number of entries is a power of two
1503 sctp_port_hashsize = rounddown_pow_of_two(num_entries);
1505 for (i = 0; i < sctp_port_hashsize; i++) {
1506 spin_lock_init(&sctp_port_hashtable[i].lock);
1507 INIT_HLIST_HEAD(&sctp_port_hashtable[i].chain);
1510 status = sctp_transport_hashtable_init();
1511 if (status)
1512 goto err_thash_alloc;
1514 pr_info("Hash tables configured (bind %d/%d)\n", sctp_port_hashsize,
1515 num_entries);
1517 sctp_sysctl_register();
1519 INIT_LIST_HEAD(&sctp_address_families);
1520 sctp_v4_pf_init();
1521 sctp_v6_pf_init();
1522 sctp_sched_ops_init();
1524 status = register_pernet_subsys(&sctp_defaults_ops);
1525 if (status)
1526 goto err_register_defaults;
1528 status = sctp_v4_protosw_init();
1529 if (status)
1530 goto err_protosw_init;
1532 status = sctp_v6_protosw_init();
1533 if (status)
1534 goto err_v6_protosw_init;
1536 status = register_pernet_subsys(&sctp_ctrlsock_ops);
1537 if (status)
1538 goto err_register_ctrlsock;
1540 status = sctp_v4_add_protocol();
1541 if (status)
1542 goto err_add_protocol;
1544 /* Register SCTP with inet6 layer. */
1545 status = sctp_v6_add_protocol();
1546 if (status)
1547 goto err_v6_add_protocol;
1549 if (sctp_offload_init() < 0)
1550 pr_crit("%s: Cannot add SCTP protocol offload\n", __func__);
1552 out:
1553 return status;
1554 err_v6_add_protocol:
1555 sctp_v4_del_protocol();
1556 err_add_protocol:
1557 unregister_pernet_subsys(&sctp_ctrlsock_ops);
1558 err_register_ctrlsock:
1559 sctp_v6_protosw_exit();
1560 err_v6_protosw_init:
1561 sctp_v4_protosw_exit();
1562 err_protosw_init:
1563 unregister_pernet_subsys(&sctp_defaults_ops);
1564 err_register_defaults:
1565 sctp_v4_pf_exit();
1566 sctp_v6_pf_exit();
1567 sctp_sysctl_unregister();
1568 free_pages((unsigned long)sctp_port_hashtable,
1569 get_order(sctp_port_hashsize *
1570 sizeof(struct sctp_bind_hashbucket)));
1571 err_bhash_alloc:
1572 sctp_transport_hashtable_destroy();
1573 err_thash_alloc:
1574 kfree(sctp_ep_hashtable);
1575 err_ehash_alloc:
1576 percpu_counter_destroy(&sctp_sockets_allocated);
1577 err_percpu_counter_init:
1578 kmem_cache_destroy(sctp_chunk_cachep);
1579 err_chunk_cachep:
1580 kmem_cache_destroy(sctp_bucket_cachep);
1581 goto out;
1584 /* Exit handler for the SCTP protocol. */
1585 static __exit void sctp_exit(void)
1587 /* BUG. This should probably do something useful like clean
1588 * up all the remaining associations and all that memory.
1591 /* Unregister with inet6/inet layers. */
1592 sctp_v6_del_protocol();
1593 sctp_v4_del_protocol();
1595 unregister_pernet_subsys(&sctp_ctrlsock_ops);
1597 /* Free protosw registrations */
1598 sctp_v6_protosw_exit();
1599 sctp_v4_protosw_exit();
1601 unregister_pernet_subsys(&sctp_defaults_ops);
1603 /* Unregister with socket layer. */
1604 sctp_v6_pf_exit();
1605 sctp_v4_pf_exit();
1607 sctp_sysctl_unregister();
1609 free_pages((unsigned long)sctp_port_hashtable,
1610 get_order(sctp_port_hashsize *
1611 sizeof(struct sctp_bind_hashbucket)));
1612 kfree(sctp_ep_hashtable);
1613 sctp_transport_hashtable_destroy();
1615 percpu_counter_destroy(&sctp_sockets_allocated);
1617 rcu_barrier(); /* Wait for completion of call_rcu()'s */
1619 kmem_cache_destroy(sctp_chunk_cachep);
1620 kmem_cache_destroy(sctp_bucket_cachep);
1623 module_init(sctp_init);
1624 module_exit(sctp_exit);
1627 * __stringify doesn't likes enums, so use IPPROTO_SCTP value (132) directly.
1629 MODULE_ALIAS("net-pf-" __stringify(PF_INET) "-proto-132");
1630 MODULE_ALIAS("net-pf-" __stringify(PF_INET6) "-proto-132");
1631 MODULE_AUTHOR("Linux Kernel SCTP developers <linux-sctp@vger.kernel.org>");
1632 MODULE_DESCRIPTION("Support for the SCTP protocol (RFC2960)");
1633 module_param_named(no_checksums, sctp_checksum_disable, bool, 0644);
1634 MODULE_PARM_DESC(no_checksums, "Disable checksums computing and verification");
1635 MODULE_LICENSE("GPL");