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