sandbox.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2015 National Instruments
  4. *
  5. * (C) Copyright 2015
  6. * Joe Hershberger <joe.hershberger@ni.com>
  7. */
  8. #include <common.h>
  9. #include <dm.h>
  10. #include <malloc.h>
  11. #include <net.h>
  12. #include <asm/test.h>
  13. DECLARE_GLOBAL_DATA_PTR;
  14. /**
  15. * struct eth_sandbox_priv - memory for sandbox mock driver
  16. *
  17. * fake_host_hwaddr: MAC address of mocked machine
  18. * fake_host_ipaddr: IP address of mocked machine
  19. * disabled: Will not respond
  20. * recv_packet_buffer: buffer of the packet returned as received
  21. * recv_packet_length: length of the packet returned as received
  22. */
  23. struct eth_sandbox_priv {
  24. uchar fake_host_hwaddr[ARP_HLEN];
  25. struct in_addr fake_host_ipaddr;
  26. bool disabled;
  27. uchar *recv_packet_buffer;
  28. int recv_packet_length;
  29. };
  30. static bool skip_timeout;
  31. /*
  32. * sandbox_eth_disable_response()
  33. *
  34. * index - The alias index (also DM seq number)
  35. * disable - If non-zero, ignore sent packets and don't send mock response
  36. */
  37. void sandbox_eth_disable_response(int index, bool disable)
  38. {
  39. struct udevice *dev;
  40. struct eth_sandbox_priv *priv;
  41. int ret;
  42. ret = uclass_get_device(UCLASS_ETH, index, &dev);
  43. if (ret)
  44. return;
  45. priv = dev_get_priv(dev);
  46. priv->disabled = disable;
  47. }
  48. /*
  49. * sandbox_eth_skip_timeout()
  50. *
  51. * When the first packet read is attempted, fast-forward time
  52. */
  53. void sandbox_eth_skip_timeout(void)
  54. {
  55. skip_timeout = true;
  56. }
  57. static int sb_eth_start(struct udevice *dev)
  58. {
  59. struct eth_sandbox_priv *priv = dev_get_priv(dev);
  60. debug("eth_sandbox: Start\n");
  61. priv->recv_packet_buffer = net_rx_packets[0];
  62. return 0;
  63. }
  64. static int sb_eth_send(struct udevice *dev, void *packet, int length)
  65. {
  66. struct eth_sandbox_priv *priv = dev_get_priv(dev);
  67. struct ethernet_hdr *eth = packet;
  68. debug("eth_sandbox: Send packet %d\n", length);
  69. if (priv->disabled)
  70. return 0;
  71. if (ntohs(eth->et_protlen) == PROT_ARP) {
  72. struct arp_hdr *arp = packet + ETHER_HDR_SIZE;
  73. if (ntohs(arp->ar_op) == ARPOP_REQUEST) {
  74. struct ethernet_hdr *eth_recv;
  75. struct arp_hdr *arp_recv;
  76. /* store this as the assumed IP of the fake host */
  77. priv->fake_host_ipaddr = net_read_ip(&arp->ar_tpa);
  78. /* Formulate a fake response */
  79. eth_recv = (void *)priv->recv_packet_buffer;
  80. memcpy(eth_recv->et_dest, eth->et_src, ARP_HLEN);
  81. memcpy(eth_recv->et_src, priv->fake_host_hwaddr,
  82. ARP_HLEN);
  83. eth_recv->et_protlen = htons(PROT_ARP);
  84. arp_recv = (void *)priv->recv_packet_buffer +
  85. ETHER_HDR_SIZE;
  86. arp_recv->ar_hrd = htons(ARP_ETHER);
  87. arp_recv->ar_pro = htons(PROT_IP);
  88. arp_recv->ar_hln = ARP_HLEN;
  89. arp_recv->ar_pln = ARP_PLEN;
  90. arp_recv->ar_op = htons(ARPOP_REPLY);
  91. memcpy(&arp_recv->ar_sha, priv->fake_host_hwaddr,
  92. ARP_HLEN);
  93. net_write_ip(&arp_recv->ar_spa, priv->fake_host_ipaddr);
  94. memcpy(&arp_recv->ar_tha, &arp->ar_sha, ARP_HLEN);
  95. net_copy_ip(&arp_recv->ar_tpa, &arp->ar_spa);
  96. priv->recv_packet_length = ETHER_HDR_SIZE +
  97. ARP_HDR_SIZE;
  98. }
  99. } else if (ntohs(eth->et_protlen) == PROT_IP) {
  100. struct ip_udp_hdr *ip = packet + ETHER_HDR_SIZE;
  101. if (ip->ip_p == IPPROTO_ICMP) {
  102. struct icmp_hdr *icmp = (struct icmp_hdr *)&ip->udp_src;
  103. if (icmp->type == ICMP_ECHO_REQUEST) {
  104. struct ethernet_hdr *eth_recv;
  105. struct ip_udp_hdr *ipr;
  106. struct icmp_hdr *icmpr;
  107. /* reply to the ping */
  108. memcpy(priv->recv_packet_buffer, packet,
  109. length);
  110. eth_recv = (void *)priv->recv_packet_buffer;
  111. ipr = (void *)priv->recv_packet_buffer +
  112. ETHER_HDR_SIZE;
  113. icmpr = (struct icmp_hdr *)&ipr->udp_src;
  114. memcpy(eth_recv->et_dest, eth->et_src,
  115. ARP_HLEN);
  116. memcpy(eth_recv->et_src, priv->fake_host_hwaddr,
  117. ARP_HLEN);
  118. ipr->ip_sum = 0;
  119. ipr->ip_off = 0;
  120. net_copy_ip((void *)&ipr->ip_dst, &ip->ip_src);
  121. net_write_ip((void *)&ipr->ip_src,
  122. priv->fake_host_ipaddr);
  123. ipr->ip_sum = compute_ip_checksum(ipr,
  124. IP_HDR_SIZE);
  125. icmpr->type = ICMP_ECHO_REPLY;
  126. icmpr->checksum = 0;
  127. icmpr->checksum = compute_ip_checksum(icmpr,
  128. ICMP_HDR_SIZE);
  129. priv->recv_packet_length = length;
  130. }
  131. }
  132. }
  133. return 0;
  134. }
  135. static int sb_eth_recv(struct udevice *dev, int flags, uchar **packetp)
  136. {
  137. struct eth_sandbox_priv *priv = dev_get_priv(dev);
  138. if (skip_timeout) {
  139. sandbox_timer_add_offset(11000UL);
  140. skip_timeout = false;
  141. }
  142. if (priv->recv_packet_length) {
  143. int lcl_recv_packet_length = priv->recv_packet_length;
  144. debug("eth_sandbox: received packet %d\n",
  145. priv->recv_packet_length);
  146. priv->recv_packet_length = 0;
  147. *packetp = priv->recv_packet_buffer;
  148. return lcl_recv_packet_length;
  149. }
  150. return 0;
  151. }
  152. static void sb_eth_stop(struct udevice *dev)
  153. {
  154. debug("eth_sandbox: Stop\n");
  155. }
  156. static int sb_eth_write_hwaddr(struct udevice *dev)
  157. {
  158. struct eth_pdata *pdata = dev_get_platdata(dev);
  159. debug("eth_sandbox %s: Write HW ADDR - %pM\n", dev->name,
  160. pdata->enetaddr);
  161. return 0;
  162. }
  163. static const struct eth_ops sb_eth_ops = {
  164. .start = sb_eth_start,
  165. .send = sb_eth_send,
  166. .recv = sb_eth_recv,
  167. .stop = sb_eth_stop,
  168. .write_hwaddr = sb_eth_write_hwaddr,
  169. };
  170. static int sb_eth_remove(struct udevice *dev)
  171. {
  172. return 0;
  173. }
  174. static int sb_eth_ofdata_to_platdata(struct udevice *dev)
  175. {
  176. struct eth_pdata *pdata = dev_get_platdata(dev);
  177. struct eth_sandbox_priv *priv = dev_get_priv(dev);
  178. const u8 *mac;
  179. pdata->iobase = dev_read_addr(dev);
  180. mac = dev_read_u8_array_ptr(dev, "fake-host-hwaddr", ARP_HLEN);
  181. if (!mac) {
  182. printf("'fake-host-hwaddr' is missing from the DT\n");
  183. return -EINVAL;
  184. }
  185. memcpy(priv->fake_host_hwaddr, mac, ARP_HLEN);
  186. priv->disabled = false;
  187. return 0;
  188. }
  189. static const struct udevice_id sb_eth_ids[] = {
  190. { .compatible = "sandbox,eth" },
  191. { }
  192. };
  193. U_BOOT_DRIVER(eth_sandbox) = {
  194. .name = "eth_sandbox",
  195. .id = UCLASS_ETH,
  196. .of_match = sb_eth_ids,
  197. .ofdata_to_platdata = sb_eth_ofdata_to_platdata,
  198. .remove = sb_eth_remove,
  199. .ops = &sb_eth_ops,
  200. .priv_auto_alloc_size = sizeof(struct eth_sandbox_priv),
  201. .platdata_auto_alloc_size = sizeof(struct eth_pdata),
  202. };