etherfun.c revision 1.3 1 1.3 wiz /* $NetBSD: etherfun.c,v 1.3 2001/06/12 15:17:19 wiz 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.1 chuck do_rev_arp ()
42 1.1 chuck {
43 1.1 chuck int i;
44 1.1 chuck
45 1.1 chuck for ( i = 0; i < 6; i++ ) {
46 1.1 chuck eh->ether_dhost[i] = 0xff;
47 1.1 chuck }
48 1.1 chuck bcopy(myea, eh->ether_shost, 6);
49 1.1 chuck eh->ether_type = ETYPE_RARP;
50 1.1 chuck
51 1.1 chuck rarp->ar_hrd = 1; /* hardware type is 1 */
52 1.1 chuck rarp->ar_pro = PTYPE_IP;
53 1.1 chuck rarp->ar_hln = 6; /* length of hardware address is 6 bytes */
54 1.1 chuck rarp->ar_pln = 4; /* length of ip address is 4 byte */
55 1.1 chuck rarp->ar_op = OPCODE_RARP;
56 1.1 chuck bcopy(myea, rarp->arp_sha, sizeof(myea));
57 1.1 chuck bcopy(myea, rarp->arp_tha, sizeof(myea));
58 1.1 chuck for ( i = 0; i < 4; i++ ) {
59 1.1 chuck rarp->arp_spa[i] = rarp->arp_tpa[i] = 0x00;
60 1.1 chuck }
61 1.1 chuck
62 1.1 chuck 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.1 chuck get_rev_arp ()
69 1.1 chuck {
70 1.1 chuck le_get(buf, sizeof(buf), 6);
71 1.1 chuck if ( eh->ether_type == ETYPE_RARP && rarp->ar_op == OPCODE_REPLY ) {
72 1.1 chuck bcopy(rarp->arp_tpa, myip, sizeof(rarp->arp_tpa));
73 1.1 chuck bcopy(rarp->arp_spa, servip, sizeof(rarp->arp_spa));
74 1.1 chuck bcopy(rarp->arp_sha, servea, sizeof(rarp->arp_sha));
75 1.1 chuck return 1;
76 1.1 chuck }
77 1.1 chuck return 0;
78 1.1 chuck }
79 1.1 chuck
80 1.1 chuck /* Try to get a reply to a rev arp request */
81 1.1 chuck
82 1.1 chuck int
83 1.1 chuck rev_arp ()
84 1.1 chuck {
85 1.1 chuck int tries = 0;
86 1.1 chuck while ( tries < 5 ) {
87 1.1 chuck do_rev_arp();
88 1.1 chuck if ( get_rev_arp() ) {
89 1.1 chuck return 1;
90 1.1 chuck }
91 1.1 chuck tries++;
92 1.1 chuck }
93 1.1 chuck return 0;
94 1.1 chuck }
95 1.1 chuck
96 1.1 chuck /* Send a tftp read request or acknowledgement
97 1.1 chuck mesgtype 0 is a read request, 1 is an aknowledgement */
98 1.1 chuck
99 1.1 chuck void
100 1.1 chuck do_send_tftp ( int mesgtype )
101 1.1 chuck {
102 1.1 chuck u_long res, iptmp, lcv;
103 1.1 chuck char *tot;
104 1.1 chuck
105 1.1 chuck if ( mesgtype == 0 ) {
106 1.1 chuck tot = tftp_r + (sizeof(MSG)-1);
107 1.1 chuck myport = (u_short)time();
108 1.1 chuck if (myport < 1000) myport += 1000;
109 1.1 chuck servport = FTP_PORT; /* to start */
110 1.1 chuck } else {
111 1.1 chuck tot = (char *)tftp_a + 4;
112 1.1 chuck }
113 1.1 chuck
114 1.1 chuck bcopy (servea, eh->ether_dhost, sizeof(servea));
115 1.1 chuck bcopy (myea, eh->ether_shost, sizeof(myea));
116 1.1 chuck eh->ether_type = ETYPE_IP;
117 1.1 chuck
118 1.1 chuck iph->ip_v = IP_VERSION;
119 1.1 chuck iph->ip_hl = IP_HLEN;
120 1.1 chuck iph->ip_tos = 0; /* type of service is 0 */
121 1.1 chuck iph->ip_id = 0; /* id field is 0 */
122 1.1 chuck iph->ip_off = IP_DF;
123 1.1 chuck iph->ip_ttl = 3; /* time to live is 3 seconds/hops */
124 1.1 chuck iph->ip_p = IPP_UDP;
125 1.1 chuck bcopy(myip, iph->ip_src, sizeof(myip));
126 1.1 chuck bcopy(servip, iph->ip_dst, sizeof(servip));
127 1.1 chuck iph->ip_sum = 0;
128 1.1 chuck iph->ip_len = tot - (char *)iph;
129 1.1 chuck res = oc_cksum(iph, sizeof(struct ip), 0);
130 1.1 chuck iph->ip_sum = 0xffff & ~res;
131 1.1 chuck udph->uh_sport = myport;
132 1.1 chuck udph->uh_dport = servport;
133 1.1 chuck udph->uh_sum = 0;
134 1.1 chuck
135 1.1 chuck if ( mesgtype ) {
136 1.1 chuck tftp_a->op_code = FTPOP_ACKN;
137 1.1 chuck tftp_a->block = (u_short)(mesgtype);
138 1.1 chuck } else {
139 1.1 chuck bcopy (myip, &iptmp, sizeof(iptmp));
140 1.1 chuck bcopy(MSG, tftp_r, (sizeof(MSG)-1));
141 1.1 chuck for (lcv = 9; lcv >= 2; lcv--) {
142 1.1 chuck tftp_r[lcv] = "0123456789ABCDEF"[iptmp & 0xF];
143 1.1 chuck
144 1.1 chuck iptmp = iptmp >> 4;
145 1.1 chuck }
146 1.1 chuck }
147 1.1 chuck
148 1.1 chuck udph->uh_ulen = tot - (char *)udph;
149 1.1 chuck
150 1.1 chuck le_put( buf, tot - buf);
151 1.1 chuck }
152 1.1 chuck
153 1.1 chuck /* Attempt to tftp a file and read it into memory */
154 1.1 chuck
155 1.1 chuck int
156 1.1 chuck do_get_file ()
157 1.1 chuck {
158 1.1 chuck int fail = 0, oldlen;
159 1.1 chuck char *loadat = (char *)LOAD_ADDR;
160 1.1 chuck last_ack = 0;
161 1.1 chuck
162 1.1 chuck do_send_tftp( READ );
163 1.1 chuck while (1) {
164 1.1 chuck if ( le_get(buf, sizeof(buf), 5) == 0) { /* timeout occured */
165 1.1 chuck if ( last_ack ) {
166 1.1 chuck do_send_tftp( last_ack );
167 1.1 chuck } else {
168 1.1 chuck do_send_tftp( READ );
169 1.1 chuck }
170 1.1 chuck fail++;
171 1.1 chuck if ( fail > 5 ) {
172 1.1 chuck printf("\n");
173 1.1 chuck return 1;
174 1.1 chuck }
175 1.1 chuck } else {
176 1.1 chuck printf("%x \r", tftp->info.block*512);
177 1.1 chuck if ((eh->ether_type != ETYPE_IP) || (iph->ip_p != IPP_UDP)) {
178 1.1 chuck fail++;
179 1.1 chuck continue;
180 1.1 chuck }
181 1.1 chuck if (servport == FTP_PORT) servport = udph->uh_sport;
182 1.1 chuck if (tftp->info.op_code == FTPOP_ERR) {
183 1.1 chuck printf("TFTP: Download error %d: %s\n",
184 1.1 chuck tftp->info.block, tftp->data);
185 1.1 chuck return 1;
186 1.1 chuck }
187 1.3 wiz if (tftp->info.block != last_ack + 1) { /* we received the wrong block */
188 1.1 chuck if (tftp->info.block < last_ack +1) {
189 1.3 wiz do_send_tftp(tftp->info.block); /* ackn whatever we received */
190 1.1 chuck } else {
191 1.1 chuck do_send_tftp( last_ack ); /* ackn the last confirmed block */
192 1.1 chuck }
193 1.1 chuck fail++;
194 1.1 chuck } else { /* we got the right block */
195 1.1 chuck fail = 0;
196 1.1 chuck last_ack++;
197 1.1 chuck oldlen = udph->uh_ulen;
198 1.1 chuck do_send_tftp( last_ack );
199 1.1 chuck /*printf("bcopy %x %x %d\n", &tftp->data, loadat, oldlen - 12);*/
200 1.1 chuck bcopy(&tftp->data, loadat, oldlen - 12);
201 1.1 chuck loadat += oldlen - 12;
202 1.1 chuck if (oldlen < (8 + 4 + 512)) {
203 1.1 chuck printf("\n");
204 1.1 chuck return 0;
205 1.1 chuck }
206 1.1 chuck }
207 1.1 chuck }
208 1.1 chuck }
209 1.1 chuck printf("\n");
210 1.1 chuck return 0;
211 1.1 chuck }
212 1.1 chuck
213 1.1 chuck
214 1.1 chuck
215 1.1 chuck
216