147505b8bSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 260c778b2SVlad Yasevich /* SCTP kernel implementation 31da177e4SLinus Torvalds * (C) Copyright IBM Corp. 2001, 2004 41da177e4SLinus Torvalds * Copyright (c) 1999-2000 Cisco, Inc. 51da177e4SLinus Torvalds * Copyright (c) 1999-2001 Motorola, Inc. 61da177e4SLinus Torvalds * Copyright (c) 2001 Intel Corp. 71da177e4SLinus Torvalds * Copyright (c) 2001 Nokia, Inc. 81da177e4SLinus Torvalds * Copyright (c) 2001 La Monte H.P. Yarroll 91da177e4SLinus Torvalds * 1060c778b2SVlad Yasevich * This file is part of the SCTP kernel implementation 111da177e4SLinus Torvalds * 121da177e4SLinus Torvalds * Initialization/cleanup for SCTP protocol support. 131da177e4SLinus Torvalds * 141da177e4SLinus Torvalds * Please send any bug reports or fixes you make to the 151da177e4SLinus Torvalds * email address(es): 1691705c61SDaniel Borkmann * lksctp developers <linux-sctp@vger.kernel.org> 171da177e4SLinus Torvalds * 181da177e4SLinus Torvalds * Written or modified by: 191da177e4SLinus Torvalds * La Monte H.P. Yarroll <piggy@acm.org> 201da177e4SLinus Torvalds * Karl Knutson <karl@athena.chicago.il.us> 211da177e4SLinus Torvalds * Jon Grimm <jgrimm@us.ibm.com> 221da177e4SLinus Torvalds * Sridhar Samudrala <sri@us.ibm.com> 231da177e4SLinus Torvalds * Daisy Chang <daisyc@us.ibm.com> 241da177e4SLinus Torvalds * Ardelle Fan <ardelle.fan@intel.com> 251da177e4SLinus Torvalds */ 261da177e4SLinus Torvalds 27145ce502SJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 28145ce502SJoe Perches 291da177e4SLinus Torvalds #include <linux/module.h> 301da177e4SLinus Torvalds #include <linux/init.h> 311da177e4SLinus Torvalds #include <linux/netdevice.h> 321da177e4SLinus Torvalds #include <linux/inetdevice.h> 331da177e4SLinus Torvalds #include <linux/seq_file.h> 3457c8a661SMike Rapoport #include <linux/memblock.h> 35845525a6SVlad Yasevich #include <linux/highmem.h> 365a0e3ad6STejun Heo #include <linux/slab.h> 37457c4cbcSEric W. Biederman #include <net/net_namespace.h> 381da177e4SLinus Torvalds #include <net/protocol.h> 391da177e4SLinus Torvalds #include <net/ip.h> 401da177e4SLinus Torvalds #include <net/ipv6.h> 4114c85021SArnaldo Carvalho de Melo #include <net/route.h> 421da177e4SLinus Torvalds #include <net/sctp/sctp.h> 431da177e4SLinus Torvalds #include <net/addrconf.h> 441da177e4SLinus Torvalds #include <net/inet_common.h> 451da177e4SLinus Torvalds #include <net/inet_ecn.h> 46965ae444SXin Long #include <net/udp_tunnel.h> 471da177e4SLinus Torvalds 48d9749fb5SNeil Horman #define MAX_SCTP_PORT_HASH_ENTRIES (64 * 1024) 49d9749fb5SNeil Horman 501da177e4SLinus Torvalds /* Global data structures. */ 514cbf1caeSBrian Haley struct sctp_globals sctp_globals __read_mostly; 521da177e4SLinus Torvalds 531da177e4SLinus Torvalds struct idr sctp_assocs_id; 541da177e4SLinus Torvalds DEFINE_SPINLOCK(sctp_assocs_id_lock); 551da177e4SLinus Torvalds 561da177e4SLinus Torvalds static struct sctp_pf *sctp_pf_inet6_specific; 571da177e4SLinus Torvalds static struct sctp_pf *sctp_pf_inet_specific; 581da177e4SLinus Torvalds static struct sctp_af *sctp_af_v4_specific; 591da177e4SLinus Torvalds static struct sctp_af *sctp_af_v6_specific; 601da177e4SLinus Torvalds 61e18b890bSChristoph Lameter struct kmem_cache *sctp_chunk_cachep __read_mostly; 62e18b890bSChristoph Lameter struct kmem_cache *sctp_bucket_cachep __read_mostly; 631da177e4SLinus Torvalds 648d987e5cSEric Dumazet long sysctl_sctp_mem[3]; 65007e3936SVlad Yasevich int sysctl_sctp_rmem[3]; 66007e3936SVlad Yasevich int sysctl_sctp_wmem[3]; 674d93df0aSNeil Horman 681da177e4SLinus Torvalds /* Private helper to extract ipv4 address and stash them in 691da177e4SLinus Torvalds * the protocol structure. 701da177e4SLinus Torvalds */ 711da177e4SLinus Torvalds static void sctp_v4_copy_addrlist(struct list_head *addrlist, 721da177e4SLinus Torvalds struct net_device *dev) 731da177e4SLinus Torvalds { 741da177e4SLinus Torvalds struct in_device *in_dev; 751da177e4SLinus Torvalds struct in_ifaddr *ifa; 761da177e4SLinus Torvalds struct sctp_sockaddr_entry *addr; 771da177e4SLinus Torvalds 781da177e4SLinus Torvalds rcu_read_lock(); 79e5ed6399SHerbert Xu if ((in_dev = __in_dev_get_rcu(dev)) == NULL) { 801da177e4SLinus Torvalds rcu_read_unlock(); 811da177e4SLinus Torvalds return; 821da177e4SLinus Torvalds } 831da177e4SLinus Torvalds 84cd5a411dSFlorian Westphal in_dev_for_each_ifa_rcu(ifa, in_dev) { 851da177e4SLinus Torvalds /* Add the address to the local list. */ 86939cfa75SDaniel Borkmann addr = kzalloc(sizeof(*addr), GFP_ATOMIC); 871da177e4SLinus Torvalds if (addr) { 882a6fd78aSAl Viro addr->a.v4.sin_family = AF_INET; 892a6fd78aSAl Viro addr->a.v4.sin_addr.s_addr = ifa->ifa_local; 9029303547SVlad Yasevich addr->valid = 1; 9129303547SVlad Yasevich INIT_LIST_HEAD(&addr->list); 921da177e4SLinus Torvalds list_add_tail(&addr->list, addrlist); 931da177e4SLinus Torvalds } 941da177e4SLinus Torvalds } 951da177e4SLinus Torvalds 961da177e4SLinus Torvalds rcu_read_unlock(); 971da177e4SLinus Torvalds } 981da177e4SLinus Torvalds 991da177e4SLinus Torvalds /* Extract our IP addresses from the system and stash them in the 1001da177e4SLinus Torvalds * protocol structure. 1011da177e4SLinus Torvalds */ 1024db67e80SEric W. Biederman static void sctp_get_local_addr_list(struct net *net) 1031da177e4SLinus Torvalds { 1041da177e4SLinus Torvalds struct net_device *dev; 1051da177e4SLinus Torvalds struct list_head *pos; 1061da177e4SLinus Torvalds struct sctp_af *af; 1071da177e4SLinus Torvalds 108c6d14c84SEric Dumazet rcu_read_lock(); 1094db67e80SEric W. Biederman for_each_netdev_rcu(net, dev) { 1102c0740e4SDave Jones list_for_each(pos, &sctp_address_families) { 1111da177e4SLinus Torvalds af = list_entry(pos, struct sctp_af, list); 1124db67e80SEric W. Biederman af->copy_addrlist(&net->sctp.local_addr_list, dev); 1131da177e4SLinus Torvalds } 1141da177e4SLinus Torvalds } 115c6d14c84SEric Dumazet rcu_read_unlock(); 1161da177e4SLinus Torvalds } 1171da177e4SLinus Torvalds 1181da177e4SLinus Torvalds /* Free the existing local addresses. */ 1194db67e80SEric W. Biederman static void sctp_free_local_addr_list(struct net *net) 1201da177e4SLinus Torvalds { 1211da177e4SLinus Torvalds struct sctp_sockaddr_entry *addr; 1221da177e4SLinus Torvalds struct list_head *pos, *temp; 1231da177e4SLinus Torvalds 1244db67e80SEric W. Biederman list_for_each_safe(pos, temp, &net->sctp.local_addr_list) { 1251da177e4SLinus Torvalds addr = list_entry(pos, struct sctp_sockaddr_entry, list); 1261da177e4SLinus Torvalds list_del(pos); 1271da177e4SLinus Torvalds kfree(addr); 1281da177e4SLinus Torvalds } 1291da177e4SLinus Torvalds } 1301da177e4SLinus Torvalds 1311da177e4SLinus Torvalds /* Copy the local addresses which are valid for 'scope' into 'bp'. */ 1324db67e80SEric W. Biederman int sctp_copy_local_addr_list(struct net *net, struct sctp_bind_addr *bp, 1331c662018SXin Long enum sctp_scope scope, gfp_t gfp, int copy_flags) 1341da177e4SLinus Torvalds { 1351da177e4SLinus Torvalds struct sctp_sockaddr_entry *addr; 1362e3ce5bcSXin Long union sctp_addr laddr; 1371da177e4SLinus Torvalds int error = 0; 1381da177e4SLinus Torvalds 13929303547SVlad Yasevich rcu_read_lock(); 1404db67e80SEric W. Biederman list_for_each_entry_rcu(addr, &net->sctp.local_addr_list, list) { 14129303547SVlad Yasevich if (!addr->valid) 14229303547SVlad Yasevich continue; 143165f2cf6SXin Long if (!sctp_in_scope(net, &addr->a, scope)) 144165f2cf6SXin Long continue; 145165f2cf6SXin Long 1461da177e4SLinus Torvalds /* Now that the address is in scope, check to see if 1471da177e4SLinus Torvalds * the address type is really supported by the local 1481da177e4SLinus Torvalds * sock as well as the remote peer. 1491da177e4SLinus Torvalds */ 150165f2cf6SXin Long if (addr->a.sa.sa_family == AF_INET && 151471e39dfSMarcelo Ricardo Leitner (!(copy_flags & SCTP_ADDR4_ALLOWED) || 152471e39dfSMarcelo Ricardo Leitner !(copy_flags & SCTP_ADDR4_PEERSUPP))) 153165f2cf6SXin Long continue; 154165f2cf6SXin Long if (addr->a.sa.sa_family == AF_INET6 && 155165f2cf6SXin Long (!(copy_flags & SCTP_ADDR6_ALLOWED) || 156165f2cf6SXin Long !(copy_flags & SCTP_ADDR6_PEERSUPP))) 157165f2cf6SXin Long continue; 158165f2cf6SXin Long 1592e3ce5bcSXin Long laddr = addr->a; 1602e3ce5bcSXin Long /* also works for setting ipv6 address port */ 1612e3ce5bcSXin Long laddr.v4.sin_port = htons(bp->port); 1622e3ce5bcSXin Long if (sctp_bind_addr_state(bp, &laddr) != -1) 163b8607805SXin Long continue; 164b8607805SXin Long 165165f2cf6SXin Long error = sctp_add_bind_addr(bp, &addr->a, sizeof(addr->a), 166f57d96b2SVlad Yasevich SCTP_ADDR_SRC, GFP_ATOMIC); 1671da177e4SLinus Torvalds if (error) 168165f2cf6SXin Long break; 1691da177e4SLinus Torvalds } 1701da177e4SLinus Torvalds 17129303547SVlad Yasevich rcu_read_unlock(); 1721da177e4SLinus Torvalds return error; 1731da177e4SLinus Torvalds } 1741da177e4SLinus Torvalds 175b7e10c25SRichard Haines /* Copy over any ip options */ 176b7e10c25SRichard Haines static void sctp_v4_copy_ip_options(struct sock *sk, struct sock *newsk) 177b7e10c25SRichard Haines { 178b7e10c25SRichard Haines struct inet_sock *newinet, *inet = inet_sk(sk); 179b7e10c25SRichard Haines struct ip_options_rcu *inet_opt, *newopt = NULL; 180b7e10c25SRichard Haines 181b7e10c25SRichard Haines newinet = inet_sk(newsk); 182b7e10c25SRichard Haines 183b7e10c25SRichard Haines rcu_read_lock(); 184b7e10c25SRichard Haines inet_opt = rcu_dereference(inet->inet_opt); 185b7e10c25SRichard Haines if (inet_opt) { 186b7e10c25SRichard Haines newopt = sock_kmalloc(newsk, sizeof(*inet_opt) + 187b7e10c25SRichard Haines inet_opt->opt.optlen, GFP_ATOMIC); 188b7e10c25SRichard Haines if (newopt) 189b7e10c25SRichard Haines memcpy(newopt, inet_opt, sizeof(*inet_opt) + 190b7e10c25SRichard Haines inet_opt->opt.optlen); 191b7e10c25SRichard Haines else 192b7e10c25SRichard Haines pr_err("%s: Failed to copy ip options\n", __func__); 193b7e10c25SRichard Haines } 194b7e10c25SRichard Haines RCU_INIT_POINTER(newinet->inet_opt, newopt); 195b7e10c25SRichard Haines rcu_read_unlock(); 196b7e10c25SRichard Haines } 197b7e10c25SRichard Haines 198b7e10c25SRichard Haines /* Account for the IP options */ 199b7e10c25SRichard Haines static int sctp_v4_ip_options_len(struct sock *sk) 200b7e10c25SRichard Haines { 201b7e10c25SRichard Haines struct inet_sock *inet = inet_sk(sk); 202b7e10c25SRichard Haines struct ip_options_rcu *inet_opt; 203b7e10c25SRichard Haines int len = 0; 204b7e10c25SRichard Haines 205b7e10c25SRichard Haines rcu_read_lock(); 206b7e10c25SRichard Haines inet_opt = rcu_dereference(inet->inet_opt); 207b7e10c25SRichard Haines if (inet_opt) 208b7e10c25SRichard Haines len = inet_opt->opt.optlen; 209b7e10c25SRichard Haines 210b7e10c25SRichard Haines rcu_read_unlock(); 211b7e10c25SRichard Haines return len; 212b7e10c25SRichard Haines } 213b7e10c25SRichard Haines 2141da177e4SLinus Torvalds /* Initialize a sctp_addr from in incoming skb. */ 2151da177e4SLinus Torvalds static void sctp_v4_from_skb(union sctp_addr *addr, struct sk_buff *skb, 2161da177e4SLinus Torvalds int is_saddr) 2171da177e4SLinus Torvalds { 2180630c56eSMarcelo Ricardo Leitner /* Always called on head skb, so this is safe */ 2190630c56eSMarcelo Ricardo Leitner struct sctphdr *sh = sctp_hdr(skb); 2200630c56eSMarcelo Ricardo Leitner struct sockaddr_in *sa = &addr->v4; 2211da177e4SLinus Torvalds 2221da177e4SLinus Torvalds addr->v4.sin_family = AF_INET; 2231da177e4SLinus Torvalds 2241da177e4SLinus Torvalds if (is_saddr) { 2250630c56eSMarcelo Ricardo Leitner sa->sin_port = sh->source; 2260630c56eSMarcelo Ricardo Leitner sa->sin_addr.s_addr = ip_hdr(skb)->saddr; 2271da177e4SLinus Torvalds } else { 2280630c56eSMarcelo Ricardo Leitner sa->sin_port = sh->dest; 2290630c56eSMarcelo Ricardo Leitner sa->sin_addr.s_addr = ip_hdr(skb)->daddr; 2301da177e4SLinus Torvalds } 231b6f3320bSXin Long memset(sa->sin_zero, 0, sizeof(sa->sin_zero)); 2321da177e4SLinus Torvalds } 2331da177e4SLinus Torvalds 2341da177e4SLinus Torvalds /* Initialize an sctp_addr from a socket. */ 2351da177e4SLinus Torvalds static void sctp_v4_from_sk(union sctp_addr *addr, struct sock *sk) 2361da177e4SLinus Torvalds { 2371da177e4SLinus Torvalds addr->v4.sin_family = AF_INET; 2387dcdbd95SAl Viro addr->v4.sin_port = 0; 239c720c7e8SEric Dumazet addr->v4.sin_addr.s_addr = inet_sk(sk)->inet_rcv_saddr; 240b6f3320bSXin Long memset(addr->v4.sin_zero, 0, sizeof(addr->v4.sin_zero)); 2411da177e4SLinus Torvalds } 2421da177e4SLinus Torvalds 2431da177e4SLinus Torvalds /* Initialize sk->sk_rcv_saddr from sctp_addr. */ 2441da177e4SLinus Torvalds static void sctp_v4_to_sk_saddr(union sctp_addr *addr, struct sock *sk) 2451da177e4SLinus Torvalds { 246c720c7e8SEric Dumazet inet_sk(sk)->inet_rcv_saddr = addr->v4.sin_addr.s_addr; 2471da177e4SLinus Torvalds } 2481da177e4SLinus Torvalds 2491da177e4SLinus Torvalds /* Initialize sk->sk_daddr from sctp_addr. */ 2501da177e4SLinus Torvalds static void sctp_v4_to_sk_daddr(union sctp_addr *addr, struct sock *sk) 2511da177e4SLinus Torvalds { 252c720c7e8SEric Dumazet inet_sk(sk)->inet_daddr = addr->v4.sin_addr.s_addr; 2531da177e4SLinus Torvalds } 2541da177e4SLinus Torvalds 2551da177e4SLinus Torvalds /* Initialize a sctp_addr from an address parameter. */ 2560c5dc070SMarcelo Ricardo Leitner static bool sctp_v4_from_addr_param(union sctp_addr *addr, 2571da177e4SLinus Torvalds union sctp_addr_param *param, 258dd86d136SAl Viro __be16 port, int iif) 2591da177e4SLinus Torvalds { 2600c5dc070SMarcelo Ricardo Leitner if (ntohs(param->v4.param_hdr.length) < sizeof(struct sctp_ipv4addr_param)) 2610c5dc070SMarcelo Ricardo Leitner return false; 2620c5dc070SMarcelo Ricardo Leitner 2631da177e4SLinus Torvalds addr->v4.sin_family = AF_INET; 2641da177e4SLinus Torvalds addr->v4.sin_port = port; 2651da177e4SLinus Torvalds addr->v4.sin_addr.s_addr = param->v4.addr.s_addr; 266b6f3320bSXin Long memset(addr->v4.sin_zero, 0, sizeof(addr->v4.sin_zero)); 2670c5dc070SMarcelo Ricardo Leitner 2680c5dc070SMarcelo Ricardo Leitner return true; 2691da177e4SLinus Torvalds } 2701da177e4SLinus Torvalds 2711da177e4SLinus Torvalds /* Initialize an address parameter from a sctp_addr and return the length 2721da177e4SLinus Torvalds * of the address parameter. 2731da177e4SLinus Torvalds */ 2741da177e4SLinus Torvalds static int sctp_v4_to_addr_param(const union sctp_addr *addr, 2751da177e4SLinus Torvalds union sctp_addr_param *param) 2761da177e4SLinus Torvalds { 277a38905e6SXin Long int length = sizeof(struct sctp_ipv4addr_param); 2781da177e4SLinus Torvalds 2791da177e4SLinus Torvalds param->v4.param_hdr.type = SCTP_PARAM_IPV4_ADDRESS; 280dbc16db1SAl Viro param->v4.param_hdr.length = htons(length); 2811da177e4SLinus Torvalds param->v4.addr.s_addr = addr->v4.sin_addr.s_addr; 2821da177e4SLinus Torvalds 2831da177e4SLinus Torvalds return length; 2841da177e4SLinus Torvalds } 2851da177e4SLinus Torvalds 2861da177e4SLinus Torvalds /* Initialize a sctp_addr from a dst_entry. */ 28718a353f4SDavid S. Miller static void sctp_v4_dst_saddr(union sctp_addr *saddr, struct flowi4 *fl4, 288854d43a4SAl Viro __be16 port) 2891da177e4SLinus Torvalds { 2901da177e4SLinus Torvalds saddr->v4.sin_family = AF_INET; 2911da177e4SLinus Torvalds saddr->v4.sin_port = port; 29218a353f4SDavid S. Miller saddr->v4.sin_addr.s_addr = fl4->saddr; 293b6f3320bSXin Long memset(saddr->v4.sin_zero, 0, sizeof(saddr->v4.sin_zero)); 2941da177e4SLinus Torvalds } 2951da177e4SLinus Torvalds 2961da177e4SLinus Torvalds /* Compare two addresses exactly. */ 2971da177e4SLinus Torvalds static int sctp_v4_cmp_addr(const union sctp_addr *addr1, 2981da177e4SLinus Torvalds const union sctp_addr *addr2) 2991da177e4SLinus Torvalds { 3001da177e4SLinus Torvalds if (addr1->sa.sa_family != addr2->sa.sa_family) 3011da177e4SLinus Torvalds return 0; 3021da177e4SLinus Torvalds if (addr1->v4.sin_port != addr2->v4.sin_port) 3031da177e4SLinus Torvalds return 0; 3041da177e4SLinus Torvalds if (addr1->v4.sin_addr.s_addr != addr2->v4.sin_addr.s_addr) 3051da177e4SLinus Torvalds return 0; 3061da177e4SLinus Torvalds 3071da177e4SLinus Torvalds return 1; 3081da177e4SLinus Torvalds } 3091da177e4SLinus Torvalds 3101da177e4SLinus Torvalds /* Initialize addr struct to INADDR_ANY. */ 3116fbfa9f9SAl Viro static void sctp_v4_inaddr_any(union sctp_addr *addr, __be16 port) 3121da177e4SLinus Torvalds { 3131da177e4SLinus Torvalds addr->v4.sin_family = AF_INET; 314e6f1cebfSAl Viro addr->v4.sin_addr.s_addr = htonl(INADDR_ANY); 3151da177e4SLinus Torvalds addr->v4.sin_port = port; 316b6f3320bSXin Long memset(addr->v4.sin_zero, 0, sizeof(addr->v4.sin_zero)); 3171da177e4SLinus Torvalds } 3181da177e4SLinus Torvalds 3191da177e4SLinus Torvalds /* Is this a wildcard address? */ 3201da177e4SLinus Torvalds static int sctp_v4_is_any(const union sctp_addr *addr) 3211da177e4SLinus Torvalds { 322e6f1cebfSAl Viro return htonl(INADDR_ANY) == addr->v4.sin_addr.s_addr; 3231da177e4SLinus Torvalds } 3241da177e4SLinus Torvalds 3251da177e4SLinus Torvalds /* This function checks if the address is a valid address to be used for 3261da177e4SLinus Torvalds * SCTP binding. 3271da177e4SLinus Torvalds * 3281da177e4SLinus Torvalds * Output: 3291da177e4SLinus Torvalds * Return 0 - If the address is a non-unicast or an illegal address. 3301da177e4SLinus Torvalds * Return 1 - If the address is a unicast. 3311da177e4SLinus Torvalds */ 3325636bef7SVlad Yasevich static int sctp_v4_addr_valid(union sctp_addr *addr, 3335636bef7SVlad Yasevich struct sctp_sock *sp, 3345636bef7SVlad Yasevich const struct sk_buff *skb) 3351da177e4SLinus Torvalds { 3367dab83deSVlad Yasevich /* IPv4 addresses not allowed */ 3377dab83deSVlad Yasevich if (sp && ipv6_only_sock(sctp_opt2sk(sp))) 3387dab83deSVlad Yasevich return 0; 3397dab83deSVlad Yasevich 3401da177e4SLinus Torvalds /* Is this a non-unicast address or a unusable SCTP address? */ 341b5cb2bbcSJoe Perches if (IS_IPV4_UNUSABLE_ADDRESS(addr->v4.sin_addr.s_addr)) 3421da177e4SLinus Torvalds return 0; 3431da177e4SLinus Torvalds 3445636bef7SVlad Yasevich /* Is this a broadcast address? */ 345511c3f92SEric Dumazet if (skb && skb_rtable(skb)->rt_flags & RTCF_BROADCAST) 3465636bef7SVlad Yasevich return 0; 3475636bef7SVlad Yasevich 3481da177e4SLinus Torvalds return 1; 3491da177e4SLinus Torvalds } 3501da177e4SLinus Torvalds 3511da177e4SLinus Torvalds /* Should this be available for binding? */ 3521da177e4SLinus Torvalds static int sctp_v4_available(union sctp_addr *addr, struct sctp_sock *sp) 3531da177e4SLinus Torvalds { 35426943aefSXin Long struct sock *sk = &sp->inet.sk; 35526943aefSXin Long struct net *net = sock_net(sk); 35626943aefSXin Long int tb_id = RT_TABLE_LOCAL; 35726943aefSXin Long int ret; 3581da177e4SLinus Torvalds 35926943aefSXin Long tb_id = l3mdev_fib_table_by_index(net, sk->sk_bound_dev_if) ?: tb_id; 36026943aefSXin Long ret = inet_addr_type_table(net, addr->v4.sin_addr.s_addr, tb_id); 361e6f1cebfSAl Viro if (addr->v4.sin_addr.s_addr != htonl(INADDR_ANY) && 362cdac4e07SNeil Horman ret != RTN_LOCAL && 363cdac4e07SNeil Horman !sp->inet.freebind && 364289d3b21SKuniyuki Iwashima !READ_ONCE(net->ipv4.sysctl_ip_nonlocal_bind)) 3651da177e4SLinus Torvalds return 0; 366cdac4e07SNeil Horman 3677dab83deSVlad Yasevich if (ipv6_only_sock(sctp_opt2sk(sp))) 3687dab83deSVlad Yasevich return 0; 3697dab83deSVlad Yasevich 3701da177e4SLinus Torvalds return 1; 3711da177e4SLinus Torvalds } 3721da177e4SLinus Torvalds 3731da177e4SLinus Torvalds /* Checking the loopback, private and other address scopes as defined in 3741da177e4SLinus Torvalds * RFC 1918. The IPv4 scoping is based on the draft for SCTP IPv4 3751da177e4SLinus Torvalds * scoping <draft-stewart-tsvwg-sctp-ipv4-00.txt>. 3761da177e4SLinus Torvalds * 3771da177e4SLinus Torvalds * Level 0 - unusable SCTP addresses 3781da177e4SLinus Torvalds * Level 1 - loopback address 3791da177e4SLinus Torvalds * Level 2 - link-local addresses 3801da177e4SLinus Torvalds * Level 3 - private addresses. 3811da177e4SLinus Torvalds * Level 4 - global addresses 3821da177e4SLinus Torvalds * For INIT and INIT-ACK address list, let L be the level of 38393c3216aSRandy Dunlap * requested destination address, sender and receiver 3841da177e4SLinus Torvalds * SHOULD include all of its addresses with level greater 3851da177e4SLinus Torvalds * than or equal to L. 38672388433SBhaskar Dutta * 38772388433SBhaskar Dutta * IPv4 scoping can be controlled through sysctl option 38872388433SBhaskar Dutta * net.sctp.addr_scope_policy 3891da177e4SLinus Torvalds */ 3901c662018SXin Long static enum sctp_scope sctp_v4_scope(union sctp_addr *addr) 3911da177e4SLinus Torvalds { 3921c662018SXin Long enum sctp_scope retval; 3931da177e4SLinus Torvalds 3941da177e4SLinus Torvalds /* Check for unusable SCTP addresses. */ 395b5cb2bbcSJoe Perches if (IS_IPV4_UNUSABLE_ADDRESS(addr->v4.sin_addr.s_addr)) { 3961da177e4SLinus Torvalds retval = SCTP_SCOPE_UNUSABLE; 397b5cb2bbcSJoe Perches } else if (ipv4_is_loopback(addr->v4.sin_addr.s_addr)) { 3981da177e4SLinus Torvalds retval = SCTP_SCOPE_LOOPBACK; 399b5cb2bbcSJoe Perches } else if (ipv4_is_linklocal_169(addr->v4.sin_addr.s_addr)) { 4001da177e4SLinus Torvalds retval = SCTP_SCOPE_LINK; 401b5cb2bbcSJoe Perches } else if (ipv4_is_private_10(addr->v4.sin_addr.s_addr) || 402b5cb2bbcSJoe Perches ipv4_is_private_172(addr->v4.sin_addr.s_addr) || 4031d11fa23SXin Long ipv4_is_private_192(addr->v4.sin_addr.s_addr) || 4041d11fa23SXin Long ipv4_is_test_198(addr->v4.sin_addr.s_addr)) { 4051da177e4SLinus Torvalds retval = SCTP_SCOPE_PRIVATE; 4061da177e4SLinus Torvalds } else { 4071da177e4SLinus Torvalds retval = SCTP_SCOPE_GLOBAL; 4081da177e4SLinus Torvalds } 4091da177e4SLinus Torvalds 4101da177e4SLinus Torvalds return retval; 4111da177e4SLinus Torvalds } 4121da177e4SLinus Torvalds 4131da177e4SLinus Torvalds /* Returns a valid dst cache entry for the given source and destination ip 4141da177e4SLinus Torvalds * addresses. If an association is passed, trys to get a dst entry with a 4151da177e4SLinus Torvalds * source address that matches an address in the bind address list. 4161da177e4SLinus Torvalds */ 417da0420beSVlad Yasevich static void sctp_v4_get_dst(struct sctp_transport *t, union sctp_addr *saddr, 418da0420beSVlad Yasevich struct flowi *fl, struct sock *sk) 4191da177e4SLinus Torvalds { 420da0420beSVlad Yasevich struct sctp_association *asoc = t->asoc; 4211da177e4SLinus Torvalds struct rtable *rt; 422582eea23SMarcelo Ricardo Leitner struct flowi _fl; 423582eea23SMarcelo Ricardo Leitner struct flowi4 *fl4 = &_fl.u.ip4; 4241da177e4SLinus Torvalds struct sctp_bind_addr *bp; 4251da177e4SLinus Torvalds struct sctp_sockaddr_entry *laddr; 4261da177e4SLinus Torvalds struct dst_entry *dst = NULL; 427da0420beSVlad Yasevich union sctp_addr *daddr = &t->ipaddr; 4281da177e4SLinus Torvalds union sctp_addr dst_saddr; 4298a9c58d2SXin Long __u8 tos = inet_sk(sk)->tos; 4301da177e4SLinus Torvalds 4318a9c58d2SXin Long if (t->dscp & SCTP_DSCP_SET_MASK) 4328a9c58d2SXin Long tos = t->dscp & SCTP_DSCP_VAL_MASK; 433582eea23SMarcelo Ricardo Leitner memset(&_fl, 0x0, sizeof(_fl)); 4349914ae3cSVlad Yasevich fl4->daddr = daddr->v4.sin_addr.s_addr; 4359914ae3cSVlad Yasevich fl4->fl4_dport = daddr->v4.sin_port; 4369914ae3cSVlad Yasevich fl4->flowi4_proto = IPPROTO_SCTP; 4371da177e4SLinus Torvalds if (asoc) { 4388a9c58d2SXin Long fl4->flowi4_tos = RT_CONN_FLAGS_TOS(asoc->base.sk, tos); 4399914ae3cSVlad Yasevich fl4->flowi4_oif = asoc->base.sk->sk_bound_dev_if; 4409914ae3cSVlad Yasevich fl4->fl4_sport = htons(asoc->base.bind_addr.port); 4411da177e4SLinus Torvalds } 4426429d3dcSWei Yongjun if (saddr) { 4439914ae3cSVlad Yasevich fl4->saddr = saddr->v4.sin_addr.s_addr; 444ecf938feSXin Long if (!fl4->fl4_sport) 4459914ae3cSVlad Yasevich fl4->fl4_sport = saddr->v4.sin_port; 4466429d3dcSWei Yongjun } 4471da177e4SLinus Torvalds 448bb33381dSDaniel Borkmann pr_debug("%s: dst:%pI4, src:%pI4 - ", __func__, &fl4->daddr, 449bb33381dSDaniel Borkmann &fl4->saddr); 4501da177e4SLinus Torvalds 451bb2db45bSEric W. Biederman rt = ip_route_output_key(sock_net(sk), fl4); 452582eea23SMarcelo Ricardo Leitner if (!IS_ERR(rt)) { 453d8d1f30bSChangli Gao dst = &rt->dst; 454582eea23SMarcelo Ricardo Leitner t->dst = dst; 455582eea23SMarcelo Ricardo Leitner memcpy(fl, &_fl, sizeof(_fl)); 456582eea23SMarcelo Ricardo Leitner } 4571da177e4SLinus Torvalds 4581da177e4SLinus Torvalds /* If there is no association or if a source address is passed, no 4591da177e4SLinus Torvalds * more validation is required. 4601da177e4SLinus Torvalds */ 4611da177e4SLinus Torvalds if (!asoc || saddr) 4621da177e4SLinus Torvalds goto out; 4631da177e4SLinus Torvalds 4641da177e4SLinus Torvalds bp = &asoc->base.bind_addr; 4651da177e4SLinus Torvalds 4661da177e4SLinus Torvalds if (dst) { 4671da177e4SLinus Torvalds /* Walk through the bind address list and look for a bind 4681da177e4SLinus Torvalds * address that matches the source address of the returned dst. 4691da177e4SLinus Torvalds */ 47018a353f4SDavid S. Miller sctp_v4_dst_saddr(&dst_saddr, fl4, htons(bp->port)); 471559cf710SVlad Yasevich rcu_read_lock(); 472559cf710SVlad Yasevich list_for_each_entry_rcu(laddr, &bp->address_list, list) { 4738a07eb0aSMichio Honda if (!laddr->valid || (laddr->state == SCTP_ADDR_DEL) || 4748a07eb0aSMichio Honda (laddr->state != SCTP_ADDR_SRC && 4758a07eb0aSMichio Honda !asoc->src_out_of_asoc_ok)) 476dc022a98SSridhar Samudrala continue; 477854d43a4SAl Viro if (sctp_v4_cmp_addr(&dst_saddr, &laddr->a)) 4781da177e4SLinus Torvalds goto out_unlock; 4791da177e4SLinus Torvalds } 480559cf710SVlad Yasevich rcu_read_unlock(); 4811da177e4SLinus Torvalds 4821da177e4SLinus Torvalds /* None of the bound addresses match the source address of the 4831da177e4SLinus Torvalds * dst. So release it. 4841da177e4SLinus Torvalds */ 4851da177e4SLinus Torvalds dst_release(dst); 4861da177e4SLinus Torvalds dst = NULL; 4871da177e4SLinus Torvalds } 4881da177e4SLinus Torvalds 4891da177e4SLinus Torvalds /* Walk through the bind address list and try to get a dst that 4901da177e4SLinus Torvalds * matches a bind address as the source address. 4911da177e4SLinus Torvalds */ 492559cf710SVlad Yasevich rcu_read_lock(); 493559cf710SVlad Yasevich list_for_each_entry_rcu(laddr, &bp->address_list, list) { 4940ca50d12SMarcelo Ricardo Leitner struct net_device *odev; 4950ca50d12SMarcelo Ricardo Leitner 496559cf710SVlad Yasevich if (!laddr->valid) 497559cf710SVlad Yasevich continue; 49807868284SMarcelo Ricardo Leitner if (laddr->state != SCTP_ADDR_SRC || 49907868284SMarcelo Ricardo Leitner AF_INET != laddr->a.sa.sa_family) 50007868284SMarcelo Ricardo Leitner continue; 50107868284SMarcelo Ricardo Leitner 5029914ae3cSVlad Yasevich fl4->fl4_sport = laddr->a.v4.sin_port; 50385350871SXufeng Zhang flowi4_update_output(fl4, 50485350871SXufeng Zhang asoc->base.sk->sk_bound_dev_if, 5058a9c58d2SXin Long RT_CONN_FLAGS_TOS(asoc->base.sk, tos), 50685350871SXufeng Zhang daddr->v4.sin_addr.s_addr, 50785350871SXufeng Zhang laddr->a.v4.sin_addr.s_addr); 50885350871SXufeng Zhang 509bb2db45bSEric W. Biederman rt = ip_route_output_key(sock_net(sk), fl4); 51007868284SMarcelo Ricardo Leitner if (IS_ERR(rt)) 51107868284SMarcelo Ricardo Leitner continue; 51207868284SMarcelo Ricardo Leitner 5130ca50d12SMarcelo Ricardo Leitner /* Ensure the src address belongs to the output 5140ca50d12SMarcelo Ricardo Leitner * interface. 5150ca50d12SMarcelo Ricardo Leitner */ 5160ca50d12SMarcelo Ricardo Leitner odev = __ip_dev_find(sock_net(sk), laddr->a.v4.sin_addr.s_addr, 5170ca50d12SMarcelo Ricardo Leitner false); 518d82f0f1fSMarcelo Ricardo Leitner if (!odev || odev->ifindex != fl4->flowi4_oif) { 519582eea23SMarcelo Ricardo Leitner if (!dst) { 5204a31a6b1STommi Rantala dst = &rt->dst; 521582eea23SMarcelo Ricardo Leitner t->dst = dst; 522582eea23SMarcelo Ricardo Leitner memcpy(fl, &_fl, sizeof(_fl)); 523582eea23SMarcelo Ricardo Leitner } else { 524d82f0f1fSMarcelo Ricardo Leitner dst_release(&rt->dst); 525582eea23SMarcelo Ricardo Leitner } 5260ca50d12SMarcelo Ricardo Leitner continue; 527d82f0f1fSMarcelo Ricardo Leitner } 5280ca50d12SMarcelo Ricardo Leitner 529410f0383SMarcelo Ricardo Leitner dst_release(dst); 530d8d1f30bSChangli Gao dst = &rt->dst; 531582eea23SMarcelo Ricardo Leitner t->dst = dst; 532582eea23SMarcelo Ricardo Leitner memcpy(fl, &_fl, sizeof(_fl)); 53307868284SMarcelo Ricardo Leitner break; 5341da177e4SLinus Torvalds } 5351da177e4SLinus Torvalds 5361da177e4SLinus Torvalds out_unlock: 537559cf710SVlad Yasevich rcu_read_unlock(); 5381da177e4SLinus Torvalds out: 539582eea23SMarcelo Ricardo Leitner if (dst) { 540bb33381dSDaniel Borkmann pr_debug("rt_dst:%pI4, rt_src:%pI4\n", 541582eea23SMarcelo Ricardo Leitner &fl->u.ip4.daddr, &fl->u.ip4.saddr); 542582eea23SMarcelo Ricardo Leitner } else { 543582eea23SMarcelo Ricardo Leitner t->dst = NULL; 544bb33381dSDaniel Borkmann pr_debug("no route\n"); 5451da177e4SLinus Torvalds } 546582eea23SMarcelo Ricardo Leitner } 5471da177e4SLinus Torvalds 5481da177e4SLinus Torvalds /* For v4, the source address is cached in the route entry(dst). So no need 5491da177e4SLinus Torvalds * to cache it separately and hence this is an empty routine. 5501da177e4SLinus Torvalds */ 551e5117101SYOSHIFUJI Hideaki static void sctp_v4_get_saddr(struct sctp_sock *sk, 5529914ae3cSVlad Yasevich struct sctp_transport *t, 5539914ae3cSVlad Yasevich struct flowi *fl) 5541da177e4SLinus Torvalds { 5559914ae3cSVlad Yasevich union sctp_addr *saddr = &t->saddr; 5569914ae3cSVlad Yasevich struct rtable *rt = (struct rtable *)t->dst; 55723ec47a0SVladislav Yasevich 5581da177e4SLinus Torvalds if (rt) { 5591da177e4SLinus Torvalds saddr->v4.sin_family = AF_INET; 560902ebd3eSDavid S. Miller saddr->v4.sin_addr.s_addr = fl->u.ip4.saddr; 5611da177e4SLinus Torvalds } 5621da177e4SLinus Torvalds } 5631da177e4SLinus Torvalds 5641da177e4SLinus Torvalds /* What interface did this skb arrive on? */ 5651da177e4SLinus Torvalds static int sctp_v4_skb_iif(const struct sk_buff *skb) 5661da177e4SLinus Torvalds { 56792101b3bSDavid S. Miller return inet_iif(skb); 5681da177e4SLinus Torvalds } 5691da177e4SLinus Torvalds 57033e93ed2SXin Long static int sctp_v4_skb_sdif(const struct sk_buff *skb) 57133e93ed2SXin Long { 57233e93ed2SXin Long return inet_sdif(skb); 57333e93ed2SXin Long } 57433e93ed2SXin Long 5751da177e4SLinus Torvalds /* Was this packet marked by Explicit Congestion Notification? */ 5761da177e4SLinus Torvalds static int sctp_v4_is_ce(const struct sk_buff *skb) 5771da177e4SLinus Torvalds { 578eddc9ec5SArnaldo Carvalho de Melo return INET_ECN_is_ce(ip_hdr(skb)->tos); 5791da177e4SLinus Torvalds } 5801da177e4SLinus Torvalds 5811da177e4SLinus Torvalds /* Create and initialize a new sk for the socket returned by accept(). */ 5821da177e4SLinus Torvalds static struct sock *sctp_v4_create_accept_sk(struct sock *sk, 583cdfbabfbSDavid Howells struct sctp_association *asoc, 584cdfbabfbSDavid Howells bool kern) 5851da177e4SLinus Torvalds { 5863b1e0a65SYOSHIFUJI Hideaki struct sock *newsk = sk_alloc(sock_net(sk), PF_INET, GFP_KERNEL, 587cdfbabfbSDavid Howells sk->sk_prot, kern); 588914e1c8bSVlad Yasevich struct inet_sock *newinet; 5891da177e4SLinus Torvalds 5901da177e4SLinus Torvalds if (!newsk) 5911da177e4SLinus Torvalds goto out; 5921da177e4SLinus Torvalds 5931da177e4SLinus Torvalds sock_init_data(NULL, newsk); 5941da177e4SLinus Torvalds 595914e1c8bSVlad Yasevich sctp_copy_sock(newsk, sk, asoc); 5961da177e4SLinus Torvalds sock_reset_flag(newsk, SOCK_ZAPPED); 5971da177e4SLinus Torvalds 598b7e10c25SRichard Haines sctp_v4_copy_ip_options(sk, newsk); 599b7e10c25SRichard Haines 6001da177e4SLinus Torvalds newinet = inet_sk(newsk); 6011da177e4SLinus Torvalds 602c720c7e8SEric Dumazet newinet->inet_daddr = asoc->peer.primary_addr.v4.sin_addr.s_addr; 6031da177e4SLinus Torvalds 604e6848976SArnaldo Carvalho de Melo sk_refcnt_debug_inc(newsk); 6051da177e4SLinus Torvalds 6061da177e4SLinus Torvalds if (newsk->sk_prot->init(newsk)) { 6071da177e4SLinus Torvalds sk_common_release(newsk); 6081da177e4SLinus Torvalds newsk = NULL; 6091da177e4SLinus Torvalds } 6101da177e4SLinus Torvalds 6111da177e4SLinus Torvalds out: 6121da177e4SLinus Torvalds return newsk; 6131da177e4SLinus Torvalds } 6141da177e4SLinus Torvalds 615299ee123SJason Gunthorpe static int sctp_v4_addr_to_user(struct sctp_sock *sp, union sctp_addr *addr) 6161da177e4SLinus Torvalds { 617299ee123SJason Gunthorpe /* No address mapping for V4 sockets */ 61809279e61SXin Long memset(addr->v4.sin_zero, 0, sizeof(addr->v4.sin_zero)); 619299ee123SJason Gunthorpe return sizeof(struct sockaddr_in); 6201da177e4SLinus Torvalds } 6211da177e4SLinus Torvalds 6221da177e4SLinus Torvalds /* Dump the v4 addr to the seq file. */ 6231da177e4SLinus Torvalds static void sctp_v4_seq_dump_addr(struct seq_file *seq, union sctp_addr *addr) 6241da177e4SLinus Torvalds { 62521454aaaSHarvey Harrison seq_printf(seq, "%pI4 ", &addr->v4.sin_addr); 6261da177e4SLinus Torvalds } 6271da177e4SLinus Torvalds 628b9031d9dSVlad Yasevich static void sctp_v4_ecn_capable(struct sock *sk) 629b9031d9dSVlad Yasevich { 630b9031d9dSVlad Yasevich INET_ECN_xmit(sk); 631b9031d9dSVlad Yasevich } 632b9031d9dSVlad Yasevich 6339c3b5751SKees Cook static void sctp_addr_wq_timeout_handler(struct timer_list *t) 6349f7d653bSMichio Honda { 6359c3b5751SKees Cook struct net *net = from_timer(net, t, sctp.addr_wq_timer); 6369f7d653bSMichio Honda struct sctp_sockaddr_entry *addrw, *temp; 6379f7d653bSMichio Honda struct sctp_sock *sp; 6389f7d653bSMichio Honda 6394db67e80SEric W. Biederman spin_lock_bh(&net->sctp.addr_wq_lock); 6409f7d653bSMichio Honda 6414db67e80SEric W. Biederman list_for_each_entry_safe(addrw, temp, &net->sctp.addr_waitq, list) { 642bb33381dSDaniel Borkmann pr_debug("%s: the first ent in wq:%p is addr:%pISc for cmd:%d at " 643bb33381dSDaniel Borkmann "entry:%p\n", __func__, &net->sctp.addr_waitq, &addrw->a.sa, 644bb33381dSDaniel Borkmann addrw->state, addrw); 6459f7d653bSMichio Honda 646dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 6479f7d653bSMichio Honda /* Now we send an ASCONF for each association */ 6489f7d653bSMichio Honda /* Note. we currently don't handle link local IPv6 addressees */ 6499f7d653bSMichio Honda if (addrw->a.sa.sa_family == AF_INET6) { 6509f7d653bSMichio Honda struct in6_addr *in6; 6519f7d653bSMichio Honda 6529f7d653bSMichio Honda if (ipv6_addr_type(&addrw->a.v6.sin6_addr) & 6539f7d653bSMichio Honda IPV6_ADDR_LINKLOCAL) 6549f7d653bSMichio Honda goto free_next; 6559f7d653bSMichio Honda 6569f7d653bSMichio Honda in6 = (struct in6_addr *)&addrw->a.v6.sin6_addr; 6574db67e80SEric W. Biederman if (ipv6_chk_addr(net, in6, NULL, 0) == 0 && 6589f7d653bSMichio Honda addrw->state == SCTP_ADDR_NEW) { 6599f7d653bSMichio Honda unsigned long timeo_val; 6609f7d653bSMichio Honda 661bb33381dSDaniel Borkmann pr_debug("%s: this is on DAD, trying %d sec " 662bb33381dSDaniel Borkmann "later\n", __func__, 6639f7d653bSMichio Honda SCTP_ADDRESS_TICK_DELAY); 664bb33381dSDaniel Borkmann 6659f7d653bSMichio Honda timeo_val = jiffies; 6669f7d653bSMichio Honda timeo_val += msecs_to_jiffies(SCTP_ADDRESS_TICK_DELAY); 6674db67e80SEric W. Biederman mod_timer(&net->sctp.addr_wq_timer, timeo_val); 6689f7d653bSMichio Honda break; 6699f7d653bSMichio Honda } 6709f7d653bSMichio Honda } 6715d0c90cfSDavid S. Miller #endif 6724db67e80SEric W. Biederman list_for_each_entry(sp, &net->sctp.auto_asconf_splist, auto_asconf_list) { 6739f7d653bSMichio Honda struct sock *sk; 6749f7d653bSMichio Honda 6759f7d653bSMichio Honda sk = sctp_opt2sk(sp); 6769f7d653bSMichio Honda /* ignore bound-specific endpoints */ 6779f7d653bSMichio Honda if (!sctp_is_ep_boundall(sk)) 6789f7d653bSMichio Honda continue; 6795bc1d1b4Swangweidong bh_lock_sock(sk); 6809f7d653bSMichio Honda if (sctp_asconf_mgmt(sp, addrw) < 0) 681bb33381dSDaniel Borkmann pr_debug("%s: sctp_asconf_mgmt failed\n", __func__); 6825bc1d1b4Swangweidong bh_unlock_sock(sk); 6839f7d653bSMichio Honda } 68439d84a58SDaniel Halperin #if IS_ENABLED(CONFIG_IPV6) 6859f7d653bSMichio Honda free_next: 68639d84a58SDaniel Halperin #endif 6879f7d653bSMichio Honda list_del(&addrw->list); 6889f7d653bSMichio Honda kfree(addrw); 6899f7d653bSMichio Honda } 6904db67e80SEric W. Biederman spin_unlock_bh(&net->sctp.addr_wq_lock); 6919f7d653bSMichio Honda } 6929f7d653bSMichio Honda 6934db67e80SEric W. Biederman static void sctp_free_addr_wq(struct net *net) 6949f7d653bSMichio Honda { 6959f7d653bSMichio Honda struct sctp_sockaddr_entry *addrw; 6969f7d653bSMichio Honda struct sctp_sockaddr_entry *temp; 6979f7d653bSMichio Honda 6984db67e80SEric W. Biederman spin_lock_bh(&net->sctp.addr_wq_lock); 6994db67e80SEric W. Biederman del_timer(&net->sctp.addr_wq_timer); 7004db67e80SEric W. Biederman list_for_each_entry_safe(addrw, temp, &net->sctp.addr_waitq, list) { 7019f7d653bSMichio Honda list_del(&addrw->list); 7029f7d653bSMichio Honda kfree(addrw); 7039f7d653bSMichio Honda } 7044db67e80SEric W. Biederman spin_unlock_bh(&net->sctp.addr_wq_lock); 7059f7d653bSMichio Honda } 7069f7d653bSMichio Honda 7079f7d653bSMichio Honda /* lookup the entry for the same address in the addr_waitq 7089f7d653bSMichio Honda * sctp_addr_wq MUST be locked 7099f7d653bSMichio Honda */ 7104db67e80SEric W. Biederman static struct sctp_sockaddr_entry *sctp_addr_wq_lookup(struct net *net, 7114db67e80SEric W. Biederman struct sctp_sockaddr_entry *addr) 7129f7d653bSMichio Honda { 7139f7d653bSMichio Honda struct sctp_sockaddr_entry *addrw; 7149f7d653bSMichio Honda 7154db67e80SEric W. Biederman list_for_each_entry(addrw, &net->sctp.addr_waitq, list) { 7169f7d653bSMichio Honda if (addrw->a.sa.sa_family != addr->a.sa.sa_family) 7179f7d653bSMichio Honda continue; 7189f7d653bSMichio Honda if (addrw->a.sa.sa_family == AF_INET) { 7199f7d653bSMichio Honda if (addrw->a.v4.sin_addr.s_addr == 7209f7d653bSMichio Honda addr->a.v4.sin_addr.s_addr) 7219f7d653bSMichio Honda return addrw; 7229f7d653bSMichio Honda } else if (addrw->a.sa.sa_family == AF_INET6) { 7239f7d653bSMichio Honda if (ipv6_addr_equal(&addrw->a.v6.sin6_addr, 7249f7d653bSMichio Honda &addr->a.v6.sin6_addr)) 7259f7d653bSMichio Honda return addrw; 7269f7d653bSMichio Honda } 7279f7d653bSMichio Honda } 7289f7d653bSMichio Honda return NULL; 7299f7d653bSMichio Honda } 7309f7d653bSMichio Honda 7314db67e80SEric W. Biederman void sctp_addr_wq_mgmt(struct net *net, struct sctp_sockaddr_entry *addr, int cmd) 7329f7d653bSMichio Honda { 7339f7d653bSMichio Honda struct sctp_sockaddr_entry *addrw; 7349f7d653bSMichio Honda unsigned long timeo_val; 7359f7d653bSMichio Honda 7369f7d653bSMichio Honda /* first, we check if an opposite message already exist in the queue. 7379f7d653bSMichio Honda * If we found such message, it is removed. 7389f7d653bSMichio Honda * This operation is a bit stupid, but the DHCP client attaches the 7399f7d653bSMichio Honda * new address after a couple of addition and deletion of that address 7409f7d653bSMichio Honda */ 7419f7d653bSMichio Honda 7424db67e80SEric W. Biederman spin_lock_bh(&net->sctp.addr_wq_lock); 7439f7d653bSMichio Honda /* Offsets existing events in addr_wq */ 7444db67e80SEric W. Biederman addrw = sctp_addr_wq_lookup(net, addr); 7459f7d653bSMichio Honda if (addrw) { 7469f7d653bSMichio Honda if (addrw->state != cmd) { 747bb33381dSDaniel Borkmann pr_debug("%s: offsets existing entry for %d, addr:%pISc " 748bb33381dSDaniel Borkmann "in wq:%p\n", __func__, addrw->state, &addrw->a.sa, 7494db67e80SEric W. Biederman &net->sctp.addr_waitq); 750bb33381dSDaniel Borkmann 7519f7d653bSMichio Honda list_del(&addrw->list); 7529f7d653bSMichio Honda kfree(addrw); 7539f7d653bSMichio Honda } 7544db67e80SEric W. Biederman spin_unlock_bh(&net->sctp.addr_wq_lock); 7559f7d653bSMichio Honda return; 7569f7d653bSMichio Honda } 7579f7d653bSMichio Honda 7589f7d653bSMichio Honda /* OK, we have to add the new address to the wait queue */ 7599f7d653bSMichio Honda addrw = kmemdup(addr, sizeof(struct sctp_sockaddr_entry), GFP_ATOMIC); 7609f7d653bSMichio Honda if (addrw == NULL) { 7614db67e80SEric W. Biederman spin_unlock_bh(&net->sctp.addr_wq_lock); 7629f7d653bSMichio Honda return; 7639f7d653bSMichio Honda } 7649f7d653bSMichio Honda addrw->state = cmd; 7654db67e80SEric W. Biederman list_add_tail(&addrw->list, &net->sctp.addr_waitq); 766bb33381dSDaniel Borkmann 767bb33381dSDaniel Borkmann pr_debug("%s: add new entry for cmd:%d, addr:%pISc in wq:%p\n", 768bb33381dSDaniel Borkmann __func__, addrw->state, &addrw->a.sa, &net->sctp.addr_waitq); 7699f7d653bSMichio Honda 7704db67e80SEric W. Biederman if (!timer_pending(&net->sctp.addr_wq_timer)) { 7719f7d653bSMichio Honda timeo_val = jiffies; 7729f7d653bSMichio Honda timeo_val += msecs_to_jiffies(SCTP_ADDRESS_TICK_DELAY); 7734db67e80SEric W. Biederman mod_timer(&net->sctp.addr_wq_timer, timeo_val); 7749f7d653bSMichio Honda } 7754db67e80SEric W. Biederman spin_unlock_bh(&net->sctp.addr_wq_lock); 7769f7d653bSMichio Honda } 7779f7d653bSMichio Honda 77829303547SVlad Yasevich /* Event handler for inet address addition/deletion events. 77929303547SVlad Yasevich * The sctp_local_addr_list needs to be protocted by a spin lock since 78029303547SVlad Yasevich * multiple notifiers (say IPv4 and IPv6) may be running at the same 78129303547SVlad Yasevich * time and thus corrupt the list. 78229303547SVlad Yasevich * The reader side is protected with RCU. 78329303547SVlad Yasevich */ 78424123186SAdrian Bunk static int sctp_inetaddr_event(struct notifier_block *this, unsigned long ev, 7851da177e4SLinus Torvalds void *ptr) 7861da177e4SLinus Torvalds { 78729c7cf96SSridhar Samudrala struct in_ifaddr *ifa = (struct in_ifaddr *)ptr; 78829303547SVlad Yasevich struct sctp_sockaddr_entry *addr = NULL; 78929303547SVlad Yasevich struct sctp_sockaddr_entry *temp; 7904db67e80SEric W. Biederman struct net *net = dev_net(ifa->ifa_dev->dev); 79122626216SChidambar 'ilLogict' Zinnoury int found = 0; 7921da177e4SLinus Torvalds 79329c7cf96SSridhar Samudrala switch (ev) { 79429c7cf96SSridhar Samudrala case NETDEV_UP: 795400b8b9aSXin Long addr = kzalloc(sizeof(*addr), GFP_ATOMIC); 79629c7cf96SSridhar Samudrala if (addr) { 79729c7cf96SSridhar Samudrala addr->a.v4.sin_family = AF_INET; 79829c7cf96SSridhar Samudrala addr->a.v4.sin_addr.s_addr = ifa->ifa_local; 79929303547SVlad Yasevich addr->valid = 1; 8004db67e80SEric W. Biederman spin_lock_bh(&net->sctp.local_addr_lock); 8014db67e80SEric W. Biederman list_add_tail_rcu(&addr->list, &net->sctp.local_addr_list); 8024db67e80SEric W. Biederman sctp_addr_wq_mgmt(net, addr, SCTP_ADDR_NEW); 8034db67e80SEric W. Biederman spin_unlock_bh(&net->sctp.local_addr_lock); 80429c7cf96SSridhar Samudrala } 80529c7cf96SSridhar Samudrala break; 80629c7cf96SSridhar Samudrala case NETDEV_DOWN: 8074db67e80SEric W. Biederman spin_lock_bh(&net->sctp.local_addr_lock); 80829303547SVlad Yasevich list_for_each_entry_safe(addr, temp, 8094db67e80SEric W. Biederman &net->sctp.local_addr_list, list) { 810a40a7d15SPavel Emelyanov if (addr->a.sa.sa_family == AF_INET && 811a40a7d15SPavel Emelyanov addr->a.v4.sin_addr.s_addr == 812a40a7d15SPavel Emelyanov ifa->ifa_local) { 8134db67e80SEric W. Biederman sctp_addr_wq_mgmt(net, addr, SCTP_ADDR_DEL); 81422626216SChidambar 'ilLogict' Zinnoury found = 1; 81529303547SVlad Yasevich addr->valid = 0; 81629303547SVlad Yasevich list_del_rcu(&addr->list); 81729c7cf96SSridhar Samudrala break; 81829c7cf96SSridhar Samudrala } 81929c7cf96SSridhar Samudrala } 8204db67e80SEric W. Biederman spin_unlock_bh(&net->sctp.local_addr_lock); 82122626216SChidambar 'ilLogict' Zinnoury if (found) 8221231f0baSLai Jiangshan kfree_rcu(addr, rcu); 82329c7cf96SSridhar Samudrala break; 82429c7cf96SSridhar Samudrala } 8251da177e4SLinus Torvalds 8261da177e4SLinus Torvalds return NOTIFY_DONE; 8271da177e4SLinus Torvalds } 8281da177e4SLinus Torvalds 8291da177e4SLinus Torvalds /* 8301da177e4SLinus Torvalds * Initialize the control inode/socket with a control endpoint data 8311da177e4SLinus Torvalds * structure. This endpoint is reserved exclusively for the OOTB processing. 8321da177e4SLinus Torvalds */ 8332ce95503SEric W. Biederman static int sctp_ctl_sock_init(struct net *net) 8341da177e4SLinus Torvalds { 8351da177e4SLinus Torvalds int err; 836fb13d9f9SBrian Haley sa_family_t family = PF_INET; 8371da177e4SLinus Torvalds 8381da177e4SLinus Torvalds if (sctp_get_pf_specific(PF_INET6)) 8391da177e4SLinus Torvalds family = PF_INET6; 8401da177e4SLinus Torvalds 8412ce95503SEric W. Biederman err = inet_ctl_sock_create(&net->sctp.ctl_sock, family, 8422ce95503SEric W. Biederman SOCK_SEQPACKET, IPPROTO_SCTP, net); 843fb13d9f9SBrian Haley 844fb13d9f9SBrian Haley /* If IPv6 socket could not be created, try the IPv4 socket */ 845fb13d9f9SBrian Haley if (err < 0 && family == PF_INET6) 8462ce95503SEric W. Biederman err = inet_ctl_sock_create(&net->sctp.ctl_sock, AF_INET, 847fb13d9f9SBrian Haley SOCK_SEQPACKET, IPPROTO_SCTP, 8482ce95503SEric W. Biederman net); 849fb13d9f9SBrian Haley 8501da177e4SLinus Torvalds if (err < 0) { 851145ce502SJoe Perches pr_err("Failed to create the SCTP control socket\n"); 8521da177e4SLinus Torvalds return err; 8531da177e4SLinus Torvalds } 8541da177e4SLinus Torvalds return 0; 8551da177e4SLinus Torvalds } 8561da177e4SLinus Torvalds 857965ae444SXin Long static int sctp_udp_rcv(struct sock *sk, struct sk_buff *skb) 858965ae444SXin Long { 859a1dd2cf2SXin Long SCTP_INPUT_CB(skb)->encap_port = udp_hdr(skb)->source; 860a1dd2cf2SXin Long 861965ae444SXin Long skb_set_transport_header(skb, sizeof(struct udphdr)); 862965ae444SXin Long sctp_rcv(skb); 863965ae444SXin Long return 0; 864965ae444SXin Long } 865965ae444SXin Long 866965ae444SXin Long int sctp_udp_sock_start(struct net *net) 867965ae444SXin Long { 868965ae444SXin Long struct udp_tunnel_sock_cfg tuncfg = {NULL}; 869965ae444SXin Long struct udp_port_cfg udp_conf = {0}; 870965ae444SXin Long struct socket *sock; 871965ae444SXin Long int err; 872965ae444SXin Long 873965ae444SXin Long udp_conf.family = AF_INET; 874965ae444SXin Long udp_conf.local_ip.s_addr = htonl(INADDR_ANY); 875965ae444SXin Long udp_conf.local_udp_port = htons(net->sctp.udp_port); 876965ae444SXin Long err = udp_sock_create(net, &udp_conf, &sock); 877965ae444SXin Long if (err) { 878965ae444SXin Long pr_err("Failed to create the SCTP UDP tunneling v4 sock\n"); 879965ae444SXin Long return err; 880965ae444SXin Long } 881965ae444SXin Long 882965ae444SXin Long tuncfg.encap_type = 1; 883965ae444SXin Long tuncfg.encap_rcv = sctp_udp_rcv; 8849e47df00SXin Long tuncfg.encap_err_lookup = sctp_udp_v4_err; 885965ae444SXin Long setup_udp_tunnel_sock(net, sock, &tuncfg); 886965ae444SXin Long net->sctp.udp4_sock = sock->sk; 887965ae444SXin Long 8889d6ba260SXin Long #if IS_ENABLED(CONFIG_IPV6) 8899d6ba260SXin Long memset(&udp_conf, 0, sizeof(udp_conf)); 8909d6ba260SXin Long 8919d6ba260SXin Long udp_conf.family = AF_INET6; 8929d6ba260SXin Long udp_conf.local_ip6 = in6addr_any; 8939d6ba260SXin Long udp_conf.local_udp_port = htons(net->sctp.udp_port); 8949d6ba260SXin Long udp_conf.use_udp6_rx_checksums = true; 8959d6ba260SXin Long udp_conf.ipv6_v6only = true; 8969d6ba260SXin Long err = udp_sock_create(net, &udp_conf, &sock); 8979d6ba260SXin Long if (err) { 8989d6ba260SXin Long pr_err("Failed to create the SCTP UDP tunneling v6 sock\n"); 8999d6ba260SXin Long udp_tunnel_sock_release(net->sctp.udp4_sock->sk_socket); 9009d6ba260SXin Long net->sctp.udp4_sock = NULL; 9019d6ba260SXin Long return err; 9029d6ba260SXin Long } 9039d6ba260SXin Long 9049d6ba260SXin Long tuncfg.encap_type = 1; 9059d6ba260SXin Long tuncfg.encap_rcv = sctp_udp_rcv; 9069e47df00SXin Long tuncfg.encap_err_lookup = sctp_udp_v6_err; 9079d6ba260SXin Long setup_udp_tunnel_sock(net, sock, &tuncfg); 9089d6ba260SXin Long net->sctp.udp6_sock = sock->sk; 9099d6ba260SXin Long #endif 9109d6ba260SXin Long 911965ae444SXin Long return 0; 912965ae444SXin Long } 913965ae444SXin Long 914965ae444SXin Long void sctp_udp_sock_stop(struct net *net) 915965ae444SXin Long { 916965ae444SXin Long if (net->sctp.udp4_sock) { 917965ae444SXin Long udp_tunnel_sock_release(net->sctp.udp4_sock->sk_socket); 918965ae444SXin Long net->sctp.udp4_sock = NULL; 919965ae444SXin Long } 9209d6ba260SXin Long if (net->sctp.udp6_sock) { 9219d6ba260SXin Long udp_tunnel_sock_release(net->sctp.udp6_sock->sk_socket); 9229d6ba260SXin Long net->sctp.udp6_sock = NULL; 9239d6ba260SXin Long } 924965ae444SXin Long } 925965ae444SXin Long 9261da177e4SLinus Torvalds /* Register address family specific functions. */ 9271da177e4SLinus Torvalds int sctp_register_af(struct sctp_af *af) 9281da177e4SLinus Torvalds { 9291da177e4SLinus Torvalds switch (af->sa_family) { 9301da177e4SLinus Torvalds case AF_INET: 9311da177e4SLinus Torvalds if (sctp_af_v4_specific) 9321da177e4SLinus Torvalds return 0; 9331da177e4SLinus Torvalds sctp_af_v4_specific = af; 9341da177e4SLinus Torvalds break; 9351da177e4SLinus Torvalds case AF_INET6: 9361da177e4SLinus Torvalds if (sctp_af_v6_specific) 9371da177e4SLinus Torvalds return 0; 9381da177e4SLinus Torvalds sctp_af_v6_specific = af; 9391da177e4SLinus Torvalds break; 9401da177e4SLinus Torvalds default: 9411da177e4SLinus Torvalds return 0; 9421da177e4SLinus Torvalds } 9431da177e4SLinus Torvalds 9441da177e4SLinus Torvalds INIT_LIST_HEAD(&af->list); 9451da177e4SLinus Torvalds list_add_tail(&af->list, &sctp_address_families); 9461da177e4SLinus Torvalds return 1; 9471da177e4SLinus Torvalds } 9481da177e4SLinus Torvalds 9491da177e4SLinus Torvalds /* Get the table of functions for manipulating a particular address 9501da177e4SLinus Torvalds * family. 9511da177e4SLinus Torvalds */ 9521da177e4SLinus Torvalds struct sctp_af *sctp_get_af_specific(sa_family_t family) 9531da177e4SLinus Torvalds { 9541da177e4SLinus Torvalds switch (family) { 9551da177e4SLinus Torvalds case AF_INET: 9561da177e4SLinus Torvalds return sctp_af_v4_specific; 9571da177e4SLinus Torvalds case AF_INET6: 9581da177e4SLinus Torvalds return sctp_af_v6_specific; 9591da177e4SLinus Torvalds default: 9601da177e4SLinus Torvalds return NULL; 9611da177e4SLinus Torvalds } 9621da177e4SLinus Torvalds } 9631da177e4SLinus Torvalds 9641da177e4SLinus Torvalds /* Common code to initialize a AF_INET msg_name. */ 9651da177e4SLinus Torvalds static void sctp_inet_msgname(char *msgname, int *addr_len) 9661da177e4SLinus Torvalds { 9671da177e4SLinus Torvalds struct sockaddr_in *sin; 9681da177e4SLinus Torvalds 9691da177e4SLinus Torvalds sin = (struct sockaddr_in *)msgname; 9701da177e4SLinus Torvalds *addr_len = sizeof(struct sockaddr_in); 9711da177e4SLinus Torvalds sin->sin_family = AF_INET; 9721da177e4SLinus Torvalds memset(sin->sin_zero, 0, sizeof(sin->sin_zero)); 9731da177e4SLinus Torvalds } 9741da177e4SLinus Torvalds 9751da177e4SLinus Torvalds /* Copy the primary address of the peer primary address as the msg_name. */ 9761da177e4SLinus Torvalds static void sctp_inet_event_msgname(struct sctp_ulpevent *event, char *msgname, 9771da177e4SLinus Torvalds int *addr_len) 9781da177e4SLinus Torvalds { 9791da177e4SLinus Torvalds struct sockaddr_in *sin, *sinfrom; 9801da177e4SLinus Torvalds 9811da177e4SLinus Torvalds if (msgname) { 9821da177e4SLinus Torvalds struct sctp_association *asoc; 9831da177e4SLinus Torvalds 9841da177e4SLinus Torvalds asoc = event->asoc; 9851da177e4SLinus Torvalds sctp_inet_msgname(msgname, addr_len); 9861da177e4SLinus Torvalds sin = (struct sockaddr_in *)msgname; 9871da177e4SLinus Torvalds sinfrom = &asoc->peer.primary_addr.v4; 9881da177e4SLinus Torvalds sin->sin_port = htons(asoc->peer.port); 9891da177e4SLinus Torvalds sin->sin_addr.s_addr = sinfrom->sin_addr.s_addr; 9901da177e4SLinus Torvalds } 9911da177e4SLinus Torvalds } 9921da177e4SLinus Torvalds 9931da177e4SLinus Torvalds /* Initialize and copy out a msgname from an inbound skb. */ 9941da177e4SLinus Torvalds static void sctp_inet_skb_msgname(struct sk_buff *skb, char *msgname, int *len) 9951da177e4SLinus Torvalds { 9961da177e4SLinus Torvalds if (msgname) { 9972c0fd387SArnaldo Carvalho de Melo struct sctphdr *sh = sctp_hdr(skb); 9982c0fd387SArnaldo Carvalho de Melo struct sockaddr_in *sin = (struct sockaddr_in *)msgname; 9992c0fd387SArnaldo Carvalho de Melo 10001da177e4SLinus Torvalds sctp_inet_msgname(msgname, len); 10011da177e4SLinus Torvalds sin->sin_port = sh->source; 1002eddc9ec5SArnaldo Carvalho de Melo sin->sin_addr.s_addr = ip_hdr(skb)->saddr; 10031da177e4SLinus Torvalds } 10041da177e4SLinus Torvalds } 10051da177e4SLinus Torvalds 10061da177e4SLinus Torvalds /* Do we support this AF? */ 10071da177e4SLinus Torvalds static int sctp_inet_af_supported(sa_family_t family, struct sctp_sock *sp) 10081da177e4SLinus Torvalds { 10091da177e4SLinus Torvalds /* PF_INET only supports AF_INET addresses. */ 1010a02cec21SEric Dumazet return AF_INET == family; 10111da177e4SLinus Torvalds } 10121da177e4SLinus Torvalds 10131da177e4SLinus Torvalds /* Address matching with wildcards allowed. */ 10141da177e4SLinus Torvalds static int sctp_inet_cmp_addr(const union sctp_addr *addr1, 10151da177e4SLinus Torvalds const union sctp_addr *addr2, 10161da177e4SLinus Torvalds struct sctp_sock *opt) 10171da177e4SLinus Torvalds { 10181da177e4SLinus Torvalds /* PF_INET only supports AF_INET addresses. */ 10191da177e4SLinus Torvalds if (addr1->sa.sa_family != addr2->sa.sa_family) 10201da177e4SLinus Torvalds return 0; 1021e6f1cebfSAl Viro if (htonl(INADDR_ANY) == addr1->v4.sin_addr.s_addr || 1022e6f1cebfSAl Viro htonl(INADDR_ANY) == addr2->v4.sin_addr.s_addr) 10231da177e4SLinus Torvalds return 1; 10241da177e4SLinus Torvalds if (addr1->v4.sin_addr.s_addr == addr2->v4.sin_addr.s_addr) 10251da177e4SLinus Torvalds return 1; 10261da177e4SLinus Torvalds 10271da177e4SLinus Torvalds return 0; 10281da177e4SLinus Torvalds } 10291da177e4SLinus Torvalds 10301da177e4SLinus Torvalds /* Verify that provided sockaddr looks bindable. Common verification has 10311da177e4SLinus Torvalds * already been taken care of. 10321da177e4SLinus Torvalds */ 10331da177e4SLinus Torvalds static int sctp_inet_bind_verify(struct sctp_sock *opt, union sctp_addr *addr) 10341da177e4SLinus Torvalds { 10351da177e4SLinus Torvalds return sctp_v4_available(addr, opt); 10361da177e4SLinus Torvalds } 10371da177e4SLinus Torvalds 10381da177e4SLinus Torvalds /* Verify that sockaddr looks sendable. Common verification has already 10391da177e4SLinus Torvalds * been taken care of. 10401da177e4SLinus Torvalds */ 10411da177e4SLinus Torvalds static int sctp_inet_send_verify(struct sctp_sock *opt, union sctp_addr *addr) 10421da177e4SLinus Torvalds { 10431da177e4SLinus Torvalds return 1; 10441da177e4SLinus Torvalds } 10451da177e4SLinus Torvalds 10461da177e4SLinus Torvalds /* Fill in Supported Address Type information for INIT and INIT-ACK 10471da177e4SLinus Torvalds * chunks. Returns number of addresses supported. 10481da177e4SLinus Torvalds */ 10491da177e4SLinus Torvalds static int sctp_inet_supported_addrs(const struct sctp_sock *opt, 10503dbe8656SAl Viro __be16 *types) 10511da177e4SLinus Torvalds { 10521da177e4SLinus Torvalds types[0] = SCTP_PARAM_IPV4_ADDRESS; 10531da177e4SLinus Torvalds return 1; 10541da177e4SLinus Torvalds } 10551da177e4SLinus Torvalds 10561da177e4SLinus Torvalds /* Wrapper routine that calls the ip transmit routine. */ 1057600af7fdSXin Long static inline int sctp_v4_xmit(struct sk_buff *skb, struct sctp_transport *t) 10581da177e4SLinus Torvalds { 1059600af7fdSXin Long struct dst_entry *dst = dst_clone(t->dst); 1060600af7fdSXin Long struct flowi4 *fl4 = &t->fl.u.ip4; 1061600af7fdSXin Long struct sock *sk = skb->sk; 1062600af7fdSXin Long struct inet_sock *inet = inet_sk(sk); 10638a9c58d2SXin Long __u8 dscp = inet->tos; 1064600af7fdSXin Long __be16 df = 0; 1065f880374cSHerbert Xu 1066bb33381dSDaniel Borkmann pr_debug("%s: skb:%p, len:%d, src:%pI4, dst:%pI4\n", __func__, skb, 1067600af7fdSXin Long skb->len, &fl4->saddr, &fl4->daddr); 10688a9c58d2SXin Long 1069600af7fdSXin Long if (t->dscp & SCTP_DSCP_SET_MASK) 1070600af7fdSXin Long dscp = t->dscp & SCTP_DSCP_VAL_MASK; 10711da177e4SLinus Torvalds 1072600af7fdSXin Long inet->pmtudisc = t->param_flags & SPP_PMTUD_ENABLE ? IP_PMTUDISC_DO 1073600af7fdSXin Long : IP_PMTUDISC_DONT; 1074600af7fdSXin Long SCTP_INC_STATS(sock_net(sk), SCTP_MIB_OUTSCTPPACKS); 1075f880374cSHerbert Xu 1076600af7fdSXin Long if (!t->encap_port || !sctp_sk(sk)->udp_port) { 1077600af7fdSXin Long skb_dst_set(skb, dst); 1078600af7fdSXin Long return __ip_queue_xmit(sk, skb, &t->fl, dscp); 1079600af7fdSXin Long } 1080bb33381dSDaniel Borkmann 1081600af7fdSXin Long if (skb_is_gso(skb)) 1082600af7fdSXin Long skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL_CSUM; 1083600af7fdSXin Long 1084600af7fdSXin Long if (ip_dont_fragment(sk, dst) && !skb->ignore_df) 1085600af7fdSXin Long df = htons(IP_DF); 1086600af7fdSXin Long 1087600af7fdSXin Long skb->encapsulation = 1; 1088600af7fdSXin Long skb_reset_inner_mac_header(skb); 1089600af7fdSXin Long skb_reset_inner_transport_header(skb); 1090600af7fdSXin Long skb_set_inner_ipproto(skb, IPPROTO_SCTP); 1091600af7fdSXin Long udp_tunnel_xmit_skb((struct rtable *)dst, sk, skb, fl4->saddr, 1092600af7fdSXin Long fl4->daddr, dscp, ip4_dst_hoplimit(dst), df, 1093600af7fdSXin Long sctp_sk(sk)->udp_port, t->encap_port, false, false); 1094600af7fdSXin Long return 0; 10951da177e4SLinus Torvalds } 10961da177e4SLinus Torvalds 109715efbe76SNeil Horman static struct sctp_af sctp_af_inet; 10981da177e4SLinus Torvalds 10991da177e4SLinus Torvalds static struct sctp_pf sctp_pf_inet = { 11001da177e4SLinus Torvalds .event_msgname = sctp_inet_event_msgname, 11011da177e4SLinus Torvalds .skb_msgname = sctp_inet_skb_msgname, 11021da177e4SLinus Torvalds .af_supported = sctp_inet_af_supported, 11031da177e4SLinus Torvalds .cmp_addr = sctp_inet_cmp_addr, 11041da177e4SLinus Torvalds .bind_verify = sctp_inet_bind_verify, 11051da177e4SLinus Torvalds .send_verify = sctp_inet_send_verify, 11061da177e4SLinus Torvalds .supported_addrs = sctp_inet_supported_addrs, 11071da177e4SLinus Torvalds .create_accept_sk = sctp_v4_create_accept_sk, 1108299ee123SJason Gunthorpe .addr_to_user = sctp_v4_addr_to_user, 1109299ee123SJason Gunthorpe .to_sk_saddr = sctp_v4_to_sk_saddr, 1110299ee123SJason Gunthorpe .to_sk_daddr = sctp_v4_to_sk_daddr, 1111b7e10c25SRichard Haines .copy_ip_options = sctp_v4_copy_ip_options, 111215efbe76SNeil Horman .af = &sctp_af_inet 11131da177e4SLinus Torvalds }; 11141da177e4SLinus Torvalds 11151da177e4SLinus Torvalds /* Notifier for inetaddr addition/deletion events. */ 11161da177e4SLinus Torvalds static struct notifier_block sctp_inetaddr_notifier = { 11171da177e4SLinus Torvalds .notifier_call = sctp_inetaddr_event, 11181da177e4SLinus Torvalds }; 11191da177e4SLinus Torvalds 11201da177e4SLinus Torvalds /* Socket operations. */ 112190ddc4f0SEric Dumazet static const struct proto_ops inet_seqpacket_ops = { 11221da177e4SLinus Torvalds .family = PF_INET, 11231da177e4SLinus Torvalds .owner = THIS_MODULE, 11241da177e4SLinus Torvalds .release = inet_release, /* Needs to be wrapped... */ 11251da177e4SLinus Torvalds .bind = inet_bind, 1126644fbdeaSXin Long .connect = sctp_inet_connect, 11271da177e4SLinus Torvalds .socketpair = sock_no_socketpair, 11281da177e4SLinus Torvalds .accept = inet_accept, 11291da177e4SLinus Torvalds .getname = inet_getname, /* Semantics are different. */ 1130a11e1d43SLinus Torvalds .poll = sctp_poll, 11311da177e4SLinus Torvalds .ioctl = inet_ioctl, 1132c7cbdbf2SArnd Bergmann .gettstamp = sock_gettstamp, 11331da177e4SLinus Torvalds .listen = sctp_inet_listen, 11341da177e4SLinus Torvalds .shutdown = inet_shutdown, /* Looks harmless. */ 1135543d9cfeSArnaldo Carvalho de Melo .setsockopt = sock_common_setsockopt, /* IP_SOL IP_OPTION is a problem */ 11361da177e4SLinus Torvalds .getsockopt = sock_common_getsockopt, 11371da177e4SLinus Torvalds .sendmsg = inet_sendmsg, 1138fd2d180aSXin Long .recvmsg = inet_recvmsg, 11391da177e4SLinus Torvalds .mmap = sock_no_mmap, 11401da177e4SLinus Torvalds .sendpage = sock_no_sendpage, 11411da177e4SLinus Torvalds }; 11421da177e4SLinus Torvalds 11431da177e4SLinus Torvalds /* Registration with AF_INET family. */ 11441da177e4SLinus Torvalds static struct inet_protosw sctp_seqpacket_protosw = { 11451da177e4SLinus Torvalds .type = SOCK_SEQPACKET, 11461da177e4SLinus Torvalds .protocol = IPPROTO_SCTP, 11471da177e4SLinus Torvalds .prot = &sctp_prot, 11481da177e4SLinus Torvalds .ops = &inet_seqpacket_ops, 11491da177e4SLinus Torvalds .flags = SCTP_PROTOSW_FLAG 11501da177e4SLinus Torvalds }; 11511da177e4SLinus Torvalds static struct inet_protosw sctp_stream_protosw = { 11521da177e4SLinus Torvalds .type = SOCK_STREAM, 11531da177e4SLinus Torvalds .protocol = IPPROTO_SCTP, 11541da177e4SLinus Torvalds .prot = &sctp_prot, 11551da177e4SLinus Torvalds .ops = &inet_seqpacket_ops, 11561da177e4SLinus Torvalds .flags = SCTP_PROTOSW_FLAG 11571da177e4SLinus Torvalds }; 11581da177e4SLinus Torvalds 1159a1dd2cf2SXin Long static int sctp4_rcv(struct sk_buff *skb) 1160a1dd2cf2SXin Long { 11610356010dSXin Long SCTP_INPUT_CB(skb)->encap_port = 0; 1162a1dd2cf2SXin Long return sctp_rcv(skb); 1163a1dd2cf2SXin Long } 1164a1dd2cf2SXin Long 11651da177e4SLinus Torvalds /* Register with IP layer. */ 116632613090SAlexey Dobriyan static const struct net_protocol sctp_protocol = { 1167a1dd2cf2SXin Long .handler = sctp4_rcv, 11681da177e4SLinus Torvalds .err_handler = sctp_v4_err, 11691da177e4SLinus Torvalds .no_policy = 1, 11708ed1dc44SHannes Frederic Sowa .icmp_strict_tag_validation = 1, 11711da177e4SLinus Torvalds }; 11721da177e4SLinus Torvalds 11731da177e4SLinus Torvalds /* IPv4 address related functions. */ 117415efbe76SNeil Horman static struct sctp_af sctp_af_inet = { 1175543d9cfeSArnaldo Carvalho de Melo .sa_family = AF_INET, 11761da177e4SLinus Torvalds .sctp_xmit = sctp_v4_xmit, 11771da177e4SLinus Torvalds .setsockopt = ip_setsockopt, 11781da177e4SLinus Torvalds .getsockopt = ip_getsockopt, 11791da177e4SLinus Torvalds .get_dst = sctp_v4_get_dst, 11801da177e4SLinus Torvalds .get_saddr = sctp_v4_get_saddr, 11811da177e4SLinus Torvalds .copy_addrlist = sctp_v4_copy_addrlist, 11821da177e4SLinus Torvalds .from_skb = sctp_v4_from_skb, 11831da177e4SLinus Torvalds .from_sk = sctp_v4_from_sk, 11841da177e4SLinus Torvalds .from_addr_param = sctp_v4_from_addr_param, 11851da177e4SLinus Torvalds .to_addr_param = sctp_v4_to_addr_param, 11861da177e4SLinus Torvalds .cmp_addr = sctp_v4_cmp_addr, 11871da177e4SLinus Torvalds .addr_valid = sctp_v4_addr_valid, 11881da177e4SLinus Torvalds .inaddr_any = sctp_v4_inaddr_any, 11891da177e4SLinus Torvalds .is_any = sctp_v4_is_any, 11901da177e4SLinus Torvalds .available = sctp_v4_available, 11911da177e4SLinus Torvalds .scope = sctp_v4_scope, 11921da177e4SLinus Torvalds .skb_iif = sctp_v4_skb_iif, 119333e93ed2SXin Long .skb_sdif = sctp_v4_skb_sdif, 11941da177e4SLinus Torvalds .is_ce = sctp_v4_is_ce, 11951da177e4SLinus Torvalds .seq_dump_addr = sctp_v4_seq_dump_addr, 1196b9031d9dSVlad Yasevich .ecn_capable = sctp_v4_ecn_capable, 11971da177e4SLinus Torvalds .net_header_len = sizeof(struct iphdr), 11981da177e4SLinus Torvalds .sockaddr_len = sizeof(struct sockaddr_in), 1199b7e10c25SRichard Haines .ip_options_len = sctp_v4_ip_options_len, 12001da177e4SLinus Torvalds }; 12011da177e4SLinus Torvalds 12028d72651dSwangweidong struct sctp_pf *sctp_get_pf_specific(sa_family_t family) 12038d72651dSwangweidong { 12041da177e4SLinus Torvalds switch (family) { 12051da177e4SLinus Torvalds case PF_INET: 12061da177e4SLinus Torvalds return sctp_pf_inet_specific; 12071da177e4SLinus Torvalds case PF_INET6: 12081da177e4SLinus Torvalds return sctp_pf_inet6_specific; 12091da177e4SLinus Torvalds default: 12101da177e4SLinus Torvalds return NULL; 12111da177e4SLinus Torvalds } 12121da177e4SLinus Torvalds } 12131da177e4SLinus Torvalds 12141da177e4SLinus Torvalds /* Register the PF specific function table. */ 12151da177e4SLinus Torvalds int sctp_register_pf(struct sctp_pf *pf, sa_family_t family) 12161da177e4SLinus Torvalds { 12171da177e4SLinus Torvalds switch (family) { 12181da177e4SLinus Torvalds case PF_INET: 12191da177e4SLinus Torvalds if (sctp_pf_inet_specific) 12201da177e4SLinus Torvalds return 0; 12211da177e4SLinus Torvalds sctp_pf_inet_specific = pf; 12221da177e4SLinus Torvalds break; 12231da177e4SLinus Torvalds case PF_INET6: 12241da177e4SLinus Torvalds if (sctp_pf_inet6_specific) 12251da177e4SLinus Torvalds return 0; 12261da177e4SLinus Torvalds sctp_pf_inet6_specific = pf; 12271da177e4SLinus Torvalds break; 12281da177e4SLinus Torvalds default: 12291da177e4SLinus Torvalds return 0; 12301da177e4SLinus Torvalds } 12311da177e4SLinus Torvalds return 1; 12321da177e4SLinus Torvalds } 12331da177e4SLinus Torvalds 1234b01a2407SEric W. Biederman static inline int init_sctp_mibs(struct net *net) 12351da177e4SLinus Torvalds { 1236698365faSWANG Cong net->sctp.sctp_statistics = alloc_percpu(struct sctp_mib); 1237698365faSWANG Cong if (!net->sctp.sctp_statistics) 1238698365faSWANG Cong return -ENOMEM; 1239698365faSWANG Cong return 0; 12401da177e4SLinus Torvalds } 12411da177e4SLinus Torvalds 1242b01a2407SEric W. Biederman static inline void cleanup_sctp_mibs(struct net *net) 12431da177e4SLinus Torvalds { 1244698365faSWANG Cong free_percpu(net->sctp.sctp_statistics); 12451da177e4SLinus Torvalds } 12461da177e4SLinus Torvalds 1247270637abSVlad Yasevich static void sctp_v4_pf_init(void) 1248270637abSVlad Yasevich { 1249270637abSVlad Yasevich /* Initialize the SCTP specific PF functions. */ 1250270637abSVlad Yasevich sctp_register_pf(&sctp_pf_inet, PF_INET); 1251270637abSVlad Yasevich sctp_register_af(&sctp_af_inet); 1252270637abSVlad Yasevich } 1253270637abSVlad Yasevich 1254270637abSVlad Yasevich static void sctp_v4_pf_exit(void) 1255270637abSVlad Yasevich { 1256270637abSVlad Yasevich list_del(&sctp_af_inet.list); 1257270637abSVlad Yasevich } 1258270637abSVlad Yasevich 1259270637abSVlad Yasevich static int sctp_v4_protosw_init(void) 1260270637abSVlad Yasevich { 1261270637abSVlad Yasevich int rc; 1262270637abSVlad Yasevich 1263270637abSVlad Yasevich rc = proto_register(&sctp_prot, 1); 1264270637abSVlad Yasevich if (rc) 1265270637abSVlad Yasevich return rc; 1266270637abSVlad Yasevich 1267270637abSVlad Yasevich /* Register SCTP(UDP and TCP style) with socket layer. */ 1268270637abSVlad Yasevich inet_register_protosw(&sctp_seqpacket_protosw); 1269270637abSVlad Yasevich inet_register_protosw(&sctp_stream_protosw); 1270270637abSVlad Yasevich 1271270637abSVlad Yasevich return 0; 1272270637abSVlad Yasevich } 1273270637abSVlad Yasevich 1274270637abSVlad Yasevich static void sctp_v4_protosw_exit(void) 1275270637abSVlad Yasevich { 1276270637abSVlad Yasevich inet_unregister_protosw(&sctp_stream_protosw); 1277270637abSVlad Yasevich inet_unregister_protosw(&sctp_seqpacket_protosw); 1278270637abSVlad Yasevich proto_unregister(&sctp_prot); 1279270637abSVlad Yasevich } 1280270637abSVlad Yasevich 1281270637abSVlad Yasevich static int sctp_v4_add_protocol(void) 1282270637abSVlad Yasevich { 1283270637abSVlad Yasevich /* Register notifier for inet address additions/deletions. */ 1284270637abSVlad Yasevich register_inetaddr_notifier(&sctp_inetaddr_notifier); 1285270637abSVlad Yasevich 1286270637abSVlad Yasevich /* Register SCTP with inet layer. */ 1287270637abSVlad Yasevich if (inet_add_protocol(&sctp_protocol, IPPROTO_SCTP) < 0) 1288270637abSVlad Yasevich return -EAGAIN; 1289270637abSVlad Yasevich 1290270637abSVlad Yasevich return 0; 1291270637abSVlad Yasevich } 1292270637abSVlad Yasevich 1293270637abSVlad Yasevich static void sctp_v4_del_protocol(void) 1294270637abSVlad Yasevich { 1295270637abSVlad Yasevich inet_del_protocol(&sctp_protocol, IPPROTO_SCTP); 1296270637abSVlad Yasevich unregister_inetaddr_notifier(&sctp_inetaddr_notifier); 1297270637abSVlad Yasevich } 1298270637abSVlad Yasevich 12998e2d61e0SMarcelo Ricardo Leitner static int __net_init sctp_defaults_init(struct net *net) 13004db67e80SEric W. Biederman { 13012ce95503SEric W. Biederman int status; 13022ce95503SEric W. Biederman 1303e1fc3b14SEric W. Biederman /* 1304e1fc3b14SEric W. Biederman * 14. Suggested SCTP Protocol Parameter Values 1305e1fc3b14SEric W. Biederman */ 1306e1fc3b14SEric W. Biederman /* The following protocol parameters are RECOMMENDED: */ 1307e1fc3b14SEric W. Biederman /* RTO.Initial - 3 seconds */ 1308e1fc3b14SEric W. Biederman net->sctp.rto_initial = SCTP_RTO_INITIAL; 1309e1fc3b14SEric W. Biederman /* RTO.Min - 1 second */ 1310e1fc3b14SEric W. Biederman net->sctp.rto_min = SCTP_RTO_MIN; 1311e1fc3b14SEric W. Biederman /* RTO.Max - 60 seconds */ 1312e1fc3b14SEric W. Biederman net->sctp.rto_max = SCTP_RTO_MAX; 1313e1fc3b14SEric W. Biederman /* RTO.Alpha - 1/8 */ 1314e1fc3b14SEric W. Biederman net->sctp.rto_alpha = SCTP_RTO_ALPHA; 1315e1fc3b14SEric W. Biederman /* RTO.Beta - 1/4 */ 1316e1fc3b14SEric W. Biederman net->sctp.rto_beta = SCTP_RTO_BETA; 1317e1fc3b14SEric W. Biederman 1318e1fc3b14SEric W. Biederman /* Valid.Cookie.Life - 60 seconds */ 1319e1fc3b14SEric W. Biederman net->sctp.valid_cookie_life = SCTP_DEFAULT_COOKIE_LIFE; 1320e1fc3b14SEric W. Biederman 1321e1fc3b14SEric W. Biederman /* Whether Cookie Preservative is enabled(1) or not(0) */ 1322e1fc3b14SEric W. Biederman net->sctp.cookie_preserve_enable = 1; 1323e1fc3b14SEric W. Biederman 13243c68198eSNeil Horman /* Default sctp sockets to use md5 as their hmac alg */ 13250d0863b0SNeil Horman #if defined (CONFIG_SCTP_DEFAULT_COOKIE_HMAC_MD5) 13263c68198eSNeil Horman net->sctp.sctp_hmac_alg = "md5"; 13270d0863b0SNeil Horman #elif defined (CONFIG_SCTP_DEFAULT_COOKIE_HMAC_SHA1) 13283c68198eSNeil Horman net->sctp.sctp_hmac_alg = "sha1"; 13293c68198eSNeil Horman #else 13303c68198eSNeil Horman net->sctp.sctp_hmac_alg = NULL; 13313c68198eSNeil Horman #endif 13323c68198eSNeil Horman 1333e1fc3b14SEric W. Biederman /* Max.Burst - 4 */ 1334e1fc3b14SEric W. Biederman net->sctp.max_burst = SCTP_DEFAULT_MAX_BURST; 1335e1fc3b14SEric W. Biederman 133634515e94SXin Long /* Disable of Primary Path Switchover by default */ 133734515e94SXin Long net->sctp.ps_retrans = SCTP_PS_RETRANS_MAX; 133834515e94SXin Long 1339566178f8SZhu Yanjun /* Enable pf state by default */ 1340566178f8SZhu Yanjun net->sctp.pf_enable = 1; 1341566178f8SZhu Yanjun 1342aef587beSXin Long /* Ignore pf exposure feature by default */ 1343aef587beSXin Long net->sctp.pf_expose = SCTP_PF_EXPOSE_UNSET; 1344aef587beSXin Long 1345e1fc3b14SEric W. Biederman /* Association.Max.Retrans - 10 attempts 1346e1fc3b14SEric W. Biederman * Path.Max.Retrans - 5 attempts (per destination address) 1347e1fc3b14SEric W. Biederman * Max.Init.Retransmits - 8 attempts 1348e1fc3b14SEric W. Biederman */ 1349e1fc3b14SEric W. Biederman net->sctp.max_retrans_association = 10; 1350e1fc3b14SEric W. Biederman net->sctp.max_retrans_path = 5; 1351e1fc3b14SEric W. Biederman net->sctp.max_retrans_init = 8; 1352e1fc3b14SEric W. Biederman 1353e1fc3b14SEric W. Biederman /* Sendbuffer growth - do per-socket accounting */ 1354e1fc3b14SEric W. Biederman net->sctp.sndbuf_policy = 0; 1355e1fc3b14SEric W. Biederman 1356e1fc3b14SEric W. Biederman /* Rcvbuffer growth - do per-socket accounting */ 1357e1fc3b14SEric W. Biederman net->sctp.rcvbuf_policy = 0; 1358e1fc3b14SEric W. Biederman 1359e1fc3b14SEric W. Biederman /* HB.interval - 30 seconds */ 1360e1fc3b14SEric W. Biederman net->sctp.hb_interval = SCTP_DEFAULT_TIMEOUT_HEARTBEAT; 1361e1fc3b14SEric W. Biederman 1362e1fc3b14SEric W. Biederman /* delayed SACK timeout */ 1363e1fc3b14SEric W. Biederman net->sctp.sack_timeout = SCTP_DEFAULT_TIMEOUT_SACK; 1364e1fc3b14SEric W. Biederman 1365e1fc3b14SEric W. Biederman /* Disable ADDIP by default. */ 1366e1fc3b14SEric W. Biederman net->sctp.addip_enable = 0; 1367e1fc3b14SEric W. Biederman net->sctp.addip_noauth = 0; 1368e1fc3b14SEric W. Biederman net->sctp.default_auto_asconf = 0; 1369e1fc3b14SEric W. Biederman 1370e1fc3b14SEric W. Biederman /* Enable PR-SCTP by default. */ 1371e1fc3b14SEric W. Biederman net->sctp.prsctp_enable = 1; 1372e1fc3b14SEric W. Biederman 1373c28445c3SXin Long /* Disable RECONF by default. */ 1374c28445c3SXin Long net->sctp.reconf_enable = 0; 1375c28445c3SXin Long 1376e1fc3b14SEric W. Biederman /* Disable AUTH by default. */ 1377e1fc3b14SEric W. Biederman net->sctp.auth_enable = 0; 1378e1fc3b14SEric W. Biederman 13791b0b8114SXin Long /* Enable ECN by default. */ 13801b0b8114SXin Long net->sctp.ecn_enable = 1; 13811b0b8114SXin Long 1382965ae444SXin Long /* Set UDP tunneling listening port to 0 by default */ 1383965ae444SXin Long net->sctp.udp_port = 0; 1384965ae444SXin Long 1385e8a3001cSXin Long /* Set remote encap port to 0 by default */ 1386e8a3001cSXin Long net->sctp.encap_port = 0; 1387e8a3001cSXin Long 1388e1fc3b14SEric W. Biederman /* Set SCOPE policy to enabled */ 1389e1fc3b14SEric W. Biederman net->sctp.scope_policy = SCTP_SCOPE_POLICY_ENABLE; 1390e1fc3b14SEric W. Biederman 1391e1fc3b14SEric W. Biederman /* Set the default rwnd update threshold */ 1392e1fc3b14SEric W. Biederman net->sctp.rwnd_upd_shift = SCTP_DEFAULT_RWND_SHIFT; 1393e1fc3b14SEric W. Biederman 1394e1fc3b14SEric W. Biederman /* Initialize maximum autoclose timeout. */ 1395e1fc3b14SEric W. Biederman net->sctp.max_autoclose = INT_MAX / HZ; 1396e1fc3b14SEric W. Biederman 1397*0af03170SXin Long #ifdef CONFIG_NET_L3_MASTER_DEV 1398*0af03170SXin Long net->sctp.l3mdev_accept = 1; 1399*0af03170SXin Long #endif 1400*0af03170SXin Long 1401ebb7e95dSEric W. Biederman status = sctp_sysctl_net_register(net); 1402ebb7e95dSEric W. Biederman if (status) 1403ebb7e95dSEric W. Biederman goto err_sysctl_register; 1404ebb7e95dSEric W. Biederman 1405b01a2407SEric W. Biederman /* Allocate and initialise sctp mibs. */ 1406b01a2407SEric W. Biederman status = init_sctp_mibs(net); 1407b01a2407SEric W. Biederman if (status) 1408b01a2407SEric W. Biederman goto err_init_mibs; 1409b01a2407SEric W. Biederman 1410d47d08c8SAl Viro #ifdef CONFIG_PROC_FS 141113d782f6SEric W. Biederman /* Initialize proc fs directory. */ 141213d782f6SEric W. Biederman status = sctp_proc_init(net); 141313d782f6SEric W. Biederman if (status) 141413d782f6SEric W. Biederman goto err_init_proc; 1415d47d08c8SAl Viro #endif 141613d782f6SEric W. Biederman 141713d782f6SEric W. Biederman sctp_dbg_objcnt_init(net); 141813d782f6SEric W. Biederman 14194db67e80SEric W. Biederman /* Initialize the local address list. */ 14204db67e80SEric W. Biederman INIT_LIST_HEAD(&net->sctp.local_addr_list); 14214db67e80SEric W. Biederman spin_lock_init(&net->sctp.local_addr_lock); 14224db67e80SEric W. Biederman sctp_get_local_addr_list(net); 14234db67e80SEric W. Biederman 14244db67e80SEric W. Biederman /* Initialize the address event list */ 14254db67e80SEric W. Biederman INIT_LIST_HEAD(&net->sctp.addr_waitq); 14264db67e80SEric W. Biederman INIT_LIST_HEAD(&net->sctp.auto_asconf_splist); 14274db67e80SEric W. Biederman spin_lock_init(&net->sctp.addr_wq_lock); 14284db67e80SEric W. Biederman net->sctp.addr_wq_timer.expires = 0; 14299c3b5751SKees Cook timer_setup(&net->sctp.addr_wq_timer, sctp_addr_wq_timeout_handler, 0); 14304db67e80SEric W. Biederman 14314db67e80SEric W. Biederman return 0; 14322ce95503SEric W. Biederman 14337ae665f1SArnd Bergmann #ifdef CONFIG_PROC_FS 143413d782f6SEric W. Biederman err_init_proc: 1435b01a2407SEric W. Biederman cleanup_sctp_mibs(net); 14367ae665f1SArnd Bergmann #endif 1437b01a2407SEric W. Biederman err_init_mibs: 1438ebb7e95dSEric W. Biederman sctp_sysctl_net_unregister(net); 1439ebb7e95dSEric W. Biederman err_sysctl_register: 14402ce95503SEric W. Biederman return status; 14414db67e80SEric W. Biederman } 14424db67e80SEric W. Biederman 14438e2d61e0SMarcelo Ricardo Leitner static void __net_exit sctp_defaults_exit(struct net *net) 14444db67e80SEric W. Biederman { 14454db67e80SEric W. Biederman /* Free the local address list */ 14464db67e80SEric W. Biederman sctp_free_addr_wq(net); 14474db67e80SEric W. Biederman sctp_free_local_addr_list(net); 14482ce95503SEric W. Biederman 1449d47d08c8SAl Viro #ifdef CONFIG_PROC_FS 1450d47d08c8SAl Viro remove_proc_subtree("sctp", net->proc_net); 1451d47d08c8SAl Viro net->sctp.proc_net_sctp = NULL; 1452d47d08c8SAl Viro #endif 1453b01a2407SEric W. Biederman cleanup_sctp_mibs(net); 1454ebb7e95dSEric W. Biederman sctp_sysctl_net_unregister(net); 14554db67e80SEric W. Biederman } 14564db67e80SEric W. Biederman 14578e2d61e0SMarcelo Ricardo Leitner static struct pernet_operations sctp_defaults_ops = { 14588e2d61e0SMarcelo Ricardo Leitner .init = sctp_defaults_init, 14598e2d61e0SMarcelo Ricardo Leitner .exit = sctp_defaults_exit, 14608e2d61e0SMarcelo Ricardo Leitner }; 14618e2d61e0SMarcelo Ricardo Leitner 14628e2d61e0SMarcelo Ricardo Leitner static int __net_init sctp_ctrlsock_init(struct net *net) 14638e2d61e0SMarcelo Ricardo Leitner { 14648e2d61e0SMarcelo Ricardo Leitner int status; 14658e2d61e0SMarcelo Ricardo Leitner 14668e2d61e0SMarcelo Ricardo Leitner /* Initialize the control inode/socket for handling OOTB packets. */ 14678e2d61e0SMarcelo Ricardo Leitner status = sctp_ctl_sock_init(net); 14688e2d61e0SMarcelo Ricardo Leitner if (status) 14698e2d61e0SMarcelo Ricardo Leitner pr_err("Failed to initialize the SCTP control sock\n"); 14708e2d61e0SMarcelo Ricardo Leitner 14718e2d61e0SMarcelo Ricardo Leitner return status; 14728e2d61e0SMarcelo Ricardo Leitner } 14738e2d61e0SMarcelo Ricardo Leitner 1474b456d724SChristophe JAILLET static void __net_exit sctp_ctrlsock_exit(struct net *net) 14758e2d61e0SMarcelo Ricardo Leitner { 14768e2d61e0SMarcelo Ricardo Leitner /* Free the control endpoint. */ 14778e2d61e0SMarcelo Ricardo Leitner inet_ctl_sock_destroy(net->sctp.ctl_sock); 14788e2d61e0SMarcelo Ricardo Leitner } 14798e2d61e0SMarcelo Ricardo Leitner 14808e2d61e0SMarcelo Ricardo Leitner static struct pernet_operations sctp_ctrlsock_ops = { 14818e2d61e0SMarcelo Ricardo Leitner .init = sctp_ctrlsock_init, 14828e2d61e0SMarcelo Ricardo Leitner .exit = sctp_ctrlsock_exit, 14834db67e80SEric W. Biederman }; 14844db67e80SEric W. Biederman 14851da177e4SLinus Torvalds /* Initialize the universe into something sensible. */ 1486dda91928SDaniel Borkmann static __init int sctp_init(void) 14871da177e4SLinus Torvalds { 1488ca79b0c2SArun KS unsigned long nr_pages = totalram_pages(); 1489623b57beSColin Ian King unsigned long limit; 1490623b57beSColin Ian King unsigned long goal; 1491d9749fb5SNeil Horman int max_entry_order; 1492623b57beSColin Ian King int num_entries; 1493623b57beSColin Ian King int max_share; 1494623b57beSColin Ian King int status; 1495623b57beSColin Ian King int order; 1496623b57beSColin Ian King int i; 14971da177e4SLinus Torvalds 1498b4772ef8SEyal Birger sock_skb_cb_check_size(sizeof(struct sctp_ulpevent)); 14991da177e4SLinus Torvalds 1500827bf122SSridhar Samudrala /* Allocate bind_bucket and chunk caches. */ 15011da177e4SLinus Torvalds status = -ENOBUFS; 15021da177e4SLinus Torvalds sctp_bucket_cachep = kmem_cache_create("sctp_bind_bucket", 15031da177e4SLinus Torvalds sizeof(struct sctp_bind_bucket), 15041da177e4SLinus Torvalds 0, SLAB_HWCACHE_ALIGN, 150520c2df83SPaul Mundt NULL); 15061da177e4SLinus Torvalds if (!sctp_bucket_cachep) 1507827bf122SSridhar Samudrala goto out; 15081da177e4SLinus Torvalds 15091da177e4SLinus Torvalds sctp_chunk_cachep = kmem_cache_create("sctp_chunk", 15101da177e4SLinus Torvalds sizeof(struct sctp_chunk), 15111da177e4SLinus Torvalds 0, SLAB_HWCACHE_ALIGN, 151220c2df83SPaul Mundt NULL); 15131da177e4SLinus Torvalds if (!sctp_chunk_cachep) 15141da177e4SLinus Torvalds goto err_chunk_cachep; 15151da177e4SLinus Torvalds 1516908c7f19STejun Heo status = percpu_counter_init(&sctp_sockets_allocated, 0, GFP_KERNEL); 1517632c928aSEric W. Biederman if (status) 1518632c928aSEric W. Biederman goto err_percpu_counter_init; 1519632c928aSEric W. Biederman 15201da177e4SLinus Torvalds /* Implementation specific variables. */ 15211da177e4SLinus Torvalds 15221da177e4SLinus Torvalds /* Initialize default stream count setup information. */ 15231da177e4SLinus Torvalds sctp_max_instreams = SCTP_DEFAULT_INSTREAMS; 15241da177e4SLinus Torvalds sctp_max_outstreams = SCTP_DEFAULT_OUTSTREAMS; 15251da177e4SLinus Torvalds 15261da177e4SLinus Torvalds /* Initialize handle used for association ids. */ 15271da177e4SLinus Torvalds idr_init(&sctp_assocs_id); 15281da177e4SLinus Torvalds 1529f03d78dbSEric Dumazet limit = nr_free_buffer_pages() / 8; 15304d93df0aSNeil Horman limit = max(limit, 128UL); 15314d93df0aSNeil Horman sysctl_sctp_mem[0] = limit / 4 * 3; 15324d93df0aSNeil Horman sysctl_sctp_mem[1] = limit; 15334d93df0aSNeil Horman sysctl_sctp_mem[2] = sysctl_sctp_mem[0] * 2; 15344d93df0aSNeil Horman 15354d93df0aSNeil Horman /* Set per-socket limits to no more than 1/128 the pressure threshold*/ 15364d93df0aSNeil Horman limit = (sysctl_sctp_mem[1]) << (PAGE_SHIFT - 7); 15374d93df0aSNeil Horman max_share = min(4UL*1024*1024, limit); 15384d93df0aSNeil Horman 1539100fdd1fSEric Dumazet sysctl_sctp_rmem[0] = PAGE_SIZE; /* give each asoc 1 page min */ 154087fb4b7bSEric Dumazet sysctl_sctp_rmem[1] = 1500 * SKB_TRUESIZE(1); 15414d93df0aSNeil Horman sysctl_sctp_rmem[2] = max(sysctl_sctp_rmem[1], max_share); 15424d93df0aSNeil Horman 1543100fdd1fSEric Dumazet sysctl_sctp_wmem[0] = PAGE_SIZE; 15444d93df0aSNeil Horman sysctl_sctp_wmem[1] = 16*1024; 15454d93df0aSNeil Horman sysctl_sctp_wmem[2] = max(64*1024, max_share); 15464d93df0aSNeil Horman 15471da177e4SLinus Torvalds /* Size and allocate the association hash table. 15481da177e4SLinus Torvalds * The methodology is similar to that of the tcp hash tables. 1549d9749fb5SNeil Horman * Though not identical. Start by getting a goal size 15501da177e4SLinus Torvalds */ 15513d6357deSArun KS if (nr_pages >= (128 * 1024)) 15523d6357deSArun KS goal = nr_pages >> (22 - PAGE_SHIFT); 15531da177e4SLinus Torvalds else 15543d6357deSArun KS goal = nr_pages >> (24 - PAGE_SHIFT); 15551da177e4SLinus Torvalds 1556d9749fb5SNeil Horman /* Then compute the page order for said goal */ 1557d9749fb5SNeil Horman order = get_order(goal); 1558d9749fb5SNeil Horman 1559d9749fb5SNeil Horman /* Now compute the required page order for the maximum sized table we 1560d9749fb5SNeil Horman * want to create 1561d9749fb5SNeil Horman */ 1562d9749fb5SNeil Horman max_entry_order = get_order(MAX_SCTP_PORT_HASH_ENTRIES * 1563d9749fb5SNeil Horman sizeof(struct sctp_bind_hashbucket)); 1564d9749fb5SNeil Horman 1565d9749fb5SNeil Horman /* Limit the page order by that maximum hash table size */ 1566d9749fb5SNeil Horman order = min(order, max_entry_order); 15671da177e4SLinus Torvalds 15681da177e4SLinus Torvalds /* Allocate and initialize the endpoint hash table. */ 15691da177e4SLinus Torvalds sctp_ep_hashsize = 64; 15703b77d661SZhang Yanfei sctp_ep_hashtable = 15716da2ec56SKees Cook kmalloc_array(64, sizeof(struct sctp_hashbucket), GFP_KERNEL); 15721da177e4SLinus Torvalds if (!sctp_ep_hashtable) { 1573145ce502SJoe Perches pr_err("Failed endpoint_hash alloc\n"); 15741da177e4SLinus Torvalds status = -ENOMEM; 15751da177e4SLinus Torvalds goto err_ehash_alloc; 15761da177e4SLinus Torvalds } 15771da177e4SLinus Torvalds for (i = 0; i < sctp_ep_hashsize; i++) { 15781da177e4SLinus Torvalds rwlock_init(&sctp_ep_hashtable[i].lock); 1579d970dbf8SVlad Yasevich INIT_HLIST_HEAD(&sctp_ep_hashtable[i].chain); 15801da177e4SLinus Torvalds } 15811da177e4SLinus Torvalds 1582d9749fb5SNeil Horman /* Allocate and initialize the SCTP port hash table. 1583d9749fb5SNeil Horman * Note that order is initalized to start at the max sized 1584d9749fb5SNeil Horman * table we want to support. If we can't get that many pages 1585d9749fb5SNeil Horman * reduce the order and try again 1586d9749fb5SNeil Horman */ 15871da177e4SLinus Torvalds do { 15881da177e4SLinus Torvalds sctp_port_hashtable = (struct sctp_bind_hashbucket *) 15896857a02aSEric Dumazet __get_free_pages(GFP_KERNEL | __GFP_NOWARN, order); 15901da177e4SLinus Torvalds } while (!sctp_port_hashtable && --order > 0); 1591d9749fb5SNeil Horman 15921da177e4SLinus Torvalds if (!sctp_port_hashtable) { 1593145ce502SJoe Perches pr_err("Failed bind hash alloc\n"); 15941da177e4SLinus Torvalds status = -ENOMEM; 15951da177e4SLinus Torvalds goto err_bhash_alloc; 15961da177e4SLinus Torvalds } 1597d9749fb5SNeil Horman 1598d9749fb5SNeil Horman /* Now compute the number of entries that will fit in the 1599d9749fb5SNeil Horman * port hash space we allocated 1600d9749fb5SNeil Horman */ 1601d9749fb5SNeil Horman num_entries = (1UL << order) * PAGE_SIZE / 1602d9749fb5SNeil Horman sizeof(struct sctp_bind_hashbucket); 1603d9749fb5SNeil Horman 160493c3216aSRandy Dunlap /* And finish by rounding it down to the nearest power of two. 160593c3216aSRandy Dunlap * This wastes some memory of course, but it's needed because 1606d9749fb5SNeil Horman * the hash function operates based on the assumption that 160793c3216aSRandy Dunlap * the number of entries is a power of two. 1608d9749fb5SNeil Horman */ 1609d9749fb5SNeil Horman sctp_port_hashsize = rounddown_pow_of_two(num_entries); 1610d9749fb5SNeil Horman 16111da177e4SLinus Torvalds for (i = 0; i < sctp_port_hashsize; i++) { 16121da177e4SLinus Torvalds spin_lock_init(&sctp_port_hashtable[i].lock); 1613d970dbf8SVlad Yasevich INIT_HLIST_HEAD(&sctp_port_hashtable[i].chain); 16141da177e4SLinus Torvalds } 16151da177e4SLinus Torvalds 1616a5e27d18SWei Yongjun status = sctp_transport_hashtable_init(); 1617a5e27d18SWei Yongjun if (status) 16184f008781SXin Long goto err_thash_alloc; 16194f008781SXin Long 1620d9749fb5SNeil Horman pr_info("Hash tables configured (bind %d/%d)\n", sctp_port_hashsize, 1621d9749fb5SNeil Horman num_entries); 16221da177e4SLinus Torvalds 16231da177e4SLinus Torvalds sctp_sysctl_register(); 16241da177e4SLinus Torvalds 16251da177e4SLinus Torvalds INIT_LIST_HEAD(&sctp_address_families); 1626270637abSVlad Yasevich sctp_v4_pf_init(); 1627270637abSVlad Yasevich sctp_v6_pf_init(); 16281ba896f6SXin Long sctp_sched_ops_init(); 16291da177e4SLinus Torvalds 16308e2d61e0SMarcelo Ricardo Leitner status = register_pernet_subsys(&sctp_defaults_ops); 16318e2d61e0SMarcelo Ricardo Leitner if (status) 16328e2d61e0SMarcelo Ricardo Leitner goto err_register_defaults; 1633270637abSVlad Yasevich 16348e2d61e0SMarcelo Ricardo Leitner status = sctp_v4_protosw_init(); 1635827bf122SSridhar Samudrala if (status) 1636270637abSVlad Yasevich goto err_protosw_init; 1637827bf122SSridhar Samudrala 1638270637abSVlad Yasevich status = sctp_v6_protosw_init(); 16391da177e4SLinus Torvalds if (status) 1640270637abSVlad Yasevich goto err_v6_protosw_init; 16411da177e4SLinus Torvalds 16428e2d61e0SMarcelo Ricardo Leitner status = register_pernet_subsys(&sctp_ctrlsock_ops); 16434db67e80SEric W. Biederman if (status) 16448e2d61e0SMarcelo Ricardo Leitner goto err_register_ctrlsock; 16454db67e80SEric W. Biederman 1646270637abSVlad Yasevich status = sctp_v4_add_protocol(); 1647270637abSVlad Yasevich if (status) 1648827bf122SSridhar Samudrala goto err_add_protocol; 1649827bf122SSridhar Samudrala 1650827bf122SSridhar Samudrala /* Register SCTP with inet6 layer. */ 1651827bf122SSridhar Samudrala status = sctp_v6_add_protocol(); 1652827bf122SSridhar Samudrala if (status) 1653827bf122SSridhar Samudrala goto err_v6_add_protocol; 1654827bf122SSridhar Samudrala 165590017accSMarcelo Ricardo Leitner if (sctp_offload_init() < 0) 165690017accSMarcelo Ricardo Leitner pr_crit("%s: Cannot add SCTP protocol offload\n", __func__); 165790017accSMarcelo Ricardo Leitner 16581da177e4SLinus Torvalds out: 16591da177e4SLinus Torvalds return status; 1660827bf122SSridhar Samudrala err_v6_add_protocol: 1661270637abSVlad Yasevich sctp_v4_del_protocol(); 1662d1dd5247SVlad Yasevich err_add_protocol: 16638e2d61e0SMarcelo Ricardo Leitner unregister_pernet_subsys(&sctp_ctrlsock_ops); 16648e2d61e0SMarcelo Ricardo Leitner err_register_ctrlsock: 1665270637abSVlad Yasevich sctp_v6_protosw_exit(); 1666270637abSVlad Yasevich err_v6_protosw_init: 1667270637abSVlad Yasevich sctp_v4_protosw_exit(); 1668270637abSVlad Yasevich err_protosw_init: 16698e2d61e0SMarcelo Ricardo Leitner unregister_pernet_subsys(&sctp_defaults_ops); 16708e2d61e0SMarcelo Ricardo Leitner err_register_defaults: 1671270637abSVlad Yasevich sctp_v4_pf_exit(); 1672270637abSVlad Yasevich sctp_v6_pf_exit(); 16731da177e4SLinus Torvalds sctp_sysctl_unregister(); 16741da177e4SLinus Torvalds free_pages((unsigned long)sctp_port_hashtable, 16751da177e4SLinus Torvalds get_order(sctp_port_hashsize * 16761da177e4SLinus Torvalds sizeof(struct sctp_bind_hashbucket))); 16771da177e4SLinus Torvalds err_bhash_alloc: 16784f008781SXin Long sctp_transport_hashtable_destroy(); 16794f008781SXin Long err_thash_alloc: 16801da177e4SLinus Torvalds kfree(sctp_ep_hashtable); 16811da177e4SLinus Torvalds err_ehash_alloc: 1682632c928aSEric W. Biederman percpu_counter_destroy(&sctp_sockets_allocated); 1683632c928aSEric W. Biederman err_percpu_counter_init: 16841da177e4SLinus Torvalds kmem_cache_destroy(sctp_chunk_cachep); 16851da177e4SLinus Torvalds err_chunk_cachep: 16861da177e4SLinus Torvalds kmem_cache_destroy(sctp_bucket_cachep); 16871da177e4SLinus Torvalds goto out; 16881da177e4SLinus Torvalds } 16891da177e4SLinus Torvalds 16901da177e4SLinus Torvalds /* Exit handler for the SCTP protocol. */ 1691dda91928SDaniel Borkmann static __exit void sctp_exit(void) 16921da177e4SLinus Torvalds { 16931da177e4SLinus Torvalds /* BUG. This should probably do something useful like clean 16941da177e4SLinus Torvalds * up all the remaining associations and all that memory. 16951da177e4SLinus Torvalds */ 16961da177e4SLinus Torvalds 1697827bf122SSridhar Samudrala /* Unregister with inet6/inet layers. */ 1698827bf122SSridhar Samudrala sctp_v6_del_protocol(); 1699270637abSVlad Yasevich sctp_v4_del_protocol(); 17001da177e4SLinus Torvalds 17018e2d61e0SMarcelo Ricardo Leitner unregister_pernet_subsys(&sctp_ctrlsock_ops); 17024db67e80SEric W. Biederman 1703270637abSVlad Yasevich /* Free protosw registrations */ 1704270637abSVlad Yasevich sctp_v6_protosw_exit(); 1705270637abSVlad Yasevich sctp_v4_protosw_exit(); 1706270637abSVlad Yasevich 17078e2d61e0SMarcelo Ricardo Leitner unregister_pernet_subsys(&sctp_defaults_ops); 17088e2d61e0SMarcelo Ricardo Leitner 1709827bf122SSridhar Samudrala /* Unregister with socket layer. */ 1710270637abSVlad Yasevich sctp_v6_pf_exit(); 1711270637abSVlad Yasevich sctp_v4_pf_exit(); 1712827bf122SSridhar Samudrala 17131da177e4SLinus Torvalds sctp_sysctl_unregister(); 17141da177e4SLinus Torvalds 17151da177e4SLinus Torvalds free_pages((unsigned long)sctp_port_hashtable, 17161da177e4SLinus Torvalds get_order(sctp_port_hashsize * 17171da177e4SLinus Torvalds sizeof(struct sctp_bind_hashbucket))); 1718b5eff712SXin Long kfree(sctp_ep_hashtable); 17194f008781SXin Long sctp_transport_hashtable_destroy(); 17201da177e4SLinus Torvalds 1721632c928aSEric W. Biederman percpu_counter_destroy(&sctp_sockets_allocated); 17221da177e4SLinus Torvalds 1723eaa184a1SJesper Dangaard Brouer rcu_barrier(); /* Wait for completion of call_rcu()'s */ 1724eaa184a1SJesper Dangaard Brouer 1725827bf122SSridhar Samudrala kmem_cache_destroy(sctp_chunk_cachep); 1726827bf122SSridhar Samudrala kmem_cache_destroy(sctp_bucket_cachep); 17271da177e4SLinus Torvalds } 17281da177e4SLinus Torvalds 17291da177e4SLinus Torvalds module_init(sctp_init); 17301da177e4SLinus Torvalds module_exit(sctp_exit); 17311da177e4SLinus Torvalds 1732bb97d31fSArnaldo Carvalho de Melo /* 1733bb97d31fSArnaldo Carvalho de Melo * __stringify doesn't likes enums, so use IPPROTO_SCTP value (132) directly. 1734bb97d31fSArnaldo Carvalho de Melo */ 1735bb97d31fSArnaldo Carvalho de Melo MODULE_ALIAS("net-pf-" __stringify(PF_INET) "-proto-132"); 1736882a382cSSridhar Samudrala MODULE_ALIAS("net-pf-" __stringify(PF_INET6) "-proto-132"); 173791705c61SDaniel Borkmann MODULE_AUTHOR("Linux Kernel SCTP developers <linux-sctp@vger.kernel.org>"); 17381da177e4SLinus Torvalds MODULE_DESCRIPTION("Support for the SCTP protocol (RFC2960)"); 173971acc0ddSDavid S. Miller module_param_named(no_checksums, sctp_checksum_disable, bool, 0644); 174071acc0ddSDavid S. Miller MODULE_PARM_DESC(no_checksums, "Disable checksums computing and verification"); 17411da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 1742