Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 5b0ec991 authored by Sean Hefty's avatar Sean Hefty Committed by Roland Dreier
Browse files

RDMA/cma: Bind to a specific address family



The RDMA CM uses a single port space for all associated (tcp, udp,
etc.) port bindings, regardless of the address family that the user
binds to.  The result is that if a user binds to AF_INET, but does not
specify an IP address, the bind will occur for AF_INET6.  This causes
an attempt to bind to the same port using AF_INET6 to fail, and
connection requests to AF_INET6 will match with the AF_INET listener.
Align the behavior with sockets and restrict the bind to AF_INET only.

If a user binds to AF_INET6, we bind the port to AF_INET6 and
AF_INET depending on the value of bindv6only.

Signed-off-by: default avatarSean Hefty <sean.hefty@intel.com>
Signed-off-by: default avatarRoland Dreier <roland@purestorage.com>
parent 6887a413
Loading
Loading
Loading
Loading
+21 −11
Original line number Diff line number Diff line
@@ -140,6 +140,7 @@ struct rdma_id_private {
	u8			srq;
	u8			tos;
	u8			reuseaddr;
	u8			afonly;
};

struct cma_multicast {
@@ -1573,6 +1574,7 @@ static void cma_listen_on_dev(struct rdma_id_private *id_priv,
	list_add_tail(&dev_id_priv->listen_list, &id_priv->listen_list);
	atomic_inc(&id_priv->refcount);
	dev_id_priv->internal_id = 1;
	dev_id_priv->afonly = id_priv->afonly;

	ret = rdma_listen(id, id_priv->backlog);
	if (ret)
@@ -2187,23 +2189,25 @@ static int cma_check_port(struct rdma_bind_list *bind_list,
	struct hlist_node *node;

	addr = (struct sockaddr *) &id_priv->id.route.addr.src_addr;
	if (cma_any_addr(addr) && !reuseaddr)
		return -EADDRNOTAVAIL;

	hlist_for_each_entry(cur_id, node, &bind_list->owners, node) {
		if (id_priv == cur_id)
			continue;

		if ((cur_id->state == RDMA_CM_LISTEN) ||
		    !reuseaddr || !cur_id->reuseaddr) {
		if ((cur_id->state != RDMA_CM_LISTEN) && reuseaddr &&
		    cur_id->reuseaddr)
			continue;

		cur_addr = (struct sockaddr *) &cur_id->id.route.addr.src_addr;
			if (cma_any_addr(cur_addr))
		if (id_priv->afonly && cur_id->afonly &&
		    (addr->sa_family != cur_addr->sa_family))
			continue;

		if (cma_any_addr(addr) || cma_any_addr(cur_addr))
			return -EADDRNOTAVAIL;

		if (!cma_addr_cmp(addr, cur_addr))
			return -EADDRINUSE;
	}
	}
	return 0;
}

@@ -2371,6 +2375,12 @@ int rdma_bind_addr(struct rdma_cm_id *id, struct sockaddr *addr)
	}

	memcpy(&id->route.addr.src_addr, addr, ip_addr_size(addr));
	if (addr->sa_family == AF_INET)
		id_priv->afonly = 1;
#if IS_ENABLED(CONFIG_IPV6)
	else if (addr->sa_family == AF_INET6)
		id_priv->afonly = init_net.ipv6.sysctl.bindv6only;
#endif
	ret = cma_get_port(id_priv);
	if (ret)
		goto err2;