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