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