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