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 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);
62 spin_unlock_bh(&msk->pm.lock);
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);
77 spin_unlock_bh(&msk->pm.lock);
81 int mptcp_userspace_pm_get_flags_and_ifindex_by_id(struct mptcp_sock *msk,
83 u8 *flags, int *ifindex)
85 struct mptcp_pm_addr_entry *entry, *match = NULL;
90 spin_lock_bh(&msk->pm.lock);
91 list_for_each_entry(entry, &msk->pm.userspace_pm_local_addr_list, list) {
92 if (id == entry->addr.id) {
97 spin_unlock_bh(&msk->pm.lock);
99 *flags = match->flags;
100 *ifindex = match->ifindex;
106 int mptcp_userspace_pm_get_local_id(struct mptcp_sock *msk,
107 struct mptcp_addr_info *skc)
109 struct mptcp_pm_addr_entry new_entry;
110 __be16 msk_sport = ((struct inet_sock *)
111 inet_sk((struct sock *)msk))->inet_sport;
113 memset(&new_entry, 0, sizeof(struct mptcp_pm_addr_entry));
114 new_entry.addr = *skc;
115 new_entry.addr.id = 0;
116 new_entry.flags = MPTCP_PM_ADDR_FLAG_IMPLICIT;
118 if (new_entry.addr.port == msk_sport)
119 new_entry.addr.port = 0;
121 return mptcp_userspace_pm_append_new_local_addr(msk, &new_entry);
124 int mptcp_nl_cmd_announce(struct sk_buff *skb, struct genl_info *info)
126 struct nlattr *token = info->attrs[MPTCP_PM_ATTR_TOKEN];
127 struct nlattr *addr = info->attrs[MPTCP_PM_ATTR_ADDR];
128 struct mptcp_pm_addr_entry addr_val;
129 struct mptcp_sock *msk;
133 if (!addr || !token) {
134 GENL_SET_ERR_MSG(info, "missing required inputs");
138 token_val = nla_get_u32(token);
140 msk = mptcp_token_get_sock(sock_net(skb->sk), token_val);
142 NL_SET_ERR_MSG_ATTR(info->extack, token, "invalid token");
146 if (!mptcp_pm_is_userspace(msk)) {
147 GENL_SET_ERR_MSG(info, "invalid request; userspace PM not selected");
151 err = mptcp_pm_parse_entry(addr, info, true, &addr_val);
153 GENL_SET_ERR_MSG(info, "error parsing local address");
157 if (addr_val.addr.id == 0 || !(addr_val.flags & MPTCP_PM_ADDR_FLAG_SIGNAL)) {
158 GENL_SET_ERR_MSG(info, "invalid addr id or flags");
162 err = mptcp_userspace_pm_append_new_local_addr(msk, &addr_val);
164 GENL_SET_ERR_MSG(info, "did not match address and id");
168 lock_sock((struct sock *)msk);
169 spin_lock_bh(&msk->pm.lock);
171 if (mptcp_pm_alloc_anno_list(msk, &addr_val)) {
172 mptcp_pm_announce_addr(msk, &addr_val.addr, false);
173 mptcp_pm_nl_addr_send_ack(msk);
176 spin_unlock_bh(&msk->pm.lock);
177 release_sock((struct sock *)msk);
181 sock_put((struct sock *)msk);
185 int mptcp_nl_cmd_remove(struct sk_buff *skb, struct genl_info *info)
187 struct nlattr *token = info->attrs[MPTCP_PM_ATTR_TOKEN];
188 struct nlattr *id = info->attrs[MPTCP_PM_ATTR_LOC_ID];
189 struct mptcp_pm_addr_entry *match = NULL;
190 struct mptcp_pm_addr_entry *entry;
191 struct mptcp_sock *msk;
192 LIST_HEAD(free_list);
198 GENL_SET_ERR_MSG(info, "missing required inputs");
202 id_val = nla_get_u8(id);
203 token_val = nla_get_u32(token);
205 msk = mptcp_token_get_sock(sock_net(skb->sk), token_val);
207 NL_SET_ERR_MSG_ATTR(info->extack, token, "invalid token");
211 if (!mptcp_pm_is_userspace(msk)) {
212 GENL_SET_ERR_MSG(info, "invalid request; userspace PM not selected");
216 lock_sock((struct sock *)msk);
218 list_for_each_entry(entry, &msk->pm.userspace_pm_local_addr_list, list) {
219 if (entry->addr.id == id_val) {
226 GENL_SET_ERR_MSG(info, "address with specified id not found");
227 release_sock((struct sock *)msk);
231 list_move(&match->list, &free_list);
233 mptcp_pm_remove_addrs_and_subflows(msk, &free_list);
235 release_sock((struct sock *)msk);
237 list_for_each_entry_safe(match, entry, &free_list, list) {
238 sock_kfree_s((struct sock *)msk, match, sizeof(*match));
243 sock_put((struct sock *)msk);
247 int mptcp_nl_cmd_sf_create(struct sk_buff *skb, struct genl_info *info)
249 struct nlattr *raddr = info->attrs[MPTCP_PM_ATTR_ADDR_REMOTE];
250 struct nlattr *token = info->attrs[MPTCP_PM_ATTR_TOKEN];
251 struct nlattr *laddr = info->attrs[MPTCP_PM_ATTR_ADDR];
252 struct mptcp_addr_info addr_r;
253 struct mptcp_addr_info addr_l;
254 struct mptcp_sock *msk;
259 if (!laddr || !raddr || !token) {
260 GENL_SET_ERR_MSG(info, "missing required inputs");
264 token_val = nla_get_u32(token);
266 msk = mptcp_token_get_sock(genl_info_net(info), token_val);
268 NL_SET_ERR_MSG_ATTR(info->extack, token, "invalid token");
272 if (!mptcp_pm_is_userspace(msk)) {
273 GENL_SET_ERR_MSG(info, "invalid request; userspace PM not selected");
277 err = mptcp_pm_parse_addr(laddr, info, &addr_l);
279 NL_SET_ERR_MSG_ATTR(info->extack, laddr, "error parsing local addr");
283 if (addr_l.id == 0) {
284 NL_SET_ERR_MSG_ATTR(info->extack, laddr, "missing local addr id");
288 err = mptcp_pm_parse_addr(raddr, info, &addr_r);
290 NL_SET_ERR_MSG_ATTR(info->extack, raddr, "error parsing remote addr");
294 sk = &msk->sk.icsk_inet.sk;
297 err = __mptcp_subflow_connect(sk, &addr_l, &addr_r);
302 sock_put((struct sock *)msk);
306 static struct sock *mptcp_nl_find_ssk(struct mptcp_sock *msk,
307 const struct mptcp_addr_info *local,
308 const struct mptcp_addr_info *remote)
310 struct mptcp_subflow_context *subflow;
312 if (local->family != remote->family)
315 mptcp_for_each_subflow(msk, subflow) {
316 const struct inet_sock *issk;
319 ssk = mptcp_subflow_tcp_sock(subflow);
321 if (local->family != ssk->sk_family)
326 switch (ssk->sk_family) {
328 if (issk->inet_saddr != local->addr.s_addr ||
329 issk->inet_daddr != remote->addr.s_addr)
332 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
334 const struct ipv6_pinfo *pinfo = inet6_sk(ssk);
336 if (!ipv6_addr_equal(&local->addr6, &pinfo->saddr) ||
337 !ipv6_addr_equal(&remote->addr6, &ssk->sk_v6_daddr))
346 if (issk->inet_sport == local->port &&
347 issk->inet_dport == remote->port)
354 int mptcp_nl_cmd_sf_destroy(struct sk_buff *skb, struct genl_info *info)
356 struct nlattr *raddr = info->attrs[MPTCP_PM_ATTR_ADDR_REMOTE];
357 struct nlattr *token = info->attrs[MPTCP_PM_ATTR_TOKEN];
358 struct nlattr *laddr = info->attrs[MPTCP_PM_ATTR_ADDR];
359 struct mptcp_addr_info addr_l;
360 struct mptcp_addr_info addr_r;
361 struct mptcp_sock *msk;
362 struct sock *sk, *ssk;
366 if (!laddr || !raddr || !token) {
367 GENL_SET_ERR_MSG(info, "missing required inputs");
371 token_val = nla_get_u32(token);
373 msk = mptcp_token_get_sock(genl_info_net(info), token_val);
375 NL_SET_ERR_MSG_ATTR(info->extack, token, "invalid token");
379 if (!mptcp_pm_is_userspace(msk)) {
380 GENL_SET_ERR_MSG(info, "invalid request; userspace PM not selected");
384 err = mptcp_pm_parse_addr(laddr, info, &addr_l);
386 NL_SET_ERR_MSG_ATTR(info->extack, laddr, "error parsing local addr");
390 err = mptcp_pm_parse_addr(raddr, info, &addr_r);
392 NL_SET_ERR_MSG_ATTR(info->extack, raddr, "error parsing remote addr");
396 if (addr_l.family != addr_r.family) {
397 GENL_SET_ERR_MSG(info, "address families do not match");
401 if (!addr_l.port || !addr_r.port) {
402 GENL_SET_ERR_MSG(info, "missing local or remote port");
406 sk = &msk->sk.icsk_inet.sk;
408 ssk = mptcp_nl_find_ssk(msk, &addr_l, &addr_r);
410 struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
412 mptcp_subflow_shutdown(sk, ssk, RCV_SHUTDOWN | SEND_SHUTDOWN);
413 mptcp_close_ssk(sk, ssk, subflow);
414 MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_RMSUBFLOW);
422 sock_put((struct sock *)msk);
426 int mptcp_userspace_pm_set_flags(struct net *net, struct nlattr *token,
427 struct mptcp_pm_addr_entry *loc,
428 struct mptcp_pm_addr_entry *rem, u8 bkup)
430 struct mptcp_sock *msk;
434 token_val = nla_get_u32(token);
436 msk = mptcp_token_get_sock(net, token_val);
440 if (!mptcp_pm_is_userspace(msk))
443 if (loc->addr.family == AF_UNSPEC ||
444 rem->addr.family == AF_UNSPEC)
447 lock_sock((struct sock *)msk);
448 ret = mptcp_pm_nl_mp_prio_send_ack(msk, &loc->addr, &rem->addr, bkup);
449 release_sock((struct sock *)msk);
452 sock_put((struct sock *)msk);