If iwpmd is running, iw_cm_map() maps the local port of every listening and connecting iw_cm_id and, for a connect, asks the remote iwpmd for the mapped port of the remote listener. For kernel users with a well known port, e.g. smbdirect using port 5445, this can be more harmful than helpful: if the state of the iwpmd on both sides doesn't match the actual listeners, e.g. because the listener was created before iwpmd was started, the connection goes to a mapped port nobody listens on, while it works fine without iwpmd. Add iw_cm_id->skip_iwpm, which makes iw_cm_map() behave as if iwpmd is not running for this iw_cm_id, and rdma_set_iwpm(), which lets the upper layer decide whether iwpmd is used for an rdma_cm_id, by setting iw_cm_id->skip_iwpm for the iw_cm_id used for rdma_listen() and rdma_connect(). The setting is copied to the per device listeners created by cma_listen_on_dev(). Like rdma_set_afonly() it needs to be called before rdma_listen() or rdma_resolve_addr(). Without iwpmd the port of such an iw_cm_id is not reserved in the port space of the host, so on iWarp devices sharing the IP addresses of the host a TCP socket on the same port competes with it, like without iwpmd running at all. For the well known port of a kernel service that's what the administrator controls anyway. Fixes: dce268ffcddc ("smb: smbdirect: introduce smbdirect_socket_init_{new,accepting}() and helpers") Cc: stable@vger.kernel.org Cc: Jason Gunthorpe Cc: Leon Romanovsky Cc: Namjae Jeon Cc: linux-rdma@vger.kernel.org Cc: linux-kernel@vger.kernel.org Signed-off-by: Stefan Metzmacher --- drivers/infiniband/core/cma.c | 22 ++++++++++++++++++++++ drivers/infiniband/core/cma_priv.h | 1 + drivers/infiniband/core/iwcm.c | 7 +++++++ include/rdma/iw_cm.h | 1 + include/rdma/rdma_cm.h | 17 +++++++++++++++++ 5 files changed, 48 insertions(+) diff --git a/drivers/infiniband/core/cma.c b/drivers/infiniband/core/cma.c index 337a49d1acf7..719b91eca088 100644 --- a/drivers/infiniband/core/cma.c +++ b/drivers/infiniband/core/cma.c @@ -2686,6 +2686,7 @@ static int cma_iw_listen(struct rdma_id_private *id_priv, int backlog) id->tos_set = id_priv->tos_set; mutex_unlock(&id_priv->qp_mutex); id->afonly = id_priv->afonly; + id->skip_iwpm = id_priv->skip_iwpm; id_priv->cm_id.iw = id; memcpy(&id_priv->cm_id.iw->local_addr, cma_src_addr(id_priv), @@ -2740,6 +2741,7 @@ static int cma_listen_on_dev(struct rdma_id_private *id_priv, return PTR_ERR(dev_id_priv); dev_id_priv->restricted_node_type = id_priv->restricted_node_type; + dev_id_priv->skip_iwpm = id_priv->skip_iwpm; dev_id_priv->state = RDMA_CM_ADDR_BOUND; memcpy(cma_src_addr(dev_id_priv), cma_src_addr(id_priv), rdma_addr_size(cma_src_addr(id_priv))); @@ -3648,6 +3650,25 @@ int rdma_set_afonly(struct rdma_cm_id *id, int afonly) } EXPORT_SYMBOL(rdma_set_afonly); +int rdma_set_iwpm(struct rdma_cm_id *id, bool use_iwpm) +{ + struct rdma_id_private *id_priv; + unsigned long flags; + int ret; + + id_priv = container_of(id, struct rdma_id_private, id); + spin_lock_irqsave(&id_priv->lock, flags); + if (id_priv->state == RDMA_CM_IDLE || id_priv->state == RDMA_CM_ADDR_BOUND) { + id_priv->skip_iwpm = !use_iwpm; + ret = 0; + } else { + ret = -EINVAL; + } + spin_unlock_irqrestore(&id_priv->lock, flags); + return ret; +} +EXPORT_SYMBOL(rdma_set_iwpm); + static void cma_bind_port(struct rdma_bind_list *bind_list, struct rdma_id_private *id_priv) { @@ -4465,6 +4486,7 @@ static int cma_connect_iw(struct rdma_id_private *id_priv, cm_id->tos = id_priv->tos; cm_id->tos_set = id_priv->tos_set; mutex_unlock(&id_priv->qp_mutex); + cm_id->skip_iwpm = id_priv->skip_iwpm; id_priv->cm_id.iw = cm_id; diff --git a/drivers/infiniband/core/cma_priv.h b/drivers/infiniband/core/cma_priv.h index 04332eb82668..246d7340009a 100644 --- a/drivers/infiniband/core/cma_priv.h +++ b/drivers/infiniband/core/cma_priv.h @@ -73,6 +73,7 @@ struct rdma_id_private { int internal_id; enum rdma_cm_state state; u8 restricted_node_type; + bool skip_iwpm; spinlock_t lock; struct mutex qp_mutex; diff --git a/drivers/infiniband/core/iwcm.c b/drivers/infiniband/core/iwcm.c index 4b05867dad89..d60878b79637 100644 --- a/drivers/infiniband/core/iwcm.c +++ b/drivers/infiniband/core/iwcm.c @@ -518,6 +518,13 @@ static int iw_cm_map(struct iw_cm_id *cm_id, bool active) cm_id->m_local_addr = cm_id->local_addr; cm_id->m_remote_addr = cm_id->remote_addr; + /* + * The upper layer doesn't want to use the port mapper, + * act as if iwpmd is not running. + */ + if (cm_id->skip_iwpm) + return 0; + strscpy(pm_reg_msg.dev_name, devname); strscpy(pm_reg_msg.if_name, ifname); diff --git a/include/rdma/iw_cm.h b/include/rdma/iw_cm.h index 57b33edd9ce7..fc6db7c8a795 100644 --- a/include/rdma/iw_cm.h +++ b/include/rdma/iw_cm.h @@ -71,6 +71,7 @@ struct iw_cm_id { bool tos_set:1; bool mapped:1; bool afonly:1; + bool skip_iwpm:1; /* don't use the iWarp port mapper */ }; struct iw_cm_conn_param { diff --git a/include/rdma/rdma_cm.h b/include/rdma/rdma_cm.h index d639ff889e64..3ed197624cf3 100644 --- a/include/rdma/rdma_cm.h +++ b/include/rdma/rdma_cm.h @@ -377,6 +377,23 @@ int rdma_set_reuseaddr(struct rdma_cm_id *id, int reuse); */ int rdma_set_afonly(struct rdma_cm_id *id, int afonly); +/** + * rdma_set_iwpm - Specify if the iWarp port mapper (iwpmd) is used + * @id: Communication identifier to configure. + * @use_iwpm: true (default) to use iwpmd if it is running, + * false to never use it, as if iwpmd is not running. + * + * Without iwpmd the iWarp device listens on and connects to the + * requested ports directly, which is what users of well known + * ports might want. + * + * Like rdma_set_afonly() it needs to be called before rdma_listen() + * or rdma_resolve_addr(). + * + * Return: 0 on success, -EINVAL if called too late. + */ +int rdma_set_iwpm(struct rdma_cm_id *id, bool use_iwpm); + int rdma_set_ack_timeout(struct rdma_cm_id *id, u8 timeout); int rdma_set_min_rnr_timer(struct rdma_cm_id *id, u8 min_rnr_timer); -- 2.43.0