blob: bd116e1949b9dfcdd1e46875bb44b05739cf2746 [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 Braune7b7a642017-12-07 13:38:49 +010037/* check if received message has a correct header length and contains valid
38 * heading and trailing eyecatchers
39 */
Ursula Braunfb4f7922020-07-08 17:05:15 +020040static bool smc_clc_msg_hdr_valid(struct smc_clc_msg_hdr *clcm, bool check_trl)
Ursula Braune7b7a642017-12-07 13:38:49 +010041{
42 struct smc_clc_msg_proposal_prefix *pclc_prfx;
43 struct smc_clc_msg_accept_confirm *clc;
44 struct smc_clc_msg_proposal *pclc;
45 struct smc_clc_msg_decline *dclc;
46 struct smc_clc_msg_trail *trl;
47
Hans Wippelc758dfd2018-06-28 19:05:09 +020048 if (memcmp(clcm->eyecatcher, SMC_EYECATCHER, sizeof(SMC_EYECATCHER)) &&
49 memcmp(clcm->eyecatcher, SMCD_EYECATCHER, sizeof(SMCD_EYECATCHER)))
Ursula Braune7b7a642017-12-07 13:38:49 +010050 return false;
51 switch (clcm->type) {
52 case SMC_CLC_PROPOSAL:
53 pclc = (struct smc_clc_msg_proposal *)clcm;
54 pclc_prfx = smc_clc_proposal_get_prefix(pclc);
Ursula Braunfb4f7922020-07-08 17:05:15 +020055 if (ntohs(pclc->hdr.length) <
Ursula Braune7b7a642017-12-07 13:38:49 +010056 sizeof(*pclc) + ntohs(pclc->iparea_offset) +
57 sizeof(*pclc_prfx) +
58 pclc_prfx->ipv6_prefixes_cnt *
59 sizeof(struct smc_clc_ipv6_prefix) +
60 sizeof(*trl))
61 return false;
62 trl = (struct smc_clc_msg_trail *)
63 ((u8 *)pclc + ntohs(pclc->hdr.length) - sizeof(*trl));
64 break;
65 case SMC_CLC_ACCEPT:
66 case SMC_CLC_CONFIRM:
Hans Wippelc758dfd2018-06-28 19:05:09 +020067 if (clcm->path != SMC_TYPE_R && clcm->path != SMC_TYPE_D)
Ursula Braune7b7a642017-12-07 13:38:49 +010068 return false;
Hans Wippelc758dfd2018-06-28 19:05:09 +020069 clc = (struct smc_clc_msg_accept_confirm *)clcm;
70 if ((clcm->path == SMC_TYPE_R &&
71 ntohs(clc->hdr.length) != SMCR_CLC_ACCEPT_CONFIRM_LEN) ||
72 (clcm->path == SMC_TYPE_D &&
73 ntohs(clc->hdr.length) != SMCD_CLC_ACCEPT_CONFIRM_LEN))
74 return false;
75 trl = (struct smc_clc_msg_trail *)
76 ((u8 *)clc + ntohs(clc->hdr.length) - sizeof(*trl));
Ursula Braune7b7a642017-12-07 13:38:49 +010077 break;
78 case SMC_CLC_DECLINE:
79 dclc = (struct smc_clc_msg_decline *)clcm;
80 if (ntohs(dclc->hdr.length) != sizeof(*dclc))
81 return false;
82 trl = &dclc->trl;
83 break;
84 default:
85 return false;
86 }
Ursula Braunfb4f7922020-07-08 17:05:15 +020087 if (check_trl &&
88 memcmp(trl->eyecatcher, SMC_EYECATCHER, sizeof(SMC_EYECATCHER)) &&
Hans Wippelc758dfd2018-06-28 19:05:09 +020089 memcmp(trl->eyecatcher, SMCD_EYECATCHER, sizeof(SMCD_EYECATCHER)))
Ursula Braune7b7a642017-12-07 13:38:49 +010090 return false;
91 return true;
92}
93
Karsten Graulc246d942018-03-16 15:06:39 +010094/* find ipv4 addr on device and get the prefix len, fill CLC proposal msg */
95static int smc_clc_prfx_set4_rcu(struct dst_entry *dst, __be32 ipv4,
96 struct smc_clc_msg_proposal_prefix *prop)
97{
98 struct in_device *in_dev = __in_dev_get_rcu(dst->dev);
Florian Westphalcd5a4112019-05-31 18:27:07 +020099 const struct in_ifaddr *ifa;
Karsten Graulc246d942018-03-16 15:06:39 +0100100
101 if (!in_dev)
102 return -ENODEV;
Florian Westphalcd5a4112019-05-31 18:27:07 +0200103
104 in_dev_for_each_ifa_rcu(ifa, in_dev) {
Karsten Graulc246d942018-03-16 15:06:39 +0100105 if (!inet_ifa_match(ipv4, ifa))
106 continue;
107 prop->prefix_len = inet_mask_len(ifa->ifa_mask);
108 prop->outgoing_subnet = ifa->ifa_address & ifa->ifa_mask;
109 /* prop->ipv6_prefixes_cnt = 0; already done by memset before */
110 return 0;
Florian Westphalcd5a4112019-05-31 18:27:07 +0200111 }
Karsten Graulc246d942018-03-16 15:06:39 +0100112 return -ENOENT;
113}
114
Karsten Graul1a26d022018-03-16 15:06:40 +0100115/* fill CLC proposal msg with ipv6 prefixes from device */
116static int smc_clc_prfx_set6_rcu(struct dst_entry *dst,
117 struct smc_clc_msg_proposal_prefix *prop,
118 struct smc_clc_ipv6_prefix *ipv6_prfx)
119{
120#if IS_ENABLED(CONFIG_IPV6)
121 struct inet6_dev *in6_dev = __in6_dev_get(dst->dev);
122 struct inet6_ifaddr *ifa;
123 int cnt = 0;
124
125 if (!in6_dev)
126 return -ENODEV;
127 /* use a maximum of 8 IPv6 prefixes from device */
128 list_for_each_entry(ifa, &in6_dev->addr_list, if_list) {
129 if (ipv6_addr_type(&ifa->addr) & IPV6_ADDR_LINKLOCAL)
130 continue;
131 ipv6_addr_prefix(&ipv6_prfx[cnt].prefix,
132 &ifa->addr, ifa->prefix_len);
133 ipv6_prfx[cnt].prefix_len = ifa->prefix_len;
134 cnt++;
135 if (cnt == SMC_CLC_MAX_V6_PREFIX)
136 break;
137 }
138 prop->ipv6_prefixes_cnt = cnt;
139 if (cnt)
140 return 0;
141#endif
142 return -ENOENT;
143}
144
Karsten Graulc246d942018-03-16 15:06:39 +0100145/* retrieve and set prefixes in CLC proposal msg */
146static int smc_clc_prfx_set(struct socket *clcsock,
Karsten Graul1a26d022018-03-16 15:06:40 +0100147 struct smc_clc_msg_proposal_prefix *prop,
148 struct smc_clc_ipv6_prefix *ipv6_prfx)
Karsten Graul696cd302018-03-01 13:51:27 +0100149{
150 struct dst_entry *dst = sk_dst_get(clcsock->sk);
Karsten Graulc246d942018-03-16 15:06:39 +0100151 struct sockaddr_storage addrs;
Karsten Graul1a26d022018-03-16 15:06:40 +0100152 struct sockaddr_in6 *addr6;
Karsten Graulc246d942018-03-16 15:06:39 +0100153 struct sockaddr_in *addr;
154 int rc = -ENOENT;
155
Karsten Graulc246d942018-03-16 15:06:39 +0100156 if (!dst) {
157 rc = -ENOTCONN;
158 goto out;
159 }
160 if (!dst->dev) {
161 rc = -ENODEV;
162 goto out_rel;
163 }
164 /* get address to which the internal TCP socket is bound */
165 kernel_getsockname(clcsock, (struct sockaddr *)&addrs);
166 /* analyze IP specific data of net_device belonging to TCP socket */
Karsten Graul1a26d022018-03-16 15:06:40 +0100167 addr6 = (struct sockaddr_in6 *)&addrs;
Karsten Graulc246d942018-03-16 15:06:39 +0100168 rcu_read_lock();
169 if (addrs.ss_family == PF_INET) {
170 /* IPv4 */
171 addr = (struct sockaddr_in *)&addrs;
172 rc = smc_clc_prfx_set4_rcu(dst, addr->sin_addr.s_addr, prop);
Karsten Graul1a26d022018-03-16 15:06:40 +0100173 } else if (ipv6_addr_v4mapped(&addr6->sin6_addr)) {
174 /* mapped IPv4 address - peer is IPv4 only */
175 rc = smc_clc_prfx_set4_rcu(dst, addr6->sin6_addr.s6_addr32[3],
176 prop);
177 } else {
178 /* IPv6 */
179 rc = smc_clc_prfx_set6_rcu(dst, prop, ipv6_prfx);
Karsten Graulc246d942018-03-16 15:06:39 +0100180 }
181 rcu_read_unlock();
182out_rel:
183 dst_release(dst);
184out:
185 return rc;
186}
187
188/* match ipv4 addrs of dev against addr in CLC proposal */
189static int smc_clc_prfx_match4_rcu(struct net_device *dev,
190 struct smc_clc_msg_proposal_prefix *prop)
191{
192 struct in_device *in_dev = __in_dev_get_rcu(dev);
Florian Westphalcd5a4112019-05-31 18:27:07 +0200193 const struct in_ifaddr *ifa;
Karsten Graulc246d942018-03-16 15:06:39 +0100194
195 if (!in_dev)
196 return -ENODEV;
Florian Westphalcd5a4112019-05-31 18:27:07 +0200197 in_dev_for_each_ifa_rcu(ifa, in_dev) {
Karsten Graulc246d942018-03-16 15:06:39 +0100198 if (prop->prefix_len == inet_mask_len(ifa->ifa_mask) &&
199 inet_ifa_match(prop->outgoing_subnet, ifa))
200 return 0;
Florian Westphalcd5a4112019-05-31 18:27:07 +0200201 }
Karsten Graulc246d942018-03-16 15:06:39 +0100202
203 return -ENOENT;
204}
205
Karsten Graul1a26d022018-03-16 15:06:40 +0100206/* match ipv6 addrs of dev against addrs in CLC proposal */
207static int smc_clc_prfx_match6_rcu(struct net_device *dev,
208 struct smc_clc_msg_proposal_prefix *prop)
209{
210#if IS_ENABLED(CONFIG_IPV6)
211 struct inet6_dev *in6_dev = __in6_dev_get(dev);
212 struct smc_clc_ipv6_prefix *ipv6_prfx;
213 struct inet6_ifaddr *ifa;
214 int i, max;
215
216 if (!in6_dev)
217 return -ENODEV;
218 /* ipv6 prefix list starts behind smc_clc_msg_proposal_prefix */
219 ipv6_prfx = (struct smc_clc_ipv6_prefix *)((u8 *)prop + sizeof(*prop));
220 max = min_t(u8, prop->ipv6_prefixes_cnt, SMC_CLC_MAX_V6_PREFIX);
221 list_for_each_entry(ifa, &in6_dev->addr_list, if_list) {
222 if (ipv6_addr_type(&ifa->addr) & IPV6_ADDR_LINKLOCAL)
223 continue;
224 for (i = 0; i < max; i++) {
225 if (ifa->prefix_len == ipv6_prfx[i].prefix_len &&
226 ipv6_prefix_equal(&ifa->addr, &ipv6_prfx[i].prefix,
227 ifa->prefix_len))
228 return 0;
229 }
230 }
231#endif
232 return -ENOENT;
233}
234
Karsten Graulc246d942018-03-16 15:06:39 +0100235/* check if proposed prefixes match one of our device prefixes */
236int smc_clc_prfx_match(struct socket *clcsock,
237 struct smc_clc_msg_proposal_prefix *prop)
238{
239 struct dst_entry *dst = sk_dst_get(clcsock->sk);
Karsten Graul1a26d022018-03-16 15:06:40 +0100240 int rc;
Karsten Graul696cd302018-03-01 13:51:27 +0100241
242 if (!dst) {
243 rc = -ENOTCONN;
244 goto out;
245 }
246 if (!dst->dev) {
247 rc = -ENODEV;
248 goto out_rel;
249 }
Karsten Graul696cd302018-03-01 13:51:27 +0100250 rcu_read_lock();
Karsten Graulc246d942018-03-16 15:06:39 +0100251 if (!prop->ipv6_prefixes_cnt)
252 rc = smc_clc_prfx_match4_rcu(dst->dev, prop);
Karsten Graul1a26d022018-03-16 15:06:40 +0100253 else
254 rc = smc_clc_prfx_match6_rcu(dst->dev, prop);
Karsten Graul696cd302018-03-01 13:51:27 +0100255 rcu_read_unlock();
Karsten Graul696cd302018-03-01 13:51:27 +0100256out_rel:
257 dst_release(dst);
258out:
259 return rc;
260}
261
Ursula Brauna046d572017-01-09 16:55:16 +0100262/* Wait for data on the tcp-socket, analyze received data
263 * Returns:
264 * 0 if success and it was not a decline that we received.
265 * SMC_CLC_DECL_REPLY if decline received for fallback w/o another decl send.
266 * clcsock error, -EINTR, -ECONNRESET, -EPROTO otherwise.
267 */
268int smc_clc_wait_msg(struct smc_sock *smc, void *buf, int buflen,
Ursula Braun2b59f582018-11-22 10:26:39 +0100269 u8 expected_type, unsigned long timeout)
Ursula Brauna046d572017-01-09 16:55:16 +0100270{
Karsten Graulf6bdc422018-07-18 15:22:51 +0200271 long rcvtimeo = smc->clcsock->sk->sk_rcvtimeo;
Ursula Brauna046d572017-01-09 16:55:16 +0100272 struct sock *clc_sk = smc->clcsock->sk;
273 struct smc_clc_msg_hdr *clcm = buf;
274 struct msghdr msg = {NULL, 0};
275 int reason_code = 0;
Al Virod63d2712017-09-20 20:21:22 -0400276 struct kvec vec = {buf, buflen};
Ursula Braunfb4f7922020-07-08 17:05:15 +0200277 int len, datlen, recvlen;
278 bool check_trl = true;
Ursula Brauna046d572017-01-09 16:55:16 +0100279 int krflags;
280
281 /* peek the first few bytes to determine length of data to receive
282 * so we don't consume any subsequent CLC message or payload data
283 * in the TCP byte stream
284 */
Al Virod63d2712017-09-20 20:21:22 -0400285 /*
286 * Caller must make sure that buflen is no less than
287 * sizeof(struct smc_clc_msg_hdr)
288 */
Ursula Brauna046d572017-01-09 16:55:16 +0100289 krflags = MSG_PEEK | MSG_WAITALL;
Ursula Braun2b59f582018-11-22 10:26:39 +0100290 clc_sk->sk_rcvtimeo = timeout;
David Howellsaa563d72018-10-20 00:57:56 +0100291 iov_iter_kvec(&msg.msg_iter, READ, &vec, 1,
Al Virod63d2712017-09-20 20:21:22 -0400292 sizeof(struct smc_clc_msg_hdr));
293 len = sock_recvmsg(smc->clcsock, &msg, krflags);
Ursula Brauna046d572017-01-09 16:55:16 +0100294 if (signal_pending(current)) {
295 reason_code = -EINTR;
296 clc_sk->sk_err = EINTR;
297 smc->sk.sk_err = EINTR;
298 goto out;
299 }
300 if (clc_sk->sk_err) {
301 reason_code = -clc_sk->sk_err;
Ursula Braun9ed28552018-11-22 10:26:37 +0100302 if (clc_sk->sk_err == EAGAIN &&
303 expected_type == SMC_CLC_DECLINE)
304 clc_sk->sk_err = 0; /* reset for fallback usage */
305 else
306 smc->sk.sk_err = clc_sk->sk_err;
Ursula Brauna046d572017-01-09 16:55:16 +0100307 goto out;
308 }
309 if (!len) { /* peer has performed orderly shutdown */
310 smc->sk.sk_err = ECONNRESET;
311 reason_code = -ECONNRESET;
312 goto out;
313 }
314 if (len < 0) {
Ursula Braun9ed28552018-11-22 10:26:37 +0100315 if (len != -EAGAIN || expected_type != SMC_CLC_DECLINE)
316 smc->sk.sk_err = -len;
Ursula Brauna046d572017-01-09 16:55:16 +0100317 reason_code = len;
318 goto out;
319 }
320 datlen = ntohs(clcm->length);
321 if ((len < sizeof(struct smc_clc_msg_hdr)) ||
Ursula Braun5ac54d82020-09-10 18:48:21 +0200322 (clcm->version < SMC_V1) ||
Ursula Brauna046d572017-01-09 16:55:16 +0100323 ((clcm->type != SMC_CLC_DECLINE) &&
324 (clcm->type != expected_type))) {
325 smc->sk.sk_err = EPROTO;
326 reason_code = -EPROTO;
327 goto out;
328 }
329
Ursula Braunfb4f7922020-07-08 17:05:15 +0200330 if (clcm->type == SMC_CLC_PROPOSAL && clcm->path == SMC_TYPE_N)
331 reason_code = SMC_CLC_DECL_VERSMISMAT; /* just V2 offered */
332
Ursula Brauna046d572017-01-09 16:55:16 +0100333 /* receive the complete CLC message */
Ursula Brauna046d572017-01-09 16:55:16 +0100334 memset(&msg, 0, sizeof(struct msghdr));
Ursula Braunfb4f7922020-07-08 17:05:15 +0200335 if (datlen > buflen) {
336 check_trl = false;
337 recvlen = buflen;
338 } else {
339 recvlen = datlen;
340 }
341 iov_iter_kvec(&msg.msg_iter, READ, &vec, 1, recvlen);
Ursula Brauna046d572017-01-09 16:55:16 +0100342 krflags = MSG_WAITALL;
Al Virod63d2712017-09-20 20:21:22 -0400343 len = sock_recvmsg(smc->clcsock, &msg, krflags);
Ursula Braunfb4f7922020-07-08 17:05:15 +0200344 if (len < recvlen || !smc_clc_msg_hdr_valid(clcm, check_trl)) {
Ursula Brauna046d572017-01-09 16:55:16 +0100345 smc->sk.sk_err = EPROTO;
346 reason_code = -EPROTO;
347 goto out;
348 }
Ursula Braunfb4f7922020-07-08 17:05:15 +0200349 datlen -= len;
350 while (datlen) {
351 u8 tmp[SMC_CLC_RECV_BUF_LEN];
352
353 vec.iov_base = &tmp;
354 vec.iov_len = SMC_CLC_RECV_BUF_LEN;
355 /* receive remaining proposal message */
356 recvlen = datlen > SMC_CLC_RECV_BUF_LEN ?
357 SMC_CLC_RECV_BUF_LEN : datlen;
358 iov_iter_kvec(&msg.msg_iter, READ, &vec, 1, recvlen);
359 len = sock_recvmsg(smc->clcsock, &msg, krflags);
360 datlen -= len;
361 }
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100362 if (clcm->type == SMC_CLC_DECLINE) {
Karsten Graul603cc142018-07-25 16:35:32 +0200363 struct smc_clc_msg_decline *dclc;
364
365 dclc = (struct smc_clc_msg_decline *)clcm;
366 reason_code = SMC_CLC_DECL_PEERDECL;
367 smc->peer_diagnosis = ntohl(dclc->peer_diagnosis);
Ursula Braunbfbedfd2017-09-21 09:16:32 +0200368 if (((struct smc_clc_msg_decline *)buf)->hdr.flag) {
Karsten Graul517c3002018-05-15 17:05:03 +0200369 smc->conn.lgr->sync_err = 1;
Karsten Graul5f78fe92020-02-17 16:24:54 +0100370 smc_lgr_terminate_sched(smc->conn.lgr);
Ursula Braunbfbedfd2017-09-21 09:16:32 +0200371 }
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100372 }
373
Ursula Brauna046d572017-01-09 16:55:16 +0100374out:
Ursula Braun2b59f582018-11-22 10:26:39 +0100375 clc_sk->sk_rcvtimeo = rcvtimeo;
Ursula Brauna046d572017-01-09 16:55:16 +0100376 return reason_code;
377}
378
379/* send CLC DECLINE message across internal TCP socket */
Ursula Braunbfbedfd2017-09-21 09:16:32 +0200380int smc_clc_send_decline(struct smc_sock *smc, u32 peer_diag_info)
Ursula Brauna046d572017-01-09 16:55:16 +0100381{
382 struct smc_clc_msg_decline dclc;
383 struct msghdr msg;
384 struct kvec vec;
385 int len;
386
387 memset(&dclc, 0, sizeof(dclc));
388 memcpy(dclc.hdr.eyecatcher, SMC_EYECATCHER, sizeof(SMC_EYECATCHER));
389 dclc.hdr.type = SMC_CLC_DECLINE;
390 dclc.hdr.length = htons(sizeof(struct smc_clc_msg_decline));
Ursula Braun5ac54d82020-09-10 18:48:21 +0200391 dclc.hdr.version = SMC_V1;
Ursula Braunbfbedfd2017-09-21 09:16:32 +0200392 dclc.hdr.flag = (peer_diag_info == SMC_CLC_DECL_SYNCERR) ? 1 : 0;
Hans Wippela082ec82020-02-25 22:41:22 +0100393 if ((!smc->conn.lgr || !smc->conn.lgr->is_smcd) &&
394 smc_ib_is_valid_local_systemid())
Ursula Braun369537c2020-02-14 08:59:00 +0100395 memcpy(dclc.id_for_peer, local_systemid,
396 sizeof(local_systemid));
Ursula Brauna046d572017-01-09 16:55:16 +0100397 dclc.peer_diagnosis = htonl(peer_diag_info);
398 memcpy(dclc.trl.eyecatcher, SMC_EYECATCHER, sizeof(SMC_EYECATCHER));
399
400 memset(&msg, 0, sizeof(msg));
401 vec.iov_base = &dclc;
402 vec.iov_len = sizeof(struct smc_clc_msg_decline);
403 len = kernel_sendmsg(smc->clcsock, &msg, &vec, 1,
404 sizeof(struct smc_clc_msg_decline));
Ursula Braun14d22d42019-01-30 18:51:00 +0100405 if (len < 0 || len < sizeof(struct smc_clc_msg_decline))
Ursula Braun6ae36bf2018-11-22 10:26:36 +0100406 len = -EPROTO;
407 return len > 0 ? 0 : len;
Ursula Brauna046d572017-01-09 16:55:16 +0100408}
409
410/* send CLC PROPOSAL message across internal TCP socket */
Hans Wippelc758dfd2018-06-28 19:05:09 +0200411int smc_clc_send_proposal(struct smc_sock *smc, int smc_type,
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200412 struct smc_init_info *ini)
Ursula Brauna046d572017-01-09 16:55:16 +0100413{
Ursula Braun6bb14e482020-09-10 18:48:22 +0200414 struct smc_clc_msg_proposal_prefix *pclc_prfx;
415 struct smc_clc_msg_proposal *pclc_base;
416 struct smc_clc_msg_proposal_area *pclc;
417 struct smc_clc_ipv6_prefix *ipv6_prfx;
418 struct smc_clc_msg_smcd *pclc_smcd;
419 struct smc_clc_msg_trail *trl;
Karsten Graul1a26d022018-03-16 15:06:40 +0100420 int len, i, plen, rc;
Ursula Brauna046d572017-01-09 16:55:16 +0100421 int reason_code = 0;
Hans Wippelc758dfd2018-06-28 19:05:09 +0200422 struct kvec vec[5];
Ursula Brauna046d572017-01-09 16:55:16 +0100423 struct msghdr msg;
Ursula Brauna046d572017-01-09 16:55:16 +0100424
Ursula Braun6bb14e482020-09-10 18:48:22 +0200425 pclc = kzalloc(sizeof(*pclc), GFP_KERNEL);
426 if (!pclc)
427 return -ENOMEM;
428
429 pclc_base = &pclc->pclc_base;
430 pclc_smcd = &pclc->pclc_smcd;
431 pclc_prfx = &pclc->pclc_prfx;
432 ipv6_prfx = pclc->pclc_prfx_ipv6;
433 trl = &pclc->pclc_trl;
434
Karsten Graulc246d942018-03-16 15:06:39 +0100435 /* retrieve ip prefixes for CLC proposal msg */
Ursula Braun6bb14e482020-09-10 18:48:22 +0200436 rc = smc_clc_prfx_set(smc->clcsock, pclc_prfx, ipv6_prfx);
437 if (rc) {
438 kfree(pclc);
Karsten Graulc246d942018-03-16 15:06:39 +0100439 return SMC_CLC_DECL_CNFERR; /* configuration error */
Ursula Braun6bb14e482020-09-10 18:48:22 +0200440 }
Karsten Graulc246d942018-03-16 15:06:39 +0100441
Ursula Brauna046d572017-01-09 16:55:16 +0100442 /* send SMC Proposal CLC message */
Ursula Braun6bb14e482020-09-10 18:48:22 +0200443 plen = sizeof(*pclc_base) + sizeof(*pclc_prfx) +
444 (pclc_prfx->ipv6_prefixes_cnt * sizeof(ipv6_prfx[0])) +
445 sizeof(*trl);
446 memcpy(pclc_base->hdr.eyecatcher, SMC_EYECATCHER,
447 sizeof(SMC_EYECATCHER));
448 pclc_base->hdr.type = SMC_CLC_PROPOSAL;
449 pclc_base->hdr.version = SMC_V1; /* SMC version */
450 pclc_base->hdr.path = smc_type;
Hans Wippelc758dfd2018-06-28 19:05:09 +0200451 if (smc_type == SMC_TYPE_R || smc_type == SMC_TYPE_B) {
452 /* add SMC-R specifics */
Ursula Braun6bb14e482020-09-10 18:48:22 +0200453 memcpy(pclc_base->lcl.id_for_peer, local_systemid,
Hans Wippelc758dfd2018-06-28 19:05:09 +0200454 sizeof(local_systemid));
Ursula Braun6bb14e482020-09-10 18:48:22 +0200455 memcpy(pclc_base->lcl.gid, ini->ib_gid, SMC_GID_SIZE);
456 memcpy(pclc_base->lcl.mac, &ini->ib_dev->mac[ini->ib_port - 1],
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200457 ETH_ALEN);
Ursula Braun6bb14e482020-09-10 18:48:22 +0200458 pclc_base->iparea_offset = htons(0);
Hans Wippelc758dfd2018-06-28 19:05:09 +0200459 }
460 if (smc_type == SMC_TYPE_D || smc_type == SMC_TYPE_B) {
461 /* add SMC-D specifics */
Ursula Braun6bb14e482020-09-10 18:48:22 +0200462 plen += sizeof(*pclc_smcd);
463 pclc_base->iparea_offset = htons(sizeof(*pclc_smcd));
464 pclc_smcd->gid = ini->ism_dev->local_gid;
Hans Wippelc758dfd2018-06-28 19:05:09 +0200465 }
Ursula Braun6bb14e482020-09-10 18:48:22 +0200466 pclc_base->hdr.length = htons(plen);
Ursula Brauna046d572017-01-09 16:55:16 +0100467
Ursula Braun6bb14e482020-09-10 18:48:22 +0200468 memcpy(trl->eyecatcher, SMC_EYECATCHER, sizeof(SMC_EYECATCHER));
Ursula Brauna046d572017-01-09 16:55:16 +0100469 memset(&msg, 0, sizeof(msg));
Karsten Graul1a26d022018-03-16 15:06:40 +0100470 i = 0;
Ursula Braun6bb14e482020-09-10 18:48:22 +0200471 vec[i].iov_base = pclc_base;
472 vec[i++].iov_len = sizeof(*pclc_base);
Hans Wippelc758dfd2018-06-28 19:05:09 +0200473 if (smc_type == SMC_TYPE_D || smc_type == SMC_TYPE_B) {
Ursula Braun6bb14e482020-09-10 18:48:22 +0200474 vec[i].iov_base = pclc_smcd;
475 vec[i++].iov_len = sizeof(*pclc_smcd);
Hans Wippelc758dfd2018-06-28 19:05:09 +0200476 }
Ursula Braun6bb14e482020-09-10 18:48:22 +0200477 vec[i].iov_base = pclc_prfx;
478 vec[i++].iov_len = sizeof(*pclc_prfx);
479 if (pclc_prfx->ipv6_prefixes_cnt > 0) {
480 vec[i].iov_base = ipv6_prfx;
481 vec[i++].iov_len = pclc_prfx->ipv6_prefixes_cnt *
Karsten Graul1a26d022018-03-16 15:06:40 +0100482 sizeof(ipv6_prfx[0]);
483 }
Ursula Braun6bb14e482020-09-10 18:48:22 +0200484 vec[i].iov_base = trl;
485 vec[i++].iov_len = sizeof(*trl);
Ursula Brauna046d572017-01-09 16:55:16 +0100486 /* due to the few bytes needed for clc-handshake this cannot block */
Karsten Graul1a26d022018-03-16 15:06:40 +0100487 len = kernel_sendmsg(smc->clcsock, &msg, vec, i, plen);
YueHaibing38189772018-09-18 15:46:38 +0200488 if (len < 0) {
489 smc->sk.sk_err = smc->clcsock->sk->sk_err;
490 reason_code = -smc->sk.sk_err;
Ursula Braun6bb14e482020-09-10 18:48:22 +0200491 } else if (len < ntohs(pclc_base->hdr.length)) {
YueHaibing38189772018-09-18 15:46:38 +0200492 reason_code = -ENETUNREACH;
493 smc->sk.sk_err = -reason_code;
Ursula Brauna046d572017-01-09 16:55:16 +0100494 }
495
Ursula Braun6bb14e482020-09-10 18:48:22 +0200496 kfree(pclc);
Ursula Brauna046d572017-01-09 16:55:16 +0100497 return reason_code;
498}
499
Ursula Braun3d9725a2020-09-10 18:48:23 +0200500/* build and send CLC CONFIRM / ACCEPT message */
501static int smc_clc_send_confirm_accept(struct smc_sock *smc,
502 struct smc_clc_msg_accept_confirm *clc,
503 int first_contact)
504{
505 struct smc_connection *conn = &smc->conn;
506 struct msghdr msg;
507 struct kvec vec;
508
509 /* send SMC Confirm CLC msg */
510 clc->hdr.version = SMC_V1; /* SMC version */
511 if (first_contact)
512 clc->hdr.flag = 1;
513 if (conn->lgr->is_smcd) {
514 /* SMC-D specific settings */
515 memcpy(clc->hdr.eyecatcher, SMCD_EYECATCHER,
516 sizeof(SMCD_EYECATCHER));
517 clc->hdr.path = SMC_TYPE_D;
518 clc->hdr.length = htons(SMCD_CLC_ACCEPT_CONFIRM_LEN);
519 clc->d0.gid = conn->lgr->smcd->local_gid;
520 clc->d0.token = conn->rmb_desc->token;
521 clc->d0.dmbe_size = conn->rmbe_size_short;
522 clc->d0.dmbe_idx = 0;
523 memcpy(&clc->d0.linkid, conn->lgr->id, SMC_LGR_ID_SIZE);
524 memcpy(clc->d0.smcd_trl.eyecatcher, SMCD_EYECATCHER,
525 sizeof(SMCD_EYECATCHER));
526 } else {
527 struct smc_link *link = conn->lnk;
528
529 /* SMC-R specific settings */
530 link = conn->lnk;
531 memcpy(clc->hdr.eyecatcher, SMC_EYECATCHER,
532 sizeof(SMC_EYECATCHER));
533 clc->hdr.path = SMC_TYPE_R;
534 clc->hdr.length = htons(SMCR_CLC_ACCEPT_CONFIRM_LEN);
535 memcpy(clc->r0.lcl.id_for_peer, local_systemid,
536 sizeof(local_systemid));
537 memcpy(&clc->r0.lcl.gid, link->gid, SMC_GID_SIZE);
538 memcpy(&clc->r0.lcl.mac, &link->smcibdev->mac[link->ibport - 1],
539 ETH_ALEN);
540 hton24(clc->r0.qpn, link->roce_qp->qp_num);
541 clc->r0.rmb_rkey =
542 htonl(conn->rmb_desc->mr_rx[link->link_idx]->rkey);
543 clc->r0.rmbe_idx = 1; /* for now: 1 RMB = 1 RMBE */
544 clc->r0.rmbe_alert_token = htonl(conn->alert_token_local);
545 switch (clc->hdr.type) {
546 case SMC_CLC_ACCEPT:
547 clc->r0.qp_mtu = link->path_mtu;
548 break;
549 case SMC_CLC_CONFIRM:
550 clc->r0.qp_mtu = min(link->path_mtu, link->peer_mtu);
551 break;
552 }
553 clc->r0.rmbe_size = conn->rmbe_size_short;
554 clc->r0.rmb_dma_addr = cpu_to_be64((u64)sg_dma_address
555 (conn->rmb_desc->sgt[link->link_idx].sgl));
556 hton24(clc->r0.psn, link->psn_initial);
557 memcpy(clc->r0.smcr_trl.eyecatcher, SMC_EYECATCHER,
558 sizeof(SMC_EYECATCHER));
559 }
560
561 memset(&msg, 0, sizeof(msg));
562 vec.iov_base = clc;
563 vec.iov_len = ntohs(clc->hdr.length);
564 return kernel_sendmsg(smc->clcsock, &msg, &vec, 1,
565 ntohs(clc->hdr.length));
566}
567
Ursula Brauna046d572017-01-09 16:55:16 +0100568/* send CLC CONFIRM message across internal TCP socket */
569int smc_clc_send_confirm(struct smc_sock *smc)
570{
571 struct smc_clc_msg_accept_confirm cclc;
572 int reason_code = 0;
Ursula Brauna046d572017-01-09 16:55:16 +0100573 int len;
574
575 /* send SMC Confirm CLC msg */
576 memset(&cclc, 0, sizeof(cclc));
Ursula Brauna046d572017-01-09 16:55:16 +0100577 cclc.hdr.type = SMC_CLC_CONFIRM;
Ursula Braun3d9725a2020-09-10 18:48:23 +0200578 len = smc_clc_send_confirm_accept(smc, &cclc, 0);
Hans Wippelc758dfd2018-06-28 19:05:09 +0200579 if (len < ntohs(cclc.hdr.length)) {
Ursula Brauna046d572017-01-09 16:55:16 +0100580 if (len >= 0) {
581 reason_code = -ENETUNREACH;
582 smc->sk.sk_err = -reason_code;
583 } else {
584 smc->sk.sk_err = smc->clcsock->sk->sk_err;
585 reason_code = -smc->sk.sk_err;
586 }
587 }
588 return reason_code;
589}
590
591/* send CLC ACCEPT message across internal TCP socket */
Ursula Braun5ac54d82020-09-10 18:48:21 +0200592int smc_clc_send_accept(struct smc_sock *new_smc, bool srv_first_contact)
Ursula Brauna046d572017-01-09 16:55:16 +0100593{
594 struct smc_clc_msg_accept_confirm aclc;
Ursula Brauna046d572017-01-09 16:55:16 +0100595 int len;
596
597 memset(&aclc, 0, sizeof(aclc));
Ursula Brauna046d572017-01-09 16:55:16 +0100598 aclc.hdr.type = SMC_CLC_ACCEPT;
Ursula Braun3d9725a2020-09-10 18:48:23 +0200599 len = smc_clc_send_confirm_accept(new_smc, &aclc, srv_first_contact);
Ursula Braun6ae36bf2018-11-22 10:26:36 +0100600 if (len < ntohs(aclc.hdr.length))
601 len = len >= 0 ? -EPROTO : -new_smc->clcsock->sk->sk_err;
Ursula Brauna046d572017-01-09 16:55:16 +0100602
Ursula Braun6ae36bf2018-11-22 10:26:36 +0100603 return len > 0 ? 0 : len;
Ursula Brauna046d572017-01-09 16:55:16 +0100604}