smc_clc.c 22.7 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
2 3 4 5 6 7
/*
 *  Shared Memory Communications over RDMA (SMC-R) and RoCE
 *
 *  CLC (connection layer control) handshake over initial TCP socket to
 *  prepare for RDMA traffic
 *
8
 *  Copyright IBM Corp. 2016, 2018
9 10 11 12 13
 *
 *  Author(s):  Ursula Braun <ubraun@linux.vnet.ibm.com>
 */

#include <linux/in.h>
14
#include <linux/inetdevice.h>
15
#include <linux/if_ether.h>
16
#include <linux/sched/signal.h>
17 18
#include <linux/utsname.h>
#include <linux/ctype.h>
19

20
#include <net/addrconf.h>
21 22 23 24
#include <net/sock.h>
#include <net/tcp.h>

#include "smc.h"
25
#include "smc_core.h"
26 27
#include "smc_clc.h"
#include "smc_ib.h"
28 29 30 31
#include "smc_ism.h"

#define SMCR_CLC_ACCEPT_CONFIRM_LEN 68
#define SMCD_CLC_ACCEPT_CONFIRM_LEN 48
U
Ursula Braun 已提交
32
#define SMCD_CLC_ACCEPT_CONFIRM_LEN_V2 78
33
#define SMC_CLC_RECV_BUF_LEN	100
34

35 36
/* eye catcher "SMCR" EBCDIC for CLC messages */
static const char SMC_EYECATCHER[4] = {'\xe2', '\xd4', '\xc3', '\xd9'};
37 38
/* eye catcher "SMCD" EBCDIC for CLC messages */
static const char SMCD_EYECATCHER[4] = {'\xe2', '\xd4', '\xc3', '\xc4'};
39

40 41
static u8 smc_hostname[SMC_MAX_HOSTNAME_LEN];

42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
/* check arriving CLC proposal */
static bool smc_clc_msg_prop_valid(struct smc_clc_msg_proposal *pclc)
{
	struct smc_clc_msg_proposal_prefix *pclc_prfx;
	struct smc_clc_smcd_v2_extension *smcd_v2_ext;
	struct smc_clc_msg_hdr *hdr = &pclc->hdr;
	struct smc_clc_v2_extension *v2_ext;

	v2_ext = smc_get_clc_v2_ext(pclc);
	pclc_prfx = smc_clc_proposal_get_prefix(pclc);
	if (hdr->version == SMC_V1) {
		if (hdr->typev1 == SMC_TYPE_N)
			return false;
		if (ntohs(hdr->length) !=
			sizeof(*pclc) + ntohs(pclc->iparea_offset) +
			sizeof(*pclc_prfx) +
			pclc_prfx->ipv6_prefixes_cnt *
				sizeof(struct smc_clc_ipv6_prefix) +
			sizeof(struct smc_clc_msg_trail))
			return false;
	} else {
		if (ntohs(hdr->length) !=
			sizeof(*pclc) +
			sizeof(struct smc_clc_msg_smcd) +
			(hdr->typev1 != SMC_TYPE_N ?
				sizeof(*pclc_prfx) +
				pclc_prfx->ipv6_prefixes_cnt *
				sizeof(struct smc_clc_ipv6_prefix) : 0) +
			(hdr->typev2 != SMC_TYPE_N ?
				sizeof(*v2_ext) +
				v2_ext->hdr.eid_cnt * SMC_MAX_EID_LEN : 0) +
			(smcd_indicated(hdr->typev2) ?
				sizeof(*smcd_v2_ext) + v2_ext->hdr.ism_gid_cnt *
					sizeof(struct smc_clc_smcd_gid_chid) :
				0) +
			sizeof(struct smc_clc_msg_trail))
			return false;
	}
	return true;
}

U
Ursula Braun 已提交
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
/* check arriving CLC accept or confirm */
static bool
smc_clc_msg_acc_conf_valid(struct smc_clc_msg_accept_confirm_v2 *clc_v2)
{
	struct smc_clc_msg_hdr *hdr = &clc_v2->hdr;

	if (hdr->typev1 != SMC_TYPE_R && hdr->typev1 != SMC_TYPE_D)
		return false;
	if (hdr->version == SMC_V1) {
		if ((hdr->typev1 == SMC_TYPE_R &&
		     ntohs(hdr->length) != SMCR_CLC_ACCEPT_CONFIRM_LEN) ||
		    (hdr->typev1 == SMC_TYPE_D &&
		     ntohs(hdr->length) != SMCD_CLC_ACCEPT_CONFIRM_LEN))
			return false;
	} else {
		if (hdr->typev1 == SMC_TYPE_D &&
99 100 101
		    ntohs(hdr->length) != SMCD_CLC_ACCEPT_CONFIRM_LEN_V2 &&
		    (ntohs(hdr->length) != SMCD_CLC_ACCEPT_CONFIRM_LEN_V2 +
				sizeof(struct smc_clc_first_contact_ext)))
U
Ursula Braun 已提交
102 103 104 105 106
			return false;
	}
	return true;
}

107 108 109 110 111 112 113 114 115
static void smc_clc_fill_fce(struct smc_clc_first_contact_ext *fce, int *len)
{
	memset(fce, 0, sizeof(*fce));
	fce->os_type = SMC_CLC_OS_LINUX;
	fce->release = SMC_RELEASE;
	memcpy(fce->hostname, smc_hostname, sizeof(smc_hostname));
	(*len) += sizeof(*fce);
}

116 117 118
/* check if received message has a correct header length and contains valid
 * heading and trailing eyecatchers
 */
119
static bool smc_clc_msg_hdr_valid(struct smc_clc_msg_hdr *clcm, bool check_trl)
120
{
U
Ursula Braun 已提交
121
	struct smc_clc_msg_accept_confirm_v2 *clc_v2;
122 123 124 125
	struct smc_clc_msg_proposal *pclc;
	struct smc_clc_msg_decline *dclc;
	struct smc_clc_msg_trail *trl;

126 127
	if (memcmp(clcm->eyecatcher, SMC_EYECATCHER, sizeof(SMC_EYECATCHER)) &&
	    memcmp(clcm->eyecatcher, SMCD_EYECATCHER, sizeof(SMCD_EYECATCHER)))
128 129 130 131
		return false;
	switch (clcm->type) {
	case SMC_CLC_PROPOSAL:
		pclc = (struct smc_clc_msg_proposal *)clcm;
132
		if (!smc_clc_msg_prop_valid(pclc))
133 134 135 136 137 138
			return false;
		trl = (struct smc_clc_msg_trail *)
			((u8 *)pclc + ntohs(pclc->hdr.length) - sizeof(*trl));
		break;
	case SMC_CLC_ACCEPT:
	case SMC_CLC_CONFIRM:
U
Ursula Braun 已提交
139 140
		clc_v2 = (struct smc_clc_msg_accept_confirm_v2 *)clcm;
		if (!smc_clc_msg_acc_conf_valid(clc_v2))
141
			return false;
142
		trl = (struct smc_clc_msg_trail *)
U
Ursula Braun 已提交
143 144
			((u8 *)clc_v2 + ntohs(clc_v2->hdr.length) -
							sizeof(*trl));
145 146 147 148 149 150 151 152 153 154
		break;
	case SMC_CLC_DECLINE:
		dclc = (struct smc_clc_msg_decline *)clcm;
		if (ntohs(dclc->hdr.length) != sizeof(*dclc))
			return false;
		trl = &dclc->trl;
		break;
	default:
		return false;
	}
155 156
	if (check_trl &&
	    memcmp(trl->eyecatcher, SMC_EYECATCHER, sizeof(SMC_EYECATCHER)) &&
157
	    memcmp(trl->eyecatcher, SMCD_EYECATCHER, sizeof(SMCD_EYECATCHER)))
158 159 160 161
		return false;
	return true;
}

162 163 164 165 166
/* find ipv4 addr on device and get the prefix len, fill CLC proposal msg */
static int smc_clc_prfx_set4_rcu(struct dst_entry *dst, __be32 ipv4,
				 struct smc_clc_msg_proposal_prefix *prop)
{
	struct in_device *in_dev = __in_dev_get_rcu(dst->dev);
167
	const struct in_ifaddr *ifa;
168 169 170

	if (!in_dev)
		return -ENODEV;
171 172

	in_dev_for_each_ifa_rcu(ifa, in_dev) {
173 174 175 176 177 178
		if (!inet_ifa_match(ipv4, ifa))
			continue;
		prop->prefix_len = inet_mask_len(ifa->ifa_mask);
		prop->outgoing_subnet = ifa->ifa_address & ifa->ifa_mask;
		/* prop->ipv6_prefixes_cnt = 0; already done by memset before */
		return 0;
179
	}
180 181 182
	return -ENOENT;
}

183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
/* fill CLC proposal msg with ipv6 prefixes from device */
static int smc_clc_prfx_set6_rcu(struct dst_entry *dst,
				 struct smc_clc_msg_proposal_prefix *prop,
				 struct smc_clc_ipv6_prefix *ipv6_prfx)
{
#if IS_ENABLED(CONFIG_IPV6)
	struct inet6_dev *in6_dev = __in6_dev_get(dst->dev);
	struct inet6_ifaddr *ifa;
	int cnt = 0;

	if (!in6_dev)
		return -ENODEV;
	/* use a maximum of 8 IPv6 prefixes from device */
	list_for_each_entry(ifa, &in6_dev->addr_list, if_list) {
		if (ipv6_addr_type(&ifa->addr) & IPV6_ADDR_LINKLOCAL)
			continue;
		ipv6_addr_prefix(&ipv6_prfx[cnt].prefix,
				 &ifa->addr, ifa->prefix_len);
		ipv6_prfx[cnt].prefix_len = ifa->prefix_len;
		cnt++;
		if (cnt == SMC_CLC_MAX_V6_PREFIX)
			break;
	}
	prop->ipv6_prefixes_cnt = cnt;
	if (cnt)
		return 0;
#endif
	return -ENOENT;
}

213 214
/* retrieve and set prefixes in CLC proposal msg */
static int smc_clc_prfx_set(struct socket *clcsock,
215 216
			    struct smc_clc_msg_proposal_prefix *prop,
			    struct smc_clc_ipv6_prefix *ipv6_prfx)
217 218
{
	struct dst_entry *dst = sk_dst_get(clcsock->sk);
219
	struct sockaddr_storage addrs;
220
	struct sockaddr_in6 *addr6;
221
	struct sockaddr_in *addr;
222 223 224 225 226 227 228 229 230 231 232
	int rc = -ENOENT;

	if (!dst) {
		rc = -ENOTCONN;
		goto out;
	}
	if (!dst->dev) {
		rc = -ENODEV;
		goto out_rel;
	}
	/* get address to which the internal TCP socket is bound */
233 234
	kernel_getsockname(clcsock, (struct sockaddr *)&addrs);
	/* analyze IP specific data of net_device belonging to TCP socket */
235
	addr6 = (struct sockaddr_in6 *)&addrs;
236
	rcu_read_lock();
237 238 239 240
	if (addrs.ss_family == PF_INET) {
		/* IPv4 */
		addr = (struct sockaddr_in *)&addrs;
		rc = smc_clc_prfx_set4_rcu(dst, addr->sin_addr.s_addr, prop);
241 242 243 244 245 246 247
	} else if (ipv6_addr_v4mapped(&addr6->sin6_addr)) {
		/* mapped IPv4 address - peer is IPv4 only */
		rc = smc_clc_prfx_set4_rcu(dst, addr6->sin6_addr.s6_addr32[3],
					   prop);
	} else {
		/* IPv6 */
		rc = smc_clc_prfx_set6_rcu(dst, prop, ipv6_prfx);
248 249 250 251 252 253 254 255 256 257 258 259 260
	}
	rcu_read_unlock();
out_rel:
	dst_release(dst);
out:
	return rc;
}

/* match ipv4 addrs of dev against addr in CLC proposal */
static int smc_clc_prfx_match4_rcu(struct net_device *dev,
				   struct smc_clc_msg_proposal_prefix *prop)
{
	struct in_device *in_dev = __in_dev_get_rcu(dev);
261
	const struct in_ifaddr *ifa;
262 263 264

	if (!in_dev)
		return -ENODEV;
265
	in_dev_for_each_ifa_rcu(ifa, in_dev) {
266 267 268
		if (prop->prefix_len == inet_mask_len(ifa->ifa_mask) &&
		    inet_ifa_match(prop->outgoing_subnet, ifa))
			return 0;
269
	}
270

271 272 273
	return -ENOENT;
}

274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
/* match ipv6 addrs of dev against addrs in CLC proposal */
static int smc_clc_prfx_match6_rcu(struct net_device *dev,
				   struct smc_clc_msg_proposal_prefix *prop)
{
#if IS_ENABLED(CONFIG_IPV6)
	struct inet6_dev *in6_dev = __in6_dev_get(dev);
	struct smc_clc_ipv6_prefix *ipv6_prfx;
	struct inet6_ifaddr *ifa;
	int i, max;

	if (!in6_dev)
		return -ENODEV;
	/* ipv6 prefix list starts behind smc_clc_msg_proposal_prefix */
	ipv6_prfx = (struct smc_clc_ipv6_prefix *)((u8 *)prop + sizeof(*prop));
	max = min_t(u8, prop->ipv6_prefixes_cnt, SMC_CLC_MAX_V6_PREFIX);
	list_for_each_entry(ifa, &in6_dev->addr_list, if_list) {
		if (ipv6_addr_type(&ifa->addr) & IPV6_ADDR_LINKLOCAL)
			continue;
		for (i = 0; i < max; i++) {
			if (ifa->prefix_len == ipv6_prfx[i].prefix_len &&
			    ipv6_prefix_equal(&ifa->addr, &ipv6_prfx[i].prefix,
					      ifa->prefix_len))
				return 0;
		}
	}
#endif
	return -ENOENT;
}

303 304 305 306 307
/* check if proposed prefixes match one of our device prefixes */
int smc_clc_prfx_match(struct socket *clcsock,
		       struct smc_clc_msg_proposal_prefix *prop)
{
	struct dst_entry *dst = sk_dst_get(clcsock->sk);
308
	int rc;
309 310 311 312 313 314 315 316 317 318 319 320

	if (!dst) {
		rc = -ENOTCONN;
		goto out;
	}
	if (!dst->dev) {
		rc = -ENODEV;
		goto out_rel;
	}
	rcu_read_lock();
	if (!prop->ipv6_prefixes_cnt)
		rc = smc_clc_prfx_match4_rcu(dst->dev, prop);
321 322
	else
		rc = smc_clc_prfx_match6_rcu(dst->dev, prop);
323
	rcu_read_unlock();
324 325 326 327 328 329
out_rel:
	dst_release(dst);
out:
	return rc;
}

330 331 332 333 334 335 336
/* Wait for data on the tcp-socket, analyze received data
 * Returns:
 * 0 if success and it was not a decline that we received.
 * SMC_CLC_DECL_REPLY if decline received for fallback w/o another decl send.
 * clcsock error, -EINTR, -ECONNRESET, -EPROTO otherwise.
 */
int smc_clc_wait_msg(struct smc_sock *smc, void *buf, int buflen,
337
		     u8 expected_type, unsigned long timeout)
338
{
339
	long rcvtimeo = smc->clcsock->sk->sk_rcvtimeo;
340 341 342 343
	struct sock *clc_sk = smc->clcsock->sk;
	struct smc_clc_msg_hdr *clcm = buf;
	struct msghdr msg = {NULL, 0};
	int reason_code = 0;
A
Al Viro 已提交
344
	struct kvec vec = {buf, buflen};
345 346
	int len, datlen, recvlen;
	bool check_trl = true;
347 348 349 350 351 352
	int krflags;

	/* peek the first few bytes to determine length of data to receive
	 * so we don't consume any subsequent CLC message or payload data
	 * in the TCP byte stream
	 */
A
Al Viro 已提交
353 354 355 356
	/*
	 * Caller must make sure that buflen is no less than
	 * sizeof(struct smc_clc_msg_hdr)
	 */
357
	krflags = MSG_PEEK | MSG_WAITALL;
358
	clc_sk->sk_rcvtimeo = timeout;
359
	iov_iter_kvec(&msg.msg_iter, READ, &vec, 1,
A
Al Viro 已提交
360 361
			sizeof(struct smc_clc_msg_hdr));
	len = sock_recvmsg(smc->clcsock, &msg, krflags);
362 363 364 365 366 367 368 369
	if (signal_pending(current)) {
		reason_code = -EINTR;
		clc_sk->sk_err = EINTR;
		smc->sk.sk_err = EINTR;
		goto out;
	}
	if (clc_sk->sk_err) {
		reason_code = -clc_sk->sk_err;
370 371 372 373 374
		if (clc_sk->sk_err == EAGAIN &&
		    expected_type == SMC_CLC_DECLINE)
			clc_sk->sk_err = 0; /* reset for fallback usage */
		else
			smc->sk.sk_err = clc_sk->sk_err;
375 376 377 378 379 380 381 382
		goto out;
	}
	if (!len) { /* peer has performed orderly shutdown */
		smc->sk.sk_err = ECONNRESET;
		reason_code = -ECONNRESET;
		goto out;
	}
	if (len < 0) {
383 384
		if (len != -EAGAIN || expected_type != SMC_CLC_DECLINE)
			smc->sk.sk_err = -len;
385 386 387 388 389
		reason_code = len;
		goto out;
	}
	datlen = ntohs(clcm->length);
	if ((len < sizeof(struct smc_clc_msg_hdr)) ||
390
	    (clcm->version < SMC_V1) ||
391 392 393 394 395 396 397 398 399
	    ((clcm->type != SMC_CLC_DECLINE) &&
	     (clcm->type != expected_type))) {
		smc->sk.sk_err = EPROTO;
		reason_code = -EPROTO;
		goto out;
	}

	/* receive the complete CLC message */
	memset(&msg, 0, sizeof(struct msghdr));
400 401 402 403 404 405 406
	if (datlen > buflen) {
		check_trl = false;
		recvlen = buflen;
	} else {
		recvlen = datlen;
	}
	iov_iter_kvec(&msg.msg_iter, READ, &vec, 1, recvlen);
407
	krflags = MSG_WAITALL;
A
Al Viro 已提交
408
	len = sock_recvmsg(smc->clcsock, &msg, krflags);
409
	if (len < recvlen || !smc_clc_msg_hdr_valid(clcm, check_trl)) {
410 411 412 413
		smc->sk.sk_err = EPROTO;
		reason_code = -EPROTO;
		goto out;
	}
414 415 416 417 418 419 420 421 422 423 424 425 426
	datlen -= len;
	while (datlen) {
		u8 tmp[SMC_CLC_RECV_BUF_LEN];

		vec.iov_base = &tmp;
		vec.iov_len = SMC_CLC_RECV_BUF_LEN;
		/* receive remaining proposal message */
		recvlen = datlen > SMC_CLC_RECV_BUF_LEN ?
						SMC_CLC_RECV_BUF_LEN : datlen;
		iov_iter_kvec(&msg.msg_iter, READ, &vec, 1, recvlen);
		len = sock_recvmsg(smc->clcsock, &msg, krflags);
		datlen -= len;
	}
427
	if (clcm->type == SMC_CLC_DECLINE) {
428 429 430 431 432
		struct smc_clc_msg_decline *dclc;

		dclc = (struct smc_clc_msg_decline *)clcm;
		reason_code = SMC_CLC_DECL_PEERDECL;
		smc->peer_diagnosis = ntohl(dclc->peer_diagnosis);
433 434
		if (((struct smc_clc_msg_decline *)buf)->hdr.typev2 &
						SMC_FIRST_CONTACT_MASK) {
435
			smc->conn.lgr->sync_err = 1;
436
			smc_lgr_terminate_sched(smc->conn.lgr);
437
		}
438 439
	}

440
out:
441
	clc_sk->sk_rcvtimeo = rcvtimeo;
442 443 444 445
	return reason_code;
}

/* send CLC DECLINE message across internal TCP socket */
446
int smc_clc_send_decline(struct smc_sock *smc, u32 peer_diag_info)
447 448 449 450 451 452 453 454 455 456
{
	struct smc_clc_msg_decline dclc;
	struct msghdr msg;
	struct kvec vec;
	int len;

	memset(&dclc, 0, sizeof(dclc));
	memcpy(dclc.hdr.eyecatcher, SMC_EYECATCHER, sizeof(SMC_EYECATCHER));
	dclc.hdr.type = SMC_CLC_DECLINE;
	dclc.hdr.length = htons(sizeof(struct smc_clc_msg_decline));
457
	dclc.hdr.version = SMC_V1;
458 459
	dclc.hdr.typev2 = (peer_diag_info == SMC_CLC_DECL_SYNCERR) ?
						SMC_FIRST_CONTACT_MASK : 0;
460 461
	if ((!smc->conn.lgr || !smc->conn.lgr->is_smcd) &&
	    smc_ib_is_valid_local_systemid())
462 463
		memcpy(dclc.id_for_peer, local_systemid,
		       sizeof(local_systemid));
464 465 466 467 468 469 470 471
	dclc.peer_diagnosis = htonl(peer_diag_info);
	memcpy(dclc.trl.eyecatcher, SMC_EYECATCHER, sizeof(SMC_EYECATCHER));

	memset(&msg, 0, sizeof(msg));
	vec.iov_base = &dclc;
	vec.iov_len = sizeof(struct smc_clc_msg_decline);
	len = kernel_sendmsg(smc->clcsock, &msg, &vec, 1,
			     sizeof(struct smc_clc_msg_decline));
472
	if (len < 0 || len < sizeof(struct smc_clc_msg_decline))
473 474
		len = -EPROTO;
	return len > 0 ? 0 : len;
475 476 477
}

/* send CLC PROPOSAL message across internal TCP socket */
478
int smc_clc_send_proposal(struct smc_sock *smc, struct smc_init_info *ini)
479
{
480
	struct smc_clc_smcd_v2_extension *smcd_v2_ext;
481 482
	struct smc_clc_msg_proposal_prefix *pclc_prfx;
	struct smc_clc_msg_proposal *pclc_base;
483
	struct smc_clc_smcd_gid_chid *gidchids;
484 485
	struct smc_clc_msg_proposal_area *pclc;
	struct smc_clc_ipv6_prefix *ipv6_prfx;
486
	struct smc_clc_v2_extension *v2_ext;
487 488
	struct smc_clc_msg_smcd *pclc_smcd;
	struct smc_clc_msg_trail *trl;
489
	int len, i, plen, rc;
490
	int reason_code = 0;
491
	struct kvec vec[8];
492 493
	struct msghdr msg;

494 495 496 497 498 499 500 501
	pclc = kzalloc(sizeof(*pclc), GFP_KERNEL);
	if (!pclc)
		return -ENOMEM;

	pclc_base = &pclc->pclc_base;
	pclc_smcd = &pclc->pclc_smcd;
	pclc_prfx = &pclc->pclc_prfx;
	ipv6_prfx = pclc->pclc_prfx_ipv6;
502 503 504
	v2_ext = &pclc->pclc_v2_ext;
	smcd_v2_ext = &pclc->pclc_smcd_v2_ext;
	gidchids = pclc->pclc_gidchids;
505 506
	trl = &pclc->pclc_trl;

507 508 509 510 511
	pclc_base->hdr.version = SMC_V2;
	pclc_base->hdr.typev1 = ini->smc_type_v1;
	pclc_base->hdr.typev2 = ini->smc_type_v2;
	plen = sizeof(*pclc_base) + sizeof(*pclc_smcd) + sizeof(*trl);

512
	/* retrieve ip prefixes for CLC proposal msg */
513 514 515 516 517 518 519 520 521 522 523 524 525 526
	if (ini->smc_type_v1 != SMC_TYPE_N) {
		rc = smc_clc_prfx_set(smc->clcsock, pclc_prfx, ipv6_prfx);
		if (rc) {
			if (ini->smc_type_v2 == SMC_TYPE_N) {
				kfree(pclc);
				return SMC_CLC_DECL_CNFERR;
			}
			pclc_base->hdr.typev1 = SMC_TYPE_N;
		} else {
			pclc_base->iparea_offset = htons(sizeof(*pclc_smcd));
			plen += sizeof(*pclc_prfx) +
					pclc_prfx->ipv6_prefixes_cnt *
					sizeof(ipv6_prfx[0]);
		}
527
	}
528

529
	/* build SMC Proposal CLC message */
530 531 532
	memcpy(pclc_base->hdr.eyecatcher, SMC_EYECATCHER,
	       sizeof(SMC_EYECATCHER));
	pclc_base->hdr.type = SMC_CLC_PROPOSAL;
533
	if (smcr_indicated(ini->smc_type_v1)) {
534
		/* add SMC-R specifics */
535
		memcpy(pclc_base->lcl.id_for_peer, local_systemid,
536
		       sizeof(local_systemid));
537 538
		memcpy(pclc_base->lcl.gid, ini->ib_gid, SMC_GID_SIZE);
		memcpy(pclc_base->lcl.mac, &ini->ib_dev->mac[ini->ib_port - 1],
539
		       ETH_ALEN);
540
	}
541
	if (smcd_indicated(ini->smc_type_v1)) {
542
		/* add SMC-D specifics */
543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586
		if (ini->ism_dev[0]) {
			pclc_smcd->ism.gid = htonll(ini->ism_dev[0]->local_gid);
			pclc_smcd->ism.chid =
				htons(smc_ism_get_chid(ini->ism_dev[0]));
		}
	}
	if (ini->smc_type_v2 == SMC_TYPE_N) {
		pclc_smcd->v2_ext_offset = 0;
	} else {
		u16 v2_ext_offset;
		u8 *eid = NULL;

		v2_ext_offset = sizeof(*pclc_smcd) -
			offsetofend(struct smc_clc_msg_smcd, v2_ext_offset);
		if (ini->smc_type_v1 != SMC_TYPE_N)
			v2_ext_offset += sizeof(*pclc_prfx) +
						pclc_prfx->ipv6_prefixes_cnt *
						sizeof(ipv6_prfx[0]);
		pclc_smcd->v2_ext_offset = htons(v2_ext_offset);
		v2_ext->hdr.eid_cnt = 0;
		v2_ext->hdr.ism_gid_cnt = ini->ism_offered_cnt;
		v2_ext->hdr.flag.release = SMC_RELEASE;
		v2_ext->hdr.flag.seid = 1;
		v2_ext->hdr.smcd_v2_ext_offset = htons(sizeof(*v2_ext) -
				offsetofend(struct smc_clnt_opts_area_hdr,
					    smcd_v2_ext_offset) +
				v2_ext->hdr.eid_cnt * SMC_MAX_EID_LEN);
		if (ini->ism_dev[0])
			smc_ism_get_system_eid(ini->ism_dev[0], &eid);
		else
			smc_ism_get_system_eid(ini->ism_dev[1], &eid);
		if (eid)
			memcpy(smcd_v2_ext->system_eid, eid, SMC_MAX_EID_LEN);
		plen += sizeof(*v2_ext) + sizeof(*smcd_v2_ext);
		if (ini->ism_offered_cnt) {
			for (i = 1; i <= ini->ism_offered_cnt; i++) {
				gidchids[i - 1].gid =
					htonll(ini->ism_dev[i]->local_gid);
				gidchids[i - 1].chid =
					htons(smc_ism_get_chid(ini->ism_dev[i]));
			}
			plen += ini->ism_offered_cnt *
				sizeof(struct smc_clc_smcd_gid_chid);
		}
587
	}
588 589
	pclc_base->hdr.length = htons(plen);
	memcpy(trl->eyecatcher, SMC_EYECATCHER, sizeof(SMC_EYECATCHER));
590 591

	/* send SMC Proposal CLC message */
592
	memset(&msg, 0, sizeof(msg));
593
	i = 0;
594 595
	vec[i].iov_base = pclc_base;
	vec[i++].iov_len = sizeof(*pclc_base);
596 597 598 599 600 601 602 603 604 605
	vec[i].iov_base = pclc_smcd;
	vec[i++].iov_len = sizeof(*pclc_smcd);
	if (ini->smc_type_v1 != SMC_TYPE_N) {
		vec[i].iov_base = pclc_prfx;
		vec[i++].iov_len = sizeof(*pclc_prfx);
		if (pclc_prfx->ipv6_prefixes_cnt > 0) {
			vec[i].iov_base = ipv6_prfx;
			vec[i++].iov_len = pclc_prfx->ipv6_prefixes_cnt *
					   sizeof(ipv6_prfx[0]);
		}
606
	}
607 608 609 610 611 612 613 614 615 616
	if (ini->smc_type_v2 != SMC_TYPE_N) {
		vec[i].iov_base = v2_ext;
		vec[i++].iov_len = sizeof(*v2_ext);
		vec[i].iov_base = smcd_v2_ext;
		vec[i++].iov_len = sizeof(*smcd_v2_ext);
		if (ini->ism_offered_cnt) {
			vec[i].iov_base = gidchids;
			vec[i++].iov_len = ini->ism_offered_cnt *
					sizeof(struct smc_clc_smcd_gid_chid);
		}
617
	}
618 619
	vec[i].iov_base = trl;
	vec[i++].iov_len = sizeof(*trl);
620
	/* due to the few bytes needed for clc-handshake this cannot block */
621
	len = kernel_sendmsg(smc->clcsock, &msg, vec, i, plen);
622 623 624
	if (len < 0) {
		smc->sk.sk_err = smc->clcsock->sk->sk_err;
		reason_code = -smc->sk.sk_err;
625
	} else if (len < ntohs(pclc_base->hdr.length)) {
626 627
		reason_code = -ENETUNREACH;
		smc->sk.sk_err = -reason_code;
628 629
	}

630
	kfree(pclc);
631 632 633
	return reason_code;
}

634 635
/* build and send CLC CONFIRM / ACCEPT message */
static int smc_clc_send_confirm_accept(struct smc_sock *smc,
U
Ursula Braun 已提交
636 637
				       struct smc_clc_msg_accept_confirm_v2 *clc_v2,
				       int first_contact, u8 version)
638
{
639
	struct smc_connection *conn = &smc->conn;
U
Ursula Braun 已提交
640
	struct smc_clc_msg_accept_confirm *clc;
641
	struct smc_clc_first_contact_ext fce;
642
	struct smc_clc_msg_trail trl;
643
	struct kvec vec[3];
644
	struct msghdr msg;
645
	int i, len;
646 647

	/* send SMC Confirm CLC msg */
U
Ursula Braun 已提交
648 649
	clc = (struct smc_clc_msg_accept_confirm *)clc_v2;
	clc->hdr.version = version;	/* SMC version */
650
	if (first_contact)
651
		clc->hdr.typev2 |= SMC_FIRST_CONTACT_MASK;
652
	if (conn->lgr->is_smcd) {
653
		/* SMC-D specific settings */
654
		memcpy(clc->hdr.eyecatcher, SMCD_EYECATCHER,
655
		       sizeof(SMCD_EYECATCHER));
656
		clc->hdr.typev1 = SMC_TYPE_D;
657 658 659 660 661
		clc->d0.gid = conn->lgr->smcd->local_gid;
		clc->d0.token = conn->rmb_desc->token;
		clc->d0.dmbe_size = conn->rmbe_size_short;
		clc->d0.dmbe_idx = 0;
		memcpy(&clc->d0.linkid, conn->lgr->id, SMC_LGR_ID_SIZE);
U
Ursula Braun 已提交
662 663 664 665 666 667 668 669 670
		if (version == SMC_V1) {
			clc->hdr.length = htons(SMCD_CLC_ACCEPT_CONFIRM_LEN);
		} else {
			u8 *eid = NULL;

			clc_v2->chid = htons(smc_ism_get_chid(conn->lgr->smcd));
			smc_ism_get_system_eid(conn->lgr->smcd, &eid);
			if (eid)
				memcpy(clc_v2->eid, eid, SMC_MAX_EID_LEN);
671 672 673 674
			len = SMCD_CLC_ACCEPT_CONFIRM_LEN_V2;
			if (first_contact)
				smc_clc_fill_fce(&fce, &len);
			clc_v2->hdr.length = htons(len);
U
Ursula Braun 已提交
675
		}
676
		memcpy(trl.eyecatcher, SMCD_EYECATCHER,
677 678
		       sizeof(SMCD_EYECATCHER));
	} else {
679 680
		struct smc_link *link = conn->lnk;

681
		/* SMC-R specific settings */
682
		link = conn->lnk;
683
		memcpy(clc->hdr.eyecatcher, SMC_EYECATCHER,
684
		       sizeof(SMC_EYECATCHER));
685
		clc->hdr.typev1 = SMC_TYPE_R;
686 687
		clc->hdr.length = htons(SMCR_CLC_ACCEPT_CONFIRM_LEN);
		memcpy(clc->r0.lcl.id_for_peer, local_systemid,
688
		       sizeof(local_systemid));
689 690
		memcpy(&clc->r0.lcl.gid, link->gid, SMC_GID_SIZE);
		memcpy(&clc->r0.lcl.mac, &link->smcibdev->mac[link->ibport - 1],
691
		       ETH_ALEN);
692 693
		hton24(clc->r0.qpn, link->roce_qp->qp_num);
		clc->r0.rmb_rkey =
694
			htonl(conn->rmb_desc->mr_rx[link->link_idx]->rkey);
695 696 697 698 699 700 701 702 703 704 705 706
		clc->r0.rmbe_idx = 1; /* for now: 1 RMB = 1 RMBE */
		clc->r0.rmbe_alert_token = htonl(conn->alert_token_local);
		switch (clc->hdr.type) {
		case SMC_CLC_ACCEPT:
			clc->r0.qp_mtu = link->path_mtu;
			break;
		case SMC_CLC_CONFIRM:
			clc->r0.qp_mtu = min(link->path_mtu, link->peer_mtu);
			break;
		}
		clc->r0.rmbe_size = conn->rmbe_size_short;
		clc->r0.rmb_dma_addr = cpu_to_be64((u64)sg_dma_address
707
				(conn->rmb_desc->sgt[link->link_idx].sgl));
708
		hton24(clc->r0.psn, link->psn_initial);
709
		memcpy(trl.eyecatcher, SMC_EYECATCHER, sizeof(SMC_EYECATCHER));
710
	}
711 712

	memset(&msg, 0, sizeof(msg));
713
	i = 0;
U
Ursula Braun 已提交
714 715 716 717 718 719 720 721
	vec[i].iov_base = clc_v2;
	if (version > SMC_V1)
		vec[i++].iov_len = SMCD_CLC_ACCEPT_CONFIRM_LEN_V2 - sizeof(trl);
	else
		vec[i++].iov_len = (clc->hdr.typev1 == SMC_TYPE_D ?
						SMCD_CLC_ACCEPT_CONFIRM_LEN :
						SMCR_CLC_ACCEPT_CONFIRM_LEN) -
				   sizeof(trl);
722 723 724 725
	if (version > SMC_V1 && first_contact) {
		vec[i].iov_base = &fce;
		vec[i++].iov_len = sizeof(fce);
	}
726 727 728
	vec[i].iov_base = &trl;
	vec[i++].iov_len = sizeof(trl);
	return kernel_sendmsg(smc->clcsock, &msg, vec, 1,
729 730 731 732
			      ntohs(clc->hdr.length));
}

/* send CLC CONFIRM message across internal TCP socket */
U
Ursula Braun 已提交
733 734
int smc_clc_send_confirm(struct smc_sock *smc, bool clnt_first_contact,
			 u8 version)
735
{
U
Ursula Braun 已提交
736
	struct smc_clc_msg_accept_confirm_v2 cclc_v2;
737 738 739 740
	int reason_code = 0;
	int len;

	/* send SMC Confirm CLC msg */
U
Ursula Braun 已提交
741 742 743 744 745
	memset(&cclc_v2, 0, sizeof(cclc_v2));
	cclc_v2.hdr.type = SMC_CLC_CONFIRM;
	len = smc_clc_send_confirm_accept(smc, &cclc_v2, clnt_first_contact,
					  version);
	if (len < ntohs(cclc_v2.hdr.length)) {
746 747 748 749 750 751 752 753 754 755 756 757
		if (len >= 0) {
			reason_code = -ENETUNREACH;
			smc->sk.sk_err = -reason_code;
		} else {
			smc->sk.sk_err = smc->clcsock->sk->sk_err;
			reason_code = -smc->sk.sk_err;
		}
	}
	return reason_code;
}

/* send CLC ACCEPT message across internal TCP socket */
U
Ursula Braun 已提交
758 759
int smc_clc_send_accept(struct smc_sock *new_smc, bool srv_first_contact,
			u8 version)
760
{
U
Ursula Braun 已提交
761
	struct smc_clc_msg_accept_confirm_v2 aclc_v2;
762 763
	int len;

U
Ursula Braun 已提交
764 765 766 767 768
	memset(&aclc_v2, 0, sizeof(aclc_v2));
	aclc_v2.hdr.type = SMC_CLC_ACCEPT;
	len = smc_clc_send_confirm_accept(new_smc, &aclc_v2, srv_first_contact,
					  version);
	if (len < ntohs(aclc_v2.hdr.length))
769
		len = len >= 0 ? -EPROTO : -new_smc->clcsock->sk->sk_err;
770

771
	return len > 0 ? 0 : len;
772
}
773 774 775 776 777 778 779 780 781 782

void __init smc_clc_init(void)
{
	struct new_utsname *u;

	memset(smc_hostname, _S, sizeof(smc_hostname)); /* ASCII blanks */
	u = utsname();
	memcpy(smc_hostname, u->nodename,
	       min_t(size_t, strlen(u->nodename), sizeof(smc_hostname)));
}