1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2015 National Instruments
14 #include <asm/global_data.h>
17 DECLARE_GLOBAL_DATA_PTR;
19 static bool skip_timeout;
22 * sandbox_eth_disable_response()
24 * index - The alias index (also DM seq number)
25 * disable - If non-zero, ignore sent packets and don't send mock response
27 void sandbox_eth_disable_response(int index, bool disable)
30 struct eth_sandbox_priv *priv;
33 ret = uclass_get_device(UCLASS_ETH, index, &dev);
37 priv = dev_get_priv(dev);
38 priv->disabled = disable;
42 * sandbox_eth_skip_timeout()
44 * When the first packet read is attempted, fast-forward time
46 void sandbox_eth_skip_timeout(void)
52 * sandbox_eth_arp_req_to_reply()
54 * Check for an arp request to be sent. If so, inject a reply
56 * returns 0 if injected, -EAGAIN if not
58 int sandbox_eth_arp_req_to_reply(struct udevice *dev, void *packet,
61 struct eth_sandbox_priv *priv = dev_get_priv(dev);
62 struct ethernet_hdr *eth = packet;
64 struct ethernet_hdr *eth_recv;
65 struct arp_hdr *arp_recv;
67 if (ntohs(eth->et_protlen) != PROT_ARP)
70 arp = packet + ETHER_HDR_SIZE;
72 if (ntohs(arp->ar_op) != ARPOP_REQUEST)
75 /* Don't allow the buffer to overrun */
76 if (priv->recv_packets >= PKTBUFSRX)
79 /* store this as the assumed IP of the fake host */
80 priv->fake_host_ipaddr = net_read_ip(&arp->ar_tpa);
82 /* Formulate a fake response */
83 eth_recv = (void *)priv->recv_packet_buffer[priv->recv_packets];
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);
88 arp_recv = (void *)eth_recv + ETHER_HDR_SIZE;
89 arp_recv->ar_hrd = htons(ARP_ETHER);
90 arp_recv->ar_pro = htons(PROT_IP);
91 arp_recv->ar_hln = ARP_HLEN;
92 arp_recv->ar_pln = ARP_PLEN;
93 arp_recv->ar_op = htons(ARPOP_REPLY);
94 memcpy(&arp_recv->ar_sha, priv->fake_host_hwaddr, ARP_HLEN);
95 net_write_ip(&arp_recv->ar_spa, priv->fake_host_ipaddr);
96 memcpy(&arp_recv->ar_tha, &arp->ar_sha, ARP_HLEN);
97 net_copy_ip(&arp_recv->ar_tpa, &arp->ar_spa);
99 priv->recv_packet_length[priv->recv_packets] =
100 ETHER_HDR_SIZE + ARP_HDR_SIZE;
101 ++priv->recv_packets;
107 * sandbox_eth_ping_req_to_reply()
109 * Check for a ping request to be sent. If so, inject a reply
111 * returns 0 if injected, -EAGAIN if not
113 int sandbox_eth_ping_req_to_reply(struct udevice *dev, void *packet,
116 struct eth_sandbox_priv *priv = dev_get_priv(dev);
117 struct ethernet_hdr *eth = packet;
118 struct ip_udp_hdr *ip;
119 struct icmp_hdr *icmp;
120 struct ethernet_hdr *eth_recv;
121 struct ip_udp_hdr *ipr;
122 struct icmp_hdr *icmpr;
124 if (ntohs(eth->et_protlen) != PROT_IP)
127 ip = packet + ETHER_HDR_SIZE;
129 if (ip->ip_p != IPPROTO_ICMP)
132 icmp = (struct icmp_hdr *)&ip->udp_src;
134 if (icmp->type != ICMP_ECHO_REQUEST)
137 /* Don't allow the buffer to overrun */
138 if (priv->recv_packets >= PKTBUFSRX)
141 /* reply to the ping */
142 eth_recv = (void *)priv->recv_packet_buffer[priv->recv_packets];
143 memcpy(eth_recv, packet, len);
144 ipr = (void *)eth_recv + ETHER_HDR_SIZE;
145 icmpr = (struct icmp_hdr *)&ipr->udp_src;
146 memcpy(eth_recv->et_dest, eth->et_src, ARP_HLEN);
147 memcpy(eth_recv->et_src, priv->fake_host_hwaddr, ARP_HLEN);
150 net_copy_ip((void *)&ipr->ip_dst, &ip->ip_src);
151 net_write_ip((void *)&ipr->ip_src, priv->fake_host_ipaddr);
152 ipr->ip_sum = compute_ip_checksum(ipr, IP_HDR_SIZE);
154 icmpr->type = ICMP_ECHO_REPLY;
156 icmpr->checksum = compute_ip_checksum(icmpr, ICMP_HDR_SIZE);
158 priv->recv_packet_length[priv->recv_packets] = len;
159 ++priv->recv_packets;
165 * sandbox_eth_recv_arp_req()
167 * Inject an ARP request for this target
169 * returns 0 if injected, -EOVERFLOW if not
171 int sandbox_eth_recv_arp_req(struct udevice *dev)
173 struct eth_sandbox_priv *priv = dev_get_priv(dev);
174 struct ethernet_hdr *eth_recv;
175 struct arp_hdr *arp_recv;
177 /* Don't allow the buffer to overrun */
178 if (priv->recv_packets >= PKTBUFSRX)
181 /* Formulate a fake request */
182 eth_recv = (void *)priv->recv_packet_buffer[priv->recv_packets];
183 memcpy(eth_recv->et_dest, net_bcast_ethaddr, ARP_HLEN);
184 memcpy(eth_recv->et_src, priv->fake_host_hwaddr, ARP_HLEN);
185 eth_recv->et_protlen = htons(PROT_ARP);
187 arp_recv = (void *)eth_recv + ETHER_HDR_SIZE;
188 arp_recv->ar_hrd = htons(ARP_ETHER);
189 arp_recv->ar_pro = htons(PROT_IP);
190 arp_recv->ar_hln = ARP_HLEN;
191 arp_recv->ar_pln = ARP_PLEN;
192 arp_recv->ar_op = htons(ARPOP_REQUEST);
193 memcpy(&arp_recv->ar_sha, priv->fake_host_hwaddr, ARP_HLEN);
194 net_write_ip(&arp_recv->ar_spa, priv->fake_host_ipaddr);
195 memcpy(&arp_recv->ar_tha, net_null_ethaddr, ARP_HLEN);
196 net_write_ip(&arp_recv->ar_tpa, net_ip);
198 priv->recv_packet_length[priv->recv_packets] =
199 ETHER_HDR_SIZE + ARP_HDR_SIZE;
200 ++priv->recv_packets;
206 * sandbox_eth_recv_ping_req()
208 * Inject a ping request for this target
210 * returns 0 if injected, -EOVERFLOW if not
212 int sandbox_eth_recv_ping_req(struct udevice *dev)
214 struct eth_sandbox_priv *priv = dev_get_priv(dev);
215 struct ethernet_hdr *eth_recv;
216 struct ip_udp_hdr *ipr;
217 struct icmp_hdr *icmpr;
219 /* Don't allow the buffer to overrun */
220 if (priv->recv_packets >= PKTBUFSRX)
223 /* Formulate a fake ping */
224 eth_recv = (void *)priv->recv_packet_buffer[priv->recv_packets];
226 memcpy(eth_recv->et_dest, net_ethaddr, ARP_HLEN);
227 memcpy(eth_recv->et_src, priv->fake_host_hwaddr, ARP_HLEN);
228 eth_recv->et_protlen = htons(PROT_IP);
230 ipr = (void *)eth_recv + ETHER_HDR_SIZE;
232 ipr->ip_len = htons(IP_ICMP_HDR_SIZE);
233 ipr->ip_off = htons(IP_FLAGS_DFRAG);
234 ipr->ip_p = IPPROTO_ICMP;
236 net_write_ip(&ipr->ip_src, priv->fake_host_ipaddr);
237 net_write_ip(&ipr->ip_dst, net_ip);
238 ipr->ip_sum = compute_ip_checksum(ipr, IP_HDR_SIZE);
240 icmpr = (struct icmp_hdr *)&ipr->udp_src;
242 icmpr->type = ICMP_ECHO_REQUEST;
245 icmpr->un.echo.id = 0;
246 icmpr->un.echo.sequence = htons(1);
247 icmpr->checksum = compute_ip_checksum(icmpr, ICMP_HDR_SIZE);
249 priv->recv_packet_length[priv->recv_packets] =
250 ETHER_HDR_SIZE + IP_ICMP_HDR_SIZE;
251 ++priv->recv_packets;
257 * sb_default_handler()
259 * perform typical responses to simple ping
261 * dev - device pointer
262 * pkt - "sent" packet buffer
263 * len - length of packet
265 static int sb_default_handler(struct udevice *dev, void *packet,
268 if (!sandbox_eth_arp_req_to_reply(dev, packet, len))
270 if (!sandbox_eth_ping_req_to_reply(dev, packet, len))
277 * sandbox_eth_set_tx_handler()
279 * Set a custom response to a packet being sent through the sandbox eth test
282 * index - interface to set the handler for
283 * handler - The func ptr to call on send. If NULL, set to default handler
285 void sandbox_eth_set_tx_handler(int index, sandbox_eth_tx_hand_f *handler)
288 struct eth_sandbox_priv *priv;
291 ret = uclass_get_device(UCLASS_ETH, index, &dev);
295 priv = dev_get_priv(dev);
297 priv->tx_handler = handler;
299 priv->tx_handler = sb_default_handler;
305 * priv - priv void ptr to store in the device
307 void sandbox_eth_set_priv(int index, void *priv)
310 struct eth_sandbox_priv *dev_priv;
313 ret = uclass_get_device(UCLASS_ETH, index, &dev);
317 dev_priv = dev_get_priv(dev);
319 dev_priv->priv = priv;
322 static int sb_eth_start(struct udevice *dev)
324 struct eth_sandbox_priv *priv = dev_get_priv(dev);
326 debug("eth_sandbox: Start\n");
328 priv->recv_packets = 0;
329 for (int i = 0; i < PKTBUFSRX; i++) {
330 priv->recv_packet_buffer[i] = net_rx_packets[i];
331 priv->recv_packet_length[i] = 0;
337 static int sb_eth_send(struct udevice *dev, void *packet, int length)
339 struct eth_sandbox_priv *priv = dev_get_priv(dev);
341 debug("eth_sandbox: Send packet %d\n", length);
346 return priv->tx_handler(dev, packet, length);
349 static int sb_eth_recv(struct udevice *dev, int flags, uchar **packetp)
351 struct eth_sandbox_priv *priv = dev_get_priv(dev);
354 timer_test_add_offset(11000UL);
355 skip_timeout = false;
358 if (priv->recv_packets) {
359 int lcl_recv_packet_length = priv->recv_packet_length[0];
361 debug("eth_sandbox: received packet[%d], %d waiting\n",
362 lcl_recv_packet_length, priv->recv_packets - 1);
363 *packetp = priv->recv_packet_buffer[0];
364 return lcl_recv_packet_length;
369 static int sb_eth_free_pkt(struct udevice *dev, uchar *packet, int length)
371 struct eth_sandbox_priv *priv = dev_get_priv(dev);
374 if (!priv->recv_packets)
377 --priv->recv_packets;
378 for (i = 0; i < priv->recv_packets; i++) {
379 priv->recv_packet_length[i] = priv->recv_packet_length[i + 1];
380 memcpy(priv->recv_packet_buffer[i],
381 priv->recv_packet_buffer[i + 1],
382 priv->recv_packet_length[i + 1]);
384 priv->recv_packet_length[priv->recv_packets] = 0;
389 static void sb_eth_stop(struct udevice *dev)
391 debug("eth_sandbox: Stop\n");
394 static int sb_eth_write_hwaddr(struct udevice *dev)
396 struct eth_pdata *pdata = dev_get_plat(dev);
397 struct eth_sandbox_priv *priv = dev_get_priv(dev);
399 debug("eth_sandbox %s: Write HW ADDR - %pM\n", dev->name,
401 memcpy(priv->fake_host_hwaddr, pdata->enetaddr, ARP_HLEN);
405 static const struct eth_ops sb_eth_ops = {
406 .start = sb_eth_start,
409 .free_pkt = sb_eth_free_pkt,
411 .write_hwaddr = sb_eth_write_hwaddr,
414 static int sb_eth_remove(struct udevice *dev)
419 static int sb_eth_of_to_plat(struct udevice *dev)
421 struct eth_pdata *pdata = dev_get_plat(dev);
422 struct eth_sandbox_priv *priv = dev_get_priv(dev);
424 pdata->iobase = dev_read_addr(dev);
425 priv->disabled = false;
426 priv->tx_handler = sb_default_handler;
431 static const struct udevice_id sb_eth_ids[] = {
432 { .compatible = "sandbox,eth" },
436 U_BOOT_DRIVER(eth_sandbox) = {
437 .name = "eth_sandbox",
439 .of_match = sb_eth_ids,
440 .of_to_plat = sb_eth_of_to_plat,
441 .remove = sb_eth_remove,
443 .priv_auto = sizeof(struct eth_sandbox_priv),
444 .plat_auto = sizeof(struct eth_pdata),