|
|
1.1 root 1: /*
2: * QEMU BOOTP/DHCP server
1.1.1.3 root 3: *
1.1 root 4: * Copyright (c) 2004 Fabrice Bellard
1.1.1.3 root 5: *
1.1 root 6: * Permission is hereby granted, free of charge, to any person obtaining a copy
7: * of this software and associated documentation files (the "Software"), to deal
8: * in the Software without restriction, including without limitation the rights
9: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10: * copies of the Software, and to permit persons to whom the Software is
11: * furnished to do so, subject to the following conditions:
12: *
13: * The above copyright notice and this permission notice shall be included in
14: * all copies or substantial portions of the Software.
15: *
16: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19: * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22: * THE SOFTWARE.
23: */
24: #include <slirp.h>
25:
26: /* XXX: only DHCP is supported */
27:
28: #define LEASE_TIME (24 * 3600)
29:
30: static const uint8_t rfc1533_cookie[] = { RFC1533_COOKIE };
31:
32: #ifdef DEBUG
1.1.1.6 root 33: #define DPRINTF(fmt, ...) \
1.1.1.5 root 34: do if (slirp_debug & DBG_CALL) { fprintf(dfd, fmt, ## __VA_ARGS__); fflush(dfd); } while (0)
1.1 root 35: #else
1.1.1.7 ! root 36: #define DPRINTF(fmt, ...) do{}while(0)
1.1 root 37: #endif
38:
1.1.1.5 root 39: static BOOTPClient *get_new_addr(Slirp *slirp, struct in_addr *paddr,
40: const uint8_t *macaddr)
1.1 root 41: {
42: BOOTPClient *bc;
43: int i;
44:
1.1.1.5 root 45: for(i = 0; i < NB_BOOTP_CLIENTS; i++) {
46: bc = &slirp->bootp_clients[i];
47: if (!bc->allocated || !memcmp(macaddr, bc->macaddr, 6))
1.1 root 48: goto found;
49: }
50: return NULL;
51: found:
1.1.1.5 root 52: bc = &slirp->bootp_clients[i];
1.1 root 53: bc->allocated = 1;
1.1.1.5 root 54: paddr->s_addr = slirp->vdhcp_startaddr.s_addr + htonl(i);
1.1 root 55: return bc;
56: }
57:
1.1.1.5 root 58: static BOOTPClient *request_addr(Slirp *slirp, const struct in_addr *paddr,
59: const uint8_t *macaddr)
60: {
61: uint32_t req_addr = ntohl(paddr->s_addr);
62: uint32_t dhcp_addr = ntohl(slirp->vdhcp_startaddr.s_addr);
63: BOOTPClient *bc;
64:
65: if (req_addr >= dhcp_addr &&
66: req_addr < (dhcp_addr + NB_BOOTP_CLIENTS)) {
67: bc = &slirp->bootp_clients[req_addr - dhcp_addr];
68: if (!bc->allocated || !memcmp(macaddr, bc->macaddr, 6)) {
69: bc->allocated = 1;
70: return bc;
71: }
72: }
73: return NULL;
74: }
75:
76: static BOOTPClient *find_addr(Slirp *slirp, struct in_addr *paddr,
77: const uint8_t *macaddr)
1.1 root 78: {
79: BOOTPClient *bc;
80: int i;
81:
1.1.1.5 root 82: for(i = 0; i < NB_BOOTP_CLIENTS; i++) {
83: if (!memcmp(macaddr, slirp->bootp_clients[i].macaddr, 6))
1.1 root 84: goto found;
85: }
86: return NULL;
87: found:
1.1.1.5 root 88: bc = &slirp->bootp_clients[i];
1.1 root 89: bc->allocated = 1;
1.1.1.5 root 90: paddr->s_addr = slirp->vdhcp_startaddr.s_addr + htonl(i);
1.1 root 91: return bc;
92: }
93:
1.1.1.5 root 94: static void dhcp_decode(const struct bootp_t *bp, int *pmsg_type,
1.1.1.7 ! root 95: struct in_addr *preq_addr)
1.1 root 96: {
97: const uint8_t *p, *p_end;
98: int len, tag;
99:
1.1.1.3 root 100: *pmsg_type = 0;
1.1.1.7 ! root 101: preq_addr->s_addr = htonl(0L);
1.1 root 102:
1.1.1.5 root 103: p = bp->bp_vend;
104: p_end = p + DHCP_OPT_LEN;
1.1 root 105: if (memcmp(p, rfc1533_cookie, 4) != 0)
106: return;
107: p += 4;
108: while (p < p_end) {
109: tag = p[0];
110: if (tag == RFC1533_PAD) {
1.1.1.3 root 111: p++;
1.1 root 112: } else if (tag == RFC1533_END) {
113: break;
114: } else {
115: p++;
116: if (p >= p_end)
117: break;
118: len = *p++;
1.1.1.6 root 119: DPRINTF("dhcp: tag=%d len=%d\n", tag, len);
1.1 root 120:
121: switch(tag) {
122: case RFC2132_MSG_TYPE:
123: if (len >= 1)
124: *pmsg_type = p[0];
125: break;
1.1.1.5 root 126: case RFC2132_REQ_ADDR:
1.1.1.7 ! root 127: if (len >= 4) {
! 128: memcpy(&(preq_addr->s_addr), p, 4);
! 129: }
1.1.1.5 root 130: break;
1.1 root 131: default:
132: break;
133: }
134: p += len;
135: }
136: }
1.1.1.7 ! root 137: if (*pmsg_type == DHCPREQUEST && preq_addr->s_addr == htonl(0L) &&
! 138: bp->bp_ciaddr.s_addr) {
! 139: memcpy(&(preq_addr->s_addr), &bp->bp_ciaddr, 4);
1.1.1.5 root 140: }
1.1 root 141: }
142:
1.1.1.5 root 143: static void bootp_reply(Slirp *slirp, const struct bootp_t *bp)
1.1 root 144: {
1.1.1.5 root 145: BOOTPClient *bc = NULL;
1.1 root 146: struct mbuf *m;
147: struct bootp_t *rbp;
148: struct sockaddr_in saddr, daddr;
1.1.1.7 ! root 149: struct in_addr preq_addr;
1.1 root 150: int dhcp_msg_type, val;
151: uint8_t *q;
152:
153: /* extract exact DHCP msg type */
1.1.1.5 root 154: dhcp_decode(bp, &dhcp_msg_type, &preq_addr);
1.1.1.6 root 155: DPRINTF("bootp packet op=%d msgtype=%d", bp->bp_op, dhcp_msg_type);
1.1.1.7 ! root 156: if (preq_addr.s_addr != htonl(0L))
! 157: DPRINTF(" req_addr=%08x\n", ntohl(preq_addr.s_addr));
1.1.1.5 root 158: else
1.1.1.6 root 159: DPRINTF("\n");
1.1.1.3 root 160:
1.1 root 161: if (dhcp_msg_type == 0)
162: dhcp_msg_type = DHCPREQUEST; /* Force reply for old BOOTP clients */
1.1.1.3 root 163:
164: if (dhcp_msg_type != DHCPDISCOVER &&
1.1 root 165: dhcp_msg_type != DHCPREQUEST)
166: return;
167: /* XXX: this is a hack to get the client mac address */
1.1.1.5 root 168: memcpy(slirp->client_ethaddr, bp->bp_hwaddr, 6);
1.1.1.3 root 169:
1.1.1.5 root 170: m = m_get(slirp);
171: if (!m) {
1.1 root 172: return;
1.1.1.5 root 173: }
1.1.1.3 root 174: m->m_data += IF_MAXLINKHDR;
1.1 root 175: rbp = (struct bootp_t *)m->m_data;
176: m->m_data += sizeof(struct udpiphdr);
177: memset(rbp, 0, sizeof(struct bootp_t));
178:
179: if (dhcp_msg_type == DHCPDISCOVER) {
1.1.1.7 ! root 180: if (preq_addr.s_addr != htonl(0L)) {
! 181: bc = request_addr(slirp, &preq_addr, slirp->client_ethaddr);
1.1.1.5 root 182: if (bc) {
1.1.1.7 ! root 183: daddr.sin_addr = preq_addr;
1.1.1.5 root 184: }
185: }
1.1 root 186: if (!bc) {
1.1.1.5 root 187: new_addr:
188: bc = get_new_addr(slirp, &daddr.sin_addr, slirp->client_ethaddr);
189: if (!bc) {
1.1.1.6 root 190: DPRINTF("no address left\n");
1.1.1.5 root 191: return;
192: }
193: }
194: memcpy(bc->macaddr, slirp->client_ethaddr, 6);
1.1.1.7 ! root 195: } else if (preq_addr.s_addr != htonl(0L)) {
! 196: bc = request_addr(slirp, &preq_addr, slirp->client_ethaddr);
1.1.1.5 root 197: if (bc) {
1.1.1.7 ! root 198: daddr.sin_addr = preq_addr;
1.1.1.5 root 199: memcpy(bc->macaddr, slirp->client_ethaddr, 6);
200: } else {
201: daddr.sin_addr.s_addr = 0;
1.1 root 202: }
203: } else {
1.1.1.5 root 204: bc = find_addr(slirp, &daddr.sin_addr, bp->bp_hwaddr);
1.1 root 205: if (!bc) {
206: /* if never assigned, behaves as if it was already
207: assigned (windows fix because it remembers its address) */
208: goto new_addr;
209: }
210: }
1.1.1.3 root 211:
1.1.1.5 root 212: saddr.sin_addr = slirp->vhost_addr;
1.1 root 213: saddr.sin_port = htons(BOOTP_SERVER);
214:
215: daddr.sin_port = htons(BOOTP_CLIENT);
216:
217: rbp->bp_op = BOOTP_REPLY;
218: rbp->bp_xid = bp->bp_xid;
219: rbp->bp_htype = 1;
220: rbp->bp_hlen = 6;
221: memcpy(rbp->bp_hwaddr, bp->bp_hwaddr, 6);
222:
223: rbp->bp_yiaddr = daddr.sin_addr; /* Client IP address */
224: rbp->bp_siaddr = saddr.sin_addr; /* Server IP address */
225:
226: q = rbp->bp_vend;
227: memcpy(q, rfc1533_cookie, 4);
228: q += 4;
229:
1.1.1.5 root 230: if (bc) {
1.1.1.6 root 231: DPRINTF("%s addr=%08x\n",
1.1.1.5 root 232: (dhcp_msg_type == DHCPDISCOVER) ? "offered" : "ack'ed",
233: ntohl(daddr.sin_addr.s_addr));
234:
235: if (dhcp_msg_type == DHCPDISCOVER) {
236: *q++ = RFC2132_MSG_TYPE;
237: *q++ = 1;
238: *q++ = DHCPOFFER;
239: } else /* DHCPREQUEST */ {
240: *q++ = RFC2132_MSG_TYPE;
241: *q++ = 1;
242: *q++ = DHCPACK;
243: }
244:
245: if (slirp->bootp_filename)
246: snprintf((char *)rbp->bp_file, sizeof(rbp->bp_file), "%s",
247: slirp->bootp_filename);
1.1.1.3 root 248:
1.1 root 249: *q++ = RFC2132_SRV_ID;
250: *q++ = 4;
251: memcpy(q, &saddr.sin_addr, 4);
252: q += 4;
253:
254: *q++ = RFC1533_NETMASK;
255: *q++ = 4;
1.1.1.5 root 256: memcpy(q, &slirp->vnetwork_mask, 4);
257: q += 4;
1.1.1.3 root 258:
1.1.1.5 root 259: if (!slirp->restricted) {
1.1.1.4 root 260: *q++ = RFC1533_GATEWAY;
261: *q++ = 4;
262: memcpy(q, &saddr.sin_addr, 4);
263: q += 4;
264:
265: *q++ = RFC1533_DNS;
266: *q++ = 4;
1.1.1.5 root 267: memcpy(q, &slirp->vnameserver_addr, 4);
1.1.1.4 root 268: q += 4;
269: }
1.1 root 270:
271: *q++ = RFC2132_LEASE_TIME;
272: *q++ = 4;
273: val = htonl(LEASE_TIME);
274: memcpy(q, &val, 4);
275: q += 4;
1.1.1.2 root 276:
1.1.1.5 root 277: if (*slirp->client_hostname) {
278: val = strlen(slirp->client_hostname);
1.1.1.2 root 279: *q++ = RFC1533_HOSTNAME;
280: *q++ = val;
1.1.1.5 root 281: memcpy(q, slirp->client_hostname, val);
1.1.1.2 root 282: q += val;
283: }
1.1.1.5 root 284: } else {
285: static const char nak_msg[] = "requested address not available";
286:
1.1.1.6 root 287: DPRINTF("nak'ed addr=%08x\n", ntohl(preq_addr->s_addr));
1.1.1.5 root 288:
289: *q++ = RFC2132_MSG_TYPE;
290: *q++ = 1;
291: *q++ = DHCPNAK;
292:
293: *q++ = RFC2132_MESSAGE;
294: *q++ = sizeof(nak_msg) - 1;
295: memcpy(q, nak_msg, sizeof(nak_msg) - 1);
296: q += sizeof(nak_msg) - 1;
1.1 root 297: }
1.1.1.6 root 298: *q = RFC1533_END;
1.1.1.3 root 299:
1.1.1.5 root 300: daddr.sin_addr.s_addr = 0xffffffffu;
301:
1.1.1.3 root 302: m->m_len = sizeof(struct bootp_t) -
1.1 root 303: sizeof(struct ip) - sizeof(struct udphdr);
304: udp_output2(NULL, m, &saddr, &daddr, IPTOS_LOWDELAY);
305: }
306:
307: void bootp_input(struct mbuf *m)
308: {
309: struct bootp_t *bp = mtod(m, struct bootp_t *);
310:
311: if (bp->bp_op == BOOTP_REQUEST) {
1.1.1.5 root 312: bootp_reply(m->slirp, bp);
1.1 root 313: }
314: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.