sandbox.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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. static bool skip_timeout;
  16. /*
  17. * sandbox_eth_disable_response()
  18. *
  19. * index - The alias index (also DM seq number)
  20. * disable - If non-zero, ignore sent packets and don't send mock response
  21. */
  22. void sandbox_eth_disable_response(int index, bool disable)
  23. {
  24. struct udevice *dev;
  25. struct eth_sandbox_priv *priv;
  26. int ret;
  27. ret = uclass_get_device(UCLASS_ETH, index, &dev);
  28. if (ret)
  29. return;
  30. priv = dev_get_priv(dev);
  31. priv->disabled = disable;
  32. }
  33. /*
  34. * sandbox_eth_skip_timeout()
  35. *
  36. * When the first packet read is attempted, fast-forward time
  37. */
  38. void sandbox_eth_skip_timeout(void)
  39. {
  40. skip_timeout = true;
  41. }
  42. /*
  43. * sandbox_eth_arp_req_to_reply()
  44. *
  45. * Check for an arp request to be sent. If so, inject a reply
  46. *
  47. * returns 0 if injected, -EAGAIN if not
  48. */
  49. int sandbox_eth_arp_req_to_reply(struct udevice *dev, void *packet,
  50. unsigned int len)
  51. {
  52. struct eth_sandbox_priv *priv = dev_get_priv(dev);
  53. struct ethernet_hdr *eth = packet;
  54. struct arp_hdr *arp;
  55. struct ethernet_hdr *eth_recv;
  56. struct arp_hdr *arp_recv;
  57. if (ntohs(eth->et_protlen) != PROT_ARP)
  58. return -EAGAIN;
  59. arp = packet + ETHER_HDR_SIZE;
  60. if (ntohs(arp->ar_op) != ARPOP_REQUEST)
  61. return -EAGAIN;
  62. /* Don't allow the buffer to overrun */
  63. if (priv->recv_packets >= PKTBUFSRX)
  64. return 0;
  65. /* store this as the assumed IP of the fake host */
  66. priv->fake_host_ipaddr = net_read_ip(&arp->ar_tpa);
  67. /* Formulate a fake response */
  68. eth_recv = (void *)priv->recv_packet_buffer[priv->recv_packets];
  69. memcpy(eth_recv->et_dest, eth->et_src, ARP_HLEN);
  70. memcpy(eth_recv->et_src, priv->fake_host_hwaddr, ARP_HLEN);
  71. eth_recv->et_protlen = htons(PROT_ARP);
  72. arp_recv = (void *)eth_recv + ETHER_HDR_SIZE;
  73. arp_recv->ar_hrd = htons(ARP_ETHER);
  74. arp_recv->ar_pro = htons(PROT_IP);
  75. arp_recv->ar_hln = ARP_HLEN;
  76. arp_recv->ar_pln = ARP_PLEN;
  77. arp_recv->ar_op = htons(ARPOP_REPLY);
  78. memcpy(&arp_recv->ar_sha, priv->fake_host_hwaddr, ARP_HLEN);
  79. net_write_ip(&arp_recv->ar_spa, priv->fake_host_ipaddr);
  80. memcpy(&arp_recv->ar_tha, &arp->ar_sha, ARP_HLEN);
  81. net_copy_ip(&arp_recv->ar_tpa, &arp->ar_spa);
  82. priv->recv_packet_length[priv->recv_packets] =
  83. ETHER_HDR_SIZE + ARP_HDR_SIZE;
  84. ++priv->recv_packets;
  85. return 0;
  86. }
  87. /*
  88. * sandbox_eth_ping_req_to_reply()
  89. *
  90. * Check for a ping request to be sent. If so, inject a reply
  91. *
  92. * returns 0 if injected, -EAGAIN if not
  93. */
  94. int sandbox_eth_ping_req_to_reply(struct udevice *dev, void *packet,
  95. unsigned int len)
  96. {
  97. struct eth_sandbox_priv *priv = dev_get_priv(dev);
  98. struct ethernet_hdr *eth = packet;
  99. struct ip_udp_hdr *ip;
  100. struct icmp_hdr *icmp;
  101. struct ethernet_hdr *eth_recv;
  102. struct ip_udp_hdr *ipr;
  103. struct icmp_hdr *icmpr;
  104. if (ntohs(eth->et_protlen) != PROT_IP)
  105. return -EAGAIN;
  106. ip = packet + ETHER_HDR_SIZE;
  107. if (ip->ip_p != IPPROTO_ICMP)
  108. return -EAGAIN;
  109. icmp = (struct icmp_hdr *)&ip->udp_src;
  110. if (icmp->type != ICMP_ECHO_REQUEST)
  111. return -EAGAIN;
  112. /* Don't allow the buffer to overrun */
  113. if (priv->recv_packets >= PKTBUFSRX)
  114. return 0;
  115. /* reply to the ping */
  116. eth_recv = (void *)priv->recv_packet_buffer[priv->recv_packets];
  117. memcpy(eth_recv, packet, len);
  118. ipr = (void *)eth_recv + ETHER_HDR_SIZE;
  119. icmpr = (struct icmp_hdr *)&ipr->udp_src;
  120. memcpy(eth_recv->et_dest, eth->et_src, ARP_HLEN);
  121. memcpy(eth_recv->et_src, priv->fake_host_hwaddr, ARP_HLEN);
  122. ipr->ip_sum = 0;
  123. ipr->ip_off = 0;
  124. net_copy_ip((void *)&ipr->ip_dst, &ip->ip_src);
  125. net_write_ip((void *)&ipr->ip_src, priv->fake_host_ipaddr);
  126. ipr->ip_sum = compute_ip_checksum(ipr, IP_HDR_SIZE);
  127. icmpr->type = ICMP_ECHO_REPLY;
  128. icmpr->checksum = 0;
  129. icmpr->checksum = compute_ip_checksum(icmpr, ICMP_HDR_SIZE);
  130. priv->recv_packet_length[priv->recv_packets] = len;
  131. ++priv->recv_packets;
  132. return 0;
  133. }
  134. /*
  135. * sandbox_eth_recv_arp_req()
  136. *
  137. * Inject an ARP request for this target
  138. *
  139. * returns 0 if injected, -EOVERFLOW if not
  140. */
  141. int sandbox_eth_recv_arp_req(struct udevice *dev)
  142. {
  143. struct eth_sandbox_priv *priv = dev_get_priv(dev);
  144. struct ethernet_hdr *eth_recv;
  145. struct arp_hdr *arp_recv;
  146. /* Don't allow the buffer to overrun */
  147. if (priv->recv_packets >= PKTBUFSRX)
  148. return -EOVERFLOW;
  149. /* Formulate a fake request */
  150. eth_recv = (void *)priv->recv_packet_buffer[priv->recv_packets];
  151. memcpy(eth_recv->et_dest, net_bcast_ethaddr, ARP_HLEN);
  152. memcpy(eth_recv->et_src, priv->fake_host_hwaddr, ARP_HLEN);
  153. eth_recv->et_protlen = htons(PROT_ARP);
  154. arp_recv = (void *)eth_recv + ETHER_HDR_SIZE;
  155. arp_recv->ar_hrd = htons(ARP_ETHER);
  156. arp_recv->ar_pro = htons(PROT_IP);
  157. arp_recv->ar_hln = ARP_HLEN;
  158. arp_recv->ar_pln = ARP_PLEN;
  159. arp_recv->ar_op = htons(ARPOP_REQUEST);
  160. memcpy(&arp_recv->ar_sha, priv->fake_host_hwaddr, ARP_HLEN);
  161. net_write_ip(&arp_recv->ar_spa, priv->fake_host_ipaddr);
  162. memcpy(&arp_recv->ar_tha, net_null_ethaddr, ARP_HLEN);
  163. net_write_ip(&arp_recv->ar_tpa, net_ip);
  164. priv->recv_packet_length[priv->recv_packets] =
  165. ETHER_HDR_SIZE + ARP_HDR_SIZE;
  166. ++priv->recv_packets;
  167. return 0;
  168. }
  169. /*
  170. * sandbox_eth_recv_ping_req()
  171. *
  172. * Inject a ping request for this target
  173. *
  174. * returns 0 if injected, -EOVERFLOW if not
  175. */
  176. int sandbox_eth_recv_ping_req(struct udevice *dev)
  177. {
  178. struct eth_sandbox_priv *priv = dev_get_priv(dev);
  179. struct ethernet_hdr *eth_recv;
  180. struct ip_udp_hdr *ipr;
  181. struct icmp_hdr *icmpr;
  182. /* Don't allow the buffer to overrun */
  183. if (priv->recv_packets >= PKTBUFSRX)
  184. return -EOVERFLOW;
  185. /* Formulate a fake ping */
  186. eth_recv = (void *)priv->recv_packet_buffer[priv->recv_packets];
  187. memcpy(eth_recv->et_dest, net_ethaddr, ARP_HLEN);
  188. memcpy(eth_recv->et_src, priv->fake_host_hwaddr, ARP_HLEN);
  189. eth_recv->et_protlen = htons(PROT_IP);
  190. ipr = (void *)eth_recv + ETHER_HDR_SIZE;
  191. ipr->ip_hl_v = 0x45;
  192. ipr->ip_len = htons(IP_ICMP_HDR_SIZE);
  193. ipr->ip_off = htons(IP_FLAGS_DFRAG);
  194. ipr->ip_p = IPPROTO_ICMP;
  195. ipr->ip_sum = 0;
  196. net_write_ip(&ipr->ip_src, priv->fake_host_ipaddr);
  197. net_write_ip(&ipr->ip_dst, net_ip);
  198. ipr->ip_sum = compute_ip_checksum(ipr, IP_HDR_SIZE);
  199. icmpr = (struct icmp_hdr *)&ipr->udp_src;
  200. icmpr->type = ICMP_ECHO_REQUEST;
  201. icmpr->code = 0;
  202. icmpr->checksum = 0;
  203. icmpr->un.echo.id = 0;
  204. icmpr->un.echo.sequence = htons(1);
  205. icmpr->checksum = compute_ip_checksum(icmpr, ICMP_HDR_SIZE);
  206. priv->recv_packet_length[priv->recv_packets] =
  207. ETHER_HDR_SIZE + IP_ICMP_HDR_SIZE;
  208. ++priv->recv_packets;
  209. return 0;
  210. }
  211. /*
  212. * sb_default_handler()
  213. *
  214. * perform typical responses to simple ping
  215. *
  216. * dev - device pointer
  217. * pkt - "sent" packet buffer
  218. * len - length of packet
  219. */
  220. static int sb_default_handler(struct udevice *dev, void *packet,
  221. unsigned int len)
  222. {
  223. if (!sandbox_eth_arp_req_to_reply(dev, packet, len))
  224. return 0;
  225. if (!sandbox_eth_ping_req_to_reply(dev, packet, len))
  226. return 0;
  227. return 0;
  228. }
  229. /*
  230. * sandbox_eth_set_tx_handler()
  231. *
  232. * Set a custom response to a packet being sent through the sandbox eth test
  233. * driver
  234. *
  235. * index - interface to set the handler for
  236. * handler - The func ptr to call on send. If NULL, set to default handler
  237. */
  238. void sandbox_eth_set_tx_handler(int index, sandbox_eth_tx_hand_f *handler)
  239. {
  240. struct udevice *dev;
  241. struct eth_sandbox_priv *priv;
  242. int ret;
  243. ret = uclass_get_device(UCLASS_ETH, index, &dev);
  244. if (ret)
  245. return;
  246. priv = dev_get_priv(dev);
  247. if (handler)
  248. priv->tx_handler = handler;
  249. else
  250. priv->tx_handler = sb_default_handler;
  251. }
  252. /*
  253. * Set priv ptr
  254. *
  255. * priv - priv void ptr to store in the device
  256. */
  257. void sandbox_eth_set_priv(int index, void *priv)
  258. {
  259. struct udevice *dev;
  260. struct eth_sandbox_priv *dev_priv;
  261. int ret;
  262. ret = uclass_get_device(UCLASS_ETH, index, &dev);
  263. if (ret)
  264. return;
  265. dev_priv = dev_get_priv(dev);
  266. dev_priv->priv = priv;
  267. }
  268. static int sb_eth_start(struct udevice *dev)
  269. {
  270. struct eth_sandbox_priv *priv = dev_get_priv(dev);
  271. debug("eth_sandbox: Start\n");
  272. priv->recv_packets = 0;
  273. for (int i = 0; i < PKTBUFSRX; i++) {
  274. priv->recv_packet_buffer[i] = net_rx_packets[i];
  275. priv->recv_packet_length[i] = 0;
  276. }
  277. return 0;
  278. }
  279. static int sb_eth_send(struct udevice *dev, void *packet, int length)
  280. {
  281. struct eth_sandbox_priv *priv = dev_get_priv(dev);
  282. debug("eth_sandbox: Send packet %d\n", length);
  283. if (priv->disabled)
  284. return 0;
  285. return priv->tx_handler(dev, packet, length);
  286. }
  287. static int sb_eth_recv(struct udevice *dev, int flags, uchar **packetp)
  288. {
  289. struct eth_sandbox_priv *priv = dev_get_priv(dev);
  290. if (skip_timeout) {
  291. sandbox_timer_add_offset(11000UL);
  292. skip_timeout = false;
  293. }
  294. if (priv->recv_packets) {
  295. int lcl_recv_packet_length = priv->recv_packet_length[0];
  296. debug("eth_sandbox: received packet[%d], %d waiting\n",
  297. lcl_recv_packet_length, priv->recv_packets - 1);
  298. *packetp = priv->recv_packet_buffer[0];
  299. return lcl_recv_packet_length;
  300. }
  301. return 0;
  302. }
  303. static int sb_eth_free_pkt(struct udevice *dev, uchar *packet, int length)
  304. {
  305. struct eth_sandbox_priv *priv = dev_get_priv(dev);
  306. int i;
  307. if (!priv->recv_packets)
  308. return 0;
  309. --priv->recv_packets;
  310. for (i = 0; i < priv->recv_packets; i++) {
  311. priv->recv_packet_length[i] = priv->recv_packet_length[i + 1];
  312. memcpy(priv->recv_packet_buffer[i],
  313. priv->recv_packet_buffer[i + 1],
  314. priv->recv_packet_length[i + 1]);
  315. }
  316. priv->recv_packet_length[priv->recv_packets] = 0;
  317. return 0;
  318. }
  319. static void sb_eth_stop(struct udevice *dev)
  320. {
  321. debug("eth_sandbox: Stop\n");
  322. }
  323. static int sb_eth_write_hwaddr(struct udevice *dev)
  324. {
  325. struct eth_pdata *pdata = dev_get_platdata(dev);
  326. debug("eth_sandbox %s: Write HW ADDR - %pM\n", dev->name,
  327. pdata->enetaddr);
  328. return 0;
  329. }
  330. static const struct eth_ops sb_eth_ops = {
  331. .start = sb_eth_start,
  332. .send = sb_eth_send,
  333. .recv = sb_eth_recv,
  334. .free_pkt = sb_eth_free_pkt,
  335. .stop = sb_eth_stop,
  336. .write_hwaddr = sb_eth_write_hwaddr,
  337. };
  338. static int sb_eth_remove(struct udevice *dev)
  339. {
  340. return 0;
  341. }
  342. static int sb_eth_ofdata_to_platdata(struct udevice *dev)
  343. {
  344. struct eth_pdata *pdata = dev_get_platdata(dev);
  345. struct eth_sandbox_priv *priv = dev_get_priv(dev);
  346. const u8 *mac;
  347. pdata->iobase = dev_read_addr(dev);
  348. mac = dev_read_u8_array_ptr(dev, "fake-host-hwaddr", ARP_HLEN);
  349. if (!mac) {
  350. printf("'fake-host-hwaddr' is missing from the DT\n");
  351. return -EINVAL;
  352. }
  353. memcpy(priv->fake_host_hwaddr, mac, ARP_HLEN);
  354. priv->disabled = false;
  355. priv->tx_handler = sb_default_handler;
  356. return 0;
  357. }
  358. static const struct udevice_id sb_eth_ids[] = {
  359. { .compatible = "sandbox,eth" },
  360. { }
  361. };
  362. U_BOOT_DRIVER(eth_sandbox) = {
  363. .name = "eth_sandbox",
  364. .id = UCLASS_ETH,
  365. .of_match = sb_eth_ids,
  366. .ofdata_to_platdata = sb_eth_ofdata_to_platdata,
  367. .remove = sb_eth_remove,
  368. .ops = &sb_eth_ops,
  369. .priv_auto_alloc_size = sizeof(struct eth_sandbox_priv),
  370. .platdata_auto_alloc_size = sizeof(struct eth_pdata),
  371. };