[POWERPC] We don't define CONFIG_HAVE_ARCH_EARLY_PFN_TO_NID
[usb.git] / net / sctp / protocol.c
blob34bab36637ac9a37345edf298cf485a45d1c9e32
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_each_netdev(dev) {
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 = sctp_hdr(skb);
239 if (is_saddr) {
240 *port = sh->source;
241 from = &ip_hdr(skb)->saddr;
242 } else {
243 *port = sh->dest;
244 from = &ip_hdr(skb)->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(ip_hdr(skb)->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 if (msgname) {
735 struct sctphdr *sh = sctp_hdr(skb);
736 struct sockaddr_in *sin = (struct sockaddr_in *)msgname;
738 sctp_inet_msgname(msgname, len);
739 sin->sin_port = sh->source;
740 sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
744 /* Do we support this AF? */
745 static int sctp_inet_af_supported(sa_family_t family, struct sctp_sock *sp)
747 /* PF_INET only supports AF_INET addresses. */
748 return (AF_INET == family);
751 /* Address matching with wildcards allowed. */
752 static int sctp_inet_cmp_addr(const union sctp_addr *addr1,
753 const union sctp_addr *addr2,
754 struct sctp_sock *opt)
756 /* PF_INET only supports AF_INET addresses. */
757 if (addr1->sa.sa_family != addr2->sa.sa_family)
758 return 0;
759 if (INADDR_ANY == addr1->v4.sin_addr.s_addr ||
760 INADDR_ANY == addr2->v4.sin_addr.s_addr)
761 return 1;
762 if (addr1->v4.sin_addr.s_addr == addr2->v4.sin_addr.s_addr)
763 return 1;
765 return 0;
768 /* Verify that provided sockaddr looks bindable. Common verification has
769 * already been taken care of.
771 static int sctp_inet_bind_verify(struct sctp_sock *opt, union sctp_addr *addr)
773 return sctp_v4_available(addr, opt);
776 /* Verify that sockaddr looks sendable. Common verification has already
777 * been taken care of.
779 static int sctp_inet_send_verify(struct sctp_sock *opt, union sctp_addr *addr)
781 return 1;
784 /* Fill in Supported Address Type information for INIT and INIT-ACK
785 * chunks. Returns number of addresses supported.
787 static int sctp_inet_supported_addrs(const struct sctp_sock *opt,
788 __be16 *types)
790 types[0] = SCTP_PARAM_IPV4_ADDRESS;
791 return 1;
794 /* Wrapper routine that calls the ip transmit routine. */
795 static inline int sctp_v4_xmit(struct sk_buff *skb,
796 struct sctp_transport *transport, int ipfragok)
798 SCTP_DEBUG_PRINTK("%s: skb:%p, len:%d, "
799 "src:%u.%u.%u.%u, dst:%u.%u.%u.%u\n",
800 __FUNCTION__, skb, skb->len,
801 NIPQUAD(((struct rtable *)skb->dst)->rt_src),
802 NIPQUAD(((struct rtable *)skb->dst)->rt_dst));
804 SCTP_INC_STATS(SCTP_MIB_OUTSCTPPACKS);
805 return ip_queue_xmit(skb, ipfragok);
808 static struct sctp_af sctp_ipv4_specific;
810 static struct sctp_pf sctp_pf_inet = {
811 .event_msgname = sctp_inet_event_msgname,
812 .skb_msgname = sctp_inet_skb_msgname,
813 .af_supported = sctp_inet_af_supported,
814 .cmp_addr = sctp_inet_cmp_addr,
815 .bind_verify = sctp_inet_bind_verify,
816 .send_verify = sctp_inet_send_verify,
817 .supported_addrs = sctp_inet_supported_addrs,
818 .create_accept_sk = sctp_v4_create_accept_sk,
819 .addr_v4map = sctp_v4_addr_v4map,
820 .af = &sctp_ipv4_specific,
823 /* Notifier for inetaddr addition/deletion events. */
824 static struct notifier_block sctp_inetaddr_notifier = {
825 .notifier_call = sctp_inetaddr_event,
828 /* Socket operations. */
829 static const struct proto_ops inet_seqpacket_ops = {
830 .family = PF_INET,
831 .owner = THIS_MODULE,
832 .release = inet_release, /* Needs to be wrapped... */
833 .bind = inet_bind,
834 .connect = inet_dgram_connect,
835 .socketpair = sock_no_socketpair,
836 .accept = inet_accept,
837 .getname = inet_getname, /* Semantics are different. */
838 .poll = sctp_poll,
839 .ioctl = inet_ioctl,
840 .listen = sctp_inet_listen,
841 .shutdown = inet_shutdown, /* Looks harmless. */
842 .setsockopt = sock_common_setsockopt, /* IP_SOL IP_OPTION is a problem */
843 .getsockopt = sock_common_getsockopt,
844 .sendmsg = inet_sendmsg,
845 .recvmsg = sock_common_recvmsg,
846 .mmap = sock_no_mmap,
847 .sendpage = sock_no_sendpage,
848 #ifdef CONFIG_COMPAT
849 .compat_setsockopt = compat_sock_common_setsockopt,
850 .compat_getsockopt = compat_sock_common_getsockopt,
851 #endif
854 /* Registration with AF_INET family. */
855 static struct inet_protosw sctp_seqpacket_protosw = {
856 .type = SOCK_SEQPACKET,
857 .protocol = IPPROTO_SCTP,
858 .prot = &sctp_prot,
859 .ops = &inet_seqpacket_ops,
860 .capability = -1,
861 .no_check = 0,
862 .flags = SCTP_PROTOSW_FLAG
864 static struct inet_protosw sctp_stream_protosw = {
865 .type = SOCK_STREAM,
866 .protocol = IPPROTO_SCTP,
867 .prot = &sctp_prot,
868 .ops = &inet_seqpacket_ops,
869 .capability = -1,
870 .no_check = 0,
871 .flags = SCTP_PROTOSW_FLAG
874 /* Register with IP layer. */
875 static struct net_protocol sctp_protocol = {
876 .handler = sctp_rcv,
877 .err_handler = sctp_v4_err,
878 .no_policy = 1,
881 /* IPv4 address related functions. */
882 static struct sctp_af sctp_ipv4_specific = {
883 .sa_family = AF_INET,
884 .sctp_xmit = sctp_v4_xmit,
885 .setsockopt = ip_setsockopt,
886 .getsockopt = ip_getsockopt,
887 .get_dst = sctp_v4_get_dst,
888 .get_saddr = sctp_v4_get_saddr,
889 .copy_addrlist = sctp_v4_copy_addrlist,
890 .from_skb = sctp_v4_from_skb,
891 .from_sk = sctp_v4_from_sk,
892 .to_sk_saddr = sctp_v4_to_sk_saddr,
893 .to_sk_daddr = sctp_v4_to_sk_daddr,
894 .from_addr_param = sctp_v4_from_addr_param,
895 .to_addr_param = sctp_v4_to_addr_param,
896 .dst_saddr = sctp_v4_dst_saddr,
897 .cmp_addr = sctp_v4_cmp_addr,
898 .addr_valid = sctp_v4_addr_valid,
899 .inaddr_any = sctp_v4_inaddr_any,
900 .is_any = sctp_v4_is_any,
901 .available = sctp_v4_available,
902 .scope = sctp_v4_scope,
903 .skb_iif = sctp_v4_skb_iif,
904 .is_ce = sctp_v4_is_ce,
905 .seq_dump_addr = sctp_v4_seq_dump_addr,
906 .net_header_len = sizeof(struct iphdr),
907 .sockaddr_len = sizeof(struct sockaddr_in),
908 #ifdef CONFIG_COMPAT
909 .compat_setsockopt = compat_ip_setsockopt,
910 .compat_getsockopt = compat_ip_getsockopt,
911 #endif
914 struct sctp_pf *sctp_get_pf_specific(sa_family_t family) {
916 switch (family) {
917 case PF_INET:
918 return sctp_pf_inet_specific;
919 case PF_INET6:
920 return sctp_pf_inet6_specific;
921 default:
922 return NULL;
926 /* Register the PF specific function table. */
927 int sctp_register_pf(struct sctp_pf *pf, sa_family_t family)
929 switch (family) {
930 case PF_INET:
931 if (sctp_pf_inet_specific)
932 return 0;
933 sctp_pf_inet_specific = pf;
934 break;
935 case PF_INET6:
936 if (sctp_pf_inet6_specific)
937 return 0;
938 sctp_pf_inet6_specific = pf;
939 break;
940 default:
941 return 0;
943 return 1;
946 static int __init init_sctp_mibs(void)
948 sctp_statistics[0] = alloc_percpu(struct sctp_mib);
949 if (!sctp_statistics[0])
950 return -ENOMEM;
951 sctp_statistics[1] = alloc_percpu(struct sctp_mib);
952 if (!sctp_statistics[1]) {
953 free_percpu(sctp_statistics[0]);
954 return -ENOMEM;
956 return 0;
960 static void cleanup_sctp_mibs(void)
962 free_percpu(sctp_statistics[0]);
963 free_percpu(sctp_statistics[1]);
966 /* Initialize the universe into something sensible. */
967 SCTP_STATIC __init int sctp_init(void)
969 int i;
970 int status = -EINVAL;
971 unsigned long goal;
972 int order;
974 /* SCTP_DEBUG sanity check. */
975 if (!sctp_sanity_check())
976 goto out;
978 /* Allocate bind_bucket and chunk caches. */
979 status = -ENOBUFS;
980 sctp_bucket_cachep = kmem_cache_create("sctp_bind_bucket",
981 sizeof(struct sctp_bind_bucket),
982 0, SLAB_HWCACHE_ALIGN,
983 NULL, NULL);
984 if (!sctp_bucket_cachep)
985 goto out;
987 sctp_chunk_cachep = kmem_cache_create("sctp_chunk",
988 sizeof(struct sctp_chunk),
989 0, SLAB_HWCACHE_ALIGN,
990 NULL, NULL);
991 if (!sctp_chunk_cachep)
992 goto err_chunk_cachep;
994 /* Allocate and initialise sctp mibs. */
995 status = init_sctp_mibs();
996 if (status)
997 goto err_init_mibs;
999 /* Initialize proc fs directory. */
1000 status = sctp_proc_init();
1001 if (status)
1002 goto err_init_proc;
1004 /* Initialize object count debugging. */
1005 sctp_dbg_objcnt_init();
1007 /* Initialize the SCTP specific PF functions. */
1008 sctp_register_pf(&sctp_pf_inet, PF_INET);
1010 * 14. Suggested SCTP Protocol Parameter Values
1012 /* The following protocol parameters are RECOMMENDED: */
1013 /* RTO.Initial - 3 seconds */
1014 sctp_rto_initial = SCTP_RTO_INITIAL;
1015 /* RTO.Min - 1 second */
1016 sctp_rto_min = SCTP_RTO_MIN;
1017 /* RTO.Max - 60 seconds */
1018 sctp_rto_max = SCTP_RTO_MAX;
1019 /* RTO.Alpha - 1/8 */
1020 sctp_rto_alpha = SCTP_RTO_ALPHA;
1021 /* RTO.Beta - 1/4 */
1022 sctp_rto_beta = SCTP_RTO_BETA;
1024 /* Valid.Cookie.Life - 60 seconds */
1025 sctp_valid_cookie_life = SCTP_DEFAULT_COOKIE_LIFE;
1027 /* Whether Cookie Preservative is enabled(1) or not(0) */
1028 sctp_cookie_preserve_enable = 1;
1030 /* Max.Burst - 4 */
1031 sctp_max_burst = SCTP_DEFAULT_MAX_BURST;
1033 /* Association.Max.Retrans - 10 attempts
1034 * Path.Max.Retrans - 5 attempts (per destination address)
1035 * Max.Init.Retransmits - 8 attempts
1037 sctp_max_retrans_association = 10;
1038 sctp_max_retrans_path = 5;
1039 sctp_max_retrans_init = 8;
1041 /* Sendbuffer growth - do per-socket accounting */
1042 sctp_sndbuf_policy = 0;
1044 /* Rcvbuffer growth - do per-socket accounting */
1045 sctp_rcvbuf_policy = 0;
1047 /* HB.interval - 30 seconds */
1048 sctp_hb_interval = SCTP_DEFAULT_TIMEOUT_HEARTBEAT;
1050 /* delayed SACK timeout */
1051 sctp_sack_timeout = SCTP_DEFAULT_TIMEOUT_SACK;
1053 /* Implementation specific variables. */
1055 /* Initialize default stream count setup information. */
1056 sctp_max_instreams = SCTP_DEFAULT_INSTREAMS;
1057 sctp_max_outstreams = SCTP_DEFAULT_OUTSTREAMS;
1059 /* Initialize handle used for association ids. */
1060 idr_init(&sctp_assocs_id);
1062 /* Size and allocate the association hash table.
1063 * The methodology is similar to that of the tcp hash tables.
1065 if (num_physpages >= (128 * 1024))
1066 goal = num_physpages >> (22 - PAGE_SHIFT);
1067 else
1068 goal = num_physpages >> (24 - PAGE_SHIFT);
1070 for (order = 0; (1UL << order) < goal; order++)
1073 do {
1074 sctp_assoc_hashsize = (1UL << order) * PAGE_SIZE /
1075 sizeof(struct sctp_hashbucket);
1076 if ((sctp_assoc_hashsize > (64 * 1024)) && order > 0)
1077 continue;
1078 sctp_assoc_hashtable = (struct sctp_hashbucket *)
1079 __get_free_pages(GFP_ATOMIC, order);
1080 } while (!sctp_assoc_hashtable && --order > 0);
1081 if (!sctp_assoc_hashtable) {
1082 printk(KERN_ERR "SCTP: Failed association hash alloc.\n");
1083 status = -ENOMEM;
1084 goto err_ahash_alloc;
1086 for (i = 0; i < sctp_assoc_hashsize; i++) {
1087 rwlock_init(&sctp_assoc_hashtable[i].lock);
1088 sctp_assoc_hashtable[i].chain = NULL;
1091 /* Allocate and initialize the endpoint hash table. */
1092 sctp_ep_hashsize = 64;
1093 sctp_ep_hashtable = (struct sctp_hashbucket *)
1094 kmalloc(64 * sizeof(struct sctp_hashbucket), GFP_KERNEL);
1095 if (!sctp_ep_hashtable) {
1096 printk(KERN_ERR "SCTP: Failed endpoint_hash alloc.\n");
1097 status = -ENOMEM;
1098 goto err_ehash_alloc;
1100 for (i = 0; i < sctp_ep_hashsize; i++) {
1101 rwlock_init(&sctp_ep_hashtable[i].lock);
1102 sctp_ep_hashtable[i].chain = NULL;
1105 /* Allocate and initialize the SCTP port hash table. */
1106 do {
1107 sctp_port_hashsize = (1UL << order) * PAGE_SIZE /
1108 sizeof(struct sctp_bind_hashbucket);
1109 if ((sctp_port_hashsize > (64 * 1024)) && order > 0)
1110 continue;
1111 sctp_port_hashtable = (struct sctp_bind_hashbucket *)
1112 __get_free_pages(GFP_ATOMIC, order);
1113 } while (!sctp_port_hashtable && --order > 0);
1114 if (!sctp_port_hashtable) {
1115 printk(KERN_ERR "SCTP: Failed bind hash alloc.");
1116 status = -ENOMEM;
1117 goto err_bhash_alloc;
1119 for (i = 0; i < sctp_port_hashsize; i++) {
1120 spin_lock_init(&sctp_port_hashtable[i].lock);
1121 sctp_port_hashtable[i].chain = NULL;
1124 spin_lock_init(&sctp_port_alloc_lock);
1125 sctp_port_rover = sysctl_local_port_range[0] - 1;
1127 printk(KERN_INFO "SCTP: Hash tables configured "
1128 "(established %d bind %d)\n",
1129 sctp_assoc_hashsize, sctp_port_hashsize);
1131 /* Disable ADDIP by default. */
1132 sctp_addip_enable = 0;
1134 /* Enable PR-SCTP by default. */
1135 sctp_prsctp_enable = 1;
1137 sctp_sysctl_register();
1139 INIT_LIST_HEAD(&sctp_address_families);
1140 sctp_register_af(&sctp_ipv4_specific);
1142 status = proto_register(&sctp_prot, 1);
1143 if (status)
1144 goto err_proto_register;
1146 /* Register SCTP(UDP and TCP style) with socket layer. */
1147 inet_register_protosw(&sctp_seqpacket_protosw);
1148 inet_register_protosw(&sctp_stream_protosw);
1150 status = sctp_v6_init();
1151 if (status)
1152 goto err_v6_init;
1154 /* Initialize the control inode/socket for handling OOTB packets. */
1155 if ((status = sctp_ctl_sock_init())) {
1156 printk (KERN_ERR
1157 "SCTP: Failed to initialize the SCTP control sock.\n");
1158 goto err_ctl_sock_init;
1161 /* Initialize the local address list. */
1162 INIT_LIST_HEAD(&sctp_local_addr_list);
1163 sctp_get_local_addr_list();
1165 /* Register notifier for inet address additions/deletions. */
1166 register_inetaddr_notifier(&sctp_inetaddr_notifier);
1168 /* Register SCTP with inet layer. */
1169 if (inet_add_protocol(&sctp_protocol, IPPROTO_SCTP) < 0) {
1170 status = -EAGAIN;
1171 goto err_add_protocol;
1174 /* Register SCTP with inet6 layer. */
1175 status = sctp_v6_add_protocol();
1176 if (status)
1177 goto err_v6_add_protocol;
1179 __unsafe(THIS_MODULE);
1180 status = 0;
1181 out:
1182 return status;
1183 err_v6_add_protocol:
1184 inet_del_protocol(&sctp_protocol, IPPROTO_SCTP);
1185 unregister_inetaddr_notifier(&sctp_inetaddr_notifier);
1186 err_add_protocol:
1187 sctp_free_local_addr_list();
1188 sock_release(sctp_ctl_socket);
1189 err_ctl_sock_init:
1190 sctp_v6_exit();
1191 err_v6_init:
1192 inet_unregister_protosw(&sctp_stream_protosw);
1193 inet_unregister_protosw(&sctp_seqpacket_protosw);
1194 proto_unregister(&sctp_prot);
1195 err_proto_register:
1196 sctp_sysctl_unregister();
1197 list_del(&sctp_ipv4_specific.list);
1198 free_pages((unsigned long)sctp_port_hashtable,
1199 get_order(sctp_port_hashsize *
1200 sizeof(struct sctp_bind_hashbucket)));
1201 err_bhash_alloc:
1202 kfree(sctp_ep_hashtable);
1203 err_ehash_alloc:
1204 free_pages((unsigned long)sctp_assoc_hashtable,
1205 get_order(sctp_assoc_hashsize *
1206 sizeof(struct sctp_hashbucket)));
1207 err_ahash_alloc:
1208 sctp_dbg_objcnt_exit();
1209 sctp_proc_exit();
1210 err_init_proc:
1211 cleanup_sctp_mibs();
1212 err_init_mibs:
1213 kmem_cache_destroy(sctp_chunk_cachep);
1214 err_chunk_cachep:
1215 kmem_cache_destroy(sctp_bucket_cachep);
1216 goto out;
1219 /* Exit handler for the SCTP protocol. */
1220 SCTP_STATIC __exit void sctp_exit(void)
1222 /* BUG. This should probably do something useful like clean
1223 * up all the remaining associations and all that memory.
1226 /* Unregister with inet6/inet layers. */
1227 sctp_v6_del_protocol();
1228 inet_del_protocol(&sctp_protocol, IPPROTO_SCTP);
1230 /* Free the local address list. */
1231 sctp_free_local_addr_list();
1233 /* Free the control endpoint. */
1234 sock_release(sctp_ctl_socket);
1236 /* Cleanup v6 initializations. */
1237 sctp_v6_exit();
1239 /* Unregister with socket layer. */
1240 inet_unregister_protosw(&sctp_stream_protosw);
1241 inet_unregister_protosw(&sctp_seqpacket_protosw);
1243 /* Unregister notifier for inet address additions/deletions. */
1244 unregister_inetaddr_notifier(&sctp_inetaddr_notifier);
1246 sctp_sysctl_unregister();
1247 list_del(&sctp_ipv4_specific.list);
1249 free_pages((unsigned long)sctp_assoc_hashtable,
1250 get_order(sctp_assoc_hashsize *
1251 sizeof(struct sctp_hashbucket)));
1252 kfree(sctp_ep_hashtable);
1253 free_pages((unsigned long)sctp_port_hashtable,
1254 get_order(sctp_port_hashsize *
1255 sizeof(struct sctp_bind_hashbucket)));
1257 sctp_dbg_objcnt_exit();
1258 sctp_proc_exit();
1259 cleanup_sctp_mibs();
1261 kmem_cache_destroy(sctp_chunk_cachep);
1262 kmem_cache_destroy(sctp_bucket_cachep);
1264 proto_unregister(&sctp_prot);
1267 module_init(sctp_init);
1268 module_exit(sctp_exit);
1271 * __stringify doesn't likes enums, so use IPPROTO_SCTP value (132) directly.
1273 MODULE_ALIAS("net-pf-" __stringify(PF_INET) "-proto-132");
1274 MODULE_ALIAS("net-pf-" __stringify(PF_INET6) "-proto-132");
1275 MODULE_AUTHOR("Linux Kernel SCTP developers <lksctp-developers@lists.sourceforge.net>");
1276 MODULE_DESCRIPTION("Support for the SCTP protocol (RFC2960)");
1277 MODULE_LICENSE("GPL");