1 /* SCTP kernel reference Implementation
2 * Copyright (c) 1999-2000 Cisco, Inc.
3 * Copyright (c) 1999-2001 Motorola, Inc.
4 * Copyright (c) 2001-2003 International Business Machines, Corp.
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 reference Implementation
11 * Initialization/cleanup for SCTP protocol support.
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)
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.
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
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 <net/protocol.h>
57 #include <net/sctp/sctp.h>
58 #include <net/addrconf.h>
59 #include <net/inet_common.h>
60 #include <net/inet_ecn.h>
62 /* Global data structures. */
63 struct sctp_globals sctp_globals;
64 struct proc_dir_entry *proc_net_sctp;
65 DEFINE_SNMP_STAT(struct sctp_mib, sctp_statistics);
67 /* This is the global socket data structure used for responding to
68 * the Out-of-the-blue (OOTB) packets. A control sock will be created
69 * for this socket at the initialization time.
71 static struct socket *sctp_ctl_socket;
73 static struct sctp_pf *sctp_pf_inet6_specific;
74 static struct sctp_pf *sctp_pf_inet_specific;
75 static struct sctp_af *sctp_af_v4_specific;
76 static struct sctp_af *sctp_af_v6_specific;
78 kmem_cache_t *sctp_chunk_cachep;
79 kmem_cache_t *sctp_bucket_cachep;
81 extern struct net_proto_family inet_family_ops;
83 extern int sctp_snmp_proc_init(void);
84 extern int sctp_snmp_proc_exit(void);
85 extern int sctp_eps_proc_init(void);
86 extern int sctp_eps_proc_exit(void);
87 extern int sctp_assocs_proc_init(void);
88 extern int sctp_assocs_proc_exit(void);
90 /* Return the address of the control sock. */
91 struct sock *sctp_get_ctl_sock(void)
93 return sctp_ctl_socket->sk;
96 /* Set up the proc fs entry for the SCTP protocol. */
97 __init int sctp_proc_init(void)
100 struct proc_dir_entry *ent;
101 ent = proc_mkdir("net/sctp", 0);
103 ent->owner = THIS_MODULE;
109 if (sctp_snmp_proc_init())
111 if (sctp_eps_proc_init())
113 if (sctp_assocs_proc_init())
122 /* Clean up the proc fs entry for the SCTP protocol.
123 * Note: Do not make this __exit as it is used in the init error
126 void sctp_proc_exit(void)
128 sctp_snmp_proc_exit();
129 sctp_eps_proc_exit();
130 sctp_assocs_proc_exit();
133 proc_net_sctp = NULL;
134 remove_proc_entry("net/sctp", 0);
138 /* Private helper to extract ipv4 address and stash them in
139 * the protocol structure.
141 static void sctp_v4_copy_addrlist(struct list_head *addrlist,
142 struct net_device *dev)
144 struct in_device *in_dev;
145 struct in_ifaddr *ifa;
146 struct sctp_sockaddr_entry *addr;
148 read_lock(&inetdev_lock);
149 if ((in_dev = __in_dev_get(dev)) == NULL) {
150 read_unlock(&inetdev_lock);
154 read_lock(&in_dev->lock);
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);
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 list_add_tail(&addr->list, addrlist);
166 read_unlock(&in_dev->lock);
167 read_unlock(&inetdev_lock);
170 /* Extract our IP addresses from the system and stash them in the
171 * protocol structure.
173 static void __sctp_get_local_addr_list(void)
175 struct net_device *dev;
176 struct list_head *pos;
179 read_lock(&dev_base_lock);
180 for (dev = dev_base; dev; dev = dev->next) {
181 __list_for_each(pos, &sctp_address_families) {
182 af = list_entry(pos, struct sctp_af, list);
183 af->copy_addrlist(&sctp_local_addr_list, dev);
186 read_unlock(&dev_base_lock);
189 static void sctp_get_local_addr_list(void)
193 sctp_spin_lock_irqsave(&sctp_local_addr_lock, flags);
194 __sctp_get_local_addr_list();
195 sctp_spin_unlock_irqrestore(&sctp_local_addr_lock, flags);
198 /* Free the existing local addresses. */
199 static void __sctp_free_local_addr_list(void)
201 struct sctp_sockaddr_entry *addr;
202 struct list_head *pos, *temp;
204 list_for_each_safe(pos, temp, &sctp_local_addr_list) {
205 addr = list_entry(pos, struct sctp_sockaddr_entry, list);
211 /* Free the existing local addresses. */
212 static void sctp_free_local_addr_list(void)
216 sctp_spin_lock_irqsave(&sctp_local_addr_lock, flags);
217 __sctp_free_local_addr_list();
218 sctp_spin_unlock_irqrestore(&sctp_local_addr_lock, flags);
221 /* Copy the local addresses which are valid for 'scope' into 'bp'. */
222 int sctp_copy_local_addr_list(struct sctp_bind_addr *bp, sctp_scope_t scope,
223 int gfp, int copy_flags)
225 struct sctp_sockaddr_entry *addr;
227 struct list_head *pos;
230 sctp_spin_lock_irqsave(&sctp_local_addr_lock, flags);
231 list_for_each(pos, &sctp_local_addr_list) {
232 addr = list_entry(pos, struct sctp_sockaddr_entry, list);
233 if (sctp_in_scope(&addr->a, scope)) {
234 /* Now that the address is in scope, check to see if
235 * the address type is really supported by the local
236 * sock as well as the remote peer.
238 if ((((AF_INET == addr->a.sa.sa_family) &&
239 (copy_flags & SCTP_ADDR4_PEERSUPP))) ||
240 (((AF_INET6 == addr->a.sa.sa_family) &&
241 (copy_flags & SCTP_ADDR6_ALLOWED) &&
242 (copy_flags & SCTP_ADDR6_PEERSUPP)))) {
243 error = sctp_add_bind_addr(bp, &addr->a,
252 sctp_spin_unlock_irqrestore(&sctp_local_addr_lock, flags);
256 /* Initialize a sctp_addr from in incoming skb. */
257 static void sctp_v4_from_skb(union sctp_addr *addr, struct sk_buff *skb,
264 port = &addr->v4.sin_port;
265 addr->v4.sin_family = AF_INET;
267 sh = (struct sctphdr *) skb->h.raw;
269 *port = ntohs(sh->source);
270 from = &skb->nh.iph->saddr;
272 *port = ntohs(sh->dest);
273 from = &skb->nh.iph->daddr;
275 memcpy(&addr->v4.sin_addr.s_addr, from, sizeof(struct in_addr));
278 /* Initialize an sctp_addr from a socket. */
279 static void sctp_v4_from_sk(union sctp_addr *addr, struct sock *sk)
281 addr->v4.sin_family = AF_INET;
282 addr->v4.sin_port = inet_sk(sk)->num;
283 addr->v4.sin_addr.s_addr = inet_sk(sk)->rcv_saddr;
286 /* Initialize sk->sk_rcv_saddr from sctp_addr. */
287 static void sctp_v4_to_sk_saddr(union sctp_addr *addr, struct sock *sk)
289 inet_sk(sk)->rcv_saddr = addr->v4.sin_addr.s_addr;
292 /* Initialize sk->sk_daddr from sctp_addr. */
293 static void sctp_v4_to_sk_daddr(union sctp_addr *addr, struct sock *sk)
295 inet_sk(sk)->daddr = addr->v4.sin_addr.s_addr;
298 /* Initialize a sctp_addr from a dst_entry. */
299 static void sctp_v4_dst_saddr(union sctp_addr *saddr, struct dst_entry *dst,
302 struct rtable *rt = (struct rtable *)dst;
303 saddr->v4.sin_family = AF_INET;
304 saddr->v4.sin_port = port;
305 saddr->v4.sin_addr.s_addr = rt->rt_src;
308 /* Compare two addresses exactly. */
309 static int sctp_v4_cmp_addr(const union sctp_addr *addr1,
310 const union sctp_addr *addr2)
312 if (addr1->sa.sa_family != addr2->sa.sa_family)
314 if (addr1->v4.sin_port != addr2->v4.sin_port)
316 if (addr1->v4.sin_addr.s_addr != addr2->v4.sin_addr.s_addr)
322 /* Initialize addr struct to INADDR_ANY. */
323 static void sctp_v4_inaddr_any(union sctp_addr *addr, unsigned short port)
325 addr->v4.sin_family = AF_INET;
326 addr->v4.sin_addr.s_addr = INADDR_ANY;
327 addr->v4.sin_port = port;
330 /* Is this a wildcard address? */
331 static int sctp_v4_is_any(const union sctp_addr *addr)
333 return INADDR_ANY == addr->v4.sin_addr.s_addr;
336 /* This function checks if the address is a valid address to be used for
340 * Return 0 - If the address is a non-unicast or an illegal address.
341 * Return 1 - If the address is a unicast.
343 static int sctp_v4_addr_valid(union sctp_addr *addr, struct sctp_opt *sp)
345 /* Is this a non-unicast address or a unusable SCTP address? */
346 if (IS_IPV4_UNUSABLE_ADDRESS(&addr->v4.sin_addr.s_addr))
352 /* Should this be available for binding? */
353 static int sctp_v4_available(union sctp_addr *addr, struct sctp_opt *sp)
355 int ret = inet_addr_type(addr->v4.sin_addr.s_addr);
357 /* FIXME: ip_nonlocal_bind sysctl support. */
359 if (addr->v4.sin_addr.s_addr != INADDR_ANY && ret != RTN_LOCAL)
364 /* Checking the loopback, private and other address scopes as defined in
365 * RFC 1918. The IPv4 scoping is based on the draft for SCTP IPv4
366 * scoping <draft-stewart-tsvwg-sctp-ipv4-00.txt>.
368 * Level 0 - unusable SCTP addresses
369 * Level 1 - loopback address
370 * Level 2 - link-local addresses
371 * Level 3 - private addresses.
372 * Level 4 - global addresses
373 * For INIT and INIT-ACK address list, let L be the level of
374 * of requested destination address, sender and receiver
375 * SHOULD include all of its addresses with level greater
376 * than or equal to L.
378 static sctp_scope_t sctp_v4_scope(union sctp_addr *addr)
382 /* Should IPv4 scoping be a sysctl configurable option
383 * so users can turn it off (default on) for certain
384 * unconventional networking environments?
387 /* Check for unusable SCTP addresses. */
388 if (IS_IPV4_UNUSABLE_ADDRESS(&addr->v4.sin_addr.s_addr)) {
389 retval = SCTP_SCOPE_UNUSABLE;
390 } else if (LOOPBACK(addr->v4.sin_addr.s_addr)) {
391 retval = SCTP_SCOPE_LOOPBACK;
392 } else if (IS_IPV4_LINK_ADDRESS(&addr->v4.sin_addr.s_addr)) {
393 retval = SCTP_SCOPE_LINK;
394 } else if (IS_IPV4_PRIVATE_ADDRESS(&addr->v4.sin_addr.s_addr)) {
395 retval = SCTP_SCOPE_PRIVATE;
397 retval = SCTP_SCOPE_GLOBAL;
403 /* Returns a valid dst cache entry for the given source and destination ip
404 * addresses. If an association is passed, trys to get a dst entry with a
405 * source address that matches an address in the bind address list.
407 struct dst_entry *sctp_v4_get_dst(struct sctp_association *asoc,
408 union sctp_addr *daddr,
409 union sctp_addr *saddr)
413 struct sctp_bind_addr *bp;
415 struct sctp_sockaddr_entry *laddr;
416 struct list_head *pos;
417 struct dst_entry *dst = NULL;
418 union sctp_addr dst_saddr;
420 memset(&fl, 0x0, sizeof(struct flowi));
421 fl.fl4_dst = daddr->v4.sin_addr.s_addr;
422 fl.proto = IPPROTO_SCTP;
425 fl.fl4_src = saddr->v4.sin_addr.s_addr;
427 SCTP_DEBUG_PRINTK("%s: DST:%u.%u.%u.%u, SRC:%u.%u.%u.%u - ",
428 __FUNCTION__, NIPQUAD(fl.fl4_dst),
429 NIPQUAD(fl.fl4_src));
431 if (!ip_route_output_key(&rt, &fl)) {
435 /* If there is no association or if a source address is passed, no
436 * more validation is required.
441 bp = &asoc->base.bind_addr;
442 addr_lock = &asoc->base.addr_lock;
445 /* Walk through the bind address list and look for a bind
446 * address that matches the source address of the returned dst.
448 sctp_read_lock(addr_lock);
449 list_for_each(pos, &bp->address_list) {
450 laddr = list_entry(pos, struct sctp_sockaddr_entry,
452 sctp_v4_dst_saddr(&dst_saddr, dst, bp->port);
453 if (sctp_v4_cmp_addr(&dst_saddr, &laddr->a))
456 sctp_read_unlock(addr_lock);
458 /* None of the bound addresses match the source address of the
459 * dst. So release it.
465 /* Walk through the bind address list and try to get a dst that
466 * matches a bind address as the source address.
468 sctp_read_lock(addr_lock);
469 list_for_each(pos, &bp->address_list) {
470 laddr = list_entry(pos, struct sctp_sockaddr_entry, list);
472 if (AF_INET == laddr->a.sa.sa_family) {
473 fl.fl4_src = laddr->a.v4.sin_addr.s_addr;
474 if (!ip_route_output_key(&rt, &fl)) {
482 sctp_read_unlock(addr_lock);
485 SCTP_DEBUG_PRINTK("rt_dst:%u.%u.%u.%u, rt_src:%u.%u.%u.%u\n",
486 NIPQUAD(rt->rt_dst), NIPQUAD(rt->rt_src));
488 SCTP_DEBUG_PRINTK("NO ROUTE\n");
493 /* For v4, the source address is cached in the route entry(dst). So no need
494 * to cache it separately and hence this is an empty routine.
496 void sctp_v4_get_saddr(struct sctp_association *asoc,
497 struct dst_entry *dst,
498 union sctp_addr *daddr,
499 union sctp_addr *saddr)
501 struct rtable *rt = (struct rtable *)dst;
504 saddr->v4.sin_family = AF_INET;
505 saddr->v4.sin_port = asoc->base.bind_addr.port;
506 saddr->v4.sin_addr.s_addr = rt->rt_src;
510 /* What interface did this skb arrive on? */
511 static int sctp_v4_skb_iif(const struct sk_buff *skb)
513 return ((struct rtable *)skb->dst)->rt_iif;
516 /* Was this packet marked by Explicit Congestion Notification? */
517 static int sctp_v4_is_ce(const struct sk_buff *skb)
519 return INET_ECN_is_ce(skb->nh.iph->tos);
522 /* Create and initialize a new sk for the socket returned by accept(). */
523 struct sock *sctp_v4_create_accept_sk(struct sock *sk,
524 struct sctp_association *asoc)
527 struct inet_opt *inet = inet_sk(sk);
528 struct inet_opt *newinet;
530 newsk = sk_alloc(PF_INET, GFP_KERNEL, sizeof(struct sctp_sock),
535 sock_init_data(NULL, newsk);
536 sk_set_owner(newsk, THIS_MODULE);
538 newsk->sk_type = SOCK_STREAM;
540 newsk->sk_prot = sk->sk_prot;
541 newsk->sk_no_check = sk->sk_no_check;
542 newsk->sk_reuse = sk->sk_reuse;
543 newsk->sk_shutdown = sk->sk_shutdown;
545 newsk->sk_destruct = inet_sock_destruct;
546 newsk->sk_zapped = 0;
547 newsk->sk_family = PF_INET;
548 newsk->sk_protocol = IPPROTO_SCTP;
549 newsk->sk_backlog_rcv = sk->sk_prot->backlog_rcv;
551 newinet = inet_sk(newsk);
553 /* Initialize sk's sport, dport, rcv_saddr and daddr for
554 * getsockname() and getpeername()
556 newinet->sport = inet->sport;
557 newinet->saddr = inet->saddr;
558 newinet->rcv_saddr = inet->rcv_saddr;
559 newinet->dport = htons(asoc->peer.port);
560 newinet->daddr = asoc->peer.primary_addr.v4.sin_addr.s_addr;
561 newinet->pmtudisc = inet->pmtudisc;
564 newinet->uc_ttl = -1;
565 newinet->mc_loop = 1;
567 newinet->mc_index = 0;
568 newinet->mc_list = NULL;
570 #ifdef INET_REFCNT_DEBUG
571 atomic_inc(&inet_sock_nr);
574 if (newsk->sk_prot->init(newsk)) {
575 inet_sock_release(newsk);
583 /* Map address, empty for v4 family */
584 static void sctp_v4_addr_v4map(struct sctp_opt *sp, union sctp_addr *addr)
589 /* Dump the v4 addr to the seq file. */
590 static void sctp_v4_seq_dump_addr(struct seq_file *seq, union sctp_addr *addr)
592 seq_printf(seq, "%d.%d.%d.%d ", NIPQUAD(addr->v4.sin_addr));
595 /* Event handler for inet address addition/deletion events.
596 * Basically, whenever there is an event, we re-build our local address list.
598 static int sctp_inetaddr_event(struct notifier_block *this, unsigned long ev,
603 sctp_spin_lock_irqsave(&sctp_local_addr_lock, flags);
604 __sctp_free_local_addr_list();
605 __sctp_get_local_addr_list();
606 sctp_spin_unlock_irqrestore(&sctp_local_addr_lock, flags);
612 * Initialize the control inode/socket with a control endpoint data
613 * structure. This endpoint is reserved exclusively for the OOTB processing.
615 int sctp_ctl_sock_init(void)
620 if (sctp_get_pf_specific(PF_INET6))
625 err = sock_create(family, SOCK_SEQPACKET, IPPROTO_SCTP,
629 "SCTP: Failed to create the SCTP control socket.\n");
632 sctp_ctl_socket->sk->sk_allocation = GFP_ATOMIC;
633 inet_sk(sctp_ctl_socket->sk)->uc_ttl = -1;
638 /* Register address family specific functions. */
639 int sctp_register_af(struct sctp_af *af)
641 switch (af->sa_family) {
643 if (sctp_af_v4_specific)
645 sctp_af_v4_specific = af;
648 if (sctp_af_v6_specific)
650 sctp_af_v6_specific = af;
656 INIT_LIST_HEAD(&af->list);
657 list_add_tail(&af->list, &sctp_address_families);
661 /* Get the table of functions for manipulating a particular address
664 struct sctp_af *sctp_get_af_specific(sa_family_t family)
668 return sctp_af_v4_specific;
670 return sctp_af_v6_specific;
676 /* Common code to initialize a AF_INET msg_name. */
677 static void sctp_inet_msgname(char *msgname, int *addr_len)
679 struct sockaddr_in *sin;
681 sin = (struct sockaddr_in *)msgname;
682 *addr_len = sizeof(struct sockaddr_in);
683 sin->sin_family = AF_INET;
684 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
687 /* Copy the primary address of the peer primary address as the msg_name. */
688 static void sctp_inet_event_msgname(struct sctp_ulpevent *event, char *msgname,
691 struct sockaddr_in *sin, *sinfrom;
694 struct sctp_association *asoc;
696 asoc = event->sndrcvinfo.sinfo_assoc_id;
697 sctp_inet_msgname(msgname, addr_len);
698 sin = (struct sockaddr_in *)msgname;
699 sinfrom = &asoc->peer.primary_addr.v4;
700 sin->sin_port = htons(asoc->peer.port);
701 sin->sin_addr.s_addr = sinfrom->sin_addr.s_addr;
705 /* Initialize and copy out a msgname from an inbound skb. */
706 static void sctp_inet_skb_msgname(struct sk_buff *skb, char *msgname, int *len)
709 struct sockaddr_in *sin;
712 sctp_inet_msgname(msgname, len);
713 sin = (struct sockaddr_in *)msgname;
714 sh = (struct sctphdr *)skb->h.raw;
715 sin->sin_port = sh->source;
716 sin->sin_addr.s_addr = skb->nh.iph->saddr;
720 /* Do we support this AF? */
721 static int sctp_inet_af_supported(sa_family_t family, struct sctp_opt *sp)
723 /* PF_INET only supports AF_INET addresses. */
724 return (AF_INET == family);
727 /* Address matching with wildcards allowed. */
728 static int sctp_inet_cmp_addr(const union sctp_addr *addr1,
729 const union sctp_addr *addr2,
730 struct sctp_opt *opt)
732 /* PF_INET only supports AF_INET addresses. */
733 if (addr1->sa.sa_family != addr2->sa.sa_family)
735 if (INADDR_ANY == addr1->v4.sin_addr.s_addr ||
736 INADDR_ANY == addr2->v4.sin_addr.s_addr)
738 if (addr1->v4.sin_addr.s_addr == addr2->v4.sin_addr.s_addr)
744 /* Verify that provided sockaddr looks bindable. Common verification has
745 * already been taken care of.
747 static int sctp_inet_bind_verify(struct sctp_opt *opt, union sctp_addr *addr)
749 return sctp_v4_available(addr, opt);
752 /* Verify that sockaddr looks sendable. Common verification has already
753 * been taken care of.
755 static int sctp_inet_send_verify(struct sctp_opt *opt, union sctp_addr *addr)
760 /* Fill in Supported Address Type information for INIT and INIT-ACK
761 * chunks. Returns number of addresses supported.
763 static int sctp_inet_supported_addrs(const struct sctp_opt *opt,
766 types[0] = SCTP_PARAM_IPV4_ADDRESS;
770 /* Wrapper routine that calls the ip transmit routine. */
771 static inline int sctp_v4_xmit(struct sk_buff *skb,
772 struct sctp_transport *transport, int ipfragok)
774 SCTP_DEBUG_PRINTK("%s: skb:%p, len:%d, "
775 "src:%u.%u.%u.%u, dst:%u.%u.%u.%u\n",
776 __FUNCTION__, skb, skb->len,
777 NIPQUAD(((struct rtable *)skb->dst)->rt_src),
778 NIPQUAD(((struct rtable *)skb->dst)->rt_dst));
780 SCTP_INC_STATS(SctpOutSCTPPacks);
781 return ip_queue_xmit(skb, ipfragok);
784 struct sctp_af sctp_ipv4_specific;
786 static struct sctp_pf sctp_pf_inet = {
787 .event_msgname = sctp_inet_event_msgname,
788 .skb_msgname = sctp_inet_skb_msgname,
789 .af_supported = sctp_inet_af_supported,
790 .cmp_addr = sctp_inet_cmp_addr,
791 .bind_verify = sctp_inet_bind_verify,
792 .send_verify = sctp_inet_send_verify,
793 .supported_addrs = sctp_inet_supported_addrs,
794 .create_accept_sk = sctp_v4_create_accept_sk,
795 .addr_v4map = sctp_v4_addr_v4map,
796 .af = &sctp_ipv4_specific,
799 /* Notifier for inetaddr addition/deletion events. */
800 struct notifier_block sctp_inetaddr_notifier = {
801 .notifier_call = sctp_inetaddr_event,
804 /* Socket operations. */
805 struct proto_ops inet_seqpacket_ops = {
807 .owner = THIS_MODULE,
808 .release = inet_release, /* Needs to be wrapped... */
810 .connect = inet_dgram_connect,
811 .socketpair = sock_no_socketpair,
812 .accept = inet_accept,
813 .getname = inet_getname, /* Semantics are different. */
816 .listen = sctp_inet_listen,
817 .shutdown = inet_shutdown, /* Looks harmless. */
818 .setsockopt = inet_setsockopt, /* IP_SOL IP_OPTION is a problem. */
819 .getsockopt = inet_getsockopt,
820 .sendmsg = inet_sendmsg,
821 .recvmsg = inet_recvmsg,
822 .mmap = sock_no_mmap,
823 .sendpage = sock_no_sendpage,
826 /* Registration with AF_INET family. */
827 static struct inet_protosw sctp_seqpacket_protosw = {
828 .type = SOCK_SEQPACKET,
829 .protocol = IPPROTO_SCTP,
831 .ops = &inet_seqpacket_ops,
834 .flags = SCTP_PROTOSW_FLAG
836 static struct inet_protosw sctp_stream_protosw = {
838 .protocol = IPPROTO_SCTP,
840 .ops = &inet_seqpacket_ops,
843 .flags = SCTP_PROTOSW_FLAG
846 /* Register with IP layer. */
847 static struct inet_protocol sctp_protocol = {
849 .err_handler = sctp_v4_err,
853 /* IPv4 address related functions. */
854 struct sctp_af sctp_ipv4_specific = {
855 .sctp_xmit = sctp_v4_xmit,
856 .setsockopt = ip_setsockopt,
857 .getsockopt = ip_getsockopt,
858 .get_dst = sctp_v4_get_dst,
859 .get_saddr = sctp_v4_get_saddr,
860 .copy_addrlist = sctp_v4_copy_addrlist,
861 .from_skb = sctp_v4_from_skb,
862 .from_sk = sctp_v4_from_sk,
863 .to_sk_saddr = sctp_v4_to_sk_saddr,
864 .to_sk_daddr = sctp_v4_to_sk_daddr,
865 .dst_saddr = sctp_v4_dst_saddr,
866 .cmp_addr = sctp_v4_cmp_addr,
867 .addr_valid = sctp_v4_addr_valid,
868 .inaddr_any = sctp_v4_inaddr_any,
869 .is_any = sctp_v4_is_any,
870 .available = sctp_v4_available,
871 .scope = sctp_v4_scope,
872 .skb_iif = sctp_v4_skb_iif,
873 .is_ce = sctp_v4_is_ce,
874 .seq_dump_addr = sctp_v4_seq_dump_addr,
875 .net_header_len = sizeof(struct iphdr),
876 .sockaddr_len = sizeof(struct sockaddr_in),
877 .sa_family = AF_INET,
880 struct sctp_pf *sctp_get_pf_specific(sa_family_t family) {
884 return sctp_pf_inet_specific;
886 return sctp_pf_inet6_specific;
892 /* Register the PF specific function table. */
893 int sctp_register_pf(struct sctp_pf *pf, sa_family_t family)
897 if (sctp_pf_inet_specific)
899 sctp_pf_inet_specific = pf;
902 if (sctp_pf_inet6_specific)
904 sctp_pf_inet6_specific = pf;
912 static int __init init_sctp_mibs(void)
914 sctp_statistics[0] = alloc_percpu(struct sctp_mib);
915 if (!sctp_statistics[0])
917 sctp_statistics[1] = alloc_percpu(struct sctp_mib);
918 if (!sctp_statistics[1]) {
919 free_percpu(sctp_statistics[0]);
926 static void cleanup_sctp_mibs(void)
928 free_percpu(sctp_statistics[0]);
929 free_percpu(sctp_statistics[1]);
932 /* Initialize the universe into something sensible. */
933 __init int sctp_init(void)
938 /* SCTP_DEBUG sanity check. */
939 if (!sctp_sanity_check())
942 /* Add SCTP to inet_protos hash table. */
943 if (inet_add_protocol(&sctp_protocol, IPPROTO_SCTP) < 0)
946 /* Add SCTP(TCP and UDP style) to inetsw linked list. */
947 inet_register_protosw(&sctp_seqpacket_protosw);
948 inet_register_protosw(&sctp_stream_protosw);
950 /* Allocate a cache pools. */
951 sctp_bucket_cachep = kmem_cache_create("sctp_bind_bucket",
952 sizeof(struct sctp_bind_bucket),
953 0, SLAB_HWCACHE_ALIGN,
956 if (!sctp_bucket_cachep)
957 goto err_bucket_cachep;
959 sctp_chunk_cachep = kmem_cache_create("sctp_chunk",
960 sizeof(struct sctp_chunk),
961 0, SLAB_HWCACHE_ALIGN,
963 if (!sctp_chunk_cachep)
964 goto err_chunk_cachep;
966 /* Allocate and initialise sctp mibs. */
967 status = init_sctp_mibs();
971 /* Initialize proc fs directory. */
974 /* Initialize object count debugging. */
975 sctp_dbg_objcnt_init();
977 /* Initialize the SCTP specific PF functions. */
978 sctp_register_pf(&sctp_pf_inet, PF_INET);
980 * 14. Suggested SCTP Protocol Parameter Values
982 /* The following protocol parameters are RECOMMENDED: */
983 /* RTO.Initial - 3 seconds */
984 sctp_rto_initial = SCTP_RTO_INITIAL;
985 /* RTO.Min - 1 second */
986 sctp_rto_min = SCTP_RTO_MIN;
987 /* RTO.Max - 60 seconds */
988 sctp_rto_max = SCTP_RTO_MAX;
989 /* RTO.Alpha - 1/8 */
990 sctp_rto_alpha = SCTP_RTO_ALPHA;
992 sctp_rto_beta = SCTP_RTO_BETA;
994 /* Valid.Cookie.Life - 60 seconds */
995 sctp_valid_cookie_life = 60 * HZ;
997 /* Whether Cookie Preservative is enabled(1) or not(0) */
998 sctp_cookie_preserve_enable = 1;
1001 sctp_max_burst = SCTP_MAX_BURST;
1003 /* Association.Max.Retrans - 10 attempts
1004 * Path.Max.Retrans - 5 attempts (per destination address)
1005 * Max.Init.Retransmits - 8 attempts
1007 sctp_max_retrans_association = 10;
1008 sctp_max_retrans_path = 5;
1009 sctp_max_retrans_init = 8;
1011 /* HB.interval - 30 seconds */
1012 sctp_hb_interval = 30 * HZ;
1014 /* Implementation specific variables. */
1016 /* Initialize default stream count setup information. */
1017 sctp_max_instreams = SCTP_DEFAULT_INSTREAMS;
1018 sctp_max_outstreams = SCTP_DEFAULT_OUTSTREAMS;
1020 /* Allocate and initialize the association hash table. */
1021 sctp_assoc_hashsize = 4096;
1022 sctp_assoc_hashbucket = (struct sctp_hashbucket *)
1023 kmalloc(4096 * sizeof(struct sctp_hashbucket), GFP_KERNEL);
1024 if (!sctp_assoc_hashbucket) {
1025 printk(KERN_ERR "SCTP: Failed association hash alloc.\n");
1027 goto err_ahash_alloc;
1029 for (i = 0; i < sctp_assoc_hashsize; i++) {
1030 sctp_assoc_hashbucket[i].lock = RW_LOCK_UNLOCKED;
1031 sctp_assoc_hashbucket[i].chain = NULL;
1034 /* Allocate and initialize the endpoint hash table. */
1035 sctp_ep_hashsize = 64;
1036 sctp_ep_hashbucket = (struct sctp_hashbucket *)
1037 kmalloc(64 * sizeof(struct sctp_hashbucket), GFP_KERNEL);
1038 if (!sctp_ep_hashbucket) {
1039 printk(KERN_ERR "SCTP: Failed endpoint_hash alloc.\n");
1041 goto err_ehash_alloc;
1044 for (i = 0; i < sctp_ep_hashsize; i++) {
1045 sctp_ep_hashbucket[i].lock = RW_LOCK_UNLOCKED;
1046 sctp_ep_hashbucket[i].chain = NULL;
1049 /* Allocate and initialize the SCTP port hash table. */
1050 sctp_port_hashsize = 4096;
1051 sctp_port_hashtable = (struct sctp_bind_hashbucket *)
1052 kmalloc(4096 * sizeof(struct sctp_bind_hashbucket),GFP_KERNEL);
1053 if (!sctp_port_hashtable) {
1054 printk(KERN_ERR "SCTP: Failed bind hash alloc.");
1056 goto err_bhash_alloc;
1059 sctp_port_alloc_lock = SPIN_LOCK_UNLOCKED;
1060 sctp_port_rover = sysctl_local_port_range[0] - 1;
1061 for (i = 0; i < sctp_port_hashsize; i++) {
1062 sctp_port_hashtable[i].lock = SPIN_LOCK_UNLOCKED;
1063 sctp_port_hashtable[i].chain = NULL;
1066 sctp_sysctl_register();
1068 INIT_LIST_HEAD(&sctp_address_families);
1069 sctp_register_af(&sctp_ipv4_specific);
1071 status = sctp_v6_init();
1075 /* Initialize the control inode/socket for handling OOTB packets. */
1076 if ((status = sctp_ctl_sock_init())) {
1078 "SCTP: Failed to initialize the SCTP control sock.\n");
1079 goto err_ctl_sock_init;
1082 /* Initialize the local address list. */
1083 INIT_LIST_HEAD(&sctp_local_addr_list);
1084 sctp_local_addr_lock = SPIN_LOCK_UNLOCKED;
1086 /* Register notifier for inet address additions/deletions. */
1087 register_inetaddr_notifier(&sctp_inetaddr_notifier);
1089 sctp_get_local_addr_list();
1091 __unsafe(THIS_MODULE);
1097 sctp_sysctl_unregister();
1098 list_del(&sctp_ipv4_specific.list);
1099 kfree(sctp_port_hashtable);
1101 kfree(sctp_ep_hashbucket);
1103 kfree(sctp_assoc_hashbucket);
1105 sctp_dbg_objcnt_exit();
1107 cleanup_sctp_mibs();
1109 kmem_cache_destroy(sctp_chunk_cachep);
1111 kmem_cache_destroy(sctp_bucket_cachep);
1113 inet_del_protocol(&sctp_protocol, IPPROTO_SCTP);
1114 inet_unregister_protosw(&sctp_seqpacket_protosw);
1115 inet_unregister_protosw(&sctp_stream_protosw);
1119 /* Exit handler for the SCTP protocol. */
1120 __exit void sctp_exit(void)
1122 /* BUG. This should probably do something useful like clean
1123 * up all the remaining associations and all that memory.
1126 /* Unregister notifier for inet address additions/deletions. */
1127 unregister_inetaddr_notifier(&sctp_inetaddr_notifier);
1129 /* Free the local address list. */
1130 sctp_free_local_addr_list();
1132 /* Free the control endpoint. */
1133 sock_release(sctp_ctl_socket);
1136 sctp_sysctl_unregister();
1137 list_del(&sctp_ipv4_specific.list);
1139 kfree(sctp_assoc_hashbucket);
1140 kfree(sctp_ep_hashbucket);
1141 kfree(sctp_port_hashtable);
1143 kmem_cache_destroy(sctp_chunk_cachep);
1144 kmem_cache_destroy(sctp_bucket_cachep);
1146 sctp_dbg_objcnt_exit();
1148 cleanup_sctp_mibs();
1150 inet_del_protocol(&sctp_protocol, IPPROTO_SCTP);
1151 inet_unregister_protosw(&sctp_seqpacket_protosw);
1152 inet_unregister_protosw(&sctp_stream_protosw);
1155 module_init(sctp_init);
1156 module_exit(sctp_exit);
1158 MODULE_AUTHOR("Linux Kernel SCTP developers <lksctp-developers@lists.sourceforge.net>");
1159 MODULE_DESCRIPTION("Support for the SCTP protocol (RFC2960)");
1160 MODULE_LICENSE("GPL");