Skip to contentSkip to conversation

ADD_ADDR: ports support

PATCH Superseded Geliang Tang 2 participants 14 messages Nov 19, 2020 – Nov 23, 2020

Subsystems (3)

v5 of 10 Superseded Latest is v10 →

lore
Share & tools
lore

Geliang Tang

author
#0 lore raw

v5 cover ADD_ADDR: ports support

v5:
- use the per netns listening socket.
- First 8 patches in v4 had been merged to the export branch, drop them from this patchset.

v4:
- hold msk->pm.lock in mptcp_pm_sport_in_anno_list.
- Merge the patchset 'Squash to "ADD_ADDR: ports support v3"' into v4.

v3:
- add two new patches, 8 and 11
- add more IS_ENABLED(CONFIG_MPTCP_IPV6) in patch 2
- define TCPOLEN_MPTCP_ADD_ADDR_HMAC in patch 4
- add flags check in patch 10
- update the testcases

v2:
- change mptcp_out_options's port field in CPU bype order.
- keep mptcp_options_received's port field in CPU bype order.
- add two new patches to simplify ADD_ADDR suboption writing.
- update mptcp_add_addr_len helper use adding up size.
- add more commit messages.

v1:
This series is the first version of ADD_ADDR ports support. I have solved the listener problem which I mentioned at the meeting on 15th of October by adding a new listening socket from the userspace (see patch 8). Up to now this patchset works well.

Geliang Tang (10):
mptcp: create the listening socket
mptcp: add the pernet listening socket operations
mptcp: bind and listen for the new port
mptcp: add port number check for MP_JOIN
mptcp: add port number announced check
mptcp: add port number listened in kernel check
mptcp: deal with MPTCP_PM_ADDR_ATTR_PORT in PM netlink
selftests: mptcp: add port argument for pm_nl_ctl
mptcp: add the mibs for ADD_ADDR with port
selftests: mptcp: add testcases for ADD_ADDR with port

 net/mptcp/mib.c                               |   4 +
 net/mptcp/mib.h                               |   4 +
 net/mptcp/options.c                           |   4 +
 net/mptcp/pm_netlink.c                        |  80 +++++++++++
 net/mptcp/protocol.c                          |   5 +
 net/mptcp/protocol.h                          |  11 +-
 net/mptcp/subflow.c                           | 134 +++++++++++++++++-
 .../testing/selftests/net/mptcp/mptcp_join.sh | 118 ++++++++++++++-
 tools/testing/selftests/net/mptcp/pm_nl_ctl.c |  24 +++-
 9 files changed, 378 insertions(+), 6 deletions(-)
Signature · 1 line
2.26.2
author
#1 lore raw

PATCH v5 1/10 mptcp: create the listening socket

This patch added a new function named mptcp_create_listen_socket to create a listening socket in the kernel side.

Signed-off-by Geliang Tang <geliangtang(a)gmail.com>
---
 net/mptcp/protocol.h |  1 +
 net/mptcp/subflow.c  | 47 ++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 48 insertions(+)
net/mptcp/protocol.h +1 -0
index b5647a14ea54..7e40773f6b3d 100644
@@ -444,6 +444,7 @@ void mptcp_subflow_reset(struct sock *ssk);
 int __mptcp_subflow_connect(struct sock *sk, const struct mptcp_addr_info *loc,
 			    const struct mptcp_addr_info *remote);
 int mptcp_subflow_create_socket(struct sock *sk, struct socket **new_sock);
+int mptcp_create_listen_socket(struct sock *sk, struct socket **new_sock);
 
 static inline void mptcp_subflow_tcp_fallback(struct sock *sk,
 					      struct mptcp_subflow_context *ctx)
net/mptcp/subflow.c +47 -0

53 lines not on this page · raw message

Signature · 1 line
2.26.2
author
#2 lore raw

PATCH v5 2/10 mptcp: add the pernet listening socket operations

This patch added three new pernet listening socket operations:

mptcp_pm_alloc_listen_socket, mptcp_pm_free_listen_socket and mptcp_pm_get_listen_socket to alloc, free and get the pernet listening socket.

Signed-off-by Geliang Tang <geliangtang(a)gmail.com>
---
 net/mptcp/pm_netlink.c | 54 ++++++++++++++++++++++++++++++++++++++++++
 net/mptcp/protocol.c   |  5 ++++
 net/mptcp/protocol.h   |  5 ++++
 net/mptcp/subflow.c    |  2 +-
 4 files changed, 65 insertions(+), 1 deletion(-)
net/mptcp/pm_netlink.c +54 -0
index 2560c502356b..19d557df4684 100644
@@ -45,6 +45,8 @@ struct pm_nl_pernet {
 	unsigned int		add_addr_accept_max;
 	unsigned int		local_addr_max;
 	unsigned int		subflows_max;
+	struct socket		*listen_socket;
+	bool			sock_created;
 	unsigned int		next_id;
 };
 
@@ -306,6 +308,56 @@ void mptcp_pm_free_anno_list(struct mptcp_sock *msk)
 	}
 }
 
+int mptcp_pm_alloc_listen_socket(struct mptcp_sock *msk)
+{
+	struct sock *sk = (struct sock *)msk;
+	struct pm_nl_pernet *pernet;
+	bool need_create = false;
+
+	pernet = net_generic(sock_net(sk), pm_nl_pernet_id);
+
+	spin_lock_bh(&pernet->lock);
+	if (!pernet->sock_created) {
+		pernet->sock_created = true;
+		need_create = true;
+	}
+	spin_unlock_bh(&pernet->lock);
+
+	if (!need_create)
+		return 0;
+
+	return mptcp_create_listen_socket(sk, &pernet->listen_socket);
+}
+
+void mptcp_pm_free_listen_socket(struct mptcp_sock *msk)
+{
+	struct sock *sk = (struct sock *)msk;
+	struct pm_nl_pernet *pernet;
+
+	pernet = net_generic(sock_net(sk), pm_nl_pernet_id);
+
+	if (!pernet || !pernet->listen_socket || !pernet->listen_socket->sk)
+		return;
+
+	subflow_drop_ctx(pernet->listen_socket->sk);
+	sock_release(pernet->listen_socket);
+	pernet->listen_socket = NULL;
+	pernet->sock_created = false;
+}
+
+struct socket *mptcp_pm_get_listen_socket(struct mptcp_sock *msk)
+{
+	struct pm_nl_pernet *pernet;
+	struct sock *sk = (struct sock *)msk;
+
+	pernet = net_generic(sock_net(sk), pm_nl_pernet_id);
+
+	if (!pernet)
+		return NULL;
+
+	return pernet->listen_socket;
+}
+
 static void mptcp_pm_create_subflow_or_signal_addr(struct mptcp_sock *msk)
 {
 	struct mptcp_addr_info remote = { 0 };
@@ -885,6 +937,8 @@ static void __reset_counters(struct pm_nl_pernet *pernet)
 	pernet->add_addr_accept_max = 0;
 	pernet->local_addr_max = 0;
 	pernet->addrs = 0;
+	pernet->listen_socket = NULL;
+	pernet->sock_created = false;
 }
 
 static int mptcp_nl_cmd_flush_addrs(struct sk_buff *skb, struct genl_info *info)
net/mptcp/protocol.c +5 -0
index 7a0364860feb..1a9e895e953e 100644
@@ -2006,6 +2006,10 @@ static int mptcp_init_sock(struct sock *sk)
 	if (ret)
 		return ret;
 
+	ret = mptcp_pm_alloc_listen_socket(mptcp_sk(sk));
+	if (ret)
+		return ret;
+
 	sk_sockets_allocated_inc(sk);
 	sk->sk_rcvbuf = sock_net(sk)->ipv4.sysctl_tcp_rmem[1];
 	sk->sk_sndbuf = sock_net(sk)->ipv4.sysctl_tcp_wmem[1];
@@ -2389,6 +2393,7 @@ void mptcp_destroy_common(struct mptcp_sock *msk)
 {
 	skb_rbtree_purge(&msk->out_of_order_queue);
 	mptcp_token_destroy(msk);
+	mptcp_pm_free_listen_socket(msk);
 	mptcp_pm_free_anno_list(msk);
 }
 
net/mptcp/protocol.h +5 -0
index 7e40773f6b3d..e713a6d00c77 100644
@@ -444,6 +444,7 @@ void mptcp_subflow_reset(struct sock *ssk);
 int __mptcp_subflow_connect(struct sock *sk, const struct mptcp_addr_info *loc,
 			    const struct mptcp_addr_info *remote);
 int mptcp_subflow_create_socket(struct sock *sk, struct socket **new_sock);
+void subflow_drop_ctx(struct sock *ssk);
 int mptcp_create_listen_socket(struct sock *sk, struct socket **new_sock);
 
 static inline void mptcp_subflow_tcp_fallback(struct sock *sk,
@@ -534,6 +535,10 @@ int mptcp_pm_announce_addr(struct mptcp_sock *msk,
 int mptcp_pm_remove_addr(struct mptcp_sock *msk, u8 local_id);
 int mptcp_pm_remove_subflow(struct mptcp_sock *msk, u8 local_id);
 
+int mptcp_pm_alloc_listen_socket(struct mptcp_sock *msk);
+void mptcp_pm_free_listen_socket(struct mptcp_sock *msk);
+struct socket *mptcp_pm_get_listen_socket(struct mptcp_sock *msk);
+
 static inline bool mptcp_pm_should_add_signal(struct mptcp_sock *msk)
 {
 	return READ_ONCE(msk->pm.add_addr_signal) & BIT(MPTCP_ADD_ADDR_SIGNAL);
net/mptcp/subflow.c +1 -1
index 86eaf4b76147..53e3b82b1ade 100644
@@ -468,7 +468,7 @@ static void subflow_ulp_fallback(struct sock *sk,
 	tcp_sk(sk)->is_mptcp = 0;
 }
 
-static void subflow_drop_ctx(struct sock *ssk)
+void subflow_drop_ctx(struct sock *ssk)
 {
 	struct mptcp_subflow_context *ctx = mptcp_subflow_ctx(ssk);
 
Signature · 1 line
2.26.2
author
#3 lore raw

PATCH v5 3/10 mptcp: bind and listen for the new port

This patch called mptcp_pm_get_listen_socket to get the pernet listening socket, and binded the new port to the socket, then listened for the connection.

Signed-off-by Geliang Tang <geliangtang(a)gmail.com>
---
 net/mptcp/pm_netlink.c |  3 +++
 net/mptcp/protocol.h   |  1 +
 net/mptcp/subflow.c    | 44 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 48 insertions(+)
net/mptcp/pm_netlink.c +3 -0
index 19d557df4684..1f9583e92d72 100644
@@ -381,6 +381,9 @@ static void mptcp_pm_create_subflow_or_signal_addr(struct mptcp_sock *msk)
 			if (mptcp_pm_alloc_anno_list(msk, local)) {
 				msk->pm.add_addr_signaled++;
 				mptcp_pm_announce_addr(msk, &local->addr, false, local->addr.port);
+				spin_unlock_bh(&msk->pm.lock);
+				mptcp_bind_and_listen_new_port(msk, local->addr.port);
+				spin_lock_bh(&msk->pm.lock);
 				mptcp_pm_nl_add_addr_send_ack(msk);
 			}
 		} else {
net/mptcp/protocol.h +1 -0
index e713a6d00c77..d6ea2a6a7237 100644
@@ -446,6 +446,7 @@ int __mptcp_subflow_connect(struct sock *sk, const struct mptcp_addr_info *loc,
 int mptcp_subflow_create_socket(struct sock *sk, struct socket **new_sock);
 void subflow_drop_ctx(struct sock *ssk);
 int mptcp_create_listen_socket(struct sock *sk, struct socket **new_sock);
+void mptcp_bind_and_listen_new_port(struct mptcp_sock *msk, __be16 port);
 
 static inline void mptcp_subflow_tcp_fallback(struct sock *sk,
 					      struct mptcp_subflow_context *ctx)
net/mptcp/subflow.c +44 -0

50 lines not on this page · raw message

Signature · 1 line
2.26.2
author
#4 lore raw

PATCH v5 4/10 mptcp: add port number check for MP_JOIN

This patch added two new helpers, subflow_use_different_sport and subflow_use_different_dport, to check whether the subflow's source or destination port number is different from the msk's port number. When we received the MP_JOIN's SYN/SYNACK/ACK, we do these port number checks and print out the different port numbers.

Signed-off-by Geliang Tang <geliangtang(a)gmail.com>
---
 net/mptcp/subflow.c | 28 ++++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)
net/mptcp/subflow.c +28 -0
index 4a01b394ecf6..3ae365130a08 100644
@@ -112,6 +112,11 @@ static int __subflow_init_req(struct request_sock *req, const struct sock *sk_li
 	return 0;
 }
 
+static bool subflow_use_different_sport(struct mptcp_sock *msk, const struct sock *sk)
+{
+	return inet_sk(sk)->inet_sport != inet_sk((struct sock *)msk)->inet_sport;
+}
+
 static void subflow_init_req(struct request_sock *req,
 			     const struct sock *sk_listener,
 			     struct sk_buff *skb)
@@ -182,6 +187,12 @@ static void subflow_init_req(struct request_sock *req,
 
 		pr_debug("token=%u, remote_nonce=%u msk=%p", subflow_req->token,
 			 subflow_req->remote_nonce, subflow_req->msk);
+
+		if (subflow_use_different_sport(subflow_req->msk, sk_listener)) {
+			pr_debug("syn inet_sport=%d %d",
+				 ntohs(inet_sk(sk_listener)->inet_sport),
+				 ntohs(inet_sk((struct sock *)subflow_req->msk)->inet_sport));
+		}
 	}
 }
 
@@ -284,6 +295,11 @@ void mptcp_subflow_reset(struct sock *ssk)
 		sock_hold(sk);
 }
 
+static bool subflow_use_different_dport(struct mptcp_sock *msk, const struct sock *sk)
+{
+	return inet_sk(sk)->inet_dport != inet_sk((struct sock *)msk)->inet_dport;
+}
+
 static void subflow_finish_connect(struct sock *sk, const struct sk_buff *skb)
 {
 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
@@ -349,6 +365,12 @@ static void subflow_finish_connect(struct sock *sk, const struct sk_buff *skb)
 
 		subflow->mp_join = 1;
 		MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_JOINSYNACKRX);
+
+		if (subflow_use_different_dport(mptcp_sk(parent), sk)) {
+			pr_debug("synack inet_dport=%d %d",
+				 ntohs(inet_sk(sk)->inet_dport),
+				 ntohs(inet_sk(parent)->inet_dport));
+		}
 	} else if (mptcp_check_fallback(sk)) {
 fallback:
 		mptcp_rcv_space_init(mptcp_sk(parent), sk);
@@ -611,6 +633,12 @@ static struct sock *subflow_syn_recv_sock(const struct sock *sk,
 
 			SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_JOINACKRX);
 			tcp_rsk(req)->drop_req = true;
+
+			if (subflow_use_different_sport(owner, sk)) {
+				pr_debug("ack inet_sport=%d %d",
+					 ntohs(inet_sk(sk)->inet_sport),
+					 ntohs(inet_sk((struct sock *)owner)->inet_sport));
+			}
 		}
 	}
 
Signature · 1 line
2.26.2
author
#5 lore raw

PATCH v5 5/10 mptcp: add port number announced check

When we received the MP_JOIN's SYN/ACK, we do the port number checks. If the subflow's source port number is different, we use a new helper mptcp_pm_sport_in_anno_list to check whether this port number is announced. If it isn't, we need to abort this connection.

Signed-off-by Geliang Tang <geliangtang(a)gmail.com>
---
 net/mptcp/pm_netlink.c | 18 ++++++++++++++++++
 net/mptcp/protocol.h   |  1 +
 net/mptcp/subflow.c    |  6 ++++++
 3 files changed, 25 insertions(+)
net/mptcp/pm_netlink.c +18 -0
index 1f9583e92d72..27aff32516dd 100644
@@ -203,6 +203,24 @@ lookup_anno_list_by_saddr(struct mptcp_sock *msk,
 	return NULL;
 }
 
+bool mptcp_pm_sport_in_anno_list(struct mptcp_sock *msk, const struct sock *sk)
+{
+	struct mptcp_pm_add_entry *entry;
+	bool ret = false;
+
+	spin_lock_bh(&msk->pm.lock);
+	list_for_each_entry(entry, &msk->pm.anno_list, list) {
+		if (entry->addr.port == inet_sk(sk)->inet_sport) {
+			ret = true;
+			goto out;
+		}
+	}
+
+out:
+	spin_unlock_bh(&msk->pm.lock);
+	return ret;
+}
+
 static void mptcp_pm_add_timer(struct timer_list *timer)
 {
 	struct mptcp_pm_add_entry *entry = from_timer(entry, timer, add_timer);
net/mptcp/protocol.h +1 -0
index d6ea2a6a7237..ba0c5f628637 100644
@@ -526,6 +526,7 @@ void mptcp_pm_add_addr_received(struct mptcp_sock *msk,
 void mptcp_pm_add_addr_send_ack(struct mptcp_sock *msk);
 void mptcp_pm_rm_addr_received(struct mptcp_sock *msk, u8 rm_id);
 void mptcp_pm_free_anno_list(struct mptcp_sock *msk);
+bool mptcp_pm_sport_in_anno_list(struct mptcp_sock *msk, const struct sock *sk);
 struct mptcp_pm_add_entry *
 mptcp_pm_del_add_timer(struct mptcp_sock *msk,
 		       struct mptcp_addr_info *addr);
net/mptcp/subflow.c +6 -0
index 3ae365130a08..76fdaa57eb1d 100644
@@ -192,6 +192,10 @@ static void subflow_init_req(struct request_sock *req,
 			pr_debug("syn inet_sport=%d %d",
 				 ntohs(inet_sk(sk_listener)->inet_sport),
 				 ntohs(inet_sk((struct sock *)subflow_req->msk)->inet_sport));
+			if (!mptcp_pm_sport_in_anno_list(subflow_req->msk, sk_listener)) {
+				subflow_req->mp_join = 0;
+				return;
+			}
 		}
 	}
 }
@@ -638,6 +642,8 @@ static struct sock *subflow_syn_recv_sock(const struct sock *sk,
 				pr_debug("ack inet_sport=%d %d",
 					 ntohs(inet_sk(sk)->inet_sport),
 					 ntohs(inet_sk((struct sock *)owner)->inet_sport));
+				if (!mptcp_pm_sport_in_anno_list(owner, sk))
+					goto out;
 			}
 		}
 	}
Signature · 1 line
2.26.2
author
#6 lore raw

PATCH v5 6/10 mptcp: add port number listened in kernel check

This patch added a new flag listen_in_kernel, to mark the new port has been listened in the kernel side. If the new port has been listened from the userspace, kernel_bind() in mptcp_subflow_listen_in_kernel will fail, the listen_in_kernel flag will be not set, then the connection will be aborted in subflow_init_req.

Signed-off-by Geliang Tang <geliangtang(a)gmail.com>
---
 net/mptcp/protocol.h | 3 ++-
 net/mptcp/subflow.c  | 6 +++++-
 2 files changed, 7 insertions(+), 2 deletions(-)
net/mptcp/protocol.h +2 -1
index ba0c5f628637..d926deae8610 100644
@@ -371,7 +371,8 @@ struct mptcp_subflow_context {
 		backup : 1,
 		rx_eof : 1,
 		can_ack : 1,        /* only after processing the remote a key */
-		disposable : 1;	    /* ctx can be free at ulp release time */
+		disposable : 1,	    /* ctx can be free at ulp release time */
+		listen_in_kernel : 1;
 	enum mptcp_data_avail data_avail;
 	u32	remote_nonce;
 	u64	thmac;
net/mptcp/subflow.c +5 -1
index 76fdaa57eb1d..4939b396847a 100644
@@ -192,7 +192,8 @@ static void subflow_init_req(struct request_sock *req,
 			pr_debug("syn inet_sport=%d %d",
 				 ntohs(inet_sk(sk_listener)->inet_sport),
 				 ntohs(inet_sk((struct sock *)subflow_req->msk)->inet_sport));
-			if (!mptcp_pm_sport_in_anno_list(subflow_req->msk, sk_listener)) {
+			if (!mptcp_pm_sport_in_anno_list(subflow_req->msk, sk_listener) ||
+			    !listener->listen_in_kernel) {
 				subflow_req->mp_join = 0;
 				return;
 			}
@@ -1171,6 +1172,7 @@ int __mptcp_subflow_connect(struct sock *sk, const struct mptcp_addr_info *loc,
 
 void mptcp_bind_and_listen_new_port(struct mptcp_sock *msk, __be16 port)
 {
+	struct mptcp_subflow_context *subflow;
 	struct sock *sk = (struct sock *)msk;
 	struct mptcp_addr_info local;
 	struct sockaddr_storage addr;
@@ -1206,6 +1208,8 @@ void mptcp_bind_and_listen_new_port(struct mptcp_sock *msk, __be16 port)
 		goto err;
 	}
 
+	subflow = mptcp_subflow_ctx(listener->sk);
+	subflow->listen_in_kernel = 1;
 	pr_debug("bind_and_listen_new_port done");
 	return;
 
Signature · 1 line
2.26.2
author
#7 lore raw

PATCH v5 7/10 mptcp: deal with MPTCP_PM_ADDR_ATTR_PORT in PM netlink

This patch added the MPTCP_PM_ADDR_ATTR_PORT filling and parsing in PM netlink.

Signed-off-by Geliang Tang <geliangtang(a)gmail.com>
---
 net/mptcp/pm_netlink.c | 5 +++++
 1 file changed, 5 insertions(+)
net/mptcp/pm_netlink.c +5 -0
index 27aff32516dd..bb01b0f3e131 100644
@@ -797,6 +797,9 @@ static int mptcp_pm_parse_addr(struct nlattr *attr, struct genl_info *info,
 	if (tb[MPTCP_PM_ADDR_ATTR_FLAGS])
 		entry->addr.flags = nla_get_u32(tb[MPTCP_PM_ADDR_ATTR_FLAGS]);
 
+	if (tb[MPTCP_PM_ADDR_ATTR_PORT])
+		entry->addr.port = htons(nla_get_u16(tb[MPTCP_PM_ADDR_ATTR_PORT]));
+
 	return 0;
 }
 
@@ -985,6 +988,8 @@ static int mptcp_nl_fill_addr(struct sk_buff *skb,
 
 	if (nla_put_u16(skb, MPTCP_PM_ADDR_ATTR_FAMILY, addr->family))
 		goto nla_put_failure;
+	if (nla_put_u16(skb, MPTCP_PM_ADDR_ATTR_PORT, ntohs(addr->port)))
+		goto nla_put_failure;
 	if (nla_put_u8(skb, MPTCP_PM_ADDR_ATTR_ID, addr->id))
 		goto nla_put_failure;
 	if (nla_put_u32(skb, MPTCP_PM_ADDR_ATTR_FLAGS, entry->addr.flags))
Signature · 1 line
2.26.2
author
#8 lore raw

PATCH v5 8/10 selftests: mptcp: add port argument for pm_nl_ctl

This patch added a new argument for pm_nl_ctl tool. We can use it like this:

 # pm_nl_ctl add 10.0.2.1 flags signal port 10100
 # pm_nl_ctl dump
 id 1 flags signal 10.0.2.1 10100
Signed-off-by Geliang Tang <geliangtang(a)gmail.com>
---
 tools/testing/selftests/net/mptcp/pm_nl_ctl.c | 24 +++++++++++++++++--
 1 file changed, 22 insertions(+), 2 deletions(-)
tools/testing/selftests/net/mptcp/pm_nl_ctl.c +22 -2
index b24a2f17d415..cf654a57552b 100644
@@ -176,8 +176,8 @@ int add_addr(int fd, int pm_family, int argc, char *argv[])
 		  1024];
 	struct rtattr *rta, *nest;
 	struct nlmsghdr *nh;
+	u_int32_t flags = 0;
 	u_int16_t family;
-	u_int32_t flags;
 	int nest_start;
 	u_int8_t id;
 	int off = 0;
@@ -223,7 +223,6 @@ int add_addr(int fd, int pm_family, int argc, char *argv[])
 			char *tok, *str;
 
 			/* flags */
-			flags = 0;
 			if (++arg >= argc)
 				error(1, 0, " missing flags value");
 
@@ -271,6 +270,20 @@ int add_addr(int fd, int pm_family, int argc, char *argv[])
 			rta->rta_len = RTA_LENGTH(4);
 			memcpy(RTA_DATA(rta), &ifindex, 4);
 			off += NLMSG_ALIGN(rta->rta_len);
+		} else if (!strcmp(argv[arg], "port")) {
+			u_int16_t port;
+
+			if (++arg >= argc)
+				error(1, 0, " missing port value");
+			if (!(flags & MPTCP_PM_ADDR_FLAG_SIGNAL))
+				error(1, 0, " flags must be signal when using port");
+
+			port = atoi(argv[arg]);
+			rta = (void *)(data + off);
+			rta->rta_type = MPTCP_PM_ADDR_ATTR_PORT;
+			rta->rta_len = RTA_LENGTH(2);
+			memcpy(RTA_DATA(rta), &port, 2);
+			off += NLMSG_ALIGN(rta->rta_len);
 		} else
 			error(1, 0, "unknown keyword %s", argv[arg]);
 	}
@@ -323,6 +336,7 @@ int del_addr(int fd, int pm_family, int argc, char *argv[])
 static void print_addr(struct rtattr *attrs, int len)
 {
 	uint16_t family = 0;
+	uint16_t port = 0;
 	char str[1024];
 	uint32_t flags;
 	uint8_t id;
@@ -330,12 +344,16 @@ static void print_addr(struct rtattr *attrs, int len)
 	while (RTA_OK(attrs, len)) {
 		if (attrs->rta_type == MPTCP_PM_ADDR_ATTR_FAMILY)
 			memcpy(&family, RTA_DATA(attrs), 2);
+		if (attrs->rta_type == MPTCP_PM_ADDR_ATTR_PORT)
+			memcpy(&port, RTA_DATA(attrs), 2);
 		if (attrs->rta_type == MPTCP_PM_ADDR_ATTR_ADDR4) {
 			if (family != AF_INET)
 				error(1, errno, "wrong IP (v4) for family %d",
 				      family);
 			inet_ntop(AF_INET, RTA_DATA(attrs), str, sizeof(str));
 			printf("%s", str);
+			if (port)
+				printf(" %d", port);
 		}
 		if (attrs->rta_type == MPTCP_PM_ADDR_ATTR_ADDR6) {
 			if (family != AF_INET6)
@@ -343,6 +361,8 @@ static void print_addr(struct rtattr *attrs, int len)
 				      family);
 			inet_ntop(AF_INET6, RTA_DATA(attrs), str, sizeof(str));
 			printf("%s", str);
+			if (port)
+				printf(" %d", port);
 		}
 		if (attrs->rta_type == MPTCP_PM_ADDR_ATTR_ID) {
 			memcpy(&id, RTA_DATA(attrs), 1);
Signature · 1 line
2.26.2
author
#9 lore raw

PATCH v5 9/10 mptcp: add the mibs for ADD_ADDR with port

This patch added the mibs for ADD_ADDR with port:

MPTCP_MIB_PORTADD for receiving of the ADD_ADDR suboption with a port number. MPTCP_MIB_PORTSYNRX, MPTCP_MIB_PORTSYNACKRX, MPTCP_MIB_PORTACKRX, for receiving of the MP_JOIN's SYN or SYN/ACK or ACK with a port number which is different from the msk's port number.

Signed-off-by Geliang Tang <geliangtang(a)gmail.com>
---
 net/mptcp/mib.c     | 4 ++++
 net/mptcp/mib.h     | 4 ++++
 net/mptcp/options.c | 4 ++++
 net/mptcp/subflow.c | 3 +++
 4 files changed, 15 insertions(+)
net/mptcp/mib.c +4 -0
index 84d119436b22..5e8f7827cfe0 100644
@@ -29,6 +29,10 @@ static const struct snmp_mib mptcp_snmp_list[] = {
 	SNMP_MIB_ITEM("DuplicateData", MPTCP_MIB_DUPDATA),
 	SNMP_MIB_ITEM("AddAddr", MPTCP_MIB_ADDADDR),
 	SNMP_MIB_ITEM("EchoAdd", MPTCP_MIB_ECHOADD),
+	SNMP_MIB_ITEM("PortAdd", MPTCP_MIB_PORTADD),
+	SNMP_MIB_ITEM("PortSynRx", MPTCP_MIB_PORTSYNRX),
+	SNMP_MIB_ITEM("PortSynAckRx", MPTCP_MIB_PORTSYNACKRX),
+	SNMP_MIB_ITEM("PortAckRx", MPTCP_MIB_PORTACKRX),
 	SNMP_MIB_ITEM("RmAddr", MPTCP_MIB_RMADDR),
 	SNMP_MIB_ITEM("RmSubflow", MPTCP_MIB_RMSUBFLOW),
 	SNMP_MIB_SENTINEL
net/mptcp/mib.h +4 -0
index 47bcecce1106..b762941d492f 100644
@@ -22,6 +22,10 @@ enum linux_mptcp_mib_field {
 	MPTCP_MIB_DUPDATA,		/* Segments discarded due to duplicate DSS */
 	MPTCP_MIB_ADDADDR,		/* Received ADD_ADDR with echo-flag=0 */
 	MPTCP_MIB_ECHOADD,		/* Received ADD_ADDR with echo-flag=1 */
+	MPTCP_MIB_PORTADD,		/* Received ADD_ADDR with port */
+	MPTCP_MIB_PORTSYNRX,		/* Received a SYN + MP_JOIN with port */
+	MPTCP_MIB_PORTSYNACKRX,		/* Received a SYN/ACK + MP_JOIN with port */
+	MPTCP_MIB_PORTACKRX,		/* Received an ACK + MP_JOIN with port */
 	MPTCP_MIB_RMADDR,		/* Received RM_ADDR */
 	MPTCP_MIB_RMSUBFLOW,		/* Remove a subflow */
 	__MPTCP_MIB_MAX
net/mptcp/options.c +4 -0
index 41b8e2667395..c24bb74608ea 100644
@@ -956,6 +956,10 @@ void mptcp_incoming_options(struct sock *sk, struct sk_buff *skb)
 			mptcp_pm_del_add_timer(msk, &addr);
 			MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_ECHOADD);
 		}
+
+		if (mp_opt.port)
+			MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_PORTADD);
+
 		mp_opt.add_addr = 0;
 	}
 
net/mptcp/subflow.c +3 -0
index 4939b396847a..429e6e765c9e 100644
@@ -197,6 +197,7 @@ static void subflow_init_req(struct request_sock *req,
 				subflow_req->mp_join = 0;
 				return;
 			}
+			SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_PORTSYNRX);
 		}
 	}
 }
@@ -375,6 +376,7 @@ static void subflow_finish_connect(struct sock *sk, const struct sk_buff *skb)
 			pr_debug("synack inet_dport=%d %d",
 				 ntohs(inet_sk(sk)->inet_dport),
 				 ntohs(inet_sk(parent)->inet_dport));
+			MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_PORTSYNACKRX);
 		}
 	} else if (mptcp_check_fallback(sk)) {
 fallback:
@@ -645,6 +647,7 @@ static struct sock *subflow_syn_recv_sock(const struct sock *sk,
 					 ntohs(inet_sk((struct sock *)owner)->inet_sport));
 				if (!mptcp_pm_sport_in_anno_list(owner, sk))
 					goto out;
+				SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_PORTACKRX);
 			}
 		}
 	}
Signature · 1 line
2.26.2
author
#10 lore raw

PATCH v5 10/10 selftests: mptcp: add testcases for ADD_ADDR with port

This patch added five new testcases for ADD_ADDR with port:

In the first four testcases, the new port is listened in the kernel side. The connections in these testcases are accepted.

The last testcase's new port is listened in the userspace, so this connection will fail. For this testcase, we added a new argument for run_tests and do_transfer, if the with_port argument is set, we need to establish a new connection to listen for the new port in the userspace.

It also added the ADD_ADDR port related MIB counters checks in chk_add_nr.

The output looks like this:

24 signal address with port syn[ ok ] - synack[ ok ] - ack[ ok ]
add[ ok ] - echo [ ok ] - pt [ ok ] syn[ ok ] - synack[ ok ] - ack[ ok ]
25 subflow and signal with port syn[ ok ] - synack[ ok ] - ack[ ok ]
add[ ok ] - echo [ ok ] - pt [ ok ] syn[ ok ] - synack[ ok ] - ack[ ok ]
26 remove single address with port syn[ ok ] - synack[ ok ] - ack[ ok ]
add[ ok ] - echo [ ok ] - pt [ ok ] syn[ ok ] - synack[ ok ] - ack[ ok ]
rm [ ok ] - sf [ ok ]

Signed-off-by Geliang Tang <geliangtang(a)gmail.com>
---
 .../testing/selftests/net/mptcp/mptcp_join.sh | 118 +++++++++++++++++-
 1 file changed, 116 insertions(+), 2 deletions(-)
tools/testing/selftests/net/mptcp/mptcp_join.sh +116 -2
index 0eae628d1ffd..dd094e3e7f14 100755
@@ -212,6 +212,7 @@ do_transfer()
 	rm_nr_ns1="$7"
 	rm_nr_ns2="$8"
 	speed="$9"
+	with_port="${10}"
 
 	port=$((10000+$TEST_COUNT))
 	TEST_COUNT=$((TEST_COUNT+1))
@@ -253,6 +254,12 @@ do_transfer()
 		-s ${srv_proto} ${local_addr} < "$sin" > "$sout" &
 	spid=$!
 
+	if [ "$with_port" -gt 0 ];then
+		ip netns exec ${listener_ns} $mptcp_connect -t $timeout -l -p $with_port \
+			-s ${srv_proto} ${local_addr} < "$sin" > "$sout" &
+		spid_with_port=$!
+	fi
+
 	sleep 1
 
 	if [ "$test_link_fail" -eq 0 ];then
@@ -292,6 +299,11 @@ do_transfer()
 	wait $spid
 	rets=$?
 
+	if [ "$with_port" -gt 0 ];then
+		sleep 1
+		kill $spid_with_port
+	fi
+
 	if [ $capture -eq 1 ]; then
 	    sleep 1
 	    kill $cappid
@@ -348,6 +360,7 @@ run_tests()
 	rm_nr_ns1="${5:-0}"
 	rm_nr_ns2="${6:-0}"
 	speed="${7:-fast}"
+	with_port="${8:-0}"
 	lret=0
 	oldin=""
 
@@ -362,7 +375,7 @@ run_tests()
 	fi
 
 	do_transfer ${listener_ns} ${connector_ns} MPTCP MPTCP ${connect_addr} \
-		${test_linkfail} ${rm_nr_ns1} ${rm_nr_ns2} ${speed}
+		${test_linkfail} ${rm_nr_ns1} ${rm_nr_ns2} ${speed} ${with_port}
 	lret=$?
 
 	if [ "$test_linkfail" -eq 1 ];then
@@ -429,6 +442,10 @@ chk_add_nr()
 {
 	local add_nr=$1
 	local echo_nr=$2
+	local port_nr=${3:-0}
+	local syn_nr=${4:-$port_nr}
+	local syn_ack_nr=${5:-$port_nr}
+	local ack_nr=${6:-$port_nr}
 	local count
 	local dump_stats
 
@@ -451,7 +468,55 @@ chk_add_nr()
 		ret=1
 		dump_stats=1
 	else
-		echo "[ ok ]"
+		echo -n "[ ok ]"
+	fi
+
+	if [ $port_nr -gt 0 ]; then
+		echo -n " - pt "
+		count=`ip netns exec $ns2 nstat -as | grep MPTcpExtPortAdd | awk '{print $2}'`
+		[ -z "$count" ] && count=0
+		if [ "$count" != "$port_nr" ]; then
+			echo "[fail] got $count ADD_ADDR[s] with port expected $port_nr"
+			ret=1
+			dump_stats=1
+		else
+			echo "[ ok ]"
+		fi
+
+		printf "%-39s %s" " " "syn"
+		count=`ip netns exec $ns1 nstat -as | grep MPTcpExtPortSynRx | awk '{print $2}'`
+		[ -z "$count" ] && count=0
+		if [ "$count" != "$syn_nr" ]; then
+			echo "[fail] got $count JOIN[s] syn with port expected $syn_nr"
+			ret=1
+			dump_stats=1
+		else
+			echo -n "[ ok ]"
+		fi
+
+		echo -n " - synack"
+		count=`ip netns exec $ns2 nstat -as | grep MPTcpExtPortSynAckRx | awk '{print $2}'`
+		[ -z "$count" ] && count=0
+		if [ "$count" != "$syn_ack_nr" ]; then
+			echo "[fail] got $count JOIN[s] synack with port expected $syn_ack_nr"
+			ret=1
+			dump_stats=1
+		else
+			echo -n "[ ok ]"
+		fi
+
+		echo -n " - ack"
+		count=`ip netns exec $ns1 nstat -as | grep MPTcpExtPortAckRx | awk '{print $2}'`
+		[ -z "$count" ] && count=0
+		if [ "$count" != "$ack_nr" ]; then
+			echo "[fail] got $count JOIN[s] ack with port expected $ack_nr"
+			ret=1
+			dump_stats=1
+		else
+			echo "[ ok ]"
+		fi
+	else
+		echo ""
 	fi
 
 	if [ "${dump_stats}" = 1 ]; then
@@ -717,6 +782,55 @@ chk_join_nr "remove subflow and signal IPv6" 2 2 2
 chk_add_nr 1 1
 chk_rm_nr 1 1
 
+# signal address with port
+reset
+ip netns exec $ns1 ./pm_nl_ctl limits 0 1
+ip netns exec $ns2 ./pm_nl_ctl limits 1 1
+ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 flags signal port 10100
+run_tests $ns1 $ns2 10.0.1.1
+chk_join_nr "signal address with port" 1 1 1
+chk_add_nr 1 1 1
+
+# subflow and signal with port
+reset
+ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 flags signal port 10100
+ip netns exec $ns1 ./pm_nl_ctl limits 0 2
+ip netns exec $ns2 ./pm_nl_ctl limits 1 2
+ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 flags subflow
+run_tests $ns1 $ns2 10.0.1.1
+chk_join_nr "subflow and signal with port" 2 2 2
+chk_add_nr 1 1 1
+
+# single address with port, remove
+reset
+ip netns exec $ns1 ./pm_nl_ctl limits 0 1
+ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 flags signal port 10100
+ip netns exec $ns2 ./pm_nl_ctl limits 1 1
+run_tests $ns1 $ns2 10.0.1.1 0 1 0 slow
+chk_join_nr "remove single address with port" 1 1 1
+chk_add_nr 1 1 1
+chk_rm_nr 0 0
+
+# subflow and signal with port, remove
+reset
+ip netns exec $ns1 ./pm_nl_ctl limits 0 2
+ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 flags signal port 10100
+ip netns exec $ns2 ./pm_nl_ctl limits 1 2
+ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 flags subflow
+run_tests $ns1 $ns2 10.0.1.1 0 1 1 slow
+chk_join_nr "remove subflow and signal with port" 2 2 2
+chk_add_nr 1 1 1
+chk_rm_nr 1 1
+
+# signal address with port, userspace
+reset
+ip netns exec $ns1 ./pm_nl_ctl limits 0 1
+ip netns exec $ns2 ./pm_nl_ctl limits 1 1
+ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 flags signal port 10100
+run_tests $ns1 $ns2 10.0.1.1 0 0 0 fast 10100
+chk_join_nr "signal address with port, userspace" 1 0 0
+chk_add_nr 1 1 1 0 0 0
+
 # single subflow, syncookies
 reset_with_cookies
 ip netns exec $ns1 ./pm_nl_ctl limits 0 1
Signature · 1 line
2.26.2
#11 lore raw

[MPTCP] Re: [MPTCP][PATCH v5 mptcp-next 02/10] mptcp: add the pernet listening socket operations

In reply to Geliang Tang
Quoted 98 lines · Geliang Tang

This patch added three new pernet listening socket operations:

mptcp_pm_alloc_listen_socket, mptcp_pm_free_listen_socket and mptcp_pm_get_listen_socket to alloc, free and get the pernet listening socket.

Signed-off-by Geliang Tang <geliangtang(a)gmail.com>
---
 net/mptcp/pm_netlink.c | 54 ++++++++++++++++++++++++++++++++++++++++++
 net/mptcp/protocol.c   |  5 ++++
 net/mptcp/protocol.h   |  5 ++++
 net/mptcp/subflow.c    |  2 +-
 4 files changed, 65 insertions(+), 1 deletion(-)
net/mptcp/pm_netlink.c +54 -0
index 2560c502356b..19d557df4684 100644
@@ -45,6 +45,8 @@ struct pm_nl_pernet {
 	unsigned int		add_addr_accept_max;
 	unsigned int		local_addr_max;
 	unsigned int		subflows_max;
+	struct socket		*listen_socket;
+	bool			sock_created;
 	unsigned int		next_id;
 };
 
@@ -306,6 +308,56 @@ void mptcp_pm_free_anno_list(struct mptcp_sock *msk)
 	}
 }
 
+int mptcp_pm_alloc_listen_socket(struct mptcp_sock *msk)
+{
+	struct sock *sk = (struct sock *)msk;
+	struct pm_nl_pernet *pernet;
+	bool need_create = false;
+
+	pernet = net_generic(sock_net(sk), pm_nl_pernet_id);
+
+	spin_lock_bh(&pernet->lock);
+	if (!pernet->sock_created) {
+		pernet->sock_created = true;
+		need_create = true;
+	}
+	spin_unlock_bh(&pernet->lock);
+
+	if (!need_create)
+		return 0;
+
+	return mptcp_create_listen_socket(sk, &pernet->listen_socket);
+}
+
+void mptcp_pm_free_listen_socket(struct mptcp_sock *msk)
+{
+	struct sock *sk = (struct sock *)msk;
+	struct pm_nl_pernet *pernet;
+
+	pernet = net_generic(sock_net(sk), pm_nl_pernet_id);
+
+	if (!pernet || !pernet->listen_socket || !pernet->listen_socket->sk)
+		return;
+
+	subflow_drop_ctx(pernet->listen_socket->sk);
+	sock_release(pernet->listen_socket);
+	pernet->listen_socket = NULL;
+	pernet->sock_created = false;
+}
+
+struct socket *mptcp_pm_get_listen_socket(struct mptcp_sock *msk)
+{
+	struct pm_nl_pernet *pernet;
+	struct sock *sk = (struct sock *)msk;
+
+	pernet = net_generic(sock_net(sk), pm_nl_pernet_id);
+
+	if (!pernet)
+		return NULL;
+
+	return pernet->listen_socket;
+}
+
 static void mptcp_pm_create_subflow_or_signal_addr(struct mptcp_sock *msk)
 {
 	struct mptcp_addr_info remote = { 0 };
@@ -885,6 +937,8 @@ static void __reset_counters(struct pm_nl_pernet *pernet)
 	pernet->add_addr_accept_max = 0;
 	pernet->local_addr_max = 0;
 	pernet->addrs = 0;
+	pernet->listen_socket = NULL;
+	pernet->sock_created = false;
 }
 
 static int mptcp_nl_cmd_flush_addrs(struct sk_buff *skb, struct genl_info *info)
net/mptcp/protocol.c +0 -0
index 7a0364860feb..1a9e895e953e 100644

net/mptcp/protocol.c static int mptcp_init_sock(struct sock *sk) · line 2015

Quoted 10 lines
net/mptcp/protocol.c +4 -0
@@ -2006,6 +2006,10 @@ static int mptcp_init_sock(struct sock *sk)
 	if (ret)
 		return ret;
 
+	ret = mptcp_pm_alloc_listen_socket(mptcp_sk(sk));
+	if (ret)
+		return ret;
+
 	sk_sockets_allocated_inc(sk);
 	sk->sk_rcvbuf = sock_net(sk)->ipv4.sysctl_tcp_rmem[1];
 	sk->sk_sndbuf = sock_net(sk)->ipv4.sysctl_tcp_wmem[1];

I think the listening socket creation and bind should be done at endpoint creation time, that is, while handling the MPTCP_PM_CMD_ADD_ADDR netlink command.

The socket pointer itself could be stored inside the 'struct mptcp_pm_addr_entr'.

That way we will have the socket created only when needed, and deleted when unneeded. Additionally we could have several ports per netns, we will not have to bind the socket at run-time, and the user space could be notified in case of socket creation or bind failure.

Hopefully codying wise should not be a large change WRT the current version.

WDYT?

Paolo

author
#12 lore raw

[MPTCP] Re: [MPTCP][PATCH v5 mptcp-next 02/10] mptcp: add the pernet listening socket operations

Hi Paolo,

Thanks for your suggestions.

Quoted 103 lines · Paolo Abeni <pabeni(a)redhat.com> 于2020年11月20日周五 下午6:24写道:
Quoted 101 lines · Geliang Tang

This patch added three new pernet listening socket operations:

mptcp_pm_alloc_listen_socket, mptcp_pm_free_listen_socket and mptcp_pm_get_listen_socket to alloc, free and get the pernet listening socket.

Signed-off-by Geliang Tang <geliangtang(a)gmail.com>
---
 net/mptcp/pm_netlink.c | 54 ++++++++++++++++++++++++++++++++++++++++++
 net/mptcp/protocol.c   |  5 ++++
 net/mptcp/protocol.h   |  5 ++++
 net/mptcp/subflow.c    |  2 +-
 4 files changed, 65 insertions(+), 1 deletion(-)
net/mptcp/pm_netlink.c +54 -0
index 2560c502356b..19d557df4684 100644
@@ -45,6 +45,8 @@ struct pm_nl_pernet {
      unsigned int            add_addr_accept_max;
      unsigned int            local_addr_max;
      unsigned int            subflows_max;
+     struct socket           *listen_socket;
+     bool                    sock_created;
      unsigned int            next_id;
 };
 
@@ -306,6 +308,56 @@ void mptcp_pm_free_anno_list(struct mptcp_sock *msk)
      }
 }
 
+int mptcp_pm_alloc_listen_socket(struct mptcp_sock *msk)
+{
+     struct sock *sk = (struct sock *)msk;
+     struct pm_nl_pernet *pernet;
+     bool need_create = false;
+
+     pernet = net_generic(sock_net(sk), pm_nl_pernet_id);
+
+     spin_lock_bh(&pernet->lock);
+     if (!pernet->sock_created) {
+             pernet->sock_created = true;
+             need_create = true;
+     }
+     spin_unlock_bh(&pernet->lock);
+
+     if (!need_create)
+             return 0;
+
+     return mptcp_create_listen_socket(sk, &pernet->listen_socket);
+}
+
+void mptcp_pm_free_listen_socket(struct mptcp_sock *msk)
+{
+     struct sock *sk = (struct sock *)msk;
+     struct pm_nl_pernet *pernet;
+
+     pernet = net_generic(sock_net(sk), pm_nl_pernet_id);
+
+     if (!pernet || !pernet->listen_socket || !pernet->listen_socket->sk)
+             return;
+
+     subflow_drop_ctx(pernet->listen_socket->sk);
+     sock_release(pernet->listen_socket);
+     pernet->listen_socket = NULL;
+     pernet->sock_created = false;
+}
+
+struct socket *mptcp_pm_get_listen_socket(struct mptcp_sock *msk)
+{
+     struct pm_nl_pernet *pernet;
+     struct sock *sk = (struct sock *)msk;
+
+     pernet = net_generic(sock_net(sk), pm_nl_pernet_id);
+
+     if (!pernet)
+             return NULL;
+
+     return pernet->listen_socket;
+}
+
 static void mptcp_pm_create_subflow_or_signal_addr(struct mptcp_sock *msk)
 {
      struct mptcp_addr_info remote = { 0 };
@@ -885,6 +937,8 @@ static void __reset_counters(struct pm_nl_pernet *pernet)
      pernet->add_addr_accept_max = 0;
      pernet->local_addr_max = 0;
      pernet->addrs = 0;
+     pernet->listen_socket = NULL;
+     pernet->sock_created = false;
 }
 
 static int mptcp_nl_cmd_flush_addrs(struct sk_buff *skb, struct genl_info *info)
net/mptcp/protocol.c +0 -0
index 7a0364860feb..1a9e895e953e 100644
@@ -2006,6 +2006,10 @@ static int mptcp_init_sock(struct sock *sk)
      if (ret)
              return ret;
 
Quoted 7 lines

net/mptcp/protocol.c static int mptcp_init_sock(struct sock *sk) · line 2015

Quoted 7 lines
net/mptcp/protocol.c +4 -0
+     ret = mptcp_pm_alloc_listen_socket(mptcp_sk(sk));
+     if (ret)
+             return ret;
+
      sk_sockets_allocated_inc(sk);
      sk->sk_rcvbuf = sock_net(sk)->ipv4.sysctl_tcp_rmem[1];
      sk->sk_sndbuf = sock_net(sk)->ipv4.sysctl_tcp_wmem[1];

This v5 still has bugs in it, it can't work when we use announced multiple addresses with port, the following new test cases will fail:

# signal addresses with port
reset
ip netns exec $ns1 ./pm_nl_ctl limits 2 2
ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 flags signal port 10100
ip netns exec $ns1 ./pm_nl_ctl add 10.0.3.1 flags signal port 10100
ip netns exec $ns2 ./pm_nl_ctl limits 2 2
run_tests $ns1 $ns2 10.0.1.1
chk_join_nr "signal addresses with port" 2 2 2
chk_add_nr 2 2
# signal addresses with ports
reset
ip netns exec $ns1 ./pm_nl_ctl limits 2 2
ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 flags signal port 10100
ip netns exec $ns1 ./pm_nl_ctl add 10.0.3.1 flags signal port 10101
ip netns exec $ns2 ./pm_nl_ctl limits 2 2
run_tests $ns1 $ns2 10.0.1.1
chk_join_nr "signal addresses with ports" 2 2 2
chk_add_nr 2 2
Quoted 4 lines

I think the listening socket creation and bind should be done at endpoint creation time, that is, while handling the MPTCP_PM_CMD_ADD_ADDR netlink command.

Do you mean we can do it in mptcp_nl_cmd_add_addr? I had tested moving the creation and bind to mptcp_nl_cmd_add_addr, but it not work. This moment is much earlier for the socket creation. Since we want to create the subflow within a msk, but at this moment, the msk is not created yet.

I think maybe we can create and bind the listening socket in mptcp_pm_create_subflow_or_signal_addr. Do you think it's OK?

Quoted 3 lines

The socket pointer itself could be stored inside the 'struct mptcp_pm_addr_entr'.

We have defined another struct mptcp_pm_add_entry, it will be added in anno_list. I think we can stored the socket pointer inside it, instead of mptcp_pm_addr_entry. But in that case, the listening socket is not per netns, it is per announced address. There are several listening sockets per netns. Is this acceptable?

Please give me more suggestions. Thanks very much.

-Geliang

Quoted 13 lines

That way we will have the socket created only when needed, and deleted when unneeded. Additionally we could have several ports per netns, we will not have to bind the socket at run-time, and the user space could be notified in case of socket creation or bind failure.

Hopefully codying wise should not be a large change WRT the current version.

WDYT?

Paolo

#13 lore raw

[MPTCP] Re: [MPTCP][PATCH v5 mptcp-next 02/10] mptcp: add the pernet listening socket operations

Quoted 107 lines · Geliang Tang

Hi Paolo,

Thanks for your suggestions.

Quoted 102 lines · Paolo Abeni <pabeni(a)redhat.com> 于2020年11月20日周五 下午6:24写道:
Quoted 101 lines · Geliang Tang

This patch added three new pernet listening socket operations:

mptcp_pm_alloc_listen_socket, mptcp_pm_free_listen_socket and mptcp_pm_get_listen_socket to alloc, free and get the pernet listening socket.

Signed-off-by Geliang Tang <geliangtang(a)gmail.com>
---
 net/mptcp/pm_netlink.c | 54 ++++++++++++++++++++++++++++++++++++++++++
 net/mptcp/protocol.c   |  5 ++++
 net/mptcp/protocol.h   |  5 ++++
 net/mptcp/subflow.c    |  2 +-
 4 files changed, 65 insertions(+), 1 deletion(-)
net/mptcp/pm_netlink.c +54 -0
index 2560c502356b..19d557df4684 100644
@@ -45,6 +45,8 @@ struct pm_nl_pernet {
      unsigned int            add_addr_accept_max;
      unsigned int            local_addr_max;
      unsigned int            subflows_max;
+     struct socket           *listen_socket;
+     bool                    sock_created;
      unsigned int            next_id;
 };
 
@@ -306,6 +308,56 @@ void mptcp_pm_free_anno_list(struct mptcp_sock *msk)
      }
 }
 
+int mptcp_pm_alloc_listen_socket(struct mptcp_sock *msk)
+{
+     struct sock *sk = (struct sock *)msk;
+     struct pm_nl_pernet *pernet;
+     bool need_create = false;
+
+     pernet = net_generic(sock_net(sk), pm_nl_pernet_id);
+
+     spin_lock_bh(&pernet->lock);
+     if (!pernet->sock_created) {
+             pernet->sock_created = true;
+             need_create = true;
+     }
+     spin_unlock_bh(&pernet->lock);
+
+     if (!need_create)
+             return 0;
+
+     return mptcp_create_listen_socket(sk, &pernet->listen_socket);
+}
+
+void mptcp_pm_free_listen_socket(struct mptcp_sock *msk)
+{
+     struct sock *sk = (struct sock *)msk;
+     struct pm_nl_pernet *pernet;
+
+     pernet = net_generic(sock_net(sk), pm_nl_pernet_id);
+
+     if (!pernet || !pernet->listen_socket || !pernet->listen_socket->sk)
+             return;
+
+     subflow_drop_ctx(pernet->listen_socket->sk);
+     sock_release(pernet->listen_socket);
+     pernet->listen_socket = NULL;
+     pernet->sock_created = false;
+}
+
+struct socket *mptcp_pm_get_listen_socket(struct mptcp_sock *msk)
+{
+     struct pm_nl_pernet *pernet;
+     struct sock *sk = (struct sock *)msk;
+
+     pernet = net_generic(sock_net(sk), pm_nl_pernet_id);
+
+     if (!pernet)
+             return NULL;
+
+     return pernet->listen_socket;
+}
+
 static void mptcp_pm_create_subflow_or_signal_addr(struct mptcp_sock *msk)
 {
      struct mptcp_addr_info remote = { 0 };
@@ -885,6 +937,8 @@ static void __reset_counters(struct pm_nl_pernet *pernet)
      pernet->add_addr_accept_max = 0;
      pernet->local_addr_max = 0;
      pernet->addrs = 0;
+     pernet->listen_socket = NULL;
+     pernet->sock_created = false;
 }
 
 static int mptcp_nl_cmd_flush_addrs(struct sk_buff *skb, struct genl_info *info)
net/mptcp/protocol.c +0 -0
index 7a0364860feb..1a9e895e953e 100644
@@ -2006,6 +2006,10 @@ static int mptcp_init_sock(struct sock *sk)
      if (ret)
              return ret;
 
Quoted 10 lines
Quoted 7 lines

net/mptcp/protocol.c static int mptcp_init_sock(struct sock *sk) · line 2015

Quoted 7 lines
net/mptcp/protocol.c +4 -0
+     ret = mptcp_pm_alloc_listen_socket(mptcp_sk(sk));
+     if (ret)
+             return ret;
+
      sk_sockets_allocated_inc(sk);
      sk->sk_rcvbuf = sock_net(sk)->ipv4.sysctl_tcp_rmem[1];
      sk->sk_sndbuf = sock_net(sk)->ipv4.sysctl_tcp_wmem[1];

This v5 still has bugs in it, it can't work when we use announced multiple addresses with port, the following new test cases will fail:

[...]

Yep, with a single socket we can't accomodate multiple ports, we would need one socket per port (specifcally one socket per port-based endpoint), see below.

Quoted 8 lines

I think the listening socket creation and bind should be done at

Quoted 2 lines

endpoint creation time, that is, while handling the MPTCP_PM_CMD_ADD_ADDR netlink command.

Do you mean we can do it in mptcp_nl_cmd_add_addr? I had tested moving the creation and bind to mptcp_nl_cmd_add_addr, but it not work. This moment is much earlier for the socket creation. Since we want to create the subflow within a msk, but at this moment, the msk is not created yet.

The idea is that the listening socket is created _before_ we actually create the msk or subflow.

Thanks to the destination port number and token checks, already in place, that should be fine from security PoV: we will accept connection only after a msk is created and fully established.

Quoted 2 lines

I think maybe we can create and bind the listening socket in mptcp_pm_create_subflow_or_signal_addr. Do you think it's OK?

Doing the creation earlier, inside the scope of the user-space process doing the netlink configuration:
- reduces the overhead for subflow creation.
- allows closing the listening socket ASA the port endpoint is deleted.
- allows reporting to the user-space any port usage conflict and/or other socket creation errors.

It looks the better option to me.

Quoted 9 lines
Quoted 2 lines

The socket pointer itself could be stored inside the 'struct mptcp_pm_addr_entr'.

We have defined another struct mptcp_pm_add_entry, it will be added in
anno_list. I think we can stored the socket pointer inside it, instead of mptcp_pm_addr_entry. But in that case, the listening socket is not per netns, it is per announced address. There are several listening sockets per netns. Is this acceptable?

I mentioned the 'mptcp_pm_addr_entry' struct because it's per netns. Why can't we use it? Hopefully should be just a matter of moving the socket creation and initialization inside mptcp_nl_cmd_add_addr().

Thanks,

Paolo