addr.c 20 KB

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