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