sandbox.c 5.6 KB

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