xref: /linux/net/smc/smc_clc.c (revision 1f2c9dd7)
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 net *net = sock_net(&smc->sk);
828 	struct smc_clc_v2_extension *v2_ext;
829 	struct smc_clc_msg_smcd *pclc_smcd;
830 	struct smc_clc_msg_trail *trl;
831 	struct smcd_dev *smcd;
832 	int len, i, plen, rc;
833 	int reason_code = 0;
834 	struct kvec vec[8];
835 	struct msghdr msg;
836 
837 	pclc = kzalloc(sizeof(*pclc), GFP_KERNEL);
838 	if (!pclc)
839 		return -ENOMEM;
840 
841 	pclc_base = &pclc->pclc_base;
842 	pclc_smcd = &pclc->pclc_smcd;
843 	pclc_prfx = &pclc->pclc_prfx;
844 	ipv6_prfx = pclc->pclc_prfx_ipv6;
845 	v2_ext = &pclc->pclc_v2_ext;
846 	smcd_v2_ext = &pclc->pclc_smcd_v2_ext;
847 	gidchids = pclc->pclc_gidchids;
848 	trl = &pclc->pclc_trl;
849 
850 	pclc_base->hdr.version = SMC_V2;
851 	pclc_base->hdr.typev1 = ini->smc_type_v1;
852 	pclc_base->hdr.typev2 = ini->smc_type_v2;
853 	plen = sizeof(*pclc_base) + sizeof(*pclc_smcd) + sizeof(*trl);
854 
855 	/* retrieve ip prefixes for CLC proposal msg */
856 	if (ini->smc_type_v1 != SMC_TYPE_N) {
857 		rc = smc_clc_prfx_set(smc->clcsock, pclc_prfx, ipv6_prfx);
858 		if (rc) {
859 			if (ini->smc_type_v2 == SMC_TYPE_N) {
860 				kfree(pclc);
861 				return SMC_CLC_DECL_CNFERR;
862 			}
863 			pclc_base->hdr.typev1 = SMC_TYPE_N;
864 		} else {
865 			pclc_base->iparea_offset = htons(sizeof(*pclc_smcd));
866 			plen += sizeof(*pclc_prfx) +
867 					pclc_prfx->ipv6_prefixes_cnt *
868 					sizeof(ipv6_prfx[0]);
869 		}
870 	}
871 
872 	/* build SMC Proposal CLC message */
873 	memcpy(pclc_base->hdr.eyecatcher, SMC_EYECATCHER,
874 	       sizeof(SMC_EYECATCHER));
875 	pclc_base->hdr.type = SMC_CLC_PROPOSAL;
876 	if (smcr_indicated(ini->smc_type_v1)) {
877 		/* add SMC-R specifics */
878 		memcpy(pclc_base->lcl.id_for_peer, local_systemid,
879 		       sizeof(local_systemid));
880 		memcpy(pclc_base->lcl.gid, ini->ib_gid, SMC_GID_SIZE);
881 		memcpy(pclc_base->lcl.mac, &ini->ib_dev->mac[ini->ib_port - 1],
882 		       ETH_ALEN);
883 	}
884 	if (smcd_indicated(ini->smc_type_v1)) {
885 		/* add SMC-D specifics */
886 		if (ini->ism_dev[0]) {
887 			smcd = ini->ism_dev[0];
888 			pclc_smcd->ism.gid =
889 				htonll(smcd->ops->get_local_gid(smcd));
890 			pclc_smcd->ism.chid =
891 				htons(smc_ism_get_chid(ini->ism_dev[0]));
892 		}
893 	}
894 	if (ini->smc_type_v2 == SMC_TYPE_N) {
895 		pclc_smcd->v2_ext_offset = 0;
896 	} else {
897 		struct smc_clc_eid_entry *ueident;
898 		u16 v2_ext_offset;
899 
900 		v2_ext->hdr.flag.release = SMC_RELEASE;
901 		v2_ext_offset = sizeof(*pclc_smcd) -
902 			offsetofend(struct smc_clc_msg_smcd, v2_ext_offset);
903 		if (ini->smc_type_v1 != SMC_TYPE_N)
904 			v2_ext_offset += sizeof(*pclc_prfx) +
905 						pclc_prfx->ipv6_prefixes_cnt *
906 						sizeof(ipv6_prfx[0]);
907 		pclc_smcd->v2_ext_offset = htons(v2_ext_offset);
908 		plen += sizeof(*v2_ext);
909 
910 		read_lock(&smc_clc_eid_table.lock);
911 		v2_ext->hdr.eid_cnt = smc_clc_eid_table.ueid_cnt;
912 		plen += smc_clc_eid_table.ueid_cnt * SMC_MAX_EID_LEN;
913 		i = 0;
914 		list_for_each_entry(ueident, &smc_clc_eid_table.list, list) {
915 			memcpy(v2_ext->user_eids[i++], ueident->eid,
916 			       sizeof(ueident->eid));
917 		}
918 		read_unlock(&smc_clc_eid_table.lock);
919 	}
920 	if (smcd_indicated(ini->smc_type_v2)) {
921 		u8 *eid = NULL;
922 
923 		v2_ext->hdr.flag.seid = smc_clc_eid_table.seid_enabled;
924 		v2_ext->hdr.ism_gid_cnt = ini->ism_offered_cnt;
925 		v2_ext->hdr.smcd_v2_ext_offset = htons(sizeof(*v2_ext) -
926 				offsetofend(struct smc_clnt_opts_area_hdr,
927 					    smcd_v2_ext_offset) +
928 				v2_ext->hdr.eid_cnt * SMC_MAX_EID_LEN);
929 		smc_ism_get_system_eid(&eid);
930 		if (eid && v2_ext->hdr.flag.seid)
931 			memcpy(smcd_v2_ext->system_eid, eid, SMC_MAX_EID_LEN);
932 		plen += sizeof(*smcd_v2_ext);
933 		if (ini->ism_offered_cnt) {
934 			for (i = 1; i <= ini->ism_offered_cnt; i++) {
935 				smcd = ini->ism_dev[i];
936 				gidchids[i - 1].gid =
937 					htonll(smcd->ops->get_local_gid(smcd));
938 				gidchids[i - 1].chid =
939 					htons(smc_ism_get_chid(ini->ism_dev[i]));
940 			}
941 			plen += ini->ism_offered_cnt *
942 				sizeof(struct smc_clc_smcd_gid_chid);
943 		}
944 	}
945 	if (smcr_indicated(ini->smc_type_v2)) {
946 		memcpy(v2_ext->roce, ini->smcrv2.ib_gid_v2, SMC_GID_SIZE);
947 		v2_ext->max_conns = net->smc.sysctl_max_conns_per_lgr;
948 		v2_ext->max_links = net->smc.sysctl_max_links_per_lgr;
949 	}
950 
951 	pclc_base->hdr.length = htons(plen);
952 	memcpy(trl->eyecatcher, SMC_EYECATCHER, sizeof(SMC_EYECATCHER));
953 
954 	/* send SMC Proposal CLC message */
955 	memset(&msg, 0, sizeof(msg));
956 	i = 0;
957 	vec[i].iov_base = pclc_base;
958 	vec[i++].iov_len = sizeof(*pclc_base);
959 	vec[i].iov_base = pclc_smcd;
960 	vec[i++].iov_len = sizeof(*pclc_smcd);
961 	if (ini->smc_type_v1 != SMC_TYPE_N) {
962 		vec[i].iov_base = pclc_prfx;
963 		vec[i++].iov_len = sizeof(*pclc_prfx);
964 		if (pclc_prfx->ipv6_prefixes_cnt > 0) {
965 			vec[i].iov_base = ipv6_prfx;
966 			vec[i++].iov_len = pclc_prfx->ipv6_prefixes_cnt *
967 					   sizeof(ipv6_prfx[0]);
968 		}
969 	}
970 	if (ini->smc_type_v2 != SMC_TYPE_N) {
971 		vec[i].iov_base = v2_ext;
972 		vec[i++].iov_len = sizeof(*v2_ext) +
973 				   (v2_ext->hdr.eid_cnt * SMC_MAX_EID_LEN);
974 		if (smcd_indicated(ini->smc_type_v2)) {
975 			vec[i].iov_base = smcd_v2_ext;
976 			vec[i++].iov_len = sizeof(*smcd_v2_ext);
977 			if (ini->ism_offered_cnt) {
978 				vec[i].iov_base = gidchids;
979 				vec[i++].iov_len = ini->ism_offered_cnt *
980 					sizeof(struct smc_clc_smcd_gid_chid);
981 			}
982 		}
983 	}
984 	vec[i].iov_base = trl;
985 	vec[i++].iov_len = sizeof(*trl);
986 	/* due to the few bytes needed for clc-handshake this cannot block */
987 	len = kernel_sendmsg(smc->clcsock, &msg, vec, i, plen);
988 	if (len < 0) {
989 		smc->sk.sk_err = smc->clcsock->sk->sk_err;
990 		reason_code = -smc->sk.sk_err;
991 	} else if (len < ntohs(pclc_base->hdr.length)) {
992 		reason_code = -ENETUNREACH;
993 		smc->sk.sk_err = -reason_code;
994 	}
995 
996 	kfree(pclc);
997 	return reason_code;
998 }
999 
1000 /* build and send CLC CONFIRM / ACCEPT message */
1001 static int smc_clc_send_confirm_accept(struct smc_sock *smc,
1002 				       struct smc_clc_msg_accept_confirm_v2 *clc_v2,
1003 				       int first_contact, u8 version,
1004 				       u8 *eid, struct smc_init_info *ini)
1005 {
1006 	struct smc_connection *conn = &smc->conn;
1007 	struct smc_clc_first_contact_ext_v2x fce;
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 =
1026 			conn->lgr->smcd->ops->get_local_gid(conn->lgr->smcd);
1027 		clc->d0.token = conn->rmb_desc->token;
1028 		clc->d0.dmbe_size = conn->rmbe_size_comp;
1029 		clc->d0.dmbe_idx = 0;
1030 		memcpy(&clc->d0.linkid, conn->lgr->id, SMC_LGR_ID_SIZE);
1031 		if (version == SMC_V1) {
1032 			clc->hdr.length = htons(SMCD_CLC_ACCEPT_CONFIRM_LEN);
1033 		} else {
1034 			clc_v2->d1.chid =
1035 				htons(smc_ism_get_chid(conn->lgr->smcd));
1036 			if (eid && eid[0])
1037 				memcpy(clc_v2->d1.eid, eid, SMC_MAX_EID_LEN);
1038 			len = SMCD_CLC_ACCEPT_CONFIRM_LEN_V2;
1039 			if (first_contact) {
1040 				fce_len = smc_clc_fill_fce(&fce, ini);
1041 				len += fce_len;
1042 			}
1043 			clc_v2->hdr.length = htons(len);
1044 		}
1045 		memcpy(trl.eyecatcher, SMCD_EYECATCHER,
1046 		       sizeof(SMCD_EYECATCHER));
1047 	} else {
1048 		struct smc_link *link = conn->lnk;
1049 
1050 		/* SMC-R specific settings */
1051 		memcpy(clc->hdr.eyecatcher, SMC_EYECATCHER,
1052 		       sizeof(SMC_EYECATCHER));
1053 		clc->hdr.typev1 = SMC_TYPE_R;
1054 		clc->hdr.length = htons(SMCR_CLC_ACCEPT_CONFIRM_LEN);
1055 		memcpy(clc->r0.lcl.id_for_peer, local_systemid,
1056 		       sizeof(local_systemid));
1057 		memcpy(&clc->r0.lcl.gid, link->gid, SMC_GID_SIZE);
1058 		memcpy(&clc->r0.lcl.mac, &link->smcibdev->mac[link->ibport - 1],
1059 		       ETH_ALEN);
1060 		hton24(clc->r0.qpn, link->roce_qp->qp_num);
1061 		clc->r0.rmb_rkey =
1062 			htonl(conn->rmb_desc->mr[link->link_idx]->rkey);
1063 		clc->r0.rmbe_idx = 1; /* for now: 1 RMB = 1 RMBE */
1064 		clc->r0.rmbe_alert_token = htonl(conn->alert_token_local);
1065 		switch (clc->hdr.type) {
1066 		case SMC_CLC_ACCEPT:
1067 			clc->r0.qp_mtu = link->path_mtu;
1068 			break;
1069 		case SMC_CLC_CONFIRM:
1070 			clc->r0.qp_mtu = min(link->path_mtu, link->peer_mtu);
1071 			break;
1072 		}
1073 		clc->r0.rmbe_size = conn->rmbe_size_comp;
1074 		clc->r0.rmb_dma_addr = conn->rmb_desc->is_vm ?
1075 			cpu_to_be64((uintptr_t)conn->rmb_desc->cpu_addr) :
1076 			cpu_to_be64((u64)sg_dma_address
1077 				    (conn->rmb_desc->sgt[link->link_idx].sgl));
1078 		hton24(clc->r0.psn, link->psn_initial);
1079 		if (version == SMC_V1) {
1080 			clc->hdr.length = htons(SMCR_CLC_ACCEPT_CONFIRM_LEN);
1081 		} else {
1082 			if (eid && eid[0])
1083 				memcpy(clc_v2->r1.eid, eid, SMC_MAX_EID_LEN);
1084 			len = SMCR_CLC_ACCEPT_CONFIRM_LEN_V2;
1085 			if (first_contact) {
1086 				fce_len = smc_clc_fill_fce(&fce, ini);
1087 				len += fce_len;
1088 				fce.fce_v2_base.v2_direct = !link->lgr->uses_gateway;
1089 				if (clc->hdr.type == SMC_CLC_CONFIRM) {
1090 					memset(&gle, 0, sizeof(gle));
1091 					gle.gid_cnt = ini->smcrv2.gidlist.len;
1092 					len += sizeof(gle);
1093 					len += gle.gid_cnt * sizeof(gle.gid[0]);
1094 				}
1095 			}
1096 			clc_v2->hdr.length = htons(len);
1097 		}
1098 		memcpy(trl.eyecatcher, SMC_EYECATCHER, sizeof(SMC_EYECATCHER));
1099 	}
1100 
1101 	memset(&msg, 0, sizeof(msg));
1102 	i = 0;
1103 	vec[i].iov_base = clc_v2;
1104 	if (version > SMC_V1)
1105 		vec[i++].iov_len = (clc->hdr.typev1 == SMC_TYPE_D ?
1106 					SMCD_CLC_ACCEPT_CONFIRM_LEN_V2 :
1107 					SMCR_CLC_ACCEPT_CONFIRM_LEN_V2) -
1108 				   sizeof(trl);
1109 	else
1110 		vec[i++].iov_len = (clc->hdr.typev1 == SMC_TYPE_D ?
1111 						SMCD_CLC_ACCEPT_CONFIRM_LEN :
1112 						SMCR_CLC_ACCEPT_CONFIRM_LEN) -
1113 				   sizeof(trl);
1114 	if (version > SMC_V1 && first_contact) {
1115 		vec[i].iov_base = &fce;
1116 		vec[i++].iov_len = fce_len;
1117 		if (!conn->lgr->is_smcd) {
1118 			if (clc->hdr.type == SMC_CLC_CONFIRM) {
1119 				vec[i].iov_base = &gle;
1120 				vec[i++].iov_len = sizeof(gle);
1121 				vec[i].iov_base = &ini->smcrv2.gidlist.list;
1122 				vec[i++].iov_len = gle.gid_cnt *
1123 						   sizeof(gle.gid[0]);
1124 			}
1125 		}
1126 	}
1127 	vec[i].iov_base = &trl;
1128 	vec[i++].iov_len = sizeof(trl);
1129 	return kernel_sendmsg(smc->clcsock, &msg, vec, 1,
1130 			      ntohs(clc->hdr.length));
1131 }
1132 
1133 /* send CLC CONFIRM message across internal TCP socket */
1134 int smc_clc_send_confirm(struct smc_sock *smc, bool clnt_first_contact,
1135 			 u8 version, u8 *eid, struct smc_init_info *ini)
1136 {
1137 	struct smc_clc_msg_accept_confirm_v2 cclc_v2;
1138 	int reason_code = 0;
1139 	int len;
1140 
1141 	/* send SMC Confirm CLC msg */
1142 	memset(&cclc_v2, 0, sizeof(cclc_v2));
1143 	cclc_v2.hdr.type = SMC_CLC_CONFIRM;
1144 	len = smc_clc_send_confirm_accept(smc, &cclc_v2, clnt_first_contact,
1145 					  version, eid, ini);
1146 	if (len < ntohs(cclc_v2.hdr.length)) {
1147 		if (len >= 0) {
1148 			reason_code = -ENETUNREACH;
1149 			smc->sk.sk_err = -reason_code;
1150 		} else {
1151 			smc->sk.sk_err = smc->clcsock->sk->sk_err;
1152 			reason_code = -smc->sk.sk_err;
1153 		}
1154 	}
1155 	return reason_code;
1156 }
1157 
1158 /* send CLC ACCEPT message across internal TCP socket */
1159 int smc_clc_send_accept(struct smc_sock *new_smc, bool srv_first_contact,
1160 			u8 version, u8 *negotiated_eid, struct smc_init_info *ini)
1161 {
1162 	struct smc_clc_msg_accept_confirm_v2 aclc_v2;
1163 	int len;
1164 
1165 	memset(&aclc_v2, 0, sizeof(aclc_v2));
1166 	aclc_v2.hdr.type = SMC_CLC_ACCEPT;
1167 	len = smc_clc_send_confirm_accept(new_smc, &aclc_v2, srv_first_contact,
1168 					  version, negotiated_eid, ini);
1169 	if (len < ntohs(aclc_v2.hdr.length))
1170 		len = len >= 0 ? -EPROTO : -new_smc->clcsock->sk->sk_err;
1171 
1172 	return len > 0 ? 0 : len;
1173 }
1174 
1175 int smc_clc_srv_v2x_features_validate(struct smc_sock *smc,
1176 				      struct smc_clc_msg_proposal *pclc,
1177 				      struct smc_init_info *ini)
1178 {
1179 	struct smc_clc_v2_extension *pclc_v2_ext;
1180 	struct net *net = sock_net(&smc->sk);
1181 
1182 	ini->max_conns = SMC_CONN_PER_LGR_MAX;
1183 	ini->max_links = SMC_LINKS_ADD_LNK_MAX;
1184 
1185 	if ((!(ini->smcd_version & SMC_V2) && !(ini->smcr_version & SMC_V2)) ||
1186 	    ini->release_nr < SMC_RELEASE_1)
1187 		return 0;
1188 
1189 	pclc_v2_ext = smc_get_clc_v2_ext(pclc);
1190 	if (!pclc_v2_ext)
1191 		return SMC_CLC_DECL_NOV2EXT;
1192 
1193 	if (ini->smcr_version & SMC_V2) {
1194 		ini->max_conns = min_t(u8, pclc_v2_ext->max_conns,
1195 				       net->smc.sysctl_max_conns_per_lgr);
1196 		if (ini->max_conns < SMC_CONN_PER_LGR_MIN)
1197 			return SMC_CLC_DECL_MAXCONNERR;
1198 
1199 		ini->max_links = min_t(u8, pclc_v2_ext->max_links,
1200 				       net->smc.sysctl_max_links_per_lgr);
1201 		if (ini->max_links < SMC_LINKS_ADD_LNK_MIN)
1202 			return SMC_CLC_DECL_MAXLINKERR;
1203 	}
1204 
1205 	return 0;
1206 }
1207 
1208 int smc_clc_clnt_v2x_features_validate(struct smc_clc_first_contact_ext *fce,
1209 				       struct smc_init_info *ini)
1210 {
1211 	struct smc_clc_first_contact_ext_v2x *fce_v2x =
1212 		(struct smc_clc_first_contact_ext_v2x *)fce;
1213 
1214 	if (ini->release_nr < SMC_RELEASE_1)
1215 		return 0;
1216 
1217 	if (!ini->is_smcd) {
1218 		if (fce_v2x->max_conns < SMC_CONN_PER_LGR_MIN)
1219 			return SMC_CLC_DECL_MAXCONNERR;
1220 		ini->max_conns = fce_v2x->max_conns;
1221 
1222 		if (fce_v2x->max_links > SMC_LINKS_ADD_LNK_MAX ||
1223 		    fce_v2x->max_links < SMC_LINKS_ADD_LNK_MIN)
1224 			return SMC_CLC_DECL_MAXLINKERR;
1225 		ini->max_links = fce_v2x->max_links;
1226 	}
1227 
1228 	return 0;
1229 }
1230 
1231 int smc_clc_v2x_features_confirm_check(struct smc_clc_msg_accept_confirm *cclc,
1232 				       struct smc_init_info *ini)
1233 {
1234 	struct smc_clc_msg_accept_confirm_v2 *clc_v2 =
1235 		(struct smc_clc_msg_accept_confirm_v2 *)cclc;
1236 	struct smc_clc_first_contact_ext *fce =
1237 		smc_get_clc_first_contact_ext(clc_v2, ini->is_smcd);
1238 	struct smc_clc_first_contact_ext_v2x *fce_v2x =
1239 		(struct smc_clc_first_contact_ext_v2x *)fce;
1240 
1241 	if (cclc->hdr.version == SMC_V1 ||
1242 	    !(cclc->hdr.typev2 & SMC_FIRST_CONTACT_MASK))
1243 		return 0;
1244 
1245 	if (ini->release_nr != fce->release)
1246 		return SMC_CLC_DECL_RELEASEERR;
1247 
1248 	if (fce->release < SMC_RELEASE_1)
1249 		return 0;
1250 
1251 	if (!ini->is_smcd) {
1252 		if (fce_v2x->max_conns != ini->max_conns)
1253 			return SMC_CLC_DECL_MAXCONNERR;
1254 		if (fce_v2x->max_links != ini->max_links)
1255 			return SMC_CLC_DECL_MAXLINKERR;
1256 	}
1257 
1258 	return 0;
1259 }
1260 
1261 void smc_clc_get_hostname(u8 **host)
1262 {
1263 	*host = &smc_hostname[0];
1264 }
1265 
1266 void __init smc_clc_init(void)
1267 {
1268 	struct new_utsname *u;
1269 
1270 	memset(smc_hostname, _S, sizeof(smc_hostname)); /* ASCII blanks */
1271 	u = utsname();
1272 	memcpy(smc_hostname, u->nodename,
1273 	       min_t(size_t, strlen(u->nodename), sizeof(smc_hostname)));
1274 
1275 	INIT_LIST_HEAD(&smc_clc_eid_table.list);
1276 	rwlock_init(&smc_clc_eid_table.lock);
1277 	smc_clc_eid_table.ueid_cnt = 0;
1278 	smc_clc_eid_table.seid_enabled = 1;
1279 }
1280 
1281 void smc_clc_exit(void)
1282 {
1283 	smc_clc_ueid_remove(NULL);
1284 }
1285