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> 541da177e4SLinus Torvalds #include <net/protocol.h> 551da177e4SLinus Torvalds #include <net/ip.h> 561da177e4SLinus Torvalds #include <net/ipv6.h> 571da177e4SLinus Torvalds #include <net/sctp/sctp.h> 581da177e4SLinus Torvalds #include <net/addrconf.h> 591da177e4SLinus Torvalds #include <net/inet_common.h> 601da177e4SLinus Torvalds #include <net/inet_ecn.h> 611da177e4SLinus Torvalds 621da177e4SLinus Torvalds /* Global data structures. */ 631da177e4SLinus Torvalds struct sctp_globals sctp_globals; 641da177e4SLinus Torvalds struct proc_dir_entry *proc_net_sctp; 65ba89966cSEric Dumazet DEFINE_SNMP_STAT(struct sctp_mib, sctp_statistics) __read_mostly; 661da177e4SLinus Torvalds 671da177e4SLinus Torvalds struct idr sctp_assocs_id; 681da177e4SLinus Torvalds DEFINE_SPINLOCK(sctp_assocs_id_lock); 691da177e4SLinus Torvalds 701da177e4SLinus Torvalds /* This is the global socket data structure used for responding to 711da177e4SLinus Torvalds * the Out-of-the-blue (OOTB) packets. A control sock will be created 721da177e4SLinus Torvalds * for this socket at the initialization time. 731da177e4SLinus Torvalds */ 741da177e4SLinus Torvalds static struct socket *sctp_ctl_socket; 751da177e4SLinus Torvalds 761da177e4SLinus Torvalds static struct sctp_pf *sctp_pf_inet6_specific; 771da177e4SLinus Torvalds static struct sctp_pf *sctp_pf_inet_specific; 781da177e4SLinus Torvalds static struct sctp_af *sctp_af_v4_specific; 791da177e4SLinus Torvalds static struct sctp_af *sctp_af_v6_specific; 801da177e4SLinus Torvalds 81ba89966cSEric Dumazet kmem_cache_t *sctp_chunk_cachep __read_mostly; 82ba89966cSEric Dumazet kmem_cache_t *sctp_bucket_cachep __read_mostly; 831da177e4SLinus Torvalds 841da177e4SLinus Torvalds extern int sctp_snmp_proc_init(void); 851da177e4SLinus Torvalds extern int sctp_snmp_proc_exit(void); 861da177e4SLinus Torvalds extern int sctp_eps_proc_init(void); 871da177e4SLinus Torvalds extern int sctp_eps_proc_exit(void); 881da177e4SLinus Torvalds extern int sctp_assocs_proc_init(void); 891da177e4SLinus Torvalds extern int sctp_assocs_proc_exit(void); 901da177e4SLinus Torvalds 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; 1021da177e4SLinus Torvalds ent = proc_mkdir("net/sctp", NULL); 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; 1351da177e4SLinus Torvalds remove_proc_entry("net/sctp", NULL); 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) { 1591da177e4SLinus Torvalds addr->a.v4.sin_family = AF_INET; 1601da177e4SLinus Torvalds addr->a.v4.sin_port = 0; 1611da177e4SLinus Torvalds addr->a.v4.sin_addr.s_addr = ifa->ifa_local; 1621da177e4SLinus Torvalds list_add_tail(&addr->list, addrlist); 1631da177e4SLinus Torvalds } 1641da177e4SLinus Torvalds } 1651da177e4SLinus Torvalds 1661da177e4SLinus Torvalds rcu_read_unlock(); 1671da177e4SLinus Torvalds } 1681da177e4SLinus Torvalds 1691da177e4SLinus Torvalds /* Extract our IP addresses from the system and stash them in the 1701da177e4SLinus Torvalds * protocol structure. 1711da177e4SLinus Torvalds */ 1721da177e4SLinus Torvalds static void __sctp_get_local_addr_list(void) 1731da177e4SLinus Torvalds { 1741da177e4SLinus Torvalds struct net_device *dev; 1751da177e4SLinus Torvalds struct list_head *pos; 1761da177e4SLinus Torvalds struct sctp_af *af; 1771da177e4SLinus Torvalds 1781da177e4SLinus Torvalds read_lock(&dev_base_lock); 1791da177e4SLinus Torvalds for (dev = dev_base; dev; dev = dev->next) { 1801da177e4SLinus Torvalds __list_for_each(pos, &sctp_address_families) { 1811da177e4SLinus Torvalds af = list_entry(pos, struct sctp_af, list); 1821da177e4SLinus Torvalds af->copy_addrlist(&sctp_local_addr_list, dev); 1831da177e4SLinus Torvalds } 1841da177e4SLinus Torvalds } 1851da177e4SLinus Torvalds read_unlock(&dev_base_lock); 1861da177e4SLinus Torvalds } 1871da177e4SLinus Torvalds 1881da177e4SLinus Torvalds static void sctp_get_local_addr_list(void) 1891da177e4SLinus Torvalds { 1901da177e4SLinus Torvalds unsigned long flags; 1911da177e4SLinus Torvalds 1921da177e4SLinus Torvalds sctp_spin_lock_irqsave(&sctp_local_addr_lock, flags); 1931da177e4SLinus Torvalds __sctp_get_local_addr_list(); 1941da177e4SLinus Torvalds sctp_spin_unlock_irqrestore(&sctp_local_addr_lock, flags); 1951da177e4SLinus Torvalds } 1961da177e4SLinus Torvalds 1971da177e4SLinus Torvalds /* Free the existing local addresses. */ 1981da177e4SLinus Torvalds static void __sctp_free_local_addr_list(void) 1991da177e4SLinus Torvalds { 2001da177e4SLinus Torvalds struct sctp_sockaddr_entry *addr; 2011da177e4SLinus Torvalds struct list_head *pos, *temp; 2021da177e4SLinus Torvalds 2031da177e4SLinus Torvalds list_for_each_safe(pos, temp, &sctp_local_addr_list) { 2041da177e4SLinus Torvalds addr = list_entry(pos, struct sctp_sockaddr_entry, list); 2051da177e4SLinus Torvalds list_del(pos); 2061da177e4SLinus Torvalds kfree(addr); 2071da177e4SLinus Torvalds } 2081da177e4SLinus Torvalds } 2091da177e4SLinus Torvalds 2101da177e4SLinus Torvalds /* Free the existing local addresses. */ 2111da177e4SLinus Torvalds static void sctp_free_local_addr_list(void) 2121da177e4SLinus Torvalds { 2131da177e4SLinus Torvalds unsigned long flags; 2141da177e4SLinus Torvalds 2151da177e4SLinus Torvalds sctp_spin_lock_irqsave(&sctp_local_addr_lock, flags); 2161da177e4SLinus Torvalds __sctp_free_local_addr_list(); 2171da177e4SLinus Torvalds sctp_spin_unlock_irqrestore(&sctp_local_addr_lock, flags); 2181da177e4SLinus Torvalds } 2191da177e4SLinus Torvalds 2201da177e4SLinus Torvalds /* Copy the local addresses which are valid for 'scope' into 'bp'. */ 2211da177e4SLinus Torvalds int sctp_copy_local_addr_list(struct sctp_bind_addr *bp, sctp_scope_t scope, 222dd0fc66fSAl Viro gfp_t gfp, int copy_flags) 2231da177e4SLinus Torvalds { 2241da177e4SLinus Torvalds struct sctp_sockaddr_entry *addr; 2251da177e4SLinus Torvalds int error = 0; 2261da177e4SLinus Torvalds struct list_head *pos; 2271da177e4SLinus Torvalds unsigned long flags; 2281da177e4SLinus Torvalds 2291da177e4SLinus Torvalds sctp_spin_lock_irqsave(&sctp_local_addr_lock, flags); 2301da177e4SLinus Torvalds list_for_each(pos, &sctp_local_addr_list) { 2311da177e4SLinus Torvalds addr = list_entry(pos, struct sctp_sockaddr_entry, list); 2321da177e4SLinus Torvalds if (sctp_in_scope(&addr->a, scope)) { 2331da177e4SLinus Torvalds /* Now that the address is in scope, check to see if 2341da177e4SLinus Torvalds * the address type is really supported by the local 2351da177e4SLinus Torvalds * sock as well as the remote peer. 2361da177e4SLinus Torvalds */ 2371da177e4SLinus Torvalds if ((((AF_INET == addr->a.sa.sa_family) && 2381da177e4SLinus Torvalds (copy_flags & SCTP_ADDR4_PEERSUPP))) || 2391da177e4SLinus Torvalds (((AF_INET6 == addr->a.sa.sa_family) && 2401da177e4SLinus Torvalds (copy_flags & SCTP_ADDR6_ALLOWED) && 2411da177e4SLinus Torvalds (copy_flags & SCTP_ADDR6_PEERSUPP)))) { 2421da177e4SLinus Torvalds error = sctp_add_bind_addr(bp, &addr->a, 2431da177e4SLinus Torvalds GFP_ATOMIC); 2441da177e4SLinus Torvalds if (error) 2451da177e4SLinus Torvalds goto end_copy; 2461da177e4SLinus Torvalds } 2471da177e4SLinus Torvalds } 2481da177e4SLinus Torvalds } 2491da177e4SLinus Torvalds 2501da177e4SLinus Torvalds end_copy: 2511da177e4SLinus Torvalds sctp_spin_unlock_irqrestore(&sctp_local_addr_lock, flags); 2521da177e4SLinus Torvalds return error; 2531da177e4SLinus Torvalds } 2541da177e4SLinus Torvalds 2551da177e4SLinus Torvalds /* Initialize a sctp_addr from in incoming skb. */ 2561da177e4SLinus Torvalds static void sctp_v4_from_skb(union sctp_addr *addr, struct sk_buff *skb, 2571da177e4SLinus Torvalds int is_saddr) 2581da177e4SLinus Torvalds { 2591da177e4SLinus Torvalds void *from; 2601da177e4SLinus Torvalds __u16 *port; 2611da177e4SLinus Torvalds struct sctphdr *sh; 2621da177e4SLinus Torvalds 2631da177e4SLinus Torvalds port = &addr->v4.sin_port; 2641da177e4SLinus Torvalds addr->v4.sin_family = AF_INET; 2651da177e4SLinus Torvalds 2661da177e4SLinus Torvalds sh = (struct sctphdr *) skb->h.raw; 2671da177e4SLinus Torvalds if (is_saddr) { 2681da177e4SLinus Torvalds *port = ntohs(sh->source); 2691da177e4SLinus Torvalds from = &skb->nh.iph->saddr; 2701da177e4SLinus Torvalds } else { 2711da177e4SLinus Torvalds *port = ntohs(sh->dest); 2721da177e4SLinus Torvalds from = &skb->nh.iph->daddr; 2731da177e4SLinus Torvalds } 2741da177e4SLinus Torvalds memcpy(&addr->v4.sin_addr.s_addr, from, sizeof(struct in_addr)); 2751da177e4SLinus Torvalds } 2761da177e4SLinus Torvalds 2771da177e4SLinus Torvalds /* Initialize an sctp_addr from a socket. */ 2781da177e4SLinus Torvalds static void sctp_v4_from_sk(union sctp_addr *addr, struct sock *sk) 2791da177e4SLinus Torvalds { 2801da177e4SLinus Torvalds addr->v4.sin_family = AF_INET; 2811da177e4SLinus Torvalds addr->v4.sin_port = inet_sk(sk)->num; 2821da177e4SLinus Torvalds addr->v4.sin_addr.s_addr = inet_sk(sk)->rcv_saddr; 2831da177e4SLinus Torvalds } 2841da177e4SLinus Torvalds 2851da177e4SLinus Torvalds /* Initialize sk->sk_rcv_saddr from sctp_addr. */ 2861da177e4SLinus Torvalds static void sctp_v4_to_sk_saddr(union sctp_addr *addr, struct sock *sk) 2871da177e4SLinus Torvalds { 2881da177e4SLinus Torvalds inet_sk(sk)->rcv_saddr = addr->v4.sin_addr.s_addr; 2891da177e4SLinus Torvalds } 2901da177e4SLinus Torvalds 2911da177e4SLinus Torvalds /* Initialize sk->sk_daddr from sctp_addr. */ 2921da177e4SLinus Torvalds static void sctp_v4_to_sk_daddr(union sctp_addr *addr, struct sock *sk) 2931da177e4SLinus Torvalds { 2941da177e4SLinus Torvalds inet_sk(sk)->daddr = addr->v4.sin_addr.s_addr; 2951da177e4SLinus Torvalds } 2961da177e4SLinus Torvalds 2971da177e4SLinus Torvalds /* Initialize a sctp_addr from an address parameter. */ 2981da177e4SLinus Torvalds static void sctp_v4_from_addr_param(union sctp_addr *addr, 2991da177e4SLinus Torvalds union sctp_addr_param *param, 3001da177e4SLinus Torvalds __u16 port, int iif) 3011da177e4SLinus Torvalds { 3021da177e4SLinus Torvalds addr->v4.sin_family = AF_INET; 3031da177e4SLinus Torvalds addr->v4.sin_port = port; 3041da177e4SLinus Torvalds addr->v4.sin_addr.s_addr = param->v4.addr.s_addr; 3051da177e4SLinus Torvalds } 3061da177e4SLinus Torvalds 3071da177e4SLinus Torvalds /* Initialize an address parameter from a sctp_addr and return the length 3081da177e4SLinus Torvalds * of the address parameter. 3091da177e4SLinus Torvalds */ 3101da177e4SLinus Torvalds static int sctp_v4_to_addr_param(const union sctp_addr *addr, 3111da177e4SLinus Torvalds union sctp_addr_param *param) 3121da177e4SLinus Torvalds { 3131da177e4SLinus Torvalds int length = sizeof(sctp_ipv4addr_param_t); 3141da177e4SLinus Torvalds 3151da177e4SLinus Torvalds param->v4.param_hdr.type = SCTP_PARAM_IPV4_ADDRESS; 3161da177e4SLinus Torvalds param->v4.param_hdr.length = ntohs(length); 3171da177e4SLinus Torvalds param->v4.addr.s_addr = addr->v4.sin_addr.s_addr; 3181da177e4SLinus Torvalds 3191da177e4SLinus Torvalds return length; 3201da177e4SLinus Torvalds } 3211da177e4SLinus Torvalds 3221da177e4SLinus Torvalds /* Initialize a sctp_addr from a dst_entry. */ 3231da177e4SLinus Torvalds static void sctp_v4_dst_saddr(union sctp_addr *saddr, struct dst_entry *dst, 3241da177e4SLinus Torvalds unsigned short port) 3251da177e4SLinus Torvalds { 3261da177e4SLinus Torvalds struct rtable *rt = (struct rtable *)dst; 3271da177e4SLinus Torvalds saddr->v4.sin_family = AF_INET; 3281da177e4SLinus Torvalds saddr->v4.sin_port = port; 3291da177e4SLinus Torvalds saddr->v4.sin_addr.s_addr = rt->rt_src; 3301da177e4SLinus Torvalds } 3311da177e4SLinus Torvalds 3321da177e4SLinus Torvalds /* Compare two addresses exactly. */ 3331da177e4SLinus Torvalds static int sctp_v4_cmp_addr(const union sctp_addr *addr1, 3341da177e4SLinus Torvalds const union sctp_addr *addr2) 3351da177e4SLinus Torvalds { 3361da177e4SLinus Torvalds if (addr1->sa.sa_family != addr2->sa.sa_family) 3371da177e4SLinus Torvalds return 0; 3381da177e4SLinus Torvalds if (addr1->v4.sin_port != addr2->v4.sin_port) 3391da177e4SLinus Torvalds return 0; 3401da177e4SLinus Torvalds if (addr1->v4.sin_addr.s_addr != addr2->v4.sin_addr.s_addr) 3411da177e4SLinus Torvalds return 0; 3421da177e4SLinus Torvalds 3431da177e4SLinus Torvalds return 1; 3441da177e4SLinus Torvalds } 3451da177e4SLinus Torvalds 3461da177e4SLinus Torvalds /* Initialize addr struct to INADDR_ANY. */ 3471da177e4SLinus Torvalds static void sctp_v4_inaddr_any(union sctp_addr *addr, unsigned short port) 3481da177e4SLinus Torvalds { 3491da177e4SLinus Torvalds addr->v4.sin_family = AF_INET; 3501da177e4SLinus Torvalds addr->v4.sin_addr.s_addr = INADDR_ANY; 3511da177e4SLinus Torvalds addr->v4.sin_port = port; 3521da177e4SLinus Torvalds } 3531da177e4SLinus Torvalds 3541da177e4SLinus Torvalds /* Is this a wildcard address? */ 3551da177e4SLinus Torvalds static int sctp_v4_is_any(const union sctp_addr *addr) 3561da177e4SLinus Torvalds { 3571da177e4SLinus Torvalds return INADDR_ANY == addr->v4.sin_addr.s_addr; 3581da177e4SLinus Torvalds } 3591da177e4SLinus Torvalds 3601da177e4SLinus Torvalds /* This function checks if the address is a valid address to be used for 3611da177e4SLinus Torvalds * SCTP binding. 3621da177e4SLinus Torvalds * 3631da177e4SLinus Torvalds * Output: 3641da177e4SLinus Torvalds * Return 0 - If the address is a non-unicast or an illegal address. 3651da177e4SLinus Torvalds * Return 1 - If the address is a unicast. 3661da177e4SLinus Torvalds */ 3671da177e4SLinus Torvalds static int sctp_v4_addr_valid(union sctp_addr *addr, struct sctp_sock *sp) 3681da177e4SLinus Torvalds { 3691da177e4SLinus Torvalds /* Is this a non-unicast address or a unusable SCTP address? */ 3701da177e4SLinus Torvalds if (IS_IPV4_UNUSABLE_ADDRESS(&addr->v4.sin_addr.s_addr)) 3711da177e4SLinus Torvalds return 0; 3721da177e4SLinus Torvalds 3731da177e4SLinus Torvalds return 1; 3741da177e4SLinus Torvalds } 3751da177e4SLinus Torvalds 3761da177e4SLinus Torvalds /* Should this be available for binding? */ 3771da177e4SLinus Torvalds static int sctp_v4_available(union sctp_addr *addr, struct sctp_sock *sp) 3781da177e4SLinus Torvalds { 3791da177e4SLinus Torvalds int ret = inet_addr_type(addr->v4.sin_addr.s_addr); 3801da177e4SLinus Torvalds 3811da177e4SLinus Torvalds 382cdac4e07SNeil Horman if (addr->v4.sin_addr.s_addr != INADDR_ANY && 383cdac4e07SNeil Horman ret != RTN_LOCAL && 384cdac4e07SNeil Horman !sp->inet.freebind && 385cdac4e07SNeil Horman !sysctl_ip_nonlocal_bind) 3861da177e4SLinus Torvalds return 0; 387cdac4e07SNeil Horman 3881da177e4SLinus Torvalds return 1; 3891da177e4SLinus Torvalds } 3901da177e4SLinus Torvalds 3911da177e4SLinus Torvalds /* Checking the loopback, private and other address scopes as defined in 3921da177e4SLinus Torvalds * RFC 1918. The IPv4 scoping is based on the draft for SCTP IPv4 3931da177e4SLinus Torvalds * scoping <draft-stewart-tsvwg-sctp-ipv4-00.txt>. 3941da177e4SLinus Torvalds * 3951da177e4SLinus Torvalds * Level 0 - unusable SCTP addresses 3961da177e4SLinus Torvalds * Level 1 - loopback address 3971da177e4SLinus Torvalds * Level 2 - link-local addresses 3981da177e4SLinus Torvalds * Level 3 - private addresses. 3991da177e4SLinus Torvalds * Level 4 - global addresses 4001da177e4SLinus Torvalds * For INIT and INIT-ACK address list, let L be the level of 4011da177e4SLinus Torvalds * of requested destination address, sender and receiver 4021da177e4SLinus Torvalds * SHOULD include all of its addresses with level greater 4031da177e4SLinus Torvalds * than or equal to L. 4041da177e4SLinus Torvalds */ 4051da177e4SLinus Torvalds static sctp_scope_t sctp_v4_scope(union sctp_addr *addr) 4061da177e4SLinus Torvalds { 4071da177e4SLinus Torvalds sctp_scope_t retval; 4081da177e4SLinus Torvalds 4091da177e4SLinus Torvalds /* Should IPv4 scoping be a sysctl configurable option 4101da177e4SLinus Torvalds * so users can turn it off (default on) for certain 4111da177e4SLinus Torvalds * unconventional networking environments? 4121da177e4SLinus Torvalds */ 4131da177e4SLinus Torvalds 4141da177e4SLinus Torvalds /* Check for unusable SCTP addresses. */ 4151da177e4SLinus Torvalds if (IS_IPV4_UNUSABLE_ADDRESS(&addr->v4.sin_addr.s_addr)) { 4161da177e4SLinus Torvalds retval = SCTP_SCOPE_UNUSABLE; 4171da177e4SLinus Torvalds } else if (LOOPBACK(addr->v4.sin_addr.s_addr)) { 4181da177e4SLinus Torvalds retval = SCTP_SCOPE_LOOPBACK; 4191da177e4SLinus Torvalds } else if (IS_IPV4_LINK_ADDRESS(&addr->v4.sin_addr.s_addr)) { 4201da177e4SLinus Torvalds retval = SCTP_SCOPE_LINK; 4211da177e4SLinus Torvalds } else if (IS_IPV4_PRIVATE_ADDRESS(&addr->v4.sin_addr.s_addr)) { 4221da177e4SLinus Torvalds retval = SCTP_SCOPE_PRIVATE; 4231da177e4SLinus Torvalds } else { 4241da177e4SLinus Torvalds retval = SCTP_SCOPE_GLOBAL; 4251da177e4SLinus Torvalds } 4261da177e4SLinus Torvalds 4271da177e4SLinus Torvalds return retval; 4281da177e4SLinus Torvalds } 4291da177e4SLinus Torvalds 4301da177e4SLinus Torvalds /* Returns a valid dst cache entry for the given source and destination ip 4311da177e4SLinus Torvalds * addresses. If an association is passed, trys to get a dst entry with a 4321da177e4SLinus Torvalds * source address that matches an address in the bind address list. 4331da177e4SLinus Torvalds */ 4341da177e4SLinus Torvalds static struct dst_entry *sctp_v4_get_dst(struct sctp_association *asoc, 4351da177e4SLinus Torvalds union sctp_addr *daddr, 4361da177e4SLinus Torvalds union sctp_addr *saddr) 4371da177e4SLinus Torvalds { 4381da177e4SLinus Torvalds struct rtable *rt; 4391da177e4SLinus Torvalds struct flowi fl; 4401da177e4SLinus Torvalds struct sctp_bind_addr *bp; 4411da177e4SLinus Torvalds rwlock_t *addr_lock; 4421da177e4SLinus Torvalds struct sctp_sockaddr_entry *laddr; 4431da177e4SLinus Torvalds struct list_head *pos; 4441da177e4SLinus Torvalds struct dst_entry *dst = NULL; 4451da177e4SLinus Torvalds union sctp_addr dst_saddr; 4461da177e4SLinus Torvalds 4471da177e4SLinus Torvalds memset(&fl, 0x0, sizeof(struct flowi)); 4481da177e4SLinus Torvalds fl.fl4_dst = daddr->v4.sin_addr.s_addr; 4491da177e4SLinus Torvalds fl.proto = IPPROTO_SCTP; 4501da177e4SLinus Torvalds if (asoc) { 4511da177e4SLinus Torvalds fl.fl4_tos = RT_CONN_FLAGS(asoc->base.sk); 4521da177e4SLinus Torvalds fl.oif = asoc->base.sk->sk_bound_dev_if; 4531da177e4SLinus Torvalds } 4541da177e4SLinus Torvalds if (saddr) 4551da177e4SLinus Torvalds fl.fl4_src = saddr->v4.sin_addr.s_addr; 4561da177e4SLinus Torvalds 4571da177e4SLinus Torvalds SCTP_DEBUG_PRINTK("%s: DST:%u.%u.%u.%u, SRC:%u.%u.%u.%u - ", 4581da177e4SLinus Torvalds __FUNCTION__, NIPQUAD(fl.fl4_dst), 4591da177e4SLinus Torvalds NIPQUAD(fl.fl4_src)); 4601da177e4SLinus Torvalds 4611da177e4SLinus Torvalds if (!ip_route_output_key(&rt, &fl)) { 4621da177e4SLinus Torvalds dst = &rt->u.dst; 4631da177e4SLinus Torvalds } 4641da177e4SLinus Torvalds 4651da177e4SLinus Torvalds /* If there is no association or if a source address is passed, no 4661da177e4SLinus Torvalds * more validation is required. 4671da177e4SLinus Torvalds */ 4681da177e4SLinus Torvalds if (!asoc || saddr) 4691da177e4SLinus Torvalds goto out; 4701da177e4SLinus Torvalds 4711da177e4SLinus Torvalds bp = &asoc->base.bind_addr; 4721da177e4SLinus Torvalds addr_lock = &asoc->base.addr_lock; 4731da177e4SLinus Torvalds 4741da177e4SLinus Torvalds if (dst) { 4751da177e4SLinus Torvalds /* Walk through the bind address list and look for a bind 4761da177e4SLinus Torvalds * address that matches the source address of the returned dst. 4771da177e4SLinus Torvalds */ 4781da177e4SLinus Torvalds sctp_read_lock(addr_lock); 4791da177e4SLinus Torvalds list_for_each(pos, &bp->address_list) { 4801da177e4SLinus Torvalds laddr = list_entry(pos, struct sctp_sockaddr_entry, 4811da177e4SLinus Torvalds list); 4821da177e4SLinus Torvalds sctp_v4_dst_saddr(&dst_saddr, dst, bp->port); 4831da177e4SLinus Torvalds if (sctp_v4_cmp_addr(&dst_saddr, &laddr->a)) 4841da177e4SLinus Torvalds goto out_unlock; 4851da177e4SLinus Torvalds } 4861da177e4SLinus Torvalds sctp_read_unlock(addr_lock); 4871da177e4SLinus Torvalds 4881da177e4SLinus Torvalds /* None of the bound addresses match the source address of the 4891da177e4SLinus Torvalds * dst. So release it. 4901da177e4SLinus Torvalds */ 4911da177e4SLinus Torvalds dst_release(dst); 4921da177e4SLinus Torvalds dst = NULL; 4931da177e4SLinus Torvalds } 4941da177e4SLinus Torvalds 4951da177e4SLinus Torvalds /* Walk through the bind address list and try to get a dst that 4961da177e4SLinus Torvalds * matches a bind address as the source address. 4971da177e4SLinus Torvalds */ 4981da177e4SLinus Torvalds sctp_read_lock(addr_lock); 4991da177e4SLinus Torvalds list_for_each(pos, &bp->address_list) { 5001da177e4SLinus Torvalds laddr = list_entry(pos, struct sctp_sockaddr_entry, list); 5011da177e4SLinus Torvalds 5021da177e4SLinus Torvalds if (AF_INET == laddr->a.sa.sa_family) { 5031da177e4SLinus Torvalds fl.fl4_src = laddr->a.v4.sin_addr.s_addr; 5041da177e4SLinus Torvalds if (!ip_route_output_key(&rt, &fl)) { 5051da177e4SLinus Torvalds dst = &rt->u.dst; 5061da177e4SLinus Torvalds goto out_unlock; 5071da177e4SLinus Torvalds } 5081da177e4SLinus Torvalds } 5091da177e4SLinus Torvalds } 5101da177e4SLinus Torvalds 5111da177e4SLinus Torvalds out_unlock: 5121da177e4SLinus Torvalds sctp_read_unlock(addr_lock); 5131da177e4SLinus Torvalds out: 5141da177e4SLinus Torvalds if (dst) 5151da177e4SLinus Torvalds SCTP_DEBUG_PRINTK("rt_dst:%u.%u.%u.%u, rt_src:%u.%u.%u.%u\n", 5161da177e4SLinus Torvalds NIPQUAD(rt->rt_dst), NIPQUAD(rt->rt_src)); 5171da177e4SLinus Torvalds else 5181da177e4SLinus Torvalds SCTP_DEBUG_PRINTK("NO ROUTE\n"); 5191da177e4SLinus Torvalds 5201da177e4SLinus Torvalds return dst; 5211da177e4SLinus Torvalds } 5221da177e4SLinus Torvalds 5231da177e4SLinus Torvalds /* For v4, the source address is cached in the route entry(dst). So no need 5241da177e4SLinus Torvalds * to cache it separately and hence this is an empty routine. 5251da177e4SLinus Torvalds */ 5261da177e4SLinus Torvalds static void sctp_v4_get_saddr(struct sctp_association *asoc, 5271da177e4SLinus Torvalds struct dst_entry *dst, 5281da177e4SLinus Torvalds union sctp_addr *daddr, 5291da177e4SLinus Torvalds union sctp_addr *saddr) 5301da177e4SLinus Torvalds { 5311da177e4SLinus Torvalds struct rtable *rt = (struct rtable *)dst; 5321da177e4SLinus Torvalds 533*23ec47a0SVladislav Yasevich if (!asoc) 534*23ec47a0SVladislav Yasevich return; 535*23ec47a0SVladislav Yasevich 5361da177e4SLinus Torvalds if (rt) { 5371da177e4SLinus Torvalds saddr->v4.sin_family = AF_INET; 5381da177e4SLinus Torvalds saddr->v4.sin_port = asoc->base.bind_addr.port; 5391da177e4SLinus Torvalds saddr->v4.sin_addr.s_addr = rt->rt_src; 5401da177e4SLinus Torvalds } 5411da177e4SLinus Torvalds } 5421da177e4SLinus Torvalds 5431da177e4SLinus Torvalds /* What interface did this skb arrive on? */ 5441da177e4SLinus Torvalds static int sctp_v4_skb_iif(const struct sk_buff *skb) 5451da177e4SLinus Torvalds { 5461da177e4SLinus Torvalds return ((struct rtable *)skb->dst)->rt_iif; 5471da177e4SLinus Torvalds } 5481da177e4SLinus Torvalds 5491da177e4SLinus Torvalds /* Was this packet marked by Explicit Congestion Notification? */ 5501da177e4SLinus Torvalds static int sctp_v4_is_ce(const struct sk_buff *skb) 5511da177e4SLinus Torvalds { 5521da177e4SLinus Torvalds return INET_ECN_is_ce(skb->nh.iph->tos); 5531da177e4SLinus Torvalds } 5541da177e4SLinus Torvalds 5551da177e4SLinus Torvalds /* Create and initialize a new sk for the socket returned by accept(). */ 5561da177e4SLinus Torvalds static struct sock *sctp_v4_create_accept_sk(struct sock *sk, 5571da177e4SLinus Torvalds struct sctp_association *asoc) 5581da177e4SLinus Torvalds { 5591da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 5601da177e4SLinus Torvalds struct inet_sock *newinet; 5611da177e4SLinus Torvalds struct sock *newsk = sk_alloc(PF_INET, GFP_KERNEL, sk->sk_prot, 1); 5621da177e4SLinus Torvalds 5631da177e4SLinus Torvalds if (!newsk) 5641da177e4SLinus Torvalds goto out; 5651da177e4SLinus Torvalds 5661da177e4SLinus Torvalds sock_init_data(NULL, newsk); 5671da177e4SLinus Torvalds 5681da177e4SLinus Torvalds newsk->sk_type = SOCK_STREAM; 5691da177e4SLinus Torvalds 5701da177e4SLinus Torvalds newsk->sk_no_check = sk->sk_no_check; 5711da177e4SLinus Torvalds newsk->sk_reuse = sk->sk_reuse; 5721da177e4SLinus Torvalds newsk->sk_shutdown = sk->sk_shutdown; 5731da177e4SLinus Torvalds 5741da177e4SLinus Torvalds newsk->sk_destruct = inet_sock_destruct; 5751da177e4SLinus Torvalds newsk->sk_family = PF_INET; 5761da177e4SLinus Torvalds newsk->sk_protocol = IPPROTO_SCTP; 5771da177e4SLinus Torvalds newsk->sk_backlog_rcv = sk->sk_prot->backlog_rcv; 5781da177e4SLinus Torvalds sock_reset_flag(newsk, SOCK_ZAPPED); 5791da177e4SLinus Torvalds 5801da177e4SLinus Torvalds newinet = inet_sk(newsk); 5811da177e4SLinus Torvalds 5821da177e4SLinus Torvalds /* Initialize sk's sport, dport, rcv_saddr and daddr for 5831da177e4SLinus Torvalds * getsockname() and getpeername() 5841da177e4SLinus Torvalds */ 5851da177e4SLinus Torvalds newinet->sport = inet->sport; 5861da177e4SLinus Torvalds newinet->saddr = inet->saddr; 5871da177e4SLinus Torvalds newinet->rcv_saddr = inet->rcv_saddr; 5881da177e4SLinus Torvalds newinet->dport = htons(asoc->peer.port); 5891da177e4SLinus Torvalds newinet->daddr = asoc->peer.primary_addr.v4.sin_addr.s_addr; 5901da177e4SLinus Torvalds newinet->pmtudisc = inet->pmtudisc; 5911da177e4SLinus Torvalds newinet->id = 0; 5921da177e4SLinus Torvalds 5931da177e4SLinus Torvalds newinet->uc_ttl = -1; 5941da177e4SLinus Torvalds newinet->mc_loop = 1; 5951da177e4SLinus Torvalds newinet->mc_ttl = 1; 5961da177e4SLinus Torvalds newinet->mc_index = 0; 5971da177e4SLinus Torvalds newinet->mc_list = NULL; 5981da177e4SLinus Torvalds 599e6848976SArnaldo Carvalho de Melo sk_refcnt_debug_inc(newsk); 6001da177e4SLinus Torvalds 6011da177e4SLinus Torvalds if (newsk->sk_prot->init(newsk)) { 6021da177e4SLinus Torvalds sk_common_release(newsk); 6031da177e4SLinus Torvalds newsk = NULL; 6041da177e4SLinus Torvalds } 6051da177e4SLinus Torvalds 6061da177e4SLinus Torvalds out: 6071da177e4SLinus Torvalds return newsk; 6081da177e4SLinus Torvalds } 6091da177e4SLinus Torvalds 6101da177e4SLinus Torvalds /* Map address, empty for v4 family */ 6111da177e4SLinus Torvalds static void sctp_v4_addr_v4map(struct sctp_sock *sp, union sctp_addr *addr) 6121da177e4SLinus Torvalds { 6131da177e4SLinus Torvalds /* Empty */ 6141da177e4SLinus Torvalds } 6151da177e4SLinus Torvalds 6161da177e4SLinus Torvalds /* Dump the v4 addr to the seq file. */ 6171da177e4SLinus Torvalds static void sctp_v4_seq_dump_addr(struct seq_file *seq, union sctp_addr *addr) 6181da177e4SLinus Torvalds { 6191da177e4SLinus Torvalds seq_printf(seq, "%d.%d.%d.%d ", NIPQUAD(addr->v4.sin_addr)); 6201da177e4SLinus Torvalds } 6211da177e4SLinus Torvalds 6221da177e4SLinus Torvalds /* Event handler for inet address addition/deletion events. 6231da177e4SLinus Torvalds * Basically, whenever there is an event, we re-build our local address list. 6241da177e4SLinus Torvalds */ 6251da177e4SLinus Torvalds int sctp_inetaddr_event(struct notifier_block *this, unsigned long ev, 6261da177e4SLinus Torvalds void *ptr) 6271da177e4SLinus Torvalds { 6281da177e4SLinus Torvalds unsigned long flags; 6291da177e4SLinus Torvalds 6301da177e4SLinus Torvalds sctp_spin_lock_irqsave(&sctp_local_addr_lock, flags); 6311da177e4SLinus Torvalds __sctp_free_local_addr_list(); 6321da177e4SLinus Torvalds __sctp_get_local_addr_list(); 6331da177e4SLinus Torvalds sctp_spin_unlock_irqrestore(&sctp_local_addr_lock, flags); 6341da177e4SLinus Torvalds 6351da177e4SLinus Torvalds return NOTIFY_DONE; 6361da177e4SLinus Torvalds } 6371da177e4SLinus Torvalds 6381da177e4SLinus Torvalds /* 6391da177e4SLinus Torvalds * Initialize the control inode/socket with a control endpoint data 6401da177e4SLinus Torvalds * structure. This endpoint is reserved exclusively for the OOTB processing. 6411da177e4SLinus Torvalds */ 6421da177e4SLinus Torvalds static int sctp_ctl_sock_init(void) 6431da177e4SLinus Torvalds { 6441da177e4SLinus Torvalds int err; 6451da177e4SLinus Torvalds sa_family_t family; 6461da177e4SLinus Torvalds 6471da177e4SLinus Torvalds if (sctp_get_pf_specific(PF_INET6)) 6481da177e4SLinus Torvalds family = PF_INET6; 6491da177e4SLinus Torvalds else 6501da177e4SLinus Torvalds family = PF_INET; 6511da177e4SLinus Torvalds 6521da177e4SLinus Torvalds err = sock_create_kern(family, SOCK_SEQPACKET, IPPROTO_SCTP, 6531da177e4SLinus Torvalds &sctp_ctl_socket); 6541da177e4SLinus Torvalds if (err < 0) { 6551da177e4SLinus Torvalds printk(KERN_ERR 6561da177e4SLinus Torvalds "SCTP: Failed to create the SCTP control socket.\n"); 6571da177e4SLinus Torvalds return err; 6581da177e4SLinus Torvalds } 6591da177e4SLinus Torvalds sctp_ctl_socket->sk->sk_allocation = GFP_ATOMIC; 6601da177e4SLinus Torvalds inet_sk(sctp_ctl_socket->sk)->uc_ttl = -1; 6611da177e4SLinus Torvalds 6621da177e4SLinus Torvalds return 0; 6631da177e4SLinus Torvalds } 6641da177e4SLinus Torvalds 6651da177e4SLinus Torvalds /* Register address family specific functions. */ 6661da177e4SLinus Torvalds int sctp_register_af(struct sctp_af *af) 6671da177e4SLinus Torvalds { 6681da177e4SLinus Torvalds switch (af->sa_family) { 6691da177e4SLinus Torvalds case AF_INET: 6701da177e4SLinus Torvalds if (sctp_af_v4_specific) 6711da177e4SLinus Torvalds return 0; 6721da177e4SLinus Torvalds sctp_af_v4_specific = af; 6731da177e4SLinus Torvalds break; 6741da177e4SLinus Torvalds case AF_INET6: 6751da177e4SLinus Torvalds if (sctp_af_v6_specific) 6761da177e4SLinus Torvalds return 0; 6771da177e4SLinus Torvalds sctp_af_v6_specific = af; 6781da177e4SLinus Torvalds break; 6791da177e4SLinus Torvalds default: 6801da177e4SLinus Torvalds return 0; 6811da177e4SLinus Torvalds } 6821da177e4SLinus Torvalds 6831da177e4SLinus Torvalds INIT_LIST_HEAD(&af->list); 6841da177e4SLinus Torvalds list_add_tail(&af->list, &sctp_address_families); 6851da177e4SLinus Torvalds return 1; 6861da177e4SLinus Torvalds } 6871da177e4SLinus Torvalds 6881da177e4SLinus Torvalds /* Get the table of functions for manipulating a particular address 6891da177e4SLinus Torvalds * family. 6901da177e4SLinus Torvalds */ 6911da177e4SLinus Torvalds struct sctp_af *sctp_get_af_specific(sa_family_t family) 6921da177e4SLinus Torvalds { 6931da177e4SLinus Torvalds switch (family) { 6941da177e4SLinus Torvalds case AF_INET: 6951da177e4SLinus Torvalds return sctp_af_v4_specific; 6961da177e4SLinus Torvalds case AF_INET6: 6971da177e4SLinus Torvalds return sctp_af_v6_specific; 6981da177e4SLinus Torvalds default: 6991da177e4SLinus Torvalds return NULL; 7001da177e4SLinus Torvalds } 7011da177e4SLinus Torvalds } 7021da177e4SLinus Torvalds 7031da177e4SLinus Torvalds /* Common code to initialize a AF_INET msg_name. */ 7041da177e4SLinus Torvalds static void sctp_inet_msgname(char *msgname, int *addr_len) 7051da177e4SLinus Torvalds { 7061da177e4SLinus Torvalds struct sockaddr_in *sin; 7071da177e4SLinus Torvalds 7081da177e4SLinus Torvalds sin = (struct sockaddr_in *)msgname; 7091da177e4SLinus Torvalds *addr_len = sizeof(struct sockaddr_in); 7101da177e4SLinus Torvalds sin->sin_family = AF_INET; 7111da177e4SLinus Torvalds memset(sin->sin_zero, 0, sizeof(sin->sin_zero)); 7121da177e4SLinus Torvalds } 7131da177e4SLinus Torvalds 7141da177e4SLinus Torvalds /* Copy the primary address of the peer primary address as the msg_name. */ 7151da177e4SLinus Torvalds static void sctp_inet_event_msgname(struct sctp_ulpevent *event, char *msgname, 7161da177e4SLinus Torvalds int *addr_len) 7171da177e4SLinus Torvalds { 7181da177e4SLinus Torvalds struct sockaddr_in *sin, *sinfrom; 7191da177e4SLinus Torvalds 7201da177e4SLinus Torvalds if (msgname) { 7211da177e4SLinus Torvalds struct sctp_association *asoc; 7221da177e4SLinus Torvalds 7231da177e4SLinus Torvalds asoc = event->asoc; 7241da177e4SLinus Torvalds sctp_inet_msgname(msgname, addr_len); 7251da177e4SLinus Torvalds sin = (struct sockaddr_in *)msgname; 7261da177e4SLinus Torvalds sinfrom = &asoc->peer.primary_addr.v4; 7271da177e4SLinus Torvalds sin->sin_port = htons(asoc->peer.port); 7281da177e4SLinus Torvalds sin->sin_addr.s_addr = sinfrom->sin_addr.s_addr; 7291da177e4SLinus Torvalds } 7301da177e4SLinus Torvalds } 7311da177e4SLinus Torvalds 7321da177e4SLinus Torvalds /* Initialize and copy out a msgname from an inbound skb. */ 7331da177e4SLinus Torvalds static void sctp_inet_skb_msgname(struct sk_buff *skb, char *msgname, int *len) 7341da177e4SLinus Torvalds { 7351da177e4SLinus Torvalds struct sctphdr *sh; 7361da177e4SLinus Torvalds struct sockaddr_in *sin; 7371da177e4SLinus Torvalds 7381da177e4SLinus Torvalds if (msgname) { 7391da177e4SLinus Torvalds sctp_inet_msgname(msgname, len); 7401da177e4SLinus Torvalds sin = (struct sockaddr_in *)msgname; 7411da177e4SLinus Torvalds sh = (struct sctphdr *)skb->h.raw; 7421da177e4SLinus Torvalds sin->sin_port = sh->source; 7431da177e4SLinus Torvalds sin->sin_addr.s_addr = skb->nh.iph->saddr; 7441da177e4SLinus Torvalds } 7451da177e4SLinus Torvalds } 7461da177e4SLinus Torvalds 7471da177e4SLinus Torvalds /* Do we support this AF? */ 7481da177e4SLinus Torvalds static int sctp_inet_af_supported(sa_family_t family, struct sctp_sock *sp) 7491da177e4SLinus Torvalds { 7501da177e4SLinus Torvalds /* PF_INET only supports AF_INET addresses. */ 7511da177e4SLinus Torvalds return (AF_INET == family); 7521da177e4SLinus Torvalds } 7531da177e4SLinus Torvalds 7541da177e4SLinus Torvalds /* Address matching with wildcards allowed. */ 7551da177e4SLinus Torvalds static int sctp_inet_cmp_addr(const union sctp_addr *addr1, 7561da177e4SLinus Torvalds const union sctp_addr *addr2, 7571da177e4SLinus Torvalds struct sctp_sock *opt) 7581da177e4SLinus Torvalds { 7591da177e4SLinus Torvalds /* PF_INET only supports AF_INET addresses. */ 7601da177e4SLinus Torvalds if (addr1->sa.sa_family != addr2->sa.sa_family) 7611da177e4SLinus Torvalds return 0; 7621da177e4SLinus Torvalds if (INADDR_ANY == addr1->v4.sin_addr.s_addr || 7631da177e4SLinus Torvalds INADDR_ANY == addr2->v4.sin_addr.s_addr) 7641da177e4SLinus Torvalds return 1; 7651da177e4SLinus Torvalds if (addr1->v4.sin_addr.s_addr == addr2->v4.sin_addr.s_addr) 7661da177e4SLinus Torvalds return 1; 7671da177e4SLinus Torvalds 7681da177e4SLinus Torvalds return 0; 7691da177e4SLinus Torvalds } 7701da177e4SLinus Torvalds 7711da177e4SLinus Torvalds /* Verify that provided sockaddr looks bindable. Common verification has 7721da177e4SLinus Torvalds * already been taken care of. 7731da177e4SLinus Torvalds */ 7741da177e4SLinus Torvalds static int sctp_inet_bind_verify(struct sctp_sock *opt, union sctp_addr *addr) 7751da177e4SLinus Torvalds { 7761da177e4SLinus Torvalds return sctp_v4_available(addr, opt); 7771da177e4SLinus Torvalds } 7781da177e4SLinus Torvalds 7791da177e4SLinus Torvalds /* Verify that sockaddr looks sendable. Common verification has already 7801da177e4SLinus Torvalds * been taken care of. 7811da177e4SLinus Torvalds */ 7821da177e4SLinus Torvalds static int sctp_inet_send_verify(struct sctp_sock *opt, union sctp_addr *addr) 7831da177e4SLinus Torvalds { 7841da177e4SLinus Torvalds return 1; 7851da177e4SLinus Torvalds } 7861da177e4SLinus Torvalds 7871da177e4SLinus Torvalds /* Fill in Supported Address Type information for INIT and INIT-ACK 7881da177e4SLinus Torvalds * chunks. Returns number of addresses supported. 7891da177e4SLinus Torvalds */ 7901da177e4SLinus Torvalds static int sctp_inet_supported_addrs(const struct sctp_sock *opt, 7911da177e4SLinus Torvalds __u16 *types) 7921da177e4SLinus Torvalds { 7931da177e4SLinus Torvalds types[0] = SCTP_PARAM_IPV4_ADDRESS; 7941da177e4SLinus Torvalds return 1; 7951da177e4SLinus Torvalds } 7961da177e4SLinus Torvalds 7971da177e4SLinus Torvalds /* Wrapper routine that calls the ip transmit routine. */ 7981da177e4SLinus Torvalds static inline int sctp_v4_xmit(struct sk_buff *skb, 7991da177e4SLinus Torvalds struct sctp_transport *transport, int ipfragok) 8001da177e4SLinus Torvalds { 8011da177e4SLinus Torvalds SCTP_DEBUG_PRINTK("%s: skb:%p, len:%d, " 8021da177e4SLinus Torvalds "src:%u.%u.%u.%u, dst:%u.%u.%u.%u\n", 8031da177e4SLinus Torvalds __FUNCTION__, skb, skb->len, 8041da177e4SLinus Torvalds NIPQUAD(((struct rtable *)skb->dst)->rt_src), 8051da177e4SLinus Torvalds NIPQUAD(((struct rtable *)skb->dst)->rt_dst)); 8061da177e4SLinus Torvalds 8071da177e4SLinus Torvalds SCTP_INC_STATS(SCTP_MIB_OUTSCTPPACKS); 8081da177e4SLinus Torvalds return ip_queue_xmit(skb, ipfragok); 8091da177e4SLinus Torvalds } 8101da177e4SLinus Torvalds 8111da177e4SLinus Torvalds static struct sctp_af sctp_ipv4_specific; 8121da177e4SLinus Torvalds 8131da177e4SLinus Torvalds static struct sctp_pf sctp_pf_inet = { 8141da177e4SLinus Torvalds .event_msgname = sctp_inet_event_msgname, 8151da177e4SLinus Torvalds .skb_msgname = sctp_inet_skb_msgname, 8161da177e4SLinus Torvalds .af_supported = sctp_inet_af_supported, 8171da177e4SLinus Torvalds .cmp_addr = sctp_inet_cmp_addr, 8181da177e4SLinus Torvalds .bind_verify = sctp_inet_bind_verify, 8191da177e4SLinus Torvalds .send_verify = sctp_inet_send_verify, 8201da177e4SLinus Torvalds .supported_addrs = sctp_inet_supported_addrs, 8211da177e4SLinus Torvalds .create_accept_sk = sctp_v4_create_accept_sk, 8221da177e4SLinus Torvalds .addr_v4map = sctp_v4_addr_v4map, 8231da177e4SLinus Torvalds .af = &sctp_ipv4_specific, 8241da177e4SLinus Torvalds }; 8251da177e4SLinus Torvalds 8261da177e4SLinus Torvalds /* Notifier for inetaddr addition/deletion events. */ 8271da177e4SLinus Torvalds static struct notifier_block sctp_inetaddr_notifier = { 8281da177e4SLinus Torvalds .notifier_call = sctp_inetaddr_event, 8291da177e4SLinus Torvalds }; 8301da177e4SLinus Torvalds 8311da177e4SLinus Torvalds /* Socket operations. */ 8321da177e4SLinus Torvalds static struct proto_ops inet_seqpacket_ops = { 8331da177e4SLinus Torvalds .family = PF_INET, 8341da177e4SLinus Torvalds .owner = THIS_MODULE, 8351da177e4SLinus Torvalds .release = inet_release, /* Needs to be wrapped... */ 8361da177e4SLinus Torvalds .bind = inet_bind, 8371da177e4SLinus Torvalds .connect = inet_dgram_connect, 8381da177e4SLinus Torvalds .socketpair = sock_no_socketpair, 8391da177e4SLinus Torvalds .accept = inet_accept, 8401da177e4SLinus Torvalds .getname = inet_getname, /* Semantics are different. */ 8411da177e4SLinus Torvalds .poll = sctp_poll, 8421da177e4SLinus Torvalds .ioctl = inet_ioctl, 8431da177e4SLinus Torvalds .listen = sctp_inet_listen, 8441da177e4SLinus Torvalds .shutdown = inet_shutdown, /* Looks harmless. */ 8451da177e4SLinus Torvalds .setsockopt = sock_common_setsockopt, /* IP_SOL IP_OPTION is a problem. */ 8461da177e4SLinus Torvalds .getsockopt = sock_common_getsockopt, 8471da177e4SLinus Torvalds .sendmsg = inet_sendmsg, 8481da177e4SLinus Torvalds .recvmsg = sock_common_recvmsg, 8491da177e4SLinus Torvalds .mmap = sock_no_mmap, 8501da177e4SLinus Torvalds .sendpage = sock_no_sendpage, 8511da177e4SLinus Torvalds }; 8521da177e4SLinus Torvalds 8531da177e4SLinus Torvalds /* Registration with AF_INET family. */ 8541da177e4SLinus Torvalds static struct inet_protosw sctp_seqpacket_protosw = { 8551da177e4SLinus Torvalds .type = SOCK_SEQPACKET, 8561da177e4SLinus Torvalds .protocol = IPPROTO_SCTP, 8571da177e4SLinus Torvalds .prot = &sctp_prot, 8581da177e4SLinus Torvalds .ops = &inet_seqpacket_ops, 8591da177e4SLinus Torvalds .capability = -1, 8601da177e4SLinus Torvalds .no_check = 0, 8611da177e4SLinus Torvalds .flags = SCTP_PROTOSW_FLAG 8621da177e4SLinus Torvalds }; 8631da177e4SLinus Torvalds static struct inet_protosw sctp_stream_protosw = { 8641da177e4SLinus Torvalds .type = SOCK_STREAM, 8651da177e4SLinus Torvalds .protocol = IPPROTO_SCTP, 8661da177e4SLinus Torvalds .prot = &sctp_prot, 8671da177e4SLinus Torvalds .ops = &inet_seqpacket_ops, 8681da177e4SLinus Torvalds .capability = -1, 8691da177e4SLinus Torvalds .no_check = 0, 8701da177e4SLinus Torvalds .flags = SCTP_PROTOSW_FLAG 8711da177e4SLinus Torvalds }; 8721da177e4SLinus Torvalds 8731da177e4SLinus Torvalds /* Register with IP layer. */ 8741da177e4SLinus Torvalds static struct net_protocol sctp_protocol = { 8751da177e4SLinus Torvalds .handler = sctp_rcv, 8761da177e4SLinus Torvalds .err_handler = sctp_v4_err, 8771da177e4SLinus Torvalds .no_policy = 1, 8781da177e4SLinus Torvalds }; 8791da177e4SLinus Torvalds 8801da177e4SLinus Torvalds /* IPv4 address related functions. */ 8811da177e4SLinus Torvalds static struct sctp_af sctp_ipv4_specific = { 8821da177e4SLinus Torvalds .sctp_xmit = sctp_v4_xmit, 8831da177e4SLinus Torvalds .setsockopt = ip_setsockopt, 8841da177e4SLinus Torvalds .getsockopt = ip_getsockopt, 8851da177e4SLinus Torvalds .get_dst = sctp_v4_get_dst, 8861da177e4SLinus Torvalds .get_saddr = sctp_v4_get_saddr, 8871da177e4SLinus Torvalds .copy_addrlist = sctp_v4_copy_addrlist, 8881da177e4SLinus Torvalds .from_skb = sctp_v4_from_skb, 8891da177e4SLinus Torvalds .from_sk = sctp_v4_from_sk, 8901da177e4SLinus Torvalds .to_sk_saddr = sctp_v4_to_sk_saddr, 8911da177e4SLinus Torvalds .to_sk_daddr = sctp_v4_to_sk_daddr, 8921da177e4SLinus Torvalds .from_addr_param= sctp_v4_from_addr_param, 8931da177e4SLinus Torvalds .to_addr_param = sctp_v4_to_addr_param, 8941da177e4SLinus Torvalds .dst_saddr = sctp_v4_dst_saddr, 8951da177e4SLinus Torvalds .cmp_addr = sctp_v4_cmp_addr, 8961da177e4SLinus Torvalds .addr_valid = sctp_v4_addr_valid, 8971da177e4SLinus Torvalds .inaddr_any = sctp_v4_inaddr_any, 8981da177e4SLinus Torvalds .is_any = sctp_v4_is_any, 8991da177e4SLinus Torvalds .available = sctp_v4_available, 9001da177e4SLinus Torvalds .scope = sctp_v4_scope, 9011da177e4SLinus Torvalds .skb_iif = sctp_v4_skb_iif, 9021da177e4SLinus Torvalds .is_ce = sctp_v4_is_ce, 9031da177e4SLinus Torvalds .seq_dump_addr = sctp_v4_seq_dump_addr, 9041da177e4SLinus Torvalds .net_header_len = sizeof(struct iphdr), 9051da177e4SLinus Torvalds .sockaddr_len = sizeof(struct sockaddr_in), 9061da177e4SLinus Torvalds .sa_family = AF_INET, 9071da177e4SLinus Torvalds }; 9081da177e4SLinus Torvalds 9091da177e4SLinus Torvalds struct sctp_pf *sctp_get_pf_specific(sa_family_t family) { 9101da177e4SLinus Torvalds 9111da177e4SLinus Torvalds switch (family) { 9121da177e4SLinus Torvalds case PF_INET: 9131da177e4SLinus Torvalds return sctp_pf_inet_specific; 9141da177e4SLinus Torvalds case PF_INET6: 9151da177e4SLinus Torvalds return sctp_pf_inet6_specific; 9161da177e4SLinus Torvalds default: 9171da177e4SLinus Torvalds return NULL; 9181da177e4SLinus Torvalds } 9191da177e4SLinus Torvalds } 9201da177e4SLinus Torvalds 9211da177e4SLinus Torvalds /* Register the PF specific function table. */ 9221da177e4SLinus Torvalds int sctp_register_pf(struct sctp_pf *pf, sa_family_t family) 9231da177e4SLinus Torvalds { 9241da177e4SLinus Torvalds switch (family) { 9251da177e4SLinus Torvalds case PF_INET: 9261da177e4SLinus Torvalds if (sctp_pf_inet_specific) 9271da177e4SLinus Torvalds return 0; 9281da177e4SLinus Torvalds sctp_pf_inet_specific = pf; 9291da177e4SLinus Torvalds break; 9301da177e4SLinus Torvalds case PF_INET6: 9311da177e4SLinus Torvalds if (sctp_pf_inet6_specific) 9321da177e4SLinus Torvalds return 0; 9331da177e4SLinus Torvalds sctp_pf_inet6_specific = pf; 9341da177e4SLinus Torvalds break; 9351da177e4SLinus Torvalds default: 9361da177e4SLinus Torvalds return 0; 9371da177e4SLinus Torvalds } 9381da177e4SLinus Torvalds return 1; 9391da177e4SLinus Torvalds } 9401da177e4SLinus Torvalds 9411da177e4SLinus Torvalds static int __init init_sctp_mibs(void) 9421da177e4SLinus Torvalds { 9431da177e4SLinus Torvalds sctp_statistics[0] = alloc_percpu(struct sctp_mib); 9441da177e4SLinus Torvalds if (!sctp_statistics[0]) 9451da177e4SLinus Torvalds return -ENOMEM; 9461da177e4SLinus Torvalds sctp_statistics[1] = alloc_percpu(struct sctp_mib); 9471da177e4SLinus Torvalds if (!sctp_statistics[1]) { 9481da177e4SLinus Torvalds free_percpu(sctp_statistics[0]); 9491da177e4SLinus Torvalds return -ENOMEM; 9501da177e4SLinus Torvalds } 9511da177e4SLinus Torvalds return 0; 9521da177e4SLinus Torvalds 9531da177e4SLinus Torvalds } 9541da177e4SLinus Torvalds 9551da177e4SLinus Torvalds static void cleanup_sctp_mibs(void) 9561da177e4SLinus Torvalds { 9571da177e4SLinus Torvalds free_percpu(sctp_statistics[0]); 9581da177e4SLinus Torvalds free_percpu(sctp_statistics[1]); 9591da177e4SLinus Torvalds } 9601da177e4SLinus Torvalds 9611da177e4SLinus Torvalds /* Initialize the universe into something sensible. */ 9621da177e4SLinus Torvalds SCTP_STATIC __init int sctp_init(void) 9631da177e4SLinus Torvalds { 9641da177e4SLinus Torvalds int i; 9651da177e4SLinus Torvalds int status = -EINVAL; 9661da177e4SLinus Torvalds unsigned long goal; 9671da177e4SLinus Torvalds int order; 9681da177e4SLinus Torvalds 9691da177e4SLinus Torvalds /* SCTP_DEBUG sanity check. */ 9701da177e4SLinus Torvalds if (!sctp_sanity_check()) 9711da177e4SLinus Torvalds goto out; 9721da177e4SLinus Torvalds 9731da177e4SLinus Torvalds status = proto_register(&sctp_prot, 1); 9741da177e4SLinus Torvalds if (status) 9751da177e4SLinus Torvalds goto out; 9761da177e4SLinus Torvalds 9771da177e4SLinus Torvalds /* Add SCTP to inet_protos hash table. */ 9781da177e4SLinus Torvalds status = -EAGAIN; 9791da177e4SLinus Torvalds if (inet_add_protocol(&sctp_protocol, IPPROTO_SCTP) < 0) 9801da177e4SLinus Torvalds goto err_add_protocol; 9811da177e4SLinus Torvalds 9821da177e4SLinus Torvalds /* Add SCTP(TCP and UDP style) to inetsw linked list. */ 9831da177e4SLinus Torvalds inet_register_protosw(&sctp_seqpacket_protosw); 9841da177e4SLinus Torvalds inet_register_protosw(&sctp_stream_protosw); 9851da177e4SLinus Torvalds 9861da177e4SLinus Torvalds /* Allocate a cache pools. */ 9871da177e4SLinus Torvalds status = -ENOBUFS; 9881da177e4SLinus Torvalds sctp_bucket_cachep = kmem_cache_create("sctp_bind_bucket", 9891da177e4SLinus Torvalds sizeof(struct sctp_bind_bucket), 9901da177e4SLinus Torvalds 0, SLAB_HWCACHE_ALIGN, 9911da177e4SLinus Torvalds NULL, NULL); 9921da177e4SLinus Torvalds 9931da177e4SLinus Torvalds if (!sctp_bucket_cachep) 9941da177e4SLinus Torvalds goto err_bucket_cachep; 9951da177e4SLinus Torvalds 9961da177e4SLinus Torvalds sctp_chunk_cachep = kmem_cache_create("sctp_chunk", 9971da177e4SLinus Torvalds sizeof(struct sctp_chunk), 9981da177e4SLinus Torvalds 0, SLAB_HWCACHE_ALIGN, 9991da177e4SLinus Torvalds NULL, NULL); 10001da177e4SLinus Torvalds if (!sctp_chunk_cachep) 10011da177e4SLinus Torvalds goto err_chunk_cachep; 10021da177e4SLinus Torvalds 10031da177e4SLinus Torvalds /* Allocate and initialise sctp mibs. */ 10041da177e4SLinus Torvalds status = init_sctp_mibs(); 10051da177e4SLinus Torvalds if (status) 10061da177e4SLinus Torvalds goto err_init_mibs; 10071da177e4SLinus Torvalds 10081da177e4SLinus Torvalds /* Initialize proc fs directory. */ 10091da177e4SLinus Torvalds status = sctp_proc_init(); 10101da177e4SLinus Torvalds if (status) 10111da177e4SLinus Torvalds goto err_init_proc; 10121da177e4SLinus Torvalds 10131da177e4SLinus Torvalds /* Initialize object count debugging. */ 10141da177e4SLinus Torvalds sctp_dbg_objcnt_init(); 10151da177e4SLinus Torvalds 10161da177e4SLinus Torvalds /* Initialize the SCTP specific PF functions. */ 10171da177e4SLinus Torvalds sctp_register_pf(&sctp_pf_inet, PF_INET); 10181da177e4SLinus Torvalds /* 10191da177e4SLinus Torvalds * 14. Suggested SCTP Protocol Parameter Values 10201da177e4SLinus Torvalds */ 10211da177e4SLinus Torvalds /* The following protocol parameters are RECOMMENDED: */ 10221da177e4SLinus Torvalds /* RTO.Initial - 3 seconds */ 10231da177e4SLinus Torvalds sctp_rto_initial = SCTP_RTO_INITIAL; 10241da177e4SLinus Torvalds /* RTO.Min - 1 second */ 10251da177e4SLinus Torvalds sctp_rto_min = SCTP_RTO_MIN; 10261da177e4SLinus Torvalds /* RTO.Max - 60 seconds */ 10271da177e4SLinus Torvalds sctp_rto_max = SCTP_RTO_MAX; 10281da177e4SLinus Torvalds /* RTO.Alpha - 1/8 */ 10291da177e4SLinus Torvalds sctp_rto_alpha = SCTP_RTO_ALPHA; 10301da177e4SLinus Torvalds /* RTO.Beta - 1/4 */ 10311da177e4SLinus Torvalds sctp_rto_beta = SCTP_RTO_BETA; 10321da177e4SLinus Torvalds 10331da177e4SLinus Torvalds /* Valid.Cookie.Life - 60 seconds */ 10341da177e4SLinus Torvalds sctp_valid_cookie_life = 60 * HZ; 10351da177e4SLinus Torvalds 10361da177e4SLinus Torvalds /* Whether Cookie Preservative is enabled(1) or not(0) */ 10371da177e4SLinus Torvalds sctp_cookie_preserve_enable = 1; 10381da177e4SLinus Torvalds 10391da177e4SLinus Torvalds /* Max.Burst - 4 */ 10401da177e4SLinus Torvalds sctp_max_burst = SCTP_MAX_BURST; 10411da177e4SLinus Torvalds 10421da177e4SLinus Torvalds /* Association.Max.Retrans - 10 attempts 10431da177e4SLinus Torvalds * Path.Max.Retrans - 5 attempts (per destination address) 10441da177e4SLinus Torvalds * Max.Init.Retransmits - 8 attempts 10451da177e4SLinus Torvalds */ 10461da177e4SLinus Torvalds sctp_max_retrans_association = 10; 10471da177e4SLinus Torvalds sctp_max_retrans_path = 5; 10481da177e4SLinus Torvalds sctp_max_retrans_init = 8; 10491da177e4SLinus Torvalds 10504eb701dfSNeil Horman /* Sendbuffer growth - do per-socket accounting */ 10514eb701dfSNeil Horman sctp_sndbuf_policy = 0; 10524eb701dfSNeil Horman 10531da177e4SLinus Torvalds /* HB.interval - 30 seconds */ 10542f85a429SVlad Yasevich sctp_hb_interval = SCTP_DEFAULT_TIMEOUT_HEARTBEAT; 10552f85a429SVlad Yasevich 10562f85a429SVlad Yasevich /* delayed SACK timeout */ 10572f85a429SVlad Yasevich sctp_sack_timeout = SCTP_DEFAULT_TIMEOUT_SACK; 10581da177e4SLinus Torvalds 10591da177e4SLinus Torvalds /* Implementation specific variables. */ 10601da177e4SLinus Torvalds 10611da177e4SLinus Torvalds /* Initialize default stream count setup information. */ 10621da177e4SLinus Torvalds sctp_max_instreams = SCTP_DEFAULT_INSTREAMS; 10631da177e4SLinus Torvalds sctp_max_outstreams = SCTP_DEFAULT_OUTSTREAMS; 10641da177e4SLinus Torvalds 10651da177e4SLinus Torvalds /* Initialize handle used for association ids. */ 10661da177e4SLinus Torvalds idr_init(&sctp_assocs_id); 10671da177e4SLinus Torvalds 10681da177e4SLinus Torvalds /* Size and allocate the association hash table. 10691da177e4SLinus Torvalds * The methodology is similar to that of the tcp hash tables. 10701da177e4SLinus Torvalds */ 10711da177e4SLinus Torvalds if (num_physpages >= (128 * 1024)) 10721da177e4SLinus Torvalds goal = num_physpages >> (22 - PAGE_SHIFT); 10731da177e4SLinus Torvalds else 10741da177e4SLinus Torvalds goal = num_physpages >> (24 - PAGE_SHIFT); 10751da177e4SLinus Torvalds 10761da177e4SLinus Torvalds for (order = 0; (1UL << order) < goal; order++) 10771da177e4SLinus Torvalds ; 10781da177e4SLinus Torvalds 10791da177e4SLinus Torvalds do { 10801da177e4SLinus Torvalds sctp_assoc_hashsize = (1UL << order) * PAGE_SIZE / 10811da177e4SLinus Torvalds sizeof(struct sctp_hashbucket); 10821da177e4SLinus Torvalds if ((sctp_assoc_hashsize > (64 * 1024)) && order > 0) 10831da177e4SLinus Torvalds continue; 10841da177e4SLinus Torvalds sctp_assoc_hashtable = (struct sctp_hashbucket *) 10851da177e4SLinus Torvalds __get_free_pages(GFP_ATOMIC, order); 10861da177e4SLinus Torvalds } while (!sctp_assoc_hashtable && --order > 0); 10871da177e4SLinus Torvalds if (!sctp_assoc_hashtable) { 10881da177e4SLinus Torvalds printk(KERN_ERR "SCTP: Failed association hash alloc.\n"); 10891da177e4SLinus Torvalds status = -ENOMEM; 10901da177e4SLinus Torvalds goto err_ahash_alloc; 10911da177e4SLinus Torvalds } 10921da177e4SLinus Torvalds for (i = 0; i < sctp_assoc_hashsize; i++) { 10931da177e4SLinus Torvalds rwlock_init(&sctp_assoc_hashtable[i].lock); 10941da177e4SLinus Torvalds sctp_assoc_hashtable[i].chain = NULL; 10951da177e4SLinus Torvalds } 10961da177e4SLinus Torvalds 10971da177e4SLinus Torvalds /* Allocate and initialize the endpoint hash table. */ 10981da177e4SLinus Torvalds sctp_ep_hashsize = 64; 10991da177e4SLinus Torvalds sctp_ep_hashtable = (struct sctp_hashbucket *) 11001da177e4SLinus Torvalds kmalloc(64 * sizeof(struct sctp_hashbucket), GFP_KERNEL); 11011da177e4SLinus Torvalds if (!sctp_ep_hashtable) { 11021da177e4SLinus Torvalds printk(KERN_ERR "SCTP: Failed endpoint_hash alloc.\n"); 11031da177e4SLinus Torvalds status = -ENOMEM; 11041da177e4SLinus Torvalds goto err_ehash_alloc; 11051da177e4SLinus Torvalds } 11061da177e4SLinus Torvalds for (i = 0; i < sctp_ep_hashsize; i++) { 11071da177e4SLinus Torvalds rwlock_init(&sctp_ep_hashtable[i].lock); 11081da177e4SLinus Torvalds sctp_ep_hashtable[i].chain = NULL; 11091da177e4SLinus Torvalds } 11101da177e4SLinus Torvalds 11111da177e4SLinus Torvalds /* Allocate and initialize the SCTP port hash table. */ 11121da177e4SLinus Torvalds do { 11131da177e4SLinus Torvalds sctp_port_hashsize = (1UL << order) * PAGE_SIZE / 11141da177e4SLinus Torvalds sizeof(struct sctp_bind_hashbucket); 11151da177e4SLinus Torvalds if ((sctp_port_hashsize > (64 * 1024)) && order > 0) 11161da177e4SLinus Torvalds continue; 11171da177e4SLinus Torvalds sctp_port_hashtable = (struct sctp_bind_hashbucket *) 11181da177e4SLinus Torvalds __get_free_pages(GFP_ATOMIC, order); 11191da177e4SLinus Torvalds } while (!sctp_port_hashtable && --order > 0); 11201da177e4SLinus Torvalds if (!sctp_port_hashtable) { 11211da177e4SLinus Torvalds printk(KERN_ERR "SCTP: Failed bind hash alloc."); 11221da177e4SLinus Torvalds status = -ENOMEM; 11231da177e4SLinus Torvalds goto err_bhash_alloc; 11241da177e4SLinus Torvalds } 11251da177e4SLinus Torvalds for (i = 0; i < sctp_port_hashsize; i++) { 11261da177e4SLinus Torvalds spin_lock_init(&sctp_port_hashtable[i].lock); 11271da177e4SLinus Torvalds sctp_port_hashtable[i].chain = NULL; 11281da177e4SLinus Torvalds } 11291da177e4SLinus Torvalds 11301da177e4SLinus Torvalds spin_lock_init(&sctp_port_alloc_lock); 11311da177e4SLinus Torvalds sctp_port_rover = sysctl_local_port_range[0] - 1; 11321da177e4SLinus Torvalds 11331da177e4SLinus Torvalds printk(KERN_INFO "SCTP: Hash tables configured " 11341da177e4SLinus Torvalds "(established %d bind %d)\n", 11351da177e4SLinus Torvalds sctp_assoc_hashsize, sctp_port_hashsize); 11361da177e4SLinus Torvalds 11371da177e4SLinus Torvalds /* Disable ADDIP by default. */ 11381da177e4SLinus Torvalds sctp_addip_enable = 0; 11391da177e4SLinus Torvalds 11401da177e4SLinus Torvalds /* Enable PR-SCTP by default. */ 11411da177e4SLinus Torvalds sctp_prsctp_enable = 1; 11421da177e4SLinus Torvalds 11431da177e4SLinus Torvalds sctp_sysctl_register(); 11441da177e4SLinus Torvalds 11451da177e4SLinus Torvalds INIT_LIST_HEAD(&sctp_address_families); 11461da177e4SLinus Torvalds sctp_register_af(&sctp_ipv4_specific); 11471da177e4SLinus Torvalds 11481da177e4SLinus Torvalds status = sctp_v6_init(); 11491da177e4SLinus Torvalds if (status) 11501da177e4SLinus Torvalds goto err_v6_init; 11511da177e4SLinus Torvalds 11521da177e4SLinus Torvalds /* Initialize the control inode/socket for handling OOTB packets. */ 11531da177e4SLinus Torvalds if ((status = sctp_ctl_sock_init())) { 11541da177e4SLinus Torvalds printk (KERN_ERR 11551da177e4SLinus Torvalds "SCTP: Failed to initialize the SCTP control sock.\n"); 11561da177e4SLinus Torvalds goto err_ctl_sock_init; 11571da177e4SLinus Torvalds } 11581da177e4SLinus Torvalds 11591da177e4SLinus Torvalds /* Initialize the local address list. */ 11601da177e4SLinus Torvalds INIT_LIST_HEAD(&sctp_local_addr_list); 11611da177e4SLinus Torvalds spin_lock_init(&sctp_local_addr_lock); 11621da177e4SLinus Torvalds 11631da177e4SLinus Torvalds /* Register notifier for inet address additions/deletions. */ 11641da177e4SLinus Torvalds register_inetaddr_notifier(&sctp_inetaddr_notifier); 11651da177e4SLinus Torvalds 11661da177e4SLinus Torvalds sctp_get_local_addr_list(); 11671da177e4SLinus Torvalds 11681da177e4SLinus Torvalds __unsafe(THIS_MODULE); 11691da177e4SLinus Torvalds status = 0; 11701da177e4SLinus Torvalds out: 11711da177e4SLinus Torvalds return status; 11721da177e4SLinus Torvalds err_ctl_sock_init: 11731da177e4SLinus Torvalds sctp_v6_exit(); 11741da177e4SLinus Torvalds err_v6_init: 11751da177e4SLinus Torvalds sctp_sysctl_unregister(); 11761da177e4SLinus Torvalds list_del(&sctp_ipv4_specific.list); 11771da177e4SLinus Torvalds free_pages((unsigned long)sctp_port_hashtable, 11781da177e4SLinus Torvalds get_order(sctp_port_hashsize * 11791da177e4SLinus Torvalds sizeof(struct sctp_bind_hashbucket))); 11801da177e4SLinus Torvalds err_bhash_alloc: 11811da177e4SLinus Torvalds kfree(sctp_ep_hashtable); 11821da177e4SLinus Torvalds err_ehash_alloc: 11831da177e4SLinus Torvalds free_pages((unsigned long)sctp_assoc_hashtable, 11841da177e4SLinus Torvalds get_order(sctp_assoc_hashsize * 11851da177e4SLinus Torvalds sizeof(struct sctp_hashbucket))); 11861da177e4SLinus Torvalds err_ahash_alloc: 11871da177e4SLinus Torvalds sctp_dbg_objcnt_exit(); 11881da177e4SLinus Torvalds err_init_proc: 11891da177e4SLinus Torvalds sctp_proc_exit(); 11901da177e4SLinus Torvalds cleanup_sctp_mibs(); 11911da177e4SLinus Torvalds err_init_mibs: 11921da177e4SLinus Torvalds kmem_cache_destroy(sctp_chunk_cachep); 11931da177e4SLinus Torvalds err_chunk_cachep: 11941da177e4SLinus Torvalds kmem_cache_destroy(sctp_bucket_cachep); 11951da177e4SLinus Torvalds err_bucket_cachep: 11961da177e4SLinus Torvalds inet_del_protocol(&sctp_protocol, IPPROTO_SCTP); 11971da177e4SLinus Torvalds inet_unregister_protosw(&sctp_seqpacket_protosw); 11981da177e4SLinus Torvalds inet_unregister_protosw(&sctp_stream_protosw); 11995e6bc34fSNeil Horman err_add_protocol: 12005e6bc34fSNeil Horman proto_unregister(&sctp_prot); 12011da177e4SLinus Torvalds goto out; 12021da177e4SLinus Torvalds } 12031da177e4SLinus Torvalds 12041da177e4SLinus Torvalds /* Exit handler for the SCTP protocol. */ 12051da177e4SLinus Torvalds SCTP_STATIC __exit void sctp_exit(void) 12061da177e4SLinus Torvalds { 12071da177e4SLinus Torvalds /* BUG. This should probably do something useful like clean 12081da177e4SLinus Torvalds * up all the remaining associations and all that memory. 12091da177e4SLinus Torvalds */ 12101da177e4SLinus Torvalds 12111da177e4SLinus Torvalds /* Unregister notifier for inet address additions/deletions. */ 12121da177e4SLinus Torvalds unregister_inetaddr_notifier(&sctp_inetaddr_notifier); 12131da177e4SLinus Torvalds 12141da177e4SLinus Torvalds /* Free the local address list. */ 12151da177e4SLinus Torvalds sctp_free_local_addr_list(); 12161da177e4SLinus Torvalds 12171da177e4SLinus Torvalds /* Free the control endpoint. */ 12181da177e4SLinus Torvalds sock_release(sctp_ctl_socket); 12191da177e4SLinus Torvalds 12201da177e4SLinus Torvalds sctp_v6_exit(); 12211da177e4SLinus Torvalds sctp_sysctl_unregister(); 12221da177e4SLinus Torvalds list_del(&sctp_ipv4_specific.list); 12231da177e4SLinus Torvalds 12241da177e4SLinus Torvalds free_pages((unsigned long)sctp_assoc_hashtable, 12251da177e4SLinus Torvalds get_order(sctp_assoc_hashsize * 12261da177e4SLinus Torvalds sizeof(struct sctp_hashbucket))); 12271da177e4SLinus Torvalds kfree(sctp_ep_hashtable); 12281da177e4SLinus Torvalds free_pages((unsigned long)sctp_port_hashtable, 12291da177e4SLinus Torvalds get_order(sctp_port_hashsize * 12301da177e4SLinus Torvalds sizeof(struct sctp_bind_hashbucket))); 12311da177e4SLinus Torvalds 12321da177e4SLinus Torvalds kmem_cache_destroy(sctp_chunk_cachep); 12331da177e4SLinus Torvalds kmem_cache_destroy(sctp_bucket_cachep); 12341da177e4SLinus Torvalds 12351da177e4SLinus Torvalds sctp_dbg_objcnt_exit(); 12361da177e4SLinus Torvalds sctp_proc_exit(); 12371da177e4SLinus Torvalds cleanup_sctp_mibs(); 12381da177e4SLinus Torvalds 12391da177e4SLinus Torvalds inet_del_protocol(&sctp_protocol, IPPROTO_SCTP); 12401da177e4SLinus Torvalds inet_unregister_protosw(&sctp_seqpacket_protosw); 12411da177e4SLinus Torvalds inet_unregister_protosw(&sctp_stream_protosw); 12421da177e4SLinus Torvalds proto_unregister(&sctp_prot); 12431da177e4SLinus Torvalds } 12441da177e4SLinus Torvalds 12451da177e4SLinus Torvalds module_init(sctp_init); 12461da177e4SLinus Torvalds module_exit(sctp_exit); 12471da177e4SLinus Torvalds 1248bb97d31fSArnaldo Carvalho de Melo /* 1249bb97d31fSArnaldo Carvalho de Melo * __stringify doesn't likes enums, so use IPPROTO_SCTP value (132) directly. 1250bb97d31fSArnaldo Carvalho de Melo */ 1251bb97d31fSArnaldo Carvalho de Melo MODULE_ALIAS("net-pf-" __stringify(PF_INET) "-proto-132"); 12521da177e4SLinus Torvalds MODULE_AUTHOR("Linux Kernel SCTP developers <lksctp-developers@lists.sourceforge.net>"); 12531da177e4SLinus Torvalds MODULE_DESCRIPTION("Support for the SCTP protocol (RFC2960)"); 12541da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 1255