addr.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785
  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. *
  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:
  12. *
  13. * Redistribution and use in source and binary forms, with or
  14. * without modification, are permitted provided that the following
  15. * conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above
  18. * copyright notice, this list of conditions and the following
  19. * disclaimer.
  20. *
  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.
  25. *
  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.
  34. */
  35. #include <linux/mutex.h>
  36. #include <linux/inetdevice.h>
  37. #include <linux/slab.h>
  38. #include <linux/workqueue.h>
  39. #include <linux/module.h>
  40. #include <net/arp.h>
  41. #include <net/neighbour.h>
  42. #include <net/route.h>
  43. #include <net/netevent.h>
  44. #include <net/addrconf.h>
  45. #include <net/ip6_route.h>
  46. #include <rdma/ib_addr.h>
  47. #include <rdma/ib.h>
  48. #include <rdma/rdma_netlink.h>
  49. #include <net/netlink.h>
  50. #include "core_priv.h"
  51. struct addr_req {
  52. struct list_head list;
  53. struct sockaddr_storage src_addr;
  54. struct sockaddr_storage dst_addr;
  55. struct rdma_dev_addr *addr;
  56. void *context;
  57. void (*callback)(int status, struct sockaddr *src_addr,
  58. struct rdma_dev_addr *addr, void *context);
  59. unsigned long timeout;
  60. struct delayed_work work;
  61. int status;
  62. u32 seq;
  63. };
  64. static atomic_t ib_nl_addr_request_seq = ATOMIC_INIT(0);
  65. static DEFINE_SPINLOCK(lock);
  66. static LIST_HEAD(req_list);
  67. static struct workqueue_struct *addr_wq;
  68. static const struct nla_policy ib_nl_addr_policy[LS_NLA_TYPE_MAX] = {
  69. [LS_NLA_TYPE_DGID] = {.type = NLA_BINARY,
  70. .len = sizeof(struct rdma_nla_ls_gid)},
  71. };
  72. static inline bool ib_nl_is_good_ip_resp(const struct nlmsghdr *nlh)
  73. {
  74. struct nlattr *tb[LS_NLA_TYPE_MAX] = {};
  75. int ret;
  76. if (nlh->nlmsg_flags & RDMA_NL_LS_F_ERR)
  77. return false;
  78. ret = nla_parse(tb, LS_NLA_TYPE_MAX - 1, nlmsg_data(nlh),
  79. nlmsg_len(nlh), ib_nl_addr_policy, NULL);
  80. if (ret)
  81. return false;
  82. return true;
  83. }
  84. static void ib_nl_process_good_ip_rsep(const struct nlmsghdr *nlh)
  85. {
  86. const struct nlattr *head, *curr;
  87. union ib_gid gid;
  88. struct addr_req *req;
  89. int len, rem;
  90. int found = 0;
  91. head = (const struct nlattr *)nlmsg_data(nlh);
  92. len = nlmsg_len(nlh);
  93. nla_for_each_attr(curr, head, len, rem) {
  94. if (curr->nla_type == LS_NLA_TYPE_DGID)
  95. memcpy(&gid, nla_data(curr), nla_len(curr));
  96. }
  97. spin_lock_bh(&lock);
  98. list_for_each_entry(req, &req_list, list) {
  99. if (nlh->nlmsg_seq != req->seq)
  100. continue;
  101. /* We set the DGID part, the rest was set earlier */
  102. rdma_addr_set_dgid(req->addr, &gid);
  103. req->status = 0;
  104. found = 1;
  105. break;
  106. }
  107. spin_unlock_bh(&lock);
  108. if (!found)
  109. pr_info("Couldn't find request waiting for DGID: %pI6\n",
  110. &gid);
  111. }
  112. int ib_nl_handle_ip_res_resp(struct sk_buff *skb,
  113. struct nlmsghdr *nlh,
  114. struct netlink_ext_ack *extack)
  115. {
  116. if ((nlh->nlmsg_flags & NLM_F_REQUEST) ||
  117. !(NETLINK_CB(skb).sk))
  118. return -EPERM;
  119. if (ib_nl_is_good_ip_resp(nlh))
  120. ib_nl_process_good_ip_rsep(nlh);
  121. return 0;
  122. }
  123. static int ib_nl_ip_send_msg(struct rdma_dev_addr *dev_addr,
  124. const void *daddr,
  125. u32 seq, u16 family)
  126. {
  127. struct sk_buff *skb = NULL;
  128. struct nlmsghdr *nlh;
  129. struct rdma_ls_ip_resolve_header *header;
  130. void *data;
  131. size_t size;
  132. int attrtype;
  133. int len;
  134. if (family == AF_INET) {
  135. size = sizeof(struct in_addr);
  136. attrtype = RDMA_NLA_F_MANDATORY | LS_NLA_TYPE_IPV4;
  137. } else {
  138. size = sizeof(struct in6_addr);
  139. attrtype = RDMA_NLA_F_MANDATORY | LS_NLA_TYPE_IPV6;
  140. }
  141. len = nla_total_size(sizeof(size));
  142. len += NLMSG_ALIGN(sizeof(*header));
  143. skb = nlmsg_new(len, GFP_KERNEL);
  144. if (!skb)
  145. return -ENOMEM;
  146. data = ibnl_put_msg(skb, &nlh, seq, 0, RDMA_NL_LS,
  147. RDMA_NL_LS_OP_IP_RESOLVE, NLM_F_REQUEST);
  148. if (!data) {
  149. nlmsg_free(skb);
  150. return -ENODATA;
  151. }
  152. /* Construct the family header first */
  153. header = skb_put(skb, NLMSG_ALIGN(sizeof(*header)));
  154. header->ifindex = dev_addr->bound_dev_if;
  155. nla_put(skb, attrtype, size, daddr);
  156. /* Repair the nlmsg header length */
  157. nlmsg_end(skb, nlh);
  158. rdma_nl_multicast(skb, RDMA_NL_GROUP_LS, GFP_KERNEL);
  159. /* Make the request retry, so when we get the response from userspace
  160. * we will have something.
  161. */
  162. return -ENODATA;
  163. }
  164. int rdma_addr_size(const struct sockaddr *addr)
  165. {
  166. switch (addr->sa_family) {
  167. case AF_INET:
  168. return sizeof(struct sockaddr_in);
  169. case AF_INET6:
  170. return sizeof(struct sockaddr_in6);
  171. case AF_IB:
  172. return sizeof(struct sockaddr_ib);
  173. default:
  174. return 0;
  175. }
  176. }
  177. EXPORT_SYMBOL(rdma_addr_size);
  178. int rdma_addr_size_in6(struct sockaddr_in6 *addr)
  179. {
  180. int ret = rdma_addr_size((struct sockaddr *) addr);
  181. return ret <= sizeof(*addr) ? ret : 0;
  182. }
  183. EXPORT_SYMBOL(rdma_addr_size_in6);
  184. int rdma_addr_size_kss(struct __kernel_sockaddr_storage *addr)
  185. {
  186. int ret = rdma_addr_size((struct sockaddr *) addr);
  187. return ret <= sizeof(*addr) ? ret : 0;
  188. }
  189. EXPORT_SYMBOL(rdma_addr_size_kss);
  190. void rdma_copy_addr(struct rdma_dev_addr *dev_addr,
  191. const struct net_device *dev,
  192. const unsigned char *dst_dev_addr)
  193. {
  194. dev_addr->dev_type = dev->type;
  195. memcpy(dev_addr->src_dev_addr, dev->dev_addr, MAX_ADDR_LEN);
  196. memcpy(dev_addr->broadcast, dev->broadcast, MAX_ADDR_LEN);
  197. if (dst_dev_addr)
  198. memcpy(dev_addr->dst_dev_addr, dst_dev_addr, MAX_ADDR_LEN);
  199. dev_addr->bound_dev_if = dev->ifindex;
  200. }
  201. EXPORT_SYMBOL(rdma_copy_addr);
  202. int rdma_translate_ip(const struct sockaddr *addr,
  203. struct rdma_dev_addr *dev_addr)
  204. {
  205. struct net_device *dev;
  206. if (dev_addr->bound_dev_if) {
  207. dev = dev_get_by_index(dev_addr->net, dev_addr->bound_dev_if);
  208. if (!dev)
  209. return -ENODEV;
  210. rdma_copy_addr(dev_addr, dev, NULL);
  211. dev_put(dev);
  212. return 0;
  213. }
  214. switch (addr->sa_family) {
  215. case AF_INET:
  216. dev = ip_dev_find(dev_addr->net,
  217. ((const struct sockaddr_in *)addr)->sin_addr.s_addr);
  218. if (!dev)
  219. return -EADDRNOTAVAIL;
  220. rdma_copy_addr(dev_addr, dev, NULL);
  221. dev_put(dev);
  222. break;
  223. #if IS_ENABLED(CONFIG_IPV6)
  224. case AF_INET6:
  225. rcu_read_lock();
  226. for_each_netdev_rcu(dev_addr->net, dev) {
  227. if (ipv6_chk_addr(dev_addr->net,
  228. &((const struct sockaddr_in6 *)addr)->sin6_addr,
  229. dev, 1)) {
  230. rdma_copy_addr(dev_addr, dev, NULL);
  231. break;
  232. }
  233. }
  234. rcu_read_unlock();
  235. break;
  236. #endif
  237. }
  238. return 0;
  239. }
  240. EXPORT_SYMBOL(rdma_translate_ip);
  241. static void set_timeout(struct addr_req *req, unsigned long time)
  242. {
  243. unsigned long delay;
  244. delay = time - jiffies;
  245. if ((long)delay < 0)
  246. delay = 0;
  247. mod_delayed_work(addr_wq, &req->work, delay);
  248. }
  249. static void queue_req(struct addr_req *req)
  250. {
  251. spin_lock_bh(&lock);
  252. list_add_tail(&req->list, &req_list);
  253. set_timeout(req, req->timeout);
  254. spin_unlock_bh(&lock);
  255. }
  256. static int ib_nl_fetch_ha(const struct dst_entry *dst,
  257. struct rdma_dev_addr *dev_addr,
  258. const void *daddr, u32 seq, u16 family)
  259. {
  260. if (rdma_nl_chk_listeners(RDMA_NL_GROUP_LS))
  261. return -EADDRNOTAVAIL;
  262. /* We fill in what we can, the response will fill the rest */
  263. rdma_copy_addr(dev_addr, dst->dev, NULL);
  264. return ib_nl_ip_send_msg(dev_addr, daddr, seq, family);
  265. }
  266. static int dst_fetch_ha(const struct dst_entry *dst,
  267. struct rdma_dev_addr *dev_addr,
  268. const void *daddr)
  269. {
  270. struct neighbour *n;
  271. int ret = 0;
  272. n = dst_neigh_lookup(dst, daddr);
  273. if (!n)
  274. return -ENODATA;
  275. if (!(n->nud_state & NUD_VALID)) {
  276. neigh_event_send(n, NULL);
  277. ret = -ENODATA;
  278. } else {
  279. rdma_copy_addr(dev_addr, dst->dev, n->ha);
  280. }
  281. neigh_release(n);
  282. return ret;
  283. }
  284. static bool has_gateway(const struct dst_entry *dst, sa_family_t family)
  285. {
  286. struct rtable *rt;
  287. struct rt6_info *rt6;
  288. if (family == AF_INET) {
  289. rt = container_of(dst, struct rtable, dst);
  290. return rt->rt_uses_gateway;
  291. }
  292. rt6 = container_of(dst, struct rt6_info, dst);
  293. return rt6->rt6i_flags & RTF_GATEWAY;
  294. }
  295. static int fetch_ha(const struct dst_entry *dst, struct rdma_dev_addr *dev_addr,
  296. const struct sockaddr *dst_in, u32 seq)
  297. {
  298. const struct sockaddr_in *dst_in4 =
  299. (const struct sockaddr_in *)dst_in;
  300. const struct sockaddr_in6 *dst_in6 =
  301. (const struct sockaddr_in6 *)dst_in;
  302. const void *daddr = (dst_in->sa_family == AF_INET) ?
  303. (const void *)&dst_in4->sin_addr.s_addr :
  304. (const void *)&dst_in6->sin6_addr;
  305. sa_family_t family = dst_in->sa_family;
  306. /* Gateway + ARPHRD_INFINIBAND -> IB router */
  307. if (has_gateway(dst, family) && dst->dev->type == ARPHRD_INFINIBAND)
  308. return ib_nl_fetch_ha(dst, dev_addr, daddr, seq, family);
  309. else
  310. return dst_fetch_ha(dst, dev_addr, daddr);
  311. }
  312. static int addr4_resolve(struct sockaddr_in *src_in,
  313. const struct sockaddr_in *dst_in,
  314. struct rdma_dev_addr *addr,
  315. struct rtable **prt)
  316. {
  317. __be32 src_ip = src_in->sin_addr.s_addr;
  318. __be32 dst_ip = dst_in->sin_addr.s_addr;
  319. struct rtable *rt;
  320. struct flowi4 fl4;
  321. int ret;
  322. memset(&fl4, 0, sizeof(fl4));
  323. fl4.daddr = dst_ip;
  324. fl4.saddr = src_ip;
  325. fl4.flowi4_oif = addr->bound_dev_if;
  326. rt = ip_route_output_key(addr->net, &fl4);
  327. ret = PTR_ERR_OR_ZERO(rt);
  328. if (ret)
  329. return ret;
  330. src_in->sin_family = AF_INET;
  331. src_in->sin_addr.s_addr = fl4.saddr;
  332. /* If there's a gateway and type of device not ARPHRD_INFINIBAND, we're
  333. * definitely in RoCE v2 (as RoCE v1 isn't routable) set the network
  334. * type accordingly.
  335. */
  336. if (rt->rt_uses_gateway && rt->dst.dev->type != ARPHRD_INFINIBAND)
  337. addr->network = RDMA_NETWORK_IPV4;
  338. addr->hoplimit = ip4_dst_hoplimit(&rt->dst);
  339. *prt = rt;
  340. return 0;
  341. }
  342. #if IS_ENABLED(CONFIG_IPV6)
  343. static int addr6_resolve(struct sockaddr_in6 *src_in,
  344. const struct sockaddr_in6 *dst_in,
  345. struct rdma_dev_addr *addr,
  346. struct dst_entry **pdst)
  347. {
  348. struct flowi6 fl6;
  349. struct dst_entry *dst;
  350. struct rt6_info *rt;
  351. memset(&fl6, 0, sizeof fl6);
  352. fl6.daddr = dst_in->sin6_addr;
  353. fl6.saddr = src_in->sin6_addr;
  354. fl6.flowi6_oif = addr->bound_dev_if;
  355. dst = ipv6_stub->ipv6_dst_lookup_flow(addr->net, NULL, &fl6, NULL);
  356. if (IS_ERR(dst))
  357. return PTR_ERR(dst);
  358. rt = (struct rt6_info *)dst;
  359. if (ipv6_addr_any(&src_in->sin6_addr)) {
  360. src_in->sin6_family = AF_INET6;
  361. src_in->sin6_addr = fl6.saddr;
  362. }
  363. /* If there's a gateway and type of device not ARPHRD_INFINIBAND, we're
  364. * definitely in RoCE v2 (as RoCE v1 isn't routable) set the network
  365. * type accordingly.
  366. */
  367. if (rt->rt6i_flags & RTF_GATEWAY &&
  368. ip6_dst_idev(dst)->dev->type != ARPHRD_INFINIBAND)
  369. addr->network = RDMA_NETWORK_IPV6;
  370. addr->hoplimit = ip6_dst_hoplimit(dst);
  371. *pdst = dst;
  372. return 0;
  373. }
  374. #else
  375. static int addr6_resolve(struct sockaddr_in6 *src_in,
  376. const struct sockaddr_in6 *dst_in,
  377. struct rdma_dev_addr *addr,
  378. struct dst_entry **pdst)
  379. {
  380. return -EADDRNOTAVAIL;
  381. }
  382. #endif
  383. static int addr_resolve_neigh(const struct dst_entry *dst,
  384. const struct sockaddr *dst_in,
  385. struct rdma_dev_addr *addr,
  386. u32 seq)
  387. {
  388. if (dst->dev->flags & IFF_LOOPBACK) {
  389. int ret;
  390. ret = rdma_translate_ip(dst_in, addr);
  391. if (!ret)
  392. memcpy(addr->dst_dev_addr, addr->src_dev_addr,
  393. MAX_ADDR_LEN);
  394. return ret;
  395. }
  396. /* If the device doesn't do ARP internally */
  397. if (!(dst->dev->flags & IFF_NOARP))
  398. return fetch_ha(dst, addr, dst_in, seq);
  399. rdma_copy_addr(addr, dst->dev, NULL);
  400. return 0;
  401. }
  402. static int addr_resolve(struct sockaddr *src_in,
  403. const struct sockaddr *dst_in,
  404. struct rdma_dev_addr *addr,
  405. bool resolve_neigh,
  406. u32 seq)
  407. {
  408. struct net_device *ndev;
  409. struct dst_entry *dst;
  410. int ret;
  411. if (!addr->net) {
  412. pr_warn_ratelimited("%s: missing namespace\n", __func__);
  413. return -EINVAL;
  414. }
  415. if (src_in->sa_family == AF_INET) {
  416. struct rtable *rt = NULL;
  417. const struct sockaddr_in *dst_in4 =
  418. (const struct sockaddr_in *)dst_in;
  419. ret = addr4_resolve((struct sockaddr_in *)src_in,
  420. dst_in4, addr, &rt);
  421. if (ret)
  422. return ret;
  423. if (resolve_neigh)
  424. ret = addr_resolve_neigh(&rt->dst, dst_in, addr, seq);
  425. if (addr->bound_dev_if) {
  426. ndev = dev_get_by_index(addr->net, addr->bound_dev_if);
  427. } else {
  428. ndev = rt->dst.dev;
  429. dev_hold(ndev);
  430. }
  431. ip_rt_put(rt);
  432. } else {
  433. const struct sockaddr_in6 *dst_in6 =
  434. (const struct sockaddr_in6 *)dst_in;
  435. ret = addr6_resolve((struct sockaddr_in6 *)src_in,
  436. dst_in6, addr,
  437. &dst);
  438. if (ret)
  439. return ret;
  440. if (resolve_neigh)
  441. ret = addr_resolve_neigh(dst, dst_in, addr, seq);
  442. if (addr->bound_dev_if) {
  443. ndev = dev_get_by_index(addr->net, addr->bound_dev_if);
  444. } else {
  445. ndev = dst->dev;
  446. dev_hold(ndev);
  447. }
  448. dst_release(dst);
  449. }
  450. if (ndev) {
  451. if (ndev->flags & IFF_LOOPBACK)
  452. ret = rdma_translate_ip(dst_in, addr);
  453. else
  454. addr->bound_dev_if = ndev->ifindex;
  455. dev_put(ndev);
  456. }
  457. return ret;
  458. }
  459. static void process_one_req(struct work_struct *_work)
  460. {
  461. struct addr_req *req;
  462. struct sockaddr *src_in, *dst_in;
  463. req = container_of(_work, struct addr_req, work.work);
  464. if (req->status == -ENODATA) {
  465. src_in = (struct sockaddr *)&req->src_addr;
  466. dst_in = (struct sockaddr *)&req->dst_addr;
  467. req->status = addr_resolve(src_in, dst_in, req->addr,
  468. true, req->seq);
  469. if (req->status && time_after_eq(jiffies, req->timeout)) {
  470. req->status = -ETIMEDOUT;
  471. } else if (req->status == -ENODATA) {
  472. /* requeue the work for retrying again */
  473. spin_lock_bh(&lock);
  474. if (!list_empty(&req->list))
  475. set_timeout(req, req->timeout);
  476. spin_unlock_bh(&lock);
  477. return;
  478. }
  479. }
  480. req->callback(req->status, (struct sockaddr *)&req->src_addr,
  481. req->addr, req->context);
  482. req->callback = NULL;
  483. spin_lock_bh(&lock);
  484. /*
  485. * Although the work will normally have been canceled by the workqueue,
  486. * it can still be requeued as long as it is on the req_list.
  487. */
  488. cancel_delayed_work(&req->work);
  489. if (!list_empty(&req->list)) {
  490. list_del_init(&req->list);
  491. kfree(req);
  492. }
  493. spin_unlock_bh(&lock);
  494. }
  495. int rdma_resolve_ip(struct sockaddr *src_addr, const struct sockaddr *dst_addr,
  496. struct rdma_dev_addr *addr, int timeout_ms,
  497. void (*callback)(int status, struct sockaddr *src_addr,
  498. struct rdma_dev_addr *addr, void *context),
  499. void *context)
  500. {
  501. struct sockaddr *src_in, *dst_in;
  502. struct addr_req *req;
  503. int ret = 0;
  504. req = kzalloc(sizeof *req, GFP_KERNEL);
  505. if (!req)
  506. return -ENOMEM;
  507. src_in = (struct sockaddr *) &req->src_addr;
  508. dst_in = (struct sockaddr *) &req->dst_addr;
  509. if (src_addr) {
  510. if (src_addr->sa_family != dst_addr->sa_family) {
  511. ret = -EINVAL;
  512. goto err;
  513. }
  514. memcpy(src_in, src_addr, rdma_addr_size(src_addr));
  515. } else {
  516. src_in->sa_family = dst_addr->sa_family;
  517. }
  518. memcpy(dst_in, dst_addr, rdma_addr_size(dst_addr));
  519. req->addr = addr;
  520. req->callback = callback;
  521. req->context = context;
  522. INIT_DELAYED_WORK(&req->work, process_one_req);
  523. req->seq = (u32)atomic_inc_return(&ib_nl_addr_request_seq);
  524. req->status = addr_resolve(src_in, dst_in, addr, true, req->seq);
  525. switch (req->status) {
  526. case 0:
  527. req->timeout = jiffies;
  528. queue_req(req);
  529. break;
  530. case -ENODATA:
  531. req->timeout = msecs_to_jiffies(timeout_ms) + jiffies;
  532. queue_req(req);
  533. break;
  534. default:
  535. ret = req->status;
  536. goto err;
  537. }
  538. return ret;
  539. err:
  540. kfree(req);
  541. return ret;
  542. }
  543. EXPORT_SYMBOL(rdma_resolve_ip);
  544. int rdma_resolve_ip_route(struct sockaddr *src_addr,
  545. const struct sockaddr *dst_addr,
  546. struct rdma_dev_addr *addr)
  547. {
  548. struct sockaddr_storage ssrc_addr = {};
  549. struct sockaddr *src_in = (struct sockaddr *)&ssrc_addr;
  550. if (src_addr) {
  551. if (src_addr->sa_family != dst_addr->sa_family)
  552. return -EINVAL;
  553. memcpy(src_in, src_addr, rdma_addr_size(src_addr));
  554. } else {
  555. src_in->sa_family = dst_addr->sa_family;
  556. }
  557. return addr_resolve(src_in, dst_addr, addr, false, 0);
  558. }
  559. void rdma_addr_cancel(struct rdma_dev_addr *addr)
  560. {
  561. struct addr_req *req, *temp_req;
  562. struct addr_req *found = NULL;
  563. spin_lock_bh(&lock);
  564. list_for_each_entry_safe(req, temp_req, &req_list, list) {
  565. if (req->addr == addr) {
  566. /*
  567. * Removing from the list means we take ownership of
  568. * the req
  569. */
  570. list_del_init(&req->list);
  571. found = req;
  572. break;
  573. }
  574. }
  575. spin_unlock_bh(&lock);
  576. if (!found)
  577. return;
  578. /*
  579. * sync canceling the work after removing it from the req_list
  580. * guarentees no work is running and none will be started.
  581. */
  582. cancel_delayed_work_sync(&found->work);
  583. if (found->callback)
  584. found->callback(-ECANCELED, (struct sockaddr *)&found->src_addr,
  585. found->addr, found->context);
  586. kfree(found);
  587. }
  588. EXPORT_SYMBOL(rdma_addr_cancel);
  589. struct resolve_cb_context {
  590. struct completion comp;
  591. int status;
  592. };
  593. static void resolve_cb(int status, struct sockaddr *src_addr,
  594. struct rdma_dev_addr *addr, void *context)
  595. {
  596. ((struct resolve_cb_context *)context)->status = status;
  597. complete(&((struct resolve_cb_context *)context)->comp);
  598. }
  599. int rdma_addr_find_l2_eth_by_grh(const union ib_gid *sgid,
  600. const union ib_gid *dgid,
  601. u8 *dmac, const struct net_device *ndev,
  602. int *hoplimit)
  603. {
  604. struct rdma_dev_addr dev_addr;
  605. struct resolve_cb_context ctx;
  606. union {
  607. struct sockaddr_in _sockaddr_in;
  608. struct sockaddr_in6 _sockaddr_in6;
  609. } sgid_addr, dgid_addr;
  610. int ret;
  611. rdma_gid2ip((struct sockaddr *)&sgid_addr, sgid);
  612. rdma_gid2ip((struct sockaddr *)&dgid_addr, dgid);
  613. memset(&dev_addr, 0, sizeof(dev_addr));
  614. dev_addr.bound_dev_if = ndev->ifindex;
  615. dev_addr.net = &init_net;
  616. init_completion(&ctx.comp);
  617. ret = rdma_resolve_ip((struct sockaddr *)&sgid_addr,
  618. (struct sockaddr *)&dgid_addr, &dev_addr, 1000,
  619. resolve_cb, &ctx);
  620. if (ret)
  621. return ret;
  622. wait_for_completion(&ctx.comp);
  623. ret = ctx.status;
  624. if (ret)
  625. return ret;
  626. memcpy(dmac, dev_addr.dst_dev_addr, ETH_ALEN);
  627. *hoplimit = dev_addr.hoplimit;
  628. return 0;
  629. }
  630. static int netevent_callback(struct notifier_block *self, unsigned long event,
  631. void *ctx)
  632. {
  633. struct addr_req *req;
  634. if (event == NETEVENT_NEIGH_UPDATE) {
  635. struct neighbour *neigh = ctx;
  636. if (neigh->nud_state & NUD_VALID) {
  637. spin_lock_bh(&lock);
  638. list_for_each_entry(req, &req_list, list)
  639. set_timeout(req, jiffies);
  640. spin_unlock_bh(&lock);
  641. }
  642. }
  643. return 0;
  644. }
  645. static struct notifier_block nb = {
  646. .notifier_call = netevent_callback
  647. };
  648. int addr_init(void)
  649. {
  650. addr_wq = alloc_ordered_workqueue("ib_addr", 0);
  651. if (!addr_wq)
  652. return -ENOMEM;
  653. register_netevent_notifier(&nb);
  654. return 0;
  655. }
  656. void addr_cleanup(void)
  657. {
  658. unregister_netevent_notifier(&nb);
  659. destroy_workqueue(addr_wq);
  660. WARN_ON(!list_empty(&req_list));
  661. }