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