2 * Copied from Linux Monitor (LiMon) - Networking.
4 * Copyright 1994 - 2000 Neil Russell.
6 * Copyright 2000 Roland Borde
7 * Copyright 2000 Paolo Scaffardi
8 * Copyright 2000-2002 Wolfgang Denk, wd@denx.de
14 static ushort PingSeqNo;
16 /* The ip address to ping */
19 static int ping_send(void)
22 struct ip_udp_hdr *ip;
26 /* XXX always send arp request */
28 memcpy(mac, NetEtherNullAddr, 6);
30 debug("sending ARP for %pI4\n", &NetPingIP);
32 NetArpWaitPacketIP = NetPingIP;
33 NetArpWaitPacketMAC = mac;
35 pkt = NetArpWaitTxPacket;
36 pkt += NetSetEther(pkt, mac, PROT_IP);
38 ip = (struct ip_udp_hdr *)pkt;
41 * Construct an IP and ICMP header.
42 * (need to set no fragment bit - XXX)
44 /* IP_HDR_SIZE / 4 (not including UDP) */
47 ip->ip_len = htons(IP_HDR_SIZE_NO_UDP + 8);
48 ip->ip_id = htons(NetIPID++);
49 ip->ip_off = htons(IP_FLAGS_DFRAG); /* Don't fragment */
51 ip->ip_p = 0x01; /* ICMP */
53 /* already in network byte order */
54 NetCopyIP((void *)&ip->ip_src, &NetOurIP);
56 NetCopyIP((void *)&ip->ip_dst, &NetPingIP);
57 ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2);
59 s = &ip->udp_src; /* XXX ICMP starts here */
60 s[0] = htons(0x0800); /* echo-request, code */
61 s[1] = 0; /* checksum */
62 s[2] = 0; /* identifier */
63 s[3] = htons(PingSeqNo++); /* sequence number */
64 s[1] = ~NetCksum((uchar *)s, 8/2);
66 /* size of the waiting packet */
67 NetArpWaitTxPacketSize =
68 (pkt - NetArpWaitTxPacket) + IP_HDR_SIZE_NO_UDP + 8;
70 /* and do the ARP request */
72 NetArpWaitTimerStart = get_timer(0);
74 return 1; /* waiting */
77 static void ping_timeout(void)
80 NetState = NETLOOP_FAIL; /* we did not get the reply */
83 static void ping_handler(uchar *pkt, unsigned dest, IPaddr_t sip,
84 unsigned src, unsigned len)
89 NetState = NETLOOP_SUCCESS;
94 printf("Using %s device\n", eth_get_name());
95 NetSetTimeout(10000UL, ping_timeout);
96 NetSetHandler(ping_handler);
101 void ping_receive(Ethernet_t *et, struct ip_udp_hdr *ip, int len)
103 ICMP_t *icmph = (ICMP_t *)&(ip->udp_src);
106 switch (icmph->type) {
107 case ICMP_ECHO_REPLY:
109 * IP header OK. Pass the packet to the
112 /* XXX point to ip packet */
113 src_ip = NetReadIP((void *)&ip->ip_src);
114 NetGetHandler()((uchar *)ip, 0, src_ip, 0, 0);
116 case ICMP_ECHO_REQUEST:
117 debug("Got ICMP ECHO REQUEST, return "
118 "%d bytes\n", ETHER_HDR_SIZE + len);
120 memcpy(&et->et_dest[0], &et->et_src[0], 6);
121 memcpy(&et->et_src[0], NetOurEther, 6);
125 NetCopyIP((void *)&ip->ip_dst, &ip->ip_src);
126 NetCopyIP((void *)&ip->ip_src, &NetOurIP);
127 ip->ip_sum = ~NetCksum((uchar *)ip,
128 IP_HDR_SIZE_NO_UDP >> 1);
130 icmph->type = ICMP_ECHO_REPLY;
132 icmph->checksum = ~NetCksum((uchar *)icmph,
133 (len - IP_HDR_SIZE_NO_UDP) >> 1);
134 (void) eth_send((uchar *)et,
135 ETHER_HDR_SIZE + len);