xref: /linux/net/smc/smc_clc.c (revision ad30469a841b50dbb541df4d6971d891f703c297)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *  Shared Memory Communications over RDMA (SMC-R) and RoCE
4  *
5  *  CLC (connection layer control) handshake over initial TCP socket to
6  *  prepare for RDMA traffic
7  *
8  *  Copyright IBM Corp. 2016, 2018
9  *
10  *  Author(s):  Ursula Braun <ubraun@linux.vnet.ibm.com>
11  */
12 
13 #include <linux/in.h>
14 #include <linux/inetdevice.h>
15 #include <linux/if_ether.h>
16 #include <linux/sched/signal.h>
17 #include <linux/utsname.h>
18 #include <linux/ctype.h>
19 
20 #include <net/addrconf.h>
21 #include <net/sock.h>
22 #include <net/tcp.h>
23 
24 #include "smc.h"
25 #include "smc_core.h"
26 #include "smc_clc.h"
27 #include "smc_ib.h"
28 #include "smc_ism.h"
29 #include "smc_netlink.h"
30 
31 #define SMCR_CLC_ACCEPT_CONFIRM_LEN 68
32 #define SMCD_CLC_ACCEPT_CONFIRM_LEN 48
33 #define SMCD_CLC_ACCEPT_CONFIRM_LEN_V2 78
34 #define SMCR_CLC_ACCEPT_CONFIRM_LEN_V2 108
35 #define SMC_CLC_RECV_BUF_LEN	100
36 
37 /* eye catcher "SMCR" EBCDIC for CLC messages */
38 static const char SMC_EYECATCHER[4] = {'\xe2', '\xd4', '\xc3', '\xd9'};
39 /* eye catcher "SMCD" EBCDIC for CLC messages */
40 static const char SMCD_EYECATCHER[4] = {'\xe2', '\xd4', '\xc3', '\xc4'};
41 
42 static u8 smc_hostname[SMC_MAX_HOSTNAME_LEN];
43 
44 struct smc_clc_eid_table {
45 	rwlock_t lock;
46 	struct list_head list;
47 	u8 ueid_cnt;
48 	u8 seid_enabled;
49 };
50 
51 static struct smc_clc_eid_table smc_clc_eid_table;
52 
53 struct smc_clc_eid_entry {
54 	struct list_head list;
55 	u8 eid[SMC_MAX_EID_LEN];
56 };
57 
58 /* The size of a user EID is 32 characters.
59  * Valid characters should be (single-byte character set) A-Z, 0-9, '.' and '-'.
60  * Blanks should only be used to pad to the expected size.
61  * First character must be alphanumeric.
62  */
63 static bool smc_clc_ueid_valid(char *ueid)
64 {
65 	char *end = ueid + SMC_MAX_EID_LEN;
66 
67 	while (--end >= ueid && isspace(*end))
68 		;
69 	if (end < ueid)
70 		return false;
71 	if (!isalnum(*ueid) || islower(*ueid))
72 		return false;
73 	while (ueid <= end) {
74 		if ((!isalnum(*ueid) || islower(*ueid)) && *ueid != '.' &&
75 		    *ueid != '-')
76 			return false;
77 		ueid++;
78 	}
79 	return true;
80 }
81 
82 static int smc_clc_ueid_add(char *ueid)
83 {
84 	struct smc_clc_eid_entry *new_ueid, *tmp_ueid;
85 	int rc;
86 
87 	if (!smc_clc_ueid_valid(ueid))
88 		return -EINVAL;
89 
90 	/* add a new ueid entry to the ueid table if there isn't one */
91 	new_ueid = kzalloc(sizeof(*new_ueid), GFP_KERNEL);
92 	if (!new_ueid)
93 		return -ENOMEM;
94 	memcpy(new_ueid->eid, ueid, SMC_MAX_EID_LEN);
95 
96 	write_lock(&smc_clc_eid_table.lock);
97 	if (smc_clc_eid_table.ueid_cnt >= SMC_MAX_UEID) {
98 		rc = -ERANGE;
99 		goto err_out;
100 	}
101 	list_for_each_entry(tmp_ueid, &smc_clc_eid_table.list, list) {
102 		if (!memcmp(tmp_ueid->eid, ueid, SMC_MAX_EID_LEN)) {
103 			rc = -EEXIST;
104 			goto err_out;
105 		}
106 	}
107 	list_add_tail(&new_ueid->list, &smc_clc_eid_table.list);
108 	smc_clc_eid_table.ueid_cnt++;
109 	write_unlock(&smc_clc_eid_table.lock);
110 	return 0;
111 
112 err_out:
113 	write_unlock(&smc_clc_eid_table.lock);
114 	kfree(new_ueid);
115 	return rc;
116 }
117 
118 int smc_clc_ueid_count(void)
119 {
120 	int count;
121 
122 	read_lock(&smc_clc_eid_table.lock);
123 	count = smc_clc_eid_table.ueid_cnt;
124 	read_unlock(&smc_clc_eid_table.lock);
125 
126 	return count;
127 }
128 
129 int smc_nl_add_ueid(struct sk_buff *skb, struct genl_info *info)
130 {
131 	struct nlattr *nla_ueid = info->attrs[SMC_NLA_EID_TABLE_ENTRY];
132 	char *ueid;
133 
134 	if (!nla_ueid || nla_len(nla_ueid) != SMC_MAX_EID_LEN + 1)
135 		return -EINVAL;
136 	ueid = (char *)nla_data(nla_ueid);
137 
138 	return smc_clc_ueid_add(ueid);
139 }
140 
141 /* remove one or all ueid entries from the table */
142 static int smc_clc_ueid_remove(char *ueid)
143 {
144 	struct smc_clc_eid_entry *lst_ueid, *tmp_ueid;
145 	int rc = -ENOENT;
146 
147 	/* remove table entry */
148 	write_lock(&smc_clc_eid_table.lock);
149 	list_for_each_entry_safe(lst_ueid, tmp_ueid, &smc_clc_eid_table.list,
150 				 list) {
151 		if (!ueid || !memcmp(lst_ueid->eid, ueid, SMC_MAX_EID_LEN)) {
152 			list_del(&lst_ueid->list);
153 			smc_clc_eid_table.ueid_cnt--;
154 			kfree(lst_ueid);
155 			rc = 0;
156 		}
157 	}
158 	if (!rc && !smc_clc_eid_table.ueid_cnt) {
159 		smc_clc_eid_table.seid_enabled = 1;
160 		rc = -EAGAIN;	/* indicate success and enabling of seid */
161 	}
162 	write_unlock(&smc_clc_eid_table.lock);
163 	return rc;
164 }
165 
166 int smc_nl_remove_ueid(struct sk_buff *skb, struct genl_info *info)
167 {
168 	struct nlattr *nla_ueid = info->attrs[SMC_NLA_EID_TABLE_ENTRY];
169 	char *ueid;
170 
171 	if (!nla_ueid || nla_len(nla_ueid) != SMC_MAX_EID_LEN + 1)
172 		return -EINVAL;
173 	ueid = (char *)nla_data(nla_ueid);
174 
175 	return smc_clc_ueid_remove(ueid);
176 }
177 
178 int smc_nl_flush_ueid(struct sk_buff *skb, struct genl_info *info)
179 {
180 	smc_clc_ueid_remove(NULL);
181 	return 0;
182 }
183 
184 static int smc_nl_ueid_dumpinfo(struct sk_buff *skb, u32 portid, u32 seq,
185 				u32 flags, char *ueid)
186 {
187 	char ueid_str[SMC_MAX_EID_LEN + 1];
188 	void *hdr;
189 
190 	hdr = genlmsg_put(skb, portid, seq, &smc_gen_nl_family,
191 			  flags, SMC_NETLINK_DUMP_UEID);
192 	if (!hdr)
193 		return -ENOMEM;
194 	memcpy(ueid_str, ueid, SMC_MAX_EID_LEN);
195 	ueid_str[SMC_MAX_EID_LEN] = 0;
196 	if (nla_put_string(skb, SMC_NLA_EID_TABLE_ENTRY, ueid_str)) {
197 		genlmsg_cancel(skb, hdr);
198 		return -EMSGSIZE;
199 	}
200 	genlmsg_end(skb, hdr);
201 	return 0;
202 }
203 
204 static int _smc_nl_ueid_dump(struct sk_buff *skb, u32 portid, u32 seq,
205 			     int start_idx)
206 {
207 	struct smc_clc_eid_entry *lst_ueid;
208 	int idx = 0;
209 
210 	read_lock(&smc_clc_eid_table.lock);
211 	list_for_each_entry(lst_ueid, &smc_clc_eid_table.list, list) {
212 		if (idx++ < start_idx)
213 			continue;
214 		if (smc_nl_ueid_dumpinfo(skb, portid, seq, NLM_F_MULTI,
215 					 lst_ueid->eid)) {
216 			--idx;
217 			break;
218 		}
219 	}
220 	read_unlock(&smc_clc_eid_table.lock);
221 	return idx;
222 }
223 
224 int smc_nl_dump_ueid(struct sk_buff *skb, struct netlink_callback *cb)
225 {
226 	struct smc_nl_dmp_ctx *cb_ctx = smc_nl_dmp_ctx(cb);
227 	int idx;
228 
229 	idx = _smc_nl_ueid_dump(skb, NETLINK_CB(cb->skb).portid,
230 				cb->nlh->nlmsg_seq, cb_ctx->pos[0]);
231 
232 	cb_ctx->pos[0] = idx;
233 	return skb->len;
234 }
235 
236 int smc_nl_dump_seid(struct sk_buff *skb, struct netlink_callback *cb)
237 {
238 	struct smc_nl_dmp_ctx *cb_ctx = smc_nl_dmp_ctx(cb);
239 	char seid_str[SMC_MAX_EID_LEN + 1];
240 	u8 seid_enabled;
241 	void *hdr;
242 	u8 *seid;
243 
244 	if (cb_ctx->pos[0])
245 		return skb->len;
246 
247 	hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
248 			  &smc_gen_nl_family, NLM_F_MULTI,
249 			  SMC_NETLINK_DUMP_SEID);
250 	if (!hdr)
251 		return -ENOMEM;
252 	if (!smc_ism_is_v2_capable())
253 		goto end;
254 
255 	smc_ism_get_system_eid(&seid);
256 	memcpy(seid_str, seid, SMC_MAX_EID_LEN);
257 	seid_str[SMC_MAX_EID_LEN] = 0;
258 	if (nla_put_string(skb, SMC_NLA_SEID_ENTRY, seid_str))
259 		goto err;
260 	read_lock(&smc_clc_eid_table.lock);
261 	seid_enabled = smc_clc_eid_table.seid_enabled;
262 	read_unlock(&smc_clc_eid_table.lock);
263 	if (nla_put_u8(skb, SMC_NLA_SEID_ENABLED, seid_enabled))
264 		goto err;
265 end:
266 	genlmsg_end(skb, hdr);
267 	cb_ctx->pos[0]++;
268 	return skb->len;
269 err:
270 	genlmsg_cancel(skb, hdr);
271 	return -EMSGSIZE;
272 }
273 
274 int smc_nl_enable_seid(struct sk_buff *skb, struct genl_info *info)
275 {
276 	write_lock(&smc_clc_eid_table.lock);
277 	smc_clc_eid_table.seid_enabled = 1;
278 	write_unlock(&smc_clc_eid_table.lock);
279 	return 0;
280 }
281 
282 int smc_nl_disable_seid(struct sk_buff *skb, struct genl_info *info)
283 {
284 	int rc = 0;
285 
286 	write_lock(&smc_clc_eid_table.lock);
287 	if (!smc_clc_eid_table.ueid_cnt)
288 		rc = -ENOENT;
289 	else
290 		smc_clc_eid_table.seid_enabled = 0;
291 	write_unlock(&smc_clc_eid_table.lock);
292 	return rc;
293 }
294 
295 static bool _smc_clc_match_ueid(u8 *peer_ueid)
296 {
297 	struct smc_clc_eid_entry *tmp_ueid;
298 
299 	list_for_each_entry(tmp_ueid, &smc_clc_eid_table.list, list) {
300 		if (!memcmp(tmp_ueid->eid, peer_ueid, SMC_MAX_EID_LEN))
301 			return true;
302 	}
303 	return false;
304 }
305 
306 bool smc_clc_match_eid(u8 *negotiated_eid,
307 		       struct smc_clc_v2_extension *smc_v2_ext,
308 		       u8 *peer_eid, u8 *local_eid)
309 {
310 	bool match = false;
311 	int i;
312 
313 	negotiated_eid[0] = 0;
314 	read_lock(&smc_clc_eid_table.lock);
315 	if (peer_eid && local_eid &&
316 	    smc_clc_eid_table.seid_enabled &&
317 	    smc_v2_ext->hdr.flag.seid &&
318 	    !memcmp(peer_eid, local_eid, SMC_MAX_EID_LEN)) {
319 		memcpy(negotiated_eid, peer_eid, SMC_MAX_EID_LEN);
320 		match = true;
321 		goto out;
322 	}
323 
324 	for (i = 0; i < smc_v2_ext->hdr.eid_cnt; i++) {
325 		if (_smc_clc_match_ueid(smc_v2_ext->user_eids[i])) {
326 			memcpy(negotiated_eid, smc_v2_ext->user_eids[i],
327 			       SMC_MAX_EID_LEN);
328 			match = true;
329 			goto out;
330 		}
331 	}
332 out:
333 	read_unlock(&smc_clc_eid_table.lock);
334 	return match;
335 }
336 
337 /* check arriving CLC proposal */
338 static bool smc_clc_msg_prop_valid(struct smc_clc_msg_proposal *pclc)
339 {
340 	struct smc_clc_msg_proposal_prefix *pclc_prfx;
341 	struct smc_clc_smcd_v2_extension *smcd_v2_ext;
342 	struct smc_clc_msg_hdr *hdr = &pclc->hdr;
343 	struct smc_clc_v2_extension *v2_ext;
344 
345 	v2_ext = smc_get_clc_v2_ext(pclc);
346 	pclc_prfx = smc_clc_proposal_get_prefix(pclc);
347 	if (hdr->version == SMC_V1) {
348 		if (hdr->typev1 == SMC_TYPE_N)
349 			return false;
350 		if (ntohs(hdr->length) !=
351 			sizeof(*pclc) + ntohs(pclc->iparea_offset) +
352 			sizeof(*pclc_prfx) +
353 			pclc_prfx->ipv6_prefixes_cnt *
354 				sizeof(struct smc_clc_ipv6_prefix) +
355 			sizeof(struct smc_clc_msg_trail))
356 			return false;
357 	} else {
358 		if (ntohs(hdr->length) !=
359 			sizeof(*pclc) +
360 			sizeof(struct smc_clc_msg_smcd) +
361 			(hdr->typev1 != SMC_TYPE_N ?
362 				sizeof(*pclc_prfx) +
363 				pclc_prfx->ipv6_prefixes_cnt *
364 				sizeof(struct smc_clc_ipv6_prefix) : 0) +
365 			(hdr->typev2 != SMC_TYPE_N ?
366 				sizeof(*v2_ext) +
367 				v2_ext->hdr.eid_cnt * SMC_MAX_EID_LEN : 0) +
368 			(smcd_indicated(hdr->typev2) ?
369 				sizeof(*smcd_v2_ext) + v2_ext->hdr.ism_gid_cnt *
370 					sizeof(struct smc_clc_smcd_gid_chid) :
371 				0) +
372 			sizeof(struct smc_clc_msg_trail))
373 			return false;
374 	}
375 	return true;
376 }
377 
378 /* check arriving CLC accept or confirm */
379 static bool
380 smc_clc_msg_acc_conf_valid(struct smc_clc_msg_accept_confirm_v2 *clc_v2)
381 {
382 	struct smc_clc_msg_hdr *hdr = &clc_v2->hdr;
383 
384 	if (hdr->typev1 != SMC_TYPE_R && hdr->typev1 != SMC_TYPE_D)
385 		return false;
386 	if (hdr->version == SMC_V1) {
387 		if ((hdr->typev1 == SMC_TYPE_R &&
388 		     ntohs(hdr->length) != SMCR_CLC_ACCEPT_CONFIRM_LEN) ||
389 		    (hdr->typev1 == SMC_TYPE_D &&
390 		     ntohs(hdr->length) != SMCD_CLC_ACCEPT_CONFIRM_LEN))
391 			return false;
392 	} else {
393 		if (hdr->typev1 == SMC_TYPE_D &&
394 		    ntohs(hdr->length) < SMCD_CLC_ACCEPT_CONFIRM_LEN_V2)
395 			return false;
396 		if (hdr->typev1 == SMC_TYPE_R &&
397 		    ntohs(hdr->length) < SMCR_CLC_ACCEPT_CONFIRM_LEN_V2)
398 			return false;
399 	}
400 	return true;
401 }
402 
403 /* check arriving CLC decline */
404 static bool
405 smc_clc_msg_decl_valid(struct smc_clc_msg_decline *dclc)
406 {
407 	struct smc_clc_msg_hdr *hdr = &dclc->hdr;
408 
409 	if (hdr->typev1 != SMC_TYPE_R && hdr->typev1 != SMC_TYPE_D)
410 		return false;
411 	if (hdr->version == SMC_V1) {
412 		if (ntohs(hdr->length) != sizeof(struct smc_clc_msg_decline))
413 			return false;
414 	} else {
415 		if (ntohs(hdr->length) != sizeof(struct smc_clc_msg_decline_v2))
416 			return false;
417 	}
418 	return true;
419 }
420 
421 static int smc_clc_fill_fce(struct smc_clc_first_contact_ext_v2x *fce,
422 			    struct smc_init_info *ini)
423 {
424 	int ret = sizeof(*fce);
425 
426 	memset(fce, 0, sizeof(*fce));
427 	fce->fce_v2_base.os_type = SMC_CLC_OS_LINUX;
428 	fce->fce_v2_base.release = ini->release_nr;
429 	memcpy(fce->fce_v2_base.hostname, smc_hostname, sizeof(smc_hostname));
430 	if (ini->is_smcd && ini->release_nr < SMC_RELEASE_1) {
431 		ret = sizeof(struct smc_clc_first_contact_ext);
432 		goto out;
433 	}
434 
435 	if (ini->release_nr >= SMC_RELEASE_1) {
436 		if (!ini->is_smcd) {
437 			fce->max_conns = ini->max_conns;
438 			fce->max_links = ini->max_links;
439 		}
440 	}
441 
442 out:
443 	return ret;
444 }
445 
446 /* check if received message has a correct header length and contains valid
447  * heading and trailing eyecatchers
448  */
449 static bool smc_clc_msg_hdr_valid(struct smc_clc_msg_hdr *clcm, bool check_trl)
450 {
451 	struct smc_clc_msg_accept_confirm_v2 *clc_v2;
452 	struct smc_clc_msg_proposal *pclc;
453 	struct smc_clc_msg_decline *dclc;
454 	struct smc_clc_msg_trail *trl;
455 
456 	if (memcmp(clcm->eyecatcher, SMC_EYECATCHER, sizeof(SMC_EYECATCHER)) &&
457 	    memcmp(clcm->eyecatcher, SMCD_EYECATCHER, sizeof(SMCD_EYECATCHER)))
458 		return false;
459 	switch (clcm->type) {
460 	case SMC_CLC_PROPOSAL:
461 		pclc = (struct smc_clc_msg_proposal *)clcm;
462 		if (!smc_clc_msg_prop_valid(pclc))
463 			return false;
464 		trl = (struct smc_clc_msg_trail *)
465 			((u8 *)pclc + ntohs(pclc->hdr.length) - sizeof(*trl));
466 		break;
467 	case SMC_CLC_ACCEPT:
468 	case SMC_CLC_CONFIRM:
469 		clc_v2 = (struct smc_clc_msg_accept_confirm_v2 *)clcm;
470 		if (!smc_clc_msg_acc_conf_valid(clc_v2))
471 			return false;
472 		trl = (struct smc_clc_msg_trail *)
473 			((u8 *)clc_v2 + ntohs(clc_v2->hdr.length) -
474 							sizeof(*trl));
475 		break;
476 	case SMC_CLC_DECLINE:
477 		dclc = (struct smc_clc_msg_decline *)clcm;
478 		if (!smc_clc_msg_decl_valid(dclc))
479 			return false;
480 		check_trl = false;
481 		break;
482 	default:
483 		return false;
484 	}
485 	if (check_trl &&
486 	    memcmp(trl->eyecatcher, SMC_EYECATCHER, sizeof(SMC_EYECATCHER)) &&
487 	    memcmp(trl->eyecatcher, SMCD_EYECATCHER, sizeof(SMCD_EYECATCHER)))
488 		return false;
489 	return true;
490 }
491 
492 /* find ipv4 addr on device and get the prefix len, fill CLC proposal msg */
493 static int smc_clc_prfx_set4_rcu(struct dst_entry *dst, __be32 ipv4,
494 				 struct smc_clc_msg_proposal_prefix *prop)
495 {
496 	struct in_device *in_dev = __in_dev_get_rcu(dst->dev);
497 	const struct in_ifaddr *ifa;
498 
499 	if (!in_dev)
500 		return -ENODEV;
501 
502 	in_dev_for_each_ifa_rcu(ifa, in_dev) {
503 		if (!inet_ifa_match(ipv4, ifa))
504 			continue;
505 		prop->prefix_len = inet_mask_len(ifa->ifa_mask);
506 		prop->outgoing_subnet = ifa->ifa_address & ifa->ifa_mask;
507 		/* prop->ipv6_prefixes_cnt = 0; already done by memset before */
508 		return 0;
509 	}
510 	return -ENOENT;
511 }
512 
513 /* fill CLC proposal msg with ipv6 prefixes from device */
514 static int smc_clc_prfx_set6_rcu(struct dst_entry *dst,
515 				 struct smc_clc_msg_proposal_prefix *prop,
516 				 struct smc_clc_ipv6_prefix *ipv6_prfx)
517 {
518 #if IS_ENABLED(CONFIG_IPV6)
519 	struct inet6_dev *in6_dev = __in6_dev_get(dst->dev);
520 	struct inet6_ifaddr *ifa;
521 	int cnt = 0;
522 
523 	if (!in6_dev)
524 		return -ENODEV;
525 	/* use a maximum of 8 IPv6 prefixes from device */
526 	list_for_each_entry(ifa, &in6_dev->addr_list, if_list) {
527 		if (ipv6_addr_type(&ifa->addr) & IPV6_ADDR_LINKLOCAL)
528 			continue;
529 		ipv6_addr_prefix(&ipv6_prfx[cnt].prefix,
530 				 &ifa->addr, ifa->prefix_len);
531 		ipv6_prfx[cnt].prefix_len = ifa->prefix_len;
532 		cnt++;
533 		if (cnt == SMC_CLC_MAX_V6_PREFIX)
534 			break;
535 	}
536 	prop->ipv6_prefixes_cnt = cnt;
537 	if (cnt)
538 		return 0;
539 #endif
540 	return -ENOENT;
541 }
542 
543 /* retrieve and set prefixes in CLC proposal msg */
544 static int smc_clc_prfx_set(struct socket *clcsock,
545 			    struct smc_clc_msg_proposal_prefix *prop,
546 			    struct smc_clc_ipv6_prefix *ipv6_prfx)
547 {
548 	struct dst_entry *dst = sk_dst_get(clcsock->sk);
549 	struct sockaddr_storage addrs;
550 	struct sockaddr_in6 *addr6;
551 	struct sockaddr_in *addr;
552 	int rc = -ENOENT;
553 
554 	if (!dst) {
555 		rc = -ENOTCONN;
556 		goto out;
557 	}
558 	if (!dst->dev) {
559 		rc = -ENODEV;
560 		goto out_rel;
561 	}
562 	/* get address to which the internal TCP socket is bound */
563 	if (kernel_getsockname(clcsock, (struct sockaddr *)&addrs) < 0)
564 		goto out_rel;
565 	/* analyze IP specific data of net_device belonging to TCP socket */
566 	addr6 = (struct sockaddr_in6 *)&addrs;
567 	rcu_read_lock();
568 	if (addrs.ss_family == PF_INET) {
569 		/* IPv4 */
570 		addr = (struct sockaddr_in *)&addrs;
571 		rc = smc_clc_prfx_set4_rcu(dst, addr->sin_addr.s_addr, prop);
572 	} else if (ipv6_addr_v4mapped(&addr6->sin6_addr)) {
573 		/* mapped IPv4 address - peer is IPv4 only */
574 		rc = smc_clc_prfx_set4_rcu(dst, addr6->sin6_addr.s6_addr32[3],
575 					   prop);
576 	} else {
577 		/* IPv6 */
578 		rc = smc_clc_prfx_set6_rcu(dst, prop, ipv6_prfx);
579 	}
580 	rcu_read_unlock();
581 out_rel:
582 	dst_release(dst);
583 out:
584 	return rc;
585 }
586 
587 /* match ipv4 addrs of dev against addr in CLC proposal */
588 static int smc_clc_prfx_match4_rcu(struct net_device *dev,
589 				   struct smc_clc_msg_proposal_prefix *prop)
590 {
591 	struct in_device *in_dev = __in_dev_get_rcu(dev);
592 	const struct in_ifaddr *ifa;
593 
594 	if (!in_dev)
595 		return -ENODEV;
596 	in_dev_for_each_ifa_rcu(ifa, in_dev) {
597 		if (prop->prefix_len == inet_mask_len(ifa->ifa_mask) &&
598 		    inet_ifa_match(prop->outgoing_subnet, ifa))
599 			return 0;
600 	}
601 
602 	return -ENOENT;
603 }
604 
605 /* match ipv6 addrs of dev against addrs in CLC proposal */
606 static int smc_clc_prfx_match6_rcu(struct net_device *dev,
607 				   struct smc_clc_msg_proposal_prefix *prop)
608 {
609 #if IS_ENABLED(CONFIG_IPV6)
610 	struct inet6_dev *in6_dev = __in6_dev_get(dev);
611 	struct smc_clc_ipv6_prefix *ipv6_prfx;
612 	struct inet6_ifaddr *ifa;
613 	int i, max;
614 
615 	if (!in6_dev)
616 		return -ENODEV;
617 	/* ipv6 prefix list starts behind smc_clc_msg_proposal_prefix */
618 	ipv6_prfx = (struct smc_clc_ipv6_prefix *)((u8 *)prop + sizeof(*prop));
619 	max = min_t(u8, prop->ipv6_prefixes_cnt, SMC_CLC_MAX_V6_PREFIX);
620 	list_for_each_entry(ifa, &in6_dev->addr_list, if_list) {
621 		if (ipv6_addr_type(&ifa->addr) & IPV6_ADDR_LINKLOCAL)
622 			continue;
623 		for (i = 0; i < max; i++) {
624 			if (ifa->prefix_len == ipv6_prfx[i].prefix_len &&
625 			    ipv6_prefix_equal(&ifa->addr, &ipv6_prfx[i].prefix,
626 					      ifa->prefix_len))
627 				return 0;
628 		}
629 	}
630 #endif
631 	return -ENOENT;
632 }
633 
634 /* check if proposed prefixes match one of our device prefixes */
635 int smc_clc_prfx_match(struct socket *clcsock,
636 		       struct smc_clc_msg_proposal_prefix *prop)
637 {
638 	struct dst_entry *dst = sk_dst_get(clcsock->sk);
639 	int rc;
640 
641 	if (!dst) {
642 		rc = -ENOTCONN;
643 		goto out;
644 	}
645 	if (!dst->dev) {
646 		rc = -ENODEV;
647 		goto out_rel;
648 	}
649 	rcu_read_lock();
650 	if (!prop->ipv6_prefixes_cnt)
651 		rc = smc_clc_prfx_match4_rcu(dst->dev, prop);
652 	else
653 		rc = smc_clc_prfx_match6_rcu(dst->dev, prop);
654 	rcu_read_unlock();
655 out_rel:
656 	dst_release(dst);
657 out:
658 	return rc;
659 }
660 
661 /* Wait for data on the tcp-socket, analyze received data
662  * Returns:
663  * 0 if success and it was not a decline that we received.
664  * SMC_CLC_DECL_REPLY if decline received for fallback w/o another decl send.
665  * clcsock error, -EINTR, -ECONNRESET, -EPROTO otherwise.
666  */
667 int smc_clc_wait_msg(struct smc_sock *smc, void *buf, int buflen,
668 		     u8 expected_type, unsigned long timeout)
669 {
670 	long rcvtimeo = smc->clcsock->sk->sk_rcvtimeo;
671 	struct sock *clc_sk = smc->clcsock->sk;
672 	struct smc_clc_msg_hdr *clcm = buf;
673 	struct msghdr msg = {NULL, 0};
674 	int reason_code = 0;
675 	struct kvec vec = {buf, buflen};
676 	int len, datlen, recvlen;
677 	bool check_trl = true;
678 	int krflags;
679 
680 	/* peek the first few bytes to determine length of data to receive
681 	 * so we don't consume any subsequent CLC message or payload data
682 	 * in the TCP byte stream
683 	 */
684 	/*
685 	 * Caller must make sure that buflen is no less than
686 	 * sizeof(struct smc_clc_msg_hdr)
687 	 */
688 	krflags = MSG_PEEK | MSG_WAITALL;
689 	clc_sk->sk_rcvtimeo = timeout;
690 	iov_iter_kvec(&msg.msg_iter, ITER_DEST, &vec, 1,
691 			sizeof(struct smc_clc_msg_hdr));
692 	len = sock_recvmsg(smc->clcsock, &msg, krflags);
693 	if (signal_pending(current)) {
694 		reason_code = -EINTR;
695 		clc_sk->sk_err = EINTR;
696 		smc->sk.sk_err = EINTR;
697 		goto out;
698 	}
699 	if (clc_sk->sk_err) {
700 		reason_code = -clc_sk->sk_err;
701 		if (clc_sk->sk_err == EAGAIN &&
702 		    expected_type == SMC_CLC_DECLINE)
703 			clc_sk->sk_err = 0; /* reset for fallback usage */
704 		else
705 			smc->sk.sk_err = clc_sk->sk_err;
706 		goto out;
707 	}
708 	if (!len) { /* peer has performed orderly shutdown */
709 		smc->sk.sk_err = ECONNRESET;
710 		reason_code = -ECONNRESET;
711 		goto out;
712 	}
713 	if (len < 0) {
714 		if (len != -EAGAIN || expected_type != SMC_CLC_DECLINE)
715 			smc->sk.sk_err = -len;
716 		reason_code = len;
717 		goto out;
718 	}
719 	datlen = ntohs(clcm->length);
720 	if ((len < sizeof(struct smc_clc_msg_hdr)) ||
721 	    (clcm->version < SMC_V1) ||
722 	    ((clcm->type != SMC_CLC_DECLINE) &&
723 	     (clcm->type != expected_type))) {
724 		smc->sk.sk_err = EPROTO;
725 		reason_code = -EPROTO;
726 		goto out;
727 	}
728 
729 	/* receive the complete CLC message */
730 	memset(&msg, 0, sizeof(struct msghdr));
731 	if (datlen > buflen) {
732 		check_trl = false;
733 		recvlen = buflen;
734 	} else {
735 		recvlen = datlen;
736 	}
737 	iov_iter_kvec(&msg.msg_iter, ITER_DEST, &vec, 1, recvlen);
738 	krflags = MSG_WAITALL;
739 	len = sock_recvmsg(smc->clcsock, &msg, krflags);
740 	if (len < recvlen || !smc_clc_msg_hdr_valid(clcm, check_trl)) {
741 		smc->sk.sk_err = EPROTO;
742 		reason_code = -EPROTO;
743 		goto out;
744 	}
745 	datlen -= len;
746 	while (datlen) {
747 		u8 tmp[SMC_CLC_RECV_BUF_LEN];
748 
749 		vec.iov_base = &tmp;
750 		vec.iov_len = SMC_CLC_RECV_BUF_LEN;
751 		/* receive remaining proposal message */
752 		recvlen = datlen > SMC_CLC_RECV_BUF_LEN ?
753 						SMC_CLC_RECV_BUF_LEN : datlen;
754 		iov_iter_kvec(&msg.msg_iter, ITER_DEST, &vec, 1, recvlen);
755 		len = sock_recvmsg(smc->clcsock, &msg, krflags);
756 		datlen -= len;
757 	}
758 	if (clcm->type == SMC_CLC_DECLINE) {
759 		struct smc_clc_msg_decline *dclc;
760 
761 		dclc = (struct smc_clc_msg_decline *)clcm;
762 		reason_code = SMC_CLC_DECL_PEERDECL;
763 		smc->peer_diagnosis = ntohl(dclc->peer_diagnosis);
764 		if (((struct smc_clc_msg_decline *)buf)->hdr.typev2 &
765 						SMC_FIRST_CONTACT_MASK) {
766 			smc->conn.lgr->sync_err = 1;
767 			smc_lgr_terminate_sched(smc->conn.lgr);
768 		}
769 	}
770 
771 out:
772 	clc_sk->sk_rcvtimeo = rcvtimeo;
773 	return reason_code;
774 }
775 
776 /* send CLC DECLINE message across internal TCP socket */
777 int smc_clc_send_decline(struct smc_sock *smc, u32 peer_diag_info, u8 version)
778 {
779 	struct smc_clc_msg_decline *dclc_v1;
780 	struct smc_clc_msg_decline_v2 dclc;
781 	struct msghdr msg;
782 	int len, send_len;
783 	struct kvec vec;
784 
785 	dclc_v1 = (struct smc_clc_msg_decline *)&dclc;
786 	memset(&dclc, 0, sizeof(dclc));
787 	memcpy(dclc.hdr.eyecatcher, SMC_EYECATCHER, sizeof(SMC_EYECATCHER));
788 	dclc.hdr.type = SMC_CLC_DECLINE;
789 	dclc.hdr.version = version;
790 	dclc.os_type = version == SMC_V1 ? 0 : SMC_CLC_OS_LINUX;
791 	dclc.hdr.typev2 = (peer_diag_info == SMC_CLC_DECL_SYNCERR) ?
792 						SMC_FIRST_CONTACT_MASK : 0;
793 	if ((!smc_conn_lgr_valid(&smc->conn) || !smc->conn.lgr->is_smcd) &&
794 	    smc_ib_is_valid_local_systemid())
795 		memcpy(dclc.id_for_peer, local_systemid,
796 		       sizeof(local_systemid));
797 	dclc.peer_diagnosis = htonl(peer_diag_info);
798 	if (version == SMC_V1) {
799 		memcpy(dclc_v1->trl.eyecatcher, SMC_EYECATCHER,
800 		       sizeof(SMC_EYECATCHER));
801 		send_len = sizeof(*dclc_v1);
802 	} else {
803 		memcpy(dclc.trl.eyecatcher, SMC_EYECATCHER,
804 		       sizeof(SMC_EYECATCHER));
805 		send_len = sizeof(dclc);
806 	}
807 	dclc.hdr.length = htons(send_len);
808 
809 	memset(&msg, 0, sizeof(msg));
810 	vec.iov_base = &dclc;
811 	vec.iov_len = send_len;
812 	len = kernel_sendmsg(smc->clcsock, &msg, &vec, 1, send_len);
813 	if (len < 0 || len < send_len)
814 		len = -EPROTO;
815 	return len > 0 ? 0 : len;
816 }
817 
818 /* send CLC PROPOSAL message across internal TCP socket */
819 int smc_clc_send_proposal(struct smc_sock *smc, struct smc_init_info *ini)
820 {
821 	struct smc_clc_smcd_v2_extension *smcd_v2_ext;
822 	struct smc_clc_msg_proposal_prefix *pclc_prfx;
823 	struct smc_clc_msg_proposal *pclc_base;
824 	struct smc_clc_smcd_gid_chid *gidchids;
825 	struct smc_clc_msg_proposal_area *pclc;
826 	struct smc_clc_ipv6_prefix *ipv6_prfx;
827 	struct smc_clc_v2_extension *v2_ext;
828 	struct smc_clc_msg_smcd *pclc_smcd;
829 	struct smc_clc_msg_trail *trl;
830 	struct smcd_dev *smcd;
831 	int len, i, plen, rc;
832 	int reason_code = 0;
833 	struct kvec vec[8];
834 	struct msghdr msg;
835 
836 	pclc = kzalloc(sizeof(*pclc), GFP_KERNEL);
837 	if (!pclc)
838 		return -ENOMEM;
839 
840 	pclc_base = &pclc->pclc_base;
841 	pclc_smcd = &pclc->pclc_smcd;
842 	pclc_prfx = &pclc->pclc_prfx;
843 	ipv6_prfx = pclc->pclc_prfx_ipv6;
844 	v2_ext = &pclc->pclc_v2_ext;
845 	smcd_v2_ext = &pclc->pclc_smcd_v2_ext;
846 	gidchids = pclc->pclc_gidchids;
847 	trl = &pclc->pclc_trl;
848 
849 	pclc_base->hdr.version = SMC_V2;
850 	pclc_base->hdr.typev1 = ini->smc_type_v1;
851 	pclc_base->hdr.typev2 = ini->smc_type_v2;
852 	plen = sizeof(*pclc_base) + sizeof(*pclc_smcd) + sizeof(*trl);
853 
854 	/* retrieve ip prefixes for CLC proposal msg */
855 	if (ini->smc_type_v1 != SMC_TYPE_N) {
856 		rc = smc_clc_prfx_set(smc->clcsock, pclc_prfx, ipv6_prfx);
857 		if (rc) {
858 			if (ini->smc_type_v2 == SMC_TYPE_N) {
859 				kfree(pclc);
860 				return SMC_CLC_DECL_CNFERR;
861 			}
862 			pclc_base->hdr.typev1 = SMC_TYPE_N;
863 		} else {
864 			pclc_base->iparea_offset = htons(sizeof(*pclc_smcd));
865 			plen += sizeof(*pclc_prfx) +
866 					pclc_prfx->ipv6_prefixes_cnt *
867 					sizeof(ipv6_prfx[0]);
868 		}
869 	}
870 
871 	/* build SMC Proposal CLC message */
872 	memcpy(pclc_base->hdr.eyecatcher, SMC_EYECATCHER,
873 	       sizeof(SMC_EYECATCHER));
874 	pclc_base->hdr.type = SMC_CLC_PROPOSAL;
875 	if (smcr_indicated(ini->smc_type_v1)) {
876 		/* add SMC-R specifics */
877 		memcpy(pclc_base->lcl.id_for_peer, local_systemid,
878 		       sizeof(local_systemid));
879 		memcpy(pclc_base->lcl.gid, ini->ib_gid, SMC_GID_SIZE);
880 		memcpy(pclc_base->lcl.mac, &ini->ib_dev->mac[ini->ib_port - 1],
881 		       ETH_ALEN);
882 	}
883 	if (smcd_indicated(ini->smc_type_v1)) {
884 		/* add SMC-D specifics */
885 		if (ini->ism_dev[0]) {
886 			smcd = ini->ism_dev[0];
887 			pclc_smcd->ism.gid =
888 				htonll(smcd->ops->get_local_gid(smcd));
889 			pclc_smcd->ism.chid =
890 				htons(smc_ism_get_chid(ini->ism_dev[0]));
891 		}
892 	}
893 	if (ini->smc_type_v2 == SMC_TYPE_N) {
894 		pclc_smcd->v2_ext_offset = 0;
895 	} else {
896 		struct smc_clc_eid_entry *ueident;
897 		u16 v2_ext_offset;
898 
899 		v2_ext->hdr.flag.release = SMC_RELEASE;
900 		v2_ext_offset = sizeof(*pclc_smcd) -
901 			offsetofend(struct smc_clc_msg_smcd, v2_ext_offset);
902 		if (ini->smc_type_v1 != SMC_TYPE_N)
903 			v2_ext_offset += sizeof(*pclc_prfx) +
904 						pclc_prfx->ipv6_prefixes_cnt *
905 						sizeof(ipv6_prfx[0]);
906 		pclc_smcd->v2_ext_offset = htons(v2_ext_offset);
907 		plen += sizeof(*v2_ext);
908 
909 		read_lock(&smc_clc_eid_table.lock);
910 		v2_ext->hdr.eid_cnt = smc_clc_eid_table.ueid_cnt;
911 		plen += smc_clc_eid_table.ueid_cnt * SMC_MAX_EID_LEN;
912 		i = 0;
913 		list_for_each_entry(ueident, &smc_clc_eid_table.list, list) {
914 			memcpy(v2_ext->user_eids[i++], ueident->eid,
915 			       sizeof(ueident->eid));
916 		}
917 		read_unlock(&smc_clc_eid_table.lock);
918 	}
919 	if (smcd_indicated(ini->smc_type_v2)) {
920 		u8 *eid = NULL;
921 
922 		v2_ext->hdr.flag.seid = smc_clc_eid_table.seid_enabled;
923 		v2_ext->hdr.ism_gid_cnt = ini->ism_offered_cnt;
924 		v2_ext->hdr.smcd_v2_ext_offset = htons(sizeof(*v2_ext) -
925 				offsetofend(struct smc_clnt_opts_area_hdr,
926 					    smcd_v2_ext_offset) +
927 				v2_ext->hdr.eid_cnt * SMC_MAX_EID_LEN);
928 		smc_ism_get_system_eid(&eid);
929 		if (eid && v2_ext->hdr.flag.seid)
930 			memcpy(smcd_v2_ext->system_eid, eid, SMC_MAX_EID_LEN);
931 		plen += sizeof(*smcd_v2_ext);
932 		if (ini->ism_offered_cnt) {
933 			for (i = 1; i <= ini->ism_offered_cnt; i++) {
934 				smcd = ini->ism_dev[i];
935 				gidchids[i - 1].gid =
936 					htonll(smcd->ops->get_local_gid(smcd));
937 				gidchids[i - 1].chid =
938 					htons(smc_ism_get_chid(ini->ism_dev[i]));
939 			}
940 			plen += ini->ism_offered_cnt *
941 				sizeof(struct smc_clc_smcd_gid_chid);
942 		}
943 	}
944 	if (smcr_indicated(ini->smc_type_v2)) {
945 		memcpy(v2_ext->roce, ini->smcrv2.ib_gid_v2, SMC_GID_SIZE);
946 		v2_ext->max_conns = SMC_CONN_PER_LGR_PREFER;
947 		v2_ext->max_links = SMC_LINKS_PER_LGR_MAX_PREFER;
948 	}
949 
950 	pclc_base->hdr.length = htons(plen);
951 	memcpy(trl->eyecatcher, SMC_EYECATCHER, sizeof(SMC_EYECATCHER));
952 
953 	/* send SMC Proposal CLC message */
954 	memset(&msg, 0, sizeof(msg));
955 	i = 0;
956 	vec[i].iov_base = pclc_base;
957 	vec[i++].iov_len = sizeof(*pclc_base);
958 	vec[i].iov_base = pclc_smcd;
959 	vec[i++].iov_len = sizeof(*pclc_smcd);
960 	if (ini->smc_type_v1 != SMC_TYPE_N) {
961 		vec[i].iov_base = pclc_prfx;
962 		vec[i++].iov_len = sizeof(*pclc_prfx);
963 		if (pclc_prfx->ipv6_prefixes_cnt > 0) {
964 			vec[i].iov_base = ipv6_prfx;
965 			vec[i++].iov_len = pclc_prfx->ipv6_prefixes_cnt *
966 					   sizeof(ipv6_prfx[0]);
967 		}
968 	}
969 	if (ini->smc_type_v2 != SMC_TYPE_N) {
970 		vec[i].iov_base = v2_ext;
971 		vec[i++].iov_len = sizeof(*v2_ext) +
972 				   (v2_ext->hdr.eid_cnt * SMC_MAX_EID_LEN);
973 		if (smcd_indicated(ini->smc_type_v2)) {
974 			vec[i].iov_base = smcd_v2_ext;
975 			vec[i++].iov_len = sizeof(*smcd_v2_ext);
976 			if (ini->ism_offered_cnt) {
977 				vec[i].iov_base = gidchids;
978 				vec[i++].iov_len = ini->ism_offered_cnt *
979 					sizeof(struct smc_clc_smcd_gid_chid);
980 			}
981 		}
982 	}
983 	vec[i].iov_base = trl;
984 	vec[i++].iov_len = sizeof(*trl);
985 	/* due to the few bytes needed for clc-handshake this cannot block */
986 	len = kernel_sendmsg(smc->clcsock, &msg, vec, i, plen);
987 	if (len < 0) {
988 		smc->sk.sk_err = smc->clcsock->sk->sk_err;
989 		reason_code = -smc->sk.sk_err;
990 	} else if (len < ntohs(pclc_base->hdr.length)) {
991 		reason_code = -ENETUNREACH;
992 		smc->sk.sk_err = -reason_code;
993 	}
994 
995 	kfree(pclc);
996 	return reason_code;
997 }
998 
999 /* build and send CLC CONFIRM / ACCEPT message */
1000 static int smc_clc_send_confirm_accept(struct smc_sock *smc,
1001 				       struct smc_clc_msg_accept_confirm_v2 *clc_v2,
1002 				       int first_contact, u8 version,
1003 				       u8 *eid, struct smc_init_info *ini)
1004 {
1005 	struct smc_connection *conn = &smc->conn;
1006 	struct smc_clc_first_contact_ext_v2x fce;
1007 	struct smcd_dev *smcd = conn->lgr->smcd;
1008 	struct smc_clc_msg_accept_confirm *clc;
1009 	struct smc_clc_fce_gid_ext gle;
1010 	struct smc_clc_msg_trail trl;
1011 	int i, len, fce_len;
1012 	struct kvec vec[5];
1013 	struct msghdr msg;
1014 
1015 	/* send SMC Confirm CLC msg */
1016 	clc = (struct smc_clc_msg_accept_confirm *)clc_v2;
1017 	clc->hdr.version = version;	/* SMC version */
1018 	if (first_contact)
1019 		clc->hdr.typev2 |= SMC_FIRST_CONTACT_MASK;
1020 	if (conn->lgr->is_smcd) {
1021 		/* SMC-D specific settings */
1022 		memcpy(clc->hdr.eyecatcher, SMCD_EYECATCHER,
1023 		       sizeof(SMCD_EYECATCHER));
1024 		clc->hdr.typev1 = SMC_TYPE_D;
1025 		clc->d0.gid = htonll(smcd->ops->get_local_gid(smcd));
1026 		clc->d0.token = htonll(conn->rmb_desc->token);
1027 		clc->d0.dmbe_size = conn->rmbe_size_comp;
1028 		clc->d0.dmbe_idx = 0;
1029 		memcpy(&clc->d0.linkid, conn->lgr->id, SMC_LGR_ID_SIZE);
1030 		if (version == SMC_V1) {
1031 			clc->hdr.length = htons(SMCD_CLC_ACCEPT_CONFIRM_LEN);
1032 		} else {
1033 			clc_v2->d1.chid = htons(smc_ism_get_chid(smcd));
1034 			if (eid && eid[0])
1035 				memcpy(clc_v2->d1.eid, eid, SMC_MAX_EID_LEN);
1036 			len = SMCD_CLC_ACCEPT_CONFIRM_LEN_V2;
1037 			if (first_contact) {
1038 				fce_len = smc_clc_fill_fce(&fce, ini);
1039 				len += fce_len;
1040 			}
1041 			clc_v2->hdr.length = htons(len);
1042 		}
1043 		memcpy(trl.eyecatcher, SMCD_EYECATCHER,
1044 		       sizeof(SMCD_EYECATCHER));
1045 	} else {
1046 		struct smc_link *link = conn->lnk;
1047 
1048 		/* SMC-R specific settings */
1049 		memcpy(clc->hdr.eyecatcher, SMC_EYECATCHER,
1050 		       sizeof(SMC_EYECATCHER));
1051 		clc->hdr.typev1 = SMC_TYPE_R;
1052 		clc->hdr.length = htons(SMCR_CLC_ACCEPT_CONFIRM_LEN);
1053 		memcpy(clc->r0.lcl.id_for_peer, local_systemid,
1054 		       sizeof(local_systemid));
1055 		memcpy(&clc->r0.lcl.gid, link->gid, SMC_GID_SIZE);
1056 		memcpy(&clc->r0.lcl.mac, &link->smcibdev->mac[link->ibport - 1],
1057 		       ETH_ALEN);
1058 		hton24(clc->r0.qpn, link->roce_qp->qp_num);
1059 		clc->r0.rmb_rkey =
1060 			htonl(conn->rmb_desc->mr[link->link_idx]->rkey);
1061 		clc->r0.rmbe_idx = 1; /* for now: 1 RMB = 1 RMBE */
1062 		clc->r0.rmbe_alert_token = htonl(conn->alert_token_local);
1063 		switch (clc->hdr.type) {
1064 		case SMC_CLC_ACCEPT:
1065 			clc->r0.qp_mtu = link->path_mtu;
1066 			break;
1067 		case SMC_CLC_CONFIRM:
1068 			clc->r0.qp_mtu = min(link->path_mtu, link->peer_mtu);
1069 			break;
1070 		}
1071 		clc->r0.rmbe_size = conn->rmbe_size_comp;
1072 		clc->r0.rmb_dma_addr = conn->rmb_desc->is_vm ?
1073 			cpu_to_be64((uintptr_t)conn->rmb_desc->cpu_addr) :
1074 			cpu_to_be64((u64)sg_dma_address
1075 				    (conn->rmb_desc->sgt[link->link_idx].sgl));
1076 		hton24(clc->r0.psn, link->psn_initial);
1077 		if (version == SMC_V1) {
1078 			clc->hdr.length = htons(SMCR_CLC_ACCEPT_CONFIRM_LEN);
1079 		} else {
1080 			if (eid && eid[0])
1081 				memcpy(clc_v2->r1.eid, eid, SMC_MAX_EID_LEN);
1082 			len = SMCR_CLC_ACCEPT_CONFIRM_LEN_V2;
1083 			if (first_contact) {
1084 				fce_len = smc_clc_fill_fce(&fce, ini);
1085 				len += fce_len;
1086 				fce.fce_v2_base.v2_direct = !link->lgr->uses_gateway;
1087 				if (clc->hdr.type == SMC_CLC_CONFIRM) {
1088 					memset(&gle, 0, sizeof(gle));
1089 					gle.gid_cnt = ini->smcrv2.gidlist.len;
1090 					len += sizeof(gle);
1091 					len += gle.gid_cnt * sizeof(gle.gid[0]);
1092 				}
1093 			}
1094 			clc_v2->hdr.length = htons(len);
1095 		}
1096 		memcpy(trl.eyecatcher, SMC_EYECATCHER, sizeof(SMC_EYECATCHER));
1097 	}
1098 
1099 	memset(&msg, 0, sizeof(msg));
1100 	i = 0;
1101 	vec[i].iov_base = clc_v2;
1102 	if (version > SMC_V1)
1103 		vec[i++].iov_len = (clc->hdr.typev1 == SMC_TYPE_D ?
1104 					SMCD_CLC_ACCEPT_CONFIRM_LEN_V2 :
1105 					SMCR_CLC_ACCEPT_CONFIRM_LEN_V2) -
1106 				   sizeof(trl);
1107 	else
1108 		vec[i++].iov_len = (clc->hdr.typev1 == SMC_TYPE_D ?
1109 						SMCD_CLC_ACCEPT_CONFIRM_LEN :
1110 						SMCR_CLC_ACCEPT_CONFIRM_LEN) -
1111 				   sizeof(trl);
1112 	if (version > SMC_V1 && first_contact) {
1113 		vec[i].iov_base = &fce;
1114 		vec[i++].iov_len = fce_len;
1115 		if (!conn->lgr->is_smcd) {
1116 			if (clc->hdr.type == SMC_CLC_CONFIRM) {
1117 				vec[i].iov_base = &gle;
1118 				vec[i++].iov_len = sizeof(gle);
1119 				vec[i].iov_base = &ini->smcrv2.gidlist.list;
1120 				vec[i++].iov_len = gle.gid_cnt *
1121 						   sizeof(gle.gid[0]);
1122 			}
1123 		}
1124 	}
1125 	vec[i].iov_base = &trl;
1126 	vec[i++].iov_len = sizeof(trl);
1127 	return kernel_sendmsg(smc->clcsock, &msg, vec, 1,
1128 			      ntohs(clc->hdr.length));
1129 }
1130 
1131 /* send CLC CONFIRM message across internal TCP socket */
1132 int smc_clc_send_confirm(struct smc_sock *smc, bool clnt_first_contact,
1133 			 u8 version, u8 *eid, struct smc_init_info *ini)
1134 {
1135 	struct smc_clc_msg_accept_confirm_v2 cclc_v2;
1136 	int reason_code = 0;
1137 	int len;
1138 
1139 	/* send SMC Confirm CLC msg */
1140 	memset(&cclc_v2, 0, sizeof(cclc_v2));
1141 	cclc_v2.hdr.type = SMC_CLC_CONFIRM;
1142 	len = smc_clc_send_confirm_accept(smc, &cclc_v2, clnt_first_contact,
1143 					  version, eid, ini);
1144 	if (len < ntohs(cclc_v2.hdr.length)) {
1145 		if (len >= 0) {
1146 			reason_code = -ENETUNREACH;
1147 			smc->sk.sk_err = -reason_code;
1148 		} else {
1149 			smc->sk.sk_err = smc->clcsock->sk->sk_err;
1150 			reason_code = -smc->sk.sk_err;
1151 		}
1152 	}
1153 	return reason_code;
1154 }
1155 
1156 /* send CLC ACCEPT message across internal TCP socket */
1157 int smc_clc_send_accept(struct smc_sock *new_smc, bool srv_first_contact,
1158 			u8 version, u8 *negotiated_eid, struct smc_init_info *ini)
1159 {
1160 	struct smc_clc_msg_accept_confirm_v2 aclc_v2;
1161 	int len;
1162 
1163 	memset(&aclc_v2, 0, sizeof(aclc_v2));
1164 	aclc_v2.hdr.type = SMC_CLC_ACCEPT;
1165 	len = smc_clc_send_confirm_accept(new_smc, &aclc_v2, srv_first_contact,
1166 					  version, negotiated_eid, ini);
1167 	if (len < ntohs(aclc_v2.hdr.length))
1168 		len = len >= 0 ? -EPROTO : -new_smc->clcsock->sk->sk_err;
1169 
1170 	return len > 0 ? 0 : len;
1171 }
1172 
1173 int smc_clc_srv_v2x_features_validate(struct smc_clc_msg_proposal *pclc,
1174 				      struct smc_init_info *ini)
1175 {
1176 	struct smc_clc_v2_extension *pclc_v2_ext;
1177 
1178 	ini->max_conns = SMC_CONN_PER_LGR_MAX;
1179 	ini->max_links = SMC_LINKS_ADD_LNK_MAX;
1180 
1181 	if ((!(ini->smcd_version & SMC_V2) && !(ini->smcr_version & SMC_V2)) ||
1182 	    ini->release_nr < SMC_RELEASE_1)
1183 		return 0;
1184 
1185 	pclc_v2_ext = smc_get_clc_v2_ext(pclc);
1186 	if (!pclc_v2_ext)
1187 		return SMC_CLC_DECL_NOV2EXT;
1188 
1189 	if (ini->smcr_version & SMC_V2) {
1190 		ini->max_conns = min_t(u8, pclc_v2_ext->max_conns, SMC_CONN_PER_LGR_PREFER);
1191 		if (ini->max_conns < SMC_CONN_PER_LGR_MIN)
1192 			return SMC_CLC_DECL_MAXCONNERR;
1193 
1194 		ini->max_links = min_t(u8, pclc_v2_ext->max_links, SMC_LINKS_PER_LGR_MAX_PREFER);
1195 		if (ini->max_links < SMC_LINKS_ADD_LNK_MIN)
1196 			return SMC_CLC_DECL_MAXLINKERR;
1197 	}
1198 
1199 	return 0;
1200 }
1201 
1202 int smc_clc_clnt_v2x_features_validate(struct smc_clc_first_contact_ext *fce,
1203 				       struct smc_init_info *ini)
1204 {
1205 	struct smc_clc_first_contact_ext_v2x *fce_v2x =
1206 		(struct smc_clc_first_contact_ext_v2x *)fce;
1207 
1208 	if (ini->release_nr < SMC_RELEASE_1)
1209 		return 0;
1210 
1211 	if (!ini->is_smcd) {
1212 		if (fce_v2x->max_conns < SMC_CONN_PER_LGR_MIN)
1213 			return SMC_CLC_DECL_MAXCONNERR;
1214 		ini->max_conns = fce_v2x->max_conns;
1215 
1216 		if (fce_v2x->max_links > SMC_LINKS_ADD_LNK_MAX ||
1217 		    fce_v2x->max_links < SMC_LINKS_ADD_LNK_MIN)
1218 			return SMC_CLC_DECL_MAXLINKERR;
1219 		ini->max_links = fce_v2x->max_links;
1220 	}
1221 
1222 	return 0;
1223 }
1224 
1225 int smc_clc_v2x_features_confirm_check(struct smc_clc_msg_accept_confirm *cclc,
1226 				       struct smc_init_info *ini)
1227 {
1228 	struct smc_clc_msg_accept_confirm_v2 *clc_v2 =
1229 		(struct smc_clc_msg_accept_confirm_v2 *)cclc;
1230 	struct smc_clc_first_contact_ext *fce =
1231 		smc_get_clc_first_contact_ext(clc_v2, ini->is_smcd);
1232 	struct smc_clc_first_contact_ext_v2x *fce_v2x =
1233 		(struct smc_clc_first_contact_ext_v2x *)fce;
1234 
1235 	if (cclc->hdr.version == SMC_V1 ||
1236 	    !(cclc->hdr.typev2 & SMC_FIRST_CONTACT_MASK))
1237 		return 0;
1238 
1239 	if (ini->release_nr != fce->release)
1240 		return SMC_CLC_DECL_RELEASEERR;
1241 
1242 	if (fce->release < SMC_RELEASE_1)
1243 		return 0;
1244 
1245 	if (!ini->is_smcd) {
1246 		if (fce_v2x->max_conns != ini->max_conns)
1247 			return SMC_CLC_DECL_MAXCONNERR;
1248 		if (fce_v2x->max_links != ini->max_links)
1249 			return SMC_CLC_DECL_MAXLINKERR;
1250 	}
1251 
1252 	return 0;
1253 }
1254 
1255 void smc_clc_get_hostname(u8 **host)
1256 {
1257 	*host = &smc_hostname[0];
1258 }
1259 
1260 void __init smc_clc_init(void)
1261 {
1262 	struct new_utsname *u;
1263 
1264 	memset(smc_hostname, _S, sizeof(smc_hostname)); /* ASCII blanks */
1265 	u = utsname();
1266 	memcpy(smc_hostname, u->nodename,
1267 	       min_t(size_t, strlen(u->nodename), sizeof(smc_hostname)));
1268 
1269 	INIT_LIST_HEAD(&smc_clc_eid_table.list);
1270 	rwlock_init(&smc_clc_eid_table.lock);
1271 	smc_clc_eid_table.ueid_cnt = 0;
1272 	smc_clc_eid_table.seid_enabled = 1;
1273 }
1274 
1275 void smc_clc_exit(void)
1276 {
1277 	smc_clc_ueid_remove(NULL);
1278 }
1279