2004-04-22 08:10:48 +08:00
|
|
|
/*
|
|
|
|
* QEMU BOOTP/DHCP server
|
2007-09-17 05:08:06 +08:00
|
|
|
*
|
2004-04-22 08:10:48 +08:00
|
|
|
* Copyright (c) 2004 Fabrice Bellard
|
2007-09-17 05:08:06 +08:00
|
|
|
*
|
2004-04-22 08:10:48 +08:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
#include <slirp.h>
|
|
|
|
|
|
|
|
/* XXX: only DHCP is supported */
|
|
|
|
|
|
|
|
#define LEASE_TIME (24 * 3600)
|
|
|
|
|
|
|
|
static const uint8_t rfc1533_cookie[] = { RFC1533_COOKIE };
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
2009-05-14 01:53:17 +08:00
|
|
|
#define dprintf(fmt, ...) \
|
2009-06-24 20:42:29 +08:00
|
|
|
do if (slirp_debug & DBG_CALL) { fprintf(dfd, fmt, ## __VA_ARGS__); fflush(dfd); } while (0)
|
2004-04-22 08:10:48 +08:00
|
|
|
#else
|
2009-05-14 01:53:17 +08:00
|
|
|
#define dprintf(fmt, ...)
|
2004-04-22 08:10:48 +08:00
|
|
|
#endif
|
|
|
|
|
2009-06-24 20:42:31 +08:00
|
|
|
static BOOTPClient *get_new_addr(Slirp *slirp, struct in_addr *paddr,
|
2009-05-22 04:43:39 +08:00
|
|
|
const uint8_t *macaddr)
|
2004-04-22 08:10:48 +08:00
|
|
|
{
|
|
|
|
BOOTPClient *bc;
|
|
|
|
int i;
|
|
|
|
|
2009-06-24 20:42:31 +08:00
|
|
|
for(i = 0; i < NB_BOOTP_CLIENTS; i++) {
|
|
|
|
bc = &slirp->bootp_clients[i];
|
2009-05-22 04:43:39 +08:00
|
|
|
if (!bc->allocated || !memcmp(macaddr, bc->macaddr, 6))
|
2004-04-22 08:10:48 +08:00
|
|
|
goto found;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
found:
|
2009-06-24 20:42:31 +08:00
|
|
|
bc = &slirp->bootp_clients[i];
|
2004-04-22 08:10:48 +08:00
|
|
|
bc->allocated = 1;
|
2009-06-24 20:42:31 +08:00
|
|
|
paddr->s_addr = slirp->vdhcp_startaddr.s_addr + htonl(i);
|
2004-04-22 08:10:48 +08:00
|
|
|
return bc;
|
|
|
|
}
|
|
|
|
|
2009-06-24 20:42:31 +08:00
|
|
|
static BOOTPClient *request_addr(Slirp *slirp, const struct in_addr *paddr,
|
2009-04-22 03:56:20 +08:00
|
|
|
const uint8_t *macaddr)
|
|
|
|
{
|
|
|
|
uint32_t req_addr = ntohl(paddr->s_addr);
|
2009-06-24 20:42:31 +08:00
|
|
|
uint32_t dhcp_addr = ntohl(slirp->vdhcp_startaddr.s_addr);
|
2009-04-22 03:56:20 +08:00
|
|
|
BOOTPClient *bc;
|
|
|
|
|
2009-06-24 20:42:28 +08:00
|
|
|
if (req_addr >= dhcp_addr &&
|
2009-06-24 20:42:31 +08:00
|
|
|
req_addr < (dhcp_addr + NB_BOOTP_CLIENTS)) {
|
|
|
|
bc = &slirp->bootp_clients[req_addr - dhcp_addr];
|
2009-04-22 03:56:20 +08:00
|
|
|
if (!bc->allocated || !memcmp(macaddr, bc->macaddr, 6)) {
|
|
|
|
bc->allocated = 1;
|
|
|
|
return bc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2009-06-24 20:42:31 +08:00
|
|
|
static BOOTPClient *find_addr(Slirp *slirp, struct in_addr *paddr,
|
|
|
|
const uint8_t *macaddr)
|
2004-05-04 11:14:47 +08:00
|
|
|
{
|
|
|
|
BOOTPClient *bc;
|
|
|
|
int i;
|
|
|
|
|
2009-06-24 20:42:31 +08:00
|
|
|
for(i = 0; i < NB_BOOTP_CLIENTS; i++) {
|
|
|
|
if (!memcmp(macaddr, slirp->bootp_clients[i].macaddr, 6))
|
2004-05-04 11:14:47 +08:00
|
|
|
goto found;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
found:
|
2009-06-24 20:42:31 +08:00
|
|
|
bc = &slirp->bootp_clients[i];
|
2004-05-04 11:14:47 +08:00
|
|
|
bc->allocated = 1;
|
2009-06-24 20:42:31 +08:00
|
|
|
paddr->s_addr = slirp->vdhcp_startaddr.s_addr + htonl(i);
|
2004-05-04 11:14:47 +08:00
|
|
|
return bc;
|
|
|
|
}
|
|
|
|
|
2009-04-22 03:56:20 +08:00
|
|
|
static void dhcp_decode(const struct bootp_t *bp, int *pmsg_type,
|
|
|
|
const struct in_addr **preq_addr)
|
2004-04-22 08:10:48 +08:00
|
|
|
{
|
|
|
|
const uint8_t *p, *p_end;
|
|
|
|
int len, tag;
|
|
|
|
|
2007-09-17 16:09:54 +08:00
|
|
|
*pmsg_type = 0;
|
2009-04-22 03:56:20 +08:00
|
|
|
*preq_addr = NULL;
|
2004-04-22 08:10:48 +08:00
|
|
|
|
2009-04-22 03:56:20 +08:00
|
|
|
p = bp->bp_vend;
|
|
|
|
p_end = p + DHCP_OPT_LEN;
|
2004-04-22 08:10:48 +08:00
|
|
|
if (memcmp(p, rfc1533_cookie, 4) != 0)
|
|
|
|
return;
|
|
|
|
p += 4;
|
|
|
|
while (p < p_end) {
|
|
|
|
tag = p[0];
|
|
|
|
if (tag == RFC1533_PAD) {
|
2007-09-17 05:08:06 +08:00
|
|
|
p++;
|
2004-04-22 08:10:48 +08:00
|
|
|
} else if (tag == RFC1533_END) {
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
p++;
|
|
|
|
if (p >= p_end)
|
|
|
|
break;
|
|
|
|
len = *p++;
|
2009-04-22 03:56:20 +08:00
|
|
|
dprintf("dhcp: tag=%d len=%d\n", tag, len);
|
2004-04-22 08:10:48 +08:00
|
|
|
|
|
|
|
switch(tag) {
|
|
|
|
case RFC2132_MSG_TYPE:
|
|
|
|
if (len >= 1)
|
|
|
|
*pmsg_type = p[0];
|
|
|
|
break;
|
2009-04-22 03:56:20 +08:00
|
|
|
case RFC2132_REQ_ADDR:
|
|
|
|
if (len >= 4)
|
|
|
|
*preq_addr = (struct in_addr *)p;
|
|
|
|
break;
|
2004-04-22 08:10:48 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
p += len;
|
|
|
|
}
|
|
|
|
}
|
2009-04-22 03:56:20 +08:00
|
|
|
if (*pmsg_type == DHCPREQUEST && !*preq_addr && bp->bp_ciaddr.s_addr) {
|
|
|
|
*preq_addr = &bp->bp_ciaddr;
|
|
|
|
}
|
2004-04-22 08:10:48 +08:00
|
|
|
}
|
|
|
|
|
2009-06-24 20:42:31 +08:00
|
|
|
static void bootp_reply(Slirp *slirp, const struct bootp_t *bp)
|
2004-04-22 08:10:48 +08:00
|
|
|
{
|
2009-04-22 03:56:20 +08:00
|
|
|
BOOTPClient *bc = NULL;
|
2004-04-22 08:10:48 +08:00
|
|
|
struct mbuf *m;
|
|
|
|
struct bootp_t *rbp;
|
|
|
|
struct sockaddr_in saddr, daddr;
|
2009-04-22 03:56:20 +08:00
|
|
|
const struct in_addr *preq_addr;
|
2004-04-22 08:10:48 +08:00
|
|
|
int dhcp_msg_type, val;
|
|
|
|
uint8_t *q;
|
|
|
|
|
|
|
|
/* extract exact DHCP msg type */
|
2009-04-22 03:56:20 +08:00
|
|
|
dhcp_decode(bp, &dhcp_msg_type, &preq_addr);
|
|
|
|
dprintf("bootp packet op=%d msgtype=%d", bp->bp_op, dhcp_msg_type);
|
|
|
|
if (preq_addr)
|
|
|
|
dprintf(" req_addr=%08x\n", ntohl(preq_addr->s_addr));
|
|
|
|
else
|
|
|
|
dprintf("\n");
|
2007-09-17 16:09:54 +08:00
|
|
|
|
2004-10-03 19:44:41 +08:00
|
|
|
if (dhcp_msg_type == 0)
|
|
|
|
dhcp_msg_type = DHCPREQUEST; /* Force reply for old BOOTP clients */
|
2007-09-17 16:09:54 +08:00
|
|
|
|
2007-09-17 05:08:06 +08:00
|
|
|
if (dhcp_msg_type != DHCPDISCOVER &&
|
2004-04-22 08:10:48 +08:00
|
|
|
dhcp_msg_type != DHCPREQUEST)
|
|
|
|
return;
|
|
|
|
/* XXX: this is a hack to get the client mac address */
|
2009-06-24 20:42:31 +08:00
|
|
|
memcpy(slirp->client_ethaddr, bp->bp_hwaddr, 6);
|
2007-09-17 16:09:54 +08:00
|
|
|
|
2009-06-24 20:42:31 +08:00
|
|
|
m = m_get(slirp);
|
|
|
|
if (!m) {
|
2004-04-22 08:10:48 +08:00
|
|
|
return;
|
2009-06-24 20:42:31 +08:00
|
|
|
}
|
2007-10-27 03:01:16 +08:00
|
|
|
m->m_data += IF_MAXLINKHDR;
|
2004-04-22 08:10:48 +08:00
|
|
|
rbp = (struct bootp_t *)m->m_data;
|
|
|
|
m->m_data += sizeof(struct udpiphdr);
|
|
|
|
memset(rbp, 0, sizeof(struct bootp_t));
|
|
|
|
|
2004-05-04 11:14:47 +08:00
|
|
|
if (dhcp_msg_type == DHCPDISCOVER) {
|
2009-04-22 03:56:20 +08:00
|
|
|
if (preq_addr) {
|
2009-06-24 20:42:31 +08:00
|
|
|
bc = request_addr(slirp, preq_addr, slirp->client_ethaddr);
|
2009-04-22 03:56:20 +08:00
|
|
|
if (bc) {
|
|
|
|
daddr.sin_addr = *preq_addr;
|
|
|
|
}
|
|
|
|
}
|
2004-05-04 11:14:47 +08:00
|
|
|
if (!bc) {
|
2009-04-22 03:56:20 +08:00
|
|
|
new_addr:
|
2009-06-24 20:42:31 +08:00
|
|
|
bc = get_new_addr(slirp, &daddr.sin_addr, slirp->client_ethaddr);
|
2009-04-22 03:56:20 +08:00
|
|
|
if (!bc) {
|
|
|
|
dprintf("no address left\n");
|
|
|
|
return;
|
|
|
|
}
|
2004-05-04 11:14:47 +08:00
|
|
|
}
|
2009-06-24 20:42:31 +08:00
|
|
|
memcpy(bc->macaddr, slirp->client_ethaddr, 6);
|
2009-04-22 03:56:20 +08:00
|
|
|
} else if (preq_addr) {
|
2009-06-24 20:42:31 +08:00
|
|
|
bc = request_addr(slirp, preq_addr, slirp->client_ethaddr);
|
2009-04-22 03:56:20 +08:00
|
|
|
if (bc) {
|
|
|
|
daddr.sin_addr = *preq_addr;
|
2009-06-24 20:42:31 +08:00
|
|
|
memcpy(bc->macaddr, slirp->client_ethaddr, 6);
|
2009-04-22 03:56:20 +08:00
|
|
|
} else {
|
|
|
|
daddr.sin_addr.s_addr = 0;
|
|
|
|
}
|
2004-05-04 11:14:47 +08:00
|
|
|
} else {
|
2009-06-24 20:42:31 +08:00
|
|
|
bc = find_addr(slirp, &daddr.sin_addr, bp->bp_hwaddr);
|
2004-05-04 11:14:47 +08:00
|
|
|
if (!bc) {
|
2004-10-01 02:57:28 +08:00
|
|
|
/* if never assigned, behaves as if it was already
|
|
|
|
assigned (windows fix because it remembers its address) */
|
|
|
|
goto new_addr;
|
2004-05-04 11:14:47 +08:00
|
|
|
}
|
2004-04-22 08:10:48 +08:00
|
|
|
}
|
2007-02-20 08:05:08 +08:00
|
|
|
|
2009-06-24 20:42:31 +08:00
|
|
|
saddr.sin_addr = slirp->vhost_addr;
|
2004-04-22 08:10:48 +08:00
|
|
|
saddr.sin_port = htons(BOOTP_SERVER);
|
|
|
|
|
|
|
|
daddr.sin_port = htons(BOOTP_CLIENT);
|
|
|
|
|
|
|
|
rbp->bp_op = BOOTP_REPLY;
|
|
|
|
rbp->bp_xid = bp->bp_xid;
|
|
|
|
rbp->bp_htype = 1;
|
|
|
|
rbp->bp_hlen = 6;
|
|
|
|
memcpy(rbp->bp_hwaddr, bp->bp_hwaddr, 6);
|
|
|
|
|
2004-10-01 06:22:08 +08:00
|
|
|
rbp->bp_yiaddr = daddr.sin_addr; /* Client IP address */
|
|
|
|
rbp->bp_siaddr = saddr.sin_addr; /* Server IP address */
|
2004-04-22 08:10:48 +08:00
|
|
|
|
|
|
|
q = rbp->bp_vend;
|
|
|
|
memcpy(q, rfc1533_cookie, 4);
|
|
|
|
q += 4;
|
|
|
|
|
2009-04-22 03:56:20 +08:00
|
|
|
if (bc) {
|
|
|
|
dprintf("%s addr=%08x\n",
|
|
|
|
(dhcp_msg_type == DHCPDISCOVER) ? "offered" : "ack'ed",
|
|
|
|
ntohl(daddr.sin_addr.s_addr));
|
|
|
|
|
|
|
|
if (dhcp_msg_type == DHCPDISCOVER) {
|
|
|
|
*q++ = RFC2132_MSG_TYPE;
|
|
|
|
*q++ = 1;
|
|
|
|
*q++ = DHCPOFFER;
|
|
|
|
} else /* DHCPREQUEST */ {
|
|
|
|
*q++ = RFC2132_MSG_TYPE;
|
|
|
|
*q++ = 1;
|
|
|
|
*q++ = DHCPACK;
|
|
|
|
}
|
|
|
|
|
2009-06-24 20:42:31 +08:00
|
|
|
if (slirp->bootp_filename)
|
2009-04-22 03:56:20 +08:00
|
|
|
snprintf((char *)rbp->bp_file, sizeof(rbp->bp_file), "%s",
|
2009-06-24 20:42:31 +08:00
|
|
|
slirp->bootp_filename);
|
2007-09-17 16:09:54 +08:00
|
|
|
|
2004-04-22 08:10:48 +08:00
|
|
|
*q++ = RFC2132_SRV_ID;
|
|
|
|
*q++ = 4;
|
|
|
|
memcpy(q, &saddr.sin_addr, 4);
|
|
|
|
q += 4;
|
|
|
|
|
|
|
|
*q++ = RFC1533_NETMASK;
|
|
|
|
*q++ = 4;
|
2009-06-24 20:42:31 +08:00
|
|
|
memcpy(q, &slirp->vnetwork_mask, 4);
|
2009-06-24 20:42:28 +08:00
|
|
|
q += 4;
|
2007-09-17 16:09:54 +08:00
|
|
|
|
2009-06-24 20:42:31 +08:00
|
|
|
if (!slirp->restricted) {
|
2009-01-09 03:24:00 +08:00
|
|
|
*q++ = RFC1533_GATEWAY;
|
|
|
|
*q++ = 4;
|
|
|
|
memcpy(q, &saddr.sin_addr, 4);
|
|
|
|
q += 4;
|
|
|
|
|
|
|
|
*q++ = RFC1533_DNS;
|
|
|
|
*q++ = 4;
|
2009-06-24 20:42:31 +08:00
|
|
|
memcpy(q, &slirp->vnameserver_addr, 4);
|
2009-01-09 03:24:00 +08:00
|
|
|
q += 4;
|
|
|
|
}
|
2004-04-22 08:10:48 +08:00
|
|
|
|
|
|
|
*q++ = RFC2132_LEASE_TIME;
|
|
|
|
*q++ = 4;
|
|
|
|
val = htonl(LEASE_TIME);
|
|
|
|
memcpy(q, &val, 4);
|
|
|
|
q += 4;
|
2006-04-16 19:06:58 +08:00
|
|
|
|
2009-06-24 20:42:31 +08:00
|
|
|
if (*slirp->client_hostname) {
|
|
|
|
val = strlen(slirp->client_hostname);
|
2006-04-16 19:06:58 +08:00
|
|
|
*q++ = RFC1533_HOSTNAME;
|
|
|
|
*q++ = val;
|
2009-06-24 20:42:31 +08:00
|
|
|
memcpy(q, slirp->client_hostname, val);
|
2006-04-16 19:06:58 +08:00
|
|
|
q += val;
|
|
|
|
}
|
2009-04-22 03:56:20 +08:00
|
|
|
} else {
|
|
|
|
static const char nak_msg[] = "requested address not available";
|
|
|
|
|
|
|
|
dprintf("nak'ed addr=%08x\n", ntohl(preq_addr->s_addr));
|
|
|
|
|
|
|
|
*q++ = RFC2132_MSG_TYPE;
|
|
|
|
*q++ = 1;
|
|
|
|
*q++ = DHCPNAK;
|
|
|
|
|
|
|
|
*q++ = RFC2132_MESSAGE;
|
|
|
|
*q++ = sizeof(nak_msg) - 1;
|
|
|
|
memcpy(q, nak_msg, sizeof(nak_msg) - 1);
|
|
|
|
q += sizeof(nak_msg) - 1;
|
2004-04-22 08:10:48 +08:00
|
|
|
}
|
|
|
|
*q++ = RFC1533_END;
|
2007-09-17 16:09:54 +08:00
|
|
|
|
2009-04-22 03:56:20 +08:00
|
|
|
daddr.sin_addr.s_addr = 0xffffffffu;
|
|
|
|
|
2007-09-17 05:08:06 +08:00
|
|
|
m->m_len = sizeof(struct bootp_t) -
|
2004-06-04 23:30:48 +08:00
|
|
|
sizeof(struct ip) - sizeof(struct udphdr);
|
2004-04-22 08:10:48 +08:00
|
|
|
udp_output2(NULL, m, &saddr, &daddr, IPTOS_LOWDELAY);
|
|
|
|
}
|
|
|
|
|
|
|
|
void bootp_input(struct mbuf *m)
|
|
|
|
{
|
2005-06-06 01:11:42 +08:00
|
|
|
struct bootp_t *bp = mtod(m, struct bootp_t *);
|
2004-04-22 08:10:48 +08:00
|
|
|
|
|
|
|
if (bp->bp_op == BOOTP_REQUEST) {
|
2009-06-24 20:42:31 +08:00
|
|
|
bootp_reply(m->slirp, bp);
|
2004-04-22 08:10:48 +08:00
|
|
|
}
|
|
|
|
}
|