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