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