1 // SPDX-License-Identifier: GPL-2.0+
12 #include "../../net/bootp.h"
15 * sandbox_eth_bootp_req_to_reply()
17 * Check if a BOOTP request was sent. If so, inject a reply
19 * returns 0 if injected, -EAGAIN if not
21 static int sandbox_eth_bootp_req_to_reply(struct udevice *dev, void *packet,
24 struct eth_sandbox_priv *priv = dev_get_priv(dev);
25 struct ethernet_hdr *eth = packet;
26 struct ip_udp_hdr *ip;
28 struct ethernet_hdr *eth_recv;
29 struct ip_udp_hdr *ipr;
30 struct bootp_hdr *bpr;
32 if (ntohs(eth->et_protlen) != PROT_IP)
35 ip = packet + ETHER_HDR_SIZE;
36 if (ip->ip_p != IPPROTO_UDP)
39 if (ntohs(ip->udp_dst) != PORT_BOOTPS)
42 bp = (void *)ip + IP_UDP_HDR_SIZE;
43 if (bp->bp_op != OP_BOOTREQUEST)
46 /* Don't allow the buffer to overrun */
47 if (priv->recv_packets >= PKTBUFSRX)
50 /* reply to the request */
51 eth_recv = (void *)priv->recv_packet_buffer[priv->recv_packets];
52 memcpy(eth_recv, packet, len);
53 ipr = (void *)eth_recv + ETHER_HDR_SIZE;
54 bpr = (void *)ipr + IP_UDP_HDR_SIZE;
55 memcpy(eth_recv->et_dest, eth->et_src, ARP_HLEN);
56 memcpy(eth_recv->et_src, priv->fake_host_hwaddr, ARP_HLEN);
59 net_write_ip(&ipr->ip_dst, net_ip);
60 net_write_ip(&ipr->ip_src, priv->fake_host_ipaddr);
61 ipr->ip_sum = compute_ip_checksum(ipr, IP_HDR_SIZE);
62 ipr->udp_src = ip->udp_dst;
63 ipr->udp_dst = ip->udp_src;
65 bpr->bp_op = OP_BOOTREPLY;
66 net_write_ip(&bpr->bp_yiaddr, net_ip);
67 net_write_ip(&bpr->bp_siaddr, priv->fake_host_ipaddr);
68 copy_filename(bpr->bp_file, CONFIG_BOOTFILE, sizeof(CONFIG_BOOTFILE));
69 memset(&bpr->bp_vend, 0, sizeof(bpr->bp_vend));
71 priv->recv_packet_length[priv->recv_packets] = len;
77 struct spl_test_net_priv {
78 struct unit_test_state *uts;
84 /* Well known TFTP port # */
86 /* Transaction ID, chosen at random */
87 #define TFTP_TID 21313
96 /* default TFTP block size */
97 #define TFTP_BLOCK_SIZE 512
104 #define TFTP_HDR_SIZE sizeof(struct tftp_hdr)
107 * sandbox_eth_tftp_req_to_reply()
109 * Check if a TFTP request was sent. If so, inject a reply. We don't support
110 * options, and we don't check for rollover, so we are limited files of less
113 * returns 0 if injected, -EAGAIN if not
115 static int sandbox_eth_tftp_req_to_reply(struct udevice *dev, void *packet,
118 struct eth_sandbox_priv *priv = dev_get_priv(dev);
119 struct spl_test_net_priv *test_priv = priv->priv;
120 struct ethernet_hdr *eth = packet;
121 struct ip_udp_hdr *ip;
122 struct tftp_hdr *tftp;
123 struct ethernet_hdr *eth_recv;
124 struct ip_udp_hdr *ipr;
125 struct tftp_hdr *tftpr;
129 if (ntohs(eth->et_protlen) != PROT_IP)
132 ip = packet + ETHER_HDR_SIZE;
133 if (ip->ip_p != IPPROTO_UDP)
136 if (ntohs(ip->udp_dst) == TFTP_PORT) {
137 tftp = (void *)ip + IP_UDP_HDR_SIZE;
138 if (htons(tftp->opcode) != TFTP_RRQ)
142 } else if (ntohs(ip->udp_dst) == TFTP_TID) {
143 tftp = (void *)ip + IP_UDP_HDR_SIZE;
144 if (htons(tftp->opcode) != TFTP_ACK)
147 block = htons(tftp->block);
152 if (block * TFTP_BLOCK_SIZE > test_priv->img_size)
155 size = min(test_priv->img_size - block * TFTP_BLOCK_SIZE,
156 (size_t)TFTP_BLOCK_SIZE);
158 /* Don't allow the buffer to overrun */
159 if (priv->recv_packets >= PKTBUFSRX)
162 /* reply to the request */
163 eth_recv = (void *)priv->recv_packet_buffer[priv->recv_packets];
164 memcpy(eth_recv->et_dest, eth->et_src, ARP_HLEN);
165 memcpy(eth_recv->et_src, priv->fake_host_hwaddr, ARP_HLEN);
166 eth_recv->et_protlen = htons(PROT_IP);
168 ipr = (void *)eth_recv + ETHER_HDR_SIZE;
170 ipr->ip_len = htons(IP_UDP_HDR_SIZE + TFTP_HDR_SIZE + size);
171 ipr->ip_off = htons(IP_FLAGS_DFRAG);
173 ipr->ip_p = IPPROTO_UDP;
175 net_copy_ip(&ipr->ip_dst, &ip->ip_src);
176 net_copy_ip(&ipr->ip_src, &ip->ip_dst);
177 ipr->ip_sum = compute_ip_checksum(ipr, IP_HDR_SIZE);
179 ipr->udp_src = htons(TFTP_TID);
180 ipr->udp_dst = ip->udp_src;
181 ipr->udp_len = htons(UDP_HDR_SIZE + TFTP_HDR_SIZE + size);
184 tftpr = (void *)ipr + IP_UDP_HDR_SIZE;
185 tftpr->opcode = htons(TFTP_DATA);
186 tftpr->block = htons(block + 1);
187 memcpy((void *)tftpr + TFTP_HDR_SIZE,
188 test_priv->img + block * TFTP_BLOCK_SIZE, size);
190 priv->recv_packet_length[priv->recv_packets] =
191 ETHER_HDR_SIZE + IP_UDP_HDR_SIZE + TFTP_HDR_SIZE + size;
192 ++priv->recv_packets;
197 static int spl_net_handler(struct udevice *dev, void *packet,
200 struct eth_sandbox_priv *priv = dev_get_priv(dev);
201 int old_packets = priv->recv_packets;
203 priv->fake_host_ipaddr = string_to_ip("1.1.2.4");
204 net_ip = string_to_ip("1.1.2.2");
206 sandbox_eth_arp_req_to_reply(dev, packet, len);
207 sandbox_eth_bootp_req_to_reply(dev, packet, len);
208 sandbox_eth_tftp_req_to_reply(dev, packet, len);
210 if (old_packets == priv->recv_packets)
216 static int spl_test_net_write_image(struct unit_test_state *uts, void *img,
219 struct spl_test_net_priv *test_priv = malloc(sizeof(*test_priv));
221 ut_assertnonnull(test_priv);
222 test_priv->uts = uts;
223 test_priv->img = img;
224 test_priv->img_size = img_size;
226 sandbox_eth_set_tx_handler(0, spl_net_handler);
227 sandbox_eth_set_priv(0, test_priv);
231 static int spl_test_net(struct unit_test_state *uts, const char *test_name,
232 enum spl_test_image type)
234 struct eth_sandbox_priv *priv;
238 net_server_ip = string_to_ip("1.1.2.4");
239 ret = do_spl_test_load(uts, test_name, type,
240 SPL_LOAD_IMAGE_GET(0, BOOT_DEVICE_CPGMAC,
241 spl_net_load_image_cpgmac),
242 spl_test_net_write_image);
244 sandbox_eth_set_tx_handler(0, NULL);
245 ut_assertok(uclass_get_device(UCLASS_ETH, 0, &dev));
246 priv = dev_get_priv(dev);
250 SPL_IMG_TEST(spl_test_net, LEGACY, DM_FLAGS);
251 SPL_IMG_TEST(spl_test_net, LEGACY_LZMA, DM_FLAGS);
252 SPL_IMG_TEST(spl_test_net, IMX8, DM_FLAGS);
253 SPL_IMG_TEST(spl_test_net, FIT_INTERNAL, DM_FLAGS);
254 SPL_IMG_TEST(spl_test_net, FIT_EXTERNAL, DM_FLAGS);