11da177e4SLinus Torvalds /* SCTP kernel reference 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 * 91da177e4SLinus Torvalds * This file is part of the SCTP kernel reference Implementation 101da177e4SLinus Torvalds * 111da177e4SLinus Torvalds * Initialization/cleanup for SCTP protocol support. 121da177e4SLinus Torvalds * 131da177e4SLinus Torvalds * The SCTP reference 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 * 191da177e4SLinus Torvalds * The SCTP reference 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; 671da177e4SLinus Torvalds struct proc_dir_entry *proc_net_sctp; 68ba89966cSEric Dumazet DEFINE_SNMP_STAT(struct sctp_mib, sctp_statistics) __read_mostly; 691da177e4SLinus Torvalds 701da177e4SLinus Torvalds struct idr sctp_assocs_id; 711da177e4SLinus Torvalds DEFINE_SPINLOCK(sctp_assocs_id_lock); 721da177e4SLinus Torvalds 731da177e4SLinus Torvalds /* This is the global socket data structure used for responding to 741da177e4SLinus Torvalds * the Out-of-the-blue (OOTB) packets. A control sock will be created 751da177e4SLinus Torvalds * for this socket at the initialization time. 761da177e4SLinus Torvalds */ 771da177e4SLinus Torvalds static struct socket *sctp_ctl_socket; 781da177e4SLinus Torvalds 791da177e4SLinus Torvalds static struct sctp_pf *sctp_pf_inet6_specific; 801da177e4SLinus Torvalds static struct sctp_pf *sctp_pf_inet_specific; 811da177e4SLinus Torvalds static struct sctp_af *sctp_af_v4_specific; 821da177e4SLinus Torvalds static struct sctp_af *sctp_af_v6_specific; 831da177e4SLinus Torvalds 84e18b890bSChristoph Lameter struct kmem_cache *sctp_chunk_cachep __read_mostly; 85e18b890bSChristoph Lameter struct kmem_cache *sctp_bucket_cachep __read_mostly; 861da177e4SLinus Torvalds 87007e3936SVlad Yasevich int sysctl_sctp_mem[3]; 88007e3936SVlad Yasevich int sysctl_sctp_rmem[3]; 89007e3936SVlad Yasevich int sysctl_sctp_wmem[3]; 904d93df0aSNeil Horman 911da177e4SLinus Torvalds /* Return the address of the control sock. */ 921da177e4SLinus Torvalds struct sock *sctp_get_ctl_sock(void) 931da177e4SLinus Torvalds { 941da177e4SLinus Torvalds return sctp_ctl_socket->sk; 951da177e4SLinus Torvalds } 961da177e4SLinus Torvalds 971da177e4SLinus Torvalds /* Set up the proc fs entry for the SCTP protocol. */ 981da177e4SLinus Torvalds static __init int sctp_proc_init(void) 991da177e4SLinus Torvalds { 1001da177e4SLinus Torvalds if (!proc_net_sctp) { 1011da177e4SLinus Torvalds struct proc_dir_entry *ent; 102457c4cbcSEric W. Biederman ent = proc_mkdir("sctp", init_net.proc_net); 1031da177e4SLinus Torvalds if (ent) { 1041da177e4SLinus Torvalds ent->owner = THIS_MODULE; 1051da177e4SLinus Torvalds proc_net_sctp = ent; 1061da177e4SLinus Torvalds } else 1071da177e4SLinus Torvalds goto out_nomem; 1081da177e4SLinus Torvalds } 1091da177e4SLinus Torvalds 1101da177e4SLinus Torvalds if (sctp_snmp_proc_init()) 1111da177e4SLinus Torvalds goto out_nomem; 1121da177e4SLinus Torvalds if (sctp_eps_proc_init()) 1131da177e4SLinus Torvalds goto out_nomem; 1141da177e4SLinus Torvalds if (sctp_assocs_proc_init()) 1151da177e4SLinus Torvalds goto out_nomem; 1161da177e4SLinus Torvalds 1171da177e4SLinus Torvalds return 0; 1181da177e4SLinus Torvalds 1191da177e4SLinus Torvalds out_nomem: 1201da177e4SLinus Torvalds return -ENOMEM; 1211da177e4SLinus Torvalds } 1221da177e4SLinus Torvalds 1231da177e4SLinus Torvalds /* Clean up the proc fs entry for the SCTP protocol. 1241da177e4SLinus Torvalds * Note: Do not make this __exit as it is used in the init error 1251da177e4SLinus Torvalds * path. 1261da177e4SLinus Torvalds */ 1271da177e4SLinus Torvalds static void sctp_proc_exit(void) 1281da177e4SLinus Torvalds { 1291da177e4SLinus Torvalds sctp_snmp_proc_exit(); 1301da177e4SLinus Torvalds sctp_eps_proc_exit(); 1311da177e4SLinus Torvalds sctp_assocs_proc_exit(); 1321da177e4SLinus Torvalds 1331da177e4SLinus Torvalds if (proc_net_sctp) { 1341da177e4SLinus Torvalds proc_net_sctp = NULL; 135457c4cbcSEric W. Biederman remove_proc_entry("sctp", init_net.proc_net); 1361da177e4SLinus Torvalds } 1371da177e4SLinus Torvalds } 1381da177e4SLinus Torvalds 1391da177e4SLinus Torvalds /* Private helper to extract ipv4 address and stash them in 1401da177e4SLinus Torvalds * the protocol structure. 1411da177e4SLinus Torvalds */ 1421da177e4SLinus Torvalds static void sctp_v4_copy_addrlist(struct list_head *addrlist, 1431da177e4SLinus Torvalds struct net_device *dev) 1441da177e4SLinus Torvalds { 1451da177e4SLinus Torvalds struct in_device *in_dev; 1461da177e4SLinus Torvalds struct in_ifaddr *ifa; 1471da177e4SLinus Torvalds struct sctp_sockaddr_entry *addr; 1481da177e4SLinus Torvalds 1491da177e4SLinus Torvalds rcu_read_lock(); 150e5ed6399SHerbert Xu if ((in_dev = __in_dev_get_rcu(dev)) == NULL) { 1511da177e4SLinus Torvalds rcu_read_unlock(); 1521da177e4SLinus Torvalds return; 1531da177e4SLinus Torvalds } 1541da177e4SLinus Torvalds 1551da177e4SLinus Torvalds for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) { 1561da177e4SLinus Torvalds /* Add the address to the local list. */ 1571da177e4SLinus Torvalds addr = t_new(struct sctp_sockaddr_entry, GFP_ATOMIC); 1581da177e4SLinus Torvalds if (addr) { 1592a6fd78aSAl Viro addr->a.v4.sin_family = AF_INET; 1602a6fd78aSAl Viro addr->a.v4.sin_port = 0; 1612a6fd78aSAl Viro addr->a.v4.sin_addr.s_addr = ifa->ifa_local; 16229303547SVlad Yasevich addr->valid = 1; 16329303547SVlad Yasevich INIT_LIST_HEAD(&addr->list); 16429303547SVlad Yasevich INIT_RCU_HEAD(&addr->rcu); 1651da177e4SLinus Torvalds list_add_tail(&addr->list, addrlist); 1661da177e4SLinus Torvalds } 1671da177e4SLinus Torvalds } 1681da177e4SLinus Torvalds 1691da177e4SLinus Torvalds rcu_read_unlock(); 1701da177e4SLinus Torvalds } 1711da177e4SLinus Torvalds 1721da177e4SLinus Torvalds /* Extract our IP addresses from the system and stash them in the 1731da177e4SLinus Torvalds * protocol structure. 1741da177e4SLinus Torvalds */ 17529c7cf96SSridhar Samudrala static void sctp_get_local_addr_list(void) 1761da177e4SLinus Torvalds { 1771da177e4SLinus Torvalds struct net_device *dev; 1781da177e4SLinus Torvalds struct list_head *pos; 1791da177e4SLinus Torvalds struct sctp_af *af; 1801da177e4SLinus Torvalds 1811da177e4SLinus Torvalds read_lock(&dev_base_lock); 182881d966bSEric W. Biederman for_each_netdev(&init_net, dev) { 1831da177e4SLinus Torvalds __list_for_each(pos, &sctp_address_families) { 1841da177e4SLinus Torvalds af = list_entry(pos, struct sctp_af, list); 1851da177e4SLinus Torvalds af->copy_addrlist(&sctp_local_addr_list, dev); 1861da177e4SLinus Torvalds } 1871da177e4SLinus Torvalds } 1881da177e4SLinus Torvalds read_unlock(&dev_base_lock); 1891da177e4SLinus Torvalds } 1901da177e4SLinus Torvalds 1911da177e4SLinus Torvalds /* Free the existing local addresses. */ 19229c7cf96SSridhar Samudrala static void sctp_free_local_addr_list(void) 1931da177e4SLinus Torvalds { 1941da177e4SLinus Torvalds struct sctp_sockaddr_entry *addr; 1951da177e4SLinus Torvalds struct list_head *pos, *temp; 1961da177e4SLinus Torvalds 1971da177e4SLinus Torvalds list_for_each_safe(pos, temp, &sctp_local_addr_list) { 1981da177e4SLinus Torvalds addr = list_entry(pos, struct sctp_sockaddr_entry, list); 1991da177e4SLinus Torvalds list_del(pos); 2001da177e4SLinus Torvalds kfree(addr); 2011da177e4SLinus Torvalds } 2021da177e4SLinus Torvalds } 2031da177e4SLinus Torvalds 20429303547SVlad Yasevich void sctp_local_addr_free(struct rcu_head *head) 20529303547SVlad Yasevich { 20629303547SVlad Yasevich struct sctp_sockaddr_entry *e = container_of(head, 20729303547SVlad Yasevich struct sctp_sockaddr_entry, rcu); 20829303547SVlad Yasevich kfree(e); 20929303547SVlad Yasevich } 21029303547SVlad Yasevich 2111da177e4SLinus Torvalds /* Copy the local addresses which are valid for 'scope' into 'bp'. */ 2121da177e4SLinus Torvalds int sctp_copy_local_addr_list(struct sctp_bind_addr *bp, sctp_scope_t scope, 213dd0fc66fSAl Viro gfp_t gfp, int copy_flags) 2141da177e4SLinus Torvalds { 2151da177e4SLinus Torvalds struct sctp_sockaddr_entry *addr; 2161da177e4SLinus Torvalds int error = 0; 2171da177e4SLinus Torvalds 21829303547SVlad Yasevich rcu_read_lock(); 21929303547SVlad Yasevich list_for_each_entry_rcu(addr, &sctp_local_addr_list, list) { 22029303547SVlad Yasevich if (!addr->valid) 22129303547SVlad Yasevich continue; 2226244be4eSAl Viro if (sctp_in_scope(&addr->a, scope)) { 2231da177e4SLinus Torvalds /* Now that the address is in scope, check to see if 2241da177e4SLinus Torvalds * the address type is really supported by the local 2251da177e4SLinus Torvalds * sock as well as the remote peer. 2261da177e4SLinus Torvalds */ 2276244be4eSAl Viro if ((((AF_INET == addr->a.sa.sa_family) && 2281da177e4SLinus Torvalds (copy_flags & SCTP_ADDR4_PEERSUPP))) || 2296244be4eSAl Viro (((AF_INET6 == addr->a.sa.sa_family) && 2301da177e4SLinus Torvalds (copy_flags & SCTP_ADDR6_ALLOWED) && 2311da177e4SLinus Torvalds (copy_flags & SCTP_ADDR6_PEERSUPP)))) { 232f57d96b2SVlad Yasevich error = sctp_add_bind_addr(bp, &addr->a, 233f57d96b2SVlad Yasevich SCTP_ADDR_SRC, GFP_ATOMIC); 2341da177e4SLinus Torvalds if (error) 2351da177e4SLinus Torvalds goto end_copy; 2361da177e4SLinus Torvalds } 2371da177e4SLinus Torvalds } 2381da177e4SLinus Torvalds } 2391da177e4SLinus Torvalds 2401da177e4SLinus Torvalds end_copy: 24129303547SVlad Yasevich rcu_read_unlock(); 2421da177e4SLinus Torvalds return error; 2431da177e4SLinus Torvalds } 2441da177e4SLinus Torvalds 2451da177e4SLinus Torvalds /* Initialize a sctp_addr from in incoming skb. */ 2461da177e4SLinus Torvalds static void sctp_v4_from_skb(union sctp_addr *addr, struct sk_buff *skb, 2471da177e4SLinus Torvalds int is_saddr) 2481da177e4SLinus Torvalds { 2491da177e4SLinus Torvalds void *from; 250d55c41b1SAl Viro __be16 *port; 2511da177e4SLinus Torvalds struct sctphdr *sh; 2521da177e4SLinus Torvalds 2531da177e4SLinus Torvalds port = &addr->v4.sin_port; 2541da177e4SLinus Torvalds addr->v4.sin_family = AF_INET; 2551da177e4SLinus Torvalds 2562c0fd387SArnaldo Carvalho de Melo sh = sctp_hdr(skb); 2571da177e4SLinus Torvalds if (is_saddr) { 258d55c41b1SAl Viro *port = sh->source; 259eddc9ec5SArnaldo Carvalho de Melo from = &ip_hdr(skb)->saddr; 2601da177e4SLinus Torvalds } else { 261d55c41b1SAl Viro *port = sh->dest; 262eddc9ec5SArnaldo Carvalho de Melo from = &ip_hdr(skb)->daddr; 2631da177e4SLinus Torvalds } 2641da177e4SLinus Torvalds memcpy(&addr->v4.sin_addr.s_addr, from, sizeof(struct in_addr)); 2651da177e4SLinus Torvalds } 2661da177e4SLinus Torvalds 2671da177e4SLinus Torvalds /* Initialize an sctp_addr from a socket. */ 2681da177e4SLinus Torvalds static void sctp_v4_from_sk(union sctp_addr *addr, struct sock *sk) 2691da177e4SLinus Torvalds { 2701da177e4SLinus Torvalds addr->v4.sin_family = AF_INET; 2717dcdbd95SAl Viro addr->v4.sin_port = 0; 2721da177e4SLinus Torvalds addr->v4.sin_addr.s_addr = inet_sk(sk)->rcv_saddr; 2731da177e4SLinus Torvalds } 2741da177e4SLinus Torvalds 2751da177e4SLinus Torvalds /* Initialize sk->sk_rcv_saddr from sctp_addr. */ 2761da177e4SLinus Torvalds static void sctp_v4_to_sk_saddr(union sctp_addr *addr, struct sock *sk) 2771da177e4SLinus Torvalds { 2781da177e4SLinus Torvalds inet_sk(sk)->rcv_saddr = addr->v4.sin_addr.s_addr; 2791da177e4SLinus Torvalds } 2801da177e4SLinus Torvalds 2811da177e4SLinus Torvalds /* Initialize sk->sk_daddr from sctp_addr. */ 2821da177e4SLinus Torvalds static void sctp_v4_to_sk_daddr(union sctp_addr *addr, struct sock *sk) 2831da177e4SLinus Torvalds { 2841da177e4SLinus Torvalds inet_sk(sk)->daddr = addr->v4.sin_addr.s_addr; 2851da177e4SLinus Torvalds } 2861da177e4SLinus Torvalds 2871da177e4SLinus Torvalds /* Initialize a sctp_addr from an address parameter. */ 2881da177e4SLinus Torvalds static void sctp_v4_from_addr_param(union sctp_addr *addr, 2891da177e4SLinus Torvalds union sctp_addr_param *param, 290dd86d136SAl Viro __be16 port, int iif) 2911da177e4SLinus Torvalds { 2921da177e4SLinus Torvalds addr->v4.sin_family = AF_INET; 2931da177e4SLinus Torvalds addr->v4.sin_port = port; 2941da177e4SLinus Torvalds addr->v4.sin_addr.s_addr = param->v4.addr.s_addr; 2951da177e4SLinus Torvalds } 2961da177e4SLinus Torvalds 2971da177e4SLinus Torvalds /* Initialize an address parameter from a sctp_addr and return the length 2981da177e4SLinus Torvalds * of the address parameter. 2991da177e4SLinus Torvalds */ 3001da177e4SLinus Torvalds static int sctp_v4_to_addr_param(const union sctp_addr *addr, 3011da177e4SLinus Torvalds union sctp_addr_param *param) 3021da177e4SLinus Torvalds { 3031da177e4SLinus Torvalds int length = sizeof(sctp_ipv4addr_param_t); 3041da177e4SLinus Torvalds 3051da177e4SLinus Torvalds param->v4.param_hdr.type = SCTP_PARAM_IPV4_ADDRESS; 306dbc16db1SAl Viro param->v4.param_hdr.length = htons(length); 3071da177e4SLinus Torvalds param->v4.addr.s_addr = addr->v4.sin_addr.s_addr; 3081da177e4SLinus Torvalds 3091da177e4SLinus Torvalds return length; 3101da177e4SLinus Torvalds } 3111da177e4SLinus Torvalds 3121da177e4SLinus Torvalds /* Initialize a sctp_addr from a dst_entry. */ 3131da177e4SLinus Torvalds static void sctp_v4_dst_saddr(union sctp_addr *saddr, struct dst_entry *dst, 314854d43a4SAl Viro __be16 port) 3151da177e4SLinus Torvalds { 3161da177e4SLinus Torvalds struct rtable *rt = (struct rtable *)dst; 3171da177e4SLinus Torvalds saddr->v4.sin_family = AF_INET; 3181da177e4SLinus Torvalds saddr->v4.sin_port = port; 3191da177e4SLinus Torvalds saddr->v4.sin_addr.s_addr = rt->rt_src; 3201da177e4SLinus Torvalds } 3211da177e4SLinus Torvalds 3221da177e4SLinus Torvalds /* Compare two addresses exactly. */ 3231da177e4SLinus Torvalds static int sctp_v4_cmp_addr(const union sctp_addr *addr1, 3241da177e4SLinus Torvalds const union sctp_addr *addr2) 3251da177e4SLinus Torvalds { 3261da177e4SLinus Torvalds if (addr1->sa.sa_family != addr2->sa.sa_family) 3271da177e4SLinus Torvalds return 0; 3281da177e4SLinus Torvalds if (addr1->v4.sin_port != addr2->v4.sin_port) 3291da177e4SLinus Torvalds return 0; 3301da177e4SLinus Torvalds if (addr1->v4.sin_addr.s_addr != addr2->v4.sin_addr.s_addr) 3311da177e4SLinus Torvalds return 0; 3321da177e4SLinus Torvalds 3331da177e4SLinus Torvalds return 1; 3341da177e4SLinus Torvalds } 3351da177e4SLinus Torvalds 3361da177e4SLinus Torvalds /* Initialize addr struct to INADDR_ANY. */ 3376fbfa9f9SAl Viro static void sctp_v4_inaddr_any(union sctp_addr *addr, __be16 port) 3381da177e4SLinus Torvalds { 3391da177e4SLinus Torvalds addr->v4.sin_family = AF_INET; 3401da177e4SLinus Torvalds addr->v4.sin_addr.s_addr = INADDR_ANY; 3411da177e4SLinus Torvalds addr->v4.sin_port = port; 3421da177e4SLinus Torvalds } 3431da177e4SLinus Torvalds 3441da177e4SLinus Torvalds /* Is this a wildcard address? */ 3451da177e4SLinus Torvalds static int sctp_v4_is_any(const union sctp_addr *addr) 3461da177e4SLinus Torvalds { 3471da177e4SLinus Torvalds return INADDR_ANY == addr->v4.sin_addr.s_addr; 3481da177e4SLinus Torvalds } 3491da177e4SLinus Torvalds 3501da177e4SLinus Torvalds /* This function checks if the address is a valid address to be used for 3511da177e4SLinus Torvalds * SCTP binding. 3521da177e4SLinus Torvalds * 3531da177e4SLinus Torvalds * Output: 3541da177e4SLinus Torvalds * Return 0 - If the address is a non-unicast or an illegal address. 3551da177e4SLinus Torvalds * Return 1 - If the address is a unicast. 3561da177e4SLinus Torvalds */ 3575636bef7SVlad Yasevich static int sctp_v4_addr_valid(union sctp_addr *addr, 3585636bef7SVlad Yasevich struct sctp_sock *sp, 3595636bef7SVlad Yasevich const struct sk_buff *skb) 3601da177e4SLinus Torvalds { 3611da177e4SLinus Torvalds /* Is this a non-unicast address or a unusable SCTP address? */ 362b5cb2bbcSJoe Perches if (IS_IPV4_UNUSABLE_ADDRESS(addr->v4.sin_addr.s_addr)) 3631da177e4SLinus Torvalds return 0; 3641da177e4SLinus Torvalds 3655636bef7SVlad Yasevich /* Is this a broadcast address? */ 3665636bef7SVlad Yasevich if (skb && ((struct rtable *)skb->dst)->rt_flags & RTCF_BROADCAST) 3675636bef7SVlad Yasevich return 0; 3685636bef7SVlad Yasevich 3691da177e4SLinus Torvalds return 1; 3701da177e4SLinus Torvalds } 3711da177e4SLinus Torvalds 3721da177e4SLinus Torvalds /* Should this be available for binding? */ 3731da177e4SLinus Torvalds static int sctp_v4_available(union sctp_addr *addr, struct sctp_sock *sp) 3741da177e4SLinus Torvalds { 3751da177e4SLinus Torvalds int ret = inet_addr_type(addr->v4.sin_addr.s_addr); 3761da177e4SLinus Torvalds 3771da177e4SLinus Torvalds 378cdac4e07SNeil Horman if (addr->v4.sin_addr.s_addr != INADDR_ANY && 379cdac4e07SNeil Horman ret != RTN_LOCAL && 380cdac4e07SNeil Horman !sp->inet.freebind && 381cdac4e07SNeil Horman !sysctl_ip_nonlocal_bind) 3821da177e4SLinus Torvalds return 0; 383cdac4e07SNeil Horman 3841da177e4SLinus Torvalds return 1; 3851da177e4SLinus Torvalds } 3861da177e4SLinus Torvalds 3871da177e4SLinus Torvalds /* Checking the loopback, private and other address scopes as defined in 3881da177e4SLinus Torvalds * RFC 1918. The IPv4 scoping is based on the draft for SCTP IPv4 3891da177e4SLinus Torvalds * scoping <draft-stewart-tsvwg-sctp-ipv4-00.txt>. 3901da177e4SLinus Torvalds * 3911da177e4SLinus Torvalds * Level 0 - unusable SCTP addresses 3921da177e4SLinus Torvalds * Level 1 - loopback address 3931da177e4SLinus Torvalds * Level 2 - link-local addresses 3941da177e4SLinus Torvalds * Level 3 - private addresses. 3951da177e4SLinus Torvalds * Level 4 - global addresses 3961da177e4SLinus Torvalds * For INIT and INIT-ACK address list, let L be the level of 3971da177e4SLinus Torvalds * of requested destination address, sender and receiver 3981da177e4SLinus Torvalds * SHOULD include all of its addresses with level greater 3991da177e4SLinus Torvalds * than or equal to L. 4001da177e4SLinus Torvalds */ 4011da177e4SLinus Torvalds static sctp_scope_t sctp_v4_scope(union sctp_addr *addr) 4021da177e4SLinus Torvalds { 4031da177e4SLinus Torvalds sctp_scope_t retval; 4041da177e4SLinus Torvalds 4051da177e4SLinus Torvalds /* Should IPv4 scoping be a sysctl configurable option 4061da177e4SLinus Torvalds * so users can turn it off (default on) for certain 4071da177e4SLinus Torvalds * unconventional networking environments? 4081da177e4SLinus Torvalds */ 4091da177e4SLinus Torvalds 4101da177e4SLinus Torvalds /* Check for unusable SCTP addresses. */ 411b5cb2bbcSJoe Perches if (IS_IPV4_UNUSABLE_ADDRESS(addr->v4.sin_addr.s_addr)) { 4121da177e4SLinus Torvalds retval = SCTP_SCOPE_UNUSABLE; 413b5cb2bbcSJoe Perches } else if (ipv4_is_loopback(addr->v4.sin_addr.s_addr)) { 4141da177e4SLinus Torvalds retval = SCTP_SCOPE_LOOPBACK; 415b5cb2bbcSJoe Perches } else if (ipv4_is_linklocal_169(addr->v4.sin_addr.s_addr)) { 4161da177e4SLinus Torvalds retval = SCTP_SCOPE_LINK; 417b5cb2bbcSJoe Perches } else if (ipv4_is_private_10(addr->v4.sin_addr.s_addr) || 418b5cb2bbcSJoe Perches ipv4_is_private_172(addr->v4.sin_addr.s_addr) || 419b5cb2bbcSJoe Perches ipv4_is_private_192(addr->v4.sin_addr.s_addr)) { 4201da177e4SLinus Torvalds retval = SCTP_SCOPE_PRIVATE; 4211da177e4SLinus Torvalds } else { 4221da177e4SLinus Torvalds retval = SCTP_SCOPE_GLOBAL; 4231da177e4SLinus Torvalds } 4241da177e4SLinus Torvalds 4251da177e4SLinus Torvalds return retval; 4261da177e4SLinus Torvalds } 4271da177e4SLinus Torvalds 4281da177e4SLinus Torvalds /* Returns a valid dst cache entry for the given source and destination ip 4291da177e4SLinus Torvalds * addresses. If an association is passed, trys to get a dst entry with a 4301da177e4SLinus Torvalds * source address that matches an address in the bind address list. 4311da177e4SLinus Torvalds */ 4321da177e4SLinus Torvalds static struct dst_entry *sctp_v4_get_dst(struct sctp_association *asoc, 4331da177e4SLinus Torvalds union sctp_addr *daddr, 4341da177e4SLinus Torvalds union sctp_addr *saddr) 4351da177e4SLinus Torvalds { 4361da177e4SLinus Torvalds struct rtable *rt; 4371da177e4SLinus Torvalds struct flowi fl; 4381da177e4SLinus Torvalds struct sctp_bind_addr *bp; 4391da177e4SLinus Torvalds struct sctp_sockaddr_entry *laddr; 4401da177e4SLinus Torvalds struct dst_entry *dst = NULL; 4411da177e4SLinus Torvalds union sctp_addr dst_saddr; 4421da177e4SLinus Torvalds 4431da177e4SLinus Torvalds memset(&fl, 0x0, sizeof(struct flowi)); 4441da177e4SLinus Torvalds fl.fl4_dst = daddr->v4.sin_addr.s_addr; 4451da177e4SLinus Torvalds fl.proto = IPPROTO_SCTP; 4461da177e4SLinus Torvalds if (asoc) { 4471da177e4SLinus Torvalds fl.fl4_tos = RT_CONN_FLAGS(asoc->base.sk); 4481da177e4SLinus Torvalds fl.oif = asoc->base.sk->sk_bound_dev_if; 4491da177e4SLinus Torvalds } 4501da177e4SLinus Torvalds if (saddr) 4511da177e4SLinus Torvalds fl.fl4_src = saddr->v4.sin_addr.s_addr; 4521da177e4SLinus Torvalds 4531da177e4SLinus Torvalds SCTP_DEBUG_PRINTK("%s: DST:%u.%u.%u.%u, SRC:%u.%u.%u.%u - ", 4541da177e4SLinus Torvalds __FUNCTION__, NIPQUAD(fl.fl4_dst), 4551da177e4SLinus Torvalds NIPQUAD(fl.fl4_src)); 4561da177e4SLinus Torvalds 4571da177e4SLinus Torvalds if (!ip_route_output_key(&rt, &fl)) { 4581da177e4SLinus Torvalds dst = &rt->u.dst; 4591da177e4SLinus Torvalds } 4601da177e4SLinus Torvalds 4611da177e4SLinus Torvalds /* If there is no association or if a source address is passed, no 4621da177e4SLinus Torvalds * more validation is required. 4631da177e4SLinus Torvalds */ 4641da177e4SLinus Torvalds if (!asoc || saddr) 4651da177e4SLinus Torvalds goto out; 4661da177e4SLinus Torvalds 4671da177e4SLinus Torvalds bp = &asoc->base.bind_addr; 4681da177e4SLinus Torvalds 4691da177e4SLinus Torvalds if (dst) { 4701da177e4SLinus Torvalds /* Walk through the bind address list and look for a bind 4711da177e4SLinus Torvalds * address that matches the source address of the returned dst. 4721da177e4SLinus Torvalds */ 473559cf710SVlad Yasevich rcu_read_lock(); 474559cf710SVlad Yasevich list_for_each_entry_rcu(laddr, &bp->address_list, list) { 475f57d96b2SVlad Yasevich if (!laddr->valid || (laddr->state != SCTP_ADDR_SRC)) 476dc022a98SSridhar Samudrala continue; 477854d43a4SAl Viro sctp_v4_dst_saddr(&dst_saddr, dst, htons(bp->port)); 478854d43a4SAl Viro if (sctp_v4_cmp_addr(&dst_saddr, &laddr->a)) 4791da177e4SLinus Torvalds goto out_unlock; 4801da177e4SLinus Torvalds } 481559cf710SVlad Yasevich rcu_read_unlock(); 4821da177e4SLinus Torvalds 4831da177e4SLinus Torvalds /* None of the bound addresses match the source address of the 4841da177e4SLinus Torvalds * dst. So release it. 4851da177e4SLinus Torvalds */ 4861da177e4SLinus Torvalds dst_release(dst); 4871da177e4SLinus Torvalds dst = NULL; 4881da177e4SLinus Torvalds } 4891da177e4SLinus Torvalds 4901da177e4SLinus Torvalds /* Walk through the bind address list and try to get a dst that 4911da177e4SLinus Torvalds * matches a bind address as the source address. 4921da177e4SLinus Torvalds */ 493559cf710SVlad Yasevich rcu_read_lock(); 494559cf710SVlad Yasevich list_for_each_entry_rcu(laddr, &bp->address_list, list) { 495559cf710SVlad Yasevich if (!laddr->valid) 496559cf710SVlad Yasevich continue; 497f57d96b2SVlad Yasevich if ((laddr->state == SCTP_ADDR_SRC) && 4986244be4eSAl Viro (AF_INET == laddr->a.sa.sa_family)) { 4996244be4eSAl Viro fl.fl4_src = laddr->a.v4.sin_addr.s_addr; 5001da177e4SLinus Torvalds if (!ip_route_output_key(&rt, &fl)) { 5011da177e4SLinus Torvalds dst = &rt->u.dst; 5021da177e4SLinus Torvalds goto out_unlock; 5031da177e4SLinus Torvalds } 5041da177e4SLinus Torvalds } 5051da177e4SLinus Torvalds } 5061da177e4SLinus Torvalds 5071da177e4SLinus Torvalds out_unlock: 508559cf710SVlad Yasevich rcu_read_unlock(); 5091da177e4SLinus Torvalds out: 5101da177e4SLinus Torvalds if (dst) 5111da177e4SLinus Torvalds SCTP_DEBUG_PRINTK("rt_dst:%u.%u.%u.%u, rt_src:%u.%u.%u.%u\n", 5121da177e4SLinus Torvalds NIPQUAD(rt->rt_dst), NIPQUAD(rt->rt_src)); 5131da177e4SLinus Torvalds else 5141da177e4SLinus Torvalds SCTP_DEBUG_PRINTK("NO ROUTE\n"); 5151da177e4SLinus Torvalds 5161da177e4SLinus Torvalds return dst; 5171da177e4SLinus Torvalds } 5181da177e4SLinus Torvalds 5191da177e4SLinus Torvalds /* For v4, the source address is cached in the route entry(dst). So no need 5201da177e4SLinus Torvalds * to cache it separately and hence this is an empty routine. 5211da177e4SLinus Torvalds */ 5221da177e4SLinus Torvalds static void sctp_v4_get_saddr(struct sctp_association *asoc, 5231da177e4SLinus Torvalds struct dst_entry *dst, 5241da177e4SLinus Torvalds union sctp_addr *daddr, 5251da177e4SLinus Torvalds union sctp_addr *saddr) 5261da177e4SLinus Torvalds { 5271da177e4SLinus Torvalds struct rtable *rt = (struct rtable *)dst; 5281da177e4SLinus Torvalds 52923ec47a0SVladislav Yasevich if (!asoc) 53023ec47a0SVladislav Yasevich return; 53123ec47a0SVladislav Yasevich 5321da177e4SLinus Torvalds if (rt) { 5331da177e4SLinus Torvalds saddr->v4.sin_family = AF_INET; 534d3f7a54aSAl Viro saddr->v4.sin_port = htons(asoc->base.bind_addr.port); 5351da177e4SLinus Torvalds saddr->v4.sin_addr.s_addr = rt->rt_src; 5361da177e4SLinus Torvalds } 5371da177e4SLinus Torvalds } 5381da177e4SLinus Torvalds 5391da177e4SLinus Torvalds /* What interface did this skb arrive on? */ 5401da177e4SLinus Torvalds static int sctp_v4_skb_iif(const struct sk_buff *skb) 5411da177e4SLinus Torvalds { 5421da177e4SLinus Torvalds return ((struct rtable *)skb->dst)->rt_iif; 5431da177e4SLinus Torvalds } 5441da177e4SLinus Torvalds 5451da177e4SLinus Torvalds /* Was this packet marked by Explicit Congestion Notification? */ 5461da177e4SLinus Torvalds static int sctp_v4_is_ce(const struct sk_buff *skb) 5471da177e4SLinus Torvalds { 548eddc9ec5SArnaldo Carvalho de Melo return INET_ECN_is_ce(ip_hdr(skb)->tos); 5491da177e4SLinus Torvalds } 5501da177e4SLinus Torvalds 5511da177e4SLinus Torvalds /* Create and initialize a new sk for the socket returned by accept(). */ 5521da177e4SLinus Torvalds static struct sock *sctp_v4_create_accept_sk(struct sock *sk, 5531da177e4SLinus Torvalds struct sctp_association *asoc) 5541da177e4SLinus Torvalds { 5551da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 5561da177e4SLinus Torvalds struct inet_sock *newinet; 5576257ff21SPavel Emelyanov struct sock *newsk = sk_alloc(sk->sk_net, PF_INET, GFP_KERNEL, 5586257ff21SPavel Emelyanov sk->sk_prot); 5591da177e4SLinus Torvalds 5601da177e4SLinus Torvalds if (!newsk) 5611da177e4SLinus Torvalds goto out; 5621da177e4SLinus Torvalds 5631da177e4SLinus Torvalds sock_init_data(NULL, newsk); 5641da177e4SLinus Torvalds 5651da177e4SLinus Torvalds newsk->sk_type = SOCK_STREAM; 5661da177e4SLinus Torvalds 5671da177e4SLinus Torvalds newsk->sk_no_check = sk->sk_no_check; 5681da177e4SLinus Torvalds newsk->sk_reuse = sk->sk_reuse; 5691da177e4SLinus Torvalds newsk->sk_shutdown = sk->sk_shutdown; 5701da177e4SLinus Torvalds 5711da177e4SLinus Torvalds newsk->sk_destruct = inet_sock_destruct; 5721da177e4SLinus Torvalds newsk->sk_family = PF_INET; 5731da177e4SLinus Torvalds newsk->sk_protocol = IPPROTO_SCTP; 5741da177e4SLinus Torvalds newsk->sk_backlog_rcv = sk->sk_prot->backlog_rcv; 5751da177e4SLinus Torvalds sock_reset_flag(newsk, SOCK_ZAPPED); 5761da177e4SLinus Torvalds 5771da177e4SLinus Torvalds newinet = inet_sk(newsk); 5781da177e4SLinus Torvalds 5791da177e4SLinus Torvalds /* Initialize sk's sport, dport, rcv_saddr and daddr for 5801da177e4SLinus Torvalds * getsockname() and getpeername() 5811da177e4SLinus Torvalds */ 5821da177e4SLinus Torvalds newinet->sport = inet->sport; 5831da177e4SLinus Torvalds newinet->saddr = inet->saddr; 5841da177e4SLinus Torvalds newinet->rcv_saddr = inet->rcv_saddr; 5851da177e4SLinus Torvalds newinet->dport = htons(asoc->peer.port); 5861da177e4SLinus Torvalds newinet->daddr = asoc->peer.primary_addr.v4.sin_addr.s_addr; 5871da177e4SLinus Torvalds newinet->pmtudisc = inet->pmtudisc; 5884f444308SVlad Yasevich newinet->id = asoc->next_tsn ^ jiffies; 5891da177e4SLinus Torvalds 5901da177e4SLinus Torvalds newinet->uc_ttl = -1; 5911da177e4SLinus Torvalds newinet->mc_loop = 1; 5921da177e4SLinus Torvalds newinet->mc_ttl = 1; 5931da177e4SLinus Torvalds newinet->mc_index = 0; 5941da177e4SLinus Torvalds newinet->mc_list = NULL; 5951da177e4SLinus Torvalds 596e6848976SArnaldo Carvalho de Melo sk_refcnt_debug_inc(newsk); 5971da177e4SLinus Torvalds 5981da177e4SLinus Torvalds if (newsk->sk_prot->init(newsk)) { 5991da177e4SLinus Torvalds sk_common_release(newsk); 6001da177e4SLinus Torvalds newsk = NULL; 6011da177e4SLinus Torvalds } 6021da177e4SLinus Torvalds 6031da177e4SLinus Torvalds out: 6041da177e4SLinus Torvalds return newsk; 6051da177e4SLinus Torvalds } 6061da177e4SLinus Torvalds 6071da177e4SLinus Torvalds /* Map address, empty for v4 family */ 6081da177e4SLinus Torvalds static void sctp_v4_addr_v4map(struct sctp_sock *sp, union sctp_addr *addr) 6091da177e4SLinus Torvalds { 6101da177e4SLinus Torvalds /* Empty */ 6111da177e4SLinus Torvalds } 6121da177e4SLinus Torvalds 6131da177e4SLinus Torvalds /* Dump the v4 addr to the seq file. */ 6141da177e4SLinus Torvalds static void sctp_v4_seq_dump_addr(struct seq_file *seq, union sctp_addr *addr) 6151da177e4SLinus Torvalds { 6161da177e4SLinus Torvalds seq_printf(seq, "%d.%d.%d.%d ", NIPQUAD(addr->v4.sin_addr)); 6171da177e4SLinus Torvalds } 6181da177e4SLinus Torvalds 61929303547SVlad Yasevich /* Event handler for inet address addition/deletion events. 62029303547SVlad Yasevich * The sctp_local_addr_list needs to be protocted by a spin lock since 62129303547SVlad Yasevich * multiple notifiers (say IPv4 and IPv6) may be running at the same 62229303547SVlad Yasevich * time and thus corrupt the list. 62329303547SVlad Yasevich * The reader side is protected with RCU. 62429303547SVlad Yasevich */ 62524123186SAdrian Bunk static int sctp_inetaddr_event(struct notifier_block *this, unsigned long ev, 6261da177e4SLinus Torvalds void *ptr) 6271da177e4SLinus Torvalds { 62829c7cf96SSridhar Samudrala struct in_ifaddr *ifa = (struct in_ifaddr *)ptr; 62929303547SVlad Yasevich struct sctp_sockaddr_entry *addr = NULL; 63029303547SVlad Yasevich struct sctp_sockaddr_entry *temp; 6311da177e4SLinus Torvalds 63229c7cf96SSridhar Samudrala switch (ev) { 63329c7cf96SSridhar Samudrala case NETDEV_UP: 63429c7cf96SSridhar Samudrala addr = kmalloc(sizeof(struct sctp_sockaddr_entry), GFP_ATOMIC); 63529c7cf96SSridhar Samudrala if (addr) { 63629c7cf96SSridhar Samudrala addr->a.v4.sin_family = AF_INET; 63729c7cf96SSridhar Samudrala addr->a.v4.sin_port = 0; 63829c7cf96SSridhar Samudrala addr->a.v4.sin_addr.s_addr = ifa->ifa_local; 63929303547SVlad Yasevich addr->valid = 1; 64029303547SVlad Yasevich spin_lock_bh(&sctp_local_addr_lock); 64129303547SVlad Yasevich list_add_tail_rcu(&addr->list, &sctp_local_addr_list); 64229303547SVlad Yasevich spin_unlock_bh(&sctp_local_addr_lock); 64329c7cf96SSridhar Samudrala } 64429c7cf96SSridhar Samudrala break; 64529c7cf96SSridhar Samudrala case NETDEV_DOWN: 64629303547SVlad Yasevich spin_lock_bh(&sctp_local_addr_lock); 64729303547SVlad Yasevich list_for_each_entry_safe(addr, temp, 64829303547SVlad Yasevich &sctp_local_addr_list, list) { 64929c7cf96SSridhar Samudrala if (addr->a.v4.sin_addr.s_addr == ifa->ifa_local) { 65029303547SVlad Yasevich addr->valid = 0; 65129303547SVlad Yasevich list_del_rcu(&addr->list); 65229c7cf96SSridhar Samudrala break; 65329c7cf96SSridhar Samudrala } 65429c7cf96SSridhar Samudrala } 65529303547SVlad Yasevich spin_unlock_bh(&sctp_local_addr_lock); 65629303547SVlad Yasevich if (addr && !addr->valid) 65729303547SVlad Yasevich call_rcu(&addr->rcu, sctp_local_addr_free); 65829c7cf96SSridhar Samudrala break; 65929c7cf96SSridhar Samudrala } 6601da177e4SLinus Torvalds 6611da177e4SLinus Torvalds return NOTIFY_DONE; 6621da177e4SLinus Torvalds } 6631da177e4SLinus Torvalds 6641da177e4SLinus Torvalds /* 6651da177e4SLinus Torvalds * Initialize the control inode/socket with a control endpoint data 6661da177e4SLinus Torvalds * structure. This endpoint is reserved exclusively for the OOTB processing. 6671da177e4SLinus Torvalds */ 6681da177e4SLinus Torvalds static int sctp_ctl_sock_init(void) 6691da177e4SLinus Torvalds { 6701da177e4SLinus Torvalds int err; 6711da177e4SLinus Torvalds sa_family_t family; 6721da177e4SLinus Torvalds 6731da177e4SLinus Torvalds if (sctp_get_pf_specific(PF_INET6)) 6741da177e4SLinus Torvalds family = PF_INET6; 6751da177e4SLinus Torvalds else 6761da177e4SLinus Torvalds family = PF_INET; 6771da177e4SLinus Torvalds 6781da177e4SLinus Torvalds err = sock_create_kern(family, SOCK_SEQPACKET, IPPROTO_SCTP, 6791da177e4SLinus Torvalds &sctp_ctl_socket); 6801da177e4SLinus Torvalds if (err < 0) { 6811da177e4SLinus Torvalds printk(KERN_ERR 6821da177e4SLinus Torvalds "SCTP: Failed to create the SCTP control socket.\n"); 6831da177e4SLinus Torvalds return err; 6841da177e4SLinus Torvalds } 6851da177e4SLinus Torvalds sctp_ctl_socket->sk->sk_allocation = GFP_ATOMIC; 6861da177e4SLinus Torvalds inet_sk(sctp_ctl_socket->sk)->uc_ttl = -1; 6871da177e4SLinus Torvalds 6881da177e4SLinus Torvalds return 0; 6891da177e4SLinus Torvalds } 6901da177e4SLinus Torvalds 6911da177e4SLinus Torvalds /* Register address family specific functions. */ 6921da177e4SLinus Torvalds int sctp_register_af(struct sctp_af *af) 6931da177e4SLinus Torvalds { 6941da177e4SLinus Torvalds switch (af->sa_family) { 6951da177e4SLinus Torvalds case AF_INET: 6961da177e4SLinus Torvalds if (sctp_af_v4_specific) 6971da177e4SLinus Torvalds return 0; 6981da177e4SLinus Torvalds sctp_af_v4_specific = af; 6991da177e4SLinus Torvalds break; 7001da177e4SLinus Torvalds case AF_INET6: 7011da177e4SLinus Torvalds if (sctp_af_v6_specific) 7021da177e4SLinus Torvalds return 0; 7031da177e4SLinus Torvalds sctp_af_v6_specific = af; 7041da177e4SLinus Torvalds break; 7051da177e4SLinus Torvalds default: 7061da177e4SLinus Torvalds return 0; 7071da177e4SLinus Torvalds } 7081da177e4SLinus Torvalds 7091da177e4SLinus Torvalds INIT_LIST_HEAD(&af->list); 7101da177e4SLinus Torvalds list_add_tail(&af->list, &sctp_address_families); 7111da177e4SLinus Torvalds return 1; 7121da177e4SLinus Torvalds } 7131da177e4SLinus Torvalds 7141da177e4SLinus Torvalds /* Get the table of functions for manipulating a particular address 7151da177e4SLinus Torvalds * family. 7161da177e4SLinus Torvalds */ 7171da177e4SLinus Torvalds struct sctp_af *sctp_get_af_specific(sa_family_t family) 7181da177e4SLinus Torvalds { 7191da177e4SLinus Torvalds switch (family) { 7201da177e4SLinus Torvalds case AF_INET: 7211da177e4SLinus Torvalds return sctp_af_v4_specific; 7221da177e4SLinus Torvalds case AF_INET6: 7231da177e4SLinus Torvalds return sctp_af_v6_specific; 7241da177e4SLinus Torvalds default: 7251da177e4SLinus Torvalds return NULL; 7261da177e4SLinus Torvalds } 7271da177e4SLinus Torvalds } 7281da177e4SLinus Torvalds 7291da177e4SLinus Torvalds /* Common code to initialize a AF_INET msg_name. */ 7301da177e4SLinus Torvalds static void sctp_inet_msgname(char *msgname, int *addr_len) 7311da177e4SLinus Torvalds { 7321da177e4SLinus Torvalds struct sockaddr_in *sin; 7331da177e4SLinus Torvalds 7341da177e4SLinus Torvalds sin = (struct sockaddr_in *)msgname; 7351da177e4SLinus Torvalds *addr_len = sizeof(struct sockaddr_in); 7361da177e4SLinus Torvalds sin->sin_family = AF_INET; 7371da177e4SLinus Torvalds memset(sin->sin_zero, 0, sizeof(sin->sin_zero)); 7381da177e4SLinus Torvalds } 7391da177e4SLinus Torvalds 7401da177e4SLinus Torvalds /* Copy the primary address of the peer primary address as the msg_name. */ 7411da177e4SLinus Torvalds static void sctp_inet_event_msgname(struct sctp_ulpevent *event, char *msgname, 7421da177e4SLinus Torvalds int *addr_len) 7431da177e4SLinus Torvalds { 7441da177e4SLinus Torvalds struct sockaddr_in *sin, *sinfrom; 7451da177e4SLinus Torvalds 7461da177e4SLinus Torvalds if (msgname) { 7471da177e4SLinus Torvalds struct sctp_association *asoc; 7481da177e4SLinus Torvalds 7491da177e4SLinus Torvalds asoc = event->asoc; 7501da177e4SLinus Torvalds sctp_inet_msgname(msgname, addr_len); 7511da177e4SLinus Torvalds sin = (struct sockaddr_in *)msgname; 7521da177e4SLinus Torvalds sinfrom = &asoc->peer.primary_addr.v4; 7531da177e4SLinus Torvalds sin->sin_port = htons(asoc->peer.port); 7541da177e4SLinus Torvalds sin->sin_addr.s_addr = sinfrom->sin_addr.s_addr; 7551da177e4SLinus Torvalds } 7561da177e4SLinus Torvalds } 7571da177e4SLinus Torvalds 7581da177e4SLinus Torvalds /* Initialize and copy out a msgname from an inbound skb. */ 7591da177e4SLinus Torvalds static void sctp_inet_skb_msgname(struct sk_buff *skb, char *msgname, int *len) 7601da177e4SLinus Torvalds { 7611da177e4SLinus Torvalds if (msgname) { 7622c0fd387SArnaldo Carvalho de Melo struct sctphdr *sh = sctp_hdr(skb); 7632c0fd387SArnaldo Carvalho de Melo struct sockaddr_in *sin = (struct sockaddr_in *)msgname; 7642c0fd387SArnaldo Carvalho de Melo 7651da177e4SLinus Torvalds sctp_inet_msgname(msgname, len); 7661da177e4SLinus Torvalds sin->sin_port = sh->source; 767eddc9ec5SArnaldo Carvalho de Melo sin->sin_addr.s_addr = ip_hdr(skb)->saddr; 7681da177e4SLinus Torvalds } 7691da177e4SLinus Torvalds } 7701da177e4SLinus Torvalds 7711da177e4SLinus Torvalds /* Do we support this AF? */ 7721da177e4SLinus Torvalds static int sctp_inet_af_supported(sa_family_t family, struct sctp_sock *sp) 7731da177e4SLinus Torvalds { 7741da177e4SLinus Torvalds /* PF_INET only supports AF_INET addresses. */ 7751da177e4SLinus Torvalds return (AF_INET == family); 7761da177e4SLinus Torvalds } 7771da177e4SLinus Torvalds 7781da177e4SLinus Torvalds /* Address matching with wildcards allowed. */ 7791da177e4SLinus Torvalds static int sctp_inet_cmp_addr(const union sctp_addr *addr1, 7801da177e4SLinus Torvalds const union sctp_addr *addr2, 7811da177e4SLinus Torvalds struct sctp_sock *opt) 7821da177e4SLinus Torvalds { 7831da177e4SLinus Torvalds /* PF_INET only supports AF_INET addresses. */ 7841da177e4SLinus Torvalds if (addr1->sa.sa_family != addr2->sa.sa_family) 7851da177e4SLinus Torvalds return 0; 7861da177e4SLinus Torvalds if (INADDR_ANY == addr1->v4.sin_addr.s_addr || 7871da177e4SLinus Torvalds INADDR_ANY == addr2->v4.sin_addr.s_addr) 7881da177e4SLinus Torvalds return 1; 7891da177e4SLinus Torvalds if (addr1->v4.sin_addr.s_addr == addr2->v4.sin_addr.s_addr) 7901da177e4SLinus Torvalds return 1; 7911da177e4SLinus Torvalds 7921da177e4SLinus Torvalds return 0; 7931da177e4SLinus Torvalds } 7941da177e4SLinus Torvalds 7951da177e4SLinus Torvalds /* Verify that provided sockaddr looks bindable. Common verification has 7961da177e4SLinus Torvalds * already been taken care of. 7971da177e4SLinus Torvalds */ 7981da177e4SLinus Torvalds static int sctp_inet_bind_verify(struct sctp_sock *opt, union sctp_addr *addr) 7991da177e4SLinus Torvalds { 8001da177e4SLinus Torvalds return sctp_v4_available(addr, opt); 8011da177e4SLinus Torvalds } 8021da177e4SLinus Torvalds 8031da177e4SLinus Torvalds /* Verify that sockaddr looks sendable. Common verification has already 8041da177e4SLinus Torvalds * been taken care of. 8051da177e4SLinus Torvalds */ 8061da177e4SLinus Torvalds static int sctp_inet_send_verify(struct sctp_sock *opt, union sctp_addr *addr) 8071da177e4SLinus Torvalds { 8081da177e4SLinus Torvalds return 1; 8091da177e4SLinus Torvalds } 8101da177e4SLinus Torvalds 8111da177e4SLinus Torvalds /* Fill in Supported Address Type information for INIT and INIT-ACK 8121da177e4SLinus Torvalds * chunks. Returns number of addresses supported. 8131da177e4SLinus Torvalds */ 8141da177e4SLinus Torvalds static int sctp_inet_supported_addrs(const struct sctp_sock *opt, 8153dbe8656SAl Viro __be16 *types) 8161da177e4SLinus Torvalds { 8171da177e4SLinus Torvalds types[0] = SCTP_PARAM_IPV4_ADDRESS; 8181da177e4SLinus Torvalds return 1; 8191da177e4SLinus Torvalds } 8201da177e4SLinus Torvalds 8211da177e4SLinus Torvalds /* Wrapper routine that calls the ip transmit routine. */ 8221da177e4SLinus Torvalds static inline int sctp_v4_xmit(struct sk_buff *skb, 8231da177e4SLinus Torvalds struct sctp_transport *transport, int ipfragok) 8241da177e4SLinus Torvalds { 8251da177e4SLinus Torvalds SCTP_DEBUG_PRINTK("%s: skb:%p, len:%d, " 8261da177e4SLinus Torvalds "src:%u.%u.%u.%u, dst:%u.%u.%u.%u\n", 8271da177e4SLinus Torvalds __FUNCTION__, skb, skb->len, 8281da177e4SLinus Torvalds NIPQUAD(((struct rtable *)skb->dst)->rt_src), 8291da177e4SLinus Torvalds NIPQUAD(((struct rtable *)skb->dst)->rt_dst)); 8301da177e4SLinus Torvalds 8311da177e4SLinus Torvalds SCTP_INC_STATS(SCTP_MIB_OUTSCTPPACKS); 832e89862f4SDavid S. Miller return ip_queue_xmit(skb, ipfragok); 8331da177e4SLinus Torvalds } 8341da177e4SLinus Torvalds 8351da177e4SLinus Torvalds static struct sctp_af sctp_ipv4_specific; 8361da177e4SLinus Torvalds 8371da177e4SLinus Torvalds static struct sctp_pf sctp_pf_inet = { 8381da177e4SLinus Torvalds .event_msgname = sctp_inet_event_msgname, 8391da177e4SLinus Torvalds .skb_msgname = sctp_inet_skb_msgname, 8401da177e4SLinus Torvalds .af_supported = sctp_inet_af_supported, 8411da177e4SLinus Torvalds .cmp_addr = sctp_inet_cmp_addr, 8421da177e4SLinus Torvalds .bind_verify = sctp_inet_bind_verify, 8431da177e4SLinus Torvalds .send_verify = sctp_inet_send_verify, 8441da177e4SLinus Torvalds .supported_addrs = sctp_inet_supported_addrs, 8451da177e4SLinus Torvalds .create_accept_sk = sctp_v4_create_accept_sk, 8461da177e4SLinus Torvalds .addr_v4map = sctp_v4_addr_v4map, 8471da177e4SLinus Torvalds .af = &sctp_ipv4_specific, 8481da177e4SLinus Torvalds }; 8491da177e4SLinus Torvalds 8501da177e4SLinus Torvalds /* Notifier for inetaddr addition/deletion events. */ 8511da177e4SLinus Torvalds static struct notifier_block sctp_inetaddr_notifier = { 8521da177e4SLinus Torvalds .notifier_call = sctp_inetaddr_event, 8531da177e4SLinus Torvalds }; 8541da177e4SLinus Torvalds 8551da177e4SLinus Torvalds /* Socket operations. */ 85690ddc4f0SEric Dumazet static const struct proto_ops inet_seqpacket_ops = { 8571da177e4SLinus Torvalds .family = PF_INET, 8581da177e4SLinus Torvalds .owner = THIS_MODULE, 8591da177e4SLinus Torvalds .release = inet_release, /* Needs to be wrapped... */ 8601da177e4SLinus Torvalds .bind = inet_bind, 8611da177e4SLinus Torvalds .connect = inet_dgram_connect, 8621da177e4SLinus Torvalds .socketpair = sock_no_socketpair, 8631da177e4SLinus Torvalds .accept = inet_accept, 8641da177e4SLinus Torvalds .getname = inet_getname, /* Semantics are different. */ 8651da177e4SLinus Torvalds .poll = sctp_poll, 8661da177e4SLinus Torvalds .ioctl = inet_ioctl, 8671da177e4SLinus Torvalds .listen = sctp_inet_listen, 8681da177e4SLinus Torvalds .shutdown = inet_shutdown, /* Looks harmless. */ 869543d9cfeSArnaldo Carvalho de Melo .setsockopt = sock_common_setsockopt, /* IP_SOL IP_OPTION is a problem */ 8701da177e4SLinus Torvalds .getsockopt = sock_common_getsockopt, 8711da177e4SLinus Torvalds .sendmsg = inet_sendmsg, 8721da177e4SLinus Torvalds .recvmsg = sock_common_recvmsg, 8731da177e4SLinus Torvalds .mmap = sock_no_mmap, 8741da177e4SLinus Torvalds .sendpage = sock_no_sendpage, 875543d9cfeSArnaldo Carvalho de Melo #ifdef CONFIG_COMPAT 876543d9cfeSArnaldo Carvalho de Melo .compat_setsockopt = compat_sock_common_setsockopt, 877543d9cfeSArnaldo Carvalho de Melo .compat_getsockopt = compat_sock_common_getsockopt, 878543d9cfeSArnaldo Carvalho de Melo #endif 8791da177e4SLinus Torvalds }; 8801da177e4SLinus Torvalds 8811da177e4SLinus Torvalds /* Registration with AF_INET family. */ 8821da177e4SLinus Torvalds static struct inet_protosw sctp_seqpacket_protosw = { 8831da177e4SLinus Torvalds .type = SOCK_SEQPACKET, 8841da177e4SLinus Torvalds .protocol = IPPROTO_SCTP, 8851da177e4SLinus Torvalds .prot = &sctp_prot, 8861da177e4SLinus Torvalds .ops = &inet_seqpacket_ops, 8871da177e4SLinus Torvalds .capability = -1, 8881da177e4SLinus Torvalds .no_check = 0, 8891da177e4SLinus Torvalds .flags = SCTP_PROTOSW_FLAG 8901da177e4SLinus Torvalds }; 8911da177e4SLinus Torvalds static struct inet_protosw sctp_stream_protosw = { 8921da177e4SLinus Torvalds .type = SOCK_STREAM, 8931da177e4SLinus Torvalds .protocol = IPPROTO_SCTP, 8941da177e4SLinus Torvalds .prot = &sctp_prot, 8951da177e4SLinus Torvalds .ops = &inet_seqpacket_ops, 8961da177e4SLinus Torvalds .capability = -1, 8971da177e4SLinus Torvalds .no_check = 0, 8981da177e4SLinus Torvalds .flags = SCTP_PROTOSW_FLAG 8991da177e4SLinus Torvalds }; 9001da177e4SLinus Torvalds 9011da177e4SLinus Torvalds /* Register with IP layer. */ 9021da177e4SLinus Torvalds static struct net_protocol sctp_protocol = { 9031da177e4SLinus Torvalds .handler = sctp_rcv, 9041da177e4SLinus Torvalds .err_handler = sctp_v4_err, 9051da177e4SLinus Torvalds .no_policy = 1, 9061da177e4SLinus Torvalds }; 9071da177e4SLinus Torvalds 9081da177e4SLinus Torvalds /* IPv4 address related functions. */ 9091da177e4SLinus Torvalds static struct sctp_af sctp_ipv4_specific = { 910543d9cfeSArnaldo Carvalho de Melo .sa_family = AF_INET, 9111da177e4SLinus Torvalds .sctp_xmit = sctp_v4_xmit, 9121da177e4SLinus Torvalds .setsockopt = ip_setsockopt, 9131da177e4SLinus Torvalds .getsockopt = ip_getsockopt, 9141da177e4SLinus Torvalds .get_dst = sctp_v4_get_dst, 9151da177e4SLinus Torvalds .get_saddr = sctp_v4_get_saddr, 9161da177e4SLinus Torvalds .copy_addrlist = sctp_v4_copy_addrlist, 9171da177e4SLinus Torvalds .from_skb = sctp_v4_from_skb, 9181da177e4SLinus Torvalds .from_sk = sctp_v4_from_sk, 9191da177e4SLinus Torvalds .to_sk_saddr = sctp_v4_to_sk_saddr, 9201da177e4SLinus Torvalds .to_sk_daddr = sctp_v4_to_sk_daddr, 9211da177e4SLinus Torvalds .from_addr_param = sctp_v4_from_addr_param, 9221da177e4SLinus Torvalds .to_addr_param = sctp_v4_to_addr_param, 9231da177e4SLinus Torvalds .dst_saddr = sctp_v4_dst_saddr, 9241da177e4SLinus Torvalds .cmp_addr = sctp_v4_cmp_addr, 9251da177e4SLinus Torvalds .addr_valid = sctp_v4_addr_valid, 9261da177e4SLinus Torvalds .inaddr_any = sctp_v4_inaddr_any, 9271da177e4SLinus Torvalds .is_any = sctp_v4_is_any, 9281da177e4SLinus Torvalds .available = sctp_v4_available, 9291da177e4SLinus Torvalds .scope = sctp_v4_scope, 9301da177e4SLinus Torvalds .skb_iif = sctp_v4_skb_iif, 9311da177e4SLinus Torvalds .is_ce = sctp_v4_is_ce, 9321da177e4SLinus Torvalds .seq_dump_addr = sctp_v4_seq_dump_addr, 9331da177e4SLinus Torvalds .net_header_len = sizeof(struct iphdr), 9341da177e4SLinus Torvalds .sockaddr_len = sizeof(struct sockaddr_in), 935543d9cfeSArnaldo Carvalho de Melo #ifdef CONFIG_COMPAT 936543d9cfeSArnaldo Carvalho de Melo .compat_setsockopt = compat_ip_setsockopt, 937543d9cfeSArnaldo Carvalho de Melo .compat_getsockopt = compat_ip_getsockopt, 938543d9cfeSArnaldo Carvalho de Melo #endif 9391da177e4SLinus Torvalds }; 9401da177e4SLinus Torvalds 9411da177e4SLinus Torvalds struct sctp_pf *sctp_get_pf_specific(sa_family_t family) { 9421da177e4SLinus Torvalds 9431da177e4SLinus Torvalds switch (family) { 9441da177e4SLinus Torvalds case PF_INET: 9451da177e4SLinus Torvalds return sctp_pf_inet_specific; 9461da177e4SLinus Torvalds case PF_INET6: 9471da177e4SLinus Torvalds return sctp_pf_inet6_specific; 9481da177e4SLinus Torvalds default: 9491da177e4SLinus Torvalds return NULL; 9501da177e4SLinus Torvalds } 9511da177e4SLinus Torvalds } 9521da177e4SLinus Torvalds 9531da177e4SLinus Torvalds /* Register the PF specific function table. */ 9541da177e4SLinus Torvalds int sctp_register_pf(struct sctp_pf *pf, sa_family_t family) 9551da177e4SLinus Torvalds { 9561da177e4SLinus Torvalds switch (family) { 9571da177e4SLinus Torvalds case PF_INET: 9581da177e4SLinus Torvalds if (sctp_pf_inet_specific) 9591da177e4SLinus Torvalds return 0; 9601da177e4SLinus Torvalds sctp_pf_inet_specific = pf; 9611da177e4SLinus Torvalds break; 9621da177e4SLinus Torvalds case PF_INET6: 9631da177e4SLinus Torvalds if (sctp_pf_inet6_specific) 9641da177e4SLinus Torvalds return 0; 9651da177e4SLinus Torvalds sctp_pf_inet6_specific = pf; 9661da177e4SLinus Torvalds break; 9671da177e4SLinus Torvalds default: 9681da177e4SLinus Torvalds return 0; 9691da177e4SLinus Torvalds } 9701da177e4SLinus Torvalds return 1; 9711da177e4SLinus Torvalds } 9721da177e4SLinus Torvalds 9731da177e4SLinus Torvalds static int __init init_sctp_mibs(void) 9741da177e4SLinus Torvalds { 9751da177e4SLinus Torvalds sctp_statistics[0] = alloc_percpu(struct sctp_mib); 9761da177e4SLinus Torvalds if (!sctp_statistics[0]) 9771da177e4SLinus Torvalds return -ENOMEM; 9781da177e4SLinus Torvalds sctp_statistics[1] = alloc_percpu(struct sctp_mib); 9791da177e4SLinus Torvalds if (!sctp_statistics[1]) { 9801da177e4SLinus Torvalds free_percpu(sctp_statistics[0]); 9811da177e4SLinus Torvalds return -ENOMEM; 9821da177e4SLinus Torvalds } 9831da177e4SLinus Torvalds return 0; 9841da177e4SLinus Torvalds 9851da177e4SLinus Torvalds } 9861da177e4SLinus Torvalds 9871da177e4SLinus Torvalds static void cleanup_sctp_mibs(void) 9881da177e4SLinus Torvalds { 9891da177e4SLinus Torvalds free_percpu(sctp_statistics[0]); 9901da177e4SLinus Torvalds free_percpu(sctp_statistics[1]); 9911da177e4SLinus Torvalds } 9921da177e4SLinus Torvalds 9931da177e4SLinus Torvalds /* Initialize the universe into something sensible. */ 9941da177e4SLinus Torvalds SCTP_STATIC __init int sctp_init(void) 9951da177e4SLinus Torvalds { 9961da177e4SLinus Torvalds int i; 9971da177e4SLinus Torvalds int status = -EINVAL; 9981da177e4SLinus Torvalds unsigned long goal; 9994d93df0aSNeil Horman unsigned long limit; 10004d93df0aSNeil Horman int max_share; 10011da177e4SLinus Torvalds int order; 10021da177e4SLinus Torvalds 10031da177e4SLinus Torvalds /* SCTP_DEBUG sanity check. */ 10041da177e4SLinus Torvalds if (!sctp_sanity_check()) 10051da177e4SLinus Torvalds goto out; 10061da177e4SLinus Torvalds 1007827bf122SSridhar Samudrala /* Allocate bind_bucket and chunk caches. */ 10081da177e4SLinus Torvalds status = -ENOBUFS; 10091da177e4SLinus Torvalds sctp_bucket_cachep = kmem_cache_create("sctp_bind_bucket", 10101da177e4SLinus Torvalds sizeof(struct sctp_bind_bucket), 10111da177e4SLinus Torvalds 0, SLAB_HWCACHE_ALIGN, 101220c2df83SPaul Mundt NULL); 10131da177e4SLinus Torvalds if (!sctp_bucket_cachep) 1014827bf122SSridhar Samudrala goto out; 10151da177e4SLinus Torvalds 10161da177e4SLinus Torvalds sctp_chunk_cachep = kmem_cache_create("sctp_chunk", 10171da177e4SLinus Torvalds sizeof(struct sctp_chunk), 10181da177e4SLinus Torvalds 0, SLAB_HWCACHE_ALIGN, 101920c2df83SPaul Mundt NULL); 10201da177e4SLinus Torvalds if (!sctp_chunk_cachep) 10211da177e4SLinus Torvalds goto err_chunk_cachep; 10221da177e4SLinus Torvalds 10231da177e4SLinus Torvalds /* Allocate and initialise sctp mibs. */ 10241da177e4SLinus Torvalds status = init_sctp_mibs(); 10251da177e4SLinus Torvalds if (status) 10261da177e4SLinus Torvalds goto err_init_mibs; 10271da177e4SLinus Torvalds 10281da177e4SLinus Torvalds /* Initialize proc fs directory. */ 10291da177e4SLinus Torvalds status = sctp_proc_init(); 10301da177e4SLinus Torvalds if (status) 10311da177e4SLinus Torvalds goto err_init_proc; 10321da177e4SLinus Torvalds 10331da177e4SLinus Torvalds /* Initialize object count debugging. */ 10341da177e4SLinus Torvalds sctp_dbg_objcnt_init(); 10351da177e4SLinus Torvalds 10361da177e4SLinus Torvalds /* Initialize the SCTP specific PF functions. */ 10371da177e4SLinus Torvalds sctp_register_pf(&sctp_pf_inet, PF_INET); 10381da177e4SLinus Torvalds /* 10391da177e4SLinus Torvalds * 14. Suggested SCTP Protocol Parameter Values 10401da177e4SLinus Torvalds */ 10411da177e4SLinus Torvalds /* The following protocol parameters are RECOMMENDED: */ 10421da177e4SLinus Torvalds /* RTO.Initial - 3 seconds */ 10431da177e4SLinus Torvalds sctp_rto_initial = SCTP_RTO_INITIAL; 10441da177e4SLinus Torvalds /* RTO.Min - 1 second */ 10451da177e4SLinus Torvalds sctp_rto_min = SCTP_RTO_MIN; 10461da177e4SLinus Torvalds /* RTO.Max - 60 seconds */ 10471da177e4SLinus Torvalds sctp_rto_max = SCTP_RTO_MAX; 10481da177e4SLinus Torvalds /* RTO.Alpha - 1/8 */ 10491da177e4SLinus Torvalds sctp_rto_alpha = SCTP_RTO_ALPHA; 10501da177e4SLinus Torvalds /* RTO.Beta - 1/4 */ 10511da177e4SLinus Torvalds sctp_rto_beta = SCTP_RTO_BETA; 10521da177e4SLinus Torvalds 10531da177e4SLinus Torvalds /* Valid.Cookie.Life - 60 seconds */ 10543fd091e7SVladislav Yasevich sctp_valid_cookie_life = SCTP_DEFAULT_COOKIE_LIFE; 10551da177e4SLinus Torvalds 10561da177e4SLinus Torvalds /* Whether Cookie Preservative is enabled(1) or not(0) */ 10571da177e4SLinus Torvalds sctp_cookie_preserve_enable = 1; 10581da177e4SLinus Torvalds 10591da177e4SLinus Torvalds /* Max.Burst - 4 */ 106070331571SVlad Yasevich sctp_max_burst = SCTP_DEFAULT_MAX_BURST; 10611da177e4SLinus Torvalds 10621da177e4SLinus Torvalds /* Association.Max.Retrans - 10 attempts 10631da177e4SLinus Torvalds * Path.Max.Retrans - 5 attempts (per destination address) 10641da177e4SLinus Torvalds * Max.Init.Retransmits - 8 attempts 10651da177e4SLinus Torvalds */ 10661da177e4SLinus Torvalds sctp_max_retrans_association = 10; 10671da177e4SLinus Torvalds sctp_max_retrans_path = 5; 10681da177e4SLinus Torvalds sctp_max_retrans_init = 8; 10691da177e4SLinus Torvalds 10704eb701dfSNeil Horman /* Sendbuffer growth - do per-socket accounting */ 10714eb701dfSNeil Horman sctp_sndbuf_policy = 0; 10724eb701dfSNeil Horman 1073049b3ff5SNeil Horman /* Rcvbuffer growth - do per-socket accounting */ 1074049b3ff5SNeil Horman sctp_rcvbuf_policy = 0; 1075049b3ff5SNeil Horman 10761da177e4SLinus Torvalds /* HB.interval - 30 seconds */ 10772f85a429SVlad Yasevich sctp_hb_interval = SCTP_DEFAULT_TIMEOUT_HEARTBEAT; 10782f85a429SVlad Yasevich 10792f85a429SVlad Yasevich /* delayed SACK timeout */ 10802f85a429SVlad Yasevich sctp_sack_timeout = SCTP_DEFAULT_TIMEOUT_SACK; 10811da177e4SLinus Torvalds 10821da177e4SLinus Torvalds /* Implementation specific variables. */ 10831da177e4SLinus Torvalds 10841da177e4SLinus Torvalds /* Initialize default stream count setup information. */ 10851da177e4SLinus Torvalds sctp_max_instreams = SCTP_DEFAULT_INSTREAMS; 10861da177e4SLinus Torvalds sctp_max_outstreams = SCTP_DEFAULT_OUTSTREAMS; 10871da177e4SLinus Torvalds 10881da177e4SLinus Torvalds /* Initialize handle used for association ids. */ 10891da177e4SLinus Torvalds idr_init(&sctp_assocs_id); 10901da177e4SLinus Torvalds 10914d93df0aSNeil Horman /* Set the pressure threshold to be a fraction of global memory that 10924d93df0aSNeil Horman * is up to 1/2 at 256 MB, decreasing toward zero with the amount of 10934d93df0aSNeil Horman * memory, with a floor of 128 pages. 10944d93df0aSNeil Horman * Note this initalizes the data in sctpv6_prot too 10954d93df0aSNeil Horman * Unabashedly stolen from tcp_init 10964d93df0aSNeil Horman */ 10974d93df0aSNeil Horman limit = min(num_physpages, 1UL<<(28-PAGE_SHIFT)) >> (20-PAGE_SHIFT); 10984d93df0aSNeil Horman limit = (limit * (num_physpages >> (20-PAGE_SHIFT))) >> (PAGE_SHIFT-11); 10994d93df0aSNeil Horman limit = max(limit, 128UL); 11004d93df0aSNeil Horman sysctl_sctp_mem[0] = limit / 4 * 3; 11014d93df0aSNeil Horman sysctl_sctp_mem[1] = limit; 11024d93df0aSNeil Horman sysctl_sctp_mem[2] = sysctl_sctp_mem[0] * 2; 11034d93df0aSNeil Horman 11044d93df0aSNeil Horman /* Set per-socket limits to no more than 1/128 the pressure threshold*/ 11054d93df0aSNeil Horman limit = (sysctl_sctp_mem[1]) << (PAGE_SHIFT - 7); 11064d93df0aSNeil Horman max_share = min(4UL*1024*1024, limit); 11074d93df0aSNeil Horman 11084d93df0aSNeil Horman sysctl_sctp_rmem[0] = PAGE_SIZE; /* give each asoc 1 page min */ 11094d93df0aSNeil Horman sysctl_sctp_rmem[1] = (1500 *(sizeof(struct sk_buff) + 1)); 11104d93df0aSNeil Horman sysctl_sctp_rmem[2] = max(sysctl_sctp_rmem[1], max_share); 11114d93df0aSNeil Horman 1112*3ab224beSHideo Aoki sysctl_sctp_wmem[0] = SK_MEM_QUANTUM; 11134d93df0aSNeil Horman sysctl_sctp_wmem[1] = 16*1024; 11144d93df0aSNeil Horman sysctl_sctp_wmem[2] = max(64*1024, max_share); 11154d93df0aSNeil Horman 11161da177e4SLinus Torvalds /* Size and allocate the association hash table. 11171da177e4SLinus Torvalds * The methodology is similar to that of the tcp hash tables. 11181da177e4SLinus Torvalds */ 11191da177e4SLinus Torvalds if (num_physpages >= (128 * 1024)) 11201da177e4SLinus Torvalds goal = num_physpages >> (22 - PAGE_SHIFT); 11211da177e4SLinus Torvalds else 11221da177e4SLinus Torvalds goal = num_physpages >> (24 - PAGE_SHIFT); 11231da177e4SLinus Torvalds 11241da177e4SLinus Torvalds for (order = 0; (1UL << order) < goal; order++) 11251da177e4SLinus Torvalds ; 11261da177e4SLinus Torvalds 11271da177e4SLinus Torvalds do { 11281da177e4SLinus Torvalds sctp_assoc_hashsize = (1UL << order) * PAGE_SIZE / 11291da177e4SLinus Torvalds sizeof(struct sctp_hashbucket); 11301da177e4SLinus Torvalds if ((sctp_assoc_hashsize > (64 * 1024)) && order > 0) 11311da177e4SLinus Torvalds continue; 11321da177e4SLinus Torvalds sctp_assoc_hashtable = (struct sctp_hashbucket *) 11331da177e4SLinus Torvalds __get_free_pages(GFP_ATOMIC, order); 11341da177e4SLinus Torvalds } while (!sctp_assoc_hashtable && --order > 0); 11351da177e4SLinus Torvalds if (!sctp_assoc_hashtable) { 11361da177e4SLinus Torvalds printk(KERN_ERR "SCTP: Failed association hash alloc.\n"); 11371da177e4SLinus Torvalds status = -ENOMEM; 11381da177e4SLinus Torvalds goto err_ahash_alloc; 11391da177e4SLinus Torvalds } 11401da177e4SLinus Torvalds for (i = 0; i < sctp_assoc_hashsize; i++) { 11411da177e4SLinus Torvalds rwlock_init(&sctp_assoc_hashtable[i].lock); 1142d970dbf8SVlad Yasevich INIT_HLIST_HEAD(&sctp_assoc_hashtable[i].chain); 11431da177e4SLinus Torvalds } 11441da177e4SLinus Torvalds 11451da177e4SLinus Torvalds /* Allocate and initialize the endpoint hash table. */ 11461da177e4SLinus Torvalds sctp_ep_hashsize = 64; 11471da177e4SLinus Torvalds sctp_ep_hashtable = (struct sctp_hashbucket *) 11481da177e4SLinus Torvalds kmalloc(64 * sizeof(struct sctp_hashbucket), GFP_KERNEL); 11491da177e4SLinus Torvalds if (!sctp_ep_hashtable) { 11501da177e4SLinus Torvalds printk(KERN_ERR "SCTP: Failed endpoint_hash alloc.\n"); 11511da177e4SLinus Torvalds status = -ENOMEM; 11521da177e4SLinus Torvalds goto err_ehash_alloc; 11531da177e4SLinus Torvalds } 11541da177e4SLinus Torvalds for (i = 0; i < sctp_ep_hashsize; i++) { 11551da177e4SLinus Torvalds rwlock_init(&sctp_ep_hashtable[i].lock); 1156d970dbf8SVlad Yasevich INIT_HLIST_HEAD(&sctp_ep_hashtable[i].chain); 11571da177e4SLinus Torvalds } 11581da177e4SLinus Torvalds 11591da177e4SLinus Torvalds /* Allocate and initialize the SCTP port hash table. */ 11601da177e4SLinus Torvalds do { 11611da177e4SLinus Torvalds sctp_port_hashsize = (1UL << order) * PAGE_SIZE / 11621da177e4SLinus Torvalds sizeof(struct sctp_bind_hashbucket); 11631da177e4SLinus Torvalds if ((sctp_port_hashsize > (64 * 1024)) && order > 0) 11641da177e4SLinus Torvalds continue; 11651da177e4SLinus Torvalds sctp_port_hashtable = (struct sctp_bind_hashbucket *) 11661da177e4SLinus Torvalds __get_free_pages(GFP_ATOMIC, order); 11671da177e4SLinus Torvalds } while (!sctp_port_hashtable && --order > 0); 11681da177e4SLinus Torvalds if (!sctp_port_hashtable) { 11691da177e4SLinus Torvalds printk(KERN_ERR "SCTP: Failed bind hash alloc."); 11701da177e4SLinus Torvalds status = -ENOMEM; 11711da177e4SLinus Torvalds goto err_bhash_alloc; 11721da177e4SLinus Torvalds } 11731da177e4SLinus Torvalds for (i = 0; i < sctp_port_hashsize; i++) { 11741da177e4SLinus Torvalds spin_lock_init(&sctp_port_hashtable[i].lock); 1175d970dbf8SVlad Yasevich INIT_HLIST_HEAD(&sctp_port_hashtable[i].chain); 11761da177e4SLinus Torvalds } 11771da177e4SLinus Torvalds 11781da177e4SLinus Torvalds printk(KERN_INFO "SCTP: Hash tables configured " 11791da177e4SLinus Torvalds "(established %d bind %d)\n", 11801da177e4SLinus Torvalds sctp_assoc_hashsize, sctp_port_hashsize); 11811da177e4SLinus Torvalds 11821da177e4SLinus Torvalds /* Disable ADDIP by default. */ 11831da177e4SLinus Torvalds sctp_addip_enable = 0; 118473d9c4fdSVlad Yasevich sctp_addip_noauth = 0; 11851da177e4SLinus Torvalds 11861da177e4SLinus Torvalds /* Enable PR-SCTP by default. */ 11871da177e4SLinus Torvalds sctp_prsctp_enable = 1; 11881da177e4SLinus Torvalds 1189a29a5bd4SVlad Yasevich /* Disable AUTH by default. */ 1190a29a5bd4SVlad Yasevich sctp_auth_enable = 0; 1191a29a5bd4SVlad Yasevich 11921da177e4SLinus Torvalds sctp_sysctl_register(); 11931da177e4SLinus Torvalds 11941da177e4SLinus Torvalds INIT_LIST_HEAD(&sctp_address_families); 11951da177e4SLinus Torvalds sctp_register_af(&sctp_ipv4_specific); 11961da177e4SLinus Torvalds 1197827bf122SSridhar Samudrala status = proto_register(&sctp_prot, 1); 1198827bf122SSridhar Samudrala if (status) 1199827bf122SSridhar Samudrala goto err_proto_register; 1200827bf122SSridhar Samudrala 1201827bf122SSridhar Samudrala /* Register SCTP(UDP and TCP style) with socket layer. */ 1202827bf122SSridhar Samudrala inet_register_protosw(&sctp_seqpacket_protosw); 1203827bf122SSridhar Samudrala inet_register_protosw(&sctp_stream_protosw); 1204827bf122SSridhar Samudrala 12051da177e4SLinus Torvalds status = sctp_v6_init(); 12061da177e4SLinus Torvalds if (status) 12071da177e4SLinus Torvalds goto err_v6_init; 12081da177e4SLinus Torvalds 12091da177e4SLinus Torvalds /* Initialize the control inode/socket for handling OOTB packets. */ 12101da177e4SLinus Torvalds if ((status = sctp_ctl_sock_init())) { 12111da177e4SLinus Torvalds printk (KERN_ERR 12121da177e4SLinus Torvalds "SCTP: Failed to initialize the SCTP control sock.\n"); 12131da177e4SLinus Torvalds goto err_ctl_sock_init; 12141da177e4SLinus Torvalds } 12151da177e4SLinus Torvalds 12161da177e4SLinus Torvalds /* Initialize the local address list. */ 12171da177e4SLinus Torvalds INIT_LIST_HEAD(&sctp_local_addr_list); 121829303547SVlad Yasevich spin_lock_init(&sctp_local_addr_lock); 121929c7cf96SSridhar Samudrala sctp_get_local_addr_list(); 12201da177e4SLinus Torvalds 12211da177e4SLinus Torvalds /* Register notifier for inet address additions/deletions. */ 12221da177e4SLinus Torvalds register_inetaddr_notifier(&sctp_inetaddr_notifier); 12231da177e4SLinus Torvalds 1224827bf122SSridhar Samudrala /* Register SCTP with inet layer. */ 1225827bf122SSridhar Samudrala if (inet_add_protocol(&sctp_protocol, IPPROTO_SCTP) < 0) { 1226827bf122SSridhar Samudrala status = -EAGAIN; 1227827bf122SSridhar Samudrala goto err_add_protocol; 1228827bf122SSridhar Samudrala } 1229827bf122SSridhar Samudrala 1230827bf122SSridhar Samudrala /* Register SCTP with inet6 layer. */ 1231827bf122SSridhar Samudrala status = sctp_v6_add_protocol(); 1232827bf122SSridhar Samudrala if (status) 1233827bf122SSridhar Samudrala goto err_v6_add_protocol; 1234827bf122SSridhar Samudrala 12351da177e4SLinus Torvalds status = 0; 12361da177e4SLinus Torvalds out: 12371da177e4SLinus Torvalds return status; 1238827bf122SSridhar Samudrala err_v6_add_protocol: 1239827bf122SSridhar Samudrala inet_del_protocol(&sctp_protocol, IPPROTO_SCTP); 1240827bf122SSridhar Samudrala unregister_inetaddr_notifier(&sctp_inetaddr_notifier); 1241827bf122SSridhar Samudrala err_add_protocol: 1242827bf122SSridhar Samudrala sctp_free_local_addr_list(); 1243827bf122SSridhar Samudrala sock_release(sctp_ctl_socket); 12441da177e4SLinus Torvalds err_ctl_sock_init: 12451da177e4SLinus Torvalds sctp_v6_exit(); 12461da177e4SLinus Torvalds err_v6_init: 1247827bf122SSridhar Samudrala inet_unregister_protosw(&sctp_stream_protosw); 1248827bf122SSridhar Samudrala inet_unregister_protosw(&sctp_seqpacket_protosw); 1249827bf122SSridhar Samudrala proto_unregister(&sctp_prot); 1250827bf122SSridhar Samudrala err_proto_register: 12511da177e4SLinus Torvalds sctp_sysctl_unregister(); 12521da177e4SLinus Torvalds list_del(&sctp_ipv4_specific.list); 12531da177e4SLinus Torvalds free_pages((unsigned long)sctp_port_hashtable, 12541da177e4SLinus Torvalds get_order(sctp_port_hashsize * 12551da177e4SLinus Torvalds sizeof(struct sctp_bind_hashbucket))); 12561da177e4SLinus Torvalds err_bhash_alloc: 12571da177e4SLinus Torvalds kfree(sctp_ep_hashtable); 12581da177e4SLinus Torvalds err_ehash_alloc: 12591da177e4SLinus Torvalds free_pages((unsigned long)sctp_assoc_hashtable, 12601da177e4SLinus Torvalds get_order(sctp_assoc_hashsize * 12611da177e4SLinus Torvalds sizeof(struct sctp_hashbucket))); 12621da177e4SLinus Torvalds err_ahash_alloc: 12631da177e4SLinus Torvalds sctp_dbg_objcnt_exit(); 12641da177e4SLinus Torvalds sctp_proc_exit(); 1265827bf122SSridhar Samudrala err_init_proc: 12661da177e4SLinus Torvalds cleanup_sctp_mibs(); 12671da177e4SLinus Torvalds err_init_mibs: 12681da177e4SLinus Torvalds kmem_cache_destroy(sctp_chunk_cachep); 12691da177e4SLinus Torvalds err_chunk_cachep: 12701da177e4SLinus Torvalds kmem_cache_destroy(sctp_bucket_cachep); 12711da177e4SLinus Torvalds goto out; 12721da177e4SLinus Torvalds } 12731da177e4SLinus Torvalds 12741da177e4SLinus Torvalds /* Exit handler for the SCTP protocol. */ 12751da177e4SLinus Torvalds SCTP_STATIC __exit void sctp_exit(void) 12761da177e4SLinus Torvalds { 12771da177e4SLinus Torvalds /* BUG. This should probably do something useful like clean 12781da177e4SLinus Torvalds * up all the remaining associations and all that memory. 12791da177e4SLinus Torvalds */ 12801da177e4SLinus Torvalds 1281827bf122SSridhar Samudrala /* Unregister with inet6/inet layers. */ 1282827bf122SSridhar Samudrala sctp_v6_del_protocol(); 1283827bf122SSridhar Samudrala inet_del_protocol(&sctp_protocol, IPPROTO_SCTP); 12841da177e4SLinus Torvalds 128529303547SVlad Yasevich /* Unregister notifier for inet address additions/deletions. */ 128629303547SVlad Yasevich unregister_inetaddr_notifier(&sctp_inetaddr_notifier); 128729303547SVlad Yasevich 12881da177e4SLinus Torvalds /* Free the local address list. */ 12891da177e4SLinus Torvalds sctp_free_local_addr_list(); 12901da177e4SLinus Torvalds 12911da177e4SLinus Torvalds /* Free the control endpoint. */ 12921da177e4SLinus Torvalds sock_release(sctp_ctl_socket); 12931da177e4SLinus Torvalds 1294827bf122SSridhar Samudrala /* Cleanup v6 initializations. */ 12951da177e4SLinus Torvalds sctp_v6_exit(); 1296827bf122SSridhar Samudrala 1297827bf122SSridhar Samudrala /* Unregister with socket layer. */ 1298827bf122SSridhar Samudrala inet_unregister_protosw(&sctp_stream_protosw); 1299827bf122SSridhar Samudrala inet_unregister_protosw(&sctp_seqpacket_protosw); 1300827bf122SSridhar Samudrala 13011da177e4SLinus Torvalds sctp_sysctl_unregister(); 13021da177e4SLinus Torvalds list_del(&sctp_ipv4_specific.list); 13031da177e4SLinus Torvalds 13041da177e4SLinus Torvalds free_pages((unsigned long)sctp_assoc_hashtable, 13051da177e4SLinus Torvalds get_order(sctp_assoc_hashsize * 13061da177e4SLinus Torvalds sizeof(struct sctp_hashbucket))); 13071da177e4SLinus Torvalds kfree(sctp_ep_hashtable); 13081da177e4SLinus Torvalds free_pages((unsigned long)sctp_port_hashtable, 13091da177e4SLinus Torvalds get_order(sctp_port_hashsize * 13101da177e4SLinus Torvalds sizeof(struct sctp_bind_hashbucket))); 13111da177e4SLinus Torvalds 13121da177e4SLinus Torvalds sctp_dbg_objcnt_exit(); 13131da177e4SLinus Torvalds sctp_proc_exit(); 13141da177e4SLinus Torvalds cleanup_sctp_mibs(); 13151da177e4SLinus Torvalds 1316827bf122SSridhar Samudrala kmem_cache_destroy(sctp_chunk_cachep); 1317827bf122SSridhar Samudrala kmem_cache_destroy(sctp_bucket_cachep); 1318827bf122SSridhar Samudrala 13191da177e4SLinus Torvalds proto_unregister(&sctp_prot); 13201da177e4SLinus Torvalds } 13211da177e4SLinus Torvalds 13221da177e4SLinus Torvalds module_init(sctp_init); 13231da177e4SLinus Torvalds module_exit(sctp_exit); 13241da177e4SLinus Torvalds 1325bb97d31fSArnaldo Carvalho de Melo /* 1326bb97d31fSArnaldo Carvalho de Melo * __stringify doesn't likes enums, so use IPPROTO_SCTP value (132) directly. 1327bb97d31fSArnaldo Carvalho de Melo */ 1328bb97d31fSArnaldo Carvalho de Melo MODULE_ALIAS("net-pf-" __stringify(PF_INET) "-proto-132"); 1329882a382cSSridhar Samudrala MODULE_ALIAS("net-pf-" __stringify(PF_INET6) "-proto-132"); 13301da177e4SLinus Torvalds MODULE_AUTHOR("Linux Kernel SCTP developers <lksctp-developers@lists.sourceforge.net>"); 13311da177e4SLinus Torvalds MODULE_DESCRIPTION("Support for the SCTP protocol (RFC2960)"); 13321da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 1333