addr.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787
  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 skb->len;
  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. int ret;
  352. memset(&fl6, 0, sizeof fl6);
  353. fl6.daddr = dst_in->sin6_addr;
  354. fl6.saddr = src_in->sin6_addr;
  355. fl6.flowi6_oif = addr->bound_dev_if;
  356. ret = ipv6_stub->ipv6_dst_lookup(addr->net, NULL, &dst, &fl6);
  357. if (ret < 0)
  358. return ret;
  359. rt = (struct rt6_info *)dst;
  360. if (ipv6_addr_any(&src_in->sin6_addr)) {
  361. src_in->sin6_family = AF_INET6;
  362. src_in->sin6_addr = fl6.saddr;
  363. }
  364. /* If there's a gateway and type of device not ARPHRD_INFINIBAND, we're
  365. * definitely in RoCE v2 (as RoCE v1 isn't routable) set the network
  366. * type accordingly.
  367. */
  368. if (rt->rt6i_flags & RTF_GATEWAY &&
  369. ip6_dst_idev(dst)->dev->type != ARPHRD_INFINIBAND)
  370. addr->network = RDMA_NETWORK_IPV6;
  371. addr->hoplimit = ip6_dst_hoplimit(dst);
  372. *pdst = dst;
  373. return 0;
  374. }
  375. #else
  376. static int addr6_resolve(struct sockaddr_in6 *src_in,
  377. const struct sockaddr_in6 *dst_in,
  378. struct rdma_dev_addr *addr,
  379. struct dst_entry **pdst)
  380. {
  381. return -EADDRNOTAVAIL;
  382. }
  383. #endif
  384. static int addr_resolve_neigh(const struct dst_entry *dst,
  385. const struct sockaddr *dst_in,
  386. struct rdma_dev_addr *addr,
  387. u32 seq)
  388. {
  389. if (dst->dev->flags & IFF_LOOPBACK) {
  390. int ret;
  391. ret = rdma_translate_ip(dst_in, addr);
  392. if (!ret)
  393. memcpy(addr->dst_dev_addr, addr->src_dev_addr,
  394. MAX_ADDR_LEN);
  395. return ret;
  396. }
  397. /* If the device doesn't do ARP internally */
  398. if (!(dst->dev->flags & IFF_NOARP))
  399. return fetch_ha(dst, addr, dst_in, seq);
  400. rdma_copy_addr(addr, dst->dev, NULL);
  401. return 0;
  402. }
  403. static int addr_resolve(struct sockaddr *src_in,
  404. const struct sockaddr *dst_in,
  405. struct rdma_dev_addr *addr,
  406. bool resolve_neigh,
  407. u32 seq)
  408. {
  409. struct net_device *ndev;
  410. struct dst_entry *dst;
  411. int ret;
  412. if (!addr->net) {
  413. pr_warn_ratelimited("%s: missing namespace\n", __func__);
  414. return -EINVAL;
  415. }
  416. if (src_in->sa_family == AF_INET) {
  417. struct rtable *rt = NULL;
  418. const struct sockaddr_in *dst_in4 =
  419. (const struct sockaddr_in *)dst_in;
  420. ret = addr4_resolve((struct sockaddr_in *)src_in,
  421. dst_in4, addr, &rt);
  422. if (ret)
  423. return ret;
  424. if (resolve_neigh)
  425. ret = addr_resolve_neigh(&rt->dst, dst_in, addr, seq);
  426. if (addr->bound_dev_if) {
  427. ndev = dev_get_by_index(addr->net, addr->bound_dev_if);
  428. } else {
  429. ndev = rt->dst.dev;
  430. dev_hold(ndev);
  431. }
  432. ip_rt_put(rt);
  433. } else {
  434. const struct sockaddr_in6 *dst_in6 =
  435. (const struct sockaddr_in6 *)dst_in;
  436. ret = addr6_resolve((struct sockaddr_in6 *)src_in,
  437. dst_in6, addr,
  438. &dst);
  439. if (ret)
  440. return ret;
  441. if (resolve_neigh)
  442. ret = addr_resolve_neigh(dst, dst_in, addr, seq);
  443. if (addr->bound_dev_if) {
  444. ndev = dev_get_by_index(addr->net, addr->bound_dev_if);
  445. } else {
  446. ndev = dst->dev;
  447. dev_hold(ndev);
  448. }
  449. dst_release(dst);
  450. }
  451. if (ndev) {
  452. if (ndev->flags & IFF_LOOPBACK)
  453. ret = rdma_translate_ip(dst_in, addr);
  454. else
  455. addr->bound_dev_if = ndev->ifindex;
  456. dev_put(ndev);
  457. }
  458. return ret;
  459. }
  460. static void process_one_req(struct work_struct *_work)
  461. {
  462. struct addr_req *req;
  463. struct sockaddr *src_in, *dst_in;
  464. req = container_of(_work, struct addr_req, work.work);
  465. if (req->status == -ENODATA) {
  466. src_in = (struct sockaddr *)&req->src_addr;
  467. dst_in = (struct sockaddr *)&req->dst_addr;
  468. req->status = addr_resolve(src_in, dst_in, req->addr,
  469. true, req->seq);
  470. if (req->status && time_after_eq(jiffies, req->timeout)) {
  471. req->status = -ETIMEDOUT;
  472. } else if (req->status == -ENODATA) {
  473. /* requeue the work for retrying again */
  474. spin_lock_bh(&lock);
  475. if (!list_empty(&req->list))
  476. set_timeout(req, req->timeout);
  477. spin_unlock_bh(&lock);
  478. return;
  479. }
  480. }
  481. req->callback(req->status, (struct sockaddr *)&req->src_addr,
  482. req->addr, req->context);
  483. req->callback = NULL;
  484. spin_lock_bh(&lock);
  485. if (!list_empty(&req->list)) {
  486. /*
  487. * Although the work will normally have been canceled by the
  488. * workqueue, it can still be requeued as long as it is on the
  489. * req_list.
  490. */
  491. cancel_delayed_work(&req->work);
  492. list_del_init(&req->list);
  493. kfree(req);
  494. }
  495. spin_unlock_bh(&lock);
  496. }
  497. int rdma_resolve_ip(struct sockaddr *src_addr, const struct sockaddr *dst_addr,
  498. struct rdma_dev_addr *addr, int timeout_ms,
  499. void (*callback)(int status, struct sockaddr *src_addr,
  500. struct rdma_dev_addr *addr, void *context),
  501. void *context)
  502. {
  503. struct sockaddr *src_in, *dst_in;
  504. struct addr_req *req;
  505. int ret = 0;
  506. req = kzalloc(sizeof *req, GFP_KERNEL);
  507. if (!req)
  508. return -ENOMEM;
  509. src_in = (struct sockaddr *) &req->src_addr;
  510. dst_in = (struct sockaddr *) &req->dst_addr;
  511. if (src_addr) {
  512. if (src_addr->sa_family != dst_addr->sa_family) {
  513. ret = -EINVAL;
  514. goto err;
  515. }
  516. memcpy(src_in, src_addr, rdma_addr_size(src_addr));
  517. } else {
  518. src_in->sa_family = dst_addr->sa_family;
  519. }
  520. memcpy(dst_in, dst_addr, rdma_addr_size(dst_addr));
  521. req->addr = addr;
  522. req->callback = callback;
  523. req->context = context;
  524. INIT_DELAYED_WORK(&req->work, process_one_req);
  525. req->seq = (u32)atomic_inc_return(&ib_nl_addr_request_seq);
  526. req->status = addr_resolve(src_in, dst_in, addr, true, req->seq);
  527. switch (req->status) {
  528. case 0:
  529. req->timeout = jiffies;
  530. queue_req(req);
  531. break;
  532. case -ENODATA:
  533. req->timeout = msecs_to_jiffies(timeout_ms) + jiffies;
  534. queue_req(req);
  535. break;
  536. default:
  537. ret = req->status;
  538. goto err;
  539. }
  540. return ret;
  541. err:
  542. kfree(req);
  543. return ret;
  544. }
  545. EXPORT_SYMBOL(rdma_resolve_ip);
  546. int rdma_resolve_ip_route(struct sockaddr *src_addr,
  547. const struct sockaddr *dst_addr,
  548. struct rdma_dev_addr *addr)
  549. {
  550. struct sockaddr_storage ssrc_addr = {};
  551. struct sockaddr *src_in = (struct sockaddr *)&ssrc_addr;
  552. if (src_addr) {
  553. if (src_addr->sa_family != dst_addr->sa_family)
  554. return -EINVAL;
  555. memcpy(src_in, src_addr, rdma_addr_size(src_addr));
  556. } else {
  557. src_in->sa_family = dst_addr->sa_family;
  558. }
  559. return addr_resolve(src_in, dst_addr, addr, false, 0);
  560. }
  561. void rdma_addr_cancel(struct rdma_dev_addr *addr)
  562. {
  563. struct addr_req *req, *temp_req;
  564. struct addr_req *found = NULL;
  565. spin_lock_bh(&lock);
  566. list_for_each_entry_safe(req, temp_req, &req_list, list) {
  567. if (req->addr == addr) {
  568. /*
  569. * Removing from the list means we take ownership of
  570. * the req
  571. */
  572. list_del_init(&req->list);
  573. found = req;
  574. break;
  575. }
  576. }
  577. spin_unlock_bh(&lock);
  578. if (!found)
  579. return;
  580. /*
  581. * sync canceling the work after removing it from the req_list
  582. * guarentees no work is running and none will be started.
  583. */
  584. cancel_delayed_work_sync(&found->work);
  585. if (found->callback)
  586. found->callback(-ECANCELED, (struct sockaddr *)&found->src_addr,
  587. found->addr, found->context);
  588. kfree(found);
  589. }
  590. EXPORT_SYMBOL(rdma_addr_cancel);
  591. struct resolve_cb_context {
  592. struct completion comp;
  593. int status;
  594. };
  595. static void resolve_cb(int status, struct sockaddr *src_addr,
  596. struct rdma_dev_addr *addr, void *context)
  597. {
  598. ((struct resolve_cb_context *)context)->status = status;
  599. complete(&((struct resolve_cb_context *)context)->comp);
  600. }
  601. int rdma_addr_find_l2_eth_by_grh(const union ib_gid *sgid,
  602. const union ib_gid *dgid,
  603. u8 *dmac, const struct net_device *ndev,
  604. int *hoplimit)
  605. {
  606. struct rdma_dev_addr dev_addr;
  607. struct resolve_cb_context ctx;
  608. union {
  609. struct sockaddr_in _sockaddr_in;
  610. struct sockaddr_in6 _sockaddr_in6;
  611. } sgid_addr, dgid_addr;
  612. int ret;
  613. rdma_gid2ip((struct sockaddr *)&sgid_addr, sgid);
  614. rdma_gid2ip((struct sockaddr *)&dgid_addr, dgid);
  615. memset(&dev_addr, 0, sizeof(dev_addr));
  616. dev_addr.bound_dev_if = ndev->ifindex;
  617. dev_addr.net = &init_net;
  618. init_completion(&ctx.comp);
  619. ret = rdma_resolve_ip((struct sockaddr *)&sgid_addr,
  620. (struct sockaddr *)&dgid_addr, &dev_addr, 1000,
  621. resolve_cb, &ctx);
  622. if (ret)
  623. return ret;
  624. wait_for_completion(&ctx.comp);
  625. ret = ctx.status;
  626. if (ret)
  627. return ret;
  628. memcpy(dmac, dev_addr.dst_dev_addr, ETH_ALEN);
  629. *hoplimit = dev_addr.hoplimit;
  630. return 0;
  631. }
  632. static int netevent_callback(struct notifier_block *self, unsigned long event,
  633. void *ctx)
  634. {
  635. struct addr_req *req;
  636. if (event == NETEVENT_NEIGH_UPDATE) {
  637. struct neighbour *neigh = ctx;
  638. if (neigh->nud_state & NUD_VALID) {
  639. spin_lock_bh(&lock);
  640. list_for_each_entry(req, &req_list, list)
  641. set_timeout(req, jiffies);
  642. spin_unlock_bh(&lock);
  643. }
  644. }
  645. return 0;
  646. }
  647. static struct notifier_block nb = {
  648. .notifier_call = netevent_callback
  649. };
  650. int addr_init(void)
  651. {
  652. addr_wq = alloc_ordered_workqueue("ib_addr", 0);
  653. if (!addr_wq)
  654. return -ENOMEM;
  655. register_netevent_notifier(&nb);
  656. return 0;
  657. }
  658. void addr_cleanup(void)
  659. {
  660. unregister_netevent_notifier(&nb);
  661. destroy_workqueue(addr_wq);
  662. WARN_ON(!list_empty(&req_list));
  663. }