blob: 37e3e7a262da4651dc9d5a3c003470fbc51728c1 [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:
Ursula Braunf1eb02f2020-09-26 12:44:20 +020067 if (clcm->typev1 != SMC_TYPE_R && clcm->typev1 != 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;
Ursula Braunf1eb02f2020-09-26 12:44:20 +020070 if ((clcm->typev1 == SMC_TYPE_R &&
Hans Wippelc758dfd2018-06-28 19:05:09 +020071 ntohs(clc->hdr.length) != SMCR_CLC_ACCEPT_CONFIRM_LEN) ||
Ursula Braunf1eb02f2020-09-26 12:44:20 +020072 (clcm->typev1 == SMC_TYPE_D &&
Hans Wippelc758dfd2018-06-28 19:05:09 +020073 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 Braunf1eb02f2020-09-26 12:44:20 +0200330 if (clcm->type == SMC_CLC_PROPOSAL && clcm->typev1 == SMC_TYPE_N)
Ursula Braunfb4f7922020-07-08 17:05:15 +0200331 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 Braunf1eb02f2020-09-26 12:44:20 +0200368 if (((struct smc_clc_msg_decline *)buf)->hdr.typev2 &
369 SMC_FIRST_CONTACT_MASK) {
Karsten Graul517c3002018-05-15 17:05:03 +0200370 smc->conn.lgr->sync_err = 1;
Karsten Graul5f78fe92020-02-17 16:24:54 +0100371 smc_lgr_terminate_sched(smc->conn.lgr);
Ursula Braunbfbedfd2017-09-21 09:16:32 +0200372 }
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100373 }
374
Ursula Brauna046d572017-01-09 16:55:16 +0100375out:
Ursula Braun2b59f582018-11-22 10:26:39 +0100376 clc_sk->sk_rcvtimeo = rcvtimeo;
Ursula Brauna046d572017-01-09 16:55:16 +0100377 return reason_code;
378}
379
380/* send CLC DECLINE message across internal TCP socket */
Ursula Braunbfbedfd2017-09-21 09:16:32 +0200381int smc_clc_send_decline(struct smc_sock *smc, u32 peer_diag_info)
Ursula Brauna046d572017-01-09 16:55:16 +0100382{
383 struct smc_clc_msg_decline dclc;
384 struct msghdr msg;
385 struct kvec vec;
386 int len;
387
388 memset(&dclc, 0, sizeof(dclc));
389 memcpy(dclc.hdr.eyecatcher, SMC_EYECATCHER, sizeof(SMC_EYECATCHER));
390 dclc.hdr.type = SMC_CLC_DECLINE;
391 dclc.hdr.length = htons(sizeof(struct smc_clc_msg_decline));
Ursula Braun5ac54d82020-09-10 18:48:21 +0200392 dclc.hdr.version = SMC_V1;
Ursula Braunf1eb02f2020-09-26 12:44:20 +0200393 dclc.hdr.typev2 = (peer_diag_info == SMC_CLC_DECL_SYNCERR) ?
394 SMC_FIRST_CONTACT_MASK : 0;
Hans Wippela082ec82020-02-25 22:41:22 +0100395 if ((!smc->conn.lgr || !smc->conn.lgr->is_smcd) &&
396 smc_ib_is_valid_local_systemid())
Ursula Braun369537c2020-02-14 08:59:00 +0100397 memcpy(dclc.id_for_peer, local_systemid,
398 sizeof(local_systemid));
Ursula Brauna046d572017-01-09 16:55:16 +0100399 dclc.peer_diagnosis = htonl(peer_diag_info);
400 memcpy(dclc.trl.eyecatcher, SMC_EYECATCHER, sizeof(SMC_EYECATCHER));
401
402 memset(&msg, 0, sizeof(msg));
403 vec.iov_base = &dclc;
404 vec.iov_len = sizeof(struct smc_clc_msg_decline);
405 len = kernel_sendmsg(smc->clcsock, &msg, &vec, 1,
406 sizeof(struct smc_clc_msg_decline));
Ursula Braun14d22d42019-01-30 18:51:00 +0100407 if (len < 0 || len < sizeof(struct smc_clc_msg_decline))
Ursula Braun6ae36bf2018-11-22 10:26:36 +0100408 len = -EPROTO;
409 return len > 0 ? 0 : len;
Ursula Brauna046d572017-01-09 16:55:16 +0100410}
411
412/* send CLC PROPOSAL message across internal TCP socket */
Ursula Braund70bf4f2020-09-26 12:44:27 +0200413int smc_clc_send_proposal(struct smc_sock *smc, struct smc_init_info *ini)
Ursula Brauna046d572017-01-09 16:55:16 +0100414{
Ursula Braun6bb14e482020-09-10 18:48:22 +0200415 struct smc_clc_msg_proposal_prefix *pclc_prfx;
416 struct smc_clc_msg_proposal *pclc_base;
417 struct smc_clc_msg_proposal_area *pclc;
418 struct smc_clc_ipv6_prefix *ipv6_prfx;
419 struct smc_clc_msg_smcd *pclc_smcd;
420 struct smc_clc_msg_trail *trl;
Karsten Graul1a26d022018-03-16 15:06:40 +0100421 int len, i, plen, rc;
Ursula Brauna046d572017-01-09 16:55:16 +0100422 int reason_code = 0;
Hans Wippelc758dfd2018-06-28 19:05:09 +0200423 struct kvec vec[5];
Ursula Brauna046d572017-01-09 16:55:16 +0100424 struct msghdr msg;
Ursula Brauna046d572017-01-09 16:55:16 +0100425
Ursula Braun6bb14e482020-09-10 18:48:22 +0200426 pclc = kzalloc(sizeof(*pclc), GFP_KERNEL);
427 if (!pclc)
428 return -ENOMEM;
429
430 pclc_base = &pclc->pclc_base;
431 pclc_smcd = &pclc->pclc_smcd;
432 pclc_prfx = &pclc->pclc_prfx;
433 ipv6_prfx = pclc->pclc_prfx_ipv6;
434 trl = &pclc->pclc_trl;
435
Karsten Graulc246d942018-03-16 15:06:39 +0100436 /* retrieve ip prefixes for CLC proposal msg */
Ursula Braun6bb14e482020-09-10 18:48:22 +0200437 rc = smc_clc_prfx_set(smc->clcsock, pclc_prfx, ipv6_prfx);
438 if (rc) {
439 kfree(pclc);
Karsten Graulc246d942018-03-16 15:06:39 +0100440 return SMC_CLC_DECL_CNFERR; /* configuration error */
Ursula Braun6bb14e482020-09-10 18:48:22 +0200441 }
Karsten Graulc246d942018-03-16 15:06:39 +0100442
Ursula Brauna046d572017-01-09 16:55:16 +0100443 /* send SMC Proposal CLC message */
Ursula Braun6bb14e482020-09-10 18:48:22 +0200444 plen = sizeof(*pclc_base) + sizeof(*pclc_prfx) +
445 (pclc_prfx->ipv6_prefixes_cnt * sizeof(ipv6_prfx[0])) +
446 sizeof(*trl);
447 memcpy(pclc_base->hdr.eyecatcher, SMC_EYECATCHER,
448 sizeof(SMC_EYECATCHER));
449 pclc_base->hdr.type = SMC_CLC_PROPOSAL;
450 pclc_base->hdr.version = SMC_V1; /* SMC version */
Ursula Braund70bf4f2020-09-26 12:44:27 +0200451 pclc_base->hdr.typev1 = ini->smc_type_v1;
452 if (smcr_indicated(ini->smc_type_v1)) {
Hans Wippelc758dfd2018-06-28 19:05:09 +0200453 /* add SMC-R specifics */
Ursula Braun6bb14e482020-09-10 18:48:22 +0200454 memcpy(pclc_base->lcl.id_for_peer, local_systemid,
Hans Wippelc758dfd2018-06-28 19:05:09 +0200455 sizeof(local_systemid));
Ursula Braun6bb14e482020-09-10 18:48:22 +0200456 memcpy(pclc_base->lcl.gid, ini->ib_gid, SMC_GID_SIZE);
457 memcpy(pclc_base->lcl.mac, &ini->ib_dev->mac[ini->ib_port - 1],
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200458 ETH_ALEN);
Ursula Braun6bb14e482020-09-10 18:48:22 +0200459 pclc_base->iparea_offset = htons(0);
Hans Wippelc758dfd2018-06-28 19:05:09 +0200460 }
Ursula Braund70bf4f2020-09-26 12:44:27 +0200461 if (smcd_indicated(ini->smc_type_v1)) {
Hans Wippelc758dfd2018-06-28 19:05:09 +0200462 /* add SMC-D specifics */
Ursula Braun6bb14e482020-09-10 18:48:22 +0200463 plen += sizeof(*pclc_smcd);
464 pclc_base->iparea_offset = htons(sizeof(*pclc_smcd));
Ursula Braun3fc64932020-09-26 12:44:23 +0200465 pclc_smcd->gid = ini->ism_dev[0]->local_gid;
Hans Wippelc758dfd2018-06-28 19:05:09 +0200466 }
Ursula Braun6bb14e482020-09-10 18:48:22 +0200467 pclc_base->hdr.length = htons(plen);
Ursula Brauna046d572017-01-09 16:55:16 +0100468
Ursula Braun6bb14e482020-09-10 18:48:22 +0200469 memcpy(trl->eyecatcher, SMC_EYECATCHER, sizeof(SMC_EYECATCHER));
Ursula Brauna046d572017-01-09 16:55:16 +0100470 memset(&msg, 0, sizeof(msg));
Karsten Graul1a26d022018-03-16 15:06:40 +0100471 i = 0;
Ursula Braun6bb14e482020-09-10 18:48:22 +0200472 vec[i].iov_base = pclc_base;
473 vec[i++].iov_len = sizeof(*pclc_base);
Ursula Braund70bf4f2020-09-26 12:44:27 +0200474 if (smcd_indicated(ini->smc_type_v1)) {
Ursula Braun6bb14e482020-09-10 18:48:22 +0200475 vec[i].iov_base = pclc_smcd;
476 vec[i++].iov_len = sizeof(*pclc_smcd);
Hans Wippelc758dfd2018-06-28 19:05:09 +0200477 }
Ursula Braun6bb14e482020-09-10 18:48:22 +0200478 vec[i].iov_base = pclc_prfx;
479 vec[i++].iov_len = sizeof(*pclc_prfx);
480 if (pclc_prfx->ipv6_prefixes_cnt > 0) {
481 vec[i].iov_base = ipv6_prfx;
482 vec[i++].iov_len = pclc_prfx->ipv6_prefixes_cnt *
Karsten Graul1a26d022018-03-16 15:06:40 +0100483 sizeof(ipv6_prfx[0]);
484 }
Ursula Braun6bb14e482020-09-10 18:48:22 +0200485 vec[i].iov_base = trl;
486 vec[i++].iov_len = sizeof(*trl);
Ursula Brauna046d572017-01-09 16:55:16 +0100487 /* due to the few bytes needed for clc-handshake this cannot block */
Karsten Graul1a26d022018-03-16 15:06:40 +0100488 len = kernel_sendmsg(smc->clcsock, &msg, vec, i, plen);
YueHaibing38189772018-09-18 15:46:38 +0200489 if (len < 0) {
490 smc->sk.sk_err = smc->clcsock->sk->sk_err;
491 reason_code = -smc->sk.sk_err;
Ursula Braun6bb14e482020-09-10 18:48:22 +0200492 } else if (len < ntohs(pclc_base->hdr.length)) {
YueHaibing38189772018-09-18 15:46:38 +0200493 reason_code = -ENETUNREACH;
494 smc->sk.sk_err = -reason_code;
Ursula Brauna046d572017-01-09 16:55:16 +0100495 }
496
Ursula Braun6bb14e482020-09-10 18:48:22 +0200497 kfree(pclc);
Ursula Brauna046d572017-01-09 16:55:16 +0100498 return reason_code;
499}
500
Ursula Braun3d9725a2020-09-10 18:48:23 +0200501/* build and send CLC CONFIRM / ACCEPT message */
502static int smc_clc_send_confirm_accept(struct smc_sock *smc,
503 struct smc_clc_msg_accept_confirm *clc,
504 int first_contact)
505{
506 struct smc_connection *conn = &smc->conn;
Ursula Braune15c6c42020-09-26 12:44:22 +0200507 struct smc_clc_msg_trail trl;
508 struct kvec vec[2];
Ursula Braun3d9725a2020-09-10 18:48:23 +0200509 struct msghdr msg;
Ursula Braune15c6c42020-09-26 12:44:22 +0200510 int i;
Ursula Braun3d9725a2020-09-10 18:48:23 +0200511
512 /* send SMC Confirm CLC msg */
513 clc->hdr.version = SMC_V1; /* SMC version */
514 if (first_contact)
Ursula Braunf1eb02f2020-09-26 12:44:20 +0200515 clc->hdr.typev2 |= SMC_FIRST_CONTACT_MASK;
Ursula Braun3d9725a2020-09-10 18:48:23 +0200516 if (conn->lgr->is_smcd) {
517 /* SMC-D specific settings */
518 memcpy(clc->hdr.eyecatcher, SMCD_EYECATCHER,
519 sizeof(SMCD_EYECATCHER));
Ursula Braunf1eb02f2020-09-26 12:44:20 +0200520 clc->hdr.typev1 = SMC_TYPE_D;
Ursula Braun3d9725a2020-09-10 18:48:23 +0200521 clc->hdr.length = htons(SMCD_CLC_ACCEPT_CONFIRM_LEN);
522 clc->d0.gid = conn->lgr->smcd->local_gid;
523 clc->d0.token = conn->rmb_desc->token;
524 clc->d0.dmbe_size = conn->rmbe_size_short;
525 clc->d0.dmbe_idx = 0;
526 memcpy(&clc->d0.linkid, conn->lgr->id, SMC_LGR_ID_SIZE);
Ursula Braune15c6c42020-09-26 12:44:22 +0200527 memcpy(trl.eyecatcher, SMCD_EYECATCHER,
Ursula Braun3d9725a2020-09-10 18:48:23 +0200528 sizeof(SMCD_EYECATCHER));
529 } else {
530 struct smc_link *link = conn->lnk;
531
532 /* SMC-R specific settings */
533 link = conn->lnk;
534 memcpy(clc->hdr.eyecatcher, SMC_EYECATCHER,
535 sizeof(SMC_EYECATCHER));
Ursula Braunf1eb02f2020-09-26 12:44:20 +0200536 clc->hdr.typev1 = SMC_TYPE_R;
Ursula Braun3d9725a2020-09-10 18:48:23 +0200537 clc->hdr.length = htons(SMCR_CLC_ACCEPT_CONFIRM_LEN);
538 memcpy(clc->r0.lcl.id_for_peer, local_systemid,
539 sizeof(local_systemid));
540 memcpy(&clc->r0.lcl.gid, link->gid, SMC_GID_SIZE);
541 memcpy(&clc->r0.lcl.mac, &link->smcibdev->mac[link->ibport - 1],
542 ETH_ALEN);
543 hton24(clc->r0.qpn, link->roce_qp->qp_num);
544 clc->r0.rmb_rkey =
545 htonl(conn->rmb_desc->mr_rx[link->link_idx]->rkey);
546 clc->r0.rmbe_idx = 1; /* for now: 1 RMB = 1 RMBE */
547 clc->r0.rmbe_alert_token = htonl(conn->alert_token_local);
548 switch (clc->hdr.type) {
549 case SMC_CLC_ACCEPT:
550 clc->r0.qp_mtu = link->path_mtu;
551 break;
552 case SMC_CLC_CONFIRM:
553 clc->r0.qp_mtu = min(link->path_mtu, link->peer_mtu);
554 break;
555 }
556 clc->r0.rmbe_size = conn->rmbe_size_short;
557 clc->r0.rmb_dma_addr = cpu_to_be64((u64)sg_dma_address
558 (conn->rmb_desc->sgt[link->link_idx].sgl));
559 hton24(clc->r0.psn, link->psn_initial);
Ursula Braune15c6c42020-09-26 12:44:22 +0200560 memcpy(trl.eyecatcher, SMC_EYECATCHER, sizeof(SMC_EYECATCHER));
Ursula Braun3d9725a2020-09-10 18:48:23 +0200561 }
562
563 memset(&msg, 0, sizeof(msg));
Ursula Braune15c6c42020-09-26 12:44:22 +0200564 i = 0;
565 vec[i].iov_base = clc;
566 vec[i++].iov_len = (clc->hdr.typev1 == SMC_TYPE_D ?
567 SMCD_CLC_ACCEPT_CONFIRM_LEN :
568 SMCR_CLC_ACCEPT_CONFIRM_LEN) -
569 sizeof(trl);
570 vec[i].iov_base = &trl;
571 vec[i++].iov_len = sizeof(trl);
572 return kernel_sendmsg(smc->clcsock, &msg, vec, 1,
Ursula Braun3d9725a2020-09-10 18:48:23 +0200573 ntohs(clc->hdr.length));
574}
575
Ursula Brauna046d572017-01-09 16:55:16 +0100576/* send CLC CONFIRM message across internal TCP socket */
577int smc_clc_send_confirm(struct smc_sock *smc)
578{
579 struct smc_clc_msg_accept_confirm cclc;
580 int reason_code = 0;
Ursula Brauna046d572017-01-09 16:55:16 +0100581 int len;
582
583 /* send SMC Confirm CLC msg */
584 memset(&cclc, 0, sizeof(cclc));
Ursula Brauna046d572017-01-09 16:55:16 +0100585 cclc.hdr.type = SMC_CLC_CONFIRM;
Ursula Braun3d9725a2020-09-10 18:48:23 +0200586 len = smc_clc_send_confirm_accept(smc, &cclc, 0);
Hans Wippelc758dfd2018-06-28 19:05:09 +0200587 if (len < ntohs(cclc.hdr.length)) {
Ursula Brauna046d572017-01-09 16:55:16 +0100588 if (len >= 0) {
589 reason_code = -ENETUNREACH;
590 smc->sk.sk_err = -reason_code;
591 } else {
592 smc->sk.sk_err = smc->clcsock->sk->sk_err;
593 reason_code = -smc->sk.sk_err;
594 }
595 }
596 return reason_code;
597}
598
599/* send CLC ACCEPT message across internal TCP socket */
Ursula Braun5ac54d82020-09-10 18:48:21 +0200600int smc_clc_send_accept(struct smc_sock *new_smc, bool srv_first_contact)
Ursula Brauna046d572017-01-09 16:55:16 +0100601{
602 struct smc_clc_msg_accept_confirm aclc;
Ursula Brauna046d572017-01-09 16:55:16 +0100603 int len;
604
605 memset(&aclc, 0, sizeof(aclc));
Ursula Brauna046d572017-01-09 16:55:16 +0100606 aclc.hdr.type = SMC_CLC_ACCEPT;
Ursula Braun3d9725a2020-09-10 18:48:23 +0200607 len = smc_clc_send_confirm_accept(new_smc, &aclc, srv_first_contact);
Ursula Braun6ae36bf2018-11-22 10:26:36 +0100608 if (len < ntohs(aclc.hdr.length))
609 len = len >= 0 ? -EPROTO : -new_smc->clcsock->sk->sk_err;
Ursula Brauna046d572017-01-09 16:55:16 +0100610
Ursula Braun6ae36bf2018-11-22 10:26:36 +0100611 return len > 0 ? 0 : len;
Ursula Brauna046d572017-01-09 16:55:16 +0100612}