Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 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 | * |
Karsten Graul | 1a26d02 | 2018-03-16 15:06:40 +0100 | [diff] [blame] | 8 | * Copyright IBM Corp. 2016, 2018 |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 9 | * |
| 10 | * Author(s): Ursula Braun <ubraun@linux.vnet.ibm.com> |
| 11 | */ |
| 12 | |
| 13 | #include <linux/in.h> |
Karsten Graul | 696cd30 | 2018-03-01 13:51:27 +0100 | [diff] [blame] | 14 | #include <linux/inetdevice.h> |
Ursula Braun | 143c017 | 2017-01-12 14:57:15 +0100 | [diff] [blame] | 15 | #include <linux/if_ether.h> |
Ingo Molnar | c3edc40 | 2017-02-02 08:35:14 +0100 | [diff] [blame] | 16 | #include <linux/sched/signal.h> |
| 17 | |
Karsten Graul | 1a26d02 | 2018-03-16 15:06:40 +0100 | [diff] [blame] | 18 | #include <net/addrconf.h> |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 19 | #include <net/sock.h> |
| 20 | #include <net/tcp.h> |
| 21 | |
| 22 | #include "smc.h" |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 23 | #include "smc_core.h" |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 24 | #include "smc_clc.h" |
| 25 | #include "smc_ib.h" |
Hans Wippel | c758dfd | 2018-06-28 19:05:09 +0200 | [diff] [blame] | 26 | #include "smc_ism.h" |
| 27 | |
| 28 | #define SMCR_CLC_ACCEPT_CONFIRM_LEN 68 |
| 29 | #define SMCD_CLC_ACCEPT_CONFIRM_LEN 48 |
Ursula Braun | fb4f792 | 2020-07-08 17:05:15 +0200 | [diff] [blame] | 30 | #define SMC_CLC_RECV_BUF_LEN 100 |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 31 | |
Stefan Raspl | 0f62712 | 2018-03-01 13:51:26 +0100 | [diff] [blame] | 32 | /* eye catcher "SMCR" EBCDIC for CLC messages */ |
| 33 | static const char SMC_EYECATCHER[4] = {'\xe2', '\xd4', '\xc3', '\xd9'}; |
Hans Wippel | c758dfd | 2018-06-28 19:05:09 +0200 | [diff] [blame] | 34 | /* eye catcher "SMCD" EBCDIC for CLC messages */ |
| 35 | static const char SMCD_EYECATCHER[4] = {'\xe2', '\xd4', '\xc3', '\xc4'}; |
Stefan Raspl | 0f62712 | 2018-03-01 13:51:26 +0100 | [diff] [blame] | 36 | |
Ursula Braun | 8c3dca3 | 2020-09-26 12:44:28 +0200 | [diff] [blame^] | 37 | /* check arriving CLC proposal */ |
| 38 | static 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 Braun | e7b7a64 | 2017-12-07 13:38:49 +0100 | [diff] [blame] | 78 | /* check if received message has a correct header length and contains valid |
| 79 | * heading and trailing eyecatchers |
| 80 | */ |
Ursula Braun | fb4f792 | 2020-07-08 17:05:15 +0200 | [diff] [blame] | 81 | static bool smc_clc_msg_hdr_valid(struct smc_clc_msg_hdr *clcm, bool check_trl) |
Ursula Braun | e7b7a64 | 2017-12-07 13:38:49 +0100 | [diff] [blame] | 82 | { |
Ursula Braun | e7b7a64 | 2017-12-07 13:38:49 +0100 | [diff] [blame] | 83 | 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 Wippel | c758dfd | 2018-06-28 19:05:09 +0200 | [diff] [blame] | 88 | if (memcmp(clcm->eyecatcher, SMC_EYECATCHER, sizeof(SMC_EYECATCHER)) && |
| 89 | memcmp(clcm->eyecatcher, SMCD_EYECATCHER, sizeof(SMCD_EYECATCHER))) |
Ursula Braun | e7b7a64 | 2017-12-07 13:38:49 +0100 | [diff] [blame] | 90 | return false; |
| 91 | switch (clcm->type) { |
| 92 | case SMC_CLC_PROPOSAL: |
| 93 | pclc = (struct smc_clc_msg_proposal *)clcm; |
Ursula Braun | 8c3dca3 | 2020-09-26 12:44:28 +0200 | [diff] [blame^] | 94 | if (!smc_clc_msg_prop_valid(pclc)) |
Ursula Braun | e7b7a64 | 2017-12-07 13:38:49 +0100 | [diff] [blame] | 95 | 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 Braun | f1eb02f | 2020-09-26 12:44:20 +0200 | [diff] [blame] | 101 | if (clcm->typev1 != SMC_TYPE_R && clcm->typev1 != SMC_TYPE_D) |
Ursula Braun | e7b7a64 | 2017-12-07 13:38:49 +0100 | [diff] [blame] | 102 | return false; |
Hans Wippel | c758dfd | 2018-06-28 19:05:09 +0200 | [diff] [blame] | 103 | clc = (struct smc_clc_msg_accept_confirm *)clcm; |
Ursula Braun | f1eb02f | 2020-09-26 12:44:20 +0200 | [diff] [blame] | 104 | if ((clcm->typev1 == SMC_TYPE_R && |
Hans Wippel | c758dfd | 2018-06-28 19:05:09 +0200 | [diff] [blame] | 105 | ntohs(clc->hdr.length) != SMCR_CLC_ACCEPT_CONFIRM_LEN) || |
Ursula Braun | f1eb02f | 2020-09-26 12:44:20 +0200 | [diff] [blame] | 106 | (clcm->typev1 == SMC_TYPE_D && |
Hans Wippel | c758dfd | 2018-06-28 19:05:09 +0200 | [diff] [blame] | 107 | 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 Braun | e7b7a64 | 2017-12-07 13:38:49 +0100 | [diff] [blame] | 111 | 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 Braun | fb4f792 | 2020-07-08 17:05:15 +0200 | [diff] [blame] | 121 | if (check_trl && |
| 122 | memcmp(trl->eyecatcher, SMC_EYECATCHER, sizeof(SMC_EYECATCHER)) && |
Hans Wippel | c758dfd | 2018-06-28 19:05:09 +0200 | [diff] [blame] | 123 | memcmp(trl->eyecatcher, SMCD_EYECATCHER, sizeof(SMCD_EYECATCHER))) |
Ursula Braun | e7b7a64 | 2017-12-07 13:38:49 +0100 | [diff] [blame] | 124 | return false; |
| 125 | return true; |
| 126 | } |
| 127 | |
Karsten Graul | c246d94 | 2018-03-16 15:06:39 +0100 | [diff] [blame] | 128 | /* find ipv4 addr on device and get the prefix len, fill CLC proposal msg */ |
| 129 | static 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 Westphal | cd5a411 | 2019-05-31 18:27:07 +0200 | [diff] [blame] | 133 | const struct in_ifaddr *ifa; |
Karsten Graul | c246d94 | 2018-03-16 15:06:39 +0100 | [diff] [blame] | 134 | |
| 135 | if (!in_dev) |
| 136 | return -ENODEV; |
Florian Westphal | cd5a411 | 2019-05-31 18:27:07 +0200 | [diff] [blame] | 137 | |
| 138 | in_dev_for_each_ifa_rcu(ifa, in_dev) { |
Karsten Graul | c246d94 | 2018-03-16 15:06:39 +0100 | [diff] [blame] | 139 | 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 Westphal | cd5a411 | 2019-05-31 18:27:07 +0200 | [diff] [blame] | 145 | } |
Karsten Graul | c246d94 | 2018-03-16 15:06:39 +0100 | [diff] [blame] | 146 | return -ENOENT; |
| 147 | } |
| 148 | |
Karsten Graul | 1a26d02 | 2018-03-16 15:06:40 +0100 | [diff] [blame] | 149 | /* fill CLC proposal msg with ipv6 prefixes from device */ |
| 150 | static 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 Graul | c246d94 | 2018-03-16 15:06:39 +0100 | [diff] [blame] | 179 | /* retrieve and set prefixes in CLC proposal msg */ |
| 180 | static int smc_clc_prfx_set(struct socket *clcsock, |
Karsten Graul | 1a26d02 | 2018-03-16 15:06:40 +0100 | [diff] [blame] | 181 | struct smc_clc_msg_proposal_prefix *prop, |
| 182 | struct smc_clc_ipv6_prefix *ipv6_prfx) |
Karsten Graul | 696cd30 | 2018-03-01 13:51:27 +0100 | [diff] [blame] | 183 | { |
| 184 | struct dst_entry *dst = sk_dst_get(clcsock->sk); |
Karsten Graul | c246d94 | 2018-03-16 15:06:39 +0100 | [diff] [blame] | 185 | struct sockaddr_storage addrs; |
Karsten Graul | 1a26d02 | 2018-03-16 15:06:40 +0100 | [diff] [blame] | 186 | struct sockaddr_in6 *addr6; |
Karsten Graul | c246d94 | 2018-03-16 15:06:39 +0100 | [diff] [blame] | 187 | struct sockaddr_in *addr; |
| 188 | int rc = -ENOENT; |
| 189 | |
Karsten Graul | c246d94 | 2018-03-16 15:06:39 +0100 | [diff] [blame] | 190 | 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 Graul | 1a26d02 | 2018-03-16 15:06:40 +0100 | [diff] [blame] | 201 | addr6 = (struct sockaddr_in6 *)&addrs; |
Karsten Graul | c246d94 | 2018-03-16 15:06:39 +0100 | [diff] [blame] | 202 | 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 Graul | 1a26d02 | 2018-03-16 15:06:40 +0100 | [diff] [blame] | 207 | } 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 Graul | c246d94 | 2018-03-16 15:06:39 +0100 | [diff] [blame] | 214 | } |
| 215 | rcu_read_unlock(); |
| 216 | out_rel: |
| 217 | dst_release(dst); |
| 218 | out: |
| 219 | return rc; |
| 220 | } |
| 221 | |
| 222 | /* match ipv4 addrs of dev against addr in CLC proposal */ |
| 223 | static 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 Westphal | cd5a411 | 2019-05-31 18:27:07 +0200 | [diff] [blame] | 227 | const struct in_ifaddr *ifa; |
Karsten Graul | c246d94 | 2018-03-16 15:06:39 +0100 | [diff] [blame] | 228 | |
| 229 | if (!in_dev) |
| 230 | return -ENODEV; |
Florian Westphal | cd5a411 | 2019-05-31 18:27:07 +0200 | [diff] [blame] | 231 | in_dev_for_each_ifa_rcu(ifa, in_dev) { |
Karsten Graul | c246d94 | 2018-03-16 15:06:39 +0100 | [diff] [blame] | 232 | if (prop->prefix_len == inet_mask_len(ifa->ifa_mask) && |
| 233 | inet_ifa_match(prop->outgoing_subnet, ifa)) |
| 234 | return 0; |
Florian Westphal | cd5a411 | 2019-05-31 18:27:07 +0200 | [diff] [blame] | 235 | } |
Karsten Graul | c246d94 | 2018-03-16 15:06:39 +0100 | [diff] [blame] | 236 | |
| 237 | return -ENOENT; |
| 238 | } |
| 239 | |
Karsten Graul | 1a26d02 | 2018-03-16 15:06:40 +0100 | [diff] [blame] | 240 | /* match ipv6 addrs of dev against addrs in CLC proposal */ |
| 241 | static 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 Graul | c246d94 | 2018-03-16 15:06:39 +0100 | [diff] [blame] | 269 | /* check if proposed prefixes match one of our device prefixes */ |
| 270 | int 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 Graul | 1a26d02 | 2018-03-16 15:06:40 +0100 | [diff] [blame] | 274 | int rc; |
Karsten Graul | 696cd30 | 2018-03-01 13:51:27 +0100 | [diff] [blame] | 275 | |
| 276 | if (!dst) { |
| 277 | rc = -ENOTCONN; |
| 278 | goto out; |
| 279 | } |
| 280 | if (!dst->dev) { |
| 281 | rc = -ENODEV; |
| 282 | goto out_rel; |
| 283 | } |
Karsten Graul | 696cd30 | 2018-03-01 13:51:27 +0100 | [diff] [blame] | 284 | rcu_read_lock(); |
Karsten Graul | c246d94 | 2018-03-16 15:06:39 +0100 | [diff] [blame] | 285 | if (!prop->ipv6_prefixes_cnt) |
| 286 | rc = smc_clc_prfx_match4_rcu(dst->dev, prop); |
Karsten Graul | 1a26d02 | 2018-03-16 15:06:40 +0100 | [diff] [blame] | 287 | else |
| 288 | rc = smc_clc_prfx_match6_rcu(dst->dev, prop); |
Karsten Graul | 696cd30 | 2018-03-01 13:51:27 +0100 | [diff] [blame] | 289 | rcu_read_unlock(); |
Karsten Graul | 696cd30 | 2018-03-01 13:51:27 +0100 | [diff] [blame] | 290 | out_rel: |
| 291 | dst_release(dst); |
| 292 | out: |
| 293 | return rc; |
| 294 | } |
| 295 | |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 296 | /* 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 | */ |
| 302 | int smc_clc_wait_msg(struct smc_sock *smc, void *buf, int buflen, |
Ursula Braun | 2b59f58 | 2018-11-22 10:26:39 +0100 | [diff] [blame] | 303 | u8 expected_type, unsigned long timeout) |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 304 | { |
Karsten Graul | f6bdc42 | 2018-07-18 15:22:51 +0200 | [diff] [blame] | 305 | long rcvtimeo = smc->clcsock->sk->sk_rcvtimeo; |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 306 | 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 Viro | d63d271 | 2017-09-20 20:21:22 -0400 | [diff] [blame] | 310 | struct kvec vec = {buf, buflen}; |
Ursula Braun | fb4f792 | 2020-07-08 17:05:15 +0200 | [diff] [blame] | 311 | int len, datlen, recvlen; |
| 312 | bool check_trl = true; |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 313 | 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 Viro | d63d271 | 2017-09-20 20:21:22 -0400 | [diff] [blame] | 319 | /* |
| 320 | * Caller must make sure that buflen is no less than |
| 321 | * sizeof(struct smc_clc_msg_hdr) |
| 322 | */ |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 323 | krflags = MSG_PEEK | MSG_WAITALL; |
Ursula Braun | 2b59f58 | 2018-11-22 10:26:39 +0100 | [diff] [blame] | 324 | clc_sk->sk_rcvtimeo = timeout; |
David Howells | aa563d7 | 2018-10-20 00:57:56 +0100 | [diff] [blame] | 325 | iov_iter_kvec(&msg.msg_iter, READ, &vec, 1, |
Al Viro | d63d271 | 2017-09-20 20:21:22 -0400 | [diff] [blame] | 326 | sizeof(struct smc_clc_msg_hdr)); |
| 327 | len = sock_recvmsg(smc->clcsock, &msg, krflags); |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 328 | 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 Braun | 9ed2855 | 2018-11-22 10:26:37 +0100 | [diff] [blame] | 336 | 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 Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 341 | 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 Braun | 9ed2855 | 2018-11-22 10:26:37 +0100 | [diff] [blame] | 349 | if (len != -EAGAIN || expected_type != SMC_CLC_DECLINE) |
| 350 | smc->sk.sk_err = -len; |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 351 | reason_code = len; |
| 352 | goto out; |
| 353 | } |
| 354 | datlen = ntohs(clcm->length); |
| 355 | if ((len < sizeof(struct smc_clc_msg_hdr)) || |
Ursula Braun | 5ac54d8 | 2020-09-10 18:48:21 +0200 | [diff] [blame] | 356 | (clcm->version < SMC_V1) || |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 357 | ((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 Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 365 | memset(&msg, 0, sizeof(struct msghdr)); |
Ursula Braun | fb4f792 | 2020-07-08 17:05:15 +0200 | [diff] [blame] | 366 | 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 Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 373 | krflags = MSG_WAITALL; |
Al Viro | d63d271 | 2017-09-20 20:21:22 -0400 | [diff] [blame] | 374 | len = sock_recvmsg(smc->clcsock, &msg, krflags); |
Ursula Braun | fb4f792 | 2020-07-08 17:05:15 +0200 | [diff] [blame] | 375 | if (len < recvlen || !smc_clc_msg_hdr_valid(clcm, check_trl)) { |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 376 | smc->sk.sk_err = EPROTO; |
| 377 | reason_code = -EPROTO; |
| 378 | goto out; |
| 379 | } |
Ursula Braun | fb4f792 | 2020-07-08 17:05:15 +0200 | [diff] [blame] | 380 | 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 Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 393 | if (clcm->type == SMC_CLC_DECLINE) { |
Karsten Graul | 603cc14 | 2018-07-25 16:35:32 +0200 | [diff] [blame] | 394 | 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 Braun | f1eb02f | 2020-09-26 12:44:20 +0200 | [diff] [blame] | 399 | if (((struct smc_clc_msg_decline *)buf)->hdr.typev2 & |
| 400 | SMC_FIRST_CONTACT_MASK) { |
Karsten Graul | 517c300 | 2018-05-15 17:05:03 +0200 | [diff] [blame] | 401 | smc->conn.lgr->sync_err = 1; |
Karsten Graul | 5f78fe9 | 2020-02-17 16:24:54 +0100 | [diff] [blame] | 402 | smc_lgr_terminate_sched(smc->conn.lgr); |
Ursula Braun | bfbedfd | 2017-09-21 09:16:32 +0200 | [diff] [blame] | 403 | } |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 404 | } |
| 405 | |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 406 | out: |
Ursula Braun | 2b59f58 | 2018-11-22 10:26:39 +0100 | [diff] [blame] | 407 | clc_sk->sk_rcvtimeo = rcvtimeo; |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 408 | return reason_code; |
| 409 | } |
| 410 | |
| 411 | /* send CLC DECLINE message across internal TCP socket */ |
Ursula Braun | bfbedfd | 2017-09-21 09:16:32 +0200 | [diff] [blame] | 412 | int smc_clc_send_decline(struct smc_sock *smc, u32 peer_diag_info) |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 413 | { |
| 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 Braun | 5ac54d8 | 2020-09-10 18:48:21 +0200 | [diff] [blame] | 423 | dclc.hdr.version = SMC_V1; |
Ursula Braun | f1eb02f | 2020-09-26 12:44:20 +0200 | [diff] [blame] | 424 | dclc.hdr.typev2 = (peer_diag_info == SMC_CLC_DECL_SYNCERR) ? |
| 425 | SMC_FIRST_CONTACT_MASK : 0; |
Hans Wippel | a082ec8 | 2020-02-25 22:41:22 +0100 | [diff] [blame] | 426 | if ((!smc->conn.lgr || !smc->conn.lgr->is_smcd) && |
| 427 | smc_ib_is_valid_local_systemid()) |
Ursula Braun | 369537c | 2020-02-14 08:59:00 +0100 | [diff] [blame] | 428 | memcpy(dclc.id_for_peer, local_systemid, |
| 429 | sizeof(local_systemid)); |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 430 | 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 Braun | 14d22d4 | 2019-01-30 18:51:00 +0100 | [diff] [blame] | 438 | if (len < 0 || len < sizeof(struct smc_clc_msg_decline)) |
Ursula Braun | 6ae36bf | 2018-11-22 10:26:36 +0100 | [diff] [blame] | 439 | len = -EPROTO; |
| 440 | return len > 0 ? 0 : len; |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | /* send CLC PROPOSAL message across internal TCP socket */ |
Ursula Braun | d70bf4f | 2020-09-26 12:44:27 +0200 | [diff] [blame] | 444 | int smc_clc_send_proposal(struct smc_sock *smc, struct smc_init_info *ini) |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 445 | { |
Ursula Braun | 8c3dca3 | 2020-09-26 12:44:28 +0200 | [diff] [blame^] | 446 | struct smc_clc_smcd_v2_extension *smcd_v2_ext; |
Ursula Braun | 6bb14e48 | 2020-09-10 18:48:22 +0200 | [diff] [blame] | 447 | struct smc_clc_msg_proposal_prefix *pclc_prfx; |
| 448 | struct smc_clc_msg_proposal *pclc_base; |
Ursula Braun | 8c3dca3 | 2020-09-26 12:44:28 +0200 | [diff] [blame^] | 449 | struct smc_clc_smcd_gid_chid *gidchids; |
Ursula Braun | 6bb14e48 | 2020-09-10 18:48:22 +0200 | [diff] [blame] | 450 | struct smc_clc_msg_proposal_area *pclc; |
| 451 | struct smc_clc_ipv6_prefix *ipv6_prfx; |
Ursula Braun | 8c3dca3 | 2020-09-26 12:44:28 +0200 | [diff] [blame^] | 452 | struct smc_clc_v2_extension *v2_ext; |
Ursula Braun | 6bb14e48 | 2020-09-10 18:48:22 +0200 | [diff] [blame] | 453 | struct smc_clc_msg_smcd *pclc_smcd; |
| 454 | struct smc_clc_msg_trail *trl; |
Karsten Graul | 1a26d02 | 2018-03-16 15:06:40 +0100 | [diff] [blame] | 455 | int len, i, plen, rc; |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 456 | int reason_code = 0; |
Ursula Braun | 8c3dca3 | 2020-09-26 12:44:28 +0200 | [diff] [blame^] | 457 | struct kvec vec[8]; |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 458 | struct msghdr msg; |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 459 | |
Ursula Braun | 6bb14e48 | 2020-09-10 18:48:22 +0200 | [diff] [blame] | 460 | 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 Braun | 8c3dca3 | 2020-09-26 12:44:28 +0200 | [diff] [blame^] | 468 | v2_ext = &pclc->pclc_v2_ext; |
| 469 | smcd_v2_ext = &pclc->pclc_smcd_v2_ext; |
| 470 | gidchids = pclc->pclc_gidchids; |
Ursula Braun | 6bb14e48 | 2020-09-10 18:48:22 +0200 | [diff] [blame] | 471 | trl = &pclc->pclc_trl; |
| 472 | |
Ursula Braun | 8c3dca3 | 2020-09-26 12:44:28 +0200 | [diff] [blame^] | 473 | 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 Graul | c246d94 | 2018-03-16 15:06:39 +0100 | [diff] [blame] | 478 | /* retrieve ip prefixes for CLC proposal msg */ |
Ursula Braun | 8c3dca3 | 2020-09-26 12:44:28 +0200 | [diff] [blame^] | 479 | 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 Braun | 6bb14e48 | 2020-09-10 18:48:22 +0200 | [diff] [blame] | 493 | } |
Karsten Graul | c246d94 | 2018-03-16 15:06:39 +0100 | [diff] [blame] | 494 | |
Ursula Braun | 8c3dca3 | 2020-09-26 12:44:28 +0200 | [diff] [blame^] | 495 | /* build SMC Proposal CLC message */ |
Ursula Braun | 6bb14e48 | 2020-09-10 18:48:22 +0200 | [diff] [blame] | 496 | memcpy(pclc_base->hdr.eyecatcher, SMC_EYECATCHER, |
| 497 | sizeof(SMC_EYECATCHER)); |
| 498 | pclc_base->hdr.type = SMC_CLC_PROPOSAL; |
Ursula Braun | d70bf4f | 2020-09-26 12:44:27 +0200 | [diff] [blame] | 499 | if (smcr_indicated(ini->smc_type_v1)) { |
Hans Wippel | c758dfd | 2018-06-28 19:05:09 +0200 | [diff] [blame] | 500 | /* add SMC-R specifics */ |
Ursula Braun | 6bb14e48 | 2020-09-10 18:48:22 +0200 | [diff] [blame] | 501 | memcpy(pclc_base->lcl.id_for_peer, local_systemid, |
Hans Wippel | c758dfd | 2018-06-28 19:05:09 +0200 | [diff] [blame] | 502 | sizeof(local_systemid)); |
Ursula Braun | 6bb14e48 | 2020-09-10 18:48:22 +0200 | [diff] [blame] | 503 | 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 Graul | bc36d2f | 2019-04-12 12:57:26 +0200 | [diff] [blame] | 505 | ETH_ALEN); |
Hans Wippel | c758dfd | 2018-06-28 19:05:09 +0200 | [diff] [blame] | 506 | } |
Ursula Braun | d70bf4f | 2020-09-26 12:44:27 +0200 | [diff] [blame] | 507 | if (smcd_indicated(ini->smc_type_v1)) { |
Hans Wippel | c758dfd | 2018-06-28 19:05:09 +0200 | [diff] [blame] | 508 | /* add SMC-D specifics */ |
Ursula Braun | 8c3dca3 | 2020-09-26 12:44:28 +0200 | [diff] [blame^] | 509 | 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 Wippel | c758dfd | 2018-06-28 19:05:09 +0200 | [diff] [blame] | 553 | } |
Ursula Braun | 6bb14e48 | 2020-09-10 18:48:22 +0200 | [diff] [blame] | 554 | pclc_base->hdr.length = htons(plen); |
Ursula Braun | 6bb14e48 | 2020-09-10 18:48:22 +0200 | [diff] [blame] | 555 | memcpy(trl->eyecatcher, SMC_EYECATCHER, sizeof(SMC_EYECATCHER)); |
Ursula Braun | 8c3dca3 | 2020-09-26 12:44:28 +0200 | [diff] [blame^] | 556 | |
| 557 | /* send SMC Proposal CLC message */ |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 558 | memset(&msg, 0, sizeof(msg)); |
Karsten Graul | 1a26d02 | 2018-03-16 15:06:40 +0100 | [diff] [blame] | 559 | i = 0; |
Ursula Braun | 6bb14e48 | 2020-09-10 18:48:22 +0200 | [diff] [blame] | 560 | vec[i].iov_base = pclc_base; |
| 561 | vec[i++].iov_len = sizeof(*pclc_base); |
Ursula Braun | 8c3dca3 | 2020-09-26 12:44:28 +0200 | [diff] [blame^] | 562 | 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 Wippel | c758dfd | 2018-06-28 19:05:09 +0200 | [diff] [blame] | 572 | } |
Ursula Braun | 8c3dca3 | 2020-09-26 12:44:28 +0200 | [diff] [blame^] | 573 | 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 Graul | 1a26d02 | 2018-03-16 15:06:40 +0100 | [diff] [blame] | 583 | } |
Ursula Braun | 6bb14e48 | 2020-09-10 18:48:22 +0200 | [diff] [blame] | 584 | vec[i].iov_base = trl; |
| 585 | vec[i++].iov_len = sizeof(*trl); |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 586 | /* due to the few bytes needed for clc-handshake this cannot block */ |
Karsten Graul | 1a26d02 | 2018-03-16 15:06:40 +0100 | [diff] [blame] | 587 | len = kernel_sendmsg(smc->clcsock, &msg, vec, i, plen); |
YueHaibing | 3818977 | 2018-09-18 15:46:38 +0200 | [diff] [blame] | 588 | if (len < 0) { |
| 589 | smc->sk.sk_err = smc->clcsock->sk->sk_err; |
| 590 | reason_code = -smc->sk.sk_err; |
Ursula Braun | 6bb14e48 | 2020-09-10 18:48:22 +0200 | [diff] [blame] | 591 | } else if (len < ntohs(pclc_base->hdr.length)) { |
YueHaibing | 3818977 | 2018-09-18 15:46:38 +0200 | [diff] [blame] | 592 | reason_code = -ENETUNREACH; |
| 593 | smc->sk.sk_err = -reason_code; |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 594 | } |
| 595 | |
Ursula Braun | 6bb14e48 | 2020-09-10 18:48:22 +0200 | [diff] [blame] | 596 | kfree(pclc); |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 597 | return reason_code; |
| 598 | } |
| 599 | |
Ursula Braun | 3d9725a | 2020-09-10 18:48:23 +0200 | [diff] [blame] | 600 | /* build and send CLC CONFIRM / ACCEPT message */ |
| 601 | static 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 Braun | e15c6c4 | 2020-09-26 12:44:22 +0200 | [diff] [blame] | 606 | struct smc_clc_msg_trail trl; |
| 607 | struct kvec vec[2]; |
Ursula Braun | 3d9725a | 2020-09-10 18:48:23 +0200 | [diff] [blame] | 608 | struct msghdr msg; |
Ursula Braun | e15c6c4 | 2020-09-26 12:44:22 +0200 | [diff] [blame] | 609 | int i; |
Ursula Braun | 3d9725a | 2020-09-10 18:48:23 +0200 | [diff] [blame] | 610 | |
| 611 | /* send SMC Confirm CLC msg */ |
| 612 | clc->hdr.version = SMC_V1; /* SMC version */ |
| 613 | if (first_contact) |
Ursula Braun | f1eb02f | 2020-09-26 12:44:20 +0200 | [diff] [blame] | 614 | clc->hdr.typev2 |= SMC_FIRST_CONTACT_MASK; |
Ursula Braun | 3d9725a | 2020-09-10 18:48:23 +0200 | [diff] [blame] | 615 | if (conn->lgr->is_smcd) { |
| 616 | /* SMC-D specific settings */ |
| 617 | memcpy(clc->hdr.eyecatcher, SMCD_EYECATCHER, |
| 618 | sizeof(SMCD_EYECATCHER)); |
Ursula Braun | f1eb02f | 2020-09-26 12:44:20 +0200 | [diff] [blame] | 619 | clc->hdr.typev1 = SMC_TYPE_D; |
Ursula Braun | 3d9725a | 2020-09-10 18:48:23 +0200 | [diff] [blame] | 620 | 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 Braun | e15c6c4 | 2020-09-26 12:44:22 +0200 | [diff] [blame] | 626 | memcpy(trl.eyecatcher, SMCD_EYECATCHER, |
Ursula Braun | 3d9725a | 2020-09-10 18:48:23 +0200 | [diff] [blame] | 627 | 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 Braun | f1eb02f | 2020-09-26 12:44:20 +0200 | [diff] [blame] | 635 | clc->hdr.typev1 = SMC_TYPE_R; |
Ursula Braun | 3d9725a | 2020-09-10 18:48:23 +0200 | [diff] [blame] | 636 | 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 Braun | e15c6c4 | 2020-09-26 12:44:22 +0200 | [diff] [blame] | 659 | memcpy(trl.eyecatcher, SMC_EYECATCHER, sizeof(SMC_EYECATCHER)); |
Ursula Braun | 3d9725a | 2020-09-10 18:48:23 +0200 | [diff] [blame] | 660 | } |
| 661 | |
| 662 | memset(&msg, 0, sizeof(msg)); |
Ursula Braun | e15c6c4 | 2020-09-26 12:44:22 +0200 | [diff] [blame] | 663 | 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 Braun | 3d9725a | 2020-09-10 18:48:23 +0200 | [diff] [blame] | 672 | ntohs(clc->hdr.length)); |
| 673 | } |
| 674 | |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 675 | /* send CLC CONFIRM message across internal TCP socket */ |
| 676 | int smc_clc_send_confirm(struct smc_sock *smc) |
| 677 | { |
| 678 | struct smc_clc_msg_accept_confirm cclc; |
| 679 | int reason_code = 0; |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 680 | int len; |
| 681 | |
| 682 | /* send SMC Confirm CLC msg */ |
| 683 | memset(&cclc, 0, sizeof(cclc)); |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 684 | cclc.hdr.type = SMC_CLC_CONFIRM; |
Ursula Braun | 3d9725a | 2020-09-10 18:48:23 +0200 | [diff] [blame] | 685 | len = smc_clc_send_confirm_accept(smc, &cclc, 0); |
Hans Wippel | c758dfd | 2018-06-28 19:05:09 +0200 | [diff] [blame] | 686 | if (len < ntohs(cclc.hdr.length)) { |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 687 | 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 Braun | 5ac54d8 | 2020-09-10 18:48:21 +0200 | [diff] [blame] | 699 | int smc_clc_send_accept(struct smc_sock *new_smc, bool srv_first_contact) |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 700 | { |
| 701 | struct smc_clc_msg_accept_confirm aclc; |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 702 | int len; |
| 703 | |
| 704 | memset(&aclc, 0, sizeof(aclc)); |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 705 | aclc.hdr.type = SMC_CLC_ACCEPT; |
Ursula Braun | 3d9725a | 2020-09-10 18:48:23 +0200 | [diff] [blame] | 706 | len = smc_clc_send_confirm_accept(new_smc, &aclc, srv_first_contact); |
Ursula Braun | 6ae36bf | 2018-11-22 10:26:36 +0100 | [diff] [blame] | 707 | if (len < ntohs(aclc.hdr.length)) |
| 708 | len = len >= 0 ? -EPROTO : -new_smc->clcsock->sk->sk_err; |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 709 | |
Ursula Braun | 6ae36bf | 2018-11-22 10:26:36 +0100 | [diff] [blame] | 710 | return len > 0 ? 0 : len; |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 711 | } |