ADD_ADDR: ports support
Subsystems (3)
Geliang Tang
v8 cover ADD_ADDR: ports support
v8:
- release the listening socket when removing the entry.
- initialize addr.port and lsk in mptcp_pm_nl_get_local_id().
- change kernel_listen's backlog to 1024.
- enable address_zero's use_port.
v7:
- use the MPTCP listening socket instead of TCP one
- release subflow_req->msk in subflow_init_req
- add mismatched port MIBs
- use sock_common in source_address
v6:
- create and bind the listening socket in mptcp_nl_cmd_add_addr.
- drop the patch "mptcp: add port number listened in kernel check" in v5.
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 (8):
mptcp: create the listening socket for new port
mptcp: add port number check for MP_JOIN
mptcp: add port number announced check
mptcp: enable use_port when invoke addresses_equal
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 | 6 + net/mptcp/mib.h | 6 + net/mptcp/options.c | 4 + net/mptcp/pm_netlink.c | 100 +++++++++++- net/mptcp/protocol.c | 2 +- net/mptcp/protocol.h | 4 + net/mptcp/subflow.c | 48 +++++- .../testing/selftests/net/mptcp/mptcp_join.sh | 148 +++++++++++++++++- tools/testing/selftests/net/mptcp/pm_nl_ctl.c | 24 ++- 9 files changed, 331 insertions(+), 11 deletions(-)
Signature · 1 line
2.29.2
Geliang Tang
PATCH v8 1/8 mptcp: create the listening socket for new port
This patch created a listening socket when an address with a port-number is added by PM netlink. Then binded the new port to the socket, and listened for the connection.
--- net/mptcp/pm_netlink.c | 64 ++++++++++++++++++++++++++++++++++++++++++ net/mptcp/protocol.c | 2 +- net/mptcp/protocol.h | 3 ++ net/mptcp/subflow.c | 4 +-- 4 files changed, 70 insertions(+), 3 deletions(-)
net/mptcp/pm_netlink.c +64 -0
@@ -26,6 +26,7 @@ struct mptcp_pm_addr_entry {struct list_head list;struct mptcp_addr_info addr;struct rcu_head rcu; +struct socket *lsk; };struct mptcp_pm_add_entry {
@@ -613,6 +614,53 @@ static int mptcp_pm_nl_append_new_local_addr(struct pm_nl_pernet *pernet,return ret; } +static int mptcp_pm_nl_create_listen_socket (struct sock *sk, +struct mptcp_pm_addr_entry *entry) +{ +struct sockaddr_storage addr; +struct mptcp_sock *msk; +struct socket *ssock; +int backlog =1024 ; +int err; + + err =sock_create_kern (sock_net (sk), entry->addr.family, +SOCK_STREAM ,IPPROTO_MPTCP , &entry->lsk); +if (err) +return err; + + msk =mptcp_sk (entry->lsk->sk); +if (!msk) { + err = -EINVAL ; + goto out; + } + + ssock =__mptcp_nmpc_socket (msk); +if (!ssock) { + err = -EINVAL ; + goto out; + } + +mptcp_info2sockaddr (&entry->addr, &addr); + err =kernel_bind (ssock, (struct sockaddr *)&addr, +sizeof (struct sockaddr_in )); +if (err) { +pr_warn ("kernel_bind error, err=%d" , err); + goto out; + } + + err =kernel_listen (ssock, backlog); +if (err) { +pr_warn ("kernel_listen error, err=%d" , err); + goto out; + } + +return 0 ; + +out: +sock_release (entry->lsk); +return err; +} +int mptcp_pm_nl_get_local_id (struct mptcp_sock *msk,struct sock_common *skc) {struct mptcp_pm_addr_entry *entry;
@@ -657,6 +705,8 @@ int mptcp_pm_nl_get_local_id(struct mptcp_sock *msk, struct sock_common *skc) entry->addr.ifindex =0 ; entry->addr.flags =0 ; entry->addr.id =0 ; + entry->addr.port =0 ; + entry->lsk =NULL ; ret =mptcp_pm_nl_append_new_local_addr (pernet, entry);if (ret <0 )kfree (entry);
@@ -808,9 +858,19 @@ static int mptcp_nl_cmd_add_addr(struct sk_buff *skb, struct genl_info *info) } *entry = addr; +if (entry->addr.port) { + ret =mptcp_pm_nl_create_listen_socket (skb->sk, entry); +if (ret) { +GENL_SET_ERR_MSG (info,"create listen socket error" ); +kfree (entry); +return ret; + } + } ret =mptcp_pm_nl_append_new_local_addr (pernet, entry);if (ret <0 ) {GENL_SET_ERR_MSG (info,"too many addresses or duplicate one" ); +if (entry->lsk) +sock_release (entry->lsk);kfree (entry);return ret; }
@@ -921,6 +981,8 @@ static int mptcp_nl_cmd_del_addr(struct sk_buff *skb, struct genl_info *info)spin_unlock_bh (&pernet->lock);mptcp_nl_remove_subflow_and_signal_addr (sock_net (skb->sk), &entry->addr); +if (entry->lsk) +sock_release (entry->lsk);kfree_rcu (entry, rcu);return ret;
@@ -934,6 +996,8 @@ static void __flush_addrs(struct net *net, struct list_head *list) cur =list_entry (list->next, struct mptcp_pm_addr_entry, list);mptcp_nl_remove_subflow_and_signal_addr (net, &cur->addr); +if (cur->lsk) +sock_release (cur->lsk);list_del_rcu (&cur->list);kfree_rcu (cur, rcu); }
net/mptcp/protocol.c +1 -1
@@ -49,7 +49,7 @@ static void __mptcp_check_send_data_fin(struct sock *sk);* completed yet or has failed, return the subflow socket. * Otherwise return NULL. */ -static struct socket *__mptcp_nmpc_socket (const struct mptcp_sock *msk) +struct socket *__mptcp_nmpc_socket (const struct mptcp_sock *msk) {if (!msk->subflow ||READ_ONCE (msk->can_ack))return NULL ;
net/mptcp/protocol.h +3 -0
@@ -473,11 +473,14 @@ void mptcp_subflow_shutdown(struct sock *sk, struct sock *ssk, int how);void __mptcp_close_ssk (struct sock *sk,struct sock *ssk,struct mptcp_subflow_context *subflow);void mptcp_subflow_reset (struct sock *ssk); +struct socket *__mptcp_nmpc_socket (const struct mptcp_sock *msk);/* called with sk socket lock held */ 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 mptcp_info2sockaddr (const struct mptcp_addr_info *info, +struct sockaddr_storage *addr);static inline void mptcp_subflow_tcp_fallback (struct sock *sk,struct mptcp_subflow_context *ctx)
net/mptcp/subflow.c +2 -2
@@ -1073,8 +1073,8 @@ void mptcpv6_handle_mapped(struct sock *sk, bool mapped) }#endif -static void mptcp_info2sockaddr (const struct mptcp_addr_info *info, -struct sockaddr_storage *addr) +void mptcp_info2sockaddr (const struct mptcp_addr_info *info, +struct sockaddr_storage *addr) {memset (addr,0 ,sizeof (*addr)); addr->ss_family = info->family;
Signature · 1 line
2.29.2
Geliang Tang
PATCH v8 2/8 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.
--- net/mptcp/subflow.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+)
net/mptcp/subflow.c +28 -0
@@ -112,6 +112,11 @@ static int __subflow_init_req(struct request_sock *req, const struct sock *sk_lireturn 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; +} +/* Init mptcp request socket. * * Returns an error code if a JOIN has failed and a TCP reset
@@ -184,6 +189,12 @@ static int subflow_init_req(struct request_sock *req,if (!subflow_req->msk)return -EPERM ; +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)); + } +if (unlikely (req->syncookie)) {if (mptcp_can_accept_new_subflow (subflow_req->msk))subflow_init_req_cookie_join_save (subflow_req, skb);
@@ -326,6 +337,11 @@ void mptcp_subflow_reset(struct sock *ssk)sock_put (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);
@@ -391,6 +407,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);
@@ -653,6 +675,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.29.2
Geliang Tang
PATCH v8 3/8 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.
--- net/mptcp/pm_netlink.c | 23 ++++++++++++++++++++++- net/mptcp/protocol.h | 1 + net/mptcp/subflow.c | 10 ++++++++++ 3 files changed, 33 insertions(+), 1 deletion(-)
net/mptcp/pm_netlink.c +22 -1
@@ -92,8 +92,8 @@ static bool address_zero(const struct mptcp_addr_info *addr)static void local_address (const struct sock_common *skc,struct mptcp_addr_info *addr) { - addr->port =0 ; addr->family = skc->skc_family; + addr->port =htons (skc->skc_num);if (addr->family ==AF_INET ) addr->addr.s_addr = skc->skc_rcv_saddr;#if IS_ENABLED (CONFIG_MPTCP_IPV6 )
@@ -204,6 +204,27 @@ 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; +struct mptcp_addr_info saddr; +bool ret = false; + +local_address ((struct sock_common *)sk, &saddr); + +spin_lock_bh (&msk->pm.lock); +list_for_each_entry (entry, &msk->pm.anno_list, list) { +if (addresses_equal (&entry->addr, &saddr, true)) { + 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
@@ -565,6 +565,7 @@ int mptcp_pm_nl_mp_prio_send_ack(struct mptcp_sock *msk,struct mptcp_addr_info *addr, u8 bkup);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 +10 -0
@@ -193,6 +193,14 @@ static int 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)) { +sock_put ((struct sock *)subflow_req->msk); +mptcp_token_destroy_request (req); + tcp_request_sock_ops.destructor (req); + subflow_req->msk =NULL ; + subflow_req->mp_join =0 ; +return -EPERM ; + } }if (unlikely (req->syncookie)) {
@@ -680,6 +688,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.29.2
Geliang Tang
PATCH v8 4/8 mptcp: enable use_port when invoke addresses_equal
When dealing with the addresses list local_addr_list or anno_list, we should enable the function addresses_equal's parameter use_port. And enable it in address_zero too.
--- net/mptcp/pm_netlink.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
net/mptcp/pm_netlink.c +4 -4
@@ -86,7 +86,7 @@ static bool address_zero(const struct mptcp_addr_info *addr)memset (&zero,0 ,sizeof (zero)); zero.family = addr->family; -return addresses_equal (addr, &zero, false); +return addresses_equal (addr, &zero, true); } static void local_address(const struct sock_common *skc,
@@ -126,7 +126,7 @@ static bool lookup_subflow_by_saddr(const struct list_head *list, skc = (struct sock_common *)mptcp_subflow_tcp_sock (subflow);local_address (skc, &cur); -if (addresses_equal (&cur, saddr, false)) +if (addresses_equal (&cur, saddr, saddr->port))return true; }
@@ -197,7 +197,7 @@ lookup_anno_list_by_saddr(struct mptcp_sock *msk,struct mptcp_pm_add_entry *entry;list_for_each_entry (entry, &msk->pm.anno_list, list) { -if (addresses_equal (&entry->addr, addr, false)) +if (addresses_equal (&entry->addr, addr, true))return entry; }
@@ -708,7 +708,7 @@ int mptcp_pm_nl_get_local_id(struct mptcp_sock *msk, struct sock_common *skc)rcu_read_lock ();list_for_each_entry_rcu (entry, &pernet->local_addr_list, list) { -if (addresses_equal (&entry->addr, &skc_local, false)) { +if (addresses_equal (&entry->addr, &skc_local, entry->addr.port)) { ret = entry->addr.id;break ; }
Signature · 1 line
2.29.2
Geliang Tang
PATCH v8 5/8 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.
--- net/mptcp/pm_netlink.c | 5 +++++ 1 file changed, 5 insertions(+)
net/mptcp/pm_netlink.c +5 -0
@@ -853,6 +853,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 ; }
@@ -1059,6 +1062,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.29.2
Geliang Tang
PATCH v8 6/8 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
--- 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
@@ -177,8 +177,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 ;
@@ -224,7 +224,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" );
@@ -272,6 +271,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]); }
@@ -324,6 +337,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;
@@ -331,12 +345,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 )
@@ -344,6 +362,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.29.2
Geliang Tang
PATCH v8 7/8 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.
MPTCP_MIB_MISMATCHPORTSYNRX and MPTCP_MIB_MISMATCHPORTACKRX, for receiving a SYN or ACK MP_JOIN with a mismatched port-number.
--- net/mptcp/mib.c | 6 ++++++ net/mptcp/mib.h | 6 ++++++ net/mptcp/options.c | 4 ++++ net/mptcp/subflow.c | 8 +++++++- 4 files changed, 23 insertions(+), 1 deletion(-)
net/mptcp/mib.c +6 -0
@@ -29,6 +29,12 @@ 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 ("MPJoinPortSynRx" ,MPTCP_MIB_JOINPORTSYNRX ), +SNMP_MIB_ITEM ("MPJoinPortSynAckRx" ,MPTCP_MIB_JOINPORTSYNACKRX ), +SNMP_MIB_ITEM ("MPJoinPortAckRx" ,MPTCP_MIB_JOINPORTACKRX ), +SNMP_MIB_ITEM ("MismatchPortSynRx" ,MPTCP_MIB_MISMATCHPORTSYNRX ), +SNMP_MIB_ITEM ("MismatchPortAckRx" ,MPTCP_MIB_MISMATCHPORTACKRX ),SNMP_MIB_ITEM ("RmAddr" ,MPTCP_MIB_RMADDR ),SNMP_MIB_ITEM ("RmSubflow" ,MPTCP_MIB_RMSUBFLOW ), SNMP_MIB_ITEM("MPPrioTx", MPTCP_MIB_MPPRIOTX),
net/mptcp/mib.h +6 -0
@@ -22,6 +22,12 @@ 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 a port-number */ +MPTCP_MIB_JOINPORTSYNRX ,/* Received a SYN MP_JOIN with a different port-number */ +MPTCP_MIB_JOINPORTSYNACKRX ,/* Received a SYNACK MP_JOIN with a different port-number */ +MPTCP_MIB_JOINPORTACKRX ,/* Received an ACK MP_JOIN with a different port-number */ +MPTCP_MIB_MISMATCHPORTSYNRX ,/* Received a SYN MP_JOIN with a mismatched port-number */ +MPTCP_MIB_MISMATCHPORTACKRX ,/* Received an ACK MP_JOIN with a mismatched port-number */ MPTCP_MIB_RMADDR ,/* Received RM_ADDR */ MPTCP_MIB_RMSUBFLOW ,/* Remove a subflow */ MPTCP_MIB_MPPRIOTX, /* Transmit a MP_PRIO */
net/mptcp/options.c +4 -0
@@ -1022,6 +1022,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 +7 -1
@@ -199,8 +199,10 @@ static int subflow_init_req(struct request_sock *req, tcp_request_sock_ops.destructor (req); subflow_req->msk =NULL ; subflow_req->mp_join =0 ; +SUBFLOW_REQ_INC_STATS (req,MPTCP_MIB_MISMATCHPORTSYNRX );return -EPERM ; } +SUBFLOW_REQ_INC_STATS (req,MPTCP_MIB_JOINPORTSYNRX ); }if (unlikely (req->syncookie)) {
@@ -420,6 +422,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_JOINPORTSYNACKRX ); } }else if (mptcp_check_fallback (sk)) { fallback:
@@ -688,8 +691,11 @@ 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)) +if (!mptcp_pm_sport_in_anno_list (owner, sk)) { +SUBFLOW_REQ_INC_STATS (req,MPTCP_MIB_MISMATCHPORTACKRX ); goto out; + } +SUBFLOW_REQ_INC_STATS (req,MPTCP_MIB_JOINPORTACKRX ); } } }
Signature · 1 line
2.29.2
Geliang Tang
PATCH v8 8/8 selftests: mptcp: add testcases for ADD_ADDR with port
This patch added testcases for ADD_ADDR with port and the related MIB counters check 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 ]
syn[ 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 ]
syn[ 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 ]
syn[ ok ] - ack [ ok ]
rm [ ok ] - sf [ ok ]
--- .../testing/selftests/net/mptcp/mptcp_join.sh | 148 +++++++++++++++++- 1 file changed, 147 insertions(+), 1 deletion(-)
tools/testing/selftests/net/mptcp/mptcp_join.sh +147 -1
@@ -453,6 +453,12 @@ 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 mis_syn_nr=${7:-0} + local mis_ack_nr=${8:-0} local count local dump_stats
@@ -475,7 +481,87 @@ 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 a port-number expected $port_nr" + ret=1 + dump_stats=1 + else + echo "[ ok ]" + fi + + printf "%-39s %s" " " "syn" + count=`ip netns exec $ns1 nstat -as | grep MPTcpExtMPJoinPortSynRx | + awk '{print $2}'` + [ -z "$count" ] && count=0 + if [ "$count" != "$syn_nr" ]; then + echo "[fail] got $count JOIN[s] syn with a different \ + port-number expected $syn_nr" + ret=1 + dump_stats=1 + else + echo -n "[ ok ]" + fi + + echo -n " - synack" + count=`ip netns exec $ns2 nstat -as | grep MPTcpExtMPJoinPortSynAckRx | + awk '{print $2}'` + [ -z "$count" ] && count=0 + if [ "$count" != "$syn_ack_nr" ]; then + echo "[fail] got $count JOIN[s] synack with a different \ + port-number 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 MPTcpExtMPJoinPortAckRx | + awk '{print $2}'` + [ -z "$count" ] && count=0 + if [ "$count" != "$ack_nr" ]; then + echo "[fail] got $count JOIN[s] ack with a different \ + port-number expected $ack_nr" + ret=1 + dump_stats=1 + else + echo "[ ok ]" + fi + + printf "%-39s %s" " " "syn" + count=`ip netns exec $ns1 nstat -as | grep MPTcpExtMismatchPortSynRx | + awk '{print $2}'` + [ -z "$count" ] && count=0 + if [ "$count" != "$mis_syn_nr" ]; then + echo "[fail] got $count JOIN[s] syn with a mismatched \ + port-number expected $mis_syn_nr" + ret=1 + dump_stats=1 + else + echo -n "[ ok ]" + fi + + echo -n " - ack " + count=`ip netns exec $ns1 nstat -as | grep MPTcpExtMismatchPortAckRx | + awk '{print $2}'` + [ -z "$count" ] && count=0 + if [ "$count" != "$mis_ack_nr" ]; then + echo "[fail] got $count JOIN[s] ack with a mismatched \ + port-number expected $mis_ack_nr" + ret=1 + dump_stats=1 + else + echo "[ ok ]" + fi + else + echo "" fi if [ "${dump_stats}" = 1 ]; then
@@ -809,6 +895,66 @@ chk_join_nr "single address, backup" 1 1 1 chk_add_nr 1 1 chk_prio_nr 1 0 +# 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 + +# multiple 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 "multiple addresses with port" 2 2 2 +chk_add_nr 2 2 2 + +# multiple 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 "multiple addresses with ports" 2 2 2 +chk_add_nr 2 2 2 + # single subflow, syncookies reset_with_cookies ip netns exec $ns1 ./pm_nl_ctl limits 0 1
Signature · 1 line
2.29.2
Mat Martineau
[MPTCP] Re: [MPTCP][PATCH v8 mptcp-next 3/8] mptcp: add port number announced check
In reply to Geliang TangQuoted 73 lines · Geliang Tang
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_listto 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 | 23 ++++++++++++++++++++++- net/mptcp/protocol.h | 1 + net/mptcp/subflow.c | 10 ++++++++++ 3 files changed, 33 insertions(+), 1 deletion(-)net/mptcp/pm_netlink.c +22
-1@@ -92,8 +92,8 @@ static bool address_zero(const struct mptcp_addr_info *addr)static void local_address (const struct sock_common *skc,struct mptcp_addr_info *addr) { - addr->port =0 ; addr->family = skc->skc_family; + addr->port =htons (skc->skc_num);if (addr->family ==AF_INET ) addr->addr.s_addr = skc->skc_rcv_saddr;#if IS_ENABLED (CONFIG_MPTCP_IPV6 )@@ -204,6 +204,27 @@ 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; +struct mptcp_addr_info saddr; +bool ret = false; + +local_address ((struct sock_common *)sk, &saddr); + +spin_lock_bh (&msk->pm.lock); +list_for_each_entry (entry, &msk->pm.anno_list, list) { +if (addresses_equal (&entry->addr, &saddr, true)) { + 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@@ -565,6 +565,7 @@ int mptcp_pm_nl_mp_prio_send_ack(struct mptcp_sock *msk,struct mptcp_addr_info *addr, u8 bkup);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(structmptcp_sock*msk,struct mptcp_addr_info *addr);net/mptcp/subflow.c +0
-0@@ -193,6 +193,14 @@ static int subflow_init_req(struct request_sock *req, pr_debug("syn inet_sport=%d %d", ntohs(inet_sk(sk_listener)->inet_sport),
net/mptcp/subflow.c static int subflow_init_req(struct request_sock *req, · line 204
Quoted 10 lines
net/mptcp/subflow.c +8
-0ntohs(inet_sk((struct sock *)subflow_req->msk)->inet_sport)); +if (!mptcp_pm_sport_in_anno_list (subflow_req->msk, sk_listener)) { +sock_put ((struct sock *)subflow_req->msk); +mptcp_token_destroy_request (req); + tcp_request_sock_ops.destructor (req); + subflow_req->msk =NULL ; + subflow_req->mp_join =0 ; +return -EPERM ; + } }
Since this adds a new error path where squbflow_req->local_nonce and subflow_req->thmac are not needed, could you create a separate helper function to initialize those and call it here? Those are more expensive to populate.
Quoted 13 lines
net/mptcp/subflow.c +2
-0if (unlikely (req->syncookie)) {@@ -680,6 +688,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.29.2
--
Mat Martineau
Intel
Mat Martineau
[MPTCP] Re: [MPTCP][PATCH v8 mptcp-next 2/8] mptcp: add port number check for MP_JOIN
In reply to Geliang TangQuoted 11 lines · Geliang Tang
This patch added two new helpers,
subflow_use_different_sportandsubflow_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(+)
Please squash this with patch 3.
Thanks,
--
Mat Martineau
Intel
Geliang Tang
[MPTCP] Re: [MPTCP][PATCH v8 mptcp-next 1/8] mptcp: create the listening socket for new port
In reply to Geliang TangQuoted 4 lines · Geliang Tang <geliangtang(a)gmail.com> 于2020年12月13日周日 上午9:52写道:
This patch created a listening socket when an address with a port-number is added by PM netlink. Then binded the new port to the socket, and listened for the connection.
Please update the commit message:
---
This patch created a listening socket when an address with a port-number is added by PM netlink. Then binded the new port to the socket, and listened for the connection.
When the address is removed or the addresses are flushed by PM netlink, release the listening socket.
---
-Geliang
Mat Martineau
[MPTCP] Re: [MPTCP][PATCH v8 mptcp-next 1/8] mptcp: create the listening socket for new port
In reply to Geliang TangQuoted 108 lines · Geliang Tang
This patch created a listening socket when an address with a port-number is added by PM netlink. Then binded the new port to the socket, and listened for the connection.
Signed-off-by Geliang Tang <geliangtang(a)gmail.com>---
net/mptcp/pm_netlink.c | 64 ++++++++++++++++++++++++++++++++++++++++++ net/mptcp/protocol.c | 2 +- net/mptcp/protocol.h | 3 ++ net/mptcp/subflow.c | 4 +-- 4 files changed, 70 insertions(+), 3 deletions(-)net/mptcp/pm_netlink.c +60
-0@@ -26,6 +26,7 @@ struct mptcp_pm_addr_entry {struct list_head list;struct mptcp_addr_info addr;struct rcu_head rcu; +struct socket *lsk; };struct mptcp_pm_add_entry {@@ -613,6 +614,53 @@ static int mptcp_pm_nl_append_new_local_addr(struct pm_nl_pernet *pernet,return ret; } +static int mptcp_pm_nl_create_listen_socket (struct sock *sk, +struct mptcp_pm_addr_entry *entry) +{ +struct sockaddr_storage addr; +struct mptcp_sock *msk; +struct socket *ssock; +int backlog =1024 ; +int err; + + err =sock_create_kern (sock_net (sk), entry->addr.family, +SOCK_STREAM ,IPPROTO_MPTCP , &entry->lsk); +if (err) +return err; + + msk =mptcp_sk (entry->lsk->sk); +if (!msk) { + err = -EINVAL ; + goto out; + } + + ssock =__mptcp_nmpc_socket (msk); +if (!ssock) { + err = -EINVAL ; + goto out; + } + +mptcp_info2sockaddr (&entry->addr, &addr); + err =kernel_bind (ssock, (struct sockaddr *)&addr, +sizeof (struct sockaddr_in )); +if (err) { +pr_warn ("kernel_bind error, err=%d" , err); + goto out; + } + + err =kernel_listen (ssock, backlog); +if (err) { +pr_warn ("kernel_listen error, err=%d" , err); + goto out; + } + +return 0 ; + +out: +sock_release (entry->lsk); +return err; +} +int mptcp_pm_nl_get_local_id (struct mptcp_sock *msk,struct sock_common *skc) {struct mptcp_pm_addr_entry *entry;@@ -657,6 +705,8 @@ int mptcp_pm_nl_get_local_id(struct mptcp_sock *msk, struct sock_common *skc) entry->addr.ifindex =0 ; entry->addr.flags =0 ; entry->addr.id =0 ; + entry->addr.port =0 ; + entry->lsk =NULL ; ret =mptcp_pm_nl_append_new_local_addr (pernet, entry);if (ret <0 )kfree (entry);@@ -808,9 +858,19 @@ static int mptcp_nl_cmd_add_addr(struct sk_buff *skb, struct genl_info *info) } *entry = addr; +if (entry->addr.port) { + ret =mptcp_pm_nl_create_listen_socket (skb->sk, entry); +if (ret) { +GENL_SET_ERR_MSG (info,"create listen socket error" ); +kfree (entry); +return ret; + } + } ret =mptcp_pm_nl_append_new_local_addr (pernet, entry);if (ret <0 ) {GENL_SET_ERR_MSG (info,"too many addresses or duplicate one" ); +if (entry->lsk) +sock_release (entry->lsk);kfree (entry);return ret; }
net/mptcp/pm_netlink.c static int mptcp_nl_cmd_del_addr(struct sk_buff *skb, struct genl_info *info) · line 986
Quoted 6 lines
net/mptcp/pm_netlink.c +2
-0@@ -921,6 +981,8 @@ static int mptcp_nl_cmd_del_addr(struct sk_buff *skb, struct genl_info *info)spin_unlock_bh (&pernet->lock);mptcp_nl_remove_subflow_and_signal_addr (sock_net (skb->sk), &entry->addr); +if (entry->lsk) +sock_release (entry->lsk);kfree_rcu (entry, rcu);
Releasing the socket here could be racy - since the list is rcu-protected, the entry could still be accessed (which is why it's freed with kfree_rcu()). Rather than calling kfree_rcu(), use a custom callback with call_rcu() that will both release lsk and kfree the list entry.
net/mptcp/pm_netlink.c static void __flush_addrs(struct net *net, struct list_head *list) · line 1002
Quoted 10 lines
net/mptcp/pm_netlink.c +2
-0return ret;@@ -934,6 +996,8 @@ static void __flush_addrs(struct net *net, struct list_head *list) cur =list_entry (list->next, struct mptcp_pm_addr_entry, list);mptcp_nl_remove_subflow_and_signal_addr (net, &cur->addr); +if (cur->lsk) +sock_release (cur->lsk);list_del_rcu (&cur->list);kfree_rcu (cur, rcu);
Same issue as above with sock_release() and rcu.
Mat
Quoted 50 lines
net/mptcp/pm_netlink.c +0
-0}net/mptcp/protocol.c +1
-1@@ -49,7 +49,7 @@ static void __mptcp_check_send_data_fin(struct sock *sk);* completed yet or has failed, return the subflow socket. * Otherwise return NULL. */ -static struct socket *__mptcp_nmpc_socket (const struct mptcp_sock *msk) +struct socket *__mptcp_nmpc_socket (const struct mptcp_sock *msk) {if (!msk->subflow ||READ_ONCE (msk->can_ack))return NULL ;net/mptcp/protocol.h +3
-0@@ -473,11 +473,14 @@ void mptcp_subflow_shutdown(struct sock *sk, struct sock *ssk, int how);void __mptcp_close_ssk (struct sock *sk,struct sock *ssk,struct mptcp_subflow_context *subflow);void mptcp_subflow_reset (struct sock *ssk); +struct socket *__mptcp_nmpc_socket (const struct mptcp_sock *msk);/* called with sk socket lock held */ 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 mptcp_info2sockaddr (const struct mptcp_addr_info *info, +struct sockaddr_storage *addr);static inline void mptcp_subflow_tcp_fallback (struct sock *sk,struct mptcp_subflow_context *ctx)net/mptcp/subflow.c +2
-2@@ -1073,8 +1073,8 @@ void mptcpv6_handle_mapped(struct sock *sk, bool mapped) }#endif -static void mptcp_info2sockaddr (const struct mptcp_addr_info *info, -struct sockaddr_storage *addr) +void mptcp_info2sockaddr (const struct mptcp_addr_info *info, +struct sockaddr_storage *addr) {memset (addr,0 ,sizeof (*addr)); addr->ss_family = info->family;Signature · 1 line
2.29.2
--
Mat Martineau
Intel
Geliang Tang
[MPTCP] Re: [MPTCP][PATCH v8 mptcp-next 1/8] mptcp: create the listening socket for new port
Hi Mat,
Quoted 113 lines · Mat Martineau <mathew.j.martineau(a)linux.intel.com> 于2020年12月18日周五 上午8:14写道:
Quoted 110 lines · Geliang Tang
This patch created a listening socket when an address with a port-number is added by PM netlink. Then binded the new port to the socket, and listened for the connection.
Signed-off-by Geliang Tang <geliangtang(a)gmail.com>---
net/mptcp/pm_netlink.c | 64 ++++++++++++++++++++++++++++++++++++++++++ net/mptcp/protocol.c | 2 +- net/mptcp/protocol.h | 3 ++ net/mptcp/subflow.c | 4 +-- 4 files changed, 70 insertions(+), 3 deletions(-)net/mptcp/pm_netlink.c +60
-0@@ -26,6 +26,7 @@ struct mptcp_pm_addr_entry {struct list_head list;struct mptcp_addr_info addr;struct rcu_head rcu; +struct socket *lsk; };struct mptcp_pm_add_entry {@@ -613,6 +614,53 @@ static int mptcp_pm_nl_append_new_local_addr(struct pm_nl_pernet *pernet,return ret; } +static int mptcp_pm_nl_create_listen_socket (struct sock *sk, +struct mptcp_pm_addr_entry *entry) +{ +struct sockaddr_storage addr; +struct mptcp_sock *msk; +struct socket *ssock; +int backlog =1024 ; +int err; + + err =sock_create_kern (sock_net (sk), entry->addr.family, +SOCK_STREAM ,IPPROTO_MPTCP , &entry->lsk); +if (err) +return err; + + msk =mptcp_sk (entry->lsk->sk); +if (!msk) { + err = -EINVAL ; + goto out; + } + + ssock =__mptcp_nmpc_socket (msk); +if (!ssock) { + err = -EINVAL ; + goto out; + } + +mptcp_info2sockaddr (&entry->addr, &addr); + err =kernel_bind (ssock, (struct sockaddr *)&addr, +sizeof (struct sockaddr_in )); +if (err) { +pr_warn ("kernel_bind error, err=%d" , err); + goto out; + } + + err =kernel_listen (ssock, backlog); +if (err) { +pr_warn ("kernel_listen error, err=%d" , err); + goto out; + } + +return 0 ; + +out: +sock_release (entry->lsk); +return err; +} +int mptcp_pm_nl_get_local_id (struct mptcp_sock *msk,struct sock_common *skc) {struct mptcp_pm_addr_entry *entry;@@ -657,6 +705,8 @@ int mptcp_pm_nl_get_local_id(struct mptcp_sock *msk, struct sock_common *skc) entry->addr.ifindex =0 ; entry->addr.flags =0 ; entry->addr.id =0 ; + entry->addr.port =0 ; + entry->lsk =NULL ; ret =mptcp_pm_nl_append_new_local_addr (pernet, entry);if (ret <0 )kfree (entry);@@ -808,9 +858,19 @@ static int mptcp_nl_cmd_add_addr(struct sk_buff *skb, struct genl_info *info) } *entry = addr; +if (entry->addr.port) { + ret =mptcp_pm_nl_create_listen_socket (skb->sk, entry); +if (ret) { +GENL_SET_ERR_MSG (info,"create listen socket error" ); +kfree (entry); +return ret; + } + } ret =mptcp_pm_nl_append_new_local_addr (pernet, entry);if (ret <0 ) {GENL_SET_ERR_MSG (info,"too many addresses or duplicate one" ); +if (entry->lsk) +sock_release (entry->lsk);kfree (entry);return ret; }@@ -921,6 +981,8 @@ static int mptcp_nl_cmd_del_addr(struct sk_buff *skb, struct genl_info *info)spin_unlock_bh (&pernet->lock);
Quoted 10 lines
net/mptcp/pm_netlink.c static int mptcp_nl_cmd_del_addr(struct sk_buff *skb, struct genl_info *info) · line 986
Quoted 4 lines
net/mptcp/pm_netlink.c +2
-0mptcp_nl_remove_subflow_and_signal_addr (sock_net (skb->sk), &entry->addr); +if (entry->lsk) +sock_release (entry->lsk);kfree_rcu (entry, rcu);Releasing the socket here could be racy - since the list is rcu-protected, the entry could still be accessed (which is why it's freed with
kfree_rcu()). Rather than callingkfree_rcu(), use a custom callback withcall_rcu()that will both release lsk and kfree the list entry.
It dosen't work. I fixed it like this:
net/mptcp/pm_netlink.c +0 -0
@@ -1010,6 +1010,16 @@ static int
mptcp_nl_remove_subflow_and_signal_addr(struct net *net,
return 0;
}
+static void mptcp_pm_addr_entry_free(struct rcu_head *head)
+{
+ struct mptcp_pm_addr_entry *entry;
+
+ entry = container_of(head, struct mptcp_pm_addr_entry, rcu);
+ if (entry->lsk)
+ sock_release(entry->lsk);
+ kfree(entry);
+}
+
static int mptcp_nl_cmd_del_addr(struct sk_buff *skb, struct genl_info *info)
{
struct nlattr *attr = info->attrs[MPTCP_PM_ATTR_ADDR];
@@ -1039,9 +1049,7 @@ static int mptcp_nl_cmd_del_addr(struct sk_buff
*skb, struct genl_info *info)
spin_unlock_bh(&pernet->lock);
mptcp_nl_remove_subflow_and_signal_addr(sock_net(skb->sk), &entry->addr);- if (entry->lsk)
- sock_release(entry->lsk);
- kfree_rcu(entry, rcu);
+ call_rcu(&entry->rcu, mptcp_pm_addr_entry_free);
return ret;
}
@@ -1054,10 +1062,8 @@ static void __flush_addrs(struct net *net,
struct list_head *list)
cur = list_entry(list->next,
struct mptcp_pm_addr_entry, list);
mptcp_nl_remove_subflow_and_signal_addr(net, &cur->addr);
- if (cur->lsk)
- sock_release(cur->lsk);
list_del_rcu(&cur->list);
- kfree_rcu(cur, rcu);
+ call_rcu(&cur->rcu, mptcp_pm_addr_entry_free);
}}
--
2.29.2
But I got this error:
[ 261.105979] MPTCP: sock_release 0000000058a9d993
[ 261.105985] BUG: sleeping function called from invalid context at
net/core/sock.c:3048
[ 261.105987] in_atomic(): 1, irqs_disabled(): 0, non_block: 0, pid:
0, name: swapper/2
[ 261.105990] 1 lock held by swapper/2/0:
[ 261.105992] #0: ffffffffac5a68e0 (rcu_callback){....}-{0:0}, at:
rcu_do_batch+0x216/0x900
[ 261.106009] CPU: 2 PID: 0 Comm: swapper/2 Kdump: loaded Not tainted
5.10.0-mptcp+ #429
[ 261.106011] Hardware name: TIMI Mi Laptop Pro 15/TM1905, BIOS
XMACM500P0301 04/08/2020
[ 261.106013] Call Trace:
[ 261.106016] <IRQ>
[ 261.106022] dump_stack+0x8b/0xb0
[ 261.106028] ___might_sleep.cold+0xb6/0xc6
[ 261.106033] lock_sock_nested+0x28/0x90
[ 261.106040] mptcp_close+0x20/0x2f0
[ 261.106043] ? rcu_do_batch+0x216/0x900
[ 261.106048] ? rcu_do_batch+0x216/0x900
[ 261.106052] inet_release+0x42/0x80
[ 261.106058] sock_release+0x20/0x70
[ 261.106063] mptcp_pm_addr_entry_free+0x3b/0x60
[ 261.106067] rcu_do_batch+0x289/0x900
[ 261.106078] rcu_core+0x27d/0x450
[ 261.106085] __do_softirq+0xd5/0x485
[ 261.106096] asm_call_irq_on_stack+0xf/0x20
[ 261.106098] </IRQ>
[ 261.106103] do_softirq_own_stack+0x5b/0x70
[ 261.106106] __irq_exit_rcu+0xda/0x120
[ 261.106110] irq_exit_rcu+0xa/0x20
[ 261.106113] sysvec_apic_timer_interrupt+0x4b/0xa0
[ 261.106118] asm_sysvec_apic_timer_interrupt+0x12/0x20
[ 261.106122] RIP: 0010:cpuidle_enter_state+0xfa/0x470-Geliang
Quoted 70 lines
net/mptcp/pm_netlink.c static void __flush_addrs(struct net *net, struct list_head *list) · line 1002
Quoted 10 lines
net/mptcp/pm_netlink.c +2
-0return ret;@@ -934,6 +996,8 @@ static void __flush_addrs(struct net *net, struct list_head *list) cur =list_entry (list->next, struct mptcp_pm_addr_entry, list);mptcp_nl_remove_subflow_and_signal_addr (net, &cur->addr); +if (cur->lsk) +sock_release (cur->lsk);list_del_rcu (&cur->list);kfree_rcu (cur, rcu);Same issue as above with
sock_release()and rcu.Mat
Quoted 49 lines
net/mptcp/pm_netlink.c +0
-0}net/mptcp/protocol.c +1
-1@@ -49,7 +49,7 @@ static void __mptcp_check_send_data_fin(struct sock *sk);* completed yet or has failed, return the subflow socket. * Otherwise return NULL. */ -static struct socket *__mptcp_nmpc_socket (const struct mptcp_sock *msk) +struct socket *__mptcp_nmpc_socket (const struct mptcp_sock *msk) {if (!msk->subflow ||READ_ONCE (msk->can_ack))return NULL ;net/mptcp/protocol.h +3
-0@@ -473,11 +473,14 @@ void mptcp_subflow_shutdown(struct sock *sk, struct sock *ssk, int how);void __mptcp_close_ssk (struct sock *sk,struct sock *ssk,struct mptcp_subflow_context *subflow);void mptcp_subflow_reset (struct sock *ssk); +struct socket *__mptcp_nmpc_socket (const struct mptcp_sock *msk);/* called with sk socket lock held */ 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 mptcp_info2sockaddr (const struct mptcp_addr_info *info, +struct sockaddr_storage *addr);static inline void mptcp_subflow_tcp_fallback (struct sock *sk,struct mptcp_subflow_context *ctx)net/mptcp/subflow.c +2
-3@@ -1073,8 +1073,8 @@ void mptcpv6_handle_mapped(struct sock *sk, bool mapped) }#endif -static void mptcp_info2sockaddr (const struct mptcp_addr_info *info, -struct sockaddr_storage *addr) +void mptcp_info2sockaddr (const struct mptcp_addr_info *info, +struct sockaddr_storage *addr) {memset (addr,0 ,sizeof (*addr)); addr->ss_family = info->family; --Quoted 1 line
2.29.2
--
Mat Martineau
Intel
Mat Martineau
[MPTCP] Re: [MPTCP][PATCH v8 mptcp-next 1/8] mptcp: create the listening socket for new port
Quoted 207 lines · Geliang Tang
Hi Mat,
Quoted 113 lines · Mat Martineau <mathew.j.martineau(a)linux.intel.com> 于2020年12月18日周五 上午8:14写道:
Quoted 110 lines · Geliang Tang
This patch created a listening socket when an address with a port-number is added by PM netlink. Then binded the new port to the socket, and listened for the connection.
Signed-off-by Geliang Tang <geliangtang(a)gmail.com>---
net/mptcp/pm_netlink.c | 64 ++++++++++++++++++++++++++++++++++++++++++ net/mptcp/protocol.c | 2 +- net/mptcp/protocol.h | 3 ++ net/mptcp/subflow.c | 4 +-- 4 files changed, 70 insertions(+), 3 deletions(-)net/mptcp/pm_netlink.c +60
-0@@ -26,6 +26,7 @@ struct mptcp_pm_addr_entry {struct list_head list;struct mptcp_addr_info addr;struct rcu_head rcu; +struct socket *lsk; };struct mptcp_pm_add_entry {@@ -613,6 +614,53 @@ static int mptcp_pm_nl_append_new_local_addr(struct pm_nl_pernet *pernet,return ret; } +static int mptcp_pm_nl_create_listen_socket (struct sock *sk, +struct mptcp_pm_addr_entry *entry) +{ +struct sockaddr_storage addr; +struct mptcp_sock *msk; +struct socket *ssock; +int backlog =1024 ; +int err; + + err =sock_create_kern (sock_net (sk), entry->addr.family, +SOCK_STREAM ,IPPROTO_MPTCP , &entry->lsk); +if (err) +return err; + + msk =mptcp_sk (entry->lsk->sk); +if (!msk) { + err = -EINVAL ; + goto out; + } + + ssock =__mptcp_nmpc_socket (msk); +if (!ssock) { + err = -EINVAL ; + goto out; + } + +mptcp_info2sockaddr (&entry->addr, &addr); + err =kernel_bind (ssock, (struct sockaddr *)&addr, +sizeof (struct sockaddr_in )); +if (err) { +pr_warn ("kernel_bind error, err=%d" , err); + goto out; + } + + err =kernel_listen (ssock, backlog); +if (err) { +pr_warn ("kernel_listen error, err=%d" , err); + goto out; + } + +return 0 ; + +out: +sock_release (entry->lsk); +return err; +} +int mptcp_pm_nl_get_local_id (struct mptcp_sock *msk,struct sock_common *skc) {struct mptcp_pm_addr_entry *entry;@@ -657,6 +705,8 @@ int mptcp_pm_nl_get_local_id(struct mptcp_sock *msk, struct sock_common *skc) entry->addr.ifindex =0 ; entry->addr.flags =0 ; entry->addr.id =0 ; + entry->addr.port =0 ; + entry->lsk =NULL ; ret =mptcp_pm_nl_append_new_local_addr (pernet, entry);if (ret <0 )kfree (entry);@@ -808,9 +858,19 @@ static int mptcp_nl_cmd_add_addr(struct sk_buff *skb, struct genl_info *info) } *entry = addr; +if (entry->addr.port) { + ret =mptcp_pm_nl_create_listen_socket (skb->sk, entry); +if (ret) { +GENL_SET_ERR_MSG (info,"create listen socket error" ); +kfree (entry); +return ret; + } + } ret =mptcp_pm_nl_append_new_local_addr (pernet, entry);if (ret <0 ) {GENL_SET_ERR_MSG (info,"too many addresses or duplicate one" ); +if (entry->lsk) +sock_release (entry->lsk);kfree (entry);return ret; }@@ -921,6 +981,8 @@ static int mptcp_nl_cmd_del_addr(struct sk_buff *skb, struct genl_info *info)spin_unlock_bh (&pernet->lock);Quoted 10 lines
net/mptcp/pm_netlink.c static int mptcp_nl_cmd_del_addr(struct sk_buff *skb, struct genl_info *info) · line 986
Quoted 4 lines
net/mptcp/pm_netlink.c +2
-0mptcp_nl_remove_subflow_and_signal_addr (sock_net (skb->sk), &entry->addr); +if (entry->lsk) +sock_release (entry->lsk);kfree_rcu (entry, rcu);Releasing the socket here could be racy - since the list is rcu-protected, the entry could still be accessed (which is why it's freed with
kfree_rcu()). Rather than callingkfree_rcu(), use a custom callback withcall_rcu()that will both release lsk and kfree the list entry.It dosen't work. I fixed it like this:
net/mptcp/pm_netlink.c +10
-0@@ -1010,6 +1010,16 @@ static int mptcp_nl_remove_subflow_and_signal_addr(struct net *net, return 0; } +static void mptcp_pm_addr_entry_free (struct rcu_head *head) +{ +struct mptcp_pm_addr_entry *entry; + + entry = container_of(head, struct mptcp_pm_addr_entry, rcu); +if (entry->lsk) +sock_release (entry->lsk); +kfree (entry); +} +static int mptcp_nl_cmd_del_addr (struct sk_buff *skb,struct genl_info *info) {struct nlattr *attr = info->attrs[MPTCP_PM_ATTR_ADDR];net/mptcp/pm_netlink.c +1
-3@@ -1039,9 +1049,7 @@ static int mptcp_nl_cmd_del_addr(struct sk_buff *skb, struct genl_info *info)spin_unlock_bh (&pernet->lock);mptcp_nl_remove_subflow_and_signal_addr (sock_net (skb->sk), &entry->addr); -if (entry->lsk) -sock_release (entry->lsk); -kfree_rcu (entry, rcu); +call_rcu (&entry->rcu, mptcp_pm_addr_entry_free);return ret;}
net/mptcp/pm_netlink.c +1
-4@@ -1054,10 +1062,8 @@ static void __flush_addrs(struct net *net, struct list_head *list) cur =list_entry (list->next, struct mptcp_pm_addr_entry, list);mptcp_nl_remove_subflow_and_signal_addr (net, &cur->addr); -if (cur->lsk) -sock_release (cur->lsk);list_del_rcu (&cur->list); -kfree_rcu (cur, rcu); +call_rcu (&cur->rcu, mptcp_pm_addr_entry_free); } } --2.29.2
But I got this error:
[ 261.105979] MPTCP: sock_release 0000000058a9d993 [ 261.105985] BUG: sleeping function called from invalid context at net/core/sock.c:3048 [ 261.105987] in_atomic(): 1, irqs_disabled(): 0, non_block: 0, pid: 0, name: swapper/2 [ 261.105990] 1 lock held by swapper/2/0: [ 261.105992] #0: ffffffffac5a68e0 (rcu_callback){....}-{0:0}, at: rcu_do_batch+0x216/0x900 [ 261.106009] CPU: 2 PID: 0 Comm: swapper/2 Kdump: loaded Not tainted 5.10.0-mptcp+ #429 [ 261.106011] Hardware name: TIMI Mi Laptop Pro 15/TM1905, BIOS XMACM500P0301 04/08/2020 [ 261.106013] Call Trace: [ 261.106016] <IRQ> [ 261.106022] dump_stack+0x8b/0xb0 [ 261.106028] ___might_sleep.cold+0xb6/0xc6 [ 261.106033] lock_sock_nested+0x28/0x90 [ 261.106040] mptcp_close+0x20/0x2f0 [ 261.106043] ? rcu_do_batch+0x216/0x900 [ 261.106048] ? rcu_do_batch+0x216/0x900 [ 261.106052] inet_release+0x42/0x80 [ 261.106058] sock_release+0x20/0x70 [ 261.106063] mptcp_pm_addr_entry_free+0x3b/0x60 [ 261.106067] rcu_do_batch+0x289/0x900 [ 261.106078] rcu_core+0x27d/0x450 [ 261.106085] __do_softirq+0xd5/0x485
Quoted 9 lines
[ 261.106096] asm_call_irq_on_stack+0xf/0x20 [ 261.106098] </IRQ> [ 261.106103] do_softirq_own_stack+0x5b/0x70 [ 261.106106] __irq_exit_rcu+0xda/0x120 [ 261.106110] irq_exit_rcu+0xa/0x20 [ 261.106113] sysvec_apic_timer_interrupt+0x4b/0xa0 [ 261.106118] asm_sysvec_apic_timer_interrupt+0x12/0x20 [ 261.106122] RIP: 0010:cpuidle_enter_state+0xfa/0x470
Oops, my mistake. Yeah, I can see how sock_release() is not able to run in a regular rcu handler.
Instead of call_rcu(), try queue_rcu_work(). That will run the handler in a context where it can sleep, after the rcu grace period.
Look at nfc_genl_rcv_nl_event() and nfc_urelease_event_work(), which use a dynamically allocated work struct. Use 'struct rcu_work' instead of 'struct work_struct' and queue_rcu_work(system_wq, rwork) instead of schedule_work().
Mat
Quoted 58 lines
Quoted 58 lines
net/mptcp/pm_netlink.c static void __flush_addrs(struct net *net, struct list_head *list) · line 1002
Quoted 11 lines
net/mptcp/pm_netlink.c +2
-0return ret;@@ -934,6 +996,8 @@ static void __flush_addrs(struct net *net, struct list_head *list) cur =list_entry (list->next, struct mptcp_pm_addr_entry, list);mptcp_nl_remove_subflow_and_signal_addr (net, &cur->addr); +if (cur->lsk) +sock_release (cur->lsk);list_del_rcu (&cur->list);kfree_rcu (cur, rcu);Same issue as above with
sock_release()and rcu.Mat
Quoted 41 lines
net/mptcp/pm_netlink.c +0
-0}net/mptcp/protocol.c +1
-1@@ -49,7 +49,7 @@ static void __mptcp_check_send_data_fin(struct sock *sk);* completed yet or has failed, return the subflow socket. * Otherwise return NULL. */ -static struct socket *__mptcp_nmpc_socket (const struct mptcp_sock *msk) +struct socket *__mptcp_nmpc_socket (const struct mptcp_sock *msk) {if (!msk->subflow ||READ_ONCE (msk->can_ack))return NULL ;net/mptcp/protocol.h +3
-0@@ -473,11 +473,14 @@ void mptcp_subflow_shutdown(struct sock *sk, struct sock *ssk, int how);void __mptcp_close_ssk (struct sock *sk,struct sock *ssk,struct mptcp_subflow_context *subflow);void mptcp_subflow_reset (struct sock *ssk); +struct socket *__mptcp_nmpc_socket (const struct mptcp_sock *msk);/* called with sk socket lock held */ 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 mptcp_info2sockaddr (const struct mptcp_addr_info *info, +struct sockaddr_storage *addr);static inline void mptcp_subflow_tcp_fallback (struct sock *sk,struct mptcp_subflow_context *ctx)net/mptcp/subflow.c +0
-0@@ -1073,8 +1073,8 @@ void mptcpv6_handle_mapped(struct sock *sk, bool mapped) }#endif
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
net/mptcp/subflow.c +2
-3-static void mptcp_info2sockaddr (const struct mptcp_addr_info *info, -struct sockaddr_storage *addr) +void mptcp_info2sockaddr (const struct mptcp_addr_info *info, +struct sockaddr_storage *addr) {memset (addr,0 ,sizeof (*addr)); addr->ss_family = info->family; --2.29.2
--
Mat Martineau
Intel