arp.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /* $OpenBSD: arp.c,v 1.12 2014/07/13 15:31:20 mpi Exp $ */
  2. /* $NetBSD: arp.c,v 1.15 1996/10/13 02:28:58 christos Exp $ */
  3. /*
  4. * Copyright (c) 1992 Regents of the University of California.
  5. * All rights reserved.
  6. *
  7. * This software was developed by the Computer Systems Engineering group
  8. * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
  9. * contributed to Berkeley.
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions
  13. * are met:
  14. * 1. Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. * 3. All advertising materials mentioning features or use of this software
  20. * must display the following acknowledgement:
  21. * This product includes software developed by the University of
  22. * California, Lawrence Berkeley Laboratory and its contributors.
  23. * 4. Neither the name of the University nor the names of its contributors
  24. * may be used to endorse or promote products derived from this software
  25. * without specific prior written permission.
  26. *
  27. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  28. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  29. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  30. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  31. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  32. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  33. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  34. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  35. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  36. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  37. * SUCH DAMAGE.
  38. *
  39. * @(#) Header: arp.c,v 1.5 93/07/15 05:52:26 leres Exp (LBL)
  40. */
  41. #include <sys/types.h>
  42. #include <sys/socket.h>
  43. #include <net/if.h>
  44. #include <netinet/in.h>
  45. #include <netinet/if_ether.h>
  46. #include "stand.h"
  47. #include "net.h"
  48. /* Cache stuff */
  49. #define ARP_NUM 8 /* need at most 3 arp entries */
  50. struct arp_list {
  51. struct in_addr addr;
  52. u_char ea[6];
  53. } arp_list[ARP_NUM] = {
  54. /* XXX - net order `INADDR_BROADCAST' must be a constant */
  55. { {0xffffffff}, BA }
  56. };
  57. int arp_num = 1;
  58. /* Local forwards */
  59. static ssize_t arpsend(struct iodesc *, void *, size_t);
  60. static ssize_t arprecv(struct iodesc *, void *, size_t, time_t);
  61. /* Broadcast an ARP packet, asking who has addr on interface d */
  62. u_char *
  63. arpwhohas(struct iodesc *d, struct in_addr addr)
  64. {
  65. int i;
  66. struct ether_arp *ah;
  67. struct arp_list *al;
  68. struct {
  69. struct ether_header eh;
  70. struct {
  71. struct ether_arp arp;
  72. u_char pad[18]; /* 60 - sizeof(...) */
  73. } data;
  74. } wbuf;
  75. struct {
  76. struct ether_header eh;
  77. struct {
  78. struct ether_arp arp;
  79. u_char pad[24]; /* extra space */
  80. } data;
  81. } rbuf;
  82. /* Try for cached answer first */
  83. for (i = 0, al = arp_list; i < arp_num; ++i, ++al)
  84. if (addr.s_addr == al->addr.s_addr)
  85. return (al->ea);
  86. /* Don't overflow cache */
  87. if (arp_num > ARP_NUM - 1) {
  88. arp_num = 1; /* recycle */
  89. printf("arpwhohas: overflowed arp_list!\n");
  90. }
  91. #ifdef ARP_DEBUG
  92. if (debug)
  93. printf("arpwhohas: send request for %s\n", inet_ntoa(addr));
  94. #endif
  95. bzero((char *)&wbuf.data, sizeof(wbuf.data));
  96. ah = &wbuf.data.arp;
  97. ah->arp_hrd = htons(ARPHRD_ETHER);
  98. ah->arp_pro = htons(ETHERTYPE_IP);
  99. ah->arp_hln = sizeof(ah->arp_sha); /* hardware address length */
  100. ah->arp_pln = sizeof(ah->arp_spa); /* protocol address length */
  101. ah->arp_op = htons(ARPOP_REQUEST);
  102. MACPY(d->myea, ah->arp_sha);
  103. bcopy(&d->myip, ah->arp_spa, sizeof(ah->arp_spa));
  104. /* Leave zeros in arp_tha */
  105. bcopy(&addr, ah->arp_tpa, sizeof(ah->arp_tpa));
  106. /* Store ip address in cache (incomplete entry). */
  107. al->addr = addr;
  108. i = sendrecv(d,
  109. arpsend, &wbuf.data, sizeof(wbuf.data),
  110. arprecv, &rbuf.data, sizeof(rbuf.data));
  111. if (i == -1) {
  112. panic("arp: no response for %s", inet_ntoa(addr));
  113. }
  114. /* Store ethernet address in cache */
  115. ah = &rbuf.data.arp;
  116. #ifdef ARP_DEBUG
  117. if (debug) {
  118. printf("arp: response from %s\n",
  119. ether_sprintf(rbuf.eh.ether_shost));
  120. printf("arp: cacheing %s --> %s\n",
  121. inet_ntoa(addr), ether_sprintf(ah->arp_sha));
  122. }
  123. #endif
  124. MACPY(ah->arp_sha, al->ea);
  125. ++arp_num;
  126. return (al->ea);
  127. }
  128. static ssize_t
  129. arpsend(struct iodesc *d, void *pkt, size_t len)
  130. {
  131. #ifdef ARP_DEBUG
  132. if (debug)
  133. printf("arpsend: called\n");
  134. #endif
  135. return (sendether(d, pkt, len, bcea, ETHERTYPE_ARP));
  136. }
  137. /*
  138. * Returns 0 if this is the packet we're waiting for
  139. * else -1 (and errno == 0)
  140. */
  141. static ssize_t
  142. arprecv(struct iodesc *d, void *pkt, size_t len, time_t tleft)
  143. {
  144. ssize_t n;
  145. struct ether_arp *ah;
  146. u_int16_t etype; /* host order */
  147. #ifdef ARP_DEBUG
  148. if (debug)
  149. printf("arprecv: ");
  150. #endif
  151. n = readether(d, pkt, len, tleft, &etype);
  152. errno = 0; /* XXX */
  153. if (n < 0 || (size_t)n < sizeof(struct ether_arp)) {
  154. #ifdef ARP_DEBUG
  155. if (debug)
  156. printf("bad len=%d\n", n);
  157. #endif
  158. return (-1);
  159. }
  160. if (etype != ETHERTYPE_ARP) {
  161. #ifdef ARP_DEBUG
  162. if (debug)
  163. printf("not arp type=%d\n", etype);
  164. #endif
  165. return (-1);
  166. }
  167. /* Ethernet address now checked in readether() */
  168. ah = (struct ether_arp *)pkt;
  169. if (ah->arp_hrd != htons(ARPHRD_ETHER) ||
  170. ah->arp_pro != htons(ETHERTYPE_IP) ||
  171. ah->arp_hln != sizeof(ah->arp_sha) ||
  172. ah->arp_pln != sizeof(ah->arp_spa)) {
  173. #ifdef ARP_DEBUG
  174. if (debug)
  175. printf("bad hrd/pro/hln/pln\n");
  176. #endif
  177. return (-1);
  178. }
  179. if (ah->arp_op == htons(ARPOP_REQUEST)) {
  180. #ifdef ARP_DEBUG
  181. if (debug)
  182. printf("is request\n");
  183. #endif
  184. arp_reply(d, ah);
  185. return (-1);
  186. }
  187. if (ah->arp_op != htons(ARPOP_REPLY)) {
  188. #ifdef ARP_DEBUG
  189. if (debug)
  190. printf("not ARP reply\n");
  191. #endif
  192. return (-1);
  193. }
  194. /* Is the reply from the source we want? */
  195. if (bcmp(&arp_list[arp_num].addr, ah->arp_spa,
  196. sizeof(ah->arp_spa))) {
  197. #ifdef ARP_DEBUG
  198. if (debug)
  199. printf("unwanted address\n");
  200. #endif
  201. return (-1);
  202. }
  203. /* We don't care who the reply was sent to. */
  204. /* We have our answer. */
  205. #ifdef ARP_DEBUG
  206. if (debug)
  207. printf("got it\n");
  208. #endif
  209. return (n);
  210. }
  211. /*
  212. * Convert an ARP request into a reply and send it.
  213. * Notes: Re-uses buffer. Pad to length = 46.
  214. */
  215. void
  216. arp_reply(struct iodesc *d, void *pkt)
  217. {
  218. struct ether_arp *arp = pkt;
  219. if (arp->arp_hrd != htons(ARPHRD_ETHER) ||
  220. arp->arp_pro != htons(ETHERTYPE_IP) ||
  221. arp->arp_hln != sizeof(arp->arp_sha) ||
  222. arp->arp_pln != sizeof(arp->arp_spa)) {
  223. #ifdef ARP_DEBUG
  224. if (debug)
  225. printf("arp_reply: bad hrd/pro/hln/pln\n");
  226. #endif
  227. return;
  228. }
  229. if (arp->arp_op != htons(ARPOP_REQUEST)) {
  230. #ifdef ARP_DEBUG
  231. if (debug)
  232. printf("arp_reply: not request!\n");
  233. #endif
  234. return;
  235. }
  236. /* If we are not the target, ignore the request. */
  237. if (bcmp(arp->arp_tpa, &d->myip, sizeof(arp->arp_tpa)))
  238. return;
  239. #ifdef ARP_DEBUG
  240. if (debug) {
  241. printf("arp_reply: to %s\n", ether_sprintf(arp->arp_sha));
  242. }
  243. #endif
  244. arp->arp_op = htons(ARPOP_REPLY);
  245. /* source becomes target */
  246. bcopy(arp->arp_sha, arp->arp_tha, sizeof(arp->arp_tha));
  247. bcopy(arp->arp_spa, arp->arp_tpa, sizeof(arp->arp_tpa));
  248. /* here becomes source */
  249. bcopy(d->myea, arp->arp_sha, sizeof(arp->arp_sha));
  250. bcopy(&d->myip, arp->arp_spa, sizeof(arp->arp_spa));
  251. /*
  252. * No need to get fancy here. If the send fails, the
  253. * requestor will just ask again.
  254. */
  255. (void) sendether(d, pkt, sizeof(*arp) + 18,
  256. arp->arp_tha, ETHERTYPE_ARP);
  257. }