2020-03-28 05:48:38 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
/* Multipath TCP
|
|
|
|
*
|
|
|
|
* Copyright (c) 2019, Intel Corporation.
|
|
|
|
*/
|
2020-04-03 17:14:08 +08:00
|
|
|
#define pr_fmt(fmt) "MPTCP: " fmt
|
|
|
|
|
2020-03-28 05:48:38 +08:00
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <net/tcp.h>
|
|
|
|
#include <net/mptcp.h>
|
|
|
|
#include "protocol.h"
|
|
|
|
|
|
|
|
/* path manager command handlers */
|
|
|
|
|
|
|
|
int mptcp_pm_announce_addr(struct mptcp_sock *msk,
|
2020-09-24 08:29:50 +08:00
|
|
|
const struct mptcp_addr_info *addr,
|
2020-12-10 07:51:24 +08:00
|
|
|
bool echo, bool port)
|
2020-03-28 05:48:38 +08:00
|
|
|
{
|
2020-12-10 07:51:27 +08:00
|
|
|
u8 add_addr = READ_ONCE(msk->pm.addr_signal);
|
2020-11-20 03:45:59 +08:00
|
|
|
|
2020-03-28 05:48:41 +08:00
|
|
|
pr_debug("msk=%p, local_id=%d", msk, addr->id);
|
|
|
|
|
2021-02-05 07:23:30 +08:00
|
|
|
lockdep_assert_held(&msk->pm.lock);
|
|
|
|
|
2020-12-10 07:51:26 +08:00
|
|
|
if (add_addr) {
|
|
|
|
pr_warn("addr_signal error, add_addr=%d", add_addr);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2020-03-28 05:48:41 +08:00
|
|
|
msk->pm.local = *addr;
|
2020-11-20 03:45:59 +08:00
|
|
|
add_addr |= BIT(MPTCP_ADD_ADDR_SIGNAL);
|
|
|
|
if (echo)
|
|
|
|
add_addr |= BIT(MPTCP_ADD_ADDR_ECHO);
|
2020-11-20 03:46:00 +08:00
|
|
|
if (addr->family == AF_INET6)
|
|
|
|
add_addr |= BIT(MPTCP_ADD_ADDR_IPV6);
|
2020-12-10 07:51:24 +08:00
|
|
|
if (port)
|
|
|
|
add_addr |= BIT(MPTCP_ADD_ADDR_PORT);
|
2020-12-10 07:51:27 +08:00
|
|
|
WRITE_ONCE(msk->pm.addr_signal, add_addr);
|
2020-03-28 05:48:41 +08:00
|
|
|
return 0;
|
2020-03-28 05:48:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int mptcp_pm_remove_addr(struct mptcp_sock *msk, u8 local_id)
|
|
|
|
{
|
2020-12-10 07:51:27 +08:00
|
|
|
u8 rm_addr = READ_ONCE(msk->pm.addr_signal);
|
2020-12-10 07:51:26 +08:00
|
|
|
|
2020-09-24 08:29:54 +08:00
|
|
|
pr_debug("msk=%p, local_id=%d", msk, local_id);
|
|
|
|
|
2020-12-10 07:51:26 +08:00
|
|
|
if (rm_addr) {
|
|
|
|
pr_warn("addr_signal error, rm_addr=%d", rm_addr);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2020-09-24 08:29:54 +08:00
|
|
|
msk->pm.rm_id = local_id;
|
2020-12-10 07:51:26 +08:00
|
|
|
rm_addr |= BIT(MPTCP_RM_ADDR_SIGNAL);
|
2020-12-10 07:51:27 +08:00
|
|
|
WRITE_ONCE(msk->pm.addr_signal, rm_addr);
|
2020-09-24 08:29:54 +08:00
|
|
|
return 0;
|
2020-03-28 05:48:38 +08:00
|
|
|
}
|
|
|
|
|
2020-09-24 08:29:55 +08:00
|
|
|
int mptcp_pm_remove_subflow(struct mptcp_sock *msk, u8 local_id)
|
2020-03-28 05:48:38 +08:00
|
|
|
{
|
2020-09-24 08:29:55 +08:00
|
|
|
pr_debug("msk=%p, local_id=%d", msk, local_id);
|
|
|
|
|
|
|
|
spin_lock_bh(&msk->pm.lock);
|
|
|
|
mptcp_pm_nl_rm_subflow_received(msk, local_id);
|
|
|
|
spin_unlock_bh(&msk->pm.lock);
|
|
|
|
return 0;
|
2020-03-28 05:48:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* path manager event handlers */
|
|
|
|
|
2021-02-13 07:59:58 +08:00
|
|
|
void mptcp_pm_new_connection(struct mptcp_sock *msk, const struct sock *ssk, int server_side)
|
2020-03-28 05:48:38 +08:00
|
|
|
{
|
|
|
|
struct mptcp_pm_data *pm = &msk->pm;
|
|
|
|
|
|
|
|
pr_debug("msk=%p, token=%u side=%d", msk, msk->token, server_side);
|
|
|
|
|
|
|
|
WRITE_ONCE(pm->server_side, server_side);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool mptcp_pm_allow_new_subflow(struct mptcp_sock *msk)
|
|
|
|
{
|
2020-03-28 05:48:41 +08:00
|
|
|
struct mptcp_pm_data *pm = &msk->pm;
|
2021-02-02 07:09:07 +08:00
|
|
|
unsigned int subflows_max;
|
2020-09-24 08:29:53 +08:00
|
|
|
int ret = 0;
|
2020-03-28 05:48:41 +08:00
|
|
|
|
2021-02-02 07:09:07 +08:00
|
|
|
subflows_max = mptcp_pm_get_subflows_max(msk);
|
|
|
|
|
2020-03-28 05:48:41 +08:00
|
|
|
pr_debug("msk=%p subflows=%d max=%d allow=%d", msk, pm->subflows,
|
2021-02-02 07:09:07 +08:00
|
|
|
subflows_max, READ_ONCE(pm->accept_subflow));
|
2020-03-28 05:48:41 +08:00
|
|
|
|
|
|
|
/* try to avoid acquiring the lock below */
|
|
|
|
if (!READ_ONCE(pm->accept_subflow))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
spin_lock_bh(&pm->lock);
|
2020-09-24 08:29:53 +08:00
|
|
|
if (READ_ONCE(pm->accept_subflow)) {
|
2021-02-02 07:09:07 +08:00
|
|
|
ret = pm->subflows < subflows_max;
|
|
|
|
if (ret && ++pm->subflows == subflows_max)
|
2020-09-24 08:29:53 +08:00
|
|
|
WRITE_ONCE(pm->accept_subflow, false);
|
|
|
|
}
|
2020-03-28 05:48:41 +08:00
|
|
|
spin_unlock_bh(&pm->lock);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* return true if the new status bit is currently cleared, that is, this event
|
|
|
|
* can be server, eventually by an already scheduled work
|
|
|
|
*/
|
|
|
|
static bool mptcp_pm_schedule_work(struct mptcp_sock *msk,
|
|
|
|
enum mptcp_pm_status new_status)
|
|
|
|
{
|
|
|
|
pr_debug("msk=%p status=%x new=%lx", msk, msk->pm.status,
|
|
|
|
BIT(new_status));
|
|
|
|
if (msk->pm.status & BIT(new_status))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
msk->pm.status |= BIT(new_status);
|
2020-11-16 17:48:05 +08:00
|
|
|
mptcp_schedule_work((struct sock *)msk);
|
2020-03-28 05:48:41 +08:00
|
|
|
return true;
|
2020-03-28 05:48:38 +08:00
|
|
|
}
|
|
|
|
|
2021-02-13 07:59:58 +08:00
|
|
|
void mptcp_pm_fully_established(struct mptcp_sock *msk, const struct sock *ssk, gfp_t gfp)
|
2020-03-28 05:48:38 +08:00
|
|
|
{
|
2020-03-28 05:48:41 +08:00
|
|
|
struct mptcp_pm_data *pm = &msk->pm;
|
|
|
|
|
2020-03-28 05:48:38 +08:00
|
|
|
pr_debug("msk=%p", msk);
|
2020-03-28 05:48:41 +08:00
|
|
|
|
|
|
|
/* try to avoid acquiring the lock below */
|
|
|
|
if (!READ_ONCE(pm->work_pending))
|
|
|
|
return;
|
|
|
|
|
|
|
|
spin_lock_bh(&pm->lock);
|
|
|
|
|
2020-12-09 19:03:29 +08:00
|
|
|
/* mptcp_pm_fully_established() can be invoked by multiple
|
|
|
|
* racing paths - accept() and check_fully_established()
|
|
|
|
* be sure to serve this event only once.
|
|
|
|
*/
|
|
|
|
if (READ_ONCE(pm->work_pending) &&
|
|
|
|
!(msk->pm.status & BIT(MPTCP_PM_ALREADY_ESTABLISHED)))
|
2020-03-28 05:48:41 +08:00
|
|
|
mptcp_pm_schedule_work(msk, MPTCP_PM_ESTABLISHED);
|
2020-12-09 19:03:29 +08:00
|
|
|
msk->pm.status |= BIT(MPTCP_PM_ALREADY_ESTABLISHED);
|
2020-03-28 05:48:41 +08:00
|
|
|
|
|
|
|
spin_unlock_bh(&pm->lock);
|
2020-03-28 05:48:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void mptcp_pm_connection_closed(struct mptcp_sock *msk)
|
|
|
|
{
|
|
|
|
pr_debug("msk=%p", msk);
|
|
|
|
}
|
|
|
|
|
|
|
|
void mptcp_pm_subflow_established(struct mptcp_sock *msk,
|
|
|
|
struct mptcp_subflow_context *subflow)
|
|
|
|
{
|
2020-03-28 05:48:41 +08:00
|
|
|
struct mptcp_pm_data *pm = &msk->pm;
|
|
|
|
|
2020-03-28 05:48:38 +08:00
|
|
|
pr_debug("msk=%p", msk);
|
2020-03-28 05:48:41 +08:00
|
|
|
|
|
|
|
if (!READ_ONCE(pm->work_pending))
|
|
|
|
return;
|
|
|
|
|
|
|
|
spin_lock_bh(&pm->lock);
|
|
|
|
|
|
|
|
if (READ_ONCE(pm->work_pending))
|
|
|
|
mptcp_pm_schedule_work(msk, MPTCP_PM_SUBFLOW_ESTABLISHED);
|
|
|
|
|
|
|
|
spin_unlock_bh(&pm->lock);
|
2020-03-28 05:48:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void mptcp_pm_subflow_closed(struct mptcp_sock *msk, u8 id)
|
|
|
|
{
|
|
|
|
pr_debug("msk=%p", msk);
|
|
|
|
}
|
|
|
|
|
|
|
|
void mptcp_pm_add_addr_received(struct mptcp_sock *msk,
|
|
|
|
const struct mptcp_addr_info *addr)
|
|
|
|
{
|
2020-03-28 05:48:41 +08:00
|
|
|
struct mptcp_pm_data *pm = &msk->pm;
|
|
|
|
|
|
|
|
pr_debug("msk=%p remote_id=%d accept=%d", msk, addr->id,
|
|
|
|
READ_ONCE(pm->accept_addr));
|
|
|
|
|
|
|
|
spin_lock_bh(&pm->lock);
|
|
|
|
|
2020-11-20 03:46:00 +08:00
|
|
|
if (!READ_ONCE(pm->accept_addr)) {
|
2020-12-10 07:51:24 +08:00
|
|
|
mptcp_pm_announce_addr(msk, addr, true, addr->port);
|
2020-11-20 03:46:00 +08:00
|
|
|
mptcp_pm_add_addr_send_ack(msk);
|
|
|
|
} else if (mptcp_pm_schedule_work(msk, MPTCP_PM_ADD_ADDR_RECEIVED)) {
|
2020-03-28 05:48:41 +08:00
|
|
|
pm->remote = *addr;
|
2020-11-20 03:46:00 +08:00
|
|
|
}
|
2020-03-28 05:48:41 +08:00
|
|
|
|
|
|
|
spin_unlock_bh(&pm->lock);
|
2020-11-20 03:46:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void mptcp_pm_add_addr_send_ack(struct mptcp_sock *msk)
|
|
|
|
{
|
2021-02-02 07:09:09 +08:00
|
|
|
if (!mptcp_pm_should_add_signal(msk))
|
2020-11-20 03:46:00 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
mptcp_pm_schedule_work(msk, MPTCP_PM_ADD_ADDR_SEND_ACK);
|
2020-03-28 05:48:38 +08:00
|
|
|
}
|
|
|
|
|
2020-09-24 08:29:49 +08:00
|
|
|
void mptcp_pm_rm_addr_received(struct mptcp_sock *msk, u8 rm_id)
|
|
|
|
{
|
|
|
|
struct mptcp_pm_data *pm = &msk->pm;
|
|
|
|
|
|
|
|
pr_debug("msk=%p remote_id=%d", msk, rm_id);
|
|
|
|
|
|
|
|
spin_lock_bh(&pm->lock);
|
|
|
|
mptcp_pm_schedule_work(msk, MPTCP_PM_RM_ADDR_RECEIVED);
|
|
|
|
pm->rm_id = rm_id;
|
|
|
|
spin_unlock_bh(&pm->lock);
|
|
|
|
}
|
|
|
|
|
2021-01-09 08:47:58 +08:00
|
|
|
void mptcp_pm_mp_prio_received(struct sock *sk, u8 bkup)
|
|
|
|
{
|
|
|
|
struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
|
|
|
|
|
|
|
|
pr_debug("subflow->backup=%d, bkup=%d\n", subflow->backup, bkup);
|
|
|
|
subflow->backup = bkup;
|
|
|
|
}
|
|
|
|
|
2020-03-28 05:48:38 +08:00
|
|
|
/* path manager helpers */
|
|
|
|
|
2020-09-24 08:29:47 +08:00
|
|
|
bool mptcp_pm_add_addr_signal(struct mptcp_sock *msk, unsigned int remaining,
|
2020-12-10 07:51:22 +08:00
|
|
|
struct mptcp_addr_info *saddr, bool *echo, bool *port)
|
2020-03-28 05:48:38 +08:00
|
|
|
{
|
2020-03-28 05:48:41 +08:00
|
|
|
int ret = false;
|
|
|
|
|
|
|
|
spin_lock_bh(&msk->pm.lock);
|
|
|
|
|
|
|
|
/* double check after the lock is acquired */
|
2020-09-24 08:29:47 +08:00
|
|
|
if (!mptcp_pm_should_add_signal(msk))
|
2020-03-28 05:48:41 +08:00
|
|
|
goto out_unlock;
|
|
|
|
|
2020-11-20 03:45:59 +08:00
|
|
|
*echo = mptcp_pm_should_add_signal_echo(msk);
|
2020-12-10 07:51:22 +08:00
|
|
|
*port = mptcp_pm_should_add_signal_port(msk);
|
2020-10-03 23:36:56 +08:00
|
|
|
|
2020-12-10 07:51:22 +08:00
|
|
|
if (remaining < mptcp_add_addr_len(msk->pm.local.family, *echo, *port))
|
2020-03-28 05:48:41 +08:00
|
|
|
goto out_unlock;
|
|
|
|
|
|
|
|
*saddr = msk->pm.local;
|
2020-12-10 07:51:27 +08:00
|
|
|
WRITE_ONCE(msk->pm.addr_signal, 0);
|
2020-03-28 05:48:41 +08:00
|
|
|
ret = true;
|
|
|
|
|
|
|
|
out_unlock:
|
|
|
|
spin_unlock_bh(&msk->pm.lock);
|
|
|
|
return ret;
|
2020-03-28 05:48:38 +08:00
|
|
|
}
|
|
|
|
|
2020-09-24 08:29:48 +08:00
|
|
|
bool mptcp_pm_rm_addr_signal(struct mptcp_sock *msk, unsigned int remaining,
|
|
|
|
u8 *rm_id)
|
|
|
|
{
|
|
|
|
int ret = false;
|
|
|
|
|
|
|
|
spin_lock_bh(&msk->pm.lock);
|
|
|
|
|
|
|
|
/* double check after the lock is acquired */
|
|
|
|
if (!mptcp_pm_should_rm_signal(msk))
|
|
|
|
goto out_unlock;
|
|
|
|
|
|
|
|
if (remaining < TCPOLEN_MPTCP_RM_ADDR_BASE)
|
|
|
|
goto out_unlock;
|
|
|
|
|
|
|
|
*rm_id = msk->pm.rm_id;
|
2020-12-10 07:51:27 +08:00
|
|
|
WRITE_ONCE(msk->pm.addr_signal, 0);
|
2020-09-24 08:29:48 +08:00
|
|
|
ret = true;
|
|
|
|
|
|
|
|
out_unlock:
|
|
|
|
spin_unlock_bh(&msk->pm.lock);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-03-28 05:48:38 +08:00
|
|
|
int mptcp_pm_get_local_id(struct mptcp_sock *msk, struct sock_common *skc)
|
|
|
|
{
|
2020-03-28 05:48:51 +08:00
|
|
|
return mptcp_pm_nl_get_local_id(msk, skc);
|
2020-03-28 05:48:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void mptcp_pm_data_init(struct mptcp_sock *msk)
|
|
|
|
{
|
|
|
|
msk->pm.add_addr_signaled = 0;
|
|
|
|
msk->pm.add_addr_accepted = 0;
|
|
|
|
msk->pm.local_addr_used = 0;
|
|
|
|
msk->pm.subflows = 0;
|
2020-09-24 08:29:48 +08:00
|
|
|
msk->pm.rm_id = 0;
|
2020-03-28 05:48:38 +08:00
|
|
|
WRITE_ONCE(msk->pm.work_pending, false);
|
2020-12-10 07:51:27 +08:00
|
|
|
WRITE_ONCE(msk->pm.addr_signal, 0);
|
2020-03-28 05:48:38 +08:00
|
|
|
WRITE_ONCE(msk->pm.accept_addr, false);
|
|
|
|
WRITE_ONCE(msk->pm.accept_subflow, false);
|
|
|
|
msk->pm.status = 0;
|
|
|
|
|
|
|
|
spin_lock_init(&msk->pm.lock);
|
2020-09-24 08:29:54 +08:00
|
|
|
INIT_LIST_HEAD(&msk->pm.anno_list);
|
2020-03-28 05:48:51 +08:00
|
|
|
|
|
|
|
mptcp_pm_nl_data_init(msk);
|
2020-03-28 05:48:38 +08:00
|
|
|
}
|
|
|
|
|
2020-06-27 01:29:59 +08:00
|
|
|
void __init mptcp_pm_init(void)
|
2020-03-28 05:48:38 +08:00
|
|
|
{
|
2020-03-28 05:48:51 +08:00
|
|
|
mptcp_pm_nl_init();
|
2020-03-28 05:48:38 +08:00
|
|
|
}
|