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