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. */
64struct sctp_globals sctp_globals __read_mostly;
65struct proc_dir_entry	*proc_net_sctp;
66DEFINE_SNMP_STAT(struct sctp_mib, sctp_statistics) __read_mostly;
67
68struct idr sctp_assocs_id;
69DEFINE_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 */
75static struct socket *sctp_ctl_socket;
76
77static struct sctp_pf *sctp_pf_inet6_specific;
78static struct sctp_pf *sctp_pf_inet_specific;
79static struct sctp_af *sctp_af_v4_specific;
80static struct sctp_af *sctp_af_v6_specific;
81
82struct kmem_cache *sctp_chunk_cachep __read_mostly;
83struct kmem_cache *sctp_bucket_cachep __read_mostly;
84
85/* Return the address of the control sock. */
86struct 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. */
92static __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
113out_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 */
121static 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 */
136static 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 */
166static 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_each_netdev(dev) {
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.  */
183static 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'.  */
196int 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
223end_copy:
224	return error;
225}
226
227/* Initialize a sctp_addr from in incoming skb.  */
228static 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. */
250static 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. */
258static 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. */
264static 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. */
270static 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 */
282static 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. */
295static 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. */
305static 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. */
319static 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? */
327static 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 */
339static 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?   */
355static 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 */
383static 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 */
412static 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
492out_unlock:
493	sctp_read_unlock(addr_lock);
494out:
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 */
507static 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? */
525static 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? */
531static 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(). */
537static 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
587out:
588	return newsk;
589}
590
591/* Map address, empty for v4 family */
592static 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. */
598static 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.  */
604static 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 */
641static 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. */
665int 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 */
690struct 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. */
703static 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. */
714static 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. */
732static 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? */
745static 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. */
752static 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 */
771static 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 */
779static 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 */
787static 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. */
795static 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
808static struct sctp_af sctp_ipv4_specific;
809
810static 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.  */
824static struct notifier_block sctp_inetaddr_notifier = {
825	.notifier_call = sctp_inetaddr_event,
826};
827
828/* Socket operations.  */
829static 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.  */
855static 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};
864static 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.  */
875static 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.  */
882static 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
914struct 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.  */
927int 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
946static 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
960static 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.  */
967SCTP_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	/* Allocate bind_bucket and chunk caches. */
979	status = -ENOBUFS;
980	sctp_bucket_cachep = kmem_cache_create("sctp_bind_bucket",
981					       sizeof(struct sctp_bind_bucket),
982					       0, SLAB_HWCACHE_ALIGN,
983					       NULL, NULL);
984	if (!sctp_bucket_cachep)
985		goto out;
986
987	sctp_chunk_cachep = kmem_cache_create("sctp_chunk",
988					       sizeof(struct sctp_chunk),
989					       0, SLAB_HWCACHE_ALIGN,
990					       NULL, NULL);
991	if (!sctp_chunk_cachep)
992		goto err_chunk_cachep;
993
994	/* Allocate and initialise sctp mibs.  */
995	status = init_sctp_mibs();
996	if (status)
997		goto err_init_mibs;
998
999	/* Initialize proc fs directory.  */
1000	status = sctp_proc_init();
1001	if (status)
1002		goto err_init_proc;
1003
1004	/* Initialize object count debugging.  */
1005	sctp_dbg_objcnt_init();
1006
1007	/* Initialize the SCTP specific PF functions. */
1008	sctp_register_pf(&sctp_pf_inet, PF_INET);
1009	/*
1010	 * 14. Suggested SCTP Protocol Parameter Values
1011	 */
1012	/* The following protocol parameters are RECOMMENDED:  */
1013	/* RTO.Initial              - 3  seconds */
1014	sctp_rto_initial		= SCTP_RTO_INITIAL;
1015	/* RTO.Min                  - 1  second */
1016	sctp_rto_min	 		= SCTP_RTO_MIN;
1017	/* RTO.Max                 -  60 seconds */
1018	sctp_rto_max 			= SCTP_RTO_MAX;
1019	/* RTO.Alpha                - 1/8 */
1020	sctp_rto_alpha	        	= SCTP_RTO_ALPHA;
1021	/* RTO.Beta                 - 1/4 */
1022	sctp_rto_beta			= SCTP_RTO_BETA;
1023
1024	/* Valid.Cookie.Life        - 60  seconds */
1025	sctp_valid_cookie_life		= SCTP_DEFAULT_COOKIE_LIFE;
1026
1027	/* Whether Cookie Preservative is enabled(1) or not(0) */
1028	sctp_cookie_preserve_enable 	= 1;
1029
1030	/* Max.Burst		    - 4 */
1031	sctp_max_burst 			= SCTP_DEFAULT_MAX_BURST;
1032
1033	/* Association.Max.Retrans  - 10 attempts
1034	 * Path.Max.Retrans         - 5  attempts (per destination address)
1035	 * Max.Init.Retransmits     - 8  attempts
1036	 */
1037	sctp_max_retrans_association 	= 10;
1038	sctp_max_retrans_path		= 5;
1039	sctp_max_retrans_init		= 8;
1040
1041	/* Sendbuffer growth	    - do per-socket accounting */
1042	sctp_sndbuf_policy		= 0;
1043
1044	/* Rcvbuffer growth	    - do per-socket accounting */
1045	sctp_rcvbuf_policy		= 0;
1046
1047	/* HB.interval              - 30 seconds */
1048	sctp_hb_interval		= SCTP_DEFAULT_TIMEOUT_HEARTBEAT;
1049
1050	/* delayed SACK timeout */
1051	sctp_sack_timeout		= SCTP_DEFAULT_TIMEOUT_SACK;
1052
1053	/* Implementation specific variables. */
1054
1055	/* Initialize default stream count setup information. */
1056	sctp_max_instreams    		= SCTP_DEFAULT_INSTREAMS;
1057	sctp_max_outstreams   		= SCTP_DEFAULT_OUTSTREAMS;
1058
1059	/* Initialize handle used for association ids. */
1060	idr_init(&sctp_assocs_id);
1061
1062	/* Size and allocate the association hash table.
1063	 * The methodology is similar to that of the tcp hash tables.
1064	 */
1065	if (num_physpages >= (128 * 1024))
1066		goal = num_physpages >> (22 - PAGE_SHIFT);
1067	else
1068		goal = num_physpages >> (24 - PAGE_SHIFT);
1069
1070	for (order = 0; (1UL << order) < goal; order++)
1071		;
1072
1073	do {
1074		sctp_assoc_hashsize = (1UL << order) * PAGE_SIZE /
1075					sizeof(struct sctp_hashbucket);
1076		if ((sctp_assoc_hashsize > (64 * 1024)) && order > 0)
1077			continue;
1078		sctp_assoc_hashtable = (struct sctp_hashbucket *)
1079					__get_free_pages(GFP_ATOMIC, order);
1080	} while (!sctp_assoc_hashtable && --order > 0);
1081	if (!sctp_assoc_hashtable) {
1082		printk(KERN_ERR "SCTP: Failed association hash alloc.\n");
1083		status = -ENOMEM;
1084		goto err_ahash_alloc;
1085	}
1086	for (i = 0; i < sctp_assoc_hashsize; i++) {
1087		rwlock_init(&sctp_assoc_hashtable[i].lock);
1088		sctp_assoc_hashtable[i].chain = NULL;
1089	}
1090
1091	/* Allocate and initialize the endpoint hash table.  */
1092	sctp_ep_hashsize = 64;
1093	sctp_ep_hashtable = (struct sctp_hashbucket *)
1094		kmalloc(64 * sizeof(struct sctp_hashbucket), GFP_KERNEL);
1095	if (!sctp_ep_hashtable) {
1096		printk(KERN_ERR "SCTP: Failed endpoint_hash alloc.\n");
1097		status = -ENOMEM;
1098		goto err_ehash_alloc;
1099	}
1100	for (i = 0; i < sctp_ep_hashsize; i++) {
1101		rwlock_init(&sctp_ep_hashtable[i].lock);
1102		sctp_ep_hashtable[i].chain = NULL;
1103	}
1104
1105	/* Allocate and initialize the SCTP port hash table.  */
1106	do {
1107		sctp_port_hashsize = (1UL << order) * PAGE_SIZE /
1108					sizeof(struct sctp_bind_hashbucket);
1109		if ((sctp_port_hashsize > (64 * 1024)) && order > 0)
1110			continue;
1111		sctp_port_hashtable = (struct sctp_bind_hashbucket *)
1112					__get_free_pages(GFP_ATOMIC, order);
1113	} while (!sctp_port_hashtable && --order > 0);
1114	if (!sctp_port_hashtable) {
1115		printk(KERN_ERR "SCTP: Failed bind hash alloc.");
1116		status = -ENOMEM;
1117		goto err_bhash_alloc;
1118	}
1119	for (i = 0; i < sctp_port_hashsize; i++) {
1120		spin_lock_init(&sctp_port_hashtable[i].lock);
1121		sctp_port_hashtable[i].chain = NULL;
1122	}
1123
1124	spin_lock_init(&sctp_port_alloc_lock);
1125	sctp_port_rover = sysctl_local_port_range[0] - 1;
1126
1127	printk(KERN_INFO "SCTP: Hash tables configured "
1128			 "(established %d bind %d)\n",
1129		sctp_assoc_hashsize, sctp_port_hashsize);
1130
1131	/* Disable ADDIP by default. */
1132	sctp_addip_enable = 0;
1133
1134	/* Enable PR-SCTP by default. */
1135	sctp_prsctp_enable = 1;
1136
1137	sctp_sysctl_register();
1138
1139	INIT_LIST_HEAD(&sctp_address_families);
1140	sctp_register_af(&sctp_ipv4_specific);
1141
1142	status = proto_register(&sctp_prot, 1);
1143	if (status)
1144		goto err_proto_register;
1145
1146	/* Register SCTP(UDP and TCP style) with socket layer.  */
1147	inet_register_protosw(&sctp_seqpacket_protosw);
1148	inet_register_protosw(&sctp_stream_protosw);
1149
1150	status = sctp_v6_init();
1151	if (status)
1152		goto err_v6_init;
1153
1154	/* Initialize the control inode/socket for handling OOTB packets.  */
1155	if ((status = sctp_ctl_sock_init())) {
1156		printk (KERN_ERR
1157			"SCTP: Failed to initialize the SCTP control sock.\n");
1158		goto err_ctl_sock_init;
1159	}
1160
1161	/* Initialize the local address list. */
1162	INIT_LIST_HEAD(&sctp_local_addr_list);
1163	sctp_get_local_addr_list();
1164
1165	/* Register notifier for inet address additions/deletions. */
1166	register_inetaddr_notifier(&sctp_inetaddr_notifier);
1167
1168	/* Register SCTP with inet layer.  */
1169	if (inet_add_protocol(&sctp_protocol, IPPROTO_SCTP) < 0) {
1170		status = -EAGAIN;
1171		goto err_add_protocol;
1172	}
1173
1174	/* Register SCTP with inet6 layer.  */
1175	status = sctp_v6_add_protocol();
1176	if (status)
1177		goto err_v6_add_protocol;
1178
1179	__unsafe(THIS_MODULE);
1180	status = 0;
1181out:
1182	return status;
1183err_v6_add_protocol:
1184	inet_del_protocol(&sctp_protocol, IPPROTO_SCTP);
1185	unregister_inetaddr_notifier(&sctp_inetaddr_notifier);
1186err_add_protocol:
1187	sctp_free_local_addr_list();
1188	sock_release(sctp_ctl_socket);
1189err_ctl_sock_init:
1190	sctp_v6_exit();
1191err_v6_init:
1192	inet_unregister_protosw(&sctp_stream_protosw);
1193	inet_unregister_protosw(&sctp_seqpacket_protosw);
1194	proto_unregister(&sctp_prot);
1195err_proto_register:
1196	sctp_sysctl_unregister();
1197	list_del(&sctp_ipv4_specific.list);
1198	free_pages((unsigned long)sctp_port_hashtable,
1199		   get_order(sctp_port_hashsize *
1200			     sizeof(struct sctp_bind_hashbucket)));
1201err_bhash_alloc:
1202	kfree(sctp_ep_hashtable);
1203err_ehash_alloc:
1204	free_pages((unsigned long)sctp_assoc_hashtable,
1205		   get_order(sctp_assoc_hashsize *
1206			     sizeof(struct sctp_hashbucket)));
1207err_ahash_alloc:
1208	sctp_dbg_objcnt_exit();
1209	sctp_proc_exit();
1210err_init_proc:
1211	cleanup_sctp_mibs();
1212err_init_mibs:
1213	kmem_cache_destroy(sctp_chunk_cachep);
1214err_chunk_cachep:
1215	kmem_cache_destroy(sctp_bucket_cachep);
1216	goto out;
1217}
1218
1219/* Exit handler for the SCTP protocol.  */
1220SCTP_STATIC __exit void sctp_exit(void)
1221{
1222	/* BUG.  This should probably do something useful like clean
1223	 * up all the remaining associations and all that memory.
1224	 */
1225
1226	/* Unregister with inet6/inet layers. */
1227	sctp_v6_del_protocol();
1228	inet_del_protocol(&sctp_protocol, IPPROTO_SCTP);
1229
1230	/* Free the local address list.  */
1231	sctp_free_local_addr_list();
1232
1233	/* Free the control endpoint.  */
1234	sock_release(sctp_ctl_socket);
1235
1236	/* Cleanup v6 initializations. */
1237	sctp_v6_exit();
1238
1239	/* Unregister with socket layer. */
1240	inet_unregister_protosw(&sctp_stream_protosw);
1241	inet_unregister_protosw(&sctp_seqpacket_protosw);
1242
1243	/* Unregister notifier for inet address additions/deletions. */
1244	unregister_inetaddr_notifier(&sctp_inetaddr_notifier);
1245
1246	sctp_sysctl_unregister();
1247	list_del(&sctp_ipv4_specific.list);
1248
1249	free_pages((unsigned long)sctp_assoc_hashtable,
1250		   get_order(sctp_assoc_hashsize *
1251			     sizeof(struct sctp_hashbucket)));
1252	kfree(sctp_ep_hashtable);
1253	free_pages((unsigned long)sctp_port_hashtable,
1254		   get_order(sctp_port_hashsize *
1255			     sizeof(struct sctp_bind_hashbucket)));
1256
1257	sctp_dbg_objcnt_exit();
1258	sctp_proc_exit();
1259	cleanup_sctp_mibs();
1260
1261	kmem_cache_destroy(sctp_chunk_cachep);
1262	kmem_cache_destroy(sctp_bucket_cachep);
1263
1264	proto_unregister(&sctp_prot);
1265}
1266
1267module_init(sctp_init);
1268module_exit(sctp_exit);
1269
1270/*
1271 * __stringify doesn't likes enums, so use IPPROTO_SCTP value (132) directly.
1272 */
1273MODULE_ALIAS("net-pf-" __stringify(PF_INET) "-proto-132");
1274MODULE_ALIAS("net-pf-" __stringify(PF_INET6) "-proto-132");
1275MODULE_AUTHOR("Linux Kernel SCTP developers <lksctp-developers@lists.sourceforge.net>");
1276MODULE_DESCRIPTION("Support for the SCTP protocol (RFC2960)");
1277MODULE_LICENSE("GPL");
1278