bootp.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /* $OpenBSD: bootp.c,v 1.15 2014/11/19 19:58:40 miod Exp $ */
  2. /* $NetBSD: bootp.c,v 1.10 1996/10/13 02:28:59 christos Exp $ */
  3. /*
  4. * Copyright (c) 1992 Regents of the University of California.
  5. * All rights reserved.
  6. *
  7. * This software was developed by the Computer Systems Engineering group
  8. * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
  9. * contributed to Berkeley.
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions
  13. * are met:
  14. * 1. Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. * 3. All advertising materials mentioning features or use of this software
  20. * must display the following acknowledgement:
  21. * This product includes software developed by the University of
  22. * California, Lawrence Berkeley Laboratory and its contributors.
  23. * 4. Neither the name of the University nor the names of its contributors
  24. * may be used to endorse or promote products derived from this software
  25. * without specific prior written permission.
  26. *
  27. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  28. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  29. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  30. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  31. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  32. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  33. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  34. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  35. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  36. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  37. * SUCH DAMAGE.
  38. *
  39. * @(#) Header: bootp.c,v 1.4 93/09/11 03:13:51 leres Exp (LBL)
  40. */
  41. #include <sys/types.h>
  42. #include <sys/socket.h>
  43. #include <netinet/in.h>
  44. #include "stand.h"
  45. #include "net.h"
  46. #include "netif.h"
  47. #include "bootp.h"
  48. static u_int32_t nmask, smask;
  49. static time_t bot;
  50. static char vm_rfc1048[4] = VM_RFC1048;
  51. static char vm_cmu[4] = VM_CMU;
  52. /* Local forwards */
  53. static ssize_t bootpsend(struct iodesc *, void *, size_t);
  54. static ssize_t bootprecv(struct iodesc *, void *, size_t, time_t);
  55. static void vend_cmu(const u_char *);
  56. static void vend_rfc1048(const u_char *, u_int);
  57. /* Fetch required bootp information */
  58. void
  59. bootp(int sock)
  60. {
  61. struct iodesc *d;
  62. struct bootp *bp;
  63. struct {
  64. struct packet_header header;
  65. struct bootp wbootp;
  66. } wbuf;
  67. struct {
  68. struct packet_header header;
  69. struct bootp rbootp;
  70. } rbuf;
  71. #ifdef BOOTP_DEBUG
  72. if (debug)
  73. printf("bootp: socket=%d\n", sock);
  74. #endif
  75. if (!bot)
  76. bot = getsecs();
  77. if (!(d = socktodesc(sock))) {
  78. printf("bootp: bad socket. %d\n", sock);
  79. return;
  80. }
  81. #ifdef BOOTP_DEBUG
  82. if (debug)
  83. printf("bootp: d=%x\n", (u_int)d);
  84. #endif
  85. bp = &wbuf.wbootp;
  86. bzero(bp, sizeof(*bp));
  87. bp->bp_op = BOOTREQUEST;
  88. bp->bp_htype = HTYPE_ETHERNET; /* 10Mb Ethernet (48 bits) */
  89. bp->bp_hlen = 6;
  90. bp->bp_xid = htonl(d->xid);
  91. MACPY(d->myea, bp->bp_chaddr);
  92. bzero(bp->bp_file, sizeof(bp->bp_file));
  93. bcopy(vm_rfc1048, bp->bp_vend, sizeof(vm_rfc1048));
  94. d->myip = myip;
  95. d->myport = htons(IPPORT_BOOTPC);
  96. d->destip.s_addr = INADDR_BROADCAST;
  97. d->destport = htons(IPPORT_BOOTPS);
  98. (void)sendrecv(d,
  99. bootpsend, bp, sizeof(*bp),
  100. bootprecv, &rbuf.rbootp, sizeof(rbuf.rbootp));
  101. /* Bump xid so next request will be unique. */
  102. ++d->xid;
  103. }
  104. /* Transmit a bootp request */
  105. static ssize_t
  106. bootpsend(struct iodesc *d, void *pkt, size_t len)
  107. {
  108. struct bootp *bp;
  109. #ifdef BOOTP_DEBUG
  110. if (debug)
  111. printf("bootpsend: d=%x called.\n", (u_int)d);
  112. #endif
  113. bp = pkt;
  114. bp->bp_secs = htons((u_short)(getsecs() - bot));
  115. #ifdef BOOTP_DEBUG
  116. if (debug)
  117. printf("bootpsend: calling sendudp\n");
  118. #endif
  119. return (sendudp(d, pkt, len));
  120. }
  121. /* Returns 0 if this is the packet we're waiting for else -1 (and errno == 0) */
  122. static ssize_t
  123. bootprecv(struct iodesc *d, void *pkt, size_t len, time_t tleft)
  124. {
  125. ssize_t n;
  126. struct bootp *bp;
  127. #ifdef BOOTP_DEBUG
  128. if (debug)
  129. printf("bootprecv: called\n");
  130. #endif
  131. n = readudp(d, pkt, len, tleft);
  132. if (n < 0 || (size_t)n < sizeof(struct bootp))
  133. goto bad;
  134. bp = (struct bootp *)pkt;
  135. #ifdef BOOTP_DEBUG
  136. if (debug)
  137. printf("bootprecv: checked. bp = 0x%x, n = %d\n",
  138. (unsigned)bp, n);
  139. #endif
  140. if (bp->bp_xid != htonl(d->xid)) {
  141. #ifdef BOOTP_DEBUG
  142. if (debug) {
  143. printf("bootprecv: expected xid 0x%lx, got 0x%lx\n",
  144. d->xid, ntohl(bp->bp_xid));
  145. }
  146. #endif
  147. goto bad;
  148. }
  149. #ifdef BOOTP_DEBUG
  150. if (debug)
  151. printf("bootprecv: got one!\n");
  152. #endif
  153. /* Pick up our ip address (and natural netmask) */
  154. myip = d->myip = bp->bp_yiaddr;
  155. #ifdef BOOTP_DEBUG
  156. if (debug)
  157. printf("our ip address is %s\n", inet_ntoa(d->myip));
  158. #endif
  159. if (IN_CLASSA(d->myip.s_addr))
  160. nmask = IN_CLASSA_NET;
  161. else if (IN_CLASSB(d->myip.s_addr))
  162. nmask = IN_CLASSB_NET;
  163. else
  164. nmask = IN_CLASSC_NET;
  165. #ifdef BOOTP_DEBUG
  166. if (debug)
  167. printf("'native netmask' is %s\n", intoa(nmask));
  168. #endif
  169. /* Pick up root or swap server address and file spec. */
  170. if (bp->bp_siaddr.s_addr != 0)
  171. rootip = bp->bp_siaddr;
  172. if (bp->bp_file[0] != '\0') {
  173. strncpy(bootfile, (char *)bp->bp_file, sizeof(bootfile));
  174. bootfile[sizeof(bootfile) - 1] = '\0';
  175. }
  176. /* Suck out vendor info */
  177. if (bcmp(vm_cmu, bp->bp_vend, sizeof(vm_cmu)) == 0)
  178. vend_cmu(bp->bp_vend);
  179. else if (bcmp(vm_rfc1048, bp->bp_vend, sizeof(vm_rfc1048)) == 0)
  180. vend_rfc1048(bp->bp_vend, sizeof(bp->bp_vend));
  181. else
  182. printf("bootprecv: unknown vendor 0x%lx\n", (long)bp->bp_vend);
  183. /* Check subnet mask against net mask; toss if bogus */
  184. if ((nmask & smask) != nmask) {
  185. #ifdef BOOTP_DEBUG
  186. if (debug)
  187. printf("subnet mask (%s) bad\n", intoa(smask));
  188. #endif
  189. smask = 0;
  190. }
  191. /* Get subnet (or natural net) mask */
  192. netmask = nmask;
  193. if (smask)
  194. netmask = smask;
  195. #ifdef BOOTP_DEBUG
  196. if (debug)
  197. printf("mask: %s\n", intoa(netmask));
  198. #endif
  199. /* We need a gateway if root or swap is on a different net */
  200. if (!SAMENET(d->myip, rootip, netmask)) {
  201. #ifdef BOOTP_DEBUG
  202. if (debug)
  203. printf("need gateway for root ip\n");
  204. #endif
  205. }
  206. if (!SAMENET(d->myip, swapip, netmask)) {
  207. #ifdef BOOTP_DEBUG
  208. if (debug)
  209. printf("need gateway for swap ip\n");
  210. #endif
  211. }
  212. /* Toss gateway if on a different net */
  213. if (!SAMENET(d->myip, gateip, netmask)) {
  214. #ifdef BOOTP_DEBUG
  215. if (debug)
  216. printf("gateway ip (%s) bad\n", inet_ntoa(gateip));
  217. #endif
  218. gateip.s_addr = 0;
  219. }
  220. return (n);
  221. bad:
  222. errno = 0;
  223. return (-1);
  224. }
  225. static void
  226. vend_cmu(const u_char *cp)
  227. {
  228. const struct cmu_vend *vp;
  229. #ifdef BOOTP_DEBUG
  230. if (debug)
  231. printf("vend_cmu bootp info.\n");
  232. #endif
  233. vp = (const struct cmu_vend *)cp;
  234. if (vp->v_smask.s_addr != 0)
  235. smask = vp->v_smask.s_addr;
  236. if (vp->v_dgate.s_addr != 0)
  237. gateip = vp->v_dgate;
  238. }
  239. static void
  240. vend_rfc1048(const u_char *cp, u_int len)
  241. {
  242. const u_char *ep;
  243. int size;
  244. u_char tag;
  245. #ifdef BOOTP_DEBUG
  246. if (debug)
  247. printf("vend_rfc1048 bootp info. len=%d\n", len);
  248. #endif
  249. ep = cp + len;
  250. /* Step over magic cookie */
  251. cp += sizeof(int);
  252. while (cp < ep) {
  253. tag = *cp++;
  254. size = *cp++;
  255. if (tag == TAG_END)
  256. break;
  257. if (tag == TAG_SUBNET_MASK)
  258. bcopy(cp, &smask, sizeof(smask));
  259. if (tag == TAG_GATEWAY)
  260. bcopy(cp, &gateip.s_addr, sizeof(gateip.s_addr));
  261. if (tag == TAG_SWAPSERVER)
  262. bcopy(cp, &swapip.s_addr, sizeof(swapip.s_addr));
  263. if (tag == TAG_DOMAIN_SERVER)
  264. bcopy(cp, &nameip.s_addr, sizeof(nameip.s_addr));
  265. if (tag == TAG_ROOTPATH) {
  266. strncpy(rootpath, (char *)cp, sizeof(rootpath));
  267. rootpath[size] = '\0';
  268. }
  269. if (tag == TAG_HOSTNAME) {
  270. strncpy(hostname, (char *)cp, sizeof(hostname));
  271. hostname[size] = '\0';
  272. }
  273. if (tag == TAG_DOMAINNAME) {
  274. strncpy(domainname, (char *)cp, sizeof(domainname));
  275. domainname[size] = '\0';
  276. }
  277. cp += size;
  278. }
  279. }