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