blob: 04a594cf53ae9944576fdc7ce525beb1dfa44d6b [file] [log] [blame]
Joe Hershbergera36b12f2012-05-23 07:58:02 +00001/*
2 * Copied from Linux Monitor (LiMon) - Networking.
3 *
4 * Copyright 1994 - 2000 Neil Russell.
5 * (See License)
6 * Copyright 2000 Roland Borde
7 * Copyright 2000 Paolo Scaffardi
8 * Copyright 2000-2002 Wolfgang Denk, wd@denx.de
9 */
10
11#include "ping.h"
12#include "arp.h"
13
14static ushort PingSeqNo;
15
16/* The ip address to ping */
17IPaddr_t NetPingIP;
18
19static int ping_send(void)
20{
21 static uchar mac[6];
Joe Hershberger594c26f2012-05-23 07:58:04 +000022 struct ip_udp_hdr *ip;
Joe Hershbergera36b12f2012-05-23 07:58:02 +000023 ushort *s;
24 uchar *pkt;
25
26 /* XXX always send arp request */
27
28 memcpy(mac, NetEtherNullAddr, 6);
29
30 debug("sending ARP for %pI4\n", &NetPingIP);
31
32 NetArpWaitPacketIP = NetPingIP;
33 NetArpWaitPacketMAC = mac;
34
35 pkt = NetArpWaitTxPacket;
36 pkt += NetSetEther(pkt, mac, PROT_IP);
37
Joe Hershberger594c26f2012-05-23 07:58:04 +000038 ip = (struct ip_udp_hdr *)pkt;
Joe Hershbergera36b12f2012-05-23 07:58:02 +000039
40 /*
41 * Construct an IP and ICMP header.
42 * (need to set no fragment bit - XXX)
43 */
44 /* IP_HDR_SIZE / 4 (not including UDP) */
45 ip->ip_hl_v = 0x45;
46 ip->ip_tos = 0;
Joe Hershbergerc5c59df2012-05-23 07:58:05 +000047 ip->ip_len = htons(IP_HDR_SIZE + 8);
Joe Hershbergera36b12f2012-05-23 07:58:02 +000048 ip->ip_id = htons(NetIPID++);
49 ip->ip_off = htons(IP_FLAGS_DFRAG); /* Don't fragment */
50 ip->ip_ttl = 255;
51 ip->ip_p = 0x01; /* ICMP */
52 ip->ip_sum = 0;
53 /* already in network byte order */
54 NetCopyIP((void *)&ip->ip_src, &NetOurIP);
55 /* - "" - */
56 NetCopyIP((void *)&ip->ip_dst, &NetPingIP);
Joe Hershbergerc5c59df2012-05-23 07:58:05 +000057 ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE / 2);
Joe Hershbergera36b12f2012-05-23 07:58:02 +000058
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);
65
66 /* size of the waiting packet */
67 NetArpWaitTxPacketSize =
Joe Hershbergerc5c59df2012-05-23 07:58:05 +000068 (pkt - NetArpWaitTxPacket) + IP_HDR_SIZE + 8;
Joe Hershbergera36b12f2012-05-23 07:58:02 +000069
70 /* and do the ARP request */
71 NetArpWaitTry = 1;
72 NetArpWaitTimerStart = get_timer(0);
73 ArpRequest();
74 return 1; /* waiting */
75}
76
77static void ping_timeout(void)
78{
79 eth_halt();
80 NetState = NETLOOP_FAIL; /* we did not get the reply */
81}
82
83static void ping_handler(uchar *pkt, unsigned dest, IPaddr_t sip,
84 unsigned src, unsigned len)
85{
86 if (sip != NetPingIP)
87 return;
88
89 NetState = NETLOOP_SUCCESS;
90}
91
92void ping_start(void)
93{
94 printf("Using %s device\n", eth_get_name());
95 NetSetTimeout(10000UL, ping_timeout);
96 NetSetHandler(ping_handler);
97
98 ping_send();
99}
100
Joe Hershbergercb487f52012-05-23 07:58:06 +0000101void ping_receive(struct ethernet_hdr *et, struct ip_udp_hdr *ip, int len)
Joe Hershbergera36b12f2012-05-23 07:58:02 +0000102{
103 ICMP_t *icmph = (ICMP_t *)&(ip->udp_src);
104 IPaddr_t src_ip;
105
106 switch (icmph->type) {
107 case ICMP_ECHO_REPLY:
108 /*
109 * IP header OK. Pass the packet to the
110 * current handler.
111 */
112 /* XXX point to ip packet */
113 src_ip = NetReadIP((void *)&ip->ip_src);
114 NetGetHandler()((uchar *)ip, 0, src_ip, 0, 0);
115 return;
116 case ICMP_ECHO_REQUEST:
117 debug("Got ICMP ECHO REQUEST, return "
118 "%d bytes\n", ETHER_HDR_SIZE + len);
119
120 memcpy(&et->et_dest[0], &et->et_src[0], 6);
121 memcpy(&et->et_src[0], NetOurEther, 6);
122
123 ip->ip_sum = 0;
124 ip->ip_off = 0;
125 NetCopyIP((void *)&ip->ip_dst, &ip->ip_src);
126 NetCopyIP((void *)&ip->ip_src, &NetOurIP);
127 ip->ip_sum = ~NetCksum((uchar *)ip,
Joe Hershbergerc5c59df2012-05-23 07:58:05 +0000128 IP_HDR_SIZE >> 1);
Joe Hershbergera36b12f2012-05-23 07:58:02 +0000129
130 icmph->type = ICMP_ECHO_REPLY;
131 icmph->checksum = 0;
132 icmph->checksum = ~NetCksum((uchar *)icmph,
Joe Hershbergerc5c59df2012-05-23 07:58:05 +0000133 (len - IP_HDR_SIZE) >> 1);
Joe Hershbergera36b12f2012-05-23 07:58:02 +0000134 (void) eth_send((uchar *)et,
135 ETHER_HDR_SIZE + len);
136 return;
137/* default:
138 return;*/
139 }
140}