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