sandbox.c 5.3 KB

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