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