1 // SPDX-License-Identifier: GPL-2.0 2 /* Multipath TCP 3 * 4 * Copyright (c) 2022, Intel Corporation. 5 */ 6 7 #include "protocol.h" 8 #include "mib.h" 9 10 void mptcp_free_local_addr_list(struct mptcp_sock *msk) 11 { 12 struct mptcp_pm_addr_entry *entry, *tmp; 13 struct sock *sk = (struct sock *)msk; 14 LIST_HEAD(free_list); 15 16 if (!mptcp_pm_is_userspace(msk)) 17 return; 18 19 spin_lock_bh(&msk->pm.lock); 20 list_splice_init(&msk->pm.userspace_pm_local_addr_list, &free_list); 21 spin_unlock_bh(&msk->pm.lock); 22 23 list_for_each_entry_safe(entry, tmp, &free_list, list) { 24 sock_kfree_s(sk, entry, sizeof(*entry)); 25 } 26 } 27 28 static int mptcp_userspace_pm_append_new_local_addr(struct mptcp_sock *msk, 29 struct mptcp_pm_addr_entry *entry) 30 { 31 DECLARE_BITMAP(id_bitmap, MPTCP_PM_MAX_ADDR_ID + 1); 32 struct mptcp_pm_addr_entry *match = NULL; 33 struct sock *sk = (struct sock *)msk; 34 struct mptcp_pm_addr_entry *e; 35 bool addr_match = false; 36 bool id_match = false; 37 int ret = -EINVAL; 38 39 bitmap_zero(id_bitmap, MPTCP_PM_MAX_ADDR_ID + 1); 40 41 spin_lock_bh(&msk->pm.lock); 42 list_for_each_entry(e, &msk->pm.userspace_pm_local_addr_list, list) { 43 addr_match = mptcp_addresses_equal(&e->addr, &entry->addr, true); 44 if (addr_match && entry->addr.id == 0) 45 entry->addr.id = e->addr.id; 46 id_match = (e->addr.id == entry->addr.id); 47 if (addr_match && id_match) { 48 match = e; 49 break; 50 } else if (addr_match || id_match) { 51 break; 52 } 53 __set_bit(e->addr.id, id_bitmap); 54 } 55 56 if (!match && !addr_match && !id_match) { 57 /* Memory for the entry is allocated from the 58 * sock option buffer. 59 */ 60 e = sock_kmalloc(sk, sizeof(*e), GFP_ATOMIC); 61 if (!e) { 62 ret = -ENOMEM; 63 goto append_err; 64 } 65 66 *e = *entry; 67 if (!e->addr.id) 68 e->addr.id = find_next_zero_bit(id_bitmap, 69 MPTCP_PM_MAX_ADDR_ID + 1, 70 1); 71 list_add_tail_rcu(&e->list, &msk->pm.userspace_pm_local_addr_list); 72 msk->pm.local_addr_used++; 73 ret = e->addr.id; 74 } else if (match) { 75 ret = entry->addr.id; 76 } 77 78 append_err: 79 spin_unlock_bh(&msk->pm.lock); 80 return ret; 81 } 82 83 /* If the subflow is closed from the other peer (not via a 84 * subflow destroy command then), we want to keep the entry 85 * not to assign the same ID to another address and to be 86 * able to send RM_ADDR after the removal of the subflow. 87 */ 88 static int mptcp_userspace_pm_delete_local_addr(struct mptcp_sock *msk, 89 struct mptcp_pm_addr_entry *addr) 90 { 91 struct mptcp_pm_addr_entry *entry, *tmp; 92 93 list_for_each_entry_safe(entry, tmp, &msk->pm.userspace_pm_local_addr_list, list) { 94 if (mptcp_addresses_equal(&entry->addr, &addr->addr, false)) { 95 /* TODO: a refcount is needed because the entry can 96 * be used multiple times (e.g. fullmesh mode). 97 */ 98 list_del_rcu(&entry->list); 99 kfree(entry); 100 msk->pm.local_addr_used--; 101 return 0; 102 } 103 } 104 105 return -EINVAL; 106 } 107 108 int mptcp_userspace_pm_get_flags_and_ifindex_by_id(struct mptcp_sock *msk, 109 unsigned int id, 110 u8 *flags, int *ifindex) 111 { 112 struct mptcp_pm_addr_entry *entry, *match = NULL; 113 114 spin_lock_bh(&msk->pm.lock); 115 list_for_each_entry(entry, &msk->pm.userspace_pm_local_addr_list, list) { 116 if (id == entry->addr.id) { 117 match = entry; 118 break; 119 } 120 } 121 spin_unlock_bh(&msk->pm.lock); 122 if (match) { 123 *flags = match->flags; 124 *ifindex = match->ifindex; 125 } 126 127 return 0; 128 } 129 130 int mptcp_userspace_pm_get_local_id(struct mptcp_sock *msk, 131 struct mptcp_addr_info *skc) 132 { 133 struct mptcp_pm_addr_entry *entry = NULL, *e, new_entry; 134 __be16 msk_sport = ((struct inet_sock *) 135 inet_sk((struct sock *)msk))->inet_sport; 136 137 spin_lock_bh(&msk->pm.lock); 138 list_for_each_entry(e, &msk->pm.userspace_pm_local_addr_list, list) { 139 if (mptcp_addresses_equal(&e->addr, skc, false)) { 140 entry = e; 141 break; 142 } 143 } 144 spin_unlock_bh(&msk->pm.lock); 145 if (entry) 146 return entry->addr.id; 147 148 memset(&new_entry, 0, sizeof(struct mptcp_pm_addr_entry)); 149 new_entry.addr = *skc; 150 new_entry.addr.id = 0; 151 new_entry.flags = MPTCP_PM_ADDR_FLAG_IMPLICIT; 152 153 if (new_entry.addr.port == msk_sport) 154 new_entry.addr.port = 0; 155 156 return mptcp_userspace_pm_append_new_local_addr(msk, &new_entry); 157 } 158 159 int mptcp_pm_nl_announce_doit(struct sk_buff *skb, struct genl_info *info) 160 { 161 struct nlattr *token = info->attrs[MPTCP_PM_ATTR_TOKEN]; 162 struct nlattr *addr = info->attrs[MPTCP_PM_ATTR_ADDR]; 163 struct mptcp_pm_addr_entry addr_val; 164 struct mptcp_sock *msk; 165 int err = -EINVAL; 166 struct sock *sk; 167 u32 token_val; 168 169 if (!addr || !token) { 170 GENL_SET_ERR_MSG(info, "missing required inputs"); 171 return err; 172 } 173 174 token_val = nla_get_u32(token); 175 176 msk = mptcp_token_get_sock(sock_net(skb->sk), token_val); 177 if (!msk) { 178 NL_SET_ERR_MSG_ATTR(info->extack, token, "invalid token"); 179 return err; 180 } 181 182 sk = (struct sock *)msk; 183 184 if (!mptcp_pm_is_userspace(msk)) { 185 GENL_SET_ERR_MSG(info, "invalid request; userspace PM not selected"); 186 goto announce_err; 187 } 188 189 err = mptcp_pm_parse_entry(addr, info, true, &addr_val); 190 if (err < 0) { 191 GENL_SET_ERR_MSG(info, "error parsing local address"); 192 goto announce_err; 193 } 194 195 if (addr_val.addr.id == 0 || !(addr_val.flags & MPTCP_PM_ADDR_FLAG_SIGNAL)) { 196 GENL_SET_ERR_MSG(info, "invalid addr id or flags"); 197 err = -EINVAL; 198 goto announce_err; 199 } 200 201 err = mptcp_userspace_pm_append_new_local_addr(msk, &addr_val); 202 if (err < 0) { 203 GENL_SET_ERR_MSG(info, "did not match address and id"); 204 goto announce_err; 205 } 206 207 lock_sock(sk); 208 spin_lock_bh(&msk->pm.lock); 209 210 if (mptcp_pm_alloc_anno_list(msk, &addr_val.addr)) { 211 msk->pm.add_addr_signaled++; 212 mptcp_pm_announce_addr(msk, &addr_val.addr, false); 213 mptcp_pm_nl_addr_send_ack(msk); 214 } 215 216 spin_unlock_bh(&msk->pm.lock); 217 release_sock(sk); 218 219 err = 0; 220 announce_err: 221 sock_put(sk); 222 return err; 223 } 224 225 static int mptcp_userspace_pm_remove_id_zero_address(struct mptcp_sock *msk, 226 struct genl_info *info) 227 { 228 struct mptcp_rm_list list = { .nr = 0 }; 229 struct mptcp_subflow_context *subflow; 230 struct sock *sk = (struct sock *)msk; 231 bool has_id_0 = false; 232 int err = -EINVAL; 233 234 lock_sock(sk); 235 mptcp_for_each_subflow(msk, subflow) { 236 if (subflow->local_id == 0) { 237 has_id_0 = true; 238 break; 239 } 240 } 241 if (!has_id_0) { 242 GENL_SET_ERR_MSG(info, "address with id 0 not found"); 243 goto remove_err; 244 } 245 246 list.ids[list.nr++] = 0; 247 248 spin_lock_bh(&msk->pm.lock); 249 mptcp_pm_remove_addr(msk, &list); 250 spin_unlock_bh(&msk->pm.lock); 251 252 err = 0; 253 254 remove_err: 255 release_sock(sk); 256 return err; 257 } 258 259 int mptcp_pm_nl_remove_doit(struct sk_buff *skb, struct genl_info *info) 260 { 261 struct nlattr *token = info->attrs[MPTCP_PM_ATTR_TOKEN]; 262 struct nlattr *id = info->attrs[MPTCP_PM_ATTR_LOC_ID]; 263 struct mptcp_pm_addr_entry *match = NULL; 264 struct mptcp_pm_addr_entry *entry; 265 struct mptcp_sock *msk; 266 LIST_HEAD(free_list); 267 int err = -EINVAL; 268 struct sock *sk; 269 u32 token_val; 270 u8 id_val; 271 272 if (!id || !token) { 273 GENL_SET_ERR_MSG(info, "missing required inputs"); 274 return err; 275 } 276 277 id_val = nla_get_u8(id); 278 token_val = nla_get_u32(token); 279 280 msk = mptcp_token_get_sock(sock_net(skb->sk), token_val); 281 if (!msk) { 282 NL_SET_ERR_MSG_ATTR(info->extack, token, "invalid token"); 283 return err; 284 } 285 286 sk = (struct sock *)msk; 287 288 if (!mptcp_pm_is_userspace(msk)) { 289 GENL_SET_ERR_MSG(info, "invalid request; userspace PM not selected"); 290 goto out; 291 } 292 293 if (id_val == 0) { 294 err = mptcp_userspace_pm_remove_id_zero_address(msk, info); 295 goto out; 296 } 297 298 lock_sock(sk); 299 300 list_for_each_entry(entry, &msk->pm.userspace_pm_local_addr_list, list) { 301 if (entry->addr.id == id_val) { 302 match = entry; 303 break; 304 } 305 } 306 307 if (!match) { 308 GENL_SET_ERR_MSG(info, "address with specified id not found"); 309 release_sock(sk); 310 goto out; 311 } 312 313 list_move(&match->list, &free_list); 314 315 mptcp_pm_remove_addrs(msk, &free_list); 316 317 release_sock(sk); 318 319 list_for_each_entry_safe(match, entry, &free_list, list) { 320 sock_kfree_s(sk, match, sizeof(*match)); 321 } 322 323 err = 0; 324 out: 325 sock_put(sk); 326 return err; 327 } 328 329 int mptcp_pm_nl_subflow_create_doit(struct sk_buff *skb, struct genl_info *info) 330 { 331 struct nlattr *raddr = info->attrs[MPTCP_PM_ATTR_ADDR_REMOTE]; 332 struct nlattr *token = info->attrs[MPTCP_PM_ATTR_TOKEN]; 333 struct nlattr *laddr = info->attrs[MPTCP_PM_ATTR_ADDR]; 334 struct mptcp_pm_addr_entry local = { 0 }; 335 struct mptcp_addr_info addr_r; 336 struct mptcp_addr_info addr_l; 337 struct mptcp_sock *msk; 338 int err = -EINVAL; 339 struct sock *sk; 340 u32 token_val; 341 342 if (!laddr || !raddr || !token) { 343 GENL_SET_ERR_MSG(info, "missing required inputs"); 344 return err; 345 } 346 347 token_val = nla_get_u32(token); 348 349 msk = mptcp_token_get_sock(genl_info_net(info), token_val); 350 if (!msk) { 351 NL_SET_ERR_MSG_ATTR(info->extack, token, "invalid token"); 352 return err; 353 } 354 355 sk = (struct sock *)msk; 356 357 if (!mptcp_pm_is_userspace(msk)) { 358 GENL_SET_ERR_MSG(info, "invalid request; userspace PM not selected"); 359 goto create_err; 360 } 361 362 err = mptcp_pm_parse_addr(laddr, info, &addr_l); 363 if (err < 0) { 364 NL_SET_ERR_MSG_ATTR(info->extack, laddr, "error parsing local addr"); 365 goto create_err; 366 } 367 368 err = mptcp_pm_parse_addr(raddr, info, &addr_r); 369 if (err < 0) { 370 NL_SET_ERR_MSG_ATTR(info->extack, raddr, "error parsing remote addr"); 371 goto create_err; 372 } 373 374 if (!mptcp_pm_addr_families_match(sk, &addr_l, &addr_r)) { 375 GENL_SET_ERR_MSG(info, "families mismatch"); 376 err = -EINVAL; 377 goto create_err; 378 } 379 380 local.addr = addr_l; 381 err = mptcp_userspace_pm_append_new_local_addr(msk, &local); 382 if (err < 0) { 383 GENL_SET_ERR_MSG(info, "did not match address and id"); 384 goto create_err; 385 } 386 387 lock_sock(sk); 388 389 err = __mptcp_subflow_connect(sk, &addr_l, &addr_r); 390 391 release_sock(sk); 392 393 spin_lock_bh(&msk->pm.lock); 394 if (err) 395 mptcp_userspace_pm_delete_local_addr(msk, &local); 396 else 397 msk->pm.subflows++; 398 spin_unlock_bh(&msk->pm.lock); 399 400 create_err: 401 sock_put(sk); 402 return err; 403 } 404 405 static struct sock *mptcp_nl_find_ssk(struct mptcp_sock *msk, 406 const struct mptcp_addr_info *local, 407 const struct mptcp_addr_info *remote) 408 { 409 struct mptcp_subflow_context *subflow; 410 411 if (local->family != remote->family) 412 return NULL; 413 414 mptcp_for_each_subflow(msk, subflow) { 415 const struct inet_sock *issk; 416 struct sock *ssk; 417 418 ssk = mptcp_subflow_tcp_sock(subflow); 419 420 if (local->family != ssk->sk_family) 421 continue; 422 423 issk = inet_sk(ssk); 424 425 switch (ssk->sk_family) { 426 case AF_INET: 427 if (issk->inet_saddr != local->addr.s_addr || 428 issk->inet_daddr != remote->addr.s_addr) 429 continue; 430 break; 431 #if IS_ENABLED(CONFIG_MPTCP_IPV6) 432 case AF_INET6: { 433 const struct ipv6_pinfo *pinfo = inet6_sk(ssk); 434 435 if (!ipv6_addr_equal(&local->addr6, &pinfo->saddr) || 436 !ipv6_addr_equal(&remote->addr6, &ssk->sk_v6_daddr)) 437 continue; 438 break; 439 } 440 #endif 441 default: 442 continue; 443 } 444 445 if (issk->inet_sport == local->port && 446 issk->inet_dport == remote->port) 447 return ssk; 448 } 449 450 return NULL; 451 } 452 453 int mptcp_pm_nl_subflow_destroy_doit(struct sk_buff *skb, struct genl_info *info) 454 { 455 struct nlattr *raddr = info->attrs[MPTCP_PM_ATTR_ADDR_REMOTE]; 456 struct nlattr *token = info->attrs[MPTCP_PM_ATTR_TOKEN]; 457 struct nlattr *laddr = info->attrs[MPTCP_PM_ATTR_ADDR]; 458 struct mptcp_addr_info addr_l; 459 struct mptcp_addr_info addr_r; 460 struct mptcp_sock *msk; 461 struct sock *sk, *ssk; 462 int err = -EINVAL; 463 u32 token_val; 464 465 if (!laddr || !raddr || !token) { 466 GENL_SET_ERR_MSG(info, "missing required inputs"); 467 return err; 468 } 469 470 token_val = nla_get_u32(token); 471 472 msk = mptcp_token_get_sock(genl_info_net(info), token_val); 473 if (!msk) { 474 NL_SET_ERR_MSG_ATTR(info->extack, token, "invalid token"); 475 return err; 476 } 477 478 sk = (struct sock *)msk; 479 480 if (!mptcp_pm_is_userspace(msk)) { 481 GENL_SET_ERR_MSG(info, "invalid request; userspace PM not selected"); 482 goto destroy_err; 483 } 484 485 err = mptcp_pm_parse_addr(laddr, info, &addr_l); 486 if (err < 0) { 487 NL_SET_ERR_MSG_ATTR(info->extack, laddr, "error parsing local addr"); 488 goto destroy_err; 489 } 490 491 err = mptcp_pm_parse_addr(raddr, info, &addr_r); 492 if (err < 0) { 493 NL_SET_ERR_MSG_ATTR(info->extack, raddr, "error parsing remote addr"); 494 goto destroy_err; 495 } 496 497 if (addr_l.family != addr_r.family) { 498 GENL_SET_ERR_MSG(info, "address families do not match"); 499 err = -EINVAL; 500 goto destroy_err; 501 } 502 503 if (!addr_l.port || !addr_r.port) { 504 GENL_SET_ERR_MSG(info, "missing local or remote port"); 505 err = -EINVAL; 506 goto destroy_err; 507 } 508 509 lock_sock(sk); 510 ssk = mptcp_nl_find_ssk(msk, &addr_l, &addr_r); 511 if (ssk) { 512 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk); 513 struct mptcp_pm_addr_entry entry = { .addr = addr_l }; 514 515 spin_lock_bh(&msk->pm.lock); 516 mptcp_userspace_pm_delete_local_addr(msk, &entry); 517 spin_unlock_bh(&msk->pm.lock); 518 mptcp_subflow_shutdown(sk, ssk, RCV_SHUTDOWN | SEND_SHUTDOWN); 519 mptcp_close_ssk(sk, ssk, subflow); 520 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RMSUBFLOW); 521 err = 0; 522 } else { 523 err = -ESRCH; 524 } 525 release_sock(sk); 526 527 destroy_err: 528 sock_put(sk); 529 return err; 530 } 531 532 int mptcp_userspace_pm_set_flags(struct net *net, struct nlattr *token, 533 struct mptcp_pm_addr_entry *loc, 534 struct mptcp_pm_addr_entry *rem, u8 bkup) 535 { 536 struct mptcp_sock *msk; 537 int ret = -EINVAL; 538 struct sock *sk; 539 u32 token_val; 540 541 token_val = nla_get_u32(token); 542 543 msk = mptcp_token_get_sock(net, token_val); 544 if (!msk) 545 return ret; 546 547 sk = (struct sock *)msk; 548 549 if (!mptcp_pm_is_userspace(msk)) 550 goto set_flags_err; 551 552 if (loc->addr.family == AF_UNSPEC || 553 rem->addr.family == AF_UNSPEC) 554 goto set_flags_err; 555 556 lock_sock(sk); 557 ret = mptcp_pm_nl_mp_prio_send_ack(msk, &loc->addr, &rem->addr, bkup); 558 release_sock(sk); 559 560 set_flags_err: 561 sock_put(sk); 562 return ret; 563 } 564