xref: /linux/net/smc/smc_clc.c (revision 621cde16e49b3ecf7d59a8106a20aaebfb4a59a9)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2a046d57dSUrsula Braun /*
3a046d57dSUrsula Braun  *  Shared Memory Communications over RDMA (SMC-R) and RoCE
4a046d57dSUrsula Braun  *
5a046d57dSUrsula Braun  *  CLC (connection layer control) handshake over initial TCP socket to
6a046d57dSUrsula Braun  *  prepare for RDMA traffic
7a046d57dSUrsula Braun  *
81a26d020SKarsten Graul  *  Copyright IBM Corp. 2016, 2018
9a046d57dSUrsula Braun  *
10a046d57dSUrsula Braun  *  Author(s):  Ursula Braun <ubraun@linux.vnet.ibm.com>
11a046d57dSUrsula Braun  */
12a046d57dSUrsula Braun 
13a046d57dSUrsula Braun #include <linux/in.h>
14696cd301SKarsten Graul #include <linux/inetdevice.h>
15143c0171SUrsula Braun #include <linux/if_ether.h>
16c3edc401SIngo Molnar #include <linux/sched/signal.h>
17b81a5eb7SUrsula Braun #include <linux/utsname.h>
18b81a5eb7SUrsula Braun #include <linux/ctype.h>
19c3edc401SIngo Molnar 
201a26d020SKarsten Graul #include <net/addrconf.h>
21a046d57dSUrsula Braun #include <net/sock.h>
22a046d57dSUrsula Braun #include <net/tcp.h>
23a046d57dSUrsula Braun 
24a046d57dSUrsula Braun #include "smc.h"
250cfdd8f9SUrsula Braun #include "smc_core.h"
26a046d57dSUrsula Braun #include "smc_clc.h"
27a046d57dSUrsula Braun #include "smc_ib.h"
28c758dfddSHans Wippel #include "smc_ism.h"
29fa086662SKarsten Graul #include "smc_netlink.h"
30c758dfddSHans Wippel 
31c758dfddSHans Wippel #define SMCR_CLC_ACCEPT_CONFIRM_LEN 68
32c758dfddSHans Wippel #define SMCD_CLC_ACCEPT_CONFIRM_LEN 48
33a7c9c5f4SUrsula Braun #define SMCD_CLC_ACCEPT_CONFIRM_LEN_V2 78
34e5c4744cSKarsten Graul #define SMCR_CLC_ACCEPT_CONFIRM_LEN_V2 108
35fb4f7926SUrsula Braun #define SMC_CLC_RECV_BUF_LEN	100
36a046d57dSUrsula Braun 
370f627126SStefan Raspl /* eye catcher "SMCR" EBCDIC for CLC messages */
380f627126SStefan Raspl static const char SMC_EYECATCHER[4] = {'\xe2', '\xd4', '\xc3', '\xd9'};
39c758dfddSHans Wippel /* eye catcher "SMCD" EBCDIC for CLC messages */
40c758dfddSHans Wippel static const char SMCD_EYECATCHER[4] = {'\xe2', '\xd4', '\xc3', '\xc4'};
410f627126SStefan Raspl 
42b81a5eb7SUrsula Braun static u8 smc_hostname[SMC_MAX_HOSTNAME_LEN];
43b81a5eb7SUrsula Braun 
44fa086662SKarsten Graul struct smc_clc_eid_table {
45fa086662SKarsten Graul 	rwlock_t lock;
46fa086662SKarsten Graul 	struct list_head list;
47fa086662SKarsten Graul 	u8 ueid_cnt;
48fa086662SKarsten Graul 	u8 seid_enabled;
49fa086662SKarsten Graul };
50fa086662SKarsten Graul 
51fa086662SKarsten Graul static struct smc_clc_eid_table smc_clc_eid_table;
52fa086662SKarsten Graul 
53fa086662SKarsten Graul struct smc_clc_eid_entry {
54fa086662SKarsten Graul 	struct list_head list;
55fa086662SKarsten Graul 	u8 eid[SMC_MAX_EID_LEN];
56fa086662SKarsten Graul };
57fa086662SKarsten Graul 
58fa086662SKarsten Graul /* The size of a user EID is 32 characters.
59fa086662SKarsten Graul  * Valid characters should be (single-byte character set) A-Z, 0-9, '.' and '-'.
60fa086662SKarsten Graul  * Blanks should only be used to pad to the expected size.
61fa086662SKarsten Graul  * First character must be alphanumeric.
62fa086662SKarsten Graul  */
smc_clc_ueid_valid(char * ueid)63fa086662SKarsten Graul static bool smc_clc_ueid_valid(char *ueid)
64fa086662SKarsten Graul {
65fa086662SKarsten Graul 	char *end = ueid + SMC_MAX_EID_LEN;
66fa086662SKarsten Graul 
67fa086662SKarsten Graul 	while (--end >= ueid && isspace(*end))
68fa086662SKarsten Graul 		;
69fa086662SKarsten Graul 	if (end < ueid)
70fa086662SKarsten Graul 		return false;
71fa086662SKarsten Graul 	if (!isalnum(*ueid) || islower(*ueid))
72fa086662SKarsten Graul 		return false;
73fa086662SKarsten Graul 	while (ueid <= end) {
74fa086662SKarsten Graul 		if ((!isalnum(*ueid) || islower(*ueid)) && *ueid != '.' &&
75fa086662SKarsten Graul 		    *ueid != '-')
76fa086662SKarsten Graul 			return false;
77fa086662SKarsten Graul 		ueid++;
78fa086662SKarsten Graul 	}
79fa086662SKarsten Graul 	return true;
80fa086662SKarsten Graul }
81fa086662SKarsten Graul 
smc_clc_ueid_add(char * ueid)82fa086662SKarsten Graul static int smc_clc_ueid_add(char *ueid)
83fa086662SKarsten Graul {
84fa086662SKarsten Graul 	struct smc_clc_eid_entry *new_ueid, *tmp_ueid;
85fa086662SKarsten Graul 	int rc;
86fa086662SKarsten Graul 
87fa086662SKarsten Graul 	if (!smc_clc_ueid_valid(ueid))
88fa086662SKarsten Graul 		return -EINVAL;
89fa086662SKarsten Graul 
90fa086662SKarsten Graul 	/* add a new ueid entry to the ueid table if there isn't one */
91fa086662SKarsten Graul 	new_ueid = kzalloc(sizeof(*new_ueid), GFP_KERNEL);
92fa086662SKarsten Graul 	if (!new_ueid)
93fa086662SKarsten Graul 		return -ENOMEM;
94fa086662SKarsten Graul 	memcpy(new_ueid->eid, ueid, SMC_MAX_EID_LEN);
95fa086662SKarsten Graul 
96fa086662SKarsten Graul 	write_lock(&smc_clc_eid_table.lock);
97fa086662SKarsten Graul 	if (smc_clc_eid_table.ueid_cnt >= SMC_MAX_UEID) {
98fa086662SKarsten Graul 		rc = -ERANGE;
99fa086662SKarsten Graul 		goto err_out;
100fa086662SKarsten Graul 	}
101fa086662SKarsten Graul 	list_for_each_entry(tmp_ueid, &smc_clc_eid_table.list, list) {
102fa086662SKarsten Graul 		if (!memcmp(tmp_ueid->eid, ueid, SMC_MAX_EID_LEN)) {
103fa086662SKarsten Graul 			rc = -EEXIST;
104fa086662SKarsten Graul 			goto err_out;
105fa086662SKarsten Graul 		}
106fa086662SKarsten Graul 	}
107fa086662SKarsten Graul 	list_add_tail(&new_ueid->list, &smc_clc_eid_table.list);
108fa086662SKarsten Graul 	smc_clc_eid_table.ueid_cnt++;
109fa086662SKarsten Graul 	write_unlock(&smc_clc_eid_table.lock);
110fa086662SKarsten Graul 	return 0;
111fa086662SKarsten Graul 
112fa086662SKarsten Graul err_out:
113fa086662SKarsten Graul 	write_unlock(&smc_clc_eid_table.lock);
114fa086662SKarsten Graul 	kfree(new_ueid);
115fa086662SKarsten Graul 	return rc;
116fa086662SKarsten Graul }
117fa086662SKarsten Graul 
smc_clc_ueid_count(void)11842042dbbSKarsten Graul int smc_clc_ueid_count(void)
11942042dbbSKarsten Graul {
12042042dbbSKarsten Graul 	int count;
12142042dbbSKarsten Graul 
12242042dbbSKarsten Graul 	read_lock(&smc_clc_eid_table.lock);
12342042dbbSKarsten Graul 	count = smc_clc_eid_table.ueid_cnt;
12442042dbbSKarsten Graul 	read_unlock(&smc_clc_eid_table.lock);
12542042dbbSKarsten Graul 
12642042dbbSKarsten Graul 	return count;
12742042dbbSKarsten Graul }
12842042dbbSKarsten Graul 
smc_nl_add_ueid(struct sk_buff * skb,struct genl_info * info)129fa086662SKarsten Graul int smc_nl_add_ueid(struct sk_buff *skb, struct genl_info *info)
130fa086662SKarsten Graul {
131fa086662SKarsten Graul 	struct nlattr *nla_ueid = info->attrs[SMC_NLA_EID_TABLE_ENTRY];
132fa086662SKarsten Graul 	char *ueid;
133fa086662SKarsten Graul 
134fa086662SKarsten Graul 	if (!nla_ueid || nla_len(nla_ueid) != SMC_MAX_EID_LEN + 1)
135fa086662SKarsten Graul 		return -EINVAL;
136fa086662SKarsten Graul 	ueid = (char *)nla_data(nla_ueid);
137fa086662SKarsten Graul 
138fa086662SKarsten Graul 	return smc_clc_ueid_add(ueid);
139fa086662SKarsten Graul }
140fa086662SKarsten Graul 
141fa086662SKarsten Graul /* remove one or all ueid entries from the table */
smc_clc_ueid_remove(char * ueid)142fa086662SKarsten Graul static int smc_clc_ueid_remove(char *ueid)
143fa086662SKarsten Graul {
144fa086662SKarsten Graul 	struct smc_clc_eid_entry *lst_ueid, *tmp_ueid;
145fa086662SKarsten Graul 	int rc = -ENOENT;
146fa086662SKarsten Graul 
147fa086662SKarsten Graul 	/* remove table entry */
148fa086662SKarsten Graul 	write_lock(&smc_clc_eid_table.lock);
149fa086662SKarsten Graul 	list_for_each_entry_safe(lst_ueid, tmp_ueid, &smc_clc_eid_table.list,
150fa086662SKarsten Graul 				 list) {
151fa086662SKarsten Graul 		if (!ueid || !memcmp(lst_ueid->eid, ueid, SMC_MAX_EID_LEN)) {
152fa086662SKarsten Graul 			list_del(&lst_ueid->list);
153fa086662SKarsten Graul 			smc_clc_eid_table.ueid_cnt--;
154fa086662SKarsten Graul 			kfree(lst_ueid);
155fa086662SKarsten Graul 			rc = 0;
156fa086662SKarsten Graul 		}
157fa086662SKarsten Graul 	}
158c6b8b8ebSWen Gu #if IS_ENABLED(CONFIG_S390)
1593c572145SKarsten Graul 	if (!rc && !smc_clc_eid_table.ueid_cnt) {
1603c572145SKarsten Graul 		smc_clc_eid_table.seid_enabled = 1;
1613c572145SKarsten Graul 		rc = -EAGAIN;	/* indicate success and enabling of seid */
1623c572145SKarsten Graul 	}
163c6b8b8ebSWen Gu #endif
164fa086662SKarsten Graul 	write_unlock(&smc_clc_eid_table.lock);
165fa086662SKarsten Graul 	return rc;
166fa086662SKarsten Graul }
167fa086662SKarsten Graul 
smc_nl_remove_ueid(struct sk_buff * skb,struct genl_info * info)168fa086662SKarsten Graul int smc_nl_remove_ueid(struct sk_buff *skb, struct genl_info *info)
169fa086662SKarsten Graul {
170fa086662SKarsten Graul 	struct nlattr *nla_ueid = info->attrs[SMC_NLA_EID_TABLE_ENTRY];
171fa086662SKarsten Graul 	char *ueid;
172fa086662SKarsten Graul 
173fa086662SKarsten Graul 	if (!nla_ueid || nla_len(nla_ueid) != SMC_MAX_EID_LEN + 1)
174fa086662SKarsten Graul 		return -EINVAL;
175fa086662SKarsten Graul 	ueid = (char *)nla_data(nla_ueid);
176fa086662SKarsten Graul 
177fa086662SKarsten Graul 	return smc_clc_ueid_remove(ueid);
178fa086662SKarsten Graul }
179fa086662SKarsten Graul 
smc_nl_flush_ueid(struct sk_buff * skb,struct genl_info * info)180fa086662SKarsten Graul int smc_nl_flush_ueid(struct sk_buff *skb, struct genl_info *info)
181fa086662SKarsten Graul {
182fa086662SKarsten Graul 	smc_clc_ueid_remove(NULL);
183fa086662SKarsten Graul 	return 0;
184fa086662SKarsten Graul }
185fa086662SKarsten Graul 
smc_nl_ueid_dumpinfo(struct sk_buff * skb,u32 portid,u32 seq,u32 flags,char * ueid)186fa086662SKarsten Graul static int smc_nl_ueid_dumpinfo(struct sk_buff *skb, u32 portid, u32 seq,
187fa086662SKarsten Graul 				u32 flags, char *ueid)
188fa086662SKarsten Graul {
189fa086662SKarsten Graul 	char ueid_str[SMC_MAX_EID_LEN + 1];
190fa086662SKarsten Graul 	void *hdr;
191fa086662SKarsten Graul 
192fa086662SKarsten Graul 	hdr = genlmsg_put(skb, portid, seq, &smc_gen_nl_family,
193fa086662SKarsten Graul 			  flags, SMC_NETLINK_DUMP_UEID);
194fa086662SKarsten Graul 	if (!hdr)
195fa086662SKarsten Graul 		return -ENOMEM;
196b1871fd4SKarsten Graul 	memcpy(ueid_str, ueid, SMC_MAX_EID_LEN);
197b1871fd4SKarsten Graul 	ueid_str[SMC_MAX_EID_LEN] = 0;
198fa086662SKarsten Graul 	if (nla_put_string(skb, SMC_NLA_EID_TABLE_ENTRY, ueid_str)) {
199fa086662SKarsten Graul 		genlmsg_cancel(skb, hdr);
200fa086662SKarsten Graul 		return -EMSGSIZE;
201fa086662SKarsten Graul 	}
202fa086662SKarsten Graul 	genlmsg_end(skb, hdr);
203fa086662SKarsten Graul 	return 0;
204fa086662SKarsten Graul }
205fa086662SKarsten Graul 
_smc_nl_ueid_dump(struct sk_buff * skb,u32 portid,u32 seq,int start_idx)206fa086662SKarsten Graul static int _smc_nl_ueid_dump(struct sk_buff *skb, u32 portid, u32 seq,
207fa086662SKarsten Graul 			     int start_idx)
208fa086662SKarsten Graul {
209fa086662SKarsten Graul 	struct smc_clc_eid_entry *lst_ueid;
210fa086662SKarsten Graul 	int idx = 0;
211fa086662SKarsten Graul 
212fa086662SKarsten Graul 	read_lock(&smc_clc_eid_table.lock);
213fa086662SKarsten Graul 	list_for_each_entry(lst_ueid, &smc_clc_eid_table.list, list) {
214fa086662SKarsten Graul 		if (idx++ < start_idx)
215fa086662SKarsten Graul 			continue;
216fa086662SKarsten Graul 		if (smc_nl_ueid_dumpinfo(skb, portid, seq, NLM_F_MULTI,
217fa086662SKarsten Graul 					 lst_ueid->eid)) {
218fa086662SKarsten Graul 			--idx;
219fa086662SKarsten Graul 			break;
220fa086662SKarsten Graul 		}
221fa086662SKarsten Graul 	}
222fa086662SKarsten Graul 	read_unlock(&smc_clc_eid_table.lock);
223fa086662SKarsten Graul 	return idx;
224fa086662SKarsten Graul }
225fa086662SKarsten Graul 
smc_nl_dump_ueid(struct sk_buff * skb,struct netlink_callback * cb)226fa086662SKarsten Graul int smc_nl_dump_ueid(struct sk_buff *skb, struct netlink_callback *cb)
227fa086662SKarsten Graul {
228fa086662SKarsten Graul 	struct smc_nl_dmp_ctx *cb_ctx = smc_nl_dmp_ctx(cb);
229fa086662SKarsten Graul 	int idx;
230fa086662SKarsten Graul 
231fa086662SKarsten Graul 	idx = _smc_nl_ueid_dump(skb, NETLINK_CB(cb->skb).portid,
232fa086662SKarsten Graul 				cb->nlh->nlmsg_seq, cb_ctx->pos[0]);
233fa086662SKarsten Graul 
234fa086662SKarsten Graul 	cb_ctx->pos[0] = idx;
235fa086662SKarsten Graul 	return skb->len;
236fa086662SKarsten Graul }
237fa086662SKarsten Graul 
smc_nl_dump_seid(struct sk_buff * skb,struct netlink_callback * cb)2383c572145SKarsten Graul int smc_nl_dump_seid(struct sk_buff *skb, struct netlink_callback *cb)
2393c572145SKarsten Graul {
2403c572145SKarsten Graul 	struct smc_nl_dmp_ctx *cb_ctx = smc_nl_dmp_ctx(cb);
2413c572145SKarsten Graul 	char seid_str[SMC_MAX_EID_LEN + 1];
2423c572145SKarsten Graul 	u8 seid_enabled;
2433c572145SKarsten Graul 	void *hdr;
2443c572145SKarsten Graul 	u8 *seid;
2453c572145SKarsten Graul 
2463c572145SKarsten Graul 	if (cb_ctx->pos[0])
2473c572145SKarsten Graul 		return skb->len;
2483c572145SKarsten Graul 
2493c572145SKarsten Graul 	hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
2503c572145SKarsten Graul 			  &smc_gen_nl_family, NLM_F_MULTI,
2513c572145SKarsten Graul 			  SMC_NETLINK_DUMP_SEID);
2523c572145SKarsten Graul 	if (!hdr)
2533c572145SKarsten Graul 		return -ENOMEM;
2543c572145SKarsten Graul 	if (!smc_ism_is_v2_capable())
2553c572145SKarsten Graul 		goto end;
2563c572145SKarsten Graul 
2573c572145SKarsten Graul 	smc_ism_get_system_eid(&seid);
258b1871fd4SKarsten Graul 	memcpy(seid_str, seid, SMC_MAX_EID_LEN);
259b1871fd4SKarsten Graul 	seid_str[SMC_MAX_EID_LEN] = 0;
2603c572145SKarsten Graul 	if (nla_put_string(skb, SMC_NLA_SEID_ENTRY, seid_str))
2613c572145SKarsten Graul 		goto err;
2623c572145SKarsten Graul 	read_lock(&smc_clc_eid_table.lock);
2633c572145SKarsten Graul 	seid_enabled = smc_clc_eid_table.seid_enabled;
2643c572145SKarsten Graul 	read_unlock(&smc_clc_eid_table.lock);
2653c572145SKarsten Graul 	if (nla_put_u8(skb, SMC_NLA_SEID_ENABLED, seid_enabled))
2663c572145SKarsten Graul 		goto err;
2673c572145SKarsten Graul end:
2683c572145SKarsten Graul 	genlmsg_end(skb, hdr);
2693c572145SKarsten Graul 	cb_ctx->pos[0]++;
2703c572145SKarsten Graul 	return skb->len;
2713c572145SKarsten Graul err:
2723c572145SKarsten Graul 	genlmsg_cancel(skb, hdr);
2733c572145SKarsten Graul 	return -EMSGSIZE;
2743c572145SKarsten Graul }
2753c572145SKarsten Graul 
smc_nl_enable_seid(struct sk_buff * skb,struct genl_info * info)2763c572145SKarsten Graul int smc_nl_enable_seid(struct sk_buff *skb, struct genl_info *info)
2773c572145SKarsten Graul {
278c6b8b8ebSWen Gu #if IS_ENABLED(CONFIG_S390)
2793c572145SKarsten Graul 	write_lock(&smc_clc_eid_table.lock);
2803c572145SKarsten Graul 	smc_clc_eid_table.seid_enabled = 1;
2813c572145SKarsten Graul 	write_unlock(&smc_clc_eid_table.lock);
2823c572145SKarsten Graul 	return 0;
283c6b8b8ebSWen Gu #else
284c6b8b8ebSWen Gu 	return -EOPNOTSUPP;
285c6b8b8ebSWen Gu #endif
2863c572145SKarsten Graul }
2873c572145SKarsten Graul 
smc_nl_disable_seid(struct sk_buff * skb,struct genl_info * info)2883c572145SKarsten Graul int smc_nl_disable_seid(struct sk_buff *skb, struct genl_info *info)
2893c572145SKarsten Graul {
2903c572145SKarsten Graul 	int rc = 0;
2913c572145SKarsten Graul 
292c6b8b8ebSWen Gu #if IS_ENABLED(CONFIG_S390)
2933c572145SKarsten Graul 	write_lock(&smc_clc_eid_table.lock);
2943c572145SKarsten Graul 	if (!smc_clc_eid_table.ueid_cnt)
2953c572145SKarsten Graul 		rc = -ENOENT;
2963c572145SKarsten Graul 	else
2973c572145SKarsten Graul 		smc_clc_eid_table.seid_enabled = 0;
2983c572145SKarsten Graul 	write_unlock(&smc_clc_eid_table.lock);
299c6b8b8ebSWen Gu #else
300c6b8b8ebSWen Gu 	rc = -EOPNOTSUPP;
301c6b8b8ebSWen Gu #endif
3023c572145SKarsten Graul 	return rc;
3033c572145SKarsten Graul }
3043c572145SKarsten Graul 
_smc_clc_match_ueid(u8 * peer_ueid)305fa086662SKarsten Graul static bool _smc_clc_match_ueid(u8 *peer_ueid)
306fa086662SKarsten Graul {
307fa086662SKarsten Graul 	struct smc_clc_eid_entry *tmp_ueid;
308fa086662SKarsten Graul 
309fa086662SKarsten Graul 	list_for_each_entry(tmp_ueid, &smc_clc_eid_table.list, list) {
310fa086662SKarsten Graul 		if (!memcmp(tmp_ueid->eid, peer_ueid, SMC_MAX_EID_LEN))
311fa086662SKarsten Graul 			return true;
312fa086662SKarsten Graul 	}
313fa086662SKarsten Graul 	return false;
314fa086662SKarsten Graul }
315fa086662SKarsten Graul 
smc_clc_match_eid(u8 * negotiated_eid,struct smc_clc_v2_extension * smc_v2_ext,u8 * peer_eid,u8 * local_eid)316fa086662SKarsten Graul bool smc_clc_match_eid(u8 *negotiated_eid,
317fa086662SKarsten Graul 		       struct smc_clc_v2_extension *smc_v2_ext,
318fa086662SKarsten Graul 		       u8 *peer_eid, u8 *local_eid)
319fa086662SKarsten Graul {
320fa086662SKarsten Graul 	bool match = false;
321fa086662SKarsten Graul 	int i;
322fa086662SKarsten Graul 
323fa086662SKarsten Graul 	negotiated_eid[0] = 0;
324fa086662SKarsten Graul 	read_lock(&smc_clc_eid_table.lock);
325e5c4744cSKarsten Graul 	if (peer_eid && local_eid &&
326e5c4744cSKarsten Graul 	    smc_clc_eid_table.seid_enabled &&
327fa086662SKarsten Graul 	    smc_v2_ext->hdr.flag.seid &&
328fa086662SKarsten Graul 	    !memcmp(peer_eid, local_eid, SMC_MAX_EID_LEN)) {
329fa086662SKarsten Graul 		memcpy(negotiated_eid, peer_eid, SMC_MAX_EID_LEN);
330fa086662SKarsten Graul 		match = true;
331fa086662SKarsten Graul 		goto out;
332fa086662SKarsten Graul 	}
333fa086662SKarsten Graul 
334fa086662SKarsten Graul 	for (i = 0; i < smc_v2_ext->hdr.eid_cnt; i++) {
335fa086662SKarsten Graul 		if (_smc_clc_match_ueid(smc_v2_ext->user_eids[i])) {
336fa086662SKarsten Graul 			memcpy(negotiated_eid, smc_v2_ext->user_eids[i],
337fa086662SKarsten Graul 			       SMC_MAX_EID_LEN);
338fa086662SKarsten Graul 			match = true;
339fa086662SKarsten Graul 			goto out;
340fa086662SKarsten Graul 		}
341fa086662SKarsten Graul 	}
342fa086662SKarsten Graul out:
343fa086662SKarsten Graul 	read_unlock(&smc_clc_eid_table.lock);
344fa086662SKarsten Graul 	return match;
345fa086662SKarsten Graul }
346fa086662SKarsten Graul 
3478c3dca34SUrsula Braun /* check arriving CLC proposal */
smc_clc_msg_prop_valid(struct smc_clc_msg_proposal * pclc)3488c3dca34SUrsula Braun static bool smc_clc_msg_prop_valid(struct smc_clc_msg_proposal *pclc)
3498c3dca34SUrsula Braun {
3508c3dca34SUrsula Braun 	struct smc_clc_msg_proposal_prefix *pclc_prfx;
3518c3dca34SUrsula Braun 	struct smc_clc_smcd_v2_extension *smcd_v2_ext;
3528c3dca34SUrsula Braun 	struct smc_clc_msg_hdr *hdr = &pclc->hdr;
3538c3dca34SUrsula Braun 	struct smc_clc_v2_extension *v2_ext;
3548c3dca34SUrsula Braun 
3558c3dca34SUrsula Braun 	v2_ext = smc_get_clc_v2_ext(pclc);
3568c3dca34SUrsula Braun 	pclc_prfx = smc_clc_proposal_get_prefix(pclc);
3578c3dca34SUrsula Braun 	if (hdr->version == SMC_V1) {
3588c3dca34SUrsula Braun 		if (hdr->typev1 == SMC_TYPE_N)
3598c3dca34SUrsula Braun 			return false;
3608c3dca34SUrsula Braun 		if (ntohs(hdr->length) !=
3618c3dca34SUrsula Braun 			sizeof(*pclc) + ntohs(pclc->iparea_offset) +
3628c3dca34SUrsula Braun 			sizeof(*pclc_prfx) +
3638c3dca34SUrsula Braun 			pclc_prfx->ipv6_prefixes_cnt *
3648c3dca34SUrsula Braun 				sizeof(struct smc_clc_ipv6_prefix) +
3658c3dca34SUrsula Braun 			sizeof(struct smc_clc_msg_trail))
3668c3dca34SUrsula Braun 			return false;
3678c3dca34SUrsula Braun 	} else {
3688c3dca34SUrsula Braun 		if (ntohs(hdr->length) !=
3698c3dca34SUrsula Braun 			sizeof(*pclc) +
3708c3dca34SUrsula Braun 			sizeof(struct smc_clc_msg_smcd) +
3718c3dca34SUrsula Braun 			(hdr->typev1 != SMC_TYPE_N ?
3728c3dca34SUrsula Braun 				sizeof(*pclc_prfx) +
3738c3dca34SUrsula Braun 				pclc_prfx->ipv6_prefixes_cnt *
3748c3dca34SUrsula Braun 				sizeof(struct smc_clc_ipv6_prefix) : 0) +
3758c3dca34SUrsula Braun 			(hdr->typev2 != SMC_TYPE_N ?
3768c3dca34SUrsula Braun 				sizeof(*v2_ext) +
3778c3dca34SUrsula Braun 				v2_ext->hdr.eid_cnt * SMC_MAX_EID_LEN : 0) +
3788c3dca34SUrsula Braun 			(smcd_indicated(hdr->typev2) ?
3798c3dca34SUrsula Braun 				sizeof(*smcd_v2_ext) + v2_ext->hdr.ism_gid_cnt *
3808c3dca34SUrsula Braun 					sizeof(struct smc_clc_smcd_gid_chid) :
3818c3dca34SUrsula Braun 				0) +
3828c3dca34SUrsula Braun 			sizeof(struct smc_clc_msg_trail))
3838c3dca34SUrsula Braun 			return false;
3848c3dca34SUrsula Braun 	}
3858c3dca34SUrsula Braun 	return true;
3868c3dca34SUrsula Braun }
3878c3dca34SUrsula Braun 
388a7c9c5f4SUrsula Braun /* check arriving CLC accept or confirm */
389a7c9c5f4SUrsula Braun static bool
smc_clc_msg_acc_conf_valid(struct smc_clc_msg_accept_confirm * clc)3909505450dSWen Gu smc_clc_msg_acc_conf_valid(struct smc_clc_msg_accept_confirm *clc)
391a7c9c5f4SUrsula Braun {
3929505450dSWen Gu 	struct smc_clc_msg_hdr *hdr = &clc->hdr;
393a7c9c5f4SUrsula Braun 
394a7c9c5f4SUrsula Braun 	if (hdr->typev1 != SMC_TYPE_R && hdr->typev1 != SMC_TYPE_D)
395a7c9c5f4SUrsula Braun 		return false;
396a7c9c5f4SUrsula Braun 	if (hdr->version == SMC_V1) {
397a7c9c5f4SUrsula Braun 		if ((hdr->typev1 == SMC_TYPE_R &&
398a7c9c5f4SUrsula Braun 		     ntohs(hdr->length) != SMCR_CLC_ACCEPT_CONFIRM_LEN) ||
399a7c9c5f4SUrsula Braun 		    (hdr->typev1 == SMC_TYPE_D &&
400a7c9c5f4SUrsula Braun 		     ntohs(hdr->length) != SMCD_CLC_ACCEPT_CONFIRM_LEN))
401a7c9c5f4SUrsula Braun 			return false;
402a7c9c5f4SUrsula Braun 	} else {
403a7c9c5f4SUrsula Braun 		if (hdr->typev1 == SMC_TYPE_D &&
4047290178aSGuangguan Wang 		    ntohs(hdr->length) < SMCD_CLC_ACCEPT_CONFIRM_LEN_V2)
405a7c9c5f4SUrsula Braun 			return false;
406e5c4744cSKarsten Graul 		if (hdr->typev1 == SMC_TYPE_R &&
407e5c4744cSKarsten Graul 		    ntohs(hdr->length) < SMCR_CLC_ACCEPT_CONFIRM_LEN_V2)
408e5c4744cSKarsten Graul 			return false;
409a7c9c5f4SUrsula Braun 	}
410a7c9c5f4SUrsula Braun 	return true;
411a7c9c5f4SUrsula Braun }
412a7c9c5f4SUrsula Braun 
4138ade200cSKarsten Graul /* check arriving CLC decline */
4148ade200cSKarsten Graul static bool
smc_clc_msg_decl_valid(struct smc_clc_msg_decline * dclc)4158ade200cSKarsten Graul smc_clc_msg_decl_valid(struct smc_clc_msg_decline *dclc)
4168ade200cSKarsten Graul {
4178ade200cSKarsten Graul 	struct smc_clc_msg_hdr *hdr = &dclc->hdr;
4188ade200cSKarsten Graul 
4198ade200cSKarsten Graul 	if (hdr->typev1 != SMC_TYPE_R && hdr->typev1 != SMC_TYPE_D)
4208ade200cSKarsten Graul 		return false;
4218ade200cSKarsten Graul 	if (hdr->version == SMC_V1) {
4228ade200cSKarsten Graul 		if (ntohs(hdr->length) != sizeof(struct smc_clc_msg_decline))
4238ade200cSKarsten Graul 			return false;
4248ade200cSKarsten Graul 	} else {
4258ade200cSKarsten Graul 		if (ntohs(hdr->length) != sizeof(struct smc_clc_msg_decline_v2))
4268ade200cSKarsten Graul 			return false;
4278ade200cSKarsten Graul 	}
4288ade200cSKarsten Graul 	return true;
4298ade200cSKarsten Graul }
4308ade200cSKarsten Graul 
smc_clc_fill_fce_v2x(struct smc_clc_first_contact_ext_v2x * fce_v2x,struct smc_init_info * ini)431ac053a16SWen Gu static int smc_clc_fill_fce_v2x(struct smc_clc_first_contact_ext_v2x *fce_v2x,
4327290178aSGuangguan Wang 				struct smc_init_info *ini)
433b81a5eb7SUrsula Braun {
434ac053a16SWen Gu 	int ret = sizeof(*fce_v2x);
4357290178aSGuangguan Wang 
436ac053a16SWen Gu 	memset(fce_v2x, 0, sizeof(*fce_v2x));
437ac053a16SWen Gu 	fce_v2x->fce_v2_base.os_type = SMC_CLC_OS_LINUX;
438ac053a16SWen Gu 	fce_v2x->fce_v2_base.release = ini->release_nr;
439ac053a16SWen Gu 	memcpy(fce_v2x->fce_v2_base.hostname,
440ac053a16SWen Gu 	       smc_hostname, sizeof(smc_hostname));
4417f0620b9SGuangguan Wang 	if (ini->is_smcd && ini->release_nr < SMC_RELEASE_1) {
4427290178aSGuangguan Wang 		ret = sizeof(struct smc_clc_first_contact_ext);
4437f0620b9SGuangguan Wang 		goto out;
4447f0620b9SGuangguan Wang 	}
4457290178aSGuangguan Wang 
4467f0620b9SGuangguan Wang 	if (ini->release_nr >= SMC_RELEASE_1) {
44769b888e3SGuangguan Wang 		if (!ini->is_smcd) {
448ac053a16SWen Gu 			fce_v2x->max_conns = ini->max_conns;
449ac053a16SWen Gu 			fce_v2x->max_links = ini->max_links;
45069b888e3SGuangguan Wang 		}
451ece60db3SWen Gu 		fce_v2x->feature_mask = htons(ini->feature_mask);
4527f0620b9SGuangguan Wang 	}
4537f0620b9SGuangguan Wang 
4547f0620b9SGuangguan Wang out:
4557290178aSGuangguan Wang 	return ret;
456b81a5eb7SUrsula Braun }
457b81a5eb7SUrsula Braun 
458e7b7a64aSUrsula Braun /* check if received message has a correct header length and contains valid
459e7b7a64aSUrsula Braun  * heading and trailing eyecatchers
460e7b7a64aSUrsula Braun  */
smc_clc_msg_hdr_valid(struct smc_clc_msg_hdr * clcm,bool check_trl)461fb4f7926SUrsula Braun static bool smc_clc_msg_hdr_valid(struct smc_clc_msg_hdr *clcm, bool check_trl)
462e7b7a64aSUrsula Braun {
4639505450dSWen Gu 	struct smc_clc_msg_accept_confirm *clc;
464e7b7a64aSUrsula Braun 	struct smc_clc_msg_proposal *pclc;
465e7b7a64aSUrsula Braun 	struct smc_clc_msg_decline *dclc;
466e7b7a64aSUrsula Braun 	struct smc_clc_msg_trail *trl;
467e7b7a64aSUrsula Braun 
468c758dfddSHans Wippel 	if (memcmp(clcm->eyecatcher, SMC_EYECATCHER, sizeof(SMC_EYECATCHER)) &&
469c758dfddSHans Wippel 	    memcmp(clcm->eyecatcher, SMCD_EYECATCHER, sizeof(SMCD_EYECATCHER)))
470e7b7a64aSUrsula Braun 		return false;
471e7b7a64aSUrsula Braun 	switch (clcm->type) {
472e7b7a64aSUrsula Braun 	case SMC_CLC_PROPOSAL:
473e7b7a64aSUrsula Braun 		pclc = (struct smc_clc_msg_proposal *)clcm;
4748c3dca34SUrsula Braun 		if (!smc_clc_msg_prop_valid(pclc))
475e7b7a64aSUrsula Braun 			return false;
476e7b7a64aSUrsula Braun 		trl = (struct smc_clc_msg_trail *)
477e7b7a64aSUrsula Braun 			((u8 *)pclc + ntohs(pclc->hdr.length) - sizeof(*trl));
478e7b7a64aSUrsula Braun 		break;
479e7b7a64aSUrsula Braun 	case SMC_CLC_ACCEPT:
480e7b7a64aSUrsula Braun 	case SMC_CLC_CONFIRM:
4819505450dSWen Gu 		clc = (struct smc_clc_msg_accept_confirm *)clcm;
4829505450dSWen Gu 		if (!smc_clc_msg_acc_conf_valid(clc))
483c758dfddSHans Wippel 			return false;
484c758dfddSHans Wippel 		trl = (struct smc_clc_msg_trail *)
4859505450dSWen Gu 			((u8 *)clc + ntohs(clc->hdr.length) - sizeof(*trl));
486e7b7a64aSUrsula Braun 		break;
487e7b7a64aSUrsula Braun 	case SMC_CLC_DECLINE:
488e7b7a64aSUrsula Braun 		dclc = (struct smc_clc_msg_decline *)clcm;
4898ade200cSKarsten Graul 		if (!smc_clc_msg_decl_valid(dclc))
490e7b7a64aSUrsula Braun 			return false;
4918ade200cSKarsten Graul 		check_trl = false;
492e7b7a64aSUrsula Braun 		break;
493e7b7a64aSUrsula Braun 	default:
494e7b7a64aSUrsula Braun 		return false;
495e7b7a64aSUrsula Braun 	}
496fb4f7926SUrsula Braun 	if (check_trl &&
497fb4f7926SUrsula Braun 	    memcmp(trl->eyecatcher, SMC_EYECATCHER, sizeof(SMC_EYECATCHER)) &&
498c758dfddSHans Wippel 	    memcmp(trl->eyecatcher, SMCD_EYECATCHER, sizeof(SMCD_EYECATCHER)))
499e7b7a64aSUrsula Braun 		return false;
500e7b7a64aSUrsula Braun 	return true;
501e7b7a64aSUrsula Braun }
502e7b7a64aSUrsula Braun 
503c246d942SKarsten Graul /* find ipv4 addr on device and get the prefix len, fill CLC proposal msg */
smc_clc_prfx_set4_rcu(struct dst_entry * dst,__be32 ipv4,struct smc_clc_msg_proposal_prefix * prop)504c246d942SKarsten Graul static int smc_clc_prfx_set4_rcu(struct dst_entry *dst, __be32 ipv4,
505c246d942SKarsten Graul 				 struct smc_clc_msg_proposal_prefix *prop)
506c246d942SKarsten Graul {
507c246d942SKarsten Graul 	struct in_device *in_dev = __in_dev_get_rcu(dst->dev);
508cd5a411dSFlorian Westphal 	const struct in_ifaddr *ifa;
509c246d942SKarsten Graul 
510c246d942SKarsten Graul 	if (!in_dev)
511c246d942SKarsten Graul 		return -ENODEV;
512cd5a411dSFlorian Westphal 
513cd5a411dSFlorian Westphal 	in_dev_for_each_ifa_rcu(ifa, in_dev) {
514c246d942SKarsten Graul 		if (!inet_ifa_match(ipv4, ifa))
515c246d942SKarsten Graul 			continue;
516c246d942SKarsten Graul 		prop->prefix_len = inet_mask_len(ifa->ifa_mask);
517c246d942SKarsten Graul 		prop->outgoing_subnet = ifa->ifa_address & ifa->ifa_mask;
518c246d942SKarsten Graul 		/* prop->ipv6_prefixes_cnt = 0; already done by memset before */
519c246d942SKarsten Graul 		return 0;
520cd5a411dSFlorian Westphal 	}
521c246d942SKarsten Graul 	return -ENOENT;
522c246d942SKarsten Graul }
523c246d942SKarsten Graul 
5241a26d020SKarsten Graul /* fill CLC proposal msg with ipv6 prefixes from device */
smc_clc_prfx_set6_rcu(struct dst_entry * dst,struct smc_clc_msg_proposal_prefix * prop,struct smc_clc_ipv6_prefix * ipv6_prfx)5251a26d020SKarsten Graul static int smc_clc_prfx_set6_rcu(struct dst_entry *dst,
5261a26d020SKarsten Graul 				 struct smc_clc_msg_proposal_prefix *prop,
5271a26d020SKarsten Graul 				 struct smc_clc_ipv6_prefix *ipv6_prfx)
5281a26d020SKarsten Graul {
5291a26d020SKarsten Graul #if IS_ENABLED(CONFIG_IPV6)
5301a26d020SKarsten Graul 	struct inet6_dev *in6_dev = __in6_dev_get(dst->dev);
5311a26d020SKarsten Graul 	struct inet6_ifaddr *ifa;
5321a26d020SKarsten Graul 	int cnt = 0;
5331a26d020SKarsten Graul 
5341a26d020SKarsten Graul 	if (!in6_dev)
5351a26d020SKarsten Graul 		return -ENODEV;
5361a26d020SKarsten Graul 	/* use a maximum of 8 IPv6 prefixes from device */
5371a26d020SKarsten Graul 	list_for_each_entry(ifa, &in6_dev->addr_list, if_list) {
5381a26d020SKarsten Graul 		if (ipv6_addr_type(&ifa->addr) & IPV6_ADDR_LINKLOCAL)
5391a26d020SKarsten Graul 			continue;
5401a26d020SKarsten Graul 		ipv6_addr_prefix(&ipv6_prfx[cnt].prefix,
5411a26d020SKarsten Graul 				 &ifa->addr, ifa->prefix_len);
5421a26d020SKarsten Graul 		ipv6_prfx[cnt].prefix_len = ifa->prefix_len;
5431a26d020SKarsten Graul 		cnt++;
5441a26d020SKarsten Graul 		if (cnt == SMC_CLC_MAX_V6_PREFIX)
5451a26d020SKarsten Graul 			break;
5461a26d020SKarsten Graul 	}
5471a26d020SKarsten Graul 	prop->ipv6_prefixes_cnt = cnt;
5481a26d020SKarsten Graul 	if (cnt)
5491a26d020SKarsten Graul 		return 0;
5501a26d020SKarsten Graul #endif
5511a26d020SKarsten Graul 	return -ENOENT;
5521a26d020SKarsten Graul }
5531a26d020SKarsten Graul 
554c246d942SKarsten Graul /* retrieve and set prefixes in CLC proposal msg */
smc_clc_prfx_set(struct socket * clcsock,struct smc_clc_msg_proposal_prefix * prop,struct smc_clc_ipv6_prefix * ipv6_prfx)555c246d942SKarsten Graul static int smc_clc_prfx_set(struct socket *clcsock,
5561a26d020SKarsten Graul 			    struct smc_clc_msg_proposal_prefix *prop,
5571a26d020SKarsten Graul 			    struct smc_clc_ipv6_prefix *ipv6_prfx)
558696cd301SKarsten Graul {
559696cd301SKarsten Graul 	struct dst_entry *dst = sk_dst_get(clcsock->sk);
560c246d942SKarsten Graul 	struct sockaddr_storage addrs;
5611a26d020SKarsten Graul 	struct sockaddr_in6 *addr6;
562c246d942SKarsten Graul 	struct sockaddr_in *addr;
563c246d942SKarsten Graul 	int rc = -ENOENT;
564c246d942SKarsten Graul 
565c246d942SKarsten Graul 	if (!dst) {
566c246d942SKarsten Graul 		rc = -ENOTCONN;
567c246d942SKarsten Graul 		goto out;
568c246d942SKarsten Graul 	}
569c246d942SKarsten Graul 	if (!dst->dev) {
570c246d942SKarsten Graul 		rc = -ENODEV;
571c246d942SKarsten Graul 		goto out_rel;
572c246d942SKarsten Graul 	}
573c246d942SKarsten Graul 	/* get address to which the internal TCP socket is bound */
5746c907319SKarsten Graul 	if (kernel_getsockname(clcsock, (struct sockaddr *)&addrs) < 0)
5756c907319SKarsten Graul 		goto out_rel;
576c246d942SKarsten Graul 	/* analyze IP specific data of net_device belonging to TCP socket */
5771a26d020SKarsten Graul 	addr6 = (struct sockaddr_in6 *)&addrs;
578c246d942SKarsten Graul 	rcu_read_lock();
579c246d942SKarsten Graul 	if (addrs.ss_family == PF_INET) {
580c246d942SKarsten Graul 		/* IPv4 */
581c246d942SKarsten Graul 		addr = (struct sockaddr_in *)&addrs;
582c246d942SKarsten Graul 		rc = smc_clc_prfx_set4_rcu(dst, addr->sin_addr.s_addr, prop);
5831a26d020SKarsten Graul 	} else if (ipv6_addr_v4mapped(&addr6->sin6_addr)) {
5841a26d020SKarsten Graul 		/* mapped IPv4 address - peer is IPv4 only */
5851a26d020SKarsten Graul 		rc = smc_clc_prfx_set4_rcu(dst, addr6->sin6_addr.s6_addr32[3],
5861a26d020SKarsten Graul 					   prop);
5871a26d020SKarsten Graul 	} else {
5881a26d020SKarsten Graul 		/* IPv6 */
5891a26d020SKarsten Graul 		rc = smc_clc_prfx_set6_rcu(dst, prop, ipv6_prfx);
590c246d942SKarsten Graul 	}
591c246d942SKarsten Graul 	rcu_read_unlock();
592c246d942SKarsten Graul out_rel:
593c246d942SKarsten Graul 	dst_release(dst);
594c246d942SKarsten Graul out:
595c246d942SKarsten Graul 	return rc;
596c246d942SKarsten Graul }
597c246d942SKarsten Graul 
598c246d942SKarsten Graul /* match ipv4 addrs of dev against addr in CLC proposal */
smc_clc_prfx_match4_rcu(struct net_device * dev,struct smc_clc_msg_proposal_prefix * prop)599c246d942SKarsten Graul static int smc_clc_prfx_match4_rcu(struct net_device *dev,
600c246d942SKarsten Graul 				   struct smc_clc_msg_proposal_prefix *prop)
601c246d942SKarsten Graul {
602c246d942SKarsten Graul 	struct in_device *in_dev = __in_dev_get_rcu(dev);
603cd5a411dSFlorian Westphal 	const struct in_ifaddr *ifa;
604c246d942SKarsten Graul 
605c246d942SKarsten Graul 	if (!in_dev)
606c246d942SKarsten Graul 		return -ENODEV;
607cd5a411dSFlorian Westphal 	in_dev_for_each_ifa_rcu(ifa, in_dev) {
608c246d942SKarsten Graul 		if (prop->prefix_len == inet_mask_len(ifa->ifa_mask) &&
609c246d942SKarsten Graul 		    inet_ifa_match(prop->outgoing_subnet, ifa))
610c246d942SKarsten Graul 			return 0;
611cd5a411dSFlorian Westphal 	}
612c246d942SKarsten Graul 
613c246d942SKarsten Graul 	return -ENOENT;
614c246d942SKarsten Graul }
615c246d942SKarsten Graul 
6161a26d020SKarsten Graul /* match ipv6 addrs of dev against addrs in CLC proposal */
smc_clc_prfx_match6_rcu(struct net_device * dev,struct smc_clc_msg_proposal_prefix * prop)6171a26d020SKarsten Graul static int smc_clc_prfx_match6_rcu(struct net_device *dev,
6181a26d020SKarsten Graul 				   struct smc_clc_msg_proposal_prefix *prop)
6191a26d020SKarsten Graul {
6201a26d020SKarsten Graul #if IS_ENABLED(CONFIG_IPV6)
6211a26d020SKarsten Graul 	struct inet6_dev *in6_dev = __in6_dev_get(dev);
6221a26d020SKarsten Graul 	struct smc_clc_ipv6_prefix *ipv6_prfx;
6231a26d020SKarsten Graul 	struct inet6_ifaddr *ifa;
6241a26d020SKarsten Graul 	int i, max;
6251a26d020SKarsten Graul 
6261a26d020SKarsten Graul 	if (!in6_dev)
6271a26d020SKarsten Graul 		return -ENODEV;
6281a26d020SKarsten Graul 	/* ipv6 prefix list starts behind smc_clc_msg_proposal_prefix */
6291a26d020SKarsten Graul 	ipv6_prfx = (struct smc_clc_ipv6_prefix *)((u8 *)prop + sizeof(*prop));
6301a26d020SKarsten Graul 	max = min_t(u8, prop->ipv6_prefixes_cnt, SMC_CLC_MAX_V6_PREFIX);
6311a26d020SKarsten Graul 	list_for_each_entry(ifa, &in6_dev->addr_list, if_list) {
6321a26d020SKarsten Graul 		if (ipv6_addr_type(&ifa->addr) & IPV6_ADDR_LINKLOCAL)
6331a26d020SKarsten Graul 			continue;
6341a26d020SKarsten Graul 		for (i = 0; i < max; i++) {
6351a26d020SKarsten Graul 			if (ifa->prefix_len == ipv6_prfx[i].prefix_len &&
6361a26d020SKarsten Graul 			    ipv6_prefix_equal(&ifa->addr, &ipv6_prfx[i].prefix,
6371a26d020SKarsten Graul 					      ifa->prefix_len))
6381a26d020SKarsten Graul 				return 0;
6391a26d020SKarsten Graul 		}
6401a26d020SKarsten Graul 	}
6411a26d020SKarsten Graul #endif
6421a26d020SKarsten Graul 	return -ENOENT;
6431a26d020SKarsten Graul }
6441a26d020SKarsten Graul 
645c246d942SKarsten Graul /* check if proposed prefixes match one of our device prefixes */
smc_clc_prfx_match(struct socket * clcsock,struct smc_clc_msg_proposal_prefix * prop)646c246d942SKarsten Graul int smc_clc_prfx_match(struct socket *clcsock,
647c246d942SKarsten Graul 		       struct smc_clc_msg_proposal_prefix *prop)
648c246d942SKarsten Graul {
649c246d942SKarsten Graul 	struct dst_entry *dst = sk_dst_get(clcsock->sk);
6501a26d020SKarsten Graul 	int rc;
651696cd301SKarsten Graul 
652696cd301SKarsten Graul 	if (!dst) {
653696cd301SKarsten Graul 		rc = -ENOTCONN;
654696cd301SKarsten Graul 		goto out;
655696cd301SKarsten Graul 	}
656696cd301SKarsten Graul 	if (!dst->dev) {
657696cd301SKarsten Graul 		rc = -ENODEV;
658696cd301SKarsten Graul 		goto out_rel;
659696cd301SKarsten Graul 	}
660696cd301SKarsten Graul 	rcu_read_lock();
661c246d942SKarsten Graul 	if (!prop->ipv6_prefixes_cnt)
662c246d942SKarsten Graul 		rc = smc_clc_prfx_match4_rcu(dst->dev, prop);
6631a26d020SKarsten Graul 	else
6641a26d020SKarsten Graul 		rc = smc_clc_prfx_match6_rcu(dst->dev, prop);
665696cd301SKarsten Graul 	rcu_read_unlock();
666696cd301SKarsten Graul out_rel:
667696cd301SKarsten Graul 	dst_release(dst);
668696cd301SKarsten Graul out:
669696cd301SKarsten Graul 	return rc;
670696cd301SKarsten Graul }
671696cd301SKarsten Graul 
672a046d57dSUrsula Braun /* Wait for data on the tcp-socket, analyze received data
673a046d57dSUrsula Braun  * Returns:
674a046d57dSUrsula Braun  * 0 if success and it was not a decline that we received.
675a046d57dSUrsula Braun  * SMC_CLC_DECL_REPLY if decline received for fallback w/o another decl send.
676a046d57dSUrsula Braun  * clcsock error, -EINTR, -ECONNRESET, -EPROTO otherwise.
677a046d57dSUrsula Braun  */
smc_clc_wait_msg(struct smc_sock * smc,void * buf,int buflen,u8 expected_type,unsigned long timeout)678a046d57dSUrsula Braun int smc_clc_wait_msg(struct smc_sock *smc, void *buf, int buflen,
6792b59f58eSUrsula Braun 		     u8 expected_type, unsigned long timeout)
680a046d57dSUrsula Braun {
681f6bdc42fSKarsten Graul 	long rcvtimeo = smc->clcsock->sk->sk_rcvtimeo;
682a046d57dSUrsula Braun 	struct sock *clc_sk = smc->clcsock->sk;
683a046d57dSUrsula Braun 	struct smc_clc_msg_hdr *clcm = buf;
684a046d57dSUrsula Braun 	struct msghdr msg = {NULL, 0};
685a046d57dSUrsula Braun 	int reason_code = 0;
686d63d271cSAl Viro 	struct kvec vec = {buf, buflen};
687fb4f7926SUrsula Braun 	int len, datlen, recvlen;
688fb4f7926SUrsula Braun 	bool check_trl = true;
689a046d57dSUrsula Braun 	int krflags;
690a046d57dSUrsula Braun 
691a046d57dSUrsula Braun 	/* peek the first few bytes to determine length of data to receive
692a046d57dSUrsula Braun 	 * so we don't consume any subsequent CLC message or payload data
693a046d57dSUrsula Braun 	 * in the TCP byte stream
694a046d57dSUrsula Braun 	 */
695d63d271cSAl Viro 	/*
696d63d271cSAl Viro 	 * Caller must make sure that buflen is no less than
697d63d271cSAl Viro 	 * sizeof(struct smc_clc_msg_hdr)
698d63d271cSAl Viro 	 */
699a046d57dSUrsula Braun 	krflags = MSG_PEEK | MSG_WAITALL;
7002b59f58eSUrsula Braun 	clc_sk->sk_rcvtimeo = timeout;
701de4eda9dSAl Viro 	iov_iter_kvec(&msg.msg_iter, ITER_DEST, &vec, 1,
702d63d271cSAl Viro 			sizeof(struct smc_clc_msg_hdr));
703d63d271cSAl Viro 	len = sock_recvmsg(smc->clcsock, &msg, krflags);
704a046d57dSUrsula Braun 	if (signal_pending(current)) {
705a046d57dSUrsula Braun 		reason_code = -EINTR;
706a046d57dSUrsula Braun 		clc_sk->sk_err = EINTR;
707a046d57dSUrsula Braun 		smc->sk.sk_err = EINTR;
708a046d57dSUrsula Braun 		goto out;
709a046d57dSUrsula Braun 	}
710a046d57dSUrsula Braun 	if (clc_sk->sk_err) {
711a046d57dSUrsula Braun 		reason_code = -clc_sk->sk_err;
7129ed28556SUrsula Braun 		if (clc_sk->sk_err == EAGAIN &&
7139ed28556SUrsula Braun 		    expected_type == SMC_CLC_DECLINE)
7149ed28556SUrsula Braun 			clc_sk->sk_err = 0; /* reset for fallback usage */
7159ed28556SUrsula Braun 		else
716a046d57dSUrsula Braun 			smc->sk.sk_err = clc_sk->sk_err;
717a046d57dSUrsula Braun 		goto out;
718a046d57dSUrsula Braun 	}
719a046d57dSUrsula Braun 	if (!len) { /* peer has performed orderly shutdown */
720a046d57dSUrsula Braun 		smc->sk.sk_err = ECONNRESET;
721a046d57dSUrsula Braun 		reason_code = -ECONNRESET;
722a046d57dSUrsula Braun 		goto out;
723a046d57dSUrsula Braun 	}
724a046d57dSUrsula Braun 	if (len < 0) {
7259ed28556SUrsula Braun 		if (len != -EAGAIN || expected_type != SMC_CLC_DECLINE)
726a046d57dSUrsula Braun 			smc->sk.sk_err = -len;
727a046d57dSUrsula Braun 		reason_code = len;
728a046d57dSUrsula Braun 		goto out;
729a046d57dSUrsula Braun 	}
730a046d57dSUrsula Braun 	datlen = ntohs(clcm->length);
731a046d57dSUrsula Braun 	if ((len < sizeof(struct smc_clc_msg_hdr)) ||
7325ac54d87SUrsula Braun 	    (clcm->version < SMC_V1) ||
733a046d57dSUrsula Braun 	    ((clcm->type != SMC_CLC_DECLINE) &&
734a046d57dSUrsula Braun 	     (clcm->type != expected_type))) {
735a046d57dSUrsula Braun 		smc->sk.sk_err = EPROTO;
736a046d57dSUrsula Braun 		reason_code = -EPROTO;
737a046d57dSUrsula Braun 		goto out;
738a046d57dSUrsula Braun 	}
739a046d57dSUrsula Braun 
740a046d57dSUrsula Braun 	/* receive the complete CLC message */
741a046d57dSUrsula Braun 	memset(&msg, 0, sizeof(struct msghdr));
742fb4f7926SUrsula Braun 	if (datlen > buflen) {
743fb4f7926SUrsula Braun 		check_trl = false;
744fb4f7926SUrsula Braun 		recvlen = buflen;
745fb4f7926SUrsula Braun 	} else {
746fb4f7926SUrsula Braun 		recvlen = datlen;
747fb4f7926SUrsula Braun 	}
748de4eda9dSAl Viro 	iov_iter_kvec(&msg.msg_iter, ITER_DEST, &vec, 1, recvlen);
749a046d57dSUrsula Braun 	krflags = MSG_WAITALL;
750d63d271cSAl Viro 	len = sock_recvmsg(smc->clcsock, &msg, krflags);
751fb4f7926SUrsula Braun 	if (len < recvlen || !smc_clc_msg_hdr_valid(clcm, check_trl)) {
752a046d57dSUrsula Braun 		smc->sk.sk_err = EPROTO;
753a046d57dSUrsula Braun 		reason_code = -EPROTO;
754a046d57dSUrsula Braun 		goto out;
755a046d57dSUrsula Braun 	}
756fb4f7926SUrsula Braun 	datlen -= len;
757fb4f7926SUrsula Braun 	while (datlen) {
758fb4f7926SUrsula Braun 		u8 tmp[SMC_CLC_RECV_BUF_LEN];
759fb4f7926SUrsula Braun 
760fb4f7926SUrsula Braun 		vec.iov_base = &tmp;
761fb4f7926SUrsula Braun 		vec.iov_len = SMC_CLC_RECV_BUF_LEN;
762fb4f7926SUrsula Braun 		/* receive remaining proposal message */
763fb4f7926SUrsula Braun 		recvlen = datlen > SMC_CLC_RECV_BUF_LEN ?
764fb4f7926SUrsula Braun 						SMC_CLC_RECV_BUF_LEN : datlen;
765de4eda9dSAl Viro 		iov_iter_kvec(&msg.msg_iter, ITER_DEST, &vec, 1, recvlen);
766fb4f7926SUrsula Braun 		len = sock_recvmsg(smc->clcsock, &msg, krflags);
767fb4f7926SUrsula Braun 		datlen -= len;
768fb4f7926SUrsula Braun 	}
7690cfdd8f9SUrsula Braun 	if (clcm->type == SMC_CLC_DECLINE) {
770603cc149SKarsten Graul 		struct smc_clc_msg_decline *dclc;
771603cc149SKarsten Graul 
772603cc149SKarsten Graul 		dclc = (struct smc_clc_msg_decline *)clcm;
773603cc149SKarsten Graul 		reason_code = SMC_CLC_DECL_PEERDECL;
774603cc149SKarsten Graul 		smc->peer_diagnosis = ntohl(dclc->peer_diagnosis);
775f1eb02f9SUrsula Braun 		if (((struct smc_clc_msg_decline *)buf)->hdr.typev2 &
776f1eb02f9SUrsula Braun 						SMC_FIRST_CONTACT_MASK) {
777517c300eSKarsten Graul 			smc->conn.lgr->sync_err = 1;
7785f78fe96SKarsten Graul 			smc_lgr_terminate_sched(smc->conn.lgr);
779bfbedfd3SUrsula Braun 		}
7800cfdd8f9SUrsula Braun 	}
7810cfdd8f9SUrsula Braun 
782a046d57dSUrsula Braun out:
7832b59f58eSUrsula Braun 	clc_sk->sk_rcvtimeo = rcvtimeo;
784a046d57dSUrsula Braun 	return reason_code;
785a046d57dSUrsula Braun }
786a046d57dSUrsula Braun 
787a046d57dSUrsula Braun /* send CLC DECLINE message across internal TCP socket */
smc_clc_send_decline(struct smc_sock * smc,u32 peer_diag_info,u8 version)788e8d726c8SUrsula Braun int smc_clc_send_decline(struct smc_sock *smc, u32 peer_diag_info, u8 version)
789a046d57dSUrsula Braun {
7908ade200cSKarsten Graul 	struct smc_clc_msg_decline *dclc_v1;
7918ade200cSKarsten Graul 	struct smc_clc_msg_decline_v2 dclc;
792a046d57dSUrsula Braun 	struct msghdr msg;
7938ade200cSKarsten Graul 	int len, send_len;
794a046d57dSUrsula Braun 	struct kvec vec;
795a046d57dSUrsula Braun 
7968ade200cSKarsten Graul 	dclc_v1 = (struct smc_clc_msg_decline *)&dclc;
797a046d57dSUrsula Braun 	memset(&dclc, 0, sizeof(dclc));
798a046d57dSUrsula Braun 	memcpy(dclc.hdr.eyecatcher, SMC_EYECATCHER, sizeof(SMC_EYECATCHER));
799a046d57dSUrsula Braun 	dclc.hdr.type = SMC_CLC_DECLINE;
800e8d726c8SUrsula Braun 	dclc.hdr.version = version;
801e8d726c8SUrsula Braun 	dclc.os_type = version == SMC_V1 ? 0 : SMC_CLC_OS_LINUX;
802f1eb02f9SUrsula Braun 	dclc.hdr.typev2 = (peer_diag_info == SMC_CLC_DECL_SYNCERR) ?
803f1eb02f9SUrsula Braun 						SMC_FIRST_CONTACT_MASK : 0;
804ea89c6c0SWen Gu 	if ((!smc_conn_lgr_valid(&smc->conn) || !smc->conn.lgr->is_smcd) &&
805a082ec89SHans Wippel 	    smc_ib_is_valid_local_systemid())
806369537c9SUrsula Braun 		memcpy(dclc.id_for_peer, local_systemid,
807369537c9SUrsula Braun 		       sizeof(local_systemid));
808a046d57dSUrsula Braun 	dclc.peer_diagnosis = htonl(peer_diag_info);
8098ade200cSKarsten Graul 	if (version == SMC_V1) {
8108ade200cSKarsten Graul 		memcpy(dclc_v1->trl.eyecatcher, SMC_EYECATCHER,
8118ade200cSKarsten Graul 		       sizeof(SMC_EYECATCHER));
8128ade200cSKarsten Graul 		send_len = sizeof(*dclc_v1);
8138ade200cSKarsten Graul 	} else {
8148ade200cSKarsten Graul 		memcpy(dclc.trl.eyecatcher, SMC_EYECATCHER,
8158ade200cSKarsten Graul 		       sizeof(SMC_EYECATCHER));
8168ade200cSKarsten Graul 		send_len = sizeof(dclc);
8178ade200cSKarsten Graul 	}
8188ade200cSKarsten Graul 	dclc.hdr.length = htons(send_len);
819a046d57dSUrsula Braun 
820a046d57dSUrsula Braun 	memset(&msg, 0, sizeof(msg));
821a046d57dSUrsula Braun 	vec.iov_base = &dclc;
8228ade200cSKarsten Graul 	vec.iov_len = send_len;
8238ade200cSKarsten Graul 	len = kernel_sendmsg(smc->clcsock, &msg, &vec, 1, send_len);
8248ade200cSKarsten Graul 	if (len < 0 || len < send_len)
8256ae36bffSUrsula Braun 		len = -EPROTO;
8266ae36bffSUrsula Braun 	return len > 0 ? 0 : len;
827a046d57dSUrsula Braun }
828a046d57dSUrsula Braun 
829a046d57dSUrsula Braun /* send CLC PROPOSAL message across internal TCP socket */
smc_clc_send_proposal(struct smc_sock * smc,struct smc_init_info * ini)830d70bf4f7SUrsula Braun int smc_clc_send_proposal(struct smc_sock *smc, struct smc_init_info *ini)
831a046d57dSUrsula Braun {
8328c3dca34SUrsula Braun 	struct smc_clc_smcd_v2_extension *smcd_v2_ext;
8336bb14e48SUrsula Braun 	struct smc_clc_msg_proposal_prefix *pclc_prfx;
8346bb14e48SUrsula Braun 	struct smc_clc_msg_proposal *pclc_base;
8358c3dca34SUrsula Braun 	struct smc_clc_smcd_gid_chid *gidchids;
8366bb14e48SUrsula Braun 	struct smc_clc_msg_proposal_area *pclc;
8376bb14e48SUrsula Braun 	struct smc_clc_ipv6_prefix *ipv6_prfx;
838f8e80fc4SGuangguan Wang 	struct net *net = sock_net(&smc->sk);
8398c3dca34SUrsula Braun 	struct smc_clc_v2_extension *v2_ext;
8406bb14e48SUrsula Braun 	struct smc_clc_msg_smcd *pclc_smcd;
8416bb14e48SUrsula Braun 	struct smc_clc_msg_trail *trl;
8429de4df7bSStefan Raspl 	struct smcd_dev *smcd;
8431a26d020SKarsten Graul 	int len, i, plen, rc;
844a046d57dSUrsula Braun 	int reason_code = 0;
8458c3dca34SUrsula Braun 	struct kvec vec[8];
846a046d57dSUrsula Braun 	struct msghdr msg;
847a046d57dSUrsula Braun 
8486bb14e48SUrsula Braun 	pclc = kzalloc(sizeof(*pclc), GFP_KERNEL);
8496bb14e48SUrsula Braun 	if (!pclc)
8506bb14e48SUrsula Braun 		return -ENOMEM;
8516bb14e48SUrsula Braun 
8526bb14e48SUrsula Braun 	pclc_base = &pclc->pclc_base;
8536bb14e48SUrsula Braun 	pclc_smcd = &pclc->pclc_smcd;
8546bb14e48SUrsula Braun 	pclc_prfx = &pclc->pclc_prfx;
8556bb14e48SUrsula Braun 	ipv6_prfx = pclc->pclc_prfx_ipv6;
856*9748dbc9SGustavo A. R. Silva 	v2_ext = container_of(&pclc->pclc_v2_ext,
857*9748dbc9SGustavo A. R. Silva 			      struct smc_clc_v2_extension, fixed);
858*9748dbc9SGustavo A. R. Silva 	smcd_v2_ext = container_of(&pclc->pclc_smcd_v2_ext,
859*9748dbc9SGustavo A. R. Silva 				   struct smc_clc_smcd_v2_extension, fixed);
8608c3dca34SUrsula Braun 	gidchids = pclc->pclc_gidchids;
8616bb14e48SUrsula Braun 	trl = &pclc->pclc_trl;
8626bb14e48SUrsula Braun 
8638c3dca34SUrsula Braun 	pclc_base->hdr.version = SMC_V2;
8648c3dca34SUrsula Braun 	pclc_base->hdr.typev1 = ini->smc_type_v1;
8658c3dca34SUrsula Braun 	pclc_base->hdr.typev2 = ini->smc_type_v2;
8668c3dca34SUrsula Braun 	plen = sizeof(*pclc_base) + sizeof(*pclc_smcd) + sizeof(*trl);
8678c3dca34SUrsula Braun 
868c246d942SKarsten Graul 	/* retrieve ip prefixes for CLC proposal msg */
8698c3dca34SUrsula Braun 	if (ini->smc_type_v1 != SMC_TYPE_N) {
8706bb14e48SUrsula Braun 		rc = smc_clc_prfx_set(smc->clcsock, pclc_prfx, ipv6_prfx);
8716bb14e48SUrsula Braun 		if (rc) {
8728c3dca34SUrsula Braun 			if (ini->smc_type_v2 == SMC_TYPE_N) {
8736bb14e48SUrsula Braun 				kfree(pclc);
8748c3dca34SUrsula Braun 				return SMC_CLC_DECL_CNFERR;
8758c3dca34SUrsula Braun 			}
8768c3dca34SUrsula Braun 			pclc_base->hdr.typev1 = SMC_TYPE_N;
8778c3dca34SUrsula Braun 		} else {
8788c3dca34SUrsula Braun 			pclc_base->iparea_offset = htons(sizeof(*pclc_smcd));
8798c3dca34SUrsula Braun 			plen += sizeof(*pclc_prfx) +
8808c3dca34SUrsula Braun 					pclc_prfx->ipv6_prefixes_cnt *
8818c3dca34SUrsula Braun 					sizeof(ipv6_prfx[0]);
8828c3dca34SUrsula Braun 		}
8836bb14e48SUrsula Braun 	}
884c246d942SKarsten Graul 
8858c3dca34SUrsula Braun 	/* build SMC Proposal CLC message */
8866bb14e48SUrsula Braun 	memcpy(pclc_base->hdr.eyecatcher, SMC_EYECATCHER,
8876bb14e48SUrsula Braun 	       sizeof(SMC_EYECATCHER));
8886bb14e48SUrsula Braun 	pclc_base->hdr.type = SMC_CLC_PROPOSAL;
889d70bf4f7SUrsula Braun 	if (smcr_indicated(ini->smc_type_v1)) {
890c758dfddSHans Wippel 		/* add SMC-R specifics */
8916bb14e48SUrsula Braun 		memcpy(pclc_base->lcl.id_for_peer, local_systemid,
892c758dfddSHans Wippel 		       sizeof(local_systemid));
8936bb14e48SUrsula Braun 		memcpy(pclc_base->lcl.gid, ini->ib_gid, SMC_GID_SIZE);
8946bb14e48SUrsula Braun 		memcpy(pclc_base->lcl.mac, &ini->ib_dev->mac[ini->ib_port - 1],
895bc36d2fcSKarsten Graul 		       ETH_ALEN);
896c758dfddSHans Wippel 	}
897d70bf4f7SUrsula Braun 	if (smcd_indicated(ini->smc_type_v1)) {
898b40584d1SWen Gu 		struct smcd_gid smcd_gid;
899b40584d1SWen Gu 
900c758dfddSHans Wippel 		/* add SMC-D specifics */
9018c3dca34SUrsula Braun 		if (ini->ism_dev[0]) {
9029de4df7bSStefan Raspl 			smcd = ini->ism_dev[0];
903b40584d1SWen Gu 			smcd->ops->get_local_gid(smcd, &smcd_gid);
904b40584d1SWen Gu 			pclc_smcd->ism.gid = htonll(smcd_gid.gid);
9058c3dca34SUrsula Braun 			pclc_smcd->ism.chid =
9068c3dca34SUrsula Braun 				htons(smc_ism_get_chid(ini->ism_dev[0]));
9078c3dca34SUrsula Braun 		}
9088c3dca34SUrsula Braun 	}
9098c3dca34SUrsula Braun 	if (ini->smc_type_v2 == SMC_TYPE_N) {
9108c3dca34SUrsula Braun 		pclc_smcd->v2_ext_offset = 0;
9118c3dca34SUrsula Braun 	} else {
912fa086662SKarsten Graul 		struct smc_clc_eid_entry *ueident;
9138c3dca34SUrsula Braun 		u16 v2_ext_offset;
9148c3dca34SUrsula Braun 
915e5c4744cSKarsten Graul 		v2_ext->hdr.flag.release = SMC_RELEASE;
9168c3dca34SUrsula Braun 		v2_ext_offset = sizeof(*pclc_smcd) -
9178c3dca34SUrsula Braun 			offsetofend(struct smc_clc_msg_smcd, v2_ext_offset);
9188c3dca34SUrsula Braun 		if (ini->smc_type_v1 != SMC_TYPE_N)
9198c3dca34SUrsula Braun 			v2_ext_offset += sizeof(*pclc_prfx) +
9208c3dca34SUrsula Braun 						pclc_prfx->ipv6_prefixes_cnt *
9218c3dca34SUrsula Braun 						sizeof(ipv6_prfx[0]);
9228c3dca34SUrsula Braun 		pclc_smcd->v2_ext_offset = htons(v2_ext_offset);
923e5c4744cSKarsten Graul 		plen += sizeof(*v2_ext);
924fa086662SKarsten Graul 
925ece60db3SWen Gu 		v2_ext->feature_mask = htons(SMC_FEATURE_MASK);
926fa086662SKarsten Graul 		read_lock(&smc_clc_eid_table.lock);
927fa086662SKarsten Graul 		v2_ext->hdr.eid_cnt = smc_clc_eid_table.ueid_cnt;
928fa086662SKarsten Graul 		plen += smc_clc_eid_table.ueid_cnt * SMC_MAX_EID_LEN;
929fa086662SKarsten Graul 		i = 0;
930fa086662SKarsten Graul 		list_for_each_entry(ueident, &smc_clc_eid_table.list, list) {
931fa086662SKarsten Graul 			memcpy(v2_ext->user_eids[i++], ueident->eid,
932fa086662SKarsten Graul 			       sizeof(ueident->eid));
933fa086662SKarsten Graul 		}
934fa086662SKarsten Graul 		read_unlock(&smc_clc_eid_table.lock);
935e5c4744cSKarsten Graul 	}
936e5c4744cSKarsten Graul 	if (smcd_indicated(ini->smc_type_v2)) {
937b40584d1SWen Gu 		struct smcd_gid smcd_gid;
938e5c4744cSKarsten Graul 		u8 *eid = NULL;
939b40584d1SWen Gu 		int entry = 0;
940e5c4744cSKarsten Graul 
941e5c4744cSKarsten Graul 		v2_ext->hdr.flag.seid = smc_clc_eid_table.seid_enabled;
9428c3dca34SUrsula Braun 		v2_ext->hdr.smcd_v2_ext_offset = htons(sizeof(*v2_ext) -
9438c3dca34SUrsula Braun 				offsetofend(struct smc_clnt_opts_area_hdr,
9448c3dca34SUrsula Braun 					    smcd_v2_ext_offset) +
9458c3dca34SUrsula Braun 				v2_ext->hdr.eid_cnt * SMC_MAX_EID_LEN);
94611a26c59SKarsten Graul 		smc_ism_get_system_eid(&eid);
947fa086662SKarsten Graul 		if (eid && v2_ext->hdr.flag.seid)
9488c3dca34SUrsula Braun 			memcpy(smcd_v2_ext->system_eid, eid, SMC_MAX_EID_LEN);
949e5c4744cSKarsten Graul 		plen += sizeof(*smcd_v2_ext);
9508c3dca34SUrsula Braun 		if (ini->ism_offered_cnt) {
9518c3dca34SUrsula Braun 			for (i = 1; i <= ini->ism_offered_cnt; i++) {
9529de4df7bSStefan Raspl 				smcd = ini->ism_dev[i];
953b40584d1SWen Gu 				smcd->ops->get_local_gid(smcd, &smcd_gid);
954b40584d1SWen Gu 				gidchids[entry].chid =
9558c3dca34SUrsula Braun 					htons(smc_ism_get_chid(ini->ism_dev[i]));
956b40584d1SWen Gu 				gidchids[entry].gid = htonll(smcd_gid.gid);
957b27696cdSWen Gu 				if (smc_ism_is_emulated(smcd)) {
958b27696cdSWen Gu 					/* an Emulated-ISM device takes two
959b40584d1SWen Gu 					 * entries. CHID of the second entry
960b40584d1SWen Gu 					 * repeats that of the first entry.
961b40584d1SWen Gu 					 */
962b40584d1SWen Gu 					gidchids[entry + 1].chid =
963b40584d1SWen Gu 						gidchids[entry].chid;
964b40584d1SWen Gu 					gidchids[entry + 1].gid =
965b40584d1SWen Gu 						htonll(smcd_gid.gid_ext);
966b40584d1SWen Gu 					entry++;
9678c3dca34SUrsula Braun 				}
968b40584d1SWen Gu 				entry++;
9698c3dca34SUrsula Braun 			}
970b40584d1SWen Gu 			plen += entry * sizeof(struct smc_clc_smcd_gid_chid);
971b40584d1SWen Gu 		}
972b40584d1SWen Gu 		v2_ext->hdr.ism_gid_cnt = entry;
973c758dfddSHans Wippel 	}
9747f0620b9SGuangguan Wang 	if (smcr_indicated(ini->smc_type_v2)) {
975e5c4744cSKarsten Graul 		memcpy(v2_ext->roce, ini->smcrv2.ib_gid_v2, SMC_GID_SIZE);
9761f2c9dd7SGuangguan Wang 		v2_ext->max_conns = net->smc.sysctl_max_conns_per_lgr;
977f8e80fc4SGuangguan Wang 		v2_ext->max_links = net->smc.sysctl_max_links_per_lgr;
9787f0620b9SGuangguan Wang 	}
979e5c4744cSKarsten Graul 
9806bb14e48SUrsula Braun 	pclc_base->hdr.length = htons(plen);
9816bb14e48SUrsula Braun 	memcpy(trl->eyecatcher, SMC_EYECATCHER, sizeof(SMC_EYECATCHER));
9828c3dca34SUrsula Braun 
9838c3dca34SUrsula Braun 	/* send SMC Proposal CLC message */
984a046d57dSUrsula Braun 	memset(&msg, 0, sizeof(msg));
9851a26d020SKarsten Graul 	i = 0;
9866bb14e48SUrsula Braun 	vec[i].iov_base = pclc_base;
9876bb14e48SUrsula Braun 	vec[i++].iov_len = sizeof(*pclc_base);
9886bb14e48SUrsula Braun 	vec[i].iov_base = pclc_smcd;
9896bb14e48SUrsula Braun 	vec[i++].iov_len = sizeof(*pclc_smcd);
9908c3dca34SUrsula Braun 	if (ini->smc_type_v1 != SMC_TYPE_N) {
9916bb14e48SUrsula Braun 		vec[i].iov_base = pclc_prfx;
9926bb14e48SUrsula Braun 		vec[i++].iov_len = sizeof(*pclc_prfx);
9936bb14e48SUrsula Braun 		if (pclc_prfx->ipv6_prefixes_cnt > 0) {
9946bb14e48SUrsula Braun 			vec[i].iov_base = ipv6_prfx;
9956bb14e48SUrsula Braun 			vec[i++].iov_len = pclc_prfx->ipv6_prefixes_cnt *
9961a26d020SKarsten Graul 					   sizeof(ipv6_prfx[0]);
9971a26d020SKarsten Graul 		}
9988c3dca34SUrsula Braun 	}
9998c3dca34SUrsula Braun 	if (ini->smc_type_v2 != SMC_TYPE_N) {
10008c3dca34SUrsula Braun 		vec[i].iov_base = v2_ext;
1001fa086662SKarsten Graul 		vec[i++].iov_len = sizeof(*v2_ext) +
1002fa086662SKarsten Graul 				   (v2_ext->hdr.eid_cnt * SMC_MAX_EID_LEN);
1003e5c4744cSKarsten Graul 		if (smcd_indicated(ini->smc_type_v2)) {
10048c3dca34SUrsula Braun 			vec[i].iov_base = smcd_v2_ext;
10058c3dca34SUrsula Braun 			vec[i++].iov_len = sizeof(*smcd_v2_ext);
10068c3dca34SUrsula Braun 			if (ini->ism_offered_cnt) {
10078c3dca34SUrsula Braun 				vec[i].iov_base = gidchids;
1008b40584d1SWen Gu 				vec[i++].iov_len = v2_ext->hdr.ism_gid_cnt *
10098c3dca34SUrsula Braun 					sizeof(struct smc_clc_smcd_gid_chid);
10108c3dca34SUrsula Braun 			}
10118c3dca34SUrsula Braun 		}
1012e5c4744cSKarsten Graul 	}
10136bb14e48SUrsula Braun 	vec[i].iov_base = trl;
10146bb14e48SUrsula Braun 	vec[i++].iov_len = sizeof(*trl);
1015a046d57dSUrsula Braun 	/* due to the few bytes needed for clc-handshake this cannot block */
10161a26d020SKarsten Graul 	len = kernel_sendmsg(smc->clcsock, &msg, vec, i, plen);
101738189779SYueHaibing 	if (len < 0) {
1018a046d57dSUrsula Braun 		smc->sk.sk_err = smc->clcsock->sk->sk_err;
1019a046d57dSUrsula Braun 		reason_code = -smc->sk.sk_err;
10206bb14e48SUrsula Braun 	} else if (len < ntohs(pclc_base->hdr.length)) {
102138189779SYueHaibing 		reason_code = -ENETUNREACH;
102238189779SYueHaibing 		smc->sk.sk_err = -reason_code;
1023a046d57dSUrsula Braun 	}
1024a046d57dSUrsula Braun 
10256bb14e48SUrsula Braun 	kfree(pclc);
1026a046d57dSUrsula Braun 	return reason_code;
1027a046d57dSUrsula Braun }
1028a046d57dSUrsula Braun 
10295205ac44SWen Gu static void
smcd_clc_prep_confirm_accept(struct smc_connection * conn,struct smc_clc_msg_accept_confirm * clc,int first_contact,u8 version,u8 * eid,struct smc_init_info * ini,int * fce_len,struct smc_clc_first_contact_ext_v2x * fce_v2x,struct smc_clc_msg_trail * trl)10305205ac44SWen Gu smcd_clc_prep_confirm_accept(struct smc_connection *conn,
10319505450dSWen Gu 			     struct smc_clc_msg_accept_confirm *clc,
1032fa086662SKarsten Graul 			     int first_contact, u8 version,
10335205ac44SWen Gu 			     u8 *eid, struct smc_init_info *ini,
10345205ac44SWen Gu 			     int *fce_len,
10355205ac44SWen Gu 			     struct smc_clc_first_contact_ext_v2x *fce_v2x,
10365205ac44SWen Gu 			     struct smc_clc_msg_trail *trl)
10373d9725a6SUrsula Braun {
1038c5a10397SWen Gu 	struct smcd_dev *smcd = conn->lgr->smcd;
1039b40584d1SWen Gu 	struct smcd_gid smcd_gid;
1040b40584d1SWen Gu 	u16 chid;
10415205ac44SWen Gu 	int len;
10423d9725a6SUrsula Braun 
10433d9725a6SUrsula Braun 	/* SMC-D specific settings */
10443d9725a6SUrsula Braun 	memcpy(clc->hdr.eyecatcher, SMCD_EYECATCHER,
10453d9725a6SUrsula Braun 	       sizeof(SMCD_EYECATCHER));
1046b40584d1SWen Gu 	smcd->ops->get_local_gid(smcd, &smcd_gid);
1047f1eb02f9SUrsula Braun 	clc->hdr.typev1 = SMC_TYPE_D;
1048b40584d1SWen Gu 	clc->d0.gid = htonll(smcd_gid.gid);
1049c5a10397SWen Gu 	clc->d0.token = htonll(conn->rmb_desc->token);
1050833bac7eSGerd Bayer 	clc->d0.dmbe_size = conn->rmbe_size_comp;
10513d9725a6SUrsula Braun 	clc->d0.dmbe_idx = 0;
10523d9725a6SUrsula Braun 	memcpy(&clc->d0.linkid, conn->lgr->id, SMC_LGR_ID_SIZE);
1053a7c9c5f4SUrsula Braun 	if (version == SMC_V1) {
1054a7c9c5f4SUrsula Braun 		clc->hdr.length = htons(SMCD_CLC_ACCEPT_CONFIRM_LEN);
1055a7c9c5f4SUrsula Braun 	} else {
1056b40584d1SWen Gu 		chid = smc_ism_get_chid(smcd);
1057b40584d1SWen Gu 		clc->d1.chid = htons(chid);
1058e5c4744cSKarsten Graul 		if (eid && eid[0])
10599505450dSWen Gu 			memcpy(clc->d1.eid, eid, SMC_MAX_EID_LEN);
1060b27696cdSWen Gu 		if (__smc_ism_is_emulated(chid))
1061b40584d1SWen Gu 			clc->d1.gid_ext = htonll(smcd_gid.gid_ext);
1062b81a5eb7SUrsula Braun 		len = SMCD_CLC_ACCEPT_CONFIRM_LEN_V2;
10637290178aSGuangguan Wang 		if (first_contact) {
10645205ac44SWen Gu 			*fce_len = smc_clc_fill_fce_v2x(fce_v2x, ini);
10655205ac44SWen Gu 			len += *fce_len;
10667290178aSGuangguan Wang 		}
10679505450dSWen Gu 		clc->hdr.length = htons(len);
1068a7c9c5f4SUrsula Braun 	}
10695205ac44SWen Gu 	memcpy(trl->eyecatcher, SMCD_EYECATCHER,
10703d9725a6SUrsula Braun 	       sizeof(SMCD_EYECATCHER));
10715205ac44SWen Gu }
10725205ac44SWen Gu 
10735205ac44SWen Gu static void
smcr_clc_prep_confirm_accept(struct smc_connection * conn,struct smc_clc_msg_accept_confirm * clc,int first_contact,u8 version,u8 * eid,struct smc_init_info * ini,int * fce_len,struct smc_clc_first_contact_ext_v2x * fce_v2x,struct smc_clc_fce_gid_ext * gle,struct smc_clc_msg_trail * trl)10745205ac44SWen Gu smcr_clc_prep_confirm_accept(struct smc_connection *conn,
10759505450dSWen Gu 			     struct smc_clc_msg_accept_confirm *clc,
10765205ac44SWen Gu 			     int first_contact, u8 version,
10775205ac44SWen Gu 			     u8 *eid, struct smc_init_info *ini,
10785205ac44SWen Gu 			     int *fce_len,
10795205ac44SWen Gu 			     struct smc_clc_first_contact_ext_v2x *fce_v2x,
10805205ac44SWen Gu 			     struct smc_clc_fce_gid_ext *gle,
10815205ac44SWen Gu 			     struct smc_clc_msg_trail *trl)
10825205ac44SWen Gu {
10833d9725a6SUrsula Braun 	struct smc_link *link = conn->lnk;
10845205ac44SWen Gu 	int len;
10853d9725a6SUrsula Braun 
10863d9725a6SUrsula Braun 	/* SMC-R specific settings */
10873d9725a6SUrsula Braun 	memcpy(clc->hdr.eyecatcher, SMC_EYECATCHER,
10883d9725a6SUrsula Braun 	       sizeof(SMC_EYECATCHER));
1089f1eb02f9SUrsula Braun 	clc->hdr.typev1 = SMC_TYPE_R;
10903d9725a6SUrsula Braun 	memcpy(clc->r0.lcl.id_for_peer, local_systemid,
10913d9725a6SUrsula Braun 	       sizeof(local_systemid));
10923d9725a6SUrsula Braun 	memcpy(&clc->r0.lcl.gid, link->gid, SMC_GID_SIZE);
10933d9725a6SUrsula Braun 	memcpy(&clc->r0.lcl.mac, &link->smcibdev->mac[link->ibport - 1],
10943d9725a6SUrsula Braun 	       ETH_ALEN);
10953d9725a6SUrsula Braun 	hton24(clc->r0.qpn, link->roce_qp->qp_num);
10963d9725a6SUrsula Braun 	clc->r0.rmb_rkey =
1097b8d19945SWen Gu 		htonl(conn->rmb_desc->mr[link->link_idx]->rkey);
10983d9725a6SUrsula Braun 	clc->r0.rmbe_idx = 1; /* for now: 1 RMB = 1 RMBE */
10993d9725a6SUrsula Braun 	clc->r0.rmbe_alert_token = htonl(conn->alert_token_local);
11003d9725a6SUrsula Braun 	switch (clc->hdr.type) {
11013d9725a6SUrsula Braun 	case SMC_CLC_ACCEPT:
11023d9725a6SUrsula Braun 		clc->r0.qp_mtu = link->path_mtu;
11033d9725a6SUrsula Braun 		break;
11043d9725a6SUrsula Braun 	case SMC_CLC_CONFIRM:
11053d9725a6SUrsula Braun 		clc->r0.qp_mtu = min(link->path_mtu, link->peer_mtu);
11063d9725a6SUrsula Braun 		break;
11073d9725a6SUrsula Braun 	}
1108833bac7eSGerd Bayer 	clc->r0.rmbe_size = conn->rmbe_size_comp;
1109b8d19945SWen Gu 	clc->r0.rmb_dma_addr = conn->rmb_desc->is_vm ?
1110b8d19945SWen Gu 		cpu_to_be64((uintptr_t)conn->rmb_desc->cpu_addr) :
1111b8d19945SWen Gu 		cpu_to_be64((u64)sg_dma_address
11123d9725a6SUrsula Braun 			    (conn->rmb_desc->sgt[link->link_idx].sgl));
11133d9725a6SUrsula Braun 	hton24(clc->r0.psn, link->psn_initial);
1114e5c4744cSKarsten Graul 	if (version == SMC_V1) {
1115e5c4744cSKarsten Graul 		clc->hdr.length = htons(SMCR_CLC_ACCEPT_CONFIRM_LEN);
1116e5c4744cSKarsten Graul 	} else {
1117e5c4744cSKarsten Graul 		if (eid && eid[0])
11189505450dSWen Gu 			memcpy(clc->r1.eid, eid, SMC_MAX_EID_LEN);
1119e5c4744cSKarsten Graul 		len = SMCR_CLC_ACCEPT_CONFIRM_LEN_V2;
1120e5c4744cSKarsten Graul 		if (first_contact) {
11215205ac44SWen Gu 			*fce_len = smc_clc_fill_fce_v2x(fce_v2x, ini);
11225205ac44SWen Gu 			len += *fce_len;
11235205ac44SWen Gu 			fce_v2x->fce_v2_base.v2_direct =
1124ac053a16SWen Gu 				!link->lgr->uses_gateway;
11251e700948SGuangguan Wang 			if (clc->hdr.type == SMC_CLC_CONFIRM) {
11265205ac44SWen Gu 				memset(gle, 0, sizeof(*gle));
11275205ac44SWen Gu 				gle->gid_cnt = ini->smcrv2.gidlist.len;
11285205ac44SWen Gu 				len += sizeof(*gle);
11295205ac44SWen Gu 				len += gle->gid_cnt * sizeof(gle->gid[0]);
1130e5c4744cSKarsten Graul 			}
1131e5c4744cSKarsten Graul 		}
11329505450dSWen Gu 		clc->hdr.length = htons(len);
1133e5c4744cSKarsten Graul 	}
11345205ac44SWen Gu 	memcpy(trl->eyecatcher, SMC_EYECATCHER, sizeof(SMC_EYECATCHER));
11353d9725a6SUrsula Braun }
11363d9725a6SUrsula Braun 
11375205ac44SWen Gu /* build and send CLC CONFIRM / ACCEPT message */
smc_clc_send_confirm_accept(struct smc_sock * smc,struct smc_clc_msg_accept_confirm * clc,int first_contact,u8 version,u8 * eid,struct smc_init_info * ini)11385205ac44SWen Gu static int smc_clc_send_confirm_accept(struct smc_sock *smc,
11399505450dSWen Gu 				       struct smc_clc_msg_accept_confirm *clc,
11405205ac44SWen Gu 				       int first_contact, u8 version,
11415205ac44SWen Gu 				       u8 *eid, struct smc_init_info *ini)
11425205ac44SWen Gu {
11435205ac44SWen Gu 	struct smc_clc_first_contact_ext_v2x fce_v2x;
11445205ac44SWen Gu 	struct smc_connection *conn = &smc->conn;
11455205ac44SWen Gu 	struct smc_clc_fce_gid_ext gle;
11465205ac44SWen Gu 	struct smc_clc_msg_trail trl;
11475205ac44SWen Gu 	int i, fce_len;
11485205ac44SWen Gu 	struct kvec vec[5];
11495205ac44SWen Gu 	struct msghdr msg;
11505205ac44SWen Gu 
11515205ac44SWen Gu 	/* send SMC Confirm CLC msg */
11525205ac44SWen Gu 	clc->hdr.version = version;	/* SMC version */
11535205ac44SWen Gu 	if (first_contact)
11545205ac44SWen Gu 		clc->hdr.typev2 |= SMC_FIRST_CONTACT_MASK;
11555205ac44SWen Gu 	if (conn->lgr->is_smcd)
11569505450dSWen Gu 		smcd_clc_prep_confirm_accept(conn, clc, first_contact,
11575205ac44SWen Gu 					     version, eid, ini, &fce_len,
11585205ac44SWen Gu 					     &fce_v2x, &trl);
11595205ac44SWen Gu 	else
11609505450dSWen Gu 		smcr_clc_prep_confirm_accept(conn, clc, first_contact,
11615205ac44SWen Gu 					     version, eid, ini, &fce_len,
11625205ac44SWen Gu 					     &fce_v2x, &gle, &trl);
11633d9725a6SUrsula Braun 	memset(&msg, 0, sizeof(msg));
1164e15c6c46SUrsula Braun 	i = 0;
11659505450dSWen Gu 	vec[i].iov_base = clc;
1166a7c9c5f4SUrsula Braun 	if (version > SMC_V1)
1167e5c4744cSKarsten Graul 		vec[i++].iov_len = (clc->hdr.typev1 == SMC_TYPE_D ?
1168e5c4744cSKarsten Graul 					SMCD_CLC_ACCEPT_CONFIRM_LEN_V2 :
1169e5c4744cSKarsten Graul 					SMCR_CLC_ACCEPT_CONFIRM_LEN_V2) -
1170e5c4744cSKarsten Graul 				   sizeof(trl);
1171a7c9c5f4SUrsula Braun 	else
1172e15c6c46SUrsula Braun 		vec[i++].iov_len = (clc->hdr.typev1 == SMC_TYPE_D ?
1173e15c6c46SUrsula Braun 						SMCD_CLC_ACCEPT_CONFIRM_LEN :
1174e15c6c46SUrsula Braun 						SMCR_CLC_ACCEPT_CONFIRM_LEN) -
1175e15c6c46SUrsula Braun 				   sizeof(trl);
1176b81a5eb7SUrsula Braun 	if (version > SMC_V1 && first_contact) {
1177ac053a16SWen Gu 		vec[i].iov_base = &fce_v2x;
11787290178aSGuangguan Wang 		vec[i++].iov_len = fce_len;
1179e5c4744cSKarsten Graul 		if (!conn->lgr->is_smcd) {
1180e5c4744cSKarsten Graul 			if (clc->hdr.type == SMC_CLC_CONFIRM) {
1181e5c4744cSKarsten Graul 				vec[i].iov_base = &gle;
1182e5c4744cSKarsten Graul 				vec[i++].iov_len = sizeof(gle);
1183e5c4744cSKarsten Graul 				vec[i].iov_base = &ini->smcrv2.gidlist.list;
1184e5c4744cSKarsten Graul 				vec[i++].iov_len = gle.gid_cnt *
1185e5c4744cSKarsten Graul 						   sizeof(gle.gid[0]);
1186e5c4744cSKarsten Graul 			}
1187e5c4744cSKarsten Graul 		}
1188b81a5eb7SUrsula Braun 	}
1189e15c6c46SUrsula Braun 	vec[i].iov_base = &trl;
1190e15c6c46SUrsula Braun 	vec[i++].iov_len = sizeof(trl);
1191e15c6c46SUrsula Braun 	return kernel_sendmsg(smc->clcsock, &msg, vec, 1,
11923d9725a6SUrsula Braun 			      ntohs(clc->hdr.length));
11933d9725a6SUrsula Braun }
11943d9725a6SUrsula Braun 
1195a046d57dSUrsula Braun /* send CLC CONFIRM message across internal TCP socket */
smc_clc_send_confirm(struct smc_sock * smc,bool clnt_first_contact,u8 version,u8 * eid,struct smc_init_info * ini)1196a7c9c5f4SUrsula Braun int smc_clc_send_confirm(struct smc_sock *smc, bool clnt_first_contact,
1197e5c4744cSKarsten Graul 			 u8 version, u8 *eid, struct smc_init_info *ini)
1198a046d57dSUrsula Braun {
11999505450dSWen Gu 	struct smc_clc_msg_accept_confirm cclc;
1200a046d57dSUrsula Braun 	int reason_code = 0;
1201a046d57dSUrsula Braun 	int len;
1202a046d57dSUrsula Braun 
1203a046d57dSUrsula Braun 	/* send SMC Confirm CLC msg */
12049505450dSWen Gu 	memset(&cclc, 0, sizeof(cclc));
12059505450dSWen Gu 	cclc.hdr.type = SMC_CLC_CONFIRM;
12069505450dSWen Gu 	len = smc_clc_send_confirm_accept(smc, &cclc, clnt_first_contact,
1207e5c4744cSKarsten Graul 					  version, eid, ini);
12089505450dSWen Gu 	if (len < ntohs(cclc.hdr.length)) {
1209a046d57dSUrsula Braun 		if (len >= 0) {
1210a046d57dSUrsula Braun 			reason_code = -ENETUNREACH;
1211a046d57dSUrsula Braun 			smc->sk.sk_err = -reason_code;
1212a046d57dSUrsula Braun 		} else {
1213a046d57dSUrsula Braun 			smc->sk.sk_err = smc->clcsock->sk->sk_err;
1214a046d57dSUrsula Braun 			reason_code = -smc->sk.sk_err;
1215a046d57dSUrsula Braun 		}
1216a046d57dSUrsula Braun 	}
1217a046d57dSUrsula Braun 	return reason_code;
1218a046d57dSUrsula Braun }
1219a046d57dSUrsula Braun 
1220a046d57dSUrsula Braun /* send CLC ACCEPT message across internal TCP socket */
smc_clc_send_accept(struct smc_sock * new_smc,bool srv_first_contact,u8 version,u8 * negotiated_eid,struct smc_init_info * ini)1221a7c9c5f4SUrsula Braun int smc_clc_send_accept(struct smc_sock *new_smc, bool srv_first_contact,
12221e700948SGuangguan Wang 			u8 version, u8 *negotiated_eid, struct smc_init_info *ini)
1223a046d57dSUrsula Braun {
12249505450dSWen Gu 	struct smc_clc_msg_accept_confirm aclc;
1225a046d57dSUrsula Braun 	int len;
1226a046d57dSUrsula Braun 
12279505450dSWen Gu 	memset(&aclc, 0, sizeof(aclc));
12289505450dSWen Gu 	aclc.hdr.type = SMC_CLC_ACCEPT;
12299505450dSWen Gu 	len = smc_clc_send_confirm_accept(new_smc, &aclc, srv_first_contact,
12301e700948SGuangguan Wang 					  version, negotiated_eid, ini);
12319505450dSWen Gu 	if (len < ntohs(aclc.hdr.length))
12326ae36bffSUrsula Braun 		len = len >= 0 ? -EPROTO : -new_smc->clcsock->sk->sk_err;
1233a046d57dSUrsula Braun 
12346ae36bffSUrsula Braun 	return len > 0 ? 0 : len;
1235a046d57dSUrsula Braun }
1236b81a5eb7SUrsula Braun 
smc_clc_srv_v2x_features_validate(struct smc_sock * smc,struct smc_clc_msg_proposal * pclc,struct smc_init_info * ini)1237f8e80fc4SGuangguan Wang int smc_clc_srv_v2x_features_validate(struct smc_sock *smc,
1238f8e80fc4SGuangguan Wang 				      struct smc_clc_msg_proposal *pclc,
12396ac1e656SGuangguan Wang 				      struct smc_init_info *ini)
12406ac1e656SGuangguan Wang {
12416ac1e656SGuangguan Wang 	struct smc_clc_v2_extension *pclc_v2_ext;
1242f8e80fc4SGuangguan Wang 	struct net *net = sock_net(&smc->sk);
12436ac1e656SGuangguan Wang 
12447f0620b9SGuangguan Wang 	ini->max_conns = SMC_CONN_PER_LGR_MAX;
124569b888e3SGuangguan Wang 	ini->max_links = SMC_LINKS_ADD_LNK_MAX;
1246ece60db3SWen Gu 	ini->feature_mask = SMC_FEATURE_MASK;
12477f0620b9SGuangguan Wang 
12486ac1e656SGuangguan Wang 	if ((!(ini->smcd_version & SMC_V2) && !(ini->smcr_version & SMC_V2)) ||
12496ac1e656SGuangguan Wang 	    ini->release_nr < SMC_RELEASE_1)
12506ac1e656SGuangguan Wang 		return 0;
12516ac1e656SGuangguan Wang 
12526ac1e656SGuangguan Wang 	pclc_v2_ext = smc_get_clc_v2_ext(pclc);
12536ac1e656SGuangguan Wang 	if (!pclc_v2_ext)
12546ac1e656SGuangguan Wang 		return SMC_CLC_DECL_NOV2EXT;
12556ac1e656SGuangguan Wang 
12567f0620b9SGuangguan Wang 	if (ini->smcr_version & SMC_V2) {
12571f2c9dd7SGuangguan Wang 		ini->max_conns = min_t(u8, pclc_v2_ext->max_conns,
12581f2c9dd7SGuangguan Wang 				       net->smc.sysctl_max_conns_per_lgr);
12597f0620b9SGuangguan Wang 		if (ini->max_conns < SMC_CONN_PER_LGR_MIN)
12607f0620b9SGuangguan Wang 			return SMC_CLC_DECL_MAXCONNERR;
126169b888e3SGuangguan Wang 
1262f8e80fc4SGuangguan Wang 		ini->max_links = min_t(u8, pclc_v2_ext->max_links,
1263f8e80fc4SGuangguan Wang 				       net->smc.sysctl_max_links_per_lgr);
126469b888e3SGuangguan Wang 		if (ini->max_links < SMC_LINKS_ADD_LNK_MIN)
126569b888e3SGuangguan Wang 			return SMC_CLC_DECL_MAXLINKERR;
12667f0620b9SGuangguan Wang 	}
12677f0620b9SGuangguan Wang 
12686ac1e656SGuangguan Wang 	return 0;
12696ac1e656SGuangguan Wang }
12706ac1e656SGuangguan Wang 
smc_clc_clnt_v2x_features_validate(struct smc_clc_first_contact_ext * fce,struct smc_init_info * ini)12716ac1e656SGuangguan Wang int smc_clc_clnt_v2x_features_validate(struct smc_clc_first_contact_ext *fce,
12726ac1e656SGuangguan Wang 				       struct smc_init_info *ini)
12736ac1e656SGuangguan Wang {
12747f0620b9SGuangguan Wang 	struct smc_clc_first_contact_ext_v2x *fce_v2x =
12757f0620b9SGuangguan Wang 		(struct smc_clc_first_contact_ext_v2x *)fce;
12767f0620b9SGuangguan Wang 
12776ac1e656SGuangguan Wang 	if (ini->release_nr < SMC_RELEASE_1)
12786ac1e656SGuangguan Wang 		return 0;
12796ac1e656SGuangguan Wang 
12807f0620b9SGuangguan Wang 	if (!ini->is_smcd) {
12817f0620b9SGuangguan Wang 		if (fce_v2x->max_conns < SMC_CONN_PER_LGR_MIN)
12827f0620b9SGuangguan Wang 			return SMC_CLC_DECL_MAXCONNERR;
12837f0620b9SGuangguan Wang 		ini->max_conns = fce_v2x->max_conns;
128469b888e3SGuangguan Wang 
128569b888e3SGuangguan Wang 		if (fce_v2x->max_links > SMC_LINKS_ADD_LNK_MAX ||
128669b888e3SGuangguan Wang 		    fce_v2x->max_links < SMC_LINKS_ADD_LNK_MIN)
128769b888e3SGuangguan Wang 			return SMC_CLC_DECL_MAXLINKERR;
128869b888e3SGuangguan Wang 		ini->max_links = fce_v2x->max_links;
12897f0620b9SGuangguan Wang 	}
1290ece60db3SWen Gu 	/* common supplemental features of server and client */
1291ece60db3SWen Gu 	ini->feature_mask = ntohs(fce_v2x->feature_mask) & SMC_FEATURE_MASK;
12927f0620b9SGuangguan Wang 
12936ac1e656SGuangguan Wang 	return 0;
12946ac1e656SGuangguan Wang }
12956ac1e656SGuangguan Wang 
smc_clc_v2x_features_confirm_check(struct smc_clc_msg_accept_confirm * cclc,struct smc_init_info * ini)12966ac1e656SGuangguan Wang int smc_clc_v2x_features_confirm_check(struct smc_clc_msg_accept_confirm *cclc,
12976ac1e656SGuangguan Wang 				       struct smc_init_info *ini)
12986ac1e656SGuangguan Wang {
12996ac1e656SGuangguan Wang 	struct smc_clc_first_contact_ext *fce =
13009505450dSWen Gu 		smc_get_clc_first_contact_ext(cclc, ini->is_smcd);
13017f0620b9SGuangguan Wang 	struct smc_clc_first_contact_ext_v2x *fce_v2x =
13027f0620b9SGuangguan Wang 		(struct smc_clc_first_contact_ext_v2x *)fce;
13036ac1e656SGuangguan Wang 
13046ac1e656SGuangguan Wang 	if (cclc->hdr.version == SMC_V1 ||
13056ac1e656SGuangguan Wang 	    !(cclc->hdr.typev2 & SMC_FIRST_CONTACT_MASK))
13066ac1e656SGuangguan Wang 		return 0;
13076ac1e656SGuangguan Wang 
13086ac1e656SGuangguan Wang 	if (ini->release_nr != fce->release)
13096ac1e656SGuangguan Wang 		return SMC_CLC_DECL_RELEASEERR;
13106ac1e656SGuangguan Wang 
13116ac1e656SGuangguan Wang 	if (fce->release < SMC_RELEASE_1)
13126ac1e656SGuangguan Wang 		return 0;
13136ac1e656SGuangguan Wang 
13147f0620b9SGuangguan Wang 	if (!ini->is_smcd) {
13157f0620b9SGuangguan Wang 		if (fce_v2x->max_conns != ini->max_conns)
13167f0620b9SGuangguan Wang 			return SMC_CLC_DECL_MAXCONNERR;
131769b888e3SGuangguan Wang 		if (fce_v2x->max_links != ini->max_links)
131869b888e3SGuangguan Wang 			return SMC_CLC_DECL_MAXLINKERR;
13197f0620b9SGuangguan Wang 	}
1320ece60db3SWen Gu 	/* common supplemental features returned by client */
1321ece60db3SWen Gu 	ini->feature_mask = ntohs(fce_v2x->feature_mask);
13227f0620b9SGuangguan Wang 
13236ac1e656SGuangguan Wang 	return 0;
13246ac1e656SGuangguan Wang }
13256ac1e656SGuangguan Wang 
smc_clc_get_hostname(u8 ** host)1326099b990bSGuvenc Gulce void smc_clc_get_hostname(u8 **host)
1327099b990bSGuvenc Gulce {
1328099b990bSGuvenc Gulce 	*host = &smc_hostname[0];
1329099b990bSGuvenc Gulce }
1330099b990bSGuvenc Gulce 
smc_clc_init(void)1331b81a5eb7SUrsula Braun void __init smc_clc_init(void)
1332b81a5eb7SUrsula Braun {
1333b81a5eb7SUrsula Braun 	struct new_utsname *u;
1334b81a5eb7SUrsula Braun 
1335b81a5eb7SUrsula Braun 	memset(smc_hostname, _S, sizeof(smc_hostname)); /* ASCII blanks */
1336b81a5eb7SUrsula Braun 	u = utsname();
1337b81a5eb7SUrsula Braun 	memcpy(smc_hostname, u->nodename,
1338b81a5eb7SUrsula Braun 	       min_t(size_t, strlen(u->nodename), sizeof(smc_hostname)));
1339fa086662SKarsten Graul 
1340fa086662SKarsten Graul 	INIT_LIST_HEAD(&smc_clc_eid_table.list);
1341fa086662SKarsten Graul 	rwlock_init(&smc_clc_eid_table.lock);
1342fa086662SKarsten Graul 	smc_clc_eid_table.ueid_cnt = 0;
1343c6b8b8ebSWen Gu #if IS_ENABLED(CONFIG_S390)
1344fa086662SKarsten Graul 	smc_clc_eid_table.seid_enabled = 1;
1345c6b8b8ebSWen Gu #else
1346c6b8b8ebSWen Gu 	smc_clc_eid_table.seid_enabled = 0;
1347c6b8b8ebSWen Gu #endif
1348fa086662SKarsten Graul }
1349fa086662SKarsten Graul 
smc_clc_exit(void)1350fa086662SKarsten Graul void smc_clc_exit(void)
1351fa086662SKarsten Graul {
1352fa086662SKarsten Graul 	smc_clc_ueid_remove(NULL);
1353b81a5eb7SUrsula Braun }
1354