blob: fed101454217d0c6c3c871e2020e4b22aa8cde2f [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>
17
Karsten Graul1a26d022018-03-16 15:06:40 +010018#include <net/addrconf.h>
Ursula Brauna046d572017-01-09 16:55:16 +010019#include <net/sock.h>
20#include <net/tcp.h>
21
22#include "smc.h"
Ursula Braun0cfdd8f2017-01-09 16:55:17 +010023#include "smc_core.h"
Ursula Brauna046d572017-01-09 16:55:16 +010024#include "smc_clc.h"
25#include "smc_ib.h"
Hans Wippelc758dfd2018-06-28 19:05:09 +020026#include "smc_ism.h"
27
28#define SMCR_CLC_ACCEPT_CONFIRM_LEN 68
29#define SMCD_CLC_ACCEPT_CONFIRM_LEN 48
Ursula Braunfb4f7922020-07-08 17:05:15 +020030#define SMC_CLC_RECV_BUF_LEN 100
Ursula Brauna046d572017-01-09 16:55:16 +010031
Stefan Raspl0f627122018-03-01 13:51:26 +010032/* eye catcher "SMCR" EBCDIC for CLC messages */
33static const char SMC_EYECATCHER[4] = {'\xe2', '\xd4', '\xc3', '\xd9'};
Hans Wippelc758dfd2018-06-28 19:05:09 +020034/* eye catcher "SMCD" EBCDIC for CLC messages */
35static const char SMCD_EYECATCHER[4] = {'\xe2', '\xd4', '\xc3', '\xc4'};
Stefan Raspl0f627122018-03-01 13:51:26 +010036
Ursula Braun8c3dca32020-09-26 12:44:28 +020037/* check arriving CLC proposal */
38static bool smc_clc_msg_prop_valid(struct smc_clc_msg_proposal *pclc)
39{
40 struct smc_clc_msg_proposal_prefix *pclc_prfx;
41 struct smc_clc_smcd_v2_extension *smcd_v2_ext;
42 struct smc_clc_msg_hdr *hdr = &pclc->hdr;
43 struct smc_clc_v2_extension *v2_ext;
44
45 v2_ext = smc_get_clc_v2_ext(pclc);
46 pclc_prfx = smc_clc_proposal_get_prefix(pclc);
47 if (hdr->version == SMC_V1) {
48 if (hdr->typev1 == SMC_TYPE_N)
49 return false;
50 if (ntohs(hdr->length) !=
51 sizeof(*pclc) + ntohs(pclc->iparea_offset) +
52 sizeof(*pclc_prfx) +
53 pclc_prfx->ipv6_prefixes_cnt *
54 sizeof(struct smc_clc_ipv6_prefix) +
55 sizeof(struct smc_clc_msg_trail))
56 return false;
57 } else {
58 if (ntohs(hdr->length) !=
59 sizeof(*pclc) +
60 sizeof(struct smc_clc_msg_smcd) +
61 (hdr->typev1 != SMC_TYPE_N ?
62 sizeof(*pclc_prfx) +
63 pclc_prfx->ipv6_prefixes_cnt *
64 sizeof(struct smc_clc_ipv6_prefix) : 0) +
65 (hdr->typev2 != SMC_TYPE_N ?
66 sizeof(*v2_ext) +
67 v2_ext->hdr.eid_cnt * SMC_MAX_EID_LEN : 0) +
68 (smcd_indicated(hdr->typev2) ?
69 sizeof(*smcd_v2_ext) + v2_ext->hdr.ism_gid_cnt *
70 sizeof(struct smc_clc_smcd_gid_chid) :
71 0) +
72 sizeof(struct smc_clc_msg_trail))
73 return false;
74 }
75 return true;
76}
77
Ursula Braune7b7a642017-12-07 13:38:49 +010078/* check if received message has a correct header length and contains valid
79 * heading and trailing eyecatchers
80 */
Ursula Braunfb4f7922020-07-08 17:05:15 +020081static bool smc_clc_msg_hdr_valid(struct smc_clc_msg_hdr *clcm, bool check_trl)
Ursula Braune7b7a642017-12-07 13:38:49 +010082{
Ursula Braune7b7a642017-12-07 13:38:49 +010083 struct smc_clc_msg_accept_confirm *clc;
84 struct smc_clc_msg_proposal *pclc;
85 struct smc_clc_msg_decline *dclc;
86 struct smc_clc_msg_trail *trl;
87
Hans Wippelc758dfd2018-06-28 19:05:09 +020088 if (memcmp(clcm->eyecatcher, SMC_EYECATCHER, sizeof(SMC_EYECATCHER)) &&
89 memcmp(clcm->eyecatcher, SMCD_EYECATCHER, sizeof(SMCD_EYECATCHER)))
Ursula Braune7b7a642017-12-07 13:38:49 +010090 return false;
91 switch (clcm->type) {
92 case SMC_CLC_PROPOSAL:
93 pclc = (struct smc_clc_msg_proposal *)clcm;
Ursula Braun8c3dca32020-09-26 12:44:28 +020094 if (!smc_clc_msg_prop_valid(pclc))
Ursula Braune7b7a642017-12-07 13:38:49 +010095 return false;
96 trl = (struct smc_clc_msg_trail *)
97 ((u8 *)pclc + ntohs(pclc->hdr.length) - sizeof(*trl));
98 break;
99 case SMC_CLC_ACCEPT:
100 case SMC_CLC_CONFIRM:
Ursula Braunf1eb02f2020-09-26 12:44:20 +0200101 if (clcm->typev1 != SMC_TYPE_R && clcm->typev1 != SMC_TYPE_D)
Ursula Braune7b7a642017-12-07 13:38:49 +0100102 return false;
Hans Wippelc758dfd2018-06-28 19:05:09 +0200103 clc = (struct smc_clc_msg_accept_confirm *)clcm;
Ursula Braunf1eb02f2020-09-26 12:44:20 +0200104 if ((clcm->typev1 == SMC_TYPE_R &&
Hans Wippelc758dfd2018-06-28 19:05:09 +0200105 ntohs(clc->hdr.length) != SMCR_CLC_ACCEPT_CONFIRM_LEN) ||
Ursula Braunf1eb02f2020-09-26 12:44:20 +0200106 (clcm->typev1 == SMC_TYPE_D &&
Hans Wippelc758dfd2018-06-28 19:05:09 +0200107 ntohs(clc->hdr.length) != SMCD_CLC_ACCEPT_CONFIRM_LEN))
108 return false;
109 trl = (struct smc_clc_msg_trail *)
110 ((u8 *)clc + ntohs(clc->hdr.length) - sizeof(*trl));
Ursula Braune7b7a642017-12-07 13:38:49 +0100111 break;
112 case SMC_CLC_DECLINE:
113 dclc = (struct smc_clc_msg_decline *)clcm;
114 if (ntohs(dclc->hdr.length) != sizeof(*dclc))
115 return false;
116 trl = &dclc->trl;
117 break;
118 default:
119 return false;
120 }
Ursula Braunfb4f7922020-07-08 17:05:15 +0200121 if (check_trl &&
122 memcmp(trl->eyecatcher, SMC_EYECATCHER, sizeof(SMC_EYECATCHER)) &&
Hans Wippelc758dfd2018-06-28 19:05:09 +0200123 memcmp(trl->eyecatcher, SMCD_EYECATCHER, sizeof(SMCD_EYECATCHER)))
Ursula Braune7b7a642017-12-07 13:38:49 +0100124 return false;
125 return true;
126}
127
Karsten Graulc246d942018-03-16 15:06:39 +0100128/* find ipv4 addr on device and get the prefix len, fill CLC proposal msg */
129static int smc_clc_prfx_set4_rcu(struct dst_entry *dst, __be32 ipv4,
130 struct smc_clc_msg_proposal_prefix *prop)
131{
132 struct in_device *in_dev = __in_dev_get_rcu(dst->dev);
Florian Westphalcd5a4112019-05-31 18:27:07 +0200133 const struct in_ifaddr *ifa;
Karsten Graulc246d942018-03-16 15:06:39 +0100134
135 if (!in_dev)
136 return -ENODEV;
Florian Westphalcd5a4112019-05-31 18:27:07 +0200137
138 in_dev_for_each_ifa_rcu(ifa, in_dev) {
Karsten Graulc246d942018-03-16 15:06:39 +0100139 if (!inet_ifa_match(ipv4, ifa))
140 continue;
141 prop->prefix_len = inet_mask_len(ifa->ifa_mask);
142 prop->outgoing_subnet = ifa->ifa_address & ifa->ifa_mask;
143 /* prop->ipv6_prefixes_cnt = 0; already done by memset before */
144 return 0;
Florian Westphalcd5a4112019-05-31 18:27:07 +0200145 }
Karsten Graulc246d942018-03-16 15:06:39 +0100146 return -ENOENT;
147}
148
Karsten Graul1a26d022018-03-16 15:06:40 +0100149/* fill CLC proposal msg with ipv6 prefixes from device */
150static int smc_clc_prfx_set6_rcu(struct dst_entry *dst,
151 struct smc_clc_msg_proposal_prefix *prop,
152 struct smc_clc_ipv6_prefix *ipv6_prfx)
153{
154#if IS_ENABLED(CONFIG_IPV6)
155 struct inet6_dev *in6_dev = __in6_dev_get(dst->dev);
156 struct inet6_ifaddr *ifa;
157 int cnt = 0;
158
159 if (!in6_dev)
160 return -ENODEV;
161 /* use a maximum of 8 IPv6 prefixes from device */
162 list_for_each_entry(ifa, &in6_dev->addr_list, if_list) {
163 if (ipv6_addr_type(&ifa->addr) & IPV6_ADDR_LINKLOCAL)
164 continue;
165 ipv6_addr_prefix(&ipv6_prfx[cnt].prefix,
166 &ifa->addr, ifa->prefix_len);
167 ipv6_prfx[cnt].prefix_len = ifa->prefix_len;
168 cnt++;
169 if (cnt == SMC_CLC_MAX_V6_PREFIX)
170 break;
171 }
172 prop->ipv6_prefixes_cnt = cnt;
173 if (cnt)
174 return 0;
175#endif
176 return -ENOENT;
177}
178
Karsten Graulc246d942018-03-16 15:06:39 +0100179/* retrieve and set prefixes in CLC proposal msg */
180static int smc_clc_prfx_set(struct socket *clcsock,
Karsten Graul1a26d022018-03-16 15:06:40 +0100181 struct smc_clc_msg_proposal_prefix *prop,
182 struct smc_clc_ipv6_prefix *ipv6_prfx)
Karsten Graul696cd302018-03-01 13:51:27 +0100183{
184 struct dst_entry *dst = sk_dst_get(clcsock->sk);
Karsten Graulc246d942018-03-16 15:06:39 +0100185 struct sockaddr_storage addrs;
Karsten Graul1a26d022018-03-16 15:06:40 +0100186 struct sockaddr_in6 *addr6;
Karsten Graulc246d942018-03-16 15:06:39 +0100187 struct sockaddr_in *addr;
188 int rc = -ENOENT;
189
Karsten Graulc246d942018-03-16 15:06:39 +0100190 if (!dst) {
191 rc = -ENOTCONN;
192 goto out;
193 }
194 if (!dst->dev) {
195 rc = -ENODEV;
196 goto out_rel;
197 }
198 /* get address to which the internal TCP socket is bound */
199 kernel_getsockname(clcsock, (struct sockaddr *)&addrs);
200 /* analyze IP specific data of net_device belonging to TCP socket */
Karsten Graul1a26d022018-03-16 15:06:40 +0100201 addr6 = (struct sockaddr_in6 *)&addrs;
Karsten Graulc246d942018-03-16 15:06:39 +0100202 rcu_read_lock();
203 if (addrs.ss_family == PF_INET) {
204 /* IPv4 */
205 addr = (struct sockaddr_in *)&addrs;
206 rc = smc_clc_prfx_set4_rcu(dst, addr->sin_addr.s_addr, prop);
Karsten Graul1a26d022018-03-16 15:06:40 +0100207 } else if (ipv6_addr_v4mapped(&addr6->sin6_addr)) {
208 /* mapped IPv4 address - peer is IPv4 only */
209 rc = smc_clc_prfx_set4_rcu(dst, addr6->sin6_addr.s6_addr32[3],
210 prop);
211 } else {
212 /* IPv6 */
213 rc = smc_clc_prfx_set6_rcu(dst, prop, ipv6_prfx);
Karsten Graulc246d942018-03-16 15:06:39 +0100214 }
215 rcu_read_unlock();
216out_rel:
217 dst_release(dst);
218out:
219 return rc;
220}
221
222/* match ipv4 addrs of dev against addr in CLC proposal */
223static int smc_clc_prfx_match4_rcu(struct net_device *dev,
224 struct smc_clc_msg_proposal_prefix *prop)
225{
226 struct in_device *in_dev = __in_dev_get_rcu(dev);
Florian Westphalcd5a4112019-05-31 18:27:07 +0200227 const struct in_ifaddr *ifa;
Karsten Graulc246d942018-03-16 15:06:39 +0100228
229 if (!in_dev)
230 return -ENODEV;
Florian Westphalcd5a4112019-05-31 18:27:07 +0200231 in_dev_for_each_ifa_rcu(ifa, in_dev) {
Karsten Graulc246d942018-03-16 15:06:39 +0100232 if (prop->prefix_len == inet_mask_len(ifa->ifa_mask) &&
233 inet_ifa_match(prop->outgoing_subnet, ifa))
234 return 0;
Florian Westphalcd5a4112019-05-31 18:27:07 +0200235 }
Karsten Graulc246d942018-03-16 15:06:39 +0100236
237 return -ENOENT;
238}
239
Karsten Graul1a26d022018-03-16 15:06:40 +0100240/* match ipv6 addrs of dev against addrs in CLC proposal */
241static int smc_clc_prfx_match6_rcu(struct net_device *dev,
242 struct smc_clc_msg_proposal_prefix *prop)
243{
244#if IS_ENABLED(CONFIG_IPV6)
245 struct inet6_dev *in6_dev = __in6_dev_get(dev);
246 struct smc_clc_ipv6_prefix *ipv6_prfx;
247 struct inet6_ifaddr *ifa;
248 int i, max;
249
250 if (!in6_dev)
251 return -ENODEV;
252 /* ipv6 prefix list starts behind smc_clc_msg_proposal_prefix */
253 ipv6_prfx = (struct smc_clc_ipv6_prefix *)((u8 *)prop + sizeof(*prop));
254 max = min_t(u8, prop->ipv6_prefixes_cnt, SMC_CLC_MAX_V6_PREFIX);
255 list_for_each_entry(ifa, &in6_dev->addr_list, if_list) {
256 if (ipv6_addr_type(&ifa->addr) & IPV6_ADDR_LINKLOCAL)
257 continue;
258 for (i = 0; i < max; i++) {
259 if (ifa->prefix_len == ipv6_prfx[i].prefix_len &&
260 ipv6_prefix_equal(&ifa->addr, &ipv6_prfx[i].prefix,
261 ifa->prefix_len))
262 return 0;
263 }
264 }
265#endif
266 return -ENOENT;
267}
268
Karsten Graulc246d942018-03-16 15:06:39 +0100269/* check if proposed prefixes match one of our device prefixes */
270int smc_clc_prfx_match(struct socket *clcsock,
271 struct smc_clc_msg_proposal_prefix *prop)
272{
273 struct dst_entry *dst = sk_dst_get(clcsock->sk);
Karsten Graul1a26d022018-03-16 15:06:40 +0100274 int rc;
Karsten Graul696cd302018-03-01 13:51:27 +0100275
276 if (!dst) {
277 rc = -ENOTCONN;
278 goto out;
279 }
280 if (!dst->dev) {
281 rc = -ENODEV;
282 goto out_rel;
283 }
Karsten Graul696cd302018-03-01 13:51:27 +0100284 rcu_read_lock();
Karsten Graulc246d942018-03-16 15:06:39 +0100285 if (!prop->ipv6_prefixes_cnt)
286 rc = smc_clc_prfx_match4_rcu(dst->dev, prop);
Karsten Graul1a26d022018-03-16 15:06:40 +0100287 else
288 rc = smc_clc_prfx_match6_rcu(dst->dev, prop);
Karsten Graul696cd302018-03-01 13:51:27 +0100289 rcu_read_unlock();
Karsten Graul696cd302018-03-01 13:51:27 +0100290out_rel:
291 dst_release(dst);
292out:
293 return rc;
294}
295
Ursula Brauna046d572017-01-09 16:55:16 +0100296/* Wait for data on the tcp-socket, analyze received data
297 * Returns:
298 * 0 if success and it was not a decline that we received.
299 * SMC_CLC_DECL_REPLY if decline received for fallback w/o another decl send.
300 * clcsock error, -EINTR, -ECONNRESET, -EPROTO otherwise.
301 */
302int smc_clc_wait_msg(struct smc_sock *smc, void *buf, int buflen,
Ursula Braun2b59f582018-11-22 10:26:39 +0100303 u8 expected_type, unsigned long timeout)
Ursula Brauna046d572017-01-09 16:55:16 +0100304{
Karsten Graulf6bdc422018-07-18 15:22:51 +0200305 long rcvtimeo = smc->clcsock->sk->sk_rcvtimeo;
Ursula Brauna046d572017-01-09 16:55:16 +0100306 struct sock *clc_sk = smc->clcsock->sk;
307 struct smc_clc_msg_hdr *clcm = buf;
308 struct msghdr msg = {NULL, 0};
309 int reason_code = 0;
Al Virod63d2712017-09-20 20:21:22 -0400310 struct kvec vec = {buf, buflen};
Ursula Braunfb4f7922020-07-08 17:05:15 +0200311 int len, datlen, recvlen;
312 bool check_trl = true;
Ursula Brauna046d572017-01-09 16:55:16 +0100313 int krflags;
314
315 /* peek the first few bytes to determine length of data to receive
316 * so we don't consume any subsequent CLC message or payload data
317 * in the TCP byte stream
318 */
Al Virod63d2712017-09-20 20:21:22 -0400319 /*
320 * Caller must make sure that buflen is no less than
321 * sizeof(struct smc_clc_msg_hdr)
322 */
Ursula Brauna046d572017-01-09 16:55:16 +0100323 krflags = MSG_PEEK | MSG_WAITALL;
Ursula Braun2b59f582018-11-22 10:26:39 +0100324 clc_sk->sk_rcvtimeo = timeout;
David Howellsaa563d72018-10-20 00:57:56 +0100325 iov_iter_kvec(&msg.msg_iter, READ, &vec, 1,
Al Virod63d2712017-09-20 20:21:22 -0400326 sizeof(struct smc_clc_msg_hdr));
327 len = sock_recvmsg(smc->clcsock, &msg, krflags);
Ursula Brauna046d572017-01-09 16:55:16 +0100328 if (signal_pending(current)) {
329 reason_code = -EINTR;
330 clc_sk->sk_err = EINTR;
331 smc->sk.sk_err = EINTR;
332 goto out;
333 }
334 if (clc_sk->sk_err) {
335 reason_code = -clc_sk->sk_err;
Ursula Braun9ed28552018-11-22 10:26:37 +0100336 if (clc_sk->sk_err == EAGAIN &&
337 expected_type == SMC_CLC_DECLINE)
338 clc_sk->sk_err = 0; /* reset for fallback usage */
339 else
340 smc->sk.sk_err = clc_sk->sk_err;
Ursula Brauna046d572017-01-09 16:55:16 +0100341 goto out;
342 }
343 if (!len) { /* peer has performed orderly shutdown */
344 smc->sk.sk_err = ECONNRESET;
345 reason_code = -ECONNRESET;
346 goto out;
347 }
348 if (len < 0) {
Ursula Braun9ed28552018-11-22 10:26:37 +0100349 if (len != -EAGAIN || expected_type != SMC_CLC_DECLINE)
350 smc->sk.sk_err = -len;
Ursula Brauna046d572017-01-09 16:55:16 +0100351 reason_code = len;
352 goto out;
353 }
354 datlen = ntohs(clcm->length);
355 if ((len < sizeof(struct smc_clc_msg_hdr)) ||
Ursula Braun5ac54d82020-09-10 18:48:21 +0200356 (clcm->version < SMC_V1) ||
Ursula Brauna046d572017-01-09 16:55:16 +0100357 ((clcm->type != SMC_CLC_DECLINE) &&
358 (clcm->type != expected_type))) {
359 smc->sk.sk_err = EPROTO;
360 reason_code = -EPROTO;
361 goto out;
362 }
363
364 /* receive the complete CLC message */
Ursula Brauna046d572017-01-09 16:55:16 +0100365 memset(&msg, 0, sizeof(struct msghdr));
Ursula Braunfb4f7922020-07-08 17:05:15 +0200366 if (datlen > buflen) {
367 check_trl = false;
368 recvlen = buflen;
369 } else {
370 recvlen = datlen;
371 }
372 iov_iter_kvec(&msg.msg_iter, READ, &vec, 1, recvlen);
Ursula Brauna046d572017-01-09 16:55:16 +0100373 krflags = MSG_WAITALL;
Al Virod63d2712017-09-20 20:21:22 -0400374 len = sock_recvmsg(smc->clcsock, &msg, krflags);
Ursula Braunfb4f7922020-07-08 17:05:15 +0200375 if (len < recvlen || !smc_clc_msg_hdr_valid(clcm, check_trl)) {
Ursula Brauna046d572017-01-09 16:55:16 +0100376 smc->sk.sk_err = EPROTO;
377 reason_code = -EPROTO;
378 goto out;
379 }
Ursula Braunfb4f7922020-07-08 17:05:15 +0200380 datlen -= len;
381 while (datlen) {
382 u8 tmp[SMC_CLC_RECV_BUF_LEN];
383
384 vec.iov_base = &tmp;
385 vec.iov_len = SMC_CLC_RECV_BUF_LEN;
386 /* receive remaining proposal message */
387 recvlen = datlen > SMC_CLC_RECV_BUF_LEN ?
388 SMC_CLC_RECV_BUF_LEN : datlen;
389 iov_iter_kvec(&msg.msg_iter, READ, &vec, 1, recvlen);
390 len = sock_recvmsg(smc->clcsock, &msg, krflags);
391 datlen -= len;
392 }
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100393 if (clcm->type == SMC_CLC_DECLINE) {
Karsten Graul603cc142018-07-25 16:35:32 +0200394 struct smc_clc_msg_decline *dclc;
395
396 dclc = (struct smc_clc_msg_decline *)clcm;
397 reason_code = SMC_CLC_DECL_PEERDECL;
398 smc->peer_diagnosis = ntohl(dclc->peer_diagnosis);
Ursula Braunf1eb02f2020-09-26 12:44:20 +0200399 if (((struct smc_clc_msg_decline *)buf)->hdr.typev2 &
400 SMC_FIRST_CONTACT_MASK) {
Karsten Graul517c3002018-05-15 17:05:03 +0200401 smc->conn.lgr->sync_err = 1;
Karsten Graul5f78fe92020-02-17 16:24:54 +0100402 smc_lgr_terminate_sched(smc->conn.lgr);
Ursula Braunbfbedfd2017-09-21 09:16:32 +0200403 }
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100404 }
405
Ursula Brauna046d572017-01-09 16:55:16 +0100406out:
Ursula Braun2b59f582018-11-22 10:26:39 +0100407 clc_sk->sk_rcvtimeo = rcvtimeo;
Ursula Brauna046d572017-01-09 16:55:16 +0100408 return reason_code;
409}
410
411/* send CLC DECLINE message across internal TCP socket */
Ursula Braunbfbedfd2017-09-21 09:16:32 +0200412int smc_clc_send_decline(struct smc_sock *smc, u32 peer_diag_info)
Ursula Brauna046d572017-01-09 16:55:16 +0100413{
414 struct smc_clc_msg_decline dclc;
415 struct msghdr msg;
416 struct kvec vec;
417 int len;
418
419 memset(&dclc, 0, sizeof(dclc));
420 memcpy(dclc.hdr.eyecatcher, SMC_EYECATCHER, sizeof(SMC_EYECATCHER));
421 dclc.hdr.type = SMC_CLC_DECLINE;
422 dclc.hdr.length = htons(sizeof(struct smc_clc_msg_decline));
Ursula Braun5ac54d82020-09-10 18:48:21 +0200423 dclc.hdr.version = SMC_V1;
Ursula Braunf1eb02f2020-09-26 12:44:20 +0200424 dclc.hdr.typev2 = (peer_diag_info == SMC_CLC_DECL_SYNCERR) ?
425 SMC_FIRST_CONTACT_MASK : 0;
Hans Wippela082ec82020-02-25 22:41:22 +0100426 if ((!smc->conn.lgr || !smc->conn.lgr->is_smcd) &&
427 smc_ib_is_valid_local_systemid())
Ursula Braun369537c2020-02-14 08:59:00 +0100428 memcpy(dclc.id_for_peer, local_systemid,
429 sizeof(local_systemid));
Ursula Brauna046d572017-01-09 16:55:16 +0100430 dclc.peer_diagnosis = htonl(peer_diag_info);
431 memcpy(dclc.trl.eyecatcher, SMC_EYECATCHER, sizeof(SMC_EYECATCHER));
432
433 memset(&msg, 0, sizeof(msg));
434 vec.iov_base = &dclc;
435 vec.iov_len = sizeof(struct smc_clc_msg_decline);
436 len = kernel_sendmsg(smc->clcsock, &msg, &vec, 1,
437 sizeof(struct smc_clc_msg_decline));
Ursula Braun14d22d42019-01-30 18:51:00 +0100438 if (len < 0 || len < sizeof(struct smc_clc_msg_decline))
Ursula Braun6ae36bf2018-11-22 10:26:36 +0100439 len = -EPROTO;
440 return len > 0 ? 0 : len;
Ursula Brauna046d572017-01-09 16:55:16 +0100441}
442
443/* send CLC PROPOSAL message across internal TCP socket */
Ursula Braund70bf4f2020-09-26 12:44:27 +0200444int smc_clc_send_proposal(struct smc_sock *smc, struct smc_init_info *ini)
Ursula Brauna046d572017-01-09 16:55:16 +0100445{
Ursula Braun8c3dca32020-09-26 12:44:28 +0200446 struct smc_clc_smcd_v2_extension *smcd_v2_ext;
Ursula Braun6bb14e482020-09-10 18:48:22 +0200447 struct smc_clc_msg_proposal_prefix *pclc_prfx;
448 struct smc_clc_msg_proposal *pclc_base;
Ursula Braun8c3dca32020-09-26 12:44:28 +0200449 struct smc_clc_smcd_gid_chid *gidchids;
Ursula Braun6bb14e482020-09-10 18:48:22 +0200450 struct smc_clc_msg_proposal_area *pclc;
451 struct smc_clc_ipv6_prefix *ipv6_prfx;
Ursula Braun8c3dca32020-09-26 12:44:28 +0200452 struct smc_clc_v2_extension *v2_ext;
Ursula Braun6bb14e482020-09-10 18:48:22 +0200453 struct smc_clc_msg_smcd *pclc_smcd;
454 struct smc_clc_msg_trail *trl;
Karsten Graul1a26d022018-03-16 15:06:40 +0100455 int len, i, plen, rc;
Ursula Brauna046d572017-01-09 16:55:16 +0100456 int reason_code = 0;
Ursula Braun8c3dca32020-09-26 12:44:28 +0200457 struct kvec vec[8];
Ursula Brauna046d572017-01-09 16:55:16 +0100458 struct msghdr msg;
Ursula Brauna046d572017-01-09 16:55:16 +0100459
Ursula Braun6bb14e482020-09-10 18:48:22 +0200460 pclc = kzalloc(sizeof(*pclc), GFP_KERNEL);
461 if (!pclc)
462 return -ENOMEM;
463
464 pclc_base = &pclc->pclc_base;
465 pclc_smcd = &pclc->pclc_smcd;
466 pclc_prfx = &pclc->pclc_prfx;
467 ipv6_prfx = pclc->pclc_prfx_ipv6;
Ursula Braun8c3dca32020-09-26 12:44:28 +0200468 v2_ext = &pclc->pclc_v2_ext;
469 smcd_v2_ext = &pclc->pclc_smcd_v2_ext;
470 gidchids = pclc->pclc_gidchids;
Ursula Braun6bb14e482020-09-10 18:48:22 +0200471 trl = &pclc->pclc_trl;
472
Ursula Braun8c3dca32020-09-26 12:44:28 +0200473 pclc_base->hdr.version = SMC_V2;
474 pclc_base->hdr.typev1 = ini->smc_type_v1;
475 pclc_base->hdr.typev2 = ini->smc_type_v2;
476 plen = sizeof(*pclc_base) + sizeof(*pclc_smcd) + sizeof(*trl);
477
Karsten Graulc246d942018-03-16 15:06:39 +0100478 /* retrieve ip prefixes for CLC proposal msg */
Ursula Braun8c3dca32020-09-26 12:44:28 +0200479 if (ini->smc_type_v1 != SMC_TYPE_N) {
480 rc = smc_clc_prfx_set(smc->clcsock, pclc_prfx, ipv6_prfx);
481 if (rc) {
482 if (ini->smc_type_v2 == SMC_TYPE_N) {
483 kfree(pclc);
484 return SMC_CLC_DECL_CNFERR;
485 }
486 pclc_base->hdr.typev1 = SMC_TYPE_N;
487 } else {
488 pclc_base->iparea_offset = htons(sizeof(*pclc_smcd));
489 plen += sizeof(*pclc_prfx) +
490 pclc_prfx->ipv6_prefixes_cnt *
491 sizeof(ipv6_prfx[0]);
492 }
Ursula Braun6bb14e482020-09-10 18:48:22 +0200493 }
Karsten Graulc246d942018-03-16 15:06:39 +0100494
Ursula Braun8c3dca32020-09-26 12:44:28 +0200495 /* build SMC Proposal CLC message */
Ursula Braun6bb14e482020-09-10 18:48:22 +0200496 memcpy(pclc_base->hdr.eyecatcher, SMC_EYECATCHER,
497 sizeof(SMC_EYECATCHER));
498 pclc_base->hdr.type = SMC_CLC_PROPOSAL;
Ursula Braund70bf4f2020-09-26 12:44:27 +0200499 if (smcr_indicated(ini->smc_type_v1)) {
Hans Wippelc758dfd2018-06-28 19:05:09 +0200500 /* add SMC-R specifics */
Ursula Braun6bb14e482020-09-10 18:48:22 +0200501 memcpy(pclc_base->lcl.id_for_peer, local_systemid,
Hans Wippelc758dfd2018-06-28 19:05:09 +0200502 sizeof(local_systemid));
Ursula Braun6bb14e482020-09-10 18:48:22 +0200503 memcpy(pclc_base->lcl.gid, ini->ib_gid, SMC_GID_SIZE);
504 memcpy(pclc_base->lcl.mac, &ini->ib_dev->mac[ini->ib_port - 1],
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200505 ETH_ALEN);
Hans Wippelc758dfd2018-06-28 19:05:09 +0200506 }
Ursula Braund70bf4f2020-09-26 12:44:27 +0200507 if (smcd_indicated(ini->smc_type_v1)) {
Hans Wippelc758dfd2018-06-28 19:05:09 +0200508 /* add SMC-D specifics */
Ursula Braun8c3dca32020-09-26 12:44:28 +0200509 if (ini->ism_dev[0]) {
510 pclc_smcd->ism.gid = htonll(ini->ism_dev[0]->local_gid);
511 pclc_smcd->ism.chid =
512 htons(smc_ism_get_chid(ini->ism_dev[0]));
513 }
514 }
515 if (ini->smc_type_v2 == SMC_TYPE_N) {
516 pclc_smcd->v2_ext_offset = 0;
517 } else {
518 u16 v2_ext_offset;
519 u8 *eid = NULL;
520
521 v2_ext_offset = sizeof(*pclc_smcd) -
522 offsetofend(struct smc_clc_msg_smcd, v2_ext_offset);
523 if (ini->smc_type_v1 != SMC_TYPE_N)
524 v2_ext_offset += sizeof(*pclc_prfx) +
525 pclc_prfx->ipv6_prefixes_cnt *
526 sizeof(ipv6_prfx[0]);
527 pclc_smcd->v2_ext_offset = htons(v2_ext_offset);
528 v2_ext->hdr.eid_cnt = 0;
529 v2_ext->hdr.ism_gid_cnt = ini->ism_offered_cnt;
530 v2_ext->hdr.flag.release = SMC_RELEASE;
531 v2_ext->hdr.flag.seid = 1;
532 v2_ext->hdr.smcd_v2_ext_offset = htons(sizeof(*v2_ext) -
533 offsetofend(struct smc_clnt_opts_area_hdr,
534 smcd_v2_ext_offset) +
535 v2_ext->hdr.eid_cnt * SMC_MAX_EID_LEN);
536 if (ini->ism_dev[0])
537 smc_ism_get_system_eid(ini->ism_dev[0], &eid);
538 else
539 smc_ism_get_system_eid(ini->ism_dev[1], &eid);
540 if (eid)
541 memcpy(smcd_v2_ext->system_eid, eid, SMC_MAX_EID_LEN);
542 plen += sizeof(*v2_ext) + sizeof(*smcd_v2_ext);
543 if (ini->ism_offered_cnt) {
544 for (i = 1; i <= ini->ism_offered_cnt; i++) {
545 gidchids[i - 1].gid =
546 htonll(ini->ism_dev[i]->local_gid);
547 gidchids[i - 1].chid =
548 htons(smc_ism_get_chid(ini->ism_dev[i]));
549 }
550 plen += ini->ism_offered_cnt *
551 sizeof(struct smc_clc_smcd_gid_chid);
552 }
Hans Wippelc758dfd2018-06-28 19:05:09 +0200553 }
Ursula Braun6bb14e482020-09-10 18:48:22 +0200554 pclc_base->hdr.length = htons(plen);
Ursula Braun6bb14e482020-09-10 18:48:22 +0200555 memcpy(trl->eyecatcher, SMC_EYECATCHER, sizeof(SMC_EYECATCHER));
Ursula Braun8c3dca32020-09-26 12:44:28 +0200556
557 /* send SMC Proposal CLC message */
Ursula Brauna046d572017-01-09 16:55:16 +0100558 memset(&msg, 0, sizeof(msg));
Karsten Graul1a26d022018-03-16 15:06:40 +0100559 i = 0;
Ursula Braun6bb14e482020-09-10 18:48:22 +0200560 vec[i].iov_base = pclc_base;
561 vec[i++].iov_len = sizeof(*pclc_base);
Ursula Braun8c3dca32020-09-26 12:44:28 +0200562 vec[i].iov_base = pclc_smcd;
563 vec[i++].iov_len = sizeof(*pclc_smcd);
564 if (ini->smc_type_v1 != SMC_TYPE_N) {
565 vec[i].iov_base = pclc_prfx;
566 vec[i++].iov_len = sizeof(*pclc_prfx);
567 if (pclc_prfx->ipv6_prefixes_cnt > 0) {
568 vec[i].iov_base = ipv6_prfx;
569 vec[i++].iov_len = pclc_prfx->ipv6_prefixes_cnt *
570 sizeof(ipv6_prfx[0]);
571 }
Hans Wippelc758dfd2018-06-28 19:05:09 +0200572 }
Ursula Braun8c3dca32020-09-26 12:44:28 +0200573 if (ini->smc_type_v2 != SMC_TYPE_N) {
574 vec[i].iov_base = v2_ext;
575 vec[i++].iov_len = sizeof(*v2_ext);
576 vec[i].iov_base = smcd_v2_ext;
577 vec[i++].iov_len = sizeof(*smcd_v2_ext);
578 if (ini->ism_offered_cnt) {
579 vec[i].iov_base = gidchids;
580 vec[i++].iov_len = ini->ism_offered_cnt *
581 sizeof(struct smc_clc_smcd_gid_chid);
582 }
Karsten Graul1a26d022018-03-16 15:06:40 +0100583 }
Ursula Braun6bb14e482020-09-10 18:48:22 +0200584 vec[i].iov_base = trl;
585 vec[i++].iov_len = sizeof(*trl);
Ursula Brauna046d572017-01-09 16:55:16 +0100586 /* due to the few bytes needed for clc-handshake this cannot block */
Karsten Graul1a26d022018-03-16 15:06:40 +0100587 len = kernel_sendmsg(smc->clcsock, &msg, vec, i, plen);
YueHaibing38189772018-09-18 15:46:38 +0200588 if (len < 0) {
589 smc->sk.sk_err = smc->clcsock->sk->sk_err;
590 reason_code = -smc->sk.sk_err;
Ursula Braun6bb14e482020-09-10 18:48:22 +0200591 } else if (len < ntohs(pclc_base->hdr.length)) {
YueHaibing38189772018-09-18 15:46:38 +0200592 reason_code = -ENETUNREACH;
593 smc->sk.sk_err = -reason_code;
Ursula Brauna046d572017-01-09 16:55:16 +0100594 }
595
Ursula Braun6bb14e482020-09-10 18:48:22 +0200596 kfree(pclc);
Ursula Brauna046d572017-01-09 16:55:16 +0100597 return reason_code;
598}
599
Ursula Braun3d9725a2020-09-10 18:48:23 +0200600/* build and send CLC CONFIRM / ACCEPT message */
601static int smc_clc_send_confirm_accept(struct smc_sock *smc,
602 struct smc_clc_msg_accept_confirm *clc,
603 int first_contact)
604{
605 struct smc_connection *conn = &smc->conn;
Ursula Braune15c6c42020-09-26 12:44:22 +0200606 struct smc_clc_msg_trail trl;
607 struct kvec vec[2];
Ursula Braun3d9725a2020-09-10 18:48:23 +0200608 struct msghdr msg;
Ursula Braune15c6c42020-09-26 12:44:22 +0200609 int i;
Ursula Braun3d9725a2020-09-10 18:48:23 +0200610
611 /* send SMC Confirm CLC msg */
612 clc->hdr.version = SMC_V1; /* SMC version */
613 if (first_contact)
Ursula Braunf1eb02f2020-09-26 12:44:20 +0200614 clc->hdr.typev2 |= SMC_FIRST_CONTACT_MASK;
Ursula Braun3d9725a2020-09-10 18:48:23 +0200615 if (conn->lgr->is_smcd) {
616 /* SMC-D specific settings */
617 memcpy(clc->hdr.eyecatcher, SMCD_EYECATCHER,
618 sizeof(SMCD_EYECATCHER));
Ursula Braunf1eb02f2020-09-26 12:44:20 +0200619 clc->hdr.typev1 = SMC_TYPE_D;
Ursula Braun3d9725a2020-09-10 18:48:23 +0200620 clc->hdr.length = htons(SMCD_CLC_ACCEPT_CONFIRM_LEN);
621 clc->d0.gid = conn->lgr->smcd->local_gid;
622 clc->d0.token = conn->rmb_desc->token;
623 clc->d0.dmbe_size = conn->rmbe_size_short;
624 clc->d0.dmbe_idx = 0;
625 memcpy(&clc->d0.linkid, conn->lgr->id, SMC_LGR_ID_SIZE);
Ursula Braune15c6c42020-09-26 12:44:22 +0200626 memcpy(trl.eyecatcher, SMCD_EYECATCHER,
Ursula Braun3d9725a2020-09-10 18:48:23 +0200627 sizeof(SMCD_EYECATCHER));
628 } else {
629 struct smc_link *link = conn->lnk;
630
631 /* SMC-R specific settings */
632 link = conn->lnk;
633 memcpy(clc->hdr.eyecatcher, SMC_EYECATCHER,
634 sizeof(SMC_EYECATCHER));
Ursula Braunf1eb02f2020-09-26 12:44:20 +0200635 clc->hdr.typev1 = SMC_TYPE_R;
Ursula Braun3d9725a2020-09-10 18:48:23 +0200636 clc->hdr.length = htons(SMCR_CLC_ACCEPT_CONFIRM_LEN);
637 memcpy(clc->r0.lcl.id_for_peer, local_systemid,
638 sizeof(local_systemid));
639 memcpy(&clc->r0.lcl.gid, link->gid, SMC_GID_SIZE);
640 memcpy(&clc->r0.lcl.mac, &link->smcibdev->mac[link->ibport - 1],
641 ETH_ALEN);
642 hton24(clc->r0.qpn, link->roce_qp->qp_num);
643 clc->r0.rmb_rkey =
644 htonl(conn->rmb_desc->mr_rx[link->link_idx]->rkey);
645 clc->r0.rmbe_idx = 1; /* for now: 1 RMB = 1 RMBE */
646 clc->r0.rmbe_alert_token = htonl(conn->alert_token_local);
647 switch (clc->hdr.type) {
648 case SMC_CLC_ACCEPT:
649 clc->r0.qp_mtu = link->path_mtu;
650 break;
651 case SMC_CLC_CONFIRM:
652 clc->r0.qp_mtu = min(link->path_mtu, link->peer_mtu);
653 break;
654 }
655 clc->r0.rmbe_size = conn->rmbe_size_short;
656 clc->r0.rmb_dma_addr = cpu_to_be64((u64)sg_dma_address
657 (conn->rmb_desc->sgt[link->link_idx].sgl));
658 hton24(clc->r0.psn, link->psn_initial);
Ursula Braune15c6c42020-09-26 12:44:22 +0200659 memcpy(trl.eyecatcher, SMC_EYECATCHER, sizeof(SMC_EYECATCHER));
Ursula Braun3d9725a2020-09-10 18:48:23 +0200660 }
661
662 memset(&msg, 0, sizeof(msg));
Ursula Braune15c6c42020-09-26 12:44:22 +0200663 i = 0;
664 vec[i].iov_base = clc;
665 vec[i++].iov_len = (clc->hdr.typev1 == SMC_TYPE_D ?
666 SMCD_CLC_ACCEPT_CONFIRM_LEN :
667 SMCR_CLC_ACCEPT_CONFIRM_LEN) -
668 sizeof(trl);
669 vec[i].iov_base = &trl;
670 vec[i++].iov_len = sizeof(trl);
671 return kernel_sendmsg(smc->clcsock, &msg, vec, 1,
Ursula Braun3d9725a2020-09-10 18:48:23 +0200672 ntohs(clc->hdr.length));
673}
674
Ursula Brauna046d572017-01-09 16:55:16 +0100675/* send CLC CONFIRM message across internal TCP socket */
676int smc_clc_send_confirm(struct smc_sock *smc)
677{
678 struct smc_clc_msg_accept_confirm cclc;
679 int reason_code = 0;
Ursula Brauna046d572017-01-09 16:55:16 +0100680 int len;
681
682 /* send SMC Confirm CLC msg */
683 memset(&cclc, 0, sizeof(cclc));
Ursula Brauna046d572017-01-09 16:55:16 +0100684 cclc.hdr.type = SMC_CLC_CONFIRM;
Ursula Braun3d9725a2020-09-10 18:48:23 +0200685 len = smc_clc_send_confirm_accept(smc, &cclc, 0);
Hans Wippelc758dfd2018-06-28 19:05:09 +0200686 if (len < ntohs(cclc.hdr.length)) {
Ursula Brauna046d572017-01-09 16:55:16 +0100687 if (len >= 0) {
688 reason_code = -ENETUNREACH;
689 smc->sk.sk_err = -reason_code;
690 } else {
691 smc->sk.sk_err = smc->clcsock->sk->sk_err;
692 reason_code = -smc->sk.sk_err;
693 }
694 }
695 return reason_code;
696}
697
698/* send CLC ACCEPT message across internal TCP socket */
Ursula Braun5ac54d82020-09-10 18:48:21 +0200699int smc_clc_send_accept(struct smc_sock *new_smc, bool srv_first_contact)
Ursula Brauna046d572017-01-09 16:55:16 +0100700{
701 struct smc_clc_msg_accept_confirm aclc;
Ursula Brauna046d572017-01-09 16:55:16 +0100702 int len;
703
704 memset(&aclc, 0, sizeof(aclc));
Ursula Brauna046d572017-01-09 16:55:16 +0100705 aclc.hdr.type = SMC_CLC_ACCEPT;
Ursula Braun3d9725a2020-09-10 18:48:23 +0200706 len = smc_clc_send_confirm_accept(new_smc, &aclc, srv_first_contact);
Ursula Braun6ae36bf2018-11-22 10:26:36 +0100707 if (len < ntohs(aclc.hdr.length))
708 len = len >= 0 ? -EPROTO : -new_smc->clcsock->sk->sk_err;
Ursula Brauna046d572017-01-09 16:55:16 +0100709
Ursula Braun6ae36bf2018-11-22 10:26:36 +0100710 return len > 0 ? 0 : len;
Ursula Brauna046d572017-01-09 16:55:16 +0100711}