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 | a7c9c5f | 2020-09-26 12:44:30 +0200 | [diff] [blame^] | 30 | #define SMCD_CLC_ACCEPT_CONFIRM_LEN_V2 78 |
Ursula Braun | fb4f792 | 2020-07-08 17:05:15 +0200 | [diff] [blame] | 31 | #define SMC_CLC_RECV_BUF_LEN 100 |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 32 | |
Stefan Raspl | 0f62712 | 2018-03-01 13:51:26 +0100 | [diff] [blame] | 33 | /* eye catcher "SMCR" EBCDIC for CLC messages */ |
| 34 | static const char SMC_EYECATCHER[4] = {'\xe2', '\xd4', '\xc3', '\xd9'}; |
Hans Wippel | c758dfd | 2018-06-28 19:05:09 +0200 | [diff] [blame] | 35 | /* eye catcher "SMCD" EBCDIC for CLC messages */ |
| 36 | static const char SMCD_EYECATCHER[4] = {'\xe2', '\xd4', '\xc3', '\xc4'}; |
Stefan Raspl | 0f62712 | 2018-03-01 13:51:26 +0100 | [diff] [blame] | 37 | |
Ursula Braun | 8c3dca3 | 2020-09-26 12:44:28 +0200 | [diff] [blame] | 38 | /* check arriving CLC proposal */ |
| 39 | static bool smc_clc_msg_prop_valid(struct smc_clc_msg_proposal *pclc) |
| 40 | { |
| 41 | struct smc_clc_msg_proposal_prefix *pclc_prfx; |
| 42 | struct smc_clc_smcd_v2_extension *smcd_v2_ext; |
| 43 | struct smc_clc_msg_hdr *hdr = &pclc->hdr; |
| 44 | struct smc_clc_v2_extension *v2_ext; |
| 45 | |
| 46 | v2_ext = smc_get_clc_v2_ext(pclc); |
| 47 | pclc_prfx = smc_clc_proposal_get_prefix(pclc); |
| 48 | if (hdr->version == SMC_V1) { |
| 49 | if (hdr->typev1 == SMC_TYPE_N) |
| 50 | return false; |
| 51 | if (ntohs(hdr->length) != |
| 52 | sizeof(*pclc) + ntohs(pclc->iparea_offset) + |
| 53 | sizeof(*pclc_prfx) + |
| 54 | pclc_prfx->ipv6_prefixes_cnt * |
| 55 | sizeof(struct smc_clc_ipv6_prefix) + |
| 56 | sizeof(struct smc_clc_msg_trail)) |
| 57 | return false; |
| 58 | } else { |
| 59 | if (ntohs(hdr->length) != |
| 60 | sizeof(*pclc) + |
| 61 | sizeof(struct smc_clc_msg_smcd) + |
| 62 | (hdr->typev1 != SMC_TYPE_N ? |
| 63 | sizeof(*pclc_prfx) + |
| 64 | pclc_prfx->ipv6_prefixes_cnt * |
| 65 | sizeof(struct smc_clc_ipv6_prefix) : 0) + |
| 66 | (hdr->typev2 != SMC_TYPE_N ? |
| 67 | sizeof(*v2_ext) + |
| 68 | v2_ext->hdr.eid_cnt * SMC_MAX_EID_LEN : 0) + |
| 69 | (smcd_indicated(hdr->typev2) ? |
| 70 | sizeof(*smcd_v2_ext) + v2_ext->hdr.ism_gid_cnt * |
| 71 | sizeof(struct smc_clc_smcd_gid_chid) : |
| 72 | 0) + |
| 73 | sizeof(struct smc_clc_msg_trail)) |
| 74 | return false; |
| 75 | } |
| 76 | return true; |
| 77 | } |
| 78 | |
Ursula Braun | a7c9c5f | 2020-09-26 12:44:30 +0200 | [diff] [blame^] | 79 | /* check arriving CLC accept or confirm */ |
| 80 | static bool |
| 81 | smc_clc_msg_acc_conf_valid(struct smc_clc_msg_accept_confirm_v2 *clc_v2) |
| 82 | { |
| 83 | struct smc_clc_msg_hdr *hdr = &clc_v2->hdr; |
| 84 | |
| 85 | if (hdr->typev1 != SMC_TYPE_R && hdr->typev1 != SMC_TYPE_D) |
| 86 | return false; |
| 87 | if (hdr->version == SMC_V1) { |
| 88 | if ((hdr->typev1 == SMC_TYPE_R && |
| 89 | ntohs(hdr->length) != SMCR_CLC_ACCEPT_CONFIRM_LEN) || |
| 90 | (hdr->typev1 == SMC_TYPE_D && |
| 91 | ntohs(hdr->length) != SMCD_CLC_ACCEPT_CONFIRM_LEN)) |
| 92 | return false; |
| 93 | } else { |
| 94 | if (hdr->typev1 == SMC_TYPE_D && |
| 95 | ntohs(hdr->length) != SMCD_CLC_ACCEPT_CONFIRM_LEN_V2) |
| 96 | return false; |
| 97 | } |
| 98 | return true; |
| 99 | } |
| 100 | |
Ursula Braun | e7b7a64 | 2017-12-07 13:38:49 +0100 | [diff] [blame] | 101 | /* check if received message has a correct header length and contains valid |
| 102 | * heading and trailing eyecatchers |
| 103 | */ |
Ursula Braun | fb4f792 | 2020-07-08 17:05:15 +0200 | [diff] [blame] | 104 | 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] | 105 | { |
Ursula Braun | a7c9c5f | 2020-09-26 12:44:30 +0200 | [diff] [blame^] | 106 | struct smc_clc_msg_accept_confirm_v2 *clc_v2; |
Ursula Braun | e7b7a64 | 2017-12-07 13:38:49 +0100 | [diff] [blame] | 107 | struct smc_clc_msg_proposal *pclc; |
| 108 | struct smc_clc_msg_decline *dclc; |
| 109 | struct smc_clc_msg_trail *trl; |
| 110 | |
Hans Wippel | c758dfd | 2018-06-28 19:05:09 +0200 | [diff] [blame] | 111 | if (memcmp(clcm->eyecatcher, SMC_EYECATCHER, sizeof(SMC_EYECATCHER)) && |
| 112 | memcmp(clcm->eyecatcher, SMCD_EYECATCHER, sizeof(SMCD_EYECATCHER))) |
Ursula Braun | e7b7a64 | 2017-12-07 13:38:49 +0100 | [diff] [blame] | 113 | return false; |
| 114 | switch (clcm->type) { |
| 115 | case SMC_CLC_PROPOSAL: |
| 116 | pclc = (struct smc_clc_msg_proposal *)clcm; |
Ursula Braun | 8c3dca3 | 2020-09-26 12:44:28 +0200 | [diff] [blame] | 117 | if (!smc_clc_msg_prop_valid(pclc)) |
Ursula Braun | e7b7a64 | 2017-12-07 13:38:49 +0100 | [diff] [blame] | 118 | return false; |
| 119 | trl = (struct smc_clc_msg_trail *) |
| 120 | ((u8 *)pclc + ntohs(pclc->hdr.length) - sizeof(*trl)); |
| 121 | break; |
| 122 | case SMC_CLC_ACCEPT: |
| 123 | case SMC_CLC_CONFIRM: |
Ursula Braun | a7c9c5f | 2020-09-26 12:44:30 +0200 | [diff] [blame^] | 124 | clc_v2 = (struct smc_clc_msg_accept_confirm_v2 *)clcm; |
| 125 | if (!smc_clc_msg_acc_conf_valid(clc_v2)) |
Hans Wippel | c758dfd | 2018-06-28 19:05:09 +0200 | [diff] [blame] | 126 | return false; |
| 127 | trl = (struct smc_clc_msg_trail *) |
Ursula Braun | a7c9c5f | 2020-09-26 12:44:30 +0200 | [diff] [blame^] | 128 | ((u8 *)clc_v2 + ntohs(clc_v2->hdr.length) - |
| 129 | sizeof(*trl)); |
Ursula Braun | e7b7a64 | 2017-12-07 13:38:49 +0100 | [diff] [blame] | 130 | break; |
| 131 | case SMC_CLC_DECLINE: |
| 132 | dclc = (struct smc_clc_msg_decline *)clcm; |
| 133 | if (ntohs(dclc->hdr.length) != sizeof(*dclc)) |
| 134 | return false; |
| 135 | trl = &dclc->trl; |
| 136 | break; |
| 137 | default: |
| 138 | return false; |
| 139 | } |
Ursula Braun | fb4f792 | 2020-07-08 17:05:15 +0200 | [diff] [blame] | 140 | if (check_trl && |
| 141 | memcmp(trl->eyecatcher, SMC_EYECATCHER, sizeof(SMC_EYECATCHER)) && |
Hans Wippel | c758dfd | 2018-06-28 19:05:09 +0200 | [diff] [blame] | 142 | memcmp(trl->eyecatcher, SMCD_EYECATCHER, sizeof(SMCD_EYECATCHER))) |
Ursula Braun | e7b7a64 | 2017-12-07 13:38:49 +0100 | [diff] [blame] | 143 | return false; |
| 144 | return true; |
| 145 | } |
| 146 | |
Karsten Graul | c246d94 | 2018-03-16 15:06:39 +0100 | [diff] [blame] | 147 | /* find ipv4 addr on device and get the prefix len, fill CLC proposal msg */ |
| 148 | static int smc_clc_prfx_set4_rcu(struct dst_entry *dst, __be32 ipv4, |
| 149 | struct smc_clc_msg_proposal_prefix *prop) |
| 150 | { |
| 151 | struct in_device *in_dev = __in_dev_get_rcu(dst->dev); |
Florian Westphal | cd5a411 | 2019-05-31 18:27:07 +0200 | [diff] [blame] | 152 | const struct in_ifaddr *ifa; |
Karsten Graul | c246d94 | 2018-03-16 15:06:39 +0100 | [diff] [blame] | 153 | |
| 154 | if (!in_dev) |
| 155 | return -ENODEV; |
Florian Westphal | cd5a411 | 2019-05-31 18:27:07 +0200 | [diff] [blame] | 156 | |
| 157 | in_dev_for_each_ifa_rcu(ifa, in_dev) { |
Karsten Graul | c246d94 | 2018-03-16 15:06:39 +0100 | [diff] [blame] | 158 | if (!inet_ifa_match(ipv4, ifa)) |
| 159 | continue; |
| 160 | prop->prefix_len = inet_mask_len(ifa->ifa_mask); |
| 161 | prop->outgoing_subnet = ifa->ifa_address & ifa->ifa_mask; |
| 162 | /* prop->ipv6_prefixes_cnt = 0; already done by memset before */ |
| 163 | return 0; |
Florian Westphal | cd5a411 | 2019-05-31 18:27:07 +0200 | [diff] [blame] | 164 | } |
Karsten Graul | c246d94 | 2018-03-16 15:06:39 +0100 | [diff] [blame] | 165 | return -ENOENT; |
| 166 | } |
| 167 | |
Karsten Graul | 1a26d02 | 2018-03-16 15:06:40 +0100 | [diff] [blame] | 168 | /* fill CLC proposal msg with ipv6 prefixes from device */ |
| 169 | static int smc_clc_prfx_set6_rcu(struct dst_entry *dst, |
| 170 | struct smc_clc_msg_proposal_prefix *prop, |
| 171 | struct smc_clc_ipv6_prefix *ipv6_prfx) |
| 172 | { |
| 173 | #if IS_ENABLED(CONFIG_IPV6) |
| 174 | struct inet6_dev *in6_dev = __in6_dev_get(dst->dev); |
| 175 | struct inet6_ifaddr *ifa; |
| 176 | int cnt = 0; |
| 177 | |
| 178 | if (!in6_dev) |
| 179 | return -ENODEV; |
| 180 | /* use a maximum of 8 IPv6 prefixes from device */ |
| 181 | list_for_each_entry(ifa, &in6_dev->addr_list, if_list) { |
| 182 | if (ipv6_addr_type(&ifa->addr) & IPV6_ADDR_LINKLOCAL) |
| 183 | continue; |
| 184 | ipv6_addr_prefix(&ipv6_prfx[cnt].prefix, |
| 185 | &ifa->addr, ifa->prefix_len); |
| 186 | ipv6_prfx[cnt].prefix_len = ifa->prefix_len; |
| 187 | cnt++; |
| 188 | if (cnt == SMC_CLC_MAX_V6_PREFIX) |
| 189 | break; |
| 190 | } |
| 191 | prop->ipv6_prefixes_cnt = cnt; |
| 192 | if (cnt) |
| 193 | return 0; |
| 194 | #endif |
| 195 | return -ENOENT; |
| 196 | } |
| 197 | |
Karsten Graul | c246d94 | 2018-03-16 15:06:39 +0100 | [diff] [blame] | 198 | /* retrieve and set prefixes in CLC proposal msg */ |
| 199 | static int smc_clc_prfx_set(struct socket *clcsock, |
Karsten Graul | 1a26d02 | 2018-03-16 15:06:40 +0100 | [diff] [blame] | 200 | struct smc_clc_msg_proposal_prefix *prop, |
| 201 | struct smc_clc_ipv6_prefix *ipv6_prfx) |
Karsten Graul | 696cd30 | 2018-03-01 13:51:27 +0100 | [diff] [blame] | 202 | { |
| 203 | struct dst_entry *dst = sk_dst_get(clcsock->sk); |
Karsten Graul | c246d94 | 2018-03-16 15:06:39 +0100 | [diff] [blame] | 204 | struct sockaddr_storage addrs; |
Karsten Graul | 1a26d02 | 2018-03-16 15:06:40 +0100 | [diff] [blame] | 205 | struct sockaddr_in6 *addr6; |
Karsten Graul | c246d94 | 2018-03-16 15:06:39 +0100 | [diff] [blame] | 206 | struct sockaddr_in *addr; |
| 207 | int rc = -ENOENT; |
| 208 | |
Karsten Graul | c246d94 | 2018-03-16 15:06:39 +0100 | [diff] [blame] | 209 | if (!dst) { |
| 210 | rc = -ENOTCONN; |
| 211 | goto out; |
| 212 | } |
| 213 | if (!dst->dev) { |
| 214 | rc = -ENODEV; |
| 215 | goto out_rel; |
| 216 | } |
| 217 | /* get address to which the internal TCP socket is bound */ |
| 218 | kernel_getsockname(clcsock, (struct sockaddr *)&addrs); |
| 219 | /* analyze IP specific data of net_device belonging to TCP socket */ |
Karsten Graul | 1a26d02 | 2018-03-16 15:06:40 +0100 | [diff] [blame] | 220 | addr6 = (struct sockaddr_in6 *)&addrs; |
Karsten Graul | c246d94 | 2018-03-16 15:06:39 +0100 | [diff] [blame] | 221 | rcu_read_lock(); |
| 222 | if (addrs.ss_family == PF_INET) { |
| 223 | /* IPv4 */ |
| 224 | addr = (struct sockaddr_in *)&addrs; |
| 225 | 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] | 226 | } else if (ipv6_addr_v4mapped(&addr6->sin6_addr)) { |
| 227 | /* mapped IPv4 address - peer is IPv4 only */ |
| 228 | rc = smc_clc_prfx_set4_rcu(dst, addr6->sin6_addr.s6_addr32[3], |
| 229 | prop); |
| 230 | } else { |
| 231 | /* IPv6 */ |
| 232 | rc = smc_clc_prfx_set6_rcu(dst, prop, ipv6_prfx); |
Karsten Graul | c246d94 | 2018-03-16 15:06:39 +0100 | [diff] [blame] | 233 | } |
| 234 | rcu_read_unlock(); |
| 235 | out_rel: |
| 236 | dst_release(dst); |
| 237 | out: |
| 238 | return rc; |
| 239 | } |
| 240 | |
| 241 | /* match ipv4 addrs of dev against addr in CLC proposal */ |
| 242 | static int smc_clc_prfx_match4_rcu(struct net_device *dev, |
| 243 | struct smc_clc_msg_proposal_prefix *prop) |
| 244 | { |
| 245 | struct in_device *in_dev = __in_dev_get_rcu(dev); |
Florian Westphal | cd5a411 | 2019-05-31 18:27:07 +0200 | [diff] [blame] | 246 | const struct in_ifaddr *ifa; |
Karsten Graul | c246d94 | 2018-03-16 15:06:39 +0100 | [diff] [blame] | 247 | |
| 248 | if (!in_dev) |
| 249 | return -ENODEV; |
Florian Westphal | cd5a411 | 2019-05-31 18:27:07 +0200 | [diff] [blame] | 250 | in_dev_for_each_ifa_rcu(ifa, in_dev) { |
Karsten Graul | c246d94 | 2018-03-16 15:06:39 +0100 | [diff] [blame] | 251 | if (prop->prefix_len == inet_mask_len(ifa->ifa_mask) && |
| 252 | inet_ifa_match(prop->outgoing_subnet, ifa)) |
| 253 | return 0; |
Florian Westphal | cd5a411 | 2019-05-31 18:27:07 +0200 | [diff] [blame] | 254 | } |
Karsten Graul | c246d94 | 2018-03-16 15:06:39 +0100 | [diff] [blame] | 255 | |
| 256 | return -ENOENT; |
| 257 | } |
| 258 | |
Karsten Graul | 1a26d02 | 2018-03-16 15:06:40 +0100 | [diff] [blame] | 259 | /* match ipv6 addrs of dev against addrs in CLC proposal */ |
| 260 | static int smc_clc_prfx_match6_rcu(struct net_device *dev, |
| 261 | struct smc_clc_msg_proposal_prefix *prop) |
| 262 | { |
| 263 | #if IS_ENABLED(CONFIG_IPV6) |
| 264 | struct inet6_dev *in6_dev = __in6_dev_get(dev); |
| 265 | struct smc_clc_ipv6_prefix *ipv6_prfx; |
| 266 | struct inet6_ifaddr *ifa; |
| 267 | int i, max; |
| 268 | |
| 269 | if (!in6_dev) |
| 270 | return -ENODEV; |
| 271 | /* ipv6 prefix list starts behind smc_clc_msg_proposal_prefix */ |
| 272 | ipv6_prfx = (struct smc_clc_ipv6_prefix *)((u8 *)prop + sizeof(*prop)); |
| 273 | max = min_t(u8, prop->ipv6_prefixes_cnt, SMC_CLC_MAX_V6_PREFIX); |
| 274 | list_for_each_entry(ifa, &in6_dev->addr_list, if_list) { |
| 275 | if (ipv6_addr_type(&ifa->addr) & IPV6_ADDR_LINKLOCAL) |
| 276 | continue; |
| 277 | for (i = 0; i < max; i++) { |
| 278 | if (ifa->prefix_len == ipv6_prfx[i].prefix_len && |
| 279 | ipv6_prefix_equal(&ifa->addr, &ipv6_prfx[i].prefix, |
| 280 | ifa->prefix_len)) |
| 281 | return 0; |
| 282 | } |
| 283 | } |
| 284 | #endif |
| 285 | return -ENOENT; |
| 286 | } |
| 287 | |
Karsten Graul | c246d94 | 2018-03-16 15:06:39 +0100 | [diff] [blame] | 288 | /* check if proposed prefixes match one of our device prefixes */ |
| 289 | int smc_clc_prfx_match(struct socket *clcsock, |
| 290 | struct smc_clc_msg_proposal_prefix *prop) |
| 291 | { |
| 292 | struct dst_entry *dst = sk_dst_get(clcsock->sk); |
Karsten Graul | 1a26d02 | 2018-03-16 15:06:40 +0100 | [diff] [blame] | 293 | int rc; |
Karsten Graul | 696cd30 | 2018-03-01 13:51:27 +0100 | [diff] [blame] | 294 | |
| 295 | if (!dst) { |
| 296 | rc = -ENOTCONN; |
| 297 | goto out; |
| 298 | } |
| 299 | if (!dst->dev) { |
| 300 | rc = -ENODEV; |
| 301 | goto out_rel; |
| 302 | } |
Karsten Graul | 696cd30 | 2018-03-01 13:51:27 +0100 | [diff] [blame] | 303 | rcu_read_lock(); |
Karsten Graul | c246d94 | 2018-03-16 15:06:39 +0100 | [diff] [blame] | 304 | if (!prop->ipv6_prefixes_cnt) |
| 305 | rc = smc_clc_prfx_match4_rcu(dst->dev, prop); |
Karsten Graul | 1a26d02 | 2018-03-16 15:06:40 +0100 | [diff] [blame] | 306 | else |
| 307 | rc = smc_clc_prfx_match6_rcu(dst->dev, prop); |
Karsten Graul | 696cd30 | 2018-03-01 13:51:27 +0100 | [diff] [blame] | 308 | rcu_read_unlock(); |
Karsten Graul | 696cd30 | 2018-03-01 13:51:27 +0100 | [diff] [blame] | 309 | out_rel: |
| 310 | dst_release(dst); |
| 311 | out: |
| 312 | return rc; |
| 313 | } |
| 314 | |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 315 | /* Wait for data on the tcp-socket, analyze received data |
| 316 | * Returns: |
| 317 | * 0 if success and it was not a decline that we received. |
| 318 | * SMC_CLC_DECL_REPLY if decline received for fallback w/o another decl send. |
| 319 | * clcsock error, -EINTR, -ECONNRESET, -EPROTO otherwise. |
| 320 | */ |
| 321 | 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] | 322 | u8 expected_type, unsigned long timeout) |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 323 | { |
Karsten Graul | f6bdc42 | 2018-07-18 15:22:51 +0200 | [diff] [blame] | 324 | long rcvtimeo = smc->clcsock->sk->sk_rcvtimeo; |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 325 | struct sock *clc_sk = smc->clcsock->sk; |
| 326 | struct smc_clc_msg_hdr *clcm = buf; |
| 327 | struct msghdr msg = {NULL, 0}; |
| 328 | int reason_code = 0; |
Al Viro | d63d271 | 2017-09-20 20:21:22 -0400 | [diff] [blame] | 329 | struct kvec vec = {buf, buflen}; |
Ursula Braun | fb4f792 | 2020-07-08 17:05:15 +0200 | [diff] [blame] | 330 | int len, datlen, recvlen; |
| 331 | bool check_trl = true; |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 332 | int krflags; |
| 333 | |
| 334 | /* peek the first few bytes to determine length of data to receive |
| 335 | * so we don't consume any subsequent CLC message or payload data |
| 336 | * in the TCP byte stream |
| 337 | */ |
Al Viro | d63d271 | 2017-09-20 20:21:22 -0400 | [diff] [blame] | 338 | /* |
| 339 | * Caller must make sure that buflen is no less than |
| 340 | * sizeof(struct smc_clc_msg_hdr) |
| 341 | */ |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 342 | krflags = MSG_PEEK | MSG_WAITALL; |
Ursula Braun | 2b59f58 | 2018-11-22 10:26:39 +0100 | [diff] [blame] | 343 | clc_sk->sk_rcvtimeo = timeout; |
David Howells | aa563d7 | 2018-10-20 00:57:56 +0100 | [diff] [blame] | 344 | iov_iter_kvec(&msg.msg_iter, READ, &vec, 1, |
Al Viro | d63d271 | 2017-09-20 20:21:22 -0400 | [diff] [blame] | 345 | sizeof(struct smc_clc_msg_hdr)); |
| 346 | len = sock_recvmsg(smc->clcsock, &msg, krflags); |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 347 | if (signal_pending(current)) { |
| 348 | reason_code = -EINTR; |
| 349 | clc_sk->sk_err = EINTR; |
| 350 | smc->sk.sk_err = EINTR; |
| 351 | goto out; |
| 352 | } |
| 353 | if (clc_sk->sk_err) { |
| 354 | reason_code = -clc_sk->sk_err; |
Ursula Braun | 9ed2855 | 2018-11-22 10:26:37 +0100 | [diff] [blame] | 355 | if (clc_sk->sk_err == EAGAIN && |
| 356 | expected_type == SMC_CLC_DECLINE) |
| 357 | clc_sk->sk_err = 0; /* reset for fallback usage */ |
| 358 | else |
| 359 | smc->sk.sk_err = clc_sk->sk_err; |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 360 | goto out; |
| 361 | } |
| 362 | if (!len) { /* peer has performed orderly shutdown */ |
| 363 | smc->sk.sk_err = ECONNRESET; |
| 364 | reason_code = -ECONNRESET; |
| 365 | goto out; |
| 366 | } |
| 367 | if (len < 0) { |
Ursula Braun | 9ed2855 | 2018-11-22 10:26:37 +0100 | [diff] [blame] | 368 | if (len != -EAGAIN || expected_type != SMC_CLC_DECLINE) |
| 369 | smc->sk.sk_err = -len; |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 370 | reason_code = len; |
| 371 | goto out; |
| 372 | } |
| 373 | datlen = ntohs(clcm->length); |
| 374 | if ((len < sizeof(struct smc_clc_msg_hdr)) || |
Ursula Braun | 5ac54d8 | 2020-09-10 18:48:21 +0200 | [diff] [blame] | 375 | (clcm->version < SMC_V1) || |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 376 | ((clcm->type != SMC_CLC_DECLINE) && |
| 377 | (clcm->type != expected_type))) { |
| 378 | smc->sk.sk_err = EPROTO; |
| 379 | reason_code = -EPROTO; |
| 380 | goto out; |
| 381 | } |
| 382 | |
| 383 | /* receive the complete CLC message */ |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 384 | memset(&msg, 0, sizeof(struct msghdr)); |
Ursula Braun | fb4f792 | 2020-07-08 17:05:15 +0200 | [diff] [blame] | 385 | if (datlen > buflen) { |
| 386 | check_trl = false; |
| 387 | recvlen = buflen; |
| 388 | } else { |
| 389 | recvlen = datlen; |
| 390 | } |
| 391 | iov_iter_kvec(&msg.msg_iter, READ, &vec, 1, recvlen); |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 392 | krflags = MSG_WAITALL; |
Al Viro | d63d271 | 2017-09-20 20:21:22 -0400 | [diff] [blame] | 393 | len = sock_recvmsg(smc->clcsock, &msg, krflags); |
Ursula Braun | fb4f792 | 2020-07-08 17:05:15 +0200 | [diff] [blame] | 394 | if (len < recvlen || !smc_clc_msg_hdr_valid(clcm, check_trl)) { |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 395 | smc->sk.sk_err = EPROTO; |
| 396 | reason_code = -EPROTO; |
| 397 | goto out; |
| 398 | } |
Ursula Braun | fb4f792 | 2020-07-08 17:05:15 +0200 | [diff] [blame] | 399 | datlen -= len; |
| 400 | while (datlen) { |
| 401 | u8 tmp[SMC_CLC_RECV_BUF_LEN]; |
| 402 | |
| 403 | vec.iov_base = &tmp; |
| 404 | vec.iov_len = SMC_CLC_RECV_BUF_LEN; |
| 405 | /* receive remaining proposal message */ |
| 406 | recvlen = datlen > SMC_CLC_RECV_BUF_LEN ? |
| 407 | SMC_CLC_RECV_BUF_LEN : datlen; |
| 408 | iov_iter_kvec(&msg.msg_iter, READ, &vec, 1, recvlen); |
| 409 | len = sock_recvmsg(smc->clcsock, &msg, krflags); |
| 410 | datlen -= len; |
| 411 | } |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 412 | if (clcm->type == SMC_CLC_DECLINE) { |
Karsten Graul | 603cc14 | 2018-07-25 16:35:32 +0200 | [diff] [blame] | 413 | struct smc_clc_msg_decline *dclc; |
| 414 | |
| 415 | dclc = (struct smc_clc_msg_decline *)clcm; |
| 416 | reason_code = SMC_CLC_DECL_PEERDECL; |
| 417 | smc->peer_diagnosis = ntohl(dclc->peer_diagnosis); |
Ursula Braun | f1eb02f | 2020-09-26 12:44:20 +0200 | [diff] [blame] | 418 | if (((struct smc_clc_msg_decline *)buf)->hdr.typev2 & |
| 419 | SMC_FIRST_CONTACT_MASK) { |
Karsten Graul | 517c300 | 2018-05-15 17:05:03 +0200 | [diff] [blame] | 420 | smc->conn.lgr->sync_err = 1; |
Karsten Graul | 5f78fe9 | 2020-02-17 16:24:54 +0100 | [diff] [blame] | 421 | smc_lgr_terminate_sched(smc->conn.lgr); |
Ursula Braun | bfbedfd | 2017-09-21 09:16:32 +0200 | [diff] [blame] | 422 | } |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 423 | } |
| 424 | |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 425 | out: |
Ursula Braun | 2b59f58 | 2018-11-22 10:26:39 +0100 | [diff] [blame] | 426 | clc_sk->sk_rcvtimeo = rcvtimeo; |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 427 | return reason_code; |
| 428 | } |
| 429 | |
| 430 | /* send CLC DECLINE message across internal TCP socket */ |
Ursula Braun | bfbedfd | 2017-09-21 09:16:32 +0200 | [diff] [blame] | 431 | 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] | 432 | { |
| 433 | struct smc_clc_msg_decline dclc; |
| 434 | struct msghdr msg; |
| 435 | struct kvec vec; |
| 436 | int len; |
| 437 | |
| 438 | memset(&dclc, 0, sizeof(dclc)); |
| 439 | memcpy(dclc.hdr.eyecatcher, SMC_EYECATCHER, sizeof(SMC_EYECATCHER)); |
| 440 | dclc.hdr.type = SMC_CLC_DECLINE; |
| 441 | dclc.hdr.length = htons(sizeof(struct smc_clc_msg_decline)); |
Ursula Braun | 5ac54d8 | 2020-09-10 18:48:21 +0200 | [diff] [blame] | 442 | dclc.hdr.version = SMC_V1; |
Ursula Braun | f1eb02f | 2020-09-26 12:44:20 +0200 | [diff] [blame] | 443 | dclc.hdr.typev2 = (peer_diag_info == SMC_CLC_DECL_SYNCERR) ? |
| 444 | SMC_FIRST_CONTACT_MASK : 0; |
Hans Wippel | a082ec8 | 2020-02-25 22:41:22 +0100 | [diff] [blame] | 445 | if ((!smc->conn.lgr || !smc->conn.lgr->is_smcd) && |
| 446 | smc_ib_is_valid_local_systemid()) |
Ursula Braun | 369537c | 2020-02-14 08:59:00 +0100 | [diff] [blame] | 447 | memcpy(dclc.id_for_peer, local_systemid, |
| 448 | sizeof(local_systemid)); |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 449 | dclc.peer_diagnosis = htonl(peer_diag_info); |
| 450 | memcpy(dclc.trl.eyecatcher, SMC_EYECATCHER, sizeof(SMC_EYECATCHER)); |
| 451 | |
| 452 | memset(&msg, 0, sizeof(msg)); |
| 453 | vec.iov_base = &dclc; |
| 454 | vec.iov_len = sizeof(struct smc_clc_msg_decline); |
| 455 | len = kernel_sendmsg(smc->clcsock, &msg, &vec, 1, |
| 456 | sizeof(struct smc_clc_msg_decline)); |
Ursula Braun | 14d22d4 | 2019-01-30 18:51:00 +0100 | [diff] [blame] | 457 | if (len < 0 || len < sizeof(struct smc_clc_msg_decline)) |
Ursula Braun | 6ae36bf | 2018-11-22 10:26:36 +0100 | [diff] [blame] | 458 | len = -EPROTO; |
| 459 | return len > 0 ? 0 : len; |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | /* send CLC PROPOSAL message across internal TCP socket */ |
Ursula Braun | d70bf4f | 2020-09-26 12:44:27 +0200 | [diff] [blame] | 463 | 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] | 464 | { |
Ursula Braun | 8c3dca3 | 2020-09-26 12:44:28 +0200 | [diff] [blame] | 465 | struct smc_clc_smcd_v2_extension *smcd_v2_ext; |
Ursula Braun | 6bb14e48 | 2020-09-10 18:48:22 +0200 | [diff] [blame] | 466 | struct smc_clc_msg_proposal_prefix *pclc_prfx; |
| 467 | struct smc_clc_msg_proposal *pclc_base; |
Ursula Braun | 8c3dca3 | 2020-09-26 12:44:28 +0200 | [diff] [blame] | 468 | struct smc_clc_smcd_gid_chid *gidchids; |
Ursula Braun | 6bb14e48 | 2020-09-10 18:48:22 +0200 | [diff] [blame] | 469 | struct smc_clc_msg_proposal_area *pclc; |
| 470 | struct smc_clc_ipv6_prefix *ipv6_prfx; |
Ursula Braun | 8c3dca3 | 2020-09-26 12:44:28 +0200 | [diff] [blame] | 471 | struct smc_clc_v2_extension *v2_ext; |
Ursula Braun | 6bb14e48 | 2020-09-10 18:48:22 +0200 | [diff] [blame] | 472 | struct smc_clc_msg_smcd *pclc_smcd; |
| 473 | struct smc_clc_msg_trail *trl; |
Karsten Graul | 1a26d02 | 2018-03-16 15:06:40 +0100 | [diff] [blame] | 474 | int len, i, plen, rc; |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 475 | int reason_code = 0; |
Ursula Braun | 8c3dca3 | 2020-09-26 12:44:28 +0200 | [diff] [blame] | 476 | struct kvec vec[8]; |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 477 | struct msghdr msg; |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 478 | |
Ursula Braun | 6bb14e48 | 2020-09-10 18:48:22 +0200 | [diff] [blame] | 479 | pclc = kzalloc(sizeof(*pclc), GFP_KERNEL); |
| 480 | if (!pclc) |
| 481 | return -ENOMEM; |
| 482 | |
| 483 | pclc_base = &pclc->pclc_base; |
| 484 | pclc_smcd = &pclc->pclc_smcd; |
| 485 | pclc_prfx = &pclc->pclc_prfx; |
| 486 | ipv6_prfx = pclc->pclc_prfx_ipv6; |
Ursula Braun | 8c3dca3 | 2020-09-26 12:44:28 +0200 | [diff] [blame] | 487 | v2_ext = &pclc->pclc_v2_ext; |
| 488 | smcd_v2_ext = &pclc->pclc_smcd_v2_ext; |
| 489 | gidchids = pclc->pclc_gidchids; |
Ursula Braun | 6bb14e48 | 2020-09-10 18:48:22 +0200 | [diff] [blame] | 490 | trl = &pclc->pclc_trl; |
| 491 | |
Ursula Braun | 8c3dca3 | 2020-09-26 12:44:28 +0200 | [diff] [blame] | 492 | pclc_base->hdr.version = SMC_V2; |
| 493 | pclc_base->hdr.typev1 = ini->smc_type_v1; |
| 494 | pclc_base->hdr.typev2 = ini->smc_type_v2; |
| 495 | plen = sizeof(*pclc_base) + sizeof(*pclc_smcd) + sizeof(*trl); |
| 496 | |
Karsten Graul | c246d94 | 2018-03-16 15:06:39 +0100 | [diff] [blame] | 497 | /* retrieve ip prefixes for CLC proposal msg */ |
Ursula Braun | 8c3dca3 | 2020-09-26 12:44:28 +0200 | [diff] [blame] | 498 | if (ini->smc_type_v1 != SMC_TYPE_N) { |
| 499 | rc = smc_clc_prfx_set(smc->clcsock, pclc_prfx, ipv6_prfx); |
| 500 | if (rc) { |
| 501 | if (ini->smc_type_v2 == SMC_TYPE_N) { |
| 502 | kfree(pclc); |
| 503 | return SMC_CLC_DECL_CNFERR; |
| 504 | } |
| 505 | pclc_base->hdr.typev1 = SMC_TYPE_N; |
| 506 | } else { |
| 507 | pclc_base->iparea_offset = htons(sizeof(*pclc_smcd)); |
| 508 | plen += sizeof(*pclc_prfx) + |
| 509 | pclc_prfx->ipv6_prefixes_cnt * |
| 510 | sizeof(ipv6_prfx[0]); |
| 511 | } |
Ursula Braun | 6bb14e48 | 2020-09-10 18:48:22 +0200 | [diff] [blame] | 512 | } |
Karsten Graul | c246d94 | 2018-03-16 15:06:39 +0100 | [diff] [blame] | 513 | |
Ursula Braun | 8c3dca3 | 2020-09-26 12:44:28 +0200 | [diff] [blame] | 514 | /* build SMC Proposal CLC message */ |
Ursula Braun | 6bb14e48 | 2020-09-10 18:48:22 +0200 | [diff] [blame] | 515 | memcpy(pclc_base->hdr.eyecatcher, SMC_EYECATCHER, |
| 516 | sizeof(SMC_EYECATCHER)); |
| 517 | pclc_base->hdr.type = SMC_CLC_PROPOSAL; |
Ursula Braun | d70bf4f | 2020-09-26 12:44:27 +0200 | [diff] [blame] | 518 | if (smcr_indicated(ini->smc_type_v1)) { |
Hans Wippel | c758dfd | 2018-06-28 19:05:09 +0200 | [diff] [blame] | 519 | /* add SMC-R specifics */ |
Ursula Braun | 6bb14e48 | 2020-09-10 18:48:22 +0200 | [diff] [blame] | 520 | memcpy(pclc_base->lcl.id_for_peer, local_systemid, |
Hans Wippel | c758dfd | 2018-06-28 19:05:09 +0200 | [diff] [blame] | 521 | sizeof(local_systemid)); |
Ursula Braun | 6bb14e48 | 2020-09-10 18:48:22 +0200 | [diff] [blame] | 522 | memcpy(pclc_base->lcl.gid, ini->ib_gid, SMC_GID_SIZE); |
| 523 | 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] | 524 | ETH_ALEN); |
Hans Wippel | c758dfd | 2018-06-28 19:05:09 +0200 | [diff] [blame] | 525 | } |
Ursula Braun | d70bf4f | 2020-09-26 12:44:27 +0200 | [diff] [blame] | 526 | if (smcd_indicated(ini->smc_type_v1)) { |
Hans Wippel | c758dfd | 2018-06-28 19:05:09 +0200 | [diff] [blame] | 527 | /* add SMC-D specifics */ |
Ursula Braun | 8c3dca3 | 2020-09-26 12:44:28 +0200 | [diff] [blame] | 528 | if (ini->ism_dev[0]) { |
| 529 | pclc_smcd->ism.gid = htonll(ini->ism_dev[0]->local_gid); |
| 530 | pclc_smcd->ism.chid = |
| 531 | htons(smc_ism_get_chid(ini->ism_dev[0])); |
| 532 | } |
| 533 | } |
| 534 | if (ini->smc_type_v2 == SMC_TYPE_N) { |
| 535 | pclc_smcd->v2_ext_offset = 0; |
| 536 | } else { |
| 537 | u16 v2_ext_offset; |
| 538 | u8 *eid = NULL; |
| 539 | |
| 540 | v2_ext_offset = sizeof(*pclc_smcd) - |
| 541 | offsetofend(struct smc_clc_msg_smcd, v2_ext_offset); |
| 542 | if (ini->smc_type_v1 != SMC_TYPE_N) |
| 543 | v2_ext_offset += sizeof(*pclc_prfx) + |
| 544 | pclc_prfx->ipv6_prefixes_cnt * |
| 545 | sizeof(ipv6_prfx[0]); |
| 546 | pclc_smcd->v2_ext_offset = htons(v2_ext_offset); |
| 547 | v2_ext->hdr.eid_cnt = 0; |
| 548 | v2_ext->hdr.ism_gid_cnt = ini->ism_offered_cnt; |
| 549 | v2_ext->hdr.flag.release = SMC_RELEASE; |
| 550 | v2_ext->hdr.flag.seid = 1; |
| 551 | v2_ext->hdr.smcd_v2_ext_offset = htons(sizeof(*v2_ext) - |
| 552 | offsetofend(struct smc_clnt_opts_area_hdr, |
| 553 | smcd_v2_ext_offset) + |
| 554 | v2_ext->hdr.eid_cnt * SMC_MAX_EID_LEN); |
| 555 | if (ini->ism_dev[0]) |
| 556 | smc_ism_get_system_eid(ini->ism_dev[0], &eid); |
| 557 | else |
| 558 | smc_ism_get_system_eid(ini->ism_dev[1], &eid); |
| 559 | if (eid) |
| 560 | memcpy(smcd_v2_ext->system_eid, eid, SMC_MAX_EID_LEN); |
| 561 | plen += sizeof(*v2_ext) + sizeof(*smcd_v2_ext); |
| 562 | if (ini->ism_offered_cnt) { |
| 563 | for (i = 1; i <= ini->ism_offered_cnt; i++) { |
| 564 | gidchids[i - 1].gid = |
| 565 | htonll(ini->ism_dev[i]->local_gid); |
| 566 | gidchids[i - 1].chid = |
| 567 | htons(smc_ism_get_chid(ini->ism_dev[i])); |
| 568 | } |
| 569 | plen += ini->ism_offered_cnt * |
| 570 | sizeof(struct smc_clc_smcd_gid_chid); |
| 571 | } |
Hans Wippel | c758dfd | 2018-06-28 19:05:09 +0200 | [diff] [blame] | 572 | } |
Ursula Braun | 6bb14e48 | 2020-09-10 18:48:22 +0200 | [diff] [blame] | 573 | pclc_base->hdr.length = htons(plen); |
Ursula Braun | 6bb14e48 | 2020-09-10 18:48:22 +0200 | [diff] [blame] | 574 | memcpy(trl->eyecatcher, SMC_EYECATCHER, sizeof(SMC_EYECATCHER)); |
Ursula Braun | 8c3dca3 | 2020-09-26 12:44:28 +0200 | [diff] [blame] | 575 | |
| 576 | /* send SMC Proposal CLC message */ |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 577 | memset(&msg, 0, sizeof(msg)); |
Karsten Graul | 1a26d02 | 2018-03-16 15:06:40 +0100 | [diff] [blame] | 578 | i = 0; |
Ursula Braun | 6bb14e48 | 2020-09-10 18:48:22 +0200 | [diff] [blame] | 579 | vec[i].iov_base = pclc_base; |
| 580 | vec[i++].iov_len = sizeof(*pclc_base); |
Ursula Braun | 8c3dca3 | 2020-09-26 12:44:28 +0200 | [diff] [blame] | 581 | vec[i].iov_base = pclc_smcd; |
| 582 | vec[i++].iov_len = sizeof(*pclc_smcd); |
| 583 | if (ini->smc_type_v1 != SMC_TYPE_N) { |
| 584 | vec[i].iov_base = pclc_prfx; |
| 585 | vec[i++].iov_len = sizeof(*pclc_prfx); |
| 586 | if (pclc_prfx->ipv6_prefixes_cnt > 0) { |
| 587 | vec[i].iov_base = ipv6_prfx; |
| 588 | vec[i++].iov_len = pclc_prfx->ipv6_prefixes_cnt * |
| 589 | sizeof(ipv6_prfx[0]); |
| 590 | } |
Hans Wippel | c758dfd | 2018-06-28 19:05:09 +0200 | [diff] [blame] | 591 | } |
Ursula Braun | 8c3dca3 | 2020-09-26 12:44:28 +0200 | [diff] [blame] | 592 | if (ini->smc_type_v2 != SMC_TYPE_N) { |
| 593 | vec[i].iov_base = v2_ext; |
| 594 | vec[i++].iov_len = sizeof(*v2_ext); |
| 595 | vec[i].iov_base = smcd_v2_ext; |
| 596 | vec[i++].iov_len = sizeof(*smcd_v2_ext); |
| 597 | if (ini->ism_offered_cnt) { |
| 598 | vec[i].iov_base = gidchids; |
| 599 | vec[i++].iov_len = ini->ism_offered_cnt * |
| 600 | sizeof(struct smc_clc_smcd_gid_chid); |
| 601 | } |
Karsten Graul | 1a26d02 | 2018-03-16 15:06:40 +0100 | [diff] [blame] | 602 | } |
Ursula Braun | 6bb14e48 | 2020-09-10 18:48:22 +0200 | [diff] [blame] | 603 | vec[i].iov_base = trl; |
| 604 | vec[i++].iov_len = sizeof(*trl); |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 605 | /* due to the few bytes needed for clc-handshake this cannot block */ |
Karsten Graul | 1a26d02 | 2018-03-16 15:06:40 +0100 | [diff] [blame] | 606 | len = kernel_sendmsg(smc->clcsock, &msg, vec, i, plen); |
YueHaibing | 3818977 | 2018-09-18 15:46:38 +0200 | [diff] [blame] | 607 | if (len < 0) { |
| 608 | smc->sk.sk_err = smc->clcsock->sk->sk_err; |
| 609 | reason_code = -smc->sk.sk_err; |
Ursula Braun | 6bb14e48 | 2020-09-10 18:48:22 +0200 | [diff] [blame] | 610 | } else if (len < ntohs(pclc_base->hdr.length)) { |
YueHaibing | 3818977 | 2018-09-18 15:46:38 +0200 | [diff] [blame] | 611 | reason_code = -ENETUNREACH; |
| 612 | smc->sk.sk_err = -reason_code; |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 613 | } |
| 614 | |
Ursula Braun | 6bb14e48 | 2020-09-10 18:48:22 +0200 | [diff] [blame] | 615 | kfree(pclc); |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 616 | return reason_code; |
| 617 | } |
| 618 | |
Ursula Braun | 3d9725a | 2020-09-10 18:48:23 +0200 | [diff] [blame] | 619 | /* build and send CLC CONFIRM / ACCEPT message */ |
| 620 | static int smc_clc_send_confirm_accept(struct smc_sock *smc, |
Ursula Braun | a7c9c5f | 2020-09-26 12:44:30 +0200 | [diff] [blame^] | 621 | struct smc_clc_msg_accept_confirm_v2 *clc_v2, |
| 622 | int first_contact, u8 version) |
Ursula Braun | 3d9725a | 2020-09-10 18:48:23 +0200 | [diff] [blame] | 623 | { |
| 624 | struct smc_connection *conn = &smc->conn; |
Ursula Braun | a7c9c5f | 2020-09-26 12:44:30 +0200 | [diff] [blame^] | 625 | struct smc_clc_msg_accept_confirm *clc; |
Ursula Braun | e15c6c4 | 2020-09-26 12:44:22 +0200 | [diff] [blame] | 626 | struct smc_clc_msg_trail trl; |
| 627 | struct kvec vec[2]; |
Ursula Braun | 3d9725a | 2020-09-10 18:48:23 +0200 | [diff] [blame] | 628 | struct msghdr msg; |
Ursula Braun | e15c6c4 | 2020-09-26 12:44:22 +0200 | [diff] [blame] | 629 | int i; |
Ursula Braun | 3d9725a | 2020-09-10 18:48:23 +0200 | [diff] [blame] | 630 | |
| 631 | /* send SMC Confirm CLC msg */ |
Ursula Braun | a7c9c5f | 2020-09-26 12:44:30 +0200 | [diff] [blame^] | 632 | clc = (struct smc_clc_msg_accept_confirm *)clc_v2; |
| 633 | clc->hdr.version = version; /* SMC version */ |
Ursula Braun | 3d9725a | 2020-09-10 18:48:23 +0200 | [diff] [blame] | 634 | if (first_contact) |
Ursula Braun | f1eb02f | 2020-09-26 12:44:20 +0200 | [diff] [blame] | 635 | clc->hdr.typev2 |= SMC_FIRST_CONTACT_MASK; |
Ursula Braun | 3d9725a | 2020-09-10 18:48:23 +0200 | [diff] [blame] | 636 | if (conn->lgr->is_smcd) { |
| 637 | /* SMC-D specific settings */ |
| 638 | memcpy(clc->hdr.eyecatcher, SMCD_EYECATCHER, |
| 639 | sizeof(SMCD_EYECATCHER)); |
Ursula Braun | f1eb02f | 2020-09-26 12:44:20 +0200 | [diff] [blame] | 640 | clc->hdr.typev1 = SMC_TYPE_D; |
Ursula Braun | 3d9725a | 2020-09-10 18:48:23 +0200 | [diff] [blame] | 641 | clc->d0.gid = conn->lgr->smcd->local_gid; |
| 642 | clc->d0.token = conn->rmb_desc->token; |
| 643 | clc->d0.dmbe_size = conn->rmbe_size_short; |
| 644 | clc->d0.dmbe_idx = 0; |
| 645 | memcpy(&clc->d0.linkid, conn->lgr->id, SMC_LGR_ID_SIZE); |
Ursula Braun | a7c9c5f | 2020-09-26 12:44:30 +0200 | [diff] [blame^] | 646 | if (version == SMC_V1) { |
| 647 | clc->hdr.length = htons(SMCD_CLC_ACCEPT_CONFIRM_LEN); |
| 648 | } else { |
| 649 | u8 *eid = NULL; |
| 650 | |
| 651 | clc_v2->chid = htons(smc_ism_get_chid(conn->lgr->smcd)); |
| 652 | smc_ism_get_system_eid(conn->lgr->smcd, &eid); |
| 653 | if (eid) |
| 654 | memcpy(clc_v2->eid, eid, SMC_MAX_EID_LEN); |
| 655 | clc_v2->hdr.length = |
| 656 | htons(SMCD_CLC_ACCEPT_CONFIRM_LEN_V2); |
| 657 | } |
Ursula Braun | e15c6c4 | 2020-09-26 12:44:22 +0200 | [diff] [blame] | 658 | memcpy(trl.eyecatcher, SMCD_EYECATCHER, |
Ursula Braun | 3d9725a | 2020-09-10 18:48:23 +0200 | [diff] [blame] | 659 | sizeof(SMCD_EYECATCHER)); |
| 660 | } else { |
| 661 | struct smc_link *link = conn->lnk; |
| 662 | |
| 663 | /* SMC-R specific settings */ |
| 664 | link = conn->lnk; |
| 665 | memcpy(clc->hdr.eyecatcher, SMC_EYECATCHER, |
| 666 | sizeof(SMC_EYECATCHER)); |
Ursula Braun | f1eb02f | 2020-09-26 12:44:20 +0200 | [diff] [blame] | 667 | clc->hdr.typev1 = SMC_TYPE_R; |
Ursula Braun | 3d9725a | 2020-09-10 18:48:23 +0200 | [diff] [blame] | 668 | clc->hdr.length = htons(SMCR_CLC_ACCEPT_CONFIRM_LEN); |
| 669 | memcpy(clc->r0.lcl.id_for_peer, local_systemid, |
| 670 | sizeof(local_systemid)); |
| 671 | memcpy(&clc->r0.lcl.gid, link->gid, SMC_GID_SIZE); |
| 672 | memcpy(&clc->r0.lcl.mac, &link->smcibdev->mac[link->ibport - 1], |
| 673 | ETH_ALEN); |
| 674 | hton24(clc->r0.qpn, link->roce_qp->qp_num); |
| 675 | clc->r0.rmb_rkey = |
| 676 | htonl(conn->rmb_desc->mr_rx[link->link_idx]->rkey); |
| 677 | clc->r0.rmbe_idx = 1; /* for now: 1 RMB = 1 RMBE */ |
| 678 | clc->r0.rmbe_alert_token = htonl(conn->alert_token_local); |
| 679 | switch (clc->hdr.type) { |
| 680 | case SMC_CLC_ACCEPT: |
| 681 | clc->r0.qp_mtu = link->path_mtu; |
| 682 | break; |
| 683 | case SMC_CLC_CONFIRM: |
| 684 | clc->r0.qp_mtu = min(link->path_mtu, link->peer_mtu); |
| 685 | break; |
| 686 | } |
| 687 | clc->r0.rmbe_size = conn->rmbe_size_short; |
| 688 | clc->r0.rmb_dma_addr = cpu_to_be64((u64)sg_dma_address |
| 689 | (conn->rmb_desc->sgt[link->link_idx].sgl)); |
| 690 | hton24(clc->r0.psn, link->psn_initial); |
Ursula Braun | e15c6c4 | 2020-09-26 12:44:22 +0200 | [diff] [blame] | 691 | memcpy(trl.eyecatcher, SMC_EYECATCHER, sizeof(SMC_EYECATCHER)); |
Ursula Braun | 3d9725a | 2020-09-10 18:48:23 +0200 | [diff] [blame] | 692 | } |
| 693 | |
| 694 | memset(&msg, 0, sizeof(msg)); |
Ursula Braun | e15c6c4 | 2020-09-26 12:44:22 +0200 | [diff] [blame] | 695 | i = 0; |
Ursula Braun | a7c9c5f | 2020-09-26 12:44:30 +0200 | [diff] [blame^] | 696 | vec[i].iov_base = clc_v2; |
| 697 | if (version > SMC_V1) |
| 698 | vec[i++].iov_len = SMCD_CLC_ACCEPT_CONFIRM_LEN_V2 - sizeof(trl); |
| 699 | else |
| 700 | vec[i++].iov_len = (clc->hdr.typev1 == SMC_TYPE_D ? |
| 701 | SMCD_CLC_ACCEPT_CONFIRM_LEN : |
| 702 | SMCR_CLC_ACCEPT_CONFIRM_LEN) - |
| 703 | sizeof(trl); |
Ursula Braun | e15c6c4 | 2020-09-26 12:44:22 +0200 | [diff] [blame] | 704 | vec[i].iov_base = &trl; |
| 705 | vec[i++].iov_len = sizeof(trl); |
| 706 | return kernel_sendmsg(smc->clcsock, &msg, vec, 1, |
Ursula Braun | 3d9725a | 2020-09-10 18:48:23 +0200 | [diff] [blame] | 707 | ntohs(clc->hdr.length)); |
| 708 | } |
| 709 | |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 710 | /* send CLC CONFIRM message across internal TCP socket */ |
Ursula Braun | a7c9c5f | 2020-09-26 12:44:30 +0200 | [diff] [blame^] | 711 | int smc_clc_send_confirm(struct smc_sock *smc, bool clnt_first_contact, |
| 712 | u8 version) |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 713 | { |
Ursula Braun | a7c9c5f | 2020-09-26 12:44:30 +0200 | [diff] [blame^] | 714 | struct smc_clc_msg_accept_confirm_v2 cclc_v2; |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 715 | int reason_code = 0; |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 716 | int len; |
| 717 | |
| 718 | /* send SMC Confirm CLC msg */ |
Ursula Braun | a7c9c5f | 2020-09-26 12:44:30 +0200 | [diff] [blame^] | 719 | memset(&cclc_v2, 0, sizeof(cclc_v2)); |
| 720 | cclc_v2.hdr.type = SMC_CLC_CONFIRM; |
| 721 | len = smc_clc_send_confirm_accept(smc, &cclc_v2, clnt_first_contact, |
| 722 | version); |
| 723 | if (len < ntohs(cclc_v2.hdr.length)) { |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 724 | if (len >= 0) { |
| 725 | reason_code = -ENETUNREACH; |
| 726 | smc->sk.sk_err = -reason_code; |
| 727 | } else { |
| 728 | smc->sk.sk_err = smc->clcsock->sk->sk_err; |
| 729 | reason_code = -smc->sk.sk_err; |
| 730 | } |
| 731 | } |
| 732 | return reason_code; |
| 733 | } |
| 734 | |
| 735 | /* send CLC ACCEPT message across internal TCP socket */ |
Ursula Braun | a7c9c5f | 2020-09-26 12:44:30 +0200 | [diff] [blame^] | 736 | int smc_clc_send_accept(struct smc_sock *new_smc, bool srv_first_contact, |
| 737 | u8 version) |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 738 | { |
Ursula Braun | a7c9c5f | 2020-09-26 12:44:30 +0200 | [diff] [blame^] | 739 | struct smc_clc_msg_accept_confirm_v2 aclc_v2; |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 740 | int len; |
| 741 | |
Ursula Braun | a7c9c5f | 2020-09-26 12:44:30 +0200 | [diff] [blame^] | 742 | memset(&aclc_v2, 0, sizeof(aclc_v2)); |
| 743 | aclc_v2.hdr.type = SMC_CLC_ACCEPT; |
| 744 | len = smc_clc_send_confirm_accept(new_smc, &aclc_v2, srv_first_contact, |
| 745 | version); |
| 746 | if (len < ntohs(aclc_v2.hdr.length)) |
Ursula Braun | 6ae36bf | 2018-11-22 10:26:36 +0100 | [diff] [blame] | 747 | len = len >= 0 ? -EPROTO : -new_smc->clcsock->sk->sk_err; |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 748 | |
Ursula Braun | 6ae36bf | 2018-11-22 10:26:36 +0100 | [diff] [blame] | 749 | return len > 0 ? 0 : len; |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 750 | } |