1 // SPDX-License-Identifier: GPL-2.0 2 /* Multipath TCP 3 * 4 * Copyright (c) 2019, Intel Corporation. 5 */ 6 #define pr_fmt(fmt) "MPTCP: " fmt 7 8 #include <linux/kernel.h> 9 #include <net/tcp.h> 10 #include <net/mptcp.h> 11 #include "protocol.h" 12 13 /* path manager command handlers */ 14 15 int mptcp_pm_announce_addr(struct mptcp_sock *msk, 16 const struct mptcp_addr_info *addr, 17 bool echo, bool port) 18 { 19 u8 add_addr = READ_ONCE(msk->pm.addr_signal); 20 21 pr_debug("msk=%p, local_id=%d", msk, addr->id); 22 23 lockdep_assert_held(&msk->pm.lock); 24 25 if (add_addr) { 26 pr_warn("addr_signal error, add_addr=%d", add_addr); 27 return -EINVAL; 28 } 29 30 msk->pm.local = *addr; 31 add_addr |= BIT(MPTCP_ADD_ADDR_SIGNAL); 32 if (echo) 33 add_addr |= BIT(MPTCP_ADD_ADDR_ECHO); 34 if (addr->family == AF_INET6) 35 add_addr |= BIT(MPTCP_ADD_ADDR_IPV6); 36 if (port) 37 add_addr |= BIT(MPTCP_ADD_ADDR_PORT); 38 WRITE_ONCE(msk->pm.addr_signal, add_addr); 39 return 0; 40 } 41 42 int mptcp_pm_remove_addr(struct mptcp_sock *msk, u8 local_id) 43 { 44 u8 rm_addr = READ_ONCE(msk->pm.addr_signal); 45 46 pr_debug("msk=%p, local_id=%d", msk, local_id); 47 48 if (rm_addr) { 49 pr_warn("addr_signal error, rm_addr=%d", rm_addr); 50 return -EINVAL; 51 } 52 53 msk->pm.rm_id = local_id; 54 rm_addr |= BIT(MPTCP_RM_ADDR_SIGNAL); 55 WRITE_ONCE(msk->pm.addr_signal, rm_addr); 56 return 0; 57 } 58 59 int mptcp_pm_remove_subflow(struct mptcp_sock *msk, u8 local_id) 60 { 61 pr_debug("msk=%p, local_id=%d", msk, local_id); 62 63 spin_lock_bh(&msk->pm.lock); 64 mptcp_pm_nl_rm_subflow_received(msk, local_id); 65 spin_unlock_bh(&msk->pm.lock); 66 return 0; 67 } 68 69 /* path manager event handlers */ 70 71 void mptcp_pm_new_connection(struct mptcp_sock *msk, int server_side) 72 { 73 struct mptcp_pm_data *pm = &msk->pm; 74 75 pr_debug("msk=%p, token=%u side=%d", msk, msk->token, server_side); 76 77 WRITE_ONCE(pm->server_side, server_side); 78 } 79 80 bool mptcp_pm_allow_new_subflow(struct mptcp_sock *msk) 81 { 82 struct mptcp_pm_data *pm = &msk->pm; 83 unsigned int subflows_max; 84 int ret = 0; 85 86 subflows_max = mptcp_pm_get_subflows_max(msk); 87 88 pr_debug("msk=%p subflows=%d max=%d allow=%d", msk, pm->subflows, 89 subflows_max, READ_ONCE(pm->accept_subflow)); 90 91 /* try to avoid acquiring the lock below */ 92 if (!READ_ONCE(pm->accept_subflow)) 93 return false; 94 95 spin_lock_bh(&pm->lock); 96 if (READ_ONCE(pm->accept_subflow)) { 97 ret = pm->subflows < subflows_max; 98 if (ret && ++pm->subflows == subflows_max) 99 WRITE_ONCE(pm->accept_subflow, false); 100 } 101 spin_unlock_bh(&pm->lock); 102 103 return ret; 104 } 105 106 /* return true if the new status bit is currently cleared, that is, this event 107 * can be server, eventually by an already scheduled work 108 */ 109 static bool mptcp_pm_schedule_work(struct mptcp_sock *msk, 110 enum mptcp_pm_status new_status) 111 { 112 pr_debug("msk=%p status=%x new=%lx", msk, msk->pm.status, 113 BIT(new_status)); 114 if (msk->pm.status & BIT(new_status)) 115 return false; 116 117 msk->pm.status |= BIT(new_status); 118 mptcp_schedule_work((struct sock *)msk); 119 return true; 120 } 121 122 void mptcp_pm_fully_established(struct mptcp_sock *msk) 123 { 124 struct mptcp_pm_data *pm = &msk->pm; 125 126 pr_debug("msk=%p", msk); 127 128 /* try to avoid acquiring the lock below */ 129 if (!READ_ONCE(pm->work_pending)) 130 return; 131 132 spin_lock_bh(&pm->lock); 133 134 /* mptcp_pm_fully_established() can be invoked by multiple 135 * racing paths - accept() and check_fully_established() 136 * be sure to serve this event only once. 137 */ 138 if (READ_ONCE(pm->work_pending) && 139 !(msk->pm.status & BIT(MPTCP_PM_ALREADY_ESTABLISHED))) 140 mptcp_pm_schedule_work(msk, MPTCP_PM_ESTABLISHED); 141 msk->pm.status |= BIT(MPTCP_PM_ALREADY_ESTABLISHED); 142 143 spin_unlock_bh(&pm->lock); 144 } 145 146 void mptcp_pm_connection_closed(struct mptcp_sock *msk) 147 { 148 pr_debug("msk=%p", msk); 149 } 150 151 void mptcp_pm_subflow_established(struct mptcp_sock *msk, 152 struct mptcp_subflow_context *subflow) 153 { 154 struct mptcp_pm_data *pm = &msk->pm; 155 156 pr_debug("msk=%p", msk); 157 158 if (!READ_ONCE(pm->work_pending)) 159 return; 160 161 spin_lock_bh(&pm->lock); 162 163 if (READ_ONCE(pm->work_pending)) 164 mptcp_pm_schedule_work(msk, MPTCP_PM_SUBFLOW_ESTABLISHED); 165 166 spin_unlock_bh(&pm->lock); 167 } 168 169 void mptcp_pm_subflow_closed(struct mptcp_sock *msk, u8 id) 170 { 171 pr_debug("msk=%p", msk); 172 } 173 174 void mptcp_pm_add_addr_received(struct mptcp_sock *msk, 175 const struct mptcp_addr_info *addr) 176 { 177 struct mptcp_pm_data *pm = &msk->pm; 178 179 pr_debug("msk=%p remote_id=%d accept=%d", msk, addr->id, 180 READ_ONCE(pm->accept_addr)); 181 182 spin_lock_bh(&pm->lock); 183 184 if (!READ_ONCE(pm->accept_addr)) { 185 mptcp_pm_announce_addr(msk, addr, true, addr->port); 186 mptcp_pm_add_addr_send_ack(msk); 187 } else if (mptcp_pm_schedule_work(msk, MPTCP_PM_ADD_ADDR_RECEIVED)) { 188 pm->remote = *addr; 189 } 190 191 spin_unlock_bh(&pm->lock); 192 } 193 194 void mptcp_pm_add_addr_send_ack(struct mptcp_sock *msk) 195 { 196 if (!mptcp_pm_should_add_signal(msk)) 197 return; 198 199 mptcp_pm_schedule_work(msk, MPTCP_PM_ADD_ADDR_SEND_ACK); 200 } 201 202 void mptcp_pm_rm_addr_received(struct mptcp_sock *msk, u8 rm_id) 203 { 204 struct mptcp_pm_data *pm = &msk->pm; 205 206 pr_debug("msk=%p remote_id=%d", msk, rm_id); 207 208 spin_lock_bh(&pm->lock); 209 mptcp_pm_schedule_work(msk, MPTCP_PM_RM_ADDR_RECEIVED); 210 pm->rm_id = rm_id; 211 spin_unlock_bh(&pm->lock); 212 } 213 214 void mptcp_pm_mp_prio_received(struct sock *sk, u8 bkup) 215 { 216 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk); 217 218 pr_debug("subflow->backup=%d, bkup=%d\n", subflow->backup, bkup); 219 subflow->backup = bkup; 220 } 221 222 /* path manager helpers */ 223 224 bool mptcp_pm_add_addr_signal(struct mptcp_sock *msk, unsigned int remaining, 225 struct mptcp_addr_info *saddr, bool *echo, bool *port) 226 { 227 int ret = false; 228 229 spin_lock_bh(&msk->pm.lock); 230 231 /* double check after the lock is acquired */ 232 if (!mptcp_pm_should_add_signal(msk)) 233 goto out_unlock; 234 235 *echo = mptcp_pm_should_add_signal_echo(msk); 236 *port = mptcp_pm_should_add_signal_port(msk); 237 238 if (remaining < mptcp_add_addr_len(msk->pm.local.family, *echo, *port)) 239 goto out_unlock; 240 241 *saddr = msk->pm.local; 242 WRITE_ONCE(msk->pm.addr_signal, 0); 243 ret = true; 244 245 out_unlock: 246 spin_unlock_bh(&msk->pm.lock); 247 return ret; 248 } 249 250 bool mptcp_pm_rm_addr_signal(struct mptcp_sock *msk, unsigned int remaining, 251 u8 *rm_id) 252 { 253 int ret = false; 254 255 spin_lock_bh(&msk->pm.lock); 256 257 /* double check after the lock is acquired */ 258 if (!mptcp_pm_should_rm_signal(msk)) 259 goto out_unlock; 260 261 if (remaining < TCPOLEN_MPTCP_RM_ADDR_BASE) 262 goto out_unlock; 263 264 *rm_id = msk->pm.rm_id; 265 WRITE_ONCE(msk->pm.addr_signal, 0); 266 ret = true; 267 268 out_unlock: 269 spin_unlock_bh(&msk->pm.lock); 270 return ret; 271 } 272 273 int mptcp_pm_get_local_id(struct mptcp_sock *msk, struct sock_common *skc) 274 { 275 return mptcp_pm_nl_get_local_id(msk, skc); 276 } 277 278 void mptcp_pm_data_init(struct mptcp_sock *msk) 279 { 280 msk->pm.add_addr_signaled = 0; 281 msk->pm.add_addr_accepted = 0; 282 msk->pm.local_addr_used = 0; 283 msk->pm.subflows = 0; 284 msk->pm.rm_id = 0; 285 WRITE_ONCE(msk->pm.work_pending, false); 286 WRITE_ONCE(msk->pm.addr_signal, 0); 287 WRITE_ONCE(msk->pm.accept_addr, false); 288 WRITE_ONCE(msk->pm.accept_subflow, false); 289 msk->pm.status = 0; 290 291 spin_lock_init(&msk->pm.lock); 292 INIT_LIST_HEAD(&msk->pm.anno_list); 293 294 mptcp_pm_nl_data_init(msk); 295 } 296 297 void __init mptcp_pm_init(void) 298 { 299 mptcp_pm_nl_init(); 300 } 301