Commit 8f79bb17a4251ec096a7184d1eaf6f5dea3d2623
Committed by
Wolfgang Denk
1 parent
d67f10ce0f
Exists in
master
and in
54 other branches
net: tftpput: Move ICMP code into its own function
NetReceive() is a very long function with a lot of indent. Before adding code to the ICMP bit, split it out. Signed-off-by: Simon Glass <sjg@chromium.org>
Showing 1 changed file with 58 additions and 43 deletions Side-by-side Diff
net/net.c
... | ... | @@ -1331,6 +1331,62 @@ |
1331 | 1331 | } |
1332 | 1332 | #endif |
1333 | 1333 | |
1334 | +/** | |
1335 | + * Receive an ICMP packet. We deal with REDIRECT and PING here, and silently | |
1336 | + * drop others. | |
1337 | + * | |
1338 | + * @parma ip IP packet containing the ICMP | |
1339 | + */ | |
1340 | +static void receive_icmp(IP_t *ip, int len, IPaddr_t src_ip, Ethernet_t *et) | |
1341 | +{ | |
1342 | + ICMP_t *icmph = (ICMP_t *)&ip->udp_src; | |
1343 | + | |
1344 | + switch (icmph->type) { | |
1345 | + case ICMP_REDIRECT: | |
1346 | + if (icmph->code != ICMP_REDIR_HOST) | |
1347 | + return; | |
1348 | + printf(" ICMP Host Redirect to %pI4 ", | |
1349 | + &icmph->un.gateway); | |
1350 | + break; | |
1351 | +#if defined(CONFIG_CMD_PING) | |
1352 | + case ICMP_ECHO_REPLY: | |
1353 | + /* | |
1354 | + * IP header OK. Pass the packet to the | |
1355 | + * current handler. | |
1356 | + */ | |
1357 | + /* | |
1358 | + * XXX point to ip packet - should this use | |
1359 | + * packet_icmp_handler? | |
1360 | + */ | |
1361 | + (*packetHandler)((uchar *)ip, 0, src_ip, 0, 0); | |
1362 | + break; | |
1363 | + case ICMP_ECHO_REQUEST: | |
1364 | + debug("Got ICMP ECHO REQUEST, return %d bytes\n", | |
1365 | + ETHER_HDR_SIZE + len); | |
1366 | + | |
1367 | + memcpy(&et->et_dest[0], &et->et_src[0], 6); | |
1368 | + memcpy(&et->et_src[0], NetOurEther, 6); | |
1369 | + | |
1370 | + ip->ip_sum = 0; | |
1371 | + ip->ip_off = 0; | |
1372 | + NetCopyIP((void *)&ip->ip_dst, &ip->ip_src); | |
1373 | + NetCopyIP((void *)&ip->ip_src, &NetOurIP); | |
1374 | + ip->ip_sum = ~NetCksum((uchar *)ip, | |
1375 | + IP_HDR_SIZE_NO_UDP >> 1); | |
1376 | + | |
1377 | + icmph->type = ICMP_ECHO_REPLY; | |
1378 | + icmph->checksum = 0; | |
1379 | + icmph->checksum = ~NetCksum((uchar *)icmph, | |
1380 | + (len - IP_HDR_SIZE_NO_UDP) >> 1); | |
1381 | + (void) eth_send((uchar *)et, | |
1382 | + ETHER_HDR_SIZE + len); | |
1383 | + break; | |
1384 | +#endif | |
1385 | + default: | |
1386 | + break; | |
1387 | + } | |
1388 | +} | |
1389 | + | |
1334 | 1390 | void |
1335 | 1391 | NetReceive(volatile uchar *inpkt, int len) |
1336 | 1392 | { |
... | ... | @@ -1617,49 +1673,8 @@ |
1617 | 1673 | * sure if there aren't any other situations. |
1618 | 1674 | */ |
1619 | 1675 | if (ip->ip_p == IPPROTO_ICMP) { |
1620 | - ICMP_t *icmph = (ICMP_t *)&(ip->udp_src); | |
1621 | - | |
1622 | - switch (icmph->type) { | |
1623 | - case ICMP_REDIRECT: | |
1624 | - if (icmph->code != ICMP_REDIR_HOST) | |
1625 | - return; | |
1626 | - printf(" ICMP Host Redirect to %pI4 ", | |
1627 | - &icmph->un.gateway); | |
1628 | - return; | |
1629 | -#if defined(CONFIG_CMD_PING) | |
1630 | - case ICMP_ECHO_REPLY: | |
1631 | - /* | |
1632 | - * IP header OK. Pass the packet to the | |
1633 | - * current handler. | |
1634 | - */ | |
1635 | - /* XXX point to ip packet */ | |
1636 | - (*packetHandler)((uchar *)ip, 0, src_ip, 0, 0); | |
1637 | - return; | |
1638 | - case ICMP_ECHO_REQUEST: | |
1639 | - debug("Got ICMP ECHO REQUEST, return %d bytes\n", | |
1640 | - ETHER_HDR_SIZE + len); | |
1641 | - | |
1642 | - memcpy(&et->et_dest[0], &et->et_src[0], 6); | |
1643 | - memcpy(&et->et_src[0], NetOurEther, 6); | |
1644 | - | |
1645 | - ip->ip_sum = 0; | |
1646 | - ip->ip_off = 0; | |
1647 | - NetCopyIP((void *)&ip->ip_dst, &ip->ip_src); | |
1648 | - NetCopyIP((void *)&ip->ip_src, &NetOurIP); | |
1649 | - ip->ip_sum = ~NetCksum((uchar *)ip, | |
1650 | - IP_HDR_SIZE_NO_UDP >> 1); | |
1651 | - | |
1652 | - icmph->type = ICMP_ECHO_REPLY; | |
1653 | - icmph->checksum = 0; | |
1654 | - icmph->checksum = ~NetCksum((uchar *)icmph, | |
1655 | - (len - IP_HDR_SIZE_NO_UDP) >> 1); | |
1656 | - (void) eth_send((uchar *)et, | |
1657 | - ETHER_HDR_SIZE + len); | |
1658 | - return; | |
1659 | -#endif | |
1660 | - default: | |
1661 | - return; | |
1662 | - } | |
1676 | + receive_icmp(ip, len, src_ip, et); | |
1677 | + return; | |
1663 | 1678 | } else if (ip->ip_p != IPPROTO_UDP) { /* Only UDP packets */ |
1664 | 1679 | return; |
1665 | 1680 | } |