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