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