ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / net / sctp / protocol.c
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>
57 #include <net/sctp/sctp.h>
58 #include <net/addrconf.h>
59 #include <net/inet_common.h>
60 #include <net/inet_ecn.h>
61
62 /* Global data structures. */
63 struct sctp_globals sctp_globals;
64 struct proc_dir_entry   *proc_net_sctp;
65 DEFINE_SNMP_STAT(struct sctp_mib, sctp_statistics);
66
67 struct idr sctp_assocs_id;
68 spinlock_t sctp_assocs_id_lock = SPIN_LOCK_UNLOCKED;
69
70 /* This is the global socket data structure used for responding to
71  * the Out-of-the-blue (OOTB) packets.  A control sock will be created
72  * for this socket at the initialization time.
73  */
74 static struct socket *sctp_ctl_socket;
75
76 static struct sctp_pf *sctp_pf_inet6_specific;
77 static struct sctp_pf *sctp_pf_inet_specific;
78 static struct sctp_af *sctp_af_v4_specific;
79 static struct sctp_af *sctp_af_v6_specific;
80
81 kmem_cache_t *sctp_chunk_cachep;
82 kmem_cache_t *sctp_bucket_cachep;
83
84 extern struct net_proto_family inet_family_ops;
85
86 extern int sctp_snmp_proc_init(void);
87 extern int sctp_snmp_proc_exit(void);
88 extern int sctp_eps_proc_init(void);
89 extern int sctp_eps_proc_exit(void);
90 extern int sctp_assocs_proc_init(void);
91 extern int sctp_assocs_proc_exit(void);
92
93 /* Return the address of the control sock. */
94 struct sock *sctp_get_ctl_sock(void)
95 {
96         return sctp_ctl_socket->sk;
97 }
98
99 /* Set up the proc fs entry for the SCTP protocol. */
100 __init int sctp_proc_init(void)
101 {
102         if (!proc_net_sctp) {
103                 struct proc_dir_entry *ent;
104                 ent = proc_mkdir("net/sctp", 0);
105                 if (ent) {
106                         ent->owner = THIS_MODULE;
107                         proc_net_sctp = ent;
108                 } else
109                         goto out_nomem;
110         }
111
112         if (sctp_snmp_proc_init())
113                 goto out_nomem; 
114         if (sctp_eps_proc_init())
115                 goto out_nomem; 
116         if (sctp_assocs_proc_init())
117                 goto out_nomem; 
118
119         return 0;
120
121 out_nomem:
122         return -ENOMEM;
123 }
124
125 /* Clean up the proc fs entry for the SCTP protocol. 
126  * Note: Do not make this __exit as it is used in the init error
127  * path.
128  */
129 void sctp_proc_exit(void)
130 {
131         sctp_snmp_proc_exit();
132         sctp_eps_proc_exit();
133         sctp_assocs_proc_exit();
134
135         if (proc_net_sctp) {
136                 proc_net_sctp = NULL;
137                 remove_proc_entry("net/sctp", 0);
138         }
139 }
140
141 /* Private helper to extract ipv4 address and stash them in
142  * the protocol structure.
143  */
144 static void sctp_v4_copy_addrlist(struct list_head *addrlist,
145                                   struct net_device *dev)
146 {
147         struct in_device *in_dev;
148         struct in_ifaddr *ifa;
149         struct sctp_sockaddr_entry *addr;
150
151         read_lock(&inetdev_lock);
152         if ((in_dev = __in_dev_get(dev)) == NULL) {
153                 read_unlock(&inetdev_lock);
154                 return;
155         }
156
157         read_lock(&in_dev->lock);
158         for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
159                 /* Add the address to the local list.  */
160                 addr = t_new(struct sctp_sockaddr_entry, GFP_ATOMIC);
161                 if (addr) {
162                         addr->a.v4.sin_family = AF_INET;
163                         addr->a.v4.sin_port = 0;
164                         addr->a.v4.sin_addr.s_addr = ifa->ifa_local;
165                         list_add_tail(&addr->list, addrlist);
166                 }
167         }
168
169         read_unlock(&in_dev->lock);
170         read_unlock(&inetdev_lock);
171 }
172
173 /* Extract our IP addresses from the system and stash them in the
174  * protocol structure.
175  */
176 static void __sctp_get_local_addr_list(void)
177 {
178         struct net_device *dev;
179         struct list_head *pos;
180         struct sctp_af *af;
181
182         read_lock(&dev_base_lock);
183         for (dev = dev_base; dev; dev = dev->next) {
184                 __list_for_each(pos, &sctp_address_families) {
185                         af = list_entry(pos, struct sctp_af, list);
186                         af->copy_addrlist(&sctp_local_addr_list, dev);
187                 }
188         }
189         read_unlock(&dev_base_lock);
190 }
191
192 static void sctp_get_local_addr_list(void)
193 {
194         unsigned long flags;
195
196         sctp_spin_lock_irqsave(&sctp_local_addr_lock, flags);
197         __sctp_get_local_addr_list();
198         sctp_spin_unlock_irqrestore(&sctp_local_addr_lock, flags);
199 }
200
201 /* Free the existing local addresses.  */
202 static void __sctp_free_local_addr_list(void)
203 {
204         struct sctp_sockaddr_entry *addr;
205         struct list_head *pos, *temp;
206
207         list_for_each_safe(pos, temp, &sctp_local_addr_list) {
208                 addr = list_entry(pos, struct sctp_sockaddr_entry, list);
209                 list_del(pos);
210                 kfree(addr);
211         }
212 }
213
214 /* Free the existing local addresses.  */
215 static void sctp_free_local_addr_list(void)
216 {
217         unsigned long flags;
218
219         sctp_spin_lock_irqsave(&sctp_local_addr_lock, flags);
220         __sctp_free_local_addr_list();
221         sctp_spin_unlock_irqrestore(&sctp_local_addr_lock, flags);
222 }
223
224 /* Copy the local addresses which are valid for 'scope' into 'bp'.  */
225 int sctp_copy_local_addr_list(struct sctp_bind_addr *bp, sctp_scope_t scope,
226                               int gfp, int copy_flags)
227 {
228         struct sctp_sockaddr_entry *addr;
229         int error = 0;
230         struct list_head *pos;
231         unsigned long flags;
232
233         sctp_spin_lock_irqsave(&sctp_local_addr_lock, flags);
234         list_for_each(pos, &sctp_local_addr_list) {
235                 addr = list_entry(pos, struct sctp_sockaddr_entry, list);
236                 if (sctp_in_scope(&addr->a, scope)) {
237                         /* Now that the address is in scope, check to see if
238                          * the address type is really supported by the local
239                          * sock as well as the remote peer.
240                          */
241                         if ((((AF_INET == addr->a.sa.sa_family) &&
242                               (copy_flags & SCTP_ADDR4_PEERSUPP))) ||
243                             (((AF_INET6 == addr->a.sa.sa_family) &&
244                               (copy_flags & SCTP_ADDR6_ALLOWED) &&
245                               (copy_flags & SCTP_ADDR6_PEERSUPP)))) {
246                                 error = sctp_add_bind_addr(bp, &addr->a, 
247                                                            GFP_ATOMIC);
248                                 if (error)
249                                         goto end_copy;
250                         }
251                 }
252         }
253
254 end_copy:
255         sctp_spin_unlock_irqrestore(&sctp_local_addr_lock, flags);
256         return error;
257 }
258
259 /* Initialize a sctp_addr from in incoming skb.  */
260 static void sctp_v4_from_skb(union sctp_addr *addr, struct sk_buff *skb,
261                              int is_saddr)
262 {
263         void *from;
264         __u16 *port;
265         struct sctphdr *sh;
266
267         port = &addr->v4.sin_port;
268         addr->v4.sin_family = AF_INET;
269
270         sh = (struct sctphdr *) skb->h.raw;
271         if (is_saddr) {
272                 *port  = ntohs(sh->source);
273                 from = &skb->nh.iph->saddr;
274         } else {
275                 *port = ntohs(sh->dest);
276                 from = &skb->nh.iph->daddr;
277         }
278         memcpy(&addr->v4.sin_addr.s_addr, from, sizeof(struct in_addr));
279 }
280
281 /* Initialize an sctp_addr from a socket. */
282 static void sctp_v4_from_sk(union sctp_addr *addr, struct sock *sk)
283 {
284         addr->v4.sin_family = AF_INET;
285         addr->v4.sin_port = inet_sk(sk)->num;
286         addr->v4.sin_addr.s_addr = inet_sk(sk)->rcv_saddr;
287 }
288
289 /* Initialize sk->sk_rcv_saddr from sctp_addr. */
290 static void sctp_v4_to_sk_saddr(union sctp_addr *addr, struct sock *sk)
291 {
292         inet_sk(sk)->rcv_saddr = addr->v4.sin_addr.s_addr;
293 }
294
295 /* Initialize sk->sk_daddr from sctp_addr. */
296 static void sctp_v4_to_sk_daddr(union sctp_addr *addr, struct sock *sk)
297 {
298         inet_sk(sk)->daddr = addr->v4.sin_addr.s_addr;
299 }
300
301 /* Initialize a sctp_addr from an address parameter. */
302 static void sctp_v4_from_addr_param(union sctp_addr *addr,
303                                     union sctp_addr_param *param,
304                                     __u16 port, int iif)
305 {
306         addr->v4.sin_family = AF_INET;
307         addr->v4.sin_port = port;
308         addr->v4.sin_addr.s_addr = param->v4.addr.s_addr;
309 }
310
311 /* Initialize an address parameter from a sctp_addr and return the length
312  * of the address parameter.
313  */
314 static int sctp_v4_to_addr_param(const union sctp_addr *addr,
315                                  union sctp_addr_param *param)
316 {
317         int length = sizeof(sctp_ipv4addr_param_t);
318
319         param->v4.param_hdr.type = SCTP_PARAM_IPV4_ADDRESS;
320         param->v4.param_hdr.length = ntohs(length);
321         param->v4.addr.s_addr = addr->v4.sin_addr.s_addr;       
322
323         return length;
324 }
325
326 /* Initialize a sctp_addr from a dst_entry. */
327 static void sctp_v4_dst_saddr(union sctp_addr *saddr, struct dst_entry *dst,
328                               unsigned short port)
329 {
330         struct rtable *rt = (struct rtable *)dst;
331         saddr->v4.sin_family = AF_INET;
332         saddr->v4.sin_port = port;
333         saddr->v4.sin_addr.s_addr = rt->rt_src;
334 }
335
336 /* Compare two addresses exactly. */
337 static int sctp_v4_cmp_addr(const union sctp_addr *addr1,
338                             const union sctp_addr *addr2)
339 {
340         if (addr1->sa.sa_family != addr2->sa.sa_family)
341                 return 0;
342         if (addr1->v4.sin_port != addr2->v4.sin_port)
343                 return 0;
344         if (addr1->v4.sin_addr.s_addr != addr2->v4.sin_addr.s_addr)
345                 return 0;
346
347         return 1;
348 }
349
350 /* Initialize addr struct to INADDR_ANY. */
351 static void sctp_v4_inaddr_any(union sctp_addr *addr, unsigned short port)
352 {
353         addr->v4.sin_family = AF_INET;
354         addr->v4.sin_addr.s_addr = INADDR_ANY;
355         addr->v4.sin_port = port;
356 }
357
358 /* Is this a wildcard address? */
359 static int sctp_v4_is_any(const union sctp_addr *addr)
360 {
361         return INADDR_ANY == addr->v4.sin_addr.s_addr;
362 }
363
364 /* This function checks if the address is a valid address to be used for
365  * SCTP binding.
366  *
367  * Output:
368  * Return 0 - If the address is a non-unicast or an illegal address.
369  * Return 1 - If the address is a unicast.
370  */
371 static int sctp_v4_addr_valid(union sctp_addr *addr, struct sctp_opt *sp)
372 {
373         /* Is this a non-unicast address or a unusable SCTP address? */
374         if (IS_IPV4_UNUSABLE_ADDRESS(&addr->v4.sin_addr.s_addr))
375                 return 0;
376
377         return 1;
378 }
379
380 /* Should this be available for binding?   */
381 static int sctp_v4_available(union sctp_addr *addr, struct sctp_opt *sp)
382 {
383         int ret = inet_addr_type(addr->v4.sin_addr.s_addr);
384
385         /* FIXME: ip_nonlocal_bind sysctl support. */
386
387         if (addr->v4.sin_addr.s_addr != INADDR_ANY && ret != RTN_LOCAL)
388                 return 0;
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 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);
483                         sctp_v4_dst_saddr(&dst_saddr, dst, bp->port);
484                         if (sctp_v4_cmp_addr(&dst_saddr, &laddr->a))
485                                 goto out_unlock;
486                 }
487                 sctp_read_unlock(addr_lock);
488
489                 /* None of the bound addresses match the source address of the
490                  * dst. So release it.
491                  */
492                 dst_release(dst);
493                 dst = NULL;
494         }
495
496         /* Walk through the bind address list and try to get a dst that
497          * matches a bind address as the source address.
498          */
499         sctp_read_lock(addr_lock);
500         list_for_each(pos, &bp->address_list) {
501                 laddr = list_entry(pos, struct sctp_sockaddr_entry, list);
502
503                 if (AF_INET == laddr->a.sa.sa_family) {
504                         fl.fl4_src = laddr->a.v4.sin_addr.s_addr;
505                         if (!ip_route_output_key(&rt, &fl)) {
506                                 dst = &rt->u.dst;
507                                 goto out_unlock;
508                         }
509                 }
510         }
511
512 out_unlock:
513         sctp_read_unlock(addr_lock);
514 out:
515         if (dst)
516                 SCTP_DEBUG_PRINTK("rt_dst:%u.%u.%u.%u, rt_src:%u.%u.%u.%u\n",
517                                   NIPQUAD(rt->rt_dst), NIPQUAD(rt->rt_src));
518         else
519                 SCTP_DEBUG_PRINTK("NO ROUTE\n");
520
521         return dst;
522 }
523
524 /* For v4, the source address is cached in the route entry(dst). So no need
525  * to cache it separately and hence this is an empty routine.
526  */
527 void sctp_v4_get_saddr(struct sctp_association *asoc,
528                        struct dst_entry *dst,
529                        union sctp_addr *daddr,
530                        union sctp_addr *saddr)
531 {
532         struct rtable *rt = (struct rtable *)dst;
533
534         if (rt) {
535                 saddr->v4.sin_family = AF_INET;
536                 saddr->v4.sin_port = asoc->base.bind_addr.port;  
537                 saddr->v4.sin_addr.s_addr = rt->rt_src; 
538         }
539 }
540
541 /* What interface did this skb arrive on? */
542 static int sctp_v4_skb_iif(const struct sk_buff *skb)
543 {
544         return ((struct rtable *)skb->dst)->rt_iif;
545 }
546
547 /* Was this packet marked by Explicit Congestion Notification? */
548 static int sctp_v4_is_ce(const struct sk_buff *skb)
549 {
550         return INET_ECN_is_ce(skb->nh.iph->tos);
551 }
552
553 /* Create and initialize a new sk for the socket returned by accept(). */
554 struct sock *sctp_v4_create_accept_sk(struct sock *sk,
555                                       struct sctp_association *asoc)
556 {
557         struct sock *newsk;
558         struct inet_opt *inet = inet_sk(sk);
559         struct inet_opt *newinet;
560
561         newsk = sk_alloc(PF_INET, GFP_KERNEL, sizeof(struct sctp_sock),
562                          sk->sk_slab);
563         if (!newsk)
564                 goto out;
565
566         sock_init_data(NULL, newsk);
567         sk_set_owner(newsk, THIS_MODULE);
568
569         newsk->sk_type = SOCK_STREAM;
570
571         newsk->sk_prot = sk->sk_prot;
572         newsk->sk_no_check = sk->sk_no_check;
573         newsk->sk_reuse = sk->sk_reuse;
574         newsk->sk_shutdown = sk->sk_shutdown;
575
576         newsk->sk_destruct = inet_sock_destruct;
577         newsk->sk_zapped = 0;
578         newsk->sk_family = PF_INET;
579         newsk->sk_protocol = IPPROTO_SCTP;
580         newsk->sk_backlog_rcv = sk->sk_prot->backlog_rcv;
581
582         newinet = inet_sk(newsk);
583
584         /* Initialize sk's sport, dport, rcv_saddr and daddr for
585          * getsockname() and getpeername()
586          */
587         newinet->sport = inet->sport;
588         newinet->saddr = inet->saddr;
589         newinet->rcv_saddr = inet->rcv_saddr;
590         newinet->dport = htons(asoc->peer.port);
591         newinet->daddr = asoc->peer.primary_addr.v4.sin_addr.s_addr;
592         newinet->pmtudisc = inet->pmtudisc;
593         newinet->id = 0;
594
595         newinet->uc_ttl = -1;
596         newinet->mc_loop = 1;
597         newinet->mc_ttl = 1;
598         newinet->mc_index = 0;
599         newinet->mc_list = NULL;
600
601 #ifdef INET_REFCNT_DEBUG
602         atomic_inc(&inet_sock_nr);
603 #endif
604
605         if (newsk->sk_prot->init(newsk)) {
606                 inet_sock_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_opt *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 static 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 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_opt *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_opt *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_opt *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_opt *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_opt *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(SctpOutSCTPPacks);
812         return ip_queue_xmit(skb, ipfragok);
813 }
814
815 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 struct notifier_block sctp_inetaddr_notifier = {
832         .notifier_call = sctp_inetaddr_event,
833 };
834
835 /* Socket operations.  */
836 struct proto_ops inet_seqpacket_ops = {
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  = inet_setsockopt,   /* IP_SOL IP_OPTION is a problem. */
850         .getsockopt  = inet_getsockopt,
851         .sendmsg     = inet_sendmsg,
852         .recvmsg     = inet_recvmsg,
853         .mmap        = sock_no_mmap,
854         .sendpage    = sock_no_sendpage,
855 };
856
857 /* Registration with AF_INET family.  */
858 static struct inet_protosw sctp_seqpacket_protosw = {
859         .type       = SOCK_SEQPACKET,
860         .protocol   = IPPROTO_SCTP,
861         .prot       = &sctp_prot,
862         .ops        = &inet_seqpacket_ops,
863         .capability = -1,
864         .no_check   = 0,
865         .flags      = SCTP_PROTOSW_FLAG
866 };
867 static struct inet_protosw sctp_stream_protosw = {
868         .type       = SOCK_STREAM,
869         .protocol   = IPPROTO_SCTP,
870         .prot       = &sctp_prot,
871         .ops        = &inet_seqpacket_ops,
872         .capability = -1,
873         .no_check   = 0,
874         .flags      = SCTP_PROTOSW_FLAG
875 };
876
877 /* Register with IP layer.  */
878 static struct inet_protocol sctp_protocol = {
879         .handler     = sctp_rcv,
880         .err_handler = sctp_v4_err,
881         .no_policy   = 1,
882 };
883
884 /* IPv4 address related functions.  */
885 struct sctp_af sctp_ipv4_specific = {
886         .sctp_xmit      = sctp_v4_xmit,
887         .setsockopt     = ip_setsockopt,
888         .getsockopt     = ip_getsockopt,
889         .get_dst        = sctp_v4_get_dst,
890         .get_saddr      = sctp_v4_get_saddr,
891         .copy_addrlist  = sctp_v4_copy_addrlist,
892         .from_skb       = sctp_v4_from_skb,
893         .from_sk        = sctp_v4_from_sk,
894         .to_sk_saddr    = sctp_v4_to_sk_saddr,
895         .to_sk_daddr    = sctp_v4_to_sk_daddr,
896         .from_addr_param= sctp_v4_from_addr_param,
897         .to_addr_param  = sctp_v4_to_addr_param,        
898         .dst_saddr      = sctp_v4_dst_saddr,
899         .cmp_addr       = sctp_v4_cmp_addr,
900         .addr_valid     = sctp_v4_addr_valid,
901         .inaddr_any     = sctp_v4_inaddr_any,
902         .is_any         = sctp_v4_is_any,
903         .available      = sctp_v4_available,
904         .scope          = sctp_v4_scope,
905         .skb_iif        = sctp_v4_skb_iif,
906         .is_ce          = sctp_v4_is_ce,
907         .seq_dump_addr  = sctp_v4_seq_dump_addr,
908         .net_header_len = sizeof(struct iphdr),
909         .sockaddr_len   = sizeof(struct sockaddr_in),
910         .sa_family      = AF_INET,
911 };
912
913 struct sctp_pf *sctp_get_pf_specific(sa_family_t family) {
914
915         switch (family) {
916         case PF_INET:
917                 return sctp_pf_inet_specific;
918         case PF_INET6:
919                 return sctp_pf_inet6_specific;
920         default:
921                 return NULL;
922         }
923 }
924
925 /* Register the PF specific function table.  */
926 int sctp_register_pf(struct sctp_pf *pf, sa_family_t family)
927 {
928         switch (family) {
929         case PF_INET:
930                 if (sctp_pf_inet_specific)
931                         return 0;
932                 sctp_pf_inet_specific = pf;
933                 break;
934         case PF_INET6:
935                 if (sctp_pf_inet6_specific)
936                         return 0;
937                 sctp_pf_inet6_specific = pf;
938                 break;
939         default:
940                 return 0;
941         }
942         return 1;
943 }
944
945 static int __init init_sctp_mibs(void)
946 {
947         sctp_statistics[0] = alloc_percpu(struct sctp_mib);
948         if (!sctp_statistics[0])
949                 return -ENOMEM;
950         sctp_statistics[1] = alloc_percpu(struct sctp_mib);
951         if (!sctp_statistics[1]) {
952                 free_percpu(sctp_statistics[0]);
953                 return -ENOMEM;
954         }
955         return 0;
956
957 }
958
959 static void cleanup_sctp_mibs(void)
960 {
961         free_percpu(sctp_statistics[0]);
962         free_percpu(sctp_statistics[1]);
963 }
964
965 /* Initialize the universe into something sensible.  */
966 __init int sctp_init(void)
967 {
968         int i;
969         int status = 0;
970         unsigned long goal;
971         int order;
972
973         /* SCTP_DEBUG sanity check. */
974         if (!sctp_sanity_check())
975                 return -EINVAL;
976
977         /* Add SCTP to inet_protos hash table.  */
978         if (inet_add_protocol(&sctp_protocol, IPPROTO_SCTP) < 0)
979                 return -EAGAIN;
980
981         /* Add SCTP(TCP and UDP style) to inetsw linked list.  */
982         inet_register_protosw(&sctp_seqpacket_protosw);
983         inet_register_protosw(&sctp_stream_protosw);
984
985         /* Allocate a cache pools. */
986         sctp_bucket_cachep = kmem_cache_create("sctp_bind_bucket",
987                                                sizeof(struct sctp_bind_bucket),
988                                                0, SLAB_HWCACHE_ALIGN,
989                                                NULL, NULL);
990
991         if (!sctp_bucket_cachep)
992                 goto err_bucket_cachep;
993
994         sctp_chunk_cachep = kmem_cache_create("sctp_chunk",
995                                                sizeof(struct sctp_chunk),
996                                                0, SLAB_HWCACHE_ALIGN,
997                                                NULL, NULL);
998         if (!sctp_chunk_cachep)
999                 goto err_chunk_cachep;
1000
1001         /* Allocate and initialise sctp mibs.  */
1002         status = init_sctp_mibs();
1003         if (status)
1004                 goto err_init_mibs;
1005
1006         /* Initialize proc fs directory.  */
1007         status = sctp_proc_init();
1008         if (status)
1009                 goto err_init_proc;
1010
1011         /* Initialize object count debugging.  */
1012         sctp_dbg_objcnt_init();
1013
1014         /* Initialize the SCTP specific PF functions. */
1015         sctp_register_pf(&sctp_pf_inet, PF_INET);
1016         /*
1017          * 14. Suggested SCTP Protocol Parameter Values
1018          */
1019         /* The following protocol parameters are RECOMMENDED:  */
1020         /* RTO.Initial              - 3  seconds */
1021         sctp_rto_initial                = SCTP_RTO_INITIAL;
1022         /* RTO.Min                  - 1  second */
1023         sctp_rto_min                    = SCTP_RTO_MIN;
1024         /* RTO.Max                 -  60 seconds */
1025         sctp_rto_max                    = SCTP_RTO_MAX;
1026         /* RTO.Alpha                - 1/8 */
1027         sctp_rto_alpha                  = SCTP_RTO_ALPHA;
1028         /* RTO.Beta                 - 1/4 */
1029         sctp_rto_beta                   = SCTP_RTO_BETA;
1030
1031         /* Valid.Cookie.Life        - 60  seconds */
1032         sctp_valid_cookie_life          = 60 * HZ;
1033
1034         /* Whether Cookie Preservative is enabled(1) or not(0) */
1035         sctp_cookie_preserve_enable     = 1;
1036
1037         /* Max.Burst                - 4 */
1038         sctp_max_burst                  = SCTP_MAX_BURST;
1039
1040         /* Association.Max.Retrans  - 10 attempts
1041          * Path.Max.Retrans         - 5  attempts (per destination address)
1042          * Max.Init.Retransmits     - 8  attempts
1043          */
1044         sctp_max_retrans_association    = 10;
1045         sctp_max_retrans_path           = 5;
1046         sctp_max_retrans_init           = 8;
1047
1048         /* HB.interval              - 30 seconds */
1049         sctp_hb_interval                = 30 * HZ;
1050
1051         /* Implementation specific variables. */
1052
1053         /* Initialize default stream count setup information. */
1054         sctp_max_instreams              = SCTP_DEFAULT_INSTREAMS;
1055         sctp_max_outstreams             = SCTP_DEFAULT_OUTSTREAMS;
1056
1057         /* Initialize handle used for association ids. */
1058         idr_init(&sctp_assocs_id);
1059
1060         /* Size and allocate the association hash table.
1061          * The methodology is similar to that of the tcp hash tables.
1062          */
1063         if (num_physpages >= (128 * 1024))
1064                 goal = num_physpages >> (22 - PAGE_SHIFT);
1065         else
1066                 goal = num_physpages >> (24 - PAGE_SHIFT);
1067
1068         for (order = 0; (1UL << order) < goal; order++)
1069                 ;
1070
1071         do {
1072                 sctp_assoc_hashsize = (1UL << order) * PAGE_SIZE /
1073                                         sizeof(struct sctp_hashbucket);
1074                 if ((sctp_assoc_hashsize > (64 * 1024)) && order > 0)
1075                         continue;
1076                 sctp_assoc_hashtable = (struct sctp_hashbucket *)
1077                                         __get_free_pages(GFP_ATOMIC, order);
1078         } while (!sctp_assoc_hashtable && --order > 0);
1079         if (!sctp_assoc_hashtable) {
1080                 printk(KERN_ERR "SCTP: Failed association hash alloc.\n");
1081                 status = -ENOMEM;
1082                 goto err_ahash_alloc;
1083         }
1084         for (i = 0; i < sctp_assoc_hashsize; i++) {
1085                 sctp_assoc_hashtable[i].lock = RW_LOCK_UNLOCKED;
1086                 sctp_assoc_hashtable[i].chain = NULL;
1087         }
1088
1089         /* Allocate and initialize the endpoint hash table.  */
1090         sctp_ep_hashsize = 64;
1091         sctp_ep_hashtable = (struct sctp_hashbucket *)
1092                 kmalloc(64 * sizeof(struct sctp_hashbucket), GFP_KERNEL);
1093         if (!sctp_ep_hashtable) {
1094                 printk(KERN_ERR "SCTP: Failed endpoint_hash alloc.\n");
1095                 status = -ENOMEM;
1096                 goto err_ehash_alloc;
1097         }
1098         for (i = 0; i < sctp_ep_hashsize; i++) {
1099                 sctp_ep_hashtable[i].lock = RW_LOCK_UNLOCKED;
1100                 sctp_ep_hashtable[i].chain = NULL;
1101         }
1102
1103         /* Allocate and initialize the SCTP port hash table.  */
1104         do {
1105                 sctp_port_hashsize = (1UL << order) * PAGE_SIZE /
1106                                         sizeof(struct sctp_bind_hashbucket);
1107                 if ((sctp_port_hashsize > (64 * 1024)) && order > 0)
1108                         continue;
1109                 sctp_port_hashtable = (struct sctp_bind_hashbucket *)
1110                                         __get_free_pages(GFP_ATOMIC, order);
1111         } while (!sctp_port_hashtable && --order > 0);
1112         if (!sctp_port_hashtable) {
1113                 printk(KERN_ERR "SCTP: Failed bind hash alloc.");
1114                 status = -ENOMEM;
1115                 goto err_bhash_alloc;
1116         }
1117         for (i = 0; i < sctp_port_hashsize; i++) {
1118                 sctp_port_hashtable[i].lock = SPIN_LOCK_UNLOCKED;
1119                 sctp_port_hashtable[i].chain = NULL;
1120         }
1121
1122         sctp_port_alloc_lock = SPIN_LOCK_UNLOCKED;
1123         sctp_port_rover = sysctl_local_port_range[0] - 1;
1124
1125         printk(KERN_INFO "SCTP: Hash tables configured "
1126                          "(established %d bind %d)\n",
1127                 sctp_assoc_hashsize, sctp_port_hashsize);
1128
1129         /* Disable ADDIP by default. */
1130         sctp_addip_enable = 0;
1131
1132         /* Enable PR-SCTP by default. */
1133         sctp_prsctp_enable = 1;
1134
1135         sctp_sysctl_register();
1136
1137         INIT_LIST_HEAD(&sctp_address_families);
1138         sctp_register_af(&sctp_ipv4_specific);
1139
1140         status = sctp_v6_init();
1141         if (status)
1142                 goto err_v6_init;
1143
1144         /* Initialize the control inode/socket for handling OOTB packets.  */
1145         if ((status = sctp_ctl_sock_init())) {
1146                 printk (KERN_ERR
1147                         "SCTP: Failed to initialize the SCTP control sock.\n");
1148                 goto err_ctl_sock_init;
1149         }
1150
1151         /* Initialize the local address list. */
1152         INIT_LIST_HEAD(&sctp_local_addr_list);
1153         sctp_local_addr_lock = SPIN_LOCK_UNLOCKED;
1154
1155         /* Register notifier for inet address additions/deletions. */
1156         register_inetaddr_notifier(&sctp_inetaddr_notifier);
1157
1158         sctp_get_local_addr_list();
1159
1160         __unsafe(THIS_MODULE);
1161         return 0;
1162
1163 err_ctl_sock_init:
1164         sctp_v6_exit();
1165 err_v6_init:
1166         sctp_sysctl_unregister();
1167         list_del(&sctp_ipv4_specific.list);
1168         free_pages((unsigned long)sctp_port_hashtable,
1169                    get_order(sctp_port_hashsize *
1170                              sizeof(struct sctp_bind_hashbucket)));
1171 err_bhash_alloc:
1172         kfree(sctp_ep_hashtable);
1173 err_ehash_alloc:
1174         free_pages((unsigned long)sctp_assoc_hashtable,
1175                    get_order(sctp_assoc_hashsize *
1176                              sizeof(struct sctp_hashbucket)));
1177 err_ahash_alloc:
1178         sctp_dbg_objcnt_exit();
1179 err_init_proc:
1180         sctp_proc_exit();
1181         cleanup_sctp_mibs();
1182 err_init_mibs:
1183         kmem_cache_destroy(sctp_chunk_cachep);
1184 err_chunk_cachep:
1185         kmem_cache_destroy(sctp_bucket_cachep);
1186 err_bucket_cachep:
1187         inet_del_protocol(&sctp_protocol, IPPROTO_SCTP);
1188         inet_unregister_protosw(&sctp_seqpacket_protosw);
1189         inet_unregister_protosw(&sctp_stream_protosw);
1190         return status;
1191 }
1192
1193 /* Exit handler for the SCTP protocol.  */
1194 __exit void sctp_exit(void)
1195 {
1196         /* BUG.  This should probably do something useful like clean
1197          * up all the remaining associations and all that memory.
1198          */
1199
1200         /* Unregister notifier for inet address additions/deletions. */
1201         unregister_inetaddr_notifier(&sctp_inetaddr_notifier);
1202
1203         /* Free the local address list.  */
1204         sctp_free_local_addr_list();
1205
1206         /* Free the control endpoint.  */
1207         sock_release(sctp_ctl_socket);
1208
1209         sctp_v6_exit();
1210         sctp_sysctl_unregister();
1211         list_del(&sctp_ipv4_specific.list);
1212
1213         free_pages((unsigned long)sctp_assoc_hashtable,
1214                    get_order(sctp_assoc_hashsize *
1215                              sizeof(struct sctp_hashbucket)));
1216         kfree(sctp_ep_hashtable);
1217         free_pages((unsigned long)sctp_port_hashtable,
1218                    get_order(sctp_port_hashsize *
1219                              sizeof(struct sctp_bind_hashbucket)));
1220
1221         kmem_cache_destroy(sctp_chunk_cachep);
1222         kmem_cache_destroy(sctp_bucket_cachep);
1223
1224         sctp_dbg_objcnt_exit();
1225         sctp_proc_exit();
1226         cleanup_sctp_mibs();
1227
1228         inet_del_protocol(&sctp_protocol, IPPROTO_SCTP);
1229         inet_unregister_protosw(&sctp_seqpacket_protosw);
1230         inet_unregister_protosw(&sctp_stream_protosw);
1231 }
1232
1233 module_init(sctp_init);
1234 module_exit(sctp_exit);
1235
1236 MODULE_AUTHOR("Linux Kernel SCTP developers <lksctp-developers@lists.sourceforge.net>");
1237 MODULE_DESCRIPTION("Support for the SCTP protocol (RFC2960)");
1238 MODULE_LICENSE("GPL");