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