Driver core: Fix cleanup when failing device_add().
[wrt350n-kernel.git] / net / sctp / protocol.c
blob688546dccd828f0821736a301535049cc3d8ca04
1 /* SCTP kernel implementation
2 * (C) Copyright IBM Corp. 2001, 2004
3 * Copyright (c) 1999-2000 Cisco, Inc.
4 * Copyright (c) 1999-2001 Motorola, Inc.
5 * Copyright (c) 2001 Intel Corp.
6 * Copyright (c) 2001 Nokia, Inc.
7 * Copyright (c) 2001 La Monte H.P. Yarroll
9 * This file is part of the SCTP kernel implementation
11 * Initialization/cleanup for SCTP protocol support.
13 * This SCTP implementation is free software;
14 * you can redistribute it and/or modify it under the terms of
15 * the GNU General Public License as published by
16 * the Free Software Foundation; either version 2, or (at your option)
17 * any later version.
19 * This SCTP implementation is distributed in the hope that it
20 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
21 * ************************
22 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
23 * See the GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with GNU CC; see the file COPYING. If not, write to
27 * the Free Software Foundation, 59 Temple Place - Suite 330,
28 * Boston, MA 02111-1307, USA.
30 * Please send any bug reports or fixes you make to the
31 * email address(es):
32 * lksctp developers <lksctp-developers@lists.sourceforge.net>
34 * Or submit a bug report through the following website:
35 * http://www.sf.net/projects/lksctp
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>
45 * Any bugs reported given to us we will try to fix... any fixes shared will
46 * be incorporated into the next SCTP release.
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 <linux/bootmem.h>
55 #include <net/net_namespace.h>
56 #include <net/protocol.h>
57 #include <net/ip.h>
58 #include <net/ipv6.h>
59 #include <net/route.h>
60 #include <net/sctp/sctp.h>
61 #include <net/addrconf.h>
62 #include <net/inet_common.h>
63 #include <net/inet_ecn.h>
65 /* Global data structures. */
66 struct sctp_globals sctp_globals __read_mostly;
67 struct proc_dir_entry *proc_net_sctp;
68 DEFINE_SNMP_STAT(struct sctp_mib, sctp_statistics) __read_mostly;
70 struct idr sctp_assocs_id;
71 DEFINE_SPINLOCK(sctp_assocs_id_lock);
73 /* This is the global socket data structure used for responding to
74 * the Out-of-the-blue (OOTB) packets. A control sock will be created
75 * for this socket at the initialization time.
77 static struct socket *sctp_ctl_socket;
79 static struct sctp_pf *sctp_pf_inet6_specific;
80 static struct sctp_pf *sctp_pf_inet_specific;
81 static struct sctp_af *sctp_af_v4_specific;
82 static struct sctp_af *sctp_af_v6_specific;
84 struct kmem_cache *sctp_chunk_cachep __read_mostly;
85 struct kmem_cache *sctp_bucket_cachep __read_mostly;
87 int sysctl_sctp_mem[3];
88 int sysctl_sctp_rmem[3];
89 int sysctl_sctp_wmem[3];
91 /* Return the address of the control sock. */
92 struct sock *sctp_get_ctl_sock(void)
94 return sctp_ctl_socket->sk;
97 /* Set up the proc fs entry for the SCTP protocol. */
98 static __init int sctp_proc_init(void)
100 if (!proc_net_sctp) {
101 struct proc_dir_entry *ent;
102 ent = proc_mkdir("sctp", init_net.proc_net);
103 if (ent) {
104 ent->owner = THIS_MODULE;
105 proc_net_sctp = ent;
106 } else
107 goto out_nomem;
110 if (sctp_snmp_proc_init())
111 goto out_nomem;
112 if (sctp_eps_proc_init())
113 goto out_nomem;
114 if (sctp_assocs_proc_init())
115 goto out_nomem;
117 return 0;
119 out_nomem:
120 return -ENOMEM;
123 /* Clean up the proc fs entry for the SCTP protocol.
124 * Note: Do not make this __exit as it is used in the init error
125 * path.
127 static void sctp_proc_exit(void)
129 sctp_snmp_proc_exit();
130 sctp_eps_proc_exit();
131 sctp_assocs_proc_exit();
133 if (proc_net_sctp) {
134 proc_net_sctp = NULL;
135 remove_proc_entry("sctp", init_net.proc_net);
139 /* Private helper to extract ipv4 address and stash them in
140 * the protocol structure.
142 static void sctp_v4_copy_addrlist(struct list_head *addrlist,
143 struct net_device *dev)
145 struct in_device *in_dev;
146 struct in_ifaddr *ifa;
147 struct sctp_sockaddr_entry *addr;
149 rcu_read_lock();
150 if ((in_dev = __in_dev_get_rcu(dev)) == NULL) {
151 rcu_read_unlock();
152 return;
155 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
156 /* Add the address to the local list. */
157 addr = t_new(struct sctp_sockaddr_entry, GFP_ATOMIC);
158 if (addr) {
159 addr->a.v4.sin_family = AF_INET;
160 addr->a.v4.sin_port = 0;
161 addr->a.v4.sin_addr.s_addr = ifa->ifa_local;
162 addr->valid = 1;
163 INIT_LIST_HEAD(&addr->list);
164 INIT_RCU_HEAD(&addr->rcu);
165 list_add_tail(&addr->list, addrlist);
169 rcu_read_unlock();
172 /* Extract our IP addresses from the system and stash them in the
173 * protocol structure.
175 static void sctp_get_local_addr_list(void)
177 struct net_device *dev;
178 struct list_head *pos;
179 struct sctp_af *af;
181 read_lock(&dev_base_lock);
182 for_each_netdev(&init_net, dev) {
183 __list_for_each(pos, &sctp_address_families) {
184 af = list_entry(pos, struct sctp_af, list);
185 af->copy_addrlist(&sctp_local_addr_list, dev);
188 read_unlock(&dev_base_lock);
191 /* Free the existing local addresses. */
192 static void sctp_free_local_addr_list(void)
194 struct sctp_sockaddr_entry *addr;
195 struct list_head *pos, *temp;
197 list_for_each_safe(pos, temp, &sctp_local_addr_list) {
198 addr = list_entry(pos, struct sctp_sockaddr_entry, list);
199 list_del(pos);
200 kfree(addr);
204 void sctp_local_addr_free(struct rcu_head *head)
206 struct sctp_sockaddr_entry *e = container_of(head,
207 struct sctp_sockaddr_entry, rcu);
208 kfree(e);
211 /* Copy the local addresses which are valid for 'scope' into 'bp'. */
212 int sctp_copy_local_addr_list(struct sctp_bind_addr *bp, sctp_scope_t scope,
213 gfp_t gfp, int copy_flags)
215 struct sctp_sockaddr_entry *addr;
216 int error = 0;
218 rcu_read_lock();
219 list_for_each_entry_rcu(addr, &sctp_local_addr_list, list) {
220 if (!addr->valid)
221 continue;
222 if (sctp_in_scope(&addr->a, scope)) {
223 /* Now that the address is in scope, check to see if
224 * the address type is really supported by the local
225 * sock as well as the remote peer.
227 if ((((AF_INET == addr->a.sa.sa_family) &&
228 (copy_flags & SCTP_ADDR4_PEERSUPP))) ||
229 (((AF_INET6 == addr->a.sa.sa_family) &&
230 (copy_flags & SCTP_ADDR6_ALLOWED) &&
231 (copy_flags & SCTP_ADDR6_PEERSUPP)))) {
232 error = sctp_add_bind_addr(bp, &addr->a,
233 SCTP_ADDR_SRC, GFP_ATOMIC);
234 if (error)
235 goto end_copy;
240 end_copy:
241 rcu_read_unlock();
242 return error;
245 /* Initialize a sctp_addr from in incoming skb. */
246 static void sctp_v4_from_skb(union sctp_addr *addr, struct sk_buff *skb,
247 int is_saddr)
249 void *from;
250 __be16 *port;
251 struct sctphdr *sh;
253 port = &addr->v4.sin_port;
254 addr->v4.sin_family = AF_INET;
256 sh = sctp_hdr(skb);
257 if (is_saddr) {
258 *port = sh->source;
259 from = &ip_hdr(skb)->saddr;
260 } else {
261 *port = sh->dest;
262 from = &ip_hdr(skb)->daddr;
264 memcpy(&addr->v4.sin_addr.s_addr, from, sizeof(struct in_addr));
267 /* Initialize an sctp_addr from a socket. */
268 static void sctp_v4_from_sk(union sctp_addr *addr, struct sock *sk)
270 addr->v4.sin_family = AF_INET;
271 addr->v4.sin_port = 0;
272 addr->v4.sin_addr.s_addr = inet_sk(sk)->rcv_saddr;
275 /* Initialize sk->sk_rcv_saddr from sctp_addr. */
276 static void sctp_v4_to_sk_saddr(union sctp_addr *addr, struct sock *sk)
278 inet_sk(sk)->rcv_saddr = addr->v4.sin_addr.s_addr;
281 /* Initialize sk->sk_daddr from sctp_addr. */
282 static void sctp_v4_to_sk_daddr(union sctp_addr *addr, struct sock *sk)
284 inet_sk(sk)->daddr = addr->v4.sin_addr.s_addr;
287 /* Initialize a sctp_addr from an address parameter. */
288 static void sctp_v4_from_addr_param(union sctp_addr *addr,
289 union sctp_addr_param *param,
290 __be16 port, int iif)
292 addr->v4.sin_family = AF_INET;
293 addr->v4.sin_port = port;
294 addr->v4.sin_addr.s_addr = param->v4.addr.s_addr;
297 /* Initialize an address parameter from a sctp_addr and return the length
298 * of the address parameter.
300 static int sctp_v4_to_addr_param(const union sctp_addr *addr,
301 union sctp_addr_param *param)
303 int length = sizeof(sctp_ipv4addr_param_t);
305 param->v4.param_hdr.type = SCTP_PARAM_IPV4_ADDRESS;
306 param->v4.param_hdr.length = htons(length);
307 param->v4.addr.s_addr = addr->v4.sin_addr.s_addr;
309 return length;
312 /* Initialize a sctp_addr from a dst_entry. */
313 static void sctp_v4_dst_saddr(union sctp_addr *saddr, struct dst_entry *dst,
314 __be16 port)
316 struct rtable *rt = (struct rtable *)dst;
317 saddr->v4.sin_family = AF_INET;
318 saddr->v4.sin_port = port;
319 saddr->v4.sin_addr.s_addr = rt->rt_src;
322 /* Compare two addresses exactly. */
323 static int sctp_v4_cmp_addr(const union sctp_addr *addr1,
324 const union sctp_addr *addr2)
326 if (addr1->sa.sa_family != addr2->sa.sa_family)
327 return 0;
328 if (addr1->v4.sin_port != addr2->v4.sin_port)
329 return 0;
330 if (addr1->v4.sin_addr.s_addr != addr2->v4.sin_addr.s_addr)
331 return 0;
333 return 1;
336 /* Initialize addr struct to INADDR_ANY. */
337 static void sctp_v4_inaddr_any(union sctp_addr *addr, __be16 port)
339 addr->v4.sin_family = AF_INET;
340 addr->v4.sin_addr.s_addr = INADDR_ANY;
341 addr->v4.sin_port = port;
344 /* Is this a wildcard address? */
345 static int sctp_v4_is_any(const union sctp_addr *addr)
347 return INADDR_ANY == addr->v4.sin_addr.s_addr;
350 /* This function checks if the address is a valid address to be used for
351 * SCTP binding.
353 * Output:
354 * Return 0 - If the address is a non-unicast or an illegal address.
355 * Return 1 - If the address is a unicast.
357 static int sctp_v4_addr_valid(union sctp_addr *addr,
358 struct sctp_sock *sp,
359 const struct sk_buff *skb)
361 /* Is this a non-unicast address or a unusable SCTP address? */
362 if (IS_IPV4_UNUSABLE_ADDRESS(addr->v4.sin_addr.s_addr))
363 return 0;
365 /* Is this a broadcast address? */
366 if (skb && ((struct rtable *)skb->dst)->rt_flags & RTCF_BROADCAST)
367 return 0;
369 return 1;
372 /* Should this be available for binding? */
373 static int sctp_v4_available(union sctp_addr *addr, struct sctp_sock *sp)
375 int ret = inet_addr_type(&init_net, addr->v4.sin_addr.s_addr);
378 if (addr->v4.sin_addr.s_addr != INADDR_ANY &&
379 ret != RTN_LOCAL &&
380 !sp->inet.freebind &&
381 !sysctl_ip_nonlocal_bind)
382 return 0;
384 return 1;
387 /* Checking the loopback, private and other address scopes as defined in
388 * RFC 1918. The IPv4 scoping is based on the draft for SCTP IPv4
389 * scoping <draft-stewart-tsvwg-sctp-ipv4-00.txt>.
391 * Level 0 - unusable SCTP addresses
392 * Level 1 - loopback address
393 * Level 2 - link-local addresses
394 * Level 3 - private addresses.
395 * Level 4 - global addresses
396 * For INIT and INIT-ACK address list, let L be the level of
397 * of requested destination address, sender and receiver
398 * SHOULD include all of its addresses with level greater
399 * than or equal to L.
401 static sctp_scope_t sctp_v4_scope(union sctp_addr *addr)
403 sctp_scope_t retval;
405 /* Should IPv4 scoping be a sysctl configurable option
406 * so users can turn it off (default on) for certain
407 * unconventional networking environments?
410 /* Check for unusable SCTP addresses. */
411 if (IS_IPV4_UNUSABLE_ADDRESS(addr->v4.sin_addr.s_addr)) {
412 retval = SCTP_SCOPE_UNUSABLE;
413 } else if (ipv4_is_loopback(addr->v4.sin_addr.s_addr)) {
414 retval = SCTP_SCOPE_LOOPBACK;
415 } else if (ipv4_is_linklocal_169(addr->v4.sin_addr.s_addr)) {
416 retval = SCTP_SCOPE_LINK;
417 } else if (ipv4_is_private_10(addr->v4.sin_addr.s_addr) ||
418 ipv4_is_private_172(addr->v4.sin_addr.s_addr) ||
419 ipv4_is_private_192(addr->v4.sin_addr.s_addr)) {
420 retval = SCTP_SCOPE_PRIVATE;
421 } else {
422 retval = SCTP_SCOPE_GLOBAL;
425 return retval;
428 /* Returns a valid dst cache entry for the given source and destination ip
429 * addresses. If an association is passed, trys to get a dst entry with a
430 * source address that matches an address in the bind address list.
432 static struct dst_entry *sctp_v4_get_dst(struct sctp_association *asoc,
433 union sctp_addr *daddr,
434 union sctp_addr *saddr)
436 struct rtable *rt;
437 struct flowi fl;
438 struct sctp_bind_addr *bp;
439 struct sctp_sockaddr_entry *laddr;
440 struct dst_entry *dst = NULL;
441 union sctp_addr dst_saddr;
443 memset(&fl, 0x0, sizeof(struct flowi));
444 fl.fl4_dst = daddr->v4.sin_addr.s_addr;
445 fl.proto = IPPROTO_SCTP;
446 if (asoc) {
447 fl.fl4_tos = RT_CONN_FLAGS(asoc->base.sk);
448 fl.oif = asoc->base.sk->sk_bound_dev_if;
450 if (saddr)
451 fl.fl4_src = saddr->v4.sin_addr.s_addr;
453 SCTP_DEBUG_PRINTK("%s: DST:%u.%u.%u.%u, SRC:%u.%u.%u.%u - ",
454 __FUNCTION__, NIPQUAD(fl.fl4_dst),
455 NIPQUAD(fl.fl4_src));
457 if (!ip_route_output_key(&init_net, &rt, &fl)) {
458 dst = &rt->u.dst;
461 /* If there is no association or if a source address is passed, no
462 * more validation is required.
464 if (!asoc || saddr)
465 goto out;
467 bp = &asoc->base.bind_addr;
469 if (dst) {
470 /* Walk through the bind address list and look for a bind
471 * address that matches the source address of the returned dst.
473 rcu_read_lock();
474 list_for_each_entry_rcu(laddr, &bp->address_list, list) {
475 if (!laddr->valid || (laddr->state != SCTP_ADDR_SRC))
476 continue;
477 sctp_v4_dst_saddr(&dst_saddr, dst, htons(bp->port));
478 if (sctp_v4_cmp_addr(&dst_saddr, &laddr->a))
479 goto out_unlock;
481 rcu_read_unlock();
483 /* None of the bound addresses match the source address of the
484 * dst. So release it.
486 dst_release(dst);
487 dst = NULL;
490 /* Walk through the bind address list and try to get a dst that
491 * matches a bind address as the source address.
493 rcu_read_lock();
494 list_for_each_entry_rcu(laddr, &bp->address_list, list) {
495 if (!laddr->valid)
496 continue;
497 if ((laddr->state == SCTP_ADDR_SRC) &&
498 (AF_INET == laddr->a.sa.sa_family)) {
499 fl.fl4_src = laddr->a.v4.sin_addr.s_addr;
500 if (!ip_route_output_key(&init_net, &rt, &fl)) {
501 dst = &rt->u.dst;
502 goto out_unlock;
507 out_unlock:
508 rcu_read_unlock();
509 out:
510 if (dst)
511 SCTP_DEBUG_PRINTK("rt_dst:%u.%u.%u.%u, rt_src:%u.%u.%u.%u\n",
512 NIPQUAD(rt->rt_dst), NIPQUAD(rt->rt_src));
513 else
514 SCTP_DEBUG_PRINTK("NO ROUTE\n");
516 return dst;
519 /* For v4, the source address is cached in the route entry(dst). So no need
520 * to cache it separately and hence this is an empty routine.
522 static void sctp_v4_get_saddr(struct sctp_association *asoc,
523 struct dst_entry *dst,
524 union sctp_addr *daddr,
525 union sctp_addr *saddr)
527 struct rtable *rt = (struct rtable *)dst;
529 if (!asoc)
530 return;
532 if (rt) {
533 saddr->v4.sin_family = AF_INET;
534 saddr->v4.sin_port = htons(asoc->base.bind_addr.port);
535 saddr->v4.sin_addr.s_addr = rt->rt_src;
539 /* What interface did this skb arrive on? */
540 static int sctp_v4_skb_iif(const struct sk_buff *skb)
542 return ((struct rtable *)skb->dst)->rt_iif;
545 /* Was this packet marked by Explicit Congestion Notification? */
546 static int sctp_v4_is_ce(const struct sk_buff *skb)
548 return INET_ECN_is_ce(ip_hdr(skb)->tos);
551 /* Create and initialize a new sk for the socket returned by accept(). */
552 static struct sock *sctp_v4_create_accept_sk(struct sock *sk,
553 struct sctp_association *asoc)
555 struct inet_sock *inet = inet_sk(sk);
556 struct inet_sock *newinet;
557 struct sock *newsk = sk_alloc(sk->sk_net, PF_INET, GFP_KERNEL,
558 sk->sk_prot);
560 if (!newsk)
561 goto out;
563 sock_init_data(NULL, newsk);
565 newsk->sk_type = SOCK_STREAM;
567 newsk->sk_no_check = sk->sk_no_check;
568 newsk->sk_reuse = sk->sk_reuse;
569 newsk->sk_shutdown = sk->sk_shutdown;
571 newsk->sk_destruct = inet_sock_destruct;
572 newsk->sk_family = PF_INET;
573 newsk->sk_protocol = IPPROTO_SCTP;
574 newsk->sk_backlog_rcv = sk->sk_prot->backlog_rcv;
575 sock_reset_flag(newsk, SOCK_ZAPPED);
577 newinet = inet_sk(newsk);
579 /* Initialize sk's sport, dport, rcv_saddr and daddr for
580 * getsockname() and getpeername()
582 newinet->sport = inet->sport;
583 newinet->saddr = inet->saddr;
584 newinet->rcv_saddr = inet->rcv_saddr;
585 newinet->dport = htons(asoc->peer.port);
586 newinet->daddr = asoc->peer.primary_addr.v4.sin_addr.s_addr;
587 newinet->pmtudisc = inet->pmtudisc;
588 newinet->id = asoc->next_tsn ^ jiffies;
590 newinet->uc_ttl = -1;
591 newinet->mc_loop = 1;
592 newinet->mc_ttl = 1;
593 newinet->mc_index = 0;
594 newinet->mc_list = NULL;
596 sk_refcnt_debug_inc(newsk);
598 if (newsk->sk_prot->init(newsk)) {
599 sk_common_release(newsk);
600 newsk = NULL;
603 out:
604 return newsk;
607 /* Map address, empty for v4 family */
608 static void sctp_v4_addr_v4map(struct sctp_sock *sp, union sctp_addr *addr)
610 /* Empty */
613 /* Dump the v4 addr to the seq file. */
614 static void sctp_v4_seq_dump_addr(struct seq_file *seq, union sctp_addr *addr)
616 seq_printf(seq, "%d.%d.%d.%d ", NIPQUAD(addr->v4.sin_addr));
619 /* Event handler for inet address addition/deletion events.
620 * The sctp_local_addr_list needs to be protocted by a spin lock since
621 * multiple notifiers (say IPv4 and IPv6) may be running at the same
622 * time and thus corrupt the list.
623 * The reader side is protected with RCU.
625 static int sctp_inetaddr_event(struct notifier_block *this, unsigned long ev,
626 void *ptr)
628 struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
629 struct sctp_sockaddr_entry *addr = NULL;
630 struct sctp_sockaddr_entry *temp;
632 switch (ev) {
633 case NETDEV_UP:
634 addr = kmalloc(sizeof(struct sctp_sockaddr_entry), GFP_ATOMIC);
635 if (addr) {
636 addr->a.v4.sin_family = AF_INET;
637 addr->a.v4.sin_port = 0;
638 addr->a.v4.sin_addr.s_addr = ifa->ifa_local;
639 addr->valid = 1;
640 spin_lock_bh(&sctp_local_addr_lock);
641 list_add_tail_rcu(&addr->list, &sctp_local_addr_list);
642 spin_unlock_bh(&sctp_local_addr_lock);
644 break;
645 case NETDEV_DOWN:
646 spin_lock_bh(&sctp_local_addr_lock);
647 list_for_each_entry_safe(addr, temp,
648 &sctp_local_addr_list, list) {
649 if (addr->a.v4.sin_addr.s_addr == ifa->ifa_local) {
650 addr->valid = 0;
651 list_del_rcu(&addr->list);
652 break;
655 spin_unlock_bh(&sctp_local_addr_lock);
656 if (addr && !addr->valid)
657 call_rcu(&addr->rcu, sctp_local_addr_free);
658 break;
661 return NOTIFY_DONE;
665 * Initialize the control inode/socket with a control endpoint data
666 * structure. This endpoint is reserved exclusively for the OOTB processing.
668 static int sctp_ctl_sock_init(void)
670 int err;
671 sa_family_t family;
673 if (sctp_get_pf_specific(PF_INET6))
674 family = PF_INET6;
675 else
676 family = PF_INET;
678 err = sock_create_kern(family, SOCK_SEQPACKET, IPPROTO_SCTP,
679 &sctp_ctl_socket);
680 if (err < 0) {
681 printk(KERN_ERR
682 "SCTP: Failed to create the SCTP control socket.\n");
683 return err;
685 sctp_ctl_socket->sk->sk_allocation = GFP_ATOMIC;
686 inet_sk(sctp_ctl_socket->sk)->uc_ttl = -1;
688 return 0;
691 /* Register address family specific functions. */
692 int sctp_register_af(struct sctp_af *af)
694 switch (af->sa_family) {
695 case AF_INET:
696 if (sctp_af_v4_specific)
697 return 0;
698 sctp_af_v4_specific = af;
699 break;
700 case AF_INET6:
701 if (sctp_af_v6_specific)
702 return 0;
703 sctp_af_v6_specific = af;
704 break;
705 default:
706 return 0;
709 INIT_LIST_HEAD(&af->list);
710 list_add_tail(&af->list, &sctp_address_families);
711 return 1;
714 /* Get the table of functions for manipulating a particular address
715 * family.
717 struct sctp_af *sctp_get_af_specific(sa_family_t family)
719 switch (family) {
720 case AF_INET:
721 return sctp_af_v4_specific;
722 case AF_INET6:
723 return sctp_af_v6_specific;
724 default:
725 return NULL;
729 /* Common code to initialize a AF_INET msg_name. */
730 static void sctp_inet_msgname(char *msgname, int *addr_len)
732 struct sockaddr_in *sin;
734 sin = (struct sockaddr_in *)msgname;
735 *addr_len = sizeof(struct sockaddr_in);
736 sin->sin_family = AF_INET;
737 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
740 /* Copy the primary address of the peer primary address as the msg_name. */
741 static void sctp_inet_event_msgname(struct sctp_ulpevent *event, char *msgname,
742 int *addr_len)
744 struct sockaddr_in *sin, *sinfrom;
746 if (msgname) {
747 struct sctp_association *asoc;
749 asoc = event->asoc;
750 sctp_inet_msgname(msgname, addr_len);
751 sin = (struct sockaddr_in *)msgname;
752 sinfrom = &asoc->peer.primary_addr.v4;
753 sin->sin_port = htons(asoc->peer.port);
754 sin->sin_addr.s_addr = sinfrom->sin_addr.s_addr;
758 /* Initialize and copy out a msgname from an inbound skb. */
759 static void sctp_inet_skb_msgname(struct sk_buff *skb, char *msgname, int *len)
761 if (msgname) {
762 struct sctphdr *sh = sctp_hdr(skb);
763 struct sockaddr_in *sin = (struct sockaddr_in *)msgname;
765 sctp_inet_msgname(msgname, len);
766 sin->sin_port = sh->source;
767 sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
771 /* Do we support this AF? */
772 static int sctp_inet_af_supported(sa_family_t family, struct sctp_sock *sp)
774 /* PF_INET only supports AF_INET addresses. */
775 return (AF_INET == family);
778 /* Address matching with wildcards allowed. */
779 static int sctp_inet_cmp_addr(const union sctp_addr *addr1,
780 const union sctp_addr *addr2,
781 struct sctp_sock *opt)
783 /* PF_INET only supports AF_INET addresses. */
784 if (addr1->sa.sa_family != addr2->sa.sa_family)
785 return 0;
786 if (INADDR_ANY == addr1->v4.sin_addr.s_addr ||
787 INADDR_ANY == addr2->v4.sin_addr.s_addr)
788 return 1;
789 if (addr1->v4.sin_addr.s_addr == addr2->v4.sin_addr.s_addr)
790 return 1;
792 return 0;
795 /* Verify that provided sockaddr looks bindable. Common verification has
796 * already been taken care of.
798 static int sctp_inet_bind_verify(struct sctp_sock *opt, union sctp_addr *addr)
800 return sctp_v4_available(addr, opt);
803 /* Verify that sockaddr looks sendable. Common verification has already
804 * been taken care of.
806 static int sctp_inet_send_verify(struct sctp_sock *opt, union sctp_addr *addr)
808 return 1;
811 /* Fill in Supported Address Type information for INIT and INIT-ACK
812 * chunks. Returns number of addresses supported.
814 static int sctp_inet_supported_addrs(const struct sctp_sock *opt,
815 __be16 *types)
817 types[0] = SCTP_PARAM_IPV4_ADDRESS;
818 return 1;
821 /* Wrapper routine that calls the ip transmit routine. */
822 static inline int sctp_v4_xmit(struct sk_buff *skb,
823 struct sctp_transport *transport, int ipfragok)
825 SCTP_DEBUG_PRINTK("%s: skb:%p, len:%d, "
826 "src:%u.%u.%u.%u, dst:%u.%u.%u.%u\n",
827 __FUNCTION__, skb, skb->len,
828 NIPQUAD(((struct rtable *)skb->dst)->rt_src),
829 NIPQUAD(((struct rtable *)skb->dst)->rt_dst));
831 SCTP_INC_STATS(SCTP_MIB_OUTSCTPPACKS);
832 return ip_queue_xmit(skb, ipfragok);
835 static struct sctp_af sctp_af_inet;
837 static struct sctp_pf sctp_pf_inet = {
838 .event_msgname = sctp_inet_event_msgname,
839 .skb_msgname = sctp_inet_skb_msgname,
840 .af_supported = sctp_inet_af_supported,
841 .cmp_addr = sctp_inet_cmp_addr,
842 .bind_verify = sctp_inet_bind_verify,
843 .send_verify = sctp_inet_send_verify,
844 .supported_addrs = sctp_inet_supported_addrs,
845 .create_accept_sk = sctp_v4_create_accept_sk,
846 .addr_v4map = sctp_v4_addr_v4map,
847 .af = &sctp_af_inet
850 /* Notifier for inetaddr addition/deletion events. */
851 static struct notifier_block sctp_inetaddr_notifier = {
852 .notifier_call = sctp_inetaddr_event,
855 /* Socket operations. */
856 static const struct proto_ops inet_seqpacket_ops = {
857 .family = PF_INET,
858 .owner = THIS_MODULE,
859 .release = inet_release, /* Needs to be wrapped... */
860 .bind = inet_bind,
861 .connect = inet_dgram_connect,
862 .socketpair = sock_no_socketpair,
863 .accept = inet_accept,
864 .getname = inet_getname, /* Semantics are different. */
865 .poll = sctp_poll,
866 .ioctl = inet_ioctl,
867 .listen = sctp_inet_listen,
868 .shutdown = inet_shutdown, /* Looks harmless. */
869 .setsockopt = sock_common_setsockopt, /* IP_SOL IP_OPTION is a problem */
870 .getsockopt = sock_common_getsockopt,
871 .sendmsg = inet_sendmsg,
872 .recvmsg = sock_common_recvmsg,
873 .mmap = sock_no_mmap,
874 .sendpage = sock_no_sendpage,
875 #ifdef CONFIG_COMPAT
876 .compat_setsockopt = compat_sock_common_setsockopt,
877 .compat_getsockopt = compat_sock_common_getsockopt,
878 #endif
881 /* Registration with AF_INET family. */
882 static struct inet_protosw sctp_seqpacket_protosw = {
883 .type = SOCK_SEQPACKET,
884 .protocol = IPPROTO_SCTP,
885 .prot = &sctp_prot,
886 .ops = &inet_seqpacket_ops,
887 .capability = -1,
888 .no_check = 0,
889 .flags = SCTP_PROTOSW_FLAG
891 static struct inet_protosw sctp_stream_protosw = {
892 .type = SOCK_STREAM,
893 .protocol = IPPROTO_SCTP,
894 .prot = &sctp_prot,
895 .ops = &inet_seqpacket_ops,
896 .capability = -1,
897 .no_check = 0,
898 .flags = SCTP_PROTOSW_FLAG
901 /* Register with IP layer. */
902 static struct net_protocol sctp_protocol = {
903 .handler = sctp_rcv,
904 .err_handler = sctp_v4_err,
905 .no_policy = 1,
908 /* IPv4 address related functions. */
909 static struct sctp_af sctp_af_inet = {
910 .sa_family = AF_INET,
911 .sctp_xmit = sctp_v4_xmit,
912 .setsockopt = ip_setsockopt,
913 .getsockopt = ip_getsockopt,
914 .get_dst = sctp_v4_get_dst,
915 .get_saddr = sctp_v4_get_saddr,
916 .copy_addrlist = sctp_v4_copy_addrlist,
917 .from_skb = sctp_v4_from_skb,
918 .from_sk = sctp_v4_from_sk,
919 .to_sk_saddr = sctp_v4_to_sk_saddr,
920 .to_sk_daddr = sctp_v4_to_sk_daddr,
921 .from_addr_param = sctp_v4_from_addr_param,
922 .to_addr_param = sctp_v4_to_addr_param,
923 .dst_saddr = sctp_v4_dst_saddr,
924 .cmp_addr = sctp_v4_cmp_addr,
925 .addr_valid = sctp_v4_addr_valid,
926 .inaddr_any = sctp_v4_inaddr_any,
927 .is_any = sctp_v4_is_any,
928 .available = sctp_v4_available,
929 .scope = sctp_v4_scope,
930 .skb_iif = sctp_v4_skb_iif,
931 .is_ce = sctp_v4_is_ce,
932 .seq_dump_addr = sctp_v4_seq_dump_addr,
933 .net_header_len = sizeof(struct iphdr),
934 .sockaddr_len = sizeof(struct sockaddr_in),
935 #ifdef CONFIG_COMPAT
936 .compat_setsockopt = compat_ip_setsockopt,
937 .compat_getsockopt = compat_ip_getsockopt,
938 #endif
941 struct sctp_pf *sctp_get_pf_specific(sa_family_t family) {
943 switch (family) {
944 case PF_INET:
945 return sctp_pf_inet_specific;
946 case PF_INET6:
947 return sctp_pf_inet6_specific;
948 default:
949 return NULL;
953 /* Register the PF specific function table. */
954 int sctp_register_pf(struct sctp_pf *pf, sa_family_t family)
956 switch (family) {
957 case PF_INET:
958 if (sctp_pf_inet_specific)
959 return 0;
960 sctp_pf_inet_specific = pf;
961 break;
962 case PF_INET6:
963 if (sctp_pf_inet6_specific)
964 return 0;
965 sctp_pf_inet6_specific = pf;
966 break;
967 default:
968 return 0;
970 return 1;
973 static int __init init_sctp_mibs(void)
975 sctp_statistics[0] = alloc_percpu(struct sctp_mib);
976 if (!sctp_statistics[0])
977 return -ENOMEM;
978 sctp_statistics[1] = alloc_percpu(struct sctp_mib);
979 if (!sctp_statistics[1]) {
980 free_percpu(sctp_statistics[0]);
981 return -ENOMEM;
983 return 0;
987 static void cleanup_sctp_mibs(void)
989 free_percpu(sctp_statistics[0]);
990 free_percpu(sctp_statistics[1]);
993 /* Initialize the universe into something sensible. */
994 SCTP_STATIC __init int sctp_init(void)
996 int i;
997 int status = -EINVAL;
998 unsigned long goal;
999 unsigned long limit;
1000 int max_share;
1001 int order;
1003 /* SCTP_DEBUG sanity check. */
1004 if (!sctp_sanity_check())
1005 goto out;
1007 /* Allocate bind_bucket and chunk caches. */
1008 status = -ENOBUFS;
1009 sctp_bucket_cachep = kmem_cache_create("sctp_bind_bucket",
1010 sizeof(struct sctp_bind_bucket),
1011 0, SLAB_HWCACHE_ALIGN,
1012 NULL);
1013 if (!sctp_bucket_cachep)
1014 goto out;
1016 sctp_chunk_cachep = kmem_cache_create("sctp_chunk",
1017 sizeof(struct sctp_chunk),
1018 0, SLAB_HWCACHE_ALIGN,
1019 NULL);
1020 if (!sctp_chunk_cachep)
1021 goto err_chunk_cachep;
1023 /* Allocate and initialise sctp mibs. */
1024 status = init_sctp_mibs();
1025 if (status)
1026 goto err_init_mibs;
1028 /* Initialize proc fs directory. */
1029 status = sctp_proc_init();
1030 if (status)
1031 goto err_init_proc;
1033 /* Initialize object count debugging. */
1034 sctp_dbg_objcnt_init();
1036 /* Initialize the SCTP specific PF functions. */
1037 sctp_register_pf(&sctp_pf_inet, PF_INET);
1039 * 14. Suggested SCTP Protocol Parameter Values
1041 /* The following protocol parameters are RECOMMENDED: */
1042 /* RTO.Initial - 3 seconds */
1043 sctp_rto_initial = SCTP_RTO_INITIAL;
1044 /* RTO.Min - 1 second */
1045 sctp_rto_min = SCTP_RTO_MIN;
1046 /* RTO.Max - 60 seconds */
1047 sctp_rto_max = SCTP_RTO_MAX;
1048 /* RTO.Alpha - 1/8 */
1049 sctp_rto_alpha = SCTP_RTO_ALPHA;
1050 /* RTO.Beta - 1/4 */
1051 sctp_rto_beta = SCTP_RTO_BETA;
1053 /* Valid.Cookie.Life - 60 seconds */
1054 sctp_valid_cookie_life = SCTP_DEFAULT_COOKIE_LIFE;
1056 /* Whether Cookie Preservative is enabled(1) or not(0) */
1057 sctp_cookie_preserve_enable = 1;
1059 /* Max.Burst - 4 */
1060 sctp_max_burst = SCTP_DEFAULT_MAX_BURST;
1062 /* Association.Max.Retrans - 10 attempts
1063 * Path.Max.Retrans - 5 attempts (per destination address)
1064 * Max.Init.Retransmits - 8 attempts
1066 sctp_max_retrans_association = 10;
1067 sctp_max_retrans_path = 5;
1068 sctp_max_retrans_init = 8;
1070 /* Sendbuffer growth - do per-socket accounting */
1071 sctp_sndbuf_policy = 0;
1073 /* Rcvbuffer growth - do per-socket accounting */
1074 sctp_rcvbuf_policy = 0;
1076 /* HB.interval - 30 seconds */
1077 sctp_hb_interval = SCTP_DEFAULT_TIMEOUT_HEARTBEAT;
1079 /* delayed SACK timeout */
1080 sctp_sack_timeout = SCTP_DEFAULT_TIMEOUT_SACK;
1082 /* Implementation specific variables. */
1084 /* Initialize default stream count setup information. */
1085 sctp_max_instreams = SCTP_DEFAULT_INSTREAMS;
1086 sctp_max_outstreams = SCTP_DEFAULT_OUTSTREAMS;
1088 /* Initialize handle used for association ids. */
1089 idr_init(&sctp_assocs_id);
1091 /* Set the pressure threshold to be a fraction of global memory that
1092 * is up to 1/2 at 256 MB, decreasing toward zero with the amount of
1093 * memory, with a floor of 128 pages.
1094 * Note this initalizes the data in sctpv6_prot too
1095 * Unabashedly stolen from tcp_init
1097 limit = min(num_physpages, 1UL<<(28-PAGE_SHIFT)) >> (20-PAGE_SHIFT);
1098 limit = (limit * (num_physpages >> (20-PAGE_SHIFT))) >> (PAGE_SHIFT-11);
1099 limit = max(limit, 128UL);
1100 sysctl_sctp_mem[0] = limit / 4 * 3;
1101 sysctl_sctp_mem[1] = limit;
1102 sysctl_sctp_mem[2] = sysctl_sctp_mem[0] * 2;
1104 /* Set per-socket limits to no more than 1/128 the pressure threshold*/
1105 limit = (sysctl_sctp_mem[1]) << (PAGE_SHIFT - 7);
1106 max_share = min(4UL*1024*1024, limit);
1108 sysctl_sctp_rmem[0] = PAGE_SIZE; /* give each asoc 1 page min */
1109 sysctl_sctp_rmem[1] = (1500 *(sizeof(struct sk_buff) + 1));
1110 sysctl_sctp_rmem[2] = max(sysctl_sctp_rmem[1], max_share);
1112 sysctl_sctp_wmem[0] = SK_MEM_QUANTUM;
1113 sysctl_sctp_wmem[1] = 16*1024;
1114 sysctl_sctp_wmem[2] = max(64*1024, max_share);
1116 /* Size and allocate the association hash table.
1117 * The methodology is similar to that of the tcp hash tables.
1119 if (num_physpages >= (128 * 1024))
1120 goal = num_physpages >> (22 - PAGE_SHIFT);
1121 else
1122 goal = num_physpages >> (24 - PAGE_SHIFT);
1124 for (order = 0; (1UL << order) < goal; order++)
1127 do {
1128 sctp_assoc_hashsize = (1UL << order) * PAGE_SIZE /
1129 sizeof(struct sctp_hashbucket);
1130 if ((sctp_assoc_hashsize > (64 * 1024)) && order > 0)
1131 continue;
1132 sctp_assoc_hashtable = (struct sctp_hashbucket *)
1133 __get_free_pages(GFP_ATOMIC, order);
1134 } while (!sctp_assoc_hashtable && --order > 0);
1135 if (!sctp_assoc_hashtable) {
1136 printk(KERN_ERR "SCTP: Failed association hash alloc.\n");
1137 status = -ENOMEM;
1138 goto err_ahash_alloc;
1140 for (i = 0; i < sctp_assoc_hashsize; i++) {
1141 rwlock_init(&sctp_assoc_hashtable[i].lock);
1142 INIT_HLIST_HEAD(&sctp_assoc_hashtable[i].chain);
1145 /* Allocate and initialize the endpoint hash table. */
1146 sctp_ep_hashsize = 64;
1147 sctp_ep_hashtable = (struct sctp_hashbucket *)
1148 kmalloc(64 * sizeof(struct sctp_hashbucket), GFP_KERNEL);
1149 if (!sctp_ep_hashtable) {
1150 printk(KERN_ERR "SCTP: Failed endpoint_hash alloc.\n");
1151 status = -ENOMEM;
1152 goto err_ehash_alloc;
1154 for (i = 0; i < sctp_ep_hashsize; i++) {
1155 rwlock_init(&sctp_ep_hashtable[i].lock);
1156 INIT_HLIST_HEAD(&sctp_ep_hashtable[i].chain);
1159 /* Allocate and initialize the SCTP port hash table. */
1160 do {
1161 sctp_port_hashsize = (1UL << order) * PAGE_SIZE /
1162 sizeof(struct sctp_bind_hashbucket);
1163 if ((sctp_port_hashsize > (64 * 1024)) && order > 0)
1164 continue;
1165 sctp_port_hashtable = (struct sctp_bind_hashbucket *)
1166 __get_free_pages(GFP_ATOMIC, order);
1167 } while (!sctp_port_hashtable && --order > 0);
1168 if (!sctp_port_hashtable) {
1169 printk(KERN_ERR "SCTP: Failed bind hash alloc.");
1170 status = -ENOMEM;
1171 goto err_bhash_alloc;
1173 for (i = 0; i < sctp_port_hashsize; i++) {
1174 spin_lock_init(&sctp_port_hashtable[i].lock);
1175 INIT_HLIST_HEAD(&sctp_port_hashtable[i].chain);
1178 printk(KERN_INFO "SCTP: Hash tables configured "
1179 "(established %d bind %d)\n",
1180 sctp_assoc_hashsize, sctp_port_hashsize);
1182 /* Disable ADDIP by default. */
1183 sctp_addip_enable = 0;
1184 sctp_addip_noauth = 0;
1186 /* Enable PR-SCTP by default. */
1187 sctp_prsctp_enable = 1;
1189 /* Disable AUTH by default. */
1190 sctp_auth_enable = 0;
1192 sctp_sysctl_register();
1194 INIT_LIST_HEAD(&sctp_address_families);
1195 sctp_register_af(&sctp_af_inet);
1197 status = proto_register(&sctp_prot, 1);
1198 if (status)
1199 goto err_proto_register;
1201 /* Register SCTP(UDP and TCP style) with socket layer. */
1202 inet_register_protosw(&sctp_seqpacket_protosw);
1203 inet_register_protosw(&sctp_stream_protosw);
1205 status = sctp_v6_init();
1206 if (status)
1207 goto err_v6_init;
1209 /* Initialize the control inode/socket for handling OOTB packets. */
1210 if ((status = sctp_ctl_sock_init())) {
1211 printk (KERN_ERR
1212 "SCTP: Failed to initialize the SCTP control sock.\n");
1213 goto err_ctl_sock_init;
1216 /* Initialize the local address list. */
1217 INIT_LIST_HEAD(&sctp_local_addr_list);
1218 spin_lock_init(&sctp_local_addr_lock);
1219 sctp_get_local_addr_list();
1221 /* Register notifier for inet address additions/deletions. */
1222 register_inetaddr_notifier(&sctp_inetaddr_notifier);
1224 /* Register SCTP with inet layer. */
1225 if (inet_add_protocol(&sctp_protocol, IPPROTO_SCTP) < 0) {
1226 status = -EAGAIN;
1227 goto err_add_protocol;
1230 /* Register SCTP with inet6 layer. */
1231 status = sctp_v6_add_protocol();
1232 if (status)
1233 goto err_v6_add_protocol;
1235 status = 0;
1236 out:
1237 return status;
1238 err_v6_add_protocol:
1239 inet_del_protocol(&sctp_protocol, IPPROTO_SCTP);
1240 unregister_inetaddr_notifier(&sctp_inetaddr_notifier);
1241 err_add_protocol:
1242 sctp_free_local_addr_list();
1243 sock_release(sctp_ctl_socket);
1244 err_ctl_sock_init:
1245 sctp_v6_exit();
1246 err_v6_init:
1247 inet_unregister_protosw(&sctp_stream_protosw);
1248 inet_unregister_protosw(&sctp_seqpacket_protosw);
1249 proto_unregister(&sctp_prot);
1250 err_proto_register:
1251 sctp_sysctl_unregister();
1252 list_del(&sctp_af_inet.list);
1253 free_pages((unsigned long)sctp_port_hashtable,
1254 get_order(sctp_port_hashsize *
1255 sizeof(struct sctp_bind_hashbucket)));
1256 err_bhash_alloc:
1257 kfree(sctp_ep_hashtable);
1258 err_ehash_alloc:
1259 free_pages((unsigned long)sctp_assoc_hashtable,
1260 get_order(sctp_assoc_hashsize *
1261 sizeof(struct sctp_hashbucket)));
1262 err_ahash_alloc:
1263 sctp_dbg_objcnt_exit();
1264 sctp_proc_exit();
1265 err_init_proc:
1266 cleanup_sctp_mibs();
1267 err_init_mibs:
1268 kmem_cache_destroy(sctp_chunk_cachep);
1269 err_chunk_cachep:
1270 kmem_cache_destroy(sctp_bucket_cachep);
1271 goto out;
1274 /* Exit handler for the SCTP protocol. */
1275 SCTP_STATIC __exit void sctp_exit(void)
1277 /* BUG. This should probably do something useful like clean
1278 * up all the remaining associations and all that memory.
1281 /* Unregister with inet6/inet layers. */
1282 sctp_v6_del_protocol();
1283 inet_del_protocol(&sctp_protocol, IPPROTO_SCTP);
1285 /* Unregister notifier for inet address additions/deletions. */
1286 unregister_inetaddr_notifier(&sctp_inetaddr_notifier);
1288 /* Free the local address list. */
1289 sctp_free_local_addr_list();
1291 /* Free the control endpoint. */
1292 sock_release(sctp_ctl_socket);
1294 /* Cleanup v6 initializations. */
1295 sctp_v6_exit();
1297 /* Unregister with socket layer. */
1298 inet_unregister_protosw(&sctp_stream_protosw);
1299 inet_unregister_protosw(&sctp_seqpacket_protosw);
1301 sctp_sysctl_unregister();
1302 list_del(&sctp_af_inet.list);
1304 free_pages((unsigned long)sctp_assoc_hashtable,
1305 get_order(sctp_assoc_hashsize *
1306 sizeof(struct sctp_hashbucket)));
1307 kfree(sctp_ep_hashtable);
1308 free_pages((unsigned long)sctp_port_hashtable,
1309 get_order(sctp_port_hashsize *
1310 sizeof(struct sctp_bind_hashbucket)));
1312 sctp_dbg_objcnt_exit();
1313 sctp_proc_exit();
1314 cleanup_sctp_mibs();
1316 kmem_cache_destroy(sctp_chunk_cachep);
1317 kmem_cache_destroy(sctp_bucket_cachep);
1319 proto_unregister(&sctp_prot);
1322 module_init(sctp_init);
1323 module_exit(sctp_exit);
1326 * __stringify doesn't likes enums, so use IPPROTO_SCTP value (132) directly.
1328 MODULE_ALIAS("net-pf-" __stringify(PF_INET) "-proto-132");
1329 MODULE_ALIAS("net-pf-" __stringify(PF_INET6) "-proto-132");
1330 MODULE_AUTHOR("Linux Kernel SCTP developers <lksctp-developers@lists.sourceforge.net>");
1331 MODULE_DESCRIPTION("Support for the SCTP protocol (RFC2960)");
1332 MODULE_LICENSE("GPL");