sandbox.c 5.7 KB

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