blob: ce27399b38b1ec2dc240a746af93fe86c18dbedf [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Ursula Brauna046d572017-01-09 16:55:16 +01002/*
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 *
Karsten Graul1a26d022018-03-16 15:06:40 +01008 * Copyright IBM Corp. 2016, 2018
Ursula Brauna046d572017-01-09 16:55:16 +01009 *
10 * Author(s): Ursula Braun <ubraun@linux.vnet.ibm.com>
11 */
12
13#include <linux/in.h>
Karsten Graul696cd302018-03-01 13:51:27 +010014#include <linux/inetdevice.h>
Ursula Braun143c0172017-01-12 14:57:15 +010015#include <linux/if_ether.h>
Ingo Molnarc3edc402017-02-02 08:35:14 +010016#include <linux/sched/signal.h>
Ursula Braunb81a5eb2020-09-26 12:44:31 +020017#include <linux/utsname.h>
18#include <linux/ctype.h>
Ingo Molnarc3edc402017-02-02 08:35:14 +010019
Karsten Graul1a26d022018-03-16 15:06:40 +010020#include <net/addrconf.h>
Ursula Brauna046d572017-01-09 16:55:16 +010021#include <net/sock.h>
22#include <net/tcp.h>
23
24#include "smc.h"
Ursula Braun0cfdd8f2017-01-09 16:55:17 +010025#include "smc_core.h"
Ursula Brauna046d572017-01-09 16:55:16 +010026#include "smc_clc.h"
27#include "smc_ib.h"
Hans Wippelc758dfd2018-06-28 19:05:09 +020028#include "smc_ism.h"
Karsten Graulfa086662021-09-14 10:35:05 +020029#include "smc_netlink.h"
Hans Wippelc758dfd2018-06-28 19:05:09 +020030
31#define SMCR_CLC_ACCEPT_CONFIRM_LEN 68
32#define SMCD_CLC_ACCEPT_CONFIRM_LEN 48
Ursula Brauna7c9c5f2020-09-26 12:44:30 +020033#define SMCD_CLC_ACCEPT_CONFIRM_LEN_V2 78
Karsten Graule5c47442021-10-16 11:37:45 +020034#define SMCR_CLC_ACCEPT_CONFIRM_LEN_V2 108
Ursula Braunfb4f7922020-07-08 17:05:15 +020035#define SMC_CLC_RECV_BUF_LEN 100
Ursula Brauna046d572017-01-09 16:55:16 +010036
Stefan Raspl0f627122018-03-01 13:51:26 +010037/* eye catcher "SMCR" EBCDIC for CLC messages */
38static const char SMC_EYECATCHER[4] = {'\xe2', '\xd4', '\xc3', '\xd9'};
Hans Wippelc758dfd2018-06-28 19:05:09 +020039/* eye catcher "SMCD" EBCDIC for CLC messages */
40static const char SMCD_EYECATCHER[4] = {'\xe2', '\xd4', '\xc3', '\xc4'};
Stefan Raspl0f627122018-03-01 13:51:26 +010041
Ursula Braunb81a5eb2020-09-26 12:44:31 +020042static u8 smc_hostname[SMC_MAX_HOSTNAME_LEN];
43
Karsten Graulfa086662021-09-14 10:35:05 +020044struct smc_clc_eid_table {
45 rwlock_t lock;
46 struct list_head list;
47 u8 ueid_cnt;
48 u8 seid_enabled;
49};
50
51static struct smc_clc_eid_table smc_clc_eid_table;
52
53struct 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 */
63static 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
82static 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
112err_out:
113 write_unlock(&smc_clc_eid_table.lock);
114 kfree(new_ueid);
115 return rc;
116}
117
Karsten Graul42042db2021-10-16 11:37:44 +0200118int 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
Karsten Graulfa086662021-09-14 10:35:05 +0200129int 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 */
142static 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 }
Karsten Graul3c572142021-09-14 10:35:07 +0200158 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 }
Karsten Graulfa086662021-09-14 10:35:05 +0200162 write_unlock(&smc_clc_eid_table.lock);
163 return rc;
164}
165
166int 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
178int smc_nl_flush_ueid(struct sk_buff *skb, struct genl_info *info)
179{
180 smc_clc_ueid_remove(NULL);
181 return 0;
182}
183
184static 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 snprintf(ueid_str, sizeof(ueid_str), "%s", ueid);
195 if (nla_put_string(skb, SMC_NLA_EID_TABLE_ENTRY, ueid_str)) {
196 genlmsg_cancel(skb, hdr);
197 return -EMSGSIZE;
198 }
199 genlmsg_end(skb, hdr);
200 return 0;
201}
202
203static int _smc_nl_ueid_dump(struct sk_buff *skb, u32 portid, u32 seq,
204 int start_idx)
205{
206 struct smc_clc_eid_entry *lst_ueid;
207 int idx = 0;
208
209 read_lock(&smc_clc_eid_table.lock);
210 list_for_each_entry(lst_ueid, &smc_clc_eid_table.list, list) {
211 if (idx++ < start_idx)
212 continue;
213 if (smc_nl_ueid_dumpinfo(skb, portid, seq, NLM_F_MULTI,
214 lst_ueid->eid)) {
215 --idx;
216 break;
217 }
218 }
219 read_unlock(&smc_clc_eid_table.lock);
220 return idx;
221}
222
223int smc_nl_dump_ueid(struct sk_buff *skb, struct netlink_callback *cb)
224{
225 struct smc_nl_dmp_ctx *cb_ctx = smc_nl_dmp_ctx(cb);
226 int idx;
227
228 idx = _smc_nl_ueid_dump(skb, NETLINK_CB(cb->skb).portid,
229 cb->nlh->nlmsg_seq, cb_ctx->pos[0]);
230
231 cb_ctx->pos[0] = idx;
232 return skb->len;
233}
234
Karsten Graul3c572142021-09-14 10:35:07 +0200235int smc_nl_dump_seid(struct sk_buff *skb, struct netlink_callback *cb)
236{
237 struct smc_nl_dmp_ctx *cb_ctx = smc_nl_dmp_ctx(cb);
238 char seid_str[SMC_MAX_EID_LEN + 1];
239 u8 seid_enabled;
240 void *hdr;
241 u8 *seid;
242
243 if (cb_ctx->pos[0])
244 return skb->len;
245
246 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
247 &smc_gen_nl_family, NLM_F_MULTI,
248 SMC_NETLINK_DUMP_SEID);
249 if (!hdr)
250 return -ENOMEM;
251 if (!smc_ism_is_v2_capable())
252 goto end;
253
254 smc_ism_get_system_eid(&seid);
255 snprintf(seid_str, sizeof(seid_str), "%s", seid);
256 if (nla_put_string(skb, SMC_NLA_SEID_ENTRY, seid_str))
257 goto err;
258 read_lock(&smc_clc_eid_table.lock);
259 seid_enabled = smc_clc_eid_table.seid_enabled;
260 read_unlock(&smc_clc_eid_table.lock);
261 if (nla_put_u8(skb, SMC_NLA_SEID_ENABLED, seid_enabled))
262 goto err;
263end:
264 genlmsg_end(skb, hdr);
265 cb_ctx->pos[0]++;
266 return skb->len;
267err:
268 genlmsg_cancel(skb, hdr);
269 return -EMSGSIZE;
270}
271
272int smc_nl_enable_seid(struct sk_buff *skb, struct genl_info *info)
273{
274 write_lock(&smc_clc_eid_table.lock);
275 smc_clc_eid_table.seid_enabled = 1;
276 write_unlock(&smc_clc_eid_table.lock);
277 return 0;
278}
279
280int smc_nl_disable_seid(struct sk_buff *skb, struct genl_info *info)
281{
282 int rc = 0;
283
284 write_lock(&smc_clc_eid_table.lock);
285 if (!smc_clc_eid_table.ueid_cnt)
286 rc = -ENOENT;
287 else
288 smc_clc_eid_table.seid_enabled = 0;
289 write_unlock(&smc_clc_eid_table.lock);
290 return rc;
291}
292
Karsten Graulfa086662021-09-14 10:35:05 +0200293static bool _smc_clc_match_ueid(u8 *peer_ueid)
294{
295 struct smc_clc_eid_entry *tmp_ueid;
296
297 list_for_each_entry(tmp_ueid, &smc_clc_eid_table.list, list) {
298 if (!memcmp(tmp_ueid->eid, peer_ueid, SMC_MAX_EID_LEN))
299 return true;
300 }
301 return false;
302}
303
304bool smc_clc_match_eid(u8 *negotiated_eid,
305 struct smc_clc_v2_extension *smc_v2_ext,
306 u8 *peer_eid, u8 *local_eid)
307{
308 bool match = false;
309 int i;
310
311 negotiated_eid[0] = 0;
312 read_lock(&smc_clc_eid_table.lock);
Karsten Graule5c47442021-10-16 11:37:45 +0200313 if (peer_eid && local_eid &&
314 smc_clc_eid_table.seid_enabled &&
Karsten Graulfa086662021-09-14 10:35:05 +0200315 smc_v2_ext->hdr.flag.seid &&
316 !memcmp(peer_eid, local_eid, SMC_MAX_EID_LEN)) {
317 memcpy(negotiated_eid, peer_eid, SMC_MAX_EID_LEN);
318 match = true;
319 goto out;
320 }
321
322 for (i = 0; i < smc_v2_ext->hdr.eid_cnt; i++) {
323 if (_smc_clc_match_ueid(smc_v2_ext->user_eids[i])) {
324 memcpy(negotiated_eid, smc_v2_ext->user_eids[i],
325 SMC_MAX_EID_LEN);
326 match = true;
327 goto out;
328 }
329 }
330out:
331 read_unlock(&smc_clc_eid_table.lock);
332 return match;
333}
334
Ursula Braun8c3dca32020-09-26 12:44:28 +0200335/* check arriving CLC proposal */
336static bool smc_clc_msg_prop_valid(struct smc_clc_msg_proposal *pclc)
337{
338 struct smc_clc_msg_proposal_prefix *pclc_prfx;
339 struct smc_clc_smcd_v2_extension *smcd_v2_ext;
340 struct smc_clc_msg_hdr *hdr = &pclc->hdr;
341 struct smc_clc_v2_extension *v2_ext;
342
343 v2_ext = smc_get_clc_v2_ext(pclc);
344 pclc_prfx = smc_clc_proposal_get_prefix(pclc);
345 if (hdr->version == SMC_V1) {
346 if (hdr->typev1 == SMC_TYPE_N)
347 return false;
348 if (ntohs(hdr->length) !=
349 sizeof(*pclc) + ntohs(pclc->iparea_offset) +
350 sizeof(*pclc_prfx) +
351 pclc_prfx->ipv6_prefixes_cnt *
352 sizeof(struct smc_clc_ipv6_prefix) +
353 sizeof(struct smc_clc_msg_trail))
354 return false;
355 } else {
356 if (ntohs(hdr->length) !=
357 sizeof(*pclc) +
358 sizeof(struct smc_clc_msg_smcd) +
359 (hdr->typev1 != SMC_TYPE_N ?
360 sizeof(*pclc_prfx) +
361 pclc_prfx->ipv6_prefixes_cnt *
362 sizeof(struct smc_clc_ipv6_prefix) : 0) +
363 (hdr->typev2 != SMC_TYPE_N ?
364 sizeof(*v2_ext) +
365 v2_ext->hdr.eid_cnt * SMC_MAX_EID_LEN : 0) +
366 (smcd_indicated(hdr->typev2) ?
367 sizeof(*smcd_v2_ext) + v2_ext->hdr.ism_gid_cnt *
368 sizeof(struct smc_clc_smcd_gid_chid) :
369 0) +
370 sizeof(struct smc_clc_msg_trail))
371 return false;
372 }
373 return true;
374}
375
Ursula Brauna7c9c5f2020-09-26 12:44:30 +0200376/* check arriving CLC accept or confirm */
377static bool
378smc_clc_msg_acc_conf_valid(struct smc_clc_msg_accept_confirm_v2 *clc_v2)
379{
380 struct smc_clc_msg_hdr *hdr = &clc_v2->hdr;
381
382 if (hdr->typev1 != SMC_TYPE_R && hdr->typev1 != SMC_TYPE_D)
383 return false;
384 if (hdr->version == SMC_V1) {
385 if ((hdr->typev1 == SMC_TYPE_R &&
386 ntohs(hdr->length) != SMCR_CLC_ACCEPT_CONFIRM_LEN) ||
387 (hdr->typev1 == SMC_TYPE_D &&
388 ntohs(hdr->length) != SMCD_CLC_ACCEPT_CONFIRM_LEN))
389 return false;
390 } else {
391 if (hdr->typev1 == SMC_TYPE_D &&
Ursula Braunb81a5eb2020-09-26 12:44:31 +0200392 ntohs(hdr->length) != SMCD_CLC_ACCEPT_CONFIRM_LEN_V2 &&
393 (ntohs(hdr->length) != SMCD_CLC_ACCEPT_CONFIRM_LEN_V2 +
394 sizeof(struct smc_clc_first_contact_ext)))
Ursula Brauna7c9c5f2020-09-26 12:44:30 +0200395 return false;
Karsten Graule5c47442021-10-16 11:37:45 +0200396 if (hdr->typev1 == SMC_TYPE_R &&
397 ntohs(hdr->length) < SMCR_CLC_ACCEPT_CONFIRM_LEN_V2)
398 return false;
Ursula Brauna7c9c5f2020-09-26 12:44:30 +0200399 }
400 return true;
401}
402
Karsten Graul8ade2002021-10-16 11:37:47 +0200403/* check arriving CLC decline */
404static bool
405smc_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
Ursula Braunb81a5eb2020-09-26 12:44:31 +0200421static void smc_clc_fill_fce(struct smc_clc_first_contact_ext *fce, int *len)
422{
423 memset(fce, 0, sizeof(*fce));
424 fce->os_type = SMC_CLC_OS_LINUX;
425 fce->release = SMC_RELEASE;
426 memcpy(fce->hostname, smc_hostname, sizeof(smc_hostname));
427 (*len) += sizeof(*fce);
428}
429
Ursula Braune7b7a642017-12-07 13:38:49 +0100430/* check if received message has a correct header length and contains valid
431 * heading and trailing eyecatchers
432 */
Ursula Braunfb4f7922020-07-08 17:05:15 +0200433static bool smc_clc_msg_hdr_valid(struct smc_clc_msg_hdr *clcm, bool check_trl)
Ursula Braune7b7a642017-12-07 13:38:49 +0100434{
Ursula Brauna7c9c5f2020-09-26 12:44:30 +0200435 struct smc_clc_msg_accept_confirm_v2 *clc_v2;
Ursula Braune7b7a642017-12-07 13:38:49 +0100436 struct smc_clc_msg_proposal *pclc;
437 struct smc_clc_msg_decline *dclc;
438 struct smc_clc_msg_trail *trl;
439
Hans Wippelc758dfd2018-06-28 19:05:09 +0200440 if (memcmp(clcm->eyecatcher, SMC_EYECATCHER, sizeof(SMC_EYECATCHER)) &&
441 memcmp(clcm->eyecatcher, SMCD_EYECATCHER, sizeof(SMCD_EYECATCHER)))
Ursula Braune7b7a642017-12-07 13:38:49 +0100442 return false;
443 switch (clcm->type) {
444 case SMC_CLC_PROPOSAL:
445 pclc = (struct smc_clc_msg_proposal *)clcm;
Ursula Braun8c3dca32020-09-26 12:44:28 +0200446 if (!smc_clc_msg_prop_valid(pclc))
Ursula Braune7b7a642017-12-07 13:38:49 +0100447 return false;
448 trl = (struct smc_clc_msg_trail *)
449 ((u8 *)pclc + ntohs(pclc->hdr.length) - sizeof(*trl));
450 break;
451 case SMC_CLC_ACCEPT:
452 case SMC_CLC_CONFIRM:
Ursula Brauna7c9c5f2020-09-26 12:44:30 +0200453 clc_v2 = (struct smc_clc_msg_accept_confirm_v2 *)clcm;
454 if (!smc_clc_msg_acc_conf_valid(clc_v2))
Hans Wippelc758dfd2018-06-28 19:05:09 +0200455 return false;
456 trl = (struct smc_clc_msg_trail *)
Ursula Brauna7c9c5f2020-09-26 12:44:30 +0200457 ((u8 *)clc_v2 + ntohs(clc_v2->hdr.length) -
458 sizeof(*trl));
Ursula Braune7b7a642017-12-07 13:38:49 +0100459 break;
460 case SMC_CLC_DECLINE:
461 dclc = (struct smc_clc_msg_decline *)clcm;
Karsten Graul8ade2002021-10-16 11:37:47 +0200462 if (!smc_clc_msg_decl_valid(dclc))
Ursula Braune7b7a642017-12-07 13:38:49 +0100463 return false;
Karsten Graul8ade2002021-10-16 11:37:47 +0200464 check_trl = false;
Ursula Braune7b7a642017-12-07 13:38:49 +0100465 break;
466 default:
467 return false;
468 }
Ursula Braunfb4f7922020-07-08 17:05:15 +0200469 if (check_trl &&
470 memcmp(trl->eyecatcher, SMC_EYECATCHER, sizeof(SMC_EYECATCHER)) &&
Hans Wippelc758dfd2018-06-28 19:05:09 +0200471 memcmp(trl->eyecatcher, SMCD_EYECATCHER, sizeof(SMCD_EYECATCHER)))
Ursula Braune7b7a642017-12-07 13:38:49 +0100472 return false;
473 return true;
474}
475
Karsten Graulc246d942018-03-16 15:06:39 +0100476/* find ipv4 addr on device and get the prefix len, fill CLC proposal msg */
477static int smc_clc_prfx_set4_rcu(struct dst_entry *dst, __be32 ipv4,
478 struct smc_clc_msg_proposal_prefix *prop)
479{
480 struct in_device *in_dev = __in_dev_get_rcu(dst->dev);
Florian Westphalcd5a4112019-05-31 18:27:07 +0200481 const struct in_ifaddr *ifa;
Karsten Graulc246d942018-03-16 15:06:39 +0100482
483 if (!in_dev)
484 return -ENODEV;
Florian Westphalcd5a4112019-05-31 18:27:07 +0200485
486 in_dev_for_each_ifa_rcu(ifa, in_dev) {
Karsten Graulc246d942018-03-16 15:06:39 +0100487 if (!inet_ifa_match(ipv4, ifa))
488 continue;
489 prop->prefix_len = inet_mask_len(ifa->ifa_mask);
490 prop->outgoing_subnet = ifa->ifa_address & ifa->ifa_mask;
491 /* prop->ipv6_prefixes_cnt = 0; already done by memset before */
492 return 0;
Florian Westphalcd5a4112019-05-31 18:27:07 +0200493 }
Karsten Graulc246d942018-03-16 15:06:39 +0100494 return -ENOENT;
495}
496
Karsten Graul1a26d022018-03-16 15:06:40 +0100497/* fill CLC proposal msg with ipv6 prefixes from device */
498static int smc_clc_prfx_set6_rcu(struct dst_entry *dst,
499 struct smc_clc_msg_proposal_prefix *prop,
500 struct smc_clc_ipv6_prefix *ipv6_prfx)
501{
502#if IS_ENABLED(CONFIG_IPV6)
503 struct inet6_dev *in6_dev = __in6_dev_get(dst->dev);
504 struct inet6_ifaddr *ifa;
505 int cnt = 0;
506
507 if (!in6_dev)
508 return -ENODEV;
509 /* use a maximum of 8 IPv6 prefixes from device */
510 list_for_each_entry(ifa, &in6_dev->addr_list, if_list) {
511 if (ipv6_addr_type(&ifa->addr) & IPV6_ADDR_LINKLOCAL)
512 continue;
513 ipv6_addr_prefix(&ipv6_prfx[cnt].prefix,
514 &ifa->addr, ifa->prefix_len);
515 ipv6_prfx[cnt].prefix_len = ifa->prefix_len;
516 cnt++;
517 if (cnt == SMC_CLC_MAX_V6_PREFIX)
518 break;
519 }
520 prop->ipv6_prefixes_cnt = cnt;
521 if (cnt)
522 return 0;
523#endif
524 return -ENOENT;
525}
526
Karsten Graulc246d942018-03-16 15:06:39 +0100527/* retrieve and set prefixes in CLC proposal msg */
528static int smc_clc_prfx_set(struct socket *clcsock,
Karsten Graul1a26d022018-03-16 15:06:40 +0100529 struct smc_clc_msg_proposal_prefix *prop,
530 struct smc_clc_ipv6_prefix *ipv6_prfx)
Karsten Graul696cd302018-03-01 13:51:27 +0100531{
532 struct dst_entry *dst = sk_dst_get(clcsock->sk);
Karsten Graulc246d942018-03-16 15:06:39 +0100533 struct sockaddr_storage addrs;
Karsten Graul1a26d022018-03-16 15:06:40 +0100534 struct sockaddr_in6 *addr6;
Karsten Graulc246d942018-03-16 15:06:39 +0100535 struct sockaddr_in *addr;
536 int rc = -ENOENT;
537
Karsten Graulc246d942018-03-16 15:06:39 +0100538 if (!dst) {
539 rc = -ENOTCONN;
540 goto out;
541 }
542 if (!dst->dev) {
543 rc = -ENODEV;
544 goto out_rel;
545 }
546 /* get address to which the internal TCP socket is bound */
Karsten Graul6c907312021-09-20 21:18:14 +0200547 if (kernel_getsockname(clcsock, (struct sockaddr *)&addrs) < 0)
548 goto out_rel;
Karsten Graulc246d942018-03-16 15:06:39 +0100549 /* analyze IP specific data of net_device belonging to TCP socket */
Karsten Graul1a26d022018-03-16 15:06:40 +0100550 addr6 = (struct sockaddr_in6 *)&addrs;
Karsten Graulc246d942018-03-16 15:06:39 +0100551 rcu_read_lock();
552 if (addrs.ss_family == PF_INET) {
553 /* IPv4 */
554 addr = (struct sockaddr_in *)&addrs;
555 rc = smc_clc_prfx_set4_rcu(dst, addr->sin_addr.s_addr, prop);
Karsten Graul1a26d022018-03-16 15:06:40 +0100556 } else if (ipv6_addr_v4mapped(&addr6->sin6_addr)) {
557 /* mapped IPv4 address - peer is IPv4 only */
558 rc = smc_clc_prfx_set4_rcu(dst, addr6->sin6_addr.s6_addr32[3],
559 prop);
560 } else {
561 /* IPv6 */
562 rc = smc_clc_prfx_set6_rcu(dst, prop, ipv6_prfx);
Karsten Graulc246d942018-03-16 15:06:39 +0100563 }
564 rcu_read_unlock();
565out_rel:
566 dst_release(dst);
567out:
568 return rc;
569}
570
571/* match ipv4 addrs of dev against addr in CLC proposal */
572static int smc_clc_prfx_match4_rcu(struct net_device *dev,
573 struct smc_clc_msg_proposal_prefix *prop)
574{
575 struct in_device *in_dev = __in_dev_get_rcu(dev);
Florian Westphalcd5a4112019-05-31 18:27:07 +0200576 const struct in_ifaddr *ifa;
Karsten Graulc246d942018-03-16 15:06:39 +0100577
578 if (!in_dev)
579 return -ENODEV;
Florian Westphalcd5a4112019-05-31 18:27:07 +0200580 in_dev_for_each_ifa_rcu(ifa, in_dev) {
Karsten Graulc246d942018-03-16 15:06:39 +0100581 if (prop->prefix_len == inet_mask_len(ifa->ifa_mask) &&
582 inet_ifa_match(prop->outgoing_subnet, ifa))
583 return 0;
Florian Westphalcd5a4112019-05-31 18:27:07 +0200584 }
Karsten Graulc246d942018-03-16 15:06:39 +0100585
586 return -ENOENT;
587}
588
Karsten Graul1a26d022018-03-16 15:06:40 +0100589/* match ipv6 addrs of dev against addrs in CLC proposal */
590static int smc_clc_prfx_match6_rcu(struct net_device *dev,
591 struct smc_clc_msg_proposal_prefix *prop)
592{
593#if IS_ENABLED(CONFIG_IPV6)
594 struct inet6_dev *in6_dev = __in6_dev_get(dev);
595 struct smc_clc_ipv6_prefix *ipv6_prfx;
596 struct inet6_ifaddr *ifa;
597 int i, max;
598
599 if (!in6_dev)
600 return -ENODEV;
601 /* ipv6 prefix list starts behind smc_clc_msg_proposal_prefix */
602 ipv6_prfx = (struct smc_clc_ipv6_prefix *)((u8 *)prop + sizeof(*prop));
603 max = min_t(u8, prop->ipv6_prefixes_cnt, SMC_CLC_MAX_V6_PREFIX);
604 list_for_each_entry(ifa, &in6_dev->addr_list, if_list) {
605 if (ipv6_addr_type(&ifa->addr) & IPV6_ADDR_LINKLOCAL)
606 continue;
607 for (i = 0; i < max; i++) {
608 if (ifa->prefix_len == ipv6_prfx[i].prefix_len &&
609 ipv6_prefix_equal(&ifa->addr, &ipv6_prfx[i].prefix,
610 ifa->prefix_len))
611 return 0;
612 }
613 }
614#endif
615 return -ENOENT;
616}
617
Karsten Graulc246d942018-03-16 15:06:39 +0100618/* check if proposed prefixes match one of our device prefixes */
619int smc_clc_prfx_match(struct socket *clcsock,
620 struct smc_clc_msg_proposal_prefix *prop)
621{
622 struct dst_entry *dst = sk_dst_get(clcsock->sk);
Karsten Graul1a26d022018-03-16 15:06:40 +0100623 int rc;
Karsten Graul696cd302018-03-01 13:51:27 +0100624
625 if (!dst) {
626 rc = -ENOTCONN;
627 goto out;
628 }
629 if (!dst->dev) {
630 rc = -ENODEV;
631 goto out_rel;
632 }
Karsten Graul696cd302018-03-01 13:51:27 +0100633 rcu_read_lock();
Karsten Graulc246d942018-03-16 15:06:39 +0100634 if (!prop->ipv6_prefixes_cnt)
635 rc = smc_clc_prfx_match4_rcu(dst->dev, prop);
Karsten Graul1a26d022018-03-16 15:06:40 +0100636 else
637 rc = smc_clc_prfx_match6_rcu(dst->dev, prop);
Karsten Graul696cd302018-03-01 13:51:27 +0100638 rcu_read_unlock();
Karsten Graul696cd302018-03-01 13:51:27 +0100639out_rel:
640 dst_release(dst);
641out:
642 return rc;
643}
644
Ursula Brauna046d572017-01-09 16:55:16 +0100645/* Wait for data on the tcp-socket, analyze received data
646 * Returns:
647 * 0 if success and it was not a decline that we received.
648 * SMC_CLC_DECL_REPLY if decline received for fallback w/o another decl send.
649 * clcsock error, -EINTR, -ECONNRESET, -EPROTO otherwise.
650 */
651int smc_clc_wait_msg(struct smc_sock *smc, void *buf, int buflen,
Ursula Braun2b59f582018-11-22 10:26:39 +0100652 u8 expected_type, unsigned long timeout)
Ursula Brauna046d572017-01-09 16:55:16 +0100653{
Karsten Graulf6bdc422018-07-18 15:22:51 +0200654 long rcvtimeo = smc->clcsock->sk->sk_rcvtimeo;
Ursula Brauna046d572017-01-09 16:55:16 +0100655 struct sock *clc_sk = smc->clcsock->sk;
656 struct smc_clc_msg_hdr *clcm = buf;
657 struct msghdr msg = {NULL, 0};
658 int reason_code = 0;
Al Virod63d2712017-09-20 20:21:22 -0400659 struct kvec vec = {buf, buflen};
Ursula Braunfb4f7922020-07-08 17:05:15 +0200660 int len, datlen, recvlen;
661 bool check_trl = true;
Ursula Brauna046d572017-01-09 16:55:16 +0100662 int krflags;
663
664 /* peek the first few bytes to determine length of data to receive
665 * so we don't consume any subsequent CLC message or payload data
666 * in the TCP byte stream
667 */
Al Virod63d2712017-09-20 20:21:22 -0400668 /*
669 * Caller must make sure that buflen is no less than
670 * sizeof(struct smc_clc_msg_hdr)
671 */
Ursula Brauna046d572017-01-09 16:55:16 +0100672 krflags = MSG_PEEK | MSG_WAITALL;
Ursula Braun2b59f582018-11-22 10:26:39 +0100673 clc_sk->sk_rcvtimeo = timeout;
David Howellsaa563d72018-10-20 00:57:56 +0100674 iov_iter_kvec(&msg.msg_iter, READ, &vec, 1,
Al Virod63d2712017-09-20 20:21:22 -0400675 sizeof(struct smc_clc_msg_hdr));
676 len = sock_recvmsg(smc->clcsock, &msg, krflags);
Ursula Brauna046d572017-01-09 16:55:16 +0100677 if (signal_pending(current)) {
678 reason_code = -EINTR;
679 clc_sk->sk_err = EINTR;
680 smc->sk.sk_err = EINTR;
681 goto out;
682 }
683 if (clc_sk->sk_err) {
684 reason_code = -clc_sk->sk_err;
Ursula Braun9ed28552018-11-22 10:26:37 +0100685 if (clc_sk->sk_err == EAGAIN &&
686 expected_type == SMC_CLC_DECLINE)
687 clc_sk->sk_err = 0; /* reset for fallback usage */
688 else
689 smc->sk.sk_err = clc_sk->sk_err;
Ursula Brauna046d572017-01-09 16:55:16 +0100690 goto out;
691 }
692 if (!len) { /* peer has performed orderly shutdown */
693 smc->sk.sk_err = ECONNRESET;
694 reason_code = -ECONNRESET;
695 goto out;
696 }
697 if (len < 0) {
Ursula Braun9ed28552018-11-22 10:26:37 +0100698 if (len != -EAGAIN || expected_type != SMC_CLC_DECLINE)
699 smc->sk.sk_err = -len;
Ursula Brauna046d572017-01-09 16:55:16 +0100700 reason_code = len;
701 goto out;
702 }
703 datlen = ntohs(clcm->length);
704 if ((len < sizeof(struct smc_clc_msg_hdr)) ||
Ursula Braun5ac54d82020-09-10 18:48:21 +0200705 (clcm->version < SMC_V1) ||
Ursula Brauna046d572017-01-09 16:55:16 +0100706 ((clcm->type != SMC_CLC_DECLINE) &&
707 (clcm->type != expected_type))) {
708 smc->sk.sk_err = EPROTO;
709 reason_code = -EPROTO;
710 goto out;
711 }
712
713 /* receive the complete CLC message */
Ursula Brauna046d572017-01-09 16:55:16 +0100714 memset(&msg, 0, sizeof(struct msghdr));
Ursula Braunfb4f7922020-07-08 17:05:15 +0200715 if (datlen > buflen) {
716 check_trl = false;
717 recvlen = buflen;
718 } else {
719 recvlen = datlen;
720 }
721 iov_iter_kvec(&msg.msg_iter, READ, &vec, 1, recvlen);
Ursula Brauna046d572017-01-09 16:55:16 +0100722 krflags = MSG_WAITALL;
Al Virod63d2712017-09-20 20:21:22 -0400723 len = sock_recvmsg(smc->clcsock, &msg, krflags);
Ursula Braunfb4f7922020-07-08 17:05:15 +0200724 if (len < recvlen || !smc_clc_msg_hdr_valid(clcm, check_trl)) {
Ursula Brauna046d572017-01-09 16:55:16 +0100725 smc->sk.sk_err = EPROTO;
726 reason_code = -EPROTO;
727 goto out;
728 }
Ursula Braunfb4f7922020-07-08 17:05:15 +0200729 datlen -= len;
730 while (datlen) {
731 u8 tmp[SMC_CLC_RECV_BUF_LEN];
732
733 vec.iov_base = &tmp;
734 vec.iov_len = SMC_CLC_RECV_BUF_LEN;
735 /* receive remaining proposal message */
736 recvlen = datlen > SMC_CLC_RECV_BUF_LEN ?
737 SMC_CLC_RECV_BUF_LEN : datlen;
738 iov_iter_kvec(&msg.msg_iter, READ, &vec, 1, recvlen);
739 len = sock_recvmsg(smc->clcsock, &msg, krflags);
740 datlen -= len;
741 }
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100742 if (clcm->type == SMC_CLC_DECLINE) {
Karsten Graul603cc142018-07-25 16:35:32 +0200743 struct smc_clc_msg_decline *dclc;
744
745 dclc = (struct smc_clc_msg_decline *)clcm;
746 reason_code = SMC_CLC_DECL_PEERDECL;
747 smc->peer_diagnosis = ntohl(dclc->peer_diagnosis);
Ursula Braunf1eb02f2020-09-26 12:44:20 +0200748 if (((struct smc_clc_msg_decline *)buf)->hdr.typev2 &
749 SMC_FIRST_CONTACT_MASK) {
Karsten Graul517c3002018-05-15 17:05:03 +0200750 smc->conn.lgr->sync_err = 1;
Karsten Graul5f78fe92020-02-17 16:24:54 +0100751 smc_lgr_terminate_sched(smc->conn.lgr);
Ursula Braunbfbedfd2017-09-21 09:16:32 +0200752 }
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100753 }
754
Ursula Brauna046d572017-01-09 16:55:16 +0100755out:
Ursula Braun2b59f582018-11-22 10:26:39 +0100756 clc_sk->sk_rcvtimeo = rcvtimeo;
Ursula Brauna046d572017-01-09 16:55:16 +0100757 return reason_code;
758}
759
760/* send CLC DECLINE message across internal TCP socket */
Ursula Braune8d726c82020-09-26 12:44:32 +0200761int smc_clc_send_decline(struct smc_sock *smc, u32 peer_diag_info, u8 version)
Ursula Brauna046d572017-01-09 16:55:16 +0100762{
Karsten Graul8ade2002021-10-16 11:37:47 +0200763 struct smc_clc_msg_decline *dclc_v1;
764 struct smc_clc_msg_decline_v2 dclc;
Ursula Brauna046d572017-01-09 16:55:16 +0100765 struct msghdr msg;
Karsten Graul8ade2002021-10-16 11:37:47 +0200766 int len, send_len;
Ursula Brauna046d572017-01-09 16:55:16 +0100767 struct kvec vec;
Ursula Brauna046d572017-01-09 16:55:16 +0100768
Karsten Graul8ade2002021-10-16 11:37:47 +0200769 dclc_v1 = (struct smc_clc_msg_decline *)&dclc;
Ursula Brauna046d572017-01-09 16:55:16 +0100770 memset(&dclc, 0, sizeof(dclc));
771 memcpy(dclc.hdr.eyecatcher, SMC_EYECATCHER, sizeof(SMC_EYECATCHER));
772 dclc.hdr.type = SMC_CLC_DECLINE;
Ursula Braune8d726c82020-09-26 12:44:32 +0200773 dclc.hdr.version = version;
774 dclc.os_type = version == SMC_V1 ? 0 : SMC_CLC_OS_LINUX;
Ursula Braunf1eb02f2020-09-26 12:44:20 +0200775 dclc.hdr.typev2 = (peer_diag_info == SMC_CLC_DECL_SYNCERR) ?
776 SMC_FIRST_CONTACT_MASK : 0;
Wen Guea89c6c2022-01-13 16:36:41 +0800777 if ((!smc_conn_lgr_valid(&smc->conn) || !smc->conn.lgr->is_smcd) &&
Hans Wippela082ec82020-02-25 22:41:22 +0100778 smc_ib_is_valid_local_systemid())
Ursula Braun369537c2020-02-14 08:59:00 +0100779 memcpy(dclc.id_for_peer, local_systemid,
780 sizeof(local_systemid));
Ursula Brauna046d572017-01-09 16:55:16 +0100781 dclc.peer_diagnosis = htonl(peer_diag_info);
Karsten Graul8ade2002021-10-16 11:37:47 +0200782 if (version == SMC_V1) {
783 memcpy(dclc_v1->trl.eyecatcher, SMC_EYECATCHER,
784 sizeof(SMC_EYECATCHER));
785 send_len = sizeof(*dclc_v1);
786 } else {
787 memcpy(dclc.trl.eyecatcher, SMC_EYECATCHER,
788 sizeof(SMC_EYECATCHER));
789 send_len = sizeof(dclc);
790 }
791 dclc.hdr.length = htons(send_len);
Ursula Brauna046d572017-01-09 16:55:16 +0100792
793 memset(&msg, 0, sizeof(msg));
794 vec.iov_base = &dclc;
Karsten Graul8ade2002021-10-16 11:37:47 +0200795 vec.iov_len = send_len;
796 len = kernel_sendmsg(smc->clcsock, &msg, &vec, 1, send_len);
797 if (len < 0 || len < send_len)
Ursula Braun6ae36bf2018-11-22 10:26:36 +0100798 len = -EPROTO;
799 return len > 0 ? 0 : len;
Ursula Brauna046d572017-01-09 16:55:16 +0100800}
801
802/* send CLC PROPOSAL message across internal TCP socket */
Ursula Braund70bf4f2020-09-26 12:44:27 +0200803int smc_clc_send_proposal(struct smc_sock *smc, struct smc_init_info *ini)
Ursula Brauna046d572017-01-09 16:55:16 +0100804{
Ursula Braun8c3dca32020-09-26 12:44:28 +0200805 struct smc_clc_smcd_v2_extension *smcd_v2_ext;
Ursula Braun6bb14e482020-09-10 18:48:22 +0200806 struct smc_clc_msg_proposal_prefix *pclc_prfx;
807 struct smc_clc_msg_proposal *pclc_base;
Ursula Braun8c3dca32020-09-26 12:44:28 +0200808 struct smc_clc_smcd_gid_chid *gidchids;
Ursula Braun6bb14e482020-09-10 18:48:22 +0200809 struct smc_clc_msg_proposal_area *pclc;
810 struct smc_clc_ipv6_prefix *ipv6_prfx;
Ursula Braun8c3dca32020-09-26 12:44:28 +0200811 struct smc_clc_v2_extension *v2_ext;
Ursula Braun6bb14e482020-09-10 18:48:22 +0200812 struct smc_clc_msg_smcd *pclc_smcd;
813 struct smc_clc_msg_trail *trl;
Karsten Graul1a26d022018-03-16 15:06:40 +0100814 int len, i, plen, rc;
Ursula Brauna046d572017-01-09 16:55:16 +0100815 int reason_code = 0;
Ursula Braun8c3dca32020-09-26 12:44:28 +0200816 struct kvec vec[8];
Ursula Brauna046d572017-01-09 16:55:16 +0100817 struct msghdr msg;
Ursula Brauna046d572017-01-09 16:55:16 +0100818
Ursula Braun6bb14e482020-09-10 18:48:22 +0200819 pclc = kzalloc(sizeof(*pclc), GFP_KERNEL);
820 if (!pclc)
821 return -ENOMEM;
822
823 pclc_base = &pclc->pclc_base;
824 pclc_smcd = &pclc->pclc_smcd;
825 pclc_prfx = &pclc->pclc_prfx;
826 ipv6_prfx = pclc->pclc_prfx_ipv6;
Ursula Braun8c3dca32020-09-26 12:44:28 +0200827 v2_ext = &pclc->pclc_v2_ext;
828 smcd_v2_ext = &pclc->pclc_smcd_v2_ext;
829 gidchids = pclc->pclc_gidchids;
Ursula Braun6bb14e482020-09-10 18:48:22 +0200830 trl = &pclc->pclc_trl;
831
Ursula Braun8c3dca32020-09-26 12:44:28 +0200832 pclc_base->hdr.version = SMC_V2;
833 pclc_base->hdr.typev1 = ini->smc_type_v1;
834 pclc_base->hdr.typev2 = ini->smc_type_v2;
835 plen = sizeof(*pclc_base) + sizeof(*pclc_smcd) + sizeof(*trl);
836
Karsten Graulc246d942018-03-16 15:06:39 +0100837 /* retrieve ip prefixes for CLC proposal msg */
Ursula Braun8c3dca32020-09-26 12:44:28 +0200838 if (ini->smc_type_v1 != SMC_TYPE_N) {
839 rc = smc_clc_prfx_set(smc->clcsock, pclc_prfx, ipv6_prfx);
840 if (rc) {
841 if (ini->smc_type_v2 == SMC_TYPE_N) {
842 kfree(pclc);
843 return SMC_CLC_DECL_CNFERR;
844 }
845 pclc_base->hdr.typev1 = SMC_TYPE_N;
846 } else {
847 pclc_base->iparea_offset = htons(sizeof(*pclc_smcd));
848 plen += sizeof(*pclc_prfx) +
849 pclc_prfx->ipv6_prefixes_cnt *
850 sizeof(ipv6_prfx[0]);
851 }
Ursula Braun6bb14e482020-09-10 18:48:22 +0200852 }
Karsten Graulc246d942018-03-16 15:06:39 +0100853
Ursula Braun8c3dca32020-09-26 12:44:28 +0200854 /* build SMC Proposal CLC message */
Ursula Braun6bb14e482020-09-10 18:48:22 +0200855 memcpy(pclc_base->hdr.eyecatcher, SMC_EYECATCHER,
856 sizeof(SMC_EYECATCHER));
857 pclc_base->hdr.type = SMC_CLC_PROPOSAL;
Ursula Braund70bf4f2020-09-26 12:44:27 +0200858 if (smcr_indicated(ini->smc_type_v1)) {
Hans Wippelc758dfd2018-06-28 19:05:09 +0200859 /* add SMC-R specifics */
Ursula Braun6bb14e482020-09-10 18:48:22 +0200860 memcpy(pclc_base->lcl.id_for_peer, local_systemid,
Hans Wippelc758dfd2018-06-28 19:05:09 +0200861 sizeof(local_systemid));
Ursula Braun6bb14e482020-09-10 18:48:22 +0200862 memcpy(pclc_base->lcl.gid, ini->ib_gid, SMC_GID_SIZE);
863 memcpy(pclc_base->lcl.mac, &ini->ib_dev->mac[ini->ib_port - 1],
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200864 ETH_ALEN);
Hans Wippelc758dfd2018-06-28 19:05:09 +0200865 }
Ursula Braund70bf4f2020-09-26 12:44:27 +0200866 if (smcd_indicated(ini->smc_type_v1)) {
Hans Wippelc758dfd2018-06-28 19:05:09 +0200867 /* add SMC-D specifics */
Ursula Braun8c3dca32020-09-26 12:44:28 +0200868 if (ini->ism_dev[0]) {
869 pclc_smcd->ism.gid = htonll(ini->ism_dev[0]->local_gid);
870 pclc_smcd->ism.chid =
871 htons(smc_ism_get_chid(ini->ism_dev[0]));
872 }
873 }
874 if (ini->smc_type_v2 == SMC_TYPE_N) {
875 pclc_smcd->v2_ext_offset = 0;
876 } else {
Karsten Graulfa086662021-09-14 10:35:05 +0200877 struct smc_clc_eid_entry *ueident;
Ursula Braun8c3dca32020-09-26 12:44:28 +0200878 u16 v2_ext_offset;
Ursula Braun8c3dca32020-09-26 12:44:28 +0200879
Karsten Graule5c47442021-10-16 11:37:45 +0200880 v2_ext->hdr.flag.release = SMC_RELEASE;
Ursula Braun8c3dca32020-09-26 12:44:28 +0200881 v2_ext_offset = sizeof(*pclc_smcd) -
882 offsetofend(struct smc_clc_msg_smcd, v2_ext_offset);
883 if (ini->smc_type_v1 != SMC_TYPE_N)
884 v2_ext_offset += sizeof(*pclc_prfx) +
885 pclc_prfx->ipv6_prefixes_cnt *
886 sizeof(ipv6_prfx[0]);
887 pclc_smcd->v2_ext_offset = htons(v2_ext_offset);
Karsten Graule5c47442021-10-16 11:37:45 +0200888 plen += sizeof(*v2_ext);
Karsten Graulfa086662021-09-14 10:35:05 +0200889
890 read_lock(&smc_clc_eid_table.lock);
891 v2_ext->hdr.eid_cnt = smc_clc_eid_table.ueid_cnt;
892 plen += smc_clc_eid_table.ueid_cnt * SMC_MAX_EID_LEN;
893 i = 0;
894 list_for_each_entry(ueident, &smc_clc_eid_table.list, list) {
895 memcpy(v2_ext->user_eids[i++], ueident->eid,
896 sizeof(ueident->eid));
897 }
Karsten Graulfa086662021-09-14 10:35:05 +0200898 read_unlock(&smc_clc_eid_table.lock);
Karsten Graule5c47442021-10-16 11:37:45 +0200899 }
900 if (smcd_indicated(ini->smc_type_v2)) {
901 u8 *eid = NULL;
902
903 v2_ext->hdr.flag.seid = smc_clc_eid_table.seid_enabled;
Ursula Braun8c3dca32020-09-26 12:44:28 +0200904 v2_ext->hdr.ism_gid_cnt = ini->ism_offered_cnt;
Ursula Braun8c3dca32020-09-26 12:44:28 +0200905 v2_ext->hdr.smcd_v2_ext_offset = htons(sizeof(*v2_ext) -
906 offsetofend(struct smc_clnt_opts_area_hdr,
907 smcd_v2_ext_offset) +
908 v2_ext->hdr.eid_cnt * SMC_MAX_EID_LEN);
Karsten Graul11a26c52021-09-14 10:35:06 +0200909 smc_ism_get_system_eid(&eid);
Karsten Graulfa086662021-09-14 10:35:05 +0200910 if (eid && v2_ext->hdr.flag.seid)
Ursula Braun8c3dca32020-09-26 12:44:28 +0200911 memcpy(smcd_v2_ext->system_eid, eid, SMC_MAX_EID_LEN);
Karsten Graule5c47442021-10-16 11:37:45 +0200912 plen += sizeof(*smcd_v2_ext);
Ursula Braun8c3dca32020-09-26 12:44:28 +0200913 if (ini->ism_offered_cnt) {
914 for (i = 1; i <= ini->ism_offered_cnt; i++) {
915 gidchids[i - 1].gid =
916 htonll(ini->ism_dev[i]->local_gid);
917 gidchids[i - 1].chid =
918 htons(smc_ism_get_chid(ini->ism_dev[i]));
919 }
920 plen += ini->ism_offered_cnt *
921 sizeof(struct smc_clc_smcd_gid_chid);
922 }
Hans Wippelc758dfd2018-06-28 19:05:09 +0200923 }
Karsten Graule5c47442021-10-16 11:37:45 +0200924 if (smcr_indicated(ini->smc_type_v2))
925 memcpy(v2_ext->roce, ini->smcrv2.ib_gid_v2, SMC_GID_SIZE);
926
Ursula Braun6bb14e482020-09-10 18:48:22 +0200927 pclc_base->hdr.length = htons(plen);
Ursula Braun6bb14e482020-09-10 18:48:22 +0200928 memcpy(trl->eyecatcher, SMC_EYECATCHER, sizeof(SMC_EYECATCHER));
Ursula Braun8c3dca32020-09-26 12:44:28 +0200929
930 /* send SMC Proposal CLC message */
Ursula Brauna046d572017-01-09 16:55:16 +0100931 memset(&msg, 0, sizeof(msg));
Karsten Graul1a26d022018-03-16 15:06:40 +0100932 i = 0;
Ursula Braun6bb14e482020-09-10 18:48:22 +0200933 vec[i].iov_base = pclc_base;
934 vec[i++].iov_len = sizeof(*pclc_base);
Ursula Braun8c3dca32020-09-26 12:44:28 +0200935 vec[i].iov_base = pclc_smcd;
936 vec[i++].iov_len = sizeof(*pclc_smcd);
937 if (ini->smc_type_v1 != SMC_TYPE_N) {
938 vec[i].iov_base = pclc_prfx;
939 vec[i++].iov_len = sizeof(*pclc_prfx);
940 if (pclc_prfx->ipv6_prefixes_cnt > 0) {
941 vec[i].iov_base = ipv6_prfx;
942 vec[i++].iov_len = pclc_prfx->ipv6_prefixes_cnt *
943 sizeof(ipv6_prfx[0]);
944 }
Hans Wippelc758dfd2018-06-28 19:05:09 +0200945 }
Ursula Braun8c3dca32020-09-26 12:44:28 +0200946 if (ini->smc_type_v2 != SMC_TYPE_N) {
947 vec[i].iov_base = v2_ext;
Karsten Graulfa086662021-09-14 10:35:05 +0200948 vec[i++].iov_len = sizeof(*v2_ext) +
949 (v2_ext->hdr.eid_cnt * SMC_MAX_EID_LEN);
Karsten Graule5c47442021-10-16 11:37:45 +0200950 if (smcd_indicated(ini->smc_type_v2)) {
951 vec[i].iov_base = smcd_v2_ext;
952 vec[i++].iov_len = sizeof(*smcd_v2_ext);
953 if (ini->ism_offered_cnt) {
954 vec[i].iov_base = gidchids;
955 vec[i++].iov_len = ini->ism_offered_cnt *
Ursula Braun8c3dca32020-09-26 12:44:28 +0200956 sizeof(struct smc_clc_smcd_gid_chid);
Karsten Graule5c47442021-10-16 11:37:45 +0200957 }
Ursula Braun8c3dca32020-09-26 12:44:28 +0200958 }
Karsten Graul1a26d022018-03-16 15:06:40 +0100959 }
Ursula Braun6bb14e482020-09-10 18:48:22 +0200960 vec[i].iov_base = trl;
961 vec[i++].iov_len = sizeof(*trl);
Ursula Brauna046d572017-01-09 16:55:16 +0100962 /* due to the few bytes needed for clc-handshake this cannot block */
Karsten Graul1a26d022018-03-16 15:06:40 +0100963 len = kernel_sendmsg(smc->clcsock, &msg, vec, i, plen);
YueHaibing38189772018-09-18 15:46:38 +0200964 if (len < 0) {
965 smc->sk.sk_err = smc->clcsock->sk->sk_err;
966 reason_code = -smc->sk.sk_err;
Ursula Braun6bb14e482020-09-10 18:48:22 +0200967 } else if (len < ntohs(pclc_base->hdr.length)) {
YueHaibing38189772018-09-18 15:46:38 +0200968 reason_code = -ENETUNREACH;
969 smc->sk.sk_err = -reason_code;
Ursula Brauna046d572017-01-09 16:55:16 +0100970 }
971
Ursula Braun6bb14e482020-09-10 18:48:22 +0200972 kfree(pclc);
Ursula Brauna046d572017-01-09 16:55:16 +0100973 return reason_code;
974}
975
Ursula Braun3d9725a2020-09-10 18:48:23 +0200976/* build and send CLC CONFIRM / ACCEPT message */
977static int smc_clc_send_confirm_accept(struct smc_sock *smc,
Ursula Brauna7c9c5f2020-09-26 12:44:30 +0200978 struct smc_clc_msg_accept_confirm_v2 *clc_v2,
Karsten Graulfa086662021-09-14 10:35:05 +0200979 int first_contact, u8 version,
Karsten Graule5c47442021-10-16 11:37:45 +0200980 u8 *eid, struct smc_init_info *ini)
Ursula Braun3d9725a2020-09-10 18:48:23 +0200981{
982 struct smc_connection *conn = &smc->conn;
Ursula Brauna7c9c5f2020-09-26 12:44:30 +0200983 struct smc_clc_msg_accept_confirm *clc;
Ursula Braunb81a5eb2020-09-26 12:44:31 +0200984 struct smc_clc_first_contact_ext fce;
Karsten Graule5c47442021-10-16 11:37:45 +0200985 struct smc_clc_fce_gid_ext gle;
Ursula Braune15c6c42020-09-26 12:44:22 +0200986 struct smc_clc_msg_trail trl;
Karsten Graule5c47442021-10-16 11:37:45 +0200987 struct kvec vec[5];
Ursula Braun3d9725a2020-09-10 18:48:23 +0200988 struct msghdr msg;
Ursula Braunb81a5eb2020-09-26 12:44:31 +0200989 int i, len;
Ursula Braun3d9725a2020-09-10 18:48:23 +0200990
991 /* send SMC Confirm CLC msg */
Ursula Brauna7c9c5f2020-09-26 12:44:30 +0200992 clc = (struct smc_clc_msg_accept_confirm *)clc_v2;
993 clc->hdr.version = version; /* SMC version */
Ursula Braun3d9725a2020-09-10 18:48:23 +0200994 if (first_contact)
Ursula Braunf1eb02f2020-09-26 12:44:20 +0200995 clc->hdr.typev2 |= SMC_FIRST_CONTACT_MASK;
Ursula Braun3d9725a2020-09-10 18:48:23 +0200996 if (conn->lgr->is_smcd) {
997 /* SMC-D specific settings */
998 memcpy(clc->hdr.eyecatcher, SMCD_EYECATCHER,
999 sizeof(SMCD_EYECATCHER));
Ursula Braunf1eb02f2020-09-26 12:44:20 +02001000 clc->hdr.typev1 = SMC_TYPE_D;
Ursula Braun3d9725a2020-09-10 18:48:23 +02001001 clc->d0.gid = conn->lgr->smcd->local_gid;
1002 clc->d0.token = conn->rmb_desc->token;
1003 clc->d0.dmbe_size = conn->rmbe_size_short;
1004 clc->d0.dmbe_idx = 0;
1005 memcpy(&clc->d0.linkid, conn->lgr->id, SMC_LGR_ID_SIZE);
Ursula Brauna7c9c5f2020-09-26 12:44:30 +02001006 if (version == SMC_V1) {
1007 clc->hdr.length = htons(SMCD_CLC_ACCEPT_CONFIRM_LEN);
1008 } else {
Karsten Graule5c47442021-10-16 11:37:45 +02001009 clc_v2->d1.chid =
1010 htons(smc_ism_get_chid(conn->lgr->smcd));
1011 if (eid && eid[0])
1012 memcpy(clc_v2->d1.eid, eid, SMC_MAX_EID_LEN);
Ursula Braunb81a5eb2020-09-26 12:44:31 +02001013 len = SMCD_CLC_ACCEPT_CONFIRM_LEN_V2;
1014 if (first_contact)
1015 smc_clc_fill_fce(&fce, &len);
1016 clc_v2->hdr.length = htons(len);
Ursula Brauna7c9c5f2020-09-26 12:44:30 +02001017 }
Ursula Braune15c6c42020-09-26 12:44:22 +02001018 memcpy(trl.eyecatcher, SMCD_EYECATCHER,
Ursula Braun3d9725a2020-09-10 18:48:23 +02001019 sizeof(SMCD_EYECATCHER));
1020 } else {
1021 struct smc_link *link = conn->lnk;
1022
1023 /* SMC-R specific settings */
Ursula Braun3d9725a2020-09-10 18:48:23 +02001024 memcpy(clc->hdr.eyecatcher, SMC_EYECATCHER,
1025 sizeof(SMC_EYECATCHER));
Ursula Braunf1eb02f2020-09-26 12:44:20 +02001026 clc->hdr.typev1 = SMC_TYPE_R;
Ursula Braun3d9725a2020-09-10 18:48:23 +02001027 clc->hdr.length = htons(SMCR_CLC_ACCEPT_CONFIRM_LEN);
1028 memcpy(clc->r0.lcl.id_for_peer, local_systemid,
1029 sizeof(local_systemid));
1030 memcpy(&clc->r0.lcl.gid, link->gid, SMC_GID_SIZE);
1031 memcpy(&clc->r0.lcl.mac, &link->smcibdev->mac[link->ibport - 1],
1032 ETH_ALEN);
1033 hton24(clc->r0.qpn, link->roce_qp->qp_num);
1034 clc->r0.rmb_rkey =
1035 htonl(conn->rmb_desc->mr_rx[link->link_idx]->rkey);
1036 clc->r0.rmbe_idx = 1; /* for now: 1 RMB = 1 RMBE */
1037 clc->r0.rmbe_alert_token = htonl(conn->alert_token_local);
1038 switch (clc->hdr.type) {
1039 case SMC_CLC_ACCEPT:
1040 clc->r0.qp_mtu = link->path_mtu;
1041 break;
1042 case SMC_CLC_CONFIRM:
1043 clc->r0.qp_mtu = min(link->path_mtu, link->peer_mtu);
1044 break;
1045 }
1046 clc->r0.rmbe_size = conn->rmbe_size_short;
1047 clc->r0.rmb_dma_addr = cpu_to_be64((u64)sg_dma_address
1048 (conn->rmb_desc->sgt[link->link_idx].sgl));
1049 hton24(clc->r0.psn, link->psn_initial);
Karsten Graule5c47442021-10-16 11:37:45 +02001050 if (version == SMC_V1) {
1051 clc->hdr.length = htons(SMCR_CLC_ACCEPT_CONFIRM_LEN);
1052 } else {
1053 if (eid && eid[0])
1054 memcpy(clc_v2->r1.eid, eid, SMC_MAX_EID_LEN);
1055 len = SMCR_CLC_ACCEPT_CONFIRM_LEN_V2;
1056 if (first_contact) {
1057 smc_clc_fill_fce(&fce, &len);
1058 fce.v2_direct = !link->lgr->uses_gateway;
1059 memset(&gle, 0, sizeof(gle));
1060 if (ini && clc->hdr.type == SMC_CLC_CONFIRM) {
1061 gle.gid_cnt = ini->smcrv2.gidlist.len;
1062 len += sizeof(gle);
1063 len += gle.gid_cnt * sizeof(gle.gid[0]);
1064 } else {
1065 len += sizeof(gle.reserved);
1066 }
1067 }
1068 clc_v2->hdr.length = htons(len);
1069 }
Ursula Braune15c6c42020-09-26 12:44:22 +02001070 memcpy(trl.eyecatcher, SMC_EYECATCHER, sizeof(SMC_EYECATCHER));
Ursula Braun3d9725a2020-09-10 18:48:23 +02001071 }
1072
1073 memset(&msg, 0, sizeof(msg));
Ursula Braune15c6c42020-09-26 12:44:22 +02001074 i = 0;
Ursula Brauna7c9c5f2020-09-26 12:44:30 +02001075 vec[i].iov_base = clc_v2;
1076 if (version > SMC_V1)
Karsten Graule5c47442021-10-16 11:37:45 +02001077 vec[i++].iov_len = (clc->hdr.typev1 == SMC_TYPE_D ?
1078 SMCD_CLC_ACCEPT_CONFIRM_LEN_V2 :
1079 SMCR_CLC_ACCEPT_CONFIRM_LEN_V2) -
1080 sizeof(trl);
Ursula Brauna7c9c5f2020-09-26 12:44:30 +02001081 else
1082 vec[i++].iov_len = (clc->hdr.typev1 == SMC_TYPE_D ?
1083 SMCD_CLC_ACCEPT_CONFIRM_LEN :
1084 SMCR_CLC_ACCEPT_CONFIRM_LEN) -
1085 sizeof(trl);
Ursula Braunb81a5eb2020-09-26 12:44:31 +02001086 if (version > SMC_V1 && first_contact) {
1087 vec[i].iov_base = &fce;
1088 vec[i++].iov_len = sizeof(fce);
Karsten Graule5c47442021-10-16 11:37:45 +02001089 if (!conn->lgr->is_smcd) {
1090 if (clc->hdr.type == SMC_CLC_CONFIRM) {
1091 vec[i].iov_base = &gle;
1092 vec[i++].iov_len = sizeof(gle);
1093 vec[i].iov_base = &ini->smcrv2.gidlist.list;
1094 vec[i++].iov_len = gle.gid_cnt *
1095 sizeof(gle.gid[0]);
1096 } else {
1097 vec[i].iov_base = &gle.reserved;
1098 vec[i++].iov_len = sizeof(gle.reserved);
1099 }
1100 }
Ursula Braunb81a5eb2020-09-26 12:44:31 +02001101 }
Ursula Braune15c6c42020-09-26 12:44:22 +02001102 vec[i].iov_base = &trl;
1103 vec[i++].iov_len = sizeof(trl);
1104 return kernel_sendmsg(smc->clcsock, &msg, vec, 1,
Ursula Braun3d9725a2020-09-10 18:48:23 +02001105 ntohs(clc->hdr.length));
1106}
1107
Ursula Brauna046d572017-01-09 16:55:16 +01001108/* send CLC CONFIRM message across internal TCP socket */
Ursula Brauna7c9c5f2020-09-26 12:44:30 +02001109int smc_clc_send_confirm(struct smc_sock *smc, bool clnt_first_contact,
Karsten Graule5c47442021-10-16 11:37:45 +02001110 u8 version, u8 *eid, struct smc_init_info *ini)
Ursula Brauna046d572017-01-09 16:55:16 +01001111{
Ursula Brauna7c9c5f2020-09-26 12:44:30 +02001112 struct smc_clc_msg_accept_confirm_v2 cclc_v2;
Ursula Brauna046d572017-01-09 16:55:16 +01001113 int reason_code = 0;
Ursula Brauna046d572017-01-09 16:55:16 +01001114 int len;
1115
1116 /* send SMC Confirm CLC msg */
Ursula Brauna7c9c5f2020-09-26 12:44:30 +02001117 memset(&cclc_v2, 0, sizeof(cclc_v2));
1118 cclc_v2.hdr.type = SMC_CLC_CONFIRM;
1119 len = smc_clc_send_confirm_accept(smc, &cclc_v2, clnt_first_contact,
Karsten Graule5c47442021-10-16 11:37:45 +02001120 version, eid, ini);
Ursula Brauna7c9c5f2020-09-26 12:44:30 +02001121 if (len < ntohs(cclc_v2.hdr.length)) {
Ursula Brauna046d572017-01-09 16:55:16 +01001122 if (len >= 0) {
1123 reason_code = -ENETUNREACH;
1124 smc->sk.sk_err = -reason_code;
1125 } else {
1126 smc->sk.sk_err = smc->clcsock->sk->sk_err;
1127 reason_code = -smc->sk.sk_err;
1128 }
1129 }
1130 return reason_code;
1131}
1132
1133/* send CLC ACCEPT message across internal TCP socket */
Ursula Brauna7c9c5f2020-09-26 12:44:30 +02001134int smc_clc_send_accept(struct smc_sock *new_smc, bool srv_first_contact,
Karsten Graulfa086662021-09-14 10:35:05 +02001135 u8 version, u8 *negotiated_eid)
Ursula Brauna046d572017-01-09 16:55:16 +01001136{
Ursula Brauna7c9c5f2020-09-26 12:44:30 +02001137 struct smc_clc_msg_accept_confirm_v2 aclc_v2;
Ursula Brauna046d572017-01-09 16:55:16 +01001138 int len;
1139
Ursula Brauna7c9c5f2020-09-26 12:44:30 +02001140 memset(&aclc_v2, 0, sizeof(aclc_v2));
1141 aclc_v2.hdr.type = SMC_CLC_ACCEPT;
1142 len = smc_clc_send_confirm_accept(new_smc, &aclc_v2, srv_first_contact,
Karsten Graule5c47442021-10-16 11:37:45 +02001143 version, negotiated_eid, NULL);
Ursula Brauna7c9c5f2020-09-26 12:44:30 +02001144 if (len < ntohs(aclc_v2.hdr.length))
Ursula Braun6ae36bf2018-11-22 10:26:36 +01001145 len = len >= 0 ? -EPROTO : -new_smc->clcsock->sk->sk_err;
Ursula Brauna046d572017-01-09 16:55:16 +01001146
Ursula Braun6ae36bf2018-11-22 10:26:36 +01001147 return len > 0 ? 0 : len;
Ursula Brauna046d572017-01-09 16:55:16 +01001148}
Ursula Braunb81a5eb2020-09-26 12:44:31 +02001149
Guvenc Gulce099b9902020-12-01 20:20:44 +01001150void smc_clc_get_hostname(u8 **host)
1151{
1152 *host = &smc_hostname[0];
1153}
1154
Ursula Braunb81a5eb2020-09-26 12:44:31 +02001155void __init smc_clc_init(void)
1156{
1157 struct new_utsname *u;
1158
1159 memset(smc_hostname, _S, sizeof(smc_hostname)); /* ASCII blanks */
1160 u = utsname();
1161 memcpy(smc_hostname, u->nodename,
1162 min_t(size_t, strlen(u->nodename), sizeof(smc_hostname)));
Karsten Graulfa086662021-09-14 10:35:05 +02001163
1164 INIT_LIST_HEAD(&smc_clc_eid_table.list);
1165 rwlock_init(&smc_clc_eid_table.lock);
1166 smc_clc_eid_table.ueid_cnt = 0;
1167 smc_clc_eid_table.seid_enabled = 1;
1168}
1169
1170void smc_clc_exit(void)
1171{
1172 smc_clc_ueid_remove(NULL);
Ursula Braunb81a5eb2020-09-26 12:44:31 +02001173}