160c778b2SVlad Yasevich /* SCTP kernel implementation 21da177e4SLinus Torvalds * (C) Copyright IBM Corp. 2001, 2004 31da177e4SLinus Torvalds * Copyright (c) 1999-2000 Cisco, Inc. 41da177e4SLinus Torvalds * Copyright (c) 1999-2001 Motorola, Inc. 51da177e4SLinus Torvalds * Copyright (c) 2001 Intel Corp. 61da177e4SLinus Torvalds * Copyright (c) 2001 Nokia, Inc. 71da177e4SLinus Torvalds * Copyright (c) 2001 La Monte H.P. Yarroll 81da177e4SLinus Torvalds * 960c778b2SVlad Yasevich * This file is part of the SCTP kernel implementation 101da177e4SLinus Torvalds * 111da177e4SLinus Torvalds * Initialization/cleanup for SCTP protocol support. 121da177e4SLinus Torvalds * 1360c778b2SVlad Yasevich * This SCTP implementation is free software; 141da177e4SLinus Torvalds * you can redistribute it and/or modify it under the terms of 151da177e4SLinus Torvalds * the GNU General Public License as published by 161da177e4SLinus Torvalds * the Free Software Foundation; either version 2, or (at your option) 171da177e4SLinus Torvalds * any later version. 181da177e4SLinus Torvalds * 1960c778b2SVlad Yasevich * This SCTP implementation is distributed in the hope that it 201da177e4SLinus Torvalds * will be useful, but WITHOUT ANY WARRANTY; without even the implied 211da177e4SLinus Torvalds * ************************ 221da177e4SLinus Torvalds * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 231da177e4SLinus Torvalds * See the GNU General Public License for more details. 241da177e4SLinus Torvalds * 251da177e4SLinus Torvalds * You should have received a copy of the GNU General Public License 261da177e4SLinus Torvalds * along with GNU CC; see the file COPYING. If not, write to 271da177e4SLinus Torvalds * the Free Software Foundation, 59 Temple Place - Suite 330, 281da177e4SLinus Torvalds * Boston, MA 02111-1307, USA. 291da177e4SLinus Torvalds * 301da177e4SLinus Torvalds * Please send any bug reports or fixes you make to the 311da177e4SLinus Torvalds * email address(es): 321da177e4SLinus Torvalds * lksctp developers <lksctp-developers@lists.sourceforge.net> 331da177e4SLinus Torvalds * 341da177e4SLinus Torvalds * Or submit a bug report through the following website: 351da177e4SLinus Torvalds * http://www.sf.net/projects/lksctp 361da177e4SLinus Torvalds * 371da177e4SLinus Torvalds * Written or modified by: 381da177e4SLinus Torvalds * La Monte H.P. Yarroll <piggy@acm.org> 391da177e4SLinus Torvalds * Karl Knutson <karl@athena.chicago.il.us> 401da177e4SLinus Torvalds * Jon Grimm <jgrimm@us.ibm.com> 411da177e4SLinus Torvalds * Sridhar Samudrala <sri@us.ibm.com> 421da177e4SLinus Torvalds * Daisy Chang <daisyc@us.ibm.com> 431da177e4SLinus Torvalds * Ardelle Fan <ardelle.fan@intel.com> 441da177e4SLinus Torvalds * 451da177e4SLinus Torvalds * Any bugs reported given to us we will try to fix... any fixes shared will 461da177e4SLinus Torvalds * be incorporated into the next SCTP release. 471da177e4SLinus Torvalds */ 481da177e4SLinus Torvalds 491da177e4SLinus Torvalds #include <linux/module.h> 501da177e4SLinus Torvalds #include <linux/init.h> 511da177e4SLinus Torvalds #include <linux/netdevice.h> 521da177e4SLinus Torvalds #include <linux/inetdevice.h> 531da177e4SLinus Torvalds #include <linux/seq_file.h> 544d93df0aSNeil Horman #include <linux/bootmem.h> 55457c4cbcSEric W. Biederman #include <net/net_namespace.h> 561da177e4SLinus Torvalds #include <net/protocol.h> 571da177e4SLinus Torvalds #include <net/ip.h> 581da177e4SLinus Torvalds #include <net/ipv6.h> 5914c85021SArnaldo Carvalho de Melo #include <net/route.h> 601da177e4SLinus Torvalds #include <net/sctp/sctp.h> 611da177e4SLinus Torvalds #include <net/addrconf.h> 621da177e4SLinus Torvalds #include <net/inet_common.h> 631da177e4SLinus Torvalds #include <net/inet_ecn.h> 641da177e4SLinus Torvalds 651da177e4SLinus Torvalds /* Global data structures. */ 664cbf1caeSBrian Haley struct sctp_globals sctp_globals __read_mostly; 67ba89966cSEric Dumazet DEFINE_SNMP_STAT(struct sctp_mib, sctp_statistics) __read_mostly; 681da177e4SLinus Torvalds 69c4e85f82SFlorian Westphal #ifdef CONFIG_PROC_FS 70c4e85f82SFlorian Westphal struct proc_dir_entry *proc_net_sctp; 71c4e85f82SFlorian Westphal #endif 72c4e85f82SFlorian Westphal 731da177e4SLinus Torvalds struct idr sctp_assocs_id; 741da177e4SLinus Torvalds DEFINE_SPINLOCK(sctp_assocs_id_lock); 751da177e4SLinus Torvalds 761da177e4SLinus Torvalds /* This is the global socket data structure used for responding to 771da177e4SLinus Torvalds * the Out-of-the-blue (OOTB) packets. A control sock will be created 781da177e4SLinus Torvalds * for this socket at the initialization time. 791da177e4SLinus Torvalds */ 808258175cSDenis V. Lunev static struct sock *sctp_ctl_sock; 811da177e4SLinus Torvalds 821da177e4SLinus Torvalds static struct sctp_pf *sctp_pf_inet6_specific; 831da177e4SLinus Torvalds static struct sctp_pf *sctp_pf_inet_specific; 841da177e4SLinus Torvalds static struct sctp_af *sctp_af_v4_specific; 851da177e4SLinus Torvalds static struct sctp_af *sctp_af_v6_specific; 861da177e4SLinus Torvalds 87e18b890bSChristoph Lameter struct kmem_cache *sctp_chunk_cachep __read_mostly; 88e18b890bSChristoph Lameter struct kmem_cache *sctp_bucket_cachep __read_mostly; 891da177e4SLinus Torvalds 90007e3936SVlad Yasevich int sysctl_sctp_mem[3]; 91007e3936SVlad Yasevich int sysctl_sctp_rmem[3]; 92007e3936SVlad Yasevich int sysctl_sctp_wmem[3]; 934d93df0aSNeil Horman 941da177e4SLinus Torvalds /* Return the address of the control sock. */ 951da177e4SLinus Torvalds struct sock *sctp_get_ctl_sock(void) 961da177e4SLinus Torvalds { 978258175cSDenis V. Lunev return sctp_ctl_sock; 981da177e4SLinus Torvalds } 991da177e4SLinus Torvalds 1001da177e4SLinus Torvalds /* Set up the proc fs entry for the SCTP protocol. */ 1011da177e4SLinus Torvalds static __init int sctp_proc_init(void) 1021da177e4SLinus Torvalds { 103c4e85f82SFlorian Westphal #ifdef CONFIG_PROC_FS 1041da177e4SLinus Torvalds if (!proc_net_sctp) { 1051da177e4SLinus Torvalds struct proc_dir_entry *ent; 106457c4cbcSEric W. Biederman ent = proc_mkdir("sctp", init_net.proc_net); 1071da177e4SLinus Torvalds if (ent) { 1081da177e4SLinus Torvalds ent->owner = THIS_MODULE; 1091da177e4SLinus Torvalds proc_net_sctp = ent; 1101da177e4SLinus Torvalds } else 1111da177e4SLinus Torvalds goto out_nomem; 1121da177e4SLinus Torvalds } 1131da177e4SLinus Torvalds 1141da177e4SLinus Torvalds if (sctp_snmp_proc_init()) 11580896a35SWei Yongjun goto out_snmp_proc_init; 1161da177e4SLinus Torvalds if (sctp_eps_proc_init()) 11780896a35SWei Yongjun goto out_eps_proc_init; 1181da177e4SLinus Torvalds if (sctp_assocs_proc_init()) 11980896a35SWei Yongjun goto out_assocs_proc_init; 12020c2c1fdSNeil Horman if (sctp_remaddr_proc_init()) 121caea902fSDavid S. Miller goto out_remaddr_proc_init; 1221da177e4SLinus Torvalds 1231da177e4SLinus Torvalds return 0; 1241da177e4SLinus Torvalds 125caea902fSDavid S. Miller out_remaddr_proc_init: 1263d00fb9eSPavel Emelyanov sctp_assocs_proc_exit(); 12780896a35SWei Yongjun out_assocs_proc_init: 12880896a35SWei Yongjun sctp_eps_proc_exit(); 12980896a35SWei Yongjun out_eps_proc_init: 13080896a35SWei Yongjun sctp_snmp_proc_exit(); 13180896a35SWei Yongjun out_snmp_proc_init: 13280896a35SWei Yongjun if (proc_net_sctp) { 13380896a35SWei Yongjun proc_net_sctp = NULL; 13480896a35SWei Yongjun remove_proc_entry("sctp", init_net.proc_net); 13580896a35SWei Yongjun } 1361da177e4SLinus Torvalds out_nomem: 1371da177e4SLinus Torvalds return -ENOMEM; 138c4e85f82SFlorian Westphal #else 139c4e85f82SFlorian Westphal return 0; 140c4e85f82SFlorian Westphal #endif /* CONFIG_PROC_FS */ 1411da177e4SLinus Torvalds } 1421da177e4SLinus Torvalds 1431da177e4SLinus Torvalds /* Clean up the proc fs entry for the SCTP protocol. 1441da177e4SLinus Torvalds * Note: Do not make this __exit as it is used in the init error 1451da177e4SLinus Torvalds * path. 1461da177e4SLinus Torvalds */ 1471da177e4SLinus Torvalds static void sctp_proc_exit(void) 1481da177e4SLinus Torvalds { 149c4e85f82SFlorian Westphal #ifdef CONFIG_PROC_FS 1501da177e4SLinus Torvalds sctp_snmp_proc_exit(); 1511da177e4SLinus Torvalds sctp_eps_proc_exit(); 1521da177e4SLinus Torvalds sctp_assocs_proc_exit(); 15320c2c1fdSNeil Horman sctp_remaddr_proc_exit(); 1541da177e4SLinus Torvalds 1551da177e4SLinus Torvalds if (proc_net_sctp) { 1561da177e4SLinus Torvalds proc_net_sctp = NULL; 157457c4cbcSEric W. Biederman remove_proc_entry("sctp", init_net.proc_net); 1581da177e4SLinus Torvalds } 159c4e85f82SFlorian Westphal #endif 1601da177e4SLinus Torvalds } 1611da177e4SLinus Torvalds 1621da177e4SLinus Torvalds /* Private helper to extract ipv4 address and stash them in 1631da177e4SLinus Torvalds * the protocol structure. 1641da177e4SLinus Torvalds */ 1651da177e4SLinus Torvalds static void sctp_v4_copy_addrlist(struct list_head *addrlist, 1661da177e4SLinus Torvalds struct net_device *dev) 1671da177e4SLinus Torvalds { 1681da177e4SLinus Torvalds struct in_device *in_dev; 1691da177e4SLinus Torvalds struct in_ifaddr *ifa; 1701da177e4SLinus Torvalds struct sctp_sockaddr_entry *addr; 1711da177e4SLinus Torvalds 1721da177e4SLinus Torvalds rcu_read_lock(); 173e5ed6399SHerbert Xu if ((in_dev = __in_dev_get_rcu(dev)) == NULL) { 1741da177e4SLinus Torvalds rcu_read_unlock(); 1751da177e4SLinus Torvalds return; 1761da177e4SLinus Torvalds } 1771da177e4SLinus Torvalds 1781da177e4SLinus Torvalds for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) { 1791da177e4SLinus Torvalds /* Add the address to the local list. */ 1801da177e4SLinus Torvalds addr = t_new(struct sctp_sockaddr_entry, GFP_ATOMIC); 1811da177e4SLinus Torvalds if (addr) { 1822a6fd78aSAl Viro addr->a.v4.sin_family = AF_INET; 1832a6fd78aSAl Viro addr->a.v4.sin_port = 0; 1842a6fd78aSAl Viro addr->a.v4.sin_addr.s_addr = ifa->ifa_local; 18529303547SVlad Yasevich addr->valid = 1; 18629303547SVlad Yasevich INIT_LIST_HEAD(&addr->list); 18729303547SVlad Yasevich INIT_RCU_HEAD(&addr->rcu); 1881da177e4SLinus Torvalds list_add_tail(&addr->list, addrlist); 1891da177e4SLinus Torvalds } 1901da177e4SLinus Torvalds } 1911da177e4SLinus Torvalds 1921da177e4SLinus Torvalds rcu_read_unlock(); 1931da177e4SLinus Torvalds } 1941da177e4SLinus Torvalds 1951da177e4SLinus Torvalds /* Extract our IP addresses from the system and stash them in the 1961da177e4SLinus Torvalds * protocol structure. 1971da177e4SLinus Torvalds */ 19829c7cf96SSridhar Samudrala static void sctp_get_local_addr_list(void) 1991da177e4SLinus Torvalds { 2001da177e4SLinus Torvalds struct net_device *dev; 2011da177e4SLinus Torvalds struct list_head *pos; 2021da177e4SLinus Torvalds struct sctp_af *af; 2031da177e4SLinus Torvalds 2041da177e4SLinus Torvalds read_lock(&dev_base_lock); 205881d966bSEric W. Biederman for_each_netdev(&init_net, dev) { 2061da177e4SLinus Torvalds __list_for_each(pos, &sctp_address_families) { 2071da177e4SLinus Torvalds af = list_entry(pos, struct sctp_af, list); 2081da177e4SLinus Torvalds af->copy_addrlist(&sctp_local_addr_list, dev); 2091da177e4SLinus Torvalds } 2101da177e4SLinus Torvalds } 2111da177e4SLinus Torvalds read_unlock(&dev_base_lock); 2121da177e4SLinus Torvalds } 2131da177e4SLinus Torvalds 2141da177e4SLinus Torvalds /* Free the existing local addresses. */ 21529c7cf96SSridhar Samudrala static void sctp_free_local_addr_list(void) 2161da177e4SLinus Torvalds { 2171da177e4SLinus Torvalds struct sctp_sockaddr_entry *addr; 2181da177e4SLinus Torvalds struct list_head *pos, *temp; 2191da177e4SLinus Torvalds 2201da177e4SLinus Torvalds list_for_each_safe(pos, temp, &sctp_local_addr_list) { 2211da177e4SLinus Torvalds addr = list_entry(pos, struct sctp_sockaddr_entry, list); 2221da177e4SLinus Torvalds list_del(pos); 2231da177e4SLinus Torvalds kfree(addr); 2241da177e4SLinus Torvalds } 2251da177e4SLinus Torvalds } 2261da177e4SLinus Torvalds 22729303547SVlad Yasevich void sctp_local_addr_free(struct rcu_head *head) 22829303547SVlad Yasevich { 22929303547SVlad Yasevich struct sctp_sockaddr_entry *e = container_of(head, 23029303547SVlad Yasevich struct sctp_sockaddr_entry, rcu); 23129303547SVlad Yasevich kfree(e); 23229303547SVlad Yasevich } 23329303547SVlad Yasevich 2341da177e4SLinus Torvalds /* Copy the local addresses which are valid for 'scope' into 'bp'. */ 2351da177e4SLinus Torvalds int sctp_copy_local_addr_list(struct sctp_bind_addr *bp, sctp_scope_t scope, 236dd0fc66fSAl Viro gfp_t gfp, int copy_flags) 2371da177e4SLinus Torvalds { 2381da177e4SLinus Torvalds struct sctp_sockaddr_entry *addr; 2391da177e4SLinus Torvalds int error = 0; 2401da177e4SLinus Torvalds 24129303547SVlad Yasevich rcu_read_lock(); 24229303547SVlad Yasevich list_for_each_entry_rcu(addr, &sctp_local_addr_list, list) { 24329303547SVlad Yasevich if (!addr->valid) 24429303547SVlad Yasevich continue; 2456244be4eSAl Viro if (sctp_in_scope(&addr->a, scope)) { 2461da177e4SLinus Torvalds /* Now that the address is in scope, check to see if 2471da177e4SLinus Torvalds * the address type is really supported by the local 2481da177e4SLinus Torvalds * sock as well as the remote peer. 2491da177e4SLinus Torvalds */ 2506244be4eSAl Viro if ((((AF_INET == addr->a.sa.sa_family) && 2511da177e4SLinus Torvalds (copy_flags & SCTP_ADDR4_PEERSUPP))) || 2526244be4eSAl Viro (((AF_INET6 == addr->a.sa.sa_family) && 2531da177e4SLinus Torvalds (copy_flags & SCTP_ADDR6_ALLOWED) && 2541da177e4SLinus Torvalds (copy_flags & SCTP_ADDR6_PEERSUPP)))) { 255f57d96b2SVlad Yasevich error = sctp_add_bind_addr(bp, &addr->a, 256f57d96b2SVlad Yasevich SCTP_ADDR_SRC, GFP_ATOMIC); 2571da177e4SLinus Torvalds if (error) 2581da177e4SLinus Torvalds goto end_copy; 2591da177e4SLinus Torvalds } 2601da177e4SLinus Torvalds } 2611da177e4SLinus Torvalds } 2621da177e4SLinus Torvalds 2631da177e4SLinus Torvalds end_copy: 26429303547SVlad Yasevich rcu_read_unlock(); 2651da177e4SLinus Torvalds return error; 2661da177e4SLinus Torvalds } 2671da177e4SLinus Torvalds 2681da177e4SLinus Torvalds /* Initialize a sctp_addr from in incoming skb. */ 2691da177e4SLinus Torvalds static void sctp_v4_from_skb(union sctp_addr *addr, struct sk_buff *skb, 2701da177e4SLinus Torvalds int is_saddr) 2711da177e4SLinus Torvalds { 2721da177e4SLinus Torvalds void *from; 273d55c41b1SAl Viro __be16 *port; 2741da177e4SLinus Torvalds struct sctphdr *sh; 2751da177e4SLinus Torvalds 2761da177e4SLinus Torvalds port = &addr->v4.sin_port; 2771da177e4SLinus Torvalds addr->v4.sin_family = AF_INET; 2781da177e4SLinus Torvalds 2792c0fd387SArnaldo Carvalho de Melo sh = sctp_hdr(skb); 2801da177e4SLinus Torvalds if (is_saddr) { 281d55c41b1SAl Viro *port = sh->source; 282eddc9ec5SArnaldo Carvalho de Melo from = &ip_hdr(skb)->saddr; 2831da177e4SLinus Torvalds } else { 284d55c41b1SAl Viro *port = sh->dest; 285eddc9ec5SArnaldo Carvalho de Melo from = &ip_hdr(skb)->daddr; 2861da177e4SLinus Torvalds } 2871da177e4SLinus Torvalds memcpy(&addr->v4.sin_addr.s_addr, from, sizeof(struct in_addr)); 2881da177e4SLinus Torvalds } 2891da177e4SLinus Torvalds 2901da177e4SLinus Torvalds /* Initialize an sctp_addr from a socket. */ 2911da177e4SLinus Torvalds static void sctp_v4_from_sk(union sctp_addr *addr, struct sock *sk) 2921da177e4SLinus Torvalds { 2931da177e4SLinus Torvalds addr->v4.sin_family = AF_INET; 2947dcdbd95SAl Viro addr->v4.sin_port = 0; 2951da177e4SLinus Torvalds addr->v4.sin_addr.s_addr = inet_sk(sk)->rcv_saddr; 2961da177e4SLinus Torvalds } 2971da177e4SLinus Torvalds 2981da177e4SLinus Torvalds /* Initialize sk->sk_rcv_saddr from sctp_addr. */ 2991da177e4SLinus Torvalds static void sctp_v4_to_sk_saddr(union sctp_addr *addr, struct sock *sk) 3001da177e4SLinus Torvalds { 3011da177e4SLinus Torvalds inet_sk(sk)->rcv_saddr = addr->v4.sin_addr.s_addr; 3021da177e4SLinus Torvalds } 3031da177e4SLinus Torvalds 3041da177e4SLinus Torvalds /* Initialize sk->sk_daddr from sctp_addr. */ 3051da177e4SLinus Torvalds static void sctp_v4_to_sk_daddr(union sctp_addr *addr, struct sock *sk) 3061da177e4SLinus Torvalds { 3071da177e4SLinus Torvalds inet_sk(sk)->daddr = addr->v4.sin_addr.s_addr; 3081da177e4SLinus Torvalds } 3091da177e4SLinus Torvalds 3101da177e4SLinus Torvalds /* Initialize a sctp_addr from an address parameter. */ 3111da177e4SLinus Torvalds static void sctp_v4_from_addr_param(union sctp_addr *addr, 3121da177e4SLinus Torvalds union sctp_addr_param *param, 313dd86d136SAl Viro __be16 port, int iif) 3141da177e4SLinus Torvalds { 3151da177e4SLinus Torvalds addr->v4.sin_family = AF_INET; 3161da177e4SLinus Torvalds addr->v4.sin_port = port; 3171da177e4SLinus Torvalds addr->v4.sin_addr.s_addr = param->v4.addr.s_addr; 3181da177e4SLinus Torvalds } 3191da177e4SLinus Torvalds 3201da177e4SLinus Torvalds /* Initialize an address parameter from a sctp_addr and return the length 3211da177e4SLinus Torvalds * of the address parameter. 3221da177e4SLinus Torvalds */ 3231da177e4SLinus Torvalds static int sctp_v4_to_addr_param(const union sctp_addr *addr, 3241da177e4SLinus Torvalds union sctp_addr_param *param) 3251da177e4SLinus Torvalds { 3261da177e4SLinus Torvalds int length = sizeof(sctp_ipv4addr_param_t); 3271da177e4SLinus Torvalds 3281da177e4SLinus Torvalds param->v4.param_hdr.type = SCTP_PARAM_IPV4_ADDRESS; 329dbc16db1SAl Viro param->v4.param_hdr.length = htons(length); 3301da177e4SLinus Torvalds param->v4.addr.s_addr = addr->v4.sin_addr.s_addr; 3311da177e4SLinus Torvalds 3321da177e4SLinus Torvalds return length; 3331da177e4SLinus Torvalds } 3341da177e4SLinus Torvalds 3351da177e4SLinus Torvalds /* Initialize a sctp_addr from a dst_entry. */ 3361da177e4SLinus Torvalds static void sctp_v4_dst_saddr(union sctp_addr *saddr, struct dst_entry *dst, 337854d43a4SAl Viro __be16 port) 3381da177e4SLinus Torvalds { 3391da177e4SLinus Torvalds struct rtable *rt = (struct rtable *)dst; 3401da177e4SLinus Torvalds saddr->v4.sin_family = AF_INET; 3411da177e4SLinus Torvalds saddr->v4.sin_port = port; 3421da177e4SLinus Torvalds saddr->v4.sin_addr.s_addr = rt->rt_src; 3431da177e4SLinus Torvalds } 3441da177e4SLinus Torvalds 3451da177e4SLinus Torvalds /* Compare two addresses exactly. */ 3461da177e4SLinus Torvalds static int sctp_v4_cmp_addr(const union sctp_addr *addr1, 3471da177e4SLinus Torvalds const union sctp_addr *addr2) 3481da177e4SLinus Torvalds { 3491da177e4SLinus Torvalds if (addr1->sa.sa_family != addr2->sa.sa_family) 3501da177e4SLinus Torvalds return 0; 3511da177e4SLinus Torvalds if (addr1->v4.sin_port != addr2->v4.sin_port) 3521da177e4SLinus Torvalds return 0; 3531da177e4SLinus Torvalds if (addr1->v4.sin_addr.s_addr != addr2->v4.sin_addr.s_addr) 3541da177e4SLinus Torvalds return 0; 3551da177e4SLinus Torvalds 3561da177e4SLinus Torvalds return 1; 3571da177e4SLinus Torvalds } 3581da177e4SLinus Torvalds 3591da177e4SLinus Torvalds /* Initialize addr struct to INADDR_ANY. */ 3606fbfa9f9SAl Viro static void sctp_v4_inaddr_any(union sctp_addr *addr, __be16 port) 3611da177e4SLinus Torvalds { 3621da177e4SLinus Torvalds addr->v4.sin_family = AF_INET; 363e6f1cebfSAl Viro addr->v4.sin_addr.s_addr = htonl(INADDR_ANY); 3641da177e4SLinus Torvalds addr->v4.sin_port = port; 3651da177e4SLinus Torvalds } 3661da177e4SLinus Torvalds 3671da177e4SLinus Torvalds /* Is this a wildcard address? */ 3681da177e4SLinus Torvalds static int sctp_v4_is_any(const union sctp_addr *addr) 3691da177e4SLinus Torvalds { 370e6f1cebfSAl Viro return htonl(INADDR_ANY) == addr->v4.sin_addr.s_addr; 3711da177e4SLinus Torvalds } 3721da177e4SLinus Torvalds 3731da177e4SLinus Torvalds /* This function checks if the address is a valid address to be used for 3741da177e4SLinus Torvalds * SCTP binding. 3751da177e4SLinus Torvalds * 3761da177e4SLinus Torvalds * Output: 3771da177e4SLinus Torvalds * Return 0 - If the address is a non-unicast or an illegal address. 3781da177e4SLinus Torvalds * Return 1 - If the address is a unicast. 3791da177e4SLinus Torvalds */ 3805636bef7SVlad Yasevich static int sctp_v4_addr_valid(union sctp_addr *addr, 3815636bef7SVlad Yasevich struct sctp_sock *sp, 3825636bef7SVlad Yasevich const struct sk_buff *skb) 3831da177e4SLinus Torvalds { 384*7dab83deSVlad Yasevich /* IPv4 addresses not allowed */ 385*7dab83deSVlad Yasevich if (sp && ipv6_only_sock(sctp_opt2sk(sp))) 386*7dab83deSVlad Yasevich return 0; 387*7dab83deSVlad Yasevich 3881da177e4SLinus Torvalds /* Is this a non-unicast address or a unusable SCTP address? */ 389b5cb2bbcSJoe Perches if (IS_IPV4_UNUSABLE_ADDRESS(addr->v4.sin_addr.s_addr)) 3901da177e4SLinus Torvalds return 0; 3911da177e4SLinus Torvalds 3925636bef7SVlad Yasevich /* Is this a broadcast address? */ 393ee6b9673SEric Dumazet if (skb && skb->rtable->rt_flags & RTCF_BROADCAST) 3945636bef7SVlad Yasevich return 0; 3955636bef7SVlad Yasevich 3961da177e4SLinus Torvalds return 1; 3971da177e4SLinus Torvalds } 3981da177e4SLinus Torvalds 3991da177e4SLinus Torvalds /* Should this be available for binding? */ 4001da177e4SLinus Torvalds static int sctp_v4_available(union sctp_addr *addr, struct sctp_sock *sp) 4011da177e4SLinus Torvalds { 4026b175b26SEric W. Biederman int ret = inet_addr_type(&init_net, addr->v4.sin_addr.s_addr); 4031da177e4SLinus Torvalds 4041da177e4SLinus Torvalds 405e6f1cebfSAl Viro if (addr->v4.sin_addr.s_addr != htonl(INADDR_ANY) && 406cdac4e07SNeil Horman ret != RTN_LOCAL && 407cdac4e07SNeil Horman !sp->inet.freebind && 408cdac4e07SNeil Horman !sysctl_ip_nonlocal_bind) 4091da177e4SLinus Torvalds return 0; 410cdac4e07SNeil Horman 411*7dab83deSVlad Yasevich if (ipv6_only_sock(sctp_opt2sk(sp))) 412*7dab83deSVlad Yasevich return 0; 413*7dab83deSVlad Yasevich 4141da177e4SLinus Torvalds return 1; 4151da177e4SLinus Torvalds } 4161da177e4SLinus Torvalds 4171da177e4SLinus Torvalds /* Checking the loopback, private and other address scopes as defined in 4181da177e4SLinus Torvalds * RFC 1918. The IPv4 scoping is based on the draft for SCTP IPv4 4191da177e4SLinus Torvalds * scoping <draft-stewart-tsvwg-sctp-ipv4-00.txt>. 4201da177e4SLinus Torvalds * 4211da177e4SLinus Torvalds * Level 0 - unusable SCTP addresses 4221da177e4SLinus Torvalds * Level 1 - loopback address 4231da177e4SLinus Torvalds * Level 2 - link-local addresses 4241da177e4SLinus Torvalds * Level 3 - private addresses. 4251da177e4SLinus Torvalds * Level 4 - global addresses 4261da177e4SLinus Torvalds * For INIT and INIT-ACK address list, let L be the level of 4271da177e4SLinus Torvalds * of requested destination address, sender and receiver 4281da177e4SLinus Torvalds * SHOULD include all of its addresses with level greater 4291da177e4SLinus Torvalds * than or equal to L. 4301da177e4SLinus Torvalds */ 4311da177e4SLinus Torvalds static sctp_scope_t sctp_v4_scope(union sctp_addr *addr) 4321da177e4SLinus Torvalds { 4331da177e4SLinus Torvalds sctp_scope_t retval; 4341da177e4SLinus Torvalds 4351da177e4SLinus Torvalds /* Should IPv4 scoping be a sysctl configurable option 4361da177e4SLinus Torvalds * so users can turn it off (default on) for certain 4371da177e4SLinus Torvalds * unconventional networking environments? 4381da177e4SLinus Torvalds */ 4391da177e4SLinus Torvalds 4401da177e4SLinus Torvalds /* Check for unusable SCTP addresses. */ 441b5cb2bbcSJoe Perches if (IS_IPV4_UNUSABLE_ADDRESS(addr->v4.sin_addr.s_addr)) { 4421da177e4SLinus Torvalds retval = SCTP_SCOPE_UNUSABLE; 443b5cb2bbcSJoe Perches } else if (ipv4_is_loopback(addr->v4.sin_addr.s_addr)) { 4441da177e4SLinus Torvalds retval = SCTP_SCOPE_LOOPBACK; 445b5cb2bbcSJoe Perches } else if (ipv4_is_linklocal_169(addr->v4.sin_addr.s_addr)) { 4461da177e4SLinus Torvalds retval = SCTP_SCOPE_LINK; 447b5cb2bbcSJoe Perches } else if (ipv4_is_private_10(addr->v4.sin_addr.s_addr) || 448b5cb2bbcSJoe Perches ipv4_is_private_172(addr->v4.sin_addr.s_addr) || 449b5cb2bbcSJoe Perches ipv4_is_private_192(addr->v4.sin_addr.s_addr)) { 4501da177e4SLinus Torvalds retval = SCTP_SCOPE_PRIVATE; 4511da177e4SLinus Torvalds } else { 4521da177e4SLinus Torvalds retval = SCTP_SCOPE_GLOBAL; 4531da177e4SLinus Torvalds } 4541da177e4SLinus Torvalds 4551da177e4SLinus Torvalds return retval; 4561da177e4SLinus Torvalds } 4571da177e4SLinus Torvalds 4581da177e4SLinus Torvalds /* Returns a valid dst cache entry for the given source and destination ip 4591da177e4SLinus Torvalds * addresses. If an association is passed, trys to get a dst entry with a 4601da177e4SLinus Torvalds * source address that matches an address in the bind address list. 4611da177e4SLinus Torvalds */ 4621da177e4SLinus Torvalds static struct dst_entry *sctp_v4_get_dst(struct sctp_association *asoc, 4631da177e4SLinus Torvalds union sctp_addr *daddr, 4641da177e4SLinus Torvalds union sctp_addr *saddr) 4651da177e4SLinus Torvalds { 4661da177e4SLinus Torvalds struct rtable *rt; 4671da177e4SLinus Torvalds struct flowi fl; 4681da177e4SLinus Torvalds struct sctp_bind_addr *bp; 4691da177e4SLinus Torvalds struct sctp_sockaddr_entry *laddr; 4701da177e4SLinus Torvalds struct dst_entry *dst = NULL; 4711da177e4SLinus Torvalds union sctp_addr dst_saddr; 4721da177e4SLinus Torvalds 4731da177e4SLinus Torvalds memset(&fl, 0x0, sizeof(struct flowi)); 4741da177e4SLinus Torvalds fl.fl4_dst = daddr->v4.sin_addr.s_addr; 4751da177e4SLinus Torvalds fl.proto = IPPROTO_SCTP; 4761da177e4SLinus Torvalds if (asoc) { 4771da177e4SLinus Torvalds fl.fl4_tos = RT_CONN_FLAGS(asoc->base.sk); 4781da177e4SLinus Torvalds fl.oif = asoc->base.sk->sk_bound_dev_if; 4791da177e4SLinus Torvalds } 4801da177e4SLinus Torvalds if (saddr) 4811da177e4SLinus Torvalds fl.fl4_src = saddr->v4.sin_addr.s_addr; 4821da177e4SLinus Torvalds 4831da177e4SLinus Torvalds SCTP_DEBUG_PRINTK("%s: DST:%u.%u.%u.%u, SRC:%u.%u.%u.%u - ", 4840dc47877SHarvey Harrison __func__, NIPQUAD(fl.fl4_dst), 4851da177e4SLinus Torvalds NIPQUAD(fl.fl4_src)); 4861da177e4SLinus Torvalds 487f206351aSDenis V. Lunev if (!ip_route_output_key(&init_net, &rt, &fl)) { 4881da177e4SLinus Torvalds dst = &rt->u.dst; 4891da177e4SLinus Torvalds } 4901da177e4SLinus Torvalds 4911da177e4SLinus Torvalds /* If there is no association or if a source address is passed, no 4921da177e4SLinus Torvalds * more validation is required. 4931da177e4SLinus Torvalds */ 4941da177e4SLinus Torvalds if (!asoc || saddr) 4951da177e4SLinus Torvalds goto out; 4961da177e4SLinus Torvalds 4971da177e4SLinus Torvalds bp = &asoc->base.bind_addr; 4981da177e4SLinus Torvalds 4991da177e4SLinus Torvalds if (dst) { 5001da177e4SLinus Torvalds /* Walk through the bind address list and look for a bind 5011da177e4SLinus Torvalds * address that matches the source address of the returned dst. 5021da177e4SLinus Torvalds */ 503159c6beaSGui Jianfeng sctp_v4_dst_saddr(&dst_saddr, dst, htons(bp->port)); 504559cf710SVlad Yasevich rcu_read_lock(); 505559cf710SVlad Yasevich list_for_each_entry_rcu(laddr, &bp->address_list, list) { 506f57d96b2SVlad Yasevich if (!laddr->valid || (laddr->state != SCTP_ADDR_SRC)) 507dc022a98SSridhar Samudrala continue; 508854d43a4SAl Viro if (sctp_v4_cmp_addr(&dst_saddr, &laddr->a)) 5091da177e4SLinus Torvalds goto out_unlock; 5101da177e4SLinus Torvalds } 511559cf710SVlad Yasevich rcu_read_unlock(); 5121da177e4SLinus Torvalds 5131da177e4SLinus Torvalds /* None of the bound addresses match the source address of the 5141da177e4SLinus Torvalds * dst. So release it. 5151da177e4SLinus Torvalds */ 5161da177e4SLinus Torvalds dst_release(dst); 5171da177e4SLinus Torvalds dst = NULL; 5181da177e4SLinus Torvalds } 5191da177e4SLinus Torvalds 5201da177e4SLinus Torvalds /* Walk through the bind address list and try to get a dst that 5211da177e4SLinus Torvalds * matches a bind address as the source address. 5221da177e4SLinus Torvalds */ 523559cf710SVlad Yasevich rcu_read_lock(); 524559cf710SVlad Yasevich list_for_each_entry_rcu(laddr, &bp->address_list, list) { 525559cf710SVlad Yasevich if (!laddr->valid) 526559cf710SVlad Yasevich continue; 527f57d96b2SVlad Yasevich if ((laddr->state == SCTP_ADDR_SRC) && 5286244be4eSAl Viro (AF_INET == laddr->a.sa.sa_family)) { 5296244be4eSAl Viro fl.fl4_src = laddr->a.v4.sin_addr.s_addr; 530f206351aSDenis V. Lunev if (!ip_route_output_key(&init_net, &rt, &fl)) { 5311da177e4SLinus Torvalds dst = &rt->u.dst; 5321da177e4SLinus Torvalds goto out_unlock; 5331da177e4SLinus Torvalds } 5341da177e4SLinus Torvalds } 5351da177e4SLinus Torvalds } 5361da177e4SLinus Torvalds 5371da177e4SLinus Torvalds out_unlock: 538559cf710SVlad Yasevich rcu_read_unlock(); 5391da177e4SLinus Torvalds out: 5401da177e4SLinus Torvalds if (dst) 5411da177e4SLinus Torvalds SCTP_DEBUG_PRINTK("rt_dst:%u.%u.%u.%u, rt_src:%u.%u.%u.%u\n", 5421da177e4SLinus Torvalds NIPQUAD(rt->rt_dst), NIPQUAD(rt->rt_src)); 5431da177e4SLinus Torvalds else 5441da177e4SLinus Torvalds SCTP_DEBUG_PRINTK("NO ROUTE\n"); 5451da177e4SLinus Torvalds 5461da177e4SLinus Torvalds return dst; 5471da177e4SLinus Torvalds } 5481da177e4SLinus Torvalds 5491da177e4SLinus Torvalds /* For v4, the source address is cached in the route entry(dst). So no need 5501da177e4SLinus Torvalds * to cache it separately and hence this is an empty routine. 5511da177e4SLinus Torvalds */ 552e5117101SYOSHIFUJI Hideaki static void sctp_v4_get_saddr(struct sctp_sock *sk, 553e5117101SYOSHIFUJI Hideaki struct sctp_association *asoc, 5541da177e4SLinus Torvalds struct dst_entry *dst, 5551da177e4SLinus Torvalds union sctp_addr *daddr, 5561da177e4SLinus Torvalds union sctp_addr *saddr) 5571da177e4SLinus Torvalds { 5581da177e4SLinus Torvalds struct rtable *rt = (struct rtable *)dst; 5591da177e4SLinus Torvalds 56023ec47a0SVladislav Yasevich if (!asoc) 56123ec47a0SVladislav Yasevich return; 56223ec47a0SVladislav Yasevich 5631da177e4SLinus Torvalds if (rt) { 5641da177e4SLinus Torvalds saddr->v4.sin_family = AF_INET; 565d3f7a54aSAl Viro saddr->v4.sin_port = htons(asoc->base.bind_addr.port); 5661da177e4SLinus Torvalds saddr->v4.sin_addr.s_addr = rt->rt_src; 5671da177e4SLinus Torvalds } 5681da177e4SLinus Torvalds } 5691da177e4SLinus Torvalds 5701da177e4SLinus Torvalds /* What interface did this skb arrive on? */ 5711da177e4SLinus Torvalds static int sctp_v4_skb_iif(const struct sk_buff *skb) 5721da177e4SLinus Torvalds { 573ee6b9673SEric Dumazet return skb->rtable->rt_iif; 5741da177e4SLinus Torvalds } 5751da177e4SLinus Torvalds 5761da177e4SLinus Torvalds /* Was this packet marked by Explicit Congestion Notification? */ 5771da177e4SLinus Torvalds static int sctp_v4_is_ce(const struct sk_buff *skb) 5781da177e4SLinus Torvalds { 579eddc9ec5SArnaldo Carvalho de Melo return INET_ECN_is_ce(ip_hdr(skb)->tos); 5801da177e4SLinus Torvalds } 5811da177e4SLinus Torvalds 5821da177e4SLinus Torvalds /* Create and initialize a new sk for the socket returned by accept(). */ 5831da177e4SLinus Torvalds static struct sock *sctp_v4_create_accept_sk(struct sock *sk, 5841da177e4SLinus Torvalds struct sctp_association *asoc) 5851da177e4SLinus Torvalds { 5861da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 5871da177e4SLinus Torvalds struct inet_sock *newinet; 5883b1e0a65SYOSHIFUJI Hideaki struct sock *newsk = sk_alloc(sock_net(sk), PF_INET, GFP_KERNEL, 5896257ff21SPavel Emelyanov sk->sk_prot); 5901da177e4SLinus Torvalds 5911da177e4SLinus Torvalds if (!newsk) 5921da177e4SLinus Torvalds goto out; 5931da177e4SLinus Torvalds 5941da177e4SLinus Torvalds sock_init_data(NULL, newsk); 5951da177e4SLinus Torvalds 5961da177e4SLinus Torvalds newsk->sk_type = SOCK_STREAM; 5971da177e4SLinus Torvalds 5981da177e4SLinus Torvalds newsk->sk_no_check = sk->sk_no_check; 5991da177e4SLinus Torvalds newsk->sk_reuse = sk->sk_reuse; 6001da177e4SLinus Torvalds newsk->sk_shutdown = sk->sk_shutdown; 6011da177e4SLinus Torvalds 6021da177e4SLinus Torvalds newsk->sk_destruct = inet_sock_destruct; 6031da177e4SLinus Torvalds newsk->sk_family = PF_INET; 6041da177e4SLinus Torvalds newsk->sk_protocol = IPPROTO_SCTP; 6051da177e4SLinus Torvalds newsk->sk_backlog_rcv = sk->sk_prot->backlog_rcv; 6061da177e4SLinus Torvalds sock_reset_flag(newsk, SOCK_ZAPPED); 6071da177e4SLinus Torvalds 6081da177e4SLinus Torvalds newinet = inet_sk(newsk); 6091da177e4SLinus Torvalds 6101da177e4SLinus Torvalds /* Initialize sk's sport, dport, rcv_saddr and daddr for 6111da177e4SLinus Torvalds * getsockname() and getpeername() 6121da177e4SLinus Torvalds */ 6131da177e4SLinus Torvalds newinet->sport = inet->sport; 6141da177e4SLinus Torvalds newinet->saddr = inet->saddr; 6151da177e4SLinus Torvalds newinet->rcv_saddr = inet->rcv_saddr; 6161da177e4SLinus Torvalds newinet->dport = htons(asoc->peer.port); 6171da177e4SLinus Torvalds newinet->daddr = asoc->peer.primary_addr.v4.sin_addr.s_addr; 6181da177e4SLinus Torvalds newinet->pmtudisc = inet->pmtudisc; 6194f444308SVlad Yasevich newinet->id = asoc->next_tsn ^ jiffies; 6201da177e4SLinus Torvalds 6211da177e4SLinus Torvalds newinet->uc_ttl = -1; 6221da177e4SLinus Torvalds newinet->mc_loop = 1; 6231da177e4SLinus Torvalds newinet->mc_ttl = 1; 6241da177e4SLinus Torvalds newinet->mc_index = 0; 6251da177e4SLinus Torvalds newinet->mc_list = NULL; 6261da177e4SLinus Torvalds 627e6848976SArnaldo Carvalho de Melo sk_refcnt_debug_inc(newsk); 6281da177e4SLinus Torvalds 6291da177e4SLinus Torvalds if (newsk->sk_prot->init(newsk)) { 6301da177e4SLinus Torvalds sk_common_release(newsk); 6311da177e4SLinus Torvalds newsk = NULL; 6321da177e4SLinus Torvalds } 6331da177e4SLinus Torvalds 6341da177e4SLinus Torvalds out: 6351da177e4SLinus Torvalds return newsk; 6361da177e4SLinus Torvalds } 6371da177e4SLinus Torvalds 6381da177e4SLinus Torvalds /* Map address, empty for v4 family */ 6391da177e4SLinus Torvalds static void sctp_v4_addr_v4map(struct sctp_sock *sp, union sctp_addr *addr) 6401da177e4SLinus Torvalds { 6411da177e4SLinus Torvalds /* Empty */ 6421da177e4SLinus Torvalds } 6431da177e4SLinus Torvalds 6441da177e4SLinus Torvalds /* Dump the v4 addr to the seq file. */ 6451da177e4SLinus Torvalds static void sctp_v4_seq_dump_addr(struct seq_file *seq, union sctp_addr *addr) 6461da177e4SLinus Torvalds { 6471da177e4SLinus Torvalds seq_printf(seq, "%d.%d.%d.%d ", NIPQUAD(addr->v4.sin_addr)); 6481da177e4SLinus Torvalds } 6491da177e4SLinus Torvalds 650b9031d9dSVlad Yasevich static void sctp_v4_ecn_capable(struct sock *sk) 651b9031d9dSVlad Yasevich { 652b9031d9dSVlad Yasevich INET_ECN_xmit(sk); 653b9031d9dSVlad Yasevich } 654b9031d9dSVlad Yasevich 65529303547SVlad Yasevich /* Event handler for inet address addition/deletion events. 65629303547SVlad Yasevich * The sctp_local_addr_list needs to be protocted by a spin lock since 65729303547SVlad Yasevich * multiple notifiers (say IPv4 and IPv6) may be running at the same 65829303547SVlad Yasevich * time and thus corrupt the list. 65929303547SVlad Yasevich * The reader side is protected with RCU. 66029303547SVlad Yasevich */ 66124123186SAdrian Bunk static int sctp_inetaddr_event(struct notifier_block *this, unsigned long ev, 6621da177e4SLinus Torvalds void *ptr) 6631da177e4SLinus Torvalds { 66429c7cf96SSridhar Samudrala struct in_ifaddr *ifa = (struct in_ifaddr *)ptr; 66529303547SVlad Yasevich struct sctp_sockaddr_entry *addr = NULL; 66629303547SVlad Yasevich struct sctp_sockaddr_entry *temp; 66722626216SChidambar 'ilLogict' Zinnoury int found = 0; 6681da177e4SLinus Torvalds 669c346dca1SYOSHIFUJI Hideaki if (dev_net(ifa->ifa_dev->dev) != &init_net) 6706133fb1aSDenis V. Lunev return NOTIFY_DONE; 6716133fb1aSDenis V. Lunev 67229c7cf96SSridhar Samudrala switch (ev) { 67329c7cf96SSridhar Samudrala case NETDEV_UP: 67429c7cf96SSridhar Samudrala addr = kmalloc(sizeof(struct sctp_sockaddr_entry), GFP_ATOMIC); 67529c7cf96SSridhar Samudrala if (addr) { 67629c7cf96SSridhar Samudrala addr->a.v4.sin_family = AF_INET; 67729c7cf96SSridhar Samudrala addr->a.v4.sin_port = 0; 67829c7cf96SSridhar Samudrala addr->a.v4.sin_addr.s_addr = ifa->ifa_local; 67929303547SVlad Yasevich addr->valid = 1; 68029303547SVlad Yasevich spin_lock_bh(&sctp_local_addr_lock); 68129303547SVlad Yasevich list_add_tail_rcu(&addr->list, &sctp_local_addr_list); 68229303547SVlad Yasevich spin_unlock_bh(&sctp_local_addr_lock); 68329c7cf96SSridhar Samudrala } 68429c7cf96SSridhar Samudrala break; 68529c7cf96SSridhar Samudrala case NETDEV_DOWN: 68629303547SVlad Yasevich spin_lock_bh(&sctp_local_addr_lock); 68729303547SVlad Yasevich list_for_each_entry_safe(addr, temp, 68829303547SVlad Yasevich &sctp_local_addr_list, list) { 689a40a7d15SPavel Emelyanov if (addr->a.sa.sa_family == AF_INET && 690a40a7d15SPavel Emelyanov addr->a.v4.sin_addr.s_addr == 691a40a7d15SPavel Emelyanov ifa->ifa_local) { 69222626216SChidambar 'ilLogict' Zinnoury found = 1; 69329303547SVlad Yasevich addr->valid = 0; 69429303547SVlad Yasevich list_del_rcu(&addr->list); 69529c7cf96SSridhar Samudrala break; 69629c7cf96SSridhar Samudrala } 69729c7cf96SSridhar Samudrala } 69829303547SVlad Yasevich spin_unlock_bh(&sctp_local_addr_lock); 69922626216SChidambar 'ilLogict' Zinnoury if (found) 70029303547SVlad Yasevich call_rcu(&addr->rcu, sctp_local_addr_free); 70129c7cf96SSridhar Samudrala break; 70229c7cf96SSridhar Samudrala } 7031da177e4SLinus Torvalds 7041da177e4SLinus Torvalds return NOTIFY_DONE; 7051da177e4SLinus Torvalds } 7061da177e4SLinus Torvalds 7071da177e4SLinus Torvalds /* 7081da177e4SLinus Torvalds * Initialize the control inode/socket with a control endpoint data 7091da177e4SLinus Torvalds * structure. This endpoint is reserved exclusively for the OOTB processing. 7101da177e4SLinus Torvalds */ 7111da177e4SLinus Torvalds static int sctp_ctl_sock_init(void) 7121da177e4SLinus Torvalds { 7131da177e4SLinus Torvalds int err; 7141da177e4SLinus Torvalds sa_family_t family; 7151da177e4SLinus Torvalds 7161da177e4SLinus Torvalds if (sctp_get_pf_specific(PF_INET6)) 7171da177e4SLinus Torvalds family = PF_INET6; 7181da177e4SLinus Torvalds else 7191da177e4SLinus Torvalds family = PF_INET; 7201da177e4SLinus Torvalds 721eee4fe4dSDenis V. Lunev err = inet_ctl_sock_create(&sctp_ctl_sock, family, 7225677242fSDenis V. Lunev SOCK_SEQPACKET, IPPROTO_SCTP, &init_net); 7231da177e4SLinus Torvalds if (err < 0) { 7241da177e4SLinus Torvalds printk(KERN_ERR 7251da177e4SLinus Torvalds "SCTP: Failed to create the SCTP control socket.\n"); 7261da177e4SLinus Torvalds return err; 7271da177e4SLinus Torvalds } 7281da177e4SLinus Torvalds return 0; 7291da177e4SLinus Torvalds } 7301da177e4SLinus Torvalds 7311da177e4SLinus Torvalds /* Register address family specific functions. */ 7321da177e4SLinus Torvalds int sctp_register_af(struct sctp_af *af) 7331da177e4SLinus Torvalds { 7341da177e4SLinus Torvalds switch (af->sa_family) { 7351da177e4SLinus Torvalds case AF_INET: 7361da177e4SLinus Torvalds if (sctp_af_v4_specific) 7371da177e4SLinus Torvalds return 0; 7381da177e4SLinus Torvalds sctp_af_v4_specific = af; 7391da177e4SLinus Torvalds break; 7401da177e4SLinus Torvalds case AF_INET6: 7411da177e4SLinus Torvalds if (sctp_af_v6_specific) 7421da177e4SLinus Torvalds return 0; 7431da177e4SLinus Torvalds sctp_af_v6_specific = af; 7441da177e4SLinus Torvalds break; 7451da177e4SLinus Torvalds default: 7461da177e4SLinus Torvalds return 0; 7471da177e4SLinus Torvalds } 7481da177e4SLinus Torvalds 7491da177e4SLinus Torvalds INIT_LIST_HEAD(&af->list); 7501da177e4SLinus Torvalds list_add_tail(&af->list, &sctp_address_families); 7511da177e4SLinus Torvalds return 1; 7521da177e4SLinus Torvalds } 7531da177e4SLinus Torvalds 7541da177e4SLinus Torvalds /* Get the table of functions for manipulating a particular address 7551da177e4SLinus Torvalds * family. 7561da177e4SLinus Torvalds */ 7571da177e4SLinus Torvalds struct sctp_af *sctp_get_af_specific(sa_family_t family) 7581da177e4SLinus Torvalds { 7591da177e4SLinus Torvalds switch (family) { 7601da177e4SLinus Torvalds case AF_INET: 7611da177e4SLinus Torvalds return sctp_af_v4_specific; 7621da177e4SLinus Torvalds case AF_INET6: 7631da177e4SLinus Torvalds return sctp_af_v6_specific; 7641da177e4SLinus Torvalds default: 7651da177e4SLinus Torvalds return NULL; 7661da177e4SLinus Torvalds } 7671da177e4SLinus Torvalds } 7681da177e4SLinus Torvalds 7691da177e4SLinus Torvalds /* Common code to initialize a AF_INET msg_name. */ 7701da177e4SLinus Torvalds static void sctp_inet_msgname(char *msgname, int *addr_len) 7711da177e4SLinus Torvalds { 7721da177e4SLinus Torvalds struct sockaddr_in *sin; 7731da177e4SLinus Torvalds 7741da177e4SLinus Torvalds sin = (struct sockaddr_in *)msgname; 7751da177e4SLinus Torvalds *addr_len = sizeof(struct sockaddr_in); 7761da177e4SLinus Torvalds sin->sin_family = AF_INET; 7771da177e4SLinus Torvalds memset(sin->sin_zero, 0, sizeof(sin->sin_zero)); 7781da177e4SLinus Torvalds } 7791da177e4SLinus Torvalds 7801da177e4SLinus Torvalds /* Copy the primary address of the peer primary address as the msg_name. */ 7811da177e4SLinus Torvalds static void sctp_inet_event_msgname(struct sctp_ulpevent *event, char *msgname, 7821da177e4SLinus Torvalds int *addr_len) 7831da177e4SLinus Torvalds { 7841da177e4SLinus Torvalds struct sockaddr_in *sin, *sinfrom; 7851da177e4SLinus Torvalds 7861da177e4SLinus Torvalds if (msgname) { 7871da177e4SLinus Torvalds struct sctp_association *asoc; 7881da177e4SLinus Torvalds 7891da177e4SLinus Torvalds asoc = event->asoc; 7901da177e4SLinus Torvalds sctp_inet_msgname(msgname, addr_len); 7911da177e4SLinus Torvalds sin = (struct sockaddr_in *)msgname; 7921da177e4SLinus Torvalds sinfrom = &asoc->peer.primary_addr.v4; 7931da177e4SLinus Torvalds sin->sin_port = htons(asoc->peer.port); 7941da177e4SLinus Torvalds sin->sin_addr.s_addr = sinfrom->sin_addr.s_addr; 7951da177e4SLinus Torvalds } 7961da177e4SLinus Torvalds } 7971da177e4SLinus Torvalds 7981da177e4SLinus Torvalds /* Initialize and copy out a msgname from an inbound skb. */ 7991da177e4SLinus Torvalds static void sctp_inet_skb_msgname(struct sk_buff *skb, char *msgname, int *len) 8001da177e4SLinus Torvalds { 8011da177e4SLinus Torvalds if (msgname) { 8022c0fd387SArnaldo Carvalho de Melo struct sctphdr *sh = sctp_hdr(skb); 8032c0fd387SArnaldo Carvalho de Melo struct sockaddr_in *sin = (struct sockaddr_in *)msgname; 8042c0fd387SArnaldo Carvalho de Melo 8051da177e4SLinus Torvalds sctp_inet_msgname(msgname, len); 8061da177e4SLinus Torvalds sin->sin_port = sh->source; 807eddc9ec5SArnaldo Carvalho de Melo sin->sin_addr.s_addr = ip_hdr(skb)->saddr; 8081da177e4SLinus Torvalds } 8091da177e4SLinus Torvalds } 8101da177e4SLinus Torvalds 8111da177e4SLinus Torvalds /* Do we support this AF? */ 8121da177e4SLinus Torvalds static int sctp_inet_af_supported(sa_family_t family, struct sctp_sock *sp) 8131da177e4SLinus Torvalds { 8141da177e4SLinus Torvalds /* PF_INET only supports AF_INET addresses. */ 8151da177e4SLinus Torvalds return (AF_INET == family); 8161da177e4SLinus Torvalds } 8171da177e4SLinus Torvalds 8181da177e4SLinus Torvalds /* Address matching with wildcards allowed. */ 8191da177e4SLinus Torvalds static int sctp_inet_cmp_addr(const union sctp_addr *addr1, 8201da177e4SLinus Torvalds const union sctp_addr *addr2, 8211da177e4SLinus Torvalds struct sctp_sock *opt) 8221da177e4SLinus Torvalds { 8231da177e4SLinus Torvalds /* PF_INET only supports AF_INET addresses. */ 8241da177e4SLinus Torvalds if (addr1->sa.sa_family != addr2->sa.sa_family) 8251da177e4SLinus Torvalds return 0; 826e6f1cebfSAl Viro if (htonl(INADDR_ANY) == addr1->v4.sin_addr.s_addr || 827e6f1cebfSAl Viro htonl(INADDR_ANY) == addr2->v4.sin_addr.s_addr) 8281da177e4SLinus Torvalds return 1; 8291da177e4SLinus Torvalds if (addr1->v4.sin_addr.s_addr == addr2->v4.sin_addr.s_addr) 8301da177e4SLinus Torvalds return 1; 8311da177e4SLinus Torvalds 8321da177e4SLinus Torvalds return 0; 8331da177e4SLinus Torvalds } 8341da177e4SLinus Torvalds 8351da177e4SLinus Torvalds /* Verify that provided sockaddr looks bindable. Common verification has 8361da177e4SLinus Torvalds * already been taken care of. 8371da177e4SLinus Torvalds */ 8381da177e4SLinus Torvalds static int sctp_inet_bind_verify(struct sctp_sock *opt, union sctp_addr *addr) 8391da177e4SLinus Torvalds { 8401da177e4SLinus Torvalds return sctp_v4_available(addr, opt); 8411da177e4SLinus Torvalds } 8421da177e4SLinus Torvalds 8431da177e4SLinus Torvalds /* Verify that sockaddr looks sendable. Common verification has already 8441da177e4SLinus Torvalds * been taken care of. 8451da177e4SLinus Torvalds */ 8461da177e4SLinus Torvalds static int sctp_inet_send_verify(struct sctp_sock *opt, union sctp_addr *addr) 8471da177e4SLinus Torvalds { 8481da177e4SLinus Torvalds return 1; 8491da177e4SLinus Torvalds } 8501da177e4SLinus Torvalds 8511da177e4SLinus Torvalds /* Fill in Supported Address Type information for INIT and INIT-ACK 8521da177e4SLinus Torvalds * chunks. Returns number of addresses supported. 8531da177e4SLinus Torvalds */ 8541da177e4SLinus Torvalds static int sctp_inet_supported_addrs(const struct sctp_sock *opt, 8553dbe8656SAl Viro __be16 *types) 8561da177e4SLinus Torvalds { 8571da177e4SLinus Torvalds types[0] = SCTP_PARAM_IPV4_ADDRESS; 8581da177e4SLinus Torvalds return 1; 8591da177e4SLinus Torvalds } 8601da177e4SLinus Torvalds 8611da177e4SLinus Torvalds /* Wrapper routine that calls the ip transmit routine. */ 8621da177e4SLinus Torvalds static inline int sctp_v4_xmit(struct sk_buff *skb, 8631da177e4SLinus Torvalds struct sctp_transport *transport, int ipfragok) 8641da177e4SLinus Torvalds { 8651da177e4SLinus Torvalds SCTP_DEBUG_PRINTK("%s: skb:%p, len:%d, " 8661da177e4SLinus Torvalds "src:%u.%u.%u.%u, dst:%u.%u.%u.%u\n", 8670dc47877SHarvey Harrison __func__, skb, skb->len, 868ee6b9673SEric Dumazet NIPQUAD(skb->rtable->rt_src), 869ee6b9673SEric Dumazet NIPQUAD(skb->rtable->rt_dst)); 8701da177e4SLinus Torvalds 8711da177e4SLinus Torvalds SCTP_INC_STATS(SCTP_MIB_OUTSCTPPACKS); 872e89862f4SDavid S. Miller return ip_queue_xmit(skb, ipfragok); 8731da177e4SLinus Torvalds } 8741da177e4SLinus Torvalds 87515efbe76SNeil Horman static struct sctp_af sctp_af_inet; 8761da177e4SLinus Torvalds 8771da177e4SLinus Torvalds static struct sctp_pf sctp_pf_inet = { 8781da177e4SLinus Torvalds .event_msgname = sctp_inet_event_msgname, 8791da177e4SLinus Torvalds .skb_msgname = sctp_inet_skb_msgname, 8801da177e4SLinus Torvalds .af_supported = sctp_inet_af_supported, 8811da177e4SLinus Torvalds .cmp_addr = sctp_inet_cmp_addr, 8821da177e4SLinus Torvalds .bind_verify = sctp_inet_bind_verify, 8831da177e4SLinus Torvalds .send_verify = sctp_inet_send_verify, 8841da177e4SLinus Torvalds .supported_addrs = sctp_inet_supported_addrs, 8851da177e4SLinus Torvalds .create_accept_sk = sctp_v4_create_accept_sk, 8861da177e4SLinus Torvalds .addr_v4map = sctp_v4_addr_v4map, 88715efbe76SNeil Horman .af = &sctp_af_inet 8881da177e4SLinus Torvalds }; 8891da177e4SLinus Torvalds 8901da177e4SLinus Torvalds /* Notifier for inetaddr addition/deletion events. */ 8911da177e4SLinus Torvalds static struct notifier_block sctp_inetaddr_notifier = { 8921da177e4SLinus Torvalds .notifier_call = sctp_inetaddr_event, 8931da177e4SLinus Torvalds }; 8941da177e4SLinus Torvalds 8951da177e4SLinus Torvalds /* Socket operations. */ 89690ddc4f0SEric Dumazet static const struct proto_ops inet_seqpacket_ops = { 8971da177e4SLinus Torvalds .family = PF_INET, 8981da177e4SLinus Torvalds .owner = THIS_MODULE, 8991da177e4SLinus Torvalds .release = inet_release, /* Needs to be wrapped... */ 9001da177e4SLinus Torvalds .bind = inet_bind, 9011da177e4SLinus Torvalds .connect = inet_dgram_connect, 9021da177e4SLinus Torvalds .socketpair = sock_no_socketpair, 9031da177e4SLinus Torvalds .accept = inet_accept, 9041da177e4SLinus Torvalds .getname = inet_getname, /* Semantics are different. */ 9051da177e4SLinus Torvalds .poll = sctp_poll, 9061da177e4SLinus Torvalds .ioctl = inet_ioctl, 9071da177e4SLinus Torvalds .listen = sctp_inet_listen, 9081da177e4SLinus Torvalds .shutdown = inet_shutdown, /* Looks harmless. */ 909543d9cfeSArnaldo Carvalho de Melo .setsockopt = sock_common_setsockopt, /* IP_SOL IP_OPTION is a problem */ 9101da177e4SLinus Torvalds .getsockopt = sock_common_getsockopt, 9111da177e4SLinus Torvalds .sendmsg = inet_sendmsg, 9121da177e4SLinus Torvalds .recvmsg = sock_common_recvmsg, 9131da177e4SLinus Torvalds .mmap = sock_no_mmap, 9141da177e4SLinus Torvalds .sendpage = sock_no_sendpage, 915543d9cfeSArnaldo Carvalho de Melo #ifdef CONFIG_COMPAT 916543d9cfeSArnaldo Carvalho de Melo .compat_setsockopt = compat_sock_common_setsockopt, 917543d9cfeSArnaldo Carvalho de Melo .compat_getsockopt = compat_sock_common_getsockopt, 918543d9cfeSArnaldo Carvalho de Melo #endif 9191da177e4SLinus Torvalds }; 9201da177e4SLinus Torvalds 9211da177e4SLinus Torvalds /* Registration with AF_INET family. */ 9221da177e4SLinus Torvalds static struct inet_protosw sctp_seqpacket_protosw = { 9231da177e4SLinus Torvalds .type = SOCK_SEQPACKET, 9241da177e4SLinus Torvalds .protocol = IPPROTO_SCTP, 9251da177e4SLinus Torvalds .prot = &sctp_prot, 9261da177e4SLinus Torvalds .ops = &inet_seqpacket_ops, 9271da177e4SLinus Torvalds .capability = -1, 9281da177e4SLinus Torvalds .no_check = 0, 9291da177e4SLinus Torvalds .flags = SCTP_PROTOSW_FLAG 9301da177e4SLinus Torvalds }; 9311da177e4SLinus Torvalds static struct inet_protosw sctp_stream_protosw = { 9321da177e4SLinus Torvalds .type = SOCK_STREAM, 9331da177e4SLinus Torvalds .protocol = IPPROTO_SCTP, 9341da177e4SLinus Torvalds .prot = &sctp_prot, 9351da177e4SLinus Torvalds .ops = &inet_seqpacket_ops, 9361da177e4SLinus Torvalds .capability = -1, 9371da177e4SLinus Torvalds .no_check = 0, 9381da177e4SLinus Torvalds .flags = SCTP_PROTOSW_FLAG 9391da177e4SLinus Torvalds }; 9401da177e4SLinus Torvalds 9411da177e4SLinus Torvalds /* Register with IP layer. */ 9421da177e4SLinus Torvalds static struct net_protocol sctp_protocol = { 9431da177e4SLinus Torvalds .handler = sctp_rcv, 9441da177e4SLinus Torvalds .err_handler = sctp_v4_err, 9451da177e4SLinus Torvalds .no_policy = 1, 9461da177e4SLinus Torvalds }; 9471da177e4SLinus Torvalds 9481da177e4SLinus Torvalds /* IPv4 address related functions. */ 94915efbe76SNeil Horman static struct sctp_af sctp_af_inet = { 950543d9cfeSArnaldo Carvalho de Melo .sa_family = AF_INET, 9511da177e4SLinus Torvalds .sctp_xmit = sctp_v4_xmit, 9521da177e4SLinus Torvalds .setsockopt = ip_setsockopt, 9531da177e4SLinus Torvalds .getsockopt = ip_getsockopt, 9541da177e4SLinus Torvalds .get_dst = sctp_v4_get_dst, 9551da177e4SLinus Torvalds .get_saddr = sctp_v4_get_saddr, 9561da177e4SLinus Torvalds .copy_addrlist = sctp_v4_copy_addrlist, 9571da177e4SLinus Torvalds .from_skb = sctp_v4_from_skb, 9581da177e4SLinus Torvalds .from_sk = sctp_v4_from_sk, 9591da177e4SLinus Torvalds .to_sk_saddr = sctp_v4_to_sk_saddr, 9601da177e4SLinus Torvalds .to_sk_daddr = sctp_v4_to_sk_daddr, 9611da177e4SLinus Torvalds .from_addr_param = sctp_v4_from_addr_param, 9621da177e4SLinus Torvalds .to_addr_param = sctp_v4_to_addr_param, 9631da177e4SLinus Torvalds .dst_saddr = sctp_v4_dst_saddr, 9641da177e4SLinus Torvalds .cmp_addr = sctp_v4_cmp_addr, 9651da177e4SLinus Torvalds .addr_valid = sctp_v4_addr_valid, 9661da177e4SLinus Torvalds .inaddr_any = sctp_v4_inaddr_any, 9671da177e4SLinus Torvalds .is_any = sctp_v4_is_any, 9681da177e4SLinus Torvalds .available = sctp_v4_available, 9691da177e4SLinus Torvalds .scope = sctp_v4_scope, 9701da177e4SLinus Torvalds .skb_iif = sctp_v4_skb_iif, 9711da177e4SLinus Torvalds .is_ce = sctp_v4_is_ce, 9721da177e4SLinus Torvalds .seq_dump_addr = sctp_v4_seq_dump_addr, 973b9031d9dSVlad Yasevich .ecn_capable = sctp_v4_ecn_capable, 9741da177e4SLinus Torvalds .net_header_len = sizeof(struct iphdr), 9751da177e4SLinus Torvalds .sockaddr_len = sizeof(struct sockaddr_in), 976543d9cfeSArnaldo Carvalho de Melo #ifdef CONFIG_COMPAT 977543d9cfeSArnaldo Carvalho de Melo .compat_setsockopt = compat_ip_setsockopt, 978543d9cfeSArnaldo Carvalho de Melo .compat_getsockopt = compat_ip_getsockopt, 979543d9cfeSArnaldo Carvalho de Melo #endif 9801da177e4SLinus Torvalds }; 9811da177e4SLinus Torvalds 9821da177e4SLinus Torvalds struct sctp_pf *sctp_get_pf_specific(sa_family_t family) { 9831da177e4SLinus Torvalds 9841da177e4SLinus Torvalds switch (family) { 9851da177e4SLinus Torvalds case PF_INET: 9861da177e4SLinus Torvalds return sctp_pf_inet_specific; 9871da177e4SLinus Torvalds case PF_INET6: 9881da177e4SLinus Torvalds return sctp_pf_inet6_specific; 9891da177e4SLinus Torvalds default: 9901da177e4SLinus Torvalds return NULL; 9911da177e4SLinus Torvalds } 9921da177e4SLinus Torvalds } 9931da177e4SLinus Torvalds 9941da177e4SLinus Torvalds /* Register the PF specific function table. */ 9951da177e4SLinus Torvalds int sctp_register_pf(struct sctp_pf *pf, sa_family_t family) 9961da177e4SLinus Torvalds { 9971da177e4SLinus Torvalds switch (family) { 9981da177e4SLinus Torvalds case PF_INET: 9991da177e4SLinus Torvalds if (sctp_pf_inet_specific) 10001da177e4SLinus Torvalds return 0; 10011da177e4SLinus Torvalds sctp_pf_inet_specific = pf; 10021da177e4SLinus Torvalds break; 10031da177e4SLinus Torvalds case PF_INET6: 10041da177e4SLinus Torvalds if (sctp_pf_inet6_specific) 10051da177e4SLinus Torvalds return 0; 10061da177e4SLinus Torvalds sctp_pf_inet6_specific = pf; 10071da177e4SLinus Torvalds break; 10081da177e4SLinus Torvalds default: 10091da177e4SLinus Torvalds return 0; 10101da177e4SLinus Torvalds } 10111da177e4SLinus Torvalds return 1; 10121da177e4SLinus Torvalds } 10131da177e4SLinus Torvalds 1014996b1dbaSYOSHIFUJI Hideaki static inline int init_sctp_mibs(void) 10151da177e4SLinus Torvalds { 1016996b1dbaSYOSHIFUJI Hideaki return snmp_mib_init((void**)sctp_statistics, sizeof(struct sctp_mib)); 10171da177e4SLinus Torvalds } 10181da177e4SLinus Torvalds 1019996b1dbaSYOSHIFUJI Hideaki static inline void cleanup_sctp_mibs(void) 10201da177e4SLinus Torvalds { 1021996b1dbaSYOSHIFUJI Hideaki snmp_mib_free((void**)sctp_statistics); 10221da177e4SLinus Torvalds } 10231da177e4SLinus Torvalds 1024270637abSVlad Yasevich static void sctp_v4_pf_init(void) 1025270637abSVlad Yasevich { 1026270637abSVlad Yasevich /* Initialize the SCTP specific PF functions. */ 1027270637abSVlad Yasevich sctp_register_pf(&sctp_pf_inet, PF_INET); 1028270637abSVlad Yasevich sctp_register_af(&sctp_af_inet); 1029270637abSVlad Yasevich } 1030270637abSVlad Yasevich 1031270637abSVlad Yasevich static void sctp_v4_pf_exit(void) 1032270637abSVlad Yasevich { 1033270637abSVlad Yasevich list_del(&sctp_af_inet.list); 1034270637abSVlad Yasevich } 1035270637abSVlad Yasevich 1036270637abSVlad Yasevich static int sctp_v4_protosw_init(void) 1037270637abSVlad Yasevich { 1038270637abSVlad Yasevich int rc; 1039270637abSVlad Yasevich 1040270637abSVlad Yasevich rc = proto_register(&sctp_prot, 1); 1041270637abSVlad Yasevich if (rc) 1042270637abSVlad Yasevich return rc; 1043270637abSVlad Yasevich 1044270637abSVlad Yasevich /* Register SCTP(UDP and TCP style) with socket layer. */ 1045270637abSVlad Yasevich inet_register_protosw(&sctp_seqpacket_protosw); 1046270637abSVlad Yasevich inet_register_protosw(&sctp_stream_protosw); 1047270637abSVlad Yasevich 1048270637abSVlad Yasevich return 0; 1049270637abSVlad Yasevich } 1050270637abSVlad Yasevich 1051270637abSVlad Yasevich static void sctp_v4_protosw_exit(void) 1052270637abSVlad Yasevich { 1053270637abSVlad Yasevich inet_unregister_protosw(&sctp_stream_protosw); 1054270637abSVlad Yasevich inet_unregister_protosw(&sctp_seqpacket_protosw); 1055270637abSVlad Yasevich proto_unregister(&sctp_prot); 1056270637abSVlad Yasevich } 1057270637abSVlad Yasevich 1058270637abSVlad Yasevich static int sctp_v4_add_protocol(void) 1059270637abSVlad Yasevich { 1060270637abSVlad Yasevich /* Register notifier for inet address additions/deletions. */ 1061270637abSVlad Yasevich register_inetaddr_notifier(&sctp_inetaddr_notifier); 1062270637abSVlad Yasevich 1063270637abSVlad Yasevich /* Register SCTP with inet layer. */ 1064270637abSVlad Yasevich if (inet_add_protocol(&sctp_protocol, IPPROTO_SCTP) < 0) 1065270637abSVlad Yasevich return -EAGAIN; 1066270637abSVlad Yasevich 1067270637abSVlad Yasevich return 0; 1068270637abSVlad Yasevich } 1069270637abSVlad Yasevich 1070270637abSVlad Yasevich static void sctp_v4_del_protocol(void) 1071270637abSVlad Yasevich { 1072270637abSVlad Yasevich inet_del_protocol(&sctp_protocol, IPPROTO_SCTP); 1073270637abSVlad Yasevich unregister_inetaddr_notifier(&sctp_inetaddr_notifier); 1074270637abSVlad Yasevich } 1075270637abSVlad Yasevich 10761da177e4SLinus Torvalds /* Initialize the universe into something sensible. */ 10771da177e4SLinus Torvalds SCTP_STATIC __init int sctp_init(void) 10781da177e4SLinus Torvalds { 10791da177e4SLinus Torvalds int i; 10801da177e4SLinus Torvalds int status = -EINVAL; 10811da177e4SLinus Torvalds unsigned long goal; 10824d93df0aSNeil Horman unsigned long limit; 10834d93df0aSNeil Horman int max_share; 10841da177e4SLinus Torvalds int order; 10851da177e4SLinus Torvalds 10861da177e4SLinus Torvalds /* SCTP_DEBUG sanity check. */ 10871da177e4SLinus Torvalds if (!sctp_sanity_check()) 10881da177e4SLinus Torvalds goto out; 10891da177e4SLinus Torvalds 1090827bf122SSridhar Samudrala /* Allocate bind_bucket and chunk caches. */ 10911da177e4SLinus Torvalds status = -ENOBUFS; 10921da177e4SLinus Torvalds sctp_bucket_cachep = kmem_cache_create("sctp_bind_bucket", 10931da177e4SLinus Torvalds sizeof(struct sctp_bind_bucket), 10941da177e4SLinus Torvalds 0, SLAB_HWCACHE_ALIGN, 109520c2df83SPaul Mundt NULL); 10961da177e4SLinus Torvalds if (!sctp_bucket_cachep) 1097827bf122SSridhar Samudrala goto out; 10981da177e4SLinus Torvalds 10991da177e4SLinus Torvalds sctp_chunk_cachep = kmem_cache_create("sctp_chunk", 11001da177e4SLinus Torvalds sizeof(struct sctp_chunk), 11011da177e4SLinus Torvalds 0, SLAB_HWCACHE_ALIGN, 110220c2df83SPaul Mundt NULL); 11031da177e4SLinus Torvalds if (!sctp_chunk_cachep) 11041da177e4SLinus Torvalds goto err_chunk_cachep; 11051da177e4SLinus Torvalds 11061da177e4SLinus Torvalds /* Allocate and initialise sctp mibs. */ 11071da177e4SLinus Torvalds status = init_sctp_mibs(); 11081da177e4SLinus Torvalds if (status) 11091da177e4SLinus Torvalds goto err_init_mibs; 11101da177e4SLinus Torvalds 11111da177e4SLinus Torvalds /* Initialize proc fs directory. */ 11121da177e4SLinus Torvalds status = sctp_proc_init(); 11131da177e4SLinus Torvalds if (status) 11141da177e4SLinus Torvalds goto err_init_proc; 11151da177e4SLinus Torvalds 11161da177e4SLinus Torvalds /* Initialize object count debugging. */ 11171da177e4SLinus Torvalds sctp_dbg_objcnt_init(); 11181da177e4SLinus Torvalds 11191da177e4SLinus Torvalds /* 11201da177e4SLinus Torvalds * 14. Suggested SCTP Protocol Parameter Values 11211da177e4SLinus Torvalds */ 11221da177e4SLinus Torvalds /* The following protocol parameters are RECOMMENDED: */ 11231da177e4SLinus Torvalds /* RTO.Initial - 3 seconds */ 11241da177e4SLinus Torvalds sctp_rto_initial = SCTP_RTO_INITIAL; 11251da177e4SLinus Torvalds /* RTO.Min - 1 second */ 11261da177e4SLinus Torvalds sctp_rto_min = SCTP_RTO_MIN; 11271da177e4SLinus Torvalds /* RTO.Max - 60 seconds */ 11281da177e4SLinus Torvalds sctp_rto_max = SCTP_RTO_MAX; 11291da177e4SLinus Torvalds /* RTO.Alpha - 1/8 */ 11301da177e4SLinus Torvalds sctp_rto_alpha = SCTP_RTO_ALPHA; 11311da177e4SLinus Torvalds /* RTO.Beta - 1/4 */ 11321da177e4SLinus Torvalds sctp_rto_beta = SCTP_RTO_BETA; 11331da177e4SLinus Torvalds 11341da177e4SLinus Torvalds /* Valid.Cookie.Life - 60 seconds */ 11353fd091e7SVladislav Yasevich sctp_valid_cookie_life = SCTP_DEFAULT_COOKIE_LIFE; 11361da177e4SLinus Torvalds 11371da177e4SLinus Torvalds /* Whether Cookie Preservative is enabled(1) or not(0) */ 11381da177e4SLinus Torvalds sctp_cookie_preserve_enable = 1; 11391da177e4SLinus Torvalds 11401da177e4SLinus Torvalds /* Max.Burst - 4 */ 114170331571SVlad Yasevich sctp_max_burst = SCTP_DEFAULT_MAX_BURST; 11421da177e4SLinus Torvalds 11431da177e4SLinus Torvalds /* Association.Max.Retrans - 10 attempts 11441da177e4SLinus Torvalds * Path.Max.Retrans - 5 attempts (per destination address) 11451da177e4SLinus Torvalds * Max.Init.Retransmits - 8 attempts 11461da177e4SLinus Torvalds */ 11471da177e4SLinus Torvalds sctp_max_retrans_association = 10; 11481da177e4SLinus Torvalds sctp_max_retrans_path = 5; 11491da177e4SLinus Torvalds sctp_max_retrans_init = 8; 11501da177e4SLinus Torvalds 11514eb701dfSNeil Horman /* Sendbuffer growth - do per-socket accounting */ 11524eb701dfSNeil Horman sctp_sndbuf_policy = 0; 11534eb701dfSNeil Horman 1154049b3ff5SNeil Horman /* Rcvbuffer growth - do per-socket accounting */ 1155049b3ff5SNeil Horman sctp_rcvbuf_policy = 0; 1156049b3ff5SNeil Horman 11571da177e4SLinus Torvalds /* HB.interval - 30 seconds */ 11582f85a429SVlad Yasevich sctp_hb_interval = SCTP_DEFAULT_TIMEOUT_HEARTBEAT; 11592f85a429SVlad Yasevich 11602f85a429SVlad Yasevich /* delayed SACK timeout */ 11612f85a429SVlad Yasevich sctp_sack_timeout = SCTP_DEFAULT_TIMEOUT_SACK; 11621da177e4SLinus Torvalds 11631da177e4SLinus Torvalds /* Implementation specific variables. */ 11641da177e4SLinus Torvalds 11651da177e4SLinus Torvalds /* Initialize default stream count setup information. */ 11661da177e4SLinus Torvalds sctp_max_instreams = SCTP_DEFAULT_INSTREAMS; 11671da177e4SLinus Torvalds sctp_max_outstreams = SCTP_DEFAULT_OUTSTREAMS; 11681da177e4SLinus Torvalds 11691da177e4SLinus Torvalds /* Initialize handle used for association ids. */ 11701da177e4SLinus Torvalds idr_init(&sctp_assocs_id); 11711da177e4SLinus Torvalds 11724d93df0aSNeil Horman /* Set the pressure threshold to be a fraction of global memory that 11734d93df0aSNeil Horman * is up to 1/2 at 256 MB, decreasing toward zero with the amount of 11744d93df0aSNeil Horman * memory, with a floor of 128 pages. 11754d93df0aSNeil Horman * Note this initalizes the data in sctpv6_prot too 11764d93df0aSNeil Horman * Unabashedly stolen from tcp_init 11774d93df0aSNeil Horman */ 11784d93df0aSNeil Horman limit = min(num_physpages, 1UL<<(28-PAGE_SHIFT)) >> (20-PAGE_SHIFT); 11794d93df0aSNeil Horman limit = (limit * (num_physpages >> (20-PAGE_SHIFT))) >> (PAGE_SHIFT-11); 11804d93df0aSNeil Horman limit = max(limit, 128UL); 11814d93df0aSNeil Horman sysctl_sctp_mem[0] = limit / 4 * 3; 11824d93df0aSNeil Horman sysctl_sctp_mem[1] = limit; 11834d93df0aSNeil Horman sysctl_sctp_mem[2] = sysctl_sctp_mem[0] * 2; 11844d93df0aSNeil Horman 11854d93df0aSNeil Horman /* Set per-socket limits to no more than 1/128 the pressure threshold*/ 11864d93df0aSNeil Horman limit = (sysctl_sctp_mem[1]) << (PAGE_SHIFT - 7); 11874d93df0aSNeil Horman max_share = min(4UL*1024*1024, limit); 11884d93df0aSNeil Horman 11894d93df0aSNeil Horman sysctl_sctp_rmem[0] = PAGE_SIZE; /* give each asoc 1 page min */ 11904d93df0aSNeil Horman sysctl_sctp_rmem[1] = (1500 *(sizeof(struct sk_buff) + 1)); 11914d93df0aSNeil Horman sysctl_sctp_rmem[2] = max(sysctl_sctp_rmem[1], max_share); 11924d93df0aSNeil Horman 11933ab224beSHideo Aoki sysctl_sctp_wmem[0] = SK_MEM_QUANTUM; 11944d93df0aSNeil Horman sysctl_sctp_wmem[1] = 16*1024; 11954d93df0aSNeil Horman sysctl_sctp_wmem[2] = max(64*1024, max_share); 11964d93df0aSNeil Horman 11971da177e4SLinus Torvalds /* Size and allocate the association hash table. 11981da177e4SLinus Torvalds * The methodology is similar to that of the tcp hash tables. 11991da177e4SLinus Torvalds */ 12001da177e4SLinus Torvalds if (num_physpages >= (128 * 1024)) 12011da177e4SLinus Torvalds goal = num_physpages >> (22 - PAGE_SHIFT); 12021da177e4SLinus Torvalds else 12031da177e4SLinus Torvalds goal = num_physpages >> (24 - PAGE_SHIFT); 12041da177e4SLinus Torvalds 12051da177e4SLinus Torvalds for (order = 0; (1UL << order) < goal; order++) 12061da177e4SLinus Torvalds ; 12071da177e4SLinus Torvalds 12081da177e4SLinus Torvalds do { 12091da177e4SLinus Torvalds sctp_assoc_hashsize = (1UL << order) * PAGE_SIZE / 12101da177e4SLinus Torvalds sizeof(struct sctp_hashbucket); 12111da177e4SLinus Torvalds if ((sctp_assoc_hashsize > (64 * 1024)) && order > 0) 12121da177e4SLinus Torvalds continue; 12131da177e4SLinus Torvalds sctp_assoc_hashtable = (struct sctp_hashbucket *) 12141da177e4SLinus Torvalds __get_free_pages(GFP_ATOMIC, order); 12151da177e4SLinus Torvalds } while (!sctp_assoc_hashtable && --order > 0); 12161da177e4SLinus Torvalds if (!sctp_assoc_hashtable) { 12171da177e4SLinus Torvalds printk(KERN_ERR "SCTP: Failed association hash alloc.\n"); 12181da177e4SLinus Torvalds status = -ENOMEM; 12191da177e4SLinus Torvalds goto err_ahash_alloc; 12201da177e4SLinus Torvalds } 12211da177e4SLinus Torvalds for (i = 0; i < sctp_assoc_hashsize; i++) { 12221da177e4SLinus Torvalds rwlock_init(&sctp_assoc_hashtable[i].lock); 1223d970dbf8SVlad Yasevich INIT_HLIST_HEAD(&sctp_assoc_hashtable[i].chain); 12241da177e4SLinus Torvalds } 12251da177e4SLinus Torvalds 12261da177e4SLinus Torvalds /* Allocate and initialize the endpoint hash table. */ 12271da177e4SLinus Torvalds sctp_ep_hashsize = 64; 12281da177e4SLinus Torvalds sctp_ep_hashtable = (struct sctp_hashbucket *) 12291da177e4SLinus Torvalds kmalloc(64 * sizeof(struct sctp_hashbucket), GFP_KERNEL); 12301da177e4SLinus Torvalds if (!sctp_ep_hashtable) { 12311da177e4SLinus Torvalds printk(KERN_ERR "SCTP: Failed endpoint_hash alloc.\n"); 12321da177e4SLinus Torvalds status = -ENOMEM; 12331da177e4SLinus Torvalds goto err_ehash_alloc; 12341da177e4SLinus Torvalds } 12351da177e4SLinus Torvalds for (i = 0; i < sctp_ep_hashsize; i++) { 12361da177e4SLinus Torvalds rwlock_init(&sctp_ep_hashtable[i].lock); 1237d970dbf8SVlad Yasevich INIT_HLIST_HEAD(&sctp_ep_hashtable[i].chain); 12381da177e4SLinus Torvalds } 12391da177e4SLinus Torvalds 12401da177e4SLinus Torvalds /* Allocate and initialize the SCTP port hash table. */ 12411da177e4SLinus Torvalds do { 12421da177e4SLinus Torvalds sctp_port_hashsize = (1UL << order) * PAGE_SIZE / 12431da177e4SLinus Torvalds sizeof(struct sctp_bind_hashbucket); 12441da177e4SLinus Torvalds if ((sctp_port_hashsize > (64 * 1024)) && order > 0) 12451da177e4SLinus Torvalds continue; 12461da177e4SLinus Torvalds sctp_port_hashtable = (struct sctp_bind_hashbucket *) 12471da177e4SLinus Torvalds __get_free_pages(GFP_ATOMIC, order); 12481da177e4SLinus Torvalds } while (!sctp_port_hashtable && --order > 0); 12491da177e4SLinus Torvalds if (!sctp_port_hashtable) { 12501da177e4SLinus Torvalds printk(KERN_ERR "SCTP: Failed bind hash alloc."); 12511da177e4SLinus Torvalds status = -ENOMEM; 12521da177e4SLinus Torvalds goto err_bhash_alloc; 12531da177e4SLinus Torvalds } 12541da177e4SLinus Torvalds for (i = 0; i < sctp_port_hashsize; i++) { 12551da177e4SLinus Torvalds spin_lock_init(&sctp_port_hashtable[i].lock); 1256d970dbf8SVlad Yasevich INIT_HLIST_HEAD(&sctp_port_hashtable[i].chain); 12571da177e4SLinus Torvalds } 12581da177e4SLinus Torvalds 12591da177e4SLinus Torvalds printk(KERN_INFO "SCTP: Hash tables configured " 12601da177e4SLinus Torvalds "(established %d bind %d)\n", 12611da177e4SLinus Torvalds sctp_assoc_hashsize, sctp_port_hashsize); 12621da177e4SLinus Torvalds 12631da177e4SLinus Torvalds /* Disable ADDIP by default. */ 12641da177e4SLinus Torvalds sctp_addip_enable = 0; 126573d9c4fdSVlad Yasevich sctp_addip_noauth = 0; 12661da177e4SLinus Torvalds 12671da177e4SLinus Torvalds /* Enable PR-SCTP by default. */ 12681da177e4SLinus Torvalds sctp_prsctp_enable = 1; 12691da177e4SLinus Torvalds 1270a29a5bd4SVlad Yasevich /* Disable AUTH by default. */ 1271a29a5bd4SVlad Yasevich sctp_auth_enable = 0; 1272a29a5bd4SVlad Yasevich 12731da177e4SLinus Torvalds sctp_sysctl_register(); 12741da177e4SLinus Torvalds 12751da177e4SLinus Torvalds INIT_LIST_HEAD(&sctp_address_families); 1276270637abSVlad Yasevich sctp_v4_pf_init(); 1277270637abSVlad Yasevich sctp_v6_pf_init(); 12781da177e4SLinus Torvalds 1279270637abSVlad Yasevich /* Initialize the local address list. */ 1280270637abSVlad Yasevich INIT_LIST_HEAD(&sctp_local_addr_list); 1281270637abSVlad Yasevich spin_lock_init(&sctp_local_addr_lock); 1282270637abSVlad Yasevich sctp_get_local_addr_list(); 1283270637abSVlad Yasevich 1284270637abSVlad Yasevich status = sctp_v4_protosw_init(); 1285270637abSVlad Yasevich 1286827bf122SSridhar Samudrala if (status) 1287270637abSVlad Yasevich goto err_protosw_init; 1288827bf122SSridhar Samudrala 1289270637abSVlad Yasevich status = sctp_v6_protosw_init(); 12901da177e4SLinus Torvalds if (status) 1291270637abSVlad Yasevich goto err_v6_protosw_init; 12921da177e4SLinus Torvalds 12931da177e4SLinus Torvalds /* Initialize the control inode/socket for handling OOTB packets. */ 12941da177e4SLinus Torvalds if ((status = sctp_ctl_sock_init())) { 12951da177e4SLinus Torvalds printk (KERN_ERR 12961da177e4SLinus Torvalds "SCTP: Failed to initialize the SCTP control sock.\n"); 12971da177e4SLinus Torvalds goto err_ctl_sock_init; 12981da177e4SLinus Torvalds } 12991da177e4SLinus Torvalds 1300270637abSVlad Yasevich status = sctp_v4_add_protocol(); 1301270637abSVlad Yasevich if (status) 1302827bf122SSridhar Samudrala goto err_add_protocol; 1303827bf122SSridhar Samudrala 1304827bf122SSridhar Samudrala /* Register SCTP with inet6 layer. */ 1305827bf122SSridhar Samudrala status = sctp_v6_add_protocol(); 1306827bf122SSridhar Samudrala if (status) 1307827bf122SSridhar Samudrala goto err_v6_add_protocol; 1308827bf122SSridhar Samudrala 13091da177e4SLinus Torvalds status = 0; 13101da177e4SLinus Torvalds out: 13111da177e4SLinus Torvalds return status; 1312827bf122SSridhar Samudrala err_v6_add_protocol: 1313270637abSVlad Yasevich sctp_v6_del_protocol(); 1314827bf122SSridhar Samudrala err_add_protocol: 1315270637abSVlad Yasevich sctp_v4_del_protocol(); 13165677242fSDenis V. Lunev inet_ctl_sock_destroy(sctp_ctl_sock); 13171da177e4SLinus Torvalds err_ctl_sock_init: 1318270637abSVlad Yasevich sctp_v6_protosw_exit(); 1319270637abSVlad Yasevich err_v6_protosw_init: 1320270637abSVlad Yasevich sctp_v4_protosw_exit(); 1321270637abSVlad Yasevich err_protosw_init: 1322270637abSVlad Yasevich sctp_free_local_addr_list(); 1323270637abSVlad Yasevich sctp_v4_pf_exit(); 1324270637abSVlad Yasevich sctp_v6_pf_exit(); 13251da177e4SLinus Torvalds sctp_sysctl_unregister(); 132615efbe76SNeil Horman list_del(&sctp_af_inet.list); 13271da177e4SLinus Torvalds free_pages((unsigned long)sctp_port_hashtable, 13281da177e4SLinus Torvalds get_order(sctp_port_hashsize * 13291da177e4SLinus Torvalds sizeof(struct sctp_bind_hashbucket))); 13301da177e4SLinus Torvalds err_bhash_alloc: 13311da177e4SLinus Torvalds kfree(sctp_ep_hashtable); 13321da177e4SLinus Torvalds err_ehash_alloc: 13331da177e4SLinus Torvalds free_pages((unsigned long)sctp_assoc_hashtable, 13341da177e4SLinus Torvalds get_order(sctp_assoc_hashsize * 13351da177e4SLinus Torvalds sizeof(struct sctp_hashbucket))); 13361da177e4SLinus Torvalds err_ahash_alloc: 13371da177e4SLinus Torvalds sctp_dbg_objcnt_exit(); 13381da177e4SLinus Torvalds sctp_proc_exit(); 1339827bf122SSridhar Samudrala err_init_proc: 13401da177e4SLinus Torvalds cleanup_sctp_mibs(); 13411da177e4SLinus Torvalds err_init_mibs: 13421da177e4SLinus Torvalds kmem_cache_destroy(sctp_chunk_cachep); 13431da177e4SLinus Torvalds err_chunk_cachep: 13441da177e4SLinus Torvalds kmem_cache_destroy(sctp_bucket_cachep); 13451da177e4SLinus Torvalds goto out; 13461da177e4SLinus Torvalds } 13471da177e4SLinus Torvalds 13481da177e4SLinus Torvalds /* Exit handler for the SCTP protocol. */ 13491da177e4SLinus Torvalds SCTP_STATIC __exit void sctp_exit(void) 13501da177e4SLinus Torvalds { 13511da177e4SLinus Torvalds /* BUG. This should probably do something useful like clean 13521da177e4SLinus Torvalds * up all the remaining associations and all that memory. 13531da177e4SLinus Torvalds */ 13541da177e4SLinus Torvalds 1355827bf122SSridhar Samudrala /* Unregister with inet6/inet layers. */ 1356827bf122SSridhar Samudrala sctp_v6_del_protocol(); 1357270637abSVlad Yasevich sctp_v4_del_protocol(); 13581da177e4SLinus Torvalds 13591da177e4SLinus Torvalds /* Free the control endpoint. */ 13605677242fSDenis V. Lunev inet_ctl_sock_destroy(sctp_ctl_sock); 13611da177e4SLinus Torvalds 1362270637abSVlad Yasevich /* Free protosw registrations */ 1363270637abSVlad Yasevich sctp_v6_protosw_exit(); 1364270637abSVlad Yasevich sctp_v4_protosw_exit(); 1365270637abSVlad Yasevich 1366270637abSVlad Yasevich /* Free the local address list. */ 1367270637abSVlad Yasevich sctp_free_local_addr_list(); 1368827bf122SSridhar Samudrala 1369827bf122SSridhar Samudrala /* Unregister with socket layer. */ 1370270637abSVlad Yasevich sctp_v6_pf_exit(); 1371270637abSVlad Yasevich sctp_v4_pf_exit(); 1372827bf122SSridhar Samudrala 13731da177e4SLinus Torvalds sctp_sysctl_unregister(); 137415efbe76SNeil Horman list_del(&sctp_af_inet.list); 13751da177e4SLinus Torvalds 13761da177e4SLinus Torvalds free_pages((unsigned long)sctp_assoc_hashtable, 13771da177e4SLinus Torvalds get_order(sctp_assoc_hashsize * 13781da177e4SLinus Torvalds sizeof(struct sctp_hashbucket))); 13791da177e4SLinus Torvalds kfree(sctp_ep_hashtable); 13801da177e4SLinus Torvalds free_pages((unsigned long)sctp_port_hashtable, 13811da177e4SLinus Torvalds get_order(sctp_port_hashsize * 13821da177e4SLinus Torvalds sizeof(struct sctp_bind_hashbucket))); 13831da177e4SLinus Torvalds 13841da177e4SLinus Torvalds sctp_dbg_objcnt_exit(); 13851da177e4SLinus Torvalds sctp_proc_exit(); 13861da177e4SLinus Torvalds cleanup_sctp_mibs(); 13871da177e4SLinus Torvalds 1388827bf122SSridhar Samudrala kmem_cache_destroy(sctp_chunk_cachep); 1389827bf122SSridhar Samudrala kmem_cache_destroy(sctp_bucket_cachep); 13901da177e4SLinus Torvalds } 13911da177e4SLinus Torvalds 13921da177e4SLinus Torvalds module_init(sctp_init); 13931da177e4SLinus Torvalds module_exit(sctp_exit); 13941da177e4SLinus Torvalds 1395bb97d31fSArnaldo Carvalho de Melo /* 1396bb97d31fSArnaldo Carvalho de Melo * __stringify doesn't likes enums, so use IPPROTO_SCTP value (132) directly. 1397bb97d31fSArnaldo Carvalho de Melo */ 1398bb97d31fSArnaldo Carvalho de Melo MODULE_ALIAS("net-pf-" __stringify(PF_INET) "-proto-132"); 1399882a382cSSridhar Samudrala MODULE_ALIAS("net-pf-" __stringify(PF_INET6) "-proto-132"); 14001da177e4SLinus Torvalds MODULE_AUTHOR("Linux Kernel SCTP developers <lksctp-developers@lists.sourceforge.net>"); 14011da177e4SLinus Torvalds MODULE_DESCRIPTION("Support for the SCTP protocol (RFC2960)"); 14021da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 1403