sandbox.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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, "fake-host-hwaddr",
  55. priv->fake_host_hwaddr, ARP_HLEN);
  56. priv->recv_packet_buffer = net_rx_packets[0];
  57. return 0;
  58. }
  59. static int sb_eth_send(struct udevice *dev, void *packet, int length)
  60. {
  61. struct eth_sandbox_priv *priv = dev_get_priv(dev);
  62. struct ethernet_hdr *eth = packet;
  63. debug("eth_sandbox: Send packet %d\n", length);
  64. if (dev->seq >= 0 && dev->seq < ARRAY_SIZE(disabled) &&
  65. disabled[dev->seq])
  66. return 0;
  67. if (ntohs(eth->et_protlen) == PROT_ARP) {
  68. struct arp_hdr *arp = packet + ETHER_HDR_SIZE;
  69. if (ntohs(arp->ar_op) == ARPOP_REQUEST) {
  70. struct ethernet_hdr *eth_recv;
  71. struct arp_hdr *arp_recv;
  72. /* store this as the assumed IP of the fake host */
  73. priv->fake_host_ipaddr = net_read_ip(&arp->ar_tpa);
  74. /* Formulate a fake response */
  75. eth_recv = (void *)priv->recv_packet_buffer;
  76. memcpy(eth_recv->et_dest, eth->et_src, ARP_HLEN);
  77. memcpy(eth_recv->et_src, priv->fake_host_hwaddr,
  78. ARP_HLEN);
  79. eth_recv->et_protlen = htons(PROT_ARP);
  80. arp_recv = (void *)priv->recv_packet_buffer +
  81. ETHER_HDR_SIZE;
  82. arp_recv->ar_hrd = htons(ARP_ETHER);
  83. arp_recv->ar_pro = htons(PROT_IP);
  84. arp_recv->ar_hln = ARP_HLEN;
  85. arp_recv->ar_pln = ARP_PLEN;
  86. arp_recv->ar_op = htons(ARPOP_REPLY);
  87. memcpy(&arp_recv->ar_sha, priv->fake_host_hwaddr,
  88. ARP_HLEN);
  89. net_write_ip(&arp_recv->ar_spa, priv->fake_host_ipaddr);
  90. memcpy(&arp_recv->ar_tha, &arp->ar_sha, ARP_HLEN);
  91. net_copy_ip(&arp_recv->ar_tpa, &arp->ar_spa);
  92. priv->recv_packet_length = ETHER_HDR_SIZE +
  93. ARP_HDR_SIZE;
  94. }
  95. } else if (ntohs(eth->et_protlen) == PROT_IP) {
  96. struct ip_udp_hdr *ip = packet + ETHER_HDR_SIZE;
  97. if (ip->ip_p == IPPROTO_ICMP) {
  98. struct icmp_hdr *icmp = (struct icmp_hdr *)&ip->udp_src;
  99. if (icmp->type == ICMP_ECHO_REQUEST) {
  100. struct ethernet_hdr *eth_recv;
  101. struct ip_udp_hdr *ipr;
  102. struct icmp_hdr *icmpr;
  103. /* reply to the ping */
  104. memcpy(priv->recv_packet_buffer, packet,
  105. length);
  106. eth_recv = (void *)priv->recv_packet_buffer;
  107. ipr = (void *)priv->recv_packet_buffer +
  108. ETHER_HDR_SIZE;
  109. icmpr = (struct icmp_hdr *)&ipr->udp_src;
  110. memcpy(eth_recv->et_dest, eth->et_src,
  111. ARP_HLEN);
  112. memcpy(eth_recv->et_src, priv->fake_host_hwaddr,
  113. ARP_HLEN);
  114. ipr->ip_sum = 0;
  115. ipr->ip_off = 0;
  116. net_copy_ip((void *)&ipr->ip_dst, &ip->ip_src);
  117. net_write_ip((void *)&ipr->ip_src,
  118. priv->fake_host_ipaddr);
  119. ipr->ip_sum = compute_ip_checksum(ipr,
  120. IP_HDR_SIZE);
  121. icmpr->type = ICMP_ECHO_REPLY;
  122. icmpr->checksum = 0;
  123. icmpr->checksum = compute_ip_checksum(icmpr,
  124. ICMP_HDR_SIZE);
  125. priv->recv_packet_length = length;
  126. }
  127. }
  128. }
  129. return 0;
  130. }
  131. static int sb_eth_recv(struct udevice *dev, int flags, uchar **packetp)
  132. {
  133. struct eth_sandbox_priv *priv = dev_get_priv(dev);
  134. if (skip_timeout) {
  135. sandbox_timer_add_offset(11000UL);
  136. skip_timeout = false;
  137. }
  138. if (priv->recv_packet_length) {
  139. int lcl_recv_packet_length = priv->recv_packet_length;
  140. debug("eth_sandbox: received packet %d\n",
  141. priv->recv_packet_length);
  142. priv->recv_packet_length = 0;
  143. *packetp = priv->recv_packet_buffer;
  144. return lcl_recv_packet_length;
  145. }
  146. return 0;
  147. }
  148. static void sb_eth_stop(struct udevice *dev)
  149. {
  150. debug("eth_sandbox: Stop\n");
  151. }
  152. static int sb_eth_write_hwaddr(struct udevice *dev)
  153. {
  154. struct eth_pdata *pdata = dev_get_platdata(dev);
  155. debug("eth_sandbox %s: Write HW ADDR - %pM\n", dev->name,
  156. pdata->enetaddr);
  157. return 0;
  158. }
  159. static const struct eth_ops sb_eth_ops = {
  160. .start = sb_eth_start,
  161. .send = sb_eth_send,
  162. .recv = sb_eth_recv,
  163. .stop = sb_eth_stop,
  164. .write_hwaddr = sb_eth_write_hwaddr,
  165. };
  166. static int sb_eth_remove(struct udevice *dev)
  167. {
  168. return 0;
  169. }
  170. static int sb_eth_ofdata_to_platdata(struct udevice *dev)
  171. {
  172. struct eth_pdata *pdata = dev_get_platdata(dev);
  173. pdata->iobase = dev_get_addr(dev);
  174. return 0;
  175. }
  176. static const struct udevice_id sb_eth_ids[] = {
  177. { .compatible = "sandbox,eth" },
  178. { }
  179. };
  180. U_BOOT_DRIVER(eth_sandbox) = {
  181. .name = "eth_sandbox",
  182. .id = UCLASS_ETH,
  183. .of_match = sb_eth_ids,
  184. .ofdata_to_platdata = sb_eth_ofdata_to_platdata,
  185. .remove = sb_eth_remove,
  186. .ops = &sb_eth_ops,
  187. .priv_auto_alloc_size = sizeof(struct eth_sandbox_priv),
  188. .platdata_auto_alloc_size = sizeof(struct eth_pdata),
  189. };