Peter Krystad | 1b1c7a0 | 2020-03-27 14:48:38 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* Multipath TCP |
| 3 | * |
| 4 | * Copyright (c) 2019, Intel Corporation. |
| 5 | */ |
Geliang Tang | c85adce | 2020-04-03 17:14:08 +0800 | [diff] [blame] | 6 | #define pr_fmt(fmt) "MPTCP: " fmt |
| 7 | |
Peter Krystad | 1b1c7a0 | 2020-03-27 14:48:38 -0700 | [diff] [blame] | 8 | #include <linux/kernel.h> |
| 9 | #include <net/tcp.h> |
| 10 | #include <net/mptcp.h> |
| 11 | #include "protocol.h" |
| 12 | |
Peter Krystad | 1b1c7a0 | 2020-03-27 14:48:38 -0700 | [diff] [blame] | 13 | /* path manager command handlers */ |
| 14 | |
| 15 | int mptcp_pm_announce_addr(struct mptcp_sock *msk, |
Geliang Tang | 6a6c05a | 2020-09-24 08:29:50 +0800 | [diff] [blame] | 16 | const struct mptcp_addr_info *addr, |
| 17 | bool echo) |
Peter Krystad | 1b1c7a0 | 2020-03-27 14:48:38 -0700 | [diff] [blame] | 18 | { |
Peter Krystad | 926bdea | 2020-03-27 14:48:41 -0700 | [diff] [blame] | 19 | pr_debug("msk=%p, local_id=%d", msk, addr->id); |
| 20 | |
| 21 | msk->pm.local = *addr; |
Geliang Tang | 6a6c05a | 2020-09-24 08:29:50 +0800 | [diff] [blame] | 22 | WRITE_ONCE(msk->pm.add_addr_echo, echo); |
Geliang Tang | f643b80 | 2020-09-24 08:29:47 +0800 | [diff] [blame] | 23 | WRITE_ONCE(msk->pm.add_addr_signal, true); |
Peter Krystad | 926bdea | 2020-03-27 14:48:41 -0700 | [diff] [blame] | 24 | return 0; |
Peter Krystad | 1b1c7a0 | 2020-03-27 14:48:38 -0700 | [diff] [blame] | 25 | } |
| 26 | |
| 27 | int mptcp_pm_remove_addr(struct mptcp_sock *msk, u8 local_id) |
| 28 | { |
Geliang Tang | b6c0838 | 2020-09-24 08:29:54 +0800 | [diff] [blame] | 29 | pr_debug("msk=%p, local_id=%d", msk, local_id); |
| 30 | |
| 31 | msk->pm.rm_id = local_id; |
| 32 | WRITE_ONCE(msk->pm.rm_addr_signal, true); |
| 33 | return 0; |
Peter Krystad | 1b1c7a0 | 2020-03-27 14:48:38 -0700 | [diff] [blame] | 34 | } |
| 35 | |
Geliang Tang | 0ee4261 | 2020-09-24 08:29:55 +0800 | [diff] [blame] | 36 | int mptcp_pm_remove_subflow(struct mptcp_sock *msk, u8 local_id) |
Peter Krystad | 1b1c7a0 | 2020-03-27 14:48:38 -0700 | [diff] [blame] | 37 | { |
Geliang Tang | 0ee4261 | 2020-09-24 08:29:55 +0800 | [diff] [blame] | 38 | pr_debug("msk=%p, local_id=%d", msk, local_id); |
| 39 | |
| 40 | spin_lock_bh(&msk->pm.lock); |
| 41 | mptcp_pm_nl_rm_subflow_received(msk, local_id); |
| 42 | spin_unlock_bh(&msk->pm.lock); |
| 43 | return 0; |
Peter Krystad | 1b1c7a0 | 2020-03-27 14:48:38 -0700 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | /* path manager event handlers */ |
| 47 | |
| 48 | void mptcp_pm_new_connection(struct mptcp_sock *msk, int server_side) |
| 49 | { |
| 50 | struct mptcp_pm_data *pm = &msk->pm; |
| 51 | |
| 52 | pr_debug("msk=%p, token=%u side=%d", msk, msk->token, server_side); |
| 53 | |
| 54 | WRITE_ONCE(pm->server_side, server_side); |
| 55 | } |
| 56 | |
| 57 | bool mptcp_pm_allow_new_subflow(struct mptcp_sock *msk) |
| 58 | { |
Peter Krystad | 926bdea | 2020-03-27 14:48:41 -0700 | [diff] [blame] | 59 | struct mptcp_pm_data *pm = &msk->pm; |
Geliang Tang | f58f065 | 2020-09-24 08:29:53 +0800 | [diff] [blame] | 60 | int ret = 0; |
Peter Krystad | 926bdea | 2020-03-27 14:48:41 -0700 | [diff] [blame] | 61 | |
| 62 | pr_debug("msk=%p subflows=%d max=%d allow=%d", msk, pm->subflows, |
| 63 | pm->subflows_max, READ_ONCE(pm->accept_subflow)); |
| 64 | |
| 65 | /* try to avoid acquiring the lock below */ |
| 66 | if (!READ_ONCE(pm->accept_subflow)) |
| 67 | return false; |
| 68 | |
| 69 | spin_lock_bh(&pm->lock); |
Geliang Tang | f58f065 | 2020-09-24 08:29:53 +0800 | [diff] [blame] | 70 | if (READ_ONCE(pm->accept_subflow)) { |
| 71 | ret = pm->subflows < pm->subflows_max; |
| 72 | if (ret && ++pm->subflows == pm->subflows_max) |
| 73 | WRITE_ONCE(pm->accept_subflow, false); |
| 74 | } |
Peter Krystad | 926bdea | 2020-03-27 14:48:41 -0700 | [diff] [blame] | 75 | spin_unlock_bh(&pm->lock); |
| 76 | |
| 77 | return ret; |
| 78 | } |
| 79 | |
| 80 | /* return true if the new status bit is currently cleared, that is, this event |
| 81 | * can be server, eventually by an already scheduled work |
| 82 | */ |
| 83 | static bool mptcp_pm_schedule_work(struct mptcp_sock *msk, |
| 84 | enum mptcp_pm_status new_status) |
| 85 | { |
| 86 | pr_debug("msk=%p status=%x new=%lx", msk, msk->pm.status, |
| 87 | BIT(new_status)); |
| 88 | if (msk->pm.status & BIT(new_status)) |
| 89 | return false; |
| 90 | |
| 91 | msk->pm.status |= BIT(new_status); |
Florian Westphal | b416268 | 2020-07-07 14:40:48 +0200 | [diff] [blame] | 92 | if (schedule_work(&msk->work)) |
Peter Krystad | 926bdea | 2020-03-27 14:48:41 -0700 | [diff] [blame] | 93 | sock_hold((struct sock *)msk); |
| 94 | return true; |
Peter Krystad | 1b1c7a0 | 2020-03-27 14:48:38 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | void mptcp_pm_fully_established(struct mptcp_sock *msk) |
| 98 | { |
Peter Krystad | 926bdea | 2020-03-27 14:48:41 -0700 | [diff] [blame] | 99 | struct mptcp_pm_data *pm = &msk->pm; |
| 100 | |
Peter Krystad | 1b1c7a0 | 2020-03-27 14:48:38 -0700 | [diff] [blame] | 101 | pr_debug("msk=%p", msk); |
Peter Krystad | 926bdea | 2020-03-27 14:48:41 -0700 | [diff] [blame] | 102 | |
| 103 | /* try to avoid acquiring the lock below */ |
| 104 | if (!READ_ONCE(pm->work_pending)) |
| 105 | return; |
| 106 | |
| 107 | spin_lock_bh(&pm->lock); |
| 108 | |
| 109 | if (READ_ONCE(pm->work_pending)) |
| 110 | mptcp_pm_schedule_work(msk, MPTCP_PM_ESTABLISHED); |
| 111 | |
| 112 | spin_unlock_bh(&pm->lock); |
Peter Krystad | 1b1c7a0 | 2020-03-27 14:48:38 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | void mptcp_pm_connection_closed(struct mptcp_sock *msk) |
| 116 | { |
| 117 | pr_debug("msk=%p", msk); |
| 118 | } |
| 119 | |
| 120 | void mptcp_pm_subflow_established(struct mptcp_sock *msk, |
| 121 | struct mptcp_subflow_context *subflow) |
| 122 | { |
Peter Krystad | 926bdea | 2020-03-27 14:48:41 -0700 | [diff] [blame] | 123 | struct mptcp_pm_data *pm = &msk->pm; |
| 124 | |
Peter Krystad | 1b1c7a0 | 2020-03-27 14:48:38 -0700 | [diff] [blame] | 125 | pr_debug("msk=%p", msk); |
Peter Krystad | 926bdea | 2020-03-27 14:48:41 -0700 | [diff] [blame] | 126 | |
| 127 | if (!READ_ONCE(pm->work_pending)) |
| 128 | return; |
| 129 | |
| 130 | spin_lock_bh(&pm->lock); |
| 131 | |
| 132 | if (READ_ONCE(pm->work_pending)) |
| 133 | mptcp_pm_schedule_work(msk, MPTCP_PM_SUBFLOW_ESTABLISHED); |
| 134 | |
| 135 | spin_unlock_bh(&pm->lock); |
Peter Krystad | 1b1c7a0 | 2020-03-27 14:48:38 -0700 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | void mptcp_pm_subflow_closed(struct mptcp_sock *msk, u8 id) |
| 139 | { |
| 140 | pr_debug("msk=%p", msk); |
| 141 | } |
| 142 | |
| 143 | void mptcp_pm_add_addr_received(struct mptcp_sock *msk, |
| 144 | const struct mptcp_addr_info *addr) |
| 145 | { |
Peter Krystad | 926bdea | 2020-03-27 14:48:41 -0700 | [diff] [blame] | 146 | struct mptcp_pm_data *pm = &msk->pm; |
| 147 | |
| 148 | pr_debug("msk=%p remote_id=%d accept=%d", msk, addr->id, |
| 149 | READ_ONCE(pm->accept_addr)); |
| 150 | |
Peter Krystad | 926bdea | 2020-03-27 14:48:41 -0700 | [diff] [blame] | 151 | spin_lock_bh(&pm->lock); |
| 152 | |
Geliang Tang | 6a6c05a | 2020-09-24 08:29:50 +0800 | [diff] [blame] | 153 | if (!READ_ONCE(pm->accept_addr)) |
| 154 | mptcp_pm_announce_addr(msk, addr, true); |
| 155 | else if (mptcp_pm_schedule_work(msk, MPTCP_PM_ADD_ADDR_RECEIVED)) |
Peter Krystad | 926bdea | 2020-03-27 14:48:41 -0700 | [diff] [blame] | 156 | pm->remote = *addr; |
| 157 | |
| 158 | spin_unlock_bh(&pm->lock); |
Peter Krystad | 1b1c7a0 | 2020-03-27 14:48:38 -0700 | [diff] [blame] | 159 | } |
| 160 | |
Geliang Tang | d0876b2 | 2020-09-24 08:29:49 +0800 | [diff] [blame] | 161 | void mptcp_pm_rm_addr_received(struct mptcp_sock *msk, u8 rm_id) |
| 162 | { |
| 163 | struct mptcp_pm_data *pm = &msk->pm; |
| 164 | |
| 165 | pr_debug("msk=%p remote_id=%d", msk, rm_id); |
| 166 | |
| 167 | spin_lock_bh(&pm->lock); |
| 168 | mptcp_pm_schedule_work(msk, MPTCP_PM_RM_ADDR_RECEIVED); |
| 169 | pm->rm_id = rm_id; |
| 170 | spin_unlock_bh(&pm->lock); |
| 171 | } |
| 172 | |
Peter Krystad | 1b1c7a0 | 2020-03-27 14:48:38 -0700 | [diff] [blame] | 173 | /* path manager helpers */ |
| 174 | |
Geliang Tang | f643b80 | 2020-09-24 08:29:47 +0800 | [diff] [blame] | 175 | bool mptcp_pm_add_addr_signal(struct mptcp_sock *msk, unsigned int remaining, |
Geliang Tang | 6a6c05a | 2020-09-24 08:29:50 +0800 | [diff] [blame] | 176 | struct mptcp_addr_info *saddr, bool *echo) |
Peter Krystad | 1b1c7a0 | 2020-03-27 14:48:38 -0700 | [diff] [blame] | 177 | { |
Peter Krystad | 926bdea | 2020-03-27 14:48:41 -0700 | [diff] [blame] | 178 | int ret = false; |
| 179 | |
| 180 | spin_lock_bh(&msk->pm.lock); |
| 181 | |
| 182 | /* double check after the lock is acquired */ |
Geliang Tang | f643b80 | 2020-09-24 08:29:47 +0800 | [diff] [blame] | 183 | if (!mptcp_pm_should_add_signal(msk)) |
Peter Krystad | 926bdea | 2020-03-27 14:48:41 -0700 | [diff] [blame] | 184 | goto out_unlock; |
| 185 | |
Matthieu Baerts | 456afe0 | 2020-10-03 17:36:56 +0200 | [diff] [blame] | 186 | *echo = READ_ONCE(msk->pm.add_addr_echo); |
| 187 | |
| 188 | if (remaining < mptcp_add_addr_len(msk->pm.local.family, *echo)) |
Peter Krystad | 926bdea | 2020-03-27 14:48:41 -0700 | [diff] [blame] | 189 | goto out_unlock; |
| 190 | |
| 191 | *saddr = msk->pm.local; |
Geliang Tang | f643b80 | 2020-09-24 08:29:47 +0800 | [diff] [blame] | 192 | WRITE_ONCE(msk->pm.add_addr_signal, false); |
Peter Krystad | 926bdea | 2020-03-27 14:48:41 -0700 | [diff] [blame] | 193 | ret = true; |
| 194 | |
| 195 | out_unlock: |
| 196 | spin_unlock_bh(&msk->pm.lock); |
| 197 | return ret; |
Peter Krystad | 1b1c7a0 | 2020-03-27 14:48:38 -0700 | [diff] [blame] | 198 | } |
| 199 | |
Geliang Tang | 5cb104a | 2020-09-24 08:29:48 +0800 | [diff] [blame] | 200 | bool mptcp_pm_rm_addr_signal(struct mptcp_sock *msk, unsigned int remaining, |
| 201 | u8 *rm_id) |
| 202 | { |
| 203 | int ret = false; |
| 204 | |
| 205 | spin_lock_bh(&msk->pm.lock); |
| 206 | |
| 207 | /* double check after the lock is acquired */ |
| 208 | if (!mptcp_pm_should_rm_signal(msk)) |
| 209 | goto out_unlock; |
| 210 | |
| 211 | if (remaining < TCPOLEN_MPTCP_RM_ADDR_BASE) |
| 212 | goto out_unlock; |
| 213 | |
| 214 | *rm_id = msk->pm.rm_id; |
| 215 | WRITE_ONCE(msk->pm.rm_addr_signal, false); |
| 216 | ret = true; |
| 217 | |
| 218 | out_unlock: |
| 219 | spin_unlock_bh(&msk->pm.lock); |
| 220 | return ret; |
| 221 | } |
| 222 | |
Peter Krystad | 1b1c7a0 | 2020-03-27 14:48:38 -0700 | [diff] [blame] | 223 | int mptcp_pm_get_local_id(struct mptcp_sock *msk, struct sock_common *skc) |
| 224 | { |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 225 | return mptcp_pm_nl_get_local_id(msk, skc); |
Peter Krystad | 1b1c7a0 | 2020-03-27 14:48:38 -0700 | [diff] [blame] | 226 | } |
| 227 | |
Peter Krystad | 1b1c7a0 | 2020-03-27 14:48:38 -0700 | [diff] [blame] | 228 | void mptcp_pm_data_init(struct mptcp_sock *msk) |
| 229 | { |
| 230 | msk->pm.add_addr_signaled = 0; |
| 231 | msk->pm.add_addr_accepted = 0; |
| 232 | msk->pm.local_addr_used = 0; |
| 233 | msk->pm.subflows = 0; |
Geliang Tang | 5cb104a | 2020-09-24 08:29:48 +0800 | [diff] [blame] | 234 | msk->pm.rm_id = 0; |
Peter Krystad | 1b1c7a0 | 2020-03-27 14:48:38 -0700 | [diff] [blame] | 235 | WRITE_ONCE(msk->pm.work_pending, false); |
Geliang Tang | f643b80 | 2020-09-24 08:29:47 +0800 | [diff] [blame] | 236 | WRITE_ONCE(msk->pm.add_addr_signal, false); |
Geliang Tang | 5cb104a | 2020-09-24 08:29:48 +0800 | [diff] [blame] | 237 | WRITE_ONCE(msk->pm.rm_addr_signal, false); |
Peter Krystad | 1b1c7a0 | 2020-03-27 14:48:38 -0700 | [diff] [blame] | 238 | WRITE_ONCE(msk->pm.accept_addr, false); |
| 239 | WRITE_ONCE(msk->pm.accept_subflow, false); |
Geliang Tang | 6a6c05a | 2020-09-24 08:29:50 +0800 | [diff] [blame] | 240 | WRITE_ONCE(msk->pm.add_addr_echo, false); |
Peter Krystad | 1b1c7a0 | 2020-03-27 14:48:38 -0700 | [diff] [blame] | 241 | msk->pm.status = 0; |
| 242 | |
| 243 | spin_lock_init(&msk->pm.lock); |
Geliang Tang | b6c0838 | 2020-09-24 08:29:54 +0800 | [diff] [blame] | 244 | INIT_LIST_HEAD(&msk->pm.anno_list); |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 245 | |
| 246 | mptcp_pm_nl_data_init(msk); |
Peter Krystad | 1b1c7a0 | 2020-03-27 14:48:38 -0700 | [diff] [blame] | 247 | } |
| 248 | |
Paolo Abeni | d39dcec | 2020-06-26 19:29:59 +0200 | [diff] [blame] | 249 | void __init mptcp_pm_init(void) |
Peter Krystad | 1b1c7a0 | 2020-03-27 14:48:38 -0700 | [diff] [blame] | 250 | { |
Paolo Abeni | 01cacb0 | 2020-03-27 14:48:51 -0700 | [diff] [blame] | 251 | mptcp_pm_nl_init(); |
Peter Krystad | 1b1c7a0 | 2020-03-27 14:48:38 -0700 | [diff] [blame] | 252 | } |