Sean Hefty | 7025fcd | 2006-06-17 20:37:28 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2005 Voltaire Inc. All rights reserved. |
| 3 | * Copyright (c) 2002-2005, Network Appliance, Inc. All rights reserved. |
| 4 | * Copyright (c) 1999-2005, Mellanox Technologies, Inc. All rights reserved. |
| 5 | * Copyright (c) 2005 Intel Corporation. All rights reserved. |
| 6 | * |
Sean Hefty | a947491 | 2008-07-14 23:48:43 -0700 | [diff] [blame] | 7 | * This software is available to you under a choice of one of two |
| 8 | * licenses. You may choose to be licensed under the terms of the GNU |
| 9 | * General Public License (GPL) Version 2, available from the file |
| 10 | * COPYING in the main directory of this source tree, or the |
| 11 | * OpenIB.org BSD license below: |
Sean Hefty | 7025fcd | 2006-06-17 20:37:28 -0700 | [diff] [blame] | 12 | * |
Sean Hefty | a947491 | 2008-07-14 23:48:43 -0700 | [diff] [blame] | 13 | * Redistribution and use in source and binary forms, with or |
| 14 | * without modification, are permitted provided that the following |
| 15 | * conditions are met: |
Sean Hefty | 7025fcd | 2006-06-17 20:37:28 -0700 | [diff] [blame] | 16 | * |
Sean Hefty | a947491 | 2008-07-14 23:48:43 -0700 | [diff] [blame] | 17 | * - Redistributions of source code must retain the above |
| 18 | * copyright notice, this list of conditions and the following |
| 19 | * disclaimer. |
Sean Hefty | 7025fcd | 2006-06-17 20:37:28 -0700 | [diff] [blame] | 20 | * |
Sean Hefty | a947491 | 2008-07-14 23:48:43 -0700 | [diff] [blame] | 21 | * - Redistributions in binary form must reproduce the above |
| 22 | * copyright notice, this list of conditions and the following |
| 23 | * disclaimer in the documentation and/or other materials |
| 24 | * provided with the distribution. |
Sean Hefty | 7025fcd | 2006-06-17 20:37:28 -0700 | [diff] [blame] | 25 | * |
Sean Hefty | a947491 | 2008-07-14 23:48:43 -0700 | [diff] [blame] | 26 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 27 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 28 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 29 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 30 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 31 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 32 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 33 | * SOFTWARE. |
Sean Hefty | 7025fcd | 2006-06-17 20:37:28 -0700 | [diff] [blame] | 34 | */ |
| 35 | |
| 36 | #include <linux/mutex.h> |
| 37 | #include <linux/inetdevice.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 38 | #include <linux/slab.h> |
Sean Hefty | 7025fcd | 2006-06-17 20:37:28 -0700 | [diff] [blame] | 39 | #include <linux/workqueue.h> |
Paul Gortmaker | e4dd23d | 2011-05-27 15:35:46 -0400 | [diff] [blame] | 40 | #include <linux/module.h> |
Sean Hefty | 7025fcd | 2006-06-17 20:37:28 -0700 | [diff] [blame] | 41 | #include <net/arp.h> |
| 42 | #include <net/neighbour.h> |
| 43 | #include <net/route.h> |
Tom Tucker | e795d09 | 2006-07-30 20:44:19 -0700 | [diff] [blame] | 44 | #include <net/netevent.h> |
Aleksey Senin | 38617c6 | 2008-12-24 10:16:37 -0800 | [diff] [blame] | 45 | #include <net/addrconf.h> |
| 46 | #include <net/ip6_route.h> |
Sean Hefty | 7025fcd | 2006-06-17 20:37:28 -0700 | [diff] [blame] | 47 | #include <rdma/ib_addr.h> |
Sean Hefty | ef56086 | 2013-05-29 10:09:10 -0700 | [diff] [blame] | 48 | #include <rdma/ib.h> |
Mark Bloch | ae43f82 | 2016-05-19 17:12:36 +0300 | [diff] [blame] | 49 | #include <rdma/rdma_netlink.h> |
| 50 | #include <net/netlink.h> |
| 51 | |
| 52 | #include "core_priv.h" |
Sean Hefty | 7025fcd | 2006-06-17 20:37:28 -0700 | [diff] [blame] | 53 | |
Sean Hefty | 7025fcd | 2006-06-17 20:37:28 -0700 | [diff] [blame] | 54 | struct addr_req { |
| 55 | struct list_head list; |
Aleksey Senin | 38617c6 | 2008-12-24 10:16:37 -0800 | [diff] [blame] | 56 | struct sockaddr_storage src_addr; |
| 57 | struct sockaddr_storage dst_addr; |
Sean Hefty | 7025fcd | 2006-06-17 20:37:28 -0700 | [diff] [blame] | 58 | struct rdma_dev_addr *addr; |
Sean Hefty | 7a118df | 2006-10-31 11:12:59 -0800 | [diff] [blame] | 59 | struct rdma_addr_client *client; |
Sean Hefty | 7025fcd | 2006-06-17 20:37:28 -0700 | [diff] [blame] | 60 | void *context; |
| 61 | void (*callback)(int status, struct sockaddr *src_addr, |
| 62 | struct rdma_dev_addr *addr, void *context); |
| 63 | unsigned long timeout; |
| 64 | int status; |
Mark Bloch | ae43f82 | 2016-05-19 17:12:36 +0300 | [diff] [blame] | 65 | u32 seq; |
Sean Hefty | 7025fcd | 2006-06-17 20:37:28 -0700 | [diff] [blame] | 66 | }; |
| 67 | |
Mark Bloch | ae43f82 | 2016-05-19 17:12:36 +0300 | [diff] [blame] | 68 | static atomic_t ib_nl_addr_request_seq = ATOMIC_INIT(0); |
| 69 | |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 70 | static void process_req(struct work_struct *work); |
Sean Hefty | 7025fcd | 2006-06-17 20:37:28 -0700 | [diff] [blame] | 71 | |
| 72 | static DEFINE_MUTEX(lock); |
| 73 | static LIST_HEAD(req_list); |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 74 | static DECLARE_DELAYED_WORK(work, process_req); |
Sean Hefty | 7025fcd | 2006-06-17 20:37:28 -0700 | [diff] [blame] | 75 | static struct workqueue_struct *addr_wq; |
| 76 | |
Mark Bloch | ae43f82 | 2016-05-19 17:12:36 +0300 | [diff] [blame] | 77 | static const struct nla_policy ib_nl_addr_policy[LS_NLA_TYPE_MAX] = { |
| 78 | [LS_NLA_TYPE_DGID] = {.type = NLA_BINARY, |
| 79 | .len = sizeof(struct rdma_nla_ls_gid)}, |
| 80 | }; |
| 81 | |
| 82 | static inline bool ib_nl_is_good_ip_resp(const struct nlmsghdr *nlh) |
| 83 | { |
| 84 | struct nlattr *tb[LS_NLA_TYPE_MAX] = {}; |
| 85 | int ret; |
| 86 | |
| 87 | if (nlh->nlmsg_flags & RDMA_NL_LS_F_ERR) |
| 88 | return false; |
| 89 | |
| 90 | ret = nla_parse(tb, LS_NLA_TYPE_MAX - 1, nlmsg_data(nlh), |
Johannes Berg | fceb643 | 2017-04-12 14:34:07 +0200 | [diff] [blame] | 91 | nlmsg_len(nlh), ib_nl_addr_policy, NULL); |
Mark Bloch | ae43f82 | 2016-05-19 17:12:36 +0300 | [diff] [blame] | 92 | if (ret) |
| 93 | return false; |
| 94 | |
| 95 | return true; |
| 96 | } |
| 97 | |
| 98 | static void ib_nl_process_good_ip_rsep(const struct nlmsghdr *nlh) |
| 99 | { |
| 100 | const struct nlattr *head, *curr; |
| 101 | union ib_gid gid; |
| 102 | struct addr_req *req; |
| 103 | int len, rem; |
| 104 | int found = 0; |
| 105 | |
| 106 | head = (const struct nlattr *)nlmsg_data(nlh); |
| 107 | len = nlmsg_len(nlh); |
| 108 | |
| 109 | nla_for_each_attr(curr, head, len, rem) { |
| 110 | if (curr->nla_type == LS_NLA_TYPE_DGID) |
| 111 | memcpy(&gid, nla_data(curr), nla_len(curr)); |
| 112 | } |
| 113 | |
| 114 | mutex_lock(&lock); |
| 115 | list_for_each_entry(req, &req_list, list) { |
| 116 | if (nlh->nlmsg_seq != req->seq) |
| 117 | continue; |
| 118 | /* We set the DGID part, the rest was set earlier */ |
| 119 | rdma_addr_set_dgid(req->addr, &gid); |
| 120 | req->status = 0; |
| 121 | found = 1; |
| 122 | break; |
| 123 | } |
| 124 | mutex_unlock(&lock); |
| 125 | |
| 126 | if (!found) |
| 127 | pr_info("Couldn't find request waiting for DGID: %pI6\n", |
| 128 | &gid); |
| 129 | } |
| 130 | |
| 131 | int ib_nl_handle_ip_res_resp(struct sk_buff *skb, |
| 132 | struct netlink_callback *cb) |
| 133 | { |
| 134 | const struct nlmsghdr *nlh = (struct nlmsghdr *)cb->nlh; |
| 135 | |
| 136 | if ((nlh->nlmsg_flags & NLM_F_REQUEST) || |
| 137 | !(NETLINK_CB(skb).sk) || |
| 138 | !netlink_capable(skb, CAP_NET_ADMIN)) |
| 139 | return -EPERM; |
| 140 | |
| 141 | if (ib_nl_is_good_ip_resp(nlh)) |
| 142 | ib_nl_process_good_ip_rsep(nlh); |
| 143 | |
| 144 | return skb->len; |
| 145 | } |
| 146 | |
| 147 | static int ib_nl_ip_send_msg(struct rdma_dev_addr *dev_addr, |
| 148 | const void *daddr, |
| 149 | u32 seq, u16 family) |
| 150 | { |
| 151 | struct sk_buff *skb = NULL; |
| 152 | struct nlmsghdr *nlh; |
| 153 | struct rdma_ls_ip_resolve_header *header; |
| 154 | void *data; |
| 155 | size_t size; |
| 156 | int attrtype; |
| 157 | int len; |
| 158 | |
| 159 | if (family == AF_INET) { |
| 160 | size = sizeof(struct in_addr); |
| 161 | attrtype = RDMA_NLA_F_MANDATORY | LS_NLA_TYPE_IPV4; |
| 162 | } else { |
| 163 | size = sizeof(struct in6_addr); |
| 164 | attrtype = RDMA_NLA_F_MANDATORY | LS_NLA_TYPE_IPV6; |
| 165 | } |
| 166 | |
| 167 | len = nla_total_size(sizeof(size)); |
| 168 | len += NLMSG_ALIGN(sizeof(*header)); |
| 169 | |
| 170 | skb = nlmsg_new(len, GFP_KERNEL); |
| 171 | if (!skb) |
| 172 | return -ENOMEM; |
| 173 | |
| 174 | data = ibnl_put_msg(skb, &nlh, seq, 0, RDMA_NL_LS, |
| 175 | RDMA_NL_LS_OP_IP_RESOLVE, NLM_F_REQUEST); |
| 176 | if (!data) { |
| 177 | nlmsg_free(skb); |
| 178 | return -ENODATA; |
| 179 | } |
| 180 | |
| 181 | /* Construct the family header first */ |
| 182 | header = (struct rdma_ls_ip_resolve_header *) |
| 183 | skb_put(skb, NLMSG_ALIGN(sizeof(*header))); |
| 184 | header->ifindex = dev_addr->bound_dev_if; |
| 185 | nla_put(skb, attrtype, size, daddr); |
| 186 | |
| 187 | /* Repair the nlmsg header length */ |
| 188 | nlmsg_end(skb, nlh); |
| 189 | ibnl_multicast(skb, nlh, RDMA_NL_GROUP_LS, GFP_KERNEL); |
| 190 | |
| 191 | /* Make the request retry, so when we get the response from userspace |
| 192 | * we will have something. |
| 193 | */ |
| 194 | return -ENODATA; |
| 195 | } |
| 196 | |
Sean Hefty | ef56086 | 2013-05-29 10:09:10 -0700 | [diff] [blame] | 197 | int rdma_addr_size(struct sockaddr *addr) |
| 198 | { |
| 199 | switch (addr->sa_family) { |
| 200 | case AF_INET: |
| 201 | return sizeof(struct sockaddr_in); |
| 202 | case AF_INET6: |
| 203 | return sizeof(struct sockaddr_in6); |
| 204 | case AF_IB: |
| 205 | return sizeof(struct sockaddr_ib); |
| 206 | default: |
| 207 | return 0; |
| 208 | } |
| 209 | } |
| 210 | EXPORT_SYMBOL(rdma_addr_size); |
| 211 | |
Matan Barak | dd5f03b | 2013-12-12 18:03:11 +0200 | [diff] [blame] | 212 | static struct rdma_addr_client self; |
| 213 | |
Sean Hefty | 7a118df | 2006-10-31 11:12:59 -0800 | [diff] [blame] | 214 | void rdma_addr_register_client(struct rdma_addr_client *client) |
| 215 | { |
| 216 | atomic_set(&client->refcount, 1); |
| 217 | init_completion(&client->comp); |
| 218 | } |
| 219 | EXPORT_SYMBOL(rdma_addr_register_client); |
| 220 | |
| 221 | static inline void put_client(struct rdma_addr_client *client) |
| 222 | { |
| 223 | if (atomic_dec_and_test(&client->refcount)) |
| 224 | complete(&client->comp); |
| 225 | } |
| 226 | |
| 227 | void rdma_addr_unregister_client(struct rdma_addr_client *client) |
| 228 | { |
| 229 | put_client(client); |
| 230 | wait_for_completion(&client->comp); |
| 231 | } |
| 232 | EXPORT_SYMBOL(rdma_addr_unregister_client); |
| 233 | |
Tom Tucker | 07ebafb | 2006-08-03 16:02:42 -0500 | [diff] [blame] | 234 | int rdma_copy_addr(struct rdma_dev_addr *dev_addr, struct net_device *dev, |
| 235 | const unsigned char *dst_dev_addr) |
Sean Hefty | 7025fcd | 2006-06-17 20:37:28 -0700 | [diff] [blame] | 236 | { |
Sean Hefty | c4315d8 | 2009-11-19 12:57:18 -0800 | [diff] [blame] | 237 | dev_addr->dev_type = dev->type; |
Sean Hefty | 7025fcd | 2006-06-17 20:37:28 -0700 | [diff] [blame] | 238 | memcpy(dev_addr->src_dev_addr, dev->dev_addr, MAX_ADDR_LEN); |
| 239 | memcpy(dev_addr->broadcast, dev->broadcast, MAX_ADDR_LEN); |
| 240 | if (dst_dev_addr) |
| 241 | memcpy(dev_addr->dst_dev_addr, dst_dev_addr, MAX_ADDR_LEN); |
Sean Hefty | 6266ed6 | 2009-11-19 12:55:22 -0800 | [diff] [blame] | 242 | dev_addr->bound_dev_if = dev->ifindex; |
Sean Hefty | 7025fcd | 2006-06-17 20:37:28 -0700 | [diff] [blame] | 243 | return 0; |
| 244 | } |
Tom Tucker | 07ebafb | 2006-08-03 16:02:42 -0500 | [diff] [blame] | 245 | EXPORT_SYMBOL(rdma_copy_addr); |
Sean Hefty | 7025fcd | 2006-06-17 20:37:28 -0700 | [diff] [blame] | 246 | |
Matan Barak | 2002983 | 2015-12-23 14:56:53 +0200 | [diff] [blame] | 247 | int rdma_translate_ip(const struct sockaddr *addr, |
| 248 | struct rdma_dev_addr *dev_addr, |
Matan Barak | dd5f03b | 2013-12-12 18:03:11 +0200 | [diff] [blame] | 249 | u16 *vlan_id) |
Sean Hefty | 7025fcd | 2006-06-17 20:37:28 -0700 | [diff] [blame] | 250 | { |
| 251 | struct net_device *dev; |
Aleksey Senin | 38617c6 | 2008-12-24 10:16:37 -0800 | [diff] [blame] | 252 | int ret = -EADDRNOTAVAIL; |
Sean Hefty | 7025fcd | 2006-06-17 20:37:28 -0700 | [diff] [blame] | 253 | |
Sean Hefty | 6266ed6 | 2009-11-19 12:55:22 -0800 | [diff] [blame] | 254 | if (dev_addr->bound_dev_if) { |
Guy Shapiro | 565edd1 | 2015-10-22 15:20:08 +0300 | [diff] [blame] | 255 | dev = dev_get_by_index(dev_addr->net, dev_addr->bound_dev_if); |
Sean Hefty | 6266ed6 | 2009-11-19 12:55:22 -0800 | [diff] [blame] | 256 | if (!dev) |
| 257 | return -ENODEV; |
| 258 | ret = rdma_copy_addr(dev_addr, dev, NULL); |
| 259 | dev_put(dev); |
| 260 | return ret; |
| 261 | } |
| 262 | |
Aleksey Senin | 38617c6 | 2008-12-24 10:16:37 -0800 | [diff] [blame] | 263 | switch (addr->sa_family) { |
| 264 | case AF_INET: |
Guy Shapiro | 565edd1 | 2015-10-22 15:20:08 +0300 | [diff] [blame] | 265 | dev = ip_dev_find(dev_addr->net, |
Matan Barak | 2002983 | 2015-12-23 14:56:53 +0200 | [diff] [blame] | 266 | ((const struct sockaddr_in *)addr)->sin_addr.s_addr); |
Sean Hefty | 7025fcd | 2006-06-17 20:37:28 -0700 | [diff] [blame] | 267 | |
Aleksey Senin | 38617c6 | 2008-12-24 10:16:37 -0800 | [diff] [blame] | 268 | if (!dev) |
| 269 | return ret; |
| 270 | |
| 271 | ret = rdma_copy_addr(dev_addr, dev, NULL); |
Matan Barak | dd5f03b | 2013-12-12 18:03:11 +0200 | [diff] [blame] | 272 | if (vlan_id) |
| 273 | *vlan_id = rdma_vlan_dev_vlan_id(dev); |
Aleksey Senin | 38617c6 | 2008-12-24 10:16:37 -0800 | [diff] [blame] | 274 | dev_put(dev); |
| 275 | break; |
Roland Dreier | d90f9b3 | 2012-07-05 22:39:34 -0700 | [diff] [blame] | 276 | #if IS_ENABLED(CONFIG_IPV6) |
Aleksey Senin | 38617c6 | 2008-12-24 10:16:37 -0800 | [diff] [blame] | 277 | case AF_INET6: |
Eric Dumazet | 22f4fbd | 2010-11-24 11:41:56 -0800 | [diff] [blame] | 278 | rcu_read_lock(); |
Guy Shapiro | 565edd1 | 2015-10-22 15:20:08 +0300 | [diff] [blame] | 279 | for_each_netdev_rcu(dev_addr->net, dev) { |
| 280 | if (ipv6_chk_addr(dev_addr->net, |
Matan Barak | 2002983 | 2015-12-23 14:56:53 +0200 | [diff] [blame] | 281 | &((const struct sockaddr_in6 *)addr)->sin6_addr, |
Aleksey Senin | 38617c6 | 2008-12-24 10:16:37 -0800 | [diff] [blame] | 282 | dev, 1)) { |
| 283 | ret = rdma_copy_addr(dev_addr, dev, NULL); |
Matan Barak | dd5f03b | 2013-12-12 18:03:11 +0200 | [diff] [blame] | 284 | if (vlan_id) |
| 285 | *vlan_id = rdma_vlan_dev_vlan_id(dev); |
Aleksey Senin | 38617c6 | 2008-12-24 10:16:37 -0800 | [diff] [blame] | 286 | break; |
| 287 | } |
| 288 | } |
Eric Dumazet | 22f4fbd | 2010-11-24 11:41:56 -0800 | [diff] [blame] | 289 | rcu_read_unlock(); |
Aleksey Senin | 38617c6 | 2008-12-24 10:16:37 -0800 | [diff] [blame] | 290 | break; |
Roland Dreier | 2c4ab62 | 2008-12-29 23:37:14 -0800 | [diff] [blame] | 291 | #endif |
Aleksey Senin | 38617c6 | 2008-12-24 10:16:37 -0800 | [diff] [blame] | 292 | } |
Sean Hefty | 7025fcd | 2006-06-17 20:37:28 -0700 | [diff] [blame] | 293 | return ret; |
| 294 | } |
| 295 | EXPORT_SYMBOL(rdma_translate_ip); |
| 296 | |
| 297 | static void set_timeout(unsigned long time) |
| 298 | { |
| 299 | unsigned long delay; |
| 300 | |
Sean Hefty | 7025fcd | 2006-06-17 20:37:28 -0700 | [diff] [blame] | 301 | delay = time - jiffies; |
Or Kehati | 346f98b | 2014-10-29 16:32:04 +0200 | [diff] [blame] | 302 | if ((long)delay < 0) |
| 303 | delay = 0; |
Sean Hefty | 7025fcd | 2006-06-17 20:37:28 -0700 | [diff] [blame] | 304 | |
Tejun Heo | 41f63c5 | 2012-08-03 10:30:47 -0700 | [diff] [blame] | 305 | mod_delayed_work(addr_wq, &work, delay); |
Sean Hefty | 7025fcd | 2006-06-17 20:37:28 -0700 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | static void queue_req(struct addr_req *req) |
| 309 | { |
| 310 | struct addr_req *temp_req; |
| 311 | |
| 312 | mutex_lock(&lock); |
| 313 | list_for_each_entry_reverse(temp_req, &req_list, list) { |
Krishna Kumar | f115db4 | 2006-10-17 10:09:09 +0530 | [diff] [blame] | 314 | if (time_after_eq(req->timeout, temp_req->timeout)) |
Sean Hefty | 7025fcd | 2006-06-17 20:37:28 -0700 | [diff] [blame] | 315 | break; |
| 316 | } |
| 317 | |
| 318 | list_add(&req->list, &temp_req->list); |
| 319 | |
| 320 | if (req_list.next == &req->list) |
| 321 | set_timeout(req->timeout); |
| 322 | mutex_unlock(&lock); |
| 323 | } |
| 324 | |
Mark Bloch | ae43f82 | 2016-05-19 17:12:36 +0300 | [diff] [blame] | 325 | static int ib_nl_fetch_ha(struct dst_entry *dst, struct rdma_dev_addr *dev_addr, |
| 326 | const void *daddr, u32 seq, u16 family) |
| 327 | { |
| 328 | if (ibnl_chk_listeners(RDMA_NL_GROUP_LS)) |
| 329 | return -EADDRNOTAVAIL; |
| 330 | |
| 331 | /* We fill in what we can, the response will fill the rest */ |
| 332 | rdma_copy_addr(dev_addr, dst->dev, NULL); |
| 333 | return ib_nl_ip_send_msg(dev_addr, daddr, seq, family); |
| 334 | } |
| 335 | |
Matan Barak | 2002983 | 2015-12-23 14:56:53 +0200 | [diff] [blame] | 336 | static int dst_fetch_ha(struct dst_entry *dst, struct rdma_dev_addr *dev_addr, |
| 337 | const void *daddr) |
David Miller | 51d4597 | 2011-12-02 16:52:14 +0000 | [diff] [blame] | 338 | { |
| 339 | struct neighbour *n; |
| 340 | int ret; |
| 341 | |
David Miller | 02b6195 | 2012-01-24 13:15:52 +0000 | [diff] [blame] | 342 | n = dst_neigh_lookup(dst, daddr); |
| 343 | |
David Miller | 51d4597 | 2011-12-02 16:52:14 +0000 | [diff] [blame] | 344 | rcu_read_lock(); |
David Miller | 51d4597 | 2011-12-02 16:52:14 +0000 | [diff] [blame] | 345 | if (!n || !(n->nud_state & NUD_VALID)) { |
| 346 | if (n) |
| 347 | neigh_event_send(n, NULL); |
| 348 | ret = -ENODATA; |
| 349 | } else { |
David Miller | 02b6195 | 2012-01-24 13:15:52 +0000 | [diff] [blame] | 350 | ret = rdma_copy_addr(dev_addr, dst->dev, n->ha); |
David Miller | 51d4597 | 2011-12-02 16:52:14 +0000 | [diff] [blame] | 351 | } |
| 352 | rcu_read_unlock(); |
| 353 | |
David Miller | 02b6195 | 2012-01-24 13:15:52 +0000 | [diff] [blame] | 354 | if (n) |
| 355 | neigh_release(n); |
| 356 | |
David Miller | 51d4597 | 2011-12-02 16:52:14 +0000 | [diff] [blame] | 357 | return ret; |
| 358 | } |
| 359 | |
Mark Bloch | ae43f82 | 2016-05-19 17:12:36 +0300 | [diff] [blame] | 360 | static bool has_gateway(struct dst_entry *dst, sa_family_t family) |
| 361 | { |
| 362 | struct rtable *rt; |
| 363 | struct rt6_info *rt6; |
| 364 | |
| 365 | if (family == AF_INET) { |
| 366 | rt = container_of(dst, struct rtable, dst); |
| 367 | return rt->rt_uses_gateway; |
| 368 | } |
| 369 | |
| 370 | rt6 = container_of(dst, struct rt6_info, dst); |
| 371 | return rt6->rt6i_flags & RTF_GATEWAY; |
| 372 | } |
| 373 | |
| 374 | static int fetch_ha(struct dst_entry *dst, struct rdma_dev_addr *dev_addr, |
| 375 | const struct sockaddr *dst_in, u32 seq) |
| 376 | { |
| 377 | const struct sockaddr_in *dst_in4 = |
| 378 | (const struct sockaddr_in *)dst_in; |
| 379 | const struct sockaddr_in6 *dst_in6 = |
| 380 | (const struct sockaddr_in6 *)dst_in; |
| 381 | const void *daddr = (dst_in->sa_family == AF_INET) ? |
| 382 | (const void *)&dst_in4->sin_addr.s_addr : |
| 383 | (const void *)&dst_in6->sin6_addr; |
| 384 | sa_family_t family = dst_in->sa_family; |
| 385 | |
| 386 | /* Gateway + ARPHRD_INFINIBAND -> IB router */ |
| 387 | if (has_gateway(dst, family) && dst->dev->type == ARPHRD_INFINIBAND) |
| 388 | return ib_nl_fetch_ha(dst, dev_addr, daddr, seq, family); |
| 389 | else |
| 390 | return dst_fetch_ha(dst, dev_addr, daddr); |
| 391 | } |
| 392 | |
Sean Hefty | 923c100 | 2009-11-19 13:26:51 -0800 | [diff] [blame] | 393 | static int addr4_resolve(struct sockaddr_in *src_in, |
Matan Barak | 2002983 | 2015-12-23 14:56:53 +0200 | [diff] [blame] | 394 | const struct sockaddr_in *dst_in, |
| 395 | struct rdma_dev_addr *addr, |
| 396 | struct rtable **prt) |
Sean Hefty | 7025fcd | 2006-06-17 20:37:28 -0700 | [diff] [blame] | 397 | { |
Al Viro | 1b90c13 | 2008-03-29 03:10:28 +0000 | [diff] [blame] | 398 | __be32 src_ip = src_in->sin_addr.s_addr; |
| 399 | __be32 dst_ip = dst_in->sin_addr.s_addr; |
Sean Hefty | 7025fcd | 2006-06-17 20:37:28 -0700 | [diff] [blame] | 400 | struct rtable *rt; |
David S. Miller | 5fc3590 | 2011-05-09 14:52:02 -0700 | [diff] [blame] | 401 | struct flowi4 fl4; |
Sean Hefty | 7025fcd | 2006-06-17 20:37:28 -0700 | [diff] [blame] | 402 | int ret; |
| 403 | |
David S. Miller | 5fc3590 | 2011-05-09 14:52:02 -0700 | [diff] [blame] | 404 | memset(&fl4, 0, sizeof(fl4)); |
| 405 | fl4.daddr = dst_ip; |
| 406 | fl4.saddr = src_ip; |
| 407 | fl4.flowi4_oif = addr->bound_dev_if; |
Guy Shapiro | 565edd1 | 2015-10-22 15:20:08 +0300 | [diff] [blame] | 408 | rt = ip_route_output_key(addr->net, &fl4); |
David S. Miller | b23dd4f | 2011-03-02 14:31:35 -0800 | [diff] [blame] | 409 | if (IS_ERR(rt)) { |
| 410 | ret = PTR_ERR(rt); |
Sean Hefty | 7025fcd | 2006-06-17 20:37:28 -0700 | [diff] [blame] | 411 | goto out; |
David S. Miller | b23dd4f | 2011-03-02 14:31:35 -0800 | [diff] [blame] | 412 | } |
Sean Hefty | 923c100 | 2009-11-19 13:26:51 -0800 | [diff] [blame] | 413 | src_in->sin_family = AF_INET; |
David S. Miller | 5fc3590 | 2011-05-09 14:52:02 -0700 | [diff] [blame] | 414 | src_in->sin_addr.s_addr = fl4.saddr; |
Sean Hefty | 923c100 | 2009-11-19 13:26:51 -0800 | [diff] [blame] | 415 | |
Mark Bloch | ae43f82 | 2016-05-19 17:12:36 +0300 | [diff] [blame] | 416 | /* If there's a gateway and type of device not ARPHRD_INFINIBAND, we're |
| 417 | * definitely in RoCE v2 (as RoCE v1 isn't routable) set the network |
| 418 | * type accordingly. |
Somnath Kotur | c865f24 | 2015-12-23 14:56:51 +0200 | [diff] [blame] | 419 | */ |
Mark Bloch | ae43f82 | 2016-05-19 17:12:36 +0300 | [diff] [blame] | 420 | if (rt->rt_uses_gateway && rt->dst.dev->type != ARPHRD_INFINIBAND) |
Somnath Kotur | c865f24 | 2015-12-23 14:56:51 +0200 | [diff] [blame] | 421 | addr->network = RDMA_NETWORK_IPV4; |
| 422 | |
Matan Barak | c3efe75 | 2016-01-04 10:49:54 +0200 | [diff] [blame] | 423 | addr->hoplimit = ip4_dst_hoplimit(&rt->dst); |
| 424 | |
Matan Barak | 2002983 | 2015-12-23 14:56:53 +0200 | [diff] [blame] | 425 | *prt = rt; |
| 426 | return 0; |
Sean Hefty | 7025fcd | 2006-06-17 20:37:28 -0700 | [diff] [blame] | 427 | out: |
| 428 | return ret; |
| 429 | } |
| 430 | |
Roland Dreier | d90f9b3 | 2012-07-05 22:39:34 -0700 | [diff] [blame] | 431 | #if IS_ENABLED(CONFIG_IPV6) |
Sean Hefty | d14714d | 2009-11-19 16:46:25 -0800 | [diff] [blame] | 432 | static int addr6_resolve(struct sockaddr_in6 *src_in, |
Matan Barak | 2002983 | 2015-12-23 14:56:53 +0200 | [diff] [blame] | 433 | const struct sockaddr_in6 *dst_in, |
| 434 | struct rdma_dev_addr *addr, |
| 435 | struct dst_entry **pdst) |
Aleksey Senin | 38617c6 | 2008-12-24 10:16:37 -0800 | [diff] [blame] | 436 | { |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 437 | struct flowi6 fl6; |
Aleksey Senin | 38617c6 | 2008-12-24 10:16:37 -0800 | [diff] [blame] | 438 | struct dst_entry *dst; |
Somnath Kotur | c865f24 | 2015-12-23 14:56:51 +0200 | [diff] [blame] | 439 | struct rt6_info *rt; |
Sean Hefty | d14714d | 2009-11-19 16:46:25 -0800 | [diff] [blame] | 440 | int ret; |
Aleksey Senin | 38617c6 | 2008-12-24 10:16:37 -0800 | [diff] [blame] | 441 | |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 442 | memset(&fl6, 0, sizeof fl6); |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 443 | fl6.daddr = dst_in->sin6_addr; |
| 444 | fl6.saddr = src_in->sin6_addr; |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 445 | fl6.flowi6_oif = addr->bound_dev_if; |
Aleksey Senin | 38617c6 | 2008-12-24 10:16:37 -0800 | [diff] [blame] | 446 | |
Paolo Abeni | eea40b8 | 2017-04-28 11:20:01 +0200 | [diff] [blame] | 447 | ret = ipv6_stub->ipv6_dst_lookup(addr->net, NULL, &dst, &fl6); |
| 448 | if (ret < 0) |
Paolo Abeni | 24b43c9 | 2017-05-02 16:03:58 +0200 | [diff] [blame] | 449 | return ret; |
Aleksey Senin | 38617c6 | 2008-12-24 10:16:37 -0800 | [diff] [blame] | 450 | |
Somnath Kotur | c865f24 | 2015-12-23 14:56:51 +0200 | [diff] [blame] | 451 | rt = (struct rt6_info *)dst; |
Roland Dreier | 79e2595 | 2017-06-06 09:22:00 -0700 | [diff] [blame] | 452 | if (ipv6_addr_any(&src_in->sin6_addr)) { |
Sean Hefty | d14714d | 2009-11-19 16:46:25 -0800 | [diff] [blame] | 453 | src_in->sin6_family = AF_INET6; |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 454 | src_in->sin6_addr = fl6.saddr; |
Aleksey Senin | 38617c6 | 2008-12-24 10:16:37 -0800 | [diff] [blame] | 455 | } |
| 456 | |
Mark Bloch | ae43f82 | 2016-05-19 17:12:36 +0300 | [diff] [blame] | 457 | /* If there's a gateway and type of device not ARPHRD_INFINIBAND, we're |
| 458 | * definitely in RoCE v2 (as RoCE v1 isn't routable) set the network |
| 459 | * type accordingly. |
Somnath Kotur | c865f24 | 2015-12-23 14:56:51 +0200 | [diff] [blame] | 460 | */ |
Mark Bloch | ae43f82 | 2016-05-19 17:12:36 +0300 | [diff] [blame] | 461 | if (rt->rt6i_flags & RTF_GATEWAY && |
| 462 | ip6_dst_idev(dst)->dev->type != ARPHRD_INFINIBAND) |
Somnath Kotur | c865f24 | 2015-12-23 14:56:51 +0200 | [diff] [blame] | 463 | addr->network = RDMA_NETWORK_IPV6; |
| 464 | |
Matan Barak | c3efe75 | 2016-01-04 10:49:54 +0200 | [diff] [blame] | 465 | addr->hoplimit = ip6_dst_hoplimit(dst); |
| 466 | |
Matan Barak | 2002983 | 2015-12-23 14:56:53 +0200 | [diff] [blame] | 467 | *pdst = dst; |
| 468 | return 0; |
Aleksey Senin | 38617c6 | 2008-12-24 10:16:37 -0800 | [diff] [blame] | 469 | } |
Roland Dreier | 2c4ab62 | 2008-12-29 23:37:14 -0800 | [diff] [blame] | 470 | #else |
Sean Hefty | d14714d | 2009-11-19 16:46:25 -0800 | [diff] [blame] | 471 | static int addr6_resolve(struct sockaddr_in6 *src_in, |
Matan Barak | 2002983 | 2015-12-23 14:56:53 +0200 | [diff] [blame] | 472 | const struct sockaddr_in6 *dst_in, |
| 473 | struct rdma_dev_addr *addr, |
| 474 | struct dst_entry **pdst) |
Roland Dreier | 2c4ab62 | 2008-12-29 23:37:14 -0800 | [diff] [blame] | 475 | { |
| 476 | return -EADDRNOTAVAIL; |
| 477 | } |
| 478 | #endif |
Aleksey Senin | 38617c6 | 2008-12-24 10:16:37 -0800 | [diff] [blame] | 479 | |
Matan Barak | 2002983 | 2015-12-23 14:56:53 +0200 | [diff] [blame] | 480 | static int addr_resolve_neigh(struct dst_entry *dst, |
| 481 | const struct sockaddr *dst_in, |
Mark Bloch | ae43f82 | 2016-05-19 17:12:36 +0300 | [diff] [blame] | 482 | struct rdma_dev_addr *addr, |
| 483 | u32 seq) |
Aleksey Senin | 38617c6 | 2008-12-24 10:16:37 -0800 | [diff] [blame] | 484 | { |
Matan Barak | 2002983 | 2015-12-23 14:56:53 +0200 | [diff] [blame] | 485 | if (dst->dev->flags & IFF_LOOPBACK) { |
| 486 | int ret; |
| 487 | |
| 488 | ret = rdma_translate_ip(dst_in, addr, NULL); |
| 489 | if (!ret) |
| 490 | memcpy(addr->dst_dev_addr, addr->src_dev_addr, |
| 491 | MAX_ADDR_LEN); |
| 492 | |
| 493 | return ret; |
| 494 | } |
| 495 | |
| 496 | /* If the device doesn't do ARP internally */ |
Mark Bloch | ae43f82 | 2016-05-19 17:12:36 +0300 | [diff] [blame] | 497 | if (!(dst->dev->flags & IFF_NOARP)) |
| 498 | return fetch_ha(dst, addr, dst_in, seq); |
Matan Barak | 2002983 | 2015-12-23 14:56:53 +0200 | [diff] [blame] | 499 | |
| 500 | return rdma_copy_addr(addr, dst->dev, NULL); |
| 501 | } |
| 502 | |
| 503 | static int addr_resolve(struct sockaddr *src_in, |
| 504 | const struct sockaddr *dst_in, |
| 505 | struct rdma_dev_addr *addr, |
Mark Bloch | ae43f82 | 2016-05-19 17:12:36 +0300 | [diff] [blame] | 506 | bool resolve_neigh, |
| 507 | u32 seq) |
Matan Barak | 2002983 | 2015-12-23 14:56:53 +0200 | [diff] [blame] | 508 | { |
| 509 | struct net_device *ndev; |
| 510 | struct dst_entry *dst; |
| 511 | int ret; |
| 512 | |
Aleksey Senin | 38617c6 | 2008-12-24 10:16:37 -0800 | [diff] [blame] | 513 | if (src_in->sa_family == AF_INET) { |
Matan Barak | 2002983 | 2015-12-23 14:56:53 +0200 | [diff] [blame] | 514 | struct rtable *rt = NULL; |
| 515 | const struct sockaddr_in *dst_in4 = |
| 516 | (const struct sockaddr_in *)dst_in; |
| 517 | |
| 518 | ret = addr4_resolve((struct sockaddr_in *)src_in, |
| 519 | dst_in4, addr, &rt); |
| 520 | if (ret) |
| 521 | return ret; |
| 522 | |
| 523 | if (resolve_neigh) |
Mark Bloch | ae43f82 | 2016-05-19 17:12:36 +0300 | [diff] [blame] | 524 | ret = addr_resolve_neigh(&rt->dst, dst_in, addr, seq); |
Matan Barak | 2002983 | 2015-12-23 14:56:53 +0200 | [diff] [blame] | 525 | |
| 526 | ndev = rt->dst.dev; |
| 527 | dev_hold(ndev); |
| 528 | |
| 529 | ip_rt_put(rt); |
| 530 | } else { |
| 531 | const struct sockaddr_in6 *dst_in6 = |
| 532 | (const struct sockaddr_in6 *)dst_in; |
| 533 | |
| 534 | ret = addr6_resolve((struct sockaddr_in6 *)src_in, |
| 535 | dst_in6, addr, |
| 536 | &dst); |
| 537 | if (ret) |
| 538 | return ret; |
| 539 | |
| 540 | if (resolve_neigh) |
Mark Bloch | ae43f82 | 2016-05-19 17:12:36 +0300 | [diff] [blame] | 541 | ret = addr_resolve_neigh(dst, dst_in, addr, seq); |
Matan Barak | 2002983 | 2015-12-23 14:56:53 +0200 | [diff] [blame] | 542 | |
| 543 | ndev = dst->dev; |
| 544 | dev_hold(ndev); |
| 545 | |
| 546 | dst_release(dst); |
| 547 | } |
| 548 | |
| 549 | addr->bound_dev_if = ndev->ifindex; |
| 550 | addr->net = dev_net(ndev); |
| 551 | dev_put(ndev); |
| 552 | |
| 553 | return ret; |
Aleksey Senin | 38617c6 | 2008-12-24 10:16:37 -0800 | [diff] [blame] | 554 | } |
| 555 | |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 556 | static void process_req(struct work_struct *work) |
Sean Hefty | 7025fcd | 2006-06-17 20:37:28 -0700 | [diff] [blame] | 557 | { |
| 558 | struct addr_req *req, *temp_req; |
Aleksey Senin | 38617c6 | 2008-12-24 10:16:37 -0800 | [diff] [blame] | 559 | struct sockaddr *src_in, *dst_in; |
Sean Hefty | 7025fcd | 2006-06-17 20:37:28 -0700 | [diff] [blame] | 560 | struct list_head done_list; |
| 561 | |
| 562 | INIT_LIST_HEAD(&done_list); |
| 563 | |
| 564 | mutex_lock(&lock); |
| 565 | list_for_each_entry_safe(req, temp_req, &req_list, list) { |
Krishna Kumar | c78bb84 | 2006-11-24 16:02:34 +0530 | [diff] [blame] | 566 | if (req->status == -ENODATA) { |
Aleksey Senin | 38617c6 | 2008-12-24 10:16:37 -0800 | [diff] [blame] | 567 | src_in = (struct sockaddr *) &req->src_addr; |
| 568 | dst_in = (struct sockaddr *) &req->dst_addr; |
Matan Barak | 2002983 | 2015-12-23 14:56:53 +0200 | [diff] [blame] | 569 | req->status = addr_resolve(src_in, dst_in, req->addr, |
Mark Bloch | ae43f82 | 2016-05-19 17:12:36 +0300 | [diff] [blame] | 570 | true, req->seq); |
Krishna Kumar | c78bb84 | 2006-11-24 16:02:34 +0530 | [diff] [blame] | 571 | if (req->status && time_after_eq(jiffies, req->timeout)) |
| 572 | req->status = -ETIMEDOUT; |
| 573 | else if (req->status == -ENODATA) |
| 574 | continue; |
Sean Hefty | 7025fcd | 2006-06-17 20:37:28 -0700 | [diff] [blame] | 575 | } |
Roland Dreier | 04699a1 | 2006-11-29 15:33:09 -0800 | [diff] [blame] | 576 | list_move_tail(&req->list, &done_list); |
Sean Hefty | 7025fcd | 2006-06-17 20:37:28 -0700 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | if (!list_empty(&req_list)) { |
| 580 | req = list_entry(req_list.next, struct addr_req, list); |
| 581 | set_timeout(req->timeout); |
| 582 | } |
| 583 | mutex_unlock(&lock); |
| 584 | |
| 585 | list_for_each_entry_safe(req, temp_req, &done_list, list) { |
| 586 | list_del(&req->list); |
Aleksey Senin | 38617c6 | 2008-12-24 10:16:37 -0800 | [diff] [blame] | 587 | req->callback(req->status, (struct sockaddr *) &req->src_addr, |
| 588 | req->addr, req->context); |
Sean Hefty | 7a118df | 2006-10-31 11:12:59 -0800 | [diff] [blame] | 589 | put_client(req->client); |
Sean Hefty | 7025fcd | 2006-06-17 20:37:28 -0700 | [diff] [blame] | 590 | kfree(req); |
| 591 | } |
| 592 | } |
| 593 | |
Sean Hefty | 7a118df | 2006-10-31 11:12:59 -0800 | [diff] [blame] | 594 | int rdma_resolve_ip(struct rdma_addr_client *client, |
| 595 | struct sockaddr *src_addr, struct sockaddr *dst_addr, |
Sean Hefty | 7025fcd | 2006-06-17 20:37:28 -0700 | [diff] [blame] | 596 | struct rdma_dev_addr *addr, int timeout_ms, |
| 597 | void (*callback)(int status, struct sockaddr *src_addr, |
| 598 | struct rdma_dev_addr *addr, void *context), |
| 599 | void *context) |
| 600 | { |
Aleksey Senin | 38617c6 | 2008-12-24 10:16:37 -0800 | [diff] [blame] | 601 | struct sockaddr *src_in, *dst_in; |
Sean Hefty | 7025fcd | 2006-06-17 20:37:28 -0700 | [diff] [blame] | 602 | struct addr_req *req; |
| 603 | int ret = 0; |
| 604 | |
Yoann Padioleau | dd00cc4 | 2007-07-19 01:49:03 -0700 | [diff] [blame] | 605 | req = kzalloc(sizeof *req, GFP_KERNEL); |
Sean Hefty | 7025fcd | 2006-06-17 20:37:28 -0700 | [diff] [blame] | 606 | if (!req) |
| 607 | return -ENOMEM; |
Sean Hefty | 7025fcd | 2006-06-17 20:37:28 -0700 | [diff] [blame] | 608 | |
Sean Hefty | d2e0886 | 2009-11-19 12:55:22 -0800 | [diff] [blame] | 609 | src_in = (struct sockaddr *) &req->src_addr; |
| 610 | dst_in = (struct sockaddr *) &req->dst_addr; |
| 611 | |
| 612 | if (src_addr) { |
| 613 | if (src_addr->sa_family != dst_addr->sa_family) { |
| 614 | ret = -EINVAL; |
| 615 | goto err; |
| 616 | } |
| 617 | |
Sean Hefty | ef56086 | 2013-05-29 10:09:10 -0700 | [diff] [blame] | 618 | memcpy(src_in, src_addr, rdma_addr_size(src_addr)); |
Sean Hefty | d2e0886 | 2009-11-19 12:55:22 -0800 | [diff] [blame] | 619 | } else { |
| 620 | src_in->sa_family = dst_addr->sa_family; |
| 621 | } |
| 622 | |
Sean Hefty | ef56086 | 2013-05-29 10:09:10 -0700 | [diff] [blame] | 623 | memcpy(dst_in, dst_addr, rdma_addr_size(dst_addr)); |
Sean Hefty | 7025fcd | 2006-06-17 20:37:28 -0700 | [diff] [blame] | 624 | req->addr = addr; |
| 625 | req->callback = callback; |
| 626 | req->context = context; |
Sean Hefty | 7a118df | 2006-10-31 11:12:59 -0800 | [diff] [blame] | 627 | req->client = client; |
| 628 | atomic_inc(&client->refcount); |
Mark Bloch | ae43f82 | 2016-05-19 17:12:36 +0300 | [diff] [blame] | 629 | req->seq = (u32)atomic_inc_return(&ib_nl_addr_request_seq); |
Sean Hefty | 7025fcd | 2006-06-17 20:37:28 -0700 | [diff] [blame] | 630 | |
Mark Bloch | ae43f82 | 2016-05-19 17:12:36 +0300 | [diff] [blame] | 631 | req->status = addr_resolve(src_in, dst_in, addr, true, req->seq); |
Sean Hefty | 7025fcd | 2006-06-17 20:37:28 -0700 | [diff] [blame] | 632 | switch (req->status) { |
| 633 | case 0: |
| 634 | req->timeout = jiffies; |
| 635 | queue_req(req); |
| 636 | break; |
| 637 | case -ENODATA: |
| 638 | req->timeout = msecs_to_jiffies(timeout_ms) + jiffies; |
| 639 | queue_req(req); |
Sean Hefty | 7025fcd | 2006-06-17 20:37:28 -0700 | [diff] [blame] | 640 | break; |
| 641 | default: |
| 642 | ret = req->status; |
Sean Hefty | 7a118df | 2006-10-31 11:12:59 -0800 | [diff] [blame] | 643 | atomic_dec(&client->refcount); |
Sean Hefty | d2e0886 | 2009-11-19 12:55:22 -0800 | [diff] [blame] | 644 | goto err; |
Sean Hefty | 7025fcd | 2006-06-17 20:37:28 -0700 | [diff] [blame] | 645 | } |
| 646 | return ret; |
Sean Hefty | d2e0886 | 2009-11-19 12:55:22 -0800 | [diff] [blame] | 647 | err: |
| 648 | kfree(req); |
| 649 | return ret; |
Sean Hefty | 7025fcd | 2006-06-17 20:37:28 -0700 | [diff] [blame] | 650 | } |
| 651 | EXPORT_SYMBOL(rdma_resolve_ip); |
| 652 | |
Matan Barak | 2002983 | 2015-12-23 14:56:53 +0200 | [diff] [blame] | 653 | int rdma_resolve_ip_route(struct sockaddr *src_addr, |
| 654 | const struct sockaddr *dst_addr, |
| 655 | struct rdma_dev_addr *addr) |
| 656 | { |
| 657 | struct sockaddr_storage ssrc_addr = {}; |
| 658 | struct sockaddr *src_in = (struct sockaddr *)&ssrc_addr; |
| 659 | |
Matan Barak | 9506902 | 2015-12-30 16:14:18 +0200 | [diff] [blame] | 660 | if (src_addr) { |
| 661 | if (src_addr->sa_family != dst_addr->sa_family) |
| 662 | return -EINVAL; |
Matan Barak | 2002983 | 2015-12-23 14:56:53 +0200 | [diff] [blame] | 663 | |
Matan Barak | 2002983 | 2015-12-23 14:56:53 +0200 | [diff] [blame] | 664 | memcpy(src_in, src_addr, rdma_addr_size(src_addr)); |
Matan Barak | 9506902 | 2015-12-30 16:14:18 +0200 | [diff] [blame] | 665 | } else { |
Matan Barak | 2002983 | 2015-12-23 14:56:53 +0200 | [diff] [blame] | 666 | src_in->sa_family = dst_addr->sa_family; |
Matan Barak | 9506902 | 2015-12-30 16:14:18 +0200 | [diff] [blame] | 667 | } |
Matan Barak | 2002983 | 2015-12-23 14:56:53 +0200 | [diff] [blame] | 668 | |
Mark Bloch | ae43f82 | 2016-05-19 17:12:36 +0300 | [diff] [blame] | 669 | return addr_resolve(src_in, dst_addr, addr, false, 0); |
Matan Barak | 2002983 | 2015-12-23 14:56:53 +0200 | [diff] [blame] | 670 | } |
| 671 | EXPORT_SYMBOL(rdma_resolve_ip_route); |
| 672 | |
Sean Hefty | 7025fcd | 2006-06-17 20:37:28 -0700 | [diff] [blame] | 673 | void rdma_addr_cancel(struct rdma_dev_addr *addr) |
| 674 | { |
| 675 | struct addr_req *req, *temp_req; |
| 676 | |
| 677 | mutex_lock(&lock); |
| 678 | list_for_each_entry_safe(req, temp_req, &req_list, list) { |
| 679 | if (req->addr == addr) { |
| 680 | req->status = -ECANCELED; |
| 681 | req->timeout = jiffies; |
Roland Dreier | 04699a1 | 2006-11-29 15:33:09 -0800 | [diff] [blame] | 682 | list_move(&req->list, &req_list); |
Sean Hefty | 7025fcd | 2006-06-17 20:37:28 -0700 | [diff] [blame] | 683 | set_timeout(req->timeout); |
| 684 | break; |
| 685 | } |
| 686 | } |
| 687 | mutex_unlock(&lock); |
| 688 | } |
| 689 | EXPORT_SYMBOL(rdma_addr_cancel); |
| 690 | |
Matan Barak | dd5f03b | 2013-12-12 18:03:11 +0200 | [diff] [blame] | 691 | struct resolve_cb_context { |
| 692 | struct rdma_dev_addr *addr; |
| 693 | struct completion comp; |
Mark Bloch | 61c37028 | 2016-10-27 16:36:29 +0300 | [diff] [blame] | 694 | int status; |
Matan Barak | dd5f03b | 2013-12-12 18:03:11 +0200 | [diff] [blame] | 695 | }; |
| 696 | |
| 697 | static void resolve_cb(int status, struct sockaddr *src_addr, |
| 698 | struct rdma_dev_addr *addr, void *context) |
| 699 | { |
Mark Bloch | 61c37028 | 2016-10-27 16:36:29 +0300 | [diff] [blame] | 700 | if (!status) |
| 701 | memcpy(((struct resolve_cb_context *)context)->addr, |
| 702 | addr, sizeof(struct rdma_dev_addr)); |
| 703 | ((struct resolve_cb_context *)context)->status = status; |
Matan Barak | dd5f03b | 2013-12-12 18:03:11 +0200 | [diff] [blame] | 704 | complete(&((struct resolve_cb_context *)context)->comp); |
| 705 | } |
| 706 | |
Matan Barak | f7f4b23e | 2016-01-04 10:49:53 +0200 | [diff] [blame] | 707 | int rdma_addr_find_l2_eth_by_grh(const union ib_gid *sgid, |
| 708 | const union ib_gid *dgid, |
Matan Barak | c3efe75 | 2016-01-04 10:49:54 +0200 | [diff] [blame] | 709 | u8 *dmac, u16 *vlan_id, int *if_index, |
| 710 | int *hoplimit) |
Matan Barak | dd5f03b | 2013-12-12 18:03:11 +0200 | [diff] [blame] | 711 | { |
| 712 | int ret = 0; |
| 713 | struct rdma_dev_addr dev_addr; |
| 714 | struct resolve_cb_context ctx; |
| 715 | struct net_device *dev; |
| 716 | |
| 717 | union { |
| 718 | struct sockaddr _sockaddr; |
| 719 | struct sockaddr_in _sockaddr_in; |
| 720 | struct sockaddr_in6 _sockaddr_in6; |
| 721 | } sgid_addr, dgid_addr; |
| 722 | |
| 723 | |
Honggang LI | 471e705 | 2015-04-29 17:40:44 +0800 | [diff] [blame] | 724 | rdma_gid2ip(&sgid_addr._sockaddr, sgid); |
| 725 | rdma_gid2ip(&dgid_addr._sockaddr, dgid); |
Matan Barak | dd5f03b | 2013-12-12 18:03:11 +0200 | [diff] [blame] | 726 | |
| 727 | memset(&dev_addr, 0, sizeof(dev_addr)); |
Matan Barak | 2002983 | 2015-12-23 14:56:53 +0200 | [diff] [blame] | 728 | if (if_index) |
| 729 | dev_addr.bound_dev_if = *if_index; |
Guy Shapiro | 565edd1 | 2015-10-22 15:20:08 +0300 | [diff] [blame] | 730 | dev_addr.net = &init_net; |
Matan Barak | dd5f03b | 2013-12-12 18:03:11 +0200 | [diff] [blame] | 731 | |
| 732 | ctx.addr = &dev_addr; |
| 733 | init_completion(&ctx.comp); |
| 734 | ret = rdma_resolve_ip(&self, &sgid_addr._sockaddr, &dgid_addr._sockaddr, |
| 735 | &dev_addr, 1000, resolve_cb, &ctx); |
| 736 | if (ret) |
| 737 | return ret; |
| 738 | |
| 739 | wait_for_completion(&ctx.comp); |
| 740 | |
Mark Bloch | 61c37028 | 2016-10-27 16:36:29 +0300 | [diff] [blame] | 741 | ret = ctx.status; |
| 742 | if (ret) |
| 743 | return ret; |
| 744 | |
Matan Barak | dd5f03b | 2013-12-12 18:03:11 +0200 | [diff] [blame] | 745 | memcpy(dmac, dev_addr.dst_dev_addr, ETH_ALEN); |
| 746 | dev = dev_get_by_index(&init_net, dev_addr.bound_dev_if); |
| 747 | if (!dev) |
| 748 | return -ENODEV; |
Matan Barak | 2002983 | 2015-12-23 14:56:53 +0200 | [diff] [blame] | 749 | if (if_index) |
| 750 | *if_index = dev_addr.bound_dev_if; |
Matan Barak | dd5f03b | 2013-12-12 18:03:11 +0200 | [diff] [blame] | 751 | if (vlan_id) |
| 752 | *vlan_id = rdma_vlan_dev_vlan_id(dev); |
Matan Barak | c3efe75 | 2016-01-04 10:49:54 +0200 | [diff] [blame] | 753 | if (hoplimit) |
| 754 | *hoplimit = dev_addr.hoplimit; |
Matan Barak | dd5f03b | 2013-12-12 18:03:11 +0200 | [diff] [blame] | 755 | dev_put(dev); |
| 756 | return ret; |
| 757 | } |
Matan Barak | f7f4b23e | 2016-01-04 10:49:53 +0200 | [diff] [blame] | 758 | EXPORT_SYMBOL(rdma_addr_find_l2_eth_by_grh); |
Matan Barak | dd5f03b | 2013-12-12 18:03:11 +0200 | [diff] [blame] | 759 | |
| 760 | int rdma_addr_find_smac_by_sgid(union ib_gid *sgid, u8 *smac, u16 *vlan_id) |
| 761 | { |
| 762 | int ret = 0; |
| 763 | struct rdma_dev_addr dev_addr; |
| 764 | union { |
| 765 | struct sockaddr _sockaddr; |
| 766 | struct sockaddr_in _sockaddr_in; |
| 767 | struct sockaddr_in6 _sockaddr_in6; |
| 768 | } gid_addr; |
| 769 | |
Honggang LI | 471e705 | 2015-04-29 17:40:44 +0800 | [diff] [blame] | 770 | rdma_gid2ip(&gid_addr._sockaddr, sgid); |
Matan Barak | dd5f03b | 2013-12-12 18:03:11 +0200 | [diff] [blame] | 771 | |
Matan Barak | dd5f03b | 2013-12-12 18:03:11 +0200 | [diff] [blame] | 772 | memset(&dev_addr, 0, sizeof(dev_addr)); |
Guy Shapiro | 565edd1 | 2015-10-22 15:20:08 +0300 | [diff] [blame] | 773 | dev_addr.net = &init_net; |
Matan Barak | dd5f03b | 2013-12-12 18:03:11 +0200 | [diff] [blame] | 774 | ret = rdma_translate_ip(&gid_addr._sockaddr, &dev_addr, vlan_id); |
| 775 | if (ret) |
| 776 | return ret; |
| 777 | |
| 778 | memcpy(smac, dev_addr.src_dev_addr, ETH_ALEN); |
| 779 | return ret; |
| 780 | } |
| 781 | EXPORT_SYMBOL(rdma_addr_find_smac_by_sgid); |
| 782 | |
Roland Dreier | 3cd9656 | 2006-09-22 15:22:46 -0700 | [diff] [blame] | 783 | static int netevent_callback(struct notifier_block *self, unsigned long event, |
Tom Tucker | e795d09 | 2006-07-30 20:44:19 -0700 | [diff] [blame] | 784 | void *ctx) |
Sean Hefty | 7025fcd | 2006-06-17 20:37:28 -0700 | [diff] [blame] | 785 | { |
Roland Dreier | 3cd9656 | 2006-09-22 15:22:46 -0700 | [diff] [blame] | 786 | if (event == NETEVENT_NEIGH_UPDATE) { |
Tom Tucker | e795d09 | 2006-07-30 20:44:19 -0700 | [diff] [blame] | 787 | struct neighbour *neigh = ctx; |
Sean Hefty | 7025fcd | 2006-06-17 20:37:28 -0700 | [diff] [blame] | 788 | |
Steve Wise | 1f12667 | 2007-01-23 19:03:17 -0600 | [diff] [blame] | 789 | if (neigh->nud_state & NUD_VALID) { |
Tom Tucker | e795d09 | 2006-07-30 20:44:19 -0700 | [diff] [blame] | 790 | set_timeout(jiffies); |
| 791 | } |
| 792 | } |
Sean Hefty | 7025fcd | 2006-06-17 20:37:28 -0700 | [diff] [blame] | 793 | return 0; |
| 794 | } |
| 795 | |
Tom Tucker | e795d09 | 2006-07-30 20:44:19 -0700 | [diff] [blame] | 796 | static struct notifier_block nb = { |
| 797 | .notifier_call = netevent_callback |
Sean Hefty | 7025fcd | 2006-06-17 20:37:28 -0700 | [diff] [blame] | 798 | }; |
| 799 | |
Leon Romanovsky | e3f20f0 | 2016-05-19 17:12:31 +0300 | [diff] [blame] | 800 | int addr_init(void) |
Sean Hefty | 7025fcd | 2006-06-17 20:37:28 -0700 | [diff] [blame] | 801 | { |
Bhaktipriya Shridhar | f548162 | 2016-08-15 23:30:32 +0530 | [diff] [blame] | 802 | addr_wq = alloc_workqueue("ib_addr", WQ_MEM_RECLAIM, 0); |
Sean Hefty | 7025fcd | 2006-06-17 20:37:28 -0700 | [diff] [blame] | 803 | if (!addr_wq) |
| 804 | return -ENOMEM; |
| 805 | |
Tom Tucker | e795d09 | 2006-07-30 20:44:19 -0700 | [diff] [blame] | 806 | register_netevent_notifier(&nb); |
Matan Barak | dd5f03b | 2013-12-12 18:03:11 +0200 | [diff] [blame] | 807 | rdma_addr_register_client(&self); |
Mark Bloch | ae43f82 | 2016-05-19 17:12:36 +0300 | [diff] [blame] | 808 | |
Sean Hefty | 7025fcd | 2006-06-17 20:37:28 -0700 | [diff] [blame] | 809 | return 0; |
| 810 | } |
| 811 | |
Leon Romanovsky | e3f20f0 | 2016-05-19 17:12:31 +0300 | [diff] [blame] | 812 | void addr_cleanup(void) |
Sean Hefty | 7025fcd | 2006-06-17 20:37:28 -0700 | [diff] [blame] | 813 | { |
Matan Barak | dd5f03b | 2013-12-12 18:03:11 +0200 | [diff] [blame] | 814 | rdma_addr_unregister_client(&self); |
Tom Tucker | e795d09 | 2006-07-30 20:44:19 -0700 | [diff] [blame] | 815 | unregister_netevent_notifier(&nb); |
Sean Hefty | 7025fcd | 2006-06-17 20:37:28 -0700 | [diff] [blame] | 816 | destroy_workqueue(addr_wq); |
| 817 | } |