Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* SCTP kernel reference Implementation |
| 2 | * (C) Copyright IBM Corp. 2001, 2004 |
| 3 | * Copyright (c) 1999-2000 Cisco, Inc. |
| 4 | * Copyright (c) 1999-2001 Motorola, Inc. |
| 5 | * Copyright (c) 2001 Intel Corp. |
| 6 | * Copyright (c) 2001 Nokia, Inc. |
| 7 | * Copyright (c) 2001 La Monte H.P. Yarroll |
| 8 | * |
| 9 | * This file is part of the SCTP kernel reference Implementation |
| 10 | * |
| 11 | * Initialization/cleanup for SCTP protocol support. |
| 12 | * |
| 13 | * The SCTP reference implementation is free software; |
| 14 | * you can redistribute it and/or modify it under the terms of |
| 15 | * the GNU General Public License as published by |
| 16 | * the Free Software Foundation; either version 2, or (at your option) |
| 17 | * any later version. |
| 18 | * |
| 19 | * The SCTP reference implementation is distributed in the hope that it |
| 20 | * will be useful, but WITHOUT ANY WARRANTY; without even the implied |
| 21 | * ************************ |
| 22 | * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 23 | * See the GNU General Public License for more details. |
| 24 | * |
| 25 | * You should have received a copy of the GNU General Public License |
| 26 | * along with GNU CC; see the file COPYING. If not, write to |
| 27 | * the Free Software Foundation, 59 Temple Place - Suite 330, |
| 28 | * Boston, MA 02111-1307, USA. |
| 29 | * |
| 30 | * Please send any bug reports or fixes you make to the |
| 31 | * email address(es): |
| 32 | * lksctp developers <lksctp-developers@lists.sourceforge.net> |
| 33 | * |
| 34 | * Or submit a bug report through the following website: |
| 35 | * http://www.sf.net/projects/lksctp |
| 36 | * |
| 37 | * Written or modified by: |
| 38 | * La Monte H.P. Yarroll <piggy@acm.org> |
| 39 | * Karl Knutson <karl@athena.chicago.il.us> |
| 40 | * Jon Grimm <jgrimm@us.ibm.com> |
| 41 | * Sridhar Samudrala <sri@us.ibm.com> |
| 42 | * Daisy Chang <daisyc@us.ibm.com> |
| 43 | * Ardelle Fan <ardelle.fan@intel.com> |
| 44 | * |
| 45 | * Any bugs reported given to us we will try to fix... any fixes shared will |
| 46 | * be incorporated into the next SCTP release. |
| 47 | */ |
| 48 | |
| 49 | #include <linux/module.h> |
| 50 | #include <linux/init.h> |
| 51 | #include <linux/netdevice.h> |
| 52 | #include <linux/inetdevice.h> |
| 53 | #include <linux/seq_file.h> |
| 54 | #include <net/protocol.h> |
| 55 | #include <net/ip.h> |
| 56 | #include <net/ipv6.h> |
Arnaldo Carvalho de Melo | 14c8502 | 2005-12-27 02:43:12 -0200 | [diff] [blame] | 57 | #include <net/route.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | #include <net/sctp/sctp.h> |
| 59 | #include <net/addrconf.h> |
| 60 | #include <net/inet_common.h> |
| 61 | #include <net/inet_ecn.h> |
| 62 | |
| 63 | /* Global data structures. */ |
Brian Haley | 4cbf1ca | 2006-09-18 00:04:22 -0700 | [diff] [blame] | 64 | struct sctp_globals sctp_globals __read_mostly; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | struct proc_dir_entry *proc_net_sctp; |
Eric Dumazet | ba89966 | 2005-08-26 12:05:31 -0700 | [diff] [blame] | 66 | DEFINE_SNMP_STAT(struct sctp_mib, sctp_statistics) __read_mostly; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | |
| 68 | struct idr sctp_assocs_id; |
| 69 | DEFINE_SPINLOCK(sctp_assocs_id_lock); |
| 70 | |
| 71 | /* This is the global socket data structure used for responding to |
| 72 | * the Out-of-the-blue (OOTB) packets. A control sock will be created |
| 73 | * for this socket at the initialization time. |
| 74 | */ |
| 75 | static struct socket *sctp_ctl_socket; |
| 76 | |
| 77 | static struct sctp_pf *sctp_pf_inet6_specific; |
| 78 | static struct sctp_pf *sctp_pf_inet_specific; |
| 79 | static struct sctp_af *sctp_af_v4_specific; |
| 80 | static struct sctp_af *sctp_af_v6_specific; |
| 81 | |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 82 | struct kmem_cache *sctp_chunk_cachep __read_mostly; |
| 83 | struct kmem_cache *sctp_bucket_cachep __read_mostly; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | /* Return the address of the control sock. */ |
| 86 | struct sock *sctp_get_ctl_sock(void) |
| 87 | { |
| 88 | return sctp_ctl_socket->sk; |
| 89 | } |
| 90 | |
| 91 | /* Set up the proc fs entry for the SCTP protocol. */ |
| 92 | static __init int sctp_proc_init(void) |
| 93 | { |
| 94 | if (!proc_net_sctp) { |
| 95 | struct proc_dir_entry *ent; |
| 96 | ent = proc_mkdir("net/sctp", NULL); |
| 97 | if (ent) { |
| 98 | ent->owner = THIS_MODULE; |
| 99 | proc_net_sctp = ent; |
| 100 | } else |
| 101 | goto out_nomem; |
| 102 | } |
| 103 | |
| 104 | if (sctp_snmp_proc_init()) |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 105 | goto out_nomem; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | if (sctp_eps_proc_init()) |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 107 | goto out_nomem; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | if (sctp_assocs_proc_init()) |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 109 | goto out_nomem; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | |
| 111 | return 0; |
| 112 | |
| 113 | out_nomem: |
| 114 | return -ENOMEM; |
| 115 | } |
| 116 | |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 117 | /* Clean up the proc fs entry for the SCTP protocol. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | * Note: Do not make this __exit as it is used in the init error |
| 119 | * path. |
| 120 | */ |
| 121 | static void sctp_proc_exit(void) |
| 122 | { |
| 123 | sctp_snmp_proc_exit(); |
| 124 | sctp_eps_proc_exit(); |
| 125 | sctp_assocs_proc_exit(); |
| 126 | |
| 127 | if (proc_net_sctp) { |
| 128 | proc_net_sctp = NULL; |
| 129 | remove_proc_entry("net/sctp", NULL); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | /* Private helper to extract ipv4 address and stash them in |
| 134 | * the protocol structure. |
| 135 | */ |
| 136 | static void sctp_v4_copy_addrlist(struct list_head *addrlist, |
| 137 | struct net_device *dev) |
| 138 | { |
| 139 | struct in_device *in_dev; |
| 140 | struct in_ifaddr *ifa; |
| 141 | struct sctp_sockaddr_entry *addr; |
| 142 | |
| 143 | rcu_read_lock(); |
Herbert Xu | e5ed639 | 2005-10-03 14:35:55 -0700 | [diff] [blame] | 144 | if ((in_dev = __in_dev_get_rcu(dev)) == NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | rcu_read_unlock(); |
| 146 | return; |
| 147 | } |
| 148 | |
| 149 | for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) { |
| 150 | /* Add the address to the local list. */ |
| 151 | addr = t_new(struct sctp_sockaddr_entry, GFP_ATOMIC); |
| 152 | if (addr) { |
Al Viro | 2a6fd78 | 2006-11-20 17:04:42 -0800 | [diff] [blame] | 153 | addr->a.v4.sin_family = AF_INET; |
| 154 | addr->a.v4.sin_port = 0; |
| 155 | addr->a.v4.sin_addr.s_addr = ifa->ifa_local; |
Vlad Yasevich | 2930354 | 2007-09-16 16:02:12 -0700 | [diff] [blame] | 156 | addr->valid = 1; |
| 157 | INIT_LIST_HEAD(&addr->list); |
| 158 | INIT_RCU_HEAD(&addr->rcu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | list_add_tail(&addr->list, addrlist); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | rcu_read_unlock(); |
| 164 | } |
| 165 | |
| 166 | /* Extract our IP addresses from the system and stash them in the |
| 167 | * protocol structure. |
| 168 | */ |
Sridhar Samudrala | 29c7cf9 | 2006-12-13 16:26:26 -0800 | [diff] [blame] | 169 | static void sctp_get_local_addr_list(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | { |
| 171 | struct net_device *dev; |
| 172 | struct list_head *pos; |
| 173 | struct sctp_af *af; |
| 174 | |
| 175 | read_lock(&dev_base_lock); |
Pavel Emelianov | 7562f87 | 2007-05-03 15:13:45 -0700 | [diff] [blame] | 176 | for_each_netdev(dev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | __list_for_each(pos, &sctp_address_families) { |
| 178 | af = list_entry(pos, struct sctp_af, list); |
| 179 | af->copy_addrlist(&sctp_local_addr_list, dev); |
| 180 | } |
| 181 | } |
| 182 | read_unlock(&dev_base_lock); |
| 183 | } |
| 184 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | /* Free the existing local addresses. */ |
Sridhar Samudrala | 29c7cf9 | 2006-12-13 16:26:26 -0800 | [diff] [blame] | 186 | static void sctp_free_local_addr_list(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | { |
| 188 | struct sctp_sockaddr_entry *addr; |
| 189 | struct list_head *pos, *temp; |
| 190 | |
| 191 | list_for_each_safe(pos, temp, &sctp_local_addr_list) { |
| 192 | addr = list_entry(pos, struct sctp_sockaddr_entry, list); |
| 193 | list_del(pos); |
| 194 | kfree(addr); |
| 195 | } |
| 196 | } |
| 197 | |
Vlad Yasevich | 2930354 | 2007-09-16 16:02:12 -0700 | [diff] [blame] | 198 | void sctp_local_addr_free(struct rcu_head *head) |
| 199 | { |
| 200 | struct sctp_sockaddr_entry *e = container_of(head, |
| 201 | struct sctp_sockaddr_entry, rcu); |
| 202 | kfree(e); |
| 203 | } |
| 204 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | /* Copy the local addresses which are valid for 'scope' into 'bp'. */ |
| 206 | int sctp_copy_local_addr_list(struct sctp_bind_addr *bp, sctp_scope_t scope, |
Al Viro | dd0fc66 | 2005-10-07 07:46:04 +0100 | [diff] [blame] | 207 | gfp_t gfp, int copy_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | { |
| 209 | struct sctp_sockaddr_entry *addr; |
| 210 | int error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | |
Vlad Yasevich | 2930354 | 2007-09-16 16:02:12 -0700 | [diff] [blame] | 212 | rcu_read_lock(); |
| 213 | list_for_each_entry_rcu(addr, &sctp_local_addr_list, list) { |
| 214 | if (!addr->valid) |
| 215 | continue; |
Al Viro | 6244be4 | 2006-11-20 17:21:44 -0800 | [diff] [blame] | 216 | if (sctp_in_scope(&addr->a, scope)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | /* Now that the address is in scope, check to see if |
| 218 | * the address type is really supported by the local |
| 219 | * sock as well as the remote peer. |
| 220 | */ |
Al Viro | 6244be4 | 2006-11-20 17:21:44 -0800 | [diff] [blame] | 221 | if ((((AF_INET == addr->a.sa.sa_family) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | (copy_flags & SCTP_ADDR4_PEERSUPP))) || |
Al Viro | 6244be4 | 2006-11-20 17:21:44 -0800 | [diff] [blame] | 223 | (((AF_INET6 == addr->a.sa.sa_family) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | (copy_flags & SCTP_ADDR6_ALLOWED) && |
| 225 | (copy_flags & SCTP_ADDR6_PEERSUPP)))) { |
Al Viro | 5ab7b85 | 2006-11-20 17:10:38 -0800 | [diff] [blame] | 226 | error = sctp_add_bind_addr(bp, &addr->a, 1, |
Vlad Yasevich | 559cf71 | 2007-09-16 16:03:28 -0700 | [diff] [blame^] | 227 | GFP_ATOMIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | if (error) |
| 229 | goto end_copy; |
| 230 | } |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | end_copy: |
Vlad Yasevich | 2930354 | 2007-09-16 16:02:12 -0700 | [diff] [blame] | 235 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | return error; |
| 237 | } |
| 238 | |
| 239 | /* Initialize a sctp_addr from in incoming skb. */ |
| 240 | static void sctp_v4_from_skb(union sctp_addr *addr, struct sk_buff *skb, |
| 241 | int is_saddr) |
| 242 | { |
| 243 | void *from; |
Al Viro | d55c41b | 2006-11-20 17:09:40 -0800 | [diff] [blame] | 244 | __be16 *port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | struct sctphdr *sh; |
| 246 | |
| 247 | port = &addr->v4.sin_port; |
| 248 | addr->v4.sin_family = AF_INET; |
| 249 | |
Arnaldo Carvalho de Melo | 2c0fd38 | 2007-03-13 13:59:32 -0300 | [diff] [blame] | 250 | sh = sctp_hdr(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | if (is_saddr) { |
Al Viro | d55c41b | 2006-11-20 17:09:40 -0800 | [diff] [blame] | 252 | *port = sh->source; |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 253 | from = &ip_hdr(skb)->saddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | } else { |
Al Viro | d55c41b | 2006-11-20 17:09:40 -0800 | [diff] [blame] | 255 | *port = sh->dest; |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 256 | from = &ip_hdr(skb)->daddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | } |
| 258 | memcpy(&addr->v4.sin_addr.s_addr, from, sizeof(struct in_addr)); |
| 259 | } |
| 260 | |
| 261 | /* Initialize an sctp_addr from a socket. */ |
| 262 | static void sctp_v4_from_sk(union sctp_addr *addr, struct sock *sk) |
| 263 | { |
| 264 | addr->v4.sin_family = AF_INET; |
Al Viro | 7dcdbd9 | 2006-11-20 17:24:21 -0800 | [diff] [blame] | 265 | addr->v4.sin_port = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | addr->v4.sin_addr.s_addr = inet_sk(sk)->rcv_saddr; |
| 267 | } |
| 268 | |
| 269 | /* Initialize sk->sk_rcv_saddr from sctp_addr. */ |
| 270 | static void sctp_v4_to_sk_saddr(union sctp_addr *addr, struct sock *sk) |
| 271 | { |
| 272 | inet_sk(sk)->rcv_saddr = addr->v4.sin_addr.s_addr; |
| 273 | } |
| 274 | |
| 275 | /* Initialize sk->sk_daddr from sctp_addr. */ |
| 276 | static void sctp_v4_to_sk_daddr(union sctp_addr *addr, struct sock *sk) |
| 277 | { |
| 278 | inet_sk(sk)->daddr = addr->v4.sin_addr.s_addr; |
| 279 | } |
| 280 | |
| 281 | /* Initialize a sctp_addr from an address parameter. */ |
| 282 | static void sctp_v4_from_addr_param(union sctp_addr *addr, |
| 283 | union sctp_addr_param *param, |
Al Viro | dd86d13 | 2006-11-20 17:11:13 -0800 | [diff] [blame] | 284 | __be16 port, int iif) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | { |
| 286 | addr->v4.sin_family = AF_INET; |
| 287 | addr->v4.sin_port = port; |
| 288 | addr->v4.sin_addr.s_addr = param->v4.addr.s_addr; |
| 289 | } |
| 290 | |
| 291 | /* Initialize an address parameter from a sctp_addr and return the length |
| 292 | * of the address parameter. |
| 293 | */ |
| 294 | static int sctp_v4_to_addr_param(const union sctp_addr *addr, |
| 295 | union sctp_addr_param *param) |
| 296 | { |
| 297 | int length = sizeof(sctp_ipv4addr_param_t); |
| 298 | |
| 299 | param->v4.param_hdr.type = SCTP_PARAM_IPV4_ADDRESS; |
Al Viro | dbc16db | 2006-11-20 17:01:42 -0800 | [diff] [blame] | 300 | param->v4.param_hdr.length = htons(length); |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 301 | param->v4.addr.s_addr = addr->v4.sin_addr.s_addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | |
| 303 | return length; |
| 304 | } |
| 305 | |
| 306 | /* Initialize a sctp_addr from a dst_entry. */ |
| 307 | static void sctp_v4_dst_saddr(union sctp_addr *saddr, struct dst_entry *dst, |
Al Viro | 854d43a | 2006-11-20 17:06:24 -0800 | [diff] [blame] | 308 | __be16 port) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | { |
| 310 | struct rtable *rt = (struct rtable *)dst; |
| 311 | saddr->v4.sin_family = AF_INET; |
| 312 | saddr->v4.sin_port = port; |
| 313 | saddr->v4.sin_addr.s_addr = rt->rt_src; |
| 314 | } |
| 315 | |
| 316 | /* Compare two addresses exactly. */ |
| 317 | static int sctp_v4_cmp_addr(const union sctp_addr *addr1, |
| 318 | const union sctp_addr *addr2) |
| 319 | { |
| 320 | if (addr1->sa.sa_family != addr2->sa.sa_family) |
| 321 | return 0; |
| 322 | if (addr1->v4.sin_port != addr2->v4.sin_port) |
| 323 | return 0; |
| 324 | if (addr1->v4.sin_addr.s_addr != addr2->v4.sin_addr.s_addr) |
| 325 | return 0; |
| 326 | |
| 327 | return 1; |
| 328 | } |
| 329 | |
| 330 | /* Initialize addr struct to INADDR_ANY. */ |
Al Viro | 6fbfa9f | 2006-11-20 17:24:53 -0800 | [diff] [blame] | 331 | static void sctp_v4_inaddr_any(union sctp_addr *addr, __be16 port) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | { |
| 333 | addr->v4.sin_family = AF_INET; |
| 334 | addr->v4.sin_addr.s_addr = INADDR_ANY; |
| 335 | addr->v4.sin_port = port; |
| 336 | } |
| 337 | |
| 338 | /* Is this a wildcard address? */ |
| 339 | static int sctp_v4_is_any(const union sctp_addr *addr) |
| 340 | { |
| 341 | return INADDR_ANY == addr->v4.sin_addr.s_addr; |
| 342 | } |
| 343 | |
| 344 | /* This function checks if the address is a valid address to be used for |
| 345 | * SCTP binding. |
| 346 | * |
| 347 | * Output: |
| 348 | * Return 0 - If the address is a non-unicast or an illegal address. |
| 349 | * Return 1 - If the address is a unicast. |
| 350 | */ |
Vlad Yasevich | 5636bef | 2006-06-17 22:55:35 -0700 | [diff] [blame] | 351 | static int sctp_v4_addr_valid(union sctp_addr *addr, |
| 352 | struct sctp_sock *sp, |
| 353 | const struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | { |
| 355 | /* Is this a non-unicast address or a unusable SCTP address? */ |
| 356 | if (IS_IPV4_UNUSABLE_ADDRESS(&addr->v4.sin_addr.s_addr)) |
| 357 | return 0; |
| 358 | |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 359 | /* Is this a broadcast address? */ |
| 360 | if (skb && ((struct rtable *)skb->dst)->rt_flags & RTCF_BROADCAST) |
| 361 | return 0; |
Vlad Yasevich | 5636bef | 2006-06-17 22:55:35 -0700 | [diff] [blame] | 362 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | return 1; |
| 364 | } |
| 365 | |
| 366 | /* Should this be available for binding? */ |
| 367 | static int sctp_v4_available(union sctp_addr *addr, struct sctp_sock *sp) |
| 368 | { |
| 369 | int ret = inet_addr_type(addr->v4.sin_addr.s_addr); |
| 370 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | |
Neil Horman | cdac4e0 | 2005-06-13 15:12:33 -0700 | [diff] [blame] | 372 | if (addr->v4.sin_addr.s_addr != INADDR_ANY && |
| 373 | ret != RTN_LOCAL && |
| 374 | !sp->inet.freebind && |
| 375 | !sysctl_ip_nonlocal_bind) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | return 0; |
Neil Horman | cdac4e0 | 2005-06-13 15:12:33 -0700 | [diff] [blame] | 377 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | return 1; |
| 379 | } |
| 380 | |
| 381 | /* Checking the loopback, private and other address scopes as defined in |
| 382 | * RFC 1918. The IPv4 scoping is based on the draft for SCTP IPv4 |
| 383 | * scoping <draft-stewart-tsvwg-sctp-ipv4-00.txt>. |
| 384 | * |
| 385 | * Level 0 - unusable SCTP addresses |
| 386 | * Level 1 - loopback address |
| 387 | * Level 2 - link-local addresses |
| 388 | * Level 3 - private addresses. |
| 389 | * Level 4 - global addresses |
| 390 | * For INIT and INIT-ACK address list, let L be the level of |
| 391 | * of requested destination address, sender and receiver |
| 392 | * SHOULD include all of its addresses with level greater |
| 393 | * than or equal to L. |
| 394 | */ |
| 395 | static sctp_scope_t sctp_v4_scope(union sctp_addr *addr) |
| 396 | { |
| 397 | sctp_scope_t retval; |
| 398 | |
| 399 | /* Should IPv4 scoping be a sysctl configurable option |
| 400 | * so users can turn it off (default on) for certain |
| 401 | * unconventional networking environments? |
| 402 | */ |
| 403 | |
| 404 | /* Check for unusable SCTP addresses. */ |
| 405 | if (IS_IPV4_UNUSABLE_ADDRESS(&addr->v4.sin_addr.s_addr)) { |
| 406 | retval = SCTP_SCOPE_UNUSABLE; |
| 407 | } else if (LOOPBACK(addr->v4.sin_addr.s_addr)) { |
| 408 | retval = SCTP_SCOPE_LOOPBACK; |
| 409 | } else if (IS_IPV4_LINK_ADDRESS(&addr->v4.sin_addr.s_addr)) { |
| 410 | retval = SCTP_SCOPE_LINK; |
| 411 | } else if (IS_IPV4_PRIVATE_ADDRESS(&addr->v4.sin_addr.s_addr)) { |
| 412 | retval = SCTP_SCOPE_PRIVATE; |
| 413 | } else { |
| 414 | retval = SCTP_SCOPE_GLOBAL; |
| 415 | } |
| 416 | |
| 417 | return retval; |
| 418 | } |
| 419 | |
| 420 | /* Returns a valid dst cache entry for the given source and destination ip |
| 421 | * addresses. If an association is passed, trys to get a dst entry with a |
| 422 | * source address that matches an address in the bind address list. |
| 423 | */ |
| 424 | static struct dst_entry *sctp_v4_get_dst(struct sctp_association *asoc, |
| 425 | union sctp_addr *daddr, |
| 426 | union sctp_addr *saddr) |
| 427 | { |
| 428 | struct rtable *rt; |
| 429 | struct flowi fl; |
| 430 | struct sctp_bind_addr *bp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | struct sctp_sockaddr_entry *laddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | struct dst_entry *dst = NULL; |
| 433 | union sctp_addr dst_saddr; |
| 434 | |
| 435 | memset(&fl, 0x0, sizeof(struct flowi)); |
| 436 | fl.fl4_dst = daddr->v4.sin_addr.s_addr; |
| 437 | fl.proto = IPPROTO_SCTP; |
| 438 | if (asoc) { |
| 439 | fl.fl4_tos = RT_CONN_FLAGS(asoc->base.sk); |
| 440 | fl.oif = asoc->base.sk->sk_bound_dev_if; |
| 441 | } |
| 442 | if (saddr) |
| 443 | fl.fl4_src = saddr->v4.sin_addr.s_addr; |
| 444 | |
| 445 | SCTP_DEBUG_PRINTK("%s: DST:%u.%u.%u.%u, SRC:%u.%u.%u.%u - ", |
| 446 | __FUNCTION__, NIPQUAD(fl.fl4_dst), |
| 447 | NIPQUAD(fl.fl4_src)); |
| 448 | |
| 449 | if (!ip_route_output_key(&rt, &fl)) { |
| 450 | dst = &rt->u.dst; |
| 451 | } |
| 452 | |
| 453 | /* If there is no association or if a source address is passed, no |
| 454 | * more validation is required. |
| 455 | */ |
| 456 | if (!asoc || saddr) |
| 457 | goto out; |
| 458 | |
| 459 | bp = &asoc->base.bind_addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | |
| 461 | if (dst) { |
| 462 | /* Walk through the bind address list and look for a bind |
| 463 | * address that matches the source address of the returned dst. |
| 464 | */ |
Vlad Yasevich | 559cf71 | 2007-09-16 16:03:28 -0700 | [diff] [blame^] | 465 | rcu_read_lock(); |
| 466 | list_for_each_entry_rcu(laddr, &bp->address_list, list) { |
| 467 | if (!laddr->valid || !laddr->use_as_src) |
Sridhar Samudrala | dc022a9 | 2006-07-21 14:49:25 -0700 | [diff] [blame] | 468 | continue; |
Al Viro | 854d43a | 2006-11-20 17:06:24 -0800 | [diff] [blame] | 469 | sctp_v4_dst_saddr(&dst_saddr, dst, htons(bp->port)); |
| 470 | if (sctp_v4_cmp_addr(&dst_saddr, &laddr->a)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | goto out_unlock; |
| 472 | } |
Vlad Yasevich | 559cf71 | 2007-09-16 16:03:28 -0700 | [diff] [blame^] | 473 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | |
| 475 | /* None of the bound addresses match the source address of the |
| 476 | * dst. So release it. |
| 477 | */ |
| 478 | dst_release(dst); |
| 479 | dst = NULL; |
| 480 | } |
| 481 | |
| 482 | /* Walk through the bind address list and try to get a dst that |
| 483 | * matches a bind address as the source address. |
| 484 | */ |
Vlad Yasevich | 559cf71 | 2007-09-16 16:03:28 -0700 | [diff] [blame^] | 485 | rcu_read_lock(); |
| 486 | list_for_each_entry_rcu(laddr, &bp->address_list, list) { |
| 487 | if (!laddr->valid) |
| 488 | continue; |
Sridhar Samudrala | dc022a9 | 2006-07-21 14:49:25 -0700 | [diff] [blame] | 489 | if ((laddr->use_as_src) && |
Al Viro | 6244be4 | 2006-11-20 17:21:44 -0800 | [diff] [blame] | 490 | (AF_INET == laddr->a.sa.sa_family)) { |
| 491 | fl.fl4_src = laddr->a.v4.sin_addr.s_addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | if (!ip_route_output_key(&rt, &fl)) { |
| 493 | dst = &rt->u.dst; |
| 494 | goto out_unlock; |
| 495 | } |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | out_unlock: |
Vlad Yasevich | 559cf71 | 2007-09-16 16:03:28 -0700 | [diff] [blame^] | 500 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | out: |
| 502 | if (dst) |
| 503 | SCTP_DEBUG_PRINTK("rt_dst:%u.%u.%u.%u, rt_src:%u.%u.%u.%u\n", |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 504 | NIPQUAD(rt->rt_dst), NIPQUAD(rt->rt_src)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | else |
| 506 | SCTP_DEBUG_PRINTK("NO ROUTE\n"); |
| 507 | |
| 508 | return dst; |
| 509 | } |
| 510 | |
| 511 | /* For v4, the source address is cached in the route entry(dst). So no need |
| 512 | * to cache it separately and hence this is an empty routine. |
| 513 | */ |
| 514 | static void sctp_v4_get_saddr(struct sctp_association *asoc, |
| 515 | struct dst_entry *dst, |
| 516 | union sctp_addr *daddr, |
| 517 | union sctp_addr *saddr) |
| 518 | { |
| 519 | struct rtable *rt = (struct rtable *)dst; |
| 520 | |
Vladislav Yasevich | 23ec47a | 2005-11-11 16:05:55 -0800 | [diff] [blame] | 521 | if (!asoc) |
| 522 | return; |
| 523 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | if (rt) { |
| 525 | saddr->v4.sin_family = AF_INET; |
Al Viro | d3f7a54 | 2006-11-20 17:12:41 -0800 | [diff] [blame] | 526 | saddr->v4.sin_port = htons(asoc->base.bind_addr.port); |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 527 | saddr->v4.sin_addr.s_addr = rt->rt_src; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | } |
| 529 | } |
| 530 | |
| 531 | /* What interface did this skb arrive on? */ |
| 532 | static int sctp_v4_skb_iif(const struct sk_buff *skb) |
| 533 | { |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 534 | return ((struct rtable *)skb->dst)->rt_iif; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | /* Was this packet marked by Explicit Congestion Notification? */ |
| 538 | static int sctp_v4_is_ce(const struct sk_buff *skb) |
| 539 | { |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 540 | return INET_ECN_is_ce(ip_hdr(skb)->tos); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | /* Create and initialize a new sk for the socket returned by accept(). */ |
| 544 | static struct sock *sctp_v4_create_accept_sk(struct sock *sk, |
| 545 | struct sctp_association *asoc) |
| 546 | { |
| 547 | struct inet_sock *inet = inet_sk(sk); |
| 548 | struct inet_sock *newinet; |
| 549 | struct sock *newsk = sk_alloc(PF_INET, GFP_KERNEL, sk->sk_prot, 1); |
| 550 | |
| 551 | if (!newsk) |
| 552 | goto out; |
| 553 | |
| 554 | sock_init_data(NULL, newsk); |
| 555 | |
| 556 | newsk->sk_type = SOCK_STREAM; |
| 557 | |
| 558 | newsk->sk_no_check = sk->sk_no_check; |
| 559 | newsk->sk_reuse = sk->sk_reuse; |
| 560 | newsk->sk_shutdown = sk->sk_shutdown; |
| 561 | |
| 562 | newsk->sk_destruct = inet_sock_destruct; |
| 563 | newsk->sk_family = PF_INET; |
| 564 | newsk->sk_protocol = IPPROTO_SCTP; |
| 565 | newsk->sk_backlog_rcv = sk->sk_prot->backlog_rcv; |
| 566 | sock_reset_flag(newsk, SOCK_ZAPPED); |
| 567 | |
| 568 | newinet = inet_sk(newsk); |
| 569 | |
| 570 | /* Initialize sk's sport, dport, rcv_saddr and daddr for |
| 571 | * getsockname() and getpeername() |
| 572 | */ |
| 573 | newinet->sport = inet->sport; |
| 574 | newinet->saddr = inet->saddr; |
| 575 | newinet->rcv_saddr = inet->rcv_saddr; |
| 576 | newinet->dport = htons(asoc->peer.port); |
| 577 | newinet->daddr = asoc->peer.primary_addr.v4.sin_addr.s_addr; |
| 578 | newinet->pmtudisc = inet->pmtudisc; |
YOSHIFUJI Hideaki | d808ad9 | 2007-02-09 23:25:18 +0900 | [diff] [blame] | 579 | newinet->id = asoc->next_tsn ^ jiffies; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 580 | |
| 581 | newinet->uc_ttl = -1; |
| 582 | newinet->mc_loop = 1; |
| 583 | newinet->mc_ttl = 1; |
| 584 | newinet->mc_index = 0; |
| 585 | newinet->mc_list = NULL; |
| 586 | |
Arnaldo Carvalho de Melo | e684897 | 2005-08-09 19:45:38 -0700 | [diff] [blame] | 587 | sk_refcnt_debug_inc(newsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 588 | |
| 589 | if (newsk->sk_prot->init(newsk)) { |
| 590 | sk_common_release(newsk); |
| 591 | newsk = NULL; |
| 592 | } |
| 593 | |
| 594 | out: |
| 595 | return newsk; |
| 596 | } |
| 597 | |
| 598 | /* Map address, empty for v4 family */ |
| 599 | static void sctp_v4_addr_v4map(struct sctp_sock *sp, union sctp_addr *addr) |
| 600 | { |
| 601 | /* Empty */ |
| 602 | } |
| 603 | |
| 604 | /* Dump the v4 addr to the seq file. */ |
| 605 | static void sctp_v4_seq_dump_addr(struct seq_file *seq, union sctp_addr *addr) |
| 606 | { |
| 607 | seq_printf(seq, "%d.%d.%d.%d ", NIPQUAD(addr->v4.sin_addr)); |
| 608 | } |
| 609 | |
Vlad Yasevich | 2930354 | 2007-09-16 16:02:12 -0700 | [diff] [blame] | 610 | /* Event handler for inet address addition/deletion events. |
| 611 | * The sctp_local_addr_list needs to be protocted by a spin lock since |
| 612 | * multiple notifiers (say IPv4 and IPv6) may be running at the same |
| 613 | * time and thus corrupt the list. |
| 614 | * The reader side is protected with RCU. |
| 615 | */ |
Adrian Bunk | 2412318 | 2006-12-20 16:08:22 -0800 | [diff] [blame] | 616 | static int sctp_inetaddr_event(struct notifier_block *this, unsigned long ev, |
| 617 | void *ptr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | { |
Sridhar Samudrala | 29c7cf9 | 2006-12-13 16:26:26 -0800 | [diff] [blame] | 619 | struct in_ifaddr *ifa = (struct in_ifaddr *)ptr; |
Vlad Yasevich | 2930354 | 2007-09-16 16:02:12 -0700 | [diff] [blame] | 620 | struct sctp_sockaddr_entry *addr = NULL; |
| 621 | struct sctp_sockaddr_entry *temp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | |
Sridhar Samudrala | 29c7cf9 | 2006-12-13 16:26:26 -0800 | [diff] [blame] | 623 | switch (ev) { |
| 624 | case NETDEV_UP: |
| 625 | addr = kmalloc(sizeof(struct sctp_sockaddr_entry), GFP_ATOMIC); |
| 626 | if (addr) { |
| 627 | addr->a.v4.sin_family = AF_INET; |
| 628 | addr->a.v4.sin_port = 0; |
| 629 | addr->a.v4.sin_addr.s_addr = ifa->ifa_local; |
Vlad Yasevich | 2930354 | 2007-09-16 16:02:12 -0700 | [diff] [blame] | 630 | addr->valid = 1; |
| 631 | spin_lock_bh(&sctp_local_addr_lock); |
| 632 | list_add_tail_rcu(&addr->list, &sctp_local_addr_list); |
| 633 | spin_unlock_bh(&sctp_local_addr_lock); |
Sridhar Samudrala | 29c7cf9 | 2006-12-13 16:26:26 -0800 | [diff] [blame] | 634 | } |
| 635 | break; |
| 636 | case NETDEV_DOWN: |
Vlad Yasevich | 2930354 | 2007-09-16 16:02:12 -0700 | [diff] [blame] | 637 | spin_lock_bh(&sctp_local_addr_lock); |
| 638 | list_for_each_entry_safe(addr, temp, |
| 639 | &sctp_local_addr_list, list) { |
Sridhar Samudrala | 29c7cf9 | 2006-12-13 16:26:26 -0800 | [diff] [blame] | 640 | if (addr->a.v4.sin_addr.s_addr == ifa->ifa_local) { |
Vlad Yasevich | 2930354 | 2007-09-16 16:02:12 -0700 | [diff] [blame] | 641 | addr->valid = 0; |
| 642 | list_del_rcu(&addr->list); |
Sridhar Samudrala | 29c7cf9 | 2006-12-13 16:26:26 -0800 | [diff] [blame] | 643 | break; |
| 644 | } |
| 645 | } |
Vlad Yasevich | 2930354 | 2007-09-16 16:02:12 -0700 | [diff] [blame] | 646 | spin_unlock_bh(&sctp_local_addr_lock); |
| 647 | if (addr && !addr->valid) |
| 648 | call_rcu(&addr->rcu, sctp_local_addr_free); |
Sridhar Samudrala | 29c7cf9 | 2006-12-13 16:26:26 -0800 | [diff] [blame] | 649 | break; |
| 650 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 651 | |
| 652 | return NOTIFY_DONE; |
| 653 | } |
| 654 | |
| 655 | /* |
| 656 | * Initialize the control inode/socket with a control endpoint data |
| 657 | * structure. This endpoint is reserved exclusively for the OOTB processing. |
| 658 | */ |
| 659 | static int sctp_ctl_sock_init(void) |
| 660 | { |
| 661 | int err; |
| 662 | sa_family_t family; |
| 663 | |
| 664 | if (sctp_get_pf_specific(PF_INET6)) |
| 665 | family = PF_INET6; |
| 666 | else |
| 667 | family = PF_INET; |
| 668 | |
| 669 | err = sock_create_kern(family, SOCK_SEQPACKET, IPPROTO_SCTP, |
| 670 | &sctp_ctl_socket); |
| 671 | if (err < 0) { |
| 672 | printk(KERN_ERR |
| 673 | "SCTP: Failed to create the SCTP control socket.\n"); |
| 674 | return err; |
| 675 | } |
| 676 | sctp_ctl_socket->sk->sk_allocation = GFP_ATOMIC; |
| 677 | inet_sk(sctp_ctl_socket->sk)->uc_ttl = -1; |
| 678 | |
| 679 | return 0; |
| 680 | } |
| 681 | |
| 682 | /* Register address family specific functions. */ |
| 683 | int sctp_register_af(struct sctp_af *af) |
| 684 | { |
| 685 | switch (af->sa_family) { |
| 686 | case AF_INET: |
| 687 | if (sctp_af_v4_specific) |
| 688 | return 0; |
| 689 | sctp_af_v4_specific = af; |
| 690 | break; |
| 691 | case AF_INET6: |
| 692 | if (sctp_af_v6_specific) |
| 693 | return 0; |
| 694 | sctp_af_v6_specific = af; |
| 695 | break; |
| 696 | default: |
| 697 | return 0; |
| 698 | } |
| 699 | |
| 700 | INIT_LIST_HEAD(&af->list); |
| 701 | list_add_tail(&af->list, &sctp_address_families); |
| 702 | return 1; |
| 703 | } |
| 704 | |
| 705 | /* Get the table of functions for manipulating a particular address |
| 706 | * family. |
| 707 | */ |
| 708 | struct sctp_af *sctp_get_af_specific(sa_family_t family) |
| 709 | { |
| 710 | switch (family) { |
| 711 | case AF_INET: |
| 712 | return sctp_af_v4_specific; |
| 713 | case AF_INET6: |
| 714 | return sctp_af_v6_specific; |
| 715 | default: |
| 716 | return NULL; |
| 717 | } |
| 718 | } |
| 719 | |
| 720 | /* Common code to initialize a AF_INET msg_name. */ |
| 721 | static void sctp_inet_msgname(char *msgname, int *addr_len) |
| 722 | { |
| 723 | struct sockaddr_in *sin; |
| 724 | |
| 725 | sin = (struct sockaddr_in *)msgname; |
| 726 | *addr_len = sizeof(struct sockaddr_in); |
| 727 | sin->sin_family = AF_INET; |
| 728 | memset(sin->sin_zero, 0, sizeof(sin->sin_zero)); |
| 729 | } |
| 730 | |
| 731 | /* Copy the primary address of the peer primary address as the msg_name. */ |
| 732 | static void sctp_inet_event_msgname(struct sctp_ulpevent *event, char *msgname, |
| 733 | int *addr_len) |
| 734 | { |
| 735 | struct sockaddr_in *sin, *sinfrom; |
| 736 | |
| 737 | if (msgname) { |
| 738 | struct sctp_association *asoc; |
| 739 | |
| 740 | asoc = event->asoc; |
| 741 | sctp_inet_msgname(msgname, addr_len); |
| 742 | sin = (struct sockaddr_in *)msgname; |
| 743 | sinfrom = &asoc->peer.primary_addr.v4; |
| 744 | sin->sin_port = htons(asoc->peer.port); |
| 745 | sin->sin_addr.s_addr = sinfrom->sin_addr.s_addr; |
| 746 | } |
| 747 | } |
| 748 | |
| 749 | /* Initialize and copy out a msgname from an inbound skb. */ |
| 750 | static void sctp_inet_skb_msgname(struct sk_buff *skb, char *msgname, int *len) |
| 751 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 752 | if (msgname) { |
Arnaldo Carvalho de Melo | 2c0fd38 | 2007-03-13 13:59:32 -0300 | [diff] [blame] | 753 | struct sctphdr *sh = sctp_hdr(skb); |
| 754 | struct sockaddr_in *sin = (struct sockaddr_in *)msgname; |
| 755 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 756 | sctp_inet_msgname(msgname, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 757 | sin->sin_port = sh->source; |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 758 | sin->sin_addr.s_addr = ip_hdr(skb)->saddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 759 | } |
| 760 | } |
| 761 | |
| 762 | /* Do we support this AF? */ |
| 763 | static int sctp_inet_af_supported(sa_family_t family, struct sctp_sock *sp) |
| 764 | { |
| 765 | /* PF_INET only supports AF_INET addresses. */ |
| 766 | return (AF_INET == family); |
| 767 | } |
| 768 | |
| 769 | /* Address matching with wildcards allowed. */ |
| 770 | static int sctp_inet_cmp_addr(const union sctp_addr *addr1, |
| 771 | const union sctp_addr *addr2, |
| 772 | struct sctp_sock *opt) |
| 773 | { |
| 774 | /* PF_INET only supports AF_INET addresses. */ |
| 775 | if (addr1->sa.sa_family != addr2->sa.sa_family) |
| 776 | return 0; |
| 777 | if (INADDR_ANY == addr1->v4.sin_addr.s_addr || |
| 778 | INADDR_ANY == addr2->v4.sin_addr.s_addr) |
| 779 | return 1; |
| 780 | if (addr1->v4.sin_addr.s_addr == addr2->v4.sin_addr.s_addr) |
| 781 | return 1; |
| 782 | |
| 783 | return 0; |
| 784 | } |
| 785 | |
| 786 | /* Verify that provided sockaddr looks bindable. Common verification has |
| 787 | * already been taken care of. |
| 788 | */ |
| 789 | static int sctp_inet_bind_verify(struct sctp_sock *opt, union sctp_addr *addr) |
| 790 | { |
| 791 | return sctp_v4_available(addr, opt); |
| 792 | } |
| 793 | |
| 794 | /* Verify that sockaddr looks sendable. Common verification has already |
| 795 | * been taken care of. |
| 796 | */ |
| 797 | static int sctp_inet_send_verify(struct sctp_sock *opt, union sctp_addr *addr) |
| 798 | { |
| 799 | return 1; |
| 800 | } |
| 801 | |
| 802 | /* Fill in Supported Address Type information for INIT and INIT-ACK |
| 803 | * chunks. Returns number of addresses supported. |
| 804 | */ |
| 805 | static int sctp_inet_supported_addrs(const struct sctp_sock *opt, |
Al Viro | 3dbe865 | 2006-11-20 17:25:49 -0800 | [diff] [blame] | 806 | __be16 *types) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 807 | { |
| 808 | types[0] = SCTP_PARAM_IPV4_ADDRESS; |
| 809 | return 1; |
| 810 | } |
| 811 | |
| 812 | /* Wrapper routine that calls the ip transmit routine. */ |
| 813 | static inline int sctp_v4_xmit(struct sk_buff *skb, |
| 814 | struct sctp_transport *transport, int ipfragok) |
| 815 | { |
| 816 | SCTP_DEBUG_PRINTK("%s: skb:%p, len:%d, " |
| 817 | "src:%u.%u.%u.%u, dst:%u.%u.%u.%u\n", |
| 818 | __FUNCTION__, skb, skb->len, |
| 819 | NIPQUAD(((struct rtable *)skb->dst)->rt_src), |
| 820 | NIPQUAD(((struct rtable *)skb->dst)->rt_dst)); |
| 821 | |
| 822 | SCTP_INC_STATS(SCTP_MIB_OUTSCTPPACKS); |
David S. Miller | e89862f | 2007-01-26 01:04:55 -0800 | [diff] [blame] | 823 | return ip_queue_xmit(skb, ipfragok); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 824 | } |
| 825 | |
| 826 | static struct sctp_af sctp_ipv4_specific; |
| 827 | |
| 828 | static struct sctp_pf sctp_pf_inet = { |
| 829 | .event_msgname = sctp_inet_event_msgname, |
| 830 | .skb_msgname = sctp_inet_skb_msgname, |
| 831 | .af_supported = sctp_inet_af_supported, |
| 832 | .cmp_addr = sctp_inet_cmp_addr, |
| 833 | .bind_verify = sctp_inet_bind_verify, |
| 834 | .send_verify = sctp_inet_send_verify, |
| 835 | .supported_addrs = sctp_inet_supported_addrs, |
| 836 | .create_accept_sk = sctp_v4_create_accept_sk, |
| 837 | .addr_v4map = sctp_v4_addr_v4map, |
| 838 | .af = &sctp_ipv4_specific, |
| 839 | }; |
| 840 | |
| 841 | /* Notifier for inetaddr addition/deletion events. */ |
| 842 | static struct notifier_block sctp_inetaddr_notifier = { |
| 843 | .notifier_call = sctp_inetaddr_event, |
| 844 | }; |
| 845 | |
| 846 | /* Socket operations. */ |
Eric Dumazet | 90ddc4f | 2005-12-22 12:49:22 -0800 | [diff] [blame] | 847 | static const struct proto_ops inet_seqpacket_ops = { |
Arnaldo Carvalho de Melo | 543d9cf | 2006-03-20 22:48:35 -0800 | [diff] [blame] | 848 | .family = PF_INET, |
| 849 | .owner = THIS_MODULE, |
| 850 | .release = inet_release, /* Needs to be wrapped... */ |
| 851 | .bind = inet_bind, |
| 852 | .connect = inet_dgram_connect, |
| 853 | .socketpair = sock_no_socketpair, |
| 854 | .accept = inet_accept, |
| 855 | .getname = inet_getname, /* Semantics are different. */ |
| 856 | .poll = sctp_poll, |
| 857 | .ioctl = inet_ioctl, |
| 858 | .listen = sctp_inet_listen, |
| 859 | .shutdown = inet_shutdown, /* Looks harmless. */ |
| 860 | .setsockopt = sock_common_setsockopt, /* IP_SOL IP_OPTION is a problem */ |
| 861 | .getsockopt = sock_common_getsockopt, |
| 862 | .sendmsg = inet_sendmsg, |
| 863 | .recvmsg = sock_common_recvmsg, |
| 864 | .mmap = sock_no_mmap, |
| 865 | .sendpage = sock_no_sendpage, |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 866 | #ifdef CONFIG_COMPAT |
Arnaldo Carvalho de Melo | 543d9cf | 2006-03-20 22:48:35 -0800 | [diff] [blame] | 867 | .compat_setsockopt = compat_sock_common_setsockopt, |
| 868 | .compat_getsockopt = compat_sock_common_getsockopt, |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 869 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 870 | }; |
| 871 | |
| 872 | /* Registration with AF_INET family. */ |
| 873 | static struct inet_protosw sctp_seqpacket_protosw = { |
| 874 | .type = SOCK_SEQPACKET, |
| 875 | .protocol = IPPROTO_SCTP, |
| 876 | .prot = &sctp_prot, |
| 877 | .ops = &inet_seqpacket_ops, |
| 878 | .capability = -1, |
| 879 | .no_check = 0, |
| 880 | .flags = SCTP_PROTOSW_FLAG |
| 881 | }; |
| 882 | static struct inet_protosw sctp_stream_protosw = { |
| 883 | .type = SOCK_STREAM, |
| 884 | .protocol = IPPROTO_SCTP, |
| 885 | .prot = &sctp_prot, |
| 886 | .ops = &inet_seqpacket_ops, |
| 887 | .capability = -1, |
| 888 | .no_check = 0, |
| 889 | .flags = SCTP_PROTOSW_FLAG |
| 890 | }; |
| 891 | |
| 892 | /* Register with IP layer. */ |
| 893 | static struct net_protocol sctp_protocol = { |
| 894 | .handler = sctp_rcv, |
| 895 | .err_handler = sctp_v4_err, |
| 896 | .no_policy = 1, |
| 897 | }; |
| 898 | |
| 899 | /* IPv4 address related functions. */ |
| 900 | static struct sctp_af sctp_ipv4_specific = { |
Arnaldo Carvalho de Melo | 543d9cf | 2006-03-20 22:48:35 -0800 | [diff] [blame] | 901 | .sa_family = AF_INET, |
| 902 | .sctp_xmit = sctp_v4_xmit, |
| 903 | .setsockopt = ip_setsockopt, |
| 904 | .getsockopt = ip_getsockopt, |
| 905 | .get_dst = sctp_v4_get_dst, |
| 906 | .get_saddr = sctp_v4_get_saddr, |
| 907 | .copy_addrlist = sctp_v4_copy_addrlist, |
| 908 | .from_skb = sctp_v4_from_skb, |
| 909 | .from_sk = sctp_v4_from_sk, |
| 910 | .to_sk_saddr = sctp_v4_to_sk_saddr, |
| 911 | .to_sk_daddr = sctp_v4_to_sk_daddr, |
| 912 | .from_addr_param = sctp_v4_from_addr_param, |
| 913 | .to_addr_param = sctp_v4_to_addr_param, |
| 914 | .dst_saddr = sctp_v4_dst_saddr, |
| 915 | .cmp_addr = sctp_v4_cmp_addr, |
| 916 | .addr_valid = sctp_v4_addr_valid, |
| 917 | .inaddr_any = sctp_v4_inaddr_any, |
| 918 | .is_any = sctp_v4_is_any, |
| 919 | .available = sctp_v4_available, |
| 920 | .scope = sctp_v4_scope, |
| 921 | .skb_iif = sctp_v4_skb_iif, |
| 922 | .is_ce = sctp_v4_is_ce, |
| 923 | .seq_dump_addr = sctp_v4_seq_dump_addr, |
| 924 | .net_header_len = sizeof(struct iphdr), |
| 925 | .sockaddr_len = sizeof(struct sockaddr_in), |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 926 | #ifdef CONFIG_COMPAT |
Arnaldo Carvalho de Melo | 543d9cf | 2006-03-20 22:48:35 -0800 | [diff] [blame] | 927 | .compat_setsockopt = compat_ip_setsockopt, |
| 928 | .compat_getsockopt = compat_ip_getsockopt, |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 929 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 930 | }; |
| 931 | |
| 932 | struct sctp_pf *sctp_get_pf_specific(sa_family_t family) { |
| 933 | |
| 934 | switch (family) { |
| 935 | case PF_INET: |
| 936 | return sctp_pf_inet_specific; |
| 937 | case PF_INET6: |
| 938 | return sctp_pf_inet6_specific; |
| 939 | default: |
| 940 | return NULL; |
| 941 | } |
| 942 | } |
| 943 | |
| 944 | /* Register the PF specific function table. */ |
| 945 | int sctp_register_pf(struct sctp_pf *pf, sa_family_t family) |
| 946 | { |
| 947 | switch (family) { |
| 948 | case PF_INET: |
| 949 | if (sctp_pf_inet_specific) |
| 950 | return 0; |
| 951 | sctp_pf_inet_specific = pf; |
| 952 | break; |
| 953 | case PF_INET6: |
| 954 | if (sctp_pf_inet6_specific) |
| 955 | return 0; |
| 956 | sctp_pf_inet6_specific = pf; |
| 957 | break; |
| 958 | default: |
| 959 | return 0; |
| 960 | } |
| 961 | return 1; |
| 962 | } |
| 963 | |
| 964 | static int __init init_sctp_mibs(void) |
| 965 | { |
| 966 | sctp_statistics[0] = alloc_percpu(struct sctp_mib); |
| 967 | if (!sctp_statistics[0]) |
| 968 | return -ENOMEM; |
| 969 | sctp_statistics[1] = alloc_percpu(struct sctp_mib); |
| 970 | if (!sctp_statistics[1]) { |
| 971 | free_percpu(sctp_statistics[0]); |
| 972 | return -ENOMEM; |
| 973 | } |
| 974 | return 0; |
| 975 | |
| 976 | } |
| 977 | |
| 978 | static void cleanup_sctp_mibs(void) |
| 979 | { |
| 980 | free_percpu(sctp_statistics[0]); |
| 981 | free_percpu(sctp_statistics[1]); |
| 982 | } |
| 983 | |
| 984 | /* Initialize the universe into something sensible. */ |
| 985 | SCTP_STATIC __init int sctp_init(void) |
| 986 | { |
| 987 | int i; |
| 988 | int status = -EINVAL; |
| 989 | unsigned long goal; |
| 990 | int order; |
| 991 | |
| 992 | /* SCTP_DEBUG sanity check. */ |
| 993 | if (!sctp_sanity_check()) |
| 994 | goto out; |
| 995 | |
Sridhar Samudrala | 827bf12 | 2007-05-04 13:36:30 -0700 | [diff] [blame] | 996 | /* Allocate bind_bucket and chunk caches. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 997 | status = -ENOBUFS; |
| 998 | sctp_bucket_cachep = kmem_cache_create("sctp_bind_bucket", |
| 999 | sizeof(struct sctp_bind_bucket), |
| 1000 | 0, SLAB_HWCACHE_ALIGN, |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 1001 | NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1002 | if (!sctp_bucket_cachep) |
Sridhar Samudrala | 827bf12 | 2007-05-04 13:36:30 -0700 | [diff] [blame] | 1003 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1004 | |
| 1005 | sctp_chunk_cachep = kmem_cache_create("sctp_chunk", |
| 1006 | sizeof(struct sctp_chunk), |
| 1007 | 0, SLAB_HWCACHE_ALIGN, |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 1008 | NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1009 | if (!sctp_chunk_cachep) |
| 1010 | goto err_chunk_cachep; |
| 1011 | |
| 1012 | /* Allocate and initialise sctp mibs. */ |
| 1013 | status = init_sctp_mibs(); |
| 1014 | if (status) |
| 1015 | goto err_init_mibs; |
| 1016 | |
| 1017 | /* Initialize proc fs directory. */ |
| 1018 | status = sctp_proc_init(); |
| 1019 | if (status) |
| 1020 | goto err_init_proc; |
| 1021 | |
| 1022 | /* Initialize object count debugging. */ |
| 1023 | sctp_dbg_objcnt_init(); |
| 1024 | |
| 1025 | /* Initialize the SCTP specific PF functions. */ |
| 1026 | sctp_register_pf(&sctp_pf_inet, PF_INET); |
| 1027 | /* |
| 1028 | * 14. Suggested SCTP Protocol Parameter Values |
| 1029 | */ |
| 1030 | /* The following protocol parameters are RECOMMENDED: */ |
| 1031 | /* RTO.Initial - 3 seconds */ |
| 1032 | sctp_rto_initial = SCTP_RTO_INITIAL; |
| 1033 | /* RTO.Min - 1 second */ |
| 1034 | sctp_rto_min = SCTP_RTO_MIN; |
| 1035 | /* RTO.Max - 60 seconds */ |
| 1036 | sctp_rto_max = SCTP_RTO_MAX; |
| 1037 | /* RTO.Alpha - 1/8 */ |
| 1038 | sctp_rto_alpha = SCTP_RTO_ALPHA; |
| 1039 | /* RTO.Beta - 1/4 */ |
| 1040 | sctp_rto_beta = SCTP_RTO_BETA; |
| 1041 | |
| 1042 | /* Valid.Cookie.Life - 60 seconds */ |
Vladislav Yasevich | 3fd091e | 2006-08-22 13:29:17 -0700 | [diff] [blame] | 1043 | sctp_valid_cookie_life = SCTP_DEFAULT_COOKIE_LIFE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1044 | |
| 1045 | /* Whether Cookie Preservative is enabled(1) or not(0) */ |
| 1046 | sctp_cookie_preserve_enable = 1; |
| 1047 | |
| 1048 | /* Max.Burst - 4 */ |
Vlad Yasevich | 7033157 | 2007-03-23 11:34:36 -0700 | [diff] [blame] | 1049 | sctp_max_burst = SCTP_DEFAULT_MAX_BURST; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1050 | |
| 1051 | /* Association.Max.Retrans - 10 attempts |
| 1052 | * Path.Max.Retrans - 5 attempts (per destination address) |
| 1053 | * Max.Init.Retransmits - 8 attempts |
| 1054 | */ |
| 1055 | sctp_max_retrans_association = 10; |
| 1056 | sctp_max_retrans_path = 5; |
| 1057 | sctp_max_retrans_init = 8; |
| 1058 | |
Neil Horman | 4eb701d | 2005-04-28 12:02:04 -0700 | [diff] [blame] | 1059 | /* Sendbuffer growth - do per-socket accounting */ |
| 1060 | sctp_sndbuf_policy = 0; |
| 1061 | |
Neil Horman | 049b3ff | 2005-11-11 16:08:24 -0800 | [diff] [blame] | 1062 | /* Rcvbuffer growth - do per-socket accounting */ |
| 1063 | sctp_rcvbuf_policy = 0; |
| 1064 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1065 | /* HB.interval - 30 seconds */ |
Vlad Yasevich | 2f85a42 | 2005-06-28 13:24:23 -0700 | [diff] [blame] | 1066 | sctp_hb_interval = SCTP_DEFAULT_TIMEOUT_HEARTBEAT; |
| 1067 | |
| 1068 | /* delayed SACK timeout */ |
| 1069 | sctp_sack_timeout = SCTP_DEFAULT_TIMEOUT_SACK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1070 | |
| 1071 | /* Implementation specific variables. */ |
| 1072 | |
| 1073 | /* Initialize default stream count setup information. */ |
| 1074 | sctp_max_instreams = SCTP_DEFAULT_INSTREAMS; |
| 1075 | sctp_max_outstreams = SCTP_DEFAULT_OUTSTREAMS; |
| 1076 | |
| 1077 | /* Initialize handle used for association ids. */ |
| 1078 | idr_init(&sctp_assocs_id); |
| 1079 | |
| 1080 | /* Size and allocate the association hash table. |
| 1081 | * The methodology is similar to that of the tcp hash tables. |
| 1082 | */ |
| 1083 | if (num_physpages >= (128 * 1024)) |
| 1084 | goal = num_physpages >> (22 - PAGE_SHIFT); |
| 1085 | else |
| 1086 | goal = num_physpages >> (24 - PAGE_SHIFT); |
| 1087 | |
| 1088 | for (order = 0; (1UL << order) < goal; order++) |
| 1089 | ; |
| 1090 | |
| 1091 | do { |
| 1092 | sctp_assoc_hashsize = (1UL << order) * PAGE_SIZE / |
| 1093 | sizeof(struct sctp_hashbucket); |
| 1094 | if ((sctp_assoc_hashsize > (64 * 1024)) && order > 0) |
| 1095 | continue; |
| 1096 | sctp_assoc_hashtable = (struct sctp_hashbucket *) |
| 1097 | __get_free_pages(GFP_ATOMIC, order); |
| 1098 | } while (!sctp_assoc_hashtable && --order > 0); |
| 1099 | if (!sctp_assoc_hashtable) { |
| 1100 | printk(KERN_ERR "SCTP: Failed association hash alloc.\n"); |
| 1101 | status = -ENOMEM; |
| 1102 | goto err_ahash_alloc; |
| 1103 | } |
| 1104 | for (i = 0; i < sctp_assoc_hashsize; i++) { |
| 1105 | rwlock_init(&sctp_assoc_hashtable[i].lock); |
| 1106 | sctp_assoc_hashtable[i].chain = NULL; |
| 1107 | } |
| 1108 | |
| 1109 | /* Allocate and initialize the endpoint hash table. */ |
| 1110 | sctp_ep_hashsize = 64; |
| 1111 | sctp_ep_hashtable = (struct sctp_hashbucket *) |
| 1112 | kmalloc(64 * sizeof(struct sctp_hashbucket), GFP_KERNEL); |
| 1113 | if (!sctp_ep_hashtable) { |
| 1114 | printk(KERN_ERR "SCTP: Failed endpoint_hash alloc.\n"); |
| 1115 | status = -ENOMEM; |
| 1116 | goto err_ehash_alloc; |
| 1117 | } |
| 1118 | for (i = 0; i < sctp_ep_hashsize; i++) { |
| 1119 | rwlock_init(&sctp_ep_hashtable[i].lock); |
| 1120 | sctp_ep_hashtable[i].chain = NULL; |
| 1121 | } |
| 1122 | |
| 1123 | /* Allocate and initialize the SCTP port hash table. */ |
| 1124 | do { |
| 1125 | sctp_port_hashsize = (1UL << order) * PAGE_SIZE / |
| 1126 | sizeof(struct sctp_bind_hashbucket); |
| 1127 | if ((sctp_port_hashsize > (64 * 1024)) && order > 0) |
| 1128 | continue; |
| 1129 | sctp_port_hashtable = (struct sctp_bind_hashbucket *) |
| 1130 | __get_free_pages(GFP_ATOMIC, order); |
| 1131 | } while (!sctp_port_hashtable && --order > 0); |
| 1132 | if (!sctp_port_hashtable) { |
| 1133 | printk(KERN_ERR "SCTP: Failed bind hash alloc."); |
| 1134 | status = -ENOMEM; |
| 1135 | goto err_bhash_alloc; |
| 1136 | } |
| 1137 | for (i = 0; i < sctp_port_hashsize; i++) { |
| 1138 | spin_lock_init(&sctp_port_hashtable[i].lock); |
| 1139 | sctp_port_hashtable[i].chain = NULL; |
| 1140 | } |
| 1141 | |
| 1142 | spin_lock_init(&sctp_port_alloc_lock); |
| 1143 | sctp_port_rover = sysctl_local_port_range[0] - 1; |
| 1144 | |
| 1145 | printk(KERN_INFO "SCTP: Hash tables configured " |
| 1146 | "(established %d bind %d)\n", |
| 1147 | sctp_assoc_hashsize, sctp_port_hashsize); |
| 1148 | |
| 1149 | /* Disable ADDIP by default. */ |
| 1150 | sctp_addip_enable = 0; |
| 1151 | |
| 1152 | /* Enable PR-SCTP by default. */ |
| 1153 | sctp_prsctp_enable = 1; |
| 1154 | |
| 1155 | sctp_sysctl_register(); |
| 1156 | |
| 1157 | INIT_LIST_HEAD(&sctp_address_families); |
| 1158 | sctp_register_af(&sctp_ipv4_specific); |
| 1159 | |
Sridhar Samudrala | 827bf12 | 2007-05-04 13:36:30 -0700 | [diff] [blame] | 1160 | status = proto_register(&sctp_prot, 1); |
| 1161 | if (status) |
| 1162 | goto err_proto_register; |
| 1163 | |
| 1164 | /* Register SCTP(UDP and TCP style) with socket layer. */ |
| 1165 | inet_register_protosw(&sctp_seqpacket_protosw); |
| 1166 | inet_register_protosw(&sctp_stream_protosw); |
| 1167 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1168 | status = sctp_v6_init(); |
| 1169 | if (status) |
| 1170 | goto err_v6_init; |
| 1171 | |
| 1172 | /* Initialize the control inode/socket for handling OOTB packets. */ |
| 1173 | if ((status = sctp_ctl_sock_init())) { |
| 1174 | printk (KERN_ERR |
| 1175 | "SCTP: Failed to initialize the SCTP control sock.\n"); |
| 1176 | goto err_ctl_sock_init; |
| 1177 | } |
| 1178 | |
| 1179 | /* Initialize the local address list. */ |
| 1180 | INIT_LIST_HEAD(&sctp_local_addr_list); |
Vlad Yasevich | 2930354 | 2007-09-16 16:02:12 -0700 | [diff] [blame] | 1181 | spin_lock_init(&sctp_local_addr_lock); |
Sridhar Samudrala | 29c7cf9 | 2006-12-13 16:26:26 -0800 | [diff] [blame] | 1182 | sctp_get_local_addr_list(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1183 | |
| 1184 | /* Register notifier for inet address additions/deletions. */ |
| 1185 | register_inetaddr_notifier(&sctp_inetaddr_notifier); |
| 1186 | |
Sridhar Samudrala | 827bf12 | 2007-05-04 13:36:30 -0700 | [diff] [blame] | 1187 | /* Register SCTP with inet layer. */ |
| 1188 | if (inet_add_protocol(&sctp_protocol, IPPROTO_SCTP) < 0) { |
| 1189 | status = -EAGAIN; |
| 1190 | goto err_add_protocol; |
| 1191 | } |
| 1192 | |
| 1193 | /* Register SCTP with inet6 layer. */ |
| 1194 | status = sctp_v6_add_protocol(); |
| 1195 | if (status) |
| 1196 | goto err_v6_add_protocol; |
| 1197 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1198 | __unsafe(THIS_MODULE); |
| 1199 | status = 0; |
| 1200 | out: |
| 1201 | return status; |
Sridhar Samudrala | 827bf12 | 2007-05-04 13:36:30 -0700 | [diff] [blame] | 1202 | err_v6_add_protocol: |
| 1203 | inet_del_protocol(&sctp_protocol, IPPROTO_SCTP); |
| 1204 | unregister_inetaddr_notifier(&sctp_inetaddr_notifier); |
| 1205 | err_add_protocol: |
| 1206 | sctp_free_local_addr_list(); |
| 1207 | sock_release(sctp_ctl_socket); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1208 | err_ctl_sock_init: |
| 1209 | sctp_v6_exit(); |
| 1210 | err_v6_init: |
Sridhar Samudrala | 827bf12 | 2007-05-04 13:36:30 -0700 | [diff] [blame] | 1211 | inet_unregister_protosw(&sctp_stream_protosw); |
| 1212 | inet_unregister_protosw(&sctp_seqpacket_protosw); |
| 1213 | proto_unregister(&sctp_prot); |
| 1214 | err_proto_register: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1215 | sctp_sysctl_unregister(); |
| 1216 | list_del(&sctp_ipv4_specific.list); |
| 1217 | free_pages((unsigned long)sctp_port_hashtable, |
| 1218 | get_order(sctp_port_hashsize * |
| 1219 | sizeof(struct sctp_bind_hashbucket))); |
| 1220 | err_bhash_alloc: |
| 1221 | kfree(sctp_ep_hashtable); |
| 1222 | err_ehash_alloc: |
| 1223 | free_pages((unsigned long)sctp_assoc_hashtable, |
| 1224 | get_order(sctp_assoc_hashsize * |
| 1225 | sizeof(struct sctp_hashbucket))); |
| 1226 | err_ahash_alloc: |
| 1227 | sctp_dbg_objcnt_exit(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1228 | sctp_proc_exit(); |
Sridhar Samudrala | 827bf12 | 2007-05-04 13:36:30 -0700 | [diff] [blame] | 1229 | err_init_proc: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1230 | cleanup_sctp_mibs(); |
| 1231 | err_init_mibs: |
| 1232 | kmem_cache_destroy(sctp_chunk_cachep); |
| 1233 | err_chunk_cachep: |
| 1234 | kmem_cache_destroy(sctp_bucket_cachep); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1235 | goto out; |
| 1236 | } |
| 1237 | |
| 1238 | /* Exit handler for the SCTP protocol. */ |
| 1239 | SCTP_STATIC __exit void sctp_exit(void) |
| 1240 | { |
| 1241 | /* BUG. This should probably do something useful like clean |
| 1242 | * up all the remaining associations and all that memory. |
| 1243 | */ |
| 1244 | |
Sridhar Samudrala | 827bf12 | 2007-05-04 13:36:30 -0700 | [diff] [blame] | 1245 | /* Unregister with inet6/inet layers. */ |
| 1246 | sctp_v6_del_protocol(); |
| 1247 | inet_del_protocol(&sctp_protocol, IPPROTO_SCTP); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1248 | |
Vlad Yasevich | 2930354 | 2007-09-16 16:02:12 -0700 | [diff] [blame] | 1249 | /* Unregister notifier for inet address additions/deletions. */ |
| 1250 | unregister_inetaddr_notifier(&sctp_inetaddr_notifier); |
| 1251 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1252 | /* Free the local address list. */ |
| 1253 | sctp_free_local_addr_list(); |
| 1254 | |
| 1255 | /* Free the control endpoint. */ |
| 1256 | sock_release(sctp_ctl_socket); |
| 1257 | |
Sridhar Samudrala | 827bf12 | 2007-05-04 13:36:30 -0700 | [diff] [blame] | 1258 | /* Cleanup v6 initializations. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1259 | sctp_v6_exit(); |
Sridhar Samudrala | 827bf12 | 2007-05-04 13:36:30 -0700 | [diff] [blame] | 1260 | |
| 1261 | /* Unregister with socket layer. */ |
| 1262 | inet_unregister_protosw(&sctp_stream_protosw); |
| 1263 | inet_unregister_protosw(&sctp_seqpacket_protosw); |
| 1264 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1265 | sctp_sysctl_unregister(); |
| 1266 | list_del(&sctp_ipv4_specific.list); |
| 1267 | |
| 1268 | free_pages((unsigned long)sctp_assoc_hashtable, |
| 1269 | get_order(sctp_assoc_hashsize * |
| 1270 | sizeof(struct sctp_hashbucket))); |
| 1271 | kfree(sctp_ep_hashtable); |
| 1272 | free_pages((unsigned long)sctp_port_hashtable, |
| 1273 | get_order(sctp_port_hashsize * |
| 1274 | sizeof(struct sctp_bind_hashbucket))); |
| 1275 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1276 | sctp_dbg_objcnt_exit(); |
| 1277 | sctp_proc_exit(); |
| 1278 | cleanup_sctp_mibs(); |
| 1279 | |
Sridhar Samudrala | 827bf12 | 2007-05-04 13:36:30 -0700 | [diff] [blame] | 1280 | kmem_cache_destroy(sctp_chunk_cachep); |
| 1281 | kmem_cache_destroy(sctp_bucket_cachep); |
| 1282 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1283 | proto_unregister(&sctp_prot); |
| 1284 | } |
| 1285 | |
| 1286 | module_init(sctp_init); |
| 1287 | module_exit(sctp_exit); |
| 1288 | |
Arnaldo Carvalho de Melo | bb97d31 | 2005-08-09 20:19:14 -0700 | [diff] [blame] | 1289 | /* |
| 1290 | * __stringify doesn't likes enums, so use IPPROTO_SCTP value (132) directly. |
| 1291 | */ |
| 1292 | MODULE_ALIAS("net-pf-" __stringify(PF_INET) "-proto-132"); |
Sridhar Samudrala | 882a382 | 2006-12-13 16:33:35 -0800 | [diff] [blame] | 1293 | MODULE_ALIAS("net-pf-" __stringify(PF_INET6) "-proto-132"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1294 | MODULE_AUTHOR("Linux Kernel SCTP developers <lksctp-developers@lists.sourceforge.net>"); |
| 1295 | MODULE_DESCRIPTION("Support for the SCTP protocol (RFC2960)"); |
| 1296 | MODULE_LICENSE("GPL"); |