[PATCH] x86: do_IRQ(): check irq number
[linux-2.6/mini2440.git] / net / sctp / protocol.c
blob816c033d78865464a8826c8ab2ae102b633a2e31
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;
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 kmem_cache_t *sctp_chunk_cachep __read_mostly;
83 kmem_cache_t *sctp_bucket_cachep __read_mostly;
85 extern int sctp_snmp_proc_init(void);
86 extern int sctp_snmp_proc_exit(void);
87 extern int sctp_eps_proc_init(void);
88 extern int sctp_eps_proc_exit(void);
89 extern int sctp_assocs_proc_init(void);
90 extern int sctp_assocs_proc_exit(void);
92 /* Return the address of the control sock. */
93 struct sock *sctp_get_ctl_sock(void)
95 return sctp_ctl_socket->sk;
98 /* Set up the proc fs entry for the SCTP protocol. */
99 static __init int sctp_proc_init(void)
101 if (!proc_net_sctp) {
102 struct proc_dir_entry *ent;
103 ent = proc_mkdir("net/sctp", NULL);
104 if (ent) {
105 ent->owner = THIS_MODULE;
106 proc_net_sctp = ent;
107 } else
108 goto out_nomem;
111 if (sctp_snmp_proc_init())
112 goto out_nomem;
113 if (sctp_eps_proc_init())
114 goto out_nomem;
115 if (sctp_assocs_proc_init())
116 goto out_nomem;
118 return 0;
120 out_nomem:
121 return -ENOMEM;
124 /* Clean up the proc fs entry for the SCTP protocol.
125 * Note: Do not make this __exit as it is used in the init error
126 * path.
128 static void sctp_proc_exit(void)
130 sctp_snmp_proc_exit();
131 sctp_eps_proc_exit();
132 sctp_assocs_proc_exit();
134 if (proc_net_sctp) {
135 proc_net_sctp = NULL;
136 remove_proc_entry("net/sctp", NULL);
140 /* Private helper to extract ipv4 address and stash them in
141 * the protocol structure.
143 static void sctp_v4_copy_addrlist(struct list_head *addrlist,
144 struct net_device *dev)
146 struct in_device *in_dev;
147 struct in_ifaddr *ifa;
148 struct sctp_sockaddr_entry *addr;
150 rcu_read_lock();
151 if ((in_dev = __in_dev_get_rcu(dev)) == NULL) {
152 rcu_read_unlock();
153 return;
156 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
157 /* Add the address to the local list. */
158 addr = t_new(struct sctp_sockaddr_entry, GFP_ATOMIC);
159 if (addr) {
160 addr->a.v4.sin_family = AF_INET;
161 addr->a.v4.sin_port = 0;
162 addr->a.v4.sin_addr.s_addr = ifa->ifa_local;
163 list_add_tail(&addr->list, addrlist);
167 rcu_read_unlock();
170 /* Extract our IP addresses from the system and stash them in the
171 * protocol structure.
173 static void __sctp_get_local_addr_list(void)
175 struct net_device *dev;
176 struct list_head *pos;
177 struct sctp_af *af;
179 read_lock(&dev_base_lock);
180 for (dev = dev_base; dev; dev = dev->next) {
181 __list_for_each(pos, &sctp_address_families) {
182 af = list_entry(pos, struct sctp_af, list);
183 af->copy_addrlist(&sctp_local_addr_list, dev);
186 read_unlock(&dev_base_lock);
189 static void sctp_get_local_addr_list(void)
191 unsigned long flags;
193 sctp_spin_lock_irqsave(&sctp_local_addr_lock, flags);
194 __sctp_get_local_addr_list();
195 sctp_spin_unlock_irqrestore(&sctp_local_addr_lock, flags);
198 /* Free the existing local addresses. */
199 static void __sctp_free_local_addr_list(void)
201 struct sctp_sockaddr_entry *addr;
202 struct list_head *pos, *temp;
204 list_for_each_safe(pos, temp, &sctp_local_addr_list) {
205 addr = list_entry(pos, struct sctp_sockaddr_entry, list);
206 list_del(pos);
207 kfree(addr);
211 /* Free the existing local addresses. */
212 static void sctp_free_local_addr_list(void)
214 unsigned long flags;
216 sctp_spin_lock_irqsave(&sctp_local_addr_lock, flags);
217 __sctp_free_local_addr_list();
218 sctp_spin_unlock_irqrestore(&sctp_local_addr_lock, flags);
221 /* Copy the local addresses which are valid for 'scope' into 'bp'. */
222 int sctp_copy_local_addr_list(struct sctp_bind_addr *bp, sctp_scope_t scope,
223 gfp_t gfp, int copy_flags)
225 struct sctp_sockaddr_entry *addr;
226 int error = 0;
227 struct list_head *pos;
228 unsigned long flags;
230 sctp_spin_lock_irqsave(&sctp_local_addr_lock, flags);
231 list_for_each(pos, &sctp_local_addr_list) {
232 addr = list_entry(pos, struct sctp_sockaddr_entry, list);
233 if (sctp_in_scope(&addr->a, scope)) {
234 /* Now that the address is in scope, check to see if
235 * the address type is really supported by the local
236 * sock as well as the remote peer.
238 if ((((AF_INET == addr->a.sa.sa_family) &&
239 (copy_flags & SCTP_ADDR4_PEERSUPP))) ||
240 (((AF_INET6 == addr->a.sa.sa_family) &&
241 (copy_flags & SCTP_ADDR6_ALLOWED) &&
242 (copy_flags & SCTP_ADDR6_PEERSUPP)))) {
243 error = sctp_add_bind_addr(bp, &addr->a,
244 GFP_ATOMIC);
245 if (error)
246 goto end_copy;
251 end_copy:
252 sctp_spin_unlock_irqrestore(&sctp_local_addr_lock, flags);
253 return error;
256 /* Initialize a sctp_addr from in incoming skb. */
257 static void sctp_v4_from_skb(union sctp_addr *addr, struct sk_buff *skb,
258 int is_saddr)
260 void *from;
261 __u16 *port;
262 struct sctphdr *sh;
264 port = &addr->v4.sin_port;
265 addr->v4.sin_family = AF_INET;
267 sh = (struct sctphdr *) skb->h.raw;
268 if (is_saddr) {
269 *port = ntohs(sh->source);
270 from = &skb->nh.iph->saddr;
271 } else {
272 *port = ntohs(sh->dest);
273 from = &skb->nh.iph->daddr;
275 memcpy(&addr->v4.sin_addr.s_addr, from, sizeof(struct in_addr));
278 /* Initialize an sctp_addr from a socket. */
279 static void sctp_v4_from_sk(union sctp_addr *addr, struct sock *sk)
281 addr->v4.sin_family = AF_INET;
282 addr->v4.sin_port = inet_sk(sk)->num;
283 addr->v4.sin_addr.s_addr = inet_sk(sk)->rcv_saddr;
286 /* Initialize sk->sk_rcv_saddr from sctp_addr. */
287 static void sctp_v4_to_sk_saddr(union sctp_addr *addr, struct sock *sk)
289 inet_sk(sk)->rcv_saddr = addr->v4.sin_addr.s_addr;
292 /* Initialize sk->sk_daddr from sctp_addr. */
293 static void sctp_v4_to_sk_daddr(union sctp_addr *addr, struct sock *sk)
295 inet_sk(sk)->daddr = addr->v4.sin_addr.s_addr;
298 /* Initialize a sctp_addr from an address parameter. */
299 static void sctp_v4_from_addr_param(union sctp_addr *addr,
300 union sctp_addr_param *param,
301 __u16 port, int iif)
303 addr->v4.sin_family = AF_INET;
304 addr->v4.sin_port = port;
305 addr->v4.sin_addr.s_addr = param->v4.addr.s_addr;
308 /* Initialize an address parameter from a sctp_addr and return the length
309 * of the address parameter.
311 static int sctp_v4_to_addr_param(const union sctp_addr *addr,
312 union sctp_addr_param *param)
314 int length = sizeof(sctp_ipv4addr_param_t);
316 param->v4.param_hdr.type = SCTP_PARAM_IPV4_ADDRESS;
317 param->v4.param_hdr.length = ntohs(length);
318 param->v4.addr.s_addr = addr->v4.sin_addr.s_addr;
320 return length;
323 /* Initialize a sctp_addr from a dst_entry. */
324 static void sctp_v4_dst_saddr(union sctp_addr *saddr, struct dst_entry *dst,
325 unsigned short port)
327 struct rtable *rt = (struct rtable *)dst;
328 saddr->v4.sin_family = AF_INET;
329 saddr->v4.sin_port = port;
330 saddr->v4.sin_addr.s_addr = rt->rt_src;
333 /* Compare two addresses exactly. */
334 static int sctp_v4_cmp_addr(const union sctp_addr *addr1,
335 const union sctp_addr *addr2)
337 if (addr1->sa.sa_family != addr2->sa.sa_family)
338 return 0;
339 if (addr1->v4.sin_port != addr2->v4.sin_port)
340 return 0;
341 if (addr1->v4.sin_addr.s_addr != addr2->v4.sin_addr.s_addr)
342 return 0;
344 return 1;
347 /* Initialize addr struct to INADDR_ANY. */
348 static void sctp_v4_inaddr_any(union sctp_addr *addr, unsigned short port)
350 addr->v4.sin_family = AF_INET;
351 addr->v4.sin_addr.s_addr = INADDR_ANY;
352 addr->v4.sin_port = port;
355 /* Is this a wildcard address? */
356 static int sctp_v4_is_any(const union sctp_addr *addr)
358 return INADDR_ANY == addr->v4.sin_addr.s_addr;
361 /* This function checks if the address is a valid address to be used for
362 * SCTP binding.
364 * Output:
365 * Return 0 - If the address is a non-unicast or an illegal address.
366 * Return 1 - If the address is a unicast.
368 static int sctp_v4_addr_valid(union sctp_addr *addr,
369 struct sctp_sock *sp,
370 const struct sk_buff *skb)
372 /* Is this a non-unicast address or a unusable SCTP address? */
373 if (IS_IPV4_UNUSABLE_ADDRESS(&addr->v4.sin_addr.s_addr))
374 return 0;
376 /* Is this a broadcast address? */
377 if (skb && ((struct rtable *)skb->dst)->rt_flags & RTCF_BROADCAST)
378 return 0;
380 return 1;
383 /* Should this be available for binding? */
384 static int sctp_v4_available(union sctp_addr *addr, struct sctp_sock *sp)
386 int ret = inet_addr_type(addr->v4.sin_addr.s_addr);
389 if (addr->v4.sin_addr.s_addr != INADDR_ANY &&
390 ret != RTN_LOCAL &&
391 !sp->inet.freebind &&
392 !sysctl_ip_nonlocal_bind)
393 return 0;
395 return 1;
398 /* Checking the loopback, private and other address scopes as defined in
399 * RFC 1918. The IPv4 scoping is based on the draft for SCTP IPv4
400 * scoping <draft-stewart-tsvwg-sctp-ipv4-00.txt>.
402 * Level 0 - unusable SCTP addresses
403 * Level 1 - loopback address
404 * Level 2 - link-local addresses
405 * Level 3 - private addresses.
406 * Level 4 - global addresses
407 * For INIT and INIT-ACK address list, let L be the level of
408 * of requested destination address, sender and receiver
409 * SHOULD include all of its addresses with level greater
410 * than or equal to L.
412 static sctp_scope_t sctp_v4_scope(union sctp_addr *addr)
414 sctp_scope_t retval;
416 /* Should IPv4 scoping be a sysctl configurable option
417 * so users can turn it off (default on) for certain
418 * unconventional networking environments?
421 /* Check for unusable SCTP addresses. */
422 if (IS_IPV4_UNUSABLE_ADDRESS(&addr->v4.sin_addr.s_addr)) {
423 retval = SCTP_SCOPE_UNUSABLE;
424 } else if (LOOPBACK(addr->v4.sin_addr.s_addr)) {
425 retval = SCTP_SCOPE_LOOPBACK;
426 } else if (IS_IPV4_LINK_ADDRESS(&addr->v4.sin_addr.s_addr)) {
427 retval = SCTP_SCOPE_LINK;
428 } else if (IS_IPV4_PRIVATE_ADDRESS(&addr->v4.sin_addr.s_addr)) {
429 retval = SCTP_SCOPE_PRIVATE;
430 } else {
431 retval = SCTP_SCOPE_GLOBAL;
434 return retval;
437 /* Returns a valid dst cache entry for the given source and destination ip
438 * addresses. If an association is passed, trys to get a dst entry with a
439 * source address that matches an address in the bind address list.
441 static struct dst_entry *sctp_v4_get_dst(struct sctp_association *asoc,
442 union sctp_addr *daddr,
443 union sctp_addr *saddr)
445 struct rtable *rt;
446 struct flowi fl;
447 struct sctp_bind_addr *bp;
448 rwlock_t *addr_lock;
449 struct sctp_sockaddr_entry *laddr;
450 struct list_head *pos;
451 struct dst_entry *dst = NULL;
452 union sctp_addr dst_saddr;
454 memset(&fl, 0x0, sizeof(struct flowi));
455 fl.fl4_dst = daddr->v4.sin_addr.s_addr;
456 fl.proto = IPPROTO_SCTP;
457 if (asoc) {
458 fl.fl4_tos = RT_CONN_FLAGS(asoc->base.sk);
459 fl.oif = asoc->base.sk->sk_bound_dev_if;
461 if (saddr)
462 fl.fl4_src = saddr->v4.sin_addr.s_addr;
464 SCTP_DEBUG_PRINTK("%s: DST:%u.%u.%u.%u, SRC:%u.%u.%u.%u - ",
465 __FUNCTION__, NIPQUAD(fl.fl4_dst),
466 NIPQUAD(fl.fl4_src));
468 if (!ip_route_output_key(&rt, &fl)) {
469 dst = &rt->u.dst;
472 /* If there is no association or if a source address is passed, no
473 * more validation is required.
475 if (!asoc || saddr)
476 goto out;
478 bp = &asoc->base.bind_addr;
479 addr_lock = &asoc->base.addr_lock;
481 if (dst) {
482 /* Walk through the bind address list and look for a bind
483 * address that matches the source address of the returned dst.
485 sctp_read_lock(addr_lock);
486 list_for_each(pos, &bp->address_list) {
487 laddr = list_entry(pos, struct sctp_sockaddr_entry,
488 list);
489 sctp_v4_dst_saddr(&dst_saddr, dst, bp->port);
490 if (sctp_v4_cmp_addr(&dst_saddr, &laddr->a))
491 goto out_unlock;
493 sctp_read_unlock(addr_lock);
495 /* None of the bound addresses match the source address of the
496 * dst. So release it.
498 dst_release(dst);
499 dst = NULL;
502 /* Walk through the bind address list and try to get a dst that
503 * matches a bind address as the source address.
505 sctp_read_lock(addr_lock);
506 list_for_each(pos, &bp->address_list) {
507 laddr = list_entry(pos, struct sctp_sockaddr_entry, list);
509 if (AF_INET == laddr->a.sa.sa_family) {
510 fl.fl4_src = laddr->a.v4.sin_addr.s_addr;
511 if (!ip_route_output_key(&rt, &fl)) {
512 dst = &rt->u.dst;
513 goto out_unlock;
518 out_unlock:
519 sctp_read_unlock(addr_lock);
520 out:
521 if (dst)
522 SCTP_DEBUG_PRINTK("rt_dst:%u.%u.%u.%u, rt_src:%u.%u.%u.%u\n",
523 NIPQUAD(rt->rt_dst), NIPQUAD(rt->rt_src));
524 else
525 SCTP_DEBUG_PRINTK("NO ROUTE\n");
527 return dst;
530 /* For v4, the source address is cached in the route entry(dst). So no need
531 * to cache it separately and hence this is an empty routine.
533 static void sctp_v4_get_saddr(struct sctp_association *asoc,
534 struct dst_entry *dst,
535 union sctp_addr *daddr,
536 union sctp_addr *saddr)
538 struct rtable *rt = (struct rtable *)dst;
540 if (!asoc)
541 return;
543 if (rt) {
544 saddr->v4.sin_family = AF_INET;
545 saddr->v4.sin_port = asoc->base.bind_addr.port;
546 saddr->v4.sin_addr.s_addr = rt->rt_src;
550 /* What interface did this skb arrive on? */
551 static int sctp_v4_skb_iif(const struct sk_buff *skb)
553 return ((struct rtable *)skb->dst)->rt_iif;
556 /* Was this packet marked by Explicit Congestion Notification? */
557 static int sctp_v4_is_ce(const struct sk_buff *skb)
559 return INET_ECN_is_ce(skb->nh.iph->tos);
562 /* Create and initialize a new sk for the socket returned by accept(). */
563 static struct sock *sctp_v4_create_accept_sk(struct sock *sk,
564 struct sctp_association *asoc)
566 struct inet_sock *inet = inet_sk(sk);
567 struct inet_sock *newinet;
568 struct sock *newsk = sk_alloc(PF_INET, GFP_KERNEL, sk->sk_prot, 1);
570 if (!newsk)
571 goto out;
573 sock_init_data(NULL, newsk);
575 newsk->sk_type = SOCK_STREAM;
577 newsk->sk_no_check = sk->sk_no_check;
578 newsk->sk_reuse = sk->sk_reuse;
579 newsk->sk_shutdown = sk->sk_shutdown;
581 newsk->sk_destruct = inet_sock_destruct;
582 newsk->sk_family = PF_INET;
583 newsk->sk_protocol = IPPROTO_SCTP;
584 newsk->sk_backlog_rcv = sk->sk_prot->backlog_rcv;
585 sock_reset_flag(newsk, SOCK_ZAPPED);
587 newinet = inet_sk(newsk);
589 /* Initialize sk's sport, dport, rcv_saddr and daddr for
590 * getsockname() and getpeername()
592 newinet->sport = inet->sport;
593 newinet->saddr = inet->saddr;
594 newinet->rcv_saddr = inet->rcv_saddr;
595 newinet->dport = htons(asoc->peer.port);
596 newinet->daddr = asoc->peer.primary_addr.v4.sin_addr.s_addr;
597 newinet->pmtudisc = inet->pmtudisc;
598 newinet->id = 0;
600 newinet->uc_ttl = -1;
601 newinet->mc_loop = 1;
602 newinet->mc_ttl = 1;
603 newinet->mc_index = 0;
604 newinet->mc_list = NULL;
606 sk_refcnt_debug_inc(newsk);
608 if (newsk->sk_prot->init(newsk)) {
609 sk_common_release(newsk);
610 newsk = NULL;
613 out:
614 return newsk;
617 /* Map address, empty for v4 family */
618 static void sctp_v4_addr_v4map(struct sctp_sock *sp, union sctp_addr *addr)
620 /* Empty */
623 /* Dump the v4 addr to the seq file. */
624 static void sctp_v4_seq_dump_addr(struct seq_file *seq, union sctp_addr *addr)
626 seq_printf(seq, "%d.%d.%d.%d ", NIPQUAD(addr->v4.sin_addr));
629 /* Event handler for inet address addition/deletion events.
630 * Basically, whenever there is an event, we re-build our local address list.
632 int sctp_inetaddr_event(struct notifier_block *this, unsigned long ev,
633 void *ptr)
635 unsigned long flags;
637 sctp_spin_lock_irqsave(&sctp_local_addr_lock, flags);
638 __sctp_free_local_addr_list();
639 __sctp_get_local_addr_list();
640 sctp_spin_unlock_irqrestore(&sctp_local_addr_lock, flags);
642 return NOTIFY_DONE;
646 * Initialize the control inode/socket with a control endpoint data
647 * structure. This endpoint is reserved exclusively for the OOTB processing.
649 static int sctp_ctl_sock_init(void)
651 int err;
652 sa_family_t family;
654 if (sctp_get_pf_specific(PF_INET6))
655 family = PF_INET6;
656 else
657 family = PF_INET;
659 err = sock_create_kern(family, SOCK_SEQPACKET, IPPROTO_SCTP,
660 &sctp_ctl_socket);
661 if (err < 0) {
662 printk(KERN_ERR
663 "SCTP: Failed to create the SCTP control socket.\n");
664 return err;
666 sctp_ctl_socket->sk->sk_allocation = GFP_ATOMIC;
667 inet_sk(sctp_ctl_socket->sk)->uc_ttl = -1;
669 return 0;
672 /* Register address family specific functions. */
673 int sctp_register_af(struct sctp_af *af)
675 switch (af->sa_family) {
676 case AF_INET:
677 if (sctp_af_v4_specific)
678 return 0;
679 sctp_af_v4_specific = af;
680 break;
681 case AF_INET6:
682 if (sctp_af_v6_specific)
683 return 0;
684 sctp_af_v6_specific = af;
685 break;
686 default:
687 return 0;
690 INIT_LIST_HEAD(&af->list);
691 list_add_tail(&af->list, &sctp_address_families);
692 return 1;
695 /* Get the table of functions for manipulating a particular address
696 * family.
698 struct sctp_af *sctp_get_af_specific(sa_family_t family)
700 switch (family) {
701 case AF_INET:
702 return sctp_af_v4_specific;
703 case AF_INET6:
704 return sctp_af_v6_specific;
705 default:
706 return NULL;
710 /* Common code to initialize a AF_INET msg_name. */
711 static void sctp_inet_msgname(char *msgname, int *addr_len)
713 struct sockaddr_in *sin;
715 sin = (struct sockaddr_in *)msgname;
716 *addr_len = sizeof(struct sockaddr_in);
717 sin->sin_family = AF_INET;
718 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
721 /* Copy the primary address of the peer primary address as the msg_name. */
722 static void sctp_inet_event_msgname(struct sctp_ulpevent *event, char *msgname,
723 int *addr_len)
725 struct sockaddr_in *sin, *sinfrom;
727 if (msgname) {
728 struct sctp_association *asoc;
730 asoc = event->asoc;
731 sctp_inet_msgname(msgname, addr_len);
732 sin = (struct sockaddr_in *)msgname;
733 sinfrom = &asoc->peer.primary_addr.v4;
734 sin->sin_port = htons(asoc->peer.port);
735 sin->sin_addr.s_addr = sinfrom->sin_addr.s_addr;
739 /* Initialize and copy out a msgname from an inbound skb. */
740 static void sctp_inet_skb_msgname(struct sk_buff *skb, char *msgname, int *len)
742 struct sctphdr *sh;
743 struct sockaddr_in *sin;
745 if (msgname) {
746 sctp_inet_msgname(msgname, len);
747 sin = (struct sockaddr_in *)msgname;
748 sh = (struct sctphdr *)skb->h.raw;
749 sin->sin_port = sh->source;
750 sin->sin_addr.s_addr = skb->nh.iph->saddr;
754 /* Do we support this AF? */
755 static int sctp_inet_af_supported(sa_family_t family, struct sctp_sock *sp)
757 /* PF_INET only supports AF_INET addresses. */
758 return (AF_INET == family);
761 /* Address matching with wildcards allowed. */
762 static int sctp_inet_cmp_addr(const union sctp_addr *addr1,
763 const union sctp_addr *addr2,
764 struct sctp_sock *opt)
766 /* PF_INET only supports AF_INET addresses. */
767 if (addr1->sa.sa_family != addr2->sa.sa_family)
768 return 0;
769 if (INADDR_ANY == addr1->v4.sin_addr.s_addr ||
770 INADDR_ANY == addr2->v4.sin_addr.s_addr)
771 return 1;
772 if (addr1->v4.sin_addr.s_addr == addr2->v4.sin_addr.s_addr)
773 return 1;
775 return 0;
778 /* Verify that provided sockaddr looks bindable. Common verification has
779 * already been taken care of.
781 static int sctp_inet_bind_verify(struct sctp_sock *opt, union sctp_addr *addr)
783 return sctp_v4_available(addr, opt);
786 /* Verify that sockaddr looks sendable. Common verification has already
787 * been taken care of.
789 static int sctp_inet_send_verify(struct sctp_sock *opt, union sctp_addr *addr)
791 return 1;
794 /* Fill in Supported Address Type information for INIT and INIT-ACK
795 * chunks. Returns number of addresses supported.
797 static int sctp_inet_supported_addrs(const struct sctp_sock *opt,
798 __u16 *types)
800 types[0] = SCTP_PARAM_IPV4_ADDRESS;
801 return 1;
804 /* Wrapper routine that calls the ip transmit routine. */
805 static inline int sctp_v4_xmit(struct sk_buff *skb,
806 struct sctp_transport *transport, int ipfragok)
808 SCTP_DEBUG_PRINTK("%s: skb:%p, len:%d, "
809 "src:%u.%u.%u.%u, dst:%u.%u.%u.%u\n",
810 __FUNCTION__, skb, skb->len,
811 NIPQUAD(((struct rtable *)skb->dst)->rt_src),
812 NIPQUAD(((struct rtable *)skb->dst)->rt_dst));
814 SCTP_INC_STATS(SCTP_MIB_OUTSCTPPACKS);
815 return ip_queue_xmit(skb, ipfragok);
818 static struct sctp_af sctp_ipv4_specific;
820 static struct sctp_pf sctp_pf_inet = {
821 .event_msgname = sctp_inet_event_msgname,
822 .skb_msgname = sctp_inet_skb_msgname,
823 .af_supported = sctp_inet_af_supported,
824 .cmp_addr = sctp_inet_cmp_addr,
825 .bind_verify = sctp_inet_bind_verify,
826 .send_verify = sctp_inet_send_verify,
827 .supported_addrs = sctp_inet_supported_addrs,
828 .create_accept_sk = sctp_v4_create_accept_sk,
829 .addr_v4map = sctp_v4_addr_v4map,
830 .af = &sctp_ipv4_specific,
833 /* Notifier for inetaddr addition/deletion events. */
834 static struct notifier_block sctp_inetaddr_notifier = {
835 .notifier_call = sctp_inetaddr_event,
838 /* Socket operations. */
839 static const struct proto_ops inet_seqpacket_ops = {
840 .family = PF_INET,
841 .owner = THIS_MODULE,
842 .release = inet_release, /* Needs to be wrapped... */
843 .bind = inet_bind,
844 .connect = inet_dgram_connect,
845 .socketpair = sock_no_socketpair,
846 .accept = inet_accept,
847 .getname = inet_getname, /* Semantics are different. */
848 .poll = sctp_poll,
849 .ioctl = inet_ioctl,
850 .listen = sctp_inet_listen,
851 .shutdown = inet_shutdown, /* Looks harmless. */
852 .setsockopt = sock_common_setsockopt, /* IP_SOL IP_OPTION is a problem */
853 .getsockopt = sock_common_getsockopt,
854 .sendmsg = inet_sendmsg,
855 .recvmsg = sock_common_recvmsg,
856 .mmap = sock_no_mmap,
857 .sendpage = sock_no_sendpage,
858 #ifdef CONFIG_COMPAT
859 .compat_setsockopt = compat_sock_common_setsockopt,
860 .compat_getsockopt = compat_sock_common_getsockopt,
861 #endif
864 /* Registration with AF_INET family. */
865 static struct inet_protosw sctp_seqpacket_protosw = {
866 .type = SOCK_SEQPACKET,
867 .protocol = IPPROTO_SCTP,
868 .prot = &sctp_prot,
869 .ops = &inet_seqpacket_ops,
870 .capability = -1,
871 .no_check = 0,
872 .flags = SCTP_PROTOSW_FLAG
874 static struct inet_protosw sctp_stream_protosw = {
875 .type = SOCK_STREAM,
876 .protocol = IPPROTO_SCTP,
877 .prot = &sctp_prot,
878 .ops = &inet_seqpacket_ops,
879 .capability = -1,
880 .no_check = 0,
881 .flags = SCTP_PROTOSW_FLAG
884 /* Register with IP layer. */
885 static struct net_protocol sctp_protocol = {
886 .handler = sctp_rcv,
887 .err_handler = sctp_v4_err,
888 .no_policy = 1,
891 /* IPv4 address related functions. */
892 static struct sctp_af sctp_ipv4_specific = {
893 .sa_family = AF_INET,
894 .sctp_xmit = sctp_v4_xmit,
895 .setsockopt = ip_setsockopt,
896 .getsockopt = ip_getsockopt,
897 .get_dst = sctp_v4_get_dst,
898 .get_saddr = sctp_v4_get_saddr,
899 .copy_addrlist = sctp_v4_copy_addrlist,
900 .from_skb = sctp_v4_from_skb,
901 .from_sk = sctp_v4_from_sk,
902 .to_sk_saddr = sctp_v4_to_sk_saddr,
903 .to_sk_daddr = sctp_v4_to_sk_daddr,
904 .from_addr_param = sctp_v4_from_addr_param,
905 .to_addr_param = sctp_v4_to_addr_param,
906 .dst_saddr = sctp_v4_dst_saddr,
907 .cmp_addr = sctp_v4_cmp_addr,
908 .addr_valid = sctp_v4_addr_valid,
909 .inaddr_any = sctp_v4_inaddr_any,
910 .is_any = sctp_v4_is_any,
911 .available = sctp_v4_available,
912 .scope = sctp_v4_scope,
913 .skb_iif = sctp_v4_skb_iif,
914 .is_ce = sctp_v4_is_ce,
915 .seq_dump_addr = sctp_v4_seq_dump_addr,
916 .net_header_len = sizeof(struct iphdr),
917 .sockaddr_len = sizeof(struct sockaddr_in),
918 #ifdef CONFIG_COMPAT
919 .compat_setsockopt = compat_ip_setsockopt,
920 .compat_getsockopt = compat_ip_getsockopt,
921 #endif
924 struct sctp_pf *sctp_get_pf_specific(sa_family_t family) {
926 switch (family) {
927 case PF_INET:
928 return sctp_pf_inet_specific;
929 case PF_INET6:
930 return sctp_pf_inet6_specific;
931 default:
932 return NULL;
936 /* Register the PF specific function table. */
937 int sctp_register_pf(struct sctp_pf *pf, sa_family_t family)
939 switch (family) {
940 case PF_INET:
941 if (sctp_pf_inet_specific)
942 return 0;
943 sctp_pf_inet_specific = pf;
944 break;
945 case PF_INET6:
946 if (sctp_pf_inet6_specific)
947 return 0;
948 sctp_pf_inet6_specific = pf;
949 break;
950 default:
951 return 0;
953 return 1;
956 static int __init init_sctp_mibs(void)
958 sctp_statistics[0] = alloc_percpu(struct sctp_mib);
959 if (!sctp_statistics[0])
960 return -ENOMEM;
961 sctp_statistics[1] = alloc_percpu(struct sctp_mib);
962 if (!sctp_statistics[1]) {
963 free_percpu(sctp_statistics[0]);
964 return -ENOMEM;
966 return 0;
970 static void cleanup_sctp_mibs(void)
972 free_percpu(sctp_statistics[0]);
973 free_percpu(sctp_statistics[1]);
976 /* Initialize the universe into something sensible. */
977 SCTP_STATIC __init int sctp_init(void)
979 int i;
980 int status = -EINVAL;
981 unsigned long goal;
982 int order;
984 /* SCTP_DEBUG sanity check. */
985 if (!sctp_sanity_check())
986 goto out;
988 status = proto_register(&sctp_prot, 1);
989 if (status)
990 goto out;
992 /* Add SCTP to inet_protos hash table. */
993 status = -EAGAIN;
994 if (inet_add_protocol(&sctp_protocol, IPPROTO_SCTP) < 0)
995 goto err_add_protocol;
997 /* Add SCTP(TCP and UDP style) to inetsw linked list. */
998 inet_register_protosw(&sctp_seqpacket_protosw);
999 inet_register_protosw(&sctp_stream_protosw);
1001 /* Allocate a cache pools. */
1002 status = -ENOBUFS;
1003 sctp_bucket_cachep = kmem_cache_create("sctp_bind_bucket",
1004 sizeof(struct sctp_bind_bucket),
1005 0, SLAB_HWCACHE_ALIGN,
1006 NULL, NULL);
1008 if (!sctp_bucket_cachep)
1009 goto err_bucket_cachep;
1011 sctp_chunk_cachep = kmem_cache_create("sctp_chunk",
1012 sizeof(struct sctp_chunk),
1013 0, SLAB_HWCACHE_ALIGN,
1014 NULL, NULL);
1015 if (!sctp_chunk_cachep)
1016 goto err_chunk_cachep;
1018 /* Allocate and initialise sctp mibs. */
1019 status = init_sctp_mibs();
1020 if (status)
1021 goto err_init_mibs;
1023 /* Initialize proc fs directory. */
1024 status = sctp_proc_init();
1025 if (status)
1026 goto err_init_proc;
1028 /* Initialize object count debugging. */
1029 sctp_dbg_objcnt_init();
1031 /* Initialize the SCTP specific PF functions. */
1032 sctp_register_pf(&sctp_pf_inet, PF_INET);
1034 * 14. Suggested SCTP Protocol Parameter Values
1036 /* The following protocol parameters are RECOMMENDED: */
1037 /* RTO.Initial - 3 seconds */
1038 sctp_rto_initial = SCTP_RTO_INITIAL;
1039 /* RTO.Min - 1 second */
1040 sctp_rto_min = SCTP_RTO_MIN;
1041 /* RTO.Max - 60 seconds */
1042 sctp_rto_max = SCTP_RTO_MAX;
1043 /* RTO.Alpha - 1/8 */
1044 sctp_rto_alpha = SCTP_RTO_ALPHA;
1045 /* RTO.Beta - 1/4 */
1046 sctp_rto_beta = SCTP_RTO_BETA;
1048 /* Valid.Cookie.Life - 60 seconds */
1049 sctp_valid_cookie_life = 60 * HZ;
1051 /* Whether Cookie Preservative is enabled(1) or not(0) */
1052 sctp_cookie_preserve_enable = 1;
1054 /* Max.Burst - 4 */
1055 sctp_max_burst = SCTP_MAX_BURST;
1057 /* Association.Max.Retrans - 10 attempts
1058 * Path.Max.Retrans - 5 attempts (per destination address)
1059 * Max.Init.Retransmits - 8 attempts
1061 sctp_max_retrans_association = 10;
1062 sctp_max_retrans_path = 5;
1063 sctp_max_retrans_init = 8;
1065 /* Sendbuffer growth - do per-socket accounting */
1066 sctp_sndbuf_policy = 0;
1068 /* Rcvbuffer growth - do per-socket accounting */
1069 sctp_rcvbuf_policy = 0;
1071 /* HB.interval - 30 seconds */
1072 sctp_hb_interval = SCTP_DEFAULT_TIMEOUT_HEARTBEAT;
1074 /* delayed SACK timeout */
1075 sctp_sack_timeout = SCTP_DEFAULT_TIMEOUT_SACK;
1077 /* Implementation specific variables. */
1079 /* Initialize default stream count setup information. */
1080 sctp_max_instreams = SCTP_DEFAULT_INSTREAMS;
1081 sctp_max_outstreams = SCTP_DEFAULT_OUTSTREAMS;
1083 /* Initialize handle used for association ids. */
1084 idr_init(&sctp_assocs_id);
1086 /* Size and allocate the association hash table.
1087 * The methodology is similar to that of the tcp hash tables.
1089 if (num_physpages >= (128 * 1024))
1090 goal = num_physpages >> (22 - PAGE_SHIFT);
1091 else
1092 goal = num_physpages >> (24 - PAGE_SHIFT);
1094 for (order = 0; (1UL << order) < goal; order++)
1097 do {
1098 sctp_assoc_hashsize = (1UL << order) * PAGE_SIZE /
1099 sizeof(struct sctp_hashbucket);
1100 if ((sctp_assoc_hashsize > (64 * 1024)) && order > 0)
1101 continue;
1102 sctp_assoc_hashtable = (struct sctp_hashbucket *)
1103 __get_free_pages(GFP_ATOMIC, order);
1104 } while (!sctp_assoc_hashtable && --order > 0);
1105 if (!sctp_assoc_hashtable) {
1106 printk(KERN_ERR "SCTP: Failed association hash alloc.\n");
1107 status = -ENOMEM;
1108 goto err_ahash_alloc;
1110 for (i = 0; i < sctp_assoc_hashsize; i++) {
1111 rwlock_init(&sctp_assoc_hashtable[i].lock);
1112 sctp_assoc_hashtable[i].chain = NULL;
1115 /* Allocate and initialize the endpoint hash table. */
1116 sctp_ep_hashsize = 64;
1117 sctp_ep_hashtable = (struct sctp_hashbucket *)
1118 kmalloc(64 * sizeof(struct sctp_hashbucket), GFP_KERNEL);
1119 if (!sctp_ep_hashtable) {
1120 printk(KERN_ERR "SCTP: Failed endpoint_hash alloc.\n");
1121 status = -ENOMEM;
1122 goto err_ehash_alloc;
1124 for (i = 0; i < sctp_ep_hashsize; i++) {
1125 rwlock_init(&sctp_ep_hashtable[i].lock);
1126 sctp_ep_hashtable[i].chain = NULL;
1129 /* Allocate and initialize the SCTP port hash table. */
1130 do {
1131 sctp_port_hashsize = (1UL << order) * PAGE_SIZE /
1132 sizeof(struct sctp_bind_hashbucket);
1133 if ((sctp_port_hashsize > (64 * 1024)) && order > 0)
1134 continue;
1135 sctp_port_hashtable = (struct sctp_bind_hashbucket *)
1136 __get_free_pages(GFP_ATOMIC, order);
1137 } while (!sctp_port_hashtable && --order > 0);
1138 if (!sctp_port_hashtable) {
1139 printk(KERN_ERR "SCTP: Failed bind hash alloc.");
1140 status = -ENOMEM;
1141 goto err_bhash_alloc;
1143 for (i = 0; i < sctp_port_hashsize; i++) {
1144 spin_lock_init(&sctp_port_hashtable[i].lock);
1145 sctp_port_hashtable[i].chain = NULL;
1148 spin_lock_init(&sctp_port_alloc_lock);
1149 sctp_port_rover = sysctl_local_port_range[0] - 1;
1151 printk(KERN_INFO "SCTP: Hash tables configured "
1152 "(established %d bind %d)\n",
1153 sctp_assoc_hashsize, sctp_port_hashsize);
1155 /* Disable ADDIP by default. */
1156 sctp_addip_enable = 0;
1158 /* Enable PR-SCTP by default. */
1159 sctp_prsctp_enable = 1;
1161 sctp_sysctl_register();
1163 INIT_LIST_HEAD(&sctp_address_families);
1164 sctp_register_af(&sctp_ipv4_specific);
1166 status = sctp_v6_init();
1167 if (status)
1168 goto err_v6_init;
1170 /* Initialize the control inode/socket for handling OOTB packets. */
1171 if ((status = sctp_ctl_sock_init())) {
1172 printk (KERN_ERR
1173 "SCTP: Failed to initialize the SCTP control sock.\n");
1174 goto err_ctl_sock_init;
1177 /* Initialize the local address list. */
1178 INIT_LIST_HEAD(&sctp_local_addr_list);
1179 spin_lock_init(&sctp_local_addr_lock);
1181 /* Register notifier for inet address additions/deletions. */
1182 register_inetaddr_notifier(&sctp_inetaddr_notifier);
1184 sctp_get_local_addr_list();
1186 __unsafe(THIS_MODULE);
1187 status = 0;
1188 out:
1189 return status;
1190 err_ctl_sock_init:
1191 sctp_v6_exit();
1192 err_v6_init:
1193 sctp_sysctl_unregister();
1194 list_del(&sctp_ipv4_specific.list);
1195 free_pages((unsigned long)sctp_port_hashtable,
1196 get_order(sctp_port_hashsize *
1197 sizeof(struct sctp_bind_hashbucket)));
1198 err_bhash_alloc:
1199 kfree(sctp_ep_hashtable);
1200 err_ehash_alloc:
1201 free_pages((unsigned long)sctp_assoc_hashtable,
1202 get_order(sctp_assoc_hashsize *
1203 sizeof(struct sctp_hashbucket)));
1204 err_ahash_alloc:
1205 sctp_dbg_objcnt_exit();
1206 err_init_proc:
1207 sctp_proc_exit();
1208 cleanup_sctp_mibs();
1209 err_init_mibs:
1210 kmem_cache_destroy(sctp_chunk_cachep);
1211 err_chunk_cachep:
1212 kmem_cache_destroy(sctp_bucket_cachep);
1213 err_bucket_cachep:
1214 inet_del_protocol(&sctp_protocol, IPPROTO_SCTP);
1215 inet_unregister_protosw(&sctp_seqpacket_protosw);
1216 inet_unregister_protosw(&sctp_stream_protosw);
1217 err_add_protocol:
1218 proto_unregister(&sctp_prot);
1219 goto out;
1222 /* Exit handler for the SCTP protocol. */
1223 SCTP_STATIC __exit void sctp_exit(void)
1225 /* BUG. This should probably do something useful like clean
1226 * up all the remaining associations and all that memory.
1229 /* Unregister notifier for inet address additions/deletions. */
1230 unregister_inetaddr_notifier(&sctp_inetaddr_notifier);
1232 /* Free the local address list. */
1233 sctp_free_local_addr_list();
1235 /* Free the control endpoint. */
1236 sock_release(sctp_ctl_socket);
1238 sctp_v6_exit();
1239 sctp_sysctl_unregister();
1240 list_del(&sctp_ipv4_specific.list);
1242 free_pages((unsigned long)sctp_assoc_hashtable,
1243 get_order(sctp_assoc_hashsize *
1244 sizeof(struct sctp_hashbucket)));
1245 kfree(sctp_ep_hashtable);
1246 free_pages((unsigned long)sctp_port_hashtable,
1247 get_order(sctp_port_hashsize *
1248 sizeof(struct sctp_bind_hashbucket)));
1250 kmem_cache_destroy(sctp_chunk_cachep);
1251 kmem_cache_destroy(sctp_bucket_cachep);
1253 sctp_dbg_objcnt_exit();
1254 sctp_proc_exit();
1255 cleanup_sctp_mibs();
1257 inet_del_protocol(&sctp_protocol, IPPROTO_SCTP);
1258 inet_unregister_protosw(&sctp_seqpacket_protosw);
1259 inet_unregister_protosw(&sctp_stream_protosw);
1260 proto_unregister(&sctp_prot);
1263 module_init(sctp_init);
1264 module_exit(sctp_exit);
1267 * __stringify doesn't likes enums, so use IPPROTO_SCTP value (132) directly.
1269 MODULE_ALIAS("net-pf-" __stringify(PF_INET) "-proto-132");
1270 MODULE_AUTHOR("Linux Kernel SCTP developers <lksctp-developers@lists.sourceforge.net>");
1271 MODULE_DESCRIPTION("Support for the SCTP protocol (RFC2960)");
1272 MODULE_LICENSE("GPL");