sandbox.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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/eth.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. * disabled: Will not respond
  21. * recv_packet_buffer: buffer of the packet returned as received
  22. * recv_packet_length: length of the packet returned as received
  23. * tx_handler - function to generate responses to sent packets
  24. */
  25. struct eth_sandbox_priv {
  26. uchar fake_host_hwaddr[ARP_HLEN];
  27. struct in_addr fake_host_ipaddr;
  28. bool disabled;
  29. uchar *recv_packet_buffer;
  30. int recv_packet_length;
  31. sandbox_eth_tx_hand_f *tx_handler;
  32. };
  33. static bool skip_timeout;
  34. /*
  35. * sandbox_eth_disable_response()
  36. *
  37. * index - The alias index (also DM seq number)
  38. * disable - If non-zero, ignore sent packets and don't send mock response
  39. */
  40. void sandbox_eth_disable_response(int index, bool disable)
  41. {
  42. struct udevice *dev;
  43. struct eth_sandbox_priv *priv;
  44. int ret;
  45. ret = uclass_get_device(UCLASS_ETH, index, &dev);
  46. if (ret)
  47. return;
  48. priv = dev_get_priv(dev);
  49. priv->disabled = disable;
  50. }
  51. /*
  52. * sandbox_eth_skip_timeout()
  53. *
  54. * When the first packet read is attempted, fast-forward time
  55. */
  56. void sandbox_eth_skip_timeout(void)
  57. {
  58. skip_timeout = true;
  59. }
  60. /*
  61. * sandbox_eth_arp_req_to_reply()
  62. *
  63. * Check for an arp request to be sent. If so, inject a reply
  64. *
  65. * returns 0 if injected, -EAGAIN if not
  66. */
  67. int sandbox_eth_arp_req_to_reply(struct udevice *dev, void *packet,
  68. unsigned int len)
  69. {
  70. struct eth_sandbox_priv *priv = dev_get_priv(dev);
  71. struct ethernet_hdr *eth = packet;
  72. struct arp_hdr *arp;
  73. struct ethernet_hdr *eth_recv;
  74. struct arp_hdr *arp_recv;
  75. if (ntohs(eth->et_protlen) != PROT_ARP)
  76. return -EAGAIN;
  77. arp = packet + ETHER_HDR_SIZE;
  78. if (ntohs(arp->ar_op) != ARPOP_REQUEST)
  79. return -EAGAIN;
  80. /* store this as the assumed IP of the fake host */
  81. priv->fake_host_ipaddr = net_read_ip(&arp->ar_tpa);
  82. /* Formulate a fake response */
  83. eth_recv = (void *)priv->recv_packet_buffer;
  84. memcpy(eth_recv->et_dest, eth->et_src, ARP_HLEN);
  85. memcpy(eth_recv->et_src, priv->fake_host_hwaddr, ARP_HLEN);
  86. eth_recv->et_protlen = htons(PROT_ARP);
  87. arp_recv = (void *)eth_recv + ETHER_HDR_SIZE;
  88. arp_recv->ar_hrd = htons(ARP_ETHER);
  89. arp_recv->ar_pro = htons(PROT_IP);
  90. arp_recv->ar_hln = ARP_HLEN;
  91. arp_recv->ar_pln = ARP_PLEN;
  92. arp_recv->ar_op = htons(ARPOP_REPLY);
  93. memcpy(&arp_recv->ar_sha, priv->fake_host_hwaddr, ARP_HLEN);
  94. net_write_ip(&arp_recv->ar_spa, priv->fake_host_ipaddr);
  95. memcpy(&arp_recv->ar_tha, &arp->ar_sha, ARP_HLEN);
  96. net_copy_ip(&arp_recv->ar_tpa, &arp->ar_spa);
  97. priv->recv_packet_length = ETHER_HDR_SIZE + ARP_HDR_SIZE;
  98. return 0;
  99. }
  100. /*
  101. * sandbox_eth_ping_req_to_reply()
  102. *
  103. * Check for a ping request to be sent. If so, inject a reply
  104. *
  105. * returns 0 if injected, -EAGAIN if not
  106. */
  107. int sandbox_eth_ping_req_to_reply(struct udevice *dev, void *packet,
  108. unsigned int len)
  109. {
  110. struct eth_sandbox_priv *priv = dev_get_priv(dev);
  111. struct ethernet_hdr *eth = packet;
  112. struct ip_udp_hdr *ip;
  113. struct icmp_hdr *icmp;
  114. struct ethernet_hdr *eth_recv;
  115. struct ip_udp_hdr *ipr;
  116. struct icmp_hdr *icmpr;
  117. if (ntohs(eth->et_protlen) != PROT_IP)
  118. return -EAGAIN;
  119. ip = packet + ETHER_HDR_SIZE;
  120. if (ip->ip_p != IPPROTO_ICMP)
  121. return -EAGAIN;
  122. icmp = (struct icmp_hdr *)&ip->udp_src;
  123. if (icmp->type != ICMP_ECHO_REQUEST)
  124. return -EAGAIN;
  125. /* reply to the ping */
  126. eth_recv = (void *)priv->recv_packet_buffer;
  127. memcpy(eth_recv, packet, len);
  128. ipr = (void *)eth_recv + ETHER_HDR_SIZE;
  129. icmpr = (struct icmp_hdr *)&ipr->udp_src;
  130. memcpy(eth_recv->et_dest, eth->et_src, ARP_HLEN);
  131. memcpy(eth_recv->et_src, priv->fake_host_hwaddr, ARP_HLEN);
  132. ipr->ip_sum = 0;
  133. ipr->ip_off = 0;
  134. net_copy_ip((void *)&ipr->ip_dst, &ip->ip_src);
  135. net_write_ip((void *)&ipr->ip_src, priv->fake_host_ipaddr);
  136. ipr->ip_sum = compute_ip_checksum(ipr, IP_HDR_SIZE);
  137. icmpr->type = ICMP_ECHO_REPLY;
  138. icmpr->checksum = 0;
  139. icmpr->checksum = compute_ip_checksum(icmpr, ICMP_HDR_SIZE);
  140. priv->recv_packet_length = len;
  141. return 0;
  142. }
  143. /*
  144. * sb_default_handler()
  145. *
  146. * perform typical responses to simple ping
  147. *
  148. * dev - device pointer
  149. * pkt - "sent" packet buffer
  150. * len - length of packet
  151. */
  152. static int sb_default_handler(struct udevice *dev, void *packet,
  153. unsigned int len)
  154. {
  155. if (!sandbox_eth_arp_req_to_reply(dev, packet, len))
  156. return 0;
  157. if (!sandbox_eth_ping_req_to_reply(dev, packet, len))
  158. return 0;
  159. return 0;
  160. }
  161. /*
  162. * sandbox_eth_set_tx_handler()
  163. *
  164. * Set a custom response to a packet being sent through the sandbox eth test
  165. * driver
  166. *
  167. * index - interface to set the handler for
  168. * handler - The func ptr to call on send. If NULL, set to default handler
  169. */
  170. void sandbox_eth_set_tx_handler(int index, sandbox_eth_tx_hand_f *handler)
  171. {
  172. struct udevice *dev;
  173. struct eth_sandbox_priv *priv;
  174. int ret;
  175. ret = uclass_get_device(UCLASS_ETH, index, &dev);
  176. if (ret)
  177. return;
  178. priv = dev_get_priv(dev);
  179. if (handler)
  180. priv->tx_handler = handler;
  181. else
  182. priv->tx_handler = sb_default_handler;
  183. }
  184. static int sb_eth_start(struct udevice *dev)
  185. {
  186. struct eth_sandbox_priv *priv = dev_get_priv(dev);
  187. debug("eth_sandbox: Start\n");
  188. priv->recv_packet_buffer = net_rx_packets[0];
  189. return 0;
  190. }
  191. static int sb_eth_send(struct udevice *dev, void *packet, int length)
  192. {
  193. struct eth_sandbox_priv *priv = dev_get_priv(dev);
  194. debug("eth_sandbox: Send packet %d\n", length);
  195. if (priv->disabled)
  196. return 0;
  197. return priv->tx_handler(dev, packet, length);
  198. }
  199. static int sb_eth_recv(struct udevice *dev, int flags, uchar **packetp)
  200. {
  201. struct eth_sandbox_priv *priv = dev_get_priv(dev);
  202. if (skip_timeout) {
  203. sandbox_timer_add_offset(11000UL);
  204. skip_timeout = false;
  205. }
  206. if (priv->recv_packet_length) {
  207. int lcl_recv_packet_length = priv->recv_packet_length;
  208. debug("eth_sandbox: received packet %d\n",
  209. priv->recv_packet_length);
  210. priv->recv_packet_length = 0;
  211. *packetp = priv->recv_packet_buffer;
  212. return lcl_recv_packet_length;
  213. }
  214. return 0;
  215. }
  216. static void sb_eth_stop(struct udevice *dev)
  217. {
  218. debug("eth_sandbox: Stop\n");
  219. }
  220. static int sb_eth_write_hwaddr(struct udevice *dev)
  221. {
  222. struct eth_pdata *pdata = dev_get_platdata(dev);
  223. debug("eth_sandbox %s: Write HW ADDR - %pM\n", dev->name,
  224. pdata->enetaddr);
  225. return 0;
  226. }
  227. static const struct eth_ops sb_eth_ops = {
  228. .start = sb_eth_start,
  229. .send = sb_eth_send,
  230. .recv = sb_eth_recv,
  231. .stop = sb_eth_stop,
  232. .write_hwaddr = sb_eth_write_hwaddr,
  233. };
  234. static int sb_eth_remove(struct udevice *dev)
  235. {
  236. return 0;
  237. }
  238. static int sb_eth_ofdata_to_platdata(struct udevice *dev)
  239. {
  240. struct eth_pdata *pdata = dev_get_platdata(dev);
  241. struct eth_sandbox_priv *priv = dev_get_priv(dev);
  242. const u8 *mac;
  243. pdata->iobase = dev_read_addr(dev);
  244. mac = dev_read_u8_array_ptr(dev, "fake-host-hwaddr", ARP_HLEN);
  245. if (!mac) {
  246. printf("'fake-host-hwaddr' is missing from the DT\n");
  247. return -EINVAL;
  248. }
  249. memcpy(priv->fake_host_hwaddr, mac, ARP_HLEN);
  250. priv->disabled = false;
  251. priv->tx_handler = sb_default_handler;
  252. return 0;
  253. }
  254. static const struct udevice_id sb_eth_ids[] = {
  255. { .compatible = "sandbox,eth" },
  256. { }
  257. };
  258. U_BOOT_DRIVER(eth_sandbox) = {
  259. .name = "eth_sandbox",
  260. .id = UCLASS_ETH,
  261. .of_match = sb_eth_ids,
  262. .ofdata_to_platdata = sb_eth_ofdata_to_platdata,
  263. .remove = sb_eth_remove,
  264. .ops = &sb_eth_ops,
  265. .priv_auto_alloc_size = sizeof(struct eth_sandbox_priv),
  266. .platdata_auto_alloc_size = sizeof(struct eth_pdata),
  267. };