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