1 // SPDX-License-Identifier: GPL-2.0
4 * Copyright (c) 2022, Intel Corporation.
10 void mptcp_free_local_addr_list(struct mptcp_sock *msk)
12 struct mptcp_pm_addr_entry *entry, *tmp;
13 struct sock *sk = (struct sock *)msk;
16 if (!mptcp_pm_is_userspace(msk))
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);
23 list_for_each_entry_safe(entry, tmp, &free_list, list) {
24 sock_kfree_s(sk, entry, sizeof(*entry));
28 static int mptcp_userspace_pm_append_new_local_addr(struct mptcp_sock *msk,
29 struct mptcp_pm_addr_entry *entry)
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;
39 bitmap_zero(id_bitmap, MPTCP_PM_MAX_ADDR_ID + 1);
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) {
50 } else if (addr_match || id_match) {
53 __set_bit(e->addr.id, id_bitmap);
56 if (!match && !addr_match && !id_match) {
57 /* Memory for the entry is allocated from the
60 e = sock_kmalloc(sk, sizeof(*e), GFP_ATOMIC);
68 e->addr.id = find_next_zero_bit(id_bitmap,
69 MPTCP_PM_MAX_ADDR_ID + 1,
71 list_add_tail_rcu(&e->list, &msk->pm.userspace_pm_local_addr_list);
72 msk->pm.local_addr_used++;
79 spin_unlock_bh(&msk->pm.lock);
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.
88 static int mptcp_userspace_pm_delete_local_addr(struct mptcp_sock *msk,
89 struct mptcp_pm_addr_entry *addr)
91 struct mptcp_pm_addr_entry *entry, *tmp;
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).
98 list_del_rcu(&entry->list);
100 msk->pm.local_addr_used--;
108 int mptcp_userspace_pm_get_flags_and_ifindex_by_id(struct mptcp_sock *msk,
110 u8 *flags, int *ifindex)
112 struct mptcp_pm_addr_entry *entry, *match = NULL;
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) {
121 spin_unlock_bh(&msk->pm.lock);
123 *flags = match->flags;
124 *ifindex = match->ifindex;
130 int mptcp_userspace_pm_get_local_id(struct mptcp_sock *msk,
131 struct mptcp_addr_info *skc)
133 struct mptcp_pm_addr_entry new_entry;
134 __be16 msk_sport = ((struct inet_sock *)
135 inet_sk((struct sock *)msk))->inet_sport;
137 memset(&new_entry, 0, sizeof(struct mptcp_pm_addr_entry));
138 new_entry.addr = *skc;
139 new_entry.addr.id = 0;
140 new_entry.flags = MPTCP_PM_ADDR_FLAG_IMPLICIT;
142 if (new_entry.addr.port == msk_sport)
143 new_entry.addr.port = 0;
145 return mptcp_userspace_pm_append_new_local_addr(msk, &new_entry);
148 int mptcp_nl_cmd_announce(struct sk_buff *skb, struct genl_info *info)
150 struct nlattr *token = info->attrs[MPTCP_PM_ATTR_TOKEN];
151 struct nlattr *addr = info->attrs[MPTCP_PM_ATTR_ADDR];
152 struct mptcp_pm_addr_entry addr_val;
153 struct mptcp_sock *msk;
157 if (!addr || !token) {
158 GENL_SET_ERR_MSG(info, "missing required inputs");
162 token_val = nla_get_u32(token);
164 msk = mptcp_token_get_sock(sock_net(skb->sk), token_val);
166 NL_SET_ERR_MSG_ATTR(info->extack, token, "invalid token");
170 if (!mptcp_pm_is_userspace(msk)) {
171 GENL_SET_ERR_MSG(info, "invalid request; userspace PM not selected");
175 err = mptcp_pm_parse_entry(addr, info, true, &addr_val);
177 GENL_SET_ERR_MSG(info, "error parsing local address");
181 if (addr_val.addr.id == 0 || !(addr_val.flags & MPTCP_PM_ADDR_FLAG_SIGNAL)) {
182 GENL_SET_ERR_MSG(info, "invalid addr id or flags");
187 err = mptcp_userspace_pm_append_new_local_addr(msk, &addr_val);
189 GENL_SET_ERR_MSG(info, "did not match address and id");
193 lock_sock((struct sock *)msk);
194 spin_lock_bh(&msk->pm.lock);
196 if (mptcp_pm_alloc_anno_list(msk, &addr_val)) {
197 msk->pm.add_addr_signaled++;
198 mptcp_pm_announce_addr(msk, &addr_val.addr, false);
199 mptcp_pm_nl_addr_send_ack(msk);
202 spin_unlock_bh(&msk->pm.lock);
203 release_sock((struct sock *)msk);
207 sock_put((struct sock *)msk);
211 int mptcp_nl_cmd_remove(struct sk_buff *skb, struct genl_info *info)
213 struct nlattr *token = info->attrs[MPTCP_PM_ATTR_TOKEN];
214 struct nlattr *id = info->attrs[MPTCP_PM_ATTR_LOC_ID];
215 struct mptcp_pm_addr_entry *match = NULL;
216 struct mptcp_pm_addr_entry *entry;
217 struct mptcp_sock *msk;
218 LIST_HEAD(free_list);
224 GENL_SET_ERR_MSG(info, "missing required inputs");
228 id_val = nla_get_u8(id);
229 token_val = nla_get_u32(token);
231 msk = mptcp_token_get_sock(sock_net(skb->sk), token_val);
233 NL_SET_ERR_MSG_ATTR(info->extack, token, "invalid token");
237 if (!mptcp_pm_is_userspace(msk)) {
238 GENL_SET_ERR_MSG(info, "invalid request; userspace PM not selected");
242 lock_sock((struct sock *)msk);
244 list_for_each_entry(entry, &msk->pm.userspace_pm_local_addr_list, list) {
245 if (entry->addr.id == id_val) {
252 GENL_SET_ERR_MSG(info, "address with specified id not found");
253 release_sock((struct sock *)msk);
257 list_move(&match->list, &free_list);
259 mptcp_pm_remove_addrs(msk, &free_list);
261 release_sock((struct sock *)msk);
263 list_for_each_entry_safe(match, entry, &free_list, list) {
264 sock_kfree_s((struct sock *)msk, match, sizeof(*match));
269 sock_put((struct sock *)msk);
273 int mptcp_nl_cmd_sf_create(struct sk_buff *skb, struct genl_info *info)
275 struct nlattr *raddr = info->attrs[MPTCP_PM_ATTR_ADDR_REMOTE];
276 struct nlattr *token = info->attrs[MPTCP_PM_ATTR_TOKEN];
277 struct nlattr *laddr = info->attrs[MPTCP_PM_ATTR_ADDR];
278 struct mptcp_pm_addr_entry local = { 0 };
279 struct mptcp_addr_info addr_r;
280 struct mptcp_addr_info addr_l;
281 struct mptcp_sock *msk;
286 if (!laddr || !raddr || !token) {
287 GENL_SET_ERR_MSG(info, "missing required inputs");
291 token_val = nla_get_u32(token);
293 msk = mptcp_token_get_sock(genl_info_net(info), token_val);
295 NL_SET_ERR_MSG_ATTR(info->extack, token, "invalid token");
299 if (!mptcp_pm_is_userspace(msk)) {
300 GENL_SET_ERR_MSG(info, "invalid request; userspace PM not selected");
304 err = mptcp_pm_parse_addr(laddr, info, &addr_l);
306 NL_SET_ERR_MSG_ATTR(info->extack, laddr, "error parsing local addr");
310 if (addr_l.id == 0) {
311 NL_SET_ERR_MSG_ATTR(info->extack, laddr, "missing local addr id");
316 err = mptcp_pm_parse_addr(raddr, info, &addr_r);
318 NL_SET_ERR_MSG_ATTR(info->extack, raddr, "error parsing remote addr");
322 sk = (struct sock *)msk;
324 if (!mptcp_pm_addr_families_match(sk, &addr_l, &addr_r)) {
325 GENL_SET_ERR_MSG(info, "families mismatch");
331 err = mptcp_userspace_pm_append_new_local_addr(msk, &local);
333 GENL_SET_ERR_MSG(info, "did not match address and id");
339 err = __mptcp_subflow_connect(sk, &addr_l, &addr_r);
343 spin_lock_bh(&msk->pm.lock);
345 mptcp_userspace_pm_delete_local_addr(msk, &local);
348 spin_unlock_bh(&msk->pm.lock);
351 sock_put((struct sock *)msk);
355 static struct sock *mptcp_nl_find_ssk(struct mptcp_sock *msk,
356 const struct mptcp_addr_info *local,
357 const struct mptcp_addr_info *remote)
359 struct mptcp_subflow_context *subflow;
361 if (local->family != remote->family)
364 mptcp_for_each_subflow(msk, subflow) {
365 const struct inet_sock *issk;
368 ssk = mptcp_subflow_tcp_sock(subflow);
370 if (local->family != ssk->sk_family)
375 switch (ssk->sk_family) {
377 if (issk->inet_saddr != local->addr.s_addr ||
378 issk->inet_daddr != remote->addr.s_addr)
381 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
383 const struct ipv6_pinfo *pinfo = inet6_sk(ssk);
385 if (!ipv6_addr_equal(&local->addr6, &pinfo->saddr) ||
386 !ipv6_addr_equal(&remote->addr6, &ssk->sk_v6_daddr))
395 if (issk->inet_sport == local->port &&
396 issk->inet_dport == remote->port)
403 int mptcp_nl_cmd_sf_destroy(struct sk_buff *skb, struct genl_info *info)
405 struct nlattr *raddr = info->attrs[MPTCP_PM_ATTR_ADDR_REMOTE];
406 struct nlattr *token = info->attrs[MPTCP_PM_ATTR_TOKEN];
407 struct nlattr *laddr = info->attrs[MPTCP_PM_ATTR_ADDR];
408 struct mptcp_addr_info addr_l;
409 struct mptcp_addr_info addr_r;
410 struct mptcp_sock *msk;
411 struct sock *sk, *ssk;
415 if (!laddr || !raddr || !token) {
416 GENL_SET_ERR_MSG(info, "missing required inputs");
420 token_val = nla_get_u32(token);
422 msk = mptcp_token_get_sock(genl_info_net(info), token_val);
424 NL_SET_ERR_MSG_ATTR(info->extack, token, "invalid token");
428 if (!mptcp_pm_is_userspace(msk)) {
429 GENL_SET_ERR_MSG(info, "invalid request; userspace PM not selected");
433 err = mptcp_pm_parse_addr(laddr, info, &addr_l);
435 NL_SET_ERR_MSG_ATTR(info->extack, laddr, "error parsing local addr");
439 err = mptcp_pm_parse_addr(raddr, info, &addr_r);
441 NL_SET_ERR_MSG_ATTR(info->extack, raddr, "error parsing remote addr");
445 if (addr_l.family != addr_r.family) {
446 GENL_SET_ERR_MSG(info, "address families do not match");
451 if (!addr_l.port || !addr_r.port) {
452 GENL_SET_ERR_MSG(info, "missing local or remote port");
457 sk = (struct sock *)msk;
459 ssk = mptcp_nl_find_ssk(msk, &addr_l, &addr_r);
461 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
462 struct mptcp_pm_addr_entry entry = { .addr = addr_l };
464 spin_lock_bh(&msk->pm.lock);
465 mptcp_userspace_pm_delete_local_addr(msk, &entry);
466 spin_unlock_bh(&msk->pm.lock);
467 mptcp_subflow_shutdown(sk, ssk, RCV_SHUTDOWN | SEND_SHUTDOWN);
468 mptcp_close_ssk(sk, ssk, subflow);
469 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RMSUBFLOW);
477 sock_put((struct sock *)msk);
481 int mptcp_userspace_pm_set_flags(struct net *net, struct nlattr *token,
482 struct mptcp_pm_addr_entry *loc,
483 struct mptcp_pm_addr_entry *rem, u8 bkup)
485 struct mptcp_sock *msk;
489 token_val = nla_get_u32(token);
491 msk = mptcp_token_get_sock(net, token_val);
495 if (!mptcp_pm_is_userspace(msk))
498 if (loc->addr.family == AF_UNSPEC ||
499 rem->addr.family == AF_UNSPEC)
502 lock_sock((struct sock *)msk);
503 ret = mptcp_pm_nl_mp_prio_send_ack(msk, &loc->addr, &rem->addr, bkup);
504 release_sock((struct sock *)msk);
507 sock_put((struct sock *)msk);