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