addr.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  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/workqueue.h>
  38. #include <net/arp.h>
  39. #include <net/neighbour.h>
  40. #include <net/route.h>
  41. #include <net/netevent.h>
  42. #include <net/addrconf.h>
  43. #include <net/ip6_route.h>
  44. #include <rdma/ib_addr.h>
  45. MODULE_AUTHOR("Sean Hefty");
  46. MODULE_DESCRIPTION("IB Address Translation");
  47. MODULE_LICENSE("Dual BSD/GPL");
  48. struct addr_req {
  49. struct list_head list;
  50. struct sockaddr_storage src_addr;
  51. struct sockaddr_storage dst_addr;
  52. struct rdma_dev_addr *addr;
  53. struct rdma_addr_client *client;
  54. void *context;
  55. void (*callback)(int status, struct sockaddr *src_addr,
  56. struct rdma_dev_addr *addr, void *context);
  57. unsigned long timeout;
  58. int status;
  59. };
  60. static void process_req(struct work_struct *work);
  61. static DEFINE_MUTEX(lock);
  62. static LIST_HEAD(req_list);
  63. static DECLARE_DELAYED_WORK(work, process_req);
  64. static struct workqueue_struct *addr_wq;
  65. void rdma_addr_register_client(struct rdma_addr_client *client)
  66. {
  67. atomic_set(&client->refcount, 1);
  68. init_completion(&client->comp);
  69. }
  70. EXPORT_SYMBOL(rdma_addr_register_client);
  71. static inline void put_client(struct rdma_addr_client *client)
  72. {
  73. if (atomic_dec_and_test(&client->refcount))
  74. complete(&client->comp);
  75. }
  76. void rdma_addr_unregister_client(struct rdma_addr_client *client)
  77. {
  78. put_client(client);
  79. wait_for_completion(&client->comp);
  80. }
  81. EXPORT_SYMBOL(rdma_addr_unregister_client);
  82. int rdma_copy_addr(struct rdma_dev_addr *dev_addr, struct net_device *dev,
  83. const unsigned char *dst_dev_addr)
  84. {
  85. dev_addr->dev_type = dev->type;
  86. memcpy(dev_addr->src_dev_addr, dev->dev_addr, MAX_ADDR_LEN);
  87. memcpy(dev_addr->broadcast, dev->broadcast, MAX_ADDR_LEN);
  88. if (dst_dev_addr)
  89. memcpy(dev_addr->dst_dev_addr, dst_dev_addr, MAX_ADDR_LEN);
  90. dev_addr->bound_dev_if = dev->ifindex;
  91. return 0;
  92. }
  93. EXPORT_SYMBOL(rdma_copy_addr);
  94. int rdma_translate_ip(struct sockaddr *addr, struct rdma_dev_addr *dev_addr)
  95. {
  96. struct net_device *dev;
  97. int ret = -EADDRNOTAVAIL;
  98. if (dev_addr->bound_dev_if) {
  99. dev = dev_get_by_index(&init_net, dev_addr->bound_dev_if);
  100. if (!dev)
  101. return -ENODEV;
  102. ret = rdma_copy_addr(dev_addr, dev, NULL);
  103. dev_put(dev);
  104. return ret;
  105. }
  106. switch (addr->sa_family) {
  107. case AF_INET:
  108. dev = ip_dev_find(&init_net,
  109. ((struct sockaddr_in *) addr)->sin_addr.s_addr);
  110. if (!dev)
  111. return ret;
  112. ret = rdma_copy_addr(dev_addr, dev, NULL);
  113. dev_put(dev);
  114. break;
  115. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  116. case AF_INET6:
  117. read_lock(&dev_base_lock);
  118. for_each_netdev(&init_net, dev) {
  119. if (ipv6_chk_addr(&init_net,
  120. &((struct sockaddr_in6 *) addr)->sin6_addr,
  121. dev, 1)) {
  122. ret = rdma_copy_addr(dev_addr, dev, NULL);
  123. break;
  124. }
  125. }
  126. read_unlock(&dev_base_lock);
  127. break;
  128. #endif
  129. }
  130. return ret;
  131. }
  132. EXPORT_SYMBOL(rdma_translate_ip);
  133. static void set_timeout(unsigned long time)
  134. {
  135. unsigned long delay;
  136. cancel_delayed_work(&work);
  137. delay = time - jiffies;
  138. if ((long)delay <= 0)
  139. delay = 1;
  140. queue_delayed_work(addr_wq, &work, delay);
  141. }
  142. static void queue_req(struct addr_req *req)
  143. {
  144. struct addr_req *temp_req;
  145. mutex_lock(&lock);
  146. list_for_each_entry_reverse(temp_req, &req_list, list) {
  147. if (time_after_eq(req->timeout, temp_req->timeout))
  148. break;
  149. }
  150. list_add(&req->list, &temp_req->list);
  151. if (req_list.next == &req->list)
  152. set_timeout(req->timeout);
  153. mutex_unlock(&lock);
  154. }
  155. static void addr_send_arp(struct sockaddr *dst_in)
  156. {
  157. struct rtable *rt;
  158. struct flowi fl;
  159. memset(&fl, 0, sizeof fl);
  160. switch (dst_in->sa_family) {
  161. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  162. case AF_INET6:
  163. {
  164. struct dst_entry *dst;
  165. fl.nl_u.ip6_u.daddr =
  166. ((struct sockaddr_in6 *) dst_in)->sin6_addr;
  167. dst = ip6_route_output(&init_net, NULL, &fl);
  168. if (!dst)
  169. return;
  170. neigh_event_send(dst->neighbour, NULL);
  171. dst_release(dst);
  172. break;
  173. }
  174. #endif
  175. }
  176. }
  177. static int addr4_resolve(struct sockaddr_in *src_in,
  178. struct sockaddr_in *dst_in,
  179. struct rdma_dev_addr *addr)
  180. {
  181. __be32 src_ip = src_in->sin_addr.s_addr;
  182. __be32 dst_ip = dst_in->sin_addr.s_addr;
  183. struct flowi fl;
  184. struct rtable *rt;
  185. struct neighbour *neigh;
  186. int ret;
  187. memset(&fl, 0, sizeof fl);
  188. fl.nl_u.ip4_u.daddr = dst_ip;
  189. fl.nl_u.ip4_u.saddr = src_ip;
  190. fl.oif = addr->bound_dev_if;
  191. ret = ip_route_output_key(&init_net, &rt, &fl);
  192. if (ret)
  193. goto out;
  194. src_in->sin_family = AF_INET;
  195. src_in->sin_addr.s_addr = rt->rt_src;
  196. if (rt->idev->dev->flags & IFF_LOOPBACK) {
  197. ret = rdma_translate_ip((struct sockaddr *) dst_in, addr);
  198. if (!ret)
  199. memcpy(addr->dst_dev_addr, addr->src_dev_addr, MAX_ADDR_LEN);
  200. goto put;
  201. }
  202. /* If the device does ARP internally, return 'done' */
  203. if (rt->idev->dev->flags & IFF_NOARP) {
  204. rdma_copy_addr(addr, rt->idev->dev, NULL);
  205. goto put;
  206. }
  207. neigh = neigh_lookup(&arp_tbl, &rt->rt_gateway, rt->idev->dev);
  208. if (!neigh || !(neigh->nud_state & NUD_VALID)) {
  209. neigh_event_send(rt->u.dst.neighbour, NULL);
  210. ret = -ENODATA;
  211. if (neigh)
  212. goto release;
  213. goto put;
  214. }
  215. ret = rdma_copy_addr(addr, neigh->dev, neigh->ha);
  216. release:
  217. neigh_release(neigh);
  218. put:
  219. ip_rt_put(rt);
  220. out:
  221. return ret;
  222. }
  223. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  224. static int addr6_resolve_remote(struct sockaddr_in6 *src_in,
  225. struct sockaddr_in6 *dst_in,
  226. struct rdma_dev_addr *addr)
  227. {
  228. struct flowi fl;
  229. struct neighbour *neigh;
  230. struct dst_entry *dst;
  231. int ret = -ENODATA;
  232. memset(&fl, 0, sizeof fl);
  233. fl.nl_u.ip6_u.daddr = dst_in->sin6_addr;
  234. fl.nl_u.ip6_u.saddr = src_in->sin6_addr;
  235. fl.oif = addr->bound_dev_if;
  236. dst = ip6_route_output(&init_net, NULL, &fl);
  237. if (!dst)
  238. return ret;
  239. if (dst->dev->flags & IFF_NOARP) {
  240. ret = rdma_copy_addr(addr, dst->dev, NULL);
  241. } else {
  242. neigh = dst->neighbour;
  243. if (neigh && (neigh->nud_state & NUD_VALID))
  244. ret = rdma_copy_addr(addr, neigh->dev, neigh->ha);
  245. }
  246. dst_release(dst);
  247. return ret;
  248. }
  249. #else
  250. static int addr6_resolve_remote(struct sockaddr_in6 *src_in,
  251. struct sockaddr_in6 *dst_in,
  252. struct rdma_dev_addr *addr)
  253. {
  254. return -EADDRNOTAVAIL;
  255. }
  256. #endif
  257. static int addr_resolve(struct sockaddr *src_in,
  258. struct sockaddr *dst_in,
  259. struct rdma_dev_addr *addr)
  260. {
  261. if (src_in->sa_family == AF_INET) {
  262. return addr4_resolve((struct sockaddr_in *) src_in,
  263. (struct sockaddr_in *) dst_in, addr);
  264. } else
  265. return addr6_resolve_remote((struct sockaddr_in6 *) src_in,
  266. (struct sockaddr_in6 *) dst_in, addr);
  267. }
  268. static void process_req(struct work_struct *work)
  269. {
  270. struct addr_req *req, *temp_req;
  271. struct sockaddr *src_in, *dst_in;
  272. struct list_head done_list;
  273. INIT_LIST_HEAD(&done_list);
  274. mutex_lock(&lock);
  275. list_for_each_entry_safe(req, temp_req, &req_list, list) {
  276. if (req->status == -ENODATA) {
  277. src_in = (struct sockaddr *) &req->src_addr;
  278. dst_in = (struct sockaddr *) &req->dst_addr;
  279. req->status = addr_resolve(src_in, dst_in, req->addr);
  280. if (req->status && time_after_eq(jiffies, req->timeout))
  281. req->status = -ETIMEDOUT;
  282. else if (req->status == -ENODATA)
  283. continue;
  284. }
  285. list_move_tail(&req->list, &done_list);
  286. }
  287. if (!list_empty(&req_list)) {
  288. req = list_entry(req_list.next, struct addr_req, list);
  289. set_timeout(req->timeout);
  290. }
  291. mutex_unlock(&lock);
  292. list_for_each_entry_safe(req, temp_req, &done_list, list) {
  293. list_del(&req->list);
  294. req->callback(req->status, (struct sockaddr *) &req->src_addr,
  295. req->addr, req->context);
  296. put_client(req->client);
  297. kfree(req);
  298. }
  299. }
  300. static int addr_resolve_local(struct sockaddr *src_in,
  301. struct sockaddr *dst_in,
  302. struct rdma_dev_addr *addr)
  303. {
  304. struct net_device *dev;
  305. int ret;
  306. switch (dst_in->sa_family) {
  307. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  308. case AF_INET6:
  309. {
  310. struct in6_addr *a;
  311. read_lock(&dev_base_lock);
  312. for_each_netdev(&init_net, dev)
  313. if (ipv6_chk_addr(&init_net,
  314. &((struct sockaddr_in6 *) dst_in)->sin6_addr,
  315. dev, 1))
  316. break;
  317. if (!dev) {
  318. read_unlock(&dev_base_lock);
  319. return -EADDRNOTAVAIL;
  320. }
  321. a = &((struct sockaddr_in6 *) src_in)->sin6_addr;
  322. if (ipv6_addr_any(a)) {
  323. src_in->sa_family = dst_in->sa_family;
  324. ((struct sockaddr_in6 *) src_in)->sin6_addr =
  325. ((struct sockaddr_in6 *) dst_in)->sin6_addr;
  326. ret = rdma_copy_addr(addr, dev, dev->dev_addr);
  327. } else if (ipv6_addr_loopback(a)) {
  328. ret = rdma_translate_ip(dst_in, addr);
  329. if (!ret)
  330. memcpy(addr->dst_dev_addr, dev->dev_addr, MAX_ADDR_LEN);
  331. } else {
  332. ret = rdma_translate_ip(src_in, addr);
  333. if (!ret)
  334. memcpy(addr->dst_dev_addr, dev->dev_addr, MAX_ADDR_LEN);
  335. }
  336. read_unlock(&dev_base_lock);
  337. break;
  338. }
  339. #endif
  340. default:
  341. ret = -EADDRNOTAVAIL;
  342. break;
  343. }
  344. return ret;
  345. }
  346. int rdma_resolve_ip(struct rdma_addr_client *client,
  347. struct sockaddr *src_addr, struct sockaddr *dst_addr,
  348. struct rdma_dev_addr *addr, int timeout_ms,
  349. void (*callback)(int status, struct sockaddr *src_addr,
  350. struct rdma_dev_addr *addr, void *context),
  351. void *context)
  352. {
  353. struct sockaddr *src_in, *dst_in;
  354. struct addr_req *req;
  355. int ret = 0;
  356. req = kzalloc(sizeof *req, GFP_KERNEL);
  357. if (!req)
  358. return -ENOMEM;
  359. src_in = (struct sockaddr *) &req->src_addr;
  360. dst_in = (struct sockaddr *) &req->dst_addr;
  361. if (src_addr) {
  362. if (src_addr->sa_family != dst_addr->sa_family) {
  363. ret = -EINVAL;
  364. goto err;
  365. }
  366. memcpy(src_in, src_addr, ip_addr_size(src_addr));
  367. } else {
  368. src_in->sa_family = dst_addr->sa_family;
  369. }
  370. memcpy(dst_in, dst_addr, ip_addr_size(dst_addr));
  371. req->addr = addr;
  372. req->callback = callback;
  373. req->context = context;
  374. req->client = client;
  375. atomic_inc(&client->refcount);
  376. req->status = addr_resolve_local(src_in, dst_in, addr);
  377. if (req->status == -EADDRNOTAVAIL)
  378. req->status = addr_resolve(src_in, dst_in, addr);
  379. switch (req->status) {
  380. case 0:
  381. req->timeout = jiffies;
  382. queue_req(req);
  383. break;
  384. case -ENODATA:
  385. req->timeout = msecs_to_jiffies(timeout_ms) + jiffies;
  386. queue_req(req);
  387. addr_send_arp(dst_in);
  388. break;
  389. default:
  390. ret = req->status;
  391. atomic_dec(&client->refcount);
  392. goto err;
  393. }
  394. return ret;
  395. err:
  396. kfree(req);
  397. return ret;
  398. }
  399. EXPORT_SYMBOL(rdma_resolve_ip);
  400. void rdma_addr_cancel(struct rdma_dev_addr *addr)
  401. {
  402. struct addr_req *req, *temp_req;
  403. mutex_lock(&lock);
  404. list_for_each_entry_safe(req, temp_req, &req_list, list) {
  405. if (req->addr == addr) {
  406. req->status = -ECANCELED;
  407. req->timeout = jiffies;
  408. list_move(&req->list, &req_list);
  409. set_timeout(req->timeout);
  410. break;
  411. }
  412. }
  413. mutex_unlock(&lock);
  414. }
  415. EXPORT_SYMBOL(rdma_addr_cancel);
  416. static int netevent_callback(struct notifier_block *self, unsigned long event,
  417. void *ctx)
  418. {
  419. if (event == NETEVENT_NEIGH_UPDATE) {
  420. struct neighbour *neigh = ctx;
  421. if (neigh->nud_state & NUD_VALID) {
  422. set_timeout(jiffies);
  423. }
  424. }
  425. return 0;
  426. }
  427. static struct notifier_block nb = {
  428. .notifier_call = netevent_callback
  429. };
  430. static int __init addr_init(void)
  431. {
  432. addr_wq = create_singlethread_workqueue("ib_addr");
  433. if (!addr_wq)
  434. return -ENOMEM;
  435. register_netevent_notifier(&nb);
  436. return 0;
  437. }
  438. static void __exit addr_cleanup(void)
  439. {
  440. unregister_netevent_notifier(&nb);
  441. destroy_workqueue(addr_wq);
  442. }
  443. module_init(addr_init);
  444. module_exit(addr_cleanup);