1 /* $NetBSD: etherfun.c,v 1.10 2011/02/02 17:53:41 chuck 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 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 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 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 94 void 95 do_send_tftp(int mesgtype) 96 { 97 u_long res, iptmp, lcv; 98 u_char *tot; 99 100 if (mesgtype == 0) { 101 tot = tftp_r + (sizeof(MSG) - 1); 102 myport = (u_short)time(); 103 if (myport < 1000) 104 myport += 1000; 105 servport = FTP_PORT; /* to start */ 106 } else { 107 tot = (u_char *)tftp_a + 4; 108 } 109 110 memcpy (eh->ether_dhost, servea, sizeof(servea)); 111 memcpy (eh->ether_shost, myea, sizeof(myea)); 112 eh->ether_type = ETYPE_IP; 113 114 iph->ip_v = IP_VERSION; 115 iph->ip_hl = IP_HLEN; 116 iph->ip_tos = 0; /* type of service is 0 */ 117 iph->ip_id = 0; /* id field is 0 */ 118 iph->ip_off = IP_DF; 119 iph->ip_ttl = 3; /* time to live is 3 seconds/hops */ 120 iph->ip_p = IPP_UDP; 121 memcpy(iph->ip_src, myip, sizeof(myip)); 122 memcpy(iph->ip_dst, servip, sizeof(servip)); 123 iph->ip_sum = 0; 124 iph->ip_len = tot - (u_char *)iph; 125 res = oc_cksum(iph, sizeof(struct ip), 0); 126 iph->ip_sum = 0xffff & ~res; 127 udph->uh_sport = myport; 128 udph->uh_dport = servport; 129 udph->uh_sum = 0; 130 131 if (mesgtype) { 132 tftp_a->op_code = FTPOP_ACKN; 133 tftp_a->block = (u_short)(mesgtype); 134 } else { 135 memcpy (&iptmp, myip, sizeof(iptmp)); 136 memcpy(tftp_r, MSG, (sizeof(MSG)-1)); 137 for (lcv = 9; lcv >= 2; lcv--) { 138 tftp_r[lcv] = HEXDIGITS[iptmp & 0xF]; 139 140 iptmp = iptmp >> 4; 141 } 142 } 143 144 udph->uh_ulen = tot - (u_char *)udph; 145 146 le_put(buf, tot - buf); 147 } 148 149 /* Attempt to tftp a file and read it into memory */ 150 151 int 152 do_get_file(void) 153 { 154 int fail = 0, oldlen; 155 char *loadat = (char *)LOAD_ADDR; 156 last_ack = 0; 157 158 do_send_tftp(READ); 159 for (;;) { 160 if (le_get(buf, sizeof(buf), 5) == 0) { /* timeout occurred */ 161 if (last_ack) { 162 do_send_tftp(last_ack); 163 } else { 164 do_send_tftp(READ); 165 } 166 fail++; 167 if (fail > 5) { 168 printf("\n"); 169 return 1; 170 } 171 } else { 172 printf("%x \r", tftp->info.block*512); 173 if ((eh->ether_type != ETYPE_IP) || 174 (iph->ip_p != IPP_UDP)) { 175 fail++; 176 continue; 177 } 178 if (servport == FTP_PORT) servport = udph->uh_sport; 179 if (tftp->info.op_code == FTPOP_ERR) { 180 printf("TFTP: Download error %d: %s\n", 181 tftp->info.block, tftp->data); 182 return 1; 183 } 184 if (tftp->info.block != last_ack + 1) { 185 /* we received the wrong block */ 186 if (tftp->info.block < last_ack +1) { 187 /* ackn whatever we received */ 188 do_send_tftp(tftp->info.block); 189 } else { 190 /* ackn the last confirmed block */ 191 do_send_tftp(last_ack); 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 #if 0 200 printf("memcpy %x %x %d\n", loadat, 201 &tftp->data, oldlen - 12); 202 #endif 203 memcpy(loadat, &tftp->data, oldlen - 12); 204 loadat += oldlen - 12; 205 if (oldlen < (8 + 4 + 512)) { 206 printf("\n"); 207 return 0; 208 } 209 } 210 } 211 } 212 printf("\n"); 213 return 0; 214 } 215