Linux 2.6.21.1
[linux/fpc-iii.git] / net / sctp / protocol.c
blobe17a823ca90fcecc1d6e5da68d7ad6b9ea70eeee
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)
17 * any later version.
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
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 <net/protocol.h>
55 #include <net/ip.h>
56 #include <net/ipv6.h>
57 #include <net/route.h>
58 #include <net/sctp/sctp.h>
59 #include <net/addrconf.h>
60 #include <net/inet_common.h>
61 #include <net/inet_ecn.h>
63 /* Global data structures. */
64 struct sctp_globals sctp_globals __read_mostly;
65 struct proc_dir_entry *proc_net_sctp;
66 DEFINE_SNMP_STAT(struct sctp_mib, sctp_statistics) __read_mostly;
68 struct idr sctp_assocs_id;
69 DEFINE_SPINLOCK(sctp_assocs_id_lock);
71 /* This is the global socket data structure used for responding to
72 * the Out-of-the-blue (OOTB) packets. A control sock will be created
73 * for this socket at the initialization time.
75 static struct socket *sctp_ctl_socket;
77 static struct sctp_pf *sctp_pf_inet6_specific;
78 static struct sctp_pf *sctp_pf_inet_specific;
79 static struct sctp_af *sctp_af_v4_specific;
80 static struct sctp_af *sctp_af_v6_specific;
82 struct kmem_cache *sctp_chunk_cachep __read_mostly;
83 struct kmem_cache *sctp_bucket_cachep __read_mostly;
85 /* Return the address of the control sock. */
86 struct sock *sctp_get_ctl_sock(void)
88 return sctp_ctl_socket->sk;
91 /* Set up the proc fs entry for the SCTP protocol. */
92 static __init int sctp_proc_init(void)
94 if (!proc_net_sctp) {
95 struct proc_dir_entry *ent;
96 ent = proc_mkdir("net/sctp", NULL);
97 if (ent) {
98 ent->owner = THIS_MODULE;
99 proc_net_sctp = ent;
100 } else
101 goto out_nomem;
104 if (sctp_snmp_proc_init())
105 goto out_nomem;
106 if (sctp_eps_proc_init())
107 goto out_nomem;
108 if (sctp_assocs_proc_init())
109 goto out_nomem;
111 return 0;
113 out_nomem:
114 return -ENOMEM;
117 /* Clean up the proc fs entry for the SCTP protocol.
118 * Note: Do not make this __exit as it is used in the init error
119 * path.
121 static void sctp_proc_exit(void)
123 sctp_snmp_proc_exit();
124 sctp_eps_proc_exit();
125 sctp_assocs_proc_exit();
127 if (proc_net_sctp) {
128 proc_net_sctp = NULL;
129 remove_proc_entry("net/sctp", NULL);
133 /* Private helper to extract ipv4 address and stash them in
134 * the protocol structure.
136 static void sctp_v4_copy_addrlist(struct list_head *addrlist,
137 struct net_device *dev)
139 struct in_device *in_dev;
140 struct in_ifaddr *ifa;
141 struct sctp_sockaddr_entry *addr;
143 rcu_read_lock();
144 if ((in_dev = __in_dev_get_rcu(dev)) == NULL) {
145 rcu_read_unlock();
146 return;
149 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
150 /* Add the address to the local list. */
151 addr = t_new(struct sctp_sockaddr_entry, GFP_ATOMIC);
152 if (addr) {
153 addr->a.v4.sin_family = AF_INET;
154 addr->a.v4.sin_port = 0;
155 addr->a.v4.sin_addr.s_addr = ifa->ifa_local;
156 list_add_tail(&addr->list, addrlist);
160 rcu_read_unlock();
163 /* Extract our IP addresses from the system and stash them in the
164 * protocol structure.
166 static void sctp_get_local_addr_list(void)
168 struct net_device *dev;
169 struct list_head *pos;
170 struct sctp_af *af;
172 read_lock(&dev_base_lock);
173 for (dev = dev_base; dev; dev = dev->next) {
174 __list_for_each(pos, &sctp_address_families) {
175 af = list_entry(pos, struct sctp_af, list);
176 af->copy_addrlist(&sctp_local_addr_list, dev);
179 read_unlock(&dev_base_lock);
182 /* Free the existing local addresses. */
183 static void sctp_free_local_addr_list(void)
185 struct sctp_sockaddr_entry *addr;
186 struct list_head *pos, *temp;
188 list_for_each_safe(pos, temp, &sctp_local_addr_list) {
189 addr = list_entry(pos, struct sctp_sockaddr_entry, list);
190 list_del(pos);
191 kfree(addr);
195 /* Copy the local addresses which are valid for 'scope' into 'bp'. */
196 int sctp_copy_local_addr_list(struct sctp_bind_addr *bp, sctp_scope_t scope,
197 gfp_t gfp, int copy_flags)
199 struct sctp_sockaddr_entry *addr;
200 int error = 0;
201 struct list_head *pos, *temp;
203 list_for_each_safe(pos, temp, &sctp_local_addr_list) {
204 addr = list_entry(pos, struct sctp_sockaddr_entry, list);
205 if (sctp_in_scope(&addr->a, scope)) {
206 /* Now that the address is in scope, check to see if
207 * the address type is really supported by the local
208 * sock as well as the remote peer.
210 if ((((AF_INET == addr->a.sa.sa_family) &&
211 (copy_flags & SCTP_ADDR4_PEERSUPP))) ||
212 (((AF_INET6 == addr->a.sa.sa_family) &&
213 (copy_flags & SCTP_ADDR6_ALLOWED) &&
214 (copy_flags & SCTP_ADDR6_PEERSUPP)))) {
215 error = sctp_add_bind_addr(bp, &addr->a, 1,
216 GFP_ATOMIC);
217 if (error)
218 goto end_copy;
223 end_copy:
224 return error;
227 /* Initialize a sctp_addr from in incoming skb. */
228 static void sctp_v4_from_skb(union sctp_addr *addr, struct sk_buff *skb,
229 int is_saddr)
231 void *from;
232 __be16 *port;
233 struct sctphdr *sh;
235 port = &addr->v4.sin_port;
236 addr->v4.sin_family = AF_INET;
238 sh = (struct sctphdr *) skb->h.raw;
239 if (is_saddr) {
240 *port = sh->source;
241 from = &skb->nh.iph->saddr;
242 } else {
243 *port = sh->dest;
244 from = &skb->nh.iph->daddr;
246 memcpy(&addr->v4.sin_addr.s_addr, from, sizeof(struct in_addr));
249 /* Initialize an sctp_addr from a socket. */
250 static void sctp_v4_from_sk(union sctp_addr *addr, struct sock *sk)
252 addr->v4.sin_family = AF_INET;
253 addr->v4.sin_port = 0;
254 addr->v4.sin_addr.s_addr = inet_sk(sk)->rcv_saddr;
257 /* Initialize sk->sk_rcv_saddr from sctp_addr. */
258 static void sctp_v4_to_sk_saddr(union sctp_addr *addr, struct sock *sk)
260 inet_sk(sk)->rcv_saddr = addr->v4.sin_addr.s_addr;
263 /* Initialize sk->sk_daddr from sctp_addr. */
264 static void sctp_v4_to_sk_daddr(union sctp_addr *addr, struct sock *sk)
266 inet_sk(sk)->daddr = addr->v4.sin_addr.s_addr;
269 /* Initialize a sctp_addr from an address parameter. */
270 static void sctp_v4_from_addr_param(union sctp_addr *addr,
271 union sctp_addr_param *param,
272 __be16 port, int iif)
274 addr->v4.sin_family = AF_INET;
275 addr->v4.sin_port = port;
276 addr->v4.sin_addr.s_addr = param->v4.addr.s_addr;
279 /* Initialize an address parameter from a sctp_addr and return the length
280 * of the address parameter.
282 static int sctp_v4_to_addr_param(const union sctp_addr *addr,
283 union sctp_addr_param *param)
285 int length = sizeof(sctp_ipv4addr_param_t);
287 param->v4.param_hdr.type = SCTP_PARAM_IPV4_ADDRESS;
288 param->v4.param_hdr.length = htons(length);
289 param->v4.addr.s_addr = addr->v4.sin_addr.s_addr;
291 return length;
294 /* Initialize a sctp_addr from a dst_entry. */
295 static void sctp_v4_dst_saddr(union sctp_addr *saddr, struct dst_entry *dst,
296 __be16 port)
298 struct rtable *rt = (struct rtable *)dst;
299 saddr->v4.sin_family = AF_INET;
300 saddr->v4.sin_port = port;
301 saddr->v4.sin_addr.s_addr = rt->rt_src;
304 /* Compare two addresses exactly. */
305 static int sctp_v4_cmp_addr(const union sctp_addr *addr1,
306 const union sctp_addr *addr2)
308 if (addr1->sa.sa_family != addr2->sa.sa_family)
309 return 0;
310 if (addr1->v4.sin_port != addr2->v4.sin_port)
311 return 0;
312 if (addr1->v4.sin_addr.s_addr != addr2->v4.sin_addr.s_addr)
313 return 0;
315 return 1;
318 /* Initialize addr struct to INADDR_ANY. */
319 static void sctp_v4_inaddr_any(union sctp_addr *addr, __be16 port)
321 addr->v4.sin_family = AF_INET;
322 addr->v4.sin_addr.s_addr = INADDR_ANY;
323 addr->v4.sin_port = port;
326 /* Is this a wildcard address? */
327 static int sctp_v4_is_any(const union sctp_addr *addr)
329 return INADDR_ANY == addr->v4.sin_addr.s_addr;
332 /* This function checks if the address is a valid address to be used for
333 * SCTP binding.
335 * Output:
336 * Return 0 - If the address is a non-unicast or an illegal address.
337 * Return 1 - If the address is a unicast.
339 static int sctp_v4_addr_valid(union sctp_addr *addr,
340 struct sctp_sock *sp,
341 const struct sk_buff *skb)
343 /* Is this a non-unicast address or a unusable SCTP address? */
344 if (IS_IPV4_UNUSABLE_ADDRESS(&addr->v4.sin_addr.s_addr))
345 return 0;
347 /* Is this a broadcast address? */
348 if (skb && ((struct rtable *)skb->dst)->rt_flags & RTCF_BROADCAST)
349 return 0;
351 return 1;
354 /* Should this be available for binding? */
355 static int sctp_v4_available(union sctp_addr *addr, struct sctp_sock *sp)
357 int ret = inet_addr_type(addr->v4.sin_addr.s_addr);
360 if (addr->v4.sin_addr.s_addr != INADDR_ANY &&
361 ret != RTN_LOCAL &&
362 !sp->inet.freebind &&
363 !sysctl_ip_nonlocal_bind)
364 return 0;
366 return 1;
369 /* Checking the loopback, private and other address scopes as defined in
370 * RFC 1918. The IPv4 scoping is based on the draft for SCTP IPv4
371 * scoping <draft-stewart-tsvwg-sctp-ipv4-00.txt>.
373 * Level 0 - unusable SCTP addresses
374 * Level 1 - loopback address
375 * Level 2 - link-local addresses
376 * Level 3 - private addresses.
377 * Level 4 - global addresses
378 * For INIT and INIT-ACK address list, let L be the level of
379 * of requested destination address, sender and receiver
380 * SHOULD include all of its addresses with level greater
381 * than or equal to L.
383 static sctp_scope_t sctp_v4_scope(union sctp_addr *addr)
385 sctp_scope_t retval;
387 /* Should IPv4 scoping be a sysctl configurable option
388 * so users can turn it off (default on) for certain
389 * unconventional networking environments?
392 /* Check for unusable SCTP addresses. */
393 if (IS_IPV4_UNUSABLE_ADDRESS(&addr->v4.sin_addr.s_addr)) {
394 retval = SCTP_SCOPE_UNUSABLE;
395 } else if (LOOPBACK(addr->v4.sin_addr.s_addr)) {
396 retval = SCTP_SCOPE_LOOPBACK;
397 } else if (IS_IPV4_LINK_ADDRESS(&addr->v4.sin_addr.s_addr)) {
398 retval = SCTP_SCOPE_LINK;
399 } else if (IS_IPV4_PRIVATE_ADDRESS(&addr->v4.sin_addr.s_addr)) {
400 retval = SCTP_SCOPE_PRIVATE;
401 } else {
402 retval = SCTP_SCOPE_GLOBAL;
405 return retval;
408 /* Returns a valid dst cache entry for the given source and destination ip
409 * addresses. If an association is passed, trys to get a dst entry with a
410 * source address that matches an address in the bind address list.
412 static struct dst_entry *sctp_v4_get_dst(struct sctp_association *asoc,
413 union sctp_addr *daddr,
414 union sctp_addr *saddr)
416 struct rtable *rt;
417 struct flowi fl;
418 struct sctp_bind_addr *bp;
419 rwlock_t *addr_lock;
420 struct sctp_sockaddr_entry *laddr;
421 struct list_head *pos;
422 struct dst_entry *dst = NULL;
423 union sctp_addr dst_saddr;
425 memset(&fl, 0x0, sizeof(struct flowi));
426 fl.fl4_dst = daddr->v4.sin_addr.s_addr;
427 fl.proto = IPPROTO_SCTP;
428 if (asoc) {
429 fl.fl4_tos = RT_CONN_FLAGS(asoc->base.sk);
430 fl.oif = asoc->base.sk->sk_bound_dev_if;
432 if (saddr)
433 fl.fl4_src = saddr->v4.sin_addr.s_addr;
435 SCTP_DEBUG_PRINTK("%s: DST:%u.%u.%u.%u, SRC:%u.%u.%u.%u - ",
436 __FUNCTION__, NIPQUAD(fl.fl4_dst),
437 NIPQUAD(fl.fl4_src));
439 if (!ip_route_output_key(&rt, &fl)) {
440 dst = &rt->u.dst;
443 /* If there is no association or if a source address is passed, no
444 * more validation is required.
446 if (!asoc || saddr)
447 goto out;
449 bp = &asoc->base.bind_addr;
450 addr_lock = &asoc->base.addr_lock;
452 if (dst) {
453 /* Walk through the bind address list and look for a bind
454 * address that matches the source address of the returned dst.
456 sctp_read_lock(addr_lock);
457 list_for_each(pos, &bp->address_list) {
458 laddr = list_entry(pos, struct sctp_sockaddr_entry,
459 list);
460 if (!laddr->use_as_src)
461 continue;
462 sctp_v4_dst_saddr(&dst_saddr, dst, htons(bp->port));
463 if (sctp_v4_cmp_addr(&dst_saddr, &laddr->a))
464 goto out_unlock;
466 sctp_read_unlock(addr_lock);
468 /* None of the bound addresses match the source address of the
469 * dst. So release it.
471 dst_release(dst);
472 dst = NULL;
475 /* Walk through the bind address list and try to get a dst that
476 * matches a bind address as the source address.
478 sctp_read_lock(addr_lock);
479 list_for_each(pos, &bp->address_list) {
480 laddr = list_entry(pos, struct sctp_sockaddr_entry, list);
482 if ((laddr->use_as_src) &&
483 (AF_INET == laddr->a.sa.sa_family)) {
484 fl.fl4_src = laddr->a.v4.sin_addr.s_addr;
485 if (!ip_route_output_key(&rt, &fl)) {
486 dst = &rt->u.dst;
487 goto out_unlock;
492 out_unlock:
493 sctp_read_unlock(addr_lock);
494 out:
495 if (dst)
496 SCTP_DEBUG_PRINTK("rt_dst:%u.%u.%u.%u, rt_src:%u.%u.%u.%u\n",
497 NIPQUAD(rt->rt_dst), NIPQUAD(rt->rt_src));
498 else
499 SCTP_DEBUG_PRINTK("NO ROUTE\n");
501 return dst;
504 /* For v4, the source address is cached in the route entry(dst). So no need
505 * to cache it separately and hence this is an empty routine.
507 static void sctp_v4_get_saddr(struct sctp_association *asoc,
508 struct dst_entry *dst,
509 union sctp_addr *daddr,
510 union sctp_addr *saddr)
512 struct rtable *rt = (struct rtable *)dst;
514 if (!asoc)
515 return;
517 if (rt) {
518 saddr->v4.sin_family = AF_INET;
519 saddr->v4.sin_port = htons(asoc->base.bind_addr.port);
520 saddr->v4.sin_addr.s_addr = rt->rt_src;
524 /* What interface did this skb arrive on? */
525 static int sctp_v4_skb_iif(const struct sk_buff *skb)
527 return ((struct rtable *)skb->dst)->rt_iif;
530 /* Was this packet marked by Explicit Congestion Notification? */
531 static int sctp_v4_is_ce(const struct sk_buff *skb)
533 return INET_ECN_is_ce(skb->nh.iph->tos);
536 /* Create and initialize a new sk for the socket returned by accept(). */
537 static struct sock *sctp_v4_create_accept_sk(struct sock *sk,
538 struct sctp_association *asoc)
540 struct inet_sock *inet = inet_sk(sk);
541 struct inet_sock *newinet;
542 struct sock *newsk = sk_alloc(PF_INET, GFP_KERNEL, sk->sk_prot, 1);
544 if (!newsk)
545 goto out;
547 sock_init_data(NULL, newsk);
549 newsk->sk_type = SOCK_STREAM;
551 newsk->sk_no_check = sk->sk_no_check;
552 newsk->sk_reuse = sk->sk_reuse;
553 newsk->sk_shutdown = sk->sk_shutdown;
555 newsk->sk_destruct = inet_sock_destruct;
556 newsk->sk_family = PF_INET;
557 newsk->sk_protocol = IPPROTO_SCTP;
558 newsk->sk_backlog_rcv = sk->sk_prot->backlog_rcv;
559 sock_reset_flag(newsk, SOCK_ZAPPED);
561 newinet = inet_sk(newsk);
563 /* Initialize sk's sport, dport, rcv_saddr and daddr for
564 * getsockname() and getpeername()
566 newinet->sport = inet->sport;
567 newinet->saddr = inet->saddr;
568 newinet->rcv_saddr = inet->rcv_saddr;
569 newinet->dport = htons(asoc->peer.port);
570 newinet->daddr = asoc->peer.primary_addr.v4.sin_addr.s_addr;
571 newinet->pmtudisc = inet->pmtudisc;
572 newinet->id = asoc->next_tsn ^ jiffies;
574 newinet->uc_ttl = -1;
575 newinet->mc_loop = 1;
576 newinet->mc_ttl = 1;
577 newinet->mc_index = 0;
578 newinet->mc_list = NULL;
580 sk_refcnt_debug_inc(newsk);
582 if (newsk->sk_prot->init(newsk)) {
583 sk_common_release(newsk);
584 newsk = NULL;
587 out:
588 return newsk;
591 /* Map address, empty for v4 family */
592 static void sctp_v4_addr_v4map(struct sctp_sock *sp, union sctp_addr *addr)
594 /* Empty */
597 /* Dump the v4 addr to the seq file. */
598 static void sctp_v4_seq_dump_addr(struct seq_file *seq, union sctp_addr *addr)
600 seq_printf(seq, "%d.%d.%d.%d ", NIPQUAD(addr->v4.sin_addr));
603 /* Event handler for inet address addition/deletion events. */
604 static int sctp_inetaddr_event(struct notifier_block *this, unsigned long ev,
605 void *ptr)
607 struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
608 struct sctp_sockaddr_entry *addr;
609 struct list_head *pos, *temp;
611 switch (ev) {
612 case NETDEV_UP:
613 addr = kmalloc(sizeof(struct sctp_sockaddr_entry), GFP_ATOMIC);
614 if (addr) {
615 addr->a.v4.sin_family = AF_INET;
616 addr->a.v4.sin_port = 0;
617 addr->a.v4.sin_addr.s_addr = ifa->ifa_local;
618 list_add_tail(&addr->list, &sctp_local_addr_list);
620 break;
621 case NETDEV_DOWN:
622 list_for_each_safe(pos, temp, &sctp_local_addr_list) {
623 addr = list_entry(pos, struct sctp_sockaddr_entry, list);
624 if (addr->a.v4.sin_addr.s_addr == ifa->ifa_local) {
625 list_del(pos);
626 kfree(addr);
627 break;
631 break;
634 return NOTIFY_DONE;
638 * Initialize the control inode/socket with a control endpoint data
639 * structure. This endpoint is reserved exclusively for the OOTB processing.
641 static int sctp_ctl_sock_init(void)
643 int err;
644 sa_family_t family;
646 if (sctp_get_pf_specific(PF_INET6))
647 family = PF_INET6;
648 else
649 family = PF_INET;
651 err = sock_create_kern(family, SOCK_SEQPACKET, IPPROTO_SCTP,
652 &sctp_ctl_socket);
653 if (err < 0) {
654 printk(KERN_ERR
655 "SCTP: Failed to create the SCTP control socket.\n");
656 return err;
658 sctp_ctl_socket->sk->sk_allocation = GFP_ATOMIC;
659 inet_sk(sctp_ctl_socket->sk)->uc_ttl = -1;
661 return 0;
664 /* Register address family specific functions. */
665 int sctp_register_af(struct sctp_af *af)
667 switch (af->sa_family) {
668 case AF_INET:
669 if (sctp_af_v4_specific)
670 return 0;
671 sctp_af_v4_specific = af;
672 break;
673 case AF_INET6:
674 if (sctp_af_v6_specific)
675 return 0;
676 sctp_af_v6_specific = af;
677 break;
678 default:
679 return 0;
682 INIT_LIST_HEAD(&af->list);
683 list_add_tail(&af->list, &sctp_address_families);
684 return 1;
687 /* Get the table of functions for manipulating a particular address
688 * family.
690 struct sctp_af *sctp_get_af_specific(sa_family_t family)
692 switch (family) {
693 case AF_INET:
694 return sctp_af_v4_specific;
695 case AF_INET6:
696 return sctp_af_v6_specific;
697 default:
698 return NULL;
702 /* Common code to initialize a AF_INET msg_name. */
703 static void sctp_inet_msgname(char *msgname, int *addr_len)
705 struct sockaddr_in *sin;
707 sin = (struct sockaddr_in *)msgname;
708 *addr_len = sizeof(struct sockaddr_in);
709 sin->sin_family = AF_INET;
710 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
713 /* Copy the primary address of the peer primary address as the msg_name. */
714 static void sctp_inet_event_msgname(struct sctp_ulpevent *event, char *msgname,
715 int *addr_len)
717 struct sockaddr_in *sin, *sinfrom;
719 if (msgname) {
720 struct sctp_association *asoc;
722 asoc = event->asoc;
723 sctp_inet_msgname(msgname, addr_len);
724 sin = (struct sockaddr_in *)msgname;
725 sinfrom = &asoc->peer.primary_addr.v4;
726 sin->sin_port = htons(asoc->peer.port);
727 sin->sin_addr.s_addr = sinfrom->sin_addr.s_addr;
731 /* Initialize and copy out a msgname from an inbound skb. */
732 static void sctp_inet_skb_msgname(struct sk_buff *skb, char *msgname, int *len)
734 struct sctphdr *sh;
735 struct sockaddr_in *sin;
737 if (msgname) {
738 sctp_inet_msgname(msgname, len);
739 sin = (struct sockaddr_in *)msgname;
740 sh = (struct sctphdr *)skb->h.raw;
741 sin->sin_port = sh->source;
742 sin->sin_addr.s_addr = skb->nh.iph->saddr;
746 /* Do we support this AF? */
747 static int sctp_inet_af_supported(sa_family_t family, struct sctp_sock *sp)
749 /* PF_INET only supports AF_INET addresses. */
750 return (AF_INET == family);
753 /* Address matching with wildcards allowed. */
754 static int sctp_inet_cmp_addr(const union sctp_addr *addr1,
755 const union sctp_addr *addr2,
756 struct sctp_sock *opt)
758 /* PF_INET only supports AF_INET addresses. */
759 if (addr1->sa.sa_family != addr2->sa.sa_family)
760 return 0;
761 if (INADDR_ANY == addr1->v4.sin_addr.s_addr ||
762 INADDR_ANY == addr2->v4.sin_addr.s_addr)
763 return 1;
764 if (addr1->v4.sin_addr.s_addr == addr2->v4.sin_addr.s_addr)
765 return 1;
767 return 0;
770 /* Verify that provided sockaddr looks bindable. Common verification has
771 * already been taken care of.
773 static int sctp_inet_bind_verify(struct sctp_sock *opt, union sctp_addr *addr)
775 return sctp_v4_available(addr, opt);
778 /* Verify that sockaddr looks sendable. Common verification has already
779 * been taken care of.
781 static int sctp_inet_send_verify(struct sctp_sock *opt, union sctp_addr *addr)
783 return 1;
786 /* Fill in Supported Address Type information for INIT and INIT-ACK
787 * chunks. Returns number of addresses supported.
789 static int sctp_inet_supported_addrs(const struct sctp_sock *opt,
790 __be16 *types)
792 types[0] = SCTP_PARAM_IPV4_ADDRESS;
793 return 1;
796 /* Wrapper routine that calls the ip transmit routine. */
797 static inline int sctp_v4_xmit(struct sk_buff *skb,
798 struct sctp_transport *transport, int ipfragok)
800 SCTP_DEBUG_PRINTK("%s: skb:%p, len:%d, "
801 "src:%u.%u.%u.%u, dst:%u.%u.%u.%u\n",
802 __FUNCTION__, skb, skb->len,
803 NIPQUAD(((struct rtable *)skb->dst)->rt_src),
804 NIPQUAD(((struct rtable *)skb->dst)->rt_dst));
806 SCTP_INC_STATS(SCTP_MIB_OUTSCTPPACKS);
807 return ip_queue_xmit(skb, ipfragok);
810 static struct sctp_af sctp_ipv4_specific;
812 static struct sctp_pf sctp_pf_inet = {
813 .event_msgname = sctp_inet_event_msgname,
814 .skb_msgname = sctp_inet_skb_msgname,
815 .af_supported = sctp_inet_af_supported,
816 .cmp_addr = sctp_inet_cmp_addr,
817 .bind_verify = sctp_inet_bind_verify,
818 .send_verify = sctp_inet_send_verify,
819 .supported_addrs = sctp_inet_supported_addrs,
820 .create_accept_sk = sctp_v4_create_accept_sk,
821 .addr_v4map = sctp_v4_addr_v4map,
822 .af = &sctp_ipv4_specific,
825 /* Notifier for inetaddr addition/deletion events. */
826 static struct notifier_block sctp_inetaddr_notifier = {
827 .notifier_call = sctp_inetaddr_event,
830 /* Socket operations. */
831 static const struct proto_ops inet_seqpacket_ops = {
832 .family = PF_INET,
833 .owner = THIS_MODULE,
834 .release = inet_release, /* Needs to be wrapped... */
835 .bind = inet_bind,
836 .connect = inet_dgram_connect,
837 .socketpair = sock_no_socketpair,
838 .accept = inet_accept,
839 .getname = inet_getname, /* Semantics are different. */
840 .poll = sctp_poll,
841 .ioctl = inet_ioctl,
842 .listen = sctp_inet_listen,
843 .shutdown = inet_shutdown, /* Looks harmless. */
844 .setsockopt = sock_common_setsockopt, /* IP_SOL IP_OPTION is a problem */
845 .getsockopt = sock_common_getsockopt,
846 .sendmsg = inet_sendmsg,
847 .recvmsg = sock_common_recvmsg,
848 .mmap = sock_no_mmap,
849 .sendpage = sock_no_sendpage,
850 #ifdef CONFIG_COMPAT
851 .compat_setsockopt = compat_sock_common_setsockopt,
852 .compat_getsockopt = compat_sock_common_getsockopt,
853 #endif
856 /* Registration with AF_INET family. */
857 static struct inet_protosw sctp_seqpacket_protosw = {
858 .type = SOCK_SEQPACKET,
859 .protocol = IPPROTO_SCTP,
860 .prot = &sctp_prot,
861 .ops = &inet_seqpacket_ops,
862 .capability = -1,
863 .no_check = 0,
864 .flags = SCTP_PROTOSW_FLAG
866 static struct inet_protosw sctp_stream_protosw = {
867 .type = SOCK_STREAM,
868 .protocol = IPPROTO_SCTP,
869 .prot = &sctp_prot,
870 .ops = &inet_seqpacket_ops,
871 .capability = -1,
872 .no_check = 0,
873 .flags = SCTP_PROTOSW_FLAG
876 /* Register with IP layer. */
877 static struct net_protocol sctp_protocol = {
878 .handler = sctp_rcv,
879 .err_handler = sctp_v4_err,
880 .no_policy = 1,
883 /* IPv4 address related functions. */
884 static struct sctp_af sctp_ipv4_specific = {
885 .sa_family = AF_INET,
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 #ifdef CONFIG_COMPAT
911 .compat_setsockopt = compat_ip_setsockopt,
912 .compat_getsockopt = compat_ip_getsockopt,
913 #endif
916 struct sctp_pf *sctp_get_pf_specific(sa_family_t family) {
918 switch (family) {
919 case PF_INET:
920 return sctp_pf_inet_specific;
921 case PF_INET6:
922 return sctp_pf_inet6_specific;
923 default:
924 return NULL;
928 /* Register the PF specific function table. */
929 int sctp_register_pf(struct sctp_pf *pf, sa_family_t family)
931 switch (family) {
932 case PF_INET:
933 if (sctp_pf_inet_specific)
934 return 0;
935 sctp_pf_inet_specific = pf;
936 break;
937 case PF_INET6:
938 if (sctp_pf_inet6_specific)
939 return 0;
940 sctp_pf_inet6_specific = pf;
941 break;
942 default:
943 return 0;
945 return 1;
948 static int __init init_sctp_mibs(void)
950 sctp_statistics[0] = alloc_percpu(struct sctp_mib);
951 if (!sctp_statistics[0])
952 return -ENOMEM;
953 sctp_statistics[1] = alloc_percpu(struct sctp_mib);
954 if (!sctp_statistics[1]) {
955 free_percpu(sctp_statistics[0]);
956 return -ENOMEM;
958 return 0;
962 static void cleanup_sctp_mibs(void)
964 free_percpu(sctp_statistics[0]);
965 free_percpu(sctp_statistics[1]);
968 /* Initialize the universe into something sensible. */
969 SCTP_STATIC __init int sctp_init(void)
971 int i;
972 int status = -EINVAL;
973 unsigned long goal;
974 int order;
976 /* SCTP_DEBUG sanity check. */
977 if (!sctp_sanity_check())
978 goto out;
980 status = proto_register(&sctp_prot, 1);
981 if (status)
982 goto out;
984 /* Add SCTP to inet_protos hash table. */
985 status = -EAGAIN;
986 if (inet_add_protocol(&sctp_protocol, IPPROTO_SCTP) < 0)
987 goto err_add_protocol;
989 /* Add SCTP(TCP and UDP style) to inetsw linked list. */
990 inet_register_protosw(&sctp_seqpacket_protosw);
991 inet_register_protosw(&sctp_stream_protosw);
993 /* Allocate a cache pools. */
994 status = -ENOBUFS;
995 sctp_bucket_cachep = kmem_cache_create("sctp_bind_bucket",
996 sizeof(struct sctp_bind_bucket),
997 0, SLAB_HWCACHE_ALIGN,
998 NULL, NULL);
1000 if (!sctp_bucket_cachep)
1001 goto err_bucket_cachep;
1003 sctp_chunk_cachep = kmem_cache_create("sctp_chunk",
1004 sizeof(struct sctp_chunk),
1005 0, SLAB_HWCACHE_ALIGN,
1006 NULL, NULL);
1007 if (!sctp_chunk_cachep)
1008 goto err_chunk_cachep;
1010 /* Allocate and initialise sctp mibs. */
1011 status = init_sctp_mibs();
1012 if (status)
1013 goto err_init_mibs;
1015 /* Initialize proc fs directory. */
1016 status = sctp_proc_init();
1017 if (status)
1018 goto err_init_proc;
1020 /* Initialize object count debugging. */
1021 sctp_dbg_objcnt_init();
1023 /* Initialize the SCTP specific PF functions. */
1024 sctp_register_pf(&sctp_pf_inet, PF_INET);
1026 * 14. Suggested SCTP Protocol Parameter Values
1028 /* The following protocol parameters are RECOMMENDED: */
1029 /* RTO.Initial - 3 seconds */
1030 sctp_rto_initial = SCTP_RTO_INITIAL;
1031 /* RTO.Min - 1 second */
1032 sctp_rto_min = SCTP_RTO_MIN;
1033 /* RTO.Max - 60 seconds */
1034 sctp_rto_max = SCTP_RTO_MAX;
1035 /* RTO.Alpha - 1/8 */
1036 sctp_rto_alpha = SCTP_RTO_ALPHA;
1037 /* RTO.Beta - 1/4 */
1038 sctp_rto_beta = SCTP_RTO_BETA;
1040 /* Valid.Cookie.Life - 60 seconds */
1041 sctp_valid_cookie_life = SCTP_DEFAULT_COOKIE_LIFE;
1043 /* Whether Cookie Preservative is enabled(1) or not(0) */
1044 sctp_cookie_preserve_enable = 1;
1046 /* Max.Burst - 4 */
1047 sctp_max_burst = SCTP_MAX_BURST;
1049 /* Association.Max.Retrans - 10 attempts
1050 * Path.Max.Retrans - 5 attempts (per destination address)
1051 * Max.Init.Retransmits - 8 attempts
1053 sctp_max_retrans_association = 10;
1054 sctp_max_retrans_path = 5;
1055 sctp_max_retrans_init = 8;
1057 /* Sendbuffer growth - do per-socket accounting */
1058 sctp_sndbuf_policy = 0;
1060 /* Rcvbuffer growth - do per-socket accounting */
1061 sctp_rcvbuf_policy = 0;
1063 /* HB.interval - 30 seconds */
1064 sctp_hb_interval = SCTP_DEFAULT_TIMEOUT_HEARTBEAT;
1066 /* delayed SACK timeout */
1067 sctp_sack_timeout = SCTP_DEFAULT_TIMEOUT_SACK;
1069 /* Implementation specific variables. */
1071 /* Initialize default stream count setup information. */
1072 sctp_max_instreams = SCTP_DEFAULT_INSTREAMS;
1073 sctp_max_outstreams = SCTP_DEFAULT_OUTSTREAMS;
1075 /* Initialize handle used for association ids. */
1076 idr_init(&sctp_assocs_id);
1078 /* Size and allocate the association hash table.
1079 * The methodology is similar to that of the tcp hash tables.
1081 if (num_physpages >= (128 * 1024))
1082 goal = num_physpages >> (22 - PAGE_SHIFT);
1083 else
1084 goal = num_physpages >> (24 - PAGE_SHIFT);
1086 for (order = 0; (1UL << order) < goal; order++)
1089 do {
1090 sctp_assoc_hashsize = (1UL << order) * PAGE_SIZE /
1091 sizeof(struct sctp_hashbucket);
1092 if ((sctp_assoc_hashsize > (64 * 1024)) && order > 0)
1093 continue;
1094 sctp_assoc_hashtable = (struct sctp_hashbucket *)
1095 __get_free_pages(GFP_ATOMIC, order);
1096 } while (!sctp_assoc_hashtable && --order > 0);
1097 if (!sctp_assoc_hashtable) {
1098 printk(KERN_ERR "SCTP: Failed association hash alloc.\n");
1099 status = -ENOMEM;
1100 goto err_ahash_alloc;
1102 for (i = 0; i < sctp_assoc_hashsize; i++) {
1103 rwlock_init(&sctp_assoc_hashtable[i].lock);
1104 sctp_assoc_hashtable[i].chain = NULL;
1107 /* Allocate and initialize the endpoint hash table. */
1108 sctp_ep_hashsize = 64;
1109 sctp_ep_hashtable = (struct sctp_hashbucket *)
1110 kmalloc(64 * sizeof(struct sctp_hashbucket), GFP_KERNEL);
1111 if (!sctp_ep_hashtable) {
1112 printk(KERN_ERR "SCTP: Failed endpoint_hash alloc.\n");
1113 status = -ENOMEM;
1114 goto err_ehash_alloc;
1116 for (i = 0; i < sctp_ep_hashsize; i++) {
1117 rwlock_init(&sctp_ep_hashtable[i].lock);
1118 sctp_ep_hashtable[i].chain = NULL;
1121 /* Allocate and initialize the SCTP port hash table. */
1122 do {
1123 sctp_port_hashsize = (1UL << order) * PAGE_SIZE /
1124 sizeof(struct sctp_bind_hashbucket);
1125 if ((sctp_port_hashsize > (64 * 1024)) && order > 0)
1126 continue;
1127 sctp_port_hashtable = (struct sctp_bind_hashbucket *)
1128 __get_free_pages(GFP_ATOMIC, order);
1129 } while (!sctp_port_hashtable && --order > 0);
1130 if (!sctp_port_hashtable) {
1131 printk(KERN_ERR "SCTP: Failed bind hash alloc.");
1132 status = -ENOMEM;
1133 goto err_bhash_alloc;
1135 for (i = 0; i < sctp_port_hashsize; i++) {
1136 spin_lock_init(&sctp_port_hashtable[i].lock);
1137 sctp_port_hashtable[i].chain = NULL;
1140 spin_lock_init(&sctp_port_alloc_lock);
1141 sctp_port_rover = sysctl_local_port_range[0] - 1;
1143 printk(KERN_INFO "SCTP: Hash tables configured "
1144 "(established %d bind %d)\n",
1145 sctp_assoc_hashsize, sctp_port_hashsize);
1147 /* Disable ADDIP by default. */
1148 sctp_addip_enable = 0;
1150 /* Enable PR-SCTP by default. */
1151 sctp_prsctp_enable = 1;
1153 sctp_sysctl_register();
1155 INIT_LIST_HEAD(&sctp_address_families);
1156 sctp_register_af(&sctp_ipv4_specific);
1158 status = sctp_v6_init();
1159 if (status)
1160 goto err_v6_init;
1162 /* Initialize the control inode/socket for handling OOTB packets. */
1163 if ((status = sctp_ctl_sock_init())) {
1164 printk (KERN_ERR
1165 "SCTP: Failed to initialize the SCTP control sock.\n");
1166 goto err_ctl_sock_init;
1169 /* Initialize the local address list. */
1170 INIT_LIST_HEAD(&sctp_local_addr_list);
1172 sctp_get_local_addr_list();
1174 /* Register notifier for inet address additions/deletions. */
1175 register_inetaddr_notifier(&sctp_inetaddr_notifier);
1177 __unsafe(THIS_MODULE);
1178 status = 0;
1179 out:
1180 return status;
1181 err_ctl_sock_init:
1182 sctp_v6_exit();
1183 err_v6_init:
1184 sctp_sysctl_unregister();
1185 list_del(&sctp_ipv4_specific.list);
1186 free_pages((unsigned long)sctp_port_hashtable,
1187 get_order(sctp_port_hashsize *
1188 sizeof(struct sctp_bind_hashbucket)));
1189 err_bhash_alloc:
1190 kfree(sctp_ep_hashtable);
1191 err_ehash_alloc:
1192 free_pages((unsigned long)sctp_assoc_hashtable,
1193 get_order(sctp_assoc_hashsize *
1194 sizeof(struct sctp_hashbucket)));
1195 err_ahash_alloc:
1196 sctp_dbg_objcnt_exit();
1197 err_init_proc:
1198 sctp_proc_exit();
1199 cleanup_sctp_mibs();
1200 err_init_mibs:
1201 kmem_cache_destroy(sctp_chunk_cachep);
1202 err_chunk_cachep:
1203 kmem_cache_destroy(sctp_bucket_cachep);
1204 err_bucket_cachep:
1205 inet_del_protocol(&sctp_protocol, IPPROTO_SCTP);
1206 inet_unregister_protosw(&sctp_seqpacket_protosw);
1207 inet_unregister_protosw(&sctp_stream_protosw);
1208 err_add_protocol:
1209 proto_unregister(&sctp_prot);
1210 goto out;
1213 /* Exit handler for the SCTP protocol. */
1214 SCTP_STATIC __exit void sctp_exit(void)
1216 /* BUG. This should probably do something useful like clean
1217 * up all the remaining associations and all that memory.
1220 /* Unregister notifier for inet address additions/deletions. */
1221 unregister_inetaddr_notifier(&sctp_inetaddr_notifier);
1223 /* Free the local address list. */
1224 sctp_free_local_addr_list();
1226 /* Free the control endpoint. */
1227 sock_release(sctp_ctl_socket);
1229 sctp_v6_exit();
1230 sctp_sysctl_unregister();
1231 list_del(&sctp_ipv4_specific.list);
1233 free_pages((unsigned long)sctp_assoc_hashtable,
1234 get_order(sctp_assoc_hashsize *
1235 sizeof(struct sctp_hashbucket)));
1236 kfree(sctp_ep_hashtable);
1237 free_pages((unsigned long)sctp_port_hashtable,
1238 get_order(sctp_port_hashsize *
1239 sizeof(struct sctp_bind_hashbucket)));
1241 kmem_cache_destroy(sctp_chunk_cachep);
1242 kmem_cache_destroy(sctp_bucket_cachep);
1244 sctp_dbg_objcnt_exit();
1245 sctp_proc_exit();
1246 cleanup_sctp_mibs();
1248 inet_del_protocol(&sctp_protocol, IPPROTO_SCTP);
1249 inet_unregister_protosw(&sctp_seqpacket_protosw);
1250 inet_unregister_protosw(&sctp_stream_protosw);
1251 proto_unregister(&sctp_prot);
1254 module_init(sctp_init);
1255 module_exit(sctp_exit);
1258 * __stringify doesn't likes enums, so use IPPROTO_SCTP value (132) directly.
1260 MODULE_ALIAS("net-pf-" __stringify(PF_INET) "-proto-132");
1261 MODULE_ALIAS("net-pf-" __stringify(PF_INET6) "-proto-132");
1262 MODULE_AUTHOR("Linux Kernel SCTP developers <lksctp-developers@lists.sourceforge.net>");
1263 MODULE_DESCRIPTION("Support for the SCTP protocol (RFC2960)");
1264 MODULE_LICENSE("GPL");