addr.c 20 KB

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