[TG3]: Set minimal hw interrupt mitigation.
[linux-2.6/verdex.git] / net / sctp / protocol.c
blob2e1f9c3556f5922a9018c30055949f7a64f333be
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/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 DEFINE_SPINLOCK(sctp_assocs_id_lock);
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 int sctp_snmp_proc_init(void);
85 extern int sctp_snmp_proc_exit(void);
86 extern int sctp_eps_proc_init(void);
87 extern int sctp_eps_proc_exit(void);
88 extern int sctp_assocs_proc_init(void);
89 extern int sctp_assocs_proc_exit(void);
91 /* Return the address of the control sock. */
92 struct sock *sctp_get_ctl_sock(void)
94 return sctp_ctl_socket->sk;
97 /* Set up the proc fs entry for the SCTP protocol. */
98 static __init int sctp_proc_init(void)
100 if (!proc_net_sctp) {
101 struct proc_dir_entry *ent;
102 ent = proc_mkdir("net/sctp", NULL);
103 if (ent) {
104 ent->owner = THIS_MODULE;
105 proc_net_sctp = ent;
106 } else
107 goto out_nomem;
110 if (sctp_snmp_proc_init())
111 goto out_nomem;
112 if (sctp_eps_proc_init())
113 goto out_nomem;
114 if (sctp_assocs_proc_init())
115 goto out_nomem;
117 return 0;
119 out_nomem:
120 return -ENOMEM;
123 /* Clean up the proc fs entry for the SCTP protocol.
124 * Note: Do not make this __exit as it is used in the init error
125 * path.
127 static void sctp_proc_exit(void)
129 sctp_snmp_proc_exit();
130 sctp_eps_proc_exit();
131 sctp_assocs_proc_exit();
133 if (proc_net_sctp) {
134 proc_net_sctp = NULL;
135 remove_proc_entry("net/sctp", NULL);
139 /* Private helper to extract ipv4 address and stash them in
140 * the protocol structure.
142 static void sctp_v4_copy_addrlist(struct list_head *addrlist,
143 struct net_device *dev)
145 struct in_device *in_dev;
146 struct in_ifaddr *ifa;
147 struct sctp_sockaddr_entry *addr;
149 rcu_read_lock();
150 if ((in_dev = __in_dev_get(dev)) == NULL) {
151 rcu_read_unlock();
152 return;
155 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
156 /* Add the address to the local list. */
157 addr = t_new(struct sctp_sockaddr_entry, GFP_ATOMIC);
158 if (addr) {
159 addr->a.v4.sin_family = AF_INET;
160 addr->a.v4.sin_port = 0;
161 addr->a.v4.sin_addr.s_addr = ifa->ifa_local;
162 list_add_tail(&addr->list, addrlist);
166 rcu_read_unlock();
169 /* Extract our IP addresses from the system and stash them in the
170 * protocol structure.
172 static void __sctp_get_local_addr_list(void)
174 struct net_device *dev;
175 struct list_head *pos;
176 struct sctp_af *af;
178 read_lock(&dev_base_lock);
179 for (dev = dev_base; dev; dev = dev->next) {
180 __list_for_each(pos, &sctp_address_families) {
181 af = list_entry(pos, struct sctp_af, list);
182 af->copy_addrlist(&sctp_local_addr_list, dev);
185 read_unlock(&dev_base_lock);
188 static void sctp_get_local_addr_list(void)
190 unsigned long flags;
192 sctp_spin_lock_irqsave(&sctp_local_addr_lock, flags);
193 __sctp_get_local_addr_list();
194 sctp_spin_unlock_irqrestore(&sctp_local_addr_lock, flags);
197 /* Free the existing local addresses. */
198 static void __sctp_free_local_addr_list(void)
200 struct sctp_sockaddr_entry *addr;
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 list_del(pos);
206 kfree(addr);
210 /* Free the existing local addresses. */
211 static void sctp_free_local_addr_list(void)
213 unsigned long flags;
215 sctp_spin_lock_irqsave(&sctp_local_addr_lock, flags);
216 __sctp_free_local_addr_list();
217 sctp_spin_unlock_irqrestore(&sctp_local_addr_lock, flags);
220 /* Copy the local addresses which are valid for 'scope' into 'bp'. */
221 int sctp_copy_local_addr_list(struct sctp_bind_addr *bp, sctp_scope_t scope,
222 int gfp, int copy_flags)
224 struct sctp_sockaddr_entry *addr;
225 int error = 0;
226 struct list_head *pos;
227 unsigned long flags;
229 sctp_spin_lock_irqsave(&sctp_local_addr_lock, flags);
230 list_for_each(pos, &sctp_local_addr_list) {
231 addr = list_entry(pos, struct sctp_sockaddr_entry, list);
232 if (sctp_in_scope(&addr->a, scope)) {
233 /* Now that the address is in scope, check to see if
234 * the address type is really supported by the local
235 * sock as well as the remote peer.
237 if ((((AF_INET == addr->a.sa.sa_family) &&
238 (copy_flags & SCTP_ADDR4_PEERSUPP))) ||
239 (((AF_INET6 == addr->a.sa.sa_family) &&
240 (copy_flags & SCTP_ADDR6_ALLOWED) &&
241 (copy_flags & SCTP_ADDR6_PEERSUPP)))) {
242 error = sctp_add_bind_addr(bp, &addr->a,
243 GFP_ATOMIC);
244 if (error)
245 goto end_copy;
250 end_copy:
251 sctp_spin_unlock_irqrestore(&sctp_local_addr_lock, flags);
252 return error;
255 /* Initialize a sctp_addr from in incoming skb. */
256 static void sctp_v4_from_skb(union sctp_addr *addr, struct sk_buff *skb,
257 int is_saddr)
259 void *from;
260 __u16 *port;
261 struct sctphdr *sh;
263 port = &addr->v4.sin_port;
264 addr->v4.sin_family = AF_INET;
266 sh = (struct sctphdr *) skb->h.raw;
267 if (is_saddr) {
268 *port = ntohs(sh->source);
269 from = &skb->nh.iph->saddr;
270 } else {
271 *port = ntohs(sh->dest);
272 from = &skb->nh.iph->daddr;
274 memcpy(&addr->v4.sin_addr.s_addr, from, sizeof(struct in_addr));
277 /* Initialize an sctp_addr from a socket. */
278 static void sctp_v4_from_sk(union sctp_addr *addr, struct sock *sk)
280 addr->v4.sin_family = AF_INET;
281 addr->v4.sin_port = inet_sk(sk)->num;
282 addr->v4.sin_addr.s_addr = inet_sk(sk)->rcv_saddr;
285 /* Initialize sk->sk_rcv_saddr from sctp_addr. */
286 static void sctp_v4_to_sk_saddr(union sctp_addr *addr, struct sock *sk)
288 inet_sk(sk)->rcv_saddr = addr->v4.sin_addr.s_addr;
291 /* Initialize sk->sk_daddr from sctp_addr. */
292 static void sctp_v4_to_sk_daddr(union sctp_addr *addr, struct sock *sk)
294 inet_sk(sk)->daddr = addr->v4.sin_addr.s_addr;
297 /* Initialize a sctp_addr from an address parameter. */
298 static void sctp_v4_from_addr_param(union sctp_addr *addr,
299 union sctp_addr_param *param,
300 __u16 port, int iif)
302 addr->v4.sin_family = AF_INET;
303 addr->v4.sin_port = port;
304 addr->v4.sin_addr.s_addr = param->v4.addr.s_addr;
307 /* Initialize an address parameter from a sctp_addr and return the length
308 * of the address parameter.
310 static int sctp_v4_to_addr_param(const union sctp_addr *addr,
311 union sctp_addr_param *param)
313 int length = sizeof(sctp_ipv4addr_param_t);
315 param->v4.param_hdr.type = SCTP_PARAM_IPV4_ADDRESS;
316 param->v4.param_hdr.length = ntohs(length);
317 param->v4.addr.s_addr = addr->v4.sin_addr.s_addr;
319 return length;
322 /* Initialize a sctp_addr from a dst_entry. */
323 static void sctp_v4_dst_saddr(union sctp_addr *saddr, struct dst_entry *dst,
324 unsigned short port)
326 struct rtable *rt = (struct rtable *)dst;
327 saddr->v4.sin_family = AF_INET;
328 saddr->v4.sin_port = port;
329 saddr->v4.sin_addr.s_addr = rt->rt_src;
332 /* Compare two addresses exactly. */
333 static int sctp_v4_cmp_addr(const union sctp_addr *addr1,
334 const union sctp_addr *addr2)
336 if (addr1->sa.sa_family != addr2->sa.sa_family)
337 return 0;
338 if (addr1->v4.sin_port != addr2->v4.sin_port)
339 return 0;
340 if (addr1->v4.sin_addr.s_addr != addr2->v4.sin_addr.s_addr)
341 return 0;
343 return 1;
346 /* Initialize addr struct to INADDR_ANY. */
347 static void sctp_v4_inaddr_any(union sctp_addr *addr, unsigned short port)
349 addr->v4.sin_family = AF_INET;
350 addr->v4.sin_addr.s_addr = INADDR_ANY;
351 addr->v4.sin_port = port;
354 /* Is this a wildcard address? */
355 static int sctp_v4_is_any(const union sctp_addr *addr)
357 return INADDR_ANY == addr->v4.sin_addr.s_addr;
360 /* This function checks if the address is a valid address to be used for
361 * SCTP binding.
363 * Output:
364 * Return 0 - If the address is a non-unicast or an illegal address.
365 * Return 1 - If the address is a unicast.
367 static int sctp_v4_addr_valid(union sctp_addr *addr, struct sctp_sock *sp)
369 /* Is this a non-unicast address or a unusable SCTP address? */
370 if (IS_IPV4_UNUSABLE_ADDRESS(&addr->v4.sin_addr.s_addr))
371 return 0;
373 return 1;
376 /* Should this be available for binding? */
377 static int sctp_v4_available(union sctp_addr *addr, struct sctp_sock *sp)
379 int ret = inet_addr_type(addr->v4.sin_addr.s_addr);
381 /* FIXME: ip_nonlocal_bind sysctl support. */
383 if (addr->v4.sin_addr.s_addr != INADDR_ANY && ret != RTN_LOCAL)
384 return 0;
385 return 1;
388 /* Checking the loopback, private and other address scopes as defined in
389 * RFC 1918. The IPv4 scoping is based on the draft for SCTP IPv4
390 * scoping <draft-stewart-tsvwg-sctp-ipv4-00.txt>.
392 * Level 0 - unusable SCTP addresses
393 * Level 1 - loopback address
394 * Level 2 - link-local addresses
395 * Level 3 - private addresses.
396 * Level 4 - global addresses
397 * For INIT and INIT-ACK address list, let L be the level of
398 * of requested destination address, sender and receiver
399 * SHOULD include all of its addresses with level greater
400 * than or equal to L.
402 static sctp_scope_t sctp_v4_scope(union sctp_addr *addr)
404 sctp_scope_t retval;
406 /* Should IPv4 scoping be a sysctl configurable option
407 * so users can turn it off (default on) for certain
408 * unconventional networking environments?
411 /* Check for unusable SCTP addresses. */
412 if (IS_IPV4_UNUSABLE_ADDRESS(&addr->v4.sin_addr.s_addr)) {
413 retval = SCTP_SCOPE_UNUSABLE;
414 } else if (LOOPBACK(addr->v4.sin_addr.s_addr)) {
415 retval = SCTP_SCOPE_LOOPBACK;
416 } else if (IS_IPV4_LINK_ADDRESS(&addr->v4.sin_addr.s_addr)) {
417 retval = SCTP_SCOPE_LINK;
418 } else if (IS_IPV4_PRIVATE_ADDRESS(&addr->v4.sin_addr.s_addr)) {
419 retval = SCTP_SCOPE_PRIVATE;
420 } else {
421 retval = SCTP_SCOPE_GLOBAL;
424 return retval;
427 /* Returns a valid dst cache entry for the given source and destination ip
428 * addresses. If an association is passed, trys to get a dst entry with a
429 * source address that matches an address in the bind address list.
431 static struct dst_entry *sctp_v4_get_dst(struct sctp_association *asoc,
432 union sctp_addr *daddr,
433 union sctp_addr *saddr)
435 struct rtable *rt;
436 struct flowi fl;
437 struct sctp_bind_addr *bp;
438 rwlock_t *addr_lock;
439 struct sctp_sockaddr_entry *laddr;
440 struct list_head *pos;
441 struct dst_entry *dst = NULL;
442 union sctp_addr dst_saddr;
444 memset(&fl, 0x0, sizeof(struct flowi));
445 fl.fl4_dst = daddr->v4.sin_addr.s_addr;
446 fl.proto = IPPROTO_SCTP;
447 if (asoc) {
448 fl.fl4_tos = RT_CONN_FLAGS(asoc->base.sk);
449 fl.oif = asoc->base.sk->sk_bound_dev_if;
451 if (saddr)
452 fl.fl4_src = saddr->v4.sin_addr.s_addr;
454 SCTP_DEBUG_PRINTK("%s: DST:%u.%u.%u.%u, SRC:%u.%u.%u.%u - ",
455 __FUNCTION__, NIPQUAD(fl.fl4_dst),
456 NIPQUAD(fl.fl4_src));
458 if (!ip_route_output_key(&rt, &fl)) {
459 dst = &rt->u.dst;
462 /* If there is no association or if a source address is passed, no
463 * more validation is required.
465 if (!asoc || saddr)
466 goto out;
468 bp = &asoc->base.bind_addr;
469 addr_lock = &asoc->base.addr_lock;
471 if (dst) {
472 /* Walk through the bind address list and look for a bind
473 * address that matches the source address of the returned dst.
475 sctp_read_lock(addr_lock);
476 list_for_each(pos, &bp->address_list) {
477 laddr = list_entry(pos, struct sctp_sockaddr_entry,
478 list);
479 sctp_v4_dst_saddr(&dst_saddr, dst, bp->port);
480 if (sctp_v4_cmp_addr(&dst_saddr, &laddr->a))
481 goto out_unlock;
483 sctp_read_unlock(addr_lock);
485 /* None of the bound addresses match the source address of the
486 * dst. So release it.
488 dst_release(dst);
489 dst = NULL;
492 /* Walk through the bind address list and try to get a dst that
493 * matches a bind address as the source address.
495 sctp_read_lock(addr_lock);
496 list_for_each(pos, &bp->address_list) {
497 laddr = list_entry(pos, struct sctp_sockaddr_entry, list);
499 if (AF_INET == laddr->a.sa.sa_family) {
500 fl.fl4_src = laddr->a.v4.sin_addr.s_addr;
501 if (!ip_route_output_key(&rt, &fl)) {
502 dst = &rt->u.dst;
503 goto out_unlock;
508 out_unlock:
509 sctp_read_unlock(addr_lock);
510 out:
511 if (dst)
512 SCTP_DEBUG_PRINTK("rt_dst:%u.%u.%u.%u, rt_src:%u.%u.%u.%u\n",
513 NIPQUAD(rt->rt_dst), NIPQUAD(rt->rt_src));
514 else
515 SCTP_DEBUG_PRINTK("NO ROUTE\n");
517 return dst;
520 /* For v4, the source address is cached in the route entry(dst). So no need
521 * to cache it separately and hence this is an empty routine.
523 static void sctp_v4_get_saddr(struct sctp_association *asoc,
524 struct dst_entry *dst,
525 union sctp_addr *daddr,
526 union sctp_addr *saddr)
528 struct rtable *rt = (struct rtable *)dst;
530 if (rt) {
531 saddr->v4.sin_family = AF_INET;
532 saddr->v4.sin_port = asoc->base.bind_addr.port;
533 saddr->v4.sin_addr.s_addr = rt->rt_src;
537 /* What interface did this skb arrive on? */
538 static int sctp_v4_skb_iif(const struct sk_buff *skb)
540 return ((struct rtable *)skb->dst)->rt_iif;
543 /* Was this packet marked by Explicit Congestion Notification? */
544 static int sctp_v4_is_ce(const struct sk_buff *skb)
546 return INET_ECN_is_ce(skb->nh.iph->tos);
549 /* Create and initialize a new sk for the socket returned by accept(). */
550 static struct sock *sctp_v4_create_accept_sk(struct sock *sk,
551 struct sctp_association *asoc)
553 struct inet_sock *inet = inet_sk(sk);
554 struct inet_sock *newinet;
555 struct sock *newsk = sk_alloc(PF_INET, GFP_KERNEL, sk->sk_prot, 1);
557 if (!newsk)
558 goto out;
560 sock_init_data(NULL, newsk);
562 newsk->sk_type = SOCK_STREAM;
564 newsk->sk_no_check = sk->sk_no_check;
565 newsk->sk_reuse = sk->sk_reuse;
566 newsk->sk_shutdown = sk->sk_shutdown;
568 newsk->sk_destruct = inet_sock_destruct;
569 newsk->sk_family = PF_INET;
570 newsk->sk_protocol = IPPROTO_SCTP;
571 newsk->sk_backlog_rcv = sk->sk_prot->backlog_rcv;
572 sock_reset_flag(newsk, SOCK_ZAPPED);
574 newinet = inet_sk(newsk);
576 /* Initialize sk's sport, dport, rcv_saddr and daddr for
577 * getsockname() and getpeername()
579 newinet->sport = inet->sport;
580 newinet->saddr = inet->saddr;
581 newinet->rcv_saddr = inet->rcv_saddr;
582 newinet->dport = htons(asoc->peer.port);
583 newinet->daddr = asoc->peer.primary_addr.v4.sin_addr.s_addr;
584 newinet->pmtudisc = inet->pmtudisc;
585 newinet->id = 0;
587 newinet->uc_ttl = -1;
588 newinet->mc_loop = 1;
589 newinet->mc_ttl = 1;
590 newinet->mc_index = 0;
591 newinet->mc_list = NULL;
593 #ifdef INET_REFCNT_DEBUG
594 atomic_inc(&inet_sock_nr);
595 #endif
597 if (newsk->sk_prot->init(newsk)) {
598 sk_common_release(newsk);
599 newsk = NULL;
602 out:
603 return newsk;
606 /* Map address, empty for v4 family */
607 static void sctp_v4_addr_v4map(struct sctp_sock *sp, union sctp_addr *addr)
609 /* Empty */
612 /* Dump the v4 addr to the seq file. */
613 static void sctp_v4_seq_dump_addr(struct seq_file *seq, union sctp_addr *addr)
615 seq_printf(seq, "%d.%d.%d.%d ", NIPQUAD(addr->v4.sin_addr));
618 /* Event handler for inet address addition/deletion events.
619 * Basically, whenever there is an event, we re-build our local address list.
621 int sctp_inetaddr_event(struct notifier_block *this, unsigned long ev,
622 void *ptr)
624 unsigned long flags;
626 sctp_spin_lock_irqsave(&sctp_local_addr_lock, flags);
627 __sctp_free_local_addr_list();
628 __sctp_get_local_addr_list();
629 sctp_spin_unlock_irqrestore(&sctp_local_addr_lock, flags);
631 return NOTIFY_DONE;
635 * Initialize the control inode/socket with a control endpoint data
636 * structure. This endpoint is reserved exclusively for the OOTB processing.
638 static int sctp_ctl_sock_init(void)
640 int err;
641 sa_family_t family;
643 if (sctp_get_pf_specific(PF_INET6))
644 family = PF_INET6;
645 else
646 family = PF_INET;
648 err = sock_create_kern(family, SOCK_SEQPACKET, IPPROTO_SCTP,
649 &sctp_ctl_socket);
650 if (err < 0) {
651 printk(KERN_ERR
652 "SCTP: Failed to create the SCTP control socket.\n");
653 return err;
655 sctp_ctl_socket->sk->sk_allocation = GFP_ATOMIC;
656 inet_sk(sctp_ctl_socket->sk)->uc_ttl = -1;
658 return 0;
661 /* Register address family specific functions. */
662 int sctp_register_af(struct sctp_af *af)
664 switch (af->sa_family) {
665 case AF_INET:
666 if (sctp_af_v4_specific)
667 return 0;
668 sctp_af_v4_specific = af;
669 break;
670 case AF_INET6:
671 if (sctp_af_v6_specific)
672 return 0;
673 sctp_af_v6_specific = af;
674 break;
675 default:
676 return 0;
679 INIT_LIST_HEAD(&af->list);
680 list_add_tail(&af->list, &sctp_address_families);
681 return 1;
684 /* Get the table of functions for manipulating a particular address
685 * family.
687 struct sctp_af *sctp_get_af_specific(sa_family_t family)
689 switch (family) {
690 case AF_INET:
691 return sctp_af_v4_specific;
692 case AF_INET6:
693 return sctp_af_v6_specific;
694 default:
695 return NULL;
699 /* Common code to initialize a AF_INET msg_name. */
700 static void sctp_inet_msgname(char *msgname, int *addr_len)
702 struct sockaddr_in *sin;
704 sin = (struct sockaddr_in *)msgname;
705 *addr_len = sizeof(struct sockaddr_in);
706 sin->sin_family = AF_INET;
707 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
710 /* Copy the primary address of the peer primary address as the msg_name. */
711 static void sctp_inet_event_msgname(struct sctp_ulpevent *event, char *msgname,
712 int *addr_len)
714 struct sockaddr_in *sin, *sinfrom;
716 if (msgname) {
717 struct sctp_association *asoc;
719 asoc = event->asoc;
720 sctp_inet_msgname(msgname, addr_len);
721 sin = (struct sockaddr_in *)msgname;
722 sinfrom = &asoc->peer.primary_addr.v4;
723 sin->sin_port = htons(asoc->peer.port);
724 sin->sin_addr.s_addr = sinfrom->sin_addr.s_addr;
728 /* Initialize and copy out a msgname from an inbound skb. */
729 static void sctp_inet_skb_msgname(struct sk_buff *skb, char *msgname, int *len)
731 struct sctphdr *sh;
732 struct sockaddr_in *sin;
734 if (msgname) {
735 sctp_inet_msgname(msgname, len);
736 sin = (struct sockaddr_in *)msgname;
737 sh = (struct sctphdr *)skb->h.raw;
738 sin->sin_port = sh->source;
739 sin->sin_addr.s_addr = skb->nh.iph->saddr;
743 /* Do we support this AF? */
744 static int sctp_inet_af_supported(sa_family_t family, struct sctp_sock *sp)
746 /* PF_INET only supports AF_INET addresses. */
747 return (AF_INET == family);
750 /* Address matching with wildcards allowed. */
751 static int sctp_inet_cmp_addr(const union sctp_addr *addr1,
752 const union sctp_addr *addr2,
753 struct sctp_sock *opt)
755 /* PF_INET only supports AF_INET addresses. */
756 if (addr1->sa.sa_family != addr2->sa.sa_family)
757 return 0;
758 if (INADDR_ANY == addr1->v4.sin_addr.s_addr ||
759 INADDR_ANY == addr2->v4.sin_addr.s_addr)
760 return 1;
761 if (addr1->v4.sin_addr.s_addr == addr2->v4.sin_addr.s_addr)
762 return 1;
764 return 0;
767 /* Verify that provided sockaddr looks bindable. Common verification has
768 * already been taken care of.
770 static int sctp_inet_bind_verify(struct sctp_sock *opt, union sctp_addr *addr)
772 return sctp_v4_available(addr, opt);
775 /* Verify that sockaddr looks sendable. Common verification has already
776 * been taken care of.
778 static int sctp_inet_send_verify(struct sctp_sock *opt, union sctp_addr *addr)
780 return 1;
783 /* Fill in Supported Address Type information for INIT and INIT-ACK
784 * chunks. Returns number of addresses supported.
786 static int sctp_inet_supported_addrs(const struct sctp_sock *opt,
787 __u16 *types)
789 types[0] = SCTP_PARAM_IPV4_ADDRESS;
790 return 1;
793 /* Wrapper routine that calls the ip transmit routine. */
794 static inline int sctp_v4_xmit(struct sk_buff *skb,
795 struct sctp_transport *transport, int ipfragok)
797 SCTP_DEBUG_PRINTK("%s: skb:%p, len:%d, "
798 "src:%u.%u.%u.%u, dst:%u.%u.%u.%u\n",
799 __FUNCTION__, skb, skb->len,
800 NIPQUAD(((struct rtable *)skb->dst)->rt_src),
801 NIPQUAD(((struct rtable *)skb->dst)->rt_dst));
803 SCTP_INC_STATS(SCTP_MIB_OUTSCTPPACKS);
804 return ip_queue_xmit(skb, ipfragok);
807 static struct sctp_af sctp_ipv4_specific;
809 static struct sctp_pf sctp_pf_inet = {
810 .event_msgname = sctp_inet_event_msgname,
811 .skb_msgname = sctp_inet_skb_msgname,
812 .af_supported = sctp_inet_af_supported,
813 .cmp_addr = sctp_inet_cmp_addr,
814 .bind_verify = sctp_inet_bind_verify,
815 .send_verify = sctp_inet_send_verify,
816 .supported_addrs = sctp_inet_supported_addrs,
817 .create_accept_sk = sctp_v4_create_accept_sk,
818 .addr_v4map = sctp_v4_addr_v4map,
819 .af = &sctp_ipv4_specific,
822 /* Notifier for inetaddr addition/deletion events. */
823 static struct notifier_block sctp_inetaddr_notifier = {
824 .notifier_call = sctp_inetaddr_event,
827 /* Socket operations. */
828 static struct proto_ops inet_seqpacket_ops = {
829 .family = PF_INET,
830 .owner = THIS_MODULE,
831 .release = inet_release, /* Needs to be wrapped... */
832 .bind = inet_bind,
833 .connect = inet_dgram_connect,
834 .socketpair = sock_no_socketpair,
835 .accept = inet_accept,
836 .getname = inet_getname, /* Semantics are different. */
837 .poll = sctp_poll,
838 .ioctl = inet_ioctl,
839 .listen = sctp_inet_listen,
840 .shutdown = inet_shutdown, /* Looks harmless. */
841 .setsockopt = sock_common_setsockopt, /* IP_SOL IP_OPTION is a problem. */
842 .getsockopt = sock_common_getsockopt,
843 .sendmsg = inet_sendmsg,
844 .recvmsg = sock_common_recvmsg,
845 .mmap = sock_no_mmap,
846 .sendpage = sock_no_sendpage,
849 /* Registration with AF_INET family. */
850 static struct inet_protosw sctp_seqpacket_protosw = {
851 .type = SOCK_SEQPACKET,
852 .protocol = IPPROTO_SCTP,
853 .prot = &sctp_prot,
854 .ops = &inet_seqpacket_ops,
855 .capability = -1,
856 .no_check = 0,
857 .flags = SCTP_PROTOSW_FLAG
859 static struct inet_protosw sctp_stream_protosw = {
860 .type = SOCK_STREAM,
861 .protocol = IPPROTO_SCTP,
862 .prot = &sctp_prot,
863 .ops = &inet_seqpacket_ops,
864 .capability = -1,
865 .no_check = 0,
866 .flags = SCTP_PROTOSW_FLAG
869 /* Register with IP layer. */
870 static struct net_protocol sctp_protocol = {
871 .handler = sctp_rcv,
872 .err_handler = sctp_v4_err,
873 .no_policy = 1,
876 /* IPv4 address related functions. */
877 static struct sctp_af sctp_ipv4_specific = {
878 .sctp_xmit = sctp_v4_xmit,
879 .setsockopt = ip_setsockopt,
880 .getsockopt = ip_getsockopt,
881 .get_dst = sctp_v4_get_dst,
882 .get_saddr = sctp_v4_get_saddr,
883 .copy_addrlist = sctp_v4_copy_addrlist,
884 .from_skb = sctp_v4_from_skb,
885 .from_sk = sctp_v4_from_sk,
886 .to_sk_saddr = sctp_v4_to_sk_saddr,
887 .to_sk_daddr = sctp_v4_to_sk_daddr,
888 .from_addr_param= sctp_v4_from_addr_param,
889 .to_addr_param = sctp_v4_to_addr_param,
890 .dst_saddr = sctp_v4_dst_saddr,
891 .cmp_addr = sctp_v4_cmp_addr,
892 .addr_valid = sctp_v4_addr_valid,
893 .inaddr_any = sctp_v4_inaddr_any,
894 .is_any = sctp_v4_is_any,
895 .available = sctp_v4_available,
896 .scope = sctp_v4_scope,
897 .skb_iif = sctp_v4_skb_iif,
898 .is_ce = sctp_v4_is_ce,
899 .seq_dump_addr = sctp_v4_seq_dump_addr,
900 .net_header_len = sizeof(struct iphdr),
901 .sockaddr_len = sizeof(struct sockaddr_in),
902 .sa_family = AF_INET,
905 struct sctp_pf *sctp_get_pf_specific(sa_family_t family) {
907 switch (family) {
908 case PF_INET:
909 return sctp_pf_inet_specific;
910 case PF_INET6:
911 return sctp_pf_inet6_specific;
912 default:
913 return NULL;
917 /* Register the PF specific function table. */
918 int sctp_register_pf(struct sctp_pf *pf, sa_family_t family)
920 switch (family) {
921 case PF_INET:
922 if (sctp_pf_inet_specific)
923 return 0;
924 sctp_pf_inet_specific = pf;
925 break;
926 case PF_INET6:
927 if (sctp_pf_inet6_specific)
928 return 0;
929 sctp_pf_inet6_specific = pf;
930 break;
931 default:
932 return 0;
934 return 1;
937 static int __init init_sctp_mibs(void)
939 sctp_statistics[0] = alloc_percpu(struct sctp_mib);
940 if (!sctp_statistics[0])
941 return -ENOMEM;
942 sctp_statistics[1] = alloc_percpu(struct sctp_mib);
943 if (!sctp_statistics[1]) {
944 free_percpu(sctp_statistics[0]);
945 return -ENOMEM;
947 return 0;
951 static void cleanup_sctp_mibs(void)
953 free_percpu(sctp_statistics[0]);
954 free_percpu(sctp_statistics[1]);
957 /* Initialize the universe into something sensible. */
958 SCTP_STATIC __init int sctp_init(void)
960 int i;
961 int status = -EINVAL;
962 unsigned long goal;
963 int order;
965 /* SCTP_DEBUG sanity check. */
966 if (!sctp_sanity_check())
967 goto out;
969 status = proto_register(&sctp_prot, 1);
970 if (status)
971 goto out;
973 /* Add SCTP to inet_protos hash table. */
974 status = -EAGAIN;
975 if (inet_add_protocol(&sctp_protocol, IPPROTO_SCTP) < 0)
976 goto err_add_protocol;
978 /* Add SCTP(TCP and UDP style) to inetsw linked list. */
979 inet_register_protosw(&sctp_seqpacket_protosw);
980 inet_register_protosw(&sctp_stream_protosw);
982 /* Allocate a cache pools. */
983 status = -ENOBUFS;
984 sctp_bucket_cachep = kmem_cache_create("sctp_bind_bucket",
985 sizeof(struct sctp_bind_bucket),
986 0, SLAB_HWCACHE_ALIGN,
987 NULL, NULL);
989 if (!sctp_bucket_cachep)
990 goto err_bucket_cachep;
992 sctp_chunk_cachep = kmem_cache_create("sctp_chunk",
993 sizeof(struct sctp_chunk),
994 0, SLAB_HWCACHE_ALIGN,
995 NULL, NULL);
996 if (!sctp_chunk_cachep)
997 goto err_chunk_cachep;
999 /* Allocate and initialise sctp mibs. */
1000 status = init_sctp_mibs();
1001 if (status)
1002 goto err_init_mibs;
1004 /* Initialize proc fs directory. */
1005 status = sctp_proc_init();
1006 if (status)
1007 goto err_init_proc;
1009 /* Initialize object count debugging. */
1010 sctp_dbg_objcnt_init();
1012 /* Initialize the SCTP specific PF functions. */
1013 sctp_register_pf(&sctp_pf_inet, PF_INET);
1015 * 14. Suggested SCTP Protocol Parameter Values
1017 /* The following protocol parameters are RECOMMENDED: */
1018 /* RTO.Initial - 3 seconds */
1019 sctp_rto_initial = SCTP_RTO_INITIAL;
1020 /* RTO.Min - 1 second */
1021 sctp_rto_min = SCTP_RTO_MIN;
1022 /* RTO.Max - 60 seconds */
1023 sctp_rto_max = SCTP_RTO_MAX;
1024 /* RTO.Alpha - 1/8 */
1025 sctp_rto_alpha = SCTP_RTO_ALPHA;
1026 /* RTO.Beta - 1/4 */
1027 sctp_rto_beta = SCTP_RTO_BETA;
1029 /* Valid.Cookie.Life - 60 seconds */
1030 sctp_valid_cookie_life = 60 * HZ;
1032 /* Whether Cookie Preservative is enabled(1) or not(0) */
1033 sctp_cookie_preserve_enable = 1;
1035 /* Max.Burst - 4 */
1036 sctp_max_burst = SCTP_MAX_BURST;
1038 /* Association.Max.Retrans - 10 attempts
1039 * Path.Max.Retrans - 5 attempts (per destination address)
1040 * Max.Init.Retransmits - 8 attempts
1042 sctp_max_retrans_association = 10;
1043 sctp_max_retrans_path = 5;
1044 sctp_max_retrans_init = 8;
1046 /* Sendbuffer growth - do per-socket accounting */
1047 sctp_sndbuf_policy = 0;
1049 /* HB.interval - 30 seconds */
1050 sctp_hb_interval = 30 * HZ;
1052 /* Implementation specific variables. */
1054 /* Initialize default stream count setup information. */
1055 sctp_max_instreams = SCTP_DEFAULT_INSTREAMS;
1056 sctp_max_outstreams = SCTP_DEFAULT_OUTSTREAMS;
1058 /* Initialize handle used for association ids. */
1059 idr_init(&sctp_assocs_id);
1061 /* Size and allocate the association hash table.
1062 * The methodology is similar to that of the tcp hash tables.
1064 if (num_physpages >= (128 * 1024))
1065 goal = num_physpages >> (22 - PAGE_SHIFT);
1066 else
1067 goal = num_physpages >> (24 - PAGE_SHIFT);
1069 for (order = 0; (1UL << order) < goal; order++)
1072 do {
1073 sctp_assoc_hashsize = (1UL << order) * PAGE_SIZE /
1074 sizeof(struct sctp_hashbucket);
1075 if ((sctp_assoc_hashsize > (64 * 1024)) && order > 0)
1076 continue;
1077 sctp_assoc_hashtable = (struct sctp_hashbucket *)
1078 __get_free_pages(GFP_ATOMIC, order);
1079 } while (!sctp_assoc_hashtable && --order > 0);
1080 if (!sctp_assoc_hashtable) {
1081 printk(KERN_ERR "SCTP: Failed association hash alloc.\n");
1082 status = -ENOMEM;
1083 goto err_ahash_alloc;
1085 for (i = 0; i < sctp_assoc_hashsize; i++) {
1086 rwlock_init(&sctp_assoc_hashtable[i].lock);
1087 sctp_assoc_hashtable[i].chain = NULL;
1090 /* Allocate and initialize the endpoint hash table. */
1091 sctp_ep_hashsize = 64;
1092 sctp_ep_hashtable = (struct sctp_hashbucket *)
1093 kmalloc(64 * sizeof(struct sctp_hashbucket), GFP_KERNEL);
1094 if (!sctp_ep_hashtable) {
1095 printk(KERN_ERR "SCTP: Failed endpoint_hash alloc.\n");
1096 status = -ENOMEM;
1097 goto err_ehash_alloc;
1099 for (i = 0; i < sctp_ep_hashsize; i++) {
1100 rwlock_init(&sctp_ep_hashtable[i].lock);
1101 sctp_ep_hashtable[i].chain = NULL;
1104 /* Allocate and initialize the SCTP port hash table. */
1105 do {
1106 sctp_port_hashsize = (1UL << order) * PAGE_SIZE /
1107 sizeof(struct sctp_bind_hashbucket);
1108 if ((sctp_port_hashsize > (64 * 1024)) && order > 0)
1109 continue;
1110 sctp_port_hashtable = (struct sctp_bind_hashbucket *)
1111 __get_free_pages(GFP_ATOMIC, order);
1112 } while (!sctp_port_hashtable && --order > 0);
1113 if (!sctp_port_hashtable) {
1114 printk(KERN_ERR "SCTP: Failed bind hash alloc.");
1115 status = -ENOMEM;
1116 goto err_bhash_alloc;
1118 for (i = 0; i < sctp_port_hashsize; i++) {
1119 spin_lock_init(&sctp_port_hashtable[i].lock);
1120 sctp_port_hashtable[i].chain = NULL;
1123 spin_lock_init(&sctp_port_alloc_lock);
1124 sctp_port_rover = sysctl_local_port_range[0] - 1;
1126 printk(KERN_INFO "SCTP: Hash tables configured "
1127 "(established %d bind %d)\n",
1128 sctp_assoc_hashsize, sctp_port_hashsize);
1130 /* Disable ADDIP by default. */
1131 sctp_addip_enable = 0;
1133 /* Enable PR-SCTP by default. */
1134 sctp_prsctp_enable = 1;
1136 sctp_sysctl_register();
1138 INIT_LIST_HEAD(&sctp_address_families);
1139 sctp_register_af(&sctp_ipv4_specific);
1141 status = sctp_v6_init();
1142 if (status)
1143 goto err_v6_init;
1145 /* Initialize the control inode/socket for handling OOTB packets. */
1146 if ((status = sctp_ctl_sock_init())) {
1147 printk (KERN_ERR
1148 "SCTP: Failed to initialize the SCTP control sock.\n");
1149 goto err_ctl_sock_init;
1152 /* Initialize the local address list. */
1153 INIT_LIST_HEAD(&sctp_local_addr_list);
1154 spin_lock_init(&sctp_local_addr_lock);
1156 /* Register notifier for inet address additions/deletions. */
1157 register_inetaddr_notifier(&sctp_inetaddr_notifier);
1159 sctp_get_local_addr_list();
1161 __unsafe(THIS_MODULE);
1162 status = 0;
1163 out:
1164 return status;
1165 err_ctl_sock_init:
1166 sctp_v6_exit();
1167 err_v6_init:
1168 sctp_sysctl_unregister();
1169 list_del(&sctp_ipv4_specific.list);
1170 free_pages((unsigned long)sctp_port_hashtable,
1171 get_order(sctp_port_hashsize *
1172 sizeof(struct sctp_bind_hashbucket)));
1173 err_bhash_alloc:
1174 kfree(sctp_ep_hashtable);
1175 err_ehash_alloc:
1176 free_pages((unsigned long)sctp_assoc_hashtable,
1177 get_order(sctp_assoc_hashsize *
1178 sizeof(struct sctp_hashbucket)));
1179 err_ahash_alloc:
1180 sctp_dbg_objcnt_exit();
1181 err_init_proc:
1182 sctp_proc_exit();
1183 cleanup_sctp_mibs();
1184 err_init_mibs:
1185 kmem_cache_destroy(sctp_chunk_cachep);
1186 err_chunk_cachep:
1187 kmem_cache_destroy(sctp_bucket_cachep);
1188 err_bucket_cachep:
1189 inet_del_protocol(&sctp_protocol, IPPROTO_SCTP);
1190 inet_unregister_protosw(&sctp_seqpacket_protosw);
1191 inet_unregister_protosw(&sctp_stream_protosw);
1192 err_add_protocol:
1193 proto_unregister(&sctp_prot);
1194 goto out;
1197 /* Exit handler for the SCTP protocol. */
1198 SCTP_STATIC __exit void sctp_exit(void)
1200 /* BUG. This should probably do something useful like clean
1201 * up all the remaining associations and all that memory.
1204 /* Unregister notifier for inet address additions/deletions. */
1205 unregister_inetaddr_notifier(&sctp_inetaddr_notifier);
1207 /* Free the local address list. */
1208 sctp_free_local_addr_list();
1210 /* Free the control endpoint. */
1211 sock_release(sctp_ctl_socket);
1213 sctp_v6_exit();
1214 sctp_sysctl_unregister();
1215 list_del(&sctp_ipv4_specific.list);
1217 free_pages((unsigned long)sctp_assoc_hashtable,
1218 get_order(sctp_assoc_hashsize *
1219 sizeof(struct sctp_hashbucket)));
1220 kfree(sctp_ep_hashtable);
1221 free_pages((unsigned long)sctp_port_hashtable,
1222 get_order(sctp_port_hashsize *
1223 sizeof(struct sctp_bind_hashbucket)));
1225 kmem_cache_destroy(sctp_chunk_cachep);
1226 kmem_cache_destroy(sctp_bucket_cachep);
1228 sctp_dbg_objcnt_exit();
1229 sctp_proc_exit();
1230 cleanup_sctp_mibs();
1232 inet_del_protocol(&sctp_protocol, IPPROTO_SCTP);
1233 inet_unregister_protosw(&sctp_seqpacket_protosw);
1234 inet_unregister_protosw(&sctp_stream_protosw);
1235 proto_unregister(&sctp_prot);
1238 module_init(sctp_init);
1239 module_exit(sctp_exit);
1241 MODULE_AUTHOR("Linux Kernel SCTP developers <lksctp-developers@lists.sourceforge.net>");
1242 MODULE_DESCRIPTION("Support for the SCTP protocol (RFC2960)");
1243 MODULE_LICENSE("GPL");