1 /* 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
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 struct idr sctp_assocs_id;
68 spinlock_t sctp_assocs_id_lock = SPIN_LOCK_UNLOCKED;
70 /* This is the global socket data structure used for responding to
71 * the Out-of-the-blue (OOTB) packets. A control sock will be created
72 * for this socket at the initialization time.
74 static struct socket *sctp_ctl_socket;
76 static struct sctp_pf *sctp_pf_inet6_specific;
77 static struct sctp_pf *sctp_pf_inet_specific;
78 static struct sctp_af *sctp_af_v4_specific;
79 static struct sctp_af *sctp_af_v6_specific;
81 kmem_cache_t *sctp_chunk_cachep;
82 kmem_cache_t *sctp_bucket_cachep;
84 extern struct net_proto_family inet_family_ops;
86 extern int sctp_snmp_proc_init(void);
87 extern int sctp_snmp_proc_exit(void);
88 extern int sctp_eps_proc_init(void);
89 extern int sctp_eps_proc_exit(void);
90 extern int sctp_assocs_proc_init(void);
91 extern int sctp_assocs_proc_exit(void);
93 /* Return the address of the control sock. */
94 struct sock *sctp_get_ctl_sock(void)
96 return sctp_ctl_socket->sk;
99 /* Set up the proc fs entry for the SCTP protocol. */
100 __init int sctp_proc_init(void)
102 if (!proc_net_sctp) {
103 struct proc_dir_entry *ent;
104 ent = proc_mkdir("net/sctp", 0);
106 ent->owner = THIS_MODULE;
112 if (sctp_snmp_proc_init())
114 if (sctp_eps_proc_init())
116 if (sctp_assocs_proc_init())
125 /* Clean up the proc fs entry for the SCTP protocol.
126 * Note: Do not make this __exit as it is used in the init error
129 void sctp_proc_exit(void)
131 sctp_snmp_proc_exit();
132 sctp_eps_proc_exit();
133 sctp_assocs_proc_exit();
136 proc_net_sctp = NULL;
137 remove_proc_entry("net/sctp", 0);
141 /* Private helper to extract ipv4 address and stash them in
142 * the protocol structure.
144 static void sctp_v4_copy_addrlist(struct list_head *addrlist,
145 struct net_device *dev)
147 struct in_device *in_dev;
148 struct in_ifaddr *ifa;
149 struct sctp_sockaddr_entry *addr;
151 read_lock(&inetdev_lock);
152 if ((in_dev = __in_dev_get(dev)) == NULL) {
153 read_unlock(&inetdev_lock);
157 read_lock(&in_dev->lock);
158 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
159 /* Add the address to the local list. */
160 addr = t_new(struct sctp_sockaddr_entry, GFP_ATOMIC);
162 addr->a.v4.sin_family = AF_INET;
163 addr->a.v4.sin_port = 0;
164 addr->a.v4.sin_addr.s_addr = ifa->ifa_local;
165 list_add_tail(&addr->list, addrlist);
169 read_unlock(&in_dev->lock);
170 read_unlock(&inetdev_lock);
173 /* Extract our IP addresses from the system and stash them in the
174 * protocol structure.
176 static void __sctp_get_local_addr_list(void)
178 struct net_device *dev;
179 struct list_head *pos;
182 read_lock(&dev_base_lock);
183 for (dev = dev_base; dev; dev = dev->next) {
184 __list_for_each(pos, &sctp_address_families) {
185 af = list_entry(pos, struct sctp_af, list);
186 af->copy_addrlist(&sctp_local_addr_list, dev);
189 read_unlock(&dev_base_lock);
192 static void sctp_get_local_addr_list(void)
196 sctp_spin_lock_irqsave(&sctp_local_addr_lock, flags);
197 __sctp_get_local_addr_list();
198 sctp_spin_unlock_irqrestore(&sctp_local_addr_lock, flags);
201 /* Free the existing local addresses. */
202 static void __sctp_free_local_addr_list(void)
204 struct sctp_sockaddr_entry *addr;
205 struct list_head *pos, *temp;
207 list_for_each_safe(pos, temp, &sctp_local_addr_list) {
208 addr = list_entry(pos, struct sctp_sockaddr_entry, list);
214 /* Free the existing local addresses. */
215 static void sctp_free_local_addr_list(void)
219 sctp_spin_lock_irqsave(&sctp_local_addr_lock, flags);
220 __sctp_free_local_addr_list();
221 sctp_spin_unlock_irqrestore(&sctp_local_addr_lock, flags);
224 /* Copy the local addresses which are valid for 'scope' into 'bp'. */
225 int sctp_copy_local_addr_list(struct sctp_bind_addr *bp, sctp_scope_t scope,
226 int gfp, int copy_flags)
228 struct sctp_sockaddr_entry *addr;
230 struct list_head *pos;
233 sctp_spin_lock_irqsave(&sctp_local_addr_lock, flags);
234 list_for_each(pos, &sctp_local_addr_list) {
235 addr = list_entry(pos, struct sctp_sockaddr_entry, list);
236 if (sctp_in_scope(&addr->a, scope)) {
237 /* Now that the address is in scope, check to see if
238 * the address type is really supported by the local
239 * sock as well as the remote peer.
241 if ((((AF_INET == addr->a.sa.sa_family) &&
242 (copy_flags & SCTP_ADDR4_PEERSUPP))) ||
243 (((AF_INET6 == addr->a.sa.sa_family) &&
244 (copy_flags & SCTP_ADDR6_ALLOWED) &&
245 (copy_flags & SCTP_ADDR6_PEERSUPP)))) {
246 error = sctp_add_bind_addr(bp, &addr->a,
255 sctp_spin_unlock_irqrestore(&sctp_local_addr_lock, flags);
259 /* Initialize a sctp_addr from in incoming skb. */
260 static void sctp_v4_from_skb(union sctp_addr *addr, struct sk_buff *skb,
267 port = &addr->v4.sin_port;
268 addr->v4.sin_family = AF_INET;
270 sh = (struct sctphdr *) skb->h.raw;
272 *port = ntohs(sh->source);
273 from = &skb->nh.iph->saddr;
275 *port = ntohs(sh->dest);
276 from = &skb->nh.iph->daddr;
278 memcpy(&addr->v4.sin_addr.s_addr, from, sizeof(struct in_addr));
281 /* Initialize an sctp_addr from a socket. */
282 static void sctp_v4_from_sk(union sctp_addr *addr, struct sock *sk)
284 addr->v4.sin_family = AF_INET;
285 addr->v4.sin_port = inet_sk(sk)->num;
286 addr->v4.sin_addr.s_addr = inet_sk(sk)->rcv_saddr;
289 /* Initialize sk->sk_rcv_saddr from sctp_addr. */
290 static void sctp_v4_to_sk_saddr(union sctp_addr *addr, struct sock *sk)
292 inet_sk(sk)->rcv_saddr = addr->v4.sin_addr.s_addr;
295 /* Initialize sk->sk_daddr from sctp_addr. */
296 static void sctp_v4_to_sk_daddr(union sctp_addr *addr, struct sock *sk)
298 inet_sk(sk)->daddr = addr->v4.sin_addr.s_addr;
301 /* Initialize a sctp_addr from an address parameter. */
302 static void sctp_v4_from_addr_param(union sctp_addr *addr,
303 union sctp_addr_param *param,
306 addr->v4.sin_family = AF_INET;
307 addr->v4.sin_port = port;
308 addr->v4.sin_addr.s_addr = param->v4.addr.s_addr;
311 /* Initialize an address parameter from a sctp_addr and return the length
312 * of the address parameter.
314 static int sctp_v4_to_addr_param(const union sctp_addr *addr,
315 union sctp_addr_param *param)
317 int length = sizeof(sctp_ipv4addr_param_t);
319 param->v4.param_hdr.type = SCTP_PARAM_IPV4_ADDRESS;
320 param->v4.param_hdr.length = ntohs(length);
321 param->v4.addr.s_addr = addr->v4.sin_addr.s_addr;
326 /* Initialize a sctp_addr from a dst_entry. */
327 static void sctp_v4_dst_saddr(union sctp_addr *saddr, struct dst_entry *dst,
330 struct rtable *rt = (struct rtable *)dst;
331 saddr->v4.sin_family = AF_INET;
332 saddr->v4.sin_port = port;
333 saddr->v4.sin_addr.s_addr = rt->rt_src;
336 /* Compare two addresses exactly. */
337 static int sctp_v4_cmp_addr(const union sctp_addr *addr1,
338 const union sctp_addr *addr2)
340 if (addr1->sa.sa_family != addr2->sa.sa_family)
342 if (addr1->v4.sin_port != addr2->v4.sin_port)
344 if (addr1->v4.sin_addr.s_addr != addr2->v4.sin_addr.s_addr)
350 /* Initialize addr struct to INADDR_ANY. */
351 static void sctp_v4_inaddr_any(union sctp_addr *addr, unsigned short port)
353 addr->v4.sin_family = AF_INET;
354 addr->v4.sin_addr.s_addr = INADDR_ANY;
355 addr->v4.sin_port = port;
358 /* Is this a wildcard address? */
359 static int sctp_v4_is_any(const union sctp_addr *addr)
361 return INADDR_ANY == addr->v4.sin_addr.s_addr;
364 /* This function checks if the address is a valid address to be used for
368 * Return 0 - If the address is a non-unicast or an illegal address.
369 * Return 1 - If the address is a unicast.
371 static int sctp_v4_addr_valid(union sctp_addr *addr, struct sctp_opt *sp)
373 /* Is this a non-unicast address or a unusable SCTP address? */
374 if (IS_IPV4_UNUSABLE_ADDRESS(&addr->v4.sin_addr.s_addr))
380 /* Should this be available for binding? */
381 static int sctp_v4_available(union sctp_addr *addr, struct sctp_opt *sp)
383 int ret = inet_addr_type(addr->v4.sin_addr.s_addr);
385 /* FIXME: ip_nonlocal_bind sysctl support. */
387 if (addr->v4.sin_addr.s_addr != INADDR_ANY && ret != RTN_LOCAL)
392 /* Checking the loopback, private and other address scopes as defined in
393 * RFC 1918. The IPv4 scoping is based on the draft for SCTP IPv4
394 * scoping <draft-stewart-tsvwg-sctp-ipv4-00.txt>.
396 * Level 0 - unusable SCTP addresses
397 * Level 1 - loopback address
398 * Level 2 - link-local addresses
399 * Level 3 - private addresses.
400 * Level 4 - global addresses
401 * For INIT and INIT-ACK address list, let L be the level of
402 * of requested destination address, sender and receiver
403 * SHOULD include all of its addresses with level greater
404 * than or equal to L.
406 static sctp_scope_t sctp_v4_scope(union sctp_addr *addr)
410 /* Should IPv4 scoping be a sysctl configurable option
411 * so users can turn it off (default on) for certain
412 * unconventional networking environments?
415 /* Check for unusable SCTP addresses. */
416 if (IS_IPV4_UNUSABLE_ADDRESS(&addr->v4.sin_addr.s_addr)) {
417 retval = SCTP_SCOPE_UNUSABLE;
418 } else if (LOOPBACK(addr->v4.sin_addr.s_addr)) {
419 retval = SCTP_SCOPE_LOOPBACK;
420 } else if (IS_IPV4_LINK_ADDRESS(&addr->v4.sin_addr.s_addr)) {
421 retval = SCTP_SCOPE_LINK;
422 } else if (IS_IPV4_PRIVATE_ADDRESS(&addr->v4.sin_addr.s_addr)) {
423 retval = SCTP_SCOPE_PRIVATE;
425 retval = SCTP_SCOPE_GLOBAL;
431 /* Returns a valid dst cache entry for the given source and destination ip
432 * addresses. If an association is passed, trys to get a dst entry with a
433 * source address that matches an address in the bind address list.
435 struct dst_entry *sctp_v4_get_dst(struct sctp_association *asoc,
436 union sctp_addr *daddr,
437 union sctp_addr *saddr)
441 struct sctp_bind_addr *bp;
443 struct sctp_sockaddr_entry *laddr;
444 struct list_head *pos;
445 struct dst_entry *dst = NULL;
446 union sctp_addr dst_saddr;
448 memset(&fl, 0x0, sizeof(struct flowi));
449 fl.fl4_dst = daddr->v4.sin_addr.s_addr;
450 fl.proto = IPPROTO_SCTP;
452 fl.fl4_tos = RT_CONN_FLAGS(asoc->base.sk);
453 fl.oif = asoc->base.sk->sk_bound_dev_if;
456 fl.fl4_src = saddr->v4.sin_addr.s_addr;
458 SCTP_DEBUG_PRINTK("%s: DST:%u.%u.%u.%u, SRC:%u.%u.%u.%u - ",
459 __FUNCTION__, NIPQUAD(fl.fl4_dst),
460 NIPQUAD(fl.fl4_src));
462 if (!ip_route_output_key(&rt, &fl)) {
466 /* If there is no association or if a source address is passed, no
467 * more validation is required.
472 bp = &asoc->base.bind_addr;
473 addr_lock = &asoc->base.addr_lock;
476 /* Walk through the bind address list and look for a bind
477 * address that matches the source address of the returned dst.
479 sctp_read_lock(addr_lock);
480 list_for_each(pos, &bp->address_list) {
481 laddr = list_entry(pos, struct sctp_sockaddr_entry,
483 sctp_v4_dst_saddr(&dst_saddr, dst, bp->port);
484 if (sctp_v4_cmp_addr(&dst_saddr, &laddr->a))
487 sctp_read_unlock(addr_lock);
489 /* None of the bound addresses match the source address of the
490 * dst. So release it.
496 /* Walk through the bind address list and try to get a dst that
497 * matches a bind address as the source address.
499 sctp_read_lock(addr_lock);
500 list_for_each(pos, &bp->address_list) {
501 laddr = list_entry(pos, struct sctp_sockaddr_entry, list);
503 if (AF_INET == laddr->a.sa.sa_family) {
504 fl.fl4_src = laddr->a.v4.sin_addr.s_addr;
505 if (!ip_route_output_key(&rt, &fl)) {
513 sctp_read_unlock(addr_lock);
516 SCTP_DEBUG_PRINTK("rt_dst:%u.%u.%u.%u, rt_src:%u.%u.%u.%u\n",
517 NIPQUAD(rt->rt_dst), NIPQUAD(rt->rt_src));
519 SCTP_DEBUG_PRINTK("NO ROUTE\n");
524 /* For v4, the source address is cached in the route entry(dst). So no need
525 * to cache it separately and hence this is an empty routine.
527 void sctp_v4_get_saddr(struct sctp_association *asoc,
528 struct dst_entry *dst,
529 union sctp_addr *daddr,
530 union sctp_addr *saddr)
532 struct rtable *rt = (struct rtable *)dst;
535 saddr->v4.sin_family = AF_INET;
536 saddr->v4.sin_port = asoc->base.bind_addr.port;
537 saddr->v4.sin_addr.s_addr = rt->rt_src;
541 /* What interface did this skb arrive on? */
542 static int sctp_v4_skb_iif(const struct sk_buff *skb)
544 return ((struct rtable *)skb->dst)->rt_iif;
547 /* Was this packet marked by Explicit Congestion Notification? */
548 static int sctp_v4_is_ce(const struct sk_buff *skb)
550 return INET_ECN_is_ce(skb->nh.iph->tos);
553 /* Create and initialize a new sk for the socket returned by accept(). */
554 struct sock *sctp_v4_create_accept_sk(struct sock *sk,
555 struct sctp_association *asoc)
558 struct inet_opt *inet = inet_sk(sk);
559 struct inet_opt *newinet;
561 newsk = sk_alloc(PF_INET, GFP_KERNEL, sizeof(struct sctp_sock),
566 sock_init_data(NULL, newsk);
567 sk_set_owner(newsk, THIS_MODULE);
569 newsk->sk_type = SOCK_STREAM;
571 newsk->sk_prot = sk->sk_prot;
572 newsk->sk_no_check = sk->sk_no_check;
573 newsk->sk_reuse = sk->sk_reuse;
574 newsk->sk_shutdown = sk->sk_shutdown;
576 newsk->sk_destruct = inet_sock_destruct;
577 newsk->sk_zapped = 0;
578 newsk->sk_family = PF_INET;
579 newsk->sk_protocol = IPPROTO_SCTP;
580 newsk->sk_backlog_rcv = sk->sk_prot->backlog_rcv;
582 newinet = inet_sk(newsk);
584 /* Initialize sk's sport, dport, rcv_saddr and daddr for
585 * getsockname() and getpeername()
587 newinet->sport = inet->sport;
588 newinet->saddr = inet->saddr;
589 newinet->rcv_saddr = inet->rcv_saddr;
590 newinet->dport = htons(asoc->peer.port);
591 newinet->daddr = asoc->peer.primary_addr.v4.sin_addr.s_addr;
592 newinet->pmtudisc = inet->pmtudisc;
595 newinet->uc_ttl = -1;
596 newinet->mc_loop = 1;
598 newinet->mc_index = 0;
599 newinet->mc_list = NULL;
601 #ifdef INET_REFCNT_DEBUG
602 atomic_inc(&inet_sock_nr);
605 if (newsk->sk_prot->init(newsk)) {
606 inet_sock_release(newsk);
614 /* Map address, empty for v4 family */
615 static void sctp_v4_addr_v4map(struct sctp_opt *sp, union sctp_addr *addr)
620 /* Dump the v4 addr to the seq file. */
621 static void sctp_v4_seq_dump_addr(struct seq_file *seq, union sctp_addr *addr)
623 seq_printf(seq, "%d.%d.%d.%d ", NIPQUAD(addr->v4.sin_addr));
626 /* Event handler for inet address addition/deletion events.
627 * Basically, whenever there is an event, we re-build our local address list.
629 static int sctp_inetaddr_event(struct notifier_block *this, unsigned long ev,
634 sctp_spin_lock_irqsave(&sctp_local_addr_lock, flags);
635 __sctp_free_local_addr_list();
636 __sctp_get_local_addr_list();
637 sctp_spin_unlock_irqrestore(&sctp_local_addr_lock, flags);
643 * Initialize the control inode/socket with a control endpoint data
644 * structure. This endpoint is reserved exclusively for the OOTB processing.
646 int sctp_ctl_sock_init(void)
651 if (sctp_get_pf_specific(PF_INET6))
656 err = sock_create_kern(family, SOCK_SEQPACKET, IPPROTO_SCTP,
660 "SCTP: Failed to create the SCTP control socket.\n");
663 sctp_ctl_socket->sk->sk_allocation = GFP_ATOMIC;
664 inet_sk(sctp_ctl_socket->sk)->uc_ttl = -1;
669 /* Register address family specific functions. */
670 int sctp_register_af(struct sctp_af *af)
672 switch (af->sa_family) {
674 if (sctp_af_v4_specific)
676 sctp_af_v4_specific = af;
679 if (sctp_af_v6_specific)
681 sctp_af_v6_specific = af;
687 INIT_LIST_HEAD(&af->list);
688 list_add_tail(&af->list, &sctp_address_families);
692 /* Get the table of functions for manipulating a particular address
695 struct sctp_af *sctp_get_af_specific(sa_family_t family)
699 return sctp_af_v4_specific;
701 return sctp_af_v6_specific;
707 /* Common code to initialize a AF_INET msg_name. */
708 static void sctp_inet_msgname(char *msgname, int *addr_len)
710 struct sockaddr_in *sin;
712 sin = (struct sockaddr_in *)msgname;
713 *addr_len = sizeof(struct sockaddr_in);
714 sin->sin_family = AF_INET;
715 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
718 /* Copy the primary address of the peer primary address as the msg_name. */
719 static void sctp_inet_event_msgname(struct sctp_ulpevent *event, char *msgname,
722 struct sockaddr_in *sin, *sinfrom;
725 struct sctp_association *asoc;
728 sctp_inet_msgname(msgname, addr_len);
729 sin = (struct sockaddr_in *)msgname;
730 sinfrom = &asoc->peer.primary_addr.v4;
731 sin->sin_port = htons(asoc->peer.port);
732 sin->sin_addr.s_addr = sinfrom->sin_addr.s_addr;
736 /* Initialize and copy out a msgname from an inbound skb. */
737 static void sctp_inet_skb_msgname(struct sk_buff *skb, char *msgname, int *len)
740 struct sockaddr_in *sin;
743 sctp_inet_msgname(msgname, len);
744 sin = (struct sockaddr_in *)msgname;
745 sh = (struct sctphdr *)skb->h.raw;
746 sin->sin_port = sh->source;
747 sin->sin_addr.s_addr = skb->nh.iph->saddr;
751 /* Do we support this AF? */
752 static int sctp_inet_af_supported(sa_family_t family, struct sctp_opt *sp)
754 /* PF_INET only supports AF_INET addresses. */
755 return (AF_INET == family);
758 /* Address matching with wildcards allowed. */
759 static int sctp_inet_cmp_addr(const union sctp_addr *addr1,
760 const union sctp_addr *addr2,
761 struct sctp_opt *opt)
763 /* PF_INET only supports AF_INET addresses. */
764 if (addr1->sa.sa_family != addr2->sa.sa_family)
766 if (INADDR_ANY == addr1->v4.sin_addr.s_addr ||
767 INADDR_ANY == addr2->v4.sin_addr.s_addr)
769 if (addr1->v4.sin_addr.s_addr == addr2->v4.sin_addr.s_addr)
775 /* Verify that provided sockaddr looks bindable. Common verification has
776 * already been taken care of.
778 static int sctp_inet_bind_verify(struct sctp_opt *opt, union sctp_addr *addr)
780 return sctp_v4_available(addr, opt);
783 /* Verify that sockaddr looks sendable. Common verification has already
784 * been taken care of.
786 static int sctp_inet_send_verify(struct sctp_opt *opt, union sctp_addr *addr)
791 /* Fill in Supported Address Type information for INIT and INIT-ACK
792 * chunks. Returns number of addresses supported.
794 static int sctp_inet_supported_addrs(const struct sctp_opt *opt,
797 types[0] = SCTP_PARAM_IPV4_ADDRESS;
801 /* Wrapper routine that calls the ip transmit routine. */
802 static inline int sctp_v4_xmit(struct sk_buff *skb,
803 struct sctp_transport *transport, int ipfragok)
805 SCTP_DEBUG_PRINTK("%s: skb:%p, len:%d, "
806 "src:%u.%u.%u.%u, dst:%u.%u.%u.%u\n",
807 __FUNCTION__, skb, skb->len,
808 NIPQUAD(((struct rtable *)skb->dst)->rt_src),
809 NIPQUAD(((struct rtable *)skb->dst)->rt_dst));
811 SCTP_INC_STATS(SctpOutSCTPPacks);
812 return ip_queue_xmit(skb, ipfragok);
815 struct sctp_af sctp_ipv4_specific;
817 static struct sctp_pf sctp_pf_inet = {
818 .event_msgname = sctp_inet_event_msgname,
819 .skb_msgname = sctp_inet_skb_msgname,
820 .af_supported = sctp_inet_af_supported,
821 .cmp_addr = sctp_inet_cmp_addr,
822 .bind_verify = sctp_inet_bind_verify,
823 .send_verify = sctp_inet_send_verify,
824 .supported_addrs = sctp_inet_supported_addrs,
825 .create_accept_sk = sctp_v4_create_accept_sk,
826 .addr_v4map = sctp_v4_addr_v4map,
827 .af = &sctp_ipv4_specific,
830 /* Notifier for inetaddr addition/deletion events. */
831 struct notifier_block sctp_inetaddr_notifier = {
832 .notifier_call = sctp_inetaddr_event,
835 /* Socket operations. */
836 struct proto_ops inet_seqpacket_ops = {
838 .owner = THIS_MODULE,
839 .release = inet_release, /* Needs to be wrapped... */
841 .connect = inet_dgram_connect,
842 .socketpair = sock_no_socketpair,
843 .accept = inet_accept,
844 .getname = inet_getname, /* Semantics are different. */
847 .listen = sctp_inet_listen,
848 .shutdown = inet_shutdown, /* Looks harmless. */
849 .setsockopt = inet_setsockopt, /* IP_SOL IP_OPTION is a problem. */
850 .getsockopt = inet_getsockopt,
851 .sendmsg = inet_sendmsg,
852 .recvmsg = inet_recvmsg,
853 .mmap = sock_no_mmap,
854 .sendpage = sock_no_sendpage,
857 /* Registration with AF_INET family. */
858 static struct inet_protosw sctp_seqpacket_protosw = {
859 .type = SOCK_SEQPACKET,
860 .protocol = IPPROTO_SCTP,
862 .ops = &inet_seqpacket_ops,
865 .flags = SCTP_PROTOSW_FLAG
867 static struct inet_protosw sctp_stream_protosw = {
869 .protocol = IPPROTO_SCTP,
871 .ops = &inet_seqpacket_ops,
874 .flags = SCTP_PROTOSW_FLAG
877 /* Register with IP layer. */
878 static struct inet_protocol sctp_protocol = {
880 .err_handler = sctp_v4_err,
884 /* IPv4 address related functions. */
885 struct sctp_af sctp_ipv4_specific = {
886 .sctp_xmit = sctp_v4_xmit,
887 .setsockopt = ip_setsockopt,
888 .getsockopt = ip_getsockopt,
889 .get_dst = sctp_v4_get_dst,
890 .get_saddr = sctp_v4_get_saddr,
891 .copy_addrlist = sctp_v4_copy_addrlist,
892 .from_skb = sctp_v4_from_skb,
893 .from_sk = sctp_v4_from_sk,
894 .to_sk_saddr = sctp_v4_to_sk_saddr,
895 .to_sk_daddr = sctp_v4_to_sk_daddr,
896 .from_addr_param= sctp_v4_from_addr_param,
897 .to_addr_param = sctp_v4_to_addr_param,
898 .dst_saddr = sctp_v4_dst_saddr,
899 .cmp_addr = sctp_v4_cmp_addr,
900 .addr_valid = sctp_v4_addr_valid,
901 .inaddr_any = sctp_v4_inaddr_any,
902 .is_any = sctp_v4_is_any,
903 .available = sctp_v4_available,
904 .scope = sctp_v4_scope,
905 .skb_iif = sctp_v4_skb_iif,
906 .is_ce = sctp_v4_is_ce,
907 .seq_dump_addr = sctp_v4_seq_dump_addr,
908 .net_header_len = sizeof(struct iphdr),
909 .sockaddr_len = sizeof(struct sockaddr_in),
910 .sa_family = AF_INET,
913 struct sctp_pf *sctp_get_pf_specific(sa_family_t family) {
917 return sctp_pf_inet_specific;
919 return sctp_pf_inet6_specific;
925 /* Register the PF specific function table. */
926 int sctp_register_pf(struct sctp_pf *pf, sa_family_t family)
930 if (sctp_pf_inet_specific)
932 sctp_pf_inet_specific = pf;
935 if (sctp_pf_inet6_specific)
937 sctp_pf_inet6_specific = pf;
945 static int __init init_sctp_mibs(void)
947 sctp_statistics[0] = alloc_percpu(struct sctp_mib);
948 if (!sctp_statistics[0])
950 sctp_statistics[1] = alloc_percpu(struct sctp_mib);
951 if (!sctp_statistics[1]) {
952 free_percpu(sctp_statistics[0]);
959 static void cleanup_sctp_mibs(void)
961 free_percpu(sctp_statistics[0]);
962 free_percpu(sctp_statistics[1]);
965 /* Initialize the universe into something sensible. */
966 __init int sctp_init(void)
973 /* SCTP_DEBUG sanity check. */
974 if (!sctp_sanity_check())
977 /* Add SCTP to inet_protos hash table. */
978 if (inet_add_protocol(&sctp_protocol, IPPROTO_SCTP) < 0)
981 /* Add SCTP(TCP and UDP style) to inetsw linked list. */
982 inet_register_protosw(&sctp_seqpacket_protosw);
983 inet_register_protosw(&sctp_stream_protosw);
985 /* Allocate a cache pools. */
986 sctp_bucket_cachep = kmem_cache_create("sctp_bind_bucket",
987 sizeof(struct sctp_bind_bucket),
988 0, SLAB_HWCACHE_ALIGN,
991 if (!sctp_bucket_cachep)
992 goto err_bucket_cachep;
994 sctp_chunk_cachep = kmem_cache_create("sctp_chunk",
995 sizeof(struct sctp_chunk),
996 0, SLAB_HWCACHE_ALIGN,
998 if (!sctp_chunk_cachep)
999 goto err_chunk_cachep;
1001 /* Allocate and initialise sctp mibs. */
1002 status = init_sctp_mibs();
1006 /* Initialize proc fs directory. */
1007 status = sctp_proc_init();
1011 /* Initialize object count debugging. */
1012 sctp_dbg_objcnt_init();
1014 /* Initialize the SCTP specific PF functions. */
1015 sctp_register_pf(&sctp_pf_inet, PF_INET);
1017 * 14. Suggested SCTP Protocol Parameter Values
1019 /* The following protocol parameters are RECOMMENDED: */
1020 /* RTO.Initial - 3 seconds */
1021 sctp_rto_initial = SCTP_RTO_INITIAL;
1022 /* RTO.Min - 1 second */
1023 sctp_rto_min = SCTP_RTO_MIN;
1024 /* RTO.Max - 60 seconds */
1025 sctp_rto_max = SCTP_RTO_MAX;
1026 /* RTO.Alpha - 1/8 */
1027 sctp_rto_alpha = SCTP_RTO_ALPHA;
1028 /* RTO.Beta - 1/4 */
1029 sctp_rto_beta = SCTP_RTO_BETA;
1031 /* Valid.Cookie.Life - 60 seconds */
1032 sctp_valid_cookie_life = 60 * HZ;
1034 /* Whether Cookie Preservative is enabled(1) or not(0) */
1035 sctp_cookie_preserve_enable = 1;
1038 sctp_max_burst = SCTP_MAX_BURST;
1040 /* Association.Max.Retrans - 10 attempts
1041 * Path.Max.Retrans - 5 attempts (per destination address)
1042 * Max.Init.Retransmits - 8 attempts
1044 sctp_max_retrans_association = 10;
1045 sctp_max_retrans_path = 5;
1046 sctp_max_retrans_init = 8;
1048 /* HB.interval - 30 seconds */
1049 sctp_hb_interval = 30 * HZ;
1051 /* Implementation specific variables. */
1053 /* Initialize default stream count setup information. */
1054 sctp_max_instreams = SCTP_DEFAULT_INSTREAMS;
1055 sctp_max_outstreams = SCTP_DEFAULT_OUTSTREAMS;
1057 /* Initialize handle used for association ids. */
1058 idr_init(&sctp_assocs_id);
1060 /* Size and allocate the association hash table.
1061 * The methodology is similar to that of the tcp hash tables.
1063 if (num_physpages >= (128 * 1024))
1064 goal = num_physpages >> (22 - PAGE_SHIFT);
1066 goal = num_physpages >> (24 - PAGE_SHIFT);
1068 for (order = 0; (1UL << order) < goal; order++)
1072 sctp_assoc_hashsize = (1UL << order) * PAGE_SIZE /
1073 sizeof(struct sctp_hashbucket);
1074 if ((sctp_assoc_hashsize > (64 * 1024)) && order > 0)
1076 sctp_assoc_hashtable = (struct sctp_hashbucket *)
1077 __get_free_pages(GFP_ATOMIC, order);
1078 } while (!sctp_assoc_hashtable && --order > 0);
1079 if (!sctp_assoc_hashtable) {
1080 printk(KERN_ERR "SCTP: Failed association hash alloc.\n");
1082 goto err_ahash_alloc;
1084 for (i = 0; i < sctp_assoc_hashsize; i++) {
1085 sctp_assoc_hashtable[i].lock = RW_LOCK_UNLOCKED;
1086 sctp_assoc_hashtable[i].chain = NULL;
1089 /* Allocate and initialize the endpoint hash table. */
1090 sctp_ep_hashsize = 64;
1091 sctp_ep_hashtable = (struct sctp_hashbucket *)
1092 kmalloc(64 * sizeof(struct sctp_hashbucket), GFP_KERNEL);
1093 if (!sctp_ep_hashtable) {
1094 printk(KERN_ERR "SCTP: Failed endpoint_hash alloc.\n");
1096 goto err_ehash_alloc;
1098 for (i = 0; i < sctp_ep_hashsize; i++) {
1099 sctp_ep_hashtable[i].lock = RW_LOCK_UNLOCKED;
1100 sctp_ep_hashtable[i].chain = NULL;
1103 /* Allocate and initialize the SCTP port hash table. */
1105 sctp_port_hashsize = (1UL << order) * PAGE_SIZE /
1106 sizeof(struct sctp_bind_hashbucket);
1107 if ((sctp_port_hashsize > (64 * 1024)) && order > 0)
1109 sctp_port_hashtable = (struct sctp_bind_hashbucket *)
1110 __get_free_pages(GFP_ATOMIC, order);
1111 } while (!sctp_port_hashtable && --order > 0);
1112 if (!sctp_port_hashtable) {
1113 printk(KERN_ERR "SCTP: Failed bind hash alloc.");
1115 goto err_bhash_alloc;
1117 for (i = 0; i < sctp_port_hashsize; i++) {
1118 sctp_port_hashtable[i].lock = SPIN_LOCK_UNLOCKED;
1119 sctp_port_hashtable[i].chain = NULL;
1122 sctp_port_alloc_lock = SPIN_LOCK_UNLOCKED;
1123 sctp_port_rover = sysctl_local_port_range[0] - 1;
1125 printk(KERN_INFO "SCTP: Hash tables configured "
1126 "(established %d bind %d)\n",
1127 sctp_assoc_hashsize, sctp_port_hashsize);
1129 /* Disable ADDIP by default. */
1130 sctp_addip_enable = 0;
1132 /* Enable PR-SCTP by default. */
1133 sctp_prsctp_enable = 1;
1135 sctp_sysctl_register();
1137 INIT_LIST_HEAD(&sctp_address_families);
1138 sctp_register_af(&sctp_ipv4_specific);
1140 status = sctp_v6_init();
1144 /* Initialize the control inode/socket for handling OOTB packets. */
1145 if ((status = sctp_ctl_sock_init())) {
1147 "SCTP: Failed to initialize the SCTP control sock.\n");
1148 goto err_ctl_sock_init;
1151 /* Initialize the local address list. */
1152 INIT_LIST_HEAD(&sctp_local_addr_list);
1153 sctp_local_addr_lock = SPIN_LOCK_UNLOCKED;
1155 /* Register notifier for inet address additions/deletions. */
1156 register_inetaddr_notifier(&sctp_inetaddr_notifier);
1158 sctp_get_local_addr_list();
1160 __unsafe(THIS_MODULE);
1166 sctp_sysctl_unregister();
1167 list_del(&sctp_ipv4_specific.list);
1168 free_pages((unsigned long)sctp_port_hashtable,
1169 get_order(sctp_port_hashsize *
1170 sizeof(struct sctp_bind_hashbucket)));
1172 kfree(sctp_ep_hashtable);
1174 free_pages((unsigned long)sctp_assoc_hashtable,
1175 get_order(sctp_assoc_hashsize *
1176 sizeof(struct sctp_hashbucket)));
1178 sctp_dbg_objcnt_exit();
1181 cleanup_sctp_mibs();
1183 kmem_cache_destroy(sctp_chunk_cachep);
1185 kmem_cache_destroy(sctp_bucket_cachep);
1187 inet_del_protocol(&sctp_protocol, IPPROTO_SCTP);
1188 inet_unregister_protosw(&sctp_seqpacket_protosw);
1189 inet_unregister_protosw(&sctp_stream_protosw);
1193 /* Exit handler for the SCTP protocol. */
1194 __exit void sctp_exit(void)
1196 /* BUG. This should probably do something useful like clean
1197 * up all the remaining associations and all that memory.
1200 /* Unregister notifier for inet address additions/deletions. */
1201 unregister_inetaddr_notifier(&sctp_inetaddr_notifier);
1203 /* Free the local address list. */
1204 sctp_free_local_addr_list();
1206 /* Free the control endpoint. */
1207 sock_release(sctp_ctl_socket);
1210 sctp_sysctl_unregister();
1211 list_del(&sctp_ipv4_specific.list);
1213 free_pages((unsigned long)sctp_assoc_hashtable,
1214 get_order(sctp_assoc_hashsize *
1215 sizeof(struct sctp_hashbucket)));
1216 kfree(sctp_ep_hashtable);
1217 free_pages((unsigned long)sctp_port_hashtable,
1218 get_order(sctp_port_hashsize *
1219 sizeof(struct sctp_bind_hashbucket)));
1221 kmem_cache_destroy(sctp_chunk_cachep);
1222 kmem_cache_destroy(sctp_bucket_cachep);
1224 sctp_dbg_objcnt_exit();
1226 cleanup_sctp_mibs();
1228 inet_del_protocol(&sctp_protocol, IPPROTO_SCTP);
1229 inet_unregister_protosw(&sctp_seqpacket_protosw);
1230 inet_unregister_protosw(&sctp_stream_protosw);
1233 module_init(sctp_init);
1234 module_exit(sctp_exit);
1236 MODULE_AUTHOR("Linux Kernel SCTP developers <lksctp-developers@lists.sourceforge.net>");
1237 MODULE_DESCRIPTION("Support for the SCTP protocol (RFC2960)");
1238 MODULE_LICENSE("GPL");