1/* SCTP kernel implementation
2 * (C) Copyright IBM Corp. 2001, 2003
3 * Copyright (c) Cisco 1999,2000
4 * Copyright (c) Motorola 1999,2000,2001
5 * Copyright (c) La Monte H.P. Yarroll 2001
6 *
7 * This file is part of the SCTP kernel implementation.
8 *
9 * A collection class to handle the storage of transport addresses.
10 *
11 * This SCTP implementation is free software;
12 * you can redistribute it and/or modify it under the terms of
13 * the GNU General Public License as published by
14 * the Free Software Foundation; either version 2, or (at your option)
15 * any later version.
16 *
17 * This SCTP implementation is distributed in the hope that it
18 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
19 *                 ************************
20 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
21 * See the GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with GNU CC; see the file COPYING.  If not, see
25 * <http://www.gnu.org/licenses/>.
26 *
27 * Please send any bug reports or fixes you make to the
28 * email address(es):
29 *    lksctp developers <linux-sctp@vger.kernel.org>
30 *
31 * Written or modified by:
32 *    La Monte H.P. Yarroll <piggy@acm.org>
33 *    Karl Knutson          <karl@athena.chicago.il.us>
34 *    Jon Grimm             <jgrimm@us.ibm.com>
35 *    Daisy Chang           <daisyc@us.ibm.com>
36 */
37
38#include <linux/types.h>
39#include <linux/slab.h>
40#include <linux/in.h>
41#include <net/sock.h>
42#include <net/ipv6.h>
43#include <net/if_inet6.h>
44#include <net/sctp/sctp.h>
45#include <net/sctp/sm.h>
46
47/* Forward declarations for internal helpers. */
48static int sctp_copy_one_addr(struct net *, struct sctp_bind_addr *,
49			      union sctp_addr *, sctp_scope_t scope, gfp_t gfp,
50			      int flags);
51static void sctp_bind_addr_clean(struct sctp_bind_addr *);
52
53/* First Level Abstractions. */
54
55/* Copy 'src' to 'dest' taking 'scope' into account.  Omit addresses
56 * in 'src' which have a broader scope than 'scope'.
57 */
58int sctp_bind_addr_copy(struct net *net, struct sctp_bind_addr *dest,
59			const struct sctp_bind_addr *src,
60			sctp_scope_t scope, gfp_t gfp,
61			int flags)
62{
63	struct sctp_sockaddr_entry *addr;
64	int error = 0;
65
66	/* All addresses share the same port.  */
67	dest->port = src->port;
68
69	/* Extract the addresses which are relevant for this scope.  */
70	list_for_each_entry(addr, &src->address_list, list) {
71		error = sctp_copy_one_addr(net, dest, &addr->a, scope,
72					   gfp, flags);
73		if (error < 0)
74			goto out;
75	}
76
77	/* If there are no addresses matching the scope and
78	 * this is global scope, try to get a link scope address, with
79	 * the assumption that we must be sitting behind a NAT.
80	 */
81	if (list_empty(&dest->address_list) && (SCTP_SCOPE_GLOBAL == scope)) {
82		list_for_each_entry(addr, &src->address_list, list) {
83			error = sctp_copy_one_addr(net, dest, &addr->a,
84						   SCTP_SCOPE_LINK, gfp,
85						   flags);
86			if (error < 0)
87				goto out;
88		}
89	}
90
91out:
92	if (error)
93		sctp_bind_addr_clean(dest);
94
95	return error;
96}
97
98/* Exactly duplicate the address lists.  This is necessary when doing
99 * peer-offs and accepts.  We don't want to put all the current system
100 * addresses into the endpoint.  That's useless.  But we do want duplicat
101 * the list of bound addresses that the older endpoint used.
102 */
103int sctp_bind_addr_dup(struct sctp_bind_addr *dest,
104			const struct sctp_bind_addr *src,
105			gfp_t gfp)
106{
107	struct sctp_sockaddr_entry *addr;
108	int error = 0;
109
110	/* All addresses share the same port.  */
111	dest->port = src->port;
112
113	list_for_each_entry(addr, &src->address_list, list) {
114		error = sctp_add_bind_addr(dest, &addr->a, 1, gfp);
115		if (error < 0)
116			break;
117	}
118
119	return error;
120}
121
122/* Initialize the SCTP_bind_addr structure for either an endpoint or
123 * an association.
124 */
125void sctp_bind_addr_init(struct sctp_bind_addr *bp, __u16 port)
126{
127	INIT_LIST_HEAD(&bp->address_list);
128	bp->port = port;
129}
130
131/* Dispose of the address list. */
132static void sctp_bind_addr_clean(struct sctp_bind_addr *bp)
133{
134	struct sctp_sockaddr_entry *addr, *temp;
135
136	/* Empty the bind address list. */
137	list_for_each_entry_safe(addr, temp, &bp->address_list, list) {
138		list_del_rcu(&addr->list);
139		kfree_rcu(addr, rcu);
140		SCTP_DBG_OBJCNT_DEC(addr);
141	}
142}
143
144/* Dispose of an SCTP_bind_addr structure  */
145void sctp_bind_addr_free(struct sctp_bind_addr *bp)
146{
147	/* Empty the bind address list. */
148	sctp_bind_addr_clean(bp);
149}
150
151/* Add an address to the bind address list in the SCTP_bind_addr structure. */
152int sctp_add_bind_addr(struct sctp_bind_addr *bp, union sctp_addr *new,
153		       __u8 addr_state, gfp_t gfp)
154{
155	struct sctp_sockaddr_entry *addr;
156
157	/* Add the address to the bind address list.  */
158	addr = kzalloc(sizeof(*addr), gfp);
159	if (!addr)
160		return -ENOMEM;
161
162	memcpy(&addr->a, new, sizeof(*new));
163
164	/* Fix up the port if it has not yet been set.
165	 * Both v4 and v6 have the port at the same offset.
166	 */
167	if (!addr->a.v4.sin_port)
168		addr->a.v4.sin_port = htons(bp->port);
169
170	addr->state = addr_state;
171	addr->valid = 1;
172
173	INIT_LIST_HEAD(&addr->list);
174
175	/* We always hold a socket lock when calling this function,
176	 * and that acts as a writer synchronizing lock.
177	 */
178	list_add_tail_rcu(&addr->list, &bp->address_list);
179	SCTP_DBG_OBJCNT_INC(addr);
180
181	return 0;
182}
183
184/* Delete an address from the bind address list in the SCTP_bind_addr
185 * structure.
186 */
187int sctp_del_bind_addr(struct sctp_bind_addr *bp, union sctp_addr *del_addr)
188{
189	struct sctp_sockaddr_entry *addr, *temp;
190	int found = 0;
191
192	/* We hold the socket lock when calling this function,
193	 * and that acts as a writer synchronizing lock.
194	 */
195	list_for_each_entry_safe(addr, temp, &bp->address_list, list) {
196		if (sctp_cmp_addr_exact(&addr->a, del_addr)) {
197			/* Found the exact match. */
198			found = 1;
199			addr->valid = 0;
200			list_del_rcu(&addr->list);
201			break;
202		}
203	}
204
205	if (found) {
206		kfree_rcu(addr, rcu);
207		SCTP_DBG_OBJCNT_DEC(addr);
208		return 0;
209	}
210
211	return -EINVAL;
212}
213
214/* Create a network byte-order representation of all the addresses
215 * formated as SCTP parameters.
216 *
217 * The second argument is the return value for the length.
218 */
219union sctp_params sctp_bind_addrs_to_raw(const struct sctp_bind_addr *bp,
220					 int *addrs_len,
221					 gfp_t gfp)
222{
223	union sctp_params addrparms;
224	union sctp_params retval;
225	int addrparms_len;
226	union sctp_addr_param rawaddr;
227	int len;
228	struct sctp_sockaddr_entry *addr;
229	struct list_head *pos;
230	struct sctp_af *af;
231
232	addrparms_len = 0;
233	len = 0;
234
235	/* Allocate enough memory at once. */
236	list_for_each(pos, &bp->address_list) {
237		len += sizeof(union sctp_addr_param);
238	}
239
240	/* Don't even bother embedding an address if there
241	 * is only one.
242	 */
243	if (len == sizeof(union sctp_addr_param)) {
244		retval.v = NULL;
245		goto end_raw;
246	}
247
248	retval.v = kmalloc(len, gfp);
249	if (!retval.v)
250		goto end_raw;
251
252	addrparms = retval;
253
254	list_for_each_entry(addr, &bp->address_list, list) {
255		af = sctp_get_af_specific(addr->a.v4.sin_family);
256		len = af->to_addr_param(&addr->a, &rawaddr);
257		memcpy(addrparms.v, &rawaddr, len);
258		addrparms.v += len;
259		addrparms_len += len;
260	}
261
262end_raw:
263	*addrs_len = addrparms_len;
264	return retval;
265}
266
267/*
268 * Create an address list out of the raw address list format (IPv4 and IPv6
269 * address parameters).
270 */
271int sctp_raw_to_bind_addrs(struct sctp_bind_addr *bp, __u8 *raw_addr_list,
272			   int addrs_len, __u16 port, gfp_t gfp)
273{
274	union sctp_addr_param *rawaddr;
275	struct sctp_paramhdr *param;
276	union sctp_addr addr;
277	int retval = 0;
278	int len;
279	struct sctp_af *af;
280
281	/* Convert the raw address to standard address format */
282	while (addrs_len) {
283		param = (struct sctp_paramhdr *)raw_addr_list;
284		rawaddr = (union sctp_addr_param *)raw_addr_list;
285
286		af = sctp_get_af_specific(param_type2af(param->type));
287		if (unlikely(!af)) {
288			retval = -EINVAL;
289			sctp_bind_addr_clean(bp);
290			break;
291		}
292
293		af->from_addr_param(&addr, rawaddr, htons(port), 0);
294		retval = sctp_add_bind_addr(bp, &addr, SCTP_ADDR_SRC, gfp);
295		if (retval) {
296			/* Can't finish building the list, clean up. */
297			sctp_bind_addr_clean(bp);
298			break;
299		}
300
301		len = ntohs(param->length);
302		addrs_len -= len;
303		raw_addr_list += len;
304	}
305
306	return retval;
307}
308
309/********************************************************************
310 * 2nd Level Abstractions
311 ********************************************************************/
312
313/* Does this contain a specified address?  Allow wildcarding. */
314int sctp_bind_addr_match(struct sctp_bind_addr *bp,
315			 const union sctp_addr *addr,
316			 struct sctp_sock *opt)
317{
318	struct sctp_sockaddr_entry *laddr;
319	int match = 0;
320
321	rcu_read_lock();
322	list_for_each_entry_rcu(laddr, &bp->address_list, list) {
323		if (!laddr->valid)
324			continue;
325		if (opt->pf->cmp_addr(&laddr->a, addr, opt)) {
326			match = 1;
327			break;
328		}
329	}
330	rcu_read_unlock();
331
332	return match;
333}
334
335/* Does the address 'addr' conflict with any addresses in
336 * the bp.
337 */
338int sctp_bind_addr_conflict(struct sctp_bind_addr *bp,
339			    const union sctp_addr *addr,
340			    struct sctp_sock *bp_sp,
341			    struct sctp_sock *addr_sp)
342{
343	struct sctp_sockaddr_entry *laddr;
344	int conflict = 0;
345	struct sctp_sock *sp;
346
347	/* Pick the IPv6 socket as the basis of comparison
348	 * since it's usually a superset of the IPv4.
349	 * If there is no IPv6 socket, then default to bind_addr.
350	 */
351	if (sctp_opt2sk(bp_sp)->sk_family == AF_INET6)
352		sp = bp_sp;
353	else if (sctp_opt2sk(addr_sp)->sk_family == AF_INET6)
354		sp = addr_sp;
355	else
356		sp = bp_sp;
357
358	rcu_read_lock();
359	list_for_each_entry_rcu(laddr, &bp->address_list, list) {
360		if (!laddr->valid)
361			continue;
362
363		conflict = sp->pf->cmp_addr(&laddr->a, addr, sp);
364		if (conflict)
365			break;
366	}
367	rcu_read_unlock();
368
369	return conflict;
370}
371
372/* Get the state of the entry in the bind_addr_list */
373int sctp_bind_addr_state(const struct sctp_bind_addr *bp,
374			 const union sctp_addr *addr)
375{
376	struct sctp_sockaddr_entry *laddr;
377	struct sctp_af *af;
378	int state = -1;
379
380	af = sctp_get_af_specific(addr->sa.sa_family);
381	if (unlikely(!af))
382		return state;
383
384	rcu_read_lock();
385	list_for_each_entry_rcu(laddr, &bp->address_list, list) {
386		if (!laddr->valid)
387			continue;
388		if (af->cmp_addr(&laddr->a, addr)) {
389			state = laddr->state;
390			break;
391		}
392	}
393	rcu_read_unlock();
394
395	return state;
396}
397
398/* Find the first address in the bind address list that is not present in
399 * the addrs packed array.
400 */
401union sctp_addr *sctp_find_unmatch_addr(struct sctp_bind_addr	*bp,
402					const union sctp_addr	*addrs,
403					int			addrcnt,
404					struct sctp_sock	*opt)
405{
406	struct sctp_sockaddr_entry	*laddr;
407	union sctp_addr			*addr;
408	void 				*addr_buf;
409	struct sctp_af			*af;
410	int				i;
411
412	/* This is only called sctp_send_asconf_del_ip() and we hold
413	 * the socket lock in that code patch, so that address list
414	 * can't change.
415	 */
416	list_for_each_entry(laddr, &bp->address_list, list) {
417		addr_buf = (union sctp_addr *)addrs;
418		for (i = 0; i < addrcnt; i++) {
419			addr = addr_buf;
420			af = sctp_get_af_specific(addr->v4.sin_family);
421			if (!af)
422				break;
423
424			if (opt->pf->cmp_addr(&laddr->a, addr, opt))
425				break;
426
427			addr_buf += af->sockaddr_len;
428		}
429		if (i == addrcnt)
430			return &laddr->a;
431	}
432
433	return NULL;
434}
435
436/* Copy out addresses from the global local address list. */
437static int sctp_copy_one_addr(struct net *net, struct sctp_bind_addr *dest,
438			      union sctp_addr *addr,
439			      sctp_scope_t scope, gfp_t gfp,
440			      int flags)
441{
442	int error = 0;
443
444	if (sctp_is_any(NULL, addr)) {
445		error = sctp_copy_local_addr_list(net, dest, scope, gfp, flags);
446	} else if (sctp_in_scope(net, addr, scope)) {
447		/* Now that the address is in scope, check to see if
448		 * the address type is supported by local sock as
449		 * well as the remote peer.
450		 */
451		if ((((AF_INET == addr->sa.sa_family) &&
452		      (flags & SCTP_ADDR4_PEERSUPP))) ||
453		    (((AF_INET6 == addr->sa.sa_family) &&
454		      (flags & SCTP_ADDR6_ALLOWED) &&
455		      (flags & SCTP_ADDR6_PEERSUPP))))
456			error = sctp_add_bind_addr(dest, addr, SCTP_ADDR_SRC,
457						    gfp);
458	}
459
460	return error;
461}
462
463/* Is this a wildcard address?  */
464int sctp_is_any(struct sock *sk, const union sctp_addr *addr)
465{
466	unsigned short fam = 0;
467	struct sctp_af *af;
468
469	/* Try to get the right address family */
470	if (addr->sa.sa_family != AF_UNSPEC)
471		fam = addr->sa.sa_family;
472	else if (sk)
473		fam = sk->sk_family;
474
475	af = sctp_get_af_specific(fam);
476	if (!af)
477		return 0;
478
479	return af->is_any(addr);
480}
481
482/* Is 'addr' valid for 'scope'?  */
483int sctp_in_scope(struct net *net, const union sctp_addr *addr, sctp_scope_t scope)
484{
485	sctp_scope_t addr_scope = sctp_scope(addr);
486
487	/* The unusable SCTP addresses will not be considered with
488	 * any defined scopes.
489	 */
490	if (SCTP_SCOPE_UNUSABLE == addr_scope)
491		return 0;
492	/*
493	 * For INIT and INIT-ACK address list, let L be the level of
494	 * of requested destination address, sender and receiver
495	 * SHOULD include all of its addresses with level greater
496	 * than or equal to L.
497	 *
498	 * Address scoping can be selectively controlled via sysctl
499	 * option
500	 */
501	switch (net->sctp.scope_policy) {
502	case SCTP_SCOPE_POLICY_DISABLE:
503		return 1;
504	case SCTP_SCOPE_POLICY_ENABLE:
505		if (addr_scope <= scope)
506			return 1;
507		break;
508	case SCTP_SCOPE_POLICY_PRIVATE:
509		if (addr_scope <= scope || SCTP_SCOPE_PRIVATE == addr_scope)
510			return 1;
511		break;
512	case SCTP_SCOPE_POLICY_LINK:
513		if (addr_scope <= scope || SCTP_SCOPE_LINK == addr_scope)
514			return 1;
515		break;
516	default:
517		break;
518	}
519
520	return 0;
521}
522
523int sctp_is_ep_boundall(struct sock *sk)
524{
525	struct sctp_bind_addr *bp;
526	struct sctp_sockaddr_entry *addr;
527
528	bp = &sctp_sk(sk)->ep->base.bind_addr;
529	if (sctp_list_single_entry(&bp->address_list)) {
530		addr = list_entry(bp->address_list.next,
531				  struct sctp_sockaddr_entry, list);
532		if (sctp_is_any(sk, &addr->a))
533			return 1;
534	}
535	return 0;
536}
537
538/********************************************************************
539 * 3rd Level Abstractions
540 ********************************************************************/
541
542/* What is the scope of 'addr'?  */
543sctp_scope_t sctp_scope(const union sctp_addr *addr)
544{
545	struct sctp_af *af;
546
547	af = sctp_get_af_specific(addr->sa.sa_family);
548	if (!af)
549		return SCTP_SCOPE_UNUSABLE;
550
551	return af->scope((union sctp_addr *)addr);
552}
553