blob: 7ee120e859135cd0f0477af4821c68e119b59d5b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* SCTP kernel reference Implementation
2 * (C) Copyright IBM Corp. 2001, 2004
3 * Copyright (c) 1999-2000 Cisco, Inc.
4 * Copyright (c) 1999-2001 Motorola, Inc.
5 * Copyright (c) 2001 Intel Corp.
6 * Copyright (c) 2001 Nokia, Inc.
7 * Copyright (c) 2001 La Monte H.P. Yarroll
8 *
9 * This file is part of the SCTP kernel reference Implementation
10 *
11 * Initialization/cleanup for SCTP protocol support.
12 *
13 * The SCTP reference implementation is free software;
14 * you can redistribute it and/or modify it under the terms of
15 * the GNU General Public License as published by
16 * the Free Software Foundation; either version 2, or (at your option)
17 * any later version.
18 *
19 * The SCTP reference implementation is distributed in the hope that it
20 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
21 * ************************
22 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
23 * See the GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License
26 * along with GNU CC; see the file COPYING. If not, 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
49#include <linux/module.h>
50#include <linux/init.h>
51#include <linux/netdevice.h>
52#include <linux/inetdevice.h>
53#include <linux/seq_file.h>
54#include <net/protocol.h>
55#include <net/ip.h>
56#include <net/ipv6.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020057#include <net/route.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#include <net/sctp/sctp.h>
59#include <net/addrconf.h>
60#include <net/inet_common.h>
61#include <net/inet_ecn.h>
62
63/* Global data structures. */
Brian Haley4cbf1ca2006-09-18 00:04:22 -070064struct sctp_globals sctp_globals __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065struct proc_dir_entry *proc_net_sctp;
Eric Dumazetba899662005-08-26 12:05:31 -070066DEFINE_SNMP_STAT(struct sctp_mib, sctp_statistics) __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68struct idr sctp_assocs_id;
69DEFINE_SPINLOCK(sctp_assocs_id_lock);
70
71/* This is the global socket data structure used for responding to
72 * the Out-of-the-blue (OOTB) packets. A control sock will be created
73 * for this socket at the initialization time.
74 */
75static struct socket *sctp_ctl_socket;
76
77static struct sctp_pf *sctp_pf_inet6_specific;
78static struct sctp_pf *sctp_pf_inet_specific;
79static struct sctp_af *sctp_af_v4_specific;
80static struct sctp_af *sctp_af_v6_specific;
81
Christoph Lametere18b8902006-12-06 20:33:20 -080082struct kmem_cache *sctp_chunk_cachep __read_mostly;
83struct kmem_cache *sctp_bucket_cachep __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Linus Torvalds1da177e2005-04-16 15:20:36 -070085/* Return the address of the control sock. */
86struct sock *sctp_get_ctl_sock(void)
87{
88 return sctp_ctl_socket->sk;
89}
90
91/* Set up the proc fs entry for the SCTP protocol. */
92static __init int sctp_proc_init(void)
93{
94 if (!proc_net_sctp) {
95 struct proc_dir_entry *ent;
96 ent = proc_mkdir("net/sctp", NULL);
97 if (ent) {
98 ent->owner = THIS_MODULE;
99 proc_net_sctp = ent;
100 } else
101 goto out_nomem;
102 }
103
104 if (sctp_snmp_proc_init())
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900105 goto out_nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 if (sctp_eps_proc_init())
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900107 goto out_nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 if (sctp_assocs_proc_init())
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900109 goto out_nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
111 return 0;
112
113out_nomem:
114 return -ENOMEM;
115}
116
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900117/* Clean up the proc fs entry for the SCTP protocol.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 * Note: Do not make this __exit as it is used in the init error
119 * path.
120 */
121static void sctp_proc_exit(void)
122{
123 sctp_snmp_proc_exit();
124 sctp_eps_proc_exit();
125 sctp_assocs_proc_exit();
126
127 if (proc_net_sctp) {
128 proc_net_sctp = NULL;
129 remove_proc_entry("net/sctp", NULL);
130 }
131}
132
133/* Private helper to extract ipv4 address and stash them in
134 * the protocol structure.
135 */
136static void sctp_v4_copy_addrlist(struct list_head *addrlist,
137 struct net_device *dev)
138{
139 struct in_device *in_dev;
140 struct in_ifaddr *ifa;
141 struct sctp_sockaddr_entry *addr;
142
143 rcu_read_lock();
Herbert Xue5ed6392005-10-03 14:35:55 -0700144 if ((in_dev = __in_dev_get_rcu(dev)) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 rcu_read_unlock();
146 return;
147 }
148
149 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
150 /* Add the address to the local list. */
151 addr = t_new(struct sctp_sockaddr_entry, GFP_ATOMIC);
152 if (addr) {
Al Viro2a6fd782006-11-20 17:04:42 -0800153 addr->a.v4.sin_family = AF_INET;
154 addr->a.v4.sin_port = 0;
155 addr->a.v4.sin_addr.s_addr = ifa->ifa_local;
Vlad Yasevich29303542007-09-16 16:02:12 -0700156 addr->valid = 1;
157 INIT_LIST_HEAD(&addr->list);
158 INIT_RCU_HEAD(&addr->rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 list_add_tail(&addr->list, addrlist);
160 }
161 }
162
163 rcu_read_unlock();
164}
165
166/* Extract our IP addresses from the system and stash them in the
167 * protocol structure.
168 */
Sridhar Samudrala29c7cf92006-12-13 16:26:26 -0800169static void sctp_get_local_addr_list(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170{
171 struct net_device *dev;
172 struct list_head *pos;
173 struct sctp_af *af;
174
175 read_lock(&dev_base_lock);
Pavel Emelianov7562f872007-05-03 15:13:45 -0700176 for_each_netdev(dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 __list_for_each(pos, &sctp_address_families) {
178 af = list_entry(pos, struct sctp_af, list);
179 af->copy_addrlist(&sctp_local_addr_list, dev);
180 }
181 }
182 read_unlock(&dev_base_lock);
183}
184
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185/* Free the existing local addresses. */
Sridhar Samudrala29c7cf92006-12-13 16:26:26 -0800186static void sctp_free_local_addr_list(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{
188 struct sctp_sockaddr_entry *addr;
189 struct list_head *pos, *temp;
190
191 list_for_each_safe(pos, temp, &sctp_local_addr_list) {
192 addr = list_entry(pos, struct sctp_sockaddr_entry, list);
193 list_del(pos);
194 kfree(addr);
195 }
196}
197
Vlad Yasevich29303542007-09-16 16:02:12 -0700198void sctp_local_addr_free(struct rcu_head *head)
199{
200 struct sctp_sockaddr_entry *e = container_of(head,
201 struct sctp_sockaddr_entry, rcu);
202 kfree(e);
203}
204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205/* Copy the local addresses which are valid for 'scope' into 'bp'. */
206int sctp_copy_local_addr_list(struct sctp_bind_addr *bp, sctp_scope_t scope,
Al Virodd0fc662005-10-07 07:46:04 +0100207 gfp_t gfp, int copy_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208{
209 struct sctp_sockaddr_entry *addr;
210 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Vlad Yasevich29303542007-09-16 16:02:12 -0700212 rcu_read_lock();
213 list_for_each_entry_rcu(addr, &sctp_local_addr_list, list) {
214 if (!addr->valid)
215 continue;
Al Viro6244be42006-11-20 17:21:44 -0800216 if (sctp_in_scope(&addr->a, scope)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 /* Now that the address is in scope, check to see if
218 * the address type is really supported by the local
219 * sock as well as the remote peer.
220 */
Al Viro6244be42006-11-20 17:21:44 -0800221 if ((((AF_INET == addr->a.sa.sa_family) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 (copy_flags & SCTP_ADDR4_PEERSUPP))) ||
Al Viro6244be42006-11-20 17:21:44 -0800223 (((AF_INET6 == addr->a.sa.sa_family) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 (copy_flags & SCTP_ADDR6_ALLOWED) &&
225 (copy_flags & SCTP_ADDR6_PEERSUPP)))) {
Al Viro5ab7b852006-11-20 17:10:38 -0800226 error = sctp_add_bind_addr(bp, &addr->a, 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 GFP_ATOMIC);
228 if (error)
229 goto end_copy;
230 }
231 }
232 }
233
234end_copy:
Vlad Yasevich29303542007-09-16 16:02:12 -0700235 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 return error;
237}
238
239/* Initialize a sctp_addr from in incoming skb. */
240static void sctp_v4_from_skb(union sctp_addr *addr, struct sk_buff *skb,
241 int is_saddr)
242{
243 void *from;
Al Virod55c41b2006-11-20 17:09:40 -0800244 __be16 *port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 struct sctphdr *sh;
246
247 port = &addr->v4.sin_port;
248 addr->v4.sin_family = AF_INET;
249
Arnaldo Carvalho de Melo2c0fd382007-03-13 13:59:32 -0300250 sh = sctp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 if (is_saddr) {
Al Virod55c41b2006-11-20 17:09:40 -0800252 *port = sh->source;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700253 from = &ip_hdr(skb)->saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 } else {
Al Virod55c41b2006-11-20 17:09:40 -0800255 *port = sh->dest;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700256 from = &ip_hdr(skb)->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 }
258 memcpy(&addr->v4.sin_addr.s_addr, from, sizeof(struct in_addr));
259}
260
261/* Initialize an sctp_addr from a socket. */
262static void sctp_v4_from_sk(union sctp_addr *addr, struct sock *sk)
263{
264 addr->v4.sin_family = AF_INET;
Al Viro7dcdbd92006-11-20 17:24:21 -0800265 addr->v4.sin_port = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 addr->v4.sin_addr.s_addr = inet_sk(sk)->rcv_saddr;
267}
268
269/* Initialize sk->sk_rcv_saddr from sctp_addr. */
270static void sctp_v4_to_sk_saddr(union sctp_addr *addr, struct sock *sk)
271{
272 inet_sk(sk)->rcv_saddr = addr->v4.sin_addr.s_addr;
273}
274
275/* Initialize sk->sk_daddr from sctp_addr. */
276static void sctp_v4_to_sk_daddr(union sctp_addr *addr, struct sock *sk)
277{
278 inet_sk(sk)->daddr = addr->v4.sin_addr.s_addr;
279}
280
281/* Initialize a sctp_addr from an address parameter. */
282static void sctp_v4_from_addr_param(union sctp_addr *addr,
283 union sctp_addr_param *param,
Al Virodd86d132006-11-20 17:11:13 -0800284 __be16 port, int iif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285{
286 addr->v4.sin_family = AF_INET;
287 addr->v4.sin_port = port;
288 addr->v4.sin_addr.s_addr = param->v4.addr.s_addr;
289}
290
291/* Initialize an address parameter from a sctp_addr and return the length
292 * of the address parameter.
293 */
294static int sctp_v4_to_addr_param(const union sctp_addr *addr,
295 union sctp_addr_param *param)
296{
297 int length = sizeof(sctp_ipv4addr_param_t);
298
299 param->v4.param_hdr.type = SCTP_PARAM_IPV4_ADDRESS;
Al Virodbc16db2006-11-20 17:01:42 -0800300 param->v4.param_hdr.length = htons(length);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900301 param->v4.addr.s_addr = addr->v4.sin_addr.s_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
303 return length;
304}
305
306/* Initialize a sctp_addr from a dst_entry. */
307static void sctp_v4_dst_saddr(union sctp_addr *saddr, struct dst_entry *dst,
Al Viro854d43a2006-11-20 17:06:24 -0800308 __be16 port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
310 struct rtable *rt = (struct rtable *)dst;
311 saddr->v4.sin_family = AF_INET;
312 saddr->v4.sin_port = port;
313 saddr->v4.sin_addr.s_addr = rt->rt_src;
314}
315
316/* Compare two addresses exactly. */
317static int sctp_v4_cmp_addr(const union sctp_addr *addr1,
318 const union sctp_addr *addr2)
319{
320 if (addr1->sa.sa_family != addr2->sa.sa_family)
321 return 0;
322 if (addr1->v4.sin_port != addr2->v4.sin_port)
323 return 0;
324 if (addr1->v4.sin_addr.s_addr != addr2->v4.sin_addr.s_addr)
325 return 0;
326
327 return 1;
328}
329
330/* Initialize addr struct to INADDR_ANY. */
Al Viro6fbfa9f2006-11-20 17:24:53 -0800331static void sctp_v4_inaddr_any(union sctp_addr *addr, __be16 port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332{
333 addr->v4.sin_family = AF_INET;
334 addr->v4.sin_addr.s_addr = INADDR_ANY;
335 addr->v4.sin_port = port;
336}
337
338/* Is this a wildcard address? */
339static int sctp_v4_is_any(const union sctp_addr *addr)
340{
341 return INADDR_ANY == addr->v4.sin_addr.s_addr;
342}
343
344/* This function checks if the address is a valid address to be used for
345 * SCTP binding.
346 *
347 * Output:
348 * Return 0 - If the address is a non-unicast or an illegal address.
349 * Return 1 - If the address is a unicast.
350 */
Vlad Yasevich5636bef2006-06-17 22:55:35 -0700351static int sctp_v4_addr_valid(union sctp_addr *addr,
352 struct sctp_sock *sp,
353 const struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354{
355 /* Is this a non-unicast address or a unusable SCTP address? */
356 if (IS_IPV4_UNUSABLE_ADDRESS(&addr->v4.sin_addr.s_addr))
357 return 0;
358
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900359 /* Is this a broadcast address? */
360 if (skb && ((struct rtable *)skb->dst)->rt_flags & RTCF_BROADCAST)
361 return 0;
Vlad Yasevich5636bef2006-06-17 22:55:35 -0700362
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 return 1;
364}
365
366/* Should this be available for binding? */
367static int sctp_v4_available(union sctp_addr *addr, struct sctp_sock *sp)
368{
369 int ret = inet_addr_type(addr->v4.sin_addr.s_addr);
370
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
Neil Hormancdac4e02005-06-13 15:12:33 -0700372 if (addr->v4.sin_addr.s_addr != INADDR_ANY &&
373 ret != RTN_LOCAL &&
374 !sp->inet.freebind &&
375 !sysctl_ip_nonlocal_bind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 return 0;
Neil Hormancdac4e02005-06-13 15:12:33 -0700377
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 return 1;
379}
380
381/* Checking the loopback, private and other address scopes as defined in
382 * RFC 1918. The IPv4 scoping is based on the draft for SCTP IPv4
383 * scoping <draft-stewart-tsvwg-sctp-ipv4-00.txt>.
384 *
385 * Level 0 - unusable SCTP addresses
386 * Level 1 - loopback address
387 * Level 2 - link-local addresses
388 * Level 3 - private addresses.
389 * Level 4 - global addresses
390 * For INIT and INIT-ACK address list, let L be the level of
391 * of requested destination address, sender and receiver
392 * SHOULD include all of its addresses with level greater
393 * than or equal to L.
394 */
395static sctp_scope_t sctp_v4_scope(union sctp_addr *addr)
396{
397 sctp_scope_t retval;
398
399 /* Should IPv4 scoping be a sysctl configurable option
400 * so users can turn it off (default on) for certain
401 * unconventional networking environments?
402 */
403
404 /* Check for unusable SCTP addresses. */
405 if (IS_IPV4_UNUSABLE_ADDRESS(&addr->v4.sin_addr.s_addr)) {
406 retval = SCTP_SCOPE_UNUSABLE;
407 } else if (LOOPBACK(addr->v4.sin_addr.s_addr)) {
408 retval = SCTP_SCOPE_LOOPBACK;
409 } else if (IS_IPV4_LINK_ADDRESS(&addr->v4.sin_addr.s_addr)) {
410 retval = SCTP_SCOPE_LINK;
411 } else if (IS_IPV4_PRIVATE_ADDRESS(&addr->v4.sin_addr.s_addr)) {
412 retval = SCTP_SCOPE_PRIVATE;
413 } else {
414 retval = SCTP_SCOPE_GLOBAL;
415 }
416
417 return retval;
418}
419
420/* Returns a valid dst cache entry for the given source and destination ip
421 * addresses. If an association is passed, trys to get a dst entry with a
422 * source address that matches an address in the bind address list.
423 */
424static struct dst_entry *sctp_v4_get_dst(struct sctp_association *asoc,
425 union sctp_addr *daddr,
426 union sctp_addr *saddr)
427{
428 struct rtable *rt;
429 struct flowi fl;
430 struct sctp_bind_addr *bp;
431 rwlock_t *addr_lock;
432 struct sctp_sockaddr_entry *laddr;
433 struct list_head *pos;
434 struct dst_entry *dst = NULL;
435 union sctp_addr dst_saddr;
436
437 memset(&fl, 0x0, sizeof(struct flowi));
438 fl.fl4_dst = daddr->v4.sin_addr.s_addr;
439 fl.proto = IPPROTO_SCTP;
440 if (asoc) {
441 fl.fl4_tos = RT_CONN_FLAGS(asoc->base.sk);
442 fl.oif = asoc->base.sk->sk_bound_dev_if;
443 }
444 if (saddr)
445 fl.fl4_src = saddr->v4.sin_addr.s_addr;
446
447 SCTP_DEBUG_PRINTK("%s: DST:%u.%u.%u.%u, SRC:%u.%u.%u.%u - ",
448 __FUNCTION__, NIPQUAD(fl.fl4_dst),
449 NIPQUAD(fl.fl4_src));
450
451 if (!ip_route_output_key(&rt, &fl)) {
452 dst = &rt->u.dst;
453 }
454
455 /* If there is no association or if a source address is passed, no
456 * more validation is required.
457 */
458 if (!asoc || saddr)
459 goto out;
460
461 bp = &asoc->base.bind_addr;
462 addr_lock = &asoc->base.addr_lock;
463
464 if (dst) {
465 /* Walk through the bind address list and look for a bind
466 * address that matches the source address of the returned dst.
467 */
468 sctp_read_lock(addr_lock);
469 list_for_each(pos, &bp->address_list) {
470 laddr = list_entry(pos, struct sctp_sockaddr_entry,
471 list);
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700472 if (!laddr->use_as_src)
473 continue;
Al Viro854d43a2006-11-20 17:06:24 -0800474 sctp_v4_dst_saddr(&dst_saddr, dst, htons(bp->port));
475 if (sctp_v4_cmp_addr(&dst_saddr, &laddr->a))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 goto out_unlock;
477 }
478 sctp_read_unlock(addr_lock);
479
480 /* None of the bound addresses match the source address of the
481 * dst. So release it.
482 */
483 dst_release(dst);
484 dst = NULL;
485 }
486
487 /* Walk through the bind address list and try to get a dst that
488 * matches a bind address as the source address.
489 */
490 sctp_read_lock(addr_lock);
491 list_for_each(pos, &bp->address_list) {
492 laddr = list_entry(pos, struct sctp_sockaddr_entry, list);
493
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700494 if ((laddr->use_as_src) &&
Al Viro6244be42006-11-20 17:21:44 -0800495 (AF_INET == laddr->a.sa.sa_family)) {
496 fl.fl4_src = laddr->a.v4.sin_addr.s_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 if (!ip_route_output_key(&rt, &fl)) {
498 dst = &rt->u.dst;
499 goto out_unlock;
500 }
501 }
502 }
503
504out_unlock:
505 sctp_read_unlock(addr_lock);
506out:
507 if (dst)
508 SCTP_DEBUG_PRINTK("rt_dst:%u.%u.%u.%u, rt_src:%u.%u.%u.%u\n",
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900509 NIPQUAD(rt->rt_dst), NIPQUAD(rt->rt_src));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 else
511 SCTP_DEBUG_PRINTK("NO ROUTE\n");
512
513 return dst;
514}
515
516/* For v4, the source address is cached in the route entry(dst). So no need
517 * to cache it separately and hence this is an empty routine.
518 */
519static void sctp_v4_get_saddr(struct sctp_association *asoc,
520 struct dst_entry *dst,
521 union sctp_addr *daddr,
522 union sctp_addr *saddr)
523{
524 struct rtable *rt = (struct rtable *)dst;
525
Vladislav Yasevich23ec47a2005-11-11 16:05:55 -0800526 if (!asoc)
527 return;
528
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 if (rt) {
530 saddr->v4.sin_family = AF_INET;
Al Virod3f7a542006-11-20 17:12:41 -0800531 saddr->v4.sin_port = htons(asoc->base.bind_addr.port);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900532 saddr->v4.sin_addr.s_addr = rt->rt_src;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 }
534}
535
536/* What interface did this skb arrive on? */
537static int sctp_v4_skb_iif(const struct sk_buff *skb)
538{
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900539 return ((struct rtable *)skb->dst)->rt_iif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540}
541
542/* Was this packet marked by Explicit Congestion Notification? */
543static int sctp_v4_is_ce(const struct sk_buff *skb)
544{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700545 return INET_ECN_is_ce(ip_hdr(skb)->tos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546}
547
548/* Create and initialize a new sk for the socket returned by accept(). */
549static struct sock *sctp_v4_create_accept_sk(struct sock *sk,
550 struct sctp_association *asoc)
551{
552 struct inet_sock *inet = inet_sk(sk);
553 struct inet_sock *newinet;
554 struct sock *newsk = sk_alloc(PF_INET, GFP_KERNEL, sk->sk_prot, 1);
555
556 if (!newsk)
557 goto out;
558
559 sock_init_data(NULL, newsk);
560
561 newsk->sk_type = SOCK_STREAM;
562
563 newsk->sk_no_check = sk->sk_no_check;
564 newsk->sk_reuse = sk->sk_reuse;
565 newsk->sk_shutdown = sk->sk_shutdown;
566
567 newsk->sk_destruct = inet_sock_destruct;
568 newsk->sk_family = PF_INET;
569 newsk->sk_protocol = IPPROTO_SCTP;
570 newsk->sk_backlog_rcv = sk->sk_prot->backlog_rcv;
571 sock_reset_flag(newsk, SOCK_ZAPPED);
572
573 newinet = inet_sk(newsk);
574
575 /* Initialize sk's sport, dport, rcv_saddr and daddr for
576 * getsockname() and getpeername()
577 */
578 newinet->sport = inet->sport;
579 newinet->saddr = inet->saddr;
580 newinet->rcv_saddr = inet->rcv_saddr;
581 newinet->dport = htons(asoc->peer.port);
582 newinet->daddr = asoc->peer.primary_addr.v4.sin_addr.s_addr;
583 newinet->pmtudisc = inet->pmtudisc;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900584 newinet->id = asoc->next_tsn ^ jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
586 newinet->uc_ttl = -1;
587 newinet->mc_loop = 1;
588 newinet->mc_ttl = 1;
589 newinet->mc_index = 0;
590 newinet->mc_list = NULL;
591
Arnaldo Carvalho de Meloe6848972005-08-09 19:45:38 -0700592 sk_refcnt_debug_inc(newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
594 if (newsk->sk_prot->init(newsk)) {
595 sk_common_release(newsk);
596 newsk = NULL;
597 }
598
599out:
600 return newsk;
601}
602
603/* Map address, empty for v4 family */
604static void sctp_v4_addr_v4map(struct sctp_sock *sp, union sctp_addr *addr)
605{
606 /* Empty */
607}
608
609/* Dump the v4 addr to the seq file. */
610static void sctp_v4_seq_dump_addr(struct seq_file *seq, union sctp_addr *addr)
611{
612 seq_printf(seq, "%d.%d.%d.%d ", NIPQUAD(addr->v4.sin_addr));
613}
614
Vlad Yasevich29303542007-09-16 16:02:12 -0700615/* Event handler for inet address addition/deletion events.
616 * The sctp_local_addr_list needs to be protocted by a spin lock since
617 * multiple notifiers (say IPv4 and IPv6) may be running at the same
618 * time and thus corrupt the list.
619 * The reader side is protected with RCU.
620 */
Adrian Bunk24123182006-12-20 16:08:22 -0800621static int sctp_inetaddr_event(struct notifier_block *this, unsigned long ev,
622 void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623{
Sridhar Samudrala29c7cf92006-12-13 16:26:26 -0800624 struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
Vlad Yasevich29303542007-09-16 16:02:12 -0700625 struct sctp_sockaddr_entry *addr = NULL;
626 struct sctp_sockaddr_entry *temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
Sridhar Samudrala29c7cf92006-12-13 16:26:26 -0800628 switch (ev) {
629 case NETDEV_UP:
630 addr = kmalloc(sizeof(struct sctp_sockaddr_entry), GFP_ATOMIC);
631 if (addr) {
632 addr->a.v4.sin_family = AF_INET;
633 addr->a.v4.sin_port = 0;
634 addr->a.v4.sin_addr.s_addr = ifa->ifa_local;
Vlad Yasevich29303542007-09-16 16:02:12 -0700635 addr->valid = 1;
636 spin_lock_bh(&sctp_local_addr_lock);
637 list_add_tail_rcu(&addr->list, &sctp_local_addr_list);
638 spin_unlock_bh(&sctp_local_addr_lock);
Sridhar Samudrala29c7cf92006-12-13 16:26:26 -0800639 }
640 break;
641 case NETDEV_DOWN:
Vlad Yasevich29303542007-09-16 16:02:12 -0700642 spin_lock_bh(&sctp_local_addr_lock);
643 list_for_each_entry_safe(addr, temp,
644 &sctp_local_addr_list, list) {
Sridhar Samudrala29c7cf92006-12-13 16:26:26 -0800645 if (addr->a.v4.sin_addr.s_addr == ifa->ifa_local) {
Vlad Yasevich29303542007-09-16 16:02:12 -0700646 addr->valid = 0;
647 list_del_rcu(&addr->list);
Sridhar Samudrala29c7cf92006-12-13 16:26:26 -0800648 break;
649 }
650 }
Vlad Yasevich29303542007-09-16 16:02:12 -0700651 spin_unlock_bh(&sctp_local_addr_lock);
652 if (addr && !addr->valid)
653 call_rcu(&addr->rcu, sctp_local_addr_free);
Sridhar Samudrala29c7cf92006-12-13 16:26:26 -0800654 break;
655 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
657 return NOTIFY_DONE;
658}
659
660/*
661 * Initialize the control inode/socket with a control endpoint data
662 * structure. This endpoint is reserved exclusively for the OOTB processing.
663 */
664static int sctp_ctl_sock_init(void)
665{
666 int err;
667 sa_family_t family;
668
669 if (sctp_get_pf_specific(PF_INET6))
670 family = PF_INET6;
671 else
672 family = PF_INET;
673
674 err = sock_create_kern(family, SOCK_SEQPACKET, IPPROTO_SCTP,
675 &sctp_ctl_socket);
676 if (err < 0) {
677 printk(KERN_ERR
678 "SCTP: Failed to create the SCTP control socket.\n");
679 return err;
680 }
681 sctp_ctl_socket->sk->sk_allocation = GFP_ATOMIC;
682 inet_sk(sctp_ctl_socket->sk)->uc_ttl = -1;
683
684 return 0;
685}
686
687/* Register address family specific functions. */
688int sctp_register_af(struct sctp_af *af)
689{
690 switch (af->sa_family) {
691 case AF_INET:
692 if (sctp_af_v4_specific)
693 return 0;
694 sctp_af_v4_specific = af;
695 break;
696 case AF_INET6:
697 if (sctp_af_v6_specific)
698 return 0;
699 sctp_af_v6_specific = af;
700 break;
701 default:
702 return 0;
703 }
704
705 INIT_LIST_HEAD(&af->list);
706 list_add_tail(&af->list, &sctp_address_families);
707 return 1;
708}
709
710/* Get the table of functions for manipulating a particular address
711 * family.
712 */
713struct sctp_af *sctp_get_af_specific(sa_family_t family)
714{
715 switch (family) {
716 case AF_INET:
717 return sctp_af_v4_specific;
718 case AF_INET6:
719 return sctp_af_v6_specific;
720 default:
721 return NULL;
722 }
723}
724
725/* Common code to initialize a AF_INET msg_name. */
726static void sctp_inet_msgname(char *msgname, int *addr_len)
727{
728 struct sockaddr_in *sin;
729
730 sin = (struct sockaddr_in *)msgname;
731 *addr_len = sizeof(struct sockaddr_in);
732 sin->sin_family = AF_INET;
733 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
734}
735
736/* Copy the primary address of the peer primary address as the msg_name. */
737static void sctp_inet_event_msgname(struct sctp_ulpevent *event, char *msgname,
738 int *addr_len)
739{
740 struct sockaddr_in *sin, *sinfrom;
741
742 if (msgname) {
743 struct sctp_association *asoc;
744
745 asoc = event->asoc;
746 sctp_inet_msgname(msgname, addr_len);
747 sin = (struct sockaddr_in *)msgname;
748 sinfrom = &asoc->peer.primary_addr.v4;
749 sin->sin_port = htons(asoc->peer.port);
750 sin->sin_addr.s_addr = sinfrom->sin_addr.s_addr;
751 }
752}
753
754/* Initialize and copy out a msgname from an inbound skb. */
755static void sctp_inet_skb_msgname(struct sk_buff *skb, char *msgname, int *len)
756{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 if (msgname) {
Arnaldo Carvalho de Melo2c0fd382007-03-13 13:59:32 -0300758 struct sctphdr *sh = sctp_hdr(skb);
759 struct sockaddr_in *sin = (struct sockaddr_in *)msgname;
760
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 sctp_inet_msgname(msgname, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 sin->sin_port = sh->source;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700763 sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 }
765}
766
767/* Do we support this AF? */
768static int sctp_inet_af_supported(sa_family_t family, struct sctp_sock *sp)
769{
770 /* PF_INET only supports AF_INET addresses. */
771 return (AF_INET == family);
772}
773
774/* Address matching with wildcards allowed. */
775static int sctp_inet_cmp_addr(const union sctp_addr *addr1,
776 const union sctp_addr *addr2,
777 struct sctp_sock *opt)
778{
779 /* PF_INET only supports AF_INET addresses. */
780 if (addr1->sa.sa_family != addr2->sa.sa_family)
781 return 0;
782 if (INADDR_ANY == addr1->v4.sin_addr.s_addr ||
783 INADDR_ANY == addr2->v4.sin_addr.s_addr)
784 return 1;
785 if (addr1->v4.sin_addr.s_addr == addr2->v4.sin_addr.s_addr)
786 return 1;
787
788 return 0;
789}
790
791/* Verify that provided sockaddr looks bindable. Common verification has
792 * already been taken care of.
793 */
794static int sctp_inet_bind_verify(struct sctp_sock *opt, union sctp_addr *addr)
795{
796 return sctp_v4_available(addr, opt);
797}
798
799/* Verify that sockaddr looks sendable. Common verification has already
800 * been taken care of.
801 */
802static int sctp_inet_send_verify(struct sctp_sock *opt, union sctp_addr *addr)
803{
804 return 1;
805}
806
807/* Fill in Supported Address Type information for INIT and INIT-ACK
808 * chunks. Returns number of addresses supported.
809 */
810static int sctp_inet_supported_addrs(const struct sctp_sock *opt,
Al Viro3dbe8652006-11-20 17:25:49 -0800811 __be16 *types)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812{
813 types[0] = SCTP_PARAM_IPV4_ADDRESS;
814 return 1;
815}
816
817/* Wrapper routine that calls the ip transmit routine. */
818static inline int sctp_v4_xmit(struct sk_buff *skb,
819 struct sctp_transport *transport, int ipfragok)
820{
821 SCTP_DEBUG_PRINTK("%s: skb:%p, len:%d, "
822 "src:%u.%u.%u.%u, dst:%u.%u.%u.%u\n",
823 __FUNCTION__, skb, skb->len,
824 NIPQUAD(((struct rtable *)skb->dst)->rt_src),
825 NIPQUAD(((struct rtable *)skb->dst)->rt_dst));
826
827 SCTP_INC_STATS(SCTP_MIB_OUTSCTPPACKS);
David S. Millere89862f2007-01-26 01:04:55 -0800828 return ip_queue_xmit(skb, ipfragok);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829}
830
831static struct sctp_af sctp_ipv4_specific;
832
833static struct sctp_pf sctp_pf_inet = {
834 .event_msgname = sctp_inet_event_msgname,
835 .skb_msgname = sctp_inet_skb_msgname,
836 .af_supported = sctp_inet_af_supported,
837 .cmp_addr = sctp_inet_cmp_addr,
838 .bind_verify = sctp_inet_bind_verify,
839 .send_verify = sctp_inet_send_verify,
840 .supported_addrs = sctp_inet_supported_addrs,
841 .create_accept_sk = sctp_v4_create_accept_sk,
842 .addr_v4map = sctp_v4_addr_v4map,
843 .af = &sctp_ipv4_specific,
844};
845
846/* Notifier for inetaddr addition/deletion events. */
847static struct notifier_block sctp_inetaddr_notifier = {
848 .notifier_call = sctp_inetaddr_event,
849};
850
851/* Socket operations. */
Eric Dumazet90ddc4f2005-12-22 12:49:22 -0800852static const struct proto_ops inet_seqpacket_ops = {
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800853 .family = PF_INET,
854 .owner = THIS_MODULE,
855 .release = inet_release, /* Needs to be wrapped... */
856 .bind = inet_bind,
857 .connect = inet_dgram_connect,
858 .socketpair = sock_no_socketpair,
859 .accept = inet_accept,
860 .getname = inet_getname, /* Semantics are different. */
861 .poll = sctp_poll,
862 .ioctl = inet_ioctl,
863 .listen = sctp_inet_listen,
864 .shutdown = inet_shutdown, /* Looks harmless. */
865 .setsockopt = sock_common_setsockopt, /* IP_SOL IP_OPTION is a problem */
866 .getsockopt = sock_common_getsockopt,
867 .sendmsg = inet_sendmsg,
868 .recvmsg = sock_common_recvmsg,
869 .mmap = sock_no_mmap,
870 .sendpage = sock_no_sendpage,
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800871#ifdef CONFIG_COMPAT
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800872 .compat_setsockopt = compat_sock_common_setsockopt,
873 .compat_getsockopt = compat_sock_common_getsockopt,
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800874#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875};
876
877/* Registration with AF_INET family. */
878static struct inet_protosw sctp_seqpacket_protosw = {
879 .type = SOCK_SEQPACKET,
880 .protocol = IPPROTO_SCTP,
881 .prot = &sctp_prot,
882 .ops = &inet_seqpacket_ops,
883 .capability = -1,
884 .no_check = 0,
885 .flags = SCTP_PROTOSW_FLAG
886};
887static struct inet_protosw sctp_stream_protosw = {
888 .type = SOCK_STREAM,
889 .protocol = IPPROTO_SCTP,
890 .prot = &sctp_prot,
891 .ops = &inet_seqpacket_ops,
892 .capability = -1,
893 .no_check = 0,
894 .flags = SCTP_PROTOSW_FLAG
895};
896
897/* Register with IP layer. */
898static struct net_protocol sctp_protocol = {
899 .handler = sctp_rcv,
900 .err_handler = sctp_v4_err,
901 .no_policy = 1,
902};
903
904/* IPv4 address related functions. */
905static struct sctp_af sctp_ipv4_specific = {
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800906 .sa_family = AF_INET,
907 .sctp_xmit = sctp_v4_xmit,
908 .setsockopt = ip_setsockopt,
909 .getsockopt = ip_getsockopt,
910 .get_dst = sctp_v4_get_dst,
911 .get_saddr = sctp_v4_get_saddr,
912 .copy_addrlist = sctp_v4_copy_addrlist,
913 .from_skb = sctp_v4_from_skb,
914 .from_sk = sctp_v4_from_sk,
915 .to_sk_saddr = sctp_v4_to_sk_saddr,
916 .to_sk_daddr = sctp_v4_to_sk_daddr,
917 .from_addr_param = sctp_v4_from_addr_param,
918 .to_addr_param = sctp_v4_to_addr_param,
919 .dst_saddr = sctp_v4_dst_saddr,
920 .cmp_addr = sctp_v4_cmp_addr,
921 .addr_valid = sctp_v4_addr_valid,
922 .inaddr_any = sctp_v4_inaddr_any,
923 .is_any = sctp_v4_is_any,
924 .available = sctp_v4_available,
925 .scope = sctp_v4_scope,
926 .skb_iif = sctp_v4_skb_iif,
927 .is_ce = sctp_v4_is_ce,
928 .seq_dump_addr = sctp_v4_seq_dump_addr,
929 .net_header_len = sizeof(struct iphdr),
930 .sockaddr_len = sizeof(struct sockaddr_in),
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800931#ifdef CONFIG_COMPAT
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800932 .compat_setsockopt = compat_ip_setsockopt,
933 .compat_getsockopt = compat_ip_getsockopt,
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800934#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935};
936
937struct sctp_pf *sctp_get_pf_specific(sa_family_t family) {
938
939 switch (family) {
940 case PF_INET:
941 return sctp_pf_inet_specific;
942 case PF_INET6:
943 return sctp_pf_inet6_specific;
944 default:
945 return NULL;
946 }
947}
948
949/* Register the PF specific function table. */
950int sctp_register_pf(struct sctp_pf *pf, sa_family_t family)
951{
952 switch (family) {
953 case PF_INET:
954 if (sctp_pf_inet_specific)
955 return 0;
956 sctp_pf_inet_specific = pf;
957 break;
958 case PF_INET6:
959 if (sctp_pf_inet6_specific)
960 return 0;
961 sctp_pf_inet6_specific = pf;
962 break;
963 default:
964 return 0;
965 }
966 return 1;
967}
968
969static int __init init_sctp_mibs(void)
970{
971 sctp_statistics[0] = alloc_percpu(struct sctp_mib);
972 if (!sctp_statistics[0])
973 return -ENOMEM;
974 sctp_statistics[1] = alloc_percpu(struct sctp_mib);
975 if (!sctp_statistics[1]) {
976 free_percpu(sctp_statistics[0]);
977 return -ENOMEM;
978 }
979 return 0;
980
981}
982
983static void cleanup_sctp_mibs(void)
984{
985 free_percpu(sctp_statistics[0]);
986 free_percpu(sctp_statistics[1]);
987}
988
989/* Initialize the universe into something sensible. */
990SCTP_STATIC __init int sctp_init(void)
991{
992 int i;
993 int status = -EINVAL;
994 unsigned long goal;
995 int order;
996
997 /* SCTP_DEBUG sanity check. */
998 if (!sctp_sanity_check())
999 goto out;
1000
Sridhar Samudrala827bf122007-05-04 13:36:30 -07001001 /* Allocate bind_bucket and chunk caches. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 status = -ENOBUFS;
1003 sctp_bucket_cachep = kmem_cache_create("sctp_bind_bucket",
1004 sizeof(struct sctp_bind_bucket),
1005 0, SLAB_HWCACHE_ALIGN,
Paul Mundt20c2df82007-07-20 10:11:58 +09001006 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 if (!sctp_bucket_cachep)
Sridhar Samudrala827bf122007-05-04 13:36:30 -07001008 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
1010 sctp_chunk_cachep = kmem_cache_create("sctp_chunk",
1011 sizeof(struct sctp_chunk),
1012 0, SLAB_HWCACHE_ALIGN,
Paul Mundt20c2df82007-07-20 10:11:58 +09001013 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 if (!sctp_chunk_cachep)
1015 goto err_chunk_cachep;
1016
1017 /* Allocate and initialise sctp mibs. */
1018 status = init_sctp_mibs();
1019 if (status)
1020 goto err_init_mibs;
1021
1022 /* Initialize proc fs directory. */
1023 status = sctp_proc_init();
1024 if (status)
1025 goto err_init_proc;
1026
1027 /* Initialize object count debugging. */
1028 sctp_dbg_objcnt_init();
1029
1030 /* Initialize the SCTP specific PF functions. */
1031 sctp_register_pf(&sctp_pf_inet, PF_INET);
1032 /*
1033 * 14. Suggested SCTP Protocol Parameter Values
1034 */
1035 /* The following protocol parameters are RECOMMENDED: */
1036 /* RTO.Initial - 3 seconds */
1037 sctp_rto_initial = SCTP_RTO_INITIAL;
1038 /* RTO.Min - 1 second */
1039 sctp_rto_min = SCTP_RTO_MIN;
1040 /* RTO.Max - 60 seconds */
1041 sctp_rto_max = SCTP_RTO_MAX;
1042 /* RTO.Alpha - 1/8 */
1043 sctp_rto_alpha = SCTP_RTO_ALPHA;
1044 /* RTO.Beta - 1/4 */
1045 sctp_rto_beta = SCTP_RTO_BETA;
1046
1047 /* Valid.Cookie.Life - 60 seconds */
Vladislav Yasevich3fd091e2006-08-22 13:29:17 -07001048 sctp_valid_cookie_life = SCTP_DEFAULT_COOKIE_LIFE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
1050 /* Whether Cookie Preservative is enabled(1) or not(0) */
1051 sctp_cookie_preserve_enable = 1;
1052
1053 /* Max.Burst - 4 */
Vlad Yasevich70331572007-03-23 11:34:36 -07001054 sctp_max_burst = SCTP_DEFAULT_MAX_BURST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
1056 /* Association.Max.Retrans - 10 attempts
1057 * Path.Max.Retrans - 5 attempts (per destination address)
1058 * Max.Init.Retransmits - 8 attempts
1059 */
1060 sctp_max_retrans_association = 10;
1061 sctp_max_retrans_path = 5;
1062 sctp_max_retrans_init = 8;
1063
Neil Horman4eb701d2005-04-28 12:02:04 -07001064 /* Sendbuffer growth - do per-socket accounting */
1065 sctp_sndbuf_policy = 0;
1066
Neil Horman049b3ff2005-11-11 16:08:24 -08001067 /* Rcvbuffer growth - do per-socket accounting */
1068 sctp_rcvbuf_policy = 0;
1069
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 /* HB.interval - 30 seconds */
Vlad Yasevich2f85a422005-06-28 13:24:23 -07001071 sctp_hb_interval = SCTP_DEFAULT_TIMEOUT_HEARTBEAT;
1072
1073 /* delayed SACK timeout */
1074 sctp_sack_timeout = SCTP_DEFAULT_TIMEOUT_SACK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
1076 /* Implementation specific variables. */
1077
1078 /* Initialize default stream count setup information. */
1079 sctp_max_instreams = SCTP_DEFAULT_INSTREAMS;
1080 sctp_max_outstreams = SCTP_DEFAULT_OUTSTREAMS;
1081
1082 /* Initialize handle used for association ids. */
1083 idr_init(&sctp_assocs_id);
1084
1085 /* Size and allocate the association hash table.
1086 * The methodology is similar to that of the tcp hash tables.
1087 */
1088 if (num_physpages >= (128 * 1024))
1089 goal = num_physpages >> (22 - PAGE_SHIFT);
1090 else
1091 goal = num_physpages >> (24 - PAGE_SHIFT);
1092
1093 for (order = 0; (1UL << order) < goal; order++)
1094 ;
1095
1096 do {
1097 sctp_assoc_hashsize = (1UL << order) * PAGE_SIZE /
1098 sizeof(struct sctp_hashbucket);
1099 if ((sctp_assoc_hashsize > (64 * 1024)) && order > 0)
1100 continue;
1101 sctp_assoc_hashtable = (struct sctp_hashbucket *)
1102 __get_free_pages(GFP_ATOMIC, order);
1103 } while (!sctp_assoc_hashtable && --order > 0);
1104 if (!sctp_assoc_hashtable) {
1105 printk(KERN_ERR "SCTP: Failed association hash alloc.\n");
1106 status = -ENOMEM;
1107 goto err_ahash_alloc;
1108 }
1109 for (i = 0; i < sctp_assoc_hashsize; i++) {
1110 rwlock_init(&sctp_assoc_hashtable[i].lock);
1111 sctp_assoc_hashtable[i].chain = NULL;
1112 }
1113
1114 /* Allocate and initialize the endpoint hash table. */
1115 sctp_ep_hashsize = 64;
1116 sctp_ep_hashtable = (struct sctp_hashbucket *)
1117 kmalloc(64 * sizeof(struct sctp_hashbucket), GFP_KERNEL);
1118 if (!sctp_ep_hashtable) {
1119 printk(KERN_ERR "SCTP: Failed endpoint_hash alloc.\n");
1120 status = -ENOMEM;
1121 goto err_ehash_alloc;
1122 }
1123 for (i = 0; i < sctp_ep_hashsize; i++) {
1124 rwlock_init(&sctp_ep_hashtable[i].lock);
1125 sctp_ep_hashtable[i].chain = NULL;
1126 }
1127
1128 /* Allocate and initialize the SCTP port hash table. */
1129 do {
1130 sctp_port_hashsize = (1UL << order) * PAGE_SIZE /
1131 sizeof(struct sctp_bind_hashbucket);
1132 if ((sctp_port_hashsize > (64 * 1024)) && order > 0)
1133 continue;
1134 sctp_port_hashtable = (struct sctp_bind_hashbucket *)
1135 __get_free_pages(GFP_ATOMIC, order);
1136 } while (!sctp_port_hashtable && --order > 0);
1137 if (!sctp_port_hashtable) {
1138 printk(KERN_ERR "SCTP: Failed bind hash alloc.");
1139 status = -ENOMEM;
1140 goto err_bhash_alloc;
1141 }
1142 for (i = 0; i < sctp_port_hashsize; i++) {
1143 spin_lock_init(&sctp_port_hashtable[i].lock);
1144 sctp_port_hashtable[i].chain = NULL;
1145 }
1146
1147 spin_lock_init(&sctp_port_alloc_lock);
1148 sctp_port_rover = sysctl_local_port_range[0] - 1;
1149
1150 printk(KERN_INFO "SCTP: Hash tables configured "
1151 "(established %d bind %d)\n",
1152 sctp_assoc_hashsize, sctp_port_hashsize);
1153
1154 /* Disable ADDIP by default. */
1155 sctp_addip_enable = 0;
1156
1157 /* Enable PR-SCTP by default. */
1158 sctp_prsctp_enable = 1;
1159
1160 sctp_sysctl_register();
1161
1162 INIT_LIST_HEAD(&sctp_address_families);
1163 sctp_register_af(&sctp_ipv4_specific);
1164
Sridhar Samudrala827bf122007-05-04 13:36:30 -07001165 status = proto_register(&sctp_prot, 1);
1166 if (status)
1167 goto err_proto_register;
1168
1169 /* Register SCTP(UDP and TCP style) with socket layer. */
1170 inet_register_protosw(&sctp_seqpacket_protosw);
1171 inet_register_protosw(&sctp_stream_protosw);
1172
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 status = sctp_v6_init();
1174 if (status)
1175 goto err_v6_init;
1176
1177 /* Initialize the control inode/socket for handling OOTB packets. */
1178 if ((status = sctp_ctl_sock_init())) {
1179 printk (KERN_ERR
1180 "SCTP: Failed to initialize the SCTP control sock.\n");
1181 goto err_ctl_sock_init;
1182 }
1183
1184 /* Initialize the local address list. */
1185 INIT_LIST_HEAD(&sctp_local_addr_list);
Vlad Yasevich29303542007-09-16 16:02:12 -07001186 spin_lock_init(&sctp_local_addr_lock);
Sridhar Samudrala29c7cf92006-12-13 16:26:26 -08001187 sctp_get_local_addr_list();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188
1189 /* Register notifier for inet address additions/deletions. */
1190 register_inetaddr_notifier(&sctp_inetaddr_notifier);
1191
Sridhar Samudrala827bf122007-05-04 13:36:30 -07001192 /* Register SCTP with inet layer. */
1193 if (inet_add_protocol(&sctp_protocol, IPPROTO_SCTP) < 0) {
1194 status = -EAGAIN;
1195 goto err_add_protocol;
1196 }
1197
1198 /* Register SCTP with inet6 layer. */
1199 status = sctp_v6_add_protocol();
1200 if (status)
1201 goto err_v6_add_protocol;
1202
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 __unsafe(THIS_MODULE);
1204 status = 0;
1205out:
1206 return status;
Sridhar Samudrala827bf122007-05-04 13:36:30 -07001207err_v6_add_protocol:
1208 inet_del_protocol(&sctp_protocol, IPPROTO_SCTP);
1209 unregister_inetaddr_notifier(&sctp_inetaddr_notifier);
1210err_add_protocol:
1211 sctp_free_local_addr_list();
1212 sock_release(sctp_ctl_socket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213err_ctl_sock_init:
1214 sctp_v6_exit();
1215err_v6_init:
Sridhar Samudrala827bf122007-05-04 13:36:30 -07001216 inet_unregister_protosw(&sctp_stream_protosw);
1217 inet_unregister_protosw(&sctp_seqpacket_protosw);
1218 proto_unregister(&sctp_prot);
1219err_proto_register:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 sctp_sysctl_unregister();
1221 list_del(&sctp_ipv4_specific.list);
1222 free_pages((unsigned long)sctp_port_hashtable,
1223 get_order(sctp_port_hashsize *
1224 sizeof(struct sctp_bind_hashbucket)));
1225err_bhash_alloc:
1226 kfree(sctp_ep_hashtable);
1227err_ehash_alloc:
1228 free_pages((unsigned long)sctp_assoc_hashtable,
1229 get_order(sctp_assoc_hashsize *
1230 sizeof(struct sctp_hashbucket)));
1231err_ahash_alloc:
1232 sctp_dbg_objcnt_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 sctp_proc_exit();
Sridhar Samudrala827bf122007-05-04 13:36:30 -07001234err_init_proc:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 cleanup_sctp_mibs();
1236err_init_mibs:
1237 kmem_cache_destroy(sctp_chunk_cachep);
1238err_chunk_cachep:
1239 kmem_cache_destroy(sctp_bucket_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 goto out;
1241}
1242
1243/* Exit handler for the SCTP protocol. */
1244SCTP_STATIC __exit void sctp_exit(void)
1245{
1246 /* BUG. This should probably do something useful like clean
1247 * up all the remaining associations and all that memory.
1248 */
1249
Sridhar Samudrala827bf122007-05-04 13:36:30 -07001250 /* Unregister with inet6/inet layers. */
1251 sctp_v6_del_protocol();
1252 inet_del_protocol(&sctp_protocol, IPPROTO_SCTP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
Vlad Yasevich29303542007-09-16 16:02:12 -07001254 /* Unregister notifier for inet address additions/deletions. */
1255 unregister_inetaddr_notifier(&sctp_inetaddr_notifier);
1256
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 /* Free the local address list. */
1258 sctp_free_local_addr_list();
1259
1260 /* Free the control endpoint. */
1261 sock_release(sctp_ctl_socket);
1262
Sridhar Samudrala827bf122007-05-04 13:36:30 -07001263 /* Cleanup v6 initializations. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 sctp_v6_exit();
Sridhar Samudrala827bf122007-05-04 13:36:30 -07001265
1266 /* Unregister with socket layer. */
1267 inet_unregister_protosw(&sctp_stream_protosw);
1268 inet_unregister_protosw(&sctp_seqpacket_protosw);
1269
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 sctp_sysctl_unregister();
1271 list_del(&sctp_ipv4_specific.list);
1272
1273 free_pages((unsigned long)sctp_assoc_hashtable,
1274 get_order(sctp_assoc_hashsize *
1275 sizeof(struct sctp_hashbucket)));
1276 kfree(sctp_ep_hashtable);
1277 free_pages((unsigned long)sctp_port_hashtable,
1278 get_order(sctp_port_hashsize *
1279 sizeof(struct sctp_bind_hashbucket)));
1280
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 sctp_dbg_objcnt_exit();
1282 sctp_proc_exit();
1283 cleanup_sctp_mibs();
1284
Sridhar Samudrala827bf122007-05-04 13:36:30 -07001285 kmem_cache_destroy(sctp_chunk_cachep);
1286 kmem_cache_destroy(sctp_bucket_cachep);
1287
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 proto_unregister(&sctp_prot);
1289}
1290
1291module_init(sctp_init);
1292module_exit(sctp_exit);
1293
Arnaldo Carvalho de Melobb97d312005-08-09 20:19:14 -07001294/*
1295 * __stringify doesn't likes enums, so use IPPROTO_SCTP value (132) directly.
1296 */
1297MODULE_ALIAS("net-pf-" __stringify(PF_INET) "-proto-132");
Sridhar Samudrala882a3822006-12-13 16:33:35 -08001298MODULE_ALIAS("net-pf-" __stringify(PF_INET6) "-proto-132");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299MODULE_AUTHOR("Linux Kernel SCTP developers <lksctp-developers@lists.sourceforge.net>");
1300MODULE_DESCRIPTION("Support for the SCTP protocol (RFC2960)");
1301MODULE_LICENSE("GPL");