1 /* $NetBSD: etherfun.c,v 1.12 2011/09/29 09:18:17 he Exp $ */
2
3 /*
4 * Copyright (c) 1995 Charles D. Cranor and Seth Widoff
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27 /* etherfun.c */
28
29 #include "sboot.h"
30 #include "etherfun.h"
31
32 /* Construct and send a rev arp packet */
33 void
do_rev_arp(void)34 do_rev_arp(void)
35 {
36 int i;
37
38 for (i = 0; i < 6; i++) {
39 eh->ether_dhost[i] = 0xff;
40 }
41 memcpy(eh->ether_shost, myea, 6);
42 eh->ether_type = ETYPE_RARP;
43
44 rarp->ar_hrd = 1; /* hardware type is 1 */
45 rarp->ar_pro = PTYPE_IP;
46 rarp->ar_hln = 6; /* length of hardware address is 6 bytes */
47 rarp->ar_pln = 4; /* length of ip address is 4 byte */
48 rarp->ar_op = OPCODE_RARP;
49 memcpy(rarp->arp_sha, myea, sizeof(myea));
50 memcpy(rarp->arp_tha, myea, sizeof(myea));
51 for (i = 0; i < 4; i++) {
52 rarp->arp_spa[i] = rarp->arp_tpa[i] = 0x00;
53 }
54
55 le_put(buf, 76);
56 }
57
58 /* Receive and disassemble the rev_arp reply */
59
60 int
get_rev_arp(void)61 get_rev_arp(void)
62 {
63
64 le_get(buf, sizeof(buf), 6);
65 if (eh->ether_type == ETYPE_RARP && rarp->ar_op == OPCODE_REPLY) {
66 memcpy(myip, rarp->arp_tpa, sizeof(rarp->arp_tpa));
67 memcpy(servip, rarp->arp_spa, sizeof(rarp->arp_spa));
68 memcpy(servea, rarp->arp_sha, sizeof(rarp->arp_sha));
69 return 1;
70 }
71 return 0;
72 }
73
74 /* Try to get a reply to a rev arp request */
75
76 int
rev_arp(void)77 rev_arp(void)
78 {
79 int tries = 0;
80
81 while (tries < 5) {
82 do_rev_arp();
83 if (get_rev_arp()) {
84 return 1;
85 }
86 tries++;
87 }
88 return 0;
89 }
90
91 /* Send a tftp read request or acknowledgement
92 mesgtype 0 is a read request, 1 is an aknowledgement */
93 const char HEXDIGITS[16] = "0123456789ABCDEF";
94
95 void
do_send_tftp(int mesgtype)96 do_send_tftp(int mesgtype)
97 {
98 u_long res, iptmp, lcv;
99 u_char *tot;
100
101 if (mesgtype == 0) {
102 tot = tftp_r + (sizeof(MSG) - 1);
103 myport = (u_short)time();
104 if (myport < 1000)
105 myport += 1000;
106 servport = FTP_PORT; /* to start */
107 } else {
108 tot = (u_char *)tftp_a + 4;
109 }
110
111 memcpy (eh->ether_dhost, servea, sizeof(servea));
112 memcpy (eh->ether_shost, myea, sizeof(myea));
113 eh->ether_type = ETYPE_IP;
114
115 iph->ip_v = IP_VERSION;
116 iph->ip_hl = IP_HLEN;
117 iph->ip_tos = 0; /* type of service is 0 */
118 iph->ip_id = 0; /* id field is 0 */
119 iph->ip_off = IP_DF;
120 iph->ip_ttl = 3; /* time to live is 3 seconds/hops */
121 iph->ip_p = IPP_UDP;
122 memcpy(iph->ip_src, myip, sizeof(myip));
123 memcpy(iph->ip_dst, servip, sizeof(servip));
124 iph->ip_sum = 0;
125 iph->ip_len = tot - (u_char *)iph;
126 res = oc_cksum(iph, sizeof(struct ip), 0);
127 iph->ip_sum = 0xffff & ~res;
128 udph->uh_sport = myport;
129 udph->uh_dport = servport;
130 udph->uh_sum = 0;
131
132 if (mesgtype) {
133 tftp_a->op_code = FTPOP_ACKN;
134 tftp_a->block = (u_short)(mesgtype);
135 } else {
136 memcpy (&iptmp, myip, sizeof(iptmp));
137 memcpy(tftp_r, MSG, (sizeof(MSG)-1));
138 for (lcv = 9; lcv >= 2; lcv--) {
139 tftp_r[lcv] = HEXDIGITS[iptmp & 0xF];
140
141 iptmp = iptmp >> 4;
142 }
143 }
144
145 udph->uh_ulen = tot - (u_char *)udph;
146
147 le_put(buf, tot - buf);
148 }
149
150 /* Attempt to tftp a file and read it into memory */
151
152 int
do_get_file(void)153 do_get_file(void)
154 {
155 int fail = 0, oldlen;
156 char *loadat = (char *)LOAD_ADDR;
157 last_ack = 0;
158
159 do_send_tftp(READ);
160 for (;;) {
161 if (le_get(buf, sizeof(buf), 5) == 0) { /* timeout occurred */
162 if (last_ack) {
163 do_send_tftp(last_ack);
164 } else {
165 do_send_tftp(READ);
166 }
167 fail++;
168 if (fail > 5) {
169 printf("\n");
170 return 1;
171 }
172 } else {
173 printf("%x \r", tftp->info.block*512);
174 if ((eh->ether_type != ETYPE_IP) ||
175 (iph->ip_p != IPP_UDP)) {
176 fail++;
177 continue;
178 }
179 if (servport == FTP_PORT) servport = udph->uh_sport;
180 if (tftp->info.op_code == FTPOP_ERR) {
181 printf("TFTP: Download error %d: %s\n",
182 tftp->info.block, tftp->data);
183 return 1;
184 }
185 if (tftp->info.block != last_ack + 1) {
186 /* we received the wrong block */
187 if (tftp->info.block < last_ack +1) {
188 /* ackn whatever we received */
189 do_send_tftp(tftp->info.block);
190 } else {
191 /* ackn the last confirmed block */
192 do_send_tftp(last_ack);
193 }
194 fail++;
195 } else { /* we got the right block */
196 fail = 0;
197 last_ack++;
198 oldlen = udph->uh_ulen;
199 do_send_tftp( last_ack );
200 #if 0
201 printf("memcpy %x %x %d\n", loadat,
202 &tftp->data, oldlen - 12);
203 #endif
204 memcpy(loadat, &tftp->data, oldlen - 12);
205 loadat += oldlen - 12;
206 if (oldlen < (8 + 4 + 512)) {
207 printf("\n");
208 return 0;
209 }
210 }
211 }
212 }
213 printf("\n");
214 return 0;
215 }
216